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_event_watcher.h" |
12 #include "ui/views/pointer_watcher.h" | 13 #include "ui/views/pointer_watcher.h" |
13 #include "ui/views/test/scoped_views_test_helper.h" | 14 #include "ui/views/test/scoped_views_test_helper.h" |
14 #include "ui/views/touch_event_watcher.h" | |
15 | 15 |
16 namespace views { | 16 namespace views { |
17 namespace { | 17 namespace { |
18 | 18 |
19 class TestPointerWatcher : public PointerWatcher { | 19 class TestPointerWatcher : public PointerWatcher { |
20 public: | 20 public: |
21 TestPointerWatcher() {} | 21 TestPointerWatcher() {} |
22 ~TestPointerWatcher() override {} | 22 ~TestPointerWatcher() override {} |
23 | 23 |
24 bool mouse_pressed() const { return mouse_pressed_; } | 24 bool mouse_pressed() const { return mouse_pressed_; } |
(...skipping 20 matching lines...) Expand all Loading... |
45 bool mouse_pressed_ = false; | 45 bool mouse_pressed_ = false; |
46 bool touch_pressed_ = false; | 46 bool touch_pressed_ = false; |
47 | 47 |
48 DISALLOW_COPY_AND_ASSIGN(TestPointerWatcher); | 48 DISALLOW_COPY_AND_ASSIGN(TestPointerWatcher); |
49 }; | 49 }; |
50 | 50 |
51 } // namespace | 51 } // namespace |
52 | 52 |
53 namespace { | 53 namespace { |
54 | 54 |
55 class TestTouchEventWatcher : public TouchEventWatcher { | 55 class TestPointerEventWatcher : public PointerEventWatcher { |
56 public: | 56 public: |
57 TestTouchEventWatcher() {} | 57 TestPointerEventWatcher() {} |
58 ~TestTouchEventWatcher() override {} | 58 ~TestPointerEventWatcher() override {} |
59 | 59 |
60 bool touch_observed() const { return touch_observed_; } | 60 bool touch_observed() const { return touch_observed_; } |
| 61 bool mouse_observed() const { return mouse_observed_; } |
61 | 62 |
62 void Reset() { touch_observed_ = false; } | 63 void Reset() { |
| 64 touch_observed_ = false; |
| 65 mouse_observed_ = false; |
| 66 } |
63 | 67 |
64 // TouchEventWatcher: | 68 // PointerEventWatcher: |
65 void OnTouchEventObserved(const ui::LocatedEvent& event, | 69 void OnTouchEventObserved(const ui::LocatedEvent& event, |
66 Widget* target) override { | 70 Widget* target) override { |
67 touch_observed_ = true; | 71 touch_observed_ = true; |
68 } | 72 } |
69 | 73 |
| 74 void OnMouseEventObserved(const ui::LocatedEvent& event, |
| 75 Widget* target) override { |
| 76 mouse_observed_ = true; |
| 77 } |
| 78 |
70 private: | 79 private: |
71 bool touch_observed_ = false; | 80 bool touch_observed_ = false; |
| 81 bool mouse_observed_ = false; |
72 | 82 |
73 DISALLOW_COPY_AND_ASSIGN(TestTouchEventWatcher); | 83 DISALLOW_COPY_AND_ASSIGN(TestPointerEventWatcher); |
74 }; | 84 }; |
75 | 85 |
76 } // namespace | 86 } // namespace |
77 | 87 |
78 class WindowManagerConnectionTest : public testing::Test { | 88 class WindowManagerConnectionTest : public testing::Test { |
79 public: | 89 public: |
80 WindowManagerConnectionTest() {} | 90 WindowManagerConnectionTest() {} |
81 ~WindowManagerConnectionTest() override {} | 91 ~WindowManagerConnectionTest() override {} |
82 | 92 |
83 void OnEventObserved(const ui::Event& event) { | 93 void OnEventObserved(const ui::Event& event) { |
(...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
147 WindowManagerConnection* connection = WindowManagerConnection::Get(); | 157 WindowManagerConnection* connection = WindowManagerConnection::Get(); |
148 ASSERT_TRUE(connection); | 158 ASSERT_TRUE(connection); |
149 | 159 |
150 const ui::EventType kMouseType[] = { | 160 const ui::EventType kMouseType[] = { |
151 ui::ET_MOUSE_PRESSED, ui::ET_MOUSE_DRAGGED, ui::ET_MOUSE_MOVED, | 161 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}; | 162 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, | 163 const ui::EventType kTouchType[] = {ui::ET_TOUCH_PRESSED, ui::ET_TOUCH_MOVED, |
154 ui::ET_TOUCH_RELEASED, | 164 ui::ET_TOUCH_RELEASED, |
155 ui::ET_TOUCH_CANCELLED}; | 165 ui::ET_TOUCH_CANCELLED}; |
156 | 166 |
157 TestTouchEventWatcher watcher1; | 167 TestPointerEventWatcher watcher1; |
158 connection->AddTouchEventWatcher(&watcher1); | 168 connection->AddTouchEventWatcher(&watcher1); |
159 | 169 |
160 // TouchEventWatchers do not trigger for mouse events. | 170 // TouchEventWatchers do not trigger for mouse events. |
161 for (size_t i = 0; i < arraysize(kMouseType); i++) { | 171 for (size_t i = 0; i < arraysize(kMouseType); i++) { |
162 ui::MouseEvent mouse_event(kMouseType[i], gfx::Point(), gfx::Point(), | 172 ui::MouseEvent mouse_event(kMouseType[i], gfx::Point(), gfx::Point(), |
163 base::TimeTicks(), 0, 0); | 173 base::TimeTicks(), 0, 0); |
164 ui::PointerEvent mouse_pointer_event(mouse_event); | 174 ui::PointerEvent mouse_pointer_event(mouse_event); |
165 EXPECT_TRUE(mouse_pointer_event.IsMousePointerEvent()); | 175 EXPECT_TRUE(mouse_pointer_event.IsMousePointerEvent()); |
166 OnEventObserved(mouse_pointer_event); | 176 OnEventObserved(mouse_pointer_event); |
167 EXPECT_FALSE(watcher1.touch_observed()); | 177 EXPECT_FALSE(watcher1.touch_observed()); |
(...skipping 10 matching lines...) Expand all Loading... |
178 watcher1.Reset(); | 188 watcher1.Reset(); |
179 | 189 |
180 ui::PointerEvent touch_pointer_event(touch_event); | 190 ui::PointerEvent touch_pointer_event(touch_event); |
181 EXPECT_TRUE(touch_pointer_event.IsTouchPointerEvent()); | 191 EXPECT_TRUE(touch_pointer_event.IsTouchPointerEvent()); |
182 OnEventObserved(touch_pointer_event); | 192 OnEventObserved(touch_pointer_event); |
183 EXPECT_TRUE(watcher1.touch_observed()); | 193 EXPECT_TRUE(watcher1.touch_observed()); |
184 watcher1.Reset(); | 194 watcher1.Reset(); |
185 } | 195 } |
186 | 196 |
187 // Two TouchEventWatchers can both receive a single observed event. | 197 // Two TouchEventWatchers can both receive a single observed event. |
188 TestTouchEventWatcher watcher2; | 198 TestPointerEventWatcher watcher2; |
189 connection->AddTouchEventWatcher(&watcher2); | 199 connection->AddTouchEventWatcher(&watcher2); |
190 ui::TouchEvent touch_event(ui::ET_TOUCH_PRESSED, gfx::Point(), 0, | 200 ui::TouchEvent touch_event(ui::ET_TOUCH_PRESSED, gfx::Point(), 0, |
191 base::TimeTicks()); | 201 base::TimeTicks()); |
192 ui::PointerEvent touch_pointer_event(touch_event); | 202 ui::PointerEvent touch_pointer_event(touch_event); |
193 OnEventObserved(touch_pointer_event); | 203 OnEventObserved(touch_pointer_event); |
194 EXPECT_TRUE(watcher1.touch_observed()); | 204 EXPECT_TRUE(watcher1.touch_observed()); |
195 EXPECT_TRUE(watcher2.touch_observed()); | 205 EXPECT_TRUE(watcher2.touch_observed()); |
196 watcher1.Reset(); | 206 watcher1.Reset(); |
197 watcher2.Reset(); | 207 watcher2.Reset(); |
198 | 208 |
199 // Removing the first TouchEventWatcher stops sending events to it. | 209 // Removing the first TouchEventWatcher stops sending events to it. |
200 connection->RemoveTouchEventWatcher(&watcher1); | 210 connection->RemoveTouchEventWatcher(&watcher1); |
201 OnEventObserved(touch_pointer_event); | 211 OnEventObserved(touch_pointer_event); |
202 EXPECT_FALSE(watcher1.touch_observed()); | 212 EXPECT_FALSE(watcher1.touch_observed()); |
203 EXPECT_TRUE(watcher2.touch_observed()); | 213 EXPECT_TRUE(watcher2.touch_observed()); |
204 watcher1.Reset(); | 214 watcher1.Reset(); |
205 watcher2.Reset(); | 215 watcher2.Reset(); |
206 | 216 |
207 // Removing the last TouchEventWatcher stops sending events to it. | 217 // Removing the last TouchEventWatcher stops sending events to it. |
208 connection->RemoveTouchEventWatcher(&watcher2); | 218 connection->RemoveTouchEventWatcher(&watcher2); |
209 OnEventObserved(touch_pointer_event); | 219 OnEventObserved(touch_pointer_event); |
210 EXPECT_FALSE(watcher1.touch_observed()); | 220 EXPECT_FALSE(watcher1.touch_observed()); |
211 EXPECT_FALSE(watcher2.touch_observed()); | 221 EXPECT_FALSE(watcher2.touch_observed()); |
212 } | 222 } |
213 | 223 |
| 224 TEST_F(WindowManagerConnectionTest, MouseEventWatcher) { |
| 225 base::MessageLoop message_loop(base::MessageLoop::TYPE_UI); |
| 226 ScopedViewsTestHelper helper; |
| 227 WindowManagerConnection* connection = WindowManagerConnection::Get(); |
| 228 ASSERT_TRUE(connection); |
| 229 |
| 230 const ui::EventType kMouseType[] = { |
| 231 ui::ET_MOUSE_PRESSED, ui::ET_MOUSE_DRAGGED, ui::ET_MOUSE_MOVED, |
| 232 ui::ET_MOUSE_ENTERED, ui::ET_MOUSE_EXITED, ui::ET_MOUSE_RELEASED}; |
| 233 const ui::EventType kTouchType[] = {ui::ET_TOUCH_PRESSED, ui::ET_TOUCH_MOVED, |
| 234 ui::ET_TOUCH_RELEASED, |
| 235 ui::ET_TOUCH_CANCELLED}; |
| 236 |
| 237 TestPointerEventWatcher watcher1; |
| 238 connection->AddMouseEventWatcher(&watcher1); |
| 239 |
| 240 // MouseEventWatchers receive both MouseEvent and MousePointerEvent. |
| 241 for (size_t i = 0; i < arraysize(kMouseType); i++) { |
| 242 ui::MouseEvent mouse_event(kMouseType[i], gfx::Point(), gfx::Point(), |
| 243 base::TimeTicks(), 0, 0); |
| 244 EXPECT_TRUE(mouse_event.IsMouseEvent()); |
| 245 OnEventObserved(mouse_event); |
| 246 EXPECT_TRUE(watcher1.mouse_observed()); |
| 247 watcher1.Reset(); |
| 248 |
| 249 ui::PointerEvent mouse_pointer_event(mouse_event); |
| 250 EXPECT_TRUE(mouse_pointer_event.IsMousePointerEvent()); |
| 251 OnEventObserved(mouse_pointer_event); |
| 252 EXPECT_TRUE(watcher1.mouse_observed()); |
| 253 watcher1.Reset(); |
| 254 } |
| 255 |
| 256 // MouseEventWatchers do not trigger for touch events. |
| 257 for (size_t i = 0; i < arraysize(kTouchType); i++) { |
| 258 ui::TouchEvent touch_event(kTouchType[i], gfx::Point(), 0, |
| 259 base::TimeTicks()); |
| 260 ui::PointerEvent touch_pointer_event(touch_event); |
| 261 EXPECT_TRUE(touch_pointer_event.IsTouchPointerEvent()); |
| 262 OnEventObserved(touch_pointer_event); |
| 263 EXPECT_FALSE(watcher1.touch_observed()); |
| 264 watcher1.Reset(); |
| 265 } |
| 266 |
| 267 // Two MouseEventWatchers can both receive a single observed event. |
| 268 TestPointerEventWatcher watcher2; |
| 269 connection->AddMouseEventWatcher(&watcher2); |
| 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_observed()); |
| 275 EXPECT_TRUE(watcher2.mouse_observed()); |
| 276 watcher1.Reset(); |
| 277 watcher2.Reset(); |
| 278 |
| 279 // Removing the first MouseEventWatcher stops sending events to it. |
| 280 connection->RemoveMouseEventWatcher(&watcher1); |
| 281 OnEventObserved(mouse_pointer_event); |
| 282 EXPECT_FALSE(watcher1.mouse_observed()); |
| 283 EXPECT_TRUE(watcher2.mouse_observed()); |
| 284 watcher1.Reset(); |
| 285 watcher2.Reset(); |
| 286 |
| 287 // Removing the last MouseEventWatcher stops sending events to it. |
| 288 connection->RemoveMouseEventWatcher(&watcher2); |
| 289 OnEventObserved(mouse_pointer_event); |
| 290 EXPECT_FALSE(watcher1.mouse_observed()); |
| 291 EXPECT_FALSE(watcher2.mouse_observed()); |
| 292 } |
| 293 |
214 } // namespace views | 294 } // namespace views |
OLD | NEW |