1 // 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.
5 #include "ash/magnifier/magnification_controller.h"
7 #include "ash/magnifier/magnifier_constants.h"
9 #include "ash/test/ash_test_base.h"
10 #include "base/strings/stringprintf.h"
11 #include "ui/aura/client/aura_constants.h"
12 #include "ui/aura/env.h"
13 #include "ui/aura/test/aura_test_utils.h"
14 #include "ui/aura/window_tree_host.h"
15 #include "ui/events/test/event_generator.h"
16 #include "ui/gfx/rect_conversions.h"
17 #include "ui/gfx/screen.h"
22 const int kRootHeight
= 600;
23 const int kRootWidth
= 800;
27 class MagnificationControllerTest
: public test::AshTestBase
{
29 MagnificationControllerTest() {}
30 virtual ~MagnificationControllerTest() {}
32 virtual void SetUp() OVERRIDE
{
34 UpdateDisplay(base::StringPrintf("%dx%d", kRootWidth
, kRootHeight
));
36 aura::Window
* root
= GetRootWindow();
37 gfx::Rect
root_bounds(root
->bounds());
40 // RootWindow and Display can't resize on Windows Ash.
41 // http://crbug.com/165962
42 EXPECT_EQ(kRootHeight
, root_bounds
.height());
43 EXPECT_EQ(kRootWidth
, root_bounds
.width());
47 virtual void TearDown() OVERRIDE
{
48 AshTestBase::TearDown();
52 aura::Window
* GetRootWindow() const {
53 return Shell::GetPrimaryRootWindow();
56 std::string
GetHostMouseLocation() {
57 const gfx::Point
& location
=
58 aura::test::QueryLatestMousePositionRequestInHost(
59 GetRootWindow()->GetHost());
60 return location
.ToString();
63 ash::MagnificationController
* GetMagnificationController() const {
64 return ash::Shell::GetInstance()->magnification_controller();
67 gfx::Rect
GetViewport() const {
68 gfx::RectF
bounds(0, 0, kRootWidth
, kRootHeight
);
69 GetRootWindow()->layer()->transform().TransformRectReverse(&bounds
);
70 return gfx::ToEnclosingRect(bounds
);
73 std::string
CurrentPointOfInterest() const {
74 return GetMagnificationController()->
75 GetPointOfInterestForTesting().ToString();
79 DISALLOW_COPY_AND_ASSIGN(MagnificationControllerTest
);
82 TEST_F(MagnificationControllerTest
, EnableAndDisable
) {
83 // Confirms the magnifier is disabled.
84 EXPECT_TRUE(GetRootWindow()->layer()->transform().IsIdentity());
85 EXPECT_EQ(1.0f
, GetMagnificationController()->GetScale());
86 EXPECT_EQ("0,0 800x600", GetViewport().ToString());
89 GetMagnificationController()->SetEnabled(true);
90 EXPECT_FALSE(GetRootWindow()->layer()->transform().IsIdentity());
91 EXPECT_EQ(2.0f
, GetMagnificationController()->GetScale());
92 EXPECT_EQ("200,150 400x300", GetViewport().ToString());
94 // Disables magnifier.
95 GetMagnificationController()->SetEnabled(false);
96 EXPECT_TRUE(GetRootWindow()->layer()->transform().IsIdentity());
97 EXPECT_EQ(1.0f
, GetMagnificationController()->GetScale());
98 EXPECT_EQ("0,0 800x600", GetViewport().ToString());
100 // Confirms the the scale can't be changed.
101 GetMagnificationController()->SetScale(4.0f
, false);
102 EXPECT_TRUE(GetRootWindow()->layer()->transform().IsIdentity());
103 EXPECT_EQ(1.0f
, GetMagnificationController()->GetScale());
104 EXPECT_EQ("0,0 800x600", GetViewport().ToString());
107 TEST_F(MagnificationControllerTest
, MagnifyAndUnmagnify
) {
108 // Enables magnifier and confirms the default scale is 2.0x.
109 GetMagnificationController()->SetEnabled(true);
110 EXPECT_FALSE(GetRootWindow()->layer()->transform().IsIdentity());
111 EXPECT_EQ(2.0f
, GetMagnificationController()->GetScale());
112 EXPECT_EQ("200,150 400x300", GetViewport().ToString());
113 EXPECT_EQ("400,300", CurrentPointOfInterest());
115 // Changes the scale.
116 GetMagnificationController()->SetScale(4.0f
, false);
117 EXPECT_EQ(4.0f
, GetMagnificationController()->GetScale());
118 EXPECT_EQ("300,225 200x150", GetViewport().ToString());
119 EXPECT_EQ("400,300", CurrentPointOfInterest());
121 GetMagnificationController()->SetScale(1.0f
, false);
122 EXPECT_EQ(1.0f
, GetMagnificationController()->GetScale());
123 EXPECT_EQ("0,0 800x600", GetViewport().ToString());
124 EXPECT_EQ("400,300", CurrentPointOfInterest());
126 GetMagnificationController()->SetScale(3.0f
, false);
127 EXPECT_EQ(3.0f
, GetMagnificationController()->GetScale());
128 EXPECT_EQ("266,200 267x200", GetViewport().ToString());
129 EXPECT_EQ("400,300", CurrentPointOfInterest());
132 TEST_F(MagnificationControllerTest
, MoveWindow
) {
133 // Enables magnifier and confirm the viewport is at center.
134 GetMagnificationController()->SetEnabled(true);
135 EXPECT_EQ(2.0f
, GetMagnificationController()->GetScale());
136 EXPECT_EQ("200,150 400x300", GetViewport().ToString());
138 // Move the viewport.
139 GetMagnificationController()->MoveWindow(0, 0, false);
140 EXPECT_EQ("0,0 400x300", GetViewport().ToString());
142 GetMagnificationController()->MoveWindow(200, 300, false);
143 EXPECT_EQ("200,300 400x300", GetViewport().ToString());
145 GetMagnificationController()->MoveWindow(400, 0, false);
146 EXPECT_EQ("400,0 400x300", GetViewport().ToString());
148 GetMagnificationController()->MoveWindow(400, 300, false);
149 EXPECT_EQ("400,300 400x300", GetViewport().ToString());
151 // Confirms that the viewport can't across the top-left border.
152 GetMagnificationController()->MoveWindow(-100, 0, false);
153 EXPECT_EQ("0,0 400x300", GetViewport().ToString());
155 GetMagnificationController()->MoveWindow(0, -100, false);
156 EXPECT_EQ("0,0 400x300", GetViewport().ToString());
158 GetMagnificationController()->MoveWindow(-100, -100, false);
159 EXPECT_EQ("0,0 400x300", GetViewport().ToString());
161 // Confirms that the viewport can't across the bittom-right border.
162 GetMagnificationController()->MoveWindow(800, 0, false);
163 EXPECT_EQ("400,0 400x300", GetViewport().ToString());
165 GetMagnificationController()->MoveWindow(0, 400, false);
166 EXPECT_EQ("0,300 400x300", GetViewport().ToString());
168 GetMagnificationController()->MoveWindow(200, 400, false);
169 EXPECT_EQ("200,300 400x300", GetViewport().ToString());
171 GetMagnificationController()->MoveWindow(1000, 1000, false);
172 EXPECT_EQ("400,300 400x300", GetViewport().ToString());
175 TEST_F(MagnificationControllerTest
, PointOfInterest
) {
176 ui::test::EventGenerator
generator(Shell::GetPrimaryRootWindow());
178 generator
.MoveMouseToInHost(gfx::Point(0, 0));
179 EXPECT_EQ("0,0", CurrentPointOfInterest());
181 generator
.MoveMouseToInHost(gfx::Point(799, 599));
182 EXPECT_EQ("799,599", CurrentPointOfInterest());
184 generator
.MoveMouseToInHost(gfx::Point(400, 300));
185 EXPECT_EQ("400,300", CurrentPointOfInterest());
187 GetMagnificationController()->SetEnabled(true);
188 EXPECT_EQ("400,300", CurrentPointOfInterest());
190 generator
.MoveMouseToInHost(gfx::Point(500, 400));
191 EXPECT_EQ("450,350", CurrentPointOfInterest());
194 TEST_F(MagnificationControllerTest
, PanWindow2xLeftToRight
) {
195 const aura::Env
* env
= aura::Env::GetInstance();
196 ui::test::EventGenerator
generator(Shell::GetPrimaryRootWindow());
198 generator
.MoveMouseToInHost(gfx::Point(0, 0));
199 EXPECT_EQ(1.f
, GetMagnificationController()->GetScale());
200 EXPECT_EQ("0,0 800x600", GetViewport().ToString());
201 EXPECT_EQ("0,0", env
->last_mouse_location().ToString());
203 // Enables magnifier and confirm the viewport is at center.
204 GetMagnificationController()->SetEnabled(true);
205 EXPECT_EQ(2.0f
, GetMagnificationController()->GetScale());
207 GetMagnificationController()->MoveWindow(0, 0, false);
208 generator
.MoveMouseToInHost(gfx::Point(0, 0));
209 EXPECT_EQ("0,0", env
->last_mouse_location().ToString());
210 EXPECT_EQ("0,0 400x300", GetViewport().ToString());
212 generator
.MoveMouseToInHost(gfx::Point(300, 150));
213 EXPECT_EQ("150,75", env
->last_mouse_location().ToString());
214 EXPECT_EQ("0,0 400x300", GetViewport().ToString());
216 generator
.MoveMouseToInHost(gfx::Point(700, 150));
217 EXPECT_EQ("350,75", env
->last_mouse_location().ToString());
218 EXPECT_EQ("0,0 400x300", GetViewport().ToString());
220 generator
.MoveMouseToInHost(gfx::Point(701, 150));
221 EXPECT_EQ("350,75", env
->last_mouse_location().ToString());
222 EXPECT_EQ("0,0 400x300", GetViewport().ToString());
224 generator
.MoveMouseToInHost(gfx::Point(702, 150));
225 EXPECT_EQ("351,75", env
->last_mouse_location().ToString());
226 EXPECT_EQ("1,0 400x300", GetViewport().ToString());
228 generator
.MoveMouseToInHost(gfx::Point(703, 150));
229 EXPECT_EQ("352,75", env
->last_mouse_location().ToString());
230 EXPECT_EQ("2,0 400x300", GetViewport().ToString());
232 generator
.MoveMouseToInHost(gfx::Point(704, 150));
233 EXPECT_EQ("354,75", env
->last_mouse_location().ToString());
234 EXPECT_EQ("4,0 400x300", GetViewport().ToString());
236 generator
.MoveMouseToInHost(gfx::Point(712, 150));
237 EXPECT_EQ("360,75", env
->last_mouse_location().ToString());
238 EXPECT_EQ("10,0 400x300", GetViewport().ToString());
240 generator
.MoveMouseToInHost(gfx::Point(600, 150));
241 EXPECT_EQ("310,75", env
->last_mouse_location().ToString());
242 EXPECT_EQ("10,0 400x300", GetViewport().ToString());
244 generator
.MoveMouseToInHost(gfx::Point(720, 150));
245 EXPECT_EQ("370,75", env
->last_mouse_location().ToString());
246 EXPECT_EQ("20,0 400x300", GetViewport().ToString());
248 generator
.MoveMouseToInHost(gfx::Point(780, 150));
249 EXPECT_EQ("410,75", env
->last_mouse_location().ToString());
250 EXPECT_EQ("410,75", CurrentPointOfInterest());
251 EXPECT_EQ("60,0 400x300", GetViewport().ToString());
253 generator
.MoveMouseToInHost(gfx::Point(799, 150));
254 EXPECT_EQ("459,75", env
->last_mouse_location().ToString());
255 EXPECT_EQ("109,0 400x300", GetViewport().ToString());
257 generator
.MoveMouseToInHost(gfx::Point(702, 150));
258 EXPECT_EQ("460,75", env
->last_mouse_location().ToString());
259 EXPECT_EQ("110,0 400x300", GetViewport().ToString());
261 generator
.MoveMouseToInHost(gfx::Point(780, 150));
262 EXPECT_EQ("500,75", env
->last_mouse_location().ToString());
263 EXPECT_EQ("150,0 400x300", GetViewport().ToString());
265 generator
.MoveMouseToInHost(gfx::Point(780, 150));
266 EXPECT_EQ("540,75", env
->last_mouse_location().ToString());
267 EXPECT_EQ("190,0 400x300", GetViewport().ToString());
269 generator
.MoveMouseToInHost(gfx::Point(780, 150));
270 EXPECT_EQ("580,75", env
->last_mouse_location().ToString());
271 EXPECT_EQ("230,0 400x300", GetViewport().ToString());
273 generator
.MoveMouseToInHost(gfx::Point(780, 150));
274 EXPECT_EQ("620,75", env
->last_mouse_location().ToString());
275 EXPECT_EQ("270,0 400x300", GetViewport().ToString());
277 generator
.MoveMouseToInHost(gfx::Point(780, 150));
278 EXPECT_EQ("660,75", env
->last_mouse_location().ToString());
279 EXPECT_EQ("310,0 400x300", GetViewport().ToString());
281 generator
.MoveMouseToInHost(gfx::Point(780, 150));
282 EXPECT_EQ("700,75", env
->last_mouse_location().ToString());
283 EXPECT_EQ("350,0 400x300", GetViewport().ToString());
285 generator
.MoveMouseToInHost(gfx::Point(780, 150));
286 EXPECT_EQ("740,75", env
->last_mouse_location().ToString());
287 EXPECT_EQ("390,0 400x300", GetViewport().ToString());
289 generator
.MoveMouseToInHost(gfx::Point(780, 150));
290 EXPECT_EQ("780,75", env
->last_mouse_location().ToString());
291 EXPECT_EQ("400,0 400x300", GetViewport().ToString());
293 generator
.MoveMouseToInHost(gfx::Point(799, 150));
294 EXPECT_EQ("799,75", env
->last_mouse_location().ToString());
295 EXPECT_EQ("400,0 400x300", GetViewport().ToString());
298 TEST_F(MagnificationControllerTest
, PanWindow2xRightToLeft
) {
299 const aura::Env
* env
= aura::Env::GetInstance();
300 ui::test::EventGenerator
generator(Shell::GetPrimaryRootWindow());
302 generator
.MoveMouseToInHost(gfx::Point(799, 300));
303 EXPECT_EQ(1.f
, GetMagnificationController()->GetScale());
304 EXPECT_EQ("0,0 800x600", GetViewport().ToString());
305 EXPECT_EQ("799,300", env
->last_mouse_location().ToString());
307 // Enables magnifier and confirm the viewport is at center.
308 GetMagnificationController()->SetEnabled(true);
310 generator
.MoveMouseToInHost(gfx::Point(799, 300));
311 EXPECT_EQ("798,300", env
->last_mouse_location().ToString());
312 EXPECT_EQ("400,150 400x300", GetViewport().ToString());
314 generator
.MoveMouseToInHost(gfx::Point(0, 300));
315 EXPECT_EQ("400,300", env
->last_mouse_location().ToString());
316 EXPECT_EQ("350,150 400x300", GetViewport().ToString());
318 generator
.MoveMouseToInHost(gfx::Point(0, 300));
319 EXPECT_EQ("350,300", env
->last_mouse_location().ToString());
320 EXPECT_EQ("300,150 400x300", GetViewport().ToString());
322 generator
.MoveMouseToInHost(gfx::Point(0, 300));
323 EXPECT_EQ("300,300", env
->last_mouse_location().ToString());
324 EXPECT_EQ("250,150 400x300", GetViewport().ToString());
326 generator
.MoveMouseToInHost(gfx::Point(0, 300));
327 EXPECT_EQ("250,300", env
->last_mouse_location().ToString());
328 EXPECT_EQ("200,150 400x300", GetViewport().ToString());
330 generator
.MoveMouseToInHost(gfx::Point(0, 300));
331 EXPECT_EQ("200,300", env
->last_mouse_location().ToString());
332 EXPECT_EQ("150,150 400x300", GetViewport().ToString());
334 generator
.MoveMouseToInHost(gfx::Point(0, 300));
335 EXPECT_EQ("150,300", env
->last_mouse_location().ToString());
336 EXPECT_EQ("100,150 400x300", GetViewport().ToString());
338 generator
.MoveMouseToInHost(gfx::Point(0, 300));
339 EXPECT_EQ("100,300", env
->last_mouse_location().ToString());
340 EXPECT_EQ("50,150 400x300", GetViewport().ToString());
342 generator
.MoveMouseToInHost(gfx::Point(0, 300));
343 EXPECT_EQ("50,300", env
->last_mouse_location().ToString());
344 EXPECT_EQ("0,150 400x300", GetViewport().ToString());
346 generator
.MoveMouseToInHost(gfx::Point(0, 300));
347 EXPECT_EQ("0,300", env
->last_mouse_location().ToString());
348 EXPECT_EQ("0,150 400x300", GetViewport().ToString());
351 TEST_F(MagnificationControllerTest
, PanWindowToRight
) {
352 const aura::Env
* env
= aura::Env::GetInstance();
353 ui::test::EventGenerator
generator(Shell::GetPrimaryRootWindow());
355 generator
.MoveMouseToInHost(gfx::Point(400, 300));
356 EXPECT_EQ(1.f
, GetMagnificationController()->GetScale());
357 EXPECT_EQ("0,0 800x600", GetViewport().ToString());
358 EXPECT_EQ("400,300", env
->last_mouse_location().ToString());
362 // Enables magnifier and confirm the viewport is at center.
363 GetMagnificationController()->SetEnabled(true);
364 EXPECT_FLOAT_EQ(2.f
, GetMagnificationController()->GetScale());
366 scale
*= kMagnificationScaleFactor
;
367 GetMagnificationController()->SetScale(scale
, false);
368 EXPECT_FLOAT_EQ(2.3784142, GetMagnificationController()->GetScale());
369 generator
.MoveMouseToInHost(gfx::Point(400, 300));
370 EXPECT_EQ("400,300", env
->last_mouse_location().ToString());
371 generator
.MoveMouseToInHost(gfx::Point(799, 300));
372 EXPECT_EQ("566,299", env
->last_mouse_location().ToString());
373 EXPECT_EQ("705,300", GetHostMouseLocation());
375 scale
*= kMagnificationScaleFactor
;
376 GetMagnificationController()->SetScale(scale
, false);
377 EXPECT_FLOAT_EQ(2.8284268, GetMagnificationController()->GetScale());
378 generator
.MoveMouseToInHost(gfx::Point(799, 300));
379 EXPECT_EQ("599,299", env
->last_mouse_location().ToString());
380 EXPECT_EQ("702,300", GetHostMouseLocation());
382 scale
*= kMagnificationScaleFactor
;
383 GetMagnificationController()->SetScale(scale
, false);
384 EXPECT_FLOAT_EQ(3.3635852, GetMagnificationController()->GetScale());
385 generator
.MoveMouseToInHost(gfx::Point(799, 300));
386 EXPECT_EQ("627,298", env
->last_mouse_location().ToString());
387 EXPECT_EQ("707,300", GetHostMouseLocation());
389 scale
*= kMagnificationScaleFactor
;
390 GetMagnificationController()->SetScale(scale
, false);
391 EXPECT_FLOAT_EQ(4.f
, GetMagnificationController()->GetScale());
392 generator
.MoveMouseToInHost(gfx::Point(799, 300));
393 EXPECT_EQ("649,298", env
->last_mouse_location().ToString());
394 EXPECT_EQ("704,300", GetHostMouseLocation());
397 TEST_F(MagnificationControllerTest
, PanWindowToLeft
) {
398 const aura::Env
* env
= aura::Env::GetInstance();
399 ui::test::EventGenerator
generator(Shell::GetPrimaryRootWindow());
401 generator
.MoveMouseToInHost(gfx::Point(400, 300));
402 EXPECT_EQ(1.f
, GetMagnificationController()->GetScale());
403 EXPECT_EQ("0,0 800x600", GetViewport().ToString());
404 EXPECT_EQ("400,300", env
->last_mouse_location().ToString());
408 // Enables magnifier and confirm the viewport is at center.
409 GetMagnificationController()->SetEnabled(true);
410 EXPECT_FLOAT_EQ(2.f
, GetMagnificationController()->GetScale());
412 scale
*= kMagnificationScaleFactor
;
413 GetMagnificationController()->SetScale(scale
, false);
414 EXPECT_FLOAT_EQ(2.3784142, GetMagnificationController()->GetScale());
415 generator
.MoveMouseToInHost(gfx::Point(400, 300));
416 EXPECT_EQ("400,300", env
->last_mouse_location().ToString());
417 generator
.MoveMouseToInHost(gfx::Point(0, 300));
418 EXPECT_EQ("231,299", env
->last_mouse_location().ToString());
419 EXPECT_EQ("100,300", GetHostMouseLocation());
421 scale
*= kMagnificationScaleFactor
;
422 GetMagnificationController()->SetScale(scale
, false);
423 EXPECT_FLOAT_EQ(2.8284268, GetMagnificationController()->GetScale());
424 generator
.MoveMouseToInHost(gfx::Point(0, 300));
425 EXPECT_EQ("194,299", env
->last_mouse_location().ToString());
426 EXPECT_EQ("99,300", GetHostMouseLocation());
428 scale
*= kMagnificationScaleFactor
;
429 GetMagnificationController()->SetScale(scale
, false);
430 EXPECT_FLOAT_EQ(3.3635852, GetMagnificationController()->GetScale());
431 generator
.MoveMouseToInHost(gfx::Point(0, 300));
432 EXPECT_EQ("164,298", env
->last_mouse_location().ToString());
433 EXPECT_EQ("98,300", GetHostMouseLocation());
435 scale
*= kMagnificationScaleFactor
;
436 GetMagnificationController()->SetScale(scale
, false);
437 EXPECT_FLOAT_EQ(4.f
, GetMagnificationController()->GetScale());
438 generator
.MoveMouseToInHost(gfx::Point(0, 300));
439 EXPECT_EQ("139,298", env
->last_mouse_location().ToString());
440 EXPECT_EQ("100,300", GetHostMouseLocation());