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 | 14 |
15 namespace views { | 15 namespace views { |
16 namespace { | 16 namespace { |
17 | 17 |
18 class TestPointerWatcher : public PointerWatcher { | 18 class TestPointerWatcher : public PointerWatcher { |
19 public: | 19 public: |
20 TestPointerWatcher() {} | 20 TestPointerWatcher() {} |
21 ~TestPointerWatcher() override {} | 21 ~TestPointerWatcher() override {} |
22 | 22 |
23 bool mouse_pressed() const { return mouse_pressed_; } | 23 bool mouse_pressed() const { return mouse_pressed_; } |
24 bool touch_pressed() const { return touch_pressed_; } | 24 bool touch_pressed() const { return touch_pressed_; } |
| 25 bool touch_released() const { return touch_released_; } |
| 26 bool touch_moved() const { return touch_moved_; } |
| 27 bool touch_cancelled() const { return touch_cancelled_; } |
25 | 28 |
26 void Reset() { | 29 void Reset() { |
27 mouse_pressed_ = false; | 30 mouse_pressed_ = false; |
28 touch_pressed_ = false; | 31 touch_pressed_ = false; |
| 32 touch_released_ = false; |
| 33 touch_moved_ = false; |
| 34 touch_cancelled_ = false; |
29 } | 35 } |
30 | 36 |
31 // PointerWatcher: | 37 // PointerWatcher: |
32 void OnMousePressed(const ui::MouseEvent& event, | 38 void OnMousePressObserved(const ui::MouseEvent& event, |
33 const gfx::Point& location_in_screen, | 39 const gfx::Point& location_in_screen, |
34 Widget* target) override { | 40 views::Widget* target) override { |
35 mouse_pressed_ = true; | 41 mouse_pressed_ = true; |
36 } | 42 } |
37 void OnTouchPressed(const ui::TouchEvent& event, | 43 void OnTouchPressObserved(const ui::TouchEvent& event, |
38 const gfx::Point& location_in_screen, | 44 const gfx::Point& location_in_screen, |
39 Widget* target) override { | 45 views::Widget* target) override { |
40 touch_pressed_ = true; | 46 touch_pressed_ = true; |
41 } | 47 } |
| 48 void OnTouchReleaseObserved(const ui::TouchEvent& event, |
| 49 const gfx::Point& location_in_screen, |
| 50 views::Widget* target) override { |
| 51 touch_released_ = true; |
| 52 } |
| 53 void OnTouchMoveObserved(const ui::TouchEvent& event, |
| 54 const gfx::Point& location_in_screen, |
| 55 views::Widget* target) override { |
| 56 touch_moved_ = true; |
| 57 } |
| 58 void OnTouchCancellObserved(const ui::TouchEvent& event, |
| 59 const gfx::Point& location_in_screen, |
| 60 views::Widget* target) override { |
| 61 touch_cancelled_ = true; |
| 62 } |
42 | 63 |
43 private: | 64 private: |
44 bool mouse_pressed_ = false; | 65 bool mouse_pressed_ = false; |
45 bool touch_pressed_ = false; | 66 bool touch_pressed_ = false; |
| 67 bool touch_released_ = false; |
| 68 bool touch_moved_ = false; |
| 69 bool touch_cancelled_ = false; |
46 | 70 |
47 DISALLOW_COPY_AND_ASSIGN(TestPointerWatcher); | 71 DISALLOW_COPY_AND_ASSIGN(TestPointerWatcher); |
48 }; | 72 }; |
49 | 73 |
50 } // namespace | 74 } // namespace |
51 | 75 |
52 class WindowManagerConnectionTest : public testing::Test { | 76 class WindowManagerConnectionTest : public testing::Test { |
53 public: | 77 public: |
54 WindowManagerConnectionTest() {} | 78 WindowManagerConnectionTest() {} |
55 ~WindowManagerConnectionTest() override {} | 79 ~WindowManagerConnectionTest() override {} |
56 | 80 |
57 void OnEventObserved(const ui::Event& event) { | 81 void OnEventObserved(const ui::Event& event) { |
58 WindowManagerConnection::Get()->OnEventObserved(event, nullptr); | 82 WindowManagerConnection::Get()->OnEventObserved(event, nullptr); |
59 } | 83 } |
60 | 84 |
61 private: | 85 private: |
62 DISALLOW_COPY_AND_ASSIGN(WindowManagerConnectionTest); | 86 DISALLOW_COPY_AND_ASSIGN(WindowManagerConnectionTest); |
63 }; | 87 }; |
64 | 88 |
65 TEST_F(WindowManagerConnectionTest, PointerWatcher) { | 89 TEST_F(WindowManagerConnectionTest, PointerWatcher) { |
66 base::MessageLoop message_loop(base::MessageLoop::TYPE_UI); | 90 base::MessageLoop message_loop(base::MessageLoop::TYPE_UI); |
67 ScopedViewsTestHelper helper; | 91 ScopedViewsTestHelper helper; |
68 WindowManagerConnection* connection = WindowManagerConnection::Get(); | 92 WindowManagerConnection* connection = WindowManagerConnection::Get(); |
69 ASSERT_TRUE(connection); | 93 ASSERT_TRUE(connection); |
70 ui::MouseEvent mouse_pressed(ui::ET_MOUSE_PRESSED, gfx::Point(), gfx::Point(), | 94 ui::MouseEvent mouse_pressed(ui::ET_MOUSE_PRESSED, gfx::Point(), gfx::Point(), |
71 base::TimeTicks(), ui::EF_NONE, 0); | 95 base::TimeTicks(), ui::EF_NONE, 0); |
72 ui::TouchEvent touch_pressed(ui::ET_TOUCH_PRESSED, gfx::Point(), 1, | 96 ui::TouchEvent touch_pressed(ui::ET_TOUCH_PRESSED, gfx::Point(), 1, |
73 base::TimeTicks()); | 97 base::TimeTicks()); |
| 98 ui::TouchEvent touch_released(ui::ET_TOUCH_RELEASED, gfx::Point(), 1, |
| 99 base::TimeTicks()); |
| 100 ui::TouchEvent touch_moved(ui::ET_TOUCH_MOVED, gfx::Point(), 1, |
| 101 base::TimeTicks()); |
| 102 ui::TouchEvent touch_cancelled(ui::ET_TOUCH_CANCELLED, gfx::Point(), 1, |
| 103 base::TimeTicks()); |
74 ui::KeyEvent key_pressed(ui::ET_KEY_PRESSED, ui::VKEY_A, 0); | 104 ui::KeyEvent key_pressed(ui::ET_KEY_PRESSED, ui::VKEY_A, 0); |
75 | 105 |
76 // PointerWatchers receive mouse events. | 106 // PointerWatchers receive mouse events. |
77 TestPointerWatcher watcher1; | 107 TestPointerWatcher watcher1; |
78 connection->AddPointerWatcher(&watcher1); | 108 connection->AddPointerWatcher(&watcher1); |
79 OnEventObserved(mouse_pressed); | 109 OnEventObserved(mouse_pressed); |
80 EXPECT_TRUE(watcher1.mouse_pressed()); | 110 EXPECT_TRUE(watcher1.mouse_pressed()); |
81 watcher1.Reset(); | 111 watcher1.Reset(); |
82 | 112 |
83 // PointerWatchers receive touch events. | 113 // PointerWatchers receive touch events. |
84 OnEventObserved(touch_pressed); | 114 OnEventObserved(touch_pressed); |
85 EXPECT_TRUE(watcher1.touch_pressed()); | 115 EXPECT_TRUE(watcher1.touch_pressed()); |
86 watcher1.Reset(); | 116 watcher1.Reset(); |
87 | 117 |
| 118 OnEventObserved(touch_released); |
| 119 EXPECT_TRUE(watcher1.touch_released()); |
| 120 watcher1.Reset(); |
| 121 |
| 122 OnEventObserved(touch_moved); |
| 123 EXPECT_TRUE(watcher1.touch_moved()); |
| 124 watcher1.Reset(); |
| 125 |
| 126 OnEventObserved(touch_cancelled); |
| 127 EXPECT_TRUE(watcher1.touch_cancelled()); |
| 128 watcher1.Reset(); |
| 129 |
88 // PointerWatchers do not trigger for key events. | 130 // PointerWatchers do not trigger for key events. |
89 OnEventObserved(key_pressed); | 131 OnEventObserved(key_pressed); |
90 EXPECT_FALSE(watcher1.mouse_pressed()); | 132 EXPECT_FALSE(watcher1.mouse_pressed()); |
91 EXPECT_FALSE(watcher1.touch_pressed()); | 133 EXPECT_FALSE(watcher1.touch_pressed()); |
92 watcher1.Reset(); | 134 watcher1.Reset(); |
93 | 135 |
94 // Two PointerWatchers can both receive a single observed event. | 136 // Two PointerWatchers can both receive a single observed event. |
95 TestPointerWatcher watcher2; | 137 TestPointerWatcher watcher2; |
96 connection->AddPointerWatcher(&watcher2); | 138 connection->AddPointerWatcher(&watcher2); |
97 OnEventObserved(mouse_pressed); | 139 OnEventObserved(mouse_pressed); |
(...skipping 11 matching lines...) Expand all Loading... |
109 watcher2.Reset(); | 151 watcher2.Reset(); |
110 | 152 |
111 // Removing the last PointerWatcher stops sending events to it. | 153 // Removing the last PointerWatcher stops sending events to it. |
112 connection->RemovePointerWatcher(&watcher2); | 154 connection->RemovePointerWatcher(&watcher2); |
113 OnEventObserved(mouse_pressed); | 155 OnEventObserved(mouse_pressed); |
114 EXPECT_FALSE(watcher1.mouse_pressed()); | 156 EXPECT_FALSE(watcher1.mouse_pressed()); |
115 EXPECT_FALSE(watcher1.touch_pressed()); | 157 EXPECT_FALSE(watcher1.touch_pressed()); |
116 } | 158 } |
117 | 159 |
118 } // namespace views | 160 } // namespace views |
OLD | NEW |