Chromium Code Reviews| 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 "base/macros.h" | 7 #include "base/macros.h" |
| 8 #include "base/memory/scoped_ptr.h" | 8 #include "base/memory/scoped_ptr.h" |
| 9 #include "base/strings/utf_string_conversions.h" | 9 #include "base/strings/utf_string_conversions.h" |
| 10 #include "build/build_config.h" | 10 #include "build/build_config.h" |
| 11 #include "ui/base/dragdrop/drag_drop_types.h" | 11 #include "ui/base/dragdrop/drag_drop_types.h" |
| 12 #include "ui/events/test/event_generator.h" | 12 #include "ui/events/test/event_generator.h" |
| 13 #include "ui/views/animation/test/test_ink_drop_delegate.h" | |
| 13 #include "ui/views/controls/button/menu_button_listener.h" | 14 #include "ui/views/controls/button/menu_button_listener.h" |
| 14 #include "ui/views/drag_controller.h" | 15 #include "ui/views/drag_controller.h" |
| 15 #include "ui/views/test/views_test_base.h" | 16 #include "ui/views/test/views_test_base.h" |
| 16 | 17 |
| 17 #if defined(USE_AURA) | 18 #if defined(USE_AURA) |
| 18 #include "ui/events/event.h" | 19 #include "ui/events/event.h" |
| 19 #include "ui/events/event_handler.h" | 20 #include "ui/events/event_handler.h" |
| 20 #include "ui/wm/public/drag_drop_client.h" | 21 #include "ui/wm/public/drag_drop_client.h" |
| 21 #endif | 22 #endif |
| 22 | 23 |
| 23 using base::ASCIIToUTF16; | 24 using base::ASCIIToUTF16; |
| 24 | 25 |
| 25 namespace views { | 26 namespace views { |
| 27 class InkDropDelegate; | |
| 28 | |
| 29 namespace test { | |
| 30 | |
| 31 // A MenuButton subclass that provides access to some MenuButton internals. | |
| 32 class TestMenuButton : public MenuButton { | |
| 33 public: | |
| 34 explicit TestMenuButton(MenuButtonListener* menu_button_listener) | |
| 35 : MenuButton(base::string16(ASCIIToUTF16("button")), | |
| 36 menu_button_listener, | |
| 37 false) {} | |
| 38 | |
| 39 ~TestMenuButton() override {} | |
| 40 | |
| 41 // Accessors to protected MenuButton methods. | |
| 42 void set_ink_drop_delegate(InkDropDelegate* ink_drop_delegate) { | |
| 43 MenuButton::set_ink_drop_delegate(ink_drop_delegate); | |
| 44 } | |
| 45 | |
| 46 private: | |
| 47 DISALLOW_COPY_AND_ASSIGN(TestMenuButton); | |
| 48 }; | |
| 26 | 49 |
| 27 class MenuButtonTest : public ViewsTestBase { | 50 class MenuButtonTest : public ViewsTestBase { |
| 28 public: | 51 public: |
| 29 MenuButtonTest() : widget_(nullptr), button_(nullptr) {} | 52 MenuButtonTest() : widget_(nullptr), button_(nullptr) {} |
| 30 ~MenuButtonTest() override {} | 53 ~MenuButtonTest() override {} |
| 31 | 54 |
| 32 void TearDown() override { | 55 void TearDown() override { |
| 33 generator_.reset(); | 56 generator_.reset(); |
| 34 if (widget_ && !widget_->IsClosed()) | 57 if (widget_ && !widget_->IsClosed()) |
| 35 widget_->Close(); | 58 widget_->Close(); |
| 36 | 59 |
| 37 ViewsTestBase::TearDown(); | 60 ViewsTestBase::TearDown(); |
| 38 } | 61 } |
| 39 | 62 |
| 40 Widget* widget() { return widget_; } | 63 Widget* widget() { return widget_; } |
| 41 MenuButton* button() { return button_; } | 64 TestMenuButton* button() { return button_; } |
| 42 ui::test::EventGenerator* generator() { return generator_.get(); } | 65 ui::test::EventGenerator* generator() { return generator_.get(); } |
| 43 | 66 |
| 44 protected: | 67 protected: |
| 45 // Creates a MenuButton with no button listener. | 68 // Creates a MenuButton with no button listener. |
| 46 void CreateMenuButtonWithNoListener() { CreateMenuButton(nullptr); } | 69 void CreateMenuButtonWithNoListener() { CreateMenuButton(nullptr); } |
| 47 | 70 |
| 48 // Creates a MenuButton with a MenuButtonListener. In this case, when the | 71 // Creates a MenuButton with a MenuButtonListener. In this case, when the |
| 49 // MenuButton is pushed, it notifies the MenuButtonListener to open a | 72 // MenuButton is pushed, it notifies the MenuButtonListener to open a |
| 50 // drop-down menu. | 73 // drop-down menu. |
| 51 void CreateMenuButtonWithMenuButtonListener( | 74 void CreateMenuButtonWithMenuButtonListener( |
| 52 MenuButtonListener* menu_button_listener) { | 75 MenuButtonListener* menu_button_listener) { |
| 53 CreateMenuButton(menu_button_listener); | 76 CreateMenuButton(menu_button_listener); |
| 54 } | 77 } |
| 55 | 78 |
| 56 private: | 79 private: |
| 57 void CreateMenuButton(MenuButtonListener* menu_button_listener) { | 80 void CreateMenuButton(MenuButtonListener* menu_button_listener) { |
| 58 CreateWidget(); | 81 CreateWidget(); |
| 59 generator_.reset(new ui::test::EventGenerator(GetContext(), | 82 generator_.reset(new ui::test::EventGenerator(GetContext(), |
| 60 widget_->GetNativeWindow())); | 83 widget_->GetNativeWindow())); |
| 61 // Set initial mouse location in a consistent way so that the menu button we | 84 // Set initial mouse location in a consistent way so that the menu button we |
| 62 // are about to create initializes its hover state in a consistent manner. | 85 // are about to create initializes its hover state in a consistent manner. |
| 63 generator_->set_current_location(gfx::Point(10, 10)); | 86 generator_->set_current_location(gfx::Point(10, 10)); |
| 64 | 87 |
| 65 const base::string16 label(ASCIIToUTF16("button")); | 88 button_ = new TestMenuButton(menu_button_listener); |
| 66 button_ = new MenuButton(label, menu_button_listener, false); | |
| 67 button_->SetBoundsRect(gfx::Rect(0, 0, 200, 20)); | 89 button_->SetBoundsRect(gfx::Rect(0, 0, 200, 20)); |
| 68 widget_->SetContentsView(button_); | 90 widget_->SetContentsView(button_); |
| 69 | 91 |
| 70 widget_->Show(); | 92 widget_->Show(); |
| 71 } | 93 } |
| 72 | 94 |
| 73 void CreateWidget() { | 95 void CreateWidget() { |
| 74 DCHECK(!widget_); | 96 DCHECK(!widget_); |
| 75 | 97 |
| 76 widget_ = new Widget; | 98 widget_ = new Widget; |
| 77 Widget::InitParams params = | 99 Widget::InitParams params = |
| 78 CreateParams(Widget::InitParams::TYPE_WINDOW_FRAMELESS); | 100 CreateParams(Widget::InitParams::TYPE_WINDOW_FRAMELESS); |
| 79 params.bounds = gfx::Rect(0, 0, 200, 200); | 101 params.bounds = gfx::Rect(0, 0, 200, 200); |
| 80 widget_->Init(params); | 102 widget_->Init(params); |
| 81 } | 103 } |
| 82 | 104 |
| 83 Widget* widget_; | 105 Widget* widget_; |
| 84 MenuButton* button_; | 106 TestMenuButton* button_; |
| 85 scoped_ptr<ui::test::EventGenerator> generator_; | 107 scoped_ptr<ui::test::EventGenerator> generator_; |
| 86 | 108 |
| 87 DISALLOW_COPY_AND_ASSIGN(MenuButtonTest); | 109 DISALLOW_COPY_AND_ASSIGN(MenuButtonTest); |
| 88 }; | 110 }; |
| 89 | 111 |
| 90 class TestButtonListener : public ButtonListener { | 112 class TestButtonListener : public ButtonListener { |
| 91 public: | 113 public: |
| 92 TestButtonListener() | 114 TestButtonListener() |
| 93 : last_sender_(nullptr), | 115 : last_sender_(nullptr), |
| 94 last_sender_state_(Button::STATE_NORMAL), | 116 last_sender_state_(Button::STATE_NORMAL), |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 133 View* last_source() { return last_source_; } | 155 View* last_source() { return last_source_; } |
| 134 Button::ButtonState last_source_state() { return last_source_state_; } | 156 Button::ButtonState last_source_state() { return last_source_state_; } |
| 135 | 157 |
| 136 private: | 158 private: |
| 137 View* last_source_; | 159 View* last_source_; |
| 138 Button::ButtonState last_source_state_; | 160 Button::ButtonState last_source_state_; |
| 139 | 161 |
| 140 DISALLOW_COPY_AND_ASSIGN(TestMenuButtonListener); | 162 DISALLOW_COPY_AND_ASSIGN(TestMenuButtonListener); |
| 141 }; | 163 }; |
| 142 | 164 |
| 165 // A MenuButtonListener that will acquire a PressedLock in the | |
| 166 // OnMenuButtonClicked() method and optionally release it as well. | |
| 167 class PressStateMenuButtonListener : public MenuButtonListener { | |
| 168 public: | |
| 169 PressStateMenuButtonListener(bool release_lock) | |
|
sky
2016/03/09 16:21:38
explicit
bruthig
2016/03/11 01:09:50
Done.
| |
| 170 : menu_button_(nullptr), release_lock_(release_lock) {} | |
| 171 | |
| 172 ~PressStateMenuButtonListener() override {} | |
| 173 | |
| 174 void set_menu_button(MenuButton* menu_button) { menu_button_ = menu_button; } | |
| 175 | |
| 176 void OnMenuButtonClicked(MenuButton* source, | |
| 177 const gfx::Point& point, | |
| 178 const ui::Event* event) override { | |
| 179 pressed_lock_.reset(new MenuButton::PressedLock(menu_button_)); | |
| 180 if (release_lock_) | |
| 181 pressed_lock_.reset(); | |
| 182 } | |
| 183 | |
| 184 private: | |
| 185 MenuButton* menu_button_; | |
| 186 | |
| 187 scoped_ptr<MenuButton::PressedLock> pressed_lock_; | |
| 188 | |
| 189 // The |pressed_lock_| will be released when true. | |
| 190 bool release_lock_; | |
| 191 | |
| 192 DISALLOW_COPY_AND_ASSIGN(PressStateMenuButtonListener); | |
| 193 }; | |
| 194 | |
| 143 // Basic implementation of a DragController, to test input behaviour for | 195 // Basic implementation of a DragController, to test input behaviour for |
| 144 // MenuButtons that can be dragged. | 196 // MenuButtons that can be dragged. |
| 145 class TestDragController : public DragController { | 197 class TestDragController : public DragController { |
| 146 public: | 198 public: |
| 147 TestDragController() {} | 199 TestDragController() {} |
| 148 ~TestDragController() override {} | 200 ~TestDragController() override {} |
| 149 | 201 |
| 150 void WriteDragDataForView(View* sender, | 202 void WriteDragDataForView(View* sender, |
| 151 const gfx::Point& press_pt, | 203 const gfx::Point& press_pt, |
| 152 ui::OSExchangeData* data) override {} | 204 ui::OSExchangeData* data) override {} |
| (...skipping 129 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 282 | 334 |
| 283 generator()->ClickLeftButton(); | 335 generator()->ClickLeftButton(); |
| 284 | 336 |
| 285 // Check that MenuButton has notified the listener, while it was in pressed | 337 // Check that MenuButton has notified the listener, while it was in pressed |
| 286 // state. | 338 // state. |
| 287 EXPECT_EQ(button(), menu_button_listener.last_source()); | 339 EXPECT_EQ(button(), menu_button_listener.last_source()); |
| 288 EXPECT_EQ(Button::STATE_HOVERED, menu_button_listener.last_source_state()); | 340 EXPECT_EQ(Button::STATE_HOVERED, menu_button_listener.last_source_state()); |
| 289 } | 341 } |
| 290 | 342 |
| 291 // Test that the MenuButton stays pressed while there are any PressedLocks. | 343 // Test that the MenuButton stays pressed while there are any PressedLocks. |
| 292 TEST_F(MenuButtonTest, MenuButtonPressedLock) { | 344 TEST_F(MenuButtonTest, ButtonStateForMenuButtonsWithPressedLocks) { |
| 293 CreateMenuButtonWithNoListener(); | 345 CreateMenuButtonWithNoListener(); |
| 294 | 346 |
| 295 // Move the mouse over the button; the button should be in a hovered state. | 347 // Move the mouse over the button; the button should be in a hovered state. |
| 296 generator()->MoveMouseTo(gfx::Point(10, 10)); | 348 generator()->MoveMouseTo(gfx::Point(10, 10)); |
| 297 EXPECT_EQ(Button::STATE_HOVERED, button()->state()); | 349 EXPECT_EQ(Button::STATE_HOVERED, button()->state()); |
| 298 | 350 |
| 299 // Introduce a PressedLock, which should make the button pressed. | 351 // Introduce a PressedLock, which should make the button pressed. |
| 300 scoped_ptr<MenuButton::PressedLock> pressed_lock1( | 352 scoped_ptr<MenuButton::PressedLock> pressed_lock1( |
| 301 new MenuButton::PressedLock(button())); | 353 new MenuButton::PressedLock(button())); |
| 302 EXPECT_EQ(Button::STATE_PRESSED, button()->state()); | 354 EXPECT_EQ(Button::STATE_PRESSED, button()->state()); |
| (...skipping 139 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 442 CreateMenuButtonWithMenuButtonListener(&menu_button_listener); | 494 CreateMenuButtonWithMenuButtonListener(&menu_button_listener); |
| 443 generator()->PressTouch(); | 495 generator()->PressTouch(); |
| 444 EXPECT_EQ(Button::STATE_HOVERED, button()->state()); | 496 EXPECT_EQ(Button::STATE_HOVERED, button()->state()); |
| 445 | 497 |
| 446 generator()->MoveTouch(gfx::Point(10, 30)); | 498 generator()->MoveTouch(gfx::Point(10, 30)); |
| 447 generator()->ReleaseTouch(); | 499 generator()->ReleaseTouch(); |
| 448 EXPECT_EQ(Button::STATE_NORMAL, button()->state()); | 500 EXPECT_EQ(Button::STATE_NORMAL, button()->state()); |
| 449 EXPECT_EQ(nullptr, menu_button_listener.last_source()); | 501 EXPECT_EQ(nullptr, menu_button_listener.last_source()); |
| 450 } | 502 } |
| 451 | 503 |
| 504 TEST_F(MenuButtonTest, InkDropStateForMenuButtonActivationsWithoutListener) { | |
| 505 CreateMenuButtonWithNoListener(); | |
| 506 TestInkDropDelegate ink_drop_delegate; | |
| 507 ink_drop_delegate.OnAction(InkDropState::ACTION_PENDING); | |
| 508 button()->set_ink_drop_delegate(&ink_drop_delegate); | |
| 509 button()->Activate(nullptr); | |
| 510 | |
| 511 EXPECT_EQ(InkDropState::HIDDEN, ink_drop_delegate.state()); | |
| 512 } | |
| 513 | |
| 514 TEST_F(MenuButtonTest, | |
| 515 InkDropStateForMenuButtonActivationsWithListenerThatDoesntAcquireALock) { | |
| 516 TestMenuButtonListener menu_button_listener; | |
| 517 CreateMenuButtonWithMenuButtonListener(&menu_button_listener); | |
| 518 TestInkDropDelegate ink_drop_delegate; | |
| 519 button()->set_ink_drop_delegate(&ink_drop_delegate); | |
| 520 button()->Activate(nullptr); | |
| 521 | |
| 522 EXPECT_EQ(InkDropState::QUICK_ACTION, ink_drop_delegate.state()); | |
| 523 } | |
| 524 | |
| 525 TEST_F( | |
| 526 MenuButtonTest, | |
| 527 InkDropStateForMenuButtonActivationsWithListenerThatDontReleaseAllLocks) { | |
| 528 PressStateMenuButtonListener menu_button_listener(false); | |
| 529 CreateMenuButtonWithMenuButtonListener(&menu_button_listener); | |
| 530 menu_button_listener.set_menu_button(button()); | |
| 531 TestInkDropDelegate ink_drop_delegate; | |
| 532 button()->set_ink_drop_delegate(&ink_drop_delegate); | |
| 533 button()->Activate(nullptr); | |
| 534 | |
| 535 EXPECT_EQ(InkDropState::ACTIVATED, ink_drop_delegate.state()); | |
| 536 } | |
| 537 | |
| 538 TEST_F(MenuButtonTest, | |
| 539 InkDropStateForMenuButtonActivationsWithListenerThatReleaseAllLocks) { | |
| 540 PressStateMenuButtonListener menu_button_listener(true); | |
| 541 CreateMenuButtonWithMenuButtonListener(&menu_button_listener); | |
| 542 menu_button_listener.set_menu_button(button()); | |
| 543 TestInkDropDelegate ink_drop_delegate; | |
| 544 button()->set_ink_drop_delegate(&ink_drop_delegate); | |
| 545 button()->Activate(nullptr); | |
| 546 | |
| 547 EXPECT_EQ(InkDropState::DEACTIVATED, ink_drop_delegate.state()); | |
| 548 } | |
| 549 | |
| 550 TEST_F(MenuButtonTest, InkDropStateForMenuButtonsWithPressedLocks) { | |
| 551 CreateMenuButtonWithNoListener(); | |
| 552 TestInkDropDelegate ink_drop_delegate; | |
| 553 button()->set_ink_drop_delegate(&ink_drop_delegate); | |
| 554 | |
| 555 scoped_ptr<MenuButton::PressedLock> pressed_lock1( | |
| 556 new MenuButton::PressedLock(button())); | |
| 557 | |
| 558 EXPECT_EQ(InkDropState::ACTIVATED, ink_drop_delegate.state()); | |
| 559 | |
| 560 scoped_ptr<MenuButton::PressedLock> pressed_lock2( | |
| 561 new MenuButton::PressedLock(button())); | |
| 562 | |
| 563 EXPECT_EQ(InkDropState::ACTIVATED, ink_drop_delegate.state()); | |
| 564 | |
| 565 pressed_lock1.reset(); | |
| 566 EXPECT_EQ(InkDropState::ACTIVATED, ink_drop_delegate.state()); | |
| 567 | |
| 568 pressed_lock2.reset(); | |
| 569 EXPECT_EQ(InkDropState::DEACTIVATED, ink_drop_delegate.state()); | |
| 570 } | |
| 571 | |
| 572 // Verifies only one ink drop animation is triggered when multiple PressedLocks | |
| 573 // are attached to a MenuButton. | |
| 574 TEST_F(MenuButtonTest, OneInkDropAnimationForReentrantPressedLocks) { | |
| 575 CreateMenuButtonWithNoListener(); | |
| 576 TestInkDropDelegate ink_drop_delegate; | |
| 577 button()->set_ink_drop_delegate(&ink_drop_delegate); | |
| 578 | |
| 579 scoped_ptr<MenuButton::PressedLock> pressed_lock1( | |
| 580 new MenuButton::PressedLock(button())); | |
| 581 | |
| 582 EXPECT_EQ(InkDropState::ACTIVATED, ink_drop_delegate.state()); | |
| 583 ink_drop_delegate.OnAction(InkDropState::ACTION_PENDING); | |
| 584 | |
| 585 scoped_ptr<MenuButton::PressedLock> pressed_lock2( | |
| 586 new MenuButton::PressedLock(button())); | |
| 587 | |
| 588 EXPECT_EQ(InkDropState::ACTION_PENDING, ink_drop_delegate.state()); | |
| 589 } | |
| 590 | |
| 452 #endif // !defined(OS_MACOSX) || defined(USE_AURA) | 591 #endif // !defined(OS_MACOSX) || defined(USE_AURA) |
| 453 | 592 |
| 454 } // namespace views | 593 } // namespace views |
| 594 } // namespace test | |
| OLD | NEW |