OLD | NEW |
| (Empty) |
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 | |
3 // found in the LICENSE file. | |
4 | |
5 #include "ash/common/system/chromeos/palette/tools/laser_pointer_mode.h" | |
6 #include "ash/common/system/chromeos/palette/tools/laser_pointer_mode_test_api.h
" | |
7 #include "ash/common/system/chromeos/palette/tools/laser_pointer_points_test_api
.h" | |
8 #include "ash/common/system/chromeos/palette/tools/laser_pointer_view.h" | |
9 #include "ash/common/test/test_palette_delegate.h" | |
10 #include "ash/common/wm_shell.h" | |
11 #include "ash/test/ash_test_base.h" | |
12 #include "ui/events/test/event_generator.h" | |
13 | |
14 namespace ash { | |
15 namespace { | |
16 | |
17 const int kTestPointsLifetimeSeconds = 5; | |
18 | |
19 class LaserPointerPointsTest : public test::AshTestBase { | |
20 public: | |
21 LaserPointerPointsTest() | |
22 : points_(base::TimeDelta::FromSeconds(kTestPointsLifetimeSeconds)), | |
23 points_test_api_(base::MakeUnique<LaserPointerPoints>( | |
24 base::TimeDelta::FromSeconds(kTestPointsLifetimeSeconds))) {} | |
25 | |
26 ~LaserPointerPointsTest() override {} | |
27 | |
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: | |
37 LaserPointerPoints points_; | |
38 LaserPointerPointsTestApi points_test_api_; | |
39 | |
40 private: | |
41 DISALLOW_COPY_AND_ASSIGN(LaserPointerPointsTest); | |
42 }; | |
43 | |
44 class LaserPointerModeTest : public test::AshTestBase { | |
45 public: | |
46 LaserPointerModeTest() {} | |
47 ~LaserPointerModeTest() override {} | |
48 | |
49 void SetUp() override { | |
50 AshTestBase::SetUp(); | |
51 WmShell::Get()->SetPaletteDelegateForTesting( | |
52 base::MakeUnique<TestPaletteDelegate>()); | |
53 mode_test_api_.reset(new LaserPointerModeTestApi( | |
54 base::WrapUnique<LaserPointerMode>(new LaserPointerMode(nullptr)))); | |
55 } | |
56 | |
57 void TearDown() override { | |
58 // This needs to be called first to remove the pointer watcher otherwise | |
59 // tear down will complain about there being more than zero pointer watcher | |
60 // alive. | |
61 mode_test_api_.reset(); | |
62 AshTestBase::TearDown(); | |
63 } | |
64 | |
65 protected: | |
66 std::unique_ptr<LaserPointerModeTestApi> mode_test_api_; | |
67 | |
68 private: | |
69 DISALLOW_COPY_AND_ASSIGN(LaserPointerModeTest); | |
70 }; | |
71 | |
72 } // namespace | |
73 | |
74 // Tests that the laser pointers internal collection handles receiving points | |
75 // and that the functions are returning the expected output. | |
76 TEST_F(LaserPointerPointsTest, LaserPointerInternalCollection) { | |
77 EXPECT_TRUE(points_.IsEmpty()); | |
78 EXPECT_EQ(gfx::Rect(), points_.GetBoundingBox()); | |
79 const gfx::Point left(1, 1); | |
80 const gfx::Point bottom(1, 9); | |
81 const gfx::Point top_right(30, 0); | |
82 const gfx::Point last(2, 2); | |
83 points_.AddPoint(left); | |
84 EXPECT_EQ(gfx::Rect(1, 1, 0, 0), points_.GetBoundingBox()); | |
85 | |
86 // Should be the new bottom of the bounding box. | |
87 points_.AddPoint(bottom); | |
88 EXPECT_EQ(gfx::Rect(1, 1, 0, bottom.y() - 1), points_.GetBoundingBox()); | |
89 | |
90 // Should be the new top and right of the bounding box. | |
91 points_.AddPoint(top_right); | |
92 EXPECT_EQ(3, points_.GetNumberOfPoints()); | |
93 EXPECT_FALSE(points_.IsEmpty()); | |
94 EXPECT_EQ(gfx::Rect(left.x(), top_right.y(), top_right.x() - left.x(), | |
95 bottom.y() - top_right.y()), | |
96 points_.GetBoundingBox()); | |
97 | |
98 // Should not expand bounding box. | |
99 points_.AddPoint(last); | |
100 EXPECT_EQ(gfx::Rect(left.x(), top_right.y(), top_right.x() - left.x(), | |
101 bottom.y() - top_right.y()), | |
102 points_.GetBoundingBox()); | |
103 | |
104 // Points should be sorted in the order they are added. | |
105 EXPECT_EQ(left, points_.GetOldest().location); | |
106 EXPECT_EQ(last, points_.GetNewest().location); | |
107 | |
108 // Add a new point which will expand the bounding box. | |
109 gfx::Point new_left_bottom(0, 40); | |
110 points_.AddPoint(new_left_bottom); | |
111 EXPECT_EQ(5, points_.GetNumberOfPoints()); | |
112 EXPECT_EQ(gfx::Rect(new_left_bottom.x(), top_right.y(), | |
113 top_right.x() - new_left_bottom.x(), | |
114 new_left_bottom.y() - top_right.y()), | |
115 points_.GetBoundingBox()); | |
116 | |
117 // Verify clearing works. | |
118 points_.Clear(); | |
119 EXPECT_TRUE(points_.IsEmpty()); | |
120 } | |
121 | |
122 // Test the laser pointer points collection to verify that old points are | |
123 // removed. | |
124 TEST_F(LaserPointerPointsTest, LaserPointerInternalCollectionDeletion) { | |
125 // When a point older than kTestPointsLifetime (5 seconds) is added, it | |
126 // should get removed. | |
127 points_test_api_.MoveForwardInTime(base::TimeDelta::FromSeconds(1)); | |
128 EXPECT_EQ(1, points_test_api_.GetNumberOfPoints()); | |
129 points_test_api_.MoveForwardInTime(base::TimeDelta::FromSeconds(1)); | |
130 EXPECT_EQ(2, points_test_api_.GetNumberOfPoints()); | |
131 | |
132 // Verify adding a point 10 seconds later will clear all other points, since | |
133 // they are older than 5 seconds. | |
134 points_test_api_.MoveForwardInTime(base::TimeDelta::FromSeconds(10)); | |
135 EXPECT_EQ(1, points_test_api_.GetNumberOfPoints()); | |
136 | |
137 // 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. | |
139 points_test_api_.MoveForwardInTime(base::TimeDelta::FromSeconds(1)); | |
140 points_test_api_.MoveForwardInTime(base::TimeDelta::FromSeconds(1)); | |
141 points_test_api_.MoveForwardInTime(base::TimeDelta::FromSeconds(1)); | |
142 EXPECT_EQ(4, points_test_api_.GetNumberOfPoints()); | |
143 | |
144 // Verify adding 1 point three seconds later will remove 2 points which are | |
145 // older than 5 seconds. | |
146 points_test_api_.MoveForwardInTime(base::TimeDelta::FromSeconds(3)); | |
147 EXPECT_EQ(3, points_test_api_.GetNumberOfPoints()); | |
148 } | |
149 | |
150 // Test to ensure the class responsible for drawing the laser pointer receives | |
151 // points from mouse movements as expected. | |
152 TEST_F(LaserPointerModeTest, LaserPointerRenderer) { | |
153 // The laser pointer mode only works with stylus. | |
154 GetEventGenerator().EnterPenPointerMode(); | |
155 GetEventGenerator().MoveMouseToInHost(gfx::Point(10, 40)); | |
156 EXPECT_EQ(0, mode_test_api_->laser_points().GetNumberOfPoints()); | |
157 | |
158 // Verify enabling the mode will start with a single point at the current | |
159 // location. | |
160 mode_test_api_->OnEnable(); | |
161 EXPECT_EQ(1, mode_test_api_->laser_points().GetNumberOfPoints()); | |
162 | |
163 // Verify moving the mouse 4 times will add 4 more points. | |
164 GetEventGenerator().MoveMouseToInHost(gfx::Point(25, 66)); | |
165 GetEventGenerator().MoveMouseToInHost(gfx::Point(91, 38)); | |
166 GetEventGenerator().MoveMouseToInHost(gfx::Point(34, 58)); | |
167 GetEventGenerator().MoveMouseToInHost(gfx::Point(19, 71)); | |
168 EXPECT_EQ(5, mode_test_api_->laser_points().GetNumberOfPoints()); | |
169 | |
170 // Verify disabling the mode will clear any active points. | |
171 mode_test_api_->OnDisable(); | |
172 EXPECT_EQ(0, mode_test_api_->laser_points().GetNumberOfPoints()); | |
173 | |
174 // Verify that the laser pointer does not add points while disabled. | |
175 GetEventGenerator().MoveMouseToInHost(gfx::Point(34, 58)); | |
176 GetEventGenerator().MoveMouseToInHost(gfx::Point(19, 71)); | |
177 EXPECT_EQ(0, mode_test_api_->laser_points().GetNumberOfPoints()); | |
178 | |
179 // Verify that the laser pointer adds the last seen stylus point when enabled | |
180 // even when stylus mode is disabled. | |
181 GetEventGenerator().ExitPenPointerMode(); | |
182 mode_test_api_->OnEnable(); | |
183 EXPECT_EQ(1, mode_test_api_->laser_points().GetNumberOfPoints()); | |
184 EXPECT_EQ(GetEventGenerator().current_location(), | |
185 mode_test_api_->laser_points().GetNewest().location); | |
186 // Verify that the laser pointer does not add additional points when move | |
187 // events are not from stylus. | |
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 } | |
192 } // namespace ash | |
OLD | NEW |