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/event_generator.h"
14 #include "ui/aura/window_tree_host.h"
15 #include "ui/gfx/rect_conversions.h"
16 #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() {
58 GetRootWindow()->GetHost()->QueryMouseLocation(&point);
59 return point.ToString();
62 ash::MagnificationController* GetMagnificationController() const {
63 return ash::Shell::GetInstance()->magnification_controller();
66 gfx::Rect GetViewport() const {
67 gfx::RectF bounds(0, 0, kRootWidth, kRootHeight);
68 GetRootWindow()->layer()->transform().TransformRectReverse(&bounds);
69 return gfx::ToEnclosingRect(bounds);
72 std::string CurrentPointOfInterest() const {
73 return GetMagnificationController()->
74 GetPointOfInterestForTesting().ToString();
78 DISALLOW_COPY_AND_ASSIGN(MagnificationControllerTest);
81 TEST_F(MagnificationControllerTest, EnableAndDisable) {
82 // Confirms the magnifier is disabled.
83 EXPECT_TRUE(GetRootWindow()->layer()->transform().IsIdentity());
84 EXPECT_EQ(1.0f, GetMagnificationController()->GetScale());
85 EXPECT_EQ("0,0 800x600", GetViewport().ToString());
88 GetMagnificationController()->SetEnabled(true);
89 EXPECT_FALSE(GetRootWindow()->layer()->transform().IsIdentity());
90 EXPECT_EQ(2.0f, GetMagnificationController()->GetScale());
91 EXPECT_EQ("200,150 400x300", GetViewport().ToString());
93 // Disables magnifier.
94 GetMagnificationController()->SetEnabled(false);
95 EXPECT_TRUE(GetRootWindow()->layer()->transform().IsIdentity());
96 EXPECT_EQ(1.0f, GetMagnificationController()->GetScale());
97 EXPECT_EQ("0,0 800x600", GetViewport().ToString());
99 // Confirms the the scale can't be changed.
100 GetMagnificationController()->SetScale(4.0f, false);
101 EXPECT_TRUE(GetRootWindow()->layer()->transform().IsIdentity());
102 EXPECT_EQ(1.0f, GetMagnificationController()->GetScale());
103 EXPECT_EQ("0,0 800x600", GetViewport().ToString());
106 TEST_F(MagnificationControllerTest, MagnifyAndUnmagnify) {
107 // Enables magnifier and confirms the default scale is 2.0x.
108 GetMagnificationController()->SetEnabled(true);
109 EXPECT_FALSE(GetRootWindow()->layer()->transform().IsIdentity());
110 EXPECT_EQ(2.0f, GetMagnificationController()->GetScale());
111 EXPECT_EQ("200,150 400x300", GetViewport().ToString());
112 EXPECT_EQ("400,300", CurrentPointOfInterest());
114 // Changes the scale.
115 GetMagnificationController()->SetScale(4.0f, false);
116 EXPECT_EQ(4.0f, GetMagnificationController()->GetScale());
117 EXPECT_EQ("300,225 200x150", GetViewport().ToString());
118 EXPECT_EQ("400,300", CurrentPointOfInterest());
120 GetMagnificationController()->SetScale(1.0f, false);
121 EXPECT_EQ(1.0f, GetMagnificationController()->GetScale());
122 EXPECT_EQ("0,0 800x600", GetViewport().ToString());
123 EXPECT_EQ("400,300", CurrentPointOfInterest());
125 GetMagnificationController()->SetScale(3.0f, false);
126 EXPECT_EQ(3.0f, GetMagnificationController()->GetScale());
127 EXPECT_EQ("266,200 267x200", GetViewport().ToString());
128 EXPECT_EQ("400,300", CurrentPointOfInterest());
131 TEST_F(MagnificationControllerTest, MoveWindow) {
132 // Enables magnifier and confirm the viewport is at center.
133 GetMagnificationController()->SetEnabled(true);
134 EXPECT_EQ(2.0f, GetMagnificationController()->GetScale());
135 EXPECT_EQ("200,150 400x300", GetViewport().ToString());
137 // Move the viewport.
138 GetMagnificationController()->MoveWindow(0, 0, false);
139 EXPECT_EQ("0,0 400x300", GetViewport().ToString());
141 GetMagnificationController()->MoveWindow(200, 300, false);
142 EXPECT_EQ("200,300 400x300", GetViewport().ToString());
144 GetMagnificationController()->MoveWindow(400, 0, false);
145 EXPECT_EQ("400,0 400x300", GetViewport().ToString());
147 GetMagnificationController()->MoveWindow(400, 300, false);
148 EXPECT_EQ("400,300 400x300", GetViewport().ToString());
150 // Confirms that the viewport can't across the top-left border.
151 GetMagnificationController()->MoveWindow(-100, 0, false);
152 EXPECT_EQ("0,0 400x300", GetViewport().ToString());
154 GetMagnificationController()->MoveWindow(0, -100, false);
155 EXPECT_EQ("0,0 400x300", GetViewport().ToString());
157 GetMagnificationController()->MoveWindow(-100, -100, false);
158 EXPECT_EQ("0,0 400x300", GetViewport().ToString());
160 // Confirms that the viewport can't across the bittom-right border.
161 GetMagnificationController()->MoveWindow(800, 0, false);
162 EXPECT_EQ("400,0 400x300", GetViewport().ToString());
164 GetMagnificationController()->MoveWindow(0, 400, false);
165 EXPECT_EQ("0,300 400x300", GetViewport().ToString());
167 GetMagnificationController()->MoveWindow(200, 400, false);
168 EXPECT_EQ("200,300 400x300", GetViewport().ToString());
170 GetMagnificationController()->MoveWindow(1000, 1000, false);
171 EXPECT_EQ("400,300 400x300", GetViewport().ToString());
174 TEST_F(MagnificationControllerTest, PointOfInterest) {
175 aura::test::EventGenerator generator(Shell::GetPrimaryRootWindow());
177 generator.MoveMouseToInHost(gfx::Point(0, 0));
178 EXPECT_EQ("0,0", CurrentPointOfInterest());
180 generator.MoveMouseToInHost(gfx::Point(799, 599));
181 EXPECT_EQ("799,599", CurrentPointOfInterest());
183 generator.MoveMouseToInHost(gfx::Point(400, 300));
184 EXPECT_EQ("400,300", CurrentPointOfInterest());
186 GetMagnificationController()->SetEnabled(true);
187 EXPECT_EQ("400,300", CurrentPointOfInterest());
189 generator.MoveMouseToInHost(gfx::Point(500, 400));
190 EXPECT_EQ("450,350", CurrentPointOfInterest());
193 TEST_F(MagnificationControllerTest, PanWindow2xLeftToRight) {
194 const aura::Env* env = aura::Env::GetInstance();
195 aura::test::EventGenerator generator(Shell::GetPrimaryRootWindow());
197 generator.MoveMouseToInHost(gfx::Point(0, 0));
198 EXPECT_EQ(1.f, GetMagnificationController()->GetScale());
199 EXPECT_EQ("0,0 800x600", GetViewport().ToString());
200 EXPECT_EQ("0,0", env->last_mouse_location().ToString());
202 // Enables magnifier and confirm the viewport is at center.
203 GetMagnificationController()->SetEnabled(true);
204 EXPECT_EQ(2.0f, GetMagnificationController()->GetScale());
206 GetMagnificationController()->MoveWindow(0, 0, false);
207 generator.MoveMouseToInHost(gfx::Point(0, 0));
208 EXPECT_EQ("0,0", env->last_mouse_location().ToString());
209 EXPECT_EQ("0,0 400x300", GetViewport().ToString());
211 generator.MoveMouseToInHost(gfx::Point(300, 150));
212 EXPECT_EQ("150,75", env->last_mouse_location().ToString());
213 EXPECT_EQ("0,0 400x300", GetViewport().ToString());
215 generator.MoveMouseToInHost(gfx::Point(700, 150));
216 EXPECT_EQ("350,75", env->last_mouse_location().ToString());
217 EXPECT_EQ("0,0 400x300", GetViewport().ToString());
219 generator.MoveMouseToInHost(gfx::Point(701, 150));
220 EXPECT_EQ("350,75", env->last_mouse_location().ToString());
221 EXPECT_EQ("0,0 400x300", GetViewport().ToString());
223 generator.MoveMouseToInHost(gfx::Point(702, 150));
224 EXPECT_EQ("351,75", env->last_mouse_location().ToString());
225 EXPECT_EQ("1,0 400x300", GetViewport().ToString());
227 generator.MoveMouseToInHost(gfx::Point(703, 150));
228 EXPECT_EQ("352,75", env->last_mouse_location().ToString());
229 EXPECT_EQ("2,0 400x300", GetViewport().ToString());
231 generator.MoveMouseToInHost(gfx::Point(704, 150));
232 EXPECT_EQ("354,75", env->last_mouse_location().ToString());
233 EXPECT_EQ("4,0 400x300", GetViewport().ToString());
235 generator.MoveMouseToInHost(gfx::Point(712, 150));
236 EXPECT_EQ("360,75", env->last_mouse_location().ToString());
237 EXPECT_EQ("10,0 400x300", GetViewport().ToString());
239 generator.MoveMouseToInHost(gfx::Point(600, 150));
240 EXPECT_EQ("310,75", env->last_mouse_location().ToString());
241 EXPECT_EQ("10,0 400x300", GetViewport().ToString());
243 generator.MoveMouseToInHost(gfx::Point(720, 150));
244 EXPECT_EQ("370,75", env->last_mouse_location().ToString());
245 EXPECT_EQ("20,0 400x300", GetViewport().ToString());
247 generator.MoveMouseToInHost(gfx::Point(780, 150));
248 EXPECT_EQ("410,75", env->last_mouse_location().ToString());
249 EXPECT_EQ("410,75", CurrentPointOfInterest());
250 EXPECT_EQ("60,0 400x300", GetViewport().ToString());
252 generator.MoveMouseToInHost(gfx::Point(799, 150));
253 EXPECT_EQ("459,75", env->last_mouse_location().ToString());
254 EXPECT_EQ("109,0 400x300", GetViewport().ToString());
256 generator.MoveMouseToInHost(gfx::Point(702, 150));
257 EXPECT_EQ("460,75", env->last_mouse_location().ToString());
258 EXPECT_EQ("110,0 400x300", GetViewport().ToString());
260 generator.MoveMouseToInHost(gfx::Point(780, 150));
261 EXPECT_EQ("500,75", env->last_mouse_location().ToString());
262 EXPECT_EQ("150,0 400x300", GetViewport().ToString());
264 generator.MoveMouseToInHost(gfx::Point(780, 150));
265 EXPECT_EQ("540,75", env->last_mouse_location().ToString());
266 EXPECT_EQ("190,0 400x300", GetViewport().ToString());
268 generator.MoveMouseToInHost(gfx::Point(780, 150));
269 EXPECT_EQ("580,75", env->last_mouse_location().ToString());
270 EXPECT_EQ("230,0 400x300", GetViewport().ToString());
272 generator.MoveMouseToInHost(gfx::Point(780, 150));
273 EXPECT_EQ("620,75", env->last_mouse_location().ToString());
274 EXPECT_EQ("270,0 400x300", GetViewport().ToString());
276 generator.MoveMouseToInHost(gfx::Point(780, 150));
277 EXPECT_EQ("660,75", env->last_mouse_location().ToString());
278 EXPECT_EQ("310,0 400x300", GetViewport().ToString());
280 generator.MoveMouseToInHost(gfx::Point(780, 150));
281 EXPECT_EQ("700,75", env->last_mouse_location().ToString());
282 EXPECT_EQ("350,0 400x300", GetViewport().ToString());
284 generator.MoveMouseToInHost(gfx::Point(780, 150));
285 EXPECT_EQ("740,75", env->last_mouse_location().ToString());
286 EXPECT_EQ("390,0 400x300", GetViewport().ToString());
288 generator.MoveMouseToInHost(gfx::Point(780, 150));
289 EXPECT_EQ("780,75", env->last_mouse_location().ToString());
290 EXPECT_EQ("400,0 400x300", GetViewport().ToString());
292 generator.MoveMouseToInHost(gfx::Point(799, 150));
293 EXPECT_EQ("799,75", env->last_mouse_location().ToString());
294 EXPECT_EQ("400,0 400x300", GetViewport().ToString());
297 TEST_F(MagnificationControllerTest, PanWindow2xRightToLeft) {
298 const aura::Env* env = aura::Env::GetInstance();
299 aura::test::EventGenerator generator(Shell::GetPrimaryRootWindow());
301 generator.MoveMouseToInHost(gfx::Point(799, 300));
302 EXPECT_EQ(1.f, GetMagnificationController()->GetScale());
303 EXPECT_EQ("0,0 800x600", GetViewport().ToString());
304 EXPECT_EQ("799,300", env->last_mouse_location().ToString());
306 // Enables magnifier and confirm the viewport is at center.
307 GetMagnificationController()->SetEnabled(true);
309 generator.MoveMouseToInHost(gfx::Point(799, 300));
310 EXPECT_EQ("798,300", env->last_mouse_location().ToString());
311 EXPECT_EQ("400,150 400x300", GetViewport().ToString());
313 generator.MoveMouseToInHost(gfx::Point(0, 300));
314 EXPECT_EQ("400,300", env->last_mouse_location().ToString());
315 EXPECT_EQ("350,150 400x300", GetViewport().ToString());
317 generator.MoveMouseToInHost(gfx::Point(0, 300));
318 EXPECT_EQ("350,300", env->last_mouse_location().ToString());
319 EXPECT_EQ("300,150 400x300", GetViewport().ToString());
321 generator.MoveMouseToInHost(gfx::Point(0, 300));
322 EXPECT_EQ("300,300", env->last_mouse_location().ToString());
323 EXPECT_EQ("250,150 400x300", GetViewport().ToString());
325 generator.MoveMouseToInHost(gfx::Point(0, 300));
326 EXPECT_EQ("250,300", env->last_mouse_location().ToString());
327 EXPECT_EQ("200,150 400x300", GetViewport().ToString());
329 generator.MoveMouseToInHost(gfx::Point(0, 300));
330 EXPECT_EQ("200,300", env->last_mouse_location().ToString());
331 EXPECT_EQ("150,150 400x300", GetViewport().ToString());
333 generator.MoveMouseToInHost(gfx::Point(0, 300));
334 EXPECT_EQ("150,300", env->last_mouse_location().ToString());
335 EXPECT_EQ("100,150 400x300", GetViewport().ToString());
337 generator.MoveMouseToInHost(gfx::Point(0, 300));
338 EXPECT_EQ("100,300", env->last_mouse_location().ToString());
339 EXPECT_EQ("50,150 400x300", GetViewport().ToString());
341 generator.MoveMouseToInHost(gfx::Point(0, 300));
342 EXPECT_EQ("50,300", env->last_mouse_location().ToString());
343 EXPECT_EQ("0,150 400x300", GetViewport().ToString());
345 generator.MoveMouseToInHost(gfx::Point(0, 300));
346 EXPECT_EQ("0,300", env->last_mouse_location().ToString());
347 EXPECT_EQ("0,150 400x300", GetViewport().ToString());
350 TEST_F(MagnificationControllerTest, PanWindowToRight) {
351 const aura::Env* env = aura::Env::GetInstance();
352 aura::test::EventGenerator generator(Shell::GetPrimaryRootWindow());
354 generator.MoveMouseToInHost(gfx::Point(400, 300));
355 EXPECT_EQ(1.f, GetMagnificationController()->GetScale());
356 EXPECT_EQ("0,0 800x600", GetViewport().ToString());
357 EXPECT_EQ("400,300", env->last_mouse_location().ToString());
361 // Enables magnifier and confirm the viewport is at center.
362 GetMagnificationController()->SetEnabled(true);
363 EXPECT_FLOAT_EQ(2.f, GetMagnificationController()->GetScale());
365 scale *= kMagnificationScaleFactor;
366 GetMagnificationController()->SetScale(scale, false);
367 EXPECT_FLOAT_EQ(2.3784142, GetMagnificationController()->GetScale());
368 generator.MoveMouseToInHost(gfx::Point(400, 300));
369 EXPECT_EQ("400,300", env->last_mouse_location().ToString());
370 generator.MoveMouseToInHost(gfx::Point(799, 300));
371 EXPECT_EQ("566,299", env->last_mouse_location().ToString());
372 EXPECT_EQ("705,300", GetHostMouseLocation());
374 scale *= kMagnificationScaleFactor;
375 GetMagnificationController()->SetScale(scale, false);
376 EXPECT_FLOAT_EQ(2.8284268, GetMagnificationController()->GetScale());
377 generator.MoveMouseToInHost(gfx::Point(799, 300));
378 EXPECT_EQ("599,299", env->last_mouse_location().ToString());
379 EXPECT_EQ("702,300", GetHostMouseLocation());
381 scale *= kMagnificationScaleFactor;
382 GetMagnificationController()->SetScale(scale, false);
383 EXPECT_FLOAT_EQ(3.3635852, GetMagnificationController()->GetScale());
384 generator.MoveMouseToInHost(gfx::Point(799, 300));
385 EXPECT_EQ("627,298", env->last_mouse_location().ToString());
386 EXPECT_EQ("707,300", GetHostMouseLocation());
388 scale *= kMagnificationScaleFactor;
389 GetMagnificationController()->SetScale(scale, false);
390 EXPECT_FLOAT_EQ(4.f, GetMagnificationController()->GetScale());
391 generator.MoveMouseToInHost(gfx::Point(799, 300));
392 EXPECT_EQ("649,298", env->last_mouse_location().ToString());
393 EXPECT_EQ("704,300", GetHostMouseLocation());
396 TEST_F(MagnificationControllerTest, PanWindowToLeft) {
397 const aura::Env* env = aura::Env::GetInstance();
398 aura::test::EventGenerator generator(Shell::GetPrimaryRootWindow());
400 generator.MoveMouseToInHost(gfx::Point(400, 300));
401 EXPECT_EQ(1.f, GetMagnificationController()->GetScale());
402 EXPECT_EQ("0,0 800x600", GetViewport().ToString());
403 EXPECT_EQ("400,300", env->last_mouse_location().ToString());
407 // Enables magnifier and confirm the viewport is at center.
408 GetMagnificationController()->SetEnabled(true);
409 EXPECT_FLOAT_EQ(2.f, GetMagnificationController()->GetScale());
411 scale *= kMagnificationScaleFactor;
412 GetMagnificationController()->SetScale(scale, false);
413 EXPECT_FLOAT_EQ(2.3784142, GetMagnificationController()->GetScale());
414 generator.MoveMouseToInHost(gfx::Point(400, 300));
415 EXPECT_EQ("400,300", env->last_mouse_location().ToString());
416 generator.MoveMouseToInHost(gfx::Point(0, 300));
417 EXPECT_EQ("231,299", env->last_mouse_location().ToString());
418 EXPECT_EQ("100,300", GetHostMouseLocation());
420 scale *= kMagnificationScaleFactor;
421 GetMagnificationController()->SetScale(scale, false);
422 EXPECT_FLOAT_EQ(2.8284268, GetMagnificationController()->GetScale());
423 generator.MoveMouseToInHost(gfx::Point(0, 300));
424 EXPECT_EQ("194,299", env->last_mouse_location().ToString());
425 EXPECT_EQ("99,300", GetHostMouseLocation());
427 scale *= kMagnificationScaleFactor;
428 GetMagnificationController()->SetScale(scale, false);
429 EXPECT_FLOAT_EQ(3.3635852, GetMagnificationController()->GetScale());
430 generator.MoveMouseToInHost(gfx::Point(0, 300));
431 EXPECT_EQ("164,298", env->last_mouse_location().ToString());
432 EXPECT_EQ("98,300", GetHostMouseLocation());
434 scale *= kMagnificationScaleFactor;
435 GetMagnificationController()->SetScale(scale, false);
436 EXPECT_FLOAT_EQ(4.f, GetMagnificationController()->GetScale());
437 generator.MoveMouseToInHost(gfx::Point(0, 300));
438 EXPECT_EQ("139,298", env->last_mouse_location().ToString());
439 EXPECT_EQ("100,300", GetHostMouseLocation());
442 } // namespace internal