Update ServiceWorkerProviderHost when the pending version changes
[chromium-blink-merge.git] / ash / magnifier / magnification_controller_unittest.cc
blob72edf410180b2d4d63d3bff1d4ddada2ce4c6091
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"
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"
18 namespace ash {
19 namespace {
21 const int kRootHeight = 600;
22 const int kRootWidth = 800;
24 } // namespace
26 class MagnificationControllerTest: public test::AshTestBase {
27 public:
28 MagnificationControllerTest() {}
29 virtual ~MagnificationControllerTest() {}
31 virtual void SetUp() OVERRIDE {
32 AshTestBase::SetUp();
33 UpdateDisplay(base::StringPrintf("%dx%d", kRootWidth, kRootHeight));
35 aura::Window* root = GetRootWindow();
36 gfx::Rect root_bounds(root->bounds());
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
46 virtual void TearDown() OVERRIDE {
47 AshTestBase::TearDown();
50 protected:
51 aura::Window* GetRootWindow() const {
52 return Shell::GetPrimaryRootWindow();
55 std::string GetHostMouseLocation() {
56 gfx::Point point;
57 GetRootWindow()->GetHost()->QueryMouseLocation(&point);
58 return point.ToString();
61 ash::MagnificationController* GetMagnificationController() const {
62 return ash::Shell::GetInstance()->magnification_controller();
65 gfx::Rect GetViewport() const {
66 gfx::RectF bounds(0, 0, kRootWidth, kRootHeight);
67 GetRootWindow()->layer()->transform().TransformRectReverse(&bounds);
68 return gfx::ToEnclosingRect(bounds);
71 std::string CurrentPointOfInterest() const {
72 return GetMagnificationController()->
73 GetPointOfInterestForTesting().ToString();
76 private:
77 DISALLOW_COPY_AND_ASSIGN(MagnificationControllerTest);
80 TEST_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());
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());
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());
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());
105 TEST_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());
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());
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());
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());
130 TEST_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());
136 // Move the viewport.
137 GetMagnificationController()->MoveWindow(0, 0, false);
138 EXPECT_EQ("0,0 400x300", GetViewport().ToString());
140 GetMagnificationController()->MoveWindow(200, 300, false);
141 EXPECT_EQ("200,300 400x300", GetViewport().ToString());
143 GetMagnificationController()->MoveWindow(400, 0, false);
144 EXPECT_EQ("400,0 400x300", GetViewport().ToString());
146 GetMagnificationController()->MoveWindow(400, 300, false);
147 EXPECT_EQ("400,300 400x300", GetViewport().ToString());
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());
153 GetMagnificationController()->MoveWindow(0, -100, false);
154 EXPECT_EQ("0,0 400x300", GetViewport().ToString());
156 GetMagnificationController()->MoveWindow(-100, -100, false);
157 EXPECT_EQ("0,0 400x300", GetViewport().ToString());
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());
163 GetMagnificationController()->MoveWindow(0, 400, false);
164 EXPECT_EQ("0,300 400x300", GetViewport().ToString());
166 GetMagnificationController()->MoveWindow(200, 400, false);
167 EXPECT_EQ("200,300 400x300", GetViewport().ToString());
169 GetMagnificationController()->MoveWindow(1000, 1000, false);
170 EXPECT_EQ("400,300 400x300", GetViewport().ToString());
173 TEST_F(MagnificationControllerTest, PointOfInterest) {
174 aura::test::EventGenerator generator(Shell::GetPrimaryRootWindow());
176 generator.MoveMouseToInHost(gfx::Point(0, 0));
177 EXPECT_EQ("0,0", CurrentPointOfInterest());
179 generator.MoveMouseToInHost(gfx::Point(799, 599));
180 EXPECT_EQ("799,599", CurrentPointOfInterest());
182 generator.MoveMouseToInHost(gfx::Point(400, 300));
183 EXPECT_EQ("400,300", CurrentPointOfInterest());
185 GetMagnificationController()->SetEnabled(true);
186 EXPECT_EQ("400,300", CurrentPointOfInterest());
188 generator.MoveMouseToInHost(gfx::Point(500, 400));
189 EXPECT_EQ("450,350", CurrentPointOfInterest());
192 TEST_F(MagnificationControllerTest, PanWindow2xLeftToRight) {
193 const aura::Env* env = aura::Env::GetInstance();
194 aura::test::EventGenerator generator(Shell::GetPrimaryRootWindow());
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());
201 // Enables magnifier and confirm the viewport is at center.
202 GetMagnificationController()->SetEnabled(true);
203 EXPECT_EQ(2.0f, GetMagnificationController()->GetScale());
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());
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());
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());
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());
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());
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());
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());
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());
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());
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());
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());
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());
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());
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());
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());
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());
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());
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());
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());
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());
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());
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());
296 TEST_F(MagnificationControllerTest, PanWindow2xRightToLeft) {
297 const aura::Env* env = aura::Env::GetInstance();
298 aura::test::EventGenerator generator(Shell::GetPrimaryRootWindow());
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());
305 // Enables magnifier and confirm the viewport is at center.
306 GetMagnificationController()->SetEnabled(true);
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());
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());
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());
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());
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());
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());
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());
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());
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());
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());
349 TEST_F(MagnificationControllerTest, PanWindowToRight) {
350 const aura::Env* env = aura::Env::GetInstance();
351 aura::test::EventGenerator generator(Shell::GetPrimaryRootWindow());
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());
358 float scale = 2.f;
360 // Enables magnifier and confirm the viewport is at center.
361 GetMagnificationController()->SetEnabled(true);
362 EXPECT_FLOAT_EQ(2.f, GetMagnificationController()->GetScale());
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());
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());
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());
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());
395 TEST_F(MagnificationControllerTest, PanWindowToLeft) {
396 const aura::Env* env = aura::Env::GetInstance();
397 aura::test::EventGenerator generator(Shell::GetPrimaryRootWindow());
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());
404 float scale = 2.f;
406 // Enables magnifier and confirm the viewport is at center.
407 GetMagnificationController()->SetEnabled(true);
408 EXPECT_FLOAT_EQ(2.f, GetMagnificationController()->GetScale());
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());
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("194,299", env->last_mouse_location().ToString());
424 EXPECT_EQ("99,300", GetHostMouseLocation());
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("164,298", env->last_mouse_location().ToString());
431 EXPECT_EQ("98,300", GetHostMouseLocation());
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("139,298", env->last_mouse_location().ToString());
438 EXPECT_EQ("100,300", GetHostMouseLocation());
441 } // namespace ash