Chromium Code Reviews| OLD | NEW |
|---|---|
| 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/ime/input_method.h" | |
| 12 #include "ui/base/ime/input_method_factory.h" | |
| 11 #include "ui/base/ime/text_input_client.h" | 13 #include "ui/base/ime/text_input_client.h" |
| 12 #include "ui/base/models/combobox_model.h" | 14 #include "ui/base/models/combobox_model.h" |
| 13 #include "ui/events/event.h" | 15 #include "ui/events/event.h" |
| 14 #include "ui/events/event_constants.h" | 16 #include "ui/events/event_constants.h" |
| 15 #include "ui/events/event_utils.h" | 17 #include "ui/events/event_utils.h" |
| 16 #include "ui/events/keycodes/keyboard_codes.h" | 18 #include "ui/events/keycodes/keyboard_codes.h" |
| 17 #include "ui/views/controls/combobox/combobox_listener.h" | 19 #include "ui/views/controls/combobox/combobox_listener.h" |
| 18 #include "ui/views/controls/menu/menu_runner.h" | 20 #include "ui/views/controls/menu/menu_runner.h" |
| 19 #include "ui/views/controls/menu/menu_runner_handler.h" | 21 #include "ui/views/controls/menu/menu_runner_handler.h" |
| 20 #include "ui/views/ime/mock_input_method.h" | |
| 21 #include "ui/views/test/menu_runner_test_api.h" | 22 #include "ui/views/test/menu_runner_test_api.h" |
| 22 #include "ui/views/test/views_test_base.h" | 23 #include "ui/views/test/views_test_base.h" |
| 23 #include "ui/views/widget/widget.h" | 24 #include "ui/views/widget/widget.h" |
| 24 | 25 |
| 25 using base::ASCIIToUTF16; | 26 using base::ASCIIToUTF16; |
| 26 | 27 |
| 27 namespace views { | 28 namespace views { |
| 28 | 29 |
| 29 namespace { | 30 namespace { |
| 30 | 31 |
| (...skipping 163 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 194 private: | 195 private: |
| 195 DISALLOW_COPY_AND_ASSIGN(TestComboboxListener); | 196 DISALLOW_COPY_AND_ASSIGN(TestComboboxListener); |
| 196 }; | 197 }; |
| 197 | 198 |
| 198 } // namespace | 199 } // namespace |
| 199 | 200 |
| 200 class ComboboxTest : public ViewsTestBase { | 201 class ComboboxTest : public ViewsTestBase { |
| 201 public: | 202 public: |
| 202 ComboboxTest() : widget_(NULL), combobox_(NULL) {} | 203 ComboboxTest() : widget_(NULL), combobox_(NULL) {} |
| 203 | 204 |
| 205 void SetUp() override { | |
| 206 ui::SetUpInputMethodFactoryForTesting(); | |
|
sky
2015/06/29 02:17:41
Should we move this clal to ViewsTestBase?
Shu Chen
2015/06/29 02:56:34
Done.
| |
| 207 ViewsTestBase::SetUp(); | |
| 208 } | |
| 209 | |
| 204 void TearDown() override { | 210 void TearDown() override { |
| 205 if (widget_) | 211 if (widget_) |
| 206 widget_->Close(); | 212 widget_->Close(); |
| 207 ViewsTestBase::TearDown(); | 213 ViewsTestBase::TearDown(); |
| 208 } | 214 } |
| 209 | 215 |
| 210 void InitCombobox(const std::set<int>* separators) { | 216 void InitCombobox(const std::set<int>* separators) { |
| 211 model_.reset(new TestComboboxModel()); | 217 model_.reset(new TestComboboxModel()); |
| 212 | 218 |
| 213 if (separators) | 219 if (separators) |
| 214 model_->SetSeparators(*separators); | 220 model_->SetSeparators(*separators); |
| 215 | 221 |
| 216 ASSERT_FALSE(combobox_); | 222 ASSERT_FALSE(combobox_); |
| 217 combobox_ = new TestCombobox(model_.get()); | 223 combobox_ = new TestCombobox(model_.get()); |
| 218 combobox_->set_id(1); | 224 combobox_->set_id(1); |
| 219 | 225 |
| 220 widget_ = new Widget; | 226 widget_ = new Widget; |
| 221 Widget::InitParams params = CreateParams(Widget::InitParams::TYPE_POPUP); | 227 Widget::InitParams params = CreateParams(Widget::InitParams::TYPE_POPUP); |
| 222 params.bounds = gfx::Rect(200, 200, 200, 200); | 228 params.bounds = gfx::Rect(200, 200, 200, 200); |
| 223 widget_->Init(params); | 229 widget_->Init(params); |
| 224 View* container = new View(); | 230 View* container = new View(); |
| 225 widget_->SetContentsView(container); | 231 widget_->SetContentsView(container); |
| 226 container->AddChildView(combobox_); | 232 container->AddChildView(combobox_); |
| 227 | 233 |
| 228 widget_->ReplaceInputMethod(new MockInputMethod); | |
| 229 | |
| 230 // Assumes the Widget is always focused. | |
| 231 widget_->GetInputMethod()->OnFocus(); | |
| 232 | |
| 233 combobox_->RequestFocus(); | 234 combobox_->RequestFocus(); |
| 234 combobox_->SizeToPreferredSize(); | 235 combobox_->SizeToPreferredSize(); |
| 235 } | 236 } |
| 236 | 237 |
| 237 protected: | 238 protected: |
| 238 void SendKeyEvent(ui::KeyboardCode key_code) { | 239 void SendKeyEvent(ui::KeyboardCode key_code) { |
| 239 SendKeyEventWithType(key_code, ui::ET_KEY_PRESSED); | 240 SendKeyEventWithType(key_code, ui::ET_KEY_PRESSED); |
| 240 } | 241 } |
| 241 | 242 |
| 242 void SendKeyEventWithType(ui::KeyboardCode key_code, ui::EventType type) { | 243 void SendKeyEventWithType(ui::KeyboardCode key_code, ui::EventType type) { |
| 243 ui::KeyEvent event(type, key_code, ui::EF_NONE); | 244 ui::KeyEvent event(type, key_code, ui::EF_NONE); |
| 244 widget_->GetInputMethod()->DispatchKeyEvent(event); | 245 FocusManager* focus_manager = widget_->GetFocusManager(); |
| 246 widget_->OnKeyEvent(&event); | |
| 247 if (!event.handled() && focus_manager) | |
| 248 focus_manager->OnKeyEvent(event); | |
| 245 } | 249 } |
| 246 | 250 |
| 247 View* GetFocusedView() { | 251 View* GetFocusedView() { |
| 248 return widget_->GetFocusManager()->GetFocusedView(); | 252 return widget_->GetFocusManager()->GetFocusedView(); |
| 249 } | 253 } |
| 250 | 254 |
| 251 void PerformClick(const gfx::Point& point) { | 255 void PerformClick(const gfx::Point& point) { |
| 252 ui::MouseEvent pressed_event = ui::MouseEvent( | 256 ui::MouseEvent pressed_event = ui::MouseEvent( |
| 253 ui::ET_MOUSE_PRESSED, point, point, ui::EventTimeForNow(), | 257 ui::ET_MOUSE_PRESSED, point, point, ui::EventTimeForNow(), |
| 254 ui::EF_LEFT_MOUSE_BUTTON, ui::EF_LEFT_MOUSE_BUTTON); | 258 ui::EF_LEFT_MOUSE_BUTTON, ui::EF_LEFT_MOUSE_BUTTON); |
| (...skipping 407 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 662 // width. | 666 // width. |
| 663 combobox.SetStyle(Combobox::STYLE_ACTION); | 667 combobox.SetStyle(Combobox::STYLE_ACTION); |
| 664 EXPECT_EQ(short_item_width, combobox.content_size_.width()); | 668 EXPECT_EQ(short_item_width, combobox.content_size_.width()); |
| 665 } | 669 } |
| 666 | 670 |
| 667 TEST_F(ComboboxTest, TypingPrefixNotifiesListener) { | 671 TEST_F(ComboboxTest, TypingPrefixNotifiesListener) { |
| 668 InitCombobox(NULL); | 672 InitCombobox(NULL); |
| 669 | 673 |
| 670 TestComboboxListener listener; | 674 TestComboboxListener listener; |
| 671 combobox_->set_listener(&listener); | 675 combobox_->set_listener(&listener); |
| 676 ui::TextInputClient* input_client = | |
| 677 widget_->GetInputMethod()->GetTextInputClient(); | |
| 672 | 678 |
| 673 // Type the first character of the second menu item ("JELLY"). | 679 // Type the first character of the second menu item ("JELLY"). |
| 674 combobox_->GetTextInputClient()->InsertChar('J', ui::EF_NONE); | 680 input_client->InsertChar('J', ui::EF_NONE); |
| 675 EXPECT_EQ(1, listener.actions_performed()); | 681 EXPECT_EQ(1, listener.actions_performed()); |
| 676 EXPECT_EQ(1, listener.perform_action_index()); | 682 EXPECT_EQ(1, listener.perform_action_index()); |
| 677 | 683 |
| 678 // Type the second character of "JELLY", item shouldn't change and | 684 // Type the second character of "JELLY", item shouldn't change and |
| 679 // OnPerformAction() shouldn't be re-called. | 685 // OnPerformAction() shouldn't be re-called. |
| 680 combobox_->GetTextInputClient()->InsertChar('E', ui::EF_NONE); | 686 input_client->InsertChar('E', ui::EF_NONE); |
| 681 EXPECT_EQ(1, listener.actions_performed()); | 687 EXPECT_EQ(1, listener.actions_performed()); |
| 682 EXPECT_EQ(1, listener.perform_action_index()); | 688 EXPECT_EQ(1, listener.perform_action_index()); |
| 683 | 689 |
| 684 // Clears the typed text. | 690 // Clears the typed text. |
| 685 combobox_->OnBlur(); | 691 combobox_->OnBlur(); |
| 692 combobox_->RequestFocus(); | |
| 686 | 693 |
| 687 // Type the first character of "PEANUT BUTTER", which should change the | 694 // Type the first character of "PEANUT BUTTER", which should change the |
| 688 // selected index and perform an action. | 695 // selected index and perform an action. |
| 689 combobox_->GetTextInputClient()->InsertChar('P', ui::EF_NONE); | 696 input_client->InsertChar('P', ui::EF_NONE); |
| 690 EXPECT_EQ(2, listener.actions_performed()); | 697 EXPECT_EQ(2, listener.actions_performed()); |
| 691 EXPECT_EQ(2, listener.perform_action_index()); | 698 EXPECT_EQ(2, listener.perform_action_index()); |
| 692 } | 699 } |
| 693 | 700 |
| 694 } // namespace views | 701 } // namespace views |
| OLD | NEW |