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#include "ash/magnifier/magnifier_constants.h"
7#include "ash/shell.h"
8#include "ash/test/ash_test_base.h"
9#include "base/strings/stringprintf.h"
10#include "ui/aura/client/aura_constants.h"
11#include "ui/aura/env.h"
12#include "ui/aura/root_window.h"
13#include "ui/aura/test/event_generator.h"
14#include "ui/gfx/rect_conversions.h"
15#include "ui/gfx/screen.h"
16
17namespace ash {
18namespace internal {
19namespace {
20
21const int kRootHeight = 600;
22const int kRootWidth = 800;
23
24}  // namespace
25
26class MagnificationControllerTest: public test::AshTestBase {
27 public:
28  MagnificationControllerTest() {}
29  virtual ~MagnificationControllerTest() {}
30
31  virtual void SetUp() OVERRIDE {
32    AshTestBase::SetUp();
33    UpdateDisplay(base::StringPrintf("%dx%d", kRootWidth, kRootHeight));
34
35    aura::RootWindow* root = GetRootWindow();
36    gfx::Rect root_bounds(root->bounds());
37
38#if defined(OS_WIN)
39    // RootWindow and Display can't resize on Windows Ash.
40    // http://crbug.com/165962
41    EXPECT_EQ(kRootHeight, root_bounds.height());
42    EXPECT_EQ(kRootWidth, root_bounds.width());
43#endif
44  }
45
46  virtual void TearDown() OVERRIDE {
47    AshTestBase::TearDown();
48  }
49
50 protected:
51  aura::RootWindow* GetRootWindow() const {
52    return Shell::GetPrimaryRootWindow();
53  }
54
55  std::string GetHostMouseLocation() {
56    gfx::Point point;
57    GetRootWindow()->QueryMouseLocationForTest(&point);
58    return point.ToString();
59  }
60
61  ash::MagnificationController* GetMagnificationController() const {
62    return ash::Shell::GetInstance()->magnification_controller();
63  }
64
65  gfx::Rect GetViewport() const {
66    gfx::RectF bounds(0, 0, kRootWidth, kRootHeight);
67    GetRootWindow()->layer()->transform().TransformRectReverse(&bounds);
68    return gfx::ToEnclosingRect(bounds);
69  }
70
71  std::string CurrentPointOfInterest() const {
72    return GetMagnificationController()->
73        GetPointOfInterestForTesting().ToString();
74  }
75
76 private:
77  DISALLOW_COPY_AND_ASSIGN(MagnificationControllerTest);
78};
79
80TEST_F(MagnificationControllerTest, EnableAndDisable) {
81  // Confirms the magnifier is disabled.
82  EXPECT_TRUE(GetRootWindow()->layer()->transform().IsIdentity());
83  EXPECT_EQ(1.0f, GetMagnificationController()->GetScale());
84  EXPECT_EQ("0,0 800x600", GetViewport().ToString());
85
86  // Enables magnifier.
87  GetMagnificationController()->SetEnabled(true);
88  EXPECT_FALSE(GetRootWindow()->layer()->transform().IsIdentity());
89  EXPECT_EQ(2.0f, GetMagnificationController()->GetScale());
90  EXPECT_EQ("200,150 400x300", GetViewport().ToString());
91
92  // Disables magnifier.
93  GetMagnificationController()->SetEnabled(false);
94  EXPECT_TRUE(GetRootWindow()->layer()->transform().IsIdentity());
95  EXPECT_EQ(1.0f, GetMagnificationController()->GetScale());
96  EXPECT_EQ("0,0 800x600", GetViewport().ToString());
97
98  // Confirms the the scale can't be changed.
99  GetMagnificationController()->SetScale(4.0f, false);
100  EXPECT_TRUE(GetRootWindow()->layer()->transform().IsIdentity());
101  EXPECT_EQ(1.0f, GetMagnificationController()->GetScale());
102  EXPECT_EQ("0,0 800x600", GetViewport().ToString());
103}
104
105TEST_F(MagnificationControllerTest, MagnifyAndUnmagnify) {
106  // Enables magnifier and confirms the default scale is 2.0x.
107  GetMagnificationController()->SetEnabled(true);
108  EXPECT_FALSE(GetRootWindow()->layer()->transform().IsIdentity());
109  EXPECT_EQ(2.0f, GetMagnificationController()->GetScale());
110  EXPECT_EQ("200,150 400x300", GetViewport().ToString());
111  EXPECT_EQ("400,300", CurrentPointOfInterest());
112
113  // Changes the scale.
114  GetMagnificationController()->SetScale(4.0f, false);
115  EXPECT_EQ(4.0f, GetMagnificationController()->GetScale());
116  EXPECT_EQ("300,225 200x150", GetViewport().ToString());
117  EXPECT_EQ("400,300", CurrentPointOfInterest());
118
119  GetMagnificationController()->SetScale(1.0f, false);
120  EXPECT_EQ(1.0f, GetMagnificationController()->GetScale());
121  EXPECT_EQ("0,0 800x600", GetViewport().ToString());
122  EXPECT_EQ("400,300", CurrentPointOfInterest());
123
124  GetMagnificationController()->SetScale(3.0f, false);
125  EXPECT_EQ(3.0f, GetMagnificationController()->GetScale());
126  EXPECT_EQ("266,200 267x200", GetViewport().ToString());
127  EXPECT_EQ("400,300", CurrentPointOfInterest());
128}
129
130TEST_F(MagnificationControllerTest, MoveWindow) {
131  // Enables magnifier and confirm the viewport is at center.
132  GetMagnificationController()->SetEnabled(true);
133  EXPECT_EQ(2.0f, GetMagnificationController()->GetScale());
134  EXPECT_EQ("200,150 400x300", GetViewport().ToString());
135
136  // Move the viewport.
137  GetMagnificationController()->MoveWindow(0, 0, false);
138  EXPECT_EQ("0,0 400x300", GetViewport().ToString());
139
140  GetMagnificationController()->MoveWindow(200, 300, false);
141  EXPECT_EQ("200,300 400x300", GetViewport().ToString());
142
143  GetMagnificationController()->MoveWindow(400, 0, false);
144  EXPECT_EQ("400,0 400x300", GetViewport().ToString());
145
146  GetMagnificationController()->MoveWindow(400, 300, false);
147  EXPECT_EQ("400,300 400x300", GetViewport().ToString());
148
149  // Confirms that the viewport can't across the top-left border.
150  GetMagnificationController()->MoveWindow(-100, 0, false);
151  EXPECT_EQ("0,0 400x300", GetViewport().ToString());
152
153  GetMagnificationController()->MoveWindow(0, -100, false);
154  EXPECT_EQ("0,0 400x300", GetViewport().ToString());
155
156  GetMagnificationController()->MoveWindow(-100, -100, false);
157  EXPECT_EQ("0,0 400x300", GetViewport().ToString());
158
159  // Confirms that the viewport can't across the bittom-right border.
160  GetMagnificationController()->MoveWindow(800, 0, false);
161  EXPECT_EQ("400,0 400x300", GetViewport().ToString());
162
163  GetMagnificationController()->MoveWindow(0, 400, false);
164  EXPECT_EQ("0,300 400x300", GetViewport().ToString());
165
166  GetMagnificationController()->MoveWindow(200, 400, false);
167  EXPECT_EQ("200,300 400x300", GetViewport().ToString());
168
169  GetMagnificationController()->MoveWindow(1000, 1000, false);
170  EXPECT_EQ("400,300 400x300", GetViewport().ToString());
171}
172
173TEST_F(MagnificationControllerTest, PointOfInterest) {
174  aura::test::EventGenerator generator(Shell::GetPrimaryRootWindow());
175
176  generator.MoveMouseToInHost(gfx::Point(0, 0));
177  EXPECT_EQ("0,0", CurrentPointOfInterest());
178
179  generator.MoveMouseToInHost(gfx::Point(799, 599));
180  EXPECT_EQ("799,599", CurrentPointOfInterest());
181
182  generator.MoveMouseToInHost(gfx::Point(400, 300));
183  EXPECT_EQ("400,300", CurrentPointOfInterest());
184
185  GetMagnificationController()->SetEnabled(true);
186  EXPECT_EQ("400,300", CurrentPointOfInterest());
187
188  generator.MoveMouseToInHost(gfx::Point(500, 400));
189  EXPECT_EQ("450,350", CurrentPointOfInterest());
190}
191
192TEST_F(MagnificationControllerTest, PanWindow2xLeftToRight) {
193  const aura::Env* env = aura::Env::GetInstance();
194  aura::test::EventGenerator generator(Shell::GetPrimaryRootWindow());
195
196  generator.MoveMouseToInHost(gfx::Point(0, 0));
197  EXPECT_EQ(1.f, GetMagnificationController()->GetScale());
198  EXPECT_EQ("0,0 800x600", GetViewport().ToString());
199  EXPECT_EQ("0,0", env->last_mouse_location().ToString());
200
201  // Enables magnifier and confirm the viewport is at center.
202  GetMagnificationController()->SetEnabled(true);
203  EXPECT_EQ(2.0f, GetMagnificationController()->GetScale());
204
205  GetMagnificationController()->MoveWindow(0, 0, false);
206  generator.MoveMouseToInHost(gfx::Point(0, 0));
207  EXPECT_EQ("0,0", env->last_mouse_location().ToString());
208  EXPECT_EQ("0,0 400x300", GetViewport().ToString());
209
210  generator.MoveMouseToInHost(gfx::Point(300, 150));
211  EXPECT_EQ("150,75", env->last_mouse_location().ToString());
212  EXPECT_EQ("0,0 400x300", GetViewport().ToString());
213
214  generator.MoveMouseToInHost(gfx::Point(700, 150));
215  EXPECT_EQ("350,75", env->last_mouse_location().ToString());
216  EXPECT_EQ("0,0 400x300", GetViewport().ToString());
217
218  generator.MoveMouseToInHost(gfx::Point(701, 150));
219  EXPECT_EQ("350,75", env->last_mouse_location().ToString());
220  EXPECT_EQ("0,0 400x300", GetViewport().ToString());
221
222  generator.MoveMouseToInHost(gfx::Point(702, 150));
223  EXPECT_EQ("351,75", env->last_mouse_location().ToString());
224  EXPECT_EQ("1,0 400x300", GetViewport().ToString());
225
226  generator.MoveMouseToInHost(gfx::Point(703, 150));
227  EXPECT_EQ("352,75", env->last_mouse_location().ToString());
228  EXPECT_EQ("2,0 400x300", GetViewport().ToString());
229
230  generator.MoveMouseToInHost(gfx::Point(704, 150));
231  EXPECT_EQ("354,75", env->last_mouse_location().ToString());
232  EXPECT_EQ("4,0 400x300", GetViewport().ToString());
233
234  generator.MoveMouseToInHost(gfx::Point(712, 150));
235  EXPECT_EQ("360,75", env->last_mouse_location().ToString());
236  EXPECT_EQ("10,0 400x300", GetViewport().ToString());
237
238  generator.MoveMouseToInHost(gfx::Point(600, 150));
239  EXPECT_EQ("310,75", env->last_mouse_location().ToString());
240  EXPECT_EQ("10,0 400x300", GetViewport().ToString());
241
242  generator.MoveMouseToInHost(gfx::Point(720, 150));
243  EXPECT_EQ("370,75", env->last_mouse_location().ToString());
244  EXPECT_EQ("20,0 400x300", GetViewport().ToString());
245
246  generator.MoveMouseToInHost(gfx::Point(780, 150));
247  EXPECT_EQ("410,75", env->last_mouse_location().ToString());
248  EXPECT_EQ("410,75", CurrentPointOfInterest());
249  EXPECT_EQ("60,0 400x300", GetViewport().ToString());
250
251  generator.MoveMouseToInHost(gfx::Point(799, 150));
252  EXPECT_EQ("459,75", env->last_mouse_location().ToString());
253  EXPECT_EQ("109,0 400x300", GetViewport().ToString());
254
255  generator.MoveMouseToInHost(gfx::Point(702, 150));
256  EXPECT_EQ("460,75", env->last_mouse_location().ToString());
257  EXPECT_EQ("110,0 400x300", GetViewport().ToString());
258
259  generator.MoveMouseToInHost(gfx::Point(780, 150));
260  EXPECT_EQ("500,75", env->last_mouse_location().ToString());
261  EXPECT_EQ("150,0 400x300", GetViewport().ToString());
262
263  generator.MoveMouseToInHost(gfx::Point(780, 150));
264  EXPECT_EQ("540,75", env->last_mouse_location().ToString());
265  EXPECT_EQ("190,0 400x300", GetViewport().ToString());
266
267  generator.MoveMouseToInHost(gfx::Point(780, 150));
268  EXPECT_EQ("580,75", env->last_mouse_location().ToString());
269  EXPECT_EQ("230,0 400x300", GetViewport().ToString());
270
271  generator.MoveMouseToInHost(gfx::Point(780, 150));
272  EXPECT_EQ("620,75", env->last_mouse_location().ToString());
273  EXPECT_EQ("270,0 400x300", GetViewport().ToString());
274
275  generator.MoveMouseToInHost(gfx::Point(780, 150));
276  EXPECT_EQ("660,75", env->last_mouse_location().ToString());
277  EXPECT_EQ("310,0 400x300", GetViewport().ToString());
278
279  generator.MoveMouseToInHost(gfx::Point(780, 150));
280  EXPECT_EQ("700,75", env->last_mouse_location().ToString());
281  EXPECT_EQ("350,0 400x300", GetViewport().ToString());
282
283  generator.MoveMouseToInHost(gfx::Point(780, 150));
284  EXPECT_EQ("740,75", env->last_mouse_location().ToString());
285  EXPECT_EQ("390,0 400x300", GetViewport().ToString());
286
287  generator.MoveMouseToInHost(gfx::Point(780, 150));
288  EXPECT_EQ("780,75", env->last_mouse_location().ToString());
289  EXPECT_EQ("400,0 400x300", GetViewport().ToString());
290
291  generator.MoveMouseToInHost(gfx::Point(799, 150));
292  EXPECT_EQ("799,75", env->last_mouse_location().ToString());
293  EXPECT_EQ("400,0 400x300", GetViewport().ToString());
294}
295
296TEST_F(MagnificationControllerTest, PanWindow2xRightToLeft) {
297  const aura::Env* env = aura::Env::GetInstance();
298  aura::test::EventGenerator generator(Shell::GetPrimaryRootWindow());
299
300  generator.MoveMouseToInHost(gfx::Point(799, 300));
301  EXPECT_EQ(1.f, GetMagnificationController()->GetScale());
302  EXPECT_EQ("0,0 800x600", GetViewport().ToString());
303  EXPECT_EQ("799,300", env->last_mouse_location().ToString());
304
305  // Enables magnifier and confirm the viewport is at center.
306  GetMagnificationController()->SetEnabled(true);
307
308  generator.MoveMouseToInHost(gfx::Point(799, 300));
309  EXPECT_EQ("798,300", env->last_mouse_location().ToString());
310  EXPECT_EQ("400,150 400x300", GetViewport().ToString());
311
312  generator.MoveMouseToInHost(gfx::Point(0, 300));
313  EXPECT_EQ("400,300", env->last_mouse_location().ToString());
314  EXPECT_EQ("350,150 400x300", GetViewport().ToString());
315
316  generator.MoveMouseToInHost(gfx::Point(0, 300));
317  EXPECT_EQ("350,300", env->last_mouse_location().ToString());
318  EXPECT_EQ("300,150 400x300", GetViewport().ToString());
319
320  generator.MoveMouseToInHost(gfx::Point(0, 300));
321  EXPECT_EQ("300,300", env->last_mouse_location().ToString());
322  EXPECT_EQ("250,150 400x300", GetViewport().ToString());
323
324  generator.MoveMouseToInHost(gfx::Point(0, 300));
325  EXPECT_EQ("250,300", env->last_mouse_location().ToString());
326  EXPECT_EQ("200,150 400x300", GetViewport().ToString());
327
328  generator.MoveMouseToInHost(gfx::Point(0, 300));
329  EXPECT_EQ("200,300", env->last_mouse_location().ToString());
330  EXPECT_EQ("150,150 400x300", GetViewport().ToString());
331
332  generator.MoveMouseToInHost(gfx::Point(0, 300));
333  EXPECT_EQ("150,300", env->last_mouse_location().ToString());
334  EXPECT_EQ("100,150 400x300", GetViewport().ToString());
335
336  generator.MoveMouseToInHost(gfx::Point(0, 300));
337  EXPECT_EQ("100,300", env->last_mouse_location().ToString());
338  EXPECT_EQ("50,150 400x300", GetViewport().ToString());
339
340  generator.MoveMouseToInHost(gfx::Point(0, 300));
341  EXPECT_EQ("50,300", env->last_mouse_location().ToString());
342  EXPECT_EQ("0,150 400x300", GetViewport().ToString());
343
344  generator.MoveMouseToInHost(gfx::Point(0, 300));
345  EXPECT_EQ("0,300", env->last_mouse_location().ToString());
346  EXPECT_EQ("0,150 400x300", GetViewport().ToString());
347}
348
349TEST_F(MagnificationControllerTest, PanWindowToRight) {
350  const aura::Env* env = aura::Env::GetInstance();
351  aura::test::EventGenerator generator(Shell::GetPrimaryRootWindow());
352
353  generator.MoveMouseToInHost(gfx::Point(400, 300));
354  EXPECT_EQ(1.f, GetMagnificationController()->GetScale());
355  EXPECT_EQ("0,0 800x600", GetViewport().ToString());
356  EXPECT_EQ("400,300", env->last_mouse_location().ToString());
357
358  float scale = 2.f;
359
360  // Enables magnifier and confirm the viewport is at center.
361  GetMagnificationController()->SetEnabled(true);
362  EXPECT_FLOAT_EQ(2.f, GetMagnificationController()->GetScale());
363
364  scale *= kMagnificationScaleFactor;
365  GetMagnificationController()->SetScale(scale, false);
366  EXPECT_FLOAT_EQ(2.3784142, GetMagnificationController()->GetScale());
367  generator.MoveMouseToInHost(gfx::Point(400, 300));
368  EXPECT_EQ("400,300", env->last_mouse_location().ToString());
369  generator.MoveMouseToInHost(gfx::Point(799, 300));
370  EXPECT_EQ("566,299", env->last_mouse_location().ToString());
371  EXPECT_EQ("705,300", GetHostMouseLocation());
372
373  scale *= kMagnificationScaleFactor;
374  GetMagnificationController()->SetScale(scale, false);
375  EXPECT_FLOAT_EQ(2.8284268, GetMagnificationController()->GetScale());
376  generator.MoveMouseToInHost(gfx::Point(799, 300));
377  EXPECT_EQ("599,299", env->last_mouse_location().ToString());
378  EXPECT_EQ("702,300", GetHostMouseLocation());
379
380  scale *= kMagnificationScaleFactor;
381  GetMagnificationController()->SetScale(scale, false);
382  EXPECT_FLOAT_EQ(3.3635852, GetMagnificationController()->GetScale());
383  generator.MoveMouseToInHost(gfx::Point(799, 300));
384  EXPECT_EQ("627,298", env->last_mouse_location().ToString());
385  EXPECT_EQ("707,300", GetHostMouseLocation());
386
387  scale *= kMagnificationScaleFactor;
388  GetMagnificationController()->SetScale(scale, false);
389  EXPECT_FLOAT_EQ(4.f, GetMagnificationController()->GetScale());
390  generator.MoveMouseToInHost(gfx::Point(799, 300));
391  EXPECT_EQ("649,298", env->last_mouse_location().ToString());
392  EXPECT_EQ("704,300", GetHostMouseLocation());
393}
394
395TEST_F(MagnificationControllerTest, PanWindowToLeft) {
396  const aura::Env* env = aura::Env::GetInstance();
397  aura::test::EventGenerator generator(Shell::GetPrimaryRootWindow());
398
399  generator.MoveMouseToInHost(gfx::Point(400, 300));
400  EXPECT_EQ(1.f, GetMagnificationController()->GetScale());
401  EXPECT_EQ("0,0 800x600", GetViewport().ToString());
402  EXPECT_EQ("400,300", env->last_mouse_location().ToString());
403
404  float scale = 2.f;
405
406  // Enables magnifier and confirm the viewport is at center.
407  GetMagnificationController()->SetEnabled(true);
408  EXPECT_FLOAT_EQ(2.f, GetMagnificationController()->GetScale());
409
410  scale *= kMagnificationScaleFactor;
411  GetMagnificationController()->SetScale(scale, false);
412  EXPECT_FLOAT_EQ(2.3784142, GetMagnificationController()->GetScale());
413  generator.MoveMouseToInHost(gfx::Point(400, 300));
414  EXPECT_EQ("400,300", env->last_mouse_location().ToString());
415  generator.MoveMouseToInHost(gfx::Point(0, 300));
416  EXPECT_EQ("231,299", env->last_mouse_location().ToString());
417  EXPECT_EQ("100,300", GetHostMouseLocation());
418
419  scale *= kMagnificationScaleFactor;
420  GetMagnificationController()->SetScale(scale, false);
421  EXPECT_FLOAT_EQ(2.8284268, GetMagnificationController()->GetScale());
422  generator.MoveMouseToInHost(gfx::Point(0, 300));
423  EXPECT_EQ("195,299", env->last_mouse_location().ToString());
424  EXPECT_EQ("99,300", GetHostMouseLocation());
425
426  scale *= kMagnificationScaleFactor;
427  GetMagnificationController()->SetScale(scale, false);
428  EXPECT_FLOAT_EQ(3.3635852, GetMagnificationController()->GetScale());
429  generator.MoveMouseToInHost(gfx::Point(0, 300));
430  EXPECT_EQ("165,298", env->last_mouse_location().ToString());
431  EXPECT_EQ("98,300", GetHostMouseLocation());
432
433  scale *= kMagnificationScaleFactor;
434  GetMagnificationController()->SetScale(scale, false);
435  EXPECT_FLOAT_EQ(4.f, GetMagnificationController()->GetScale());
436  generator.MoveMouseToInHost(gfx::Point(0, 300));
437  EXPECT_EQ("140,298", env->last_mouse_location().ToString());
438  EXPECT_EQ("100,300", GetHostMouseLocation());
439}
440
441}  // namespace internal
442}  // namespace ash
443