Chromium Code Reviews| 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/laser/laser_pointer_controller.h" | |
| 6 #include "ash/laser/laser_pointer_controller_test_api.h" | |
| 7 #include "ash/laser/laser_pointer_points_test_api.h" | |
| 8 #include "ash/laser/laser_pointer_view.h" | |
| 9 #include "ash/shell.h" | |
| 10 #include "ash/test/ash_test_base.h" | |
| 11 #include "ui/events/test/event_generator.h" | |
| 12 | |
| 13 namespace ash { | |
| 14 namespace { | |
| 15 | |
| 16 const int kTestPointsLifetimeSeconds = 5; | |
| 17 | |
| 18 // TODO(sammiequon): Move this test into a different file. See | |
| 19 // http://crbug.com/646953. | |
| 20 class LaserPointerPointsTest : public test::AshTestBase { | |
| 21 public: | |
| 22 LaserPointerPointsTest() | |
| 23 : points_(base::TimeDelta::FromSeconds(kTestPointsLifetimeSeconds)) {} | |
| 24 | |
| 25 ~LaserPointerPointsTest() override {} | |
| 26 | |
| 27 void SetUp() override { | |
| 28 AshTestBase::SetUp(); | |
| 29 points_test_api_instance_.reset(new LaserPointerPoints( | |
|
James Cook
2016/09/16 18:13:31
I don't understand why you need this.
A typical w
sammiequon
2016/09/16 19:21:04
Done.
| |
| 30 base::TimeDelta::FromSeconds(kTestPointsLifetimeSeconds))); | |
| 31 points_test_api_.reset( | |
| 32 new LaserPointerPointsTestApi(points_test_api_instance_.get())); | |
| 33 } | |
| 34 | |
| 35 protected: | |
| 36 LaserPointerPoints points_; | |
| 37 std::unique_ptr<LaserPointerPoints> points_test_api_instance_; | |
| 38 std::unique_ptr<LaserPointerPointsTestApi> points_test_api_; | |
| 39 | |
| 40 private: | |
| 41 DISALLOW_COPY_AND_ASSIGN(LaserPointerPointsTest); | |
| 42 }; | |
| 43 | |
| 44 class LaserPointerControllerTest : public test::AshTestBase { | |
| 45 public: | |
| 46 LaserPointerControllerTest() {} | |
| 47 ~LaserPointerControllerTest() override {} | |
| 48 | |
| 49 void SetUp() override { | |
| 50 AshTestBase::SetUp(); | |
| 51 controller_test_api_instance_.reset(new LaserPointerController()); | |
| 52 controller_test_api_.reset( | |
| 53 new LaserPointerControllerTestApi(controller_test_api_instance_.get())); | |
| 54 } | |
| 55 | |
| 56 void TearDown() override { | |
| 57 // This needs to be called first to remove the event handler before the | |
| 58 // shell instance gets torn down. | |
| 59 controller_test_api_instance_.reset(); | |
| 60 controller_test_api_.reset(); | |
| 61 AshTestBase::TearDown(); | |
| 62 } | |
| 63 | |
| 64 protected: | |
| 65 std::unique_ptr<LaserPointerController> controller_test_api_instance_; | |
| 66 std::unique_ptr<LaserPointerControllerTestApi> controller_test_api_; | |
| 67 | |
| 68 private: | |
| 69 DISALLOW_COPY_AND_ASSIGN(LaserPointerControllerTest); | |
| 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 stylus movements as expected. | |
| 152 TEST_F(LaserPointerControllerTest, LaserPointerRenderer) { | |
| 153 // The laser pointer mode only works with stylus. | |
| 154 GetEventGenerator().EnterPenPointerMode(); | |
| 155 | |
| 156 // When disabled the laser pointer should not be showing. | |
| 157 GetEventGenerator().MoveMouseToInHost(gfx::Point(1, 1)); | |
| 158 EXPECT_FALSE(controller_test_api_->IsShowingLaserPointer()); | |
| 159 | |
| 160 // Verify that by enabling the mode, the laser pointer should still not be | |
| 161 // showing. | |
| 162 controller_test_api_->SetEnabled(true); | |
| 163 EXPECT_FALSE(controller_test_api_->IsShowingLaserPointer()); | |
| 164 | |
| 165 // Verify moving the stylus 4 times will the laser pointer should still not be | |
| 166 // showing. | |
| 167 GetEventGenerator().MoveMouseToInHost(gfx::Point(2, 2)); | |
| 168 GetEventGenerator().MoveMouseToInHost(gfx::Point(3, 3)); | |
| 169 GetEventGenerator().MoveMouseToInHost(gfx::Point(4, 4)); | |
| 170 GetEventGenerator().MoveMouseToInHost(gfx::Point(5, 5)); | |
| 171 EXPECT_FALSE(controller_test_api_->IsShowingLaserPointer()); | |
| 172 | |
| 173 // Verify pressing the stylus will add a point, and the laser pointer should | |
| 174 // be shown. | |
| 175 GetEventGenerator().PressLeftButton(); | |
| 176 EXPECT_TRUE(controller_test_api_->IsShowingLaserPointer()); | |
| 177 EXPECT_EQ(1, controller_test_api_->laser_points().GetNumberOfPoints()); | |
| 178 | |
| 179 // Verify dragging the stylus 2 times will add 2 more points. | |
| 180 GetEventGenerator().MoveMouseToInHost(gfx::Point(6, 6)); | |
| 181 GetEventGenerator().MoveMouseToInHost(gfx::Point(7, 7)); | |
| 182 EXPECT_EQ(3, controller_test_api_->laser_points().GetNumberOfPoints()); | |
| 183 | |
| 184 // Verify by releasing the stylus, the laser pointer will not be shown. | |
| 185 GetEventGenerator().ReleaseLeftButton(); | |
| 186 EXPECT_FALSE(controller_test_api_->IsShowingLaserPointer()); | |
| 187 | |
| 188 // Verify that by disabling the mode, the laser pointer view not be shown. | |
| 189 controller_test_api_->SetEnabled(false); | |
| 190 EXPECT_FALSE(controller_test_api_->IsShowingLaserPointer()); | |
| 191 | |
| 192 // Verify that the laser pointer does not add points while disabled. | |
| 193 GetEventGenerator().PressLeftButton(); | |
| 194 GetEventGenerator().MoveMouseToInHost(gfx::Point(8, 8)); | |
| 195 GetEventGenerator().ReleaseLeftButton(); | |
| 196 GetEventGenerator().MoveMouseToInHost(gfx::Point(9, 9)); | |
| 197 EXPECT_FALSE(controller_test_api_->IsShowingLaserPointer()); | |
| 198 | |
| 199 // Verify that the laser pointer does not get shown if points are not coming | |
| 200 // from the stylus, even when enabled. | |
| 201 GetEventGenerator().ExitPenPointerMode(); | |
| 202 controller_test_api_->SetEnabled(true); | |
| 203 GetEventGenerator().PressLeftButton(); | |
| 204 GetEventGenerator().MoveMouseToInHost(gfx::Point(10, 10)); | |
| 205 GetEventGenerator().MoveMouseToInHost(gfx::Point(11, 11)); | |
| 206 EXPECT_FALSE(controller_test_api_->IsShowingLaserPointer()); | |
| 207 GetEventGenerator().ReleaseLeftButton(); | |
| 208 } | |
| 209 } // namespace ash | |
| OLD | NEW |