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"
9 #include "ash/test/ash_test_base.h"
10 #include "ash/test/display_manager_test_api.h"
11 #include "base/strings/stringprintf.h"
12 #include "ui/aura/client/aura_constants.h"
13 #include "ui/aura/env.h"
14 #include "ui/aura/test/aura_test_utils.h"
15 #include "ui/aura/window_tree_host.h"
16 #include "ui/base/ime/input_method.h"
17 #include "ui/chromeos/accessibility_types.h"
18 #include "ui/events/test/event_generator.h"
19 #include "ui/gfx/geometry/rect_conversions.h"
20 #include "ui/gfx/screen.h"
21 #include "ui/views/controls/textfield/textfield.h"
22 #include "ui/views/layout/fill_layout.h"
23 #include "ui/views/widget/widget.h"
24 #include "ui/views/widget/widget_delegate.h"
25 #include "ui/wm/core/coordinate_conversion.h"
30 const int kRootHeight
= 600;
31 const int kRootWidth
= 800;
33 const int kTextInputWindowWidth
= 50;
34 const int kTextInputWindowHeight
= 50;
36 class TextInputView
: public views::WidgetDelegateView
{
38 TextInputView() : text_field_(new views::Textfield
) {
39 text_field_
->SetTextInputType(ui::TEXT_INPUT_TYPE_TEXT
);
40 AddChildView(text_field_
);
41 SetLayoutManager(new views::FillLayout
);
44 ~TextInputView() override
{}
46 gfx::Size
GetPreferredSize() const override
{
47 return gfx::Size(kTextInputWindowWidth
, kTextInputWindowHeight
);
50 // Overridden from views::WidgetDelegate:
51 views::View
* GetContentsView() override
{ return this; }
53 void FocusOnTextInput() { GetFocusManager()->SetFocusedView(text_field_
); }
56 views::Textfield
* text_field_
; // owned by views hierarchy
58 DISALLOW_COPY_AND_ASSIGN(TextInputView
);
63 class MagnificationControllerTest
: public test::AshTestBase
{
65 MagnificationControllerTest() : text_input_view_(NULL
) {}
66 ~MagnificationControllerTest() override
{}
68 void SetUp() override
{
70 UpdateDisplay(base::StringPrintf("%dx%d", kRootWidth
, kRootHeight
));
72 aura::Window
* root
= GetRootWindow();
73 gfx::Rect
root_bounds(root
->bounds());
76 // RootWindow and Display can't resize on Windows Ash.
77 // http://crbug.com/165962
78 EXPECT_EQ(kRootHeight
, root_bounds
.height());
79 EXPECT_EQ(kRootWidth
, root_bounds
.width());
82 GetMagnificationController()->DisableMoveMagnifierDelayForTesting();
85 void TearDown() override
{ AshTestBase::TearDown(); }
88 aura::Window
* GetRootWindow() const {
89 return Shell::GetPrimaryRootWindow();
92 std::string
GetHostMouseLocation() {
93 const gfx::Point
& location
=
94 aura::test::QueryLatestMousePositionRequestInHost(
95 GetRootWindow()->GetHost());
96 return location
.ToString();
99 ash::MagnificationController
* GetMagnificationController() const {
100 return ash::Shell::GetInstance()->magnification_controller();
103 gfx::Rect
GetViewport() const {
104 gfx::RectF
bounds(0, 0, kRootWidth
, kRootHeight
);
105 GetRootWindow()->layer()->transform().TransformRectReverse(&bounds
);
106 return gfx::ToEnclosingRect(bounds
);
109 std::string
CurrentPointOfInterest() const {
110 return GetMagnificationController()->
111 GetPointOfInterestForTesting().ToString();
114 void CreateAndShowTextInputView(const gfx::Rect
& bounds
) {
115 text_input_view_
= new TextInputView
;
116 views::Widget
* widget
= views::Widget::CreateWindowWithContextAndBounds(
117 text_input_view_
, GetRootWindow(), bounds
);
121 // Returns the text input view's bounds in root window coordinates.
122 gfx::Rect
GetTextInputViewBounds() {
123 DCHECK(text_input_view_
);
124 gfx::Rect bounds
= text_input_view_
->bounds();
125 gfx::Point origin
= bounds
.origin();
126 // Convert origin to screen coordinates.
127 views::View::ConvertPointToScreen(text_input_view_
, &origin
);
128 // Convert origin to root_window_ coordinates.
129 wm::ConvertPointFromScreen(GetRootWindow(), &origin
);
130 return gfx::Rect(origin
.x(), origin
.y(), bounds
.width(), bounds
.height());
133 // Returns the caret bounds in root window coordinates.
134 gfx::Rect
GetCaretBounds() {
135 gfx::Rect caret_bounds
=
136 GetInputMethod()->GetTextInputClient()->GetCaretBounds();
137 gfx::Point origin
= caret_bounds
.origin();
138 wm::ConvertPointFromScreen(GetRootWindow(), &origin
);
140 origin
.x(), origin
.y(), caret_bounds
.width(), caret_bounds
.height());
143 void FocusOnTextInputView() {
144 DCHECK(text_input_view_
);
145 text_input_view_
->FocusOnTextInput();
149 TextInputView
* text_input_view_
;
151 ui::InputMethod
* GetInputMethod() {
152 DCHECK(text_input_view_
);
153 return text_input_view_
->GetWidget()
156 ->GetProperty(aura::client::kRootWindowInputMethodKey
);
159 DISALLOW_COPY_AND_ASSIGN(MagnificationControllerTest
);
162 TEST_F(MagnificationControllerTest
, EnableAndDisable
) {
163 // Confirms the magnifier is disabled.
164 EXPECT_TRUE(GetRootWindow()->layer()->transform().IsIdentity());
165 EXPECT_EQ(1.0f
, GetMagnificationController()->GetScale());
166 EXPECT_EQ("0,0 800x600", GetViewport().ToString());
168 // Enables magnifier.
169 GetMagnificationController()->SetEnabled(true);
170 EXPECT_FALSE(GetRootWindow()->layer()->transform().IsIdentity());
171 EXPECT_EQ(2.0f
, GetMagnificationController()->GetScale());
172 EXPECT_EQ("200,150 400x300", GetViewport().ToString());
174 // Disables magnifier.
175 GetMagnificationController()->SetEnabled(false);
176 EXPECT_TRUE(GetRootWindow()->layer()->transform().IsIdentity());
177 EXPECT_EQ(1.0f
, GetMagnificationController()->GetScale());
178 EXPECT_EQ("0,0 800x600", GetViewport().ToString());
180 // Confirms the the scale can't be changed.
181 GetMagnificationController()->SetScale(4.0f
, false);
182 EXPECT_TRUE(GetRootWindow()->layer()->transform().IsIdentity());
183 EXPECT_EQ(1.0f
, GetMagnificationController()->GetScale());
184 EXPECT_EQ("0,0 800x600", GetViewport().ToString());
187 TEST_F(MagnificationControllerTest
, MagnifyAndUnmagnify
) {
188 // Enables magnifier and confirms the default scale is 2.0x.
189 GetMagnificationController()->SetEnabled(true);
190 EXPECT_FALSE(GetRootWindow()->layer()->transform().IsIdentity());
191 EXPECT_EQ(2.0f
, GetMagnificationController()->GetScale());
192 EXPECT_EQ("200,150 400x300", GetViewport().ToString());
193 EXPECT_EQ("400,300", CurrentPointOfInterest());
195 // Changes the scale.
196 GetMagnificationController()->SetScale(4.0f
, false);
197 EXPECT_EQ(4.0f
, GetMagnificationController()->GetScale());
198 EXPECT_EQ("300,225 200x150", GetViewport().ToString());
199 EXPECT_EQ("400,300", CurrentPointOfInterest());
201 GetMagnificationController()->SetScale(1.0f
, false);
202 EXPECT_EQ(1.0f
, GetMagnificationController()->GetScale());
203 EXPECT_EQ("0,0 800x600", GetViewport().ToString());
204 EXPECT_EQ("400,300", CurrentPointOfInterest());
206 GetMagnificationController()->SetScale(3.0f
, false);
207 EXPECT_EQ(3.0f
, GetMagnificationController()->GetScale());
208 EXPECT_EQ("266,200 267x200", GetViewport().ToString());
209 EXPECT_EQ("400,300", CurrentPointOfInterest());
212 TEST_F(MagnificationControllerTest
, MoveWindow
) {
213 // Enables magnifier and confirm the viewport is at center.
214 GetMagnificationController()->SetEnabled(true);
215 EXPECT_EQ(2.0f
, GetMagnificationController()->GetScale());
216 EXPECT_EQ("200,150 400x300", GetViewport().ToString());
218 // Move the viewport.
219 GetMagnificationController()->MoveWindow(0, 0, false);
220 EXPECT_EQ("0,0 400x300", GetViewport().ToString());
222 GetMagnificationController()->MoveWindow(200, 300, false);
223 EXPECT_EQ("200,300 400x300", GetViewport().ToString());
225 GetMagnificationController()->MoveWindow(400, 0, false);
226 EXPECT_EQ("400,0 400x300", GetViewport().ToString());
228 GetMagnificationController()->MoveWindow(400, 300, false);
229 EXPECT_EQ("400,300 400x300", GetViewport().ToString());
231 // Confirms that the viewport can't across the top-left border.
232 GetMagnificationController()->MoveWindow(-100, 0, false);
233 EXPECT_EQ("0,0 400x300", GetViewport().ToString());
235 GetMagnificationController()->MoveWindow(0, -100, false);
236 EXPECT_EQ("0,0 400x300", GetViewport().ToString());
238 GetMagnificationController()->MoveWindow(-100, -100, false);
239 EXPECT_EQ("0,0 400x300", GetViewport().ToString());
241 // Confirms that the viewport can't across the bittom-right border.
242 GetMagnificationController()->MoveWindow(800, 0, false);
243 EXPECT_EQ("400,0 400x300", GetViewport().ToString());
245 GetMagnificationController()->MoveWindow(0, 400, false);
246 EXPECT_EQ("0,300 400x300", GetViewport().ToString());
248 GetMagnificationController()->MoveWindow(200, 400, false);
249 EXPECT_EQ("200,300 400x300", GetViewport().ToString());
251 GetMagnificationController()->MoveWindow(1000, 1000, false);
252 EXPECT_EQ("400,300 400x300", GetViewport().ToString());
255 TEST_F(MagnificationControllerTest
, PointOfInterest
) {
256 ui::test::EventGenerator
generator(Shell::GetPrimaryRootWindow());
258 generator
.MoveMouseToInHost(gfx::Point(0, 0));
259 EXPECT_EQ("0,0", CurrentPointOfInterest());
261 generator
.MoveMouseToInHost(gfx::Point(799, 599));
262 EXPECT_EQ("799,599", CurrentPointOfInterest());
264 generator
.MoveMouseToInHost(gfx::Point(400, 300));
265 EXPECT_EQ("400,300", CurrentPointOfInterest());
267 GetMagnificationController()->SetEnabled(true);
268 EXPECT_EQ("400,300", CurrentPointOfInterest());
270 generator
.MoveMouseToInHost(gfx::Point(500, 400));
271 EXPECT_EQ("450,350", CurrentPointOfInterest());
274 TEST_F(MagnificationControllerTest
, PanWindow2xLeftToRight
) {
275 const aura::Env
* env
= aura::Env::GetInstance();
276 ui::test::EventGenerator
generator(Shell::GetPrimaryRootWindow());
278 generator
.MoveMouseToInHost(gfx::Point(0, 0));
279 EXPECT_EQ(1.f
, GetMagnificationController()->GetScale());
280 EXPECT_EQ("0,0 800x600", GetViewport().ToString());
281 EXPECT_EQ("0,0", env
->last_mouse_location().ToString());
283 // Enables magnifier and confirm the viewport is at center.
284 GetMagnificationController()->SetEnabled(true);
285 EXPECT_EQ(2.0f
, GetMagnificationController()->GetScale());
287 GetMagnificationController()->MoveWindow(0, 0, false);
288 generator
.MoveMouseToInHost(gfx::Point(0, 0));
289 EXPECT_EQ("0,0", env
->last_mouse_location().ToString());
290 EXPECT_EQ("0,0 400x300", GetViewport().ToString());
292 generator
.MoveMouseToInHost(gfx::Point(300, 150));
293 EXPECT_EQ("150,75", env
->last_mouse_location().ToString());
294 EXPECT_EQ("0,0 400x300", GetViewport().ToString());
296 generator
.MoveMouseToInHost(gfx::Point(700, 150));
297 EXPECT_EQ("350,75", env
->last_mouse_location().ToString());
298 EXPECT_EQ("0,0 400x300", GetViewport().ToString());
300 generator
.MoveMouseToInHost(gfx::Point(701, 150));
301 EXPECT_EQ("350,75", env
->last_mouse_location().ToString());
302 EXPECT_EQ("0,0 400x300", GetViewport().ToString());
304 generator
.MoveMouseToInHost(gfx::Point(702, 150));
305 EXPECT_EQ("351,75", env
->last_mouse_location().ToString());
306 EXPECT_EQ("1,0 400x300", GetViewport().ToString());
308 generator
.MoveMouseToInHost(gfx::Point(703, 150));
309 EXPECT_EQ("352,75", env
->last_mouse_location().ToString());
310 EXPECT_EQ("2,0 400x300", GetViewport().ToString());
312 generator
.MoveMouseToInHost(gfx::Point(704, 150));
313 EXPECT_EQ("354,75", env
->last_mouse_location().ToString());
314 EXPECT_EQ("4,0 400x300", GetViewport().ToString());
316 generator
.MoveMouseToInHost(gfx::Point(712, 150));
317 EXPECT_EQ("360,75", env
->last_mouse_location().ToString());
318 EXPECT_EQ("10,0 400x300", GetViewport().ToString());
320 generator
.MoveMouseToInHost(gfx::Point(600, 150));
321 EXPECT_EQ("310,75", env
->last_mouse_location().ToString());
322 EXPECT_EQ("10,0 400x300", GetViewport().ToString());
324 generator
.MoveMouseToInHost(gfx::Point(720, 150));
325 EXPECT_EQ("370,75", env
->last_mouse_location().ToString());
326 EXPECT_EQ("20,0 400x300", GetViewport().ToString());
328 generator
.MoveMouseToInHost(gfx::Point(780, 150));
329 EXPECT_EQ("410,75", env
->last_mouse_location().ToString());
330 EXPECT_EQ("410,75", CurrentPointOfInterest());
331 EXPECT_EQ("60,0 400x300", GetViewport().ToString());
333 generator
.MoveMouseToInHost(gfx::Point(799, 150));
334 EXPECT_EQ("459,75", env
->last_mouse_location().ToString());
335 EXPECT_EQ("109,0 400x300", GetViewport().ToString());
337 generator
.MoveMouseToInHost(gfx::Point(702, 150));
338 EXPECT_EQ("460,75", env
->last_mouse_location().ToString());
339 EXPECT_EQ("110,0 400x300", GetViewport().ToString());
341 generator
.MoveMouseToInHost(gfx::Point(780, 150));
342 EXPECT_EQ("500,75", env
->last_mouse_location().ToString());
343 EXPECT_EQ("150,0 400x300", GetViewport().ToString());
345 generator
.MoveMouseToInHost(gfx::Point(780, 150));
346 EXPECT_EQ("540,75", env
->last_mouse_location().ToString());
347 EXPECT_EQ("190,0 400x300", GetViewport().ToString());
349 generator
.MoveMouseToInHost(gfx::Point(780, 150));
350 EXPECT_EQ("580,75", env
->last_mouse_location().ToString());
351 EXPECT_EQ("230,0 400x300", GetViewport().ToString());
353 generator
.MoveMouseToInHost(gfx::Point(780, 150));
354 EXPECT_EQ("620,75", env
->last_mouse_location().ToString());
355 EXPECT_EQ("270,0 400x300", GetViewport().ToString());
357 generator
.MoveMouseToInHost(gfx::Point(780, 150));
358 EXPECT_EQ("660,75", env
->last_mouse_location().ToString());
359 EXPECT_EQ("310,0 400x300", GetViewport().ToString());
361 generator
.MoveMouseToInHost(gfx::Point(780, 150));
362 EXPECT_EQ("700,75", env
->last_mouse_location().ToString());
363 EXPECT_EQ("350,0 400x300", GetViewport().ToString());
365 generator
.MoveMouseToInHost(gfx::Point(780, 150));
366 EXPECT_EQ("740,75", env
->last_mouse_location().ToString());
367 EXPECT_EQ("390,0 400x300", GetViewport().ToString());
369 generator
.MoveMouseToInHost(gfx::Point(780, 150));
370 EXPECT_EQ("780,75", env
->last_mouse_location().ToString());
371 EXPECT_EQ("400,0 400x300", GetViewport().ToString());
373 generator
.MoveMouseToInHost(gfx::Point(799, 150));
374 EXPECT_EQ("799,75", env
->last_mouse_location().ToString());
375 EXPECT_EQ("400,0 400x300", GetViewport().ToString());
378 TEST_F(MagnificationControllerTest
, PanWindow2xRightToLeft
) {
379 const aura::Env
* env
= aura::Env::GetInstance();
380 ui::test::EventGenerator
generator(Shell::GetPrimaryRootWindow());
382 generator
.MoveMouseToInHost(gfx::Point(799, 300));
383 EXPECT_EQ(1.f
, GetMagnificationController()->GetScale());
384 EXPECT_EQ("0,0 800x600", GetViewport().ToString());
385 EXPECT_EQ("799,300", env
->last_mouse_location().ToString());
387 // Enables magnifier and confirm the viewport is at center.
388 GetMagnificationController()->SetEnabled(true);
390 generator
.MoveMouseToInHost(gfx::Point(799, 300));
391 EXPECT_EQ("798,300", env
->last_mouse_location().ToString());
392 EXPECT_EQ("400,150 400x300", GetViewport().ToString());
394 generator
.MoveMouseToInHost(gfx::Point(0, 300));
395 EXPECT_EQ("400,300", env
->last_mouse_location().ToString());
396 EXPECT_EQ("350,150 400x300", GetViewport().ToString());
398 generator
.MoveMouseToInHost(gfx::Point(0, 300));
399 EXPECT_EQ("350,300", env
->last_mouse_location().ToString());
400 EXPECT_EQ("300,150 400x300", GetViewport().ToString());
402 generator
.MoveMouseToInHost(gfx::Point(0, 300));
403 EXPECT_EQ("300,300", env
->last_mouse_location().ToString());
404 EXPECT_EQ("250,150 400x300", GetViewport().ToString());
406 generator
.MoveMouseToInHost(gfx::Point(0, 300));
407 EXPECT_EQ("250,300", env
->last_mouse_location().ToString());
408 EXPECT_EQ("200,150 400x300", GetViewport().ToString());
410 generator
.MoveMouseToInHost(gfx::Point(0, 300));
411 EXPECT_EQ("200,300", env
->last_mouse_location().ToString());
412 EXPECT_EQ("150,150 400x300", GetViewport().ToString());
414 generator
.MoveMouseToInHost(gfx::Point(0, 300));
415 EXPECT_EQ("150,300", env
->last_mouse_location().ToString());
416 EXPECT_EQ("100,150 400x300", GetViewport().ToString());
418 generator
.MoveMouseToInHost(gfx::Point(0, 300));
419 EXPECT_EQ("100,300", env
->last_mouse_location().ToString());
420 EXPECT_EQ("50,150 400x300", GetViewport().ToString());
422 generator
.MoveMouseToInHost(gfx::Point(0, 300));
423 EXPECT_EQ("50,300", env
->last_mouse_location().ToString());
424 EXPECT_EQ("0,150 400x300", GetViewport().ToString());
426 generator
.MoveMouseToInHost(gfx::Point(0, 300));
427 EXPECT_EQ("0,300", env
->last_mouse_location().ToString());
428 EXPECT_EQ("0,150 400x300", GetViewport().ToString());
431 TEST_F(MagnificationControllerTest
, PanWindowToRight
) {
432 const aura::Env
* env
= aura::Env::GetInstance();
433 ui::test::EventGenerator
generator(Shell::GetPrimaryRootWindow());
435 generator
.MoveMouseToInHost(gfx::Point(400, 300));
436 EXPECT_EQ(1.f
, GetMagnificationController()->GetScale());
437 EXPECT_EQ("0,0 800x600", GetViewport().ToString());
438 EXPECT_EQ("400,300", env
->last_mouse_location().ToString());
442 // Enables magnifier and confirm the viewport is at center.
443 GetMagnificationController()->SetEnabled(true);
444 EXPECT_FLOAT_EQ(2.f
, GetMagnificationController()->GetScale());
446 scale
*= ui::kMagnificationScaleFactor
;
447 GetMagnificationController()->SetScale(scale
, false);
448 EXPECT_FLOAT_EQ(2.3784142, GetMagnificationController()->GetScale());
449 generator
.MoveMouseToInHost(gfx::Point(400, 300));
450 EXPECT_EQ("400,300", env
->last_mouse_location().ToString());
451 generator
.MoveMouseToInHost(gfx::Point(799, 300));
452 EXPECT_EQ("566,299", env
->last_mouse_location().ToString());
453 EXPECT_EQ("705,300", GetHostMouseLocation());
455 scale
*= ui::kMagnificationScaleFactor
;
456 GetMagnificationController()->SetScale(scale
, false);
457 EXPECT_FLOAT_EQ(2.8284268, GetMagnificationController()->GetScale());
458 generator
.MoveMouseToInHost(gfx::Point(799, 300));
459 EXPECT_EQ("599,299", env
->last_mouse_location().ToString());
460 EXPECT_EQ("702,300", GetHostMouseLocation());
462 scale
*= ui::kMagnificationScaleFactor
;
463 GetMagnificationController()->SetScale(scale
, false);
464 EXPECT_FLOAT_EQ(3.3635852, GetMagnificationController()->GetScale());
465 generator
.MoveMouseToInHost(gfx::Point(799, 300));
466 EXPECT_EQ("627,298", env
->last_mouse_location().ToString());
467 EXPECT_EQ("707,300", GetHostMouseLocation());
469 scale
*= ui::kMagnificationScaleFactor
;
470 GetMagnificationController()->SetScale(scale
, false);
471 EXPECT_FLOAT_EQ(4.f
, GetMagnificationController()->GetScale());
472 generator
.MoveMouseToInHost(gfx::Point(799, 300));
473 EXPECT_EQ("649,298", env
->last_mouse_location().ToString());
474 EXPECT_EQ("704,300", GetHostMouseLocation());
477 TEST_F(MagnificationControllerTest
, PanWindowToLeft
) {
478 const aura::Env
* env
= aura::Env::GetInstance();
479 ui::test::EventGenerator
generator(Shell::GetPrimaryRootWindow());
481 generator
.MoveMouseToInHost(gfx::Point(400, 300));
482 EXPECT_EQ(1.f
, GetMagnificationController()->GetScale());
483 EXPECT_EQ("0,0 800x600", GetViewport().ToString());
484 EXPECT_EQ("400,300", env
->last_mouse_location().ToString());
488 // Enables magnifier and confirm the viewport is at center.
489 GetMagnificationController()->SetEnabled(true);
490 EXPECT_FLOAT_EQ(2.f
, GetMagnificationController()->GetScale());
492 scale
*= ui::kMagnificationScaleFactor
;
493 GetMagnificationController()->SetScale(scale
, false);
494 EXPECT_FLOAT_EQ(2.3784142, GetMagnificationController()->GetScale());
495 generator
.MoveMouseToInHost(gfx::Point(400, 300));
496 EXPECT_EQ("400,300", env
->last_mouse_location().ToString());
497 generator
.MoveMouseToInHost(gfx::Point(0, 300));
498 EXPECT_EQ("231,299", env
->last_mouse_location().ToString());
499 EXPECT_EQ("100,300", GetHostMouseLocation());
501 scale
*= ui::kMagnificationScaleFactor
;
502 GetMagnificationController()->SetScale(scale
, false);
503 EXPECT_FLOAT_EQ(2.8284268, GetMagnificationController()->GetScale());
504 generator
.MoveMouseToInHost(gfx::Point(0, 300));
505 EXPECT_EQ("194,299", env
->last_mouse_location().ToString());
506 EXPECT_EQ("99,300", GetHostMouseLocation());
508 scale
*= ui::kMagnificationScaleFactor
;
509 GetMagnificationController()->SetScale(scale
, false);
510 EXPECT_FLOAT_EQ(3.3635852, GetMagnificationController()->GetScale());
511 generator
.MoveMouseToInHost(gfx::Point(0, 300));
512 EXPECT_EQ("164,298", env
->last_mouse_location().ToString());
513 EXPECT_EQ("98,300", GetHostMouseLocation());
515 scale
*= ui::kMagnificationScaleFactor
;
516 GetMagnificationController()->SetScale(scale
, false);
517 EXPECT_FLOAT_EQ(4.f
, GetMagnificationController()->GetScale());
518 generator
.MoveMouseToInHost(gfx::Point(0, 300));
519 EXPECT_EQ("139,298", env
->last_mouse_location().ToString());
520 EXPECT_EQ("100,300", GetHostMouseLocation());
523 TEST_F(MagnificationControllerTest
, FollowTextInputFieldFocus
) {
524 CreateAndShowTextInputView(gfx::Rect(500, 300, 80, 80));
525 gfx::Rect text_input_bounds
= GetTextInputViewBounds();
527 // Enables magnifier and confirm the viewport is at center.
528 GetMagnificationController()->SetEnabled(true);
529 EXPECT_EQ(2.0f
, GetMagnificationController()->GetScale());
530 EXPECT_EQ("200,150 400x300", GetViewport().ToString());
531 EXPECT_FALSE(GetMagnificationController()->KeepFocusCentered());
533 // Move the viewport to (0, 0), so that text input field will be out of
534 // the viewport region.
535 GetMagnificationController()->MoveWindow(0, 0, false);
536 EXPECT_EQ("0,0 400x300", GetViewport().ToString());
537 EXPECT_FALSE(GetViewport().Intersects(text_input_bounds
));
539 // Focus on the text input field.
540 FocusOnTextInputView();
542 // Verify the view port has been moved to the place where the text field is
543 // contained in the view port and the caret is at the center of the view port.
544 gfx::Rect view_port
= GetViewport();
545 EXPECT_TRUE(view_port
.Contains(text_input_bounds
));
546 gfx::Rect caret_bounds
= GetCaretBounds();
547 EXPECT_TRUE(text_input_bounds
.Contains(caret_bounds
));
548 EXPECT_EQ(caret_bounds
.CenterPoint(), view_port
.CenterPoint());
551 // Tests the following case. First the text input field intersects on the right
552 // edge with the view port, with focus caret sitting just a little left to the
553 // caret panning margin, so that when it gets focus, the view port won't move.
554 // Then when user types a character, the caret moves beyond the right panning
555 // edge, the view port will be moved to center the caret horizontally.
556 TEST_F(MagnificationControllerTest
, FollowTextInputFieldKeyPress
) {
557 const int kCaretPanningMargin
= 50;
558 const int kScale
= 2.0f
;
559 const int kViewportWidth
= 400;
560 // Add some extra distance horizontally from text caret to to left edge of
561 // the text input view.
562 int x
= kViewportWidth
- (kCaretPanningMargin
+ 20) / kScale
;
563 CreateAndShowTextInputView(gfx::Rect(x
, 200, 80, 80));
564 gfx::Rect text_input_bounds
= GetTextInputViewBounds();
566 // Enables magnifier and confirm the viewport is at center.
567 GetMagnificationController()->SetEnabled(true);
568 EXPECT_EQ(2.0f
, GetMagnificationController()->GetScale());
569 EXPECT_EQ("200,150 400x300", GetViewport().ToString());
570 EXPECT_FALSE(GetMagnificationController()->KeepFocusCentered());
572 // Move the viewport to (0, 0), so that text input field intersects the
573 // view port at the right edge.
574 GetMagnificationController()->MoveWindow(0, 0, false);
575 EXPECT_EQ("0,0 400x300", GetViewport().ToString());
576 EXPECT_TRUE(GetViewport().Intersects(text_input_bounds
));
578 // Focus on the text input field.
579 FocusOnTextInputView();
581 // Verify the view port is not moved, and the caret is inside the view port
582 // and not beyond the caret right panning margin.
583 gfx::Rect view_port
= GetViewport();
584 EXPECT_EQ("0,0 400x300", view_port
.ToString());
585 EXPECT_TRUE(text_input_bounds
.Contains(GetCaretBounds()));
586 EXPECT_GT(view_port
.right() - kCaretPanningMargin
/ kScale
,
587 GetCaretBounds().x());
589 // Press keys on text input simulate typing on text field and the caret
590 // moves beyond the caret right panning margin. The view port is moved to the
591 // place where caret's x coordinate is centered at the new view port.
592 ui::test::EventGenerator
generator(Shell::GetPrimaryRootWindow());
593 generator
.PressKey(ui::VKEY_A
, 0);
594 generator
.ReleaseKey(ui::VKEY_A
, 0);
595 gfx::Rect caret_bounds
= GetCaretBounds();
596 EXPECT_LT(view_port
.right() - kCaretPanningMargin
/ kScale
,
597 GetCaretBounds().x());
599 gfx::Rect new_view_port
= GetViewport();
600 EXPECT_EQ(caret_bounds
.CenterPoint().x(), new_view_port
.CenterPoint().x());
603 TEST_F(MagnificationControllerTest
, CenterTextCaretNotInsideViewport
) {
604 CreateAndShowTextInputView(gfx::Rect(500, 300, 50, 30));
605 gfx::Rect text_input_bounds
= GetTextInputViewBounds();
607 // Enables magnifier and confirm the viewport is at center.
608 GetMagnificationController()->SetKeepFocusCentered(true);
609 GetMagnificationController()->SetEnabled(true);
610 EXPECT_EQ(2.0f
, GetMagnificationController()->GetScale());
611 EXPECT_EQ("200,150 400x300", GetViewport().ToString());
612 EXPECT_TRUE(GetMagnificationController()->KeepFocusCentered());
614 // Move the viewport to (0, 0), so that text input field will be out of
615 // the viewport region.
616 GetMagnificationController()->MoveWindow(0, 0, false);
617 EXPECT_EQ("0,0 400x300", GetViewport().ToString());
618 EXPECT_FALSE(GetViewport().Contains(text_input_bounds
));
620 // Focus on the text input field.
621 FocusOnTextInputView();
622 RunAllPendingInMessageLoop();
623 // Verify the view port has been moved to the place where the text field is
624 // contained in the view port and the caret is at the center of the view port.
625 gfx::Rect view_port
= GetViewport();
626 EXPECT_TRUE(view_port
.Contains(text_input_bounds
));
627 gfx::Rect caret_bounds
= GetCaretBounds();
628 EXPECT_EQ(caret_bounds
.CenterPoint(), view_port
.CenterPoint());
630 // Press keys on text input simulate typing on text field and the view port
631 // should be moved to keep the caret centered.
632 ui::test::EventGenerator
generator(Shell::GetPrimaryRootWindow());
633 generator
.PressKey(ui::VKEY_A
, 0);
634 generator
.ReleaseKey(ui::VKEY_A
, 0);
635 RunAllPendingInMessageLoop();
636 gfx::Rect new_caret_bounds
= GetCaretBounds();
637 EXPECT_NE(caret_bounds
, new_caret_bounds
);
639 gfx::Rect new_view_port
= GetViewport();
640 EXPECT_NE(view_port
, new_view_port
);
641 EXPECT_TRUE(new_view_port
.Contains(new_caret_bounds
));
642 EXPECT_EQ(new_caret_bounds
.CenterPoint(), new_view_port
.CenterPoint());
645 TEST_F(MagnificationControllerTest
, CenterTextCaretInViewport
) {
646 CreateAndShowTextInputView(gfx::Rect(250, 200, 50, 30));
647 gfx::Rect text_input_bounds
= GetTextInputViewBounds();
649 // Enables magnifier and confirm the viewport is at center.
650 GetMagnificationController()->SetKeepFocusCentered(true);
651 GetMagnificationController()->SetEnabled(true);
652 EXPECT_EQ(2.0f
, GetMagnificationController()->GetScale());
653 EXPECT_EQ("200,150 400x300", GetViewport().ToString());
654 EXPECT_TRUE(GetMagnificationController()->KeepFocusCentered());
656 // Verify the text input field is inside the view port.
657 gfx::Rect view_port
= GetViewport();
658 EXPECT_TRUE(view_port
.Contains(text_input_bounds
));
660 // Focus on the text input field.
661 FocusOnTextInputView();
662 RunAllPendingInMessageLoop();
664 // Verify the view port has been moved to the place where the text field is
665 // contained in the view port and the caret is at the center of the view port.
666 gfx::Rect new_view_port
= GetViewport();
667 EXPECT_NE(view_port
, new_view_port
);
668 EXPECT_TRUE(new_view_port
.Contains(text_input_bounds
));
669 gfx::Rect caret_bounds
= GetCaretBounds();
670 EXPECT_EQ(caret_bounds
.CenterPoint(), new_view_port
.CenterPoint());
674 // Make sure that unified desktop can enter magnified mode.
675 TEST_F(MagnificationControllerTest
, EnableMagnifierInUnifiedDesktop
) {
676 if (!SupportsMultipleDisplays())
678 test::DisplayManagerTestApi::EnableUnifiedDesktopForTest();
680 DisplayManager
* display_manager
= Shell::GetInstance()->display_manager();
681 display_manager
->SetDefaultMultiDisplayMode(DisplayManager::UNIFIED
);
682 display_manager
->SetMultiDisplayMode(DisplayManager::UNIFIED
);
684 EXPECT_EQ(1.0f
, GetMagnificationController()->GetScale());
686 GetMagnificationController()->SetEnabled(true);
688 gfx::Screen
* screen
=
689 gfx::Screen::GetScreenFor(Shell::GetPrimaryRootWindow());
691 UpdateDisplay("500x500, 500x500");
692 EXPECT_EQ("0,0 1000x500", screen
->GetPrimaryDisplay().bounds().ToString());
693 EXPECT_EQ(2.0f
, GetMagnificationController()->GetScale());
695 GetMagnificationController()->SetEnabled(false);
697 EXPECT_EQ(1.0f
, GetMagnificationController()->GetScale());
699 GetMagnificationController()->SetEnabled(true);
700 EXPECT_EQ(2.0f
, GetMagnificationController()->GetScale());
702 UpdateDisplay("500x500");
703 EXPECT_EQ("0,0 500x500", screen
->GetPrimaryDisplay().bounds().ToString());
704 EXPECT_EQ(2.0f
, GetMagnificationController()->GetScale());
706 GetMagnificationController()->SetEnabled(false);
707 EXPECT_EQ("0,0 500x500", screen
->GetPrimaryDisplay().bounds().ToString());
708 EXPECT_EQ(1.0f
, GetMagnificationController()->GetScale());