cygprofile: increase timeouts to allow showing web contents
[chromium-blink-merge.git] / ash / magnifier / magnification_controller_unittest.cc
blobfcd7381a8d4c4d947ee8480540f5f8c0ebb0dbed
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/display/display_manager.h"
8 #include "ash/shell.h"
9 #include "ash/test/ash_test_base.h"
10 #include "base/strings/stringprintf.h"
11 #include "ui/aura/env.h"
12 #include "ui/aura/test/aura_test_utils.h"
13 #include "ui/aura/window_tree_host.h"
14 #include "ui/base/ime/input_method.h"
15 #include "ui/chromeos/accessibility_types.h"
16 #include "ui/events/test/event_generator.h"
17 #include "ui/gfx/geometry/rect_conversions.h"
18 #include "ui/gfx/screen.h"
19 #include "ui/views/controls/textfield/textfield.h"
20 #include "ui/views/layout/fill_layout.h"
21 #include "ui/views/widget/widget.h"
22 #include "ui/views/widget/widget_delegate.h"
23 #include "ui/wm/core/coordinate_conversion.h"
25 namespace ash {
26 namespace {
28 const int kRootHeight = 600;
29 const int kRootWidth = 800;
31 const int kTextInputWindowWidth = 50;
32 const int kTextInputWindowHeight = 50;
34 class TextInputView : public views::WidgetDelegateView {
35 public:
36 TextInputView() : text_field_(new views::Textfield) {
37 text_field_->SetTextInputType(ui::TEXT_INPUT_TYPE_TEXT);
38 AddChildView(text_field_);
39 SetLayoutManager(new views::FillLayout);
42 ~TextInputView() override {}
44 gfx::Size GetPreferredSize() const override {
45 return gfx::Size(kTextInputWindowWidth, kTextInputWindowHeight);
48 // Overridden from views::WidgetDelegate:
49 views::View* GetContentsView() override { return this; }
51 void FocusOnTextInput() { GetFocusManager()->SetFocusedView(text_field_); }
53 private:
54 views::Textfield* text_field_; // owned by views hierarchy
56 DISALLOW_COPY_AND_ASSIGN(TextInputView);
59 } // namespace
61 class MagnificationControllerTest: public test::AshTestBase {
62 public:
63 MagnificationControllerTest() : text_input_view_(NULL) {}
64 ~MagnificationControllerTest() override {}
66 void SetUp() override {
67 AshTestBase::SetUp();
68 UpdateDisplay(base::StringPrintf("%dx%d", kRootWidth, kRootHeight));
70 aura::Window* root = GetRootWindow();
71 gfx::Rect root_bounds(root->bounds());
73 #if defined(OS_WIN)
74 // RootWindow and Display can't resize on Windows Ash.
75 // http://crbug.com/165962
76 EXPECT_EQ(kRootHeight, root_bounds.height());
77 EXPECT_EQ(kRootWidth, root_bounds.width());
78 #endif
80 GetMagnificationController()->DisableMoveMagnifierDelayForTesting();
83 void TearDown() override { AshTestBase::TearDown(); }
85 protected:
86 aura::Window* GetRootWindow() const {
87 return Shell::GetPrimaryRootWindow();
90 std::string GetHostMouseLocation() {
91 const gfx::Point& location =
92 aura::test::QueryLatestMousePositionRequestInHost(
93 GetRootWindow()->GetHost());
94 return location.ToString();
97 ash::MagnificationController* GetMagnificationController() const {
98 return ash::Shell::GetInstance()->magnification_controller();
101 gfx::Rect GetViewport() const {
102 gfx::RectF bounds(0, 0, kRootWidth, kRootHeight);
103 GetRootWindow()->layer()->transform().TransformRectReverse(&bounds);
104 return gfx::ToEnclosingRect(bounds);
107 std::string CurrentPointOfInterest() const {
108 return GetMagnificationController()->
109 GetPointOfInterestForTesting().ToString();
112 void CreateAndShowTextInputView(const gfx::Rect& bounds) {
113 text_input_view_ = new TextInputView;
114 views::Widget* widget = views::Widget::CreateWindowWithContextAndBounds(
115 text_input_view_, GetRootWindow(), bounds);
116 widget->Show();
119 // Returns the text input view's bounds in root window coordinates.
120 gfx::Rect GetTextInputViewBounds() {
121 DCHECK(text_input_view_);
122 gfx::Rect bounds = text_input_view_->bounds();
123 gfx::Point origin = bounds.origin();
124 // Convert origin to screen coordinates.
125 views::View::ConvertPointToScreen(text_input_view_, &origin);
126 // Convert origin to root_window_ coordinates.
127 wm::ConvertPointFromScreen(GetRootWindow(), &origin);
128 return gfx::Rect(origin.x(), origin.y(), bounds.width(), bounds.height());
131 // Returns the caret bounds in root window coordinates.
132 gfx::Rect GetCaretBounds() {
133 gfx::Rect caret_bounds =
134 GetInputMethod()->GetTextInputClient()->GetCaretBounds();
135 gfx::Point origin = caret_bounds.origin();
136 wm::ConvertPointFromScreen(GetRootWindow(), &origin);
137 return gfx::Rect(
138 origin.x(), origin.y(), caret_bounds.width(), caret_bounds.height());
141 void FocusOnTextInputView() {
142 DCHECK(text_input_view_);
143 text_input_view_->FocusOnTextInput();
146 private:
147 TextInputView* text_input_view_;
149 ui::InputMethod* GetInputMethod() {
150 DCHECK(text_input_view_);
151 return text_input_view_->GetWidget()->GetInputMethod();
154 DISALLOW_COPY_AND_ASSIGN(MagnificationControllerTest);
157 TEST_F(MagnificationControllerTest, EnableAndDisable) {
158 // Confirms the magnifier is disabled.
159 EXPECT_TRUE(GetRootWindow()->layer()->transform().IsIdentity());
160 EXPECT_EQ(1.0f, GetMagnificationController()->GetScale());
161 EXPECT_EQ("0,0 800x600", GetViewport().ToString());
163 // Enables magnifier.
164 GetMagnificationController()->SetEnabled(true);
165 EXPECT_FALSE(GetRootWindow()->layer()->transform().IsIdentity());
166 EXPECT_EQ(2.0f, GetMagnificationController()->GetScale());
167 EXPECT_EQ("200,150 400x300", GetViewport().ToString());
169 // Disables magnifier.
170 GetMagnificationController()->SetEnabled(false);
171 EXPECT_TRUE(GetRootWindow()->layer()->transform().IsIdentity());
172 EXPECT_EQ(1.0f, GetMagnificationController()->GetScale());
173 EXPECT_EQ("0,0 800x600", GetViewport().ToString());
175 // Confirms the the scale can't be changed.
176 GetMagnificationController()->SetScale(4.0f, false);
177 EXPECT_TRUE(GetRootWindow()->layer()->transform().IsIdentity());
178 EXPECT_EQ(1.0f, GetMagnificationController()->GetScale());
179 EXPECT_EQ("0,0 800x600", GetViewport().ToString());
182 TEST_F(MagnificationControllerTest, MagnifyAndUnmagnify) {
183 // Enables magnifier and confirms the default scale is 2.0x.
184 GetMagnificationController()->SetEnabled(true);
185 EXPECT_FALSE(GetRootWindow()->layer()->transform().IsIdentity());
186 EXPECT_EQ(2.0f, GetMagnificationController()->GetScale());
187 EXPECT_EQ("200,150 400x300", GetViewport().ToString());
188 EXPECT_EQ("400,300", CurrentPointOfInterest());
190 // Changes the scale.
191 GetMagnificationController()->SetScale(4.0f, false);
192 EXPECT_EQ(4.0f, GetMagnificationController()->GetScale());
193 EXPECT_EQ("300,225 200x150", GetViewport().ToString());
194 EXPECT_EQ("400,300", CurrentPointOfInterest());
196 GetMagnificationController()->SetScale(1.0f, false);
197 EXPECT_EQ(1.0f, GetMagnificationController()->GetScale());
198 EXPECT_EQ("0,0 800x600", GetViewport().ToString());
199 EXPECT_EQ("400,300", CurrentPointOfInterest());
201 GetMagnificationController()->SetScale(3.0f, false);
202 EXPECT_EQ(3.0f, GetMagnificationController()->GetScale());
203 EXPECT_EQ("266,200 267x200", GetViewport().ToString());
204 EXPECT_EQ("400,300", CurrentPointOfInterest());
207 TEST_F(MagnificationControllerTest, MoveWindow) {
208 // Enables magnifier and confirm the viewport is at center.
209 GetMagnificationController()->SetEnabled(true);
210 EXPECT_EQ(2.0f, GetMagnificationController()->GetScale());
211 EXPECT_EQ("200,150 400x300", GetViewport().ToString());
213 // Move the viewport.
214 GetMagnificationController()->MoveWindow(0, 0, false);
215 EXPECT_EQ("0,0 400x300", GetViewport().ToString());
217 GetMagnificationController()->MoveWindow(200, 300, false);
218 EXPECT_EQ("200,300 400x300", GetViewport().ToString());
220 GetMagnificationController()->MoveWindow(400, 0, false);
221 EXPECT_EQ("400,0 400x300", GetViewport().ToString());
223 GetMagnificationController()->MoveWindow(400, 300, false);
224 EXPECT_EQ("400,300 400x300", GetViewport().ToString());
226 // Confirms that the viewport can't across the top-left border.
227 GetMagnificationController()->MoveWindow(-100, 0, false);
228 EXPECT_EQ("0,0 400x300", GetViewport().ToString());
230 GetMagnificationController()->MoveWindow(0, -100, false);
231 EXPECT_EQ("0,0 400x300", GetViewport().ToString());
233 GetMagnificationController()->MoveWindow(-100, -100, false);
234 EXPECT_EQ("0,0 400x300", GetViewport().ToString());
236 // Confirms that the viewport can't across the bittom-right border.
237 GetMagnificationController()->MoveWindow(800, 0, false);
238 EXPECT_EQ("400,0 400x300", GetViewport().ToString());
240 GetMagnificationController()->MoveWindow(0, 400, false);
241 EXPECT_EQ("0,300 400x300", GetViewport().ToString());
243 GetMagnificationController()->MoveWindow(200, 400, false);
244 EXPECT_EQ("200,300 400x300", GetViewport().ToString());
246 GetMagnificationController()->MoveWindow(1000, 1000, false);
247 EXPECT_EQ("400,300 400x300", GetViewport().ToString());
250 TEST_F(MagnificationControllerTest, PointOfInterest) {
251 ui::test::EventGenerator generator(Shell::GetPrimaryRootWindow());
253 generator.MoveMouseToInHost(gfx::Point(0, 0));
254 EXPECT_EQ("0,0", CurrentPointOfInterest());
256 generator.MoveMouseToInHost(gfx::Point(799, 599));
257 EXPECT_EQ("799,599", CurrentPointOfInterest());
259 generator.MoveMouseToInHost(gfx::Point(400, 300));
260 EXPECT_EQ("400,300", CurrentPointOfInterest());
262 GetMagnificationController()->SetEnabled(true);
263 EXPECT_EQ("400,300", CurrentPointOfInterest());
265 generator.MoveMouseToInHost(gfx::Point(500, 400));
266 EXPECT_EQ("450,350", CurrentPointOfInterest());
269 TEST_F(MagnificationControllerTest, PanWindow2xLeftToRight) {
270 const aura::Env* env = aura::Env::GetInstance();
271 ui::test::EventGenerator generator(Shell::GetPrimaryRootWindow());
273 generator.MoveMouseToInHost(gfx::Point(0, 0));
274 EXPECT_EQ(1.f, GetMagnificationController()->GetScale());
275 EXPECT_EQ("0,0 800x600", GetViewport().ToString());
276 EXPECT_EQ("0,0", env->last_mouse_location().ToString());
278 // Enables magnifier and confirm the viewport is at center.
279 GetMagnificationController()->SetEnabled(true);
280 EXPECT_EQ(2.0f, GetMagnificationController()->GetScale());
282 GetMagnificationController()->MoveWindow(0, 0, false);
283 generator.MoveMouseToInHost(gfx::Point(0, 0));
284 EXPECT_EQ("0,0", env->last_mouse_location().ToString());
285 EXPECT_EQ("0,0 400x300", GetViewport().ToString());
287 generator.MoveMouseToInHost(gfx::Point(300, 150));
288 EXPECT_EQ("150,75", env->last_mouse_location().ToString());
289 EXPECT_EQ("0,0 400x300", GetViewport().ToString());
291 generator.MoveMouseToInHost(gfx::Point(700, 150));
292 EXPECT_EQ("350,75", env->last_mouse_location().ToString());
293 EXPECT_EQ("0,0 400x300", GetViewport().ToString());
295 generator.MoveMouseToInHost(gfx::Point(701, 150));
296 EXPECT_EQ("350,75", env->last_mouse_location().ToString());
297 EXPECT_EQ("0,0 400x300", GetViewport().ToString());
299 generator.MoveMouseToInHost(gfx::Point(702, 150));
300 EXPECT_EQ("351,75", env->last_mouse_location().ToString());
301 EXPECT_EQ("1,0 400x300", GetViewport().ToString());
303 generator.MoveMouseToInHost(gfx::Point(703, 150));
304 EXPECT_EQ("352,75", env->last_mouse_location().ToString());
305 EXPECT_EQ("2,0 400x300", GetViewport().ToString());
307 generator.MoveMouseToInHost(gfx::Point(704, 150));
308 EXPECT_EQ("354,75", env->last_mouse_location().ToString());
309 EXPECT_EQ("4,0 400x300", GetViewport().ToString());
311 generator.MoveMouseToInHost(gfx::Point(712, 150));
312 EXPECT_EQ("360,75", env->last_mouse_location().ToString());
313 EXPECT_EQ("10,0 400x300", GetViewport().ToString());
315 generator.MoveMouseToInHost(gfx::Point(600, 150));
316 EXPECT_EQ("310,75", env->last_mouse_location().ToString());
317 EXPECT_EQ("10,0 400x300", GetViewport().ToString());
319 generator.MoveMouseToInHost(gfx::Point(720, 150));
320 EXPECT_EQ("370,75", env->last_mouse_location().ToString());
321 EXPECT_EQ("20,0 400x300", GetViewport().ToString());
323 generator.MoveMouseToInHost(gfx::Point(780, 150));
324 EXPECT_EQ("410,75", env->last_mouse_location().ToString());
325 EXPECT_EQ("410,75", CurrentPointOfInterest());
326 EXPECT_EQ("60,0 400x300", GetViewport().ToString());
328 generator.MoveMouseToInHost(gfx::Point(799, 150));
329 EXPECT_EQ("459,75", env->last_mouse_location().ToString());
330 EXPECT_EQ("109,0 400x300", GetViewport().ToString());
332 generator.MoveMouseToInHost(gfx::Point(702, 150));
333 EXPECT_EQ("460,75", env->last_mouse_location().ToString());
334 EXPECT_EQ("110,0 400x300", GetViewport().ToString());
336 generator.MoveMouseToInHost(gfx::Point(780, 150));
337 EXPECT_EQ("500,75", env->last_mouse_location().ToString());
338 EXPECT_EQ("150,0 400x300", GetViewport().ToString());
340 generator.MoveMouseToInHost(gfx::Point(780, 150));
341 EXPECT_EQ("540,75", env->last_mouse_location().ToString());
342 EXPECT_EQ("190,0 400x300", GetViewport().ToString());
344 generator.MoveMouseToInHost(gfx::Point(780, 150));
345 EXPECT_EQ("580,75", env->last_mouse_location().ToString());
346 EXPECT_EQ("230,0 400x300", GetViewport().ToString());
348 generator.MoveMouseToInHost(gfx::Point(780, 150));
349 EXPECT_EQ("620,75", env->last_mouse_location().ToString());
350 EXPECT_EQ("270,0 400x300", GetViewport().ToString());
352 generator.MoveMouseToInHost(gfx::Point(780, 150));
353 EXPECT_EQ("660,75", env->last_mouse_location().ToString());
354 EXPECT_EQ("310,0 400x300", GetViewport().ToString());
356 generator.MoveMouseToInHost(gfx::Point(780, 150));
357 EXPECT_EQ("700,75", env->last_mouse_location().ToString());
358 EXPECT_EQ("350,0 400x300", GetViewport().ToString());
360 generator.MoveMouseToInHost(gfx::Point(780, 150));
361 EXPECT_EQ("740,75", env->last_mouse_location().ToString());
362 EXPECT_EQ("390,0 400x300", GetViewport().ToString());
364 generator.MoveMouseToInHost(gfx::Point(780, 150));
365 EXPECT_EQ("780,75", env->last_mouse_location().ToString());
366 EXPECT_EQ("400,0 400x300", GetViewport().ToString());
368 generator.MoveMouseToInHost(gfx::Point(799, 150));
369 EXPECT_EQ("799,75", env->last_mouse_location().ToString());
370 EXPECT_EQ("400,0 400x300", GetViewport().ToString());
373 TEST_F(MagnificationControllerTest, PanWindow2xRightToLeft) {
374 const aura::Env* env = aura::Env::GetInstance();
375 ui::test::EventGenerator generator(Shell::GetPrimaryRootWindow());
377 generator.MoveMouseToInHost(gfx::Point(799, 300));
378 EXPECT_EQ(1.f, GetMagnificationController()->GetScale());
379 EXPECT_EQ("0,0 800x600", GetViewport().ToString());
380 EXPECT_EQ("799,300", env->last_mouse_location().ToString());
382 // Enables magnifier and confirm the viewport is at center.
383 GetMagnificationController()->SetEnabled(true);
385 generator.MoveMouseToInHost(gfx::Point(799, 300));
386 EXPECT_EQ("798,300", env->last_mouse_location().ToString());
387 EXPECT_EQ("400,150 400x300", GetViewport().ToString());
389 generator.MoveMouseToInHost(gfx::Point(0, 300));
390 EXPECT_EQ("400,300", env->last_mouse_location().ToString());
391 EXPECT_EQ("350,150 400x300", GetViewport().ToString());
393 generator.MoveMouseToInHost(gfx::Point(0, 300));
394 EXPECT_EQ("350,300", env->last_mouse_location().ToString());
395 EXPECT_EQ("300,150 400x300", GetViewport().ToString());
397 generator.MoveMouseToInHost(gfx::Point(0, 300));
398 EXPECT_EQ("300,300", env->last_mouse_location().ToString());
399 EXPECT_EQ("250,150 400x300", GetViewport().ToString());
401 generator.MoveMouseToInHost(gfx::Point(0, 300));
402 EXPECT_EQ("250,300", env->last_mouse_location().ToString());
403 EXPECT_EQ("200,150 400x300", GetViewport().ToString());
405 generator.MoveMouseToInHost(gfx::Point(0, 300));
406 EXPECT_EQ("200,300", env->last_mouse_location().ToString());
407 EXPECT_EQ("150,150 400x300", GetViewport().ToString());
409 generator.MoveMouseToInHost(gfx::Point(0, 300));
410 EXPECT_EQ("150,300", env->last_mouse_location().ToString());
411 EXPECT_EQ("100,150 400x300", GetViewport().ToString());
413 generator.MoveMouseToInHost(gfx::Point(0, 300));
414 EXPECT_EQ("100,300", env->last_mouse_location().ToString());
415 EXPECT_EQ("50,150 400x300", GetViewport().ToString());
417 generator.MoveMouseToInHost(gfx::Point(0, 300));
418 EXPECT_EQ("50,300", env->last_mouse_location().ToString());
419 EXPECT_EQ("0,150 400x300", GetViewport().ToString());
421 generator.MoveMouseToInHost(gfx::Point(0, 300));
422 EXPECT_EQ("0,300", env->last_mouse_location().ToString());
423 EXPECT_EQ("0,150 400x300", GetViewport().ToString());
426 TEST_F(MagnificationControllerTest, PanWindowToRight) {
427 const aura::Env* env = aura::Env::GetInstance();
428 ui::test::EventGenerator generator(Shell::GetPrimaryRootWindow());
430 generator.MoveMouseToInHost(gfx::Point(400, 300));
431 EXPECT_EQ(1.f, GetMagnificationController()->GetScale());
432 EXPECT_EQ("0,0 800x600", GetViewport().ToString());
433 EXPECT_EQ("400,300", env->last_mouse_location().ToString());
435 float scale = 2.f;
437 // Enables magnifier and confirm the viewport is at center.
438 GetMagnificationController()->SetEnabled(true);
439 EXPECT_FLOAT_EQ(2.f, GetMagnificationController()->GetScale());
441 scale *= ui::kMagnificationScaleFactor;
442 GetMagnificationController()->SetScale(scale, false);
443 EXPECT_FLOAT_EQ(2.3784142, GetMagnificationController()->GetScale());
444 generator.MoveMouseToInHost(gfx::Point(400, 300));
445 EXPECT_EQ("400,300", env->last_mouse_location().ToString());
446 generator.MoveMouseToInHost(gfx::Point(799, 300));
447 EXPECT_EQ("566,299", env->last_mouse_location().ToString());
448 EXPECT_EQ("705,300", GetHostMouseLocation());
450 scale *= ui::kMagnificationScaleFactor;
451 GetMagnificationController()->SetScale(scale, false);
452 EXPECT_FLOAT_EQ(2.8284268, GetMagnificationController()->GetScale());
453 generator.MoveMouseToInHost(gfx::Point(799, 300));
454 EXPECT_EQ("599,299", env->last_mouse_location().ToString());
455 EXPECT_EQ("702,300", GetHostMouseLocation());
457 scale *= ui::kMagnificationScaleFactor;
458 GetMagnificationController()->SetScale(scale, false);
459 EXPECT_FLOAT_EQ(3.3635852, GetMagnificationController()->GetScale());
460 generator.MoveMouseToInHost(gfx::Point(799, 300));
461 EXPECT_EQ("627,298", env->last_mouse_location().ToString());
462 EXPECT_EQ("707,300", GetHostMouseLocation());
464 scale *= ui::kMagnificationScaleFactor;
465 GetMagnificationController()->SetScale(scale, false);
466 EXPECT_FLOAT_EQ(4.f, GetMagnificationController()->GetScale());
467 generator.MoveMouseToInHost(gfx::Point(799, 300));
468 EXPECT_EQ("649,298", env->last_mouse_location().ToString());
469 EXPECT_EQ("704,300", GetHostMouseLocation());
472 TEST_F(MagnificationControllerTest, PanWindowToLeft) {
473 const aura::Env* env = aura::Env::GetInstance();
474 ui::test::EventGenerator generator(Shell::GetPrimaryRootWindow());
476 generator.MoveMouseToInHost(gfx::Point(400, 300));
477 EXPECT_EQ(1.f, GetMagnificationController()->GetScale());
478 EXPECT_EQ("0,0 800x600", GetViewport().ToString());
479 EXPECT_EQ("400,300", env->last_mouse_location().ToString());
481 float scale = 2.f;
483 // Enables magnifier and confirm the viewport is at center.
484 GetMagnificationController()->SetEnabled(true);
485 EXPECT_FLOAT_EQ(2.f, GetMagnificationController()->GetScale());
487 scale *= ui::kMagnificationScaleFactor;
488 GetMagnificationController()->SetScale(scale, false);
489 EXPECT_FLOAT_EQ(2.3784142, GetMagnificationController()->GetScale());
490 generator.MoveMouseToInHost(gfx::Point(400, 300));
491 EXPECT_EQ("400,300", env->last_mouse_location().ToString());
492 generator.MoveMouseToInHost(gfx::Point(0, 300));
493 EXPECT_EQ("231,299", env->last_mouse_location().ToString());
494 EXPECT_EQ("100,300", GetHostMouseLocation());
496 scale *= ui::kMagnificationScaleFactor;
497 GetMagnificationController()->SetScale(scale, false);
498 EXPECT_FLOAT_EQ(2.8284268, GetMagnificationController()->GetScale());
499 generator.MoveMouseToInHost(gfx::Point(0, 300));
500 EXPECT_EQ("194,299", env->last_mouse_location().ToString());
501 EXPECT_EQ("99,300", GetHostMouseLocation());
503 scale *= ui::kMagnificationScaleFactor;
504 GetMagnificationController()->SetScale(scale, false);
505 EXPECT_FLOAT_EQ(3.3635852, GetMagnificationController()->GetScale());
506 generator.MoveMouseToInHost(gfx::Point(0, 300));
507 EXPECT_EQ("164,298", env->last_mouse_location().ToString());
508 EXPECT_EQ("98,300", GetHostMouseLocation());
510 scale *= ui::kMagnificationScaleFactor;
511 GetMagnificationController()->SetScale(scale, false);
512 EXPECT_FLOAT_EQ(4.f, GetMagnificationController()->GetScale());
513 generator.MoveMouseToInHost(gfx::Point(0, 300));
514 EXPECT_EQ("139,298", env->last_mouse_location().ToString());
515 EXPECT_EQ("100,300", GetHostMouseLocation());
518 TEST_F(MagnificationControllerTest, FollowTextInputFieldFocus) {
519 CreateAndShowTextInputView(gfx::Rect(500, 300, 80, 80));
520 gfx::Rect text_input_bounds = GetTextInputViewBounds();
522 // Enables magnifier and confirm the viewport is at center.
523 GetMagnificationController()->SetEnabled(true);
524 EXPECT_EQ(2.0f, GetMagnificationController()->GetScale());
525 EXPECT_EQ("200,150 400x300", GetViewport().ToString());
526 EXPECT_FALSE(GetMagnificationController()->KeepFocusCentered());
528 // Move the viewport to (0, 0), so that text input field will be out of
529 // the viewport region.
530 GetMagnificationController()->MoveWindow(0, 0, false);
531 EXPECT_EQ("0,0 400x300", GetViewport().ToString());
532 EXPECT_FALSE(GetViewport().Intersects(text_input_bounds));
534 // Focus on the text input field.
535 FocusOnTextInputView();
537 // Verify the view port has been moved to the place where the text field is
538 // contained in the view port and the caret is at the center of the view port.
539 gfx::Rect view_port = GetViewport();
540 EXPECT_TRUE(view_port.Contains(text_input_bounds));
541 gfx::Rect caret_bounds = GetCaretBounds();
542 EXPECT_TRUE(text_input_bounds.Contains(caret_bounds));
543 EXPECT_EQ(caret_bounds.CenterPoint(), view_port.CenterPoint());
546 // Tests the following case. First the text input field intersects on the right
547 // edge with the view port, with focus caret sitting just a little left to the
548 // caret panning margin, so that when it gets focus, the view port won't move.
549 // Then when user types a character, the caret moves beyond the right panning
550 // edge, the view port will be moved to center the caret horizontally.
551 TEST_F(MagnificationControllerTest, FollowTextInputFieldKeyPress) {
552 const int kCaretPanningMargin = 50;
553 const int kScale = 2.0f;
554 const int kViewportWidth = 400;
555 // Add some extra distance horizontally from text caret to to left edge of
556 // the text input view.
557 int x = kViewportWidth - (kCaretPanningMargin + 20) / kScale;
558 CreateAndShowTextInputView(gfx::Rect(x, 200, 80, 80));
559 gfx::Rect text_input_bounds = GetTextInputViewBounds();
561 // Enables magnifier and confirm the viewport is at center.
562 GetMagnificationController()->SetEnabled(true);
563 EXPECT_EQ(2.0f, GetMagnificationController()->GetScale());
564 EXPECT_EQ("200,150 400x300", GetViewport().ToString());
565 EXPECT_FALSE(GetMagnificationController()->KeepFocusCentered());
567 // Move the viewport to (0, 0), so that text input field intersects the
568 // view port at the right edge.
569 GetMagnificationController()->MoveWindow(0, 0, false);
570 EXPECT_EQ("0,0 400x300", GetViewport().ToString());
571 EXPECT_TRUE(GetViewport().Intersects(text_input_bounds));
573 // Focus on the text input field.
574 FocusOnTextInputView();
576 // Verify the view port is not moved, and the caret is inside the view port
577 // and not beyond the caret right panning margin.
578 gfx::Rect view_port = GetViewport();
579 EXPECT_EQ("0,0 400x300", view_port.ToString());
580 EXPECT_TRUE(text_input_bounds.Contains(GetCaretBounds()));
581 EXPECT_GT(view_port.right() - kCaretPanningMargin / kScale,
582 GetCaretBounds().x());
584 // Press keys on text input simulate typing on text field and the caret
585 // moves beyond the caret right panning margin. The view port is moved to the
586 // place where caret's x coordinate is centered at the new view port.
587 ui::test::EventGenerator generator(Shell::GetPrimaryRootWindow());
588 generator.PressKey(ui::VKEY_A, 0);
589 generator.ReleaseKey(ui::VKEY_A, 0);
590 gfx::Rect caret_bounds = GetCaretBounds();
591 EXPECT_LT(view_port.right() - kCaretPanningMargin / kScale,
592 GetCaretBounds().x());
594 gfx::Rect new_view_port = GetViewport();
595 EXPECT_EQ(caret_bounds.CenterPoint().x(), new_view_port.CenterPoint().x());
598 TEST_F(MagnificationControllerTest, CenterTextCaretNotInsideViewport) {
599 CreateAndShowTextInputView(gfx::Rect(500, 300, 50, 30));
600 gfx::Rect text_input_bounds = GetTextInputViewBounds();
602 // Enables magnifier and confirm the viewport is at center.
603 GetMagnificationController()->SetKeepFocusCentered(true);
604 GetMagnificationController()->SetEnabled(true);
605 EXPECT_EQ(2.0f, GetMagnificationController()->GetScale());
606 EXPECT_EQ("200,150 400x300", GetViewport().ToString());
607 EXPECT_TRUE(GetMagnificationController()->KeepFocusCentered());
609 // Move the viewport to (0, 0), so that text input field will be out of
610 // the viewport region.
611 GetMagnificationController()->MoveWindow(0, 0, false);
612 EXPECT_EQ("0,0 400x300", GetViewport().ToString());
613 EXPECT_FALSE(GetViewport().Contains(text_input_bounds));
615 // Focus on the text input field.
616 FocusOnTextInputView();
617 RunAllPendingInMessageLoop();
618 // Verify the view port has been moved to the place where the text field is
619 // contained in the view port and the caret is at the center of the view port.
620 gfx::Rect view_port = GetViewport();
621 EXPECT_TRUE(view_port.Contains(text_input_bounds));
622 gfx::Rect caret_bounds = GetCaretBounds();
623 EXPECT_EQ(caret_bounds.CenterPoint(), view_port.CenterPoint());
625 // Press keys on text input simulate typing on text field and the view port
626 // should be moved to keep the caret centered.
627 ui::test::EventGenerator generator(Shell::GetPrimaryRootWindow());
628 generator.PressKey(ui::VKEY_A, 0);
629 generator.ReleaseKey(ui::VKEY_A, 0);
630 RunAllPendingInMessageLoop();
631 gfx::Rect new_caret_bounds = GetCaretBounds();
632 EXPECT_NE(caret_bounds, new_caret_bounds);
634 gfx::Rect new_view_port = GetViewport();
635 EXPECT_NE(view_port, new_view_port);
636 EXPECT_TRUE(new_view_port.Contains(new_caret_bounds));
637 EXPECT_EQ(new_caret_bounds.CenterPoint(), new_view_port.CenterPoint());
640 TEST_F(MagnificationControllerTest, CenterTextCaretInViewport) {
641 CreateAndShowTextInputView(gfx::Rect(250, 200, 50, 30));
642 gfx::Rect text_input_bounds = GetTextInputViewBounds();
644 // Enables magnifier and confirm the viewport is at center.
645 GetMagnificationController()->SetKeepFocusCentered(true);
646 GetMagnificationController()->SetEnabled(true);
647 EXPECT_EQ(2.0f, GetMagnificationController()->GetScale());
648 EXPECT_EQ("200,150 400x300", GetViewport().ToString());
649 EXPECT_TRUE(GetMagnificationController()->KeepFocusCentered());
651 // Verify the text input field is inside the view port.
652 gfx::Rect view_port = GetViewport();
653 EXPECT_TRUE(view_port.Contains(text_input_bounds));
655 // Focus on the text input field.
656 FocusOnTextInputView();
657 RunAllPendingInMessageLoop();
659 // Verify the view port has been moved to the place where the text field is
660 // contained in the view port and the caret is at the center of the view port.
661 gfx::Rect new_view_port = GetViewport();
662 EXPECT_NE(view_port, new_view_port);
663 EXPECT_TRUE(new_view_port.Contains(text_input_bounds));
664 gfx::Rect caret_bounds = GetCaretBounds();
665 EXPECT_EQ(caret_bounds.CenterPoint(), new_view_port.CenterPoint());
669 // Make sure that unified desktop can enter magnified mode.
670 TEST_F(MagnificationControllerTest, EnableMagnifierInUnifiedDesktop) {
671 if (!SupportsMultipleDisplays())
672 return;
673 Shell::GetInstance()->display_manager()->SetUnifiedDesktopEnabled(true);
675 EXPECT_EQ(1.0f, GetMagnificationController()->GetScale());
677 GetMagnificationController()->SetEnabled(true);
679 gfx::Screen* screen =
680 gfx::Screen::GetScreenFor(Shell::GetPrimaryRootWindow());
682 UpdateDisplay("500x500, 500x500");
683 EXPECT_EQ("0,0 1000x500", screen->GetPrimaryDisplay().bounds().ToString());
684 EXPECT_EQ(2.0f, GetMagnificationController()->GetScale());
686 GetMagnificationController()->SetEnabled(false);
688 EXPECT_EQ(1.0f, GetMagnificationController()->GetScale());
690 GetMagnificationController()->SetEnabled(true);
691 EXPECT_EQ(2.0f, GetMagnificationController()->GetScale());
693 UpdateDisplay("500x500");
694 EXPECT_EQ("0,0 500x500", screen->GetPrimaryDisplay().bounds().ToString());
695 EXPECT_EQ(2.0f, GetMagnificationController()->GetScale());
697 GetMagnificationController()->SetEnabled(false);
698 EXPECT_EQ("0,0 500x500", screen->GetPrimaryDisplay().bounds().ToString());
699 EXPECT_EQ(1.0f, GetMagnificationController()->GetScale());
702 } // namespace ash