1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
|
// Copyright (c) 2011 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "base/utf_string_conversions.h"
#include "ui/base/keycodes/keyboard_codes.h"
#include "ui/base/models/combobox_model.h"
#include "ui/views/ime/mock_input_method.h"
#include "ui/views/test/views_test_base.h"
#include "views/controls/combobox/combobox.h"
#include "views/controls/combobox/native_combobox_views.h"
#include "views/widget/native_widget_private.h"
#include "views/widget/widget.h"
namespace {
// A wrapper of Combobox to intercept the result of OnKeyPressed() and
// OnKeyReleased() methods.
class TestCombobox : public views::Combobox {
public:
TestCombobox(ui::ComboboxModel* model)
: Combobox(model),
key_handled_(false),
key_received_(false) {
}
virtual bool OnKeyPressed(const views::KeyEvent& e) OVERRIDE {
key_received_ = true;
key_handled_ = views::Combobox::OnKeyPressed(e);
return key_handled_;
}
virtual bool OnKeyReleased(const views::KeyEvent& e) OVERRIDE {
key_received_ = true;
key_handled_ = views::Combobox::OnKeyReleased(e);
return key_handled_;
}
bool key_handled() const { return key_handled_; }
bool key_received() const { return key_received_; }
void clear() {
key_received_ = key_handled_ = false;
}
private:
bool key_handled_;
bool key_received_;
DISALLOW_COPY_AND_ASSIGN(TestCombobox);
};
// A concrete class is needed to test the combobox
class TestComboboxModel : public ui::ComboboxModel {
public:
TestComboboxModel() {}
virtual ~TestComboboxModel() {}
virtual int GetItemCount() {
return 4;
}
virtual string16 GetItemAt(int index) {
EXPECT_GE(index, 0);
EXPECT_LT(index, GetItemCount());
return string16();
}
private:
DISALLOW_COPY_AND_ASSIGN(TestComboboxModel);
};
} // namespace
namespace views {
class NativeComboboxViewsTest : public ViewsTestBase {
public:
NativeComboboxViewsTest()
: widget_(NULL),
combobox_(NULL),
combobox_view_(NULL),
model_(NULL),
input_method_(NULL) {
}
// ::testing::Test:
virtual void SetUp() {
ViewsTestBase::SetUp();
Widget::SetPureViews(true);
}
virtual void TearDown() {
Widget::SetPureViews(false);
if (widget_)
widget_->Close();
ViewsTestBase::TearDown();
}
void InitCombobox() {
model_.reset(new TestComboboxModel());
ASSERT_FALSE(combobox_);
combobox_ = new TestCombobox(model_.get());
combobox_->set_id(1);
widget_ = new Widget;
Widget::InitParams params(Widget::InitParams::TYPE_POPUP);
params.bounds = gfx::Rect(100, 100, 100, 100);
widget_->Init(params);
View* container = new View();
widget_->SetContentsView(container);
container->AddChildView(combobox_);
combobox_view_ = static_cast<NativeComboboxViews*>(
combobox_->GetNativeWrapperForTesting());
ASSERT_TRUE(combobox_view_);
input_method_ = new MockInputMethod();
widget_->ReplaceInputMethod(input_method_);
// Assumes the Widget is always focused.
input_method_->OnFocus();
combobox_->RequestFocus();
}
protected:
void SendKeyEvent(ui::KeyboardCode key_code) {
KeyEvent event(ui::ET_KEY_PRESSED, key_code, 0);
input_method_->DispatchKeyEvent(event);
}
View* GetFocusedView() {
return widget_->GetFocusManager()->GetFocusedView();
}
// We need widget to populate wrapper class.
Widget* widget_;
// combobox_ will be allocated InitCombobox() and then owned by widget_.
TestCombobox* combobox_;
// combobox_view_ is the pointer to the pure Views interface of combobox_.
NativeComboboxViews* combobox_view_;
// Combobox does not take ownership of model_, which needs to be scoped.
scoped_ptr<ui::ComboboxModel> model_;
// For testing input method related behaviors.
MockInputMethod* input_method_;
};
TEST_F(NativeComboboxViewsTest, KeyTest) {
InitCombobox();
SendKeyEvent(ui::VKEY_END);
EXPECT_EQ(combobox_->selected_item() + 1, model_->GetItemCount());
SendKeyEvent(ui::VKEY_HOME);
EXPECT_EQ(combobox_->selected_item(), 0);
SendKeyEvent(ui::VKEY_DOWN);
SendKeyEvent(ui::VKEY_DOWN);
EXPECT_EQ(combobox_->selected_item(), 2);
SendKeyEvent(ui::VKEY_RIGHT);
EXPECT_EQ(combobox_->selected_item(), 2);
SendKeyEvent(ui::VKEY_LEFT);
EXPECT_EQ(combobox_->selected_item(), 2);
}
} // namespace views
|