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" |
| (...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 = NULL) { |
|
sadrul
2014/07/10 15:24:35
I believe default parameters are not allowed. Just
Andre
2014/07/10 17:44:27
Done.
| |
| 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 81 matching lines...) Expand 10 before | Expand all | Expand 10 after 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 |
| (...skipping 236 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 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 |