Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(46)

Side by Side Diff: ui/views/controls/combobox/combobox_unittest.cc

Issue 141523005: Combobox: Rename styles to STYLE_NORMAL and STYLE_ACTION and modify behaviors (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Rename OnSelectedChanged -> OnPerformAction Created 6 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 99 matching lines...) Expand 10 before | Expand all | Expand 10 after
110 void SetSeparators(const std::set<int>& separators) { 110 void SetSeparators(const std::set<int>& separators) {
111 separators_ = separators; 111 separators_ = separators;
112 } 112 }
113 113
114 private: 114 private:
115 std::set<int> separators_; 115 std::set<int> separators_;
116 116
117 DISALLOW_COPY_AND_ASSIGN(TestComboboxModel); 117 DISALLOW_COPY_AND_ASSIGN(TestComboboxModel);
118 }; 118 };
119 119
120 // A combobox model which refers to a vector.
121 class VectorComboboxModel : public ui::ComboboxModel {
122 public:
123 VectorComboboxModel(std::vector<std::string>* values)
124 : values_(values) {
125 }
126 virtual ~VectorComboboxModel() {}
127
128 // ui::ComboboxModel:
129 virtual int GetItemCount() const OVERRIDE {
130 return values_->size();
131 }
132 virtual base::string16 GetItemAt(int index) OVERRIDE {
133 return ASCIIToUTF16(values_->at(index));
134 }
135 virtual bool IsItemSeparatorAt(int index) OVERRIDE {
136 return false;
137 }
138
139 private:
140 std::vector<std::string>* values_;
141 };
142
120 class EvilListener : public ComboboxListener { 143 class EvilListener : public ComboboxListener {
121 public: 144 public:
122 EvilListener() : deleted_(false) {}; 145 EvilListener() : deleted_(false) {};
123 virtual ~EvilListener() {}; 146 virtual ~EvilListener() {};
124 147
125 // ComboboxListener: 148 // ComboboxListener:
126 virtual void OnSelectedIndexChanged(Combobox* combobox) OVERRIDE { 149 virtual void OnPerformAction(Combobox* combobox) OVERRIDE {
127 delete combobox; 150 delete combobox;
128 deleted_ = true; 151 deleted_ = true;
129 } 152 }
130 153
131 bool deleted() const { return deleted_; } 154 bool deleted() const { return deleted_; }
132 155
133 private: 156 private:
134 bool deleted_; 157 bool deleted_;
135 158
136 DISALLOW_COPY_AND_ASSIGN(EvilListener); 159 DISALLOW_COPY_AND_ASSIGN(EvilListener);
137 }; 160 };
138 161
139 class TestComboboxListener : public views::ComboboxListener { 162 class TestComboboxListener : public views::ComboboxListener {
140 public: 163 public:
141 TestComboboxListener() 164 TestComboboxListener()
142 : on_selected_index_changed_called_(false), 165 : perform_action_index_(-1),
143 on_combobox_text_button_clicked_called_(false) { 166 on_perform_action_called_(false) {
144 } 167 }
145 virtual ~TestComboboxListener() {} 168 virtual ~TestComboboxListener() {}
146 169
147 virtual void OnSelectedIndexChanged(views::Combobox* combobox) OVERRIDE { 170 virtual void OnPerformAction(views::Combobox* combobox) OVERRIDE {
148 on_selected_index_changed_called_ = true; 171 perform_action_index_ = combobox->selected_index();
172 on_perform_action_called_ = true;
149 } 173 }
150 174
151 virtual void OnComboboxTextButtonClicked(views::Combobox* combobox) OVERRIDE { 175 int perform_action_index() const {
152 on_combobox_text_button_clicked_called_ = true; 176 return perform_action_index_;
153 } 177 }
154 178
155 bool on_selected_index_changed_called() const { 179 bool on_perform_action_called() const {
156 return on_selected_index_changed_called_; 180 return on_perform_action_called_;
157 }
158
159 bool on_combobox_text_button_clicked_called() const {
160 return on_combobox_text_button_clicked_called_;
161 } 181 }
162 182
163 private: 183 private:
164 bool on_selected_index_changed_called_; 184 int perform_action_index_;
165 bool on_combobox_text_button_clicked_called_; 185 bool on_perform_action_called_;
166 186
167 private: 187 private:
168 DISALLOW_COPY_AND_ASSIGN(TestComboboxListener); 188 DISALLOW_COPY_AND_ASSIGN(TestComboboxListener);
169 }; 189 };
170 190
171 } // namespace 191 } // namespace
172 192
173 class ComboboxTest : public ViewsTestBase { 193 class ComboboxTest : public ViewsTestBase {
174 public: 194 public:
175 ComboboxTest() : widget_(NULL), combobox_(NULL), input_method_(NULL) {} 195 ComboboxTest() : widget_(NULL), combobox_(NULL), input_method_(NULL) {}
(...skipping 256 matching lines...) Expand 10 before | Expand all | Expand 10 after
432 // Verifies selecting the first matching value (and returning whether found). 452 // Verifies selecting the first matching value (and returning whether found).
433 TEST_F(ComboboxTest, SelectValue) { 453 TEST_F(ComboboxTest, SelectValue) {
434 InitCombobox(); 454 InitCombobox();
435 ASSERT_EQ(model_->GetDefaultIndex(), combobox_->selected_index()); 455 ASSERT_EQ(model_->GetDefaultIndex(), combobox_->selected_index());
436 EXPECT_TRUE(combobox_->SelectValue(ASCIIToUTF16("PEANUT BUTTER"))); 456 EXPECT_TRUE(combobox_->SelectValue(ASCIIToUTF16("PEANUT BUTTER")));
437 EXPECT_EQ(0, combobox_->selected_index()); 457 EXPECT_EQ(0, combobox_->selected_index());
438 EXPECT_TRUE(combobox_->SelectValue(ASCIIToUTF16("JELLY"))); 458 EXPECT_TRUE(combobox_->SelectValue(ASCIIToUTF16("JELLY")));
439 EXPECT_EQ(1, combobox_->selected_index()); 459 EXPECT_EQ(1, combobox_->selected_index());
440 EXPECT_FALSE(combobox_->SelectValue(ASCIIToUTF16("BANANAS"))); 460 EXPECT_FALSE(combobox_->SelectValue(ASCIIToUTF16("BANANAS")));
441 EXPECT_EQ(1, combobox_->selected_index()); 461 EXPECT_EQ(1, combobox_->selected_index());
462
463 // With the action style, the selected index is always 0.
464 combobox_->SetStyle(Combobox::STYLE_ACTION);
465 EXPECT_FALSE(combobox_->SelectValue(ASCIIToUTF16("PEANUT BUTTER")));
466 EXPECT_EQ(0, combobox_->selected_index());
467 EXPECT_FALSE(combobox_->SelectValue(ASCIIToUTF16("JELLY")));
468 EXPECT_EQ(0, combobox_->selected_index());
469 EXPECT_FALSE(combobox_->SelectValue(ASCIIToUTF16("BANANAS")));
470 EXPECT_EQ(0, combobox_->selected_index());
471 }
472
473 TEST_F(ComboboxTest, SelectIndexActionStyle) {
474 InitCombobox();
475
476 // With the action style, the selected index is always 0.
477 combobox_->SetStyle(Combobox::STYLE_ACTION);
478 combobox_->SetSelectedIndex(1);
479 EXPECT_EQ(0, combobox_->selected_index());
480 combobox_->SetSelectedIndex(2);
481 EXPECT_EQ(0, combobox_->selected_index());
482 combobox_->SetSelectedIndex(3);
483 EXPECT_EQ(0, combobox_->selected_index());
442 } 484 }
443 485
444 TEST_F(ComboboxTest, ListenerHandlesDelete) { 486 TEST_F(ComboboxTest, ListenerHandlesDelete) {
445 TestComboboxModel model; 487 TestComboboxModel model;
446 TestCombobox* combobox = new TestCombobox(&model); // Deleted on change. 488 TestCombobox* combobox = new TestCombobox(&model); // Deleted on change.
447 EvilListener evil_listener; 489 EvilListener evil_listener;
448 combobox->set_listener(&evil_listener); 490 combobox->set_listener(&evil_listener);
449 ASSERT_NO_FATAL_FAILURE(combobox->ExecuteCommand(2)); 491 ASSERT_NO_FATAL_FAILURE(combobox->ExecuteCommand(2));
492 base::MessageLoop::current()->RunUntilIdle();
sky 2014/02/03 16:58:19 This shouldn't be necessary any where in this file
hajimehoshi 2014/02/04 04:06:32 Done.
450 EXPECT_TRUE(evil_listener.deleted()); 493 EXPECT_TRUE(evil_listener.deleted());
451 } 494 }
452 495
453 TEST_F(ComboboxTest, Click) { 496 TEST_F(ComboboxTest, Click) {
454 InitCombobox(); 497 InitCombobox();
455 498
456 TestComboboxListener listener; 499 TestComboboxListener listener;
457 combobox_->set_listener(&listener); 500 combobox_->set_listener(&listener);
458 501
459 combobox_->Layout(); 502 combobox_->Layout();
460 503
461 // Click the left side. The menu is shown. 504 // Click the left side. The menu is shown.
462 TestMenuRunnerHandler* test_menu_runner_handler = new TestMenuRunnerHandler(); 505 TestMenuRunnerHandler* test_menu_runner_handler = new TestMenuRunnerHandler();
463 scoped_ptr<MenuRunnerHandler> menu_runner_handler(test_menu_runner_handler); 506 scoped_ptr<MenuRunnerHandler> menu_runner_handler(test_menu_runner_handler);
464 test::MenuRunnerTestAPI test_api( 507 test::MenuRunnerTestAPI test_api(
465 combobox_->dropdown_list_menu_runner_.get()); 508 combobox_->dropdown_list_menu_runner_.get());
466 test_api.SetMenuRunnerHandler(menu_runner_handler.Pass()); 509 test_api.SetMenuRunnerHandler(menu_runner_handler.Pass());
467 PerformClick(gfx::Point(combobox_->x() + 1, 510 PerformClick(gfx::Point(combobox_->x() + 1,
468 combobox_->y() + combobox_->height() / 2)); 511 combobox_->y() + combobox_->height() / 2));
469 EXPECT_FALSE(listener.on_combobox_text_button_clicked_called()); 512 base::MessageLoop::current()->RunUntilIdle();
513 EXPECT_FALSE(listener.on_perform_action_called());
470 EXPECT_TRUE(test_menu_runner_handler->executed()); 514 EXPECT_TRUE(test_menu_runner_handler->executed());
471 } 515 }
472 516
473 TEST_F(ComboboxTest, NotifyOnClickWithReturnKey) { 517 TEST_F(ComboboxTest, NotifyOnClickWithReturnKey) {
474 InitCombobox(); 518 InitCombobox();
475 519
476 TestComboboxListener listener; 520 TestComboboxListener listener;
477 combobox_->set_listener(&listener); 521 combobox_->set_listener(&listener);
478 522
479 // With STYLE_SHOW_DROP_DOWN_ON_CLICK, the click event is ignored. 523 // With STYLE_NORMAL, the click event is ignored.
480 SendKeyEvent(ui::VKEY_RETURN); 524 SendKeyEvent(ui::VKEY_RETURN);
481 EXPECT_FALSE(listener.on_combobox_text_button_clicked_called()); 525 base::MessageLoop::current()->RunUntilIdle();
526 EXPECT_FALSE(listener.on_perform_action_called());
482 527
483 // With STYLE_NOTIFY_ON_CLICK, the click event is notified. 528 // With STYLE_ACTION, the click event is notified.
484 combobox_->SetStyle(Combobox::STYLE_NOTIFY_ON_CLICK); 529 combobox_->SetStyle(Combobox::STYLE_ACTION);
485 SendKeyEvent(ui::VKEY_RETURN); 530 SendKeyEvent(ui::VKEY_RETURN);
486 EXPECT_TRUE(listener.on_combobox_text_button_clicked_called()); 531 base::MessageLoop::current()->RunUntilIdle();
532 EXPECT_TRUE(listener.on_perform_action_called());
533 EXPECT_EQ(0, listener.perform_action_index());
487 } 534 }
488 535
489 TEST_F(ComboboxTest, NotifyOnClickWithSpaceKey) { 536 TEST_F(ComboboxTest, NotifyOnClickWithSpaceKey) {
490 InitCombobox(); 537 InitCombobox();
491 538
492 TestComboboxListener listener; 539 TestComboboxListener listener;
493 combobox_->set_listener(&listener); 540 combobox_->set_listener(&listener);
494 541
495 // With STYLE_SHOW_DROP_DOWN_ON_CLICK, the click event is ignored. 542 // With STYLE_NORMAL, the click event is ignored.
496 SendKeyEvent(ui::VKEY_SPACE); 543 SendKeyEvent(ui::VKEY_SPACE);
497 EXPECT_FALSE(listener.on_combobox_text_button_clicked_called()); 544 base::MessageLoop::current()->RunUntilIdle();
545 EXPECT_FALSE(listener.on_perform_action_called());
498 SendKeyEventWithType(ui::VKEY_SPACE, ui::ET_KEY_RELEASED); 546 SendKeyEventWithType(ui::VKEY_SPACE, ui::ET_KEY_RELEASED);
499 EXPECT_FALSE(listener.on_combobox_text_button_clicked_called()); 547 base::MessageLoop::current()->RunUntilIdle();
548 EXPECT_FALSE(listener.on_perform_action_called());
500 549
501 // With STYLE_NOTIFY_ON_CLICK, the click event is notified after releasing. 550 // With STYLE_ACTION, the click event is notified after releasing.
502 combobox_->SetStyle(Combobox::STYLE_NOTIFY_ON_CLICK); 551 combobox_->SetStyle(Combobox::STYLE_ACTION);
503 SendKeyEvent(ui::VKEY_SPACE); 552 SendKeyEvent(ui::VKEY_SPACE);
504 EXPECT_FALSE(listener.on_combobox_text_button_clicked_called()); 553 base::MessageLoop::current()->RunUntilIdle();
554 EXPECT_FALSE(listener.on_perform_action_called());
505 SendKeyEventWithType(ui::VKEY_SPACE, ui::ET_KEY_RELEASED); 555 SendKeyEventWithType(ui::VKEY_SPACE, ui::ET_KEY_RELEASED);
506 EXPECT_TRUE(listener.on_combobox_text_button_clicked_called()); 556 base::MessageLoop::current()->RunUntilIdle();
557 EXPECT_TRUE(listener.on_perform_action_called());
558 EXPECT_EQ(0, listener.perform_action_index());
507 } 559 }
508 560
509 TEST_F(ComboboxTest, NotifyOnClickWithMouse) { 561 TEST_F(ComboboxTest, NotifyOnClickWithMouse) {
510 InitCombobox(); 562 InitCombobox();
511 563
512 TestComboboxListener listener; 564 TestComboboxListener listener;
513 combobox_->set_listener(&listener); 565 combobox_->set_listener(&listener);
514 566
515 combobox_->SetStyle(Combobox::STYLE_NOTIFY_ON_CLICK); 567 combobox_->SetStyle(Combobox::STYLE_ACTION);
516 combobox_->Layout(); 568 combobox_->Layout();
517 569
518 // Click the right side (arrow button). The menu is shown. 570 // Click the right side (arrow button). The menu is shown.
519 TestMenuRunnerHandler* test_menu_runner_handler = new TestMenuRunnerHandler(); 571 TestMenuRunnerHandler* test_menu_runner_handler = new TestMenuRunnerHandler();
520 scoped_ptr<MenuRunnerHandler> menu_runner_handler(test_menu_runner_handler); 572 scoped_ptr<MenuRunnerHandler> menu_runner_handler(test_menu_runner_handler);
521 scoped_ptr<test::MenuRunnerTestAPI> test_api( 573 scoped_ptr<test::MenuRunnerTestAPI> test_api(
522 new test::MenuRunnerTestAPI(combobox_->dropdown_list_menu_runner_.get())); 574 new test::MenuRunnerTestAPI(combobox_->dropdown_list_menu_runner_.get()));
523 test_api->SetMenuRunnerHandler(menu_runner_handler.Pass()); 575 test_api->SetMenuRunnerHandler(menu_runner_handler.Pass());
524 576
525 PerformClick(gfx::Point(combobox_->x() + combobox_->width() - 1, 577 PerformClick(gfx::Point(combobox_->x() + combobox_->width() - 1,
526 combobox_->y() + combobox_->height() / 2)); 578 combobox_->y() + combobox_->height() / 2));
527 EXPECT_FALSE(listener.on_combobox_text_button_clicked_called()); 579 base::MessageLoop::current()->RunUntilIdle();
580 EXPECT_FALSE(listener.on_perform_action_called());
528 EXPECT_TRUE(test_menu_runner_handler->executed()); 581 EXPECT_TRUE(test_menu_runner_handler->executed());
529 582
530 // Click the left side (text button). The click event is notified. 583 // Click the left side (text button). The click event is notified.
531 test_menu_runner_handler = new TestMenuRunnerHandler(); 584 test_menu_runner_handler = new TestMenuRunnerHandler();
532 menu_runner_handler.reset(test_menu_runner_handler); 585 menu_runner_handler.reset(test_menu_runner_handler);
533 test_api.reset( 586 test_api.reset(
534 new test::MenuRunnerTestAPI(combobox_->dropdown_list_menu_runner_.get())); 587 new test::MenuRunnerTestAPI(combobox_->dropdown_list_menu_runner_.get()));
535 test_api->SetMenuRunnerHandler(menu_runner_handler.Pass()); 588 test_api->SetMenuRunnerHandler(menu_runner_handler.Pass());
536 PerformClick(gfx::Point(combobox_->x() + 1, 589 PerformClick(gfx::Point(combobox_->x() + 1,
537 combobox_->y() + combobox_->height() / 2)); 590 combobox_->y() + combobox_->height() / 2));
538 EXPECT_TRUE(listener.on_combobox_text_button_clicked_called()); 591 base::MessageLoop::current()->RunUntilIdle();
592 EXPECT_TRUE(listener.on_perform_action_called());
539 EXPECT_FALSE(test_menu_runner_handler->executed()); 593 EXPECT_FALSE(test_menu_runner_handler->executed());
594 EXPECT_EQ(0, listener.perform_action_index());
540 } 595 }
541 596
542 TEST_F(ComboboxTest, ConsumingPressKeyEvents) { 597 TEST_F(ComboboxTest, ConsumingPressKeyEvents) {
543 InitCombobox(); 598 InitCombobox();
544 599
545 EXPECT_FALSE(combobox_->OnKeyPressed( 600 EXPECT_FALSE(combobox_->OnKeyPressed(
546 ui::KeyEvent(ui::ET_KEY_PRESSED, ui::VKEY_RETURN, 0, false))); 601 ui::KeyEvent(ui::ET_KEY_PRESSED, ui::VKEY_RETURN, 0, false)));
547 EXPECT_FALSE(combobox_->OnKeyPressed( 602 EXPECT_FALSE(combobox_->OnKeyPressed(
548 ui::KeyEvent(ui::ET_KEY_PRESSED, ui::VKEY_SPACE, 0, false))); 603 ui::KeyEvent(ui::ET_KEY_PRESSED, ui::VKEY_SPACE, 0, false)));
549 604
550 // When the combobox's style is STYLE_NOTIFY_ON_CLICK, pressing events of 605 // When the combobox's style is STYLE_ACTION, pressing events of a space key
551 // a space key or an enter key will be consumed. 606 // or an enter key will be consumed.
552 combobox_->SetStyle(Combobox::STYLE_NOTIFY_ON_CLICK); 607 combobox_->SetStyle(Combobox::STYLE_ACTION);
553 EXPECT_TRUE(combobox_->OnKeyPressed( 608 EXPECT_TRUE(combobox_->OnKeyPressed(
554 ui::KeyEvent(ui::ET_KEY_PRESSED, ui::VKEY_RETURN, 0, false))); 609 ui::KeyEvent(ui::ET_KEY_PRESSED, ui::VKEY_RETURN, 0, false)));
555 EXPECT_TRUE(combobox_->OnKeyPressed( 610 EXPECT_TRUE(combobox_->OnKeyPressed(
556 ui::KeyEvent(ui::ET_KEY_PRESSED, ui::VKEY_SPACE, 0, false))); 611 ui::KeyEvent(ui::ET_KEY_PRESSED, ui::VKEY_SPACE, 0, false)));
557 } 612 }
558 613
614 TEST_F(ComboboxTest, ContentWidth) {
615 std::vector<std::string> values;
616
617 scoped_ptr<VectorComboboxModel> model(new VectorComboboxModel(&values));
618 combobox_ = new TestCombobox(model.get());
619
620 std::string long_item = "this is the long item";
621 std::string short_item = "s";
622
623 values.resize(1);
624 values[0] = long_item;
625 combobox_->ModelChanged();
626
627 const int long_item_width = combobox_->content_size_.width();
628
629 values[0] = short_item;
630 combobox_->ModelChanged();
631
632 const int short_item_width = combobox_->content_size_.width();
633
634 values.resize(2);
635 values[0] = short_item;
636 values[1] = long_item;
637 combobox_->ModelChanged();
638
639 // When the style is STYLE_NORMAL, the width will fit with the longest item.
640 combobox_->SetStyle(Combobox::STYLE_NORMAL);
641 EXPECT_EQ(long_item_width, combobox_->content_size_.width());
642
643 // When the style is STYLE_ACTION, the width will fit with the first items'
644 // width.
645 combobox_->SetStyle(Combobox::STYLE_ACTION);
646 EXPECT_EQ(short_item_width, combobox_->content_size_.width());
647 }
648
559 } // namespace views 649 } // namespace views
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698