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 ~LaserPointerPointsTest() override {} | |
23 | |
24 void SetUp() override { | |
25 AshTestBase::SetUp(); | |
26 points_.reset(new LaserPointerPoints( | |
27 base::TimeDelta::FromSeconds(kTestPointsLifetimeSeconds))); | |
28 points_test_api_.reset(new LaserPointerPointsTestApi( | |
29 base::WrapUnique<LaserPointerPoints>(new LaserPointerPoints( | |
30 base::TimeDelta::FromSeconds(kTestPointsLifetimeSeconds))))); | |
31 } | |
32 | |
33 void TearDown() override { AshTestBase::TearDown(); } | |
oshima
2016/08/24 23:01:03
you don't need this.
sammiequon
2016/08/25 17:59:04
Done.
| |
34 | |
35 protected: | |
36 std::unique_ptr<LaserPointerPoints> points_; | |
37 std::unique_ptr<LaserPointerPointsTestApi> points_test_api_; | |
oshima
2016/08/24 23:01:03
optional: do these have to be allocated in heap?
sammiequon
2016/08/25 17:59:04
Nope. Done.
| |
38 | |
39 private: | |
40 DISALLOW_COPY_AND_ASSIGN(LaserPointerPointsTest); | |
41 }; | |
42 | |
43 class LaserPointerModeTest : public test::AshTestBase { | |
44 public: | |
45 LaserPointerModeTest() {} | |
46 ~LaserPointerModeTest() override {} | |
47 | |
48 void SetUp() override { | |
49 AshTestBase::SetUp(); | |
50 WmShell::Get()->SetPaletteDelegateForTesting( | |
51 base::MakeUnique<TestPaletteDelegate>()); | |
52 mode_test_api_.reset(new LaserPointerModeTestApi( | |
53 base::WrapUnique<LaserPointerMode>(new LaserPointerMode(nullptr)))); | |
54 } | |
55 | |
56 void TearDown() override { | |
57 // This needs to be called first to remove the pointer watcher otherwise | |
58 // tear down will complain about there being more than zero pointer watcher | |
59 // alive. | |
60 mode_test_api_.reset(); | |
61 AshTestBase::TearDown(); | |
62 } | |
63 | |
64 protected: | |
65 std::unique_ptr<LaserPointerModeTestApi> mode_test_api_; | |
oshima
2016/08/24 23:01:03
ditto
sammiequon
2016/08/25 17:59:05
This one needs to because otherwise there will be
| |
66 | |
67 private: | |
68 DISALLOW_COPY_AND_ASSIGN(LaserPointerModeTest); | |
69 }; | |
70 | |
71 } // namespace | |
72 | |
73 // Tests that the laser pointers internal collection handles receiving points | |
74 // and that the functions are returning the expected output. | |
75 TEST_F(LaserPointerPointsTest, LaserPointerInternalCollection) { | |
76 EXPECT_TRUE(points_->IsEmpty()); | |
77 EXPECT_EQ(gfx::Rect(), points_->GetBoundingBox()); | |
78 gfx::Point left(1, 1); | |
79 gfx::Point bottom(1, 9); | |
80 gfx::Point top_right(30, 0); | |
81 gfx::Point last(2, 2); | |
oshima
2016/08/24 23:01:04
const ?
sammiequon
2016/08/25 17:59:04
Done.
| |
82 points_->AddPoint(left); | |
83 EXPECT_EQ(gfx::Rect(1, 1, 0, 0), points_->GetBoundingBox()); | |
84 | |
85 // Should be the new bottom of the bounding box. | |
86 points_->AddPoint(bottom); | |
87 EXPECT_EQ(gfx::Rect(1, 1, 0, bottom.y() - 1), points_->GetBoundingBox()); | |
88 | |
89 // Should be the new top and right of the bounding box. | |
90 points_->AddPoint(top_right); | |
91 EXPECT_EQ(3, points_->GetNumberOfPoints()); | |
92 EXPECT_FALSE(points_->IsEmpty()); | |
93 EXPECT_EQ(gfx::Rect(left.x(), top_right.y(), top_right.x() - left.x(), | |
94 bottom.y() - top_right.y()), | |
95 points_->GetBoundingBox()); | |
96 | |
97 // Should not expand bounding box. | |
98 points_->AddPoint(last); | |
99 EXPECT_EQ(gfx::Rect(left.x(), top_right.y(), top_right.x() - left.x(), | |
100 bottom.y() - top_right.y()), | |
101 points_->GetBoundingBox()); | |
102 | |
103 // Points should be sorted in the order they are added. | |
104 EXPECT_EQ(left, points_->GetOldest().location); | |
105 EXPECT_EQ(last, points_->GetNewest().location); | |
106 | |
107 // Add a new point which will expand the bounding box. | |
108 gfx::Point new_left_bottom(0, 40); | |
109 points_->AddPoint(new_left_bottom); | |
110 EXPECT_EQ(5, points_->GetNumberOfPoints()); | |
111 EXPECT_EQ(gfx::Rect(new_left_bottom.x(), top_right.y(), | |
112 top_right.x() - new_left_bottom.x(), | |
113 new_left_bottom.y() - top_right.y()), | |
114 points_->GetBoundingBox()); | |
115 | |
116 // Verify clearing works. | |
117 points_->Clear(); | |
118 EXPECT_TRUE(points_->IsEmpty()); | |
119 } | |
120 | |
121 // Test the laser pointer points collection to verify that old points are | |
122 // removed. | |
123 TEST_F(LaserPointerPointsTest, LaserPointerInternalCollectionDeletion) { | |
124 // When a point older than kTestPointsLifetime (5 seconds) is added, it | |
125 // should get removed. | |
126 points_test_api_->MoveForwardInTime(base::TimeDelta::FromSeconds(1)); | |
127 EXPECT_EQ(1, points_test_api_->GetNumberOfPoints()); | |
128 points_test_api_->MoveForwardInTime(base::TimeDelta::FromSeconds(1)); | |
129 EXPECT_EQ(2, points_test_api_->GetNumberOfPoints()); | |
130 | |
131 // Verify adding a point 10 seconds later will clear all other points, since | |
132 // they are older than 5 seconds. | |
133 points_test_api_->MoveForwardInTime(base::TimeDelta::FromSeconds(10)); | |
134 EXPECT_EQ(1, points_test_api_->GetNumberOfPoints()); | |
135 | |
136 // Verify adding 3 points one second apart each will add 3 points to the | |
137 // collection, since all 4 poitns are younger than 5 seconds. | |
138 points_test_api_->MoveForwardInTime(base::TimeDelta::FromSeconds(1)); | |
139 points_test_api_->MoveForwardInTime(base::TimeDelta::FromSeconds(1)); | |
140 points_test_api_->MoveForwardInTime(base::TimeDelta::FromSeconds(1)); | |
141 EXPECT_EQ(4, points_test_api_->GetNumberOfPoints()); | |
142 | |
143 // Verify adding 1 point three seconds later will remove 2 points which are | |
144 // older than 5 seconds. | |
145 points_test_api_->MoveForwardInTime(base::TimeDelta::FromSeconds(3)); | |
146 EXPECT_EQ(3, points_test_api_->GetNumberOfPoints()); | |
147 } | |
148 | |
149 // Test to ensure the class responsible for drawing the laser pointer receives | |
150 // points from mouse movements as expected. | |
151 TEST_F(LaserPointerModeTest, LaserPointerRenderer) { | |
152 // TODO(sammiequon): Update these tests once event generator with stylus | |
153 // events cl lands. | |
154 GetEventGenerator().MoveMouseToInHost(gfx::Point(10, 40)); | |
155 EXPECT_EQ(0, mode_test_api_->laser_points().GetNumberOfPoints()); | |
156 | |
157 // Verify enabling the mode will start with a single point at the current | |
158 // location. | |
159 mode_test_api_->OnEnable(); | |
160 EXPECT_EQ(1, mode_test_api_->laser_points().GetNumberOfPoints()); | |
161 | |
162 // Verify moving the mouse 4 times will add 4 more points. | |
163 GetEventGenerator().MoveMouseToInHost(gfx::Point(25, 66)); | |
164 GetEventGenerator().MoveMouseToInHost(gfx::Point(91, 38)); | |
165 GetEventGenerator().MoveMouseToInHost(gfx::Point(34, 58)); | |
166 GetEventGenerator().MoveMouseToInHost(gfx::Point(19, 71)); | |
167 EXPECT_EQ(5, mode_test_api_->laser_points().GetNumberOfPoints()); | |
168 | |
169 // Verify disabling the mode will clear any active points. | |
170 mode_test_api_->OnDisable(); | |
171 EXPECT_EQ(0, mode_test_api_->laser_points().GetNumberOfPoints()); | |
172 | |
173 // Verify that the laser pointer does not add points while disabled. | |
174 GetEventGenerator().MoveMouseToInHost(gfx::Point(34, 58)); | |
175 GetEventGenerator().MoveMouseToInHost(gfx::Point(19, 71)); | |
176 EXPECT_EQ(0, mode_test_api_->laser_points().GetNumberOfPoints()); | |
177 | |
178 mode_test_api_->OnEnable(); | |
179 EXPECT_EQ(1, mode_test_api_->laser_points().GetNumberOfPoints()); | |
180 EXPECT_EQ(GetEventGenerator().current_location(), | |
181 mode_test_api_->laser_points().GetNewest().location); | |
182 } | |
183 } // namespace ash | |
OLD | NEW |