Chromium Code Reviews| 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/common/wm_shell.h" | 5 #include "ash/common/wm_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 PointerWatcherAdapterTest = test::AshTestBase; | 15 using PointerWatcherAdapterTest = test::AshTestBase; |
| 16 | 16 |
| 17 enum TestPointerCaptureEvents { | |
| 18 NONE = 0x01, | |
| 19 CAPTURE = 0x02, | |
| 20 WHEEL = 0x04, | |
| 21 PRESS_OR_RELEASE = 0x08, | |
| 22 MOVE_OR_DRAG = 0x10, | |
| 23 }; | |
| 24 | |
| 17 // Records calls to OnPointerEventObserved() in |mouse_wheel_event_count| for a | 25 // Records calls to OnPointerEventObserved() in |mouse_wheel_event_count| for a |
| 18 // mouse wheel event, in |capture_changed_count_| for a mouse capture change | 26 // mouse wheel event, in |capture_changed_count_| for a mouse capture change |
| 19 // event and in |pointer_event_count_| for all other pointer events. | 27 // event and in |pointer_event_count_| for all other pointer events. |
| 20 class TestPointerWatcher : public views::PointerWatcher { | 28 class TestPointerWatcher : public views::PointerWatcher { |
| 21 public: | 29 public: |
| 22 explicit TestPointerWatcher(bool wants_moves) { | 30 explicit TestPointerWatcher(views::PointerWatcherEventTypes events) { |
| 23 WmShell::Get()->AddPointerWatcher(this, wants_moves); | 31 WmShell::Get()->AddPointerWatcher(this, events); |
| 24 } | 32 } |
| 25 ~TestPointerWatcher() override { WmShell::Get()->RemovePointerWatcher(this); } | 33 ~TestPointerWatcher() override { WmShell::Get()->RemovePointerWatcher(this); } |
| 26 | 34 |
| 27 void ClearCounts() { | 35 void ClearCounts() { |
| 28 pointer_event_count_ = capture_changed_count_ = mouse_wheel_event_count_ = | 36 pointer_event_count_ = capture_changed_count_ = mouse_wheel_event_count_ = |
| 29 0; | 37 move_drag_changed_count_ = 0; |
| 30 } | 38 } |
| 31 | 39 |
| 32 int pointer_event_count() const { return pointer_event_count_; } | 40 int pointer_event_count() const { return pointer_event_count_; } |
| 33 int capture_changed_count() const { return capture_changed_count_; } | 41 int capture_changed_count() const { return capture_changed_count_; } |
| 34 int mouse_wheel_event_count() const { return mouse_wheel_event_count_; } | 42 int mouse_wheel_event_count() const { return mouse_wheel_event_count_; } |
| 43 int move_drag_changed_count() const { return move_drag_changed_count_; } | |
| 35 | 44 |
| 36 // views::PointerWatcher: | 45 // views::PointerWatcher: |
| 37 void OnPointerEventObserved(const ui::PointerEvent& event, | 46 void OnPointerEventObserved(const ui::PointerEvent& event, |
| 38 const gfx::Point& location_in_screen, | 47 const gfx::Point& location_in_screen, |
| 39 views::Widget* target) override { | 48 views::Widget* target) override { |
| 40 if (event.type() == ui::ET_POINTER_WHEEL_CHANGED) | 49 if (event.type() == ui::ET_POINTER_WHEEL_CHANGED) |
| 41 mouse_wheel_event_count_++; | 50 mouse_wheel_event_count_++; |
| 42 else if (event.type() == ui::ET_POINTER_CAPTURE_CHANGED) | 51 else if (event.type() == ui::ET_POINTER_CAPTURE_CHANGED) |
| 43 capture_changed_count_++; | 52 capture_changed_count_++; |
| 53 else if (event.type() == ui::ET_POINTER_MOVED) | |
|
sky
2016/09/01 02:54:14
Sorry for not realizing this last time. The way yo
sammiequon
2016/09/01 17:06:48
Sorry for all the back and forth. Is this way or a
| |
| 54 move_drag_changed_count_++; | |
| 44 else | 55 else |
| 45 pointer_event_count_++; | 56 pointer_event_count_++; |
| 46 } | 57 } |
| 47 | 58 |
| 48 private: | 59 private: |
| 49 int pointer_event_count_ = 0; | 60 int pointer_event_count_ = 0; |
| 50 int capture_changed_count_ = 0; | 61 int capture_changed_count_ = 0; |
| 51 int mouse_wheel_event_count_ = 0; | 62 int mouse_wheel_event_count_ = 0; |
| 63 int move_drag_changed_count_ = 0; | |
| 52 | 64 |
| 53 DISALLOW_COPY_AND_ASSIGN(TestPointerWatcher); | 65 DISALLOW_COPY_AND_ASSIGN(TestPointerWatcher); |
| 54 }; | 66 }; |
| 55 | 67 |
| 56 // Creates two TestPointerWatchers, one that wants moves and one that doesn't. | 68 // Creates three TestPointerWatchers, one that wants moves and one that wants |
| 69 // drags, and one that does not want either. | |
| 57 class TestHelper { | 70 class TestHelper { |
| 58 public: | 71 public: |
| 59 TestHelper() : non_move_watcher_(false), move_watcher_(true) {} | 72 TestHelper() |
| 73 : basic_watcher_(views::PointerWatcherEventTypes::BASIC), | |
| 74 move_watcher_(views::PointerWatcherEventTypes::MOVES), | |
| 75 drag_watcher_(views::PointerWatcherEventTypes::DRAGS) {} | |
| 60 ~TestHelper() {} | 76 ~TestHelper() {} |
| 61 | 77 |
| 62 // Used to verify call counts. | 78 // Used to verify call counts. One ExpectCallCount call should be made after |
| 63 void ExpectCallCount(int non_move_pointer_event_count, | 79 // each generated mouse events. |basic_events_bitmask| defines which events |
| 64 int non_move_capture_changed_count, | 80 // the test basic watcher should receive and |move_events_bitamsk| defines |
| 65 int non_move_mouse_wheel_event_count, | 81 // which events the test move watcher should receive and |drag_events_bitmask| |
| 66 int move_pointer_event_count, | 82 // defines which events the test drag watcher should receive. |
| 67 int move_capture_changed_count, | 83 void ExpectCallCount(int basic_events_bitmask, |
| 68 int move_mouse_wheel_event_count) { | 84 int move_events_bitmask, |
| 69 EXPECT_EQ(non_move_pointer_event_count, | 85 int drag_events_bitmask) { |
| 70 non_move_watcher_.pointer_event_count()); | 86 // Compare the expected events in the |basic_events_bitmask| with the actual |
| 71 EXPECT_EQ(non_move_capture_changed_count, | 87 // counts. Basic watcher should never have any move drag counts. |
| 72 non_move_watcher_.capture_changed_count()); | 88 EXPECT_EQ(0, basic_watcher_.move_drag_changed_count()); |
| 73 EXPECT_EQ(non_move_mouse_wheel_event_count, | 89 EXPECT_EQ(basic_events_bitmask & PRESS_OR_RELEASE ? 1 : 0, |
| 74 non_move_watcher_.mouse_wheel_event_count()); | 90 basic_watcher_.pointer_event_count()); |
| 75 EXPECT_EQ(move_pointer_event_count, move_watcher_.pointer_event_count()); | 91 EXPECT_EQ(basic_events_bitmask & CAPTURE ? 1 : 0, |
| 76 EXPECT_EQ(move_capture_changed_count, | 92 basic_watcher_.capture_changed_count()); |
| 93 EXPECT_EQ(basic_events_bitmask & WHEEL ? 1 : 0, | |
| 94 basic_watcher_.mouse_wheel_event_count()); | |
| 95 // Compare the expected events in the |move_events_bitmask| with the actual | |
| 96 // counts. | |
| 97 EXPECT_EQ(move_events_bitmask & MOVE_OR_DRAG ? 1 : 0, | |
| 98 move_watcher_.move_drag_changed_count()); | |
| 99 EXPECT_EQ(move_events_bitmask & PRESS_OR_RELEASE ? 1 : 0, | |
| 100 move_watcher_.pointer_event_count()); | |
| 101 EXPECT_EQ(move_events_bitmask & CAPTURE ? 1 : 0, | |
| 77 move_watcher_.capture_changed_count()); | 102 move_watcher_.capture_changed_count()); |
| 78 EXPECT_EQ(move_mouse_wheel_event_count, | 103 EXPECT_EQ(move_events_bitmask & WHEEL ? 1 : 0, |
| 79 move_watcher_.mouse_wheel_event_count()); | 104 move_watcher_.mouse_wheel_event_count()); |
| 105 // Compare the expected events in the |drag_events_bitmask| with the actual | |
| 106 // counts. | |
| 107 EXPECT_EQ(drag_events_bitmask & MOVE_OR_DRAG ? 1 : 0, | |
| 108 drag_watcher_.move_drag_changed_count()); | |
| 109 EXPECT_EQ(drag_events_bitmask & PRESS_OR_RELEASE ? 1 : 0, | |
| 110 drag_watcher_.pointer_event_count()); | |
| 111 EXPECT_EQ(drag_events_bitmask & CAPTURE ? 1 : 0, | |
| 112 drag_watcher_.capture_changed_count()); | |
| 113 EXPECT_EQ(drag_events_bitmask & WHEEL ? 1 : 0, | |
| 114 drag_watcher_.mouse_wheel_event_count()); | |
| 80 | 115 |
| 81 non_move_watcher_.ClearCounts(); | 116 basic_watcher_.ClearCounts(); |
| 82 move_watcher_.ClearCounts(); | 117 move_watcher_.ClearCounts(); |
| 118 drag_watcher_.ClearCounts(); | |
| 83 } | 119 } |
| 84 | 120 |
| 85 private: | 121 private: |
| 86 TestPointerWatcher non_move_watcher_; | 122 TestPointerWatcher basic_watcher_; |
| 87 TestPointerWatcher move_watcher_; | 123 TestPointerWatcher move_watcher_; |
| 124 TestPointerWatcher drag_watcher_; | |
| 88 | 125 |
| 89 DISALLOW_COPY_AND_ASSIGN(TestHelper); | 126 DISALLOW_COPY_AND_ASSIGN(TestHelper); |
| 90 }; | 127 }; |
| 91 | 128 |
| 92 TEST_F(PointerWatcherAdapterTest, MouseEvents) { | 129 TEST_F(PointerWatcherAdapterTest, MouseEvents) { |
| 93 TestHelper helper; | 130 TestHelper helper; |
| 94 | 131 |
| 95 // Move: only the move PointerWatcher should get the event. | 132 // Move: only the move and drag PointerWatcher should get the event. |
| 96 GetEventGenerator().MoveMouseTo(gfx::Point(10, 10)); | 133 GetEventGenerator().MoveMouseTo(gfx::Point(10, 10)); |
| 97 helper.ExpectCallCount(0, 0, 0, 1, 0, 0); | 134 helper.ExpectCallCount(NONE, MOVE_OR_DRAG, MOVE_OR_DRAG); |
| 98 | 135 |
| 99 // Press: both. | 136 // Press: all. |
| 100 GetEventGenerator().PressLeftButton(); | 137 GetEventGenerator().PressLeftButton(); |
| 101 helper.ExpectCallCount(1, 0, 0, 1, 0, 0); | 138 helper.ExpectCallCount(PRESS_OR_RELEASE, PRESS_OR_RELEASE, PRESS_OR_RELEASE); |
| 102 | 139 |
| 103 // Drag: none. | 140 // Drag: only drag PointerWatcher should get the event. |
| 104 GetEventGenerator().MoveMouseTo(gfx::Point(20, 30)); | 141 GetEventGenerator().MoveMouseTo(gfx::Point(20, 30)); |
| 105 helper.ExpectCallCount(0, 0, 0, 0, 0, 0); | 142 helper.ExpectCallCount(NONE, NONE, MOVE_OR_DRAG); |
| 106 | 143 |
| 107 // Release: both (aura generates a capture event here). | 144 // Release: all (aura generates a capture event here). |
| 108 GetEventGenerator().ReleaseLeftButton(); | 145 GetEventGenerator().ReleaseLeftButton(); |
| 109 helper.ExpectCallCount(1, 1, 0, 1, 1, 0); | 146 helper.ExpectCallCount(CAPTURE | PRESS_OR_RELEASE, CAPTURE | PRESS_OR_RELEASE, |
| 147 CAPTURE | PRESS_OR_RELEASE); | |
| 110 | 148 |
| 111 // Exit: none. | 149 // Exit: none. |
| 112 GetEventGenerator().SendMouseExit(); | 150 GetEventGenerator().SendMouseExit(); |
| 113 helper.ExpectCallCount(0, 0, 0, 0, 0, 0); | 151 helper.ExpectCallCount(NONE, NONE, NONE); |
| 114 | 152 |
| 115 // Enter: none. | 153 // Enter: none. |
| 116 ui::MouseEvent enter_event(ui::ET_MOUSE_ENTERED, gfx::Point(), gfx::Point(), | 154 ui::MouseEvent enter_event(ui::ET_MOUSE_ENTERED, gfx::Point(), gfx::Point(), |
| 117 ui::EventTimeForNow(), 0, 0); | 155 ui::EventTimeForNow(), 0, 0); |
| 118 GetEventGenerator().Dispatch(&enter_event); | 156 GetEventGenerator().Dispatch(&enter_event); |
| 119 helper.ExpectCallCount(0, 0, 0, 0, 0, 0); | 157 helper.ExpectCallCount(NONE, NONE, NONE); |
| 120 | 158 |
| 121 // Wheel: both | 159 // Wheel: all |
| 122 GetEventGenerator().MoveMouseWheel(10, 11); | 160 GetEventGenerator().MoveMouseWheel(10, 11); |
| 123 helper.ExpectCallCount(0, 0, 1, 0, 0, 1); | 161 helper.ExpectCallCount(WHEEL, WHEEL, WHEEL); |
| 124 | 162 |
| 125 // Capture: both. | 163 // Capture: all. |
| 126 ui::MouseEvent capture_event(ui::ET_MOUSE_CAPTURE_CHANGED, gfx::Point(), | 164 ui::MouseEvent capture_event(ui::ET_MOUSE_CAPTURE_CHANGED, gfx::Point(), |
| 127 gfx::Point(), ui::EventTimeForNow(), 0, 0); | 165 gfx::Point(), ui::EventTimeForNow(), 0, 0); |
| 128 GetEventGenerator().Dispatch(&capture_event); | 166 GetEventGenerator().Dispatch(&capture_event); |
| 129 helper.ExpectCallCount(0, 1, 0, 0, 1, 0); | 167 helper.ExpectCallCount(CAPTURE, CAPTURE, CAPTURE); |
| 130 } | 168 } |
| 131 | 169 |
| 132 TEST_F(PointerWatcherAdapterTest, TouchEvents) { | 170 TEST_F(PointerWatcherAdapterTest, TouchEvents) { |
| 133 TestHelper helper; | 171 TestHelper helper; |
| 134 | 172 |
| 135 // Press: both. | 173 // Press: all. |
| 136 const int touch_id = 11; | 174 const int touch_id = 11; |
| 137 GetEventGenerator().PressTouchId(touch_id); | 175 GetEventGenerator().PressTouchId(touch_id); |
| 138 helper.ExpectCallCount(1, 0, 0, 1, 0, 0); | 176 helper.ExpectCallCount(PRESS_OR_RELEASE, PRESS_OR_RELEASE, PRESS_OR_RELEASE); |
| 139 | 177 |
| 140 // Drag: none. | 178 // Drag: only drag. |
| 141 GetEventGenerator().MoveTouchId(gfx::Point(20, 30), touch_id); | 179 GetEventGenerator().MoveTouchId(gfx::Point(20, 30), touch_id); |
| 142 helper.ExpectCallCount(0, 0, 0, 0, 0, 0); | 180 helper.ExpectCallCount(NONE, NONE, MOVE_OR_DRAG); |
| 143 | 181 |
| 144 // Release: both (contrary to mouse above, touch does not implicitly generate | 182 // Release: both (contrary to mouse above, touch does not implicitly generate |
| 145 // capture). | 183 // capture). |
| 146 GetEventGenerator().ReleaseTouchId(touch_id); | 184 GetEventGenerator().ReleaseTouchId(touch_id); |
| 147 helper.ExpectCallCount(1, 0, 0, 1, 0, 0); | 185 helper.ExpectCallCount(PRESS_OR_RELEASE, PRESS_OR_RELEASE, PRESS_OR_RELEASE); |
| 148 } | 186 } |
| 149 | 187 |
| 150 } // namespace ash | 188 } // namespace ash |
| OLD | NEW |