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 "ash/common/system/chromeos/palette/tools/laser_pointer_mode.h" | 5 #include "ash/laser/laser_pointer_controller.h" |
6 #include "ash/common/system/chromeos/palette/tools/laser_pointer_mode_test_api.h
" | 6 #include "ash/laser/laser_pointer_controller_test_api.h" |
7 #include "ash/common/system/chromeos/palette/tools/laser_pointer_points_test_api
.h" | 7 #include "ash/laser/laser_pointer_points_test_api.h" |
8 #include "ash/common/system/chromeos/palette/tools/laser_pointer_view.h" | 8 #include "ash/laser/laser_pointer_view.h" |
9 #include "ash/common/test/test_palette_delegate.h" | 9 #include "ash/shell.h" |
10 #include "ash/common/wm_shell.h" | |
11 #include "ash/test/ash_test_base.h" | 10 #include "ash/test/ash_test_base.h" |
12 #include "ui/events/test/event_generator.h" | 11 #include "ui/events/test/event_generator.h" |
13 | 12 |
14 namespace ash { | 13 namespace ash { |
15 namespace { | 14 namespace { |
16 | 15 |
17 const int kTestPointsLifetimeSeconds = 5; | 16 const int kTestPointsLifetimeSeconds = 5; |
18 | 17 |
| 18 // TODO(sammiequon): Move this test into a different file. See |
| 19 // http://crbug.com/646953. |
19 class LaserPointerPointsTest : public test::AshTestBase { | 20 class LaserPointerPointsTest : public test::AshTestBase { |
20 public: | 21 public: |
21 LaserPointerPointsTest() | 22 LaserPointerPointsTest() |
22 : points_(base::TimeDelta::FromSeconds(kTestPointsLifetimeSeconds)), | 23 : points_(base::TimeDelta::FromSeconds(kTestPointsLifetimeSeconds)) {} |
23 points_test_api_(base::MakeUnique<LaserPointerPoints>( | |
24 base::TimeDelta::FromSeconds(kTestPointsLifetimeSeconds))) {} | |
25 | 24 |
26 ~LaserPointerPointsTest() override {} | 25 ~LaserPointerPointsTest() override {} |
27 | 26 |
28 void SetUp() override { | |
29 AshTestBase::SetUp(); | |
30 // Add a test delegate so that laser pointer mode does not complain when | |
31 // being destroyed. | |
32 WmShell::Get()->SetPaletteDelegateForTesting( | |
33 base::MakeUnique<TestPaletteDelegate>()); | |
34 } | |
35 | |
36 protected: | 27 protected: |
37 LaserPointerPoints points_; | 28 LaserPointerPoints points_; |
38 LaserPointerPointsTestApi points_test_api_; | |
39 | 29 |
40 private: | 30 private: |
41 DISALLOW_COPY_AND_ASSIGN(LaserPointerPointsTest); | 31 DISALLOW_COPY_AND_ASSIGN(LaserPointerPointsTest); |
42 }; | 32 }; |
43 | 33 |
44 class LaserPointerModeTest : public test::AshTestBase { | 34 class LaserPointerControllerTest : public test::AshTestBase { |
45 public: | 35 public: |
46 LaserPointerModeTest() {} | 36 LaserPointerControllerTest() {} |
47 ~LaserPointerModeTest() override {} | 37 ~LaserPointerControllerTest() override {} |
48 | 38 |
49 void SetUp() override { | 39 void SetUp() override { |
50 AshTestBase::SetUp(); | 40 AshTestBase::SetUp(); |
51 WmShell::Get()->SetPaletteDelegateForTesting( | 41 controller_.reset(new LaserPointerController()); |
52 base::MakeUnique<TestPaletteDelegate>()); | |
53 mode_test_api_.reset(new LaserPointerModeTestApi( | |
54 base::WrapUnique<LaserPointerMode>(new LaserPointerMode(nullptr)))); | |
55 } | 42 } |
56 | 43 |
57 void TearDown() override { | 44 void TearDown() override { |
58 // This needs to be called first to remove the pointer watcher otherwise | 45 // This needs to be called first to remove the event handler before the |
59 // tear down will complain about there being more than zero pointer watcher | 46 // shell instance gets torn down. |
60 // alive. | 47 controller_.reset(); |
61 mode_test_api_.reset(); | |
62 AshTestBase::TearDown(); | 48 AshTestBase::TearDown(); |
63 } | 49 } |
64 | 50 |
65 protected: | 51 protected: |
66 std::unique_ptr<LaserPointerModeTestApi> mode_test_api_; | 52 std::unique_ptr<LaserPointerController> controller_; |
67 | 53 |
68 private: | 54 private: |
69 DISALLOW_COPY_AND_ASSIGN(LaserPointerModeTest); | 55 DISALLOW_COPY_AND_ASSIGN(LaserPointerControllerTest); |
70 }; | 56 }; |
71 | 57 |
72 } // namespace | 58 } // namespace |
73 | 59 |
74 // Tests that the laser pointers internal collection handles receiving points | 60 // Tests that the laser pointers internal collection handles receiving points |
75 // and that the functions are returning the expected output. | 61 // and that the functions are returning the expected output. |
76 TEST_F(LaserPointerPointsTest, LaserPointerInternalCollection) { | 62 TEST_F(LaserPointerPointsTest, LaserPointerInternalCollection) { |
77 EXPECT_TRUE(points_.IsEmpty()); | 63 EXPECT_TRUE(points_.IsEmpty()); |
78 EXPECT_EQ(gfx::Rect(), points_.GetBoundingBox()); | 64 EXPECT_EQ(gfx::Rect(), points_.GetBoundingBox()); |
79 const gfx::Point left(1, 1); | 65 const gfx::Point left(1, 1); |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
115 points_.GetBoundingBox()); | 101 points_.GetBoundingBox()); |
116 | 102 |
117 // Verify clearing works. | 103 // Verify clearing works. |
118 points_.Clear(); | 104 points_.Clear(); |
119 EXPECT_TRUE(points_.IsEmpty()); | 105 EXPECT_TRUE(points_.IsEmpty()); |
120 } | 106 } |
121 | 107 |
122 // Test the laser pointer points collection to verify that old points are | 108 // Test the laser pointer points collection to verify that old points are |
123 // removed. | 109 // removed. |
124 TEST_F(LaserPointerPointsTest, LaserPointerInternalCollectionDeletion) { | 110 TEST_F(LaserPointerPointsTest, LaserPointerInternalCollectionDeletion) { |
| 111 LaserPointerPointsTestApi points_test_api_(&points_); |
| 112 |
125 // When a point older than kTestPointsLifetime (5 seconds) is added, it | 113 // When a point older than kTestPointsLifetime (5 seconds) is added, it |
126 // should get removed. | 114 // should get removed. |
127 points_test_api_.MoveForwardInTime(base::TimeDelta::FromSeconds(1)); | 115 points_test_api_.MoveForwardInTime(base::TimeDelta::FromSeconds(1)); |
128 EXPECT_EQ(1, points_test_api_.GetNumberOfPoints()); | 116 EXPECT_EQ(1, points_test_api_.GetNumberOfPoints()); |
129 points_test_api_.MoveForwardInTime(base::TimeDelta::FromSeconds(1)); | 117 points_test_api_.MoveForwardInTime(base::TimeDelta::FromSeconds(1)); |
130 EXPECT_EQ(2, points_test_api_.GetNumberOfPoints()); | 118 EXPECT_EQ(2, points_test_api_.GetNumberOfPoints()); |
131 | 119 |
132 // Verify adding a point 10 seconds later will clear all other points, since | 120 // Verify adding a point 10 seconds later will clear all other points, since |
133 // they are older than 5 seconds. | 121 // they are older than 5 seconds. |
134 points_test_api_.MoveForwardInTime(base::TimeDelta::FromSeconds(10)); | 122 points_test_api_.MoveForwardInTime(base::TimeDelta::FromSeconds(10)); |
135 EXPECT_EQ(1, points_test_api_.GetNumberOfPoints()); | 123 EXPECT_EQ(1, points_test_api_.GetNumberOfPoints()); |
136 | 124 |
137 // Verify adding 3 points one second apart each will add 3 points to the | 125 // Verify adding 3 points one second apart each will add 3 points to the |
138 // collection, since all 4 poitns are younger than 5 seconds. | 126 // collection, since all 4 poitns are younger than 5 seconds. |
139 points_test_api_.MoveForwardInTime(base::TimeDelta::FromSeconds(1)); | 127 points_test_api_.MoveForwardInTime(base::TimeDelta::FromSeconds(1)); |
140 points_test_api_.MoveForwardInTime(base::TimeDelta::FromSeconds(1)); | 128 points_test_api_.MoveForwardInTime(base::TimeDelta::FromSeconds(1)); |
141 points_test_api_.MoveForwardInTime(base::TimeDelta::FromSeconds(1)); | 129 points_test_api_.MoveForwardInTime(base::TimeDelta::FromSeconds(1)); |
142 EXPECT_EQ(4, points_test_api_.GetNumberOfPoints()); | 130 EXPECT_EQ(4, points_test_api_.GetNumberOfPoints()); |
143 | 131 |
144 // Verify adding 1 point three seconds later will remove 2 points which are | 132 // Verify adding 1 point three seconds later will remove 2 points which are |
145 // older than 5 seconds. | 133 // older than 5 seconds. |
146 points_test_api_.MoveForwardInTime(base::TimeDelta::FromSeconds(3)); | 134 points_test_api_.MoveForwardInTime(base::TimeDelta::FromSeconds(3)); |
147 EXPECT_EQ(3, points_test_api_.GetNumberOfPoints()); | 135 EXPECT_EQ(3, points_test_api_.GetNumberOfPoints()); |
148 } | 136 } |
149 | 137 |
150 // Test to ensure the class responsible for drawing the laser pointer receives | 138 // Test to ensure the class responsible for drawing the laser pointer receives |
151 // points from mouse movements as expected. | 139 // points from stylus movements as expected. |
152 TEST_F(LaserPointerModeTest, LaserPointerRenderer) { | 140 TEST_F(LaserPointerControllerTest, LaserPointerRenderer) { |
| 141 LaserPointerControllerTestApi controller_test_api_(controller_.get()); |
| 142 |
153 // The laser pointer mode only works with stylus. | 143 // The laser pointer mode only works with stylus. |
154 GetEventGenerator().EnterPenPointerMode(); | 144 GetEventGenerator().EnterPenPointerMode(); |
155 GetEventGenerator().MoveMouseToInHost(gfx::Point(10, 40)); | |
156 EXPECT_EQ(0, mode_test_api_->laser_points().GetNumberOfPoints()); | |
157 | 145 |
158 // Verify enabling the mode will start with a single point at the current | 146 // When disabled the laser pointer should not be showing. |
159 // location. | 147 GetEventGenerator().MoveMouseToInHost(gfx::Point(1, 1)); |
160 mode_test_api_->OnEnable(); | 148 EXPECT_FALSE(controller_test_api_.IsShowingLaserPointer()); |
161 EXPECT_EQ(1, mode_test_api_->laser_points().GetNumberOfPoints()); | |
162 | 149 |
163 // Verify moving the mouse 4 times will add 4 more points. | 150 // Verify that by enabling the mode, the laser pointer should still not be |
164 GetEventGenerator().MoveMouseToInHost(gfx::Point(25, 66)); | 151 // showing. |
165 GetEventGenerator().MoveMouseToInHost(gfx::Point(91, 38)); | 152 controller_test_api_.SetEnabled(true); |
166 GetEventGenerator().MoveMouseToInHost(gfx::Point(34, 58)); | 153 EXPECT_FALSE(controller_test_api_.IsShowingLaserPointer()); |
167 GetEventGenerator().MoveMouseToInHost(gfx::Point(19, 71)); | |
168 EXPECT_EQ(5, mode_test_api_->laser_points().GetNumberOfPoints()); | |
169 | 154 |
170 // Verify disabling the mode will clear any active points. | 155 // Verify moving the stylus 4 times will not display the laser pointer. |
171 mode_test_api_->OnDisable(); | 156 GetEventGenerator().MoveMouseToInHost(gfx::Point(2, 2)); |
172 EXPECT_EQ(0, mode_test_api_->laser_points().GetNumberOfPoints()); | 157 GetEventGenerator().MoveMouseToInHost(gfx::Point(3, 3)); |
| 158 GetEventGenerator().MoveMouseToInHost(gfx::Point(4, 4)); |
| 159 GetEventGenerator().MoveMouseToInHost(gfx::Point(5, 5)); |
| 160 EXPECT_FALSE(controller_test_api_.IsShowingLaserPointer()); |
| 161 |
| 162 // Verify pressing the stylus will show the laser pointer and add a point. |
| 163 GetEventGenerator().PressLeftButton(); |
| 164 EXPECT_TRUE(controller_test_api_.IsShowingLaserPointer()); |
| 165 EXPECT_EQ(1, controller_test_api_.laser_points().GetNumberOfPoints()); |
| 166 |
| 167 // Verify dragging the stylus 2 times will add 2 more points. |
| 168 GetEventGenerator().MoveMouseToInHost(gfx::Point(6, 6)); |
| 169 GetEventGenerator().MoveMouseToInHost(gfx::Point(7, 7)); |
| 170 EXPECT_EQ(3, controller_test_api_.laser_points().GetNumberOfPoints()); |
| 171 |
| 172 // Verify releasing the stylus hides the laser pointer. |
| 173 GetEventGenerator().ReleaseLeftButton(); |
| 174 EXPECT_FALSE(controller_test_api_.IsShowingLaserPointer()); |
| 175 |
| 176 // Verify that disabling the mode does not display the laser pointer. |
| 177 controller_test_api_.SetEnabled(false); |
| 178 EXPECT_FALSE(controller_test_api_.IsShowingLaserPointer()); |
| 179 |
| 180 // Verify that disabling the mode while laser pointer is displayed does not |
| 181 // display the laser pointer. |
| 182 controller_test_api_.SetEnabled(true); |
| 183 GetEventGenerator().PressLeftButton(); |
| 184 GetEventGenerator().MoveMouseToInHost(gfx::Point(6, 6)); |
| 185 EXPECT_TRUE(controller_test_api_.IsShowingLaserPointer()); |
| 186 controller_test_api_.SetEnabled(false); |
| 187 EXPECT_FALSE(controller_test_api_.IsShowingLaserPointer()); |
173 | 188 |
174 // Verify that the laser pointer does not add points while disabled. | 189 // Verify that the laser pointer does not add points while disabled. |
175 GetEventGenerator().MoveMouseToInHost(gfx::Point(34, 58)); | 190 GetEventGenerator().PressLeftButton(); |
176 GetEventGenerator().MoveMouseToInHost(gfx::Point(19, 71)); | 191 GetEventGenerator().MoveMouseToInHost(gfx::Point(8, 8)); |
177 EXPECT_EQ(0, mode_test_api_->laser_points().GetNumberOfPoints()); | 192 GetEventGenerator().ReleaseLeftButton(); |
| 193 GetEventGenerator().MoveMouseToInHost(gfx::Point(9, 9)); |
| 194 EXPECT_FALSE(controller_test_api_.IsShowingLaserPointer()); |
178 | 195 |
179 // Verify that the laser pointer adds the last seen stylus point when enabled | 196 // Verify that the laser pointer does not get shown if points are not coming |
180 // even when stylus mode is disabled. | 197 // from the stylus, even when enabled. |
181 GetEventGenerator().ExitPenPointerMode(); | 198 GetEventGenerator().ExitPenPointerMode(); |
182 mode_test_api_->OnEnable(); | 199 controller_test_api_.SetEnabled(true); |
183 EXPECT_EQ(1, mode_test_api_->laser_points().GetNumberOfPoints()); | 200 GetEventGenerator().PressLeftButton(); |
184 EXPECT_EQ(GetEventGenerator().current_location(), | 201 GetEventGenerator().MoveMouseToInHost(gfx::Point(10, 10)); |
185 mode_test_api_->laser_points().GetNewest().location); | 202 GetEventGenerator().MoveMouseToInHost(gfx::Point(11, 11)); |
186 // Verify that the laser pointer does not add additional points when move | 203 EXPECT_FALSE(controller_test_api_.IsShowingLaserPointer()); |
187 // events are not from stylus. | 204 GetEventGenerator().ReleaseLeftButton(); |
188 GetEventGenerator().MoveMouseToInHost(gfx::Point(34, 58)); | |
189 GetEventGenerator().MoveMouseToInHost(gfx::Point(19, 71)); | |
190 EXPECT_EQ(1, mode_test_api_->laser_points().GetNumberOfPoints()); | |
191 } | 205 } |
192 } // namespace ash | 206 } // namespace ash |
OLD | NEW |