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