Update To 11.40.268.0
[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/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/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"
24
25 namespace ash {
26 namespace {
27
28 const int kRootHeight = 600;
29 const int kRootWidth = 800;
30
31 const int kTextInputWindowWidth = 50;
32 const int kTextInputWindowHeight = 50;
33
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);
40   }
41
42   virtual ~TextInputView() {}
43
44   virtual gfx::Size GetPreferredSize() const override {
45     return gfx::Size(kTextInputWindowWidth, kTextInputWindowHeight);
46   }
47
48   // Overridden from views::WidgetDelegate:
49   virtual views::View* GetContentsView() override { return this; }
50
51   void FocusOnTextInput() { GetFocusManager()->SetFocusedView(text_field_); }
52
53  private:
54   views::Textfield* text_field_;  // owned by views hierarchy
55
56   DISALLOW_COPY_AND_ASSIGN(TextInputView);
57 };
58
59 }  // namespace
60
61 class MagnificationControllerTest: public test::AshTestBase {
62  public:
63   MagnificationControllerTest() : text_input_view_(NULL) {}
64   virtual ~MagnificationControllerTest() {}
65
66   virtual void SetUp() override {
67     AshTestBase::SetUp();
68     UpdateDisplay(base::StringPrintf("%dx%d", kRootWidth, kRootHeight));
69
70     aura::Window* root = GetRootWindow();
71     gfx::Rect root_bounds(root->bounds());
72
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
79   }
80
81   virtual void TearDown() override {
82     AshTestBase::TearDown();
83   }
84
85  protected:
86   aura::Window* GetRootWindow() const {
87     return Shell::GetPrimaryRootWindow();
88   }
89
90   std::string GetHostMouseLocation() {
91     const gfx::Point& location =
92         aura::test::QueryLatestMousePositionRequestInHost(
93             GetRootWindow()->GetHost());
94     return location.ToString();
95   }
96
97   ash::MagnificationController* GetMagnificationController() const {
98     return ash::Shell::GetInstance()->magnification_controller();
99   }
100
101   gfx::Rect GetViewport() const {
102     gfx::RectF bounds(0, 0, kRootWidth, kRootHeight);
103     GetRootWindow()->layer()->transform().TransformRectReverse(&bounds);
104     return gfx::ToEnclosingRect(bounds);
105   }
106
107   std::string CurrentPointOfInterest() const {
108     return GetMagnificationController()->
109         GetPointOfInterestForTesting().ToString();
110   }
111
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();
117   }
118
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());
129   }
130
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());
139   }
140
141   void FocusOnTextInputView() {
142     DCHECK(text_input_view_);
143     text_input_view_->FocusOnTextInput();
144   }
145
146  private:
147   TextInputView* text_input_view_;
148
149   ui::InputMethod* GetInputMethod() {
150     DCHECK(text_input_view_);
151     return text_input_view_->GetWidget()
152         ->GetNativeWindow()
153         ->GetRootWindow()
154         ->GetProperty(aura::client::kRootWindowInputMethodKey);
155   }
156
157   DISALLOW_COPY_AND_ASSIGN(MagnificationControllerTest);
158 };
159
160 TEST_F(MagnificationControllerTest, EnableAndDisable) {
161   // Confirms the magnifier is disabled.
162   EXPECT_TRUE(GetRootWindow()->layer()->transform().IsIdentity());
163   EXPECT_EQ(1.0f, GetMagnificationController()->GetScale());
164   EXPECT_EQ("0,0 800x600", GetViewport().ToString());
165
166   // Enables magnifier.
167   GetMagnificationController()->SetEnabled(true);
168   EXPECT_FALSE(GetRootWindow()->layer()->transform().IsIdentity());
169   EXPECT_EQ(2.0f, GetMagnificationController()->GetScale());
170   EXPECT_EQ("200,150 400x300", GetViewport().ToString());
171
172   // Disables magnifier.
173   GetMagnificationController()->SetEnabled(false);
174   EXPECT_TRUE(GetRootWindow()->layer()->transform().IsIdentity());
175   EXPECT_EQ(1.0f, GetMagnificationController()->GetScale());
176   EXPECT_EQ("0,0 800x600", GetViewport().ToString());
177
178   // Confirms the the scale can't be changed.
179   GetMagnificationController()->SetScale(4.0f, false);
180   EXPECT_TRUE(GetRootWindow()->layer()->transform().IsIdentity());
181   EXPECT_EQ(1.0f, GetMagnificationController()->GetScale());
182   EXPECT_EQ("0,0 800x600", GetViewport().ToString());
183 }
184
185 TEST_F(MagnificationControllerTest, MagnifyAndUnmagnify) {
186   // Enables magnifier and confirms the default scale is 2.0x.
187   GetMagnificationController()->SetEnabled(true);
188   EXPECT_FALSE(GetRootWindow()->layer()->transform().IsIdentity());
189   EXPECT_EQ(2.0f, GetMagnificationController()->GetScale());
190   EXPECT_EQ("200,150 400x300", GetViewport().ToString());
191   EXPECT_EQ("400,300", CurrentPointOfInterest());
192
193   // Changes the scale.
194   GetMagnificationController()->SetScale(4.0f, false);
195   EXPECT_EQ(4.0f, GetMagnificationController()->GetScale());
196   EXPECT_EQ("300,225 200x150", GetViewport().ToString());
197   EXPECT_EQ("400,300", CurrentPointOfInterest());
198
199   GetMagnificationController()->SetScale(1.0f, false);
200   EXPECT_EQ(1.0f, GetMagnificationController()->GetScale());
201   EXPECT_EQ("0,0 800x600", GetViewport().ToString());
202   EXPECT_EQ("400,300", CurrentPointOfInterest());
203
204   GetMagnificationController()->SetScale(3.0f, false);
205   EXPECT_EQ(3.0f, GetMagnificationController()->GetScale());
206   EXPECT_EQ("266,200 267x200", GetViewport().ToString());
207   EXPECT_EQ("400,300", CurrentPointOfInterest());
208 }
209
210 TEST_F(MagnificationControllerTest, MoveWindow) {
211   // Enables magnifier and confirm the viewport is at center.
212   GetMagnificationController()->SetEnabled(true);
213   EXPECT_EQ(2.0f, GetMagnificationController()->GetScale());
214   EXPECT_EQ("200,150 400x300", GetViewport().ToString());
215
216   // Move the viewport.
217   GetMagnificationController()->MoveWindow(0, 0, false);
218   EXPECT_EQ("0,0 400x300", GetViewport().ToString());
219
220   GetMagnificationController()->MoveWindow(200, 300, false);
221   EXPECT_EQ("200,300 400x300", GetViewport().ToString());
222
223   GetMagnificationController()->MoveWindow(400, 0, false);
224   EXPECT_EQ("400,0 400x300", GetViewport().ToString());
225
226   GetMagnificationController()->MoveWindow(400, 300, false);
227   EXPECT_EQ("400,300 400x300", GetViewport().ToString());
228
229   // Confirms that the viewport can't across the top-left border.
230   GetMagnificationController()->MoveWindow(-100, 0, false);
231   EXPECT_EQ("0,0 400x300", GetViewport().ToString());
232
233   GetMagnificationController()->MoveWindow(0, -100, false);
234   EXPECT_EQ("0,0 400x300", GetViewport().ToString());
235
236   GetMagnificationController()->MoveWindow(-100, -100, false);
237   EXPECT_EQ("0,0 400x300", GetViewport().ToString());
238
239   // Confirms that the viewport can't across the bittom-right border.
240   GetMagnificationController()->MoveWindow(800, 0, false);
241   EXPECT_EQ("400,0 400x300", GetViewport().ToString());
242
243   GetMagnificationController()->MoveWindow(0, 400, false);
244   EXPECT_EQ("0,300 400x300", GetViewport().ToString());
245
246   GetMagnificationController()->MoveWindow(200, 400, false);
247   EXPECT_EQ("200,300 400x300", GetViewport().ToString());
248
249   GetMagnificationController()->MoveWindow(1000, 1000, false);
250   EXPECT_EQ("400,300 400x300", GetViewport().ToString());
251 }
252
253 TEST_F(MagnificationControllerTest, PointOfInterest) {
254   ui::test::EventGenerator generator(Shell::GetPrimaryRootWindow());
255
256   generator.MoveMouseToInHost(gfx::Point(0, 0));
257   EXPECT_EQ("0,0", CurrentPointOfInterest());
258
259   generator.MoveMouseToInHost(gfx::Point(799, 599));
260   EXPECT_EQ("799,599", CurrentPointOfInterest());
261
262   generator.MoveMouseToInHost(gfx::Point(400, 300));
263   EXPECT_EQ("400,300", CurrentPointOfInterest());
264
265   GetMagnificationController()->SetEnabled(true);
266   EXPECT_EQ("400,300", CurrentPointOfInterest());
267
268   generator.MoveMouseToInHost(gfx::Point(500, 400));
269   EXPECT_EQ("450,350", CurrentPointOfInterest());
270 }
271
272 TEST_F(MagnificationControllerTest, PanWindow2xLeftToRight) {
273   const aura::Env* env = aura::Env::GetInstance();
274   ui::test::EventGenerator generator(Shell::GetPrimaryRootWindow());
275
276   generator.MoveMouseToInHost(gfx::Point(0, 0));
277   EXPECT_EQ(1.f, GetMagnificationController()->GetScale());
278   EXPECT_EQ("0,0 800x600", GetViewport().ToString());
279   EXPECT_EQ("0,0", env->last_mouse_location().ToString());
280
281   // Enables magnifier and confirm the viewport is at center.
282   GetMagnificationController()->SetEnabled(true);
283   EXPECT_EQ(2.0f, GetMagnificationController()->GetScale());
284
285   GetMagnificationController()->MoveWindow(0, 0, false);
286   generator.MoveMouseToInHost(gfx::Point(0, 0));
287   EXPECT_EQ("0,0", env->last_mouse_location().ToString());
288   EXPECT_EQ("0,0 400x300", GetViewport().ToString());
289
290   generator.MoveMouseToInHost(gfx::Point(300, 150));
291   EXPECT_EQ("150,75", env->last_mouse_location().ToString());
292   EXPECT_EQ("0,0 400x300", GetViewport().ToString());
293
294   generator.MoveMouseToInHost(gfx::Point(700, 150));
295   EXPECT_EQ("350,75", env->last_mouse_location().ToString());
296   EXPECT_EQ("0,0 400x300", GetViewport().ToString());
297
298   generator.MoveMouseToInHost(gfx::Point(701, 150));
299   EXPECT_EQ("350,75", env->last_mouse_location().ToString());
300   EXPECT_EQ("0,0 400x300", GetViewport().ToString());
301
302   generator.MoveMouseToInHost(gfx::Point(702, 150));
303   EXPECT_EQ("351,75", env->last_mouse_location().ToString());
304   EXPECT_EQ("1,0 400x300", GetViewport().ToString());
305
306   generator.MoveMouseToInHost(gfx::Point(703, 150));
307   EXPECT_EQ("352,75", env->last_mouse_location().ToString());
308   EXPECT_EQ("2,0 400x300", GetViewport().ToString());
309
310   generator.MoveMouseToInHost(gfx::Point(704, 150));
311   EXPECT_EQ("354,75", env->last_mouse_location().ToString());
312   EXPECT_EQ("4,0 400x300", GetViewport().ToString());
313
314   generator.MoveMouseToInHost(gfx::Point(712, 150));
315   EXPECT_EQ("360,75", env->last_mouse_location().ToString());
316   EXPECT_EQ("10,0 400x300", GetViewport().ToString());
317
318   generator.MoveMouseToInHost(gfx::Point(600, 150));
319   EXPECT_EQ("310,75", env->last_mouse_location().ToString());
320   EXPECT_EQ("10,0 400x300", GetViewport().ToString());
321
322   generator.MoveMouseToInHost(gfx::Point(720, 150));
323   EXPECT_EQ("370,75", env->last_mouse_location().ToString());
324   EXPECT_EQ("20,0 400x300", GetViewport().ToString());
325
326   generator.MoveMouseToInHost(gfx::Point(780, 150));
327   EXPECT_EQ("410,75", env->last_mouse_location().ToString());
328   EXPECT_EQ("410,75", CurrentPointOfInterest());
329   EXPECT_EQ("60,0 400x300", GetViewport().ToString());
330
331   generator.MoveMouseToInHost(gfx::Point(799, 150));
332   EXPECT_EQ("459,75", env->last_mouse_location().ToString());
333   EXPECT_EQ("109,0 400x300", GetViewport().ToString());
334
335   generator.MoveMouseToInHost(gfx::Point(702, 150));
336   EXPECT_EQ("460,75", env->last_mouse_location().ToString());
337   EXPECT_EQ("110,0 400x300", GetViewport().ToString());
338
339   generator.MoveMouseToInHost(gfx::Point(780, 150));
340   EXPECT_EQ("500,75", env->last_mouse_location().ToString());
341   EXPECT_EQ("150,0 400x300", GetViewport().ToString());
342
343   generator.MoveMouseToInHost(gfx::Point(780, 150));
344   EXPECT_EQ("540,75", env->last_mouse_location().ToString());
345   EXPECT_EQ("190,0 400x300", GetViewport().ToString());
346
347   generator.MoveMouseToInHost(gfx::Point(780, 150));
348   EXPECT_EQ("580,75", env->last_mouse_location().ToString());
349   EXPECT_EQ("230,0 400x300", GetViewport().ToString());
350
351   generator.MoveMouseToInHost(gfx::Point(780, 150));
352   EXPECT_EQ("620,75", env->last_mouse_location().ToString());
353   EXPECT_EQ("270,0 400x300", GetViewport().ToString());
354
355   generator.MoveMouseToInHost(gfx::Point(780, 150));
356   EXPECT_EQ("660,75", env->last_mouse_location().ToString());
357   EXPECT_EQ("310,0 400x300", GetViewport().ToString());
358
359   generator.MoveMouseToInHost(gfx::Point(780, 150));
360   EXPECT_EQ("700,75", env->last_mouse_location().ToString());
361   EXPECT_EQ("350,0 400x300", GetViewport().ToString());
362
363   generator.MoveMouseToInHost(gfx::Point(780, 150));
364   EXPECT_EQ("740,75", env->last_mouse_location().ToString());
365   EXPECT_EQ("390,0 400x300", GetViewport().ToString());
366
367   generator.MoveMouseToInHost(gfx::Point(780, 150));
368   EXPECT_EQ("780,75", env->last_mouse_location().ToString());
369   EXPECT_EQ("400,0 400x300", GetViewport().ToString());
370
371   generator.MoveMouseToInHost(gfx::Point(799, 150));
372   EXPECT_EQ("799,75", env->last_mouse_location().ToString());
373   EXPECT_EQ("400,0 400x300", GetViewport().ToString());
374 }
375
376 TEST_F(MagnificationControllerTest, PanWindow2xRightToLeft) {
377   const aura::Env* env = aura::Env::GetInstance();
378   ui::test::EventGenerator generator(Shell::GetPrimaryRootWindow());
379
380   generator.MoveMouseToInHost(gfx::Point(799, 300));
381   EXPECT_EQ(1.f, GetMagnificationController()->GetScale());
382   EXPECT_EQ("0,0 800x600", GetViewport().ToString());
383   EXPECT_EQ("799,300", env->last_mouse_location().ToString());
384
385   // Enables magnifier and confirm the viewport is at center.
386   GetMagnificationController()->SetEnabled(true);
387
388   generator.MoveMouseToInHost(gfx::Point(799, 300));
389   EXPECT_EQ("798,300", env->last_mouse_location().ToString());
390   EXPECT_EQ("400,150 400x300", GetViewport().ToString());
391
392   generator.MoveMouseToInHost(gfx::Point(0, 300));
393   EXPECT_EQ("400,300", env->last_mouse_location().ToString());
394   EXPECT_EQ("350,150 400x300", GetViewport().ToString());
395
396   generator.MoveMouseToInHost(gfx::Point(0, 300));
397   EXPECT_EQ("350,300", env->last_mouse_location().ToString());
398   EXPECT_EQ("300,150 400x300", GetViewport().ToString());
399
400   generator.MoveMouseToInHost(gfx::Point(0, 300));
401   EXPECT_EQ("300,300", env->last_mouse_location().ToString());
402   EXPECT_EQ("250,150 400x300", GetViewport().ToString());
403
404   generator.MoveMouseToInHost(gfx::Point(0, 300));
405   EXPECT_EQ("250,300", env->last_mouse_location().ToString());
406   EXPECT_EQ("200,150 400x300", GetViewport().ToString());
407
408   generator.MoveMouseToInHost(gfx::Point(0, 300));
409   EXPECT_EQ("200,300", env->last_mouse_location().ToString());
410   EXPECT_EQ("150,150 400x300", GetViewport().ToString());
411
412   generator.MoveMouseToInHost(gfx::Point(0, 300));
413   EXPECT_EQ("150,300", env->last_mouse_location().ToString());
414   EXPECT_EQ("100,150 400x300", GetViewport().ToString());
415
416   generator.MoveMouseToInHost(gfx::Point(0, 300));
417   EXPECT_EQ("100,300", env->last_mouse_location().ToString());
418   EXPECT_EQ("50,150 400x300", GetViewport().ToString());
419
420   generator.MoveMouseToInHost(gfx::Point(0, 300));
421   EXPECT_EQ("50,300", env->last_mouse_location().ToString());
422   EXPECT_EQ("0,150 400x300", GetViewport().ToString());
423
424   generator.MoveMouseToInHost(gfx::Point(0, 300));
425   EXPECT_EQ("0,300", env->last_mouse_location().ToString());
426   EXPECT_EQ("0,150 400x300", GetViewport().ToString());
427 }
428
429 TEST_F(MagnificationControllerTest, PanWindowToRight) {
430   const aura::Env* env = aura::Env::GetInstance();
431   ui::test::EventGenerator generator(Shell::GetPrimaryRootWindow());
432
433   generator.MoveMouseToInHost(gfx::Point(400, 300));
434   EXPECT_EQ(1.f, GetMagnificationController()->GetScale());
435   EXPECT_EQ("0,0 800x600", GetViewport().ToString());
436   EXPECT_EQ("400,300", env->last_mouse_location().ToString());
437
438   float scale = 2.f;
439
440   // Enables magnifier and confirm the viewport is at center.
441   GetMagnificationController()->SetEnabled(true);
442   EXPECT_FLOAT_EQ(2.f, GetMagnificationController()->GetScale());
443
444   scale *= ui::kMagnificationScaleFactor;
445   GetMagnificationController()->SetScale(scale, false);
446   EXPECT_FLOAT_EQ(2.3784142, GetMagnificationController()->GetScale());
447   generator.MoveMouseToInHost(gfx::Point(400, 300));
448   EXPECT_EQ("400,300", env->last_mouse_location().ToString());
449   generator.MoveMouseToInHost(gfx::Point(799, 300));
450   EXPECT_EQ("566,299", env->last_mouse_location().ToString());
451   EXPECT_EQ("705,300", GetHostMouseLocation());
452
453   scale *= ui::kMagnificationScaleFactor;
454   GetMagnificationController()->SetScale(scale, false);
455   EXPECT_FLOAT_EQ(2.8284268, GetMagnificationController()->GetScale());
456   generator.MoveMouseToInHost(gfx::Point(799, 300));
457   EXPECT_EQ("599,299", env->last_mouse_location().ToString());
458   EXPECT_EQ("702,300", GetHostMouseLocation());
459
460   scale *= ui::kMagnificationScaleFactor;
461   GetMagnificationController()->SetScale(scale, false);
462   EXPECT_FLOAT_EQ(3.3635852, GetMagnificationController()->GetScale());
463   generator.MoveMouseToInHost(gfx::Point(799, 300));
464   EXPECT_EQ("627,298", env->last_mouse_location().ToString());
465   EXPECT_EQ("707,300", GetHostMouseLocation());
466
467   scale *= ui::kMagnificationScaleFactor;
468   GetMagnificationController()->SetScale(scale, false);
469   EXPECT_FLOAT_EQ(4.f, GetMagnificationController()->GetScale());
470   generator.MoveMouseToInHost(gfx::Point(799, 300));
471   EXPECT_EQ("649,298", env->last_mouse_location().ToString());
472   EXPECT_EQ("704,300", GetHostMouseLocation());
473 }
474
475 TEST_F(MagnificationControllerTest, PanWindowToLeft) {
476   const aura::Env* env = aura::Env::GetInstance();
477   ui::test::EventGenerator generator(Shell::GetPrimaryRootWindow());
478
479   generator.MoveMouseToInHost(gfx::Point(400, 300));
480   EXPECT_EQ(1.f, GetMagnificationController()->GetScale());
481   EXPECT_EQ("0,0 800x600", GetViewport().ToString());
482   EXPECT_EQ("400,300", env->last_mouse_location().ToString());
483
484   float scale = 2.f;
485
486   // Enables magnifier and confirm the viewport is at center.
487   GetMagnificationController()->SetEnabled(true);
488   EXPECT_FLOAT_EQ(2.f, GetMagnificationController()->GetScale());
489
490   scale *= ui::kMagnificationScaleFactor;
491   GetMagnificationController()->SetScale(scale, false);
492   EXPECT_FLOAT_EQ(2.3784142, GetMagnificationController()->GetScale());
493   generator.MoveMouseToInHost(gfx::Point(400, 300));
494   EXPECT_EQ("400,300", env->last_mouse_location().ToString());
495   generator.MoveMouseToInHost(gfx::Point(0, 300));
496   EXPECT_EQ("231,299", env->last_mouse_location().ToString());
497   EXPECT_EQ("100,300", GetHostMouseLocation());
498
499   scale *= ui::kMagnificationScaleFactor;
500   GetMagnificationController()->SetScale(scale, false);
501   EXPECT_FLOAT_EQ(2.8284268, GetMagnificationController()->GetScale());
502   generator.MoveMouseToInHost(gfx::Point(0, 300));
503   EXPECT_EQ("194,299", env->last_mouse_location().ToString());
504   EXPECT_EQ("99,300", GetHostMouseLocation());
505
506   scale *= ui::kMagnificationScaleFactor;
507   GetMagnificationController()->SetScale(scale, false);
508   EXPECT_FLOAT_EQ(3.3635852, GetMagnificationController()->GetScale());
509   generator.MoveMouseToInHost(gfx::Point(0, 300));
510   EXPECT_EQ("164,298", env->last_mouse_location().ToString());
511   EXPECT_EQ("98,300", GetHostMouseLocation());
512
513   scale *= ui::kMagnificationScaleFactor;
514   GetMagnificationController()->SetScale(scale, false);
515   EXPECT_FLOAT_EQ(4.f, GetMagnificationController()->GetScale());
516   generator.MoveMouseToInHost(gfx::Point(0, 300));
517   EXPECT_EQ("139,298", env->last_mouse_location().ToString());
518   EXPECT_EQ("100,300", GetHostMouseLocation());
519 }
520
521 TEST_F(MagnificationControllerTest, FollowTextInputFieldFocus) {
522   CreateAndShowTextInputView(gfx::Rect(500, 300, 80, 80));
523   gfx::Rect text_input_bounds = GetTextInputViewBounds();
524
525   // Enables magnifier and confirm the viewport is at center.
526   GetMagnificationController()->SetEnabled(true);
527   EXPECT_EQ(2.0f, GetMagnificationController()->GetScale());
528   EXPECT_EQ("200,150 400x300", GetViewport().ToString());
529
530   // Move the viewport to (0, 0), so that text input field will be out of
531   // the viewport region.
532   GetMagnificationController()->MoveWindow(0, 0, false);
533   EXPECT_EQ("0,0 400x300", GetViewport().ToString());
534   EXPECT_FALSE(GetViewport().Intersects(text_input_bounds));
535
536   // Focus on the text input field.
537   FocusOnTextInputView();
538
539   // Verify the view port has been moved to the place where the text field is
540   // contained in the view port and the caret is at the center of the view port.
541   gfx::Rect view_port = GetViewport();
542   EXPECT_TRUE(view_port.Contains(text_input_bounds));
543   gfx::Rect caret_bounds = GetCaretBounds();
544   EXPECT_TRUE(text_input_bounds.Contains(caret_bounds));
545   EXPECT_EQ(caret_bounds.origin(), view_port.CenterPoint());
546 }
547
548 TEST_F(MagnificationControllerTest, FollowTextInputFieldKeyPress) {
549   CreateAndShowTextInputView(gfx::Rect(385, 200, 80, 80));
550   gfx::Rect text_input_bounds = GetTextInputViewBounds();
551
552   // Enables magnifier and confirm the viewport is at center.
553   GetMagnificationController()->SetEnabled(true);
554   EXPECT_EQ(2.0f, GetMagnificationController()->GetScale());
555   EXPECT_EQ("200,150 400x300", GetViewport().ToString());
556
557   // Move the viewport to (0, 0), so that text input field intersects the
558   // view port at the right edge.
559   GetMagnificationController()->MoveWindow(0, 0, false);
560   EXPECT_EQ("0,0 400x300", GetViewport().ToString());
561   EXPECT_TRUE(GetViewport().Intersects(text_input_bounds));
562
563   // Focus on the text input field.
564   FocusOnTextInputView();
565
566   // Verify the view port is not moved, and the caret is inside the view port.
567   gfx::Rect view_port = GetViewport();
568   EXPECT_EQ("0,0 400x300", view_port.ToString());
569   EXPECT_TRUE(view_port.Intersects(text_input_bounds));
570   EXPECT_TRUE(text_input_bounds.Contains(GetCaretBounds()));
571
572   // Press keys on text input simulate typing on text field and the caret
573   // moves out of the old view port region. The view port is moved to the place
574   // where caret's x coordinate is centered at the new view port.
575   ui::test::EventGenerator generator(Shell::GetPrimaryRootWindow());
576   generator.PressKey(ui::VKEY_A, 0);
577   generator.ReleaseKey(ui::VKEY_A, 0);
578   gfx::Rect caret_bounds = GetCaretBounds();
579   EXPECT_FALSE(view_port.Intersects(caret_bounds));
580
581   gfx::Rect new_view_port = GetViewport();
582   EXPECT_TRUE(new_view_port.Contains(caret_bounds));
583   EXPECT_EQ(caret_bounds.x(), new_view_port.CenterPoint().x());
584   EXPECT_EQ(view_port.y(), new_view_port.y());
585 }
586
587 }  // namespace ash