| 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 OnPointerEventObserved(const ui::LocatedEvent& event, |
| 66 Widget* target) override { | 70 Widget* target) override { |
| 67 touch_observed_ = true; | 71 if (event.IsTouchPointerEvent() || event.IsTouchEvent()) |
| 72 touch_observed_ = true; |
| 73 else if (event.IsMousePointerEvent() || event.IsMouseEvent()) |
| 74 mouse_observed_ = true; |
| 68 } | 75 } |
| 69 | 76 |
| 70 private: | 77 private: |
| 71 bool touch_observed_ = false; | 78 bool touch_observed_ = false; |
| 79 bool mouse_observed_ = false; |
| 72 | 80 |
| 73 DISALLOW_COPY_AND_ASSIGN(TestTouchEventWatcher); | 81 DISALLOW_COPY_AND_ASSIGN(TestPointerEventWatcher); |
| 74 }; | 82 }; |
| 75 | 83 |
| 76 } // namespace | 84 } // namespace |
| 77 | 85 |
| 78 class WindowManagerConnectionTest : public testing::Test { | 86 class WindowManagerConnectionTest : public testing::Test { |
| 79 public: | 87 public: |
| 80 WindowManagerConnectionTest() {} | 88 WindowManagerConnectionTest() {} |
| 81 ~WindowManagerConnectionTest() override {} | 89 ~WindowManagerConnectionTest() override {} |
| 82 | 90 |
| 83 void OnEventObserved(const ui::Event& event) { | 91 void OnEventObserved(const ui::Event& event) { |
| (...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 147 WindowManagerConnection* connection = WindowManagerConnection::Get(); | 155 WindowManagerConnection* connection = WindowManagerConnection::Get(); |
| 148 ASSERT_TRUE(connection); | 156 ASSERT_TRUE(connection); |
| 149 | 157 |
| 150 const ui::EventType kMouseType[] = { | 158 const ui::EventType kMouseType[] = { |
| 151 ui::ET_MOUSE_PRESSED, ui::ET_MOUSE_DRAGGED, ui::ET_MOUSE_MOVED, | 159 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}; | 160 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, | 161 const ui::EventType kTouchType[] = {ui::ET_TOUCH_PRESSED, ui::ET_TOUCH_MOVED, |
| 154 ui::ET_TOUCH_RELEASED, | 162 ui::ET_TOUCH_RELEASED, |
| 155 ui::ET_TOUCH_CANCELLED}; | 163 ui::ET_TOUCH_CANCELLED}; |
| 156 | 164 |
| 157 TestTouchEventWatcher watcher1; | 165 TestPointerEventWatcher watcher1; |
| 158 connection->AddTouchEventWatcher(&watcher1); | 166 connection->AddPointerEventWatcher(&watcher1, |
| 167 ui::EventPointerType::POINTER_TYPE_TOUCH); |
| 159 | 168 |
| 160 // TouchEventWatchers do not trigger for mouse events. | 169 // TouchEventWatchers do not trigger for mouse events. |
| 161 for (size_t i = 0; i < arraysize(kMouseType); i++) { | 170 for (size_t i = 0; i < arraysize(kMouseType); i++) { |
| 162 ui::MouseEvent mouse_event(kMouseType[i], gfx::Point(), gfx::Point(), | 171 ui::MouseEvent mouse_event(kMouseType[i], gfx::Point(), gfx::Point(), |
| 163 base::TimeTicks(), 0, 0); | 172 base::TimeTicks(), 0, 0); |
| 164 ui::PointerEvent mouse_pointer_event(mouse_event); | 173 ui::PointerEvent mouse_pointer_event(mouse_event); |
| 165 EXPECT_TRUE(mouse_pointer_event.IsMousePointerEvent()); | 174 EXPECT_TRUE(mouse_pointer_event.IsMousePointerEvent()); |
| 166 OnEventObserved(mouse_pointer_event); | 175 OnEventObserved(mouse_pointer_event); |
| 167 EXPECT_FALSE(watcher1.touch_observed()); | 176 EXPECT_FALSE(watcher1.touch_observed()); |
| 168 watcher1.Reset(); | 177 watcher1.Reset(); |
| 169 } | 178 } |
| 170 | 179 |
| 171 // TouchEventWatchers receive both TouchEvent and TouchPointerEvent. | 180 // TouchEventWatchers receive both TouchEvent and TouchPointerEvent. |
| 172 for (size_t i = 0; i < arraysize(kTouchType); i++) { | 181 for (size_t i = 0; i < arraysize(kTouchType); i++) { |
| 173 ui::TouchEvent touch_event(kTouchType[i], gfx::Point(), 0, | 182 ui::TouchEvent touch_event(kTouchType[i], gfx::Point(), 0, |
| 174 base::TimeTicks()); | 183 base::TimeTicks()); |
| 175 EXPECT_TRUE(touch_event.IsTouchEvent()); | 184 EXPECT_TRUE(touch_event.IsTouchEvent()); |
| 176 OnEventObserved(touch_event); | 185 OnEventObserved(touch_event); |
| 177 EXPECT_TRUE(watcher1.touch_observed()); | 186 EXPECT_TRUE(watcher1.touch_observed()); |
| 178 watcher1.Reset(); | 187 watcher1.Reset(); |
| 179 | 188 |
| 180 ui::PointerEvent touch_pointer_event(touch_event); | 189 ui::PointerEvent touch_pointer_event(touch_event); |
| 181 EXPECT_TRUE(touch_pointer_event.IsTouchPointerEvent()); | 190 EXPECT_TRUE(touch_pointer_event.IsTouchPointerEvent()); |
| 182 OnEventObserved(touch_pointer_event); | 191 OnEventObserved(touch_pointer_event); |
| 183 EXPECT_TRUE(watcher1.touch_observed()); | 192 EXPECT_TRUE(watcher1.touch_observed()); |
| 184 watcher1.Reset(); | 193 watcher1.Reset(); |
| 185 } | 194 } |
| 186 | 195 |
| 187 // Two TouchEventWatchers can both receive a single observed event. | 196 // Two TouchEventWatchers can both receive a single observed event. |
| 188 TestTouchEventWatcher watcher2; | 197 TestPointerEventWatcher watcher2; |
| 189 connection->AddTouchEventWatcher(&watcher2); | 198 connection->AddPointerEventWatcher(&watcher2, |
| 199 ui::EventPointerType::POINTER_TYPE_TOUCH); |
| 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->RemovePointerEventWatcher( |
| 211 &watcher1, ui::EventPointerType::POINTER_TYPE_TOUCH); |
| 201 OnEventObserved(touch_pointer_event); | 212 OnEventObserved(touch_pointer_event); |
| 202 EXPECT_FALSE(watcher1.touch_observed()); | 213 EXPECT_FALSE(watcher1.touch_observed()); |
| 203 EXPECT_TRUE(watcher2.touch_observed()); | 214 EXPECT_TRUE(watcher2.touch_observed()); |
| 204 watcher1.Reset(); | 215 watcher1.Reset(); |
| 205 watcher2.Reset(); | 216 watcher2.Reset(); |
| 206 | 217 |
| 207 // Removing the last TouchEventWatcher stops sending events to it. | 218 // Removing the last TouchEventWatcher stops sending events to it. |
| 208 connection->RemoveTouchEventWatcher(&watcher2); | 219 connection->RemovePointerEventWatcher( |
| 220 &watcher2, ui::EventPointerType::POINTER_TYPE_TOUCH); |
| 209 OnEventObserved(touch_pointer_event); | 221 OnEventObserved(touch_pointer_event); |
| 210 EXPECT_FALSE(watcher1.touch_observed()); | 222 EXPECT_FALSE(watcher1.touch_observed()); |
| 211 EXPECT_FALSE(watcher2.touch_observed()); | 223 EXPECT_FALSE(watcher2.touch_observed()); |
| 212 } | 224 } |
| 213 | 225 |
| 226 TEST_F(WindowManagerConnectionTest, MouseEventWatcher) { |
| 227 base::MessageLoop message_loop(base::MessageLoop::TYPE_UI); |
| 228 ScopedViewsTestHelper helper; |
| 229 WindowManagerConnection* connection = WindowManagerConnection::Get(); |
| 230 ASSERT_TRUE(connection); |
| 231 |
| 232 const ui::EventType kMouseType[] = { |
| 233 ui::ET_MOUSE_PRESSED, ui::ET_MOUSE_DRAGGED, ui::ET_MOUSE_MOVED, |
| 234 ui::ET_MOUSE_ENTERED, ui::ET_MOUSE_EXITED, ui::ET_MOUSE_RELEASED}; |
| 235 const ui::EventType kTouchType[] = {ui::ET_TOUCH_PRESSED, ui::ET_TOUCH_MOVED, |
| 236 ui::ET_TOUCH_RELEASED, |
| 237 ui::ET_TOUCH_CANCELLED}; |
| 238 |
| 239 TestPointerEventWatcher watcher1; |
| 240 connection->AddPointerEventWatcher(&watcher1, |
| 241 ui::EventPointerType::POINTER_TYPE_MOUSE); |
| 242 |
| 243 // MouseEventWatchers receive both MouseEvent and MousePointerEvent. |
| 244 for (size_t i = 0; i < arraysize(kMouseType); i++) { |
| 245 ui::MouseEvent mouse_event(kMouseType[i], gfx::Point(), gfx::Point(), |
| 246 base::TimeTicks(), 0, 0); |
| 247 EXPECT_TRUE(mouse_event.IsMouseEvent()); |
| 248 OnEventObserved(mouse_event); |
| 249 EXPECT_TRUE(watcher1.mouse_observed()); |
| 250 watcher1.Reset(); |
| 251 |
| 252 ui::PointerEvent mouse_pointer_event(mouse_event); |
| 253 EXPECT_TRUE(mouse_pointer_event.IsMousePointerEvent()); |
| 254 OnEventObserved(mouse_pointer_event); |
| 255 EXPECT_TRUE(watcher1.mouse_observed()); |
| 256 watcher1.Reset(); |
| 257 } |
| 258 |
| 259 // MouseEventWatchers do not trigger for touch events. |
| 260 for (size_t i = 0; i < arraysize(kTouchType); i++) { |
| 261 ui::TouchEvent touch_event(kTouchType[i], gfx::Point(), 0, |
| 262 base::TimeTicks()); |
| 263 ui::PointerEvent touch_pointer_event(touch_event); |
| 264 EXPECT_TRUE(touch_pointer_event.IsTouchPointerEvent()); |
| 265 OnEventObserved(touch_pointer_event); |
| 266 EXPECT_FALSE(watcher1.touch_observed()); |
| 267 watcher1.Reset(); |
| 268 } |
| 269 |
| 270 // Two MouseEventWatchers can both receive a single observed event. |
| 271 TestPointerEventWatcher watcher2; |
| 272 connection->AddPointerEventWatcher(&watcher2, |
| 273 ui::EventPointerType::POINTER_TYPE_MOUSE); |
| 274 ui::MouseEvent mouse_event(ui::ET_MOUSE_ENTERED, gfx::Point(), gfx::Point(), |
| 275 base::TimeTicks(), 0, 0); |
| 276 ui::PointerEvent mouse_pointer_event(mouse_event); |
| 277 OnEventObserved(mouse_pointer_event); |
| 278 EXPECT_TRUE(watcher1.mouse_observed()); |
| 279 EXPECT_TRUE(watcher2.mouse_observed()); |
| 280 watcher1.Reset(); |
| 281 watcher2.Reset(); |
| 282 |
| 283 // Removing the first MouseEventWatcher stops sending events to it. |
| 284 connection->RemovePointerEventWatcher( |
| 285 &watcher1, ui::EventPointerType::POINTER_TYPE_MOUSE); |
| 286 OnEventObserved(mouse_pointer_event); |
| 287 EXPECT_FALSE(watcher1.mouse_observed()); |
| 288 EXPECT_TRUE(watcher2.mouse_observed()); |
| 289 watcher1.Reset(); |
| 290 watcher2.Reset(); |
| 291 |
| 292 // Removing the last MouseEventWatcher stops sending events to it. |
| 293 connection->RemovePointerEventWatcher( |
| 294 &watcher2, ui::EventPointerType::POINTER_TYPE_MOUSE); |
| 295 OnEventObserved(mouse_pointer_event); |
| 296 EXPECT_FALSE(watcher1.mouse_observed()); |
| 297 EXPECT_FALSE(watcher2.mouse_observed()); |
| 298 } |
| 299 |
| 214 } // namespace views | 300 } // namespace views |
| OLD | NEW |