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" |
14 | 15 |
15 namespace views { | 16 namespace views { |
16 namespace { | 17 namespace { |
17 | 18 |
18 class TestPointerWatcher : public PointerWatcher { | 19 class TestPointerWatcher : public PointerWatcher { |
19 public: | 20 public: |
20 TestPointerWatcher() {} | 21 TestPointerWatcher() {} |
21 ~TestPointerWatcher() override {} | 22 ~TestPointerWatcher() override {} |
22 | 23 |
23 bool mouse_pressed() const { return mouse_pressed_; } | 24 bool mouse_pressed() const { return mouse_pressed_; } |
(...skipping 18 matching lines...) Expand all Loading... |
42 | 43 |
43 private: | 44 private: |
44 bool mouse_pressed_ = false; | 45 bool mouse_pressed_ = false; |
45 bool touch_pressed_ = false; | 46 bool touch_pressed_ = false; |
46 | 47 |
47 DISALLOW_COPY_AND_ASSIGN(TestPointerWatcher); | 48 DISALLOW_COPY_AND_ASSIGN(TestPointerWatcher); |
48 }; | 49 }; |
49 | 50 |
50 } // namespace | 51 } // namespace |
51 | 52 |
| 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() { |
| 63 touch_observed_ = false; |
| 64 } |
| 65 |
| 66 // TouchEventWatcher: |
| 67 void OnTouchEventObserved(const ui::LocatedEvent& event, |
| 68 const gfx::Point& location_in_screen, |
| 69 Widget* target) override { |
| 70 touch_observed_ = true; |
| 71 } |
| 72 |
| 73 private: |
| 74 bool touch_observed_ = false; |
| 75 |
| 76 DISALLOW_COPY_AND_ASSIGN(TestTouchEventWatcher); |
| 77 }; |
| 78 |
| 79 } // namespace |
| 80 |
52 class WindowManagerConnectionTest : public testing::Test { | 81 class WindowManagerConnectionTest : public testing::Test { |
53 public: | 82 public: |
54 WindowManagerConnectionTest() {} | 83 WindowManagerConnectionTest() {} |
55 ~WindowManagerConnectionTest() override {} | 84 ~WindowManagerConnectionTest() override {} |
56 | 85 |
57 void OnEventObserved(const ui::Event& event) { | 86 void OnEventObserved(const ui::Event& event) { |
58 WindowManagerConnection::Get()->OnEventObserved(event, nullptr); | 87 WindowManagerConnection::Get()->OnEventObserved(event, nullptr); |
59 } | 88 } |
60 | 89 |
61 private: | 90 private: |
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
108 watcher1.Reset(); | 137 watcher1.Reset(); |
109 watcher2.Reset(); | 138 watcher2.Reset(); |
110 | 139 |
111 // Removing the last PointerWatcher stops sending events to it. | 140 // Removing the last PointerWatcher stops sending events to it. |
112 connection->RemovePointerWatcher(&watcher2); | 141 connection->RemovePointerWatcher(&watcher2); |
113 OnEventObserved(mouse_pressed); | 142 OnEventObserved(mouse_pressed); |
114 EXPECT_FALSE(watcher1.mouse_pressed()); | 143 EXPECT_FALSE(watcher1.mouse_pressed()); |
115 EXPECT_FALSE(watcher1.touch_pressed()); | 144 EXPECT_FALSE(watcher1.touch_pressed()); |
116 } | 145 } |
117 | 146 |
| 147 TEST_F(WindowManagerConnectionTest, TouchEventWatcher) { |
| 148 base::MessageLoop message_loop(base::MessageLoop::TYPE_UI); |
| 149 ScopedViewsTestHelper helper; |
| 150 WindowManagerConnection* connection = WindowManagerConnection::Get(); |
| 151 ASSERT_TRUE(connection); |
| 152 |
| 153 const ui::EventType kMouseType[] = { |
| 154 ui::ET_MOUSE_PRESSED, ui::ET_MOUSE_DRAGGED, |
| 155 ui::ET_MOUSE_MOVED, ui::ET_MOUSE_ENTERED, |
| 156 ui::ET_MOUSE_EXITED, ui::ET_MOUSE_RELEASED |
| 157 }; |
| 158 const ui::EventType kTouchType[] = { |
| 159 ui::ET_TOUCH_PRESSED, ui::ET_TOUCH_MOVED, |
| 160 ui::ET_TOUCH_RELEASED, ui::ET_TOUCH_CANCELLED |
| 161 }; |
| 162 |
| 163 TestTouchEventWatcher watcher1; |
| 164 connection->AddTouchEventWatcher(&watcher1); |
| 165 |
| 166 // TouchEventWatchers do not trigger for mouse events. |
| 167 for (size_t i = 0; i < arraysize(kMouseType); i++) { |
| 168 ui::MouseEvent mouse_event(kMouseType[i], gfx::Point(0, 0), |
| 169 gfx::Point(0, 0), base::TimeTicks(), 0, 0); |
| 170 ui::PointerEvent mouse_pointer_event(mouse_event); |
| 171 EXPECT_TRUE(mouse_pointer_event.IsMousePointerEvent()); |
| 172 OnEventObserved(mouse_pointer_event); |
| 173 EXPECT_FALSE(watcher1.touch_observed()); |
| 174 watcher1.Reset(); |
| 175 } |
| 176 |
| 177 // TouchEventWatchers receive both TouchEvent and TouchPointerEvent. |
| 178 for (size_t i = 0; i < arraysize(kTouchType); i++) { |
| 179 ui::TouchEvent touch_event(kTouchType[i], gfx::Point(0, 0), 0, |
| 180 base::TimeTicks()); |
| 181 EXPECT_TRUE(touch_event.IsTouchEvent()); |
| 182 OnEventObserved(touch_event); |
| 183 EXPECT_TRUE(watcher1.touch_observed()); |
| 184 watcher1.Reset(); |
| 185 |
| 186 ui::PointerEvent touch_pointer_event(touch_event); |
| 187 EXPECT_TRUE(touch_pointer_event.IsTouchPointerEvent()); |
| 188 OnEventObserved(touch_pointer_event); |
| 189 EXPECT_TRUE(watcher1.touch_observed()); |
| 190 watcher1.Reset(); |
| 191 } |
| 192 |
| 193 // Two TouchEventWatchers can both receive a single observed event. |
| 194 TestTouchEventWatcher watcher2; |
| 195 connection->AddTouchEventWatcher(&watcher2); |
| 196 ui::TouchEvent touch_event(ui::ET_TOUCH_PRESSED, gfx::Point(0, 0), 0, |
| 197 base::TimeTicks()); |
| 198 ui::PointerEvent touch_pointer_event(touch_event); |
| 199 OnEventObserved(touch_pointer_event); |
| 200 EXPECT_TRUE(watcher1.touch_observed()); |
| 201 EXPECT_TRUE(watcher2.touch_observed()); |
| 202 watcher1.Reset(); |
| 203 watcher2.Reset(); |
| 204 |
| 205 // Removing the first TouchEventWatcher stops sending events to it. |
| 206 connection->RemoveTouchEventWatcher(&watcher1); |
| 207 OnEventObserved(touch_pointer_event); |
| 208 EXPECT_FALSE(watcher1.touch_observed()); |
| 209 EXPECT_TRUE(watcher2.touch_observed()); |
| 210 watcher1.Reset(); |
| 211 watcher2.Reset(); |
| 212 |
| 213 // Removing the last TouchEventWatcher stops sending events to it. |
| 214 connection->RemoveTouchEventWatcher(&watcher2); |
| 215 OnEventObserved(touch_pointer_event); |
| 216 EXPECT_FALSE(watcher1.touch_observed()); |
| 217 EXPECT_FALSE(watcher2.touch_observed()); |
| 218 } |
| 219 |
118 } // namespace views | 220 } // namespace views |
OLD | NEW |