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.
4
5 #include "ash/magnifier/magnification_controller.h"
6
7 #include "ash/magnifier/magnifier_constants.h"
8 #include "ash/shell.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"
18
19 namespace ash {
20 namespace {
21
22 const int kRootHeight = 600;
23 const int kRootWidth = 800;
24
25 } // namespace
26
27 class MagnificationControllerTest: public test::AshTestBase {
28 public:
MagnificationControllerTest()29 MagnificationControllerTest() {}
~MagnificationControllerTest()30 virtual ~MagnificationControllerTest() {}
31
SetUp()32 virtual void SetUp() OVERRIDE {
33 AshTestBase::SetUp();
34 UpdateDisplay(base::StringPrintf("%dx%d", kRootWidth, kRootHeight));
35
36 aura::Window* root = GetRootWindow();
37 gfx::Rect root_bounds(root->bounds());
38
39 #if defined(OS_WIN)
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());
44 #endif
45 }
46
TearDown()47 virtual void TearDown() OVERRIDE {
48 AshTestBase::TearDown();
49 }
50
51 protected:
GetRootWindow() const52 aura::Window* GetRootWindow() const {
53 return Shell::GetPrimaryRootWindow();
54 }
55
GetHostMouseLocation()56 std::string GetHostMouseLocation() {
57 const gfx::Point& location =
58 aura::test::QueryLatestMousePositionRequestInHost(
59 GetRootWindow()->GetHost());
60 return location.ToString();
61 }
62
GetMagnificationController() const63 ash::MagnificationController* GetMagnificationController() const {
64 return ash::Shell::GetInstance()->magnification_controller();
65 }
66
GetViewport() const67 gfx::Rect GetViewport() const {
68 gfx::RectF bounds(0, 0, kRootWidth, kRootHeight);
69 GetRootWindow()->layer()->transform().TransformRectReverse(&bounds);
70 return gfx::ToEnclosingRect(bounds);
71 }
72
CurrentPointOfInterest() const73 std::string CurrentPointOfInterest() const {
74 return GetMagnificationController()->
75 GetPointOfInterestForTesting().ToString();
76 }
77
78 private:
79 DISALLOW_COPY_AND_ASSIGN(MagnificationControllerTest);
80 };
81
TEST_F(MagnificationControllerTest,EnableAndDisable)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());
87
88 // Enables magnifier.
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());
93
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());
99
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());
105 }
106
TEST_F(MagnificationControllerTest,MagnifyAndUnmagnify)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());
114
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());
120
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());
125
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());
130 }
131
TEST_F(MagnificationControllerTest,MoveWindow)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());
137
138 // Move the viewport.
139 GetMagnificationController()->MoveWindow(0, 0, false);
140 EXPECT_EQ("0,0 400x300", GetViewport().ToString());
141
142 GetMagnificationController()->MoveWindow(200, 300, false);
143 EXPECT_EQ("200,300 400x300", GetViewport().ToString());
144
145 GetMagnificationController()->MoveWindow(400, 0, false);
146 EXPECT_EQ("400,0 400x300", GetViewport().ToString());
147
148 GetMagnificationController()->MoveWindow(400, 300, false);
149 EXPECT_EQ("400,300 400x300", GetViewport().ToString());
150
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());
154
155 GetMagnificationController()->MoveWindow(0, -100, false);
156 EXPECT_EQ("0,0 400x300", GetViewport().ToString());
157
158 GetMagnificationController()->MoveWindow(-100, -100, false);
159 EXPECT_EQ("0,0 400x300", GetViewport().ToString());
160
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());
164
165 GetMagnificationController()->MoveWindow(0, 400, false);
166 EXPECT_EQ("0,300 400x300", GetViewport().ToString());
167
168 GetMagnificationController()->MoveWindow(200, 400, false);
169 EXPECT_EQ("200,300 400x300", GetViewport().ToString());
170
171 GetMagnificationController()->MoveWindow(1000, 1000, false);
172 EXPECT_EQ("400,300 400x300", GetViewport().ToString());
173 }
174
TEST_F(MagnificationControllerTest,PointOfInterest)175 TEST_F(MagnificationControllerTest, PointOfInterest) {
176 ui::test::EventGenerator generator(Shell::GetPrimaryRootWindow());
177
178 generator.MoveMouseToInHost(gfx::Point(0, 0));
179 EXPECT_EQ("0,0", CurrentPointOfInterest());
180
181 generator.MoveMouseToInHost(gfx::Point(799, 599));
182 EXPECT_EQ("799,599", CurrentPointOfInterest());
183
184 generator.MoveMouseToInHost(gfx::Point(400, 300));
185 EXPECT_EQ("400,300", CurrentPointOfInterest());
186
187 GetMagnificationController()->SetEnabled(true);
188 EXPECT_EQ("400,300", CurrentPointOfInterest());
189
190 generator.MoveMouseToInHost(gfx::Point(500, 400));
191 EXPECT_EQ("450,350", CurrentPointOfInterest());
192 }
193
TEST_F(MagnificationControllerTest,PanWindow2xLeftToRight)194 TEST_F(MagnificationControllerTest, PanWindow2xLeftToRight) {
195 const aura::Env* env = aura::Env::GetInstance();
196 ui::test::EventGenerator generator(Shell::GetPrimaryRootWindow());
197
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());
202
203 // Enables magnifier and confirm the viewport is at center.
204 GetMagnificationController()->SetEnabled(true);
205 EXPECT_EQ(2.0f, GetMagnificationController()->GetScale());
206
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());
211
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());
215
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());
219
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());
223
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());
227
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());
231
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());
235
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());
239
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());
243
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());
247
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());
252
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());
256
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());
260
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());
264
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());
268
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());
272
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());
276
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());
280
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());
284
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());
288
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());
292
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());
296 }
297
TEST_F(MagnificationControllerTest,PanWindow2xRightToLeft)298 TEST_F(MagnificationControllerTest, PanWindow2xRightToLeft) {
299 const aura::Env* env = aura::Env::GetInstance();
300 ui::test::EventGenerator generator(Shell::GetPrimaryRootWindow());
301
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());
306
307 // Enables magnifier and confirm the viewport is at center.
308 GetMagnificationController()->SetEnabled(true);
309
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());
313
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());
317
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());
321
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());
325
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());
329
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());
333
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());
337
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());
341
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());
345
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());
349 }
350
TEST_F(MagnificationControllerTest,PanWindowToRight)351 TEST_F(MagnificationControllerTest, PanWindowToRight) {
352 const aura::Env* env = aura::Env::GetInstance();
353 ui::test::EventGenerator generator(Shell::GetPrimaryRootWindow());
354
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());
359
360 float scale = 2.f;
361
362 // Enables magnifier and confirm the viewport is at center.
363 GetMagnificationController()->SetEnabled(true);
364 EXPECT_FLOAT_EQ(2.f, GetMagnificationController()->GetScale());
365
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());
374
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());
381
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());
388
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());
395 }
396
TEST_F(MagnificationControllerTest,PanWindowToLeft)397 TEST_F(MagnificationControllerTest, PanWindowToLeft) {
398 const aura::Env* env = aura::Env::GetInstance();
399 ui::test::EventGenerator generator(Shell::GetPrimaryRootWindow());
400
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());
405
406 float scale = 2.f;
407
408 // Enables magnifier and confirm the viewport is at center.
409 GetMagnificationController()->SetEnabled(true);
410 EXPECT_FLOAT_EQ(2.f, GetMagnificationController()->GetScale());
411
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());
420
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());
427
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());
434
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());
441 }
442
443 } // namespace ash
444