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 74 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
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 virtual ~TestComboboxModel() {} |
94 | 94 |
| 95 static const int kItemCount = 10; |
| 96 |
95 // ui::ComboboxModel: | 97 // ui::ComboboxModel: |
96 virtual int GetItemCount() const OVERRIDE { | 98 virtual int GetItemCount() const OVERRIDE { |
97 return 10; | 99 return kItemCount; |
98 } | 100 } |
99 virtual base::string16 GetItemAt(int index) OVERRIDE { | 101 virtual base::string16 GetItemAt(int index) OVERRIDE { |
100 if (IsItemSeparatorAt(index)) { | 102 if (IsItemSeparatorAt(index)) { |
101 NOTREACHED(); | 103 NOTREACHED(); |
102 return ASCIIToUTF16("SEPARATOR"); | 104 return ASCIIToUTF16("SEPARATOR"); |
103 } | 105 } |
104 return ASCIIToUTF16(index % 2 == 0 ? "PEANUT BUTTER" : "JELLY"); | 106 return ASCIIToUTF16(index % 2 == 0 ? "PEANUT BUTTER" : "JELLY"); |
105 } | 107 } |
106 virtual bool IsItemSeparatorAt(int index) OVERRIDE { | 108 virtual bool IsItemSeparatorAt(int index) OVERRIDE { |
107 return separators_.find(index) != separators_.end(); | 109 return separators_.find(index) != separators_.end(); |
108 } | 110 } |
109 | 111 |
| 112 virtual int GetDefaultIndex() const OVERRIDE { |
| 113 // Return the first index that is not a separator. |
| 114 for (int index = 0; index < kItemCount; ++index) { |
| 115 if (separators_.find(index) == separators_.end()) |
| 116 return index; |
| 117 } |
| 118 NOTREACHED(); |
| 119 return 0; |
| 120 } |
| 121 |
110 void SetSeparators(const std::set<int>& separators) { | 122 void SetSeparators(const std::set<int>& separators) { |
111 separators_ = separators; | 123 separators_ = separators; |
112 } | 124 } |
113 | 125 |
114 private: | 126 private: |
115 std::set<int> separators_; | 127 std::set<int> separators_; |
116 | 128 |
117 DISALLOW_COPY_AND_ASSIGN(TestComboboxModel); | 129 DISALLOW_COPY_AND_ASSIGN(TestComboboxModel); |
118 }; | 130 }; |
119 | 131 |
(...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
193 class ComboboxTest : public ViewsTestBase { | 205 class ComboboxTest : public ViewsTestBase { |
194 public: | 206 public: |
195 ComboboxTest() : widget_(NULL), combobox_(NULL) {} | 207 ComboboxTest() : widget_(NULL), combobox_(NULL) {} |
196 | 208 |
197 virtual void TearDown() OVERRIDE { | 209 virtual void TearDown() OVERRIDE { |
198 if (widget_) | 210 if (widget_) |
199 widget_->Close(); | 211 widget_->Close(); |
200 ViewsTestBase::TearDown(); | 212 ViewsTestBase::TearDown(); |
201 } | 213 } |
202 | 214 |
203 void InitCombobox() { | 215 void InitCombobox(const std::set<int>* separators) { |
204 model_.reset(new TestComboboxModel()); | 216 model_.reset(new TestComboboxModel()); |
205 | 217 |
| 218 if (separators) |
| 219 model_->SetSeparators(*separators); |
| 220 |
206 ASSERT_FALSE(combobox_); | 221 ASSERT_FALSE(combobox_); |
207 combobox_ = new TestCombobox(model_.get()); | 222 combobox_ = new TestCombobox(model_.get()); |
208 combobox_->set_id(1); | 223 combobox_->set_id(1); |
209 | 224 |
210 widget_ = new Widget; | 225 widget_ = new Widget; |
211 Widget::InitParams params = CreateParams(Widget::InitParams::TYPE_POPUP); | 226 Widget::InitParams params = CreateParams(Widget::InitParams::TYPE_POPUP); |
212 params.bounds = gfx::Rect(200, 200, 200, 200); | 227 params.bounds = gfx::Rect(200, 200, 200, 200); |
213 widget_->Init(params); | 228 widget_->Init(params); |
214 View* container = new View(); | 229 View* container = new View(); |
215 widget_->SetContentsView(container); | 230 widget_->SetContentsView(container); |
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
255 Widget* widget_; | 270 Widget* widget_; |
256 | 271 |
257 // |combobox_| will be allocated InitCombobox() and then owned by |widget_|. | 272 // |combobox_| will be allocated InitCombobox() and then owned by |widget_|. |
258 TestCombobox* combobox_; | 273 TestCombobox* combobox_; |
259 | 274 |
260 // Combobox does not take ownership of the model, hence it needs to be scoped. | 275 // Combobox does not take ownership of the model, hence it needs to be scoped. |
261 scoped_ptr<TestComboboxModel> model_; | 276 scoped_ptr<TestComboboxModel> model_; |
262 }; | 277 }; |
263 | 278 |
264 TEST_F(ComboboxTest, KeyTest) { | 279 TEST_F(ComboboxTest, KeyTest) { |
265 InitCombobox(); | 280 InitCombobox(NULL); |
266 SendKeyEvent(ui::VKEY_END); | 281 SendKeyEvent(ui::VKEY_END); |
267 EXPECT_EQ(combobox_->selected_index() + 1, model_->GetItemCount()); | 282 EXPECT_EQ(combobox_->selected_index() + 1, model_->GetItemCount()); |
268 SendKeyEvent(ui::VKEY_HOME); | 283 SendKeyEvent(ui::VKEY_HOME); |
269 EXPECT_EQ(combobox_->selected_index(), 0); | 284 EXPECT_EQ(combobox_->selected_index(), 0); |
270 SendKeyEvent(ui::VKEY_DOWN); | 285 SendKeyEvent(ui::VKEY_DOWN); |
271 SendKeyEvent(ui::VKEY_DOWN); | 286 SendKeyEvent(ui::VKEY_DOWN); |
272 EXPECT_EQ(combobox_->selected_index(), 2); | 287 EXPECT_EQ(combobox_->selected_index(), 2); |
273 SendKeyEvent(ui::VKEY_RIGHT); | 288 SendKeyEvent(ui::VKEY_RIGHT); |
274 EXPECT_EQ(combobox_->selected_index(), 2); | 289 EXPECT_EQ(combobox_->selected_index(), 2); |
275 SendKeyEvent(ui::VKEY_LEFT); | 290 SendKeyEvent(ui::VKEY_LEFT); |
(...skipping 21 matching lines...) Expand all Loading... |
297 widget_->Init(params); | 312 widget_->Init(params); |
298 View* container = new View(); | 313 View* container = new View(); |
299 widget_->SetContentsView(container); | 314 widget_->SetContentsView(container); |
300 container->AddChildView(combobox_); | 315 container->AddChildView(combobox_); |
301 EXPECT_FALSE(combobox_->enabled()); | 316 EXPECT_FALSE(combobox_->enabled()); |
302 } | 317 } |
303 | 318 |
304 // Verifies that we don't select a separator line in combobox when navigating | 319 // Verifies that we don't select a separator line in combobox when navigating |
305 // through keyboard. | 320 // through keyboard. |
306 TEST_F(ComboboxTest, SkipSeparatorSimple) { | 321 TEST_F(ComboboxTest, SkipSeparatorSimple) { |
307 InitCombobox(); | |
308 std::set<int> separators; | 322 std::set<int> separators; |
309 separators.insert(2); | 323 separators.insert(2); |
310 model_->SetSeparators(separators); | 324 InitCombobox(&separators); |
311 EXPECT_EQ(0, combobox_->selected_index()); | 325 EXPECT_EQ(0, combobox_->selected_index()); |
312 SendKeyEvent(ui::VKEY_DOWN); | 326 SendKeyEvent(ui::VKEY_DOWN); |
313 EXPECT_EQ(1, combobox_->selected_index()); | 327 EXPECT_EQ(1, combobox_->selected_index()); |
314 SendKeyEvent(ui::VKEY_DOWN); | 328 SendKeyEvent(ui::VKEY_DOWN); |
315 EXPECT_EQ(3, combobox_->selected_index()); | 329 EXPECT_EQ(3, combobox_->selected_index()); |
316 SendKeyEvent(ui::VKEY_UP); | 330 SendKeyEvent(ui::VKEY_UP); |
317 EXPECT_EQ(1, combobox_->selected_index()); | 331 EXPECT_EQ(1, combobox_->selected_index()); |
318 SendKeyEvent(ui::VKEY_HOME); | 332 SendKeyEvent(ui::VKEY_HOME); |
319 EXPECT_EQ(0, combobox_->selected_index()); | 333 EXPECT_EQ(0, combobox_->selected_index()); |
320 SendKeyEvent(ui::VKEY_PRIOR); | 334 SendKeyEvent(ui::VKEY_PRIOR); |
321 EXPECT_EQ(0, combobox_->selected_index()); | 335 EXPECT_EQ(0, combobox_->selected_index()); |
322 SendKeyEvent(ui::VKEY_END); | 336 SendKeyEvent(ui::VKEY_END); |
323 EXPECT_EQ(9, combobox_->selected_index()); | 337 EXPECT_EQ(9, combobox_->selected_index()); |
324 } | 338 } |
325 | 339 |
326 // Verifies that we never select the separator that is in the beginning of the | 340 // Verifies that we never select the separator that is in the beginning of the |
327 // combobox list when navigating through keyboard. | 341 // combobox list when navigating through keyboard. |
328 TEST_F(ComboboxTest, SkipSeparatorBeginning) { | 342 TEST_F(ComboboxTest, SkipSeparatorBeginning) { |
329 InitCombobox(); | |
330 std::set<int> separators; | 343 std::set<int> separators; |
331 separators.insert(0); | 344 separators.insert(0); |
332 model_->SetSeparators(separators); | 345 InitCombobox(&separators); |
333 EXPECT_EQ(0, combobox_->selected_index()); | |
334 SendKeyEvent(ui::VKEY_DOWN); | |
335 EXPECT_EQ(1, combobox_->selected_index()); | 346 EXPECT_EQ(1, combobox_->selected_index()); |
336 SendKeyEvent(ui::VKEY_DOWN); | 347 SendKeyEvent(ui::VKEY_DOWN); |
337 EXPECT_EQ(2, combobox_->selected_index()); | 348 EXPECT_EQ(2, combobox_->selected_index()); |
| 349 SendKeyEvent(ui::VKEY_DOWN); |
| 350 EXPECT_EQ(3, combobox_->selected_index()); |
338 SendKeyEvent(ui::VKEY_UP); | 351 SendKeyEvent(ui::VKEY_UP); |
339 EXPECT_EQ(1, combobox_->selected_index()); | 352 EXPECT_EQ(2, combobox_->selected_index()); |
340 SendKeyEvent(ui::VKEY_HOME); | 353 SendKeyEvent(ui::VKEY_HOME); |
341 EXPECT_EQ(1, combobox_->selected_index()); | 354 EXPECT_EQ(1, combobox_->selected_index()); |
342 SendKeyEvent(ui::VKEY_PRIOR); | 355 SendKeyEvent(ui::VKEY_PRIOR); |
343 EXPECT_EQ(1, combobox_->selected_index()); | 356 EXPECT_EQ(1, combobox_->selected_index()); |
344 SendKeyEvent(ui::VKEY_END); | 357 SendKeyEvent(ui::VKEY_END); |
345 EXPECT_EQ(9, combobox_->selected_index()); | 358 EXPECT_EQ(9, combobox_->selected_index()); |
346 } | 359 } |
347 | 360 |
348 // Verifies that we never select the separator that is in the end of the | 361 // Verifies that we never select the separator that is in the end of the |
349 // combobox list when navigating through keyboard. | 362 // combobox list when navigating through keyboard. |
350 TEST_F(ComboboxTest, SkipSeparatorEnd) { | 363 TEST_F(ComboboxTest, SkipSeparatorEnd) { |
351 InitCombobox(); | |
352 std::set<int> separators; | 364 std::set<int> separators; |
353 separators.insert(model_->GetItemCount() - 1); | 365 separators.insert(TestComboboxModel::kItemCount - 1); |
354 model_->SetSeparators(separators); | 366 InitCombobox(&separators); |
355 combobox_->SetSelectedIndex(8); | 367 combobox_->SetSelectedIndex(8); |
356 SendKeyEvent(ui::VKEY_DOWN); | 368 SendKeyEvent(ui::VKEY_DOWN); |
357 EXPECT_EQ(8, combobox_->selected_index()); | 369 EXPECT_EQ(8, combobox_->selected_index()); |
358 SendKeyEvent(ui::VKEY_UP); | 370 SendKeyEvent(ui::VKEY_UP); |
359 EXPECT_EQ(7, combobox_->selected_index()); | 371 EXPECT_EQ(7, combobox_->selected_index()); |
360 SendKeyEvent(ui::VKEY_END); | 372 SendKeyEvent(ui::VKEY_END); |
361 EXPECT_EQ(8, combobox_->selected_index()); | 373 EXPECT_EQ(8, combobox_->selected_index()); |
362 } | 374 } |
363 | 375 |
364 // Verifies that we never select any of the adjacent separators (multiple | 376 // Verifies that we never select any of the adjacent separators (multiple |
365 // consecutive) that appear in the beginning of the combobox list when | 377 // consecutive) that appear in the beginning of the combobox list when |
366 // navigating through keyboard. | 378 // navigating through keyboard. |
367 TEST_F(ComboboxTest, SkipMultipleSeparatorsAtBeginning) { | 379 TEST_F(ComboboxTest, SkipMultipleSeparatorsAtBeginning) { |
368 InitCombobox(); | |
369 std::set<int> separators; | 380 std::set<int> separators; |
370 separators.insert(0); | 381 separators.insert(0); |
371 separators.insert(1); | 382 separators.insert(1); |
372 separators.insert(2); | 383 separators.insert(2); |
373 model_->SetSeparators(separators); | 384 InitCombobox(&separators); |
374 EXPECT_EQ(0, combobox_->selected_index()); | 385 EXPECT_EQ(3, combobox_->selected_index()); |
375 SendKeyEvent(ui::VKEY_DOWN); | 386 SendKeyEvent(ui::VKEY_DOWN); |
376 EXPECT_EQ(3, combobox_->selected_index()); | 387 EXPECT_EQ(4, combobox_->selected_index()); |
377 SendKeyEvent(ui::VKEY_UP); | 388 SendKeyEvent(ui::VKEY_UP); |
378 EXPECT_EQ(3, combobox_->selected_index()); | 389 EXPECT_EQ(3, combobox_->selected_index()); |
379 SendKeyEvent(ui::VKEY_NEXT); | 390 SendKeyEvent(ui::VKEY_NEXT); |
380 EXPECT_EQ(9, combobox_->selected_index()); | 391 EXPECT_EQ(9, combobox_->selected_index()); |
381 SendKeyEvent(ui::VKEY_HOME); | 392 SendKeyEvent(ui::VKEY_HOME); |
382 EXPECT_EQ(3, combobox_->selected_index()); | 393 EXPECT_EQ(3, combobox_->selected_index()); |
383 SendKeyEvent(ui::VKEY_END); | 394 SendKeyEvent(ui::VKEY_END); |
384 EXPECT_EQ(9, combobox_->selected_index()); | 395 EXPECT_EQ(9, combobox_->selected_index()); |
385 SendKeyEvent(ui::VKEY_PRIOR); | 396 SendKeyEvent(ui::VKEY_PRIOR); |
386 EXPECT_EQ(3, combobox_->selected_index()); | 397 EXPECT_EQ(3, combobox_->selected_index()); |
387 } | 398 } |
388 | 399 |
389 // Verifies that we never select any of the adjacent separators (multiple | 400 // Verifies that we never select any of the adjacent separators (multiple |
390 // consecutive) that appear in the middle of the combobox list when navigating | 401 // consecutive) that appear in the middle of the combobox list when navigating |
391 // through keyboard. | 402 // through keyboard. |
392 TEST_F(ComboboxTest, SkipMultipleAdjacentSeparatorsAtMiddle) { | 403 TEST_F(ComboboxTest, SkipMultipleAdjacentSeparatorsAtMiddle) { |
393 InitCombobox(); | |
394 std::set<int> separators; | 404 std::set<int> separators; |
395 separators.insert(4); | 405 separators.insert(4); |
396 separators.insert(5); | 406 separators.insert(5); |
397 separators.insert(6); | 407 separators.insert(6); |
398 model_->SetSeparators(separators); | 408 InitCombobox(&separators); |
399 combobox_->SetSelectedIndex(3); | 409 combobox_->SetSelectedIndex(3); |
400 SendKeyEvent(ui::VKEY_DOWN); | 410 SendKeyEvent(ui::VKEY_DOWN); |
401 EXPECT_EQ(7, combobox_->selected_index()); | 411 EXPECT_EQ(7, combobox_->selected_index()); |
402 SendKeyEvent(ui::VKEY_UP); | 412 SendKeyEvent(ui::VKEY_UP); |
403 EXPECT_EQ(3, combobox_->selected_index()); | 413 EXPECT_EQ(3, combobox_->selected_index()); |
404 } | 414 } |
405 | 415 |
406 // Verifies that we never select any of the adjacent separators (multiple | 416 // Verifies that we never select any of the adjacent separators (multiple |
407 // consecutive) that appear in the end of the combobox list when navigating | 417 // consecutive) that appear in the end of the combobox list when navigating |
408 // through keyboard. | 418 // through keyboard. |
409 TEST_F(ComboboxTest, SkipMultipleSeparatorsAtEnd) { | 419 TEST_F(ComboboxTest, SkipMultipleSeparatorsAtEnd) { |
410 InitCombobox(); | |
411 std::set<int> separators; | 420 std::set<int> separators; |
412 separators.insert(7); | 421 separators.insert(7); |
413 separators.insert(8); | 422 separators.insert(8); |
414 separators.insert(9); | 423 separators.insert(9); |
415 model_->SetSeparators(separators); | 424 InitCombobox(&separators); |
416 combobox_->SetSelectedIndex(6); | 425 combobox_->SetSelectedIndex(6); |
417 SendKeyEvent(ui::VKEY_DOWN); | 426 SendKeyEvent(ui::VKEY_DOWN); |
418 EXPECT_EQ(6, combobox_->selected_index()); | 427 EXPECT_EQ(6, combobox_->selected_index()); |
419 SendKeyEvent(ui::VKEY_UP); | 428 SendKeyEvent(ui::VKEY_UP); |
420 EXPECT_EQ(5, combobox_->selected_index()); | 429 EXPECT_EQ(5, combobox_->selected_index()); |
421 SendKeyEvent(ui::VKEY_HOME); | 430 SendKeyEvent(ui::VKEY_HOME); |
422 EXPECT_EQ(0, combobox_->selected_index()); | 431 EXPECT_EQ(0, combobox_->selected_index()); |
423 SendKeyEvent(ui::VKEY_NEXT); | 432 SendKeyEvent(ui::VKEY_NEXT); |
424 EXPECT_EQ(6, combobox_->selected_index()); | 433 EXPECT_EQ(6, combobox_->selected_index()); |
425 SendKeyEvent(ui::VKEY_PRIOR); | 434 SendKeyEvent(ui::VKEY_PRIOR); |
426 EXPECT_EQ(0, combobox_->selected_index()); | 435 EXPECT_EQ(0, combobox_->selected_index()); |
427 SendKeyEvent(ui::VKEY_END); | 436 SendKeyEvent(ui::VKEY_END); |
428 EXPECT_EQ(6, combobox_->selected_index()); | 437 EXPECT_EQ(6, combobox_->selected_index()); |
429 } | 438 } |
430 | 439 |
431 TEST_F(ComboboxTest, GetTextForRowTest) { | 440 TEST_F(ComboboxTest, GetTextForRowTest) { |
432 InitCombobox(); | |
433 std::set<int> separators; | 441 std::set<int> separators; |
434 separators.insert(0); | 442 separators.insert(0); |
435 separators.insert(1); | 443 separators.insert(1); |
436 separators.insert(9); | 444 separators.insert(9); |
437 model_->SetSeparators(separators); | 445 InitCombobox(&separators); |
438 for (int i = 0; i < combobox_->GetRowCount(); ++i) { | 446 for (int i = 0; i < combobox_->GetRowCount(); ++i) { |
439 if (separators.count(i) != 0) { | 447 if (separators.count(i) != 0) { |
440 EXPECT_TRUE(combobox_->GetTextForRow(i).empty()) << i; | 448 EXPECT_TRUE(combobox_->GetTextForRow(i).empty()) << i; |
441 } else { | 449 } else { |
442 EXPECT_EQ(ASCIIToUTF16(i % 2 == 0 ? "PEANUT BUTTER" : "JELLY"), | 450 EXPECT_EQ(ASCIIToUTF16(i % 2 == 0 ? "PEANUT BUTTER" : "JELLY"), |
443 combobox_->GetTextForRow(i)) << i; | 451 combobox_->GetTextForRow(i)) << i; |
444 } | 452 } |
445 } | 453 } |
446 } | 454 } |
447 | 455 |
448 // Verifies selecting the first matching value (and returning whether found). | 456 // Verifies selecting the first matching value (and returning whether found). |
449 TEST_F(ComboboxTest, SelectValue) { | 457 TEST_F(ComboboxTest, SelectValue) { |
450 InitCombobox(); | 458 InitCombobox(NULL); |
451 ASSERT_EQ(model_->GetDefaultIndex(), combobox_->selected_index()); | 459 ASSERT_EQ(model_->GetDefaultIndex(), combobox_->selected_index()); |
452 EXPECT_TRUE(combobox_->SelectValue(ASCIIToUTF16("PEANUT BUTTER"))); | 460 EXPECT_TRUE(combobox_->SelectValue(ASCIIToUTF16("PEANUT BUTTER"))); |
453 EXPECT_EQ(0, combobox_->selected_index()); | 461 EXPECT_EQ(0, combobox_->selected_index()); |
454 EXPECT_TRUE(combobox_->SelectValue(ASCIIToUTF16("JELLY"))); | 462 EXPECT_TRUE(combobox_->SelectValue(ASCIIToUTF16("JELLY"))); |
455 EXPECT_EQ(1, combobox_->selected_index()); | 463 EXPECT_EQ(1, combobox_->selected_index()); |
456 EXPECT_FALSE(combobox_->SelectValue(ASCIIToUTF16("BANANAS"))); | 464 EXPECT_FALSE(combobox_->SelectValue(ASCIIToUTF16("BANANAS"))); |
457 EXPECT_EQ(1, combobox_->selected_index()); | 465 EXPECT_EQ(1, combobox_->selected_index()); |
458 | 466 |
459 // With the action style, the selected index is always 0. | 467 // With the action style, the selected index is always 0. |
460 combobox_->SetStyle(Combobox::STYLE_ACTION); | 468 combobox_->SetStyle(Combobox::STYLE_ACTION); |
461 EXPECT_FALSE(combobox_->SelectValue(ASCIIToUTF16("PEANUT BUTTER"))); | 469 EXPECT_FALSE(combobox_->SelectValue(ASCIIToUTF16("PEANUT BUTTER"))); |
462 EXPECT_EQ(0, combobox_->selected_index()); | 470 EXPECT_EQ(0, combobox_->selected_index()); |
463 EXPECT_FALSE(combobox_->SelectValue(ASCIIToUTF16("JELLY"))); | 471 EXPECT_FALSE(combobox_->SelectValue(ASCIIToUTF16("JELLY"))); |
464 EXPECT_EQ(0, combobox_->selected_index()); | 472 EXPECT_EQ(0, combobox_->selected_index()); |
465 EXPECT_FALSE(combobox_->SelectValue(ASCIIToUTF16("BANANAS"))); | 473 EXPECT_FALSE(combobox_->SelectValue(ASCIIToUTF16("BANANAS"))); |
466 EXPECT_EQ(0, combobox_->selected_index()); | 474 EXPECT_EQ(0, combobox_->selected_index()); |
467 } | 475 } |
468 | 476 |
469 TEST_F(ComboboxTest, SelectIndexActionStyle) { | 477 TEST_F(ComboboxTest, SelectIndexActionStyle) { |
470 InitCombobox(); | 478 InitCombobox(NULL); |
471 | 479 |
472 // With the action style, the selected index is always 0. | 480 // With the action style, the selected index is always 0. |
473 combobox_->SetStyle(Combobox::STYLE_ACTION); | 481 combobox_->SetStyle(Combobox::STYLE_ACTION); |
474 combobox_->SetSelectedIndex(1); | 482 combobox_->SetSelectedIndex(1); |
475 EXPECT_EQ(0, combobox_->selected_index()); | 483 EXPECT_EQ(0, combobox_->selected_index()); |
476 combobox_->SetSelectedIndex(2); | 484 combobox_->SetSelectedIndex(2); |
477 EXPECT_EQ(0, combobox_->selected_index()); | 485 EXPECT_EQ(0, combobox_->selected_index()); |
478 combobox_->SetSelectedIndex(3); | 486 combobox_->SetSelectedIndex(3); |
479 EXPECT_EQ(0, combobox_->selected_index()); | 487 EXPECT_EQ(0, combobox_->selected_index()); |
480 } | 488 } |
(...skipping 12 matching lines...) Expand all Loading... |
493 // |combobox| will be deleted on change. | 501 // |combobox| will be deleted on change. |
494 combobox = new TestCombobox(&model); | 502 combobox = new TestCombobox(&model); |
495 evil_listener.reset(new EvilListener()); | 503 evil_listener.reset(new EvilListener()); |
496 combobox->set_listener(evil_listener.get()); | 504 combobox->set_listener(evil_listener.get()); |
497 combobox->SetStyle(Combobox::STYLE_ACTION); | 505 combobox->SetStyle(Combobox::STYLE_ACTION); |
498 ASSERT_NO_FATAL_FAILURE(combobox->ExecuteCommand(2)); | 506 ASSERT_NO_FATAL_FAILURE(combobox->ExecuteCommand(2)); |
499 EXPECT_TRUE(evil_listener->deleted()); | 507 EXPECT_TRUE(evil_listener->deleted()); |
500 } | 508 } |
501 | 509 |
502 TEST_F(ComboboxTest, Click) { | 510 TEST_F(ComboboxTest, Click) { |
503 InitCombobox(); | 511 InitCombobox(NULL); |
504 | 512 |
505 TestComboboxListener listener; | 513 TestComboboxListener listener; |
506 combobox_->set_listener(&listener); | 514 combobox_->set_listener(&listener); |
507 | 515 |
508 combobox_->Layout(); | 516 combobox_->Layout(); |
509 | 517 |
510 // Click the left side. The menu is shown. | 518 // Click the left side. The menu is shown. |
511 TestMenuRunnerHandler* test_menu_runner_handler = new TestMenuRunnerHandler(); | 519 TestMenuRunnerHandler* test_menu_runner_handler = new TestMenuRunnerHandler(); |
512 scoped_ptr<MenuRunnerHandler> menu_runner_handler(test_menu_runner_handler); | 520 scoped_ptr<MenuRunnerHandler> menu_runner_handler(test_menu_runner_handler); |
513 test::MenuRunnerTestAPI test_api( | 521 test::MenuRunnerTestAPI test_api( |
514 combobox_->dropdown_list_menu_runner_.get()); | 522 combobox_->dropdown_list_menu_runner_.get()); |
515 test_api.SetMenuRunnerHandler(menu_runner_handler.Pass()); | 523 test_api.SetMenuRunnerHandler(menu_runner_handler.Pass()); |
516 PerformClick(gfx::Point(combobox_->x() + 1, | 524 PerformClick(gfx::Point(combobox_->x() + 1, |
517 combobox_->y() + combobox_->height() / 2)); | 525 combobox_->y() + combobox_->height() / 2)); |
518 EXPECT_FALSE(listener.on_perform_action_called()); | 526 EXPECT_FALSE(listener.on_perform_action_called()); |
519 EXPECT_TRUE(test_menu_runner_handler->executed()); | 527 EXPECT_TRUE(test_menu_runner_handler->executed()); |
520 } | 528 } |
521 | 529 |
522 TEST_F(ComboboxTest, ClickButDisabled) { | 530 TEST_F(ComboboxTest, ClickButDisabled) { |
523 InitCombobox(); | 531 InitCombobox(NULL); |
524 | 532 |
525 TestComboboxListener listener; | 533 TestComboboxListener listener; |
526 combobox_->set_listener(&listener); | 534 combobox_->set_listener(&listener); |
527 | 535 |
528 combobox_->Layout(); | 536 combobox_->Layout(); |
529 combobox_->SetEnabled(false); | 537 combobox_->SetEnabled(false); |
530 | 538 |
531 // Click the left side, but nothing happens since the combobox is disabled. | 539 // Click the left side, but nothing happens since the combobox is disabled. |
532 TestMenuRunnerHandler* test_menu_runner_handler = new TestMenuRunnerHandler(); | 540 TestMenuRunnerHandler* test_menu_runner_handler = new TestMenuRunnerHandler(); |
533 scoped_ptr<MenuRunnerHandler> menu_runner_handler(test_menu_runner_handler); | 541 scoped_ptr<MenuRunnerHandler> menu_runner_handler(test_menu_runner_handler); |
534 test::MenuRunnerTestAPI test_api( | 542 test::MenuRunnerTestAPI test_api( |
535 combobox_->dropdown_list_menu_runner_.get()); | 543 combobox_->dropdown_list_menu_runner_.get()); |
536 test_api.SetMenuRunnerHandler(menu_runner_handler.Pass()); | 544 test_api.SetMenuRunnerHandler(menu_runner_handler.Pass()); |
537 PerformClick(gfx::Point(combobox_->x() + 1, | 545 PerformClick(gfx::Point(combobox_->x() + 1, |
538 combobox_->y() + combobox_->height() / 2)); | 546 combobox_->y() + combobox_->height() / 2)); |
539 EXPECT_FALSE(listener.on_perform_action_called()); | 547 EXPECT_FALSE(listener.on_perform_action_called()); |
540 EXPECT_FALSE(test_menu_runner_handler->executed()); | 548 EXPECT_FALSE(test_menu_runner_handler->executed()); |
541 } | 549 } |
542 | 550 |
543 TEST_F(ComboboxTest, NotifyOnClickWithReturnKey) { | 551 TEST_F(ComboboxTest, NotifyOnClickWithReturnKey) { |
544 InitCombobox(); | 552 InitCombobox(NULL); |
545 | 553 |
546 TestComboboxListener listener; | 554 TestComboboxListener listener; |
547 combobox_->set_listener(&listener); | 555 combobox_->set_listener(&listener); |
548 | 556 |
549 // With STYLE_NORMAL, the click event is ignored. | 557 // With STYLE_NORMAL, the click event is ignored. |
550 SendKeyEvent(ui::VKEY_RETURN); | 558 SendKeyEvent(ui::VKEY_RETURN); |
551 EXPECT_FALSE(listener.on_perform_action_called()); | 559 EXPECT_FALSE(listener.on_perform_action_called()); |
552 | 560 |
553 // With STYLE_ACTION, the click event is notified. | 561 // With STYLE_ACTION, the click event is notified. |
554 combobox_->SetStyle(Combobox::STYLE_ACTION); | 562 combobox_->SetStyle(Combobox::STYLE_ACTION); |
555 SendKeyEvent(ui::VKEY_RETURN); | 563 SendKeyEvent(ui::VKEY_RETURN); |
556 EXPECT_TRUE(listener.on_perform_action_called()); | 564 EXPECT_TRUE(listener.on_perform_action_called()); |
557 EXPECT_EQ(0, listener.perform_action_index()); | 565 EXPECT_EQ(0, listener.perform_action_index()); |
558 } | 566 } |
559 | 567 |
560 TEST_F(ComboboxTest, NotifyOnClickWithSpaceKey) { | 568 TEST_F(ComboboxTest, NotifyOnClickWithSpaceKey) { |
561 InitCombobox(); | 569 InitCombobox(NULL); |
562 | 570 |
563 TestComboboxListener listener; | 571 TestComboboxListener listener; |
564 combobox_->set_listener(&listener); | 572 combobox_->set_listener(&listener); |
565 | 573 |
566 // With STYLE_NORMAL, the click event is ignored. | 574 // With STYLE_NORMAL, the click event is ignored. |
567 SendKeyEvent(ui::VKEY_SPACE); | 575 SendKeyEvent(ui::VKEY_SPACE); |
568 EXPECT_FALSE(listener.on_perform_action_called()); | 576 EXPECT_FALSE(listener.on_perform_action_called()); |
569 SendKeyEventWithType(ui::VKEY_SPACE, ui::ET_KEY_RELEASED); | 577 SendKeyEventWithType(ui::VKEY_SPACE, ui::ET_KEY_RELEASED); |
570 EXPECT_FALSE(listener.on_perform_action_called()); | 578 EXPECT_FALSE(listener.on_perform_action_called()); |
571 | 579 |
572 // With STYLE_ACTION, the click event is notified after releasing. | 580 // With STYLE_ACTION, the click event is notified after releasing. |
573 combobox_->SetStyle(Combobox::STYLE_ACTION); | 581 combobox_->SetStyle(Combobox::STYLE_ACTION); |
574 SendKeyEvent(ui::VKEY_SPACE); | 582 SendKeyEvent(ui::VKEY_SPACE); |
575 EXPECT_FALSE(listener.on_perform_action_called()); | 583 EXPECT_FALSE(listener.on_perform_action_called()); |
576 SendKeyEventWithType(ui::VKEY_SPACE, ui::ET_KEY_RELEASED); | 584 SendKeyEventWithType(ui::VKEY_SPACE, ui::ET_KEY_RELEASED); |
577 EXPECT_TRUE(listener.on_perform_action_called()); | 585 EXPECT_TRUE(listener.on_perform_action_called()); |
578 EXPECT_EQ(0, listener.perform_action_index()); | 586 EXPECT_EQ(0, listener.perform_action_index()); |
579 } | 587 } |
580 | 588 |
581 TEST_F(ComboboxTest, NotifyOnClickWithMouse) { | 589 TEST_F(ComboboxTest, NotifyOnClickWithMouse) { |
582 InitCombobox(); | 590 InitCombobox(NULL); |
583 | 591 |
584 TestComboboxListener listener; | 592 TestComboboxListener listener; |
585 combobox_->set_listener(&listener); | 593 combobox_->set_listener(&listener); |
586 | 594 |
587 combobox_->SetStyle(Combobox::STYLE_ACTION); | 595 combobox_->SetStyle(Combobox::STYLE_ACTION); |
588 combobox_->Layout(); | 596 combobox_->Layout(); |
589 | 597 |
590 // Click the right side (arrow button). The menu is shown. | 598 // Click the right side (arrow button). The menu is shown. |
591 TestMenuRunnerHandler* test_menu_runner_handler = new TestMenuRunnerHandler(); | 599 TestMenuRunnerHandler* test_menu_runner_handler = new TestMenuRunnerHandler(); |
592 scoped_ptr<MenuRunnerHandler> menu_runner_handler(test_menu_runner_handler); | 600 scoped_ptr<MenuRunnerHandler> menu_runner_handler(test_menu_runner_handler); |
(...skipping 13 matching lines...) Expand all Loading... |
606 new test::MenuRunnerTestAPI(combobox_->dropdown_list_menu_runner_.get())); | 614 new test::MenuRunnerTestAPI(combobox_->dropdown_list_menu_runner_.get())); |
607 test_api->SetMenuRunnerHandler(menu_runner_handler.Pass()); | 615 test_api->SetMenuRunnerHandler(menu_runner_handler.Pass()); |
608 PerformClick(gfx::Point(combobox_->x() + 1, | 616 PerformClick(gfx::Point(combobox_->x() + 1, |
609 combobox_->y() + combobox_->height() / 2)); | 617 combobox_->y() + combobox_->height() / 2)); |
610 EXPECT_TRUE(listener.on_perform_action_called()); | 618 EXPECT_TRUE(listener.on_perform_action_called()); |
611 EXPECT_FALSE(test_menu_runner_handler->executed()); | 619 EXPECT_FALSE(test_menu_runner_handler->executed()); |
612 EXPECT_EQ(0, listener.perform_action_index()); | 620 EXPECT_EQ(0, listener.perform_action_index()); |
613 } | 621 } |
614 | 622 |
615 TEST_F(ComboboxTest, ConsumingPressKeyEvents) { | 623 TEST_F(ComboboxTest, ConsumingPressKeyEvents) { |
616 InitCombobox(); | 624 InitCombobox(NULL); |
617 | 625 |
618 EXPECT_FALSE(combobox_->OnKeyPressed( | 626 EXPECT_FALSE(combobox_->OnKeyPressed( |
619 ui::KeyEvent(ui::ET_KEY_PRESSED, ui::VKEY_RETURN, 0, false))); | 627 ui::KeyEvent(ui::ET_KEY_PRESSED, ui::VKEY_RETURN, 0, false))); |
620 EXPECT_FALSE(combobox_->OnKeyPressed( | 628 EXPECT_FALSE(combobox_->OnKeyPressed( |
621 ui::KeyEvent(ui::ET_KEY_PRESSED, ui::VKEY_SPACE, 0, false))); | 629 ui::KeyEvent(ui::ET_KEY_PRESSED, ui::VKEY_SPACE, 0, false))); |
622 | 630 |
623 // When the combobox's style is STYLE_ACTION, pressing events of a space key | 631 // When the combobox's style is STYLE_ACTION, pressing events of a space key |
624 // or an enter key will be consumed. | 632 // or an enter key will be consumed. |
625 combobox_->SetStyle(Combobox::STYLE_ACTION); | 633 combobox_->SetStyle(Combobox::STYLE_ACTION); |
626 EXPECT_TRUE(combobox_->OnKeyPressed( | 634 EXPECT_TRUE(combobox_->OnKeyPressed( |
(...skipping 30 matching lines...) Expand all Loading... |
657 combobox.SetStyle(Combobox::STYLE_NORMAL); | 665 combobox.SetStyle(Combobox::STYLE_NORMAL); |
658 EXPECT_EQ(long_item_width, combobox.content_size_.width()); | 666 EXPECT_EQ(long_item_width, combobox.content_size_.width()); |
659 | 667 |
660 // When the style is STYLE_ACTION, the width will fit with the first items' | 668 // When the style is STYLE_ACTION, the width will fit with the first items' |
661 // width. | 669 // width. |
662 combobox.SetStyle(Combobox::STYLE_ACTION); | 670 combobox.SetStyle(Combobox::STYLE_ACTION); |
663 EXPECT_EQ(short_item_width, combobox.content_size_.width()); | 671 EXPECT_EQ(short_item_width, combobox.content_size_.width()); |
664 } | 672 } |
665 | 673 |
666 TEST_F(ComboboxTest, TypingPrefixNotifiesListener) { | 674 TEST_F(ComboboxTest, TypingPrefixNotifiesListener) { |
667 InitCombobox(); | 675 InitCombobox(NULL); |
668 | 676 |
669 TestComboboxListener listener; | 677 TestComboboxListener listener; |
670 combobox_->set_listener(&listener); | 678 combobox_->set_listener(&listener); |
671 | 679 |
672 // Type the first character of the second menu item ("JELLY"). | 680 // Type the first character of the second menu item ("JELLY"). |
673 combobox_->GetTextInputClient()->InsertChar('J', ui::EF_NONE); | 681 combobox_->GetTextInputClient()->InsertChar('J', ui::EF_NONE); |
674 EXPECT_EQ(1, listener.actions_performed()); | 682 EXPECT_EQ(1, listener.actions_performed()); |
675 EXPECT_EQ(1, listener.perform_action_index()); | 683 EXPECT_EQ(1, listener.perform_action_index()); |
676 | 684 |
677 // Type the second character of "JELLY", item shouldn't change and | 685 // Type the second character of "JELLY", item shouldn't change and |
678 // OnPerformAction() shouldn't be re-called. | 686 // OnPerformAction() shouldn't be re-called. |
679 combobox_->GetTextInputClient()->InsertChar('E', ui::EF_NONE); | 687 combobox_->GetTextInputClient()->InsertChar('E', ui::EF_NONE); |
680 EXPECT_EQ(1, listener.actions_performed()); | 688 EXPECT_EQ(1, listener.actions_performed()); |
681 EXPECT_EQ(1, listener.perform_action_index()); | 689 EXPECT_EQ(1, listener.perform_action_index()); |
682 | 690 |
683 // Clears the typed text. | 691 // Clears the typed text. |
684 combobox_->OnBlur(); | 692 combobox_->OnBlur(); |
685 | 693 |
686 // Type the first character of "PEANUT BUTTER", which should change the | 694 // Type the first character of "PEANUT BUTTER", which should change the |
687 // selected index and perform an action. | 695 // selected index and perform an action. |
688 combobox_->GetTextInputClient()->InsertChar('P', ui::EF_NONE); | 696 combobox_->GetTextInputClient()->InsertChar('P', ui::EF_NONE); |
689 EXPECT_EQ(2, listener.actions_performed()); | 697 EXPECT_EQ(2, listener.actions_performed()); |
690 EXPECT_EQ(2, listener.perform_action_index()); | 698 EXPECT_EQ(2, listener.perform_action_index()); |
691 } | 699 } |
692 | 700 |
693 } // namespace views | 701 } // namespace views |
OLD | NEW |