jdufault | 62b8b6e | 2016-08-24 20:34:53 | [diff] [blame] | 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 | |
jdufault | 62b8b6e | 2016-08-24 20:34:53 | [diff] [blame] | 5 | #include "ash/magnifier/magnification_controller.h" |
| 6 | #include "ash/magnifier/partial_magnification_controller.h" |
| 7 | #include "ash/shell.h" |
| 8 | #include "ash/test/ash_test_base.h" |
rjkroege | 72f8154f | 2016-10-29 00:49:02 | [diff] [blame] | 9 | #include "ui/display/manager/display_manager.h" |
jdufault | 62b8b6e | 2016-08-24 20:34:53 | [diff] [blame] | 10 | #include "ui/events/test/event_generator.h" |
| 11 | #include "ui/views/widget/widget.h" |
| 12 | |
| 13 | namespace ash { |
| 14 | |
| 15 | // Wrapper for PartialMagnificationController that exposes internal state to |
| 16 | // test functions. |
| 17 | class PartialMagnificationControllerTestApi { |
| 18 | public: |
| 19 | explicit PartialMagnificationControllerTestApi( |
| 20 | PartialMagnificationController* controller) |
| 21 | : controller_(controller) {} |
| 22 | ~PartialMagnificationControllerTestApi() {} |
| 23 | |
| 24 | bool is_enabled() const { return controller_->is_enabled_; } |
| 25 | bool is_active() const { return controller_->is_active_; } |
| 26 | views::Widget* host_widget() const { return controller_->host_widget_; } |
| 27 | |
| 28 | gfx::Point GetWidgetOrigin() const { |
| 29 | return host_widget()->GetWindowBoundsInScreen().origin(); |
| 30 | } |
| 31 | |
| 32 | private: |
| 33 | PartialMagnificationController* controller_; |
| 34 | |
| 35 | DISALLOW_ASSIGN(PartialMagnificationControllerTestApi); |
| 36 | }; |
| 37 | |
James Cook | 317781a | 2017-07-18 02:08:06 | [diff] [blame] | 38 | class PartialMagnificationControllerTest : public AshTestBase { |
jdufault | 62b8b6e | 2016-08-24 20:34:53 | [diff] [blame] | 39 | public: |
| 40 | PartialMagnificationControllerTest() {} |
| 41 | ~PartialMagnificationControllerTest() override {} |
| 42 | |
| 43 | void SetUp() override { |
| 44 | AshTestBase::SetUp(); |
sky | cb4be5b | 2017-04-06 17:52:45 | [diff] [blame] | 45 | Shell::Get()->display_manager()->UpdateDisplays(); |
jdufault | 62b8b6e | 2016-08-24 20:34:53 | [diff] [blame] | 46 | } |
| 47 | |
| 48 | protected: |
| 49 | PartialMagnificationController* GetController() const { |
sky | cb4be5b | 2017-04-06 17:52:45 | [diff] [blame] | 50 | return Shell::Get()->partial_magnification_controller(); |
jdufault | 62b8b6e | 2016-08-24 20:34:53 | [diff] [blame] | 51 | } |
| 52 | |
| 53 | PartialMagnificationControllerTestApi GetTestApi() const { |
| 54 | return PartialMagnificationControllerTestApi( |
sky | cb4be5b | 2017-04-06 17:52:45 | [diff] [blame] | 55 | Shell::Get()->partial_magnification_controller()); |
jdufault | 62b8b6e | 2016-08-24 20:34:53 | [diff] [blame] | 56 | } |
| 57 | |
| 58 | private: |
| 59 | DISALLOW_COPY_AND_ASSIGN(PartialMagnificationControllerTest); |
| 60 | }; |
| 61 | |
| 62 | // The magnifier should not show up immediately after being enabled. |
| 63 | TEST_F(PartialMagnificationControllerTest, InactiveByDefault) { |
| 64 | GetController()->SetEnabled(true); |
| 65 | EXPECT_FALSE(GetTestApi().is_active()); |
| 66 | EXPECT_FALSE(GetTestApi().host_widget()); |
| 67 | } |
| 68 | |
| 69 | // The magnifier should show up only after a pointer is pressed while enabled. |
| 70 | TEST_F(PartialMagnificationControllerTest, ActiveOnPointerDown) { |
| 71 | GetEventGenerator().EnterPenPointerMode(); |
| 72 | |
| 73 | // While disabled no magnifier shows up. |
jdufault | 1df36f3 | 2016-12-05 20:47:02 | [diff] [blame] | 74 | GetEventGenerator().PressTouch(); |
jdufault | 62b8b6e | 2016-08-24 20:34:53 | [diff] [blame] | 75 | EXPECT_FALSE(GetTestApi().is_active()); |
| 76 | EXPECT_FALSE(GetTestApi().host_widget()); |
jdufault | 1df36f3 | 2016-12-05 20:47:02 | [diff] [blame] | 77 | GetEventGenerator().ReleaseTouch(); |
jdufault | 62b8b6e | 2016-08-24 20:34:53 | [diff] [blame] | 78 | |
| 79 | // While enabled the magnifier is only active while the pointer is down. |
| 80 | GetController()->SetEnabled(true); |
jdufault | 1df36f3 | 2016-12-05 20:47:02 | [diff] [blame] | 81 | GetEventGenerator().PressTouch(); |
jdufault | 62b8b6e | 2016-08-24 20:34:53 | [diff] [blame] | 82 | EXPECT_TRUE(GetTestApi().is_active()); |
| 83 | EXPECT_TRUE(GetTestApi().host_widget()); |
jdufault | 1df36f3 | 2016-12-05 20:47:02 | [diff] [blame] | 84 | GetEventGenerator().ReleaseTouch(); |
jdufault | 62b8b6e | 2016-08-24 20:34:53 | [diff] [blame] | 85 | EXPECT_FALSE(GetTestApi().is_active()); |
| 86 | EXPECT_FALSE(GetTestApi().host_widget()); |
| 87 | } |
| 88 | |
| 89 | // Verifies that nothing bad happens if a second display is disconnected while |
| 90 | // the magnifier is active. |
| 91 | TEST_F(PartialMagnificationControllerTest, MultipleDisplays) { |
jdufault | 62b8b6e | 2016-08-24 20:34:53 | [diff] [blame] | 92 | GetEventGenerator().EnterPenPointerMode(); |
| 93 | |
| 94 | // Active magnifier with two displays, move it to the second display. |
| 95 | UpdateDisplay("800x600,800x600"); |
| 96 | GetController()->SetEnabled(true); |
jdufault | 1df36f3 | 2016-12-05 20:47:02 | [diff] [blame] | 97 | GetEventGenerator().PressTouch(); |
| 98 | GetEventGenerator().MoveTouch(gfx::Point(1200, 300)); |
jdufault | 62b8b6e | 2016-08-24 20:34:53 | [diff] [blame] | 99 | EXPECT_TRUE(GetTestApi().is_active()); |
| 100 | EXPECT_TRUE(GetTestApi().host_widget()); |
| 101 | |
| 102 | // Disconnect the second display, verify that magnifier is still active. |
| 103 | UpdateDisplay("800x600"); |
| 104 | GetController()->SwitchTargetRootWindowIfNeeded(nullptr); |
| 105 | EXPECT_TRUE(GetTestApi().is_active()); |
| 106 | EXPECT_TRUE(GetTestApi().host_widget()); |
| 107 | } |
| 108 | |
| 109 | // Turning the magnifier off while it is active destroys the window. |
| 110 | TEST_F(PartialMagnificationControllerTest, DisablingDisablesActive) { |
| 111 | GetEventGenerator().EnterPenPointerMode(); |
| 112 | |
| 113 | GetController()->SetEnabled(true); |
jdufault | 1df36f3 | 2016-12-05 20:47:02 | [diff] [blame] | 114 | GetEventGenerator().PressTouch(); |
jdufault | 62b8b6e | 2016-08-24 20:34:53 | [diff] [blame] | 115 | EXPECT_TRUE(GetTestApi().is_active()); |
| 116 | |
| 117 | GetController()->SetEnabled(false); |
| 118 | EXPECT_FALSE(GetTestApi().is_active()); |
| 119 | EXPECT_FALSE(GetTestApi().host_widget()); |
| 120 | } |
| 121 | |
| 122 | // The magnifier only activates for pointer events. |
| 123 | TEST_F(PartialMagnificationControllerTest, ActivatesOnlyForPointer) { |
| 124 | GetController()->SetEnabled(true); |
jdufault | 62b8b6e | 2016-08-24 20:34:53 | [diff] [blame] | 125 | GetEventGenerator().PressTouch(); |
| 126 | EXPECT_FALSE(GetTestApi().is_active()); |
| 127 | } |
| 128 | |
| 129 | // The magnifier is always located at pointer. |
| 130 | TEST_F(PartialMagnificationControllerTest, MagnifierFollowsPointer) { |
| 131 | GetEventGenerator().EnterPenPointerMode(); |
| 132 | GetController()->SetEnabled(true); |
| 133 | |
| 134 | // The window does not have to be centered on the press; compute the initial |
| 135 | // window placement offset. Use a Vector2d for the + operator overload. |
jdufault | 1df36f3 | 2016-12-05 20:47:02 | [diff] [blame] | 136 | GetEventGenerator().PressTouch(); |
jdufault | 62b8b6e | 2016-08-24 20:34:53 | [diff] [blame] | 137 | gfx::Vector2d offset(GetTestApi().GetWidgetOrigin().x(), |
| 138 | GetTestApi().GetWidgetOrigin().y()); |
| 139 | |
| 140 | // Move the pointer around, make sure the window follows it. |
jdufault | 1df36f3 | 2016-12-05 20:47:02 | [diff] [blame] | 141 | GetEventGenerator().MoveTouch(gfx::Point(32, 32)); |
jdufault | 62b8b6e | 2016-08-24 20:34:53 | [diff] [blame] | 142 | EXPECT_EQ(GetEventGenerator().current_location() + offset, |
| 143 | GetTestApi().GetWidgetOrigin()); |
| 144 | |
jdufault | 1df36f3 | 2016-12-05 20:47:02 | [diff] [blame] | 145 | GetEventGenerator().MoveTouch(gfx::Point(0, 10)); |
jdufault | 62b8b6e | 2016-08-24 20:34:53 | [diff] [blame] | 146 | EXPECT_EQ(GetEventGenerator().current_location() + offset, |
| 147 | GetTestApi().GetWidgetOrigin()); |
| 148 | |
jdufault | 1df36f3 | 2016-12-05 20:47:02 | [diff] [blame] | 149 | GetEventGenerator().MoveTouch(gfx::Point(10, 0)); |
jdufault | 62b8b6e | 2016-08-24 20:34:53 | [diff] [blame] | 150 | EXPECT_EQ(GetEventGenerator().current_location() + offset, |
| 151 | GetTestApi().GetWidgetOrigin()); |
| 152 | |
jdufault | 1df36f3 | 2016-12-05 20:47:02 | [diff] [blame] | 153 | GetEventGenerator().ReleaseTouch(); |
jdufault | 62b8b6e | 2016-08-24 20:34:53 | [diff] [blame] | 154 | |
| 155 | // Make sure the window is initially placed correctly. |
| 156 | GetEventGenerator().set_current_location(gfx::Point(50, 20)); |
| 157 | EXPECT_FALSE(GetTestApi().is_active()); |
jdufault | 1df36f3 | 2016-12-05 20:47:02 | [diff] [blame] | 158 | GetEventGenerator().PressTouch(); |
jdufault | 62b8b6e | 2016-08-24 20:34:53 | [diff] [blame] | 159 | EXPECT_EQ(GetEventGenerator().current_location() + offset, |
| 160 | GetTestApi().GetWidgetOrigin()); |
| 161 | } |
| 162 | |
| 163 | } // namespace ash |