OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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/button/menu_button.h" | 5 #include "ui/views/controls/button/menu_button.h" |
6 | 6 |
7 #include <memory> | 7 #include <memory> |
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" |
11 #include "build/build_config.h" | 11 #include "build/build_config.h" |
12 #include "ui/base/dragdrop/drag_drop_types.h" | 12 #include "ui/base/dragdrop/drag_drop_types.h" |
13 #include "ui/events/test/event_generator.h" | 13 #include "ui/events/test/event_generator.h" |
14 #include "ui/views/animation/test/test_ink_drop_delegate.h" | 14 #include "ui/views/animation/test/ink_drop_host_view_test_api.h" |
| 15 #include "ui/views/animation/test/test_ink_drop.h" |
15 #include "ui/views/controls/button/menu_button_listener.h" | 16 #include "ui/views/controls/button/menu_button_listener.h" |
16 #include "ui/views/drag_controller.h" | 17 #include "ui/views/drag_controller.h" |
17 #include "ui/views/test/views_test_base.h" | 18 #include "ui/views/test/views_test_base.h" |
18 | 19 |
19 #if defined(USE_AURA) | 20 #if defined(USE_AURA) |
20 #include "ui/events/event.h" | 21 #include "ui/events/event.h" |
21 #include "ui/events/event_handler.h" | 22 #include "ui/events/event_handler.h" |
22 #include "ui/wm/public/drag_drop_client.h" | 23 #include "ui/wm/public/drag_drop_client.h" |
23 #endif | 24 #endif |
24 | 25 |
25 using base::ASCIIToUTF16; | 26 using base::ASCIIToUTF16; |
26 | 27 |
27 namespace views { | 28 namespace views { |
28 class InkDropDelegate; | |
29 | 29 |
30 namespace test { | 30 using test::InkDropHostViewTestApi; |
| 31 using test::TestInkDrop; |
31 | 32 |
32 // A MenuButton subclass that provides access to some MenuButton internals. | 33 // A MenuButton subclass that provides access to some MenuButton internals. |
33 class TestMenuButton : public MenuButton { | 34 class TestMenuButton : public MenuButton { |
34 public: | 35 public: |
35 explicit TestMenuButton(MenuButtonListener* menu_button_listener) | 36 explicit TestMenuButton(MenuButtonListener* menu_button_listener) |
36 : MenuButton(base::string16(ASCIIToUTF16("button")), | 37 : MenuButton(base::string16(ASCIIToUTF16("button")), |
37 menu_button_listener, | 38 menu_button_listener, |
38 false) {} | 39 false) {} |
39 | 40 |
40 ~TestMenuButton() override {} | 41 ~TestMenuButton() override {} |
41 | 42 |
42 // Accessors to protected MenuButton methods. | 43 void SetInkDrop(std::unique_ptr<InkDrop> ink_drop) { |
43 void set_ink_drop_delegate( | 44 InkDropHostViewTestApi(this).SetInkDrop(std::move(ink_drop)); |
44 std::unique_ptr<InkDropDelegate> ink_drop_delegate) { | |
45 InkDropHostView::set_ink_drop_delegate(std::move(ink_drop_delegate)); | |
46 } | 45 } |
47 | 46 |
48 private: | 47 private: |
49 DISALLOW_COPY_AND_ASSIGN(TestMenuButton); | 48 DISALLOW_COPY_AND_ASSIGN(TestMenuButton); |
50 }; | 49 }; |
51 | 50 |
52 class MenuButtonTest : public ViewsTestBase { | 51 class MenuButtonTest : public ViewsTestBase { |
53 public: | 52 public: |
54 MenuButtonTest() : widget_(nullptr), button_(nullptr) {} | 53 MenuButtonTest() : widget_(nullptr), button_(nullptr), ink_drop_(nullptr) {} |
55 ~MenuButtonTest() override {} | 54 ~MenuButtonTest() override {} |
56 | 55 |
57 void TearDown() override { | 56 void TearDown() override { |
58 generator_.reset(); | 57 generator_.reset(); |
59 if (widget_ && !widget_->IsClosed()) | 58 if (widget_ && !widget_->IsClosed()) |
60 widget_->Close(); | 59 widget_->Close(); |
61 | 60 |
62 ViewsTestBase::TearDown(); | 61 ViewsTestBase::TearDown(); |
63 } | 62 } |
64 | 63 |
65 Widget* widget() { return widget_; } | 64 Widget* widget() { return widget_; } |
66 TestMenuButton* button() { return button_; } | 65 TestMenuButton* button() { return button_; } |
67 ui::test::EventGenerator* generator() { return generator_.get(); } | 66 ui::test::EventGenerator* generator() { return generator_.get(); } |
68 | 67 |
69 protected: | 68 protected: |
| 69 TestInkDrop* ink_drop() { return ink_drop_; } |
| 70 |
70 // Creates a MenuButton with no button listener. | 71 // Creates a MenuButton with no button listener. |
71 void CreateMenuButtonWithNoListener() { CreateMenuButton(nullptr); } | 72 void CreateMenuButtonWithNoListener() { CreateMenuButton(nullptr); } |
72 | 73 |
73 // Creates a MenuButton with a MenuButtonListener. In this case, when the | 74 // Creates a MenuButton with a MenuButtonListener. In this case, when the |
74 // MenuButton is pushed, it notifies the MenuButtonListener to open a | 75 // MenuButton is pushed, it notifies the MenuButtonListener to open a |
75 // drop-down menu. | 76 // drop-down menu. |
76 void CreateMenuButtonWithMenuButtonListener( | 77 void CreateMenuButtonWithMenuButtonListener( |
77 MenuButtonListener* menu_button_listener) { | 78 MenuButtonListener* menu_button_listener) { |
78 CreateMenuButton(menu_button_listener); | 79 CreateMenuButton(menu_button_listener); |
79 } | 80 } |
80 | 81 |
| 82 void AttachInkDrop() { |
| 83 ink_drop_ = new test::TestInkDrop(); |
| 84 test::InkDropHostViewTestApi(button_).SetInkDrop( |
| 85 base::WrapUnique(ink_drop_)); |
| 86 } |
| 87 |
81 private: | 88 private: |
82 void CreateMenuButton(MenuButtonListener* menu_button_listener) { | 89 void CreateMenuButton(MenuButtonListener* menu_button_listener) { |
83 CreateWidget(); | 90 CreateWidget(); |
84 generator_.reset(new ui::test::EventGenerator(GetContext(), | 91 generator_.reset(new ui::test::EventGenerator(GetContext(), |
85 widget_->GetNativeWindow())); | 92 widget_->GetNativeWindow())); |
86 // Set initial mouse location in a consistent way so that the menu button we | 93 // Set initial mouse location in a consistent way so that the menu button we |
87 // are about to create initializes its hover state in a consistent manner. | 94 // are about to create initializes its hover state in a consistent manner. |
88 generator_->set_current_location(gfx::Point(10, 10)); | 95 generator_->set_current_location(gfx::Point(10, 10)); |
89 | 96 |
90 button_ = new TestMenuButton(menu_button_listener); | 97 button_ = new TestMenuButton(menu_button_listener); |
(...skipping 10 matching lines...) Expand all Loading... |
101 Widget::InitParams params = | 108 Widget::InitParams params = |
102 CreateParams(Widget::InitParams::TYPE_WINDOW_FRAMELESS); | 109 CreateParams(Widget::InitParams::TYPE_WINDOW_FRAMELESS); |
103 params.bounds = gfx::Rect(0, 0, 200, 200); | 110 params.bounds = gfx::Rect(0, 0, 200, 200); |
104 widget_->Init(params); | 111 widget_->Init(params); |
105 } | 112 } |
106 | 113 |
107 Widget* widget_; | 114 Widget* widget_; |
108 TestMenuButton* button_; | 115 TestMenuButton* button_; |
109 std::unique_ptr<ui::test::EventGenerator> generator_; | 116 std::unique_ptr<ui::test::EventGenerator> generator_; |
110 | 117 |
| 118 // Weak ptr, |button_| owns the instance. |
| 119 TestInkDrop* ink_drop_; |
| 120 |
111 DISALLOW_COPY_AND_ASSIGN(MenuButtonTest); | 121 DISALLOW_COPY_AND_ASSIGN(MenuButtonTest); |
112 }; | 122 }; |
113 | 123 |
114 class TestButtonListener : public ButtonListener { | 124 class TestButtonListener : public ButtonListener { |
115 public: | 125 public: |
116 TestButtonListener() | 126 TestButtonListener() |
117 : last_sender_(nullptr), | 127 : last_sender_(nullptr), |
118 last_sender_state_(Button::STATE_NORMAL), | 128 last_sender_state_(Button::STATE_NORMAL), |
119 last_event_type_(ui::ET_UNKNOWN) {} | 129 last_event_type_(ui::ET_UNKNOWN) {} |
120 ~TestButtonListener() override {} | 130 ~TestButtonListener() override {} |
(...skipping 305 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
426 generator()->PressLeftButton(); | 436 generator()->PressLeftButton(); |
427 EXPECT_EQ(nullptr, menu_button_listener.last_source()); | 437 EXPECT_EQ(nullptr, menu_button_listener.last_source()); |
428 | 438 |
429 generator()->ReleaseLeftButton(); | 439 generator()->ReleaseLeftButton(); |
430 EXPECT_EQ(button(), menu_button_listener.last_source()); | 440 EXPECT_EQ(button(), menu_button_listener.last_source()); |
431 EXPECT_EQ(Button::STATE_HOVERED, menu_button_listener.last_source_state()); | 441 EXPECT_EQ(Button::STATE_HOVERED, menu_button_listener.last_source_state()); |
432 } | 442 } |
433 | 443 |
434 TEST_F(MenuButtonTest, InkDropStateForMenuButtonActivationsWithoutListener) { | 444 TEST_F(MenuButtonTest, InkDropStateForMenuButtonActivationsWithoutListener) { |
435 CreateMenuButtonWithNoListener(); | 445 CreateMenuButtonWithNoListener(); |
436 TestInkDropDelegate* ink_drop_delegate = new TestInkDropDelegate(); | 446 AttachInkDrop(); |
437 ink_drop_delegate->OnAction(InkDropState::ACTION_PENDING); | 447 ink_drop()->AnimateToState(InkDropState::ACTION_PENDING); |
438 button()->set_ink_drop_delegate(base::WrapUnique(ink_drop_delegate)); | |
439 button()->Activate(nullptr); | 448 button()->Activate(nullptr); |
440 | 449 |
441 EXPECT_EQ(InkDropState::HIDDEN, ink_drop_delegate->GetTargetInkDropState()); | 450 EXPECT_EQ(InkDropState::HIDDEN, ink_drop()->GetTargetInkDropState()); |
442 } | 451 } |
443 | 452 |
444 TEST_F(MenuButtonTest, | 453 TEST_F(MenuButtonTest, |
445 InkDropStateForMenuButtonActivationsWithListenerThatDoesntAcquireALock) { | 454 InkDropStateForMenuButtonActivationsWithListenerThatDoesntAcquireALock) { |
446 TestMenuButtonListener menu_button_listener; | 455 TestMenuButtonListener menu_button_listener; |
447 CreateMenuButtonWithMenuButtonListener(&menu_button_listener); | 456 CreateMenuButtonWithMenuButtonListener(&menu_button_listener); |
448 TestInkDropDelegate* ink_drop_delegate = new TestInkDropDelegate(); | 457 AttachInkDrop(); |
449 button()->set_ink_drop_delegate(base::WrapUnique(ink_drop_delegate)); | |
450 button()->Activate(nullptr); | 458 button()->Activate(nullptr); |
451 | 459 |
452 EXPECT_EQ(InkDropState::ACTION_TRIGGERED, | 460 EXPECT_EQ(InkDropState::ACTION_TRIGGERED, |
453 ink_drop_delegate->GetTargetInkDropState()); | 461 ink_drop()->GetTargetInkDropState()); |
454 } | 462 } |
455 | 463 |
456 TEST_F( | 464 TEST_F( |
457 MenuButtonTest, | 465 MenuButtonTest, |
458 InkDropStateForMenuButtonActivationsWithListenerThatDontReleaseAllLocks) { | 466 InkDropStateForMenuButtonActivationsWithListenerThatDontReleaseAllLocks) { |
459 PressStateMenuButtonListener menu_button_listener(false); | 467 PressStateMenuButtonListener menu_button_listener(false); |
460 CreateMenuButtonWithMenuButtonListener(&menu_button_listener); | 468 CreateMenuButtonWithMenuButtonListener(&menu_button_listener); |
| 469 AttachInkDrop(); |
461 menu_button_listener.set_menu_button(button()); | 470 menu_button_listener.set_menu_button(button()); |
462 TestInkDropDelegate* ink_drop_delegate = new TestInkDropDelegate(); | |
463 button()->set_ink_drop_delegate(base::WrapUnique(ink_drop_delegate)); | |
464 button()->Activate(nullptr); | 471 button()->Activate(nullptr); |
465 | 472 |
466 EXPECT_EQ(InkDropState::ACTIVATED, | 473 EXPECT_EQ(InkDropState::ACTIVATED, ink_drop()->GetTargetInkDropState()); |
467 ink_drop_delegate->GetTargetInkDropState()); | |
468 } | 474 } |
469 | 475 |
470 TEST_F(MenuButtonTest, | 476 TEST_F(MenuButtonTest, |
471 InkDropStateForMenuButtonActivationsWithListenerThatReleaseAllLocks) { | 477 InkDropStateForMenuButtonActivationsWithListenerThatReleaseAllLocks) { |
472 PressStateMenuButtonListener menu_button_listener(true); | 478 PressStateMenuButtonListener menu_button_listener(true); |
473 CreateMenuButtonWithMenuButtonListener(&menu_button_listener); | 479 CreateMenuButtonWithMenuButtonListener(&menu_button_listener); |
474 menu_button_listener.set_menu_button(button()); | 480 menu_button_listener.set_menu_button(button()); |
475 TestInkDropDelegate* ink_drop_delegate = new TestInkDropDelegate(); | 481 AttachInkDrop(); |
476 button()->set_ink_drop_delegate(base::WrapUnique(ink_drop_delegate)); | |
477 button()->Activate(nullptr); | 482 button()->Activate(nullptr); |
478 | 483 |
479 EXPECT_EQ(InkDropState::DEACTIVATED, | 484 EXPECT_EQ(InkDropState::DEACTIVATED, ink_drop()->GetTargetInkDropState()); |
480 ink_drop_delegate->GetTargetInkDropState()); | |
481 } | 485 } |
482 | 486 |
483 TEST_F(MenuButtonTest, InkDropStateForMenuButtonsWithPressedLocks) { | 487 TEST_F(MenuButtonTest, InkDropStateForMenuButtonsWithPressedLocks) { |
484 CreateMenuButtonWithNoListener(); | 488 CreateMenuButtonWithNoListener(); |
485 TestInkDropDelegate* ink_drop_delegate = new TestInkDropDelegate(); | 489 AttachInkDrop(); |
486 button()->set_ink_drop_delegate(base::WrapUnique(ink_drop_delegate)); | |
487 | 490 |
488 std::unique_ptr<MenuButton::PressedLock> pressed_lock1( | 491 std::unique_ptr<MenuButton::PressedLock> pressed_lock1( |
489 new MenuButton::PressedLock(button())); | 492 new MenuButton::PressedLock(button())); |
490 | 493 |
491 EXPECT_EQ(InkDropState::ACTIVATED, | 494 EXPECT_EQ(InkDropState::ACTIVATED, ink_drop()->GetTargetInkDropState()); |
492 ink_drop_delegate->GetTargetInkDropState()); | |
493 | 495 |
494 std::unique_ptr<MenuButton::PressedLock> pressed_lock2( | 496 std::unique_ptr<MenuButton::PressedLock> pressed_lock2( |
495 new MenuButton::PressedLock(button())); | 497 new MenuButton::PressedLock(button())); |
496 | 498 |
497 EXPECT_EQ(InkDropState::ACTIVATED, | 499 EXPECT_EQ(InkDropState::ACTIVATED, ink_drop()->GetTargetInkDropState()); |
498 ink_drop_delegate->GetTargetInkDropState()); | |
499 | 500 |
500 pressed_lock1.reset(); | 501 pressed_lock1.reset(); |
501 EXPECT_EQ(InkDropState::ACTIVATED, | 502 EXPECT_EQ(InkDropState::ACTIVATED, ink_drop()->GetTargetInkDropState()); |
502 ink_drop_delegate->GetTargetInkDropState()); | |
503 | 503 |
504 pressed_lock2.reset(); | 504 pressed_lock2.reset(); |
505 EXPECT_EQ(InkDropState::DEACTIVATED, | 505 EXPECT_EQ(InkDropState::DEACTIVATED, ink_drop()->GetTargetInkDropState()); |
506 ink_drop_delegate->GetTargetInkDropState()); | |
507 } | 506 } |
508 | 507 |
509 // Verifies only one ink drop animation is triggered when multiple PressedLocks | 508 // Verifies only one ink drop animation is triggered when multiple PressedLocks |
510 // are attached to a MenuButton. | 509 // are attached to a MenuButton. |
511 TEST_F(MenuButtonTest, OneInkDropAnimationForReentrantPressedLocks) { | 510 TEST_F(MenuButtonTest, OneInkDropAnimationForReentrantPressedLocks) { |
512 CreateMenuButtonWithNoListener(); | 511 CreateMenuButtonWithNoListener(); |
513 TestInkDropDelegate* ink_drop_delegate = new TestInkDropDelegate(); | 512 AttachInkDrop(); |
514 button()->set_ink_drop_delegate(base::WrapUnique(ink_drop_delegate)); | |
515 | 513 |
516 std::unique_ptr<MenuButton::PressedLock> pressed_lock1( | 514 std::unique_ptr<MenuButton::PressedLock> pressed_lock1( |
517 new MenuButton::PressedLock(button())); | 515 new MenuButton::PressedLock(button())); |
518 | 516 |
519 EXPECT_EQ(InkDropState::ACTIVATED, | 517 EXPECT_EQ(InkDropState::ACTIVATED, ink_drop()->GetTargetInkDropState()); |
520 ink_drop_delegate->GetTargetInkDropState()); | 518 ink_drop()->AnimateToState(InkDropState::ACTION_PENDING); |
521 ink_drop_delegate->OnAction(InkDropState::ACTION_PENDING); | |
522 | 519 |
523 std::unique_ptr<MenuButton::PressedLock> pressed_lock2( | 520 std::unique_ptr<MenuButton::PressedLock> pressed_lock2( |
524 new MenuButton::PressedLock(button())); | 521 new MenuButton::PressedLock(button())); |
525 | 522 |
526 EXPECT_EQ(InkDropState::ACTION_PENDING, | 523 EXPECT_EQ(InkDropState::ACTION_PENDING, ink_drop()->GetTargetInkDropState()); |
527 ink_drop_delegate->GetTargetInkDropState()); | |
528 } | 524 } |
529 | 525 |
530 // Verifies the InkDropState is left as ACTIVATED if a PressedLock is active | 526 // Verifies the InkDropState is left as ACTIVATED if a PressedLock is active |
531 // before another Activation occurs. | 527 // before another Activation occurs. |
532 TEST_F(MenuButtonTest, | 528 TEST_F(MenuButtonTest, |
533 InkDropStateForMenuButtonWithPressedLockBeforeActivation) { | 529 InkDropStateForMenuButtonWithPressedLockBeforeActivation) { |
534 TestMenuButtonListener menu_button_listener; | 530 TestMenuButtonListener menu_button_listener; |
535 CreateMenuButtonWithMenuButtonListener(&menu_button_listener); | 531 CreateMenuButtonWithMenuButtonListener(&menu_button_listener); |
536 TestInkDropDelegate* ink_drop_delegate = new TestInkDropDelegate(); | 532 AttachInkDrop(); |
537 button()->set_ink_drop_delegate(base::WrapUnique(ink_drop_delegate)); | |
538 MenuButton::PressedLock lock(button()); | 533 MenuButton::PressedLock lock(button()); |
539 | 534 |
540 button()->Activate(nullptr); | 535 button()->Activate(nullptr); |
541 | 536 |
542 EXPECT_EQ(InkDropState::ACTIVATED, | 537 EXPECT_EQ(InkDropState::ACTIVATED, ink_drop()->GetTargetInkDropState()); |
543 ink_drop_delegate->GetTargetInkDropState()); | |
544 } | 538 } |
545 | 539 |
546 #if defined(USE_AURA) | 540 #if defined(USE_AURA) |
547 | 541 |
548 // Tests that the MenuButton does not become pressed if it can be dragged, and a | 542 // Tests that the MenuButton does not become pressed if it can be dragged, and a |
549 // DragDropClient is processing the events. | 543 // DragDropClient is processing the events. |
550 TEST_F(MenuButtonTest, DraggableMenuButtonDoesNotActivateOnDrag) { | 544 TEST_F(MenuButtonTest, DraggableMenuButtonDoesNotActivateOnDrag) { |
551 TestMenuButtonListener menu_button_listener; | 545 TestMenuButtonListener menu_button_listener; |
552 CreateMenuButtonWithMenuButtonListener(&menu_button_listener); | 546 CreateMenuButtonWithMenuButtonListener(&menu_button_listener); |
553 TestDragController drag_controller; | 547 TestDragController drag_controller; |
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
611 | 605 |
612 generator()->MoveTouch(gfx::Point(10, 30)); | 606 generator()->MoveTouch(gfx::Point(10, 30)); |
613 generator()->ReleaseTouch(); | 607 generator()->ReleaseTouch(); |
614 EXPECT_EQ(Button::STATE_NORMAL, button()->state()); | 608 EXPECT_EQ(Button::STATE_NORMAL, button()->state()); |
615 EXPECT_EQ(nullptr, menu_button_listener.last_source()); | 609 EXPECT_EQ(nullptr, menu_button_listener.last_source()); |
616 } | 610 } |
617 | 611 |
618 #endif // !defined(OS_MACOSX) || defined(USE_AURA) | 612 #endif // !defined(OS_MACOSX) || defined(USE_AURA) |
619 | 613 |
620 } // namespace views | 614 } // namespace views |
621 } // namespace test | |
OLD | NEW |