blob: 4da69eeda80623ac232ca8906c547bb5da5e9f67 [file] [log] [blame]
jdufault62b8b6e2016-08-24 20:34:531// 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
jdufault62b8b6e2016-08-24 20:34:535#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"
rjkroege72f8154f2016-10-29 00:49:029#include "ui/display/manager/display_manager.h"
jdufault62b8b6e2016-08-24 20:34:5310#include "ui/events/test/event_generator.h"
11#include "ui/views/widget/widget.h"
12
13namespace ash {
14
15// Wrapper for PartialMagnificationController that exposes internal state to
16// test functions.
17class 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 Cook317781a2017-07-18 02:08:0638class PartialMagnificationControllerTest : public AshTestBase {
jdufault62b8b6e2016-08-24 20:34:5339 public:
40 PartialMagnificationControllerTest() {}
41 ~PartialMagnificationControllerTest() override {}
42
43 void SetUp() override {
44 AshTestBase::SetUp();
skycb4be5b2017-04-06 17:52:4545 Shell::Get()->display_manager()->UpdateDisplays();
jdufault62b8b6e2016-08-24 20:34:5346 }
47
48 protected:
49 PartialMagnificationController* GetController() const {
skycb4be5b2017-04-06 17:52:4550 return Shell::Get()->partial_magnification_controller();
jdufault62b8b6e2016-08-24 20:34:5351 }
52
53 PartialMagnificationControllerTestApi GetTestApi() const {
54 return PartialMagnificationControllerTestApi(
skycb4be5b2017-04-06 17:52:4555 Shell::Get()->partial_magnification_controller());
jdufault62b8b6e2016-08-24 20:34:5356 }
57
58 private:
59 DISALLOW_COPY_AND_ASSIGN(PartialMagnificationControllerTest);
60};
61
62// The magnifier should not show up immediately after being enabled.
63TEST_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.
70TEST_F(PartialMagnificationControllerTest, ActiveOnPointerDown) {
71 GetEventGenerator().EnterPenPointerMode();
72
73 // While disabled no magnifier shows up.
jdufault1df36f32016-12-05 20:47:0274 GetEventGenerator().PressTouch();
jdufault62b8b6e2016-08-24 20:34:5375 EXPECT_FALSE(GetTestApi().is_active());
76 EXPECT_FALSE(GetTestApi().host_widget());
jdufault1df36f32016-12-05 20:47:0277 GetEventGenerator().ReleaseTouch();
jdufault62b8b6e2016-08-24 20:34:5378
79 // While enabled the magnifier is only active while the pointer is down.
80 GetController()->SetEnabled(true);
jdufault1df36f32016-12-05 20:47:0281 GetEventGenerator().PressTouch();
jdufault62b8b6e2016-08-24 20:34:5382 EXPECT_TRUE(GetTestApi().is_active());
83 EXPECT_TRUE(GetTestApi().host_widget());
jdufault1df36f32016-12-05 20:47:0284 GetEventGenerator().ReleaseTouch();
jdufault62b8b6e2016-08-24 20:34:5385 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.
91TEST_F(PartialMagnificationControllerTest, MultipleDisplays) {
jdufault62b8b6e2016-08-24 20:34:5392 GetEventGenerator().EnterPenPointerMode();
93
94 // Active magnifier with two displays, move it to the second display.
95 UpdateDisplay("800x600,800x600");
96 GetController()->SetEnabled(true);
jdufault1df36f32016-12-05 20:47:0297 GetEventGenerator().PressTouch();
98 GetEventGenerator().MoveTouch(gfx::Point(1200, 300));
jdufault62b8b6e2016-08-24 20:34:5399 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.
110TEST_F(PartialMagnificationControllerTest, DisablingDisablesActive) {
111 GetEventGenerator().EnterPenPointerMode();
112
113 GetController()->SetEnabled(true);
jdufault1df36f32016-12-05 20:47:02114 GetEventGenerator().PressTouch();
jdufault62b8b6e2016-08-24 20:34:53115 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.
123TEST_F(PartialMagnificationControllerTest, ActivatesOnlyForPointer) {
124 GetController()->SetEnabled(true);
jdufault62b8b6e2016-08-24 20:34:53125 GetEventGenerator().PressTouch();
126 EXPECT_FALSE(GetTestApi().is_active());
127}
128
129// The magnifier is always located at pointer.
130TEST_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.
jdufault1df36f32016-12-05 20:47:02136 GetEventGenerator().PressTouch();
jdufault62b8b6e2016-08-24 20:34:53137 gfx::Vector2d offset(GetTestApi().GetWidgetOrigin().x(),
138 GetTestApi().GetWidgetOrigin().y());
139
140 // Move the pointer around, make sure the window follows it.
jdufault1df36f32016-12-05 20:47:02141 GetEventGenerator().MoveTouch(gfx::Point(32, 32));
jdufault62b8b6e2016-08-24 20:34:53142 EXPECT_EQ(GetEventGenerator().current_location() + offset,
143 GetTestApi().GetWidgetOrigin());
144
jdufault1df36f32016-12-05 20:47:02145 GetEventGenerator().MoveTouch(gfx::Point(0, 10));
jdufault62b8b6e2016-08-24 20:34:53146 EXPECT_EQ(GetEventGenerator().current_location() + offset,
147 GetTestApi().GetWidgetOrigin());
148
jdufault1df36f32016-12-05 20:47:02149 GetEventGenerator().MoveTouch(gfx::Point(10, 0));
jdufault62b8b6e2016-08-24 20:34:53150 EXPECT_EQ(GetEventGenerator().current_location() + offset,
151 GetTestApi().GetWidgetOrigin());
152
jdufault1df36f32016-12-05 20:47:02153 GetEventGenerator().ReleaseTouch();
jdufault62b8b6e2016-08-24 20:34:53154
155 // Make sure the window is initially placed correctly.
156 GetEventGenerator().set_current_location(gfx::Point(50, 20));
157 EXPECT_FALSE(GetTestApi().is_active());
jdufault1df36f32016-12-05 20:47:02158 GetEventGenerator().PressTouch();
jdufault62b8b6e2016-08-24 20:34:53159 EXPECT_EQ(GetEventGenerator().current_location() + offset,
160 GetTestApi().GetWidgetOrigin());
161}
162
163} // namespace ash