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