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

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: Bug fix for Windows 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
« no previous file with comments | « ui/views/controls/combobox/combobox_listener.h ('k') | ui/views/examples/combobox_example.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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 (int)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
447 EvilListener evil_listener; 489 // |combobox| will be deleted on change.
448 combobox->set_listener(&evil_listener); 490 TestCombobox* combobox = new TestCombobox(&model);
491 scoped_ptr<EvilListener> evil_listener(new EvilListener());
492 combobox->set_listener(evil_listener.get());
449 ASSERT_NO_FATAL_FAILURE(combobox->ExecuteCommand(2)); 493 ASSERT_NO_FATAL_FAILURE(combobox->ExecuteCommand(2));
450 EXPECT_TRUE(evil_listener.deleted()); 494 EXPECT_TRUE(evil_listener->deleted());
495
496 // With STYLE_ACTION
497 // |combobox| will be deleted on change.
498 combobox = new TestCombobox(&model);
499 evil_listener.reset(new EvilListener());
500 combobox->set_listener(evil_listener.get());
501 combobox->SetStyle(Combobox::STYLE_ACTION);
502 ASSERT_NO_FATAL_FAILURE(combobox->ExecuteCommand(2));
503 EXPECT_TRUE(evil_listener->deleted());
451 } 504 }
452 505
453 TEST_F(ComboboxTest, Click) { 506 TEST_F(ComboboxTest, Click) {
454 InitCombobox(); 507 InitCombobox();
455 508
456 TestComboboxListener listener; 509 TestComboboxListener listener;
457 combobox_->set_listener(&listener); 510 combobox_->set_listener(&listener);
458 511
459 combobox_->Layout(); 512 combobox_->Layout();
460 513
461 // Click the left side. The menu is shown. 514 // Click the left side. The menu is shown.
462 TestMenuRunnerHandler* test_menu_runner_handler = new TestMenuRunnerHandler(); 515 TestMenuRunnerHandler* test_menu_runner_handler = new TestMenuRunnerHandler();
463 scoped_ptr<MenuRunnerHandler> menu_runner_handler(test_menu_runner_handler); 516 scoped_ptr<MenuRunnerHandler> menu_runner_handler(test_menu_runner_handler);
464 test::MenuRunnerTestAPI test_api( 517 test::MenuRunnerTestAPI test_api(
465 combobox_->dropdown_list_menu_runner_.get()); 518 combobox_->dropdown_list_menu_runner_.get());
466 test_api.SetMenuRunnerHandler(menu_runner_handler.Pass()); 519 test_api.SetMenuRunnerHandler(menu_runner_handler.Pass());
467 PerformClick(gfx::Point(combobox_->x() + 1, 520 PerformClick(gfx::Point(combobox_->x() + 1,
468 combobox_->y() + combobox_->height() / 2)); 521 combobox_->y() + combobox_->height() / 2));
469 EXPECT_FALSE(listener.on_combobox_text_button_clicked_called()); 522 EXPECT_FALSE(listener.on_perform_action_called());
470 EXPECT_TRUE(test_menu_runner_handler->executed()); 523 EXPECT_TRUE(test_menu_runner_handler->executed());
471 } 524 }
472 525
473 TEST_F(ComboboxTest, NotifyOnClickWithReturnKey) { 526 TEST_F(ComboboxTest, NotifyOnClickWithReturnKey) {
474 InitCombobox(); 527 InitCombobox();
475 528
476 TestComboboxListener listener; 529 TestComboboxListener listener;
477 combobox_->set_listener(&listener); 530 combobox_->set_listener(&listener);
478 531
479 // With STYLE_SHOW_DROP_DOWN_ON_CLICK, the click event is ignored. 532 // With STYLE_NORMAL, the click event is ignored.
480 SendKeyEvent(ui::VKEY_RETURN); 533 SendKeyEvent(ui::VKEY_RETURN);
481 EXPECT_FALSE(listener.on_combobox_text_button_clicked_called()); 534 EXPECT_FALSE(listener.on_perform_action_called());
482 535
483 // With STYLE_NOTIFY_ON_CLICK, the click event is notified. 536 // With STYLE_ACTION, the click event is notified.
484 combobox_->SetStyle(Combobox::STYLE_NOTIFY_ON_CLICK); 537 combobox_->SetStyle(Combobox::STYLE_ACTION);
485 SendKeyEvent(ui::VKEY_RETURN); 538 SendKeyEvent(ui::VKEY_RETURN);
486 EXPECT_TRUE(listener.on_combobox_text_button_clicked_called()); 539 EXPECT_TRUE(listener.on_perform_action_called());
540 EXPECT_EQ(0, listener.perform_action_index());
487 } 541 }
488 542
489 TEST_F(ComboboxTest, NotifyOnClickWithSpaceKey) { 543 TEST_F(ComboboxTest, NotifyOnClickWithSpaceKey) {
490 InitCombobox(); 544 InitCombobox();
491 545
492 TestComboboxListener listener; 546 TestComboboxListener listener;
493 combobox_->set_listener(&listener); 547 combobox_->set_listener(&listener);
494 548
495 // With STYLE_SHOW_DROP_DOWN_ON_CLICK, the click event is ignored. 549 // With STYLE_NORMAL, the click event is ignored.
496 SendKeyEvent(ui::VKEY_SPACE); 550 SendKeyEvent(ui::VKEY_SPACE);
497 EXPECT_FALSE(listener.on_combobox_text_button_clicked_called()); 551 EXPECT_FALSE(listener.on_perform_action_called());
498 SendKeyEventWithType(ui::VKEY_SPACE, ui::ET_KEY_RELEASED); 552 SendKeyEventWithType(ui::VKEY_SPACE, ui::ET_KEY_RELEASED);
499 EXPECT_FALSE(listener.on_combobox_text_button_clicked_called()); 553 EXPECT_FALSE(listener.on_perform_action_called());
500 554
501 // With STYLE_NOTIFY_ON_CLICK, the click event is notified after releasing. 555 // With STYLE_ACTION, the click event is notified after releasing.
502 combobox_->SetStyle(Combobox::STYLE_NOTIFY_ON_CLICK); 556 combobox_->SetStyle(Combobox::STYLE_ACTION);
503 SendKeyEvent(ui::VKEY_SPACE); 557 SendKeyEvent(ui::VKEY_SPACE);
504 EXPECT_FALSE(listener.on_combobox_text_button_clicked_called()); 558 EXPECT_FALSE(listener.on_perform_action_called());
505 SendKeyEventWithType(ui::VKEY_SPACE, ui::ET_KEY_RELEASED); 559 SendKeyEventWithType(ui::VKEY_SPACE, ui::ET_KEY_RELEASED);
506 EXPECT_TRUE(listener.on_combobox_text_button_clicked_called()); 560 EXPECT_TRUE(listener.on_perform_action_called());
561 EXPECT_EQ(0, listener.perform_action_index());
507 } 562 }
508 563
509 TEST_F(ComboboxTest, NotifyOnClickWithMouse) { 564 TEST_F(ComboboxTest, NotifyOnClickWithMouse) {
510 InitCombobox(); 565 InitCombobox();
511 566
512 TestComboboxListener listener; 567 TestComboboxListener listener;
513 combobox_->set_listener(&listener); 568 combobox_->set_listener(&listener);
514 569
515 combobox_->SetStyle(Combobox::STYLE_NOTIFY_ON_CLICK); 570 combobox_->SetStyle(Combobox::STYLE_ACTION);
516 combobox_->Layout(); 571 combobox_->Layout();
517 572
518 // Click the right side (arrow button). The menu is shown. 573 // Click the right side (arrow button). The menu is shown.
519 TestMenuRunnerHandler* test_menu_runner_handler = new TestMenuRunnerHandler(); 574 TestMenuRunnerHandler* test_menu_runner_handler = new TestMenuRunnerHandler();
520 scoped_ptr<MenuRunnerHandler> menu_runner_handler(test_menu_runner_handler); 575 scoped_ptr<MenuRunnerHandler> menu_runner_handler(test_menu_runner_handler);
521 scoped_ptr<test::MenuRunnerTestAPI> test_api( 576 scoped_ptr<test::MenuRunnerTestAPI> test_api(
522 new test::MenuRunnerTestAPI(combobox_->dropdown_list_menu_runner_.get())); 577 new test::MenuRunnerTestAPI(combobox_->dropdown_list_menu_runner_.get()));
523 test_api->SetMenuRunnerHandler(menu_runner_handler.Pass()); 578 test_api->SetMenuRunnerHandler(menu_runner_handler.Pass());
524 579
525 PerformClick(gfx::Point(combobox_->x() + combobox_->width() - 1, 580 PerformClick(gfx::Point(combobox_->x() + combobox_->width() - 1,
526 combobox_->y() + combobox_->height() / 2)); 581 combobox_->y() + combobox_->height() / 2));
527 EXPECT_FALSE(listener.on_combobox_text_button_clicked_called()); 582 EXPECT_FALSE(listener.on_perform_action_called());
528 EXPECT_TRUE(test_menu_runner_handler->executed()); 583 EXPECT_TRUE(test_menu_runner_handler->executed());
529 584
530 // Click the left side (text button). The click event is notified. 585 // Click the left side (text button). The click event is notified.
531 test_menu_runner_handler = new TestMenuRunnerHandler(); 586 test_menu_runner_handler = new TestMenuRunnerHandler();
532 menu_runner_handler.reset(test_menu_runner_handler); 587 menu_runner_handler.reset(test_menu_runner_handler);
533 test_api.reset( 588 test_api.reset(
534 new test::MenuRunnerTestAPI(combobox_->dropdown_list_menu_runner_.get())); 589 new test::MenuRunnerTestAPI(combobox_->dropdown_list_menu_runner_.get()));
535 test_api->SetMenuRunnerHandler(menu_runner_handler.Pass()); 590 test_api->SetMenuRunnerHandler(menu_runner_handler.Pass());
536 PerformClick(gfx::Point(combobox_->x() + 1, 591 PerformClick(gfx::Point(combobox_->x() + 1,
537 combobox_->y() + combobox_->height() / 2)); 592 combobox_->y() + combobox_->height() / 2));
538 EXPECT_TRUE(listener.on_combobox_text_button_clicked_called()); 593 EXPECT_TRUE(listener.on_perform_action_called());
539 EXPECT_FALSE(test_menu_runner_handler->executed()); 594 EXPECT_FALSE(test_menu_runner_handler->executed());
595 EXPECT_EQ(0, listener.perform_action_index());
540 } 596 }
541 597
542 TEST_F(ComboboxTest, ConsumingPressKeyEvents) { 598 TEST_F(ComboboxTest, ConsumingPressKeyEvents) {
543 InitCombobox(); 599 InitCombobox();
544 600
545 EXPECT_FALSE(combobox_->OnKeyPressed( 601 EXPECT_FALSE(combobox_->OnKeyPressed(
546 ui::KeyEvent(ui::ET_KEY_PRESSED, ui::VKEY_RETURN, 0, false))); 602 ui::KeyEvent(ui::ET_KEY_PRESSED, ui::VKEY_RETURN, 0, false)));
547 EXPECT_FALSE(combobox_->OnKeyPressed( 603 EXPECT_FALSE(combobox_->OnKeyPressed(
548 ui::KeyEvent(ui::ET_KEY_PRESSED, ui::VKEY_SPACE, 0, false))); 604 ui::KeyEvent(ui::ET_KEY_PRESSED, ui::VKEY_SPACE, 0, false)));
549 605
550 // When the combobox's style is STYLE_NOTIFY_ON_CLICK, pressing events of 606 // 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. 607 // or an enter key will be consumed.
552 combobox_->SetStyle(Combobox::STYLE_NOTIFY_ON_CLICK); 608 combobox_->SetStyle(Combobox::STYLE_ACTION);
553 EXPECT_TRUE(combobox_->OnKeyPressed( 609 EXPECT_TRUE(combobox_->OnKeyPressed(
554 ui::KeyEvent(ui::ET_KEY_PRESSED, ui::VKEY_RETURN, 0, false))); 610 ui::KeyEvent(ui::ET_KEY_PRESSED, ui::VKEY_RETURN, 0, false)));
555 EXPECT_TRUE(combobox_->OnKeyPressed( 611 EXPECT_TRUE(combobox_->OnKeyPressed(
556 ui::KeyEvent(ui::ET_KEY_PRESSED, ui::VKEY_SPACE, 0, false))); 612 ui::KeyEvent(ui::ET_KEY_PRESSED, ui::VKEY_SPACE, 0, false)));
557 } 613 }
558 614
615 TEST_F(ComboboxTest, ContentWidth) {
616 std::vector<std::string> values;
617
618 scoped_ptr<VectorComboboxModel> model(new VectorComboboxModel(&values));
619 combobox_ = new TestCombobox(model.get());
620
621 std::string long_item = "this is the long item";
622 std::string short_item = "s";
623
624 values.resize(1);
625 values[0] = long_item;
626 combobox_->ModelChanged();
627
628 const int long_item_width = combobox_->content_size_.width();
629
630 values[0] = short_item;
631 combobox_->ModelChanged();
632
633 const int short_item_width = combobox_->content_size_.width();
634
635 values.resize(2);
636 values[0] = short_item;
637 values[1] = long_item;
638 combobox_->ModelChanged();
639
640 // When the style is STYLE_NORMAL, the width will fit with the longest item.
641 combobox_->SetStyle(Combobox::STYLE_NORMAL);
642 EXPECT_EQ(long_item_width, combobox_->content_size_.width());
643
644 // When the style is STYLE_ACTION, the width will fit with the first items'
645 // width.
646 combobox_->SetStyle(Combobox::STYLE_ACTION);
647 EXPECT_EQ(short_item_width, combobox_->content_size_.width());
648 }
649
559 } // namespace views 650 } // namespace views
OLDNEW
« no previous file with comments | « ui/views/controls/combobox/combobox_listener.h ('k') | ui/views/examples/combobox_example.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698