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_view.h" | |
7 #include "ash/common/wm_shell.h" | |
8 #include "ash/test/ash_test_base.h" | |
9 #include "ui/events/test/event_generator.h" | |
10 | |
11 namespace ash { | |
12 | |
13 class LaserPointerPointsTestApi : public LaserPointerPoints { | |
jdufault
2016/08/22 18:37:36
Can you make this take the LaserPointerPoints inst
sammiequon
2016/08/23 00:20:33
Done.
| |
14 public: | |
15 LaserPointerPointsTestApi(base::TimeDelta life_time) | |
16 : LaserPointerPoints(life_time) {} | |
17 ~LaserPointerPointsTestApi() {} | |
18 | |
19 void MoveForwardInTime(const base::TimeDelta& delta, | |
20 const base::Time& time, | |
21 const gfx::Point& location) { | |
22 for (LaserPointerPoints::LaserPoint& point : points_) | |
23 point.creation_time -= delta; | |
24 LaserPointerPoints::LaserPoint new_point; | |
jdufault
2016/08/22 18:37:36
Instead of building a new_point instance just call
sammiequon
2016/08/23 00:20:33
I did it this way since AddPoint adds it at Time::
| |
25 new_point.creation_time = time; | |
26 new_point.location = location; | |
27 points_.push_back(new_point); | |
28 ClearOldPoints(); | |
29 } | |
30 | |
31 private: | |
32 DISALLOW_COPY_AND_ASSIGN(LaserPointerPointsTestApi); | |
33 }; | |
34 | |
35 class LaserPointerModeTestApi { | |
36 public: | |
37 LaserPointerModeTestApi(std::unique_ptr<LaserPointerMode> instance) | |
38 : instance_(std::move(instance)) {} | |
39 | |
40 ~LaserPointerModeTestApi() {} | |
41 | |
42 void OnEnable() { instance_->OnEnable(); } | |
43 | |
44 void OnDisable() { instance_->OnDisable(); } | |
45 | |
46 const LaserPointerPoints& laser_points() { | |
47 return instance_->laser_pointer_view_->laser_points_; | |
48 } | |
49 | |
50 gfx::Point current_mouse_location() { | |
51 return instance_->current_mouse_location_; | |
52 } | |
53 | |
54 private: | |
55 std::unique_ptr<LaserPointerMode> instance_; | |
56 | |
57 DISALLOW_COPY_AND_ASSIGN(LaserPointerModeTestApi); | |
58 }; | |
59 | |
60 namespace { | |
jdufault
2016/08/22 18:37:36
The anonymous namespace should be at the top. Pull
sammiequon
2016/08/23 00:20:33
Done.
| |
61 | |
62 const int kTestPointsLifetime = 5; | |
jdufault
2016/08/22 18:37:36
Can you specify the unit here?
sammiequon
2016/08/23 00:20:33
Done.
| |
63 | |
64 class LaserPointerPointsTest : public test::AshTestBase { | |
65 public: | |
66 LaserPointerPointsTest() {} | |
67 ~LaserPointerPointsTest() override {} | |
68 | |
69 void SetUp() override { | |
70 AshTestBase::SetUp(); | |
71 points_.reset(new LaserPointerPoints( | |
72 base::TimeDelta::FromSeconds(kTestPointsLifetime))); | |
73 points_test_api_.reset(new LaserPointerPointsTestApi( | |
74 base::TimeDelta::FromSeconds(kTestPointsLifetime))); | |
75 } | |
76 | |
77 void TearDown() override { AshTestBase::TearDown(); } | |
78 | |
79 protected: | |
80 std::unique_ptr<LaserPointerPoints> points_; | |
81 std::unique_ptr<LaserPointerPointsTestApi> points_test_api_; | |
82 | |
83 private: | |
84 DISALLOW_COPY_AND_ASSIGN(LaserPointerPointsTest); | |
85 }; | |
86 | |
87 class LaserPointerModeTest : public test::AshTestBase { | |
88 public: | |
89 LaserPointerModeTest() {} | |
90 ~LaserPointerModeTest() override {} | |
91 | |
92 void SetUp() override { | |
93 AshTestBase::SetUp(); | |
94 mode_test_api_.reset(new LaserPointerModeTestApi( | |
95 base::WrapUnique<LaserPointerMode>(new LaserPointerMode(nullptr)))); | |
96 } | |
97 | |
98 void TearDown() override { | |
99 mode_test_api_.reset(); | |
100 AshTestBase::TearDown(); | |
101 } | |
102 | |
103 protected: | |
104 std::unique_ptr<LaserPointerModeTestApi> mode_test_api_; | |
105 | |
106 private: | |
107 DISALLOW_COPY_AND_ASSIGN(LaserPointerModeTest); | |
108 }; | |
109 } // namespace | |
110 | |
111 // Tests that the laser pointers internal collection handles receiving points | |
112 // and that the functions are returning the expected output. | |
113 TEST_F(LaserPointerPointsTest, LaserPointerInternalCollection) { | |
114 gfx::Point left(1, 1); | |
115 gfx::Point bottom(3, 9); | |
116 gfx::Point top_right(30, 0); | |
117 points_->AddPoint(left); | |
118 points_->AddPoint(gfx::Point(2, 2)); // Should not expand bounding box. | |
119 points_->AddPoint(bottom); | |
120 points_->AddPoint(top_right); | |
jdufault
2016/08/22 18:37:36
I think you should also verify the bounding box wh
sammiequon
2016/08/23 00:20:33
Done.
| |
121 | |
122 // Test laser pointer points collection and helper functions. | |
jdufault
2016/08/22 18:37:36
Replace this comment with a series of comments tha
sammiequon
2016/08/23 00:20:33
Done.
| |
123 EXPECT_EQ(4, points_->GetNumberOfPoints()); | |
124 EXPECT_FALSE(points_->IsEmpty()); | |
125 EXPECT_EQ(gfx::Rect(left.x(), top_right.y(), top_right.x() - left.x(), | |
126 bottom.y() - top_right.y()), | |
127 points_->GetBoundingBox()); | |
128 EXPECT_EQ(left, points_->GetOldest().location); | |
jdufault
2016/08/22 18:37:36
Instead of testing GetOldest/GetNewest here, I'd w
sammiequon
2016/08/23 00:20:33
The time stuff is in the next test.
| |
129 EXPECT_EQ(top_right, points_->GetNewest().location); | |
130 | |
131 gfx::Point new_left_bottom(0, 40); | |
jdufault
2016/08/22 18:37:36
// Add a new point which will expand the bounding
sammiequon
2016/08/23 00:20:33
Done.
| |
132 points_->AddPoint(new_left_bottom); | |
133 EXPECT_EQ(5, points_->GetNumberOfPoints()); | |
134 EXPECT_EQ(gfx::Rect(new_left_bottom.x(), top_right.y(), | |
135 top_right.x() - new_left_bottom.x(), | |
136 new_left_bottom.y() - top_right.y()), | |
137 points_->GetBoundingBox()); | |
138 | |
139 points_->Clear(); | |
140 EXPECT_TRUE(points_->IsEmpty()); | |
141 } | |
142 | |
143 // Test the laser pointer points collection to verify that old points are | |
144 // removed. | |
145 TEST_F(LaserPointerPointsTest, LaserPointerInternalCollectionDeletion) { | |
146 gfx::Point test_point(0, 0); | |
147 base::Time start_time = base::Time::Now(); | |
148 | |
149 // When a point older than kTestPointsLifetime is added, it should get | |
150 // removed. | |
jdufault
2016/08/22 18:37:36
Can you add comments at the interesting points in
sammiequon
2016/08/23 00:20:33
Done.
| |
151 points_test_api_->MoveForwardInTime(base::TimeDelta::FromSeconds(1), | |
152 start_time, test_point); | |
153 EXPECT_EQ(points_test_api_->GetNumberOfPoints(), 1); | |
154 points_test_api_->MoveForwardInTime(base::TimeDelta::FromSeconds(1), | |
155 start_time, test_point); | |
156 EXPECT_EQ(points_test_api_->GetNumberOfPoints(), 2); | |
157 points_test_api_->MoveForwardInTime(base::TimeDelta::FromSeconds(10), | |
158 start_time, test_point); | |
159 EXPECT_EQ(points_test_api_->GetNumberOfPoints(), 1); | |
160 points_test_api_->MoveForwardInTime(base::TimeDelta::FromSeconds(1), | |
161 start_time, test_point); | |
162 points_test_api_->MoveForwardInTime(base::TimeDelta::FromSeconds(1), | |
163 start_time, test_point); | |
164 points_test_api_->MoveForwardInTime(base::TimeDelta::FromSeconds(1), | |
165 start_time, test_point); | |
166 EXPECT_EQ(points_test_api_->GetNumberOfPoints(), 4); | |
167 points_test_api_->MoveForwardInTime(base::TimeDelta::FromSeconds(3), | |
168 start_time, test_point); | |
169 EXPECT_EQ(points_test_api_->GetNumberOfPoints(), 3); | |
170 } | |
171 | |
172 // Test to ensure the class responsible for drawing the laser pointer receives | |
173 // points from mouse movements as expected. | |
174 TEST_F(LaserPointerModeTest, LaserPointerRenderer) { | |
175 // TODO(sammiequon): Update these tests once event generator with stylus | |
176 // events cl lands. | |
177 GetEventGenerator().MoveMouseToInHost(gfx::Point(10, 40)); | |
178 EXPECT_EQ(0, mode_test_api_->laser_points().GetNumberOfPoints()); | |
179 mode_test_api_->OnEnable(); | |
180 | |
181 GetEventGenerator().MoveMouseToInHost(gfx::Point(25, 66)); | |
182 GetEventGenerator().MoveMouseToInHost(gfx::Point(91, 38)); | |
183 GetEventGenerator().MoveMouseToInHost(gfx::Point(34, 58)); | |
184 GetEventGenerator().MoveMouseToInHost(gfx::Point(19, 71)); | |
185 | |
186 EXPECT_EQ(5, mode_test_api_->laser_points().GetNumberOfPoints()); | |
187 | |
188 mode_test_api_->OnDisable(); | |
189 GetEventGenerator().MoveMouseToInHost(gfx::Point(34, 58)); | |
190 GetEventGenerator().MoveMouseToInHost(gfx::Point(19, 71)); | |
191 EXPECT_EQ(0, mode_test_api_->laser_points().GetNumberOfPoints()); | |
jdufault
2016/08/22 18:37:36
I'd add another EXPECT_EQ(0, ...) right after the
sammiequon
2016/08/23 00:20:33
Done.
| |
192 | |
193 mode_test_api_->OnEnable(); | |
194 EXPECT_EQ(1, mode_test_api_->laser_points().GetNumberOfPoints()); | |
195 EXPECT_EQ(GetEventGenerator().current_location(), | |
196 mode_test_api_->laser_points().GetNewest().location); | |
197 } | |
198 } // namespace ash | |
OLD | NEW |