PermissionRequestCreatorApiary: switch from deprecated "namespace" to "eventType"
[chromium-blink-merge.git] / ash / display / root_window_transformers_unittest.cc
blob17c8b36310e3d59d67d6be122950ccd361f4370f
1 // Copyright 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/display/root_window_transformers.h"
7 #include "ash/display/display_info.h"
8 #include "ash/display/display_manager.h"
9 #include "ash/host/root_window_transformer.h"
10 #include "ash/magnifier/magnification_controller.h"
11 #include "ash/screen_util.h"
12 #include "ash/shelf/shelf.h"
13 #include "ash/shelf/shelf_widget.h"
14 #include "ash/shell.h"
15 #include "ash/test/ash_test_base.h"
16 #include "ash/test/cursor_manager_test_api.h"
17 #include "ash/test/display_manager_test_api.h"
18 #include "ash/test/mirror_window_test_api.h"
19 #include "base/synchronization/waitable_event.h"
20 #include "ui/aura/env.h"
21 #include "ui/aura/window_event_dispatcher.h"
22 #include "ui/aura/window_tracker.h"
23 #include "ui/events/event_handler.h"
24 #include "ui/events/test/event_generator.h"
25 #include "ui/gfx/display.h"
26 #include "ui/gfx/geometry/rect_conversions.h"
27 #include "ui/gfx/screen.h"
28 #include "ui/views/widget/widget.h"
30 namespace ash {
31 namespace {
33 const char kDesktopBackgroundView[] = "DesktopBackgroundView";
35 class TestEventHandler : public ui::EventHandler {
36 public:
37 TestEventHandler() : target_root_(NULL),
38 touch_radius_x_(0.0),
39 touch_radius_y_(0.0),
40 scroll_x_offset_(0.0),
41 scroll_y_offset_(0.0),
42 scroll_x_offset_ordinal_(0.0),
43 scroll_y_offset_ordinal_(0.0) {}
44 ~TestEventHandler() override {}
46 void OnMouseEvent(ui::MouseEvent* event) override {
47 if (event->flags() & ui::EF_IS_SYNTHESIZED)
48 return;
49 aura::Window* target = static_cast<aura::Window*>(event->target());
50 mouse_location_ = event->root_location();
51 target_root_ = target->GetRootWindow();
52 event->StopPropagation();
55 void OnTouchEvent(ui::TouchEvent* event) override {
56 aura::Window* target = static_cast<aura::Window*>(event->target());
57 // Only record when the target is the background which covers
58 // entire root window.
59 if (target->name() != kDesktopBackgroundView)
60 return;
61 touch_radius_x_ = event->radius_x();
62 touch_radius_y_ = event->radius_y();
63 event->StopPropagation();
66 void OnScrollEvent(ui::ScrollEvent* event) override {
67 aura::Window* target = static_cast<aura::Window*>(event->target());
68 // Only record when the target is the background which covers
69 // entire root window.
70 if (target->name() != kDesktopBackgroundView)
71 return;
73 if (event->type() == ui::ET_SCROLL) {
74 scroll_x_offset_ = event->x_offset();
75 scroll_y_offset_ = event->y_offset();
76 scroll_x_offset_ordinal_ = event->x_offset_ordinal();
77 scroll_y_offset_ordinal_ = event->y_offset_ordinal();
79 event->StopPropagation();
82 std::string GetLocationAndReset() {
83 std::string result = mouse_location_.ToString();
84 mouse_location_.SetPoint(0, 0);
85 target_root_ = NULL;
86 return result;
89 float touch_radius_x() const { return touch_radius_x_; }
90 float touch_radius_y() const { return touch_radius_y_; }
91 float scroll_x_offset() const { return scroll_x_offset_; }
92 float scroll_y_offset() const { return scroll_y_offset_; }
93 float scroll_x_offset_ordinal() const { return scroll_x_offset_ordinal_; }
94 float scroll_y_offset_ordinal() const { return scroll_y_offset_ordinal_; }
96 private:
97 gfx::Point mouse_location_;
98 aura::Window* target_root_;
100 float touch_radius_x_;
101 float touch_radius_y_;
102 float scroll_x_offset_;
103 float scroll_y_offset_;
104 float scroll_x_offset_ordinal_;
105 float scroll_y_offset_ordinal_;
107 DISALLOW_COPY_AND_ASSIGN(TestEventHandler);
110 gfx::Display::Rotation GetStoredRotation(int64 id) {
111 return Shell::GetInstance()->display_manager()->GetDisplayInfo(id).rotation();
114 float GetStoredUIScale(int64 id) {
115 return Shell::GetInstance()->display_manager()->GetDisplayInfo(id).
116 GetEffectiveUIScale();
119 } // namespace
121 typedef test::AshTestBase RootWindowTransformersTest;
123 #if defined(OS_WIN)
124 // TODO(scottmg): RootWindow doesn't get resized on Windows
125 // Ash. http://crbug.com/247916.
126 #define MAYBE_RotateAndMagnify DISABLED_RotateAndMagniy
127 #define MAYBE_TouchScaleAndMagnify DISABLED_TouchScaleAndMagnify
128 #define MAYBE_ConvertHostToRootCoords DISABLED_ConvertHostToRootCoords
129 #else
130 #define MAYBE_RotateAndMagnify RotateAndMagniy
131 #define MAYBE_TouchScaleAndMagnify TouchScaleAndMagnify
132 #define MAYBE_ConvertHostToRootCoords ConvertHostToRootCoords
133 #endif
135 TEST_F(RootWindowTransformersTest, MAYBE_RotateAndMagnify) {
136 MagnificationController* magnifier =
137 Shell::GetInstance()->magnification_controller();
138 DisplayManager* display_manager = Shell::GetInstance()->display_manager();
140 TestEventHandler event_handler;
141 Shell::GetInstance()->AddPreTargetHandler(&event_handler);
143 UpdateDisplay("120x200,300x400*2");
144 gfx::Display display1 = Shell::GetScreen()->GetPrimaryDisplay();
145 int64 display2_id = ScreenUtil::GetSecondaryDisplay().id();
147 aura::Window::Windows root_windows = Shell::GetAllRootWindows();
148 ui::test::EventGenerator generator1(root_windows[0]);
149 ui::test::EventGenerator generator2(root_windows[1]);
151 magnifier->SetEnabled(true);
152 EXPECT_EQ(2.0f, magnifier->GetScale());
153 EXPECT_EQ("120x200", root_windows[0]->bounds().size().ToString());
154 EXPECT_EQ("150x200", root_windows[1]->bounds().size().ToString());
155 EXPECT_EQ("120,0 150x200",
156 ScreenUtil::GetSecondaryDisplay().bounds().ToString());
157 generator1.MoveMouseToInHost(40, 80);
158 EXPECT_EQ("50,90", event_handler.GetLocationAndReset());
159 EXPECT_EQ("50,90",
160 aura::Env::GetInstance()->last_mouse_location().ToString());
161 EXPECT_EQ(gfx::Display::ROTATE_0, GetStoredRotation(display1.id()));
162 EXPECT_EQ(gfx::Display::ROTATE_0, GetStoredRotation(display2_id));
163 magnifier->SetEnabled(false);
165 display_manager->SetDisplayRotation(display1.id(),
166 gfx::Display::ROTATE_90);
167 // Move the cursor to the center of the first root window.
168 generator1.MoveMouseToInHost(59, 100);
170 magnifier->SetEnabled(true);
171 EXPECT_EQ(2.0f, magnifier->GetScale());
172 EXPECT_EQ("200x120", root_windows[0]->bounds().size().ToString());
173 EXPECT_EQ("150x200", root_windows[1]->bounds().size().ToString());
174 EXPECT_EQ("200,0 150x200",
175 ScreenUtil::GetSecondaryDisplay().bounds().ToString());
176 generator1.MoveMouseToInHost(39, 120);
177 EXPECT_EQ("110,70", event_handler.GetLocationAndReset());
178 EXPECT_EQ("110,70",
179 aura::Env::GetInstance()->last_mouse_location().ToString());
180 EXPECT_EQ(gfx::Display::ROTATE_90, GetStoredRotation(display1.id()));
181 EXPECT_EQ(gfx::Display::ROTATE_0, GetStoredRotation(display2_id));
182 magnifier->SetEnabled(false);
184 DisplayLayout display_layout(DisplayLayout::BOTTOM, 50);
185 display_manager->SetLayoutForCurrentDisplays(display_layout);
186 EXPECT_EQ("50,120 150x200",
187 ScreenUtil::GetSecondaryDisplay().bounds().ToString());
189 display_manager->SetDisplayRotation(display2_id,
190 gfx::Display::ROTATE_270);
191 // Move the cursor to the center of the second root window.
192 generator2.MoveMouseToInHost(151, 199);
194 magnifier->SetEnabled(true);
195 EXPECT_EQ("200x120", root_windows[0]->bounds().size().ToString());
196 EXPECT_EQ("200x150", root_windows[1]->bounds().size().ToString());
197 EXPECT_EQ("50,120 200x150",
198 ScreenUtil::GetSecondaryDisplay().bounds().ToString());
199 generator2.MoveMouseToInHost(172, 219);
200 EXPECT_EQ("95,80", event_handler.GetLocationAndReset());
201 EXPECT_EQ("145,200",
202 aura::Env::GetInstance()->last_mouse_location().ToString());
203 EXPECT_EQ(gfx::Display::ROTATE_90, GetStoredRotation(display1.id()));
204 EXPECT_EQ(gfx::Display::ROTATE_270, GetStoredRotation(display2_id));
205 magnifier->SetEnabled(false);
207 display_manager->SetDisplayRotation(display1.id(),
208 gfx::Display::ROTATE_180);
209 // Move the cursor to the center of the first root window.
210 generator1.MoveMouseToInHost(59, 99);
212 magnifier->SetEnabled(true);
213 EXPECT_EQ("120x200", root_windows[0]->bounds().size().ToString());
214 EXPECT_EQ("200x150", root_windows[1]->bounds().size().ToString());
215 // Dislay must share at least 100, so the x's offset becomes 20.
216 EXPECT_EQ("20,200 200x150",
217 ScreenUtil::GetSecondaryDisplay().bounds().ToString());
218 generator1.MoveMouseToInHost(39, 59);
219 EXPECT_EQ("70,120", event_handler.GetLocationAndReset());
220 EXPECT_EQ(gfx::Display::ROTATE_180, GetStoredRotation(display1.id()));
221 EXPECT_EQ(gfx::Display::ROTATE_270, GetStoredRotation(display2_id));
222 magnifier->SetEnabled(false);
224 Shell::GetInstance()->RemovePreTargetHandler(&event_handler);
227 TEST_F(RootWindowTransformersTest, ScaleAndMagnify) {
228 if (!SupportsMultipleDisplays())
229 return;
231 TestEventHandler event_handler;
232 Shell::GetInstance()->AddPreTargetHandler(&event_handler);
234 UpdateDisplay("600x400*2@1.5,500x300");
236 gfx::Display display1 = Shell::GetScreen()->GetPrimaryDisplay();
237 test::DisplayManagerTestApi(Shell::GetInstance()->display_manager())
238 .SetInternalDisplayId(display1.id());
239 gfx::Display display2 = ScreenUtil::GetSecondaryDisplay();
240 aura::Window::Windows root_windows = Shell::GetAllRootWindows();
241 MagnificationController* magnifier =
242 Shell::GetInstance()->magnification_controller();
244 magnifier->SetEnabled(true);
245 EXPECT_EQ(2.0f, magnifier->GetScale());
246 EXPECT_EQ("0,0 450x300", display1.bounds().ToString());
247 EXPECT_EQ("0,0 450x300", root_windows[0]->bounds().ToString());
248 EXPECT_EQ("450,0 500x300", display2.bounds().ToString());
249 EXPECT_EQ(1.5f, GetStoredUIScale(display1.id()));
250 EXPECT_EQ(1.0f, GetStoredUIScale(display2.id()));
252 ui::test::EventGenerator generator(root_windows[0]);
253 generator.MoveMouseToInHost(500, 200);
254 EXPECT_EQ("299,150", event_handler.GetLocationAndReset());
255 magnifier->SetEnabled(false);
257 DisplayManager* display_manager = Shell::GetInstance()->display_manager();
258 display_manager->SetDisplayUIScale(display1.id(), 1.25);
259 display1 = Shell::GetScreen()->GetPrimaryDisplay();
260 display2 = ScreenUtil::GetSecondaryDisplay();
261 magnifier->SetEnabled(true);
262 EXPECT_EQ(2.0f, magnifier->GetScale());
263 EXPECT_EQ("0,0 375x250", display1.bounds().ToString());
264 EXPECT_EQ("0,0 375x250", root_windows[0]->bounds().ToString());
265 EXPECT_EQ("375,0 500x300", display2.bounds().ToString());
266 EXPECT_EQ(1.25f, GetStoredUIScale(display1.id()));
267 EXPECT_EQ(1.0f, GetStoredUIScale(display2.id()));
268 magnifier->SetEnabled(false);
270 Shell::GetInstance()->RemovePreTargetHandler(&event_handler);
273 TEST_F(RootWindowTransformersTest, MAYBE_TouchScaleAndMagnify) {
274 TestEventHandler event_handler;
275 Shell::GetInstance()->AddPreTargetHandler(&event_handler);
277 UpdateDisplay("200x200*2");
278 gfx::Display display = Shell::GetScreen()->GetPrimaryDisplay();
279 aura::Window::Windows root_windows = Shell::GetAllRootWindows();
280 aura::Window* root_window = root_windows[0];
281 ui::test::EventGenerator generator(root_window);
282 MagnificationController* magnifier =
283 Shell::GetInstance()->magnification_controller();
285 magnifier->SetEnabled(true);
286 EXPECT_FLOAT_EQ(2.0f, magnifier->GetScale());
287 magnifier->SetScale(2.5f, false);
288 EXPECT_FLOAT_EQ(2.5f, magnifier->GetScale());
289 generator.PressMoveAndReleaseTouchTo(50, 50);
290 // Default test touches have radius_x/y = 1.0, with device scale
291 // factor = 2, the scaled radius_x/y should be 0.5.
292 EXPECT_FLOAT_EQ(0.2f, event_handler.touch_radius_x());
293 EXPECT_FLOAT_EQ(0.2f, event_handler.touch_radius_y());
295 generator.ScrollSequence(gfx::Point(0,0),
296 base::TimeDelta::FromMilliseconds(100),
297 10.0, 1.0, 5, 1);
299 // ordinal_offset is invariant to the device scale factor.
300 EXPECT_FLOAT_EQ(event_handler.scroll_x_offset(),
301 event_handler.scroll_x_offset_ordinal());
302 EXPECT_FLOAT_EQ(event_handler.scroll_y_offset(),
303 event_handler.scroll_y_offset_ordinal());
304 magnifier->SetEnabled(false);
306 Shell::GetInstance()->RemovePreTargetHandler(&event_handler);
309 TEST_F(RootWindowTransformersTest, MAYBE_ConvertHostToRootCoords) {
310 TestEventHandler event_handler;
311 Shell::GetInstance()->AddPreTargetHandler(&event_handler);
312 MagnificationController* magnifier =
313 Shell::GetInstance()->magnification_controller();
315 // Test 1
316 UpdateDisplay("600x400*2/r@1.5");
318 gfx::Display display1 = Shell::GetScreen()->GetPrimaryDisplay();
319 aura::Window::Windows root_windows = Shell::GetAllRootWindows();
320 EXPECT_EQ("0,0 300x450", display1.bounds().ToString());
321 EXPECT_EQ("0,0 300x450", root_windows[0]->bounds().ToString());
322 EXPECT_EQ(1.5f, GetStoredUIScale(display1.id()));
324 ui::test::EventGenerator generator(root_windows[0]);
325 generator.MoveMouseToInHost(300, 200);
326 magnifier->SetEnabled(true);
327 EXPECT_EQ("150,224", event_handler.GetLocationAndReset());
328 EXPECT_FLOAT_EQ(2.0f, magnifier->GetScale());
330 generator.MoveMouseToInHost(300, 200);
331 EXPECT_EQ("150,224", event_handler.GetLocationAndReset());
332 generator.MoveMouseToInHost(200, 300);
333 EXPECT_EQ("187,261", event_handler.GetLocationAndReset());
334 generator.MoveMouseToInHost(100, 400);
335 EXPECT_EQ("237,299", event_handler.GetLocationAndReset());
336 generator.MoveMouseToInHost(0, 0);
337 EXPECT_EQ("137,348", event_handler.GetLocationAndReset());
339 magnifier->SetEnabled(false);
340 EXPECT_FLOAT_EQ(1.0f, magnifier->GetScale());
342 // Test 2
343 UpdateDisplay("600x400*2/u@1.5");
344 display1 = Shell::GetScreen()->GetPrimaryDisplay();
345 root_windows = Shell::GetAllRootWindows();
346 EXPECT_EQ("0,0 450x300", display1.bounds().ToString());
347 EXPECT_EQ("0,0 450x300", root_windows[0]->bounds().ToString());
348 EXPECT_EQ(1.5f, GetStoredUIScale(display1.id()));
350 generator.MoveMouseToInHost(300, 200);
351 magnifier->SetEnabled(true);
352 EXPECT_EQ("224,149", event_handler.GetLocationAndReset());
353 EXPECT_FLOAT_EQ(2.0f, magnifier->GetScale());
355 generator.MoveMouseToInHost(300, 200);
356 EXPECT_EQ("224,148", event_handler.GetLocationAndReset());
357 generator.MoveMouseToInHost(200, 300);
358 EXPECT_EQ("261,111", event_handler.GetLocationAndReset());
359 generator.MoveMouseToInHost(100, 400);
360 EXPECT_EQ("299,60", event_handler.GetLocationAndReset());
361 generator.MoveMouseToInHost(0, 0);
362 EXPECT_EQ("348,159", event_handler.GetLocationAndReset());
364 magnifier->SetEnabled(false);
365 EXPECT_FLOAT_EQ(1.0f, magnifier->GetScale());
367 // Test 3
368 UpdateDisplay("600x400*2/l@1.5");
369 display1 = Shell::GetScreen()->GetPrimaryDisplay();
370 root_windows = Shell::GetAllRootWindows();
371 EXPECT_EQ("0,0 300x450", display1.bounds().ToString());
372 EXPECT_EQ("0,0 300x450", root_windows[0]->bounds().ToString());
373 EXPECT_EQ(1.5f, GetStoredUIScale(display1.id()));
375 generator.MoveMouseToInHost(300, 200);
376 magnifier->SetEnabled(true);
377 EXPECT_EQ("149,225", event_handler.GetLocationAndReset());
378 EXPECT_FLOAT_EQ(2.0f, magnifier->GetScale());
380 generator.MoveMouseToInHost(300, 200);
381 EXPECT_EQ("148,224", event_handler.GetLocationAndReset());
382 generator.MoveMouseToInHost(200, 300);
383 EXPECT_EQ("111,187", event_handler.GetLocationAndReset());
384 generator.MoveMouseToInHost(100, 400);
385 EXPECT_EQ("60,149", event_handler.GetLocationAndReset());
386 generator.MoveMouseToInHost(0, 0);
387 EXPECT_EQ("159,99", event_handler.GetLocationAndReset());
389 magnifier->SetEnabled(false);
390 EXPECT_FLOAT_EQ(1.0f, magnifier->GetScale());
392 Shell::GetInstance()->RemovePreTargetHandler(&event_handler);
395 TEST_F(RootWindowTransformersTest, LetterBoxPillarBox) {
396 if (!SupportsMultipleDisplays())
397 return;
398 test::MirrorWindowTestApi test_api;
399 DisplayManager* display_manager = Shell::GetInstance()->display_manager();
400 display_manager->SetSecondDisplayMode(DisplayManager::MIRRORING);
401 UpdateDisplay("400x200,500x500");
402 scoped_ptr<RootWindowTransformer> transformer(
403 test_api.CreateCurrentRootWindowTransformer());
404 // Y margin must be margin is (500 - 500/400 * 200) / 2 = 125.
405 EXPECT_EQ("0,125,0,125", transformer->GetHostInsets().ToString());
407 UpdateDisplay("200x400,500x500");
408 // The aspect ratio is flipped, so X margin is now 125.
409 transformer = test_api.CreateCurrentRootWindowTransformer();
410 EXPECT_EQ("125,0,125,0", transformer->GetHostInsets().ToString());
413 } // namespace ash