blob: 8f5e7c2cbd28092c5f541460ca0884aae7d1e8bb [file] [log] [blame]
[email protected]7e368732013-01-16 13:31:231// Copyright (c) 2013 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
[email protected]7e368732013-01-16 13:31:235#include "ash/magnifier/magnification_controller.h"
[email protected]3dff2ef2014-02-09 22:50:396
jamescook27417442016-06-20 21:37:057#include "ash/common/accessibility_types.h"
[email protected]7e368732013-01-16 13:31:238#include "ash/shell.h"
9#include "ash/test/ash_test_base.h"
[email protected]0836da02013-06-10 19:33:3510#include "base/strings/stringprintf.h"
[email protected]6f162492013-04-26 09:02:0911#include "ui/aura/env.h"
[email protected]dc645d52014-04-28 14:42:5512#include "ui/aura/test/aura_test_utils.h"
[email protected]7a60cd3a2014-03-20 20:54:5713#include "ui/aura/window_tree_host.h"
jennyz91b6ed0c2014-10-23 21:52:4114#include "ui/base/ime/input_method.h"
rjkroege72f8154f2016-10-29 00:49:0215#include "ui/display/manager/display_manager.h"
oshimaf84b0da722016-04-27 19:47:1916#include "ui/display/screen.h"
[email protected]73c9fd02014-07-28 01:48:5217#include "ui/events/test/event_generator.h"
Avi Drissmanfefc2f82014-12-22 19:25:2918#include "ui/gfx/geometry/rect_conversions.h"
jennyz91b6ed0c2014-10-23 21:52:4119#include "ui/views/controls/textfield/textfield.h"
20#include "ui/views/layout/fill_layout.h"
21#include "ui/views/widget/widget.h"
22#include "ui/views/widget/widget_delegate.h"
23#include "ui/wm/core/coordinate_conversion.h"
[email protected]7e368732013-01-16 13:31:2324
25namespace ash {
[email protected]7e368732013-01-16 13:31:2326namespace {
27
28const int kRootHeight = 600;
29const int kRootWidth = 800;
30
jennyz91b6ed0c2014-10-23 21:52:4131const int kTextInputWindowWidth = 50;
32const int kTextInputWindowHeight = 50;
33
34class TextInputView : public views::WidgetDelegateView {
35 public:
36 TextInputView() : text_field_(new views::Textfield) {
37 text_field_->SetTextInputType(ui::TEXT_INPUT_TYPE_TEXT);
38 AddChildView(text_field_);
39 SetLayoutManager(new views::FillLayout);
40 }
41
dcheng222b9c72015-01-16 00:48:0142 ~TextInputView() override {}
jennyz91b6ed0c2014-10-23 21:52:4143
dcheng222b9c72015-01-16 00:48:0144 gfx::Size GetPreferredSize() const override {
jennyz91b6ed0c2014-10-23 21:52:4145 return gfx::Size(kTextInputWindowWidth, kTextInputWindowHeight);
46 }
47
48 // Overridden from views::WidgetDelegate:
jennyz91b6ed0c2014-10-23 21:52:4149 void FocusOnTextInput() { GetFocusManager()->SetFocusedView(text_field_); }
50
51 private:
52 views::Textfield* text_field_; // owned by views hierarchy
53
54 DISALLOW_COPY_AND_ASSIGN(TextInputView);
55};
56
[email protected]7e368732013-01-16 13:31:2357} // namespace
58
jamescookb8dcef522016-06-25 14:42:5559class MagnificationControllerTest : public test::AshTestBase {
[email protected]7e368732013-01-16 13:31:2360 public:
jennyz91b6ed0c2014-10-23 21:52:4161 MagnificationControllerTest() : text_input_view_(NULL) {}
dcheng222b9c72015-01-16 00:48:0162 ~MagnificationControllerTest() override {}
[email protected]7e368732013-01-16 13:31:2363
dcheng222b9c72015-01-16 00:48:0164 void SetUp() override {
[email protected]7e368732013-01-16 13:31:2365 AshTestBase::SetUp();
[email protected]7d3cbc92013-03-18 22:33:0466 UpdateDisplay(base::StringPrintf("%dx%d", kRootWidth, kRootHeight));
[email protected]7e368732013-01-16 13:31:2367
jennyzb894b6d2015-06-01 20:12:1868 GetMagnificationController()->DisableMoveMagnifierDelayForTesting();
[email protected]7e368732013-01-16 13:31:2369 }
70
dcheng222b9c72015-01-16 00:48:0171 void TearDown() override { AshTestBase::TearDown(); }
[email protected]7e368732013-01-16 13:31:2372
73 protected:
jamescookb8dcef522016-06-25 14:42:5574 aura::Window* GetRootWindow() const { return Shell::GetPrimaryRootWindow(); }
[email protected]7e368732013-01-16 13:31:2375
[email protected]6f162492013-04-26 09:02:0976 std::string GetHostMouseLocation() {
[email protected]dc645d52014-04-28 14:42:5577 const gfx::Point& location =
78 aura::test::QueryLatestMousePositionRequestInHost(
79 GetRootWindow()->GetHost());
80 return location.ToString();
[email protected]6f162492013-04-26 09:02:0981 }
82
83 ash::MagnificationController* GetMagnificationController() const {
[email protected]7e368732013-01-16 13:31:2384 return ash::Shell::GetInstance()->magnification_controller();
85 }
86
[email protected]6f162492013-04-26 09:02:0987 gfx::Rect GetViewport() const {
[email protected]7e368732013-01-16 13:31:2388 gfx::RectF bounds(0, 0, kRootWidth, kRootHeight);
89 GetRootWindow()->layer()->transform().TransformRectReverse(&bounds);
90 return gfx::ToEnclosingRect(bounds);
91 }
92
[email protected]6f162492013-04-26 09:02:0993 std::string CurrentPointOfInterest() const {
jamescookb8dcef522016-06-25 14:42:5594 return GetMagnificationController()
95 ->GetPointOfInterestForTesting()
96 .ToString();
[email protected]6f162492013-04-26 09:02:0997 }
98
jennyz91b6ed0c2014-10-23 21:52:4199 void CreateAndShowTextInputView(const gfx::Rect& bounds) {
100 text_input_view_ = new TextInputView;
101 views::Widget* widget = views::Widget::CreateWindowWithContextAndBounds(
102 text_input_view_, GetRootWindow(), bounds);
103 widget->Show();
104 }
105
106 // Returns the text input view's bounds in root window coordinates.
107 gfx::Rect GetTextInputViewBounds() {
108 DCHECK(text_input_view_);
109 gfx::Rect bounds = text_input_view_->bounds();
110 gfx::Point origin = bounds.origin();
111 // Convert origin to screen coordinates.
112 views::View::ConvertPointToScreen(text_input_view_, &origin);
113 // Convert origin to root_window_ coordinates.
sky984c1892016-04-20 00:00:34114 ::wm::ConvertPointFromScreen(GetRootWindow(), &origin);
jennyz91b6ed0c2014-10-23 21:52:41115 return gfx::Rect(origin.x(), origin.y(), bounds.width(), bounds.height());
116 }
117
118 // Returns the caret bounds in root window coordinates.
119 gfx::Rect GetCaretBounds() {
120 gfx::Rect caret_bounds =
121 GetInputMethod()->GetTextInputClient()->GetCaretBounds();
122 gfx::Point origin = caret_bounds.origin();
sky984c1892016-04-20 00:00:34123 ::wm::ConvertPointFromScreen(GetRootWindow(), &origin);
jamescookb8dcef522016-06-25 14:42:55124 return gfx::Rect(origin.x(), origin.y(), caret_bounds.width(),
125 caret_bounds.height());
jennyz91b6ed0c2014-10-23 21:52:41126 }
127
128 void FocusOnTextInputView() {
129 DCHECK(text_input_view_);
130 text_input_view_->FocusOnTextInput();
131 }
132
[email protected]7e368732013-01-16 13:31:23133 private:
jennyz91b6ed0c2014-10-23 21:52:41134 TextInputView* text_input_view_;
135
136 ui::InputMethod* GetInputMethod() {
137 DCHECK(text_input_view_);
shuchen12f77cb2015-07-02 02:11:14138 return text_input_view_->GetWidget()->GetInputMethod();
jennyz91b6ed0c2014-10-23 21:52:41139 }
140
[email protected]7e368732013-01-16 13:31:23141 DISALLOW_COPY_AND_ASSIGN(MagnificationControllerTest);
142};
143
144TEST_F(MagnificationControllerTest, EnableAndDisable) {
145 // Confirms the magnifier is disabled.
146 EXPECT_TRUE(GetRootWindow()->layer()->transform().IsIdentity());
147 EXPECT_EQ(1.0f, GetMagnificationController()->GetScale());
148 EXPECT_EQ("0,0 800x600", GetViewport().ToString());
149
150 // Enables magnifier.
151 GetMagnificationController()->SetEnabled(true);
152 EXPECT_FALSE(GetRootWindow()->layer()->transform().IsIdentity());
153 EXPECT_EQ(2.0f, GetMagnificationController()->GetScale());
154 EXPECT_EQ("200,150 400x300", GetViewport().ToString());
155
156 // Disables magnifier.
157 GetMagnificationController()->SetEnabled(false);
158 EXPECT_TRUE(GetRootWindow()->layer()->transform().IsIdentity());
159 EXPECT_EQ(1.0f, GetMagnificationController()->GetScale());
160 EXPECT_EQ("0,0 800x600", GetViewport().ToString());
161
162 // Confirms the the scale can't be changed.
163 GetMagnificationController()->SetScale(4.0f, false);
164 EXPECT_TRUE(GetRootWindow()->layer()->transform().IsIdentity());
165 EXPECT_EQ(1.0f, GetMagnificationController()->GetScale());
166 EXPECT_EQ("0,0 800x600", GetViewport().ToString());
167}
168
169TEST_F(MagnificationControllerTest, MagnifyAndUnmagnify) {
170 // Enables magnifier and confirms the default scale is 2.0x.
171 GetMagnificationController()->SetEnabled(true);
172 EXPECT_FALSE(GetRootWindow()->layer()->transform().IsIdentity());
173 EXPECT_EQ(2.0f, GetMagnificationController()->GetScale());
174 EXPECT_EQ("200,150 400x300", GetViewport().ToString());
[email protected]6f162492013-04-26 09:02:09175 EXPECT_EQ("400,300", CurrentPointOfInterest());
[email protected]7e368732013-01-16 13:31:23176
177 // Changes the scale.
178 GetMagnificationController()->SetScale(4.0f, false);
179 EXPECT_EQ(4.0f, GetMagnificationController()->GetScale());
180 EXPECT_EQ("300,225 200x150", GetViewport().ToString());
[email protected]6f162492013-04-26 09:02:09181 EXPECT_EQ("400,300", CurrentPointOfInterest());
[email protected]7e368732013-01-16 13:31:23182
183 GetMagnificationController()->SetScale(1.0f, false);
184 EXPECT_EQ(1.0f, GetMagnificationController()->GetScale());
185 EXPECT_EQ("0,0 800x600", GetViewport().ToString());
[email protected]6f162492013-04-26 09:02:09186 EXPECT_EQ("400,300", CurrentPointOfInterest());
[email protected]7e368732013-01-16 13:31:23187
188 GetMagnificationController()->SetScale(3.0f, false);
189 EXPECT_EQ(3.0f, GetMagnificationController()->GetScale());
[email protected]0ec58342013-04-24 07:39:36190 EXPECT_EQ("266,200 267x200", GetViewport().ToString());
[email protected]6f162492013-04-26 09:02:09191 EXPECT_EQ("400,300", CurrentPointOfInterest());
[email protected]7e368732013-01-16 13:31:23192}
193
194TEST_F(MagnificationControllerTest, MoveWindow) {
195 // Enables magnifier and confirm the viewport is at center.
196 GetMagnificationController()->SetEnabled(true);
197 EXPECT_EQ(2.0f, GetMagnificationController()->GetScale());
198 EXPECT_EQ("200,150 400x300", GetViewport().ToString());
199
200 // Move the viewport.
201 GetMagnificationController()->MoveWindow(0, 0, false);
202 EXPECT_EQ("0,0 400x300", GetViewport().ToString());
203
204 GetMagnificationController()->MoveWindow(200, 300, false);
205 EXPECT_EQ("200,300 400x300", GetViewport().ToString());
206
207 GetMagnificationController()->MoveWindow(400, 0, false);
208 EXPECT_EQ("400,0 400x300", GetViewport().ToString());
209
210 GetMagnificationController()->MoveWindow(400, 300, false);
211 EXPECT_EQ("400,300 400x300", GetViewport().ToString());
212
213 // Confirms that the viewport can't across the top-left border.
214 GetMagnificationController()->MoveWindow(-100, 0, false);
215 EXPECT_EQ("0,0 400x300", GetViewport().ToString());
216
217 GetMagnificationController()->MoveWindow(0, -100, false);
218 EXPECT_EQ("0,0 400x300", GetViewport().ToString());
219
220 GetMagnificationController()->MoveWindow(-100, -100, false);
221 EXPECT_EQ("0,0 400x300", GetViewport().ToString());
222
223 // Confirms that the viewport can't across the bittom-right border.
224 GetMagnificationController()->MoveWindow(800, 0, false);
225 EXPECT_EQ("400,0 400x300", GetViewport().ToString());
226
227 GetMagnificationController()->MoveWindow(0, 400, false);
228 EXPECT_EQ("0,300 400x300", GetViewport().ToString());
229
230 GetMagnificationController()->MoveWindow(200, 400, false);
231 EXPECT_EQ("200,300 400x300", GetViewport().ToString());
232
233 GetMagnificationController()->MoveWindow(1000, 1000, false);
234 EXPECT_EQ("400,300 400x300", GetViewport().ToString());
235}
236
[email protected]6f162492013-04-26 09:02:09237TEST_F(MagnificationControllerTest, PointOfInterest) {
kinaba5d46c352016-03-31 01:04:46238 GetEventGenerator().MoveMouseToInHost(gfx::Point(0, 0));
[email protected]6f162492013-04-26 09:02:09239 EXPECT_EQ("0,0", CurrentPointOfInterest());
240
kinaba5d46c352016-03-31 01:04:46241 GetEventGenerator().MoveMouseToInHost(gfx::Point(799, 599));
[email protected]6f162492013-04-26 09:02:09242 EXPECT_EQ("799,599", CurrentPointOfInterest());
243
kinaba5d46c352016-03-31 01:04:46244 GetEventGenerator().MoveMouseToInHost(gfx::Point(400, 300));
[email protected]6f162492013-04-26 09:02:09245 EXPECT_EQ("400,300", CurrentPointOfInterest());
246
247 GetMagnificationController()->SetEnabled(true);
248 EXPECT_EQ("400,300", CurrentPointOfInterest());
249
kinaba5d46c352016-03-31 01:04:46250 GetEventGenerator().MoveMouseToInHost(gfx::Point(500, 400));
[email protected]6f162492013-04-26 09:02:09251 EXPECT_EQ("450,350", CurrentPointOfInterest());
252}
253
glevin0043bf42016-01-08 22:05:13254TEST_F(MagnificationControllerTest, FollowFocusChanged) {
255 // Enables magnifier and confirm the viewport is at center.
256 GetMagnificationController()->SetEnabled(true);
257 EXPECT_EQ(2.0f, GetMagnificationController()->GetScale());
258 EXPECT_EQ("200,150 400x300", GetViewport().ToString());
259
260 // Don't move viewport when focusing edit box.
261 GetMagnificationController()->HandleFocusedNodeChanged(
262 true, gfx::Rect(0, 0, 10, 10));
263 EXPECT_EQ("200,150 400x300", GetViewport().ToString());
264
265 // Move viewport to element in upper left.
266 GetMagnificationController()->HandleFocusedNodeChanged(
267 false, gfx::Rect(0, 0, 10, 10));
268 EXPECT_EQ("0,0 400x300", GetViewport().ToString());
269
270 // Move viewport to element in lower right.
271 GetMagnificationController()->HandleFocusedNodeChanged(
272 false, gfx::Rect(790, 590, 10, 10));
273 EXPECT_EQ("400,300 400x300", GetViewport().ToString());
274
275 // Don't follow focus onto empty rectangle.
jamescookb8dcef522016-06-25 14:42:55276 GetMagnificationController()->HandleFocusedNodeChanged(false,
277 gfx::Rect(0, 0, 0, 0));
glevin0043bf42016-01-08 22:05:13278 EXPECT_EQ("400,300 400x300", GetViewport().ToString());
279}
280
[email protected]6f162492013-04-26 09:02:09281TEST_F(MagnificationControllerTest, PanWindow2xLeftToRight) {
282 const aura::Env* env = aura::Env::GetInstance();
283
kinaba5d46c352016-03-31 01:04:46284 GetEventGenerator().MoveMouseToInHost(gfx::Point(0, 0));
[email protected]6f162492013-04-26 09:02:09285 EXPECT_EQ(1.f, GetMagnificationController()->GetScale());
286 EXPECT_EQ("0,0 800x600", GetViewport().ToString());
287 EXPECT_EQ("0,0", env->last_mouse_location().ToString());
288
289 // Enables magnifier and confirm the viewport is at center.
290 GetMagnificationController()->SetEnabled(true);
291 EXPECT_EQ(2.0f, GetMagnificationController()->GetScale());
292
293 GetMagnificationController()->MoveWindow(0, 0, false);
kinaba5d46c352016-03-31 01:04:46294 GetEventGenerator().MoveMouseToInHost(gfx::Point(0, 0));
[email protected]6f162492013-04-26 09:02:09295 EXPECT_EQ("0,0", env->last_mouse_location().ToString());
296 EXPECT_EQ("0,0 400x300", GetViewport().ToString());
297
kinaba5d46c352016-03-31 01:04:46298 GetEventGenerator().MoveMouseToInHost(gfx::Point(300, 150));
[email protected]6f162492013-04-26 09:02:09299 EXPECT_EQ("150,75", env->last_mouse_location().ToString());
300 EXPECT_EQ("0,0 400x300", GetViewport().ToString());
301
kinaba5d46c352016-03-31 01:04:46302 GetEventGenerator().MoveMouseToInHost(gfx::Point(700, 150));
[email protected]6f162492013-04-26 09:02:09303 EXPECT_EQ("350,75", env->last_mouse_location().ToString());
304 EXPECT_EQ("0,0 400x300", GetViewport().ToString());
305
kinaba5d46c352016-03-31 01:04:46306 GetEventGenerator().MoveMouseToInHost(gfx::Point(701, 150));
[email protected]6f162492013-04-26 09:02:09307 EXPECT_EQ("350,75", env->last_mouse_location().ToString());
308 EXPECT_EQ("0,0 400x300", GetViewport().ToString());
309
kinaba5d46c352016-03-31 01:04:46310 GetEventGenerator().MoveMouseToInHost(gfx::Point(702, 150));
[email protected]6f162492013-04-26 09:02:09311 EXPECT_EQ("351,75", env->last_mouse_location().ToString());
312 EXPECT_EQ("1,0 400x300", GetViewport().ToString());
313
kinaba5d46c352016-03-31 01:04:46314 GetEventGenerator().MoveMouseToInHost(gfx::Point(703, 150));
[email protected]6f162492013-04-26 09:02:09315 EXPECT_EQ("352,75", env->last_mouse_location().ToString());
316 EXPECT_EQ("2,0 400x300", GetViewport().ToString());
317
kinaba5d46c352016-03-31 01:04:46318 GetEventGenerator().MoveMouseToInHost(gfx::Point(704, 150));
[email protected]6f162492013-04-26 09:02:09319 EXPECT_EQ("354,75", env->last_mouse_location().ToString());
320 EXPECT_EQ("4,0 400x300", GetViewport().ToString());
321
kinaba5d46c352016-03-31 01:04:46322 GetEventGenerator().MoveMouseToInHost(gfx::Point(712, 150));
[email protected]6f162492013-04-26 09:02:09323 EXPECT_EQ("360,75", env->last_mouse_location().ToString());
324 EXPECT_EQ("10,0 400x300", GetViewport().ToString());
325
kinaba5d46c352016-03-31 01:04:46326 GetEventGenerator().MoveMouseToInHost(gfx::Point(600, 150));
[email protected]6f162492013-04-26 09:02:09327 EXPECT_EQ("310,75", env->last_mouse_location().ToString());
328 EXPECT_EQ("10,0 400x300", GetViewport().ToString());
329
kinaba5d46c352016-03-31 01:04:46330 GetEventGenerator().MoveMouseToInHost(gfx::Point(720, 150));
[email protected]6f162492013-04-26 09:02:09331 EXPECT_EQ("370,75", env->last_mouse_location().ToString());
332 EXPECT_EQ("20,0 400x300", GetViewport().ToString());
333
kinaba5d46c352016-03-31 01:04:46334 GetEventGenerator().MoveMouseToInHost(gfx::Point(780, 150));
[email protected]6f162492013-04-26 09:02:09335 EXPECT_EQ("410,75", env->last_mouse_location().ToString());
336 EXPECT_EQ("410,75", CurrentPointOfInterest());
337 EXPECT_EQ("60,0 400x300", GetViewport().ToString());
338
kinaba5d46c352016-03-31 01:04:46339 GetEventGenerator().MoveMouseToInHost(gfx::Point(799, 150));
[email protected]6f162492013-04-26 09:02:09340 EXPECT_EQ("459,75", env->last_mouse_location().ToString());
341 EXPECT_EQ("109,0 400x300", GetViewport().ToString());
342
kinaba5d46c352016-03-31 01:04:46343 GetEventGenerator().MoveMouseToInHost(gfx::Point(702, 150));
[email protected]6f162492013-04-26 09:02:09344 EXPECT_EQ("460,75", env->last_mouse_location().ToString());
345 EXPECT_EQ("110,0 400x300", GetViewport().ToString());
346
kinaba5d46c352016-03-31 01:04:46347 GetEventGenerator().MoveMouseToInHost(gfx::Point(780, 150));
[email protected]6f162492013-04-26 09:02:09348 EXPECT_EQ("500,75", env->last_mouse_location().ToString());
349 EXPECT_EQ("150,0 400x300", GetViewport().ToString());
350
kinaba5d46c352016-03-31 01:04:46351 GetEventGenerator().MoveMouseToInHost(gfx::Point(780, 150));
[email protected]6f162492013-04-26 09:02:09352 EXPECT_EQ("540,75", env->last_mouse_location().ToString());
353 EXPECT_EQ("190,0 400x300", GetViewport().ToString());
354
kinaba5d46c352016-03-31 01:04:46355 GetEventGenerator().MoveMouseToInHost(gfx::Point(780, 150));
[email protected]6f162492013-04-26 09:02:09356 EXPECT_EQ("580,75", env->last_mouse_location().ToString());
357 EXPECT_EQ("230,0 400x300", GetViewport().ToString());
358
kinaba5d46c352016-03-31 01:04:46359 GetEventGenerator().MoveMouseToInHost(gfx::Point(780, 150));
[email protected]6f162492013-04-26 09:02:09360 EXPECT_EQ("620,75", env->last_mouse_location().ToString());
361 EXPECT_EQ("270,0 400x300", GetViewport().ToString());
362
kinaba5d46c352016-03-31 01:04:46363 GetEventGenerator().MoveMouseToInHost(gfx::Point(780, 150));
[email protected]6f162492013-04-26 09:02:09364 EXPECT_EQ("660,75", env->last_mouse_location().ToString());
365 EXPECT_EQ("310,0 400x300", GetViewport().ToString());
366
kinaba5d46c352016-03-31 01:04:46367 GetEventGenerator().MoveMouseToInHost(gfx::Point(780, 150));
[email protected]6f162492013-04-26 09:02:09368 EXPECT_EQ("700,75", env->last_mouse_location().ToString());
369 EXPECT_EQ("350,0 400x300", GetViewport().ToString());
370
kinaba5d46c352016-03-31 01:04:46371 GetEventGenerator().MoveMouseToInHost(gfx::Point(780, 150));
[email protected]6f162492013-04-26 09:02:09372 EXPECT_EQ("740,75", env->last_mouse_location().ToString());
373 EXPECT_EQ("390,0 400x300", GetViewport().ToString());
374
kinaba5d46c352016-03-31 01:04:46375 GetEventGenerator().MoveMouseToInHost(gfx::Point(780, 150));
[email protected]6f162492013-04-26 09:02:09376 EXPECT_EQ("780,75", env->last_mouse_location().ToString());
377 EXPECT_EQ("400,0 400x300", GetViewport().ToString());
378
kinaba5d46c352016-03-31 01:04:46379 GetEventGenerator().MoveMouseToInHost(gfx::Point(799, 150));
[email protected]6f162492013-04-26 09:02:09380 EXPECT_EQ("799,75", env->last_mouse_location().ToString());
381 EXPECT_EQ("400,0 400x300", GetViewport().ToString());
382}
383
384TEST_F(MagnificationControllerTest, PanWindow2xRightToLeft) {
385 const aura::Env* env = aura::Env::GetInstance();
386
kinaba5d46c352016-03-31 01:04:46387 GetEventGenerator().MoveMouseToInHost(gfx::Point(799, 300));
[email protected]6f162492013-04-26 09:02:09388 EXPECT_EQ(1.f, GetMagnificationController()->GetScale());
389 EXPECT_EQ("0,0 800x600", GetViewport().ToString());
390 EXPECT_EQ("799,300", env->last_mouse_location().ToString());
391
392 // Enables magnifier and confirm the viewport is at center.
393 GetMagnificationController()->SetEnabled(true);
394
kinaba5d46c352016-03-31 01:04:46395 GetEventGenerator().MoveMouseToInHost(gfx::Point(799, 300));
[email protected]6f162492013-04-26 09:02:09396 EXPECT_EQ("798,300", env->last_mouse_location().ToString());
397 EXPECT_EQ("400,150 400x300", GetViewport().ToString());
398
kinaba5d46c352016-03-31 01:04:46399 GetEventGenerator().MoveMouseToInHost(gfx::Point(0, 300));
[email protected]6f162492013-04-26 09:02:09400 EXPECT_EQ("400,300", env->last_mouse_location().ToString());
401 EXPECT_EQ("350,150 400x300", GetViewport().ToString());
402
kinaba5d46c352016-03-31 01:04:46403 GetEventGenerator().MoveMouseToInHost(gfx::Point(0, 300));
[email protected]6f162492013-04-26 09:02:09404 EXPECT_EQ("350,300", env->last_mouse_location().ToString());
405 EXPECT_EQ("300,150 400x300", GetViewport().ToString());
406
kinaba5d46c352016-03-31 01:04:46407 GetEventGenerator().MoveMouseToInHost(gfx::Point(0, 300));
[email protected]6f162492013-04-26 09:02:09408 EXPECT_EQ("300,300", env->last_mouse_location().ToString());
409 EXPECT_EQ("250,150 400x300", GetViewport().ToString());
410
kinaba5d46c352016-03-31 01:04:46411 GetEventGenerator().MoveMouseToInHost(gfx::Point(0, 300));
[email protected]6f162492013-04-26 09:02:09412 EXPECT_EQ("250,300", env->last_mouse_location().ToString());
413 EXPECT_EQ("200,150 400x300", GetViewport().ToString());
414
kinaba5d46c352016-03-31 01:04:46415 GetEventGenerator().MoveMouseToInHost(gfx::Point(0, 300));
[email protected]6f162492013-04-26 09:02:09416 EXPECT_EQ("200,300", env->last_mouse_location().ToString());
417 EXPECT_EQ("150,150 400x300", GetViewport().ToString());
418
kinaba5d46c352016-03-31 01:04:46419 GetEventGenerator().MoveMouseToInHost(gfx::Point(0, 300));
[email protected]6f162492013-04-26 09:02:09420 EXPECT_EQ("150,300", env->last_mouse_location().ToString());
421 EXPECT_EQ("100,150 400x300", GetViewport().ToString());
422
kinaba5d46c352016-03-31 01:04:46423 GetEventGenerator().MoveMouseToInHost(gfx::Point(0, 300));
[email protected]6f162492013-04-26 09:02:09424 EXPECT_EQ("100,300", env->last_mouse_location().ToString());
425 EXPECT_EQ("50,150 400x300", GetViewport().ToString());
426
kinaba5d46c352016-03-31 01:04:46427 GetEventGenerator().MoveMouseToInHost(gfx::Point(0, 300));
[email protected]6f162492013-04-26 09:02:09428 EXPECT_EQ("50,300", env->last_mouse_location().ToString());
429 EXPECT_EQ("0,150 400x300", GetViewport().ToString());
430
kinaba5d46c352016-03-31 01:04:46431 GetEventGenerator().MoveMouseToInHost(gfx::Point(0, 300));
[email protected]6f162492013-04-26 09:02:09432 EXPECT_EQ("0,300", env->last_mouse_location().ToString());
433 EXPECT_EQ("0,150 400x300", GetViewport().ToString());
434}
435
436TEST_F(MagnificationControllerTest, PanWindowToRight) {
437 const aura::Env* env = aura::Env::GetInstance();
438
kinaba5d46c352016-03-31 01:04:46439 GetEventGenerator().MoveMouseToInHost(gfx::Point(400, 300));
[email protected]6f162492013-04-26 09:02:09440 EXPECT_EQ(1.f, GetMagnificationController()->GetScale());
441 EXPECT_EQ("0,0 800x600", GetViewport().ToString());
442 EXPECT_EQ("400,300", env->last_mouse_location().ToString());
443
444 float scale = 2.f;
445
446 // Enables magnifier and confirm the viewport is at center.
447 GetMagnificationController()->SetEnabled(true);
448 EXPECT_FLOAT_EQ(2.f, GetMagnificationController()->GetScale());
449
jamescooka7f8dab2016-06-21 01:44:03450 scale *= kMagnificationScaleFactor;
[email protected]6f162492013-04-26 09:02:09451 GetMagnificationController()->SetScale(scale, false);
452 EXPECT_FLOAT_EQ(2.3784142, GetMagnificationController()->GetScale());
kinaba5d46c352016-03-31 01:04:46453 GetEventGenerator().MoveMouseToInHost(gfx::Point(400, 300));
[email protected]6f162492013-04-26 09:02:09454 EXPECT_EQ("400,300", env->last_mouse_location().ToString());
kinaba5d46c352016-03-31 01:04:46455 GetEventGenerator().MoveMouseToInHost(gfx::Point(799, 300));
[email protected]6f162492013-04-26 09:02:09456 EXPECT_EQ("566,299", env->last_mouse_location().ToString());
457 EXPECT_EQ("705,300", GetHostMouseLocation());
458
jamescooka7f8dab2016-06-21 01:44:03459 scale *= kMagnificationScaleFactor;
[email protected]6f162492013-04-26 09:02:09460 GetMagnificationController()->SetScale(scale, false);
461 EXPECT_FLOAT_EQ(2.8284268, GetMagnificationController()->GetScale());
kinaba5d46c352016-03-31 01:04:46462 GetEventGenerator().MoveMouseToInHost(gfx::Point(799, 300));
[email protected]6f162492013-04-26 09:02:09463 EXPECT_EQ("599,299", env->last_mouse_location().ToString());
464 EXPECT_EQ("702,300", GetHostMouseLocation());
465
jamescooka7f8dab2016-06-21 01:44:03466 scale *= kMagnificationScaleFactor;
[email protected]6f162492013-04-26 09:02:09467 GetMagnificationController()->SetScale(scale, false);
468 EXPECT_FLOAT_EQ(3.3635852, GetMagnificationController()->GetScale());
kinaba5d46c352016-03-31 01:04:46469 GetEventGenerator().MoveMouseToInHost(gfx::Point(799, 300));
[email protected]6f162492013-04-26 09:02:09470 EXPECT_EQ("627,298", env->last_mouse_location().ToString());
471 EXPECT_EQ("707,300", GetHostMouseLocation());
472
jamescooka7f8dab2016-06-21 01:44:03473 scale *= kMagnificationScaleFactor;
[email protected]6f162492013-04-26 09:02:09474 GetMagnificationController()->SetScale(scale, false);
475 EXPECT_FLOAT_EQ(4.f, GetMagnificationController()->GetScale());
kinaba5d46c352016-03-31 01:04:46476 GetEventGenerator().MoveMouseToInHost(gfx::Point(799, 300));
[email protected]6f162492013-04-26 09:02:09477 EXPECT_EQ("649,298", env->last_mouse_location().ToString());
478 EXPECT_EQ("704,300", GetHostMouseLocation());
479}
480
481TEST_F(MagnificationControllerTest, PanWindowToLeft) {
482 const aura::Env* env = aura::Env::GetInstance();
483
kinaba5d46c352016-03-31 01:04:46484 GetEventGenerator().MoveMouseToInHost(gfx::Point(400, 300));
[email protected]6f162492013-04-26 09:02:09485 EXPECT_EQ(1.f, GetMagnificationController()->GetScale());
486 EXPECT_EQ("0,0 800x600", GetViewport().ToString());
487 EXPECT_EQ("400,300", env->last_mouse_location().ToString());
488
489 float scale = 2.f;
490
491 // Enables magnifier and confirm the viewport is at center.
492 GetMagnificationController()->SetEnabled(true);
493 EXPECT_FLOAT_EQ(2.f, GetMagnificationController()->GetScale());
494
jamescooka7f8dab2016-06-21 01:44:03495 scale *= kMagnificationScaleFactor;
[email protected]6f162492013-04-26 09:02:09496 GetMagnificationController()->SetScale(scale, false);
497 EXPECT_FLOAT_EQ(2.3784142, GetMagnificationController()->GetScale());
kinaba5d46c352016-03-31 01:04:46498 GetEventGenerator().MoveMouseToInHost(gfx::Point(400, 300));
[email protected]6f162492013-04-26 09:02:09499 EXPECT_EQ("400,300", env->last_mouse_location().ToString());
kinaba5d46c352016-03-31 01:04:46500 GetEventGenerator().MoveMouseToInHost(gfx::Point(0, 300));
[email protected]6f162492013-04-26 09:02:09501 EXPECT_EQ("231,299", env->last_mouse_location().ToString());
502 EXPECT_EQ("100,300", GetHostMouseLocation());
503
jamescooka7f8dab2016-06-21 01:44:03504 scale *= kMagnificationScaleFactor;
[email protected]6f162492013-04-26 09:02:09505 GetMagnificationController()->SetScale(scale, false);
506 EXPECT_FLOAT_EQ(2.8284268, GetMagnificationController()->GetScale());
kinaba5d46c352016-03-31 01:04:46507 GetEventGenerator().MoveMouseToInHost(gfx::Point(0, 300));
[email protected]a15328272013-10-02 22:52:03508 EXPECT_EQ("194,299", env->last_mouse_location().ToString());
[email protected]6f162492013-04-26 09:02:09509 EXPECT_EQ("99,300", GetHostMouseLocation());
510
jamescooka7f8dab2016-06-21 01:44:03511 scale *= kMagnificationScaleFactor;
[email protected]6f162492013-04-26 09:02:09512 GetMagnificationController()->SetScale(scale, false);
513 EXPECT_FLOAT_EQ(3.3635852, GetMagnificationController()->GetScale());
kinaba5d46c352016-03-31 01:04:46514 GetEventGenerator().MoveMouseToInHost(gfx::Point(0, 300));
[email protected]a15328272013-10-02 22:52:03515 EXPECT_EQ("164,298", env->last_mouse_location().ToString());
[email protected]6f162492013-04-26 09:02:09516 EXPECT_EQ("98,300", GetHostMouseLocation());
517
jamescooka7f8dab2016-06-21 01:44:03518 scale *= kMagnificationScaleFactor;
[email protected]6f162492013-04-26 09:02:09519 GetMagnificationController()->SetScale(scale, false);
520 EXPECT_FLOAT_EQ(4.f, GetMagnificationController()->GetScale());
kinaba5d46c352016-03-31 01:04:46521 GetEventGenerator().MoveMouseToInHost(gfx::Point(0, 300));
[email protected]a15328272013-10-02 22:52:03522 EXPECT_EQ("139,298", env->last_mouse_location().ToString());
[email protected]6f162492013-04-26 09:02:09523 EXPECT_EQ("100,300", GetHostMouseLocation());
524}
525
jennyz91b6ed0c2014-10-23 21:52:41526TEST_F(MagnificationControllerTest, FollowTextInputFieldFocus) {
527 CreateAndShowTextInputView(gfx::Rect(500, 300, 80, 80));
528 gfx::Rect text_input_bounds = GetTextInputViewBounds();
529
530 // Enables magnifier and confirm the viewport is at center.
531 GetMagnificationController()->SetEnabled(true);
532 EXPECT_EQ(2.0f, GetMagnificationController()->GetScale());
533 EXPECT_EQ("200,150 400x300", GetViewport().ToString());
jennyzb894b6d2015-06-01 20:12:18534 EXPECT_FALSE(GetMagnificationController()->KeepFocusCentered());
jennyz91b6ed0c2014-10-23 21:52:41535
536 // Move the viewport to (0, 0), so that text input field will be out of
537 // the viewport region.
538 GetMagnificationController()->MoveWindow(0, 0, false);
539 EXPECT_EQ("0,0 400x300", GetViewport().ToString());
540 EXPECT_FALSE(GetViewport().Intersects(text_input_bounds));
541
542 // Focus on the text input field.
543 FocusOnTextInputView();
544
545 // Verify the view port has been moved to the place where the text field is
546 // contained in the view port and the caret is at the center of the view port.
547 gfx::Rect view_port = GetViewport();
548 EXPECT_TRUE(view_port.Contains(text_input_bounds));
549 gfx::Rect caret_bounds = GetCaretBounds();
550 EXPECT_TRUE(text_input_bounds.Contains(caret_bounds));
jennyzb894b6d2015-06-01 20:12:18551 EXPECT_EQ(caret_bounds.CenterPoint(), view_port.CenterPoint());
jennyz91b6ed0c2014-10-23 21:52:41552}
553
jennyzb894b6d2015-06-01 20:12:18554// Tests the following case. First the text input field intersects on the right
555// edge with the view port, with focus caret sitting just a little left to the
556// caret panning margin, so that when it gets focus, the view port won't move.
557// Then when user types a character, the caret moves beyond the right panning
558// edge, the view port will be moved to center the caret horizontally.
jennyz91b6ed0c2014-10-23 21:52:41559TEST_F(MagnificationControllerTest, FollowTextInputFieldKeyPress) {
jennyzb894b6d2015-06-01 20:12:18560 const int kCaretPanningMargin = 50;
561 const int kScale = 2.0f;
562 const int kViewportWidth = 400;
563 // Add some extra distance horizontally from text caret to to left edge of
564 // the text input view.
565 int x = kViewportWidth - (kCaretPanningMargin + 20) / kScale;
566 CreateAndShowTextInputView(gfx::Rect(x, 200, 80, 80));
jennyz91b6ed0c2014-10-23 21:52:41567 gfx::Rect text_input_bounds = GetTextInputViewBounds();
568
569 // Enables magnifier and confirm the viewport is at center.
570 GetMagnificationController()->SetEnabled(true);
571 EXPECT_EQ(2.0f, GetMagnificationController()->GetScale());
572 EXPECT_EQ("200,150 400x300", GetViewport().ToString());
jennyzb894b6d2015-06-01 20:12:18573 EXPECT_FALSE(GetMagnificationController()->KeepFocusCentered());
jennyz91b6ed0c2014-10-23 21:52:41574
575 // Move the viewport to (0, 0), so that text input field intersects the
576 // view port at the right edge.
577 GetMagnificationController()->MoveWindow(0, 0, false);
578 EXPECT_EQ("0,0 400x300", GetViewport().ToString());
579 EXPECT_TRUE(GetViewport().Intersects(text_input_bounds));
580
581 // Focus on the text input field.
582 FocusOnTextInputView();
583
jennyzb894b6d2015-06-01 20:12:18584 // Verify the view port is not moved, and the caret is inside the view port
585 // and not beyond the caret right panning margin.
jennyz91b6ed0c2014-10-23 21:52:41586 gfx::Rect view_port = GetViewport();
587 EXPECT_EQ("0,0 400x300", view_port.ToString());
jennyz91b6ed0c2014-10-23 21:52:41588 EXPECT_TRUE(text_input_bounds.Contains(GetCaretBounds()));
jennyzb894b6d2015-06-01 20:12:18589 EXPECT_GT(view_port.right() - kCaretPanningMargin / kScale,
590 GetCaretBounds().x());
jennyz91b6ed0c2014-10-23 21:52:41591
592 // Press keys on text input simulate typing on text field and the caret
jennyzb894b6d2015-06-01 20:12:18593 // moves beyond the caret right panning margin. The view port is moved to the
594 // place where caret's x coordinate is centered at the new view port.
kinaba5d46c352016-03-31 01:04:46595 GetEventGenerator().PressKey(ui::VKEY_A, 0);
596 GetEventGenerator().ReleaseKey(ui::VKEY_A, 0);
jennyz91b6ed0c2014-10-23 21:52:41597 gfx::Rect caret_bounds = GetCaretBounds();
jennyzb894b6d2015-06-01 20:12:18598 EXPECT_LT(view_port.right() - kCaretPanningMargin / kScale,
599 GetCaretBounds().x());
jennyz91b6ed0c2014-10-23 21:52:41600
601 gfx::Rect new_view_port = GetViewport();
jennyzb894b6d2015-06-01 20:12:18602 EXPECT_EQ(caret_bounds.CenterPoint().x(), new_view_port.CenterPoint().x());
jennyz91b6ed0c2014-10-23 21:52:41603}
604
jennyzb894b6d2015-06-01 20:12:18605TEST_F(MagnificationControllerTest, CenterTextCaretNotInsideViewport) {
606 CreateAndShowTextInputView(gfx::Rect(500, 300, 50, 30));
607 gfx::Rect text_input_bounds = GetTextInputViewBounds();
608
609 // Enables magnifier and confirm the viewport is at center.
610 GetMagnificationController()->SetKeepFocusCentered(true);
611 GetMagnificationController()->SetEnabled(true);
612 EXPECT_EQ(2.0f, GetMagnificationController()->GetScale());
613 EXPECT_EQ("200,150 400x300", GetViewport().ToString());
614 EXPECT_TRUE(GetMagnificationController()->KeepFocusCentered());
615
616 // Move the viewport to (0, 0), so that text input field will be out of
617 // the viewport region.
618 GetMagnificationController()->MoveWindow(0, 0, false);
619 EXPECT_EQ("0,0 400x300", GetViewport().ToString());
620 EXPECT_FALSE(GetViewport().Contains(text_input_bounds));
621
622 // Focus on the text input field.
623 FocusOnTextInputView();
624 RunAllPendingInMessageLoop();
625 // Verify the view port has been moved to the place where the text field is
626 // contained in the view port and the caret is at the center of the view port.
627 gfx::Rect view_port = GetViewport();
628 EXPECT_TRUE(view_port.Contains(text_input_bounds));
629 gfx::Rect caret_bounds = GetCaretBounds();
630 EXPECT_EQ(caret_bounds.CenterPoint(), view_port.CenterPoint());
631
632 // Press keys on text input simulate typing on text field and the view port
633 // should be moved to keep the caret centered.
kinaba5d46c352016-03-31 01:04:46634 GetEventGenerator().PressKey(ui::VKEY_A, 0);
635 GetEventGenerator().ReleaseKey(ui::VKEY_A, 0);
jennyzb894b6d2015-06-01 20:12:18636 RunAllPendingInMessageLoop();
637 gfx::Rect new_caret_bounds = GetCaretBounds();
638 EXPECT_NE(caret_bounds, new_caret_bounds);
639
640 gfx::Rect new_view_port = GetViewport();
641 EXPECT_NE(view_port, new_view_port);
642 EXPECT_TRUE(new_view_port.Contains(new_caret_bounds));
643 EXPECT_EQ(new_caret_bounds.CenterPoint(), new_view_port.CenterPoint());
644}
645
646TEST_F(MagnificationControllerTest, CenterTextCaretInViewport) {
647 CreateAndShowTextInputView(gfx::Rect(250, 200, 50, 30));
648 gfx::Rect text_input_bounds = GetTextInputViewBounds();
649
650 // Enables magnifier and confirm the viewport is at center.
651 GetMagnificationController()->SetKeepFocusCentered(true);
652 GetMagnificationController()->SetEnabled(true);
653 EXPECT_EQ(2.0f, GetMagnificationController()->GetScale());
654 EXPECT_EQ("200,150 400x300", GetViewport().ToString());
655 EXPECT_TRUE(GetMagnificationController()->KeepFocusCentered());
656
657 // Verify the text input field is inside the view port.
658 gfx::Rect view_port = GetViewport();
659 EXPECT_TRUE(view_port.Contains(text_input_bounds));
660
661 // Focus on the text input field.
662 FocusOnTextInputView();
663 RunAllPendingInMessageLoop();
664
665 // Verify the view port has been moved to the place where the text field is
666 // contained in the view port and the caret is at the center of the view port.
667 gfx::Rect new_view_port = GetViewport();
668 EXPECT_NE(view_port, new_view_port);
669 EXPECT_TRUE(new_view_port.Contains(text_input_bounds));
670 gfx::Rect caret_bounds = GetCaretBounds();
671 EXPECT_EQ(caret_bounds.CenterPoint(), new_view_port.CenterPoint());
672}
673
oshima38dffad2015-05-05 17:22:56674// Make sure that unified desktop can enter magnified mode.
675TEST_F(MagnificationControllerTest, EnableMagnifierInUnifiedDesktop) {
676 if (!SupportsMultipleDisplays())
677 return;
oshima628a6172015-08-01 01:33:14678 Shell::GetInstance()->display_manager()->SetUnifiedDesktopEnabled(true);
oshima38dffad2015-05-05 17:22:56679
oshima38dffad2015-05-05 17:22:56680 EXPECT_EQ(1.0f, GetMagnificationController()->GetScale());
681
682 GetMagnificationController()->SetEnabled(true);
683
oshimaf84b0da722016-04-27 19:47:19684 display::Screen* screen = display::Screen::GetScreen();
oshima38dffad2015-05-05 17:22:56685
686 UpdateDisplay("500x500, 500x500");
687 EXPECT_EQ("0,0 1000x500", screen->GetPrimaryDisplay().bounds().ToString());
688 EXPECT_EQ(2.0f, GetMagnificationController()->GetScale());
689
690 GetMagnificationController()->SetEnabled(false);
691
692 EXPECT_EQ(1.0f, GetMagnificationController()->GetScale());
693
694 GetMagnificationController()->SetEnabled(true);
695 EXPECT_EQ(2.0f, GetMagnificationController()->GetScale());
696
697 UpdateDisplay("500x500");
698 EXPECT_EQ("0,0 500x500", screen->GetPrimaryDisplay().bounds().ToString());
699 EXPECT_EQ(2.0f, GetMagnificationController()->GetScale());
700
701 GetMagnificationController()->SetEnabled(false);
702 EXPECT_EQ("0,0 500x500", screen->GetPrimaryDisplay().bounds().ToString());
703 EXPECT_EQ(1.0f, GetMagnificationController()->GetScale());
704}
705
[email protected]7e368732013-01-16 13:31:23706} // namespace ash