blob: 4b37e32e7fc22e643c8ce9e148739904bc601861 [file] [log] [blame]
[email protected]7e368732013-01-16 13:31:231// 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
[email protected]7e368732013-01-16 13:31:235#include "ash/magnifier/magnification_controller.h"
[email protected]3dff2ef2014-02-09 22:50:396
jamescook27417442016-06-20 21:37:057#include "ash/common/accessibility_types.h"
oshima628a6172015-08-01 01:33:148#include "ash/display/display_manager.h"
[email protected]7e368732013-01-16 13:31:239#include "ash/shell.h"
10#include "ash/test/ash_test_base.h"
[email protected]0836da02013-06-10 19:33:3511#include "base/strings/stringprintf.h"
[email protected]6f162492013-04-26 09:02:0912#include "ui/aura/env.h"
[email protected]dc645d52014-04-28 14:42:5513#include "ui/aura/test/aura_test_utils.h"
[email protected]7a60cd3a2014-03-20 20:54:5714#include "ui/aura/window_tree_host.h"
jennyz91b6ed0c2014-10-23 21:52:4115#include "ui/base/ime/input_method.h"
oshimaf84b0da722016-04-27 19:47:1916#include "ui/display/screen.h"
[email protected]73c9fd02014-07-28 01:48:5217#include "ui/events/test/event_generator.h"
Avi Drissmanfefc2f82014-12-22 19:25:2918#include "ui/gfx/geometry/rect_conversions.h"
jennyz91b6ed0c2014-10-23 21:52:4119#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"
[email protected]7e368732013-01-16 13:31:2324
25namespace ash {
[email protected]7e368732013-01-16 13:31:2326namespace {
27
28const int kRootHeight = 600;
29const int kRootWidth = 800;
30
jennyz91b6ed0c2014-10-23 21:52:4131const int kTextInputWindowWidth = 50;
32const int kTextInputWindowHeight = 50;
33
34class 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
dcheng222b9c72015-01-16 00:48:0142 ~TextInputView() override {}
jennyz91b6ed0c2014-10-23 21:52:4143
dcheng222b9c72015-01-16 00:48:0144 gfx::Size GetPreferredSize() const override {
jennyz91b6ed0c2014-10-23 21:52:4145 return gfx::Size(kTextInputWindowWidth, kTextInputWindowHeight);
46 }
47
48 // Overridden from views::WidgetDelegate:
dcheng222b9c72015-01-16 00:48:0149 views::View* GetContentsView() override { return this; }
jennyz91b6ed0c2014-10-23 21:52:4150
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
[email protected]7e368732013-01-16 13:31:2359} // namespace
60
61class MagnificationControllerTest: public test::AshTestBase {
62 public:
jennyz91b6ed0c2014-10-23 21:52:4163 MagnificationControllerTest() : text_input_view_(NULL) {}
dcheng222b9c72015-01-16 00:48:0164 ~MagnificationControllerTest() override {}
[email protected]7e368732013-01-16 13:31:2365
dcheng222b9c72015-01-16 00:48:0166 void SetUp() override {
[email protected]7e368732013-01-16 13:31:2367 AshTestBase::SetUp();
[email protected]7d3cbc92013-03-18 22:33:0468 UpdateDisplay(base::StringPrintf("%dx%d", kRootWidth, kRootHeight));
[email protected]7e368732013-01-16 13:31:2369
[email protected]7e368732013-01-16 13:31:2370#if defined(OS_WIN)
71 // RootWindow and Display can't resize on Windows Ash.
72 // http://crbug.com/165962
pkasting7da13a52016-06-13 21:51:1673 aura::Window* root = GetRootWindow();
74 gfx::Rect root_bounds(root->bounds());
[email protected]7e368732013-01-16 13:31:2375 EXPECT_EQ(kRootHeight, root_bounds.height());
76 EXPECT_EQ(kRootWidth, root_bounds.width());
77#endif
jennyzb894b6d2015-06-01 20:12:1878
79 GetMagnificationController()->DisableMoveMagnifierDelayForTesting();
[email protected]7e368732013-01-16 13:31:2380 }
81
dcheng222b9c72015-01-16 00:48:0182 void TearDown() override { AshTestBase::TearDown(); }
[email protected]7e368732013-01-16 13:31:2383
84 protected:
[email protected]bf9cdb362013-10-25 19:22:4585 aura::Window* GetRootWindow() const {
[email protected]7e368732013-01-16 13:31:2386 return Shell::GetPrimaryRootWindow();
87 }
88
[email protected]6f162492013-04-26 09:02:0989 std::string GetHostMouseLocation() {
[email protected]dc645d52014-04-28 14:42:5590 const gfx::Point& location =
91 aura::test::QueryLatestMousePositionRequestInHost(
92 GetRootWindow()->GetHost());
93 return location.ToString();
[email protected]6f162492013-04-26 09:02:0994 }
95
96 ash::MagnificationController* GetMagnificationController() const {
[email protected]7e368732013-01-16 13:31:2397 return ash::Shell::GetInstance()->magnification_controller();
98 }
99
[email protected]6f162492013-04-26 09:02:09100 gfx::Rect GetViewport() const {
[email protected]7e368732013-01-16 13:31:23101 gfx::RectF bounds(0, 0, kRootWidth, kRootHeight);
102 GetRootWindow()->layer()->transform().TransformRectReverse(&bounds);
103 return gfx::ToEnclosingRect(bounds);
104 }
105
[email protected]6f162492013-04-26 09:02:09106 std::string CurrentPointOfInterest() const {
107 return GetMagnificationController()->
108 GetPointOfInterestForTesting().ToString();
109 }
110
jennyz91b6ed0c2014-10-23 21:52:41111 void CreateAndShowTextInputView(const gfx::Rect& bounds) {
112 text_input_view_ = new TextInputView;
113 views::Widget* widget = views::Widget::CreateWindowWithContextAndBounds(
114 text_input_view_, GetRootWindow(), bounds);
115 widget->Show();
116 }
117
118 // Returns the text input view's bounds in root window coordinates.
119 gfx::Rect GetTextInputViewBounds() {
120 DCHECK(text_input_view_);
121 gfx::Rect bounds = text_input_view_->bounds();
122 gfx::Point origin = bounds.origin();
123 // Convert origin to screen coordinates.
124 views::View::ConvertPointToScreen(text_input_view_, &origin);
125 // Convert origin to root_window_ coordinates.
sky984c1892016-04-20 00:00:34126 ::wm::ConvertPointFromScreen(GetRootWindow(), &origin);
jennyz91b6ed0c2014-10-23 21:52:41127 return gfx::Rect(origin.x(), origin.y(), bounds.width(), bounds.height());
128 }
129
130 // Returns the caret bounds in root window coordinates.
131 gfx::Rect GetCaretBounds() {
132 gfx::Rect caret_bounds =
133 GetInputMethod()->GetTextInputClient()->GetCaretBounds();
134 gfx::Point origin = caret_bounds.origin();
sky984c1892016-04-20 00:00:34135 ::wm::ConvertPointFromScreen(GetRootWindow(), &origin);
jennyz91b6ed0c2014-10-23 21:52:41136 return gfx::Rect(
137 origin.x(), origin.y(), caret_bounds.width(), caret_bounds.height());
138 }
139
140 void FocusOnTextInputView() {
141 DCHECK(text_input_view_);
142 text_input_view_->FocusOnTextInput();
143 }
144
[email protected]7e368732013-01-16 13:31:23145 private:
jennyz91b6ed0c2014-10-23 21:52:41146 TextInputView* text_input_view_;
147
148 ui::InputMethod* GetInputMethod() {
149 DCHECK(text_input_view_);
shuchen12f77cb2015-07-02 02:11:14150 return text_input_view_->GetWidget()->GetInputMethod();
jennyz91b6ed0c2014-10-23 21:52:41151 }
152
[email protected]7e368732013-01-16 13:31:23153 DISALLOW_COPY_AND_ASSIGN(MagnificationControllerTest);
154};
155
156TEST_F(MagnificationControllerTest, EnableAndDisable) {
157 // Confirms the magnifier is disabled.
158 EXPECT_TRUE(GetRootWindow()->layer()->transform().IsIdentity());
159 EXPECT_EQ(1.0f, GetMagnificationController()->GetScale());
160 EXPECT_EQ("0,0 800x600", GetViewport().ToString());
161
162 // Enables magnifier.
163 GetMagnificationController()->SetEnabled(true);
164 EXPECT_FALSE(GetRootWindow()->layer()->transform().IsIdentity());
165 EXPECT_EQ(2.0f, GetMagnificationController()->GetScale());
166 EXPECT_EQ("200,150 400x300", GetViewport().ToString());
167
168 // Disables magnifier.
169 GetMagnificationController()->SetEnabled(false);
170 EXPECT_TRUE(GetRootWindow()->layer()->transform().IsIdentity());
171 EXPECT_EQ(1.0f, GetMagnificationController()->GetScale());
172 EXPECT_EQ("0,0 800x600", GetViewport().ToString());
173
174 // Confirms the the scale can't be changed.
175 GetMagnificationController()->SetScale(4.0f, false);
176 EXPECT_TRUE(GetRootWindow()->layer()->transform().IsIdentity());
177 EXPECT_EQ(1.0f, GetMagnificationController()->GetScale());
178 EXPECT_EQ("0,0 800x600", GetViewport().ToString());
179}
180
181TEST_F(MagnificationControllerTest, MagnifyAndUnmagnify) {
182 // Enables magnifier and confirms the default scale is 2.0x.
183 GetMagnificationController()->SetEnabled(true);
184 EXPECT_FALSE(GetRootWindow()->layer()->transform().IsIdentity());
185 EXPECT_EQ(2.0f, GetMagnificationController()->GetScale());
186 EXPECT_EQ("200,150 400x300", GetViewport().ToString());
[email protected]6f162492013-04-26 09:02:09187 EXPECT_EQ("400,300", CurrentPointOfInterest());
[email protected]7e368732013-01-16 13:31:23188
189 // Changes the scale.
190 GetMagnificationController()->SetScale(4.0f, false);
191 EXPECT_EQ(4.0f, GetMagnificationController()->GetScale());
192 EXPECT_EQ("300,225 200x150", GetViewport().ToString());
[email protected]6f162492013-04-26 09:02:09193 EXPECT_EQ("400,300", CurrentPointOfInterest());
[email protected]7e368732013-01-16 13:31:23194
195 GetMagnificationController()->SetScale(1.0f, false);
196 EXPECT_EQ(1.0f, GetMagnificationController()->GetScale());
197 EXPECT_EQ("0,0 800x600", GetViewport().ToString());
[email protected]6f162492013-04-26 09:02:09198 EXPECT_EQ("400,300", CurrentPointOfInterest());
[email protected]7e368732013-01-16 13:31:23199
200 GetMagnificationController()->SetScale(3.0f, false);
201 EXPECT_EQ(3.0f, GetMagnificationController()->GetScale());
[email protected]0ec58342013-04-24 07:39:36202 EXPECT_EQ("266,200 267x200", GetViewport().ToString());
[email protected]6f162492013-04-26 09:02:09203 EXPECT_EQ("400,300", CurrentPointOfInterest());
[email protected]7e368732013-01-16 13:31:23204}
205
206TEST_F(MagnificationControllerTest, MoveWindow) {
207 // Enables magnifier and confirm the viewport is at center.
208 GetMagnificationController()->SetEnabled(true);
209 EXPECT_EQ(2.0f, GetMagnificationController()->GetScale());
210 EXPECT_EQ("200,150 400x300", GetViewport().ToString());
211
212 // Move the viewport.
213 GetMagnificationController()->MoveWindow(0, 0, false);
214 EXPECT_EQ("0,0 400x300", GetViewport().ToString());
215
216 GetMagnificationController()->MoveWindow(200, 300, false);
217 EXPECT_EQ("200,300 400x300", GetViewport().ToString());
218
219 GetMagnificationController()->MoveWindow(400, 0, false);
220 EXPECT_EQ("400,0 400x300", GetViewport().ToString());
221
222 GetMagnificationController()->MoveWindow(400, 300, false);
223 EXPECT_EQ("400,300 400x300", GetViewport().ToString());
224
225 // Confirms that the viewport can't across the top-left border.
226 GetMagnificationController()->MoveWindow(-100, 0, false);
227 EXPECT_EQ("0,0 400x300", GetViewport().ToString());
228
229 GetMagnificationController()->MoveWindow(0, -100, false);
230 EXPECT_EQ("0,0 400x300", GetViewport().ToString());
231
232 GetMagnificationController()->MoveWindow(-100, -100, false);
233 EXPECT_EQ("0,0 400x300", GetViewport().ToString());
234
235 // Confirms that the viewport can't across the bittom-right border.
236 GetMagnificationController()->MoveWindow(800, 0, false);
237 EXPECT_EQ("400,0 400x300", GetViewport().ToString());
238
239 GetMagnificationController()->MoveWindow(0, 400, false);
240 EXPECT_EQ("0,300 400x300", GetViewport().ToString());
241
242 GetMagnificationController()->MoveWindow(200, 400, false);
243 EXPECT_EQ("200,300 400x300", GetViewport().ToString());
244
245 GetMagnificationController()->MoveWindow(1000, 1000, false);
246 EXPECT_EQ("400,300 400x300", GetViewport().ToString());
247}
248
[email protected]6f162492013-04-26 09:02:09249TEST_F(MagnificationControllerTest, PointOfInterest) {
kinaba5d46c352016-03-31 01:04:46250 GetEventGenerator().MoveMouseToInHost(gfx::Point(0, 0));
[email protected]6f162492013-04-26 09:02:09251 EXPECT_EQ("0,0", CurrentPointOfInterest());
252
kinaba5d46c352016-03-31 01:04:46253 GetEventGenerator().MoveMouseToInHost(gfx::Point(799, 599));
[email protected]6f162492013-04-26 09:02:09254 EXPECT_EQ("799,599", CurrentPointOfInterest());
255
kinaba5d46c352016-03-31 01:04:46256 GetEventGenerator().MoveMouseToInHost(gfx::Point(400, 300));
[email protected]6f162492013-04-26 09:02:09257 EXPECT_EQ("400,300", CurrentPointOfInterest());
258
259 GetMagnificationController()->SetEnabled(true);
260 EXPECT_EQ("400,300", CurrentPointOfInterest());
261
kinaba5d46c352016-03-31 01:04:46262 GetEventGenerator().MoveMouseToInHost(gfx::Point(500, 400));
[email protected]6f162492013-04-26 09:02:09263 EXPECT_EQ("450,350", CurrentPointOfInterest());
264}
265
glevin0043bf42016-01-08 22:05:13266TEST_F(MagnificationControllerTest, FollowFocusChanged) {
267 // Enables magnifier and confirm the viewport is at center.
268 GetMagnificationController()->SetEnabled(true);
269 EXPECT_EQ(2.0f, GetMagnificationController()->GetScale());
270 EXPECT_EQ("200,150 400x300", GetViewport().ToString());
271
272 // Don't move viewport when focusing edit box.
273 GetMagnificationController()->HandleFocusedNodeChanged(
274 true, gfx::Rect(0, 0, 10, 10));
275 EXPECT_EQ("200,150 400x300", GetViewport().ToString());
276
277 // Move viewport to element in upper left.
278 GetMagnificationController()->HandleFocusedNodeChanged(
279 false, gfx::Rect(0, 0, 10, 10));
280 EXPECT_EQ("0,0 400x300", GetViewport().ToString());
281
282 // Move viewport to element in lower right.
283 GetMagnificationController()->HandleFocusedNodeChanged(
284 false, gfx::Rect(790, 590, 10, 10));
285 EXPECT_EQ("400,300 400x300", GetViewport().ToString());
286
287 // Don't follow focus onto empty rectangle.
288 GetMagnificationController()->HandleFocusedNodeChanged(
289 false, gfx::Rect(0, 0, 0, 0));
290 EXPECT_EQ("400,300 400x300", GetViewport().ToString());
291}
292
[email protected]6f162492013-04-26 09:02:09293TEST_F(MagnificationControllerTest, PanWindow2xLeftToRight) {
294 const aura::Env* env = aura::Env::GetInstance();
295
kinaba5d46c352016-03-31 01:04:46296 GetEventGenerator().MoveMouseToInHost(gfx::Point(0, 0));
[email protected]6f162492013-04-26 09:02:09297 EXPECT_EQ(1.f, GetMagnificationController()->GetScale());
298 EXPECT_EQ("0,0 800x600", GetViewport().ToString());
299 EXPECT_EQ("0,0", env->last_mouse_location().ToString());
300
301 // Enables magnifier and confirm the viewport is at center.
302 GetMagnificationController()->SetEnabled(true);
303 EXPECT_EQ(2.0f, GetMagnificationController()->GetScale());
304
305 GetMagnificationController()->MoveWindow(0, 0, false);
kinaba5d46c352016-03-31 01:04:46306 GetEventGenerator().MoveMouseToInHost(gfx::Point(0, 0));
[email protected]6f162492013-04-26 09:02:09307 EXPECT_EQ("0,0", env->last_mouse_location().ToString());
308 EXPECT_EQ("0,0 400x300", GetViewport().ToString());
309
kinaba5d46c352016-03-31 01:04:46310 GetEventGenerator().MoveMouseToInHost(gfx::Point(300, 150));
[email protected]6f162492013-04-26 09:02:09311 EXPECT_EQ("150,75", env->last_mouse_location().ToString());
312 EXPECT_EQ("0,0 400x300", GetViewport().ToString());
313
kinaba5d46c352016-03-31 01:04:46314 GetEventGenerator().MoveMouseToInHost(gfx::Point(700, 150));
[email protected]6f162492013-04-26 09:02:09315 EXPECT_EQ("350,75", env->last_mouse_location().ToString());
316 EXPECT_EQ("0,0 400x300", GetViewport().ToString());
317
kinaba5d46c352016-03-31 01:04:46318 GetEventGenerator().MoveMouseToInHost(gfx::Point(701, 150));
[email protected]6f162492013-04-26 09:02:09319 EXPECT_EQ("350,75", env->last_mouse_location().ToString());
320 EXPECT_EQ("0,0 400x300", GetViewport().ToString());
321
kinaba5d46c352016-03-31 01:04:46322 GetEventGenerator().MoveMouseToInHost(gfx::Point(702, 150));
[email protected]6f162492013-04-26 09:02:09323 EXPECT_EQ("351,75", env->last_mouse_location().ToString());
324 EXPECT_EQ("1,0 400x300", GetViewport().ToString());
325
kinaba5d46c352016-03-31 01:04:46326 GetEventGenerator().MoveMouseToInHost(gfx::Point(703, 150));
[email protected]6f162492013-04-26 09:02:09327 EXPECT_EQ("352,75", env->last_mouse_location().ToString());
328 EXPECT_EQ("2,0 400x300", GetViewport().ToString());
329
kinaba5d46c352016-03-31 01:04:46330 GetEventGenerator().MoveMouseToInHost(gfx::Point(704, 150));
[email protected]6f162492013-04-26 09:02:09331 EXPECT_EQ("354,75", env->last_mouse_location().ToString());
332 EXPECT_EQ("4,0 400x300", GetViewport().ToString());
333
kinaba5d46c352016-03-31 01:04:46334 GetEventGenerator().MoveMouseToInHost(gfx::Point(712, 150));
[email protected]6f162492013-04-26 09:02:09335 EXPECT_EQ("360,75", env->last_mouse_location().ToString());
336 EXPECT_EQ("10,0 400x300", GetViewport().ToString());
337
kinaba5d46c352016-03-31 01:04:46338 GetEventGenerator().MoveMouseToInHost(gfx::Point(600, 150));
[email protected]6f162492013-04-26 09:02:09339 EXPECT_EQ("310,75", env->last_mouse_location().ToString());
340 EXPECT_EQ("10,0 400x300", GetViewport().ToString());
341
kinaba5d46c352016-03-31 01:04:46342 GetEventGenerator().MoveMouseToInHost(gfx::Point(720, 150));
[email protected]6f162492013-04-26 09:02:09343 EXPECT_EQ("370,75", env->last_mouse_location().ToString());
344 EXPECT_EQ("20,0 400x300", GetViewport().ToString());
345
kinaba5d46c352016-03-31 01:04:46346 GetEventGenerator().MoveMouseToInHost(gfx::Point(780, 150));
[email protected]6f162492013-04-26 09:02:09347 EXPECT_EQ("410,75", env->last_mouse_location().ToString());
348 EXPECT_EQ("410,75", CurrentPointOfInterest());
349 EXPECT_EQ("60,0 400x300", GetViewport().ToString());
350
kinaba5d46c352016-03-31 01:04:46351 GetEventGenerator().MoveMouseToInHost(gfx::Point(799, 150));
[email protected]6f162492013-04-26 09:02:09352 EXPECT_EQ("459,75", env->last_mouse_location().ToString());
353 EXPECT_EQ("109,0 400x300", GetViewport().ToString());
354
kinaba5d46c352016-03-31 01:04:46355 GetEventGenerator().MoveMouseToInHost(gfx::Point(702, 150));
[email protected]6f162492013-04-26 09:02:09356 EXPECT_EQ("460,75", env->last_mouse_location().ToString());
357 EXPECT_EQ("110,0 400x300", GetViewport().ToString());
358
kinaba5d46c352016-03-31 01:04:46359 GetEventGenerator().MoveMouseToInHost(gfx::Point(780, 150));
[email protected]6f162492013-04-26 09:02:09360 EXPECT_EQ("500,75", env->last_mouse_location().ToString());
361 EXPECT_EQ("150,0 400x300", GetViewport().ToString());
362
kinaba5d46c352016-03-31 01:04:46363 GetEventGenerator().MoveMouseToInHost(gfx::Point(780, 150));
[email protected]6f162492013-04-26 09:02:09364 EXPECT_EQ("540,75", env->last_mouse_location().ToString());
365 EXPECT_EQ("190,0 400x300", GetViewport().ToString());
366
kinaba5d46c352016-03-31 01:04:46367 GetEventGenerator().MoveMouseToInHost(gfx::Point(780, 150));
[email protected]6f162492013-04-26 09:02:09368 EXPECT_EQ("580,75", env->last_mouse_location().ToString());
369 EXPECT_EQ("230,0 400x300", GetViewport().ToString());
370
kinaba5d46c352016-03-31 01:04:46371 GetEventGenerator().MoveMouseToInHost(gfx::Point(780, 150));
[email protected]6f162492013-04-26 09:02:09372 EXPECT_EQ("620,75", env->last_mouse_location().ToString());
373 EXPECT_EQ("270,0 400x300", GetViewport().ToString());
374
kinaba5d46c352016-03-31 01:04:46375 GetEventGenerator().MoveMouseToInHost(gfx::Point(780, 150));
[email protected]6f162492013-04-26 09:02:09376 EXPECT_EQ("660,75", env->last_mouse_location().ToString());
377 EXPECT_EQ("310,0 400x300", GetViewport().ToString());
378
kinaba5d46c352016-03-31 01:04:46379 GetEventGenerator().MoveMouseToInHost(gfx::Point(780, 150));
[email protected]6f162492013-04-26 09:02:09380 EXPECT_EQ("700,75", env->last_mouse_location().ToString());
381 EXPECT_EQ("350,0 400x300", GetViewport().ToString());
382
kinaba5d46c352016-03-31 01:04:46383 GetEventGenerator().MoveMouseToInHost(gfx::Point(780, 150));
[email protected]6f162492013-04-26 09:02:09384 EXPECT_EQ("740,75", env->last_mouse_location().ToString());
385 EXPECT_EQ("390,0 400x300", GetViewport().ToString());
386
kinaba5d46c352016-03-31 01:04:46387 GetEventGenerator().MoveMouseToInHost(gfx::Point(780, 150));
[email protected]6f162492013-04-26 09:02:09388 EXPECT_EQ("780,75", env->last_mouse_location().ToString());
389 EXPECT_EQ("400,0 400x300", GetViewport().ToString());
390
kinaba5d46c352016-03-31 01:04:46391 GetEventGenerator().MoveMouseToInHost(gfx::Point(799, 150));
[email protected]6f162492013-04-26 09:02:09392 EXPECT_EQ("799,75", env->last_mouse_location().ToString());
393 EXPECT_EQ("400,0 400x300", GetViewport().ToString());
394}
395
396TEST_F(MagnificationControllerTest, PanWindow2xRightToLeft) {
397 const aura::Env* env = aura::Env::GetInstance();
398
kinaba5d46c352016-03-31 01:04:46399 GetEventGenerator().MoveMouseToInHost(gfx::Point(799, 300));
[email protected]6f162492013-04-26 09:02:09400 EXPECT_EQ(1.f, GetMagnificationController()->GetScale());
401 EXPECT_EQ("0,0 800x600", GetViewport().ToString());
402 EXPECT_EQ("799,300", env->last_mouse_location().ToString());
403
404 // Enables magnifier and confirm the viewport is at center.
405 GetMagnificationController()->SetEnabled(true);
406
kinaba5d46c352016-03-31 01:04:46407 GetEventGenerator().MoveMouseToInHost(gfx::Point(799, 300));
[email protected]6f162492013-04-26 09:02:09408 EXPECT_EQ("798,300", env->last_mouse_location().ToString());
409 EXPECT_EQ("400,150 400x300", GetViewport().ToString());
410
kinaba5d46c352016-03-31 01:04:46411 GetEventGenerator().MoveMouseToInHost(gfx::Point(0, 300));
[email protected]6f162492013-04-26 09:02:09412 EXPECT_EQ("400,300", env->last_mouse_location().ToString());
413 EXPECT_EQ("350,150 400x300", GetViewport().ToString());
414
kinaba5d46c352016-03-31 01:04:46415 GetEventGenerator().MoveMouseToInHost(gfx::Point(0, 300));
[email protected]6f162492013-04-26 09:02:09416 EXPECT_EQ("350,300", env->last_mouse_location().ToString());
417 EXPECT_EQ("300,150 400x300", GetViewport().ToString());
418
kinaba5d46c352016-03-31 01:04:46419 GetEventGenerator().MoveMouseToInHost(gfx::Point(0, 300));
[email protected]6f162492013-04-26 09:02:09420 EXPECT_EQ("300,300", env->last_mouse_location().ToString());
421 EXPECT_EQ("250,150 400x300", GetViewport().ToString());
422
kinaba5d46c352016-03-31 01:04:46423 GetEventGenerator().MoveMouseToInHost(gfx::Point(0, 300));
[email protected]6f162492013-04-26 09:02:09424 EXPECT_EQ("250,300", env->last_mouse_location().ToString());
425 EXPECT_EQ("200,150 400x300", GetViewport().ToString());
426
kinaba5d46c352016-03-31 01:04:46427 GetEventGenerator().MoveMouseToInHost(gfx::Point(0, 300));
[email protected]6f162492013-04-26 09:02:09428 EXPECT_EQ("200,300", env->last_mouse_location().ToString());
429 EXPECT_EQ("150,150 400x300", GetViewport().ToString());
430
kinaba5d46c352016-03-31 01:04:46431 GetEventGenerator().MoveMouseToInHost(gfx::Point(0, 300));
[email protected]6f162492013-04-26 09:02:09432 EXPECT_EQ("150,300", env->last_mouse_location().ToString());
433 EXPECT_EQ("100,150 400x300", GetViewport().ToString());
434
kinaba5d46c352016-03-31 01:04:46435 GetEventGenerator().MoveMouseToInHost(gfx::Point(0, 300));
[email protected]6f162492013-04-26 09:02:09436 EXPECT_EQ("100,300", env->last_mouse_location().ToString());
437 EXPECT_EQ("50,150 400x300", GetViewport().ToString());
438
kinaba5d46c352016-03-31 01:04:46439 GetEventGenerator().MoveMouseToInHost(gfx::Point(0, 300));
[email protected]6f162492013-04-26 09:02:09440 EXPECT_EQ("50,300", env->last_mouse_location().ToString());
441 EXPECT_EQ("0,150 400x300", GetViewport().ToString());
442
kinaba5d46c352016-03-31 01:04:46443 GetEventGenerator().MoveMouseToInHost(gfx::Point(0, 300));
[email protected]6f162492013-04-26 09:02:09444 EXPECT_EQ("0,300", env->last_mouse_location().ToString());
445 EXPECT_EQ("0,150 400x300", GetViewport().ToString());
446}
447
448TEST_F(MagnificationControllerTest, PanWindowToRight) {
449 const aura::Env* env = aura::Env::GetInstance();
450
kinaba5d46c352016-03-31 01:04:46451 GetEventGenerator().MoveMouseToInHost(gfx::Point(400, 300));
[email protected]6f162492013-04-26 09:02:09452 EXPECT_EQ(1.f, GetMagnificationController()->GetScale());
453 EXPECT_EQ("0,0 800x600", GetViewport().ToString());
454 EXPECT_EQ("400,300", env->last_mouse_location().ToString());
455
456 float scale = 2.f;
457
458 // Enables magnifier and confirm the viewport is at center.
459 GetMagnificationController()->SetEnabled(true);
460 EXPECT_FLOAT_EQ(2.f, GetMagnificationController()->GetScale());
461
pkotwicz0991c822014-10-31 04:21:03462 scale *= ui::kMagnificationScaleFactor;
[email protected]6f162492013-04-26 09:02:09463 GetMagnificationController()->SetScale(scale, false);
464 EXPECT_FLOAT_EQ(2.3784142, GetMagnificationController()->GetScale());
kinaba5d46c352016-03-31 01:04:46465 GetEventGenerator().MoveMouseToInHost(gfx::Point(400, 300));
[email protected]6f162492013-04-26 09:02:09466 EXPECT_EQ("400,300", env->last_mouse_location().ToString());
kinaba5d46c352016-03-31 01:04:46467 GetEventGenerator().MoveMouseToInHost(gfx::Point(799, 300));
[email protected]6f162492013-04-26 09:02:09468 EXPECT_EQ("566,299", env->last_mouse_location().ToString());
469 EXPECT_EQ("705,300", GetHostMouseLocation());
470
pkotwicz0991c822014-10-31 04:21:03471 scale *= ui::kMagnificationScaleFactor;
[email protected]6f162492013-04-26 09:02:09472 GetMagnificationController()->SetScale(scale, false);
473 EXPECT_FLOAT_EQ(2.8284268, GetMagnificationController()->GetScale());
kinaba5d46c352016-03-31 01:04:46474 GetEventGenerator().MoveMouseToInHost(gfx::Point(799, 300));
[email protected]6f162492013-04-26 09:02:09475 EXPECT_EQ("599,299", env->last_mouse_location().ToString());
476 EXPECT_EQ("702,300", GetHostMouseLocation());
477
pkotwicz0991c822014-10-31 04:21:03478 scale *= ui::kMagnificationScaleFactor;
[email protected]6f162492013-04-26 09:02:09479 GetMagnificationController()->SetScale(scale, false);
480 EXPECT_FLOAT_EQ(3.3635852, GetMagnificationController()->GetScale());
kinaba5d46c352016-03-31 01:04:46481 GetEventGenerator().MoveMouseToInHost(gfx::Point(799, 300));
[email protected]6f162492013-04-26 09:02:09482 EXPECT_EQ("627,298", env->last_mouse_location().ToString());
483 EXPECT_EQ("707,300", GetHostMouseLocation());
484
pkotwicz0991c822014-10-31 04:21:03485 scale *= ui::kMagnificationScaleFactor;
[email protected]6f162492013-04-26 09:02:09486 GetMagnificationController()->SetScale(scale, false);
487 EXPECT_FLOAT_EQ(4.f, GetMagnificationController()->GetScale());
kinaba5d46c352016-03-31 01:04:46488 GetEventGenerator().MoveMouseToInHost(gfx::Point(799, 300));
[email protected]6f162492013-04-26 09:02:09489 EXPECT_EQ("649,298", env->last_mouse_location().ToString());
490 EXPECT_EQ("704,300", GetHostMouseLocation());
491}
492
493TEST_F(MagnificationControllerTest, PanWindowToLeft) {
494 const aura::Env* env = aura::Env::GetInstance();
495
kinaba5d46c352016-03-31 01:04:46496 GetEventGenerator().MoveMouseToInHost(gfx::Point(400, 300));
[email protected]6f162492013-04-26 09:02:09497 EXPECT_EQ(1.f, GetMagnificationController()->GetScale());
498 EXPECT_EQ("0,0 800x600", GetViewport().ToString());
499 EXPECT_EQ("400,300", env->last_mouse_location().ToString());
500
501 float scale = 2.f;
502
503 // Enables magnifier and confirm the viewport is at center.
504 GetMagnificationController()->SetEnabled(true);
505 EXPECT_FLOAT_EQ(2.f, GetMagnificationController()->GetScale());
506
pkotwicz0991c822014-10-31 04:21:03507 scale *= ui::kMagnificationScaleFactor;
[email protected]6f162492013-04-26 09:02:09508 GetMagnificationController()->SetScale(scale, false);
509 EXPECT_FLOAT_EQ(2.3784142, GetMagnificationController()->GetScale());
kinaba5d46c352016-03-31 01:04:46510 GetEventGenerator().MoveMouseToInHost(gfx::Point(400, 300));
[email protected]6f162492013-04-26 09:02:09511 EXPECT_EQ("400,300", env->last_mouse_location().ToString());
kinaba5d46c352016-03-31 01:04:46512 GetEventGenerator().MoveMouseToInHost(gfx::Point(0, 300));
[email protected]6f162492013-04-26 09:02:09513 EXPECT_EQ("231,299", env->last_mouse_location().ToString());
514 EXPECT_EQ("100,300", GetHostMouseLocation());
515
pkotwicz0991c822014-10-31 04:21:03516 scale *= ui::kMagnificationScaleFactor;
[email protected]6f162492013-04-26 09:02:09517 GetMagnificationController()->SetScale(scale, false);
518 EXPECT_FLOAT_EQ(2.8284268, GetMagnificationController()->GetScale());
kinaba5d46c352016-03-31 01:04:46519 GetEventGenerator().MoveMouseToInHost(gfx::Point(0, 300));
[email protected]a15328272013-10-02 22:52:03520 EXPECT_EQ("194,299", env->last_mouse_location().ToString());
[email protected]6f162492013-04-26 09:02:09521 EXPECT_EQ("99,300", GetHostMouseLocation());
522
pkotwicz0991c822014-10-31 04:21:03523 scale *= ui::kMagnificationScaleFactor;
[email protected]6f162492013-04-26 09:02:09524 GetMagnificationController()->SetScale(scale, false);
525 EXPECT_FLOAT_EQ(3.3635852, GetMagnificationController()->GetScale());
kinaba5d46c352016-03-31 01:04:46526 GetEventGenerator().MoveMouseToInHost(gfx::Point(0, 300));
[email protected]a15328272013-10-02 22:52:03527 EXPECT_EQ("164,298", env->last_mouse_location().ToString());
[email protected]6f162492013-04-26 09:02:09528 EXPECT_EQ("98,300", GetHostMouseLocation());
529
pkotwicz0991c822014-10-31 04:21:03530 scale *= ui::kMagnificationScaleFactor;
[email protected]6f162492013-04-26 09:02:09531 GetMagnificationController()->SetScale(scale, false);
532 EXPECT_FLOAT_EQ(4.f, GetMagnificationController()->GetScale());
kinaba5d46c352016-03-31 01:04:46533 GetEventGenerator().MoveMouseToInHost(gfx::Point(0, 300));
[email protected]a15328272013-10-02 22:52:03534 EXPECT_EQ("139,298", env->last_mouse_location().ToString());
[email protected]6f162492013-04-26 09:02:09535 EXPECT_EQ("100,300", GetHostMouseLocation());
536}
537
jennyz91b6ed0c2014-10-23 21:52:41538TEST_F(MagnificationControllerTest, FollowTextInputFieldFocus) {
539 CreateAndShowTextInputView(gfx::Rect(500, 300, 80, 80));
540 gfx::Rect text_input_bounds = GetTextInputViewBounds();
541
542 // Enables magnifier and confirm the viewport is at center.
543 GetMagnificationController()->SetEnabled(true);
544 EXPECT_EQ(2.0f, GetMagnificationController()->GetScale());
545 EXPECT_EQ("200,150 400x300", GetViewport().ToString());
jennyzb894b6d2015-06-01 20:12:18546 EXPECT_FALSE(GetMagnificationController()->KeepFocusCentered());
jennyz91b6ed0c2014-10-23 21:52:41547
548 // Move the viewport to (0, 0), so that text input field will be out of
549 // the viewport region.
550 GetMagnificationController()->MoveWindow(0, 0, false);
551 EXPECT_EQ("0,0 400x300", GetViewport().ToString());
552 EXPECT_FALSE(GetViewport().Intersects(text_input_bounds));
553
554 // Focus on the text input field.
555 FocusOnTextInputView();
556
557 // Verify the view port has been moved to the place where the text field is
558 // contained in the view port and the caret is at the center of the view port.
559 gfx::Rect view_port = GetViewport();
560 EXPECT_TRUE(view_port.Contains(text_input_bounds));
561 gfx::Rect caret_bounds = GetCaretBounds();
562 EXPECT_TRUE(text_input_bounds.Contains(caret_bounds));
jennyzb894b6d2015-06-01 20:12:18563 EXPECT_EQ(caret_bounds.CenterPoint(), view_port.CenterPoint());
jennyz91b6ed0c2014-10-23 21:52:41564}
565
jennyzb894b6d2015-06-01 20:12:18566// Tests the following case. First the text input field intersects on the right
567// edge with the view port, with focus caret sitting just a little left to the
568// caret panning margin, so that when it gets focus, the view port won't move.
569// Then when user types a character, the caret moves beyond the right panning
570// edge, the view port will be moved to center the caret horizontally.
jennyz91b6ed0c2014-10-23 21:52:41571TEST_F(MagnificationControllerTest, FollowTextInputFieldKeyPress) {
jennyzb894b6d2015-06-01 20:12:18572 const int kCaretPanningMargin = 50;
573 const int kScale = 2.0f;
574 const int kViewportWidth = 400;
575 // Add some extra distance horizontally from text caret to to left edge of
576 // the text input view.
577 int x = kViewportWidth - (kCaretPanningMargin + 20) / kScale;
578 CreateAndShowTextInputView(gfx::Rect(x, 200, 80, 80));
jennyz91b6ed0c2014-10-23 21:52:41579 gfx::Rect text_input_bounds = GetTextInputViewBounds();
580
581 // Enables magnifier and confirm the viewport is at center.
582 GetMagnificationController()->SetEnabled(true);
583 EXPECT_EQ(2.0f, GetMagnificationController()->GetScale());
584 EXPECT_EQ("200,150 400x300", GetViewport().ToString());
jennyzb894b6d2015-06-01 20:12:18585 EXPECT_FALSE(GetMagnificationController()->KeepFocusCentered());
jennyz91b6ed0c2014-10-23 21:52:41586
587 // Move the viewport to (0, 0), so that text input field intersects the
588 // view port at the right edge.
589 GetMagnificationController()->MoveWindow(0, 0, false);
590 EXPECT_EQ("0,0 400x300", GetViewport().ToString());
591 EXPECT_TRUE(GetViewport().Intersects(text_input_bounds));
592
593 // Focus on the text input field.
594 FocusOnTextInputView();
595
jennyzb894b6d2015-06-01 20:12:18596 // Verify the view port is not moved, and the caret is inside the view port
597 // and not beyond the caret right panning margin.
jennyz91b6ed0c2014-10-23 21:52:41598 gfx::Rect view_port = GetViewport();
599 EXPECT_EQ("0,0 400x300", view_port.ToString());
jennyz91b6ed0c2014-10-23 21:52:41600 EXPECT_TRUE(text_input_bounds.Contains(GetCaretBounds()));
jennyzb894b6d2015-06-01 20:12:18601 EXPECT_GT(view_port.right() - kCaretPanningMargin / kScale,
602 GetCaretBounds().x());
jennyz91b6ed0c2014-10-23 21:52:41603
604 // Press keys on text input simulate typing on text field and the caret
jennyzb894b6d2015-06-01 20:12:18605 // moves beyond the caret right panning margin. The view port is moved to the
606 // place where caret's x coordinate is centered at the new view port.
kinaba5d46c352016-03-31 01:04:46607 GetEventGenerator().PressKey(ui::VKEY_A, 0);
608 GetEventGenerator().ReleaseKey(ui::VKEY_A, 0);
jennyz91b6ed0c2014-10-23 21:52:41609 gfx::Rect caret_bounds = GetCaretBounds();
jennyzb894b6d2015-06-01 20:12:18610 EXPECT_LT(view_port.right() - kCaretPanningMargin / kScale,
611 GetCaretBounds().x());
jennyz91b6ed0c2014-10-23 21:52:41612
613 gfx::Rect new_view_port = GetViewport();
jennyzb894b6d2015-06-01 20:12:18614 EXPECT_EQ(caret_bounds.CenterPoint().x(), new_view_port.CenterPoint().x());
jennyz91b6ed0c2014-10-23 21:52:41615}
616
jennyzb894b6d2015-06-01 20:12:18617TEST_F(MagnificationControllerTest, CenterTextCaretNotInsideViewport) {
618 CreateAndShowTextInputView(gfx::Rect(500, 300, 50, 30));
619 gfx::Rect text_input_bounds = GetTextInputViewBounds();
620
621 // Enables magnifier and confirm the viewport is at center.
622 GetMagnificationController()->SetKeepFocusCentered(true);
623 GetMagnificationController()->SetEnabled(true);
624 EXPECT_EQ(2.0f, GetMagnificationController()->GetScale());
625 EXPECT_EQ("200,150 400x300", GetViewport().ToString());
626 EXPECT_TRUE(GetMagnificationController()->KeepFocusCentered());
627
628 // Move the viewport to (0, 0), so that text input field will be out of
629 // the viewport region.
630 GetMagnificationController()->MoveWindow(0, 0, false);
631 EXPECT_EQ("0,0 400x300", GetViewport().ToString());
632 EXPECT_FALSE(GetViewport().Contains(text_input_bounds));
633
634 // Focus on the text input field.
635 FocusOnTextInputView();
636 RunAllPendingInMessageLoop();
637 // Verify the view port has been moved to the place where the text field is
638 // contained in the view port and the caret is at the center of the view port.
639 gfx::Rect view_port = GetViewport();
640 EXPECT_TRUE(view_port.Contains(text_input_bounds));
641 gfx::Rect caret_bounds = GetCaretBounds();
642 EXPECT_EQ(caret_bounds.CenterPoint(), view_port.CenterPoint());
643
644 // Press keys on text input simulate typing on text field and the view port
645 // should be moved to keep the caret centered.
kinaba5d46c352016-03-31 01:04:46646 GetEventGenerator().PressKey(ui::VKEY_A, 0);
647 GetEventGenerator().ReleaseKey(ui::VKEY_A, 0);
jennyzb894b6d2015-06-01 20:12:18648 RunAllPendingInMessageLoop();
649 gfx::Rect new_caret_bounds = GetCaretBounds();
650 EXPECT_NE(caret_bounds, new_caret_bounds);
651
652 gfx::Rect new_view_port = GetViewport();
653 EXPECT_NE(view_port, new_view_port);
654 EXPECT_TRUE(new_view_port.Contains(new_caret_bounds));
655 EXPECT_EQ(new_caret_bounds.CenterPoint(), new_view_port.CenterPoint());
656}
657
658TEST_F(MagnificationControllerTest, CenterTextCaretInViewport) {
659 CreateAndShowTextInputView(gfx::Rect(250, 200, 50, 30));
660 gfx::Rect text_input_bounds = GetTextInputViewBounds();
661
662 // Enables magnifier and confirm the viewport is at center.
663 GetMagnificationController()->SetKeepFocusCentered(true);
664 GetMagnificationController()->SetEnabled(true);
665 EXPECT_EQ(2.0f, GetMagnificationController()->GetScale());
666 EXPECT_EQ("200,150 400x300", GetViewport().ToString());
667 EXPECT_TRUE(GetMagnificationController()->KeepFocusCentered());
668
669 // Verify the text input field is inside the view port.
670 gfx::Rect view_port = GetViewport();
671 EXPECT_TRUE(view_port.Contains(text_input_bounds));
672
673 // Focus on the text input field.
674 FocusOnTextInputView();
675 RunAllPendingInMessageLoop();
676
677 // Verify the view port has been moved to the place where the text field is
678 // contained in the view port and the caret is at the center of the view port.
679 gfx::Rect new_view_port = GetViewport();
680 EXPECT_NE(view_port, new_view_port);
681 EXPECT_TRUE(new_view_port.Contains(text_input_bounds));
682 gfx::Rect caret_bounds = GetCaretBounds();
683 EXPECT_EQ(caret_bounds.CenterPoint(), new_view_port.CenterPoint());
684}
685
686
oshima38dffad2015-05-05 17:22:56687// Make sure that unified desktop can enter magnified mode.
688TEST_F(MagnificationControllerTest, EnableMagnifierInUnifiedDesktop) {
689 if (!SupportsMultipleDisplays())
690 return;
oshima628a6172015-08-01 01:33:14691 Shell::GetInstance()->display_manager()->SetUnifiedDesktopEnabled(true);
oshima38dffad2015-05-05 17:22:56692
oshima38dffad2015-05-05 17:22:56693 EXPECT_EQ(1.0f, GetMagnificationController()->GetScale());
694
695 GetMagnificationController()->SetEnabled(true);
696
oshimaf84b0da722016-04-27 19:47:19697 display::Screen* screen = display::Screen::GetScreen();
oshima38dffad2015-05-05 17:22:56698
699 UpdateDisplay("500x500, 500x500");
700 EXPECT_EQ("0,0 1000x500", screen->GetPrimaryDisplay().bounds().ToString());
701 EXPECT_EQ(2.0f, GetMagnificationController()->GetScale());
702
703 GetMagnificationController()->SetEnabled(false);
704
705 EXPECT_EQ(1.0f, GetMagnificationController()->GetScale());
706
707 GetMagnificationController()->SetEnabled(true);
708 EXPECT_EQ(2.0f, GetMagnificationController()->GetScale());
709
710 UpdateDisplay("500x500");
711 EXPECT_EQ("0,0 500x500", screen->GetPrimaryDisplay().bounds().ToString());
712 EXPECT_EQ(2.0f, GetMagnificationController()->GetScale());
713
714 GetMagnificationController()->SetEnabled(false);
715 EXPECT_EQ("0,0 500x500", screen->GetPrimaryDisplay().bounds().ToString());
716 EXPECT_EQ(1.0f, GetMagnificationController()->GetScale());
717}
718
[email protected]7e368732013-01-16 13:31:23719} // namespace ash