| OLD | NEW | 
|---|
| (Empty) |  | 
|  | 1 // Copyright (c) 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/palette_tool_manager.h" | 
|  | 6 #include "ash/common/system/chromeos/palette/tools/laser_pointer_mode.h" | 
|  | 7 #include "ash/common/system/chromeos/palette/tools/laser_pointer_points.h" | 
|  | 8 #include "ash/common/wm_shell.h" | 
|  | 9 #include "ash/test/ash_test_base.h" | 
|  | 10 #include "ui/events/test/event_generator.h" | 
|  | 11 | 
|  | 12 namespace ash { | 
|  | 13 namespace { | 
|  | 14 | 
|  | 15 class TestPointerWatcherWithMove : public views::PointerWatcher { | 
|  | 16  public: | 
|  | 17   TestPointerWatcherWithMove() {} | 
|  | 18   ~TestPointerWatcherWithMove() override {} | 
|  | 19 | 
|  | 20   void RegisterForEvents(bool wants_moves) { | 
|  | 21     WmShell::Get()->AddPointerWatcher(this, wants_moves); | 
|  | 22   } | 
|  | 23 | 
|  | 24   void UnregisterForEvents() { WmShell::Get()->RemovePointerWatcher(this); } | 
|  | 25 | 
|  | 26   void SetCurrentLocation(const gfx::Point& new_point) { | 
|  | 27     current_location_ = new_point; | 
|  | 28   } | 
|  | 29 | 
|  | 30   gfx::Point current_location_; | 
|  | 31   // views::PointerWatcher: | 
|  | 32   void OnPointerEventObserved(const ui::PointerEvent& event, | 
|  | 33                               const gfx::Point& location_in_screen, | 
|  | 34                               views::Widget* target) override { | 
|  | 35     if (event.type() == ui::ET_POINTER_MOVED) | 
|  | 36       current_location_ = location_in_screen; | 
|  | 37   } | 
|  | 38 | 
|  | 39   DISALLOW_COPY_AND_ASSIGN(TestPointerWatcherWithMove); | 
|  | 40 }; | 
|  | 41 | 
|  | 42 class TestLaserPointerMode : public LaserPointerMode { | 
|  | 43  public: | 
|  | 44   TestLaserPointerMode(Delegate* delegate) : LaserPointerMode(delegate) {} | 
|  | 45 | 
|  | 46   // Expose the internal points structure for testing. | 
|  | 47   LaserPointerPoints GetPoints() { return laser_points_; } | 
|  | 48   bool Enabled() { return enabled(); } | 
|  | 49 | 
|  | 50   DISALLOW_COPY_AND_ASSIGN(TestLaserPointerMode); | 
|  | 51 }; | 
|  | 52 | 
|  | 53 class LaserPointerModeTest : public test::AshTestBase, | 
|  | 54                              public PaletteToolManager::Delegate { | 
|  | 55  public: | 
|  | 56   LaserPointerModeTest() | 
|  | 57       : laser_pointer_points_(base::TimeDelta::FromSecondsD(5.0)) {} | 
|  | 58   ~LaserPointerModeTest() override {} | 
|  | 59 | 
|  | 60   void SetUp() override { | 
|  | 61     AshTestBase::SetUp(); | 
|  | 62     palette_tool_manager_.reset(new PaletteToolManager(this)); | 
|  | 63     laser_pointer_mode_ = new TestLaserPointerMode(NULL); | 
|  | 64     palette_tool_manager_->AddTool(base::WrapUnique(laser_pointer_mode_)); | 
|  | 65   } | 
|  | 66 | 
|  | 67   void TearDown() override { | 
|  | 68     laser_pointer_mode_ = NULL; | 
|  | 69     palette_tool_manager_.reset(nullptr); | 
|  | 70     AshTestBase::TearDown(); | 
|  | 71   } | 
|  | 72 | 
|  | 73  protected: | 
|  | 74   // PaletteToolManager::Delegate: | 
|  | 75   void HidePalette() override {} | 
|  | 76   void OnActiveToolChanged() override {} | 
|  | 77   WmWindow* GetWindow() override { | 
|  | 78     NOTREACHED(); | 
|  | 79     return nullptr; | 
|  | 80   } | 
|  | 81   std::unique_ptr<PaletteToolManager> palette_tool_manager_; | 
|  | 82   TestLaserPointerMode* laser_pointer_mode_; | 
|  | 83   TestPointerWatcherWithMove watcher_; | 
|  | 84   LaserPointerPoints laser_pointer_points_; | 
|  | 85 | 
|  | 86  private: | 
|  | 87   DISALLOW_COPY_AND_ASSIGN(LaserPointerModeTest); | 
|  | 88 }; | 
|  | 89 | 
|  | 90 TEST_F(LaserPointerModeTest, PointerWatcherWithMove) { | 
|  | 91   gfx::Point point1(100, 1); | 
|  | 92   gfx::Point point2(2, 432); | 
|  | 93   gfx::Point point3(322, 19); | 
|  | 94   gfx::Point point4(0, 0); | 
|  | 95 | 
|  | 96   // If move events wanted moving the point should change our local point. | 
|  | 97   watcher_.RegisterForEvents(true); | 
|  | 98   GetEventGenerator().MoveMouseToInHost(point1); | 
|  | 99   EXPECT_EQ(point1, watcher_.current_location_); | 
|  | 100   GetEventGenerator().MoveMouseToInHost(point2); | 
|  | 101   EXPECT_EQ(point2, watcher_.current_location_); | 
|  | 102   GetEventGenerator().MoveMouseToInHost(point3); | 
|  | 103   EXPECT_EQ(point3, watcher_.current_location_); | 
|  | 104   watcher_.UnregisterForEvents(); | 
|  | 105 | 
|  | 106   // If move events are not wanted moving the point should not affect our local | 
|  | 107   // point. | 
|  | 108   watcher_.RegisterForEvents(false); | 
|  | 109   watcher_.SetCurrentLocation(point4); | 
|  | 110   GetEventGenerator().MoveMouseToInHost(point1); | 
|  | 111   EXPECT_EQ(point4, watcher_.current_location_); | 
|  | 112   GetEventGenerator().MoveMouseToInHost(point2); | 
|  | 113   EXPECT_EQ(point4, watcher_.current_location_); | 
|  | 114   GetEventGenerator().MoveMouseToInHost(point3); | 
|  | 115   EXPECT_EQ(point4, watcher_.current_location_); | 
|  | 116   watcher_.UnregisterForEvents(); | 
|  | 117 } | 
|  | 118 | 
|  | 119 TEST_F(LaserPointerModeTest, LaserPointerPoints) { | 
|  | 120   gfx::Point point1(1, 1); | 
|  | 121   gfx::Point point2(2, 2); | 
|  | 122   gfx::Point point3(3, 9); | 
|  | 123   gfx::Point point4(30, 0); | 
|  | 124   gfx::Point point5(0, 40); | 
|  | 125   laser_pointer_points_.AddPoint(point1); | 
|  | 126   laser_pointer_points_.AddPoint(point2); | 
|  | 127   laser_pointer_points_.AddPoint(point3); | 
|  | 128   laser_pointer_points_.AddPoint(point4); | 
|  | 129 | 
|  | 130   // Test laser pointer points collection and helper functions. | 
|  | 131   EXPECT_EQ(4, laser_pointer_points_.GetNumberOfPoints()); | 
|  | 132   EXPECT_FALSE(laser_pointer_points_.IsEmpty()); | 
|  | 133   EXPECT_EQ(gfx::Rect(1, 0, 29, 9), laser_pointer_points_.GetBoundingBox()); | 
|  | 134   EXPECT_EQ(point1, laser_pointer_points_.GetOldest().location_); | 
|  | 135   EXPECT_EQ(point4, laser_pointer_points_.GetMostRecent().location_); | 
|  | 136 | 
|  | 137   laser_pointer_points_.AddPoint(point5); | 
|  | 138   EXPECT_EQ(5, laser_pointer_points_.GetNumberOfPoints()); | 
|  | 139   EXPECT_EQ(gfx::Rect(0, 0, 30, 40), laser_pointer_points_.GetBoundingBox()); | 
|  | 140 | 
|  | 141   laser_pointer_points_.Clear(); | 
|  | 142   EXPECT_TRUE(laser_pointer_points_.IsEmpty()); | 
|  | 143 } | 
|  | 144 | 
|  | 145 TEST_F(LaserPointerModeTest, LaserPointerView) { | 
|  | 146   EXPECT_FALSE(laser_pointer_mode_->Enabled()); | 
|  | 147   EXPECT_EQ(0, laser_pointer_mode_->GetPoints().GetNumberOfPoints()); | 
|  | 148   palette_tool_manager_->ActivateTool(PaletteToolId::LASER_POINTER); | 
|  | 149   EXPECT_TRUE(laser_pointer_mode_->Enabled()); | 
|  | 150   EXPECT_EQ(1, laser_pointer_mode_->GetPoints().GetNumberOfPoints()); | 
|  | 151 | 
|  | 152   GetEventGenerator().MoveMouseToInHost(gfx::Point(10, 40)); | 
|  | 153   GetEventGenerator().MoveMouseToInHost(gfx::Point(25, 66)); | 
|  | 154   GetEventGenerator().MoveMouseToInHost(gfx::Point(91, 38)); | 
|  | 155   GetEventGenerator().MoveMouseToInHost(gfx::Point(34, 58)); | 
|  | 156   GetEventGenerator().MoveMouseToInHost(gfx::Point(19, 71)); | 
|  | 157   EXPECT_EQ(6, laser_pointer_mode_->GetPoints().GetNumberOfPoints()); | 
|  | 158   EXPECT_EQ(gfx::Rect(0, 0, 91, 71), | 
|  | 159             laser_pointer_mode_->GetPoints().GetBoundingBox()); | 
|  | 160 | 
|  | 161   palette_tool_manager_->DeactivateTool(PaletteToolId::LASER_POINTER); | 
|  | 162   EXPECT_FALSE(laser_pointer_mode_->Enabled()); | 
|  | 163   EXPECT_EQ(0, laser_pointer_mode_->GetPoints().GetNumberOfPoints()); | 
|  | 164   GetEventGenerator().MoveMouseToInHost(gfx::Point(49, 11)); | 
|  | 165   GetEventGenerator().MoveMouseToInHost(gfx::Point(43, 11)); | 
|  | 166   palette_tool_manager_->ActivateTool(PaletteToolId::LASER_POINTER); | 
|  | 167   GetEventGenerator().MoveMouseToInHost(gfx::Point(23, 51)); | 
|  | 168   EXPECT_EQ(2, laser_pointer_mode_->GetPoints().GetNumberOfPoints()); | 
|  | 169   EXPECT_EQ(gfx::Point(43, 11), | 
|  | 170             laser_pointer_mode_->GetPoints().GetOldest().location_); | 
|  | 171   EXPECT_EQ(gfx::Point(23, 51), | 
|  | 172             laser_pointer_mode_->GetPoints().GetMostRecent().location_); | 
|  | 173 } | 
|  | 174 } | 
|  | 175 }  // namespace ash | 
| OLD | NEW | 
|---|