5ad2d12b99a955c0f8d54c591d44da6a7191bcd4
[platform/framework/web/crosswalk.git] / src / ash / magnifier / magnification_controller_unittest.cc
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/event_generator.h"
14 #include "ui/aura/window_tree_host.h"
15 #include "ui/gfx/rect_conversions.h"
16 #include "ui/gfx/screen.h"
17
18 namespace ash {
19 namespace internal {
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:
29   MagnificationControllerTest() {}
30   virtual ~MagnificationControllerTest() {}
31
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
47   virtual void TearDown() OVERRIDE {
48     AshTestBase::TearDown();
49   }
50
51  protected:
52   aura::Window* GetRootWindow() const {
53     return Shell::GetPrimaryRootWindow();
54   }
55
56   std::string GetHostMouseLocation() {
57     gfx::Point point;
58     GetRootWindow()->GetHost()->QueryMouseLocation(&point);
59     return point.ToString();
60   }
61
62   ash::MagnificationController* GetMagnificationController() const {
63     return ash::Shell::GetInstance()->magnification_controller();
64   }
65
66   gfx::Rect GetViewport() const {
67     gfx::RectF bounds(0, 0, kRootWidth, kRootHeight);
68     GetRootWindow()->layer()->transform().TransformRectReverse(&bounds);
69     return gfx::ToEnclosingRect(bounds);
70   }
71
72   std::string CurrentPointOfInterest() const {
73     return GetMagnificationController()->
74         GetPointOfInterestForTesting().ToString();
75   }
76
77  private:
78   DISALLOW_COPY_AND_ASSIGN(MagnificationControllerTest);
79 };
80
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());
86
87   // Enables magnifier.
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());
92
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());
98
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());
104 }
105
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());
113
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());
119
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());
124
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());
129 }
130
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());
136
137   // Move the viewport.
138   GetMagnificationController()->MoveWindow(0, 0, false);
139   EXPECT_EQ("0,0 400x300", GetViewport().ToString());
140
141   GetMagnificationController()->MoveWindow(200, 300, false);
142   EXPECT_EQ("200,300 400x300", GetViewport().ToString());
143
144   GetMagnificationController()->MoveWindow(400, 0, false);
145   EXPECT_EQ("400,0 400x300", GetViewport().ToString());
146
147   GetMagnificationController()->MoveWindow(400, 300, false);
148   EXPECT_EQ("400,300 400x300", GetViewport().ToString());
149
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());
153
154   GetMagnificationController()->MoveWindow(0, -100, false);
155   EXPECT_EQ("0,0 400x300", GetViewport().ToString());
156
157   GetMagnificationController()->MoveWindow(-100, -100, false);
158   EXPECT_EQ("0,0 400x300", GetViewport().ToString());
159
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());
163
164   GetMagnificationController()->MoveWindow(0, 400, false);
165   EXPECT_EQ("0,300 400x300", GetViewport().ToString());
166
167   GetMagnificationController()->MoveWindow(200, 400, false);
168   EXPECT_EQ("200,300 400x300", GetViewport().ToString());
169
170   GetMagnificationController()->MoveWindow(1000, 1000, false);
171   EXPECT_EQ("400,300 400x300", GetViewport().ToString());
172 }
173
174 TEST_F(MagnificationControllerTest, PointOfInterest) {
175   aura::test::EventGenerator generator(Shell::GetPrimaryRootWindow());
176
177   generator.MoveMouseToInHost(gfx::Point(0, 0));
178   EXPECT_EQ("0,0", CurrentPointOfInterest());
179
180   generator.MoveMouseToInHost(gfx::Point(799, 599));
181   EXPECT_EQ("799,599", CurrentPointOfInterest());
182
183   generator.MoveMouseToInHost(gfx::Point(400, 300));
184   EXPECT_EQ("400,300", CurrentPointOfInterest());
185
186   GetMagnificationController()->SetEnabled(true);
187   EXPECT_EQ("400,300", CurrentPointOfInterest());
188
189   generator.MoveMouseToInHost(gfx::Point(500, 400));
190   EXPECT_EQ("450,350", CurrentPointOfInterest());
191 }
192
193 TEST_F(MagnificationControllerTest, PanWindow2xLeftToRight) {
194   const aura::Env* env = aura::Env::GetInstance();
195   aura::test::EventGenerator generator(Shell::GetPrimaryRootWindow());
196
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());
201
202   // Enables magnifier and confirm the viewport is at center.
203   GetMagnificationController()->SetEnabled(true);
204   EXPECT_EQ(2.0f, GetMagnificationController()->GetScale());
205
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());
210
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());
214
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());
218
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());
222
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());
226
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());
230
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());
234
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());
238
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());
242
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());
246
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());
251
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());
255
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());
259
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());
263
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());
267
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());
271
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());
275
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());
279
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());
283
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());
287
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());
291
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());
295 }
296
297 TEST_F(MagnificationControllerTest, PanWindow2xRightToLeft) {
298   const aura::Env* env = aura::Env::GetInstance();
299   aura::test::EventGenerator generator(Shell::GetPrimaryRootWindow());
300
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());
305
306   // Enables magnifier and confirm the viewport is at center.
307   GetMagnificationController()->SetEnabled(true);
308
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());
312
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());
316
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());
320
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());
324
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());
328
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());
332
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());
336
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());
340
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());
344
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());
348 }
349
350 TEST_F(MagnificationControllerTest, PanWindowToRight) {
351   const aura::Env* env = aura::Env::GetInstance();
352   aura::test::EventGenerator generator(Shell::GetPrimaryRootWindow());
353
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());
358
359   float scale = 2.f;
360
361   // Enables magnifier and confirm the viewport is at center.
362   GetMagnificationController()->SetEnabled(true);
363   EXPECT_FLOAT_EQ(2.f, GetMagnificationController()->GetScale());
364
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());
373
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());
380
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());
387
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());
394 }
395
396 TEST_F(MagnificationControllerTest, PanWindowToLeft) {
397   const aura::Env* env = aura::Env::GetInstance();
398   aura::test::EventGenerator generator(Shell::GetPrimaryRootWindow());
399
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());
404
405   float scale = 2.f;
406
407   // Enables magnifier and confirm the viewport is at center.
408   GetMagnificationController()->SetEnabled(true);
409   EXPECT_FLOAT_EQ(2.f, GetMagnificationController()->GetScale());
410
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());
419
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());
426
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());
433
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());
440 }
441
442 }  // namespace internal
443 }  // namespace ash