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" |
(...skipping 17 matching lines...) Expand all Loading... |
28 namespace { | 28 namespace { |
29 | 29 |
30 // An dummy implementation of MenuRunnerHandler to check if the dropdown menu is | 30 // An dummy implementation of MenuRunnerHandler to check if the dropdown menu is |
31 // shown or not. | 31 // shown or not. |
32 class TestMenuRunnerHandler : public MenuRunnerHandler { | 32 class TestMenuRunnerHandler : public MenuRunnerHandler { |
33 public: | 33 public: |
34 TestMenuRunnerHandler() : executed_(false) {} | 34 TestMenuRunnerHandler() : executed_(false) {} |
35 | 35 |
36 bool executed() const { return executed_; } | 36 bool executed() const { return executed_; } |
37 | 37 |
38 virtual MenuRunner::RunResult RunMenuAt(Widget* parent, | 38 MenuRunner::RunResult RunMenuAt(Widget* parent, |
39 MenuButton* button, | 39 MenuButton* button, |
40 const gfx::Rect& bounds, | 40 const gfx::Rect& bounds, |
41 MenuAnchorPosition anchor, | 41 MenuAnchorPosition anchor, |
42 ui::MenuSourceType source_type, | 42 ui::MenuSourceType source_type, |
43 int32 types) override { | 43 int32 types) override { |
44 executed_ = true; | 44 executed_ = true; |
45 return MenuRunner::NORMAL_EXIT; | 45 return MenuRunner::NORMAL_EXIT; |
46 } | 46 } |
47 | 47 |
48 private: | 48 private: |
49 bool executed_; | 49 bool executed_; |
50 | 50 |
51 DISALLOW_COPY_AND_ASSIGN(TestMenuRunnerHandler); | 51 DISALLOW_COPY_AND_ASSIGN(TestMenuRunnerHandler); |
52 }; | 52 }; |
53 | 53 |
54 // A wrapper of Combobox to intercept the result of OnKeyPressed() and | 54 // A wrapper of Combobox to intercept the result of OnKeyPressed() and |
55 // OnKeyReleased() methods. | 55 // OnKeyReleased() methods. |
56 class TestCombobox : public Combobox { | 56 class TestCombobox : public Combobox { |
57 public: | 57 public: |
58 explicit TestCombobox(ui::ComboboxModel* model) | 58 explicit TestCombobox(ui::ComboboxModel* model) |
59 : Combobox(model), | 59 : Combobox(model), |
60 key_handled_(false), | 60 key_handled_(false), |
61 key_received_(false) {} | 61 key_received_(false) {} |
62 | 62 |
63 virtual bool OnKeyPressed(const ui::KeyEvent& e) override { | 63 bool OnKeyPressed(const ui::KeyEvent& e) override { |
64 key_received_ = true; | 64 key_received_ = true; |
65 key_handled_ = Combobox::OnKeyPressed(e); | 65 key_handled_ = Combobox::OnKeyPressed(e); |
66 return key_handled_; | 66 return key_handled_; |
67 } | 67 } |
68 | 68 |
69 virtual bool OnKeyReleased(const ui::KeyEvent& e) override { | 69 bool OnKeyReleased(const ui::KeyEvent& e) override { |
70 key_received_ = true; | 70 key_received_ = true; |
71 key_handled_ = Combobox::OnKeyReleased(e); | 71 key_handled_ = Combobox::OnKeyReleased(e); |
72 return key_handled_; | 72 return key_handled_; |
73 } | 73 } |
74 | 74 |
75 bool key_handled() const { return key_handled_; } | 75 bool key_handled() const { return key_handled_; } |
76 bool key_received() const { return key_received_; } | 76 bool key_received() const { return key_received_; } |
77 | 77 |
78 void clear() { | 78 void clear() { |
79 key_received_ = key_handled_ = false; | 79 key_received_ = key_handled_ = false; |
80 } | 80 } |
81 | 81 |
82 private: | 82 private: |
83 bool key_handled_; | 83 bool key_handled_; |
84 bool key_received_; | 84 bool key_received_; |
85 | 85 |
86 DISALLOW_COPY_AND_ASSIGN(TestCombobox); | 86 DISALLOW_COPY_AND_ASSIGN(TestCombobox); |
87 }; | 87 }; |
88 | 88 |
89 // A concrete class is needed to test the combobox. | 89 // A concrete class is needed to test the combobox. |
90 class TestComboboxModel : public ui::ComboboxModel { | 90 class TestComboboxModel : public ui::ComboboxModel { |
91 public: | 91 public: |
92 TestComboboxModel() {} | 92 TestComboboxModel() {} |
93 virtual ~TestComboboxModel() {} | 93 ~TestComboboxModel() override {} |
94 | 94 |
95 static const int kItemCount = 10; | 95 static const int kItemCount = 10; |
96 | 96 |
97 // ui::ComboboxModel: | 97 // ui::ComboboxModel: |
98 virtual int GetItemCount() const override { | 98 int GetItemCount() const override { return kItemCount; } |
99 return kItemCount; | 99 base::string16 GetItemAt(int index) override { |
100 } | |
101 virtual base::string16 GetItemAt(int index) override { | |
102 if (IsItemSeparatorAt(index)) { | 100 if (IsItemSeparatorAt(index)) { |
103 NOTREACHED(); | 101 NOTREACHED(); |
104 return ASCIIToUTF16("SEPARATOR"); | 102 return ASCIIToUTF16("SEPARATOR"); |
105 } | 103 } |
106 return ASCIIToUTF16(index % 2 == 0 ? "PEANUT BUTTER" : "JELLY"); | 104 return ASCIIToUTF16(index % 2 == 0 ? "PEANUT BUTTER" : "JELLY"); |
107 } | 105 } |
108 virtual bool IsItemSeparatorAt(int index) override { | 106 bool IsItemSeparatorAt(int index) override { |
109 return separators_.find(index) != separators_.end(); | 107 return separators_.find(index) != separators_.end(); |
110 } | 108 } |
111 | 109 |
112 virtual int GetDefaultIndex() const override { | 110 int GetDefaultIndex() const override { |
113 // Return the first index that is not a separator. | 111 // Return the first index that is not a separator. |
114 for (int index = 0; index < kItemCount; ++index) { | 112 for (int index = 0; index < kItemCount; ++index) { |
115 if (separators_.find(index) == separators_.end()) | 113 if (separators_.find(index) == separators_.end()) |
116 return index; | 114 return index; |
117 } | 115 } |
118 NOTREACHED(); | 116 NOTREACHED(); |
119 return 0; | 117 return 0; |
120 } | 118 } |
121 | 119 |
122 void SetSeparators(const std::set<int>& separators) { | 120 void SetSeparators(const std::set<int>& separators) { |
123 separators_ = separators; | 121 separators_ = separators; |
124 } | 122 } |
125 | 123 |
126 private: | 124 private: |
127 std::set<int> separators_; | 125 std::set<int> separators_; |
128 | 126 |
129 DISALLOW_COPY_AND_ASSIGN(TestComboboxModel); | 127 DISALLOW_COPY_AND_ASSIGN(TestComboboxModel); |
130 }; | 128 }; |
131 | 129 |
132 // A combobox model which refers to a vector. | 130 // A combobox model which refers to a vector. |
133 class VectorComboboxModel : public ui::ComboboxModel { | 131 class VectorComboboxModel : public ui::ComboboxModel { |
134 public: | 132 public: |
135 explicit VectorComboboxModel(std::vector<std::string>* values) | 133 explicit VectorComboboxModel(std::vector<std::string>* values) |
136 : values_(values) {} | 134 : values_(values) {} |
137 virtual ~VectorComboboxModel() {} | 135 ~VectorComboboxModel() override {} |
138 | 136 |
139 // ui::ComboboxModel: | 137 // ui::ComboboxModel: |
140 virtual int GetItemCount() const override { | 138 int GetItemCount() const override { return (int)values_->size(); } |
141 return (int)values_->size(); | 139 base::string16 GetItemAt(int index) override { |
142 } | |
143 virtual base::string16 GetItemAt(int index) override { | |
144 return ASCIIToUTF16(values_->at(index)); | 140 return ASCIIToUTF16(values_->at(index)); |
145 } | 141 } |
146 virtual bool IsItemSeparatorAt(int index) override { | 142 bool IsItemSeparatorAt(int index) override { return false; } |
147 return false; | |
148 } | |
149 | 143 |
150 private: | 144 private: |
151 std::vector<std::string>* values_; | 145 std::vector<std::string>* values_; |
152 }; | 146 }; |
153 | 147 |
154 class EvilListener : public ComboboxListener { | 148 class EvilListener : public ComboboxListener { |
155 public: | 149 public: |
156 EvilListener() : deleted_(false) {} | 150 EvilListener() : deleted_(false) {} |
157 virtual ~EvilListener() {}; | 151 ~EvilListener() override{}; |
158 | 152 |
159 // ComboboxListener: | 153 // ComboboxListener: |
160 virtual void OnPerformAction(Combobox* combobox) override { | 154 void OnPerformAction(Combobox* combobox) override { |
161 delete combobox; | 155 delete combobox; |
162 deleted_ = true; | 156 deleted_ = true; |
163 } | 157 } |
164 | 158 |
165 bool deleted() const { return deleted_; } | 159 bool deleted() const { return deleted_; } |
166 | 160 |
167 private: | 161 private: |
168 bool deleted_; | 162 bool deleted_; |
169 | 163 |
170 DISALLOW_COPY_AND_ASSIGN(EvilListener); | 164 DISALLOW_COPY_AND_ASSIGN(EvilListener); |
171 }; | 165 }; |
172 | 166 |
173 class TestComboboxListener : public views::ComboboxListener { | 167 class TestComboboxListener : public views::ComboboxListener { |
174 public: | 168 public: |
175 TestComboboxListener() : perform_action_index_(-1), actions_performed_(0) {} | 169 TestComboboxListener() : perform_action_index_(-1), actions_performed_(0) {} |
176 virtual ~TestComboboxListener() {} | 170 ~TestComboboxListener() override {} |
177 | 171 |
178 virtual void OnPerformAction(views::Combobox* combobox) override { | 172 void OnPerformAction(views::Combobox* combobox) override { |
179 perform_action_index_ = combobox->selected_index(); | 173 perform_action_index_ = combobox->selected_index(); |
180 actions_performed_++; | 174 actions_performed_++; |
181 } | 175 } |
182 | 176 |
183 int perform_action_index() const { | 177 int perform_action_index() const { |
184 return perform_action_index_; | 178 return perform_action_index_; |
185 } | 179 } |
186 | 180 |
187 bool on_perform_action_called() const { | 181 bool on_perform_action_called() const { |
188 return actions_performed_ > 0; | 182 return actions_performed_ > 0; |
(...skipping 10 matching lines...) Expand all Loading... |
199 private: | 193 private: |
200 DISALLOW_COPY_AND_ASSIGN(TestComboboxListener); | 194 DISALLOW_COPY_AND_ASSIGN(TestComboboxListener); |
201 }; | 195 }; |
202 | 196 |
203 } // namespace | 197 } // namespace |
204 | 198 |
205 class ComboboxTest : public ViewsTestBase { | 199 class ComboboxTest : public ViewsTestBase { |
206 public: | 200 public: |
207 ComboboxTest() : widget_(NULL), combobox_(NULL) {} | 201 ComboboxTest() : widget_(NULL), combobox_(NULL) {} |
208 | 202 |
209 virtual void TearDown() override { | 203 void TearDown() override { |
210 if (widget_) | 204 if (widget_) |
211 widget_->Close(); | 205 widget_->Close(); |
212 ViewsTestBase::TearDown(); | 206 ViewsTestBase::TearDown(); |
213 } | 207 } |
214 | 208 |
215 void InitCombobox(const std::set<int>* separators) { | 209 void InitCombobox(const std::set<int>* separators) { |
216 model_.reset(new TestComboboxModel()); | 210 model_.reset(new TestComboboxModel()); |
217 | 211 |
218 if (separators) | 212 if (separators) |
219 model_->SetSeparators(*separators); | 213 model_->SetSeparators(*separators); |
(...skipping 472 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
692 combobox_->OnBlur(); | 686 combobox_->OnBlur(); |
693 | 687 |
694 // Type the first character of "PEANUT BUTTER", which should change the | 688 // Type the first character of "PEANUT BUTTER", which should change the |
695 // selected index and perform an action. | 689 // selected index and perform an action. |
696 combobox_->GetTextInputClient()->InsertChar('P', ui::EF_NONE); | 690 combobox_->GetTextInputClient()->InsertChar('P', ui::EF_NONE); |
697 EXPECT_EQ(2, listener.actions_performed()); | 691 EXPECT_EQ(2, listener.actions_performed()); |
698 EXPECT_EQ(2, listener.perform_action_index()); | 692 EXPECT_EQ(2, listener.perform_action_index()); |
699 } | 693 } |
700 | 694 |
701 } // namespace views | 695 } // namespace views |
OLD | NEW |