| 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 ui::PointerEvent* last_event_observed() { return last_event_observed_.get(); } |
| 25 bool touch_pressed() const { return touch_pressed_; } | |
| 26 | 24 |
| 27 void Reset() { | 25 void Reset() { last_event_observed_.reset(); } |
| 28 mouse_pressed_ = false; | |
| 29 touch_pressed_ = false; | |
| 30 } | |
| 31 | 26 |
| 32 // PointerWatcher: | 27 // PointerWatcher: |
| 33 void OnMousePressed(const ui::MouseEvent& event, | 28 void OnPointerEventObserved(const ui::PointerEvent& event, |
| 34 const gfx::Point& location_in_screen, | 29 const gfx::Point& location_in_screen, |
| 35 Widget* target) override { | 30 Widget* target) override { |
| 36 mouse_pressed_ = true; | 31 last_event_observed_.reset(new ui::PointerEvent(event)); |
| 37 } | |
| 38 void OnTouchPressed(const ui::TouchEvent& event, | |
| 39 const gfx::Point& location_in_screen, | |
| 40 Widget* target) override { | |
| 41 touch_pressed_ = true; | |
| 42 } | 32 } |
| 43 | 33 |
| 44 private: | 34 private: |
| 45 bool mouse_pressed_ = false; | 35 std::unique_ptr<ui::PointerEvent> last_event_observed_; |
| 46 bool touch_pressed_ = false; | |
| 47 | 36 |
| 48 DISALLOW_COPY_AND_ASSIGN(TestPointerWatcher); | 37 DISALLOW_COPY_AND_ASSIGN(TestPointerWatcher); |
| 49 }; | 38 }; |
| 50 | 39 |
| 51 } // namespace | 40 } // namespace |
| 52 | 41 |
| 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 { | 42 class WindowManagerConnectionTest : public testing::Test { |
| 79 public: | 43 public: |
| 80 WindowManagerConnectionTest() {} | 44 WindowManagerConnectionTest() {} |
| 81 ~WindowManagerConnectionTest() override {} | 45 ~WindowManagerConnectionTest() override {} |
| 82 | 46 |
| 83 void OnEventObserved(const ui::Event& event) { | 47 void OnPointerEventObserved(const ui::PointerEvent& event) { |
| 84 WindowManagerConnection::Get()->OnEventObserved(event, nullptr); | 48 WindowManagerConnection::Get()->OnPointerEventObserved(event, nullptr); |
| 85 } | 49 } |
| 86 | 50 |
| 87 private: | 51 private: |
| 88 DISALLOW_COPY_AND_ASSIGN(WindowManagerConnectionTest); | 52 DISALLOW_COPY_AND_ASSIGN(WindowManagerConnectionTest); |
| 89 }; | 53 }; |
| 90 | 54 |
| 91 TEST_F(WindowManagerConnectionTest, PointerWatcher) { | 55 TEST_F(WindowManagerConnectionTest, PointerWatcherNoMove) { |
| 92 base::MessageLoop message_loop(base::MessageLoop::TYPE_UI); | 56 base::MessageLoop message_loop(base::MessageLoop::TYPE_UI); |
| 93 ScopedViewsTestHelper helper; | 57 ScopedViewsTestHelper helper; |
| 94 WindowManagerConnection* connection = WindowManagerConnection::Get(); | 58 WindowManagerConnection* connection = WindowManagerConnection::Get(); |
| 95 ASSERT_TRUE(connection); | 59 ASSERT_TRUE(connection); |
| 96 ui::MouseEvent mouse_pressed(ui::ET_MOUSE_PRESSED, gfx::Point(), gfx::Point(), | |
| 97 base::TimeTicks(), ui::EF_NONE, 0); | |
| 98 ui::TouchEvent touch_pressed(ui::ET_TOUCH_PRESSED, gfx::Point(), 1, | |
| 99 base::TimeTicks()); | |
| 100 ui::KeyEvent key_pressed(ui::ET_KEY_PRESSED, ui::VKEY_A, 0); | |
| 101 | 60 |
| 102 // PointerWatchers receive mouse events. | 61 ui::PointerEvent pointer_event_down( |
| 62 ui::ET_POINTER_DOWN, gfx::Point(), gfx::Point(), ui::EF_NONE, 1, |
| 63 ui::PointerDetails(ui::EventPointerType::POINTER_TYPE_TOUCH), |
| 64 base::TimeTicks()); |
| 65 ui::PointerEvent pointer_event_up( |
| 66 ui::ET_POINTER_UP, gfx::Point(), gfx::Point(), ui::EF_NONE, 1, |
| 67 ui::PointerDetails(ui::EventPointerType::POINTER_TYPE_MOUSE), |
| 68 base::TimeTicks()); |
| 69 |
| 70 // PointerWatchers receive pointer down events. |
| 103 TestPointerWatcher watcher1; | 71 TestPointerWatcher watcher1; |
| 104 connection->AddPointerWatcher(&watcher1); | 72 connection->AddPointerWatcher(&watcher1, false); |
| 105 OnEventObserved(mouse_pressed); | 73 OnPointerEventObserved(pointer_event_down); |
| 106 EXPECT_TRUE(watcher1.mouse_pressed()); | 74 EXPECT_EQ(ui::ET_POINTER_DOWN, watcher1.last_event_observed()->type()); |
| 107 watcher1.Reset(); | 75 watcher1.Reset(); |
| 108 | 76 |
| 109 // PointerWatchers receive touch events. | 77 // PointerWatchers receive pointer up events. |
| 110 OnEventObserved(touch_pressed); | 78 OnPointerEventObserved(pointer_event_up); |
| 111 EXPECT_TRUE(watcher1.touch_pressed()); | 79 EXPECT_EQ(ui::ET_POINTER_UP, watcher1.last_event_observed()->type()); |
| 112 watcher1.Reset(); | |
| 113 | |
| 114 // PointerWatchers do not trigger for key events. | |
| 115 OnEventObserved(key_pressed); | |
| 116 EXPECT_FALSE(watcher1.mouse_pressed()); | |
| 117 EXPECT_FALSE(watcher1.touch_pressed()); | |
| 118 watcher1.Reset(); | 80 watcher1.Reset(); |
| 119 | 81 |
| 120 // Two PointerWatchers can both receive a single observed event. | 82 // Two PointerWatchers can both receive a single observed event. |
| 121 TestPointerWatcher watcher2; | 83 TestPointerWatcher watcher2; |
| 122 connection->AddPointerWatcher(&watcher2); | 84 connection->AddPointerWatcher(&watcher2, false); |
| 123 OnEventObserved(mouse_pressed); | 85 OnPointerEventObserved(pointer_event_down); |
| 124 EXPECT_TRUE(watcher1.mouse_pressed()); | 86 EXPECT_EQ(ui::ET_POINTER_DOWN, watcher1.last_event_observed()->type()); |
| 125 EXPECT_TRUE(watcher2.mouse_pressed()); | 87 EXPECT_EQ(ui::ET_POINTER_DOWN, watcher2.last_event_observed()->type()); |
| 126 watcher1.Reset(); | 88 watcher1.Reset(); |
| 127 watcher2.Reset(); | 89 watcher2.Reset(); |
| 128 | 90 |
| 129 // Removing the first PointerWatcher stops sending events to it. | 91 // Removing the first PointerWatcher stops sending events to it. |
| 130 connection->RemovePointerWatcher(&watcher1); | 92 connection->RemovePointerWatcher(&watcher1); |
| 131 OnEventObserved(mouse_pressed); | 93 OnPointerEventObserved(pointer_event_down); |
| 132 EXPECT_FALSE(watcher1.mouse_pressed()); | 94 EXPECT_FALSE(watcher1.last_event_observed()); |
| 133 EXPECT_TRUE(watcher2.mouse_pressed()); | 95 EXPECT_EQ(ui::ET_POINTER_DOWN, watcher2.last_event_observed()->type()); |
| 134 watcher1.Reset(); | 96 watcher1.Reset(); |
| 135 watcher2.Reset(); | 97 watcher2.Reset(); |
| 136 | 98 |
| 137 // Removing the last PointerWatcher stops sending events to it. | 99 // Removing the last PointerWatcher stops sending events to it. |
| 138 connection->RemovePointerWatcher(&watcher2); | 100 connection->RemovePointerWatcher(&watcher2); |
| 139 OnEventObserved(mouse_pressed); | 101 OnPointerEventObserved(pointer_event_down); |
| 140 EXPECT_FALSE(watcher1.mouse_pressed()); | 102 EXPECT_FALSE(watcher1.last_event_observed()); |
| 141 EXPECT_FALSE(watcher1.touch_pressed()); | 103 EXPECT_FALSE(watcher2.last_event_observed()); |
| 142 } | 104 } |
| 143 | 105 |
| 144 TEST_F(WindowManagerConnectionTest, TouchEventWatcher) { | 106 TEST_F(WindowManagerConnectionTest, PointerWatcherMove) { |
| 145 base::MessageLoop message_loop(base::MessageLoop::TYPE_UI); | 107 base::MessageLoop message_loop(base::MessageLoop::TYPE_UI); |
| 146 ScopedViewsTestHelper helper; | 108 ScopedViewsTestHelper helper; |
| 147 WindowManagerConnection* connection = WindowManagerConnection::Get(); | 109 WindowManagerConnection* connection = WindowManagerConnection::Get(); |
| 148 ASSERT_TRUE(connection); | 110 ASSERT_TRUE(connection); |
| 149 | 111 |
| 150 const ui::EventType kMouseType[] = { | 112 ui::PointerEvent pointer_event_down( |
| 151 ui::ET_MOUSE_PRESSED, ui::ET_MOUSE_DRAGGED, ui::ET_MOUSE_MOVED, | 113 ui::ET_POINTER_DOWN, gfx::Point(), gfx::Point(), ui::EF_NONE, 1, |
| 152 ui::ET_MOUSE_ENTERED, ui::ET_MOUSE_EXITED, ui::ET_MOUSE_RELEASED}; | 114 ui::PointerDetails(ui::EventPointerType::POINTER_TYPE_TOUCH), |
| 153 const ui::EventType kTouchType[] = {ui::ET_TOUCH_PRESSED, ui::ET_TOUCH_MOVED, | 115 base::TimeTicks()); |
| 154 ui::ET_TOUCH_RELEASED, | 116 ui::PointerEvent pointer_event_move( |
| 155 ui::ET_TOUCH_CANCELLED}; | 117 ui::ET_POINTER_MOVED, gfx::Point(), gfx::Point(), ui::EF_NONE, 1, |
| 118 ui::PointerDetails(ui::EventPointerType::POINTER_TYPE_TOUCH), |
| 119 base::TimeTicks()); |
| 120 ui::PointerEvent pointer_event_enter( |
| 121 ui::ET_POINTER_ENTERED, gfx::Point(), gfx::Point(), ui::EF_NONE, 1, |
| 122 ui::PointerDetails(ui::EventPointerType::POINTER_TYPE_MOUSE), |
| 123 base::TimeTicks()); |
| 124 ui::PointerEvent pointer_event_exit( |
| 125 ui::ET_POINTER_EXITED, gfx::Point(), gfx::Point(), ui::EF_NONE, 1, |
| 126 ui::PointerDetails(ui::EventPointerType::POINTER_TYPE_MOUSE), |
| 127 base::TimeTicks()); |
| 156 | 128 |
| 157 TestTouchEventWatcher watcher1; | 129 // PointerWatchers receive pointer down events. |
| 158 connection->AddTouchEventWatcher(&watcher1); | 130 TestPointerWatcher watcher1; |
| 131 connection->AddPointerWatcher(&watcher1, true); |
| 132 OnPointerEventObserved(pointer_event_down); |
| 133 EXPECT_EQ(ui::ET_POINTER_DOWN, watcher1.last_event_observed()->type()); |
| 134 watcher1.Reset(); |
| 159 | 135 |
| 160 // TouchEventWatchers do not trigger for mouse events. | 136 // PointerWatchers receive pointer move events. |
| 161 for (size_t i = 0; i < arraysize(kMouseType); i++) { | 137 OnPointerEventObserved(pointer_event_move); |
| 162 ui::MouseEvent mouse_event(kMouseType[i], gfx::Point(), gfx::Point(), | 138 EXPECT_EQ(ui::ET_POINTER_MOVED, watcher1.last_event_observed()->type()); |
| 163 base::TimeTicks(), 0, 0); | 139 watcher1.Reset(); |
| 164 ui::PointerEvent mouse_pointer_event(mouse_event); | |
| 165 EXPECT_TRUE(mouse_pointer_event.IsMousePointerEvent()); | |
| 166 OnEventObserved(mouse_pointer_event); | |
| 167 EXPECT_FALSE(watcher1.touch_observed()); | |
| 168 watcher1.Reset(); | |
| 169 } | |
| 170 | 140 |
| 171 // TouchEventWatchers receive both TouchEvent and TouchPointerEvent. | 141 // PointerWatchers receive pointer enter events. |
| 172 for (size_t i = 0; i < arraysize(kTouchType); i++) { | 142 OnPointerEventObserved(pointer_event_enter); |
| 173 ui::TouchEvent touch_event(kTouchType[i], gfx::Point(), 0, | 143 EXPECT_EQ(ui::ET_POINTER_ENTERED, watcher1.last_event_observed()->type()); |
| 174 base::TimeTicks()); | 144 watcher1.Reset(); |
| 175 EXPECT_TRUE(touch_event.IsTouchEvent()); | |
| 176 OnEventObserved(touch_event); | |
| 177 EXPECT_TRUE(watcher1.touch_observed()); | |
| 178 watcher1.Reset(); | |
| 179 | 145 |
| 180 ui::PointerEvent touch_pointer_event(touch_event); | 146 // PointerWatchers receive pointer exit events. |
| 181 EXPECT_TRUE(touch_pointer_event.IsTouchPointerEvent()); | 147 OnPointerEventObserved(pointer_event_exit); |
| 182 OnEventObserved(touch_pointer_event); | 148 EXPECT_EQ(ui::ET_POINTER_EXITED, watcher1.last_event_observed()->type()); |
| 183 EXPECT_TRUE(watcher1.touch_observed()); | 149 watcher1.Reset(); |
| 184 watcher1.Reset(); | |
| 185 } | |
| 186 | 150 |
| 187 // Two TouchEventWatchers can both receive a single observed event. | 151 // Two PointerWatchers can both receive a single observed event. |
| 188 TestTouchEventWatcher watcher2; | 152 TestPointerWatcher watcher2; |
| 189 connection->AddTouchEventWatcher(&watcher2); | 153 connection->AddPointerWatcher(&watcher2, true); |
| 190 ui::TouchEvent touch_event(ui::ET_TOUCH_PRESSED, gfx::Point(), 0, | 154 OnPointerEventObserved(pointer_event_move); |
| 191 base::TimeTicks()); | 155 EXPECT_EQ(ui::ET_POINTER_MOVED, watcher1.last_event_observed()->type()); |
| 192 ui::PointerEvent touch_pointer_event(touch_event); | 156 EXPECT_EQ(ui::ET_POINTER_MOVED, watcher2.last_event_observed()->type()); |
| 193 OnEventObserved(touch_pointer_event); | |
| 194 EXPECT_TRUE(watcher1.touch_observed()); | |
| 195 EXPECT_TRUE(watcher2.touch_observed()); | |
| 196 watcher1.Reset(); | 157 watcher1.Reset(); |
| 197 watcher2.Reset(); | 158 watcher2.Reset(); |
| 198 | 159 |
| 199 // Removing the first TouchEventWatcher stops sending events to it. | 160 // Removing the first PointerWatcher stops sending events to it. |
| 200 connection->RemoveTouchEventWatcher(&watcher1); | 161 connection->RemovePointerWatcher(&watcher1); |
| 201 OnEventObserved(touch_pointer_event); | 162 OnPointerEventObserved(pointer_event_move); |
| 202 EXPECT_FALSE(watcher1.touch_observed()); | 163 EXPECT_FALSE(watcher1.last_event_observed()); |
| 203 EXPECT_TRUE(watcher2.touch_observed()); | 164 EXPECT_EQ(ui::ET_POINTER_MOVED, watcher2.last_event_observed()->type()); |
| 204 watcher1.Reset(); | 165 watcher1.Reset(); |
| 205 watcher2.Reset(); | 166 watcher2.Reset(); |
| 206 | 167 |
| 207 // Removing the last TouchEventWatcher stops sending events to it. | 168 // Removing the last PointerWatcher stops sending events to it. |
| 208 connection->RemoveTouchEventWatcher(&watcher2); | 169 connection->RemovePointerWatcher(&watcher2); |
| 209 OnEventObserved(touch_pointer_event); | 170 OnPointerEventObserved(pointer_event_move); |
| 210 EXPECT_FALSE(watcher1.touch_observed()); | 171 EXPECT_FALSE(watcher1.last_event_observed()); |
| 211 EXPECT_FALSE(watcher2.touch_observed()); | 172 EXPECT_FALSE(watcher2.last_event_observed()); |
| 212 } | 173 } |
| 213 | 174 |
| 214 } // namespace views | 175 } // namespace views |
| OLD | NEW |