| 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" |
| 11 #include "ui/base/ime/text_input_client.h" | 12 #include "ui/base/ime/text_input_client.h" |
| 12 #include "ui/base/models/combobox_model.h" | 13 #include "ui/base/models/combobox_model.h" |
| 13 #include "ui/events/event.h" | 14 #include "ui/events/event.h" |
| 14 #include "ui/events/event_constants.h" | 15 #include "ui/events/event_constants.h" |
| 15 #include "ui/events/event_utils.h" | 16 #include "ui/events/event_utils.h" |
| 16 #include "ui/events/keycodes/keyboard_codes.h" | 17 #include "ui/events/keycodes/keyboard_codes.h" |
| 17 #include "ui/views/controls/combobox/combobox_listener.h" | 18 #include "ui/views/controls/combobox/combobox_listener.h" |
| 18 #include "ui/views/controls/menu/menu_runner.h" | 19 #include "ui/views/controls/menu/menu_runner.h" |
| 19 #include "ui/views/controls/menu/menu_runner_handler.h" | 20 #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" | 21 #include "ui/views/test/menu_runner_test_api.h" |
| 22 #include "ui/views/test/views_test_base.h" | 22 #include "ui/views/test/views_test_base.h" |
| 23 #include "ui/views/widget/widget.h" | 23 #include "ui/views/widget/widget.h" |
| 24 | 24 |
| 25 using base::ASCIIToUTF16; | 25 using base::ASCIIToUTF16; |
| 26 | 26 |
| 27 namespace views { | 27 namespace views { |
| 28 | 28 |
| 29 namespace { | 29 namespace { |
| 30 | 30 |
| (...skipping 187 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 218 combobox_->set_id(1); | 218 combobox_->set_id(1); |
| 219 | 219 |
| 220 widget_ = new Widget; | 220 widget_ = new Widget; |
| 221 Widget::InitParams params = CreateParams(Widget::InitParams::TYPE_POPUP); | 221 Widget::InitParams params = CreateParams(Widget::InitParams::TYPE_POPUP); |
| 222 params.bounds = gfx::Rect(200, 200, 200, 200); | 222 params.bounds = gfx::Rect(200, 200, 200, 200); |
| 223 widget_->Init(params); | 223 widget_->Init(params); |
| 224 View* container = new View(); | 224 View* container = new View(); |
| 225 widget_->SetContentsView(container); | 225 widget_->SetContentsView(container); |
| 226 container->AddChildView(combobox_); | 226 container->AddChildView(combobox_); |
| 227 | 227 |
| 228 widget_->ReplaceInputMethod(new MockInputMethod); | |
| 229 | |
| 230 // Assumes the Widget is always focused. | |
| 231 widget_->GetInputMethod()->OnFocus(); | |
| 232 | |
| 233 combobox_->RequestFocus(); | 228 combobox_->RequestFocus(); |
| 234 combobox_->SizeToPreferredSize(); | 229 combobox_->SizeToPreferredSize(); |
| 235 } | 230 } |
| 236 | 231 |
| 237 protected: | 232 protected: |
| 238 void SendKeyEvent(ui::KeyboardCode key_code) { | 233 void SendKeyEvent(ui::KeyboardCode key_code) { |
| 239 SendKeyEventWithType(key_code, ui::ET_KEY_PRESSED); | 234 SendKeyEventWithType(key_code, ui::ET_KEY_PRESSED); |
| 240 } | 235 } |
| 241 | 236 |
| 242 void SendKeyEventWithType(ui::KeyboardCode key_code, ui::EventType type) { | 237 void SendKeyEventWithType(ui::KeyboardCode key_code, ui::EventType type) { |
| 243 ui::KeyEvent event(type, key_code, ui::EF_NONE); | 238 ui::KeyEvent event(type, key_code, ui::EF_NONE); |
| 244 widget_->GetInputMethod()->DispatchKeyEvent(event); | 239 FocusManager* focus_manager = widget_->GetFocusManager(); |
| 240 widget_->OnKeyEvent(&event); |
| 241 if (!event.handled() && focus_manager) |
| 242 focus_manager->OnKeyEvent(event); |
| 245 } | 243 } |
| 246 | 244 |
| 247 View* GetFocusedView() { | 245 View* GetFocusedView() { |
| 248 return widget_->GetFocusManager()->GetFocusedView(); | 246 return widget_->GetFocusManager()->GetFocusedView(); |
| 249 } | 247 } |
| 250 | 248 |
| 251 void PerformClick(const gfx::Point& point) { | 249 void PerformClick(const gfx::Point& point) { |
| 252 ui::MouseEvent pressed_event = ui::MouseEvent( | 250 ui::MouseEvent pressed_event = ui::MouseEvent( |
| 253 ui::ET_MOUSE_PRESSED, point, point, ui::EventTimeForNow(), | 251 ui::ET_MOUSE_PRESSED, point, point, ui::EventTimeForNow(), |
| 254 ui::EF_LEFT_MOUSE_BUTTON, ui::EF_LEFT_MOUSE_BUTTON); | 252 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. | 660 // width. |
| 663 combobox.SetStyle(Combobox::STYLE_ACTION); | 661 combobox.SetStyle(Combobox::STYLE_ACTION); |
| 664 EXPECT_EQ(short_item_width, combobox.content_size_.width()); | 662 EXPECT_EQ(short_item_width, combobox.content_size_.width()); |
| 665 } | 663 } |
| 666 | 664 |
| 667 TEST_F(ComboboxTest, TypingPrefixNotifiesListener) { | 665 TEST_F(ComboboxTest, TypingPrefixNotifiesListener) { |
| 668 InitCombobox(NULL); | 666 InitCombobox(NULL); |
| 669 | 667 |
| 670 TestComboboxListener listener; | 668 TestComboboxListener listener; |
| 671 combobox_->set_listener(&listener); | 669 combobox_->set_listener(&listener); |
| 670 ui::TextInputClient* input_client = |
| 671 widget_->GetInputMethod()->GetTextInputClient(); |
| 672 | 672 |
| 673 // Type the first character of the second menu item ("JELLY"). | 673 // Type the first character of the second menu item ("JELLY"). |
| 674 combobox_->GetTextInputClient()->InsertChar('J', ui::EF_NONE); | 674 input_client->InsertChar('J', ui::EF_NONE); |
| 675 EXPECT_EQ(1, listener.actions_performed()); | 675 EXPECT_EQ(1, listener.actions_performed()); |
| 676 EXPECT_EQ(1, listener.perform_action_index()); | 676 EXPECT_EQ(1, listener.perform_action_index()); |
| 677 | 677 |
| 678 // Type the second character of "JELLY", item shouldn't change and | 678 // Type the second character of "JELLY", item shouldn't change and |
| 679 // OnPerformAction() shouldn't be re-called. | 679 // OnPerformAction() shouldn't be re-called. |
| 680 combobox_->GetTextInputClient()->InsertChar('E', ui::EF_NONE); | 680 input_client->InsertChar('E', ui::EF_NONE); |
| 681 EXPECT_EQ(1, listener.actions_performed()); | 681 EXPECT_EQ(1, listener.actions_performed()); |
| 682 EXPECT_EQ(1, listener.perform_action_index()); | 682 EXPECT_EQ(1, listener.perform_action_index()); |
| 683 | 683 |
| 684 // Clears the typed text. | 684 // Clears the typed text. |
| 685 combobox_->OnBlur(); | 685 combobox_->OnBlur(); |
| 686 combobox_->RequestFocus(); |
| 686 | 687 |
| 687 // Type the first character of "PEANUT BUTTER", which should change the | 688 // Type the first character of "PEANUT BUTTER", which should change the |
| 688 // selected index and perform an action. | 689 // selected index and perform an action. |
| 689 combobox_->GetTextInputClient()->InsertChar('P', ui::EF_NONE); | 690 input_client->InsertChar('P', ui::EF_NONE); |
| 690 EXPECT_EQ(2, listener.actions_performed()); | 691 EXPECT_EQ(2, listener.actions_performed()); |
| 691 EXPECT_EQ(2, listener.perform_action_index()); | 692 EXPECT_EQ(2, listener.perform_action_index()); |
| 692 } | 693 } |
| 693 | 694 |
| 694 } // namespace views | 695 } // namespace views |
| OLD | NEW |