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

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 (3) 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/strings/utf_string_conversions.h" 9 #include "base/strings/utf_string_conversions.h"
10 #include "ui/base/models/combobox_model.h" 10 #include "ui/base/models/combobox_model.h"
11 #include "ui/events/event.h" 11 #include "ui/events/event.h"
12 #include "ui/events/keycodes/keyboard_codes.h" 12 #include "ui/events/keycodes/keyboard_codes.h"
13 #include "ui/views/controls/combobox/combobox_listener.h"
13 #include "ui/views/ime/mock_input_method.h" 14 #include "ui/views/ime/mock_input_method.h"
14 #include "ui/views/test/views_test_base.h" 15 #include "ui/views/test/views_test_base.h"
15 #include "ui/views/widget/widget.h" 16 #include "ui/views/widget/widget.h"
16 17
17 namespace { 18 namespace {
18 19
19 // A wrapper of Combobox to intercept the result of OnKeyPressed() and 20 // A wrapper of Combobox to intercept the result of OnKeyPressed() and
20 // OnKeyReleased() methods. 21 // OnKeyReleased() methods.
21 class TestCombobox : public views::Combobox { 22 class TestCombobox : public views::Combobox {
22 public: 23 public:
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after
76 void SetSeparators(const std::set<int>& separators) { 77 void SetSeparators(const std::set<int>& separators) {
77 separators_ = separators; 78 separators_ = separators;
78 } 79 }
79 80
80 private: 81 private:
81 std::set<int> separators_; 82 std::set<int> separators_;
82 83
83 DISALLOW_COPY_AND_ASSIGN(TestComboboxModel); 84 DISALLOW_COPY_AND_ASSIGN(TestComboboxModel);
84 }; 85 };
85 86
87 class TestComboboxListener : public views::ComboboxListener {
88 public:
89 TestComboboxListener()
90 : on_selected_index_changed_(false),
91 on_combobox_text_button_clicked_called_(false) {
92 }
93 virtual ~TestComboboxListener() {}
94
95 virtual void OnSelectedIndexChanged(views::Combobox* combobox) OVERRIDE {
96 on_selected_index_changed_ = true;
97 }
98
99 virtual void OnComboboxTextButtonClicked(views::Combobox* combobox) OVERRIDE {
100 on_combobox_text_button_clicked_called_ = true;
101 }
102
103 bool on_selected_index_changed_;
sky 2013/12/05 16:46:32 private and add accessors/setters.
hajimehoshi 2013/12/06 12:27:04 Done.
104 bool on_combobox_text_button_clicked_called_;
105
106 private:
107 DISALLOW_COPY_AND_ASSIGN(TestComboboxListener);
108 };
109
86 } // namespace 110 } // namespace
87 111
88 namespace views { 112 namespace views {
89 113
90 class ComboboxTest : public ViewsTestBase { 114 class ComboboxTest : public ViewsTestBase {
91 public: 115 public:
92 ComboboxTest() : widget_(NULL), combobox_(NULL), input_method_(NULL) {} 116 ComboboxTest() : widget_(NULL), combobox_(NULL), input_method_(NULL) {}
93 117
94 virtual void TearDown() OVERRIDE { 118 virtual void TearDown() OVERRIDE {
95 if (widget_) 119 if (widget_)
(...skipping 20 matching lines...) Expand all
116 widget_->ReplaceInputMethod(input_method_); 140 widget_->ReplaceInputMethod(input_method_);
117 141
118 // Assumes the Widget is always focused. 142 // Assumes the Widget is always focused.
119 input_method_->OnFocus(); 143 input_method_->OnFocus();
120 144
121 combobox_->RequestFocus(); 145 combobox_->RequestFocus();
122 } 146 }
123 147
124 protected: 148 protected:
125 void SendKeyEvent(ui::KeyboardCode key_code) { 149 void SendKeyEvent(ui::KeyboardCode key_code) {
126 ui::KeyEvent event(ui::ET_KEY_PRESSED, key_code, 0, false); 150 SendKeyEventWithType(key_code, ui::ET_KEY_PRESSED);
151 }
152
153 void SendKeyEventWithType(ui::KeyboardCode key_code, ui::EventType type) {
154 ui::KeyEvent event(type, key_code, 0, false);
127 input_method_->DispatchKeyEvent(event); 155 input_method_->DispatchKeyEvent(event);
128 } 156 }
129 157
130 View* GetFocusedView() { 158 View* GetFocusedView() {
131 return widget_->GetFocusManager()->GetFocusedView(); 159 return widget_->GetFocusManager()->GetFocusedView();
132 } 160 }
133 161
134 // We need widget to populate wrapper class. 162 // We need widget to populate wrapper class.
135 Widget* widget_; 163 Widget* widget_;
136 164
(...skipping 196 matching lines...) Expand 10 before | Expand all | Expand 10 after
333 InitCombobox(); 361 InitCombobox();
334 ASSERT_EQ(model_->GetDefaultIndex(), combobox_->selected_index()); 362 ASSERT_EQ(model_->GetDefaultIndex(), combobox_->selected_index());
335 EXPECT_TRUE(combobox_->SelectValue(ASCIIToUTF16("PEANUT BUTTER"))); 363 EXPECT_TRUE(combobox_->SelectValue(ASCIIToUTF16("PEANUT BUTTER")));
336 EXPECT_EQ(0, combobox_->selected_index()); 364 EXPECT_EQ(0, combobox_->selected_index());
337 EXPECT_TRUE(combobox_->SelectValue(ASCIIToUTF16("JELLY"))); 365 EXPECT_TRUE(combobox_->SelectValue(ASCIIToUTF16("JELLY")));
338 EXPECT_EQ(1, combobox_->selected_index()); 366 EXPECT_EQ(1, combobox_->selected_index());
339 EXPECT_FALSE(combobox_->SelectValue(ASCIIToUTF16("BANANAS"))); 367 EXPECT_FALSE(combobox_->SelectValue(ASCIIToUTF16("BANANAS")));
340 EXPECT_EQ(1, combobox_->selected_index()); 368 EXPECT_EQ(1, combobox_->selected_index());
341 } 369 }
342 370
371 TEST_F(ComboboxTest, NotifyOnClick) {
sky 2013/12/05 16:46:32 Add coverage of mouse events too.
hajimehoshi 2013/12/06 12:27:04 Done. (Added a class ComboboxMenuRunner)
372 InitCombobox();
373
374 TestComboboxListener listener;
375 combobox_->set_listener(&listener);
376
377 // With STYLE_SHOW_DROP_DOWN_ON_CLICK, the click event is ignored.
378 SendKeyEvent(ui::VKEY_RETURN);
379 EXPECT_FALSE(listener.on_combobox_text_button_clicked_called_);
380
381 // With STYLE_NOTIFY_ON_CLICK, the click event is notified.
382 combobox_->SetStyle(Combobox::STYLE_NOTIFY_ON_CLICK);
383 SendKeyEvent(ui::VKEY_RETURN);
384 EXPECT_TRUE(listener.on_combobox_text_button_clicked_called_);
385
386 // With a space key, the click event is notified after releasing.
387 listener.on_combobox_text_button_clicked_called_ = false;
388 SendKeyEvent(ui::VKEY_SPACE);
389 EXPECT_FALSE(listener.on_combobox_text_button_clicked_called_);
390 SendKeyEventWithType(ui::VKEY_SPACE, ui::ET_KEY_RELEASED);
391 EXPECT_TRUE(listener.on_combobox_text_button_clicked_called_);
392 }
393
343 } // namespace views 394 } // namespace views
OLDNEW
« ui/views/controls/combobox/combobox.cc ('K') | « ui/views/controls/combobox/combobox_listener.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698