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

Side by Side Diff: ui/views/controls/button/menu_button_unittest.cc

Issue 1798523003: Moved AppMenu ACTIVATED/DEACTIVATED ink drop handling in to MenuButton. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@2661
Patch Set: Created 4 years, 9 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/button/menu_button.cc ('k') | ui/views/views.gyp » ('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 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
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 explicit PressStateMenuButtonListener(bool release_lock)
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
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 67 matching lines...) Expand 10 before | Expand all | Expand 10 after
370 button()->set_drag_controller(&drag_controller); 422 button()->set_drag_controller(&drag_controller);
371 423
372 generator()->PressLeftButton(); 424 generator()->PressLeftButton();
373 EXPECT_EQ(nullptr, menu_button_listener.last_source()); 425 EXPECT_EQ(nullptr, menu_button_listener.last_source());
374 426
375 generator()->ReleaseLeftButton(); 427 generator()->ReleaseLeftButton();
376 EXPECT_EQ(button(), menu_button_listener.last_source()); 428 EXPECT_EQ(button(), menu_button_listener.last_source());
377 EXPECT_EQ(Button::STATE_HOVERED, menu_button_listener.last_source_state()); 429 EXPECT_EQ(Button::STATE_HOVERED, menu_button_listener.last_source_state());
378 } 430 }
379 431
432 TEST_F(MenuButtonTest, InkDropStateForMenuButtonActivationsWithoutListener) {
433 CreateMenuButtonWithNoListener();
434 TestInkDropDelegate ink_drop_delegate;
435 ink_drop_delegate.OnAction(InkDropState::ACTION_PENDING);
436 button()->set_ink_drop_delegate(&ink_drop_delegate);
437 button()->Activate(nullptr);
438
439 EXPECT_EQ(InkDropState::HIDDEN, ink_drop_delegate.state());
440 }
441
442 TEST_F(MenuButtonTest,
443 InkDropStateForMenuButtonActivationsWithListenerThatDoesntAcquireALock) {
444 TestMenuButtonListener menu_button_listener;
445 CreateMenuButtonWithMenuButtonListener(&menu_button_listener);
446 TestInkDropDelegate ink_drop_delegate;
447 button()->set_ink_drop_delegate(&ink_drop_delegate);
448 button()->Activate(nullptr);
449
450 EXPECT_EQ(InkDropState::QUICK_ACTION, ink_drop_delegate.state());
451 }
452
453 TEST_F(
454 MenuButtonTest,
455 InkDropStateForMenuButtonActivationsWithListenerThatDontReleaseAllLocks) {
456 PressStateMenuButtonListener menu_button_listener(false);
457 CreateMenuButtonWithMenuButtonListener(&menu_button_listener);
458 menu_button_listener.set_menu_button(button());
459 TestInkDropDelegate ink_drop_delegate;
460 button()->set_ink_drop_delegate(&ink_drop_delegate);
461 button()->Activate(nullptr);
462
463 EXPECT_EQ(InkDropState::ACTIVATED, ink_drop_delegate.state());
464
465 // Prevent the button from accessing invalid memory during clean up.
466 button()->set_ink_drop_delegate(nullptr);
467 }
468
469 TEST_F(MenuButtonTest,
470 InkDropStateForMenuButtonActivationsWithListenerThatReleaseAllLocks) {
471 PressStateMenuButtonListener menu_button_listener(true);
472 CreateMenuButtonWithMenuButtonListener(&menu_button_listener);
473 menu_button_listener.set_menu_button(button());
474 TestInkDropDelegate ink_drop_delegate;
475 button()->set_ink_drop_delegate(&ink_drop_delegate);
476 button()->Activate(nullptr);
477
478 EXPECT_EQ(InkDropState::DEACTIVATED, ink_drop_delegate.state());
479 }
480
481 TEST_F(MenuButtonTest, InkDropStateForMenuButtonsWithPressedLocks) {
482 CreateMenuButtonWithNoListener();
483 TestInkDropDelegate ink_drop_delegate;
484 button()->set_ink_drop_delegate(&ink_drop_delegate);
485
486 scoped_ptr<MenuButton::PressedLock> pressed_lock1(
487 new MenuButton::PressedLock(button()));
488
489 EXPECT_EQ(InkDropState::ACTIVATED, ink_drop_delegate.state());
490
491 scoped_ptr<MenuButton::PressedLock> pressed_lock2(
492 new MenuButton::PressedLock(button()));
493
494 EXPECT_EQ(InkDropState::ACTIVATED, ink_drop_delegate.state());
495
496 pressed_lock1.reset();
497 EXPECT_EQ(InkDropState::ACTIVATED, ink_drop_delegate.state());
498
499 pressed_lock2.reset();
500 EXPECT_EQ(InkDropState::DEACTIVATED, ink_drop_delegate.state());
501 }
502
503 // Verifies only one ink drop animation is triggered when multiple PressedLocks
504 // are attached to a MenuButton.
505 TEST_F(MenuButtonTest, OneInkDropAnimationForReentrantPressedLocks) {
506 CreateMenuButtonWithNoListener();
507 TestInkDropDelegate ink_drop_delegate;
508 button()->set_ink_drop_delegate(&ink_drop_delegate);
509
510 scoped_ptr<MenuButton::PressedLock> pressed_lock1(
511 new MenuButton::PressedLock(button()));
512
513 EXPECT_EQ(InkDropState::ACTIVATED, ink_drop_delegate.state());
514 ink_drop_delegate.OnAction(InkDropState::ACTION_PENDING);
515
516 scoped_ptr<MenuButton::PressedLock> pressed_lock2(
517 new MenuButton::PressedLock(button()));
518
519 EXPECT_EQ(InkDropState::ACTION_PENDING, ink_drop_delegate.state());
520 }
521
522 // Verifies the InkDropState is left as ACTIVATED if a PressedLock is active
523 // before another Activation occurs.
524 TEST_F(MenuButtonTest,
525 InkDropStateForMenuButtonWithPressedLockBeforeActivation) {
526 TestMenuButtonListener menu_button_listener;
527 CreateMenuButtonWithMenuButtonListener(&menu_button_listener);
528 TestInkDropDelegate ink_drop_delegate;
529 button()->set_ink_drop_delegate(&ink_drop_delegate);
530 MenuButton::PressedLock lock(button());
531
532 button()->Activate(nullptr);
533
534 EXPECT_EQ(InkDropState::ACTIVATED, ink_drop_delegate.state());
535 }
536
380 #if defined(USE_AURA) 537 #if defined(USE_AURA)
381 538
382 // Tests that the MenuButton does not become pressed if it can be dragged, and a 539 // Tests that the MenuButton does not become pressed if it can be dragged, and a
383 // DragDropClient is processing the events. 540 // DragDropClient is processing the events.
384 TEST_F(MenuButtonTest, DraggableMenuButtonDoesNotActivateOnDrag) { 541 TEST_F(MenuButtonTest, DraggableMenuButtonDoesNotActivateOnDrag) {
385 TestMenuButtonListener menu_button_listener; 542 TestMenuButtonListener menu_button_listener;
386 CreateMenuButtonWithMenuButtonListener(&menu_button_listener); 543 CreateMenuButtonWithMenuButtonListener(&menu_button_listener);
387 TestDragController drag_controller; 544 TestDragController drag_controller;
388 button()->set_drag_controller(&drag_controller); 545 button()->set_drag_controller(&drag_controller);
389 546
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after
445 602
446 generator()->MoveTouch(gfx::Point(10, 30)); 603 generator()->MoveTouch(gfx::Point(10, 30));
447 generator()->ReleaseTouch(); 604 generator()->ReleaseTouch();
448 EXPECT_EQ(Button::STATE_NORMAL, button()->state()); 605 EXPECT_EQ(Button::STATE_NORMAL, button()->state());
449 EXPECT_EQ(nullptr, menu_button_listener.last_source()); 606 EXPECT_EQ(nullptr, menu_button_listener.last_source());
450 } 607 }
451 608
452 #endif // !defined(OS_MACOSX) || defined(USE_AURA) 609 #endif // !defined(OS_MACOSX) || defined(USE_AURA)
453 610
454 } // namespace views 611 } // namespace views
612 } // namespace test
OLDNEW
« no previous file with comments | « ui/views/controls/button/menu_button.cc ('k') | ui/views/views.gyp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698