| OLD | NEW |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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 "ash/shell.h" | 5 #include "ash/shell.h" |
| 6 #include "ash/test/ash_test_base.h" | 6 #include "ash/test/ash_test_base.h" |
| 7 #include "ui/events/base_event_utils.h" | 7 #include "ui/events/base_event_utils.h" |
| 8 #include "ui/events/event.h" | 8 #include "ui/events/event.h" |
| 9 #include "ui/events/test/event_generator.h" | 9 #include "ui/events/test/event_generator.h" |
| 10 #include "ui/views/pointer_watcher.h" | 10 #include "ui/views/pointer_watcher.h" |
| 11 #include "ui/views/widget/widget.h" | 11 #include "ui/views/widget/widget.h" |
| 12 | 12 |
| 13 namespace ash { | 13 namespace ash { |
| 14 | 14 |
| 15 using PointerWatcherDelegateAuraTest = test::AshTestBase; | 15 using PointerWatcherDelegateAuraTest = test::AshTestBase; |
| 16 | 16 |
| 17 // Records calls to OnPointerEventObserved() in |pointer_event_count_| and | 17 // Records calls to OnPointerEventObserved() in |mouse_wheel_event_count| for a |
| 18 // calls to OnMouseCaptureChanged() to |capture_changed_count_|. | 18 // mouse wheel event, in |capture_changed_count_| for a mouse capture change |
| 19 // event and in |pointer_event_count_| for all other pointer events. |
| 19 class TestPointerWatcher : public views::PointerWatcher { | 20 class TestPointerWatcher : public views::PointerWatcher { |
| 20 public: | 21 public: |
| 21 explicit TestPointerWatcher(bool wants_moves) { | 22 explicit TestPointerWatcher(bool wants_moves) { |
| 22 Shell::GetInstance()->AddPointerWatcher(this, wants_moves); | 23 Shell::GetInstance()->AddPointerWatcher(this, wants_moves); |
| 23 } | 24 } |
| 24 ~TestPointerWatcher() override { | 25 ~TestPointerWatcher() override { |
| 25 Shell::GetInstance()->RemovePointerWatcher(this); | 26 Shell::GetInstance()->RemovePointerWatcher(this); |
| 26 } | 27 } |
| 27 | 28 |
| 28 void ClearCounts() { pointer_event_count_ = capture_changed_count_ = 0; } | 29 void ClearCounts() { |
| 30 pointer_event_count_ = capture_changed_count_ = mouse_wheel_event_count_ = |
| 31 0; |
| 32 } |
| 29 | 33 |
| 30 int pointer_event_count() const { return pointer_event_count_; } | 34 int pointer_event_count() const { return pointer_event_count_; } |
| 31 int capture_changed_count() const { return capture_changed_count_; } | 35 int capture_changed_count() const { return capture_changed_count_; } |
| 36 int mouse_wheel_event_count() const { return mouse_wheel_event_count_; } |
| 32 | 37 |
| 33 // views::PointerWatcher: | 38 // views::PointerWatcher: |
| 34 void OnPointerEventObserved(const ui::PointerEvent& event, | 39 void OnPointerEventObserved(const ui::PointerEvent& event, |
| 35 const gfx::Point& location_in_screen, | 40 const gfx::Point& location_in_screen, |
| 36 views::Widget* target) override { | 41 views::Widget* target) override { |
| 37 pointer_event_count_++; | 42 if (event.type() == ui::ET_POINTER_WHEEL_CHANGED) |
| 43 mouse_wheel_event_count_++; |
| 44 else if (event.type() == ui::ET_POINTER_CAPTURE_CHANGED) |
| 45 capture_changed_count_++; |
| 46 else |
| 47 pointer_event_count_++; |
| 38 } | 48 } |
| 39 void OnMouseCaptureChanged() override { capture_changed_count_++; } | |
| 40 | 49 |
| 41 private: | 50 private: |
| 42 int pointer_event_count_ = 0; | 51 int pointer_event_count_ = 0; |
| 43 int capture_changed_count_ = 0; | 52 int capture_changed_count_ = 0; |
| 53 int mouse_wheel_event_count_ = 0; |
| 44 | 54 |
| 45 DISALLOW_COPY_AND_ASSIGN(TestPointerWatcher); | 55 DISALLOW_COPY_AND_ASSIGN(TestPointerWatcher); |
| 46 }; | 56 }; |
| 47 | 57 |
| 48 // Creates two TestPointerWatchers, one that wants moves and one that doesn't. | 58 // Creates two TestPointerWatchers, one that wants moves and one that doesn't. |
| 49 class TestHelper { | 59 class TestHelper { |
| 50 public: | 60 public: |
| 51 TestHelper() : non_move_watcher_(false), move_watcher_(true) {} | 61 TestHelper() : non_move_watcher_(false), move_watcher_(true) {} |
| 52 ~TestHelper() {} | 62 ~TestHelper() {} |
| 53 | 63 |
| 54 // Used to verify call counts. | 64 // Used to verify call counts. |
| 55 void ExpectCallCount(int non_move_pointer_event_count, | 65 void ExpectCallCount(int non_move_pointer_event_count, |
| 56 int non_move_capture_changed_count, | 66 int non_move_capture_changed_count, |
| 67 int non_move_mouse_wheel_event_count, |
| 57 int move_pointer_event_count, | 68 int move_pointer_event_count, |
| 58 int move_capture_changed_count) { | 69 int move_capture_changed_count, |
| 70 int move_mouse_wheel_event_count) { |
| 59 EXPECT_EQ(non_move_pointer_event_count, | 71 EXPECT_EQ(non_move_pointer_event_count, |
| 60 non_move_watcher_.pointer_event_count()); | 72 non_move_watcher_.pointer_event_count()); |
| 61 EXPECT_EQ(non_move_capture_changed_count, | 73 EXPECT_EQ(non_move_capture_changed_count, |
| 62 non_move_watcher_.capture_changed_count()); | 74 non_move_watcher_.capture_changed_count()); |
| 75 EXPECT_EQ(non_move_mouse_wheel_event_count, |
| 76 non_move_watcher_.mouse_wheel_event_count()); |
| 63 EXPECT_EQ(move_pointer_event_count, move_watcher_.pointer_event_count()); | 77 EXPECT_EQ(move_pointer_event_count, move_watcher_.pointer_event_count()); |
| 64 EXPECT_EQ(move_capture_changed_count, | 78 EXPECT_EQ(move_capture_changed_count, |
| 65 move_watcher_.capture_changed_count()); | 79 move_watcher_.capture_changed_count()); |
| 80 EXPECT_EQ(move_mouse_wheel_event_count, |
| 81 move_watcher_.mouse_wheel_event_count()); |
| 66 | 82 |
| 67 non_move_watcher_.ClearCounts(); | 83 non_move_watcher_.ClearCounts(); |
| 68 move_watcher_.ClearCounts(); | 84 move_watcher_.ClearCounts(); |
| 69 } | 85 } |
| 70 | 86 |
| 71 private: | 87 private: |
| 72 TestPointerWatcher non_move_watcher_; | 88 TestPointerWatcher non_move_watcher_; |
| 73 TestPointerWatcher move_watcher_; | 89 TestPointerWatcher move_watcher_; |
| 74 | 90 |
| 75 DISALLOW_COPY_AND_ASSIGN(TestHelper); | 91 DISALLOW_COPY_AND_ASSIGN(TestHelper); |
| 76 }; | 92 }; |
| 77 | 93 |
| 78 TEST_F(PointerWatcherDelegateAuraTest, MouseEvents) { | 94 TEST_F(PointerWatcherDelegateAuraTest, MouseEvents) { |
| 79 TestHelper helper; | 95 TestHelper helper; |
| 80 | 96 |
| 81 // Move: only the move PointerWatcher should get the event. | 97 // Move: only the move PointerWatcher should get the event. |
| 82 GetEventGenerator().MoveMouseTo(gfx::Point(10, 10)); | 98 GetEventGenerator().MoveMouseTo(gfx::Point(10, 10)); |
| 83 helper.ExpectCallCount(0, 0, 1, 0); | 99 helper.ExpectCallCount(0, 0, 0, 1, 0, 0); |
| 84 | 100 |
| 85 // Press: both. | 101 // Press: both. |
| 86 GetEventGenerator().PressLeftButton(); | 102 GetEventGenerator().PressLeftButton(); |
| 87 helper.ExpectCallCount(1, 0, 1, 0); | 103 helper.ExpectCallCount(1, 0, 0, 1, 0, 0); |
| 88 | 104 |
| 89 // Drag: none. | 105 // Drag: none. |
| 90 GetEventGenerator().MoveMouseTo(gfx::Point(20, 30)); | 106 GetEventGenerator().MoveMouseTo(gfx::Point(20, 30)); |
| 91 helper.ExpectCallCount(0, 0, 0, 0); | 107 helper.ExpectCallCount(0, 0, 0, 0, 0, 0); |
| 92 | 108 |
| 93 // Release: both (aura generates a capture event here). | 109 // Release: both (aura generates a capture event here). |
| 94 GetEventGenerator().ReleaseLeftButton(); | 110 GetEventGenerator().ReleaseLeftButton(); |
| 95 helper.ExpectCallCount(1, 1, 1, 1); | 111 helper.ExpectCallCount(1, 1, 0, 1, 1, 0); |
| 96 | 112 |
| 97 // Exit: none. | 113 // Exit: none. |
| 98 GetEventGenerator().SendMouseExit(); | 114 GetEventGenerator().SendMouseExit(); |
| 99 helper.ExpectCallCount(0, 0, 0, 0); | 115 helper.ExpectCallCount(0, 0, 0, 0, 0, 0); |
| 100 | 116 |
| 101 // Enter: none. | 117 // Enter: none. |
| 102 ui::MouseEvent enter_event(ui::ET_MOUSE_ENTERED, gfx::Point(), gfx::Point(), | 118 ui::MouseEvent enter_event(ui::ET_MOUSE_ENTERED, gfx::Point(), gfx::Point(), |
| 103 ui::EventTimeForNow(), 0, 0); | 119 ui::EventTimeForNow(), 0, 0); |
| 104 GetEventGenerator().Dispatch(&enter_event); | 120 GetEventGenerator().Dispatch(&enter_event); |
| 105 helper.ExpectCallCount(0, 0, 0, 0); | 121 helper.ExpectCallCount(0, 0, 0, 0, 0, 0); |
| 106 | 122 |
| 107 // Wheel: none | 123 // Wheel: both |
| 108 GetEventGenerator().MoveMouseWheel(10, 11); | 124 GetEventGenerator().MoveMouseWheel(10, 11); |
| 109 helper.ExpectCallCount(0, 0, 0, 0); | 125 helper.ExpectCallCount(0, 0, 1, 0, 0, 1); |
| 110 | 126 |
| 111 // Capture: both. | 127 // Capture: both. |
| 112 ui::MouseEvent capture_event(ui::ET_MOUSE_CAPTURE_CHANGED, gfx::Point(), | 128 ui::MouseEvent capture_event(ui::ET_MOUSE_CAPTURE_CHANGED, gfx::Point(), |
| 113 gfx::Point(), ui::EventTimeForNow(), 0, 0); | 129 gfx::Point(), ui::EventTimeForNow(), 0, 0); |
| 114 GetEventGenerator().Dispatch(&capture_event); | 130 GetEventGenerator().Dispatch(&capture_event); |
| 115 helper.ExpectCallCount(0, 1, 0, 1); | 131 helper.ExpectCallCount(0, 1, 0, 0, 1, 0); |
| 116 } | 132 } |
| 117 | 133 |
| 118 TEST_F(PointerWatcherDelegateAuraTest, TouchEvents) { | 134 TEST_F(PointerWatcherDelegateAuraTest, TouchEvents) { |
| 119 TestHelper helper; | 135 TestHelper helper; |
| 120 | 136 |
| 121 // Press: both. | 137 // Press: both. |
| 122 const int touch_id = 11; | 138 const int touch_id = 11; |
| 123 GetEventGenerator().PressTouchId(touch_id); | 139 GetEventGenerator().PressTouchId(touch_id); |
| 124 helper.ExpectCallCount(1, 0, 1, 0); | 140 helper.ExpectCallCount(1, 0, 0, 1, 0, 0); |
| 125 | 141 |
| 126 // Drag: none. | 142 // Drag: none. |
| 127 GetEventGenerator().MoveTouchId(gfx::Point(20, 30), touch_id); | 143 GetEventGenerator().MoveTouchId(gfx::Point(20, 30), touch_id); |
| 128 helper.ExpectCallCount(0, 0, 0, 0); | 144 helper.ExpectCallCount(0, 0, 0, 0, 0, 0); |
| 129 | 145 |
| 130 // Release: both (contrary to mouse above, touch does not implicitly generate | 146 // Release: both (contrary to mouse above, touch does not implicitly generate |
| 131 // capture). | 147 // capture). |
| 132 GetEventGenerator().ReleaseTouchId(touch_id); | 148 GetEventGenerator().ReleaseTouchId(touch_id); |
| 133 helper.ExpectCallCount(1, 0, 1, 0); | 149 helper.ExpectCallCount(1, 0, 0, 1, 0, 0); |
| 134 } | 150 } |
| 135 | 151 |
| 136 } // namespace ash | 152 } // namespace ash |
| OLD | NEW |