Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(305)

Side by Side Diff: ui/views/controls/combobox/combobox_unittest.cc

Issue 59383003: Add the button style for combobox (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: sky's review (4) Created 7 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2013 The Chromium Authors. All rights reserved. 1 // Copyright 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "ui/views/controls/combobox/combobox.h" 5 #include "ui/views/controls/combobox/combobox.h"
6 6
7 #include <set> 7 #include <set>
8 8
9 #include "base/basictypes.h" 9 #include "base/basictypes.h"
10 #include "base/strings/utf_string_conversions.h" 10 #include "base/strings/utf_string_conversions.h"
11 #include "ui/base/models/combobox_model.h" 11 #include "ui/base/models/combobox_model.h"
12 #include "ui/events/event.h" 12 #include "ui/events/event.h"
13 #include "ui/events/keycodes/keyboard_codes.h" 13 #include "ui/events/keycodes/keyboard_codes.h"
14 #include "ui/views/controls/combobox/combobox_listener.h" 14 #include "ui/views/controls/combobox/combobox_listener.h"
15 #include "ui/views/controls/combobox/combobox_menu_runner.h"
15 #include "ui/views/ime/mock_input_method.h" 16 #include "ui/views/ime/mock_input_method.h"
16 #include "ui/views/test/views_test_base.h" 17 #include "ui/views/test/views_test_base.h"
17 #include "ui/views/widget/widget.h" 18 #include "ui/views/widget/widget.h"
18 19
19 namespace views { 20 namespace views {
20 21
21 namespace { 22 namespace {
22 23
24 // An dummy implementation of ComboboxMenuRunner to check if the dropdown menu
25 // is shown or not.
26 class TestComboboxMenuRunner : public ComboboxMenuRunner {
27 public:
28 TestComboboxMenuRunner()
29 : executed_(false) {}
30
31 bool executed() const { return executed_; }
32
33 virtual MenuRunner::RunResult Run(Combobox* combobox,
34 MenuRunner* menu_runner,
35 const gfx::Rect& bounds,
36 ui::MenuSourceType source_type) OVERRIDE {
37 executed_ = true;
38 return MenuRunner::NORMAL_EXIT;
39 }
40
41 private:
42 bool executed_;
43
44 DISALLOW_COPY_AND_ASSIGN(TestComboboxMenuRunner);
45 };
46
23 // A wrapper of Combobox to intercept the result of OnKeyPressed() and 47 // A wrapper of Combobox to intercept the result of OnKeyPressed() and
24 // OnKeyReleased() methods. 48 // OnKeyReleased() methods.
25 class TestCombobox : public Combobox { 49 class TestCombobox : public Combobox {
26 public: 50 public:
27 explicit TestCombobox(ui::ComboboxModel* model) 51 explicit TestCombobox(ui::ComboboxModel* model)
28 : Combobox(model), 52 : Combobox(model),
29 key_handled_(false), 53 key_handled_(false),
30 key_received_(false) { 54 key_received_(false) {
31 } 55 }
32 56
(...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after
99 } 123 }
100 124
101 bool deleted() const { return deleted_; } 125 bool deleted() const { return deleted_; }
102 126
103 private: 127 private:
104 bool deleted_; 128 bool deleted_;
105 129
106 DISALLOW_COPY_AND_ASSIGN(EvilListener); 130 DISALLOW_COPY_AND_ASSIGN(EvilListener);
107 }; 131 };
108 132
133 class TestComboboxListener : public views::ComboboxListener {
134 public:
135 TestComboboxListener()
136 : on_selected_index_changed_called_(false),
137 on_combobox_text_button_clicked_called_(false) {
138 }
139 virtual ~TestComboboxListener() {}
140
141 virtual void OnSelectedIndexChanged(views::Combobox* combobox) OVERRIDE {
142 on_selected_index_changed_called_ = true;
143 }
144
145 virtual void OnComboboxTextButtonClicked(views::Combobox* combobox) OVERRIDE {
146 on_combobox_text_button_clicked_called_ = true;
147 }
148
149 bool on_selected_index_changed_called() const {
150 return on_selected_index_changed_called_;
151 }
152
153 bool on_combobox_text_button_clicked_called() const {
154 return on_combobox_text_button_clicked_called_;
155 }
156
157 private:
158 bool on_selected_index_changed_called_;
159 bool on_combobox_text_button_clicked_called_;
160
161 private:
162 DISALLOW_COPY_AND_ASSIGN(TestComboboxListener);
163 };
164
109 } // namespace 165 } // namespace
110 166
111 class ComboboxTest : public ViewsTestBase { 167 class ComboboxTest : public ViewsTestBase {
112 public: 168 public:
113 ComboboxTest() : widget_(NULL), combobox_(NULL), input_method_(NULL) {} 169 ComboboxTest() : widget_(NULL), combobox_(NULL), input_method_(NULL) {}
114 170
115 virtual void TearDown() OVERRIDE { 171 virtual void TearDown() OVERRIDE {
116 if (widget_) 172 if (widget_)
117 widget_->Close(); 173 widget_->Close();
118 ViewsTestBase::TearDown(); 174 ViewsTestBase::TearDown();
119 } 175 }
120 176
121 void InitCombobox() { 177 void InitCombobox() {
122 model_.reset(new TestComboboxModel()); 178 model_.reset(new TestComboboxModel());
123 179
124 ASSERT_FALSE(combobox_); 180 ASSERT_FALSE(combobox_);
125 combobox_ = new TestCombobox(model_.get()); 181 combobox_ = new TestCombobox(model_.get());
126 combobox_->set_id(1); 182 combobox_->set_id(1);
127 183
128 widget_ = new Widget; 184 widget_ = new Widget;
129 Widget::InitParams params = CreateParams(Widget::InitParams::TYPE_POPUP); 185 Widget::InitParams params = CreateParams(Widget::InitParams::TYPE_POPUP);
130 params.bounds = gfx::Rect(100, 100, 100, 100); 186 params.bounds = gfx::Rect(200, 200, 200, 200);
131 widget_->Init(params); 187 widget_->Init(params);
132 View* container = new View(); 188 View* container = new View();
133 widget_->SetContentsView(container); 189 widget_->SetContentsView(container);
134 container->AddChildView(combobox_); 190 container->AddChildView(combobox_);
135 191
136 input_method_ = new MockInputMethod(); 192 input_method_ = new MockInputMethod();
137 widget_->ReplaceInputMethod(input_method_); 193 widget_->ReplaceInputMethod(input_method_);
138 194
139 // Assumes the Widget is always focused. 195 // Assumes the Widget is always focused.
140 input_method_->OnFocus(); 196 input_method_->OnFocus();
141 197
142 combobox_->RequestFocus(); 198 combobox_->RequestFocus();
199 combobox_->SizeToPreferredSize();
143 } 200 }
144 201
145 protected: 202 protected:
146 void SendKeyEvent(ui::KeyboardCode key_code) { 203 void SendKeyEvent(ui::KeyboardCode key_code) {
147 ui::KeyEvent event(ui::ET_KEY_PRESSED, key_code, 0, false); 204 SendKeyEventWithType(key_code, ui::ET_KEY_PRESSED);
205 }
206
207 void SendKeyEventWithType(ui::KeyboardCode key_code, ui::EventType type) {
208 ui::KeyEvent event(type, key_code, 0, false);
148 input_method_->DispatchKeyEvent(event); 209 input_method_->DispatchKeyEvent(event);
149 } 210 }
150 211
151 View* GetFocusedView() { 212 View* GetFocusedView() {
152 return widget_->GetFocusManager()->GetFocusedView(); 213 return widget_->GetFocusManager()->GetFocusedView();
153 } 214 }
154 215
216 void PerformClick(const gfx::Point& point) {
217 ui::MouseEvent pressed_event = ui::MouseEvent(ui::ET_MOUSE_PRESSED, point,
218 point,
219 ui::EF_LEFT_MOUSE_BUTTON);
220 widget_->OnMouseEvent(&pressed_event);
221 ui::MouseEvent released_event = ui::MouseEvent(ui::ET_MOUSE_RELEASED, point,
222 point,
223 ui::EF_LEFT_MOUSE_BUTTON);
224 widget_->OnMouseEvent(&released_event);
225 }
226
155 // We need widget to populate wrapper class. 227 // We need widget to populate wrapper class.
156 Widget* widget_; 228 Widget* widget_;
157 229
158 // |combobox_| will be allocated InitCombobox() and then owned by |widget_|. 230 // |combobox_| will be allocated InitCombobox() and then owned by |widget_|.
159 TestCombobox* combobox_; 231 TestCombobox* combobox_;
160 232
161 // Combobox does not take ownership of the model, hence it needs to be scoped. 233 // Combobox does not take ownership of the model, hence it needs to be scoped.
162 scoped_ptr<TestComboboxModel> model_; 234 scoped_ptr<TestComboboxModel> model_;
163 235
164 // For testing input method related behaviors. 236 // For testing input method related behaviors.
(...skipping 198 matching lines...) Expand 10 before | Expand all | Expand 10 after
363 435
364 TEST_F(ComboboxTest, ListenerHandlesDelete) { 436 TEST_F(ComboboxTest, ListenerHandlesDelete) {
365 TestComboboxModel model; 437 TestComboboxModel model;
366 TestCombobox* combobox = new TestCombobox(&model); // Deleted on change. 438 TestCombobox* combobox = new TestCombobox(&model); // Deleted on change.
367 EvilListener evil_listener; 439 EvilListener evil_listener;
368 combobox->set_listener(&evil_listener); 440 combobox->set_listener(&evil_listener);
369 ASSERT_NO_FATAL_FAILURE(combobox->ExecuteCommand(2)); 441 ASSERT_NO_FATAL_FAILURE(combobox->ExecuteCommand(2));
370 EXPECT_TRUE(evil_listener.deleted()); 442 EXPECT_TRUE(evil_listener.deleted());
371 } 443 }
372 444
445 TEST_F(ComboboxTest, Click) {
446 InitCombobox();
447
448 TestComboboxListener listener;
449 combobox_->set_listener(&listener);
450
451 combobox_->Layout();
452
453 // Click the left side. The menu is shown.
454 TestComboboxMenuRunner* menu_runner = new TestComboboxMenuRunner();
455 combobox_->SetComboboxMenuRunner(menu_runner);
456 PerformClick(gfx::Point(combobox_->x() + 1,
457 combobox_->y() + combobox_->height() / 2));
458 EXPECT_FALSE(listener.on_combobox_text_button_clicked_called());
459 EXPECT_TRUE(menu_runner->executed());
460 }
461
462 TEST_F(ComboboxTest, NotifyOnClickWithReturnKey) {
463 InitCombobox();
464
465 TestComboboxListener listener;
466 combobox_->set_listener(&listener);
467
468 // With STYLE_SHOW_DROP_DOWN_ON_CLICK, the click event is ignored.
469 SendKeyEvent(ui::VKEY_RETURN);
470 EXPECT_FALSE(listener.on_combobox_text_button_clicked_called());
471
472 // With STYLE_NOTIFY_ON_CLICK, the click event is notified.
473 combobox_->SetStyle(Combobox::STYLE_NOTIFY_ON_CLICK);
474 SendKeyEvent(ui::VKEY_RETURN);
475 EXPECT_TRUE(listener.on_combobox_text_button_clicked_called());
476 }
477
478 TEST_F(ComboboxTest, NotifyOnClickWithSpaceKey) {
479 InitCombobox();
480
481 TestComboboxListener listener;
482 combobox_->set_listener(&listener);
483
484 // With STYLE_SHOW_DROP_DOWN_ON_CLICK, the click event is ignored.
485 SendKeyEvent(ui::VKEY_SPACE);
486 EXPECT_FALSE(listener.on_combobox_text_button_clicked_called());
487 SendKeyEventWithType(ui::VKEY_SPACE, ui::ET_KEY_RELEASED);
488 EXPECT_FALSE(listener.on_combobox_text_button_clicked_called());
489
490 // With STYLE_NOTIFY_ON_CLICK, the click event is notified after releasing.
491 combobox_->SetStyle(Combobox::STYLE_NOTIFY_ON_CLICK);
492 SendKeyEvent(ui::VKEY_SPACE);
493 EXPECT_FALSE(listener.on_combobox_text_button_clicked_called());
494 SendKeyEventWithType(ui::VKEY_SPACE, ui::ET_KEY_RELEASED);
495 EXPECT_TRUE(listener.on_combobox_text_button_clicked_called());
496 }
497
498 TEST_F(ComboboxTest, NotifyOnClickWithMouse) {
499 InitCombobox();
500
501 TestComboboxListener listener;
502 combobox_->set_listener(&listener);
503
504 combobox_->SetStyle(Combobox::STYLE_NOTIFY_ON_CLICK);
505 combobox_->Layout();
506
507 // Click the right side (arrow button). The menu is shown.
508 TestComboboxMenuRunner* menu_runner = new TestComboboxMenuRunner();
509 combobox_->SetComboboxMenuRunner(menu_runner);
510 PerformClick(gfx::Point(combobox_->x() + combobox_->width() - 1,
511 combobox_->y() + combobox_->height() / 2));
512 EXPECT_FALSE(listener.on_combobox_text_button_clicked_called());
513 EXPECT_TRUE(menu_runner->executed());
514
515 // Click the left side (text button). The click event is notified.
516 menu_runner = new TestComboboxMenuRunner();
517 combobox_->SetComboboxMenuRunner(menu_runner);
518 PerformClick(gfx::Point(combobox_->x() + 1,
519 combobox_->y() + combobox_->height() / 2));
520 EXPECT_TRUE(listener.on_combobox_text_button_clicked_called());
521 EXPECT_FALSE(menu_runner->executed());
522 }
523
373 } // namespace views 524 } // namespace views
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698