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/macros.h" | 9 #include "base/macros.h" |
10 #include "base/strings/utf_string_conversions.h" | 10 #include "base/strings/utf_string_conversions.h" |
(...skipping 167 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
178 int actions_performed_; | 178 int actions_performed_; |
179 | 179 |
180 private: | 180 private: |
181 DISALLOW_COPY_AND_ASSIGN(TestComboboxListener); | 181 DISALLOW_COPY_AND_ASSIGN(TestComboboxListener); |
182 }; | 182 }; |
183 | 183 |
184 } // namespace | 184 } // namespace |
185 | 185 |
186 class ComboboxTest : public ViewsTestBase { | 186 class ComboboxTest : public ViewsTestBase { |
187 public: | 187 public: |
188 ComboboxTest() : widget_(NULL), combobox_(NULL) {} | 188 ComboboxTest() : widget_(NULL), combobox_(NULL), menu_show_count_(0) {} |
tapted
2017/01/11 15:11:53
nit: move these to inline initializers, like
wid
karandeepb
2017/01/11 23:58:31
Done.
| |
189 | 189 |
190 void TearDown() override { | 190 void TearDown() override { |
191 if (widget_) | 191 if (widget_) |
192 widget_->Close(); | 192 widget_->Close(); |
193 ViewsTestBase::TearDown(); | 193 ViewsTestBase::TearDown(); |
194 } | 194 } |
195 | 195 |
196 void InitCombobox(const std::set<int>* separators, Combobox::Style style) { | 196 void InitCombobox(const std::set<int>* separators, Combobox::Style style) { |
197 model_.reset(new TestComboboxModel()); | 197 model_.reset(new TestComboboxModel()); |
198 | 198 |
199 if (separators) | 199 if (separators) |
200 model_->SetSeparators(*separators); | 200 model_->SetSeparators(*separators); |
201 | 201 |
202 ASSERT_FALSE(combobox_); | 202 ASSERT_FALSE(combobox_); |
203 combobox_ = new TestCombobox(model_.get(), style); | 203 combobox_ = new TestCombobox(model_.get(), style); |
204 test_api_.reset(new ComboboxTestApi(combobox_)); | 204 test_api_.reset(new ComboboxTestApi(combobox_)); |
205 test_api_->InstallTestMenuRunner(&menu_show_count_); | |
205 combobox_->set_id(1); | 206 combobox_->set_id(1); |
206 | 207 |
207 widget_ = new Widget; | 208 widget_ = new Widget; |
208 Widget::InitParams params = CreateParams(Widget::InitParams::TYPE_POPUP); | 209 Widget::InitParams params = CreateParams(Widget::InitParams::TYPE_POPUP); |
209 params.bounds = gfx::Rect(200, 200, 200, 200); | 210 params.bounds = gfx::Rect(200, 200, 200, 200); |
210 widget_->Init(params); | 211 widget_->Init(params); |
211 View* container = new View(); | 212 View* container = new View(); |
212 widget_->SetContentsView(container); | 213 widget_->SetContentsView(container); |
213 container->AddChildView(combobox_); | 214 container->AddChildView(combobox_); |
214 widget_->Show(); | 215 widget_->Show(); |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
246 ui::ET_MOUSE_RELEASED, point, point, ui::EventTimeForNow(), | 247 ui::ET_MOUSE_RELEASED, point, point, ui::EventTimeForNow(), |
247 ui::EF_LEFT_MOUSE_BUTTON, ui::EF_LEFT_MOUSE_BUTTON); | 248 ui::EF_LEFT_MOUSE_BUTTON, ui::EF_LEFT_MOUSE_BUTTON); |
248 widget_->OnMouseEvent(&released_event); | 249 widget_->OnMouseEvent(&released_event); |
249 } | 250 } |
250 | 251 |
251 void PerformClick(const gfx::Point& point) { | 252 void PerformClick(const gfx::Point& point) { |
252 PerformMousePress(point); | 253 PerformMousePress(point); |
253 PerformMouseRelease(point); | 254 PerformMouseRelease(point); |
254 } | 255 } |
255 | 256 |
257 int menu_show_count() const { return menu_show_count_; } | |
tapted
2017/01/11 15:11:53
is this needed - it's OK for a test harness to hav
karandeepb
2017/01/11 23:58:31
Oh yeah I should have declared menu_show_count_ pr
| |
258 | |
256 // We need widget to populate wrapper class. | 259 // We need widget to populate wrapper class. |
257 Widget* widget_; | 260 Widget* widget_; |
258 | 261 |
259 // |combobox_| will be allocated InitCombobox() and then owned by |widget_|. | 262 // |combobox_| will be allocated InitCombobox() and then owned by |widget_|. |
260 TestCombobox* combobox_; | 263 TestCombobox* combobox_; |
261 std::unique_ptr<ComboboxTestApi> test_api_; | 264 std::unique_ptr<ComboboxTestApi> test_api_; |
262 | 265 |
263 // Combobox does not take ownership of the model, hence it needs to be scoped. | 266 // Combobox does not take ownership of the model, hence it needs to be scoped. |
264 std::unique_ptr<TestComboboxModel> model_; | 267 std::unique_ptr<TestComboboxModel> model_; |
265 | 268 |
269 // The current menu show count. | |
270 int menu_show_count_; | |
271 | |
266 private: | 272 private: |
267 DISALLOW_COPY_AND_ASSIGN(ComboboxTest); | 273 DISALLOW_COPY_AND_ASSIGN(ComboboxTest); |
268 }; | 274 }; |
269 | 275 |
270 TEST_F(ComboboxTest, KeyTest) { | 276 TEST_F(ComboboxTest, KeyTest) { |
271 InitCombobox(nullptr, Combobox::STYLE_NORMAL); | 277 InitCombobox(nullptr, Combobox::STYLE_NORMAL); |
272 SendKeyEvent(ui::VKEY_END); | 278 SendKeyEvent(ui::VKEY_END); |
273 EXPECT_EQ(combobox_->selected_index() + 1, model_->GetItemCount()); | 279 EXPECT_EQ(combobox_->selected_index() + 1, model_->GetItemCount()); |
274 SendKeyEvent(ui::VKEY_HOME); | 280 SendKeyEvent(ui::VKEY_HOME); |
275 EXPECT_EQ(combobox_->selected_index(), 0); | 281 EXPECT_EQ(combobox_->selected_index(), 0); |
(...skipping 220 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
496 combobox->set_listener(evil_listener.get()); | 502 combobox->set_listener(evil_listener.get()); |
497 ASSERT_NO_FATAL_FAILURE(ComboboxTestApi(combobox).PerformActionAt(2)); | 503 ASSERT_NO_FATAL_FAILURE(ComboboxTestApi(combobox).PerformActionAt(2)); |
498 EXPECT_TRUE(evil_listener->deleted()); | 504 EXPECT_TRUE(evil_listener->deleted()); |
499 } | 505 } |
500 | 506 |
501 TEST_F(ComboboxTest, Click) { | 507 TEST_F(ComboboxTest, Click) { |
502 InitCombobox(nullptr, Combobox::STYLE_NORMAL); | 508 InitCombobox(nullptr, Combobox::STYLE_NORMAL); |
503 | 509 |
504 TestComboboxListener listener; | 510 TestComboboxListener listener; |
505 combobox_->set_listener(&listener); | 511 combobox_->set_listener(&listener); |
506 | |
507 combobox_->Layout(); | 512 combobox_->Layout(); |
508 int menu_show_count = 0; | |
509 test_api_->InstallTestMenuRunner(&menu_show_count); | |
510 | 513 |
511 // Click the left side. The menu is shown. | 514 // Click the left side. The menu is shown. |
512 EXPECT_EQ(0, menu_show_count); | 515 EXPECT_EQ(0, menu_show_count()); |
513 PerformClick(gfx::Point(combobox_->x() + 1, | 516 PerformClick(gfx::Point(combobox_->x() + 1, |
514 combobox_->y() + combobox_->height() / 2)); | 517 combobox_->y() + combobox_->height() / 2)); |
515 EXPECT_FALSE(listener.on_perform_action_called()); | 518 EXPECT_FALSE(listener.on_perform_action_called()); |
516 EXPECT_EQ(1, menu_show_count); | 519 EXPECT_EQ(1, menu_show_count()); |
517 } | 520 } |
518 | 521 |
519 TEST_F(ComboboxTest, ClickButDisabled) { | 522 TEST_F(ComboboxTest, ClickButDisabled) { |
520 InitCombobox(nullptr, Combobox::STYLE_NORMAL); | 523 InitCombobox(nullptr, Combobox::STYLE_NORMAL); |
521 | 524 |
522 TestComboboxListener listener; | 525 TestComboboxListener listener; |
523 combobox_->set_listener(&listener); | 526 combobox_->set_listener(&listener); |
524 | 527 |
525 combobox_->Layout(); | 528 combobox_->Layout(); |
526 combobox_->SetEnabled(false); | 529 combobox_->SetEnabled(false); |
527 | 530 |
528 // Click the left side, but nothing happens since the combobox is disabled. | 531 // Click the left side, but nothing happens since the combobox is disabled. |
529 int menu_show_count = 0; | |
530 test_api_->InstallTestMenuRunner(&menu_show_count); | |
531 PerformClick(gfx::Point(combobox_->x() + 1, | 532 PerformClick(gfx::Point(combobox_->x() + 1, |
532 combobox_->y() + combobox_->height() / 2)); | 533 combobox_->y() + combobox_->height() / 2)); |
533 EXPECT_FALSE(listener.on_perform_action_called()); | 534 EXPECT_FALSE(listener.on_perform_action_called()); |
534 EXPECT_EQ(0, menu_show_count); | 535 EXPECT_EQ(0, menu_show_count()); |
535 } | 536 } |
536 | 537 |
537 TEST_F(ComboboxTest, NotifyOnClickWithReturnKey) { | 538 TEST_F(ComboboxTest, NotifyOnClickWithReturnKey) { |
538 InitCombobox(nullptr, Combobox::STYLE_NORMAL); | 539 InitCombobox(nullptr, Combobox::STYLE_NORMAL); |
539 | 540 |
540 TestComboboxListener listener; | 541 TestComboboxListener listener; |
541 combobox_->set_listener(&listener); | 542 combobox_->set_listener(&listener); |
542 | 543 |
543 // With STYLE_NORMAL, the click event is ignored. | 544 // With STYLE_NORMAL, the click event is ignored. Instead the menu is shown. |
544 SendKeyEvent(ui::VKEY_RETURN); | 545 SendKeyEvent(ui::VKEY_RETURN); |
546 EXPECT_EQ(1, menu_show_count()); | |
545 EXPECT_FALSE(listener.on_perform_action_called()); | 547 EXPECT_FALSE(listener.on_perform_action_called()); |
546 } | 548 } |
547 | 549 |
548 TEST_F(ComboboxTest, NotifyOnClickWithReturnKeyActionStyle) { | 550 TEST_F(ComboboxTest, NotifyOnClickWithReturnKeyActionStyle) { |
549 InitCombobox(nullptr, Combobox::STYLE_ACTION); | 551 InitCombobox(nullptr, Combobox::STYLE_ACTION); |
550 | 552 |
551 TestComboboxListener listener; | 553 TestComboboxListener listener; |
552 combobox_->set_listener(&listener); | 554 combobox_->set_listener(&listener); |
553 | 555 |
554 // With STYLE_ACTION, the click event is notified. | 556 // With STYLE_ACTION, the click event is notified and the menu is not shown. |
555 SendKeyEvent(ui::VKEY_RETURN); | 557 SendKeyEvent(ui::VKEY_RETURN); |
558 EXPECT_EQ(0, menu_show_count()); | |
556 EXPECT_TRUE(listener.on_perform_action_called()); | 559 EXPECT_TRUE(listener.on_perform_action_called()); |
557 EXPECT_EQ(0, listener.perform_action_index()); | 560 EXPECT_EQ(0, listener.perform_action_index()); |
558 } | 561 } |
559 | 562 |
560 TEST_F(ComboboxTest, NotifyOnClickWithSpaceKey) { | 563 TEST_F(ComboboxTest, NotifyOnClickWithSpaceKey) { |
561 InitCombobox(nullptr, Combobox::STYLE_NORMAL); | 564 InitCombobox(nullptr, Combobox::STYLE_NORMAL); |
562 | 565 |
563 TestComboboxListener listener; | 566 TestComboboxListener listener; |
564 combobox_->set_listener(&listener); | 567 combobox_->set_listener(&listener); |
565 | 568 |
566 // With STYLE_NORMAL, the click event is ignored. | 569 // With STYLE_NORMAL, the click event is ignored. Instead the menu is shwon. |
567 SendKeyEvent(ui::VKEY_SPACE); | 570 SendKeyEvent(ui::VKEY_SPACE); |
571 EXPECT_EQ(1, menu_show_count()); | |
568 EXPECT_FALSE(listener.on_perform_action_called()); | 572 EXPECT_FALSE(listener.on_perform_action_called()); |
573 | |
569 SendKeyEventWithType(ui::VKEY_SPACE, ui::ET_KEY_RELEASED); | 574 SendKeyEventWithType(ui::VKEY_SPACE, ui::ET_KEY_RELEASED); |
575 EXPECT_EQ(1, menu_show_count()); | |
570 EXPECT_FALSE(listener.on_perform_action_called()); | 576 EXPECT_FALSE(listener.on_perform_action_called()); |
571 } | 577 } |
572 | 578 |
573 TEST_F(ComboboxTest, NotifyOnClickWithSpaceKeyActionStyle) { | 579 TEST_F(ComboboxTest, NotifyOnClickWithSpaceKeyActionStyle) { |
574 InitCombobox(nullptr, Combobox::STYLE_ACTION); | 580 InitCombobox(nullptr, Combobox::STYLE_ACTION); |
575 | 581 |
576 TestComboboxListener listener; | 582 TestComboboxListener listener; |
577 combobox_->set_listener(&listener); | 583 combobox_->set_listener(&listener); |
578 | 584 |
579 // With STYLE_ACTION, the click event is notified after releasing. | 585 // With STYLE_ACTION, the click event is notified after releasing and the menu |
586 // is not shown. | |
580 SendKeyEvent(ui::VKEY_SPACE); | 587 SendKeyEvent(ui::VKEY_SPACE); |
588 EXPECT_EQ(0, menu_show_count()); | |
581 EXPECT_FALSE(listener.on_perform_action_called()); | 589 EXPECT_FALSE(listener.on_perform_action_called()); |
590 | |
582 SendKeyEventWithType(ui::VKEY_SPACE, ui::ET_KEY_RELEASED); | 591 SendKeyEventWithType(ui::VKEY_SPACE, ui::ET_KEY_RELEASED); |
592 EXPECT_EQ(0, menu_show_count()); | |
583 EXPECT_TRUE(listener.on_perform_action_called()); | 593 EXPECT_TRUE(listener.on_perform_action_called()); |
584 EXPECT_EQ(0, listener.perform_action_index()); | 594 EXPECT_EQ(0, listener.perform_action_index()); |
585 } | 595 } |
586 | 596 |
587 TEST_F(ComboboxTest, NotifyOnClickWithMouse) { | 597 TEST_F(ComboboxTest, NotifyOnClickWithMouse) { |
588 InitCombobox(nullptr, Combobox::STYLE_ACTION); | 598 InitCombobox(nullptr, Combobox::STYLE_ACTION); |
589 | 599 |
590 TestComboboxListener listener; | 600 TestComboboxListener listener; |
591 combobox_->set_listener(&listener); | 601 combobox_->set_listener(&listener); |
592 | 602 |
593 combobox_->Layout(); | 603 combobox_->Layout(); |
594 | 604 |
595 // Click the right side (arrow button). The menu is shown. | 605 // Click the right side (arrow button). The menu is shown. |
596 int menu_show_count = 0; | |
597 test_api_->InstallTestMenuRunner(&menu_show_count); | |
598 const gfx::Point right_point(combobox_->x() + combobox_->width() - 1, | 606 const gfx::Point right_point(combobox_->x() + combobox_->width() - 1, |
599 combobox_->y() + combobox_->height() / 2); | 607 combobox_->y() + combobox_->height() / 2); |
600 | 608 |
601 EXPECT_EQ(0, menu_show_count); | 609 EXPECT_EQ(0, menu_show_count()); |
602 | 610 |
603 // On Mac, actions occur on mouse down. Otherwise mouse up. | 611 // On Mac, actions occur on mouse down. Otherwise mouse up. |
604 #if defined(OS_MACOSX) | 612 #if defined(OS_MACOSX) |
605 const int kActOnMouseDown = 1; | 613 const int kActOnMouseDown = 1; |
606 #else | 614 #else |
607 const int kActOnMouseDown = 0; | 615 const int kActOnMouseDown = 0; |
608 #endif | 616 #endif |
609 | 617 |
610 PerformMousePress(right_point); | 618 PerformMousePress(right_point); |
611 EXPECT_EQ(kActOnMouseDown, menu_show_count); | 619 EXPECT_EQ(kActOnMouseDown, menu_show_count()); |
612 PerformMouseRelease(right_point); | 620 PerformMouseRelease(right_point); |
613 EXPECT_EQ(1, menu_show_count); | 621 EXPECT_EQ(1, menu_show_count()); |
614 | 622 |
615 // Click the left side (text button). The click event is notified. | 623 // Click the left side (text button). The click event is notified. |
616 const gfx::Point left_point( | 624 const gfx::Point left_point( |
617 gfx::Point(combobox_->x() + 1, combobox_->y() + combobox_->height() / 2)); | 625 gfx::Point(combobox_->x() + 1, combobox_->y() + combobox_->height() / 2)); |
618 test_api_->InstallTestMenuRunner(&menu_show_count); | |
619 | 626 |
620 PerformMousePress(left_point); | 627 PerformMousePress(left_point); |
621 PerformMouseRelease(left_point); | 628 PerformMouseRelease(left_point); |
622 | 629 |
623 EXPECT_EQ(1, menu_show_count); // Unchanged. | 630 EXPECT_EQ(1, menu_show_count()); // Unchanged. |
624 EXPECT_EQ(0, listener.perform_action_index()); | 631 EXPECT_EQ(0, listener.perform_action_index()); |
625 } | 632 } |
626 | 633 |
627 TEST_F(ComboboxTest, ConsumingPressKeyEvents) { | 634 TEST_F(ComboboxTest, ConsumingPressKeyEvents) { |
628 InitCombobox(nullptr, Combobox::STYLE_NORMAL); | 635 InitCombobox(nullptr, Combobox::STYLE_NORMAL); |
629 | 636 |
630 int menu_show_count = 0; | |
631 test_api_->InstallTestMenuRunner(&menu_show_count); | |
632 EXPECT_TRUE(combobox_->OnKeyPressed( | 637 EXPECT_TRUE(combobox_->OnKeyPressed( |
633 ui::KeyEvent(ui::ET_KEY_PRESSED, ui::VKEY_RETURN, ui::EF_NONE))); | 638 ui::KeyEvent(ui::ET_KEY_PRESSED, ui::VKEY_RETURN, ui::EF_NONE))); |
634 EXPECT_EQ(1, menu_show_count); | 639 EXPECT_EQ(1, menu_show_count()); |
635 EXPECT_TRUE(combobox_->OnKeyPressed( | 640 EXPECT_TRUE(combobox_->OnKeyPressed( |
636 ui::KeyEvent(ui::ET_KEY_PRESSED, ui::VKEY_SPACE, ui::EF_NONE))); | 641 ui::KeyEvent(ui::ET_KEY_PRESSED, ui::VKEY_SPACE, ui::EF_NONE))); |
637 EXPECT_EQ(2, menu_show_count); | 642 EXPECT_EQ(2, menu_show_count()); |
638 } | 643 } |
639 | 644 |
640 TEST_F(ComboboxTest, ConsumingKeyPressEventsActionStyle) { | 645 TEST_F(ComboboxTest, ConsumingKeyPressEventsActionStyle) { |
641 // When the combobox's style is STYLE_ACTION, pressing events of a space key | 646 // When the combobox's style is STYLE_ACTION, pressing events of a space key |
642 // or an enter key will be consumed. | 647 // or an enter key will be consumed and the menu is not shown. |
643 InitCombobox(nullptr, Combobox::STYLE_ACTION); | 648 InitCombobox(nullptr, Combobox::STYLE_ACTION); |
649 | |
644 EXPECT_TRUE(combobox_->OnKeyPressed( | 650 EXPECT_TRUE(combobox_->OnKeyPressed( |
645 ui::KeyEvent(ui::ET_KEY_PRESSED, ui::VKEY_RETURN, ui::EF_NONE))); | 651 ui::KeyEvent(ui::ET_KEY_PRESSED, ui::VKEY_RETURN, ui::EF_NONE))); |
652 EXPECT_EQ(0, menu_show_count()); | |
646 EXPECT_TRUE(combobox_->OnKeyPressed( | 653 EXPECT_TRUE(combobox_->OnKeyPressed( |
647 ui::KeyEvent(ui::ET_KEY_PRESSED, ui::VKEY_SPACE, ui::EF_NONE))); | 654 ui::KeyEvent(ui::ET_KEY_PRESSED, ui::VKEY_SPACE, ui::EF_NONE))); |
655 EXPECT_EQ(0, menu_show_count()); | |
648 } | 656 } |
649 | 657 |
650 TEST_F(ComboboxTest, ContentWidth) { | 658 TEST_F(ComboboxTest, ContentWidth) { |
651 std::vector<std::string> values; | 659 std::vector<std::string> values; |
652 VectorComboboxModel model(&values); | 660 VectorComboboxModel model(&values); |
653 TestCombobox combobox(&model, Combobox::STYLE_NORMAL); | 661 TestCombobox combobox(&model, Combobox::STYLE_NORMAL); |
654 TestCombobox action_combobox(&model, Combobox::STYLE_ACTION); | 662 TestCombobox action_combobox(&model, Combobox::STYLE_ACTION); |
655 ComboboxTestApi test_api(&combobox); | 663 ComboboxTestApi test_api(&combobox); |
656 ComboboxTestApi action_test_api(&action_combobox); | 664 ComboboxTestApi action_test_api(&action_combobox); |
657 | 665 |
(...skipping 160 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
818 EXPECT_EQ(ui::MenuModel::TYPE_COMMAND, menu_model->GetTypeAt(0)); | 826 EXPECT_EQ(ui::MenuModel::TYPE_COMMAND, menu_model->GetTypeAt(0)); |
819 EXPECT_EQ(ui::MenuModel::TYPE_COMMAND, menu_model->GetTypeAt(1)); | 827 EXPECT_EQ(ui::MenuModel::TYPE_COMMAND, menu_model->GetTypeAt(1)); |
820 | 828 |
821 EXPECT_EQ(ASCIIToUTF16("PEANUT BUTTER"), menu_model->GetLabelAt(0)); | 829 EXPECT_EQ(ASCIIToUTF16("PEANUT BUTTER"), menu_model->GetLabelAt(0)); |
822 EXPECT_EQ(ASCIIToUTF16("JELLY"), menu_model->GetLabelAt(1)); | 830 EXPECT_EQ(ASCIIToUTF16("JELLY"), menu_model->GetLabelAt(1)); |
823 EXPECT_FALSE(menu_model->IsVisibleAt(0)); | 831 EXPECT_FALSE(menu_model->IsVisibleAt(0)); |
824 EXPECT_TRUE(menu_model->IsVisibleAt(1)); | 832 EXPECT_TRUE(menu_model->IsVisibleAt(1)); |
825 } | 833 } |
826 | 834 |
827 } // namespace views | 835 } // namespace views |
OLD | NEW |