Add abhijeet.k@samsung.com to AUTHORS list.
[chromium-blink-merge.git] / ash / magnifier / magnification_controller_unittest.cc
blobf6425fffbef41490041bb0a42c5248d3f1e361c1
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/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/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
81 void TearDown() override { AshTestBase::TearDown(); }
83 protected:
84 aura::Window* GetRootWindow() const {
85 return Shell::GetPrimaryRootWindow();
88 std::string GetHostMouseLocation() {
89 const gfx::Point& location =
90 aura::test::QueryLatestMousePositionRequestInHost(
91 GetRootWindow()->GetHost());
92 return location.ToString();
95 ash::MagnificationController* GetMagnificationController() const {
96 return ash::Shell::GetInstance()->magnification_controller();
99 gfx::Rect GetViewport() const {
100 gfx::RectF bounds(0, 0, kRootWidth, kRootHeight);
101 GetRootWindow()->layer()->transform().TransformRectReverse(&bounds);
102 return gfx::ToEnclosingRect(bounds);
105 std::string CurrentPointOfInterest() const {
106 return GetMagnificationController()->
107 GetPointOfInterestForTesting().ToString();
110 void CreateAndShowTextInputView(const gfx::Rect& bounds) {
111 text_input_view_ = new TextInputView;
112 views::Widget* widget = views::Widget::CreateWindowWithContextAndBounds(
113 text_input_view_, GetRootWindow(), bounds);
114 widget->Show();
117 // Returns the text input view's bounds in root window coordinates.
118 gfx::Rect GetTextInputViewBounds() {
119 DCHECK(text_input_view_);
120 gfx::Rect bounds = text_input_view_->bounds();
121 gfx::Point origin = bounds.origin();
122 // Convert origin to screen coordinates.
123 views::View::ConvertPointToScreen(text_input_view_, &origin);
124 // Convert origin to root_window_ coordinates.
125 wm::ConvertPointFromScreen(GetRootWindow(), &origin);
126 return gfx::Rect(origin.x(), origin.y(), bounds.width(), bounds.height());
129 // Returns the caret bounds in root window coordinates.
130 gfx::Rect GetCaretBounds() {
131 gfx::Rect caret_bounds =
132 GetInputMethod()->GetTextInputClient()->GetCaretBounds();
133 gfx::Point origin = caret_bounds.origin();
134 wm::ConvertPointFromScreen(GetRootWindow(), &origin);
135 return gfx::Rect(
136 origin.x(), origin.y(), caret_bounds.width(), caret_bounds.height());
139 void FocusOnTextInputView() {
140 DCHECK(text_input_view_);
141 text_input_view_->FocusOnTextInput();
144 private:
145 TextInputView* text_input_view_;
147 ui::InputMethod* GetInputMethod() {
148 DCHECK(text_input_view_);
149 return text_input_view_->GetWidget()
150 ->GetNativeWindow()
151 ->GetRootWindow()
152 ->GetProperty(aura::client::kRootWindowInputMethodKey);
155 DISALLOW_COPY_AND_ASSIGN(MagnificationControllerTest);
158 TEST_F(MagnificationControllerTest, EnableAndDisable) {
159 // Confirms the magnifier is disabled.
160 EXPECT_TRUE(GetRootWindow()->layer()->transform().IsIdentity());
161 EXPECT_EQ(1.0f, GetMagnificationController()->GetScale());
162 EXPECT_EQ("0,0 800x600", GetViewport().ToString());
164 // Enables magnifier.
165 GetMagnificationController()->SetEnabled(true);
166 EXPECT_FALSE(GetRootWindow()->layer()->transform().IsIdentity());
167 EXPECT_EQ(2.0f, GetMagnificationController()->GetScale());
168 EXPECT_EQ("200,150 400x300", GetViewport().ToString());
170 // Disables magnifier.
171 GetMagnificationController()->SetEnabled(false);
172 EXPECT_TRUE(GetRootWindow()->layer()->transform().IsIdentity());
173 EXPECT_EQ(1.0f, GetMagnificationController()->GetScale());
174 EXPECT_EQ("0,0 800x600", GetViewport().ToString());
176 // Confirms the the scale can't be changed.
177 GetMagnificationController()->SetScale(4.0f, false);
178 EXPECT_TRUE(GetRootWindow()->layer()->transform().IsIdentity());
179 EXPECT_EQ(1.0f, GetMagnificationController()->GetScale());
180 EXPECT_EQ("0,0 800x600", GetViewport().ToString());
183 TEST_F(MagnificationControllerTest, MagnifyAndUnmagnify) {
184 // Enables magnifier and confirms the default scale is 2.0x.
185 GetMagnificationController()->SetEnabled(true);
186 EXPECT_FALSE(GetRootWindow()->layer()->transform().IsIdentity());
187 EXPECT_EQ(2.0f, GetMagnificationController()->GetScale());
188 EXPECT_EQ("200,150 400x300", GetViewport().ToString());
189 EXPECT_EQ("400,300", CurrentPointOfInterest());
191 // Changes the scale.
192 GetMagnificationController()->SetScale(4.0f, false);
193 EXPECT_EQ(4.0f, GetMagnificationController()->GetScale());
194 EXPECT_EQ("300,225 200x150", GetViewport().ToString());
195 EXPECT_EQ("400,300", CurrentPointOfInterest());
197 GetMagnificationController()->SetScale(1.0f, false);
198 EXPECT_EQ(1.0f, GetMagnificationController()->GetScale());
199 EXPECT_EQ("0,0 800x600", GetViewport().ToString());
200 EXPECT_EQ("400,300", CurrentPointOfInterest());
202 GetMagnificationController()->SetScale(3.0f, false);
203 EXPECT_EQ(3.0f, GetMagnificationController()->GetScale());
204 EXPECT_EQ("266,200 267x200", GetViewport().ToString());
205 EXPECT_EQ("400,300", CurrentPointOfInterest());
208 TEST_F(MagnificationControllerTest, MoveWindow) {
209 // Enables magnifier and confirm the viewport is at center.
210 GetMagnificationController()->SetEnabled(true);
211 EXPECT_EQ(2.0f, GetMagnificationController()->GetScale());
212 EXPECT_EQ("200,150 400x300", GetViewport().ToString());
214 // Move the viewport.
215 GetMagnificationController()->MoveWindow(0, 0, false);
216 EXPECT_EQ("0,0 400x300", GetViewport().ToString());
218 GetMagnificationController()->MoveWindow(200, 300, false);
219 EXPECT_EQ("200,300 400x300", GetViewport().ToString());
221 GetMagnificationController()->MoveWindow(400, 0, false);
222 EXPECT_EQ("400,0 400x300", GetViewport().ToString());
224 GetMagnificationController()->MoveWindow(400, 300, false);
225 EXPECT_EQ("400,300 400x300", GetViewport().ToString());
227 // Confirms that the viewport can't across the top-left border.
228 GetMagnificationController()->MoveWindow(-100, 0, false);
229 EXPECT_EQ("0,0 400x300", GetViewport().ToString());
231 GetMagnificationController()->MoveWindow(0, -100, false);
232 EXPECT_EQ("0,0 400x300", GetViewport().ToString());
234 GetMagnificationController()->MoveWindow(-100, -100, false);
235 EXPECT_EQ("0,0 400x300", GetViewport().ToString());
237 // Confirms that the viewport can't across the bittom-right border.
238 GetMagnificationController()->MoveWindow(800, 0, false);
239 EXPECT_EQ("400,0 400x300", GetViewport().ToString());
241 GetMagnificationController()->MoveWindow(0, 400, false);
242 EXPECT_EQ("0,300 400x300", GetViewport().ToString());
244 GetMagnificationController()->MoveWindow(200, 400, false);
245 EXPECT_EQ("200,300 400x300", GetViewport().ToString());
247 GetMagnificationController()->MoveWindow(1000, 1000, false);
248 EXPECT_EQ("400,300 400x300", GetViewport().ToString());
251 TEST_F(MagnificationControllerTest, PointOfInterest) {
252 ui::test::EventGenerator generator(Shell::GetPrimaryRootWindow());
254 generator.MoveMouseToInHost(gfx::Point(0, 0));
255 EXPECT_EQ("0,0", CurrentPointOfInterest());
257 generator.MoveMouseToInHost(gfx::Point(799, 599));
258 EXPECT_EQ("799,599", CurrentPointOfInterest());
260 generator.MoveMouseToInHost(gfx::Point(400, 300));
261 EXPECT_EQ("400,300", CurrentPointOfInterest());
263 GetMagnificationController()->SetEnabled(true);
264 EXPECT_EQ("400,300", CurrentPointOfInterest());
266 generator.MoveMouseToInHost(gfx::Point(500, 400));
267 EXPECT_EQ("450,350", CurrentPointOfInterest());
270 TEST_F(MagnificationControllerTest, PanWindow2xLeftToRight) {
271 const aura::Env* env = aura::Env::GetInstance();
272 ui::test::EventGenerator generator(Shell::GetPrimaryRootWindow());
274 generator.MoveMouseToInHost(gfx::Point(0, 0));
275 EXPECT_EQ(1.f, GetMagnificationController()->GetScale());
276 EXPECT_EQ("0,0 800x600", GetViewport().ToString());
277 EXPECT_EQ("0,0", env->last_mouse_location().ToString());
279 // Enables magnifier and confirm the viewport is at center.
280 GetMagnificationController()->SetEnabled(true);
281 EXPECT_EQ(2.0f, GetMagnificationController()->GetScale());
283 GetMagnificationController()->MoveWindow(0, 0, false);
284 generator.MoveMouseToInHost(gfx::Point(0, 0));
285 EXPECT_EQ("0,0", env->last_mouse_location().ToString());
286 EXPECT_EQ("0,0 400x300", GetViewport().ToString());
288 generator.MoveMouseToInHost(gfx::Point(300, 150));
289 EXPECT_EQ("150,75", env->last_mouse_location().ToString());
290 EXPECT_EQ("0,0 400x300", GetViewport().ToString());
292 generator.MoveMouseToInHost(gfx::Point(700, 150));
293 EXPECT_EQ("350,75", env->last_mouse_location().ToString());
294 EXPECT_EQ("0,0 400x300", GetViewport().ToString());
296 generator.MoveMouseToInHost(gfx::Point(701, 150));
297 EXPECT_EQ("350,75", env->last_mouse_location().ToString());
298 EXPECT_EQ("0,0 400x300", GetViewport().ToString());
300 generator.MoveMouseToInHost(gfx::Point(702, 150));
301 EXPECT_EQ("351,75", env->last_mouse_location().ToString());
302 EXPECT_EQ("1,0 400x300", GetViewport().ToString());
304 generator.MoveMouseToInHost(gfx::Point(703, 150));
305 EXPECT_EQ("352,75", env->last_mouse_location().ToString());
306 EXPECT_EQ("2,0 400x300", GetViewport().ToString());
308 generator.MoveMouseToInHost(gfx::Point(704, 150));
309 EXPECT_EQ("354,75", env->last_mouse_location().ToString());
310 EXPECT_EQ("4,0 400x300", GetViewport().ToString());
312 generator.MoveMouseToInHost(gfx::Point(712, 150));
313 EXPECT_EQ("360,75", env->last_mouse_location().ToString());
314 EXPECT_EQ("10,0 400x300", GetViewport().ToString());
316 generator.MoveMouseToInHost(gfx::Point(600, 150));
317 EXPECT_EQ("310,75", env->last_mouse_location().ToString());
318 EXPECT_EQ("10,0 400x300", GetViewport().ToString());
320 generator.MoveMouseToInHost(gfx::Point(720, 150));
321 EXPECT_EQ("370,75", env->last_mouse_location().ToString());
322 EXPECT_EQ("20,0 400x300", GetViewport().ToString());
324 generator.MoveMouseToInHost(gfx::Point(780, 150));
325 EXPECT_EQ("410,75", env->last_mouse_location().ToString());
326 EXPECT_EQ("410,75", CurrentPointOfInterest());
327 EXPECT_EQ("60,0 400x300", GetViewport().ToString());
329 generator.MoveMouseToInHost(gfx::Point(799, 150));
330 EXPECT_EQ("459,75", env->last_mouse_location().ToString());
331 EXPECT_EQ("109,0 400x300", GetViewport().ToString());
333 generator.MoveMouseToInHost(gfx::Point(702, 150));
334 EXPECT_EQ("460,75", env->last_mouse_location().ToString());
335 EXPECT_EQ("110,0 400x300", GetViewport().ToString());
337 generator.MoveMouseToInHost(gfx::Point(780, 150));
338 EXPECT_EQ("500,75", env->last_mouse_location().ToString());
339 EXPECT_EQ("150,0 400x300", GetViewport().ToString());
341 generator.MoveMouseToInHost(gfx::Point(780, 150));
342 EXPECT_EQ("540,75", env->last_mouse_location().ToString());
343 EXPECT_EQ("190,0 400x300", GetViewport().ToString());
345 generator.MoveMouseToInHost(gfx::Point(780, 150));
346 EXPECT_EQ("580,75", env->last_mouse_location().ToString());
347 EXPECT_EQ("230,0 400x300", GetViewport().ToString());
349 generator.MoveMouseToInHost(gfx::Point(780, 150));
350 EXPECT_EQ("620,75", env->last_mouse_location().ToString());
351 EXPECT_EQ("270,0 400x300", GetViewport().ToString());
353 generator.MoveMouseToInHost(gfx::Point(780, 150));
354 EXPECT_EQ("660,75", env->last_mouse_location().ToString());
355 EXPECT_EQ("310,0 400x300", GetViewport().ToString());
357 generator.MoveMouseToInHost(gfx::Point(780, 150));
358 EXPECT_EQ("700,75", env->last_mouse_location().ToString());
359 EXPECT_EQ("350,0 400x300", GetViewport().ToString());
361 generator.MoveMouseToInHost(gfx::Point(780, 150));
362 EXPECT_EQ("740,75", env->last_mouse_location().ToString());
363 EXPECT_EQ("390,0 400x300", GetViewport().ToString());
365 generator.MoveMouseToInHost(gfx::Point(780, 150));
366 EXPECT_EQ("780,75", env->last_mouse_location().ToString());
367 EXPECT_EQ("400,0 400x300", GetViewport().ToString());
369 generator.MoveMouseToInHost(gfx::Point(799, 150));
370 EXPECT_EQ("799,75", env->last_mouse_location().ToString());
371 EXPECT_EQ("400,0 400x300", GetViewport().ToString());
374 TEST_F(MagnificationControllerTest, PanWindow2xRightToLeft) {
375 const aura::Env* env = aura::Env::GetInstance();
376 ui::test::EventGenerator generator(Shell::GetPrimaryRootWindow());
378 generator.MoveMouseToInHost(gfx::Point(799, 300));
379 EXPECT_EQ(1.f, GetMagnificationController()->GetScale());
380 EXPECT_EQ("0,0 800x600", GetViewport().ToString());
381 EXPECT_EQ("799,300", env->last_mouse_location().ToString());
383 // Enables magnifier and confirm the viewport is at center.
384 GetMagnificationController()->SetEnabled(true);
386 generator.MoveMouseToInHost(gfx::Point(799, 300));
387 EXPECT_EQ("798,300", env->last_mouse_location().ToString());
388 EXPECT_EQ("400,150 400x300", GetViewport().ToString());
390 generator.MoveMouseToInHost(gfx::Point(0, 300));
391 EXPECT_EQ("400,300", env->last_mouse_location().ToString());
392 EXPECT_EQ("350,150 400x300", GetViewport().ToString());
394 generator.MoveMouseToInHost(gfx::Point(0, 300));
395 EXPECT_EQ("350,300", env->last_mouse_location().ToString());
396 EXPECT_EQ("300,150 400x300", GetViewport().ToString());
398 generator.MoveMouseToInHost(gfx::Point(0, 300));
399 EXPECT_EQ("300,300", env->last_mouse_location().ToString());
400 EXPECT_EQ("250,150 400x300", GetViewport().ToString());
402 generator.MoveMouseToInHost(gfx::Point(0, 300));
403 EXPECT_EQ("250,300", env->last_mouse_location().ToString());
404 EXPECT_EQ("200,150 400x300", GetViewport().ToString());
406 generator.MoveMouseToInHost(gfx::Point(0, 300));
407 EXPECT_EQ("200,300", env->last_mouse_location().ToString());
408 EXPECT_EQ("150,150 400x300", GetViewport().ToString());
410 generator.MoveMouseToInHost(gfx::Point(0, 300));
411 EXPECT_EQ("150,300", env->last_mouse_location().ToString());
412 EXPECT_EQ("100,150 400x300", GetViewport().ToString());
414 generator.MoveMouseToInHost(gfx::Point(0, 300));
415 EXPECT_EQ("100,300", env->last_mouse_location().ToString());
416 EXPECT_EQ("50,150 400x300", GetViewport().ToString());
418 generator.MoveMouseToInHost(gfx::Point(0, 300));
419 EXPECT_EQ("50,300", env->last_mouse_location().ToString());
420 EXPECT_EQ("0,150 400x300", GetViewport().ToString());
422 generator.MoveMouseToInHost(gfx::Point(0, 300));
423 EXPECT_EQ("0,300", env->last_mouse_location().ToString());
424 EXPECT_EQ("0,150 400x300", GetViewport().ToString());
427 TEST_F(MagnificationControllerTest, PanWindowToRight) {
428 const aura::Env* env = aura::Env::GetInstance();
429 ui::test::EventGenerator generator(Shell::GetPrimaryRootWindow());
431 generator.MoveMouseToInHost(gfx::Point(400, 300));
432 EXPECT_EQ(1.f, GetMagnificationController()->GetScale());
433 EXPECT_EQ("0,0 800x600", GetViewport().ToString());
434 EXPECT_EQ("400,300", env->last_mouse_location().ToString());
436 float scale = 2.f;
438 // Enables magnifier and confirm the viewport is at center.
439 GetMagnificationController()->SetEnabled(true);
440 EXPECT_FLOAT_EQ(2.f, GetMagnificationController()->GetScale());
442 scale *= ui::kMagnificationScaleFactor;
443 GetMagnificationController()->SetScale(scale, false);
444 EXPECT_FLOAT_EQ(2.3784142, GetMagnificationController()->GetScale());
445 generator.MoveMouseToInHost(gfx::Point(400, 300));
446 EXPECT_EQ("400,300", env->last_mouse_location().ToString());
447 generator.MoveMouseToInHost(gfx::Point(799, 300));
448 EXPECT_EQ("566,299", env->last_mouse_location().ToString());
449 EXPECT_EQ("705,300", GetHostMouseLocation());
451 scale *= ui::kMagnificationScaleFactor;
452 GetMagnificationController()->SetScale(scale, false);
453 EXPECT_FLOAT_EQ(2.8284268, GetMagnificationController()->GetScale());
454 generator.MoveMouseToInHost(gfx::Point(799, 300));
455 EXPECT_EQ("599,299", env->last_mouse_location().ToString());
456 EXPECT_EQ("702,300", GetHostMouseLocation());
458 scale *= ui::kMagnificationScaleFactor;
459 GetMagnificationController()->SetScale(scale, false);
460 EXPECT_FLOAT_EQ(3.3635852, GetMagnificationController()->GetScale());
461 generator.MoveMouseToInHost(gfx::Point(799, 300));
462 EXPECT_EQ("627,298", env->last_mouse_location().ToString());
463 EXPECT_EQ("707,300", GetHostMouseLocation());
465 scale *= ui::kMagnificationScaleFactor;
466 GetMagnificationController()->SetScale(scale, false);
467 EXPECT_FLOAT_EQ(4.f, GetMagnificationController()->GetScale());
468 generator.MoveMouseToInHost(gfx::Point(799, 300));
469 EXPECT_EQ("649,298", env->last_mouse_location().ToString());
470 EXPECT_EQ("704,300", GetHostMouseLocation());
473 TEST_F(MagnificationControllerTest, PanWindowToLeft) {
474 const aura::Env* env = aura::Env::GetInstance();
475 ui::test::EventGenerator generator(Shell::GetPrimaryRootWindow());
477 generator.MoveMouseToInHost(gfx::Point(400, 300));
478 EXPECT_EQ(1.f, GetMagnificationController()->GetScale());
479 EXPECT_EQ("0,0 800x600", GetViewport().ToString());
480 EXPECT_EQ("400,300", env->last_mouse_location().ToString());
482 float scale = 2.f;
484 // Enables magnifier and confirm the viewport is at center.
485 GetMagnificationController()->SetEnabled(true);
486 EXPECT_FLOAT_EQ(2.f, GetMagnificationController()->GetScale());
488 scale *= ui::kMagnificationScaleFactor;
489 GetMagnificationController()->SetScale(scale, false);
490 EXPECT_FLOAT_EQ(2.3784142, GetMagnificationController()->GetScale());
491 generator.MoveMouseToInHost(gfx::Point(400, 300));
492 EXPECT_EQ("400,300", env->last_mouse_location().ToString());
493 generator.MoveMouseToInHost(gfx::Point(0, 300));
494 EXPECT_EQ("231,299", env->last_mouse_location().ToString());
495 EXPECT_EQ("100,300", GetHostMouseLocation());
497 scale *= ui::kMagnificationScaleFactor;
498 GetMagnificationController()->SetScale(scale, false);
499 EXPECT_FLOAT_EQ(2.8284268, GetMagnificationController()->GetScale());
500 generator.MoveMouseToInHost(gfx::Point(0, 300));
501 EXPECT_EQ("194,299", env->last_mouse_location().ToString());
502 EXPECT_EQ("99,300", GetHostMouseLocation());
504 scale *= ui::kMagnificationScaleFactor;
505 GetMagnificationController()->SetScale(scale, false);
506 EXPECT_FLOAT_EQ(3.3635852, GetMagnificationController()->GetScale());
507 generator.MoveMouseToInHost(gfx::Point(0, 300));
508 EXPECT_EQ("164,298", env->last_mouse_location().ToString());
509 EXPECT_EQ("98,300", GetHostMouseLocation());
511 scale *= ui::kMagnificationScaleFactor;
512 GetMagnificationController()->SetScale(scale, false);
513 EXPECT_FLOAT_EQ(4.f, GetMagnificationController()->GetScale());
514 generator.MoveMouseToInHost(gfx::Point(0, 300));
515 EXPECT_EQ("139,298", env->last_mouse_location().ToString());
516 EXPECT_EQ("100,300", GetHostMouseLocation());
519 TEST_F(MagnificationControllerTest, FollowTextInputFieldFocus) {
520 CreateAndShowTextInputView(gfx::Rect(500, 300, 80, 80));
521 gfx::Rect text_input_bounds = GetTextInputViewBounds();
523 // Enables magnifier and confirm the viewport is at center.
524 GetMagnificationController()->SetEnabled(true);
525 EXPECT_EQ(2.0f, GetMagnificationController()->GetScale());
526 EXPECT_EQ("200,150 400x300", GetViewport().ToString());
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.origin(), view_port.CenterPoint());
546 TEST_F(MagnificationControllerTest, FollowTextInputFieldKeyPress) {
547 CreateAndShowTextInputView(gfx::Rect(385, 200, 80, 80));
548 gfx::Rect text_input_bounds = GetTextInputViewBounds();
550 // Enables magnifier and confirm the viewport is at center.
551 GetMagnificationController()->SetEnabled(true);
552 EXPECT_EQ(2.0f, GetMagnificationController()->GetScale());
553 EXPECT_EQ("200,150 400x300", GetViewport().ToString());
555 // Move the viewport to (0, 0), so that text input field intersects the
556 // view port at the right edge.
557 GetMagnificationController()->MoveWindow(0, 0, false);
558 EXPECT_EQ("0,0 400x300", GetViewport().ToString());
559 EXPECT_TRUE(GetViewport().Intersects(text_input_bounds));
561 // Focus on the text input field.
562 FocusOnTextInputView();
564 // Verify the view port is not moved, and the caret is inside the view port.
565 gfx::Rect view_port = GetViewport();
566 EXPECT_EQ("0,0 400x300", view_port.ToString());
567 EXPECT_TRUE(view_port.Intersects(text_input_bounds));
568 EXPECT_TRUE(text_input_bounds.Contains(GetCaretBounds()));
570 // Press keys on text input simulate typing on text field and the caret
571 // moves out of the old view port region. The view port is moved to the place
572 // where caret's x coordinate is centered at the new view port.
573 ui::test::EventGenerator generator(Shell::GetPrimaryRootWindow());
574 generator.PressKey(ui::VKEY_A, 0);
575 generator.ReleaseKey(ui::VKEY_A, 0);
576 gfx::Rect caret_bounds = GetCaretBounds();
577 EXPECT_FALSE(view_port.Intersects(caret_bounds));
579 gfx::Rect new_view_port = GetViewport();
580 EXPECT_TRUE(new_view_port.Contains(caret_bounds));
581 EXPECT_EQ(caret_bounds.x(), new_view_port.CenterPoint().x());
582 EXPECT_EQ(view_port.y(), new_view_port.y());
585 } // namespace ash