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 "ui/views/mus/window_manager_connection.h" | 5 #include "ui/views/mus/window_manager_connection.h" |
6 | 6 |
7 #include <memory> | 7 #include <memory> |
8 | 8 |
9 #include "base/message_loop/message_loop.h" | 9 #include "base/message_loop/message_loop.h" |
10 #include "testing/gtest/include/gtest/gtest.h" | 10 #include "testing/gtest/include/gtest/gtest.h" |
11 #include "ui/events/event.h" | 11 #include "ui/events/event.h" |
12 #include "ui/views/pointer_watcher.h" | 12 #include "ui/views/pointer_watcher.h" |
13 #include "ui/views/test/scoped_views_test_helper.h" | 13 #include "ui/views/test/scoped_views_test_helper.h" |
14 #include "ui/views/touch_event_watcher.h" | |
15 | 14 |
16 namespace views { | 15 namespace views { |
17 namespace { | 16 namespace { |
18 | 17 |
19 class TestPointerWatcher : public PointerWatcher { | 18 class TestPointerWatcher : public PointerWatcher { |
20 public: | 19 public: |
21 TestPointerWatcher() {} | 20 TestPointerWatcher() {} |
22 ~TestPointerWatcher() override {} | 21 ~TestPointerWatcher() override {} |
23 | 22 |
24 bool mouse_pressed() const { return mouse_pressed_; } | 23 bool mouse_pressed() const { return mouse_pressed_; } |
25 bool touch_pressed() const { return touch_pressed_; } | 24 bool touch_pressed() const { return touch_pressed_; } |
| 25 bool mouse_event_observed() const { return mouse_event_observed_; } |
| 26 bool touch_event_observed() const { return touch_event_observed_; } |
| 27 |
| 28 void set_want_moves(bool want_moves) { want_moves_ = want_moves; } |
26 | 29 |
27 void Reset() { | 30 void Reset() { |
28 mouse_pressed_ = false; | 31 mouse_pressed_ = false; |
29 touch_pressed_ = false; | 32 touch_pressed_ = false; |
| 33 mouse_event_observed_ = false; |
| 34 touch_event_observed_ = false; |
30 } | 35 } |
31 | 36 |
32 // PointerWatcher: | 37 // PointerWatcher: |
33 void OnMousePressed(const ui::MouseEvent& event, | 38 void OnPointerEventObserved(const ui::LocatedEvent& event, |
34 const gfx::Point& location_in_screen, | 39 const gfx::Point& location_in_screen, |
35 Widget* target) override { | 40 Widget* target) override { |
36 mouse_pressed_ = true; | 41 if (want_moves_) { |
37 } | 42 if (event.IsTouchPointerEvent() || event.IsTouchEvent()) |
38 void OnTouchPressed(const ui::TouchEvent& event, | 43 touch_event_observed_ = true; |
39 const gfx::Point& location_in_screen, | 44 else if (event.IsMousePointerEvent() || event.IsMouseEvent()) |
40 Widget* target) override { | 45 mouse_event_observed_ = true; |
41 touch_pressed_ = true; | 46 } else { |
| 47 if (event.type() == ui::ET_TOUCH_PRESSED) |
| 48 touch_pressed_ = true; |
| 49 else if (event.type() == ui::ET_MOUSE_PRESSED) |
| 50 mouse_pressed_ = true; |
| 51 } |
42 } | 52 } |
43 | 53 |
44 private: | 54 private: |
45 bool mouse_pressed_ = false; | 55 bool mouse_pressed_ = false; |
46 bool touch_pressed_ = false; | 56 bool touch_pressed_ = false; |
| 57 bool mouse_event_observed_ = false; |
| 58 bool touch_event_observed_ = false; |
| 59 bool want_moves_ = false; |
47 | 60 |
48 DISALLOW_COPY_AND_ASSIGN(TestPointerWatcher); | 61 DISALLOW_COPY_AND_ASSIGN(TestPointerWatcher); |
49 }; | 62 }; |
50 | 63 |
51 } // namespace | 64 } // namespace |
52 | 65 |
53 namespace { | |
54 | |
55 class TestTouchEventWatcher : public TouchEventWatcher { | |
56 public: | |
57 TestTouchEventWatcher() {} | |
58 ~TestTouchEventWatcher() override {} | |
59 | |
60 bool touch_observed() const { return touch_observed_; } | |
61 | |
62 void Reset() { touch_observed_ = false; } | |
63 | |
64 // TouchEventWatcher: | |
65 void OnTouchEventObserved(const ui::LocatedEvent& event, | |
66 Widget* target) override { | |
67 touch_observed_ = true; | |
68 } | |
69 | |
70 private: | |
71 bool touch_observed_ = false; | |
72 | |
73 DISALLOW_COPY_AND_ASSIGN(TestTouchEventWatcher); | |
74 }; | |
75 | |
76 } // namespace | |
77 | |
78 class WindowManagerConnectionTest : public testing::Test { | 66 class WindowManagerConnectionTest : public testing::Test { |
79 public: | 67 public: |
80 WindowManagerConnectionTest() {} | 68 WindowManagerConnectionTest() {} |
81 ~WindowManagerConnectionTest() override {} | 69 ~WindowManagerConnectionTest() override {} |
82 | 70 |
83 void OnEventObserved(const ui::Event& event) { | 71 void OnEventObserved(const ui::Event& event) { |
84 WindowManagerConnection::Get()->OnEventObserved(event, nullptr); | 72 WindowManagerConnection::Get()->OnEventObserved(event, nullptr); |
85 } | 73 } |
86 | 74 |
87 private: | 75 private: |
88 DISALLOW_COPY_AND_ASSIGN(WindowManagerConnectionTest); | 76 DISALLOW_COPY_AND_ASSIGN(WindowManagerConnectionTest); |
89 }; | 77 }; |
90 | 78 |
91 TEST_F(WindowManagerConnectionTest, PointerWatcher) { | 79 TEST_F(WindowManagerConnectionTest, PointerDownWatcher) { |
92 base::MessageLoop message_loop(base::MessageLoop::TYPE_UI); | 80 base::MessageLoop message_loop(base::MessageLoop::TYPE_UI); |
93 ScopedViewsTestHelper helper; | 81 ScopedViewsTestHelper helper; |
94 WindowManagerConnection* connection = WindowManagerConnection::Get(); | 82 WindowManagerConnection* connection = WindowManagerConnection::Get(); |
95 ASSERT_TRUE(connection); | 83 ASSERT_TRUE(connection); |
96 ui::MouseEvent mouse_pressed(ui::ET_MOUSE_PRESSED, gfx::Point(), gfx::Point(), | 84 ui::MouseEvent mouse_pressed(ui::ET_MOUSE_PRESSED, gfx::Point(), gfx::Point(), |
97 base::TimeTicks(), ui::EF_NONE, 0); | 85 base::TimeTicks(), ui::EF_NONE, 0); |
98 ui::TouchEvent touch_pressed(ui::ET_TOUCH_PRESSED, gfx::Point(), 1, | 86 ui::TouchEvent touch_pressed(ui::ET_TOUCH_PRESSED, gfx::Point(), 1, |
99 base::TimeTicks()); | 87 base::TimeTicks()); |
100 ui::KeyEvent key_pressed(ui::ET_KEY_PRESSED, ui::VKEY_A, 0); | 88 ui::KeyEvent key_pressed(ui::ET_KEY_PRESSED, ui::VKEY_A, 0); |
101 | 89 |
102 // PointerWatchers receive mouse events. | 90 // PointerWatchers receive mouse events. |
103 TestPointerWatcher watcher1; | 91 TestPointerWatcher watcher1; |
104 connection->AddPointerWatcher(&watcher1); | 92 watcher1.set_want_moves(false); |
| 93 connection->AddPointerWatcher(&watcher1, |
| 94 ui::EventPointerType::POINTER_TYPE_ANY, false); |
105 OnEventObserved(mouse_pressed); | 95 OnEventObserved(mouse_pressed); |
106 EXPECT_TRUE(watcher1.mouse_pressed()); | 96 EXPECT_TRUE(watcher1.mouse_pressed()); |
| 97 EXPECT_FALSE(watcher1.mouse_event_observed()); |
107 watcher1.Reset(); | 98 watcher1.Reset(); |
108 | 99 |
109 // PointerWatchers receive touch events. | 100 // PointerWatchers receive touch events. |
110 OnEventObserved(touch_pressed); | 101 OnEventObserved(touch_pressed); |
111 EXPECT_TRUE(watcher1.touch_pressed()); | 102 EXPECT_TRUE(watcher1.touch_pressed()); |
| 103 EXPECT_FALSE(watcher1.touch_event_observed()); |
112 watcher1.Reset(); | 104 watcher1.Reset(); |
113 | 105 |
114 // PointerWatchers do not trigger for key events. | 106 // PointerWatchers do not trigger for key events. |
115 OnEventObserved(key_pressed); | 107 OnEventObserved(key_pressed); |
116 EXPECT_FALSE(watcher1.mouse_pressed()); | 108 EXPECT_FALSE(watcher1.mouse_pressed()); |
117 EXPECT_FALSE(watcher1.touch_pressed()); | 109 EXPECT_FALSE(watcher1.touch_pressed()); |
118 watcher1.Reset(); | 110 watcher1.Reset(); |
119 | 111 |
120 // Two PointerWatchers can both receive a single observed event. | 112 // Two PointerWatchers can both receive a single observed event. |
121 TestPointerWatcher watcher2; | 113 TestPointerWatcher watcher2; |
122 connection->AddPointerWatcher(&watcher2); | 114 watcher2.set_want_moves(false); |
| 115 connection->AddPointerWatcher(&watcher2, |
| 116 ui::EventPointerType::POINTER_TYPE_ANY, false); |
123 OnEventObserved(mouse_pressed); | 117 OnEventObserved(mouse_pressed); |
124 EXPECT_TRUE(watcher1.mouse_pressed()); | 118 EXPECT_TRUE(watcher1.mouse_pressed()); |
125 EXPECT_TRUE(watcher2.mouse_pressed()); | 119 EXPECT_TRUE(watcher2.mouse_pressed()); |
| 120 EXPECT_FALSE(watcher1.mouse_event_observed()); |
| 121 EXPECT_FALSE(watcher2.mouse_event_observed()); |
126 watcher1.Reset(); | 122 watcher1.Reset(); |
127 watcher2.Reset(); | 123 watcher2.Reset(); |
128 | 124 |
129 // Removing the first PointerWatcher stops sending events to it. | 125 // Removing the first PointerWatcher stops sending events to it. |
130 connection->RemovePointerWatcher(&watcher1); | 126 connection->RemovePointerWatcher(&watcher1); |
131 OnEventObserved(mouse_pressed); | 127 OnEventObserved(mouse_pressed); |
132 EXPECT_FALSE(watcher1.mouse_pressed()); | 128 EXPECT_FALSE(watcher1.mouse_pressed()); |
133 EXPECT_TRUE(watcher2.mouse_pressed()); | 129 EXPECT_TRUE(watcher2.mouse_pressed()); |
134 watcher1.Reset(); | 130 watcher1.Reset(); |
135 watcher2.Reset(); | 131 watcher2.Reset(); |
(...skipping 11 matching lines...) Expand all Loading... |
147 WindowManagerConnection* connection = WindowManagerConnection::Get(); | 143 WindowManagerConnection* connection = WindowManagerConnection::Get(); |
148 ASSERT_TRUE(connection); | 144 ASSERT_TRUE(connection); |
149 | 145 |
150 const ui::EventType kMouseType[] = { | 146 const ui::EventType kMouseType[] = { |
151 ui::ET_MOUSE_PRESSED, ui::ET_MOUSE_DRAGGED, ui::ET_MOUSE_MOVED, | 147 ui::ET_MOUSE_PRESSED, ui::ET_MOUSE_DRAGGED, ui::ET_MOUSE_MOVED, |
152 ui::ET_MOUSE_ENTERED, ui::ET_MOUSE_EXITED, ui::ET_MOUSE_RELEASED}; | 148 ui::ET_MOUSE_ENTERED, ui::ET_MOUSE_EXITED, ui::ET_MOUSE_RELEASED}; |
153 const ui::EventType kTouchType[] = {ui::ET_TOUCH_PRESSED, ui::ET_TOUCH_MOVED, | 149 const ui::EventType kTouchType[] = {ui::ET_TOUCH_PRESSED, ui::ET_TOUCH_MOVED, |
154 ui::ET_TOUCH_RELEASED, | 150 ui::ET_TOUCH_RELEASED, |
155 ui::ET_TOUCH_CANCELLED}; | 151 ui::ET_TOUCH_CANCELLED}; |
156 | 152 |
157 TestTouchEventWatcher watcher1; | 153 TestPointerWatcher watcher1; |
158 connection->AddTouchEventWatcher(&watcher1); | 154 watcher1.set_want_moves(true); |
| 155 connection->AddPointerWatcher(&watcher1, |
| 156 ui::EventPointerType::POINTER_TYPE_TOUCH, true); |
159 | 157 |
160 // TouchEventWatchers do not trigger for mouse events. | 158 // TouchEventWatchers do not trigger for mouse events. |
161 for (size_t i = 0; i < arraysize(kMouseType); i++) { | 159 for (size_t i = 0; i < arraysize(kMouseType); i++) { |
162 ui::MouseEvent mouse_event(kMouseType[i], gfx::Point(), gfx::Point(), | 160 ui::MouseEvent mouse_event(kMouseType[i], gfx::Point(), gfx::Point(), |
163 base::TimeTicks(), 0, 0); | 161 base::TimeTicks(), 0, 0); |
164 ui::PointerEvent mouse_pointer_event(mouse_event); | 162 ui::PointerEvent mouse_pointer_event(mouse_event); |
165 EXPECT_TRUE(mouse_pointer_event.IsMousePointerEvent()); | 163 EXPECT_TRUE(mouse_pointer_event.IsMousePointerEvent()); |
166 OnEventObserved(mouse_pointer_event); | 164 OnEventObserved(mouse_pointer_event); |
167 EXPECT_FALSE(watcher1.touch_observed()); | 165 EXPECT_FALSE(watcher1.touch_event_observed()); |
168 watcher1.Reset(); | 166 watcher1.Reset(); |
169 } | 167 } |
170 | 168 |
171 // TouchEventWatchers receive both TouchEvent and TouchPointerEvent. | 169 // TouchEventWatchers receive both TouchEvent and TouchPointerEvent. |
172 for (size_t i = 0; i < arraysize(kTouchType); i++) { | 170 for (size_t i = 0; i < arraysize(kTouchType); i++) { |
173 ui::TouchEvent touch_event(kTouchType[i], gfx::Point(), 0, | 171 ui::TouchEvent touch_event(kTouchType[i], gfx::Point(), 0, |
174 base::TimeTicks()); | 172 base::TimeTicks()); |
175 EXPECT_TRUE(touch_event.IsTouchEvent()); | 173 EXPECT_TRUE(touch_event.IsTouchEvent()); |
176 OnEventObserved(touch_event); | 174 OnEventObserved(touch_event); |
177 EXPECT_TRUE(watcher1.touch_observed()); | 175 EXPECT_TRUE(watcher1.touch_event_observed()); |
| 176 EXPECT_FALSE(watcher1.touch_pressed()); |
178 watcher1.Reset(); | 177 watcher1.Reset(); |
179 | 178 |
180 ui::PointerEvent touch_pointer_event(touch_event); | 179 ui::PointerEvent touch_pointer_event(touch_event); |
181 EXPECT_TRUE(touch_pointer_event.IsTouchPointerEvent()); | 180 EXPECT_TRUE(touch_pointer_event.IsTouchPointerEvent()); |
182 OnEventObserved(touch_pointer_event); | 181 OnEventObserved(touch_pointer_event); |
183 EXPECT_TRUE(watcher1.touch_observed()); | 182 EXPECT_TRUE(watcher1.touch_event_observed()); |
| 183 EXPECT_FALSE(watcher1.touch_pressed()); |
184 watcher1.Reset(); | 184 watcher1.Reset(); |
185 } | 185 } |
186 | 186 |
187 // Two TouchEventWatchers can both receive a single observed event. | 187 // Two TouchEventWatchers can both receive a single observed event. |
188 TestTouchEventWatcher watcher2; | 188 TestPointerWatcher watcher2; |
189 connection->AddTouchEventWatcher(&watcher2); | 189 watcher2.set_want_moves(true); |
| 190 connection->AddPointerWatcher(&watcher2, |
| 191 ui::EventPointerType::POINTER_TYPE_TOUCH, true); |
190 ui::TouchEvent touch_event(ui::ET_TOUCH_PRESSED, gfx::Point(), 0, | 192 ui::TouchEvent touch_event(ui::ET_TOUCH_PRESSED, gfx::Point(), 0, |
191 base::TimeTicks()); | 193 base::TimeTicks()); |
192 ui::PointerEvent touch_pointer_event(touch_event); | 194 ui::PointerEvent touch_pointer_event(touch_event); |
193 OnEventObserved(touch_pointer_event); | 195 OnEventObserved(touch_pointer_event); |
194 EXPECT_TRUE(watcher1.touch_observed()); | 196 EXPECT_TRUE(watcher1.touch_event_observed()); |
195 EXPECT_TRUE(watcher2.touch_observed()); | 197 EXPECT_TRUE(watcher2.touch_event_observed()); |
| 198 EXPECT_FALSE(watcher1.touch_pressed()); |
| 199 EXPECT_FALSE(watcher2.touch_pressed()); |
196 watcher1.Reset(); | 200 watcher1.Reset(); |
197 watcher2.Reset(); | 201 watcher2.Reset(); |
198 | 202 |
199 // Removing the first TouchEventWatcher stops sending events to it. | 203 // Removing the first TouchEventWatcher stops sending events to it. |
200 connection->RemoveTouchEventWatcher(&watcher1); | 204 connection->RemovePointerWatcher(&watcher1); |
201 OnEventObserved(touch_pointer_event); | 205 OnEventObserved(touch_pointer_event); |
202 EXPECT_FALSE(watcher1.touch_observed()); | 206 EXPECT_FALSE(watcher1.touch_event_observed()); |
203 EXPECT_TRUE(watcher2.touch_observed()); | 207 EXPECT_TRUE(watcher2.touch_event_observed()); |
204 watcher1.Reset(); | 208 watcher1.Reset(); |
205 watcher2.Reset(); | 209 watcher2.Reset(); |
206 | 210 |
207 // Removing the last TouchEventWatcher stops sending events to it. | 211 // Removing the last TouchEventWatcher stops sending events to it. |
208 connection->RemoveTouchEventWatcher(&watcher2); | 212 connection->RemovePointerWatcher(&watcher2); |
209 OnEventObserved(touch_pointer_event); | 213 OnEventObserved(touch_pointer_event); |
210 EXPECT_FALSE(watcher1.touch_observed()); | 214 EXPECT_FALSE(watcher1.touch_event_observed()); |
211 EXPECT_FALSE(watcher2.touch_observed()); | 215 EXPECT_FALSE(watcher2.touch_event_observed()); |
| 216 } |
| 217 |
| 218 TEST_F(WindowManagerConnectionTest, MouseEventWatcher) { |
| 219 base::MessageLoop message_loop(base::MessageLoop::TYPE_UI); |
| 220 ScopedViewsTestHelper helper; |
| 221 WindowManagerConnection* connection = WindowManagerConnection::Get(); |
| 222 ASSERT_TRUE(connection); |
| 223 |
| 224 const ui::EventType kMouseType[] = { |
| 225 ui::ET_MOUSE_PRESSED, ui::ET_MOUSE_DRAGGED, ui::ET_MOUSE_MOVED, |
| 226 ui::ET_MOUSE_ENTERED, ui::ET_MOUSE_EXITED, ui::ET_MOUSE_RELEASED}; |
| 227 const ui::EventType kTouchType[] = {ui::ET_TOUCH_PRESSED, ui::ET_TOUCH_MOVED, |
| 228 ui::ET_TOUCH_RELEASED, |
| 229 ui::ET_TOUCH_CANCELLED}; |
| 230 |
| 231 TestPointerWatcher watcher1; |
| 232 watcher1.set_want_moves(true); |
| 233 connection->AddPointerWatcher(&watcher1, |
| 234 ui::EventPointerType::POINTER_TYPE_MOUSE, true); |
| 235 |
| 236 // MouseEventWatchers receive both MouseEvent and MousePointerEvent. |
| 237 for (size_t i = 0; i < arraysize(kMouseType); i++) { |
| 238 ui::MouseEvent mouse_event(kMouseType[i], gfx::Point(), gfx::Point(), |
| 239 base::TimeTicks(), 0, 0); |
| 240 EXPECT_TRUE(mouse_event.IsMouseEvent()); |
| 241 OnEventObserved(mouse_event); |
| 242 EXPECT_TRUE(watcher1.mouse_event_observed()); |
| 243 EXPECT_FALSE(watcher1.mouse_pressed()); |
| 244 watcher1.Reset(); |
| 245 |
| 246 ui::PointerEvent mouse_pointer_event(mouse_event); |
| 247 EXPECT_TRUE(mouse_pointer_event.IsMousePointerEvent()); |
| 248 OnEventObserved(mouse_pointer_event); |
| 249 EXPECT_TRUE(watcher1.mouse_event_observed()); |
| 250 EXPECT_FALSE(watcher1.mouse_pressed()); |
| 251 watcher1.Reset(); |
| 252 } |
| 253 |
| 254 // MouseEventWatchers do not trigger for touch events. |
| 255 for (size_t i = 0; i < arraysize(kTouchType); i++) { |
| 256 ui::TouchEvent touch_event(kTouchType[i], gfx::Point(), 0, |
| 257 base::TimeTicks()); |
| 258 ui::PointerEvent touch_pointer_event(touch_event); |
| 259 EXPECT_TRUE(touch_pointer_event.IsTouchPointerEvent()); |
| 260 OnEventObserved(touch_pointer_event); |
| 261 EXPECT_FALSE(watcher1.touch_event_observed()); |
| 262 watcher1.Reset(); |
| 263 } |
| 264 |
| 265 // Two MouseEventWatchers can both receive a single observed event. |
| 266 TestPointerWatcher watcher2; |
| 267 watcher2.set_want_moves(true); |
| 268 connection->AddPointerWatcher(&watcher2, |
| 269 ui::EventPointerType::POINTER_TYPE_MOUSE, true); |
| 270 ui::MouseEvent mouse_event(ui::ET_MOUSE_ENTERED, gfx::Point(), gfx::Point(), |
| 271 base::TimeTicks(), 0, 0); |
| 272 ui::PointerEvent mouse_pointer_event(mouse_event); |
| 273 OnEventObserved(mouse_pointer_event); |
| 274 EXPECT_TRUE(watcher1.mouse_event_observed()); |
| 275 EXPECT_TRUE(watcher2.mouse_event_observed()); |
| 276 EXPECT_FALSE(watcher1.mouse_pressed()); |
| 277 EXPECT_FALSE(watcher2.mouse_pressed()); |
| 278 watcher1.Reset(); |
| 279 watcher2.Reset(); |
| 280 |
| 281 // Removing the first MouseEventWatcher stops sending events to it. |
| 282 connection->RemovePointerWatcher(&watcher1); |
| 283 OnEventObserved(mouse_pointer_event); |
| 284 EXPECT_FALSE(watcher1.mouse_event_observed()); |
| 285 EXPECT_TRUE(watcher2.mouse_event_observed()); |
| 286 watcher1.Reset(); |
| 287 watcher2.Reset(); |
| 288 |
| 289 // Removing the last MouseEventWatcher stops sending events to it. |
| 290 connection->RemovePointerWatcher(&watcher2); |
| 291 OnEventObserved(mouse_pointer_event); |
| 292 EXPECT_FALSE(watcher1.mouse_event_observed()); |
| 293 EXPECT_FALSE(watcher2.mouse_event_observed()); |
212 } | 294 } |
213 | 295 |
214 } // namespace views | 296 } // namespace views |
OLD | NEW |