Enable v2 app panels on Chrome OS
[chromium-blink-merge.git] / ash / display / root_window_transformers_unittest.cc
blob39e8c64814ecb30353a160489414b5f5e28e5df0
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 float GetStoredUIScale(int64 id) {
111 return Shell::GetInstance()->display_manager()->GetDisplayInfo(id).
112 GetEffectiveUIScale();
115 scoped_ptr<RootWindowTransformer>
116 CreateCurrentRootWindowTransformerForMirroring() {
117 DisplayManager* display_manager = Shell::GetInstance()->display_manager();
118 DCHECK(display_manager->IsInMirrorMode());
119 const DisplayInfo& mirror_display_info =
120 display_manager->GetDisplayInfo(display_manager->mirroring_display_id());
121 const DisplayInfo& source_display_info = display_manager->GetDisplayInfo(
122 Shell::GetScreen()->GetPrimaryDisplay().id());
123 return scoped_ptr<RootWindowTransformer>(
124 CreateRootWindowTransformerForMirroredDisplay(source_display_info,
125 mirror_display_info));
128 } // namespace
130 typedef test::AshTestBase RootWindowTransformersTest;
132 #if defined(OS_WIN)
133 // TODO(scottmg): RootWindow doesn't get resized on Windows
134 // Ash. http://crbug.com/247916.
135 #define MAYBE_RotateAndMagnify DISABLED_RotateAndMagniy
136 #define MAYBE_TouchScaleAndMagnify DISABLED_TouchScaleAndMagnify
137 #define MAYBE_ConvertHostToRootCoords DISABLED_ConvertHostToRootCoords
138 #else
139 #define MAYBE_RotateAndMagnify RotateAndMagniy
140 #define MAYBE_TouchScaleAndMagnify TouchScaleAndMagnify
141 #define MAYBE_ConvertHostToRootCoords ConvertHostToRootCoords
142 #endif
144 TEST_F(RootWindowTransformersTest, MAYBE_RotateAndMagnify) {
145 MagnificationController* magnifier =
146 Shell::GetInstance()->magnification_controller();
147 DisplayManager* display_manager = Shell::GetInstance()->display_manager();
149 TestEventHandler event_handler;
150 Shell::GetInstance()->AddPreTargetHandler(&event_handler);
152 UpdateDisplay("120x200,300x400*2");
153 gfx::Display display1 = Shell::GetScreen()->GetPrimaryDisplay();
154 int64 display2_id = ScreenUtil::GetSecondaryDisplay().id();
156 aura::Window::Windows root_windows = Shell::GetAllRootWindows();
157 ui::test::EventGenerator generator1(root_windows[0]);
158 ui::test::EventGenerator generator2(root_windows[1]);
160 magnifier->SetEnabled(true);
161 EXPECT_EQ(2.0f, magnifier->GetScale());
162 EXPECT_EQ("120x200", root_windows[0]->bounds().size().ToString());
163 EXPECT_EQ("150x200", root_windows[1]->bounds().size().ToString());
164 EXPECT_EQ("120,0 150x200",
165 ScreenUtil::GetSecondaryDisplay().bounds().ToString());
166 generator1.MoveMouseToInHost(40, 80);
167 EXPECT_EQ("50,90", event_handler.GetLocationAndReset());
168 EXPECT_EQ("50,90",
169 aura::Env::GetInstance()->last_mouse_location().ToString());
170 EXPECT_EQ(gfx::Display::ROTATE_0, GetActiveDisplayRotation(display1.id()));
171 EXPECT_EQ(gfx::Display::ROTATE_0, GetActiveDisplayRotation(display2_id));
172 magnifier->SetEnabled(false);
174 display_manager->SetDisplayRotation(display1.id(), gfx::Display::ROTATE_90,
175 gfx::Display::ROTATION_SOURCE_ACTIVE);
176 // Move the cursor to the center of the first root window.
177 generator1.MoveMouseToInHost(59, 100);
179 magnifier->SetEnabled(true);
180 EXPECT_EQ(2.0f, magnifier->GetScale());
181 EXPECT_EQ("200x120", root_windows[0]->bounds().size().ToString());
182 EXPECT_EQ("150x200", root_windows[1]->bounds().size().ToString());
183 EXPECT_EQ("200,0 150x200",
184 ScreenUtil::GetSecondaryDisplay().bounds().ToString());
185 generator1.MoveMouseToInHost(39, 120);
186 EXPECT_EQ("110,70", event_handler.GetLocationAndReset());
187 EXPECT_EQ("110,70",
188 aura::Env::GetInstance()->last_mouse_location().ToString());
189 EXPECT_EQ(gfx::Display::ROTATE_90, GetActiveDisplayRotation(display1.id()));
190 EXPECT_EQ(gfx::Display::ROTATE_0, GetActiveDisplayRotation(display2_id));
191 magnifier->SetEnabled(false);
193 DisplayLayout display_layout(DisplayLayout::BOTTOM, 50);
194 display_manager->SetLayoutForCurrentDisplays(display_layout);
195 EXPECT_EQ("50,120 150x200",
196 ScreenUtil::GetSecondaryDisplay().bounds().ToString());
198 display_manager->SetDisplayRotation(display2_id, gfx::Display::ROTATE_270,
199 gfx::Display::ROTATION_SOURCE_ACTIVE);
200 // Move the cursor to the center of the second root window.
201 generator2.MoveMouseToInHost(151, 199);
203 magnifier->SetEnabled(true);
204 EXPECT_EQ("200x120", root_windows[0]->bounds().size().ToString());
205 EXPECT_EQ("200x150", root_windows[1]->bounds().size().ToString());
206 EXPECT_EQ("50,120 200x150",
207 ScreenUtil::GetSecondaryDisplay().bounds().ToString());
208 generator2.MoveMouseToInHost(172, 219);
209 EXPECT_EQ("95,80", event_handler.GetLocationAndReset());
210 EXPECT_EQ("145,200",
211 aura::Env::GetInstance()->last_mouse_location().ToString());
212 EXPECT_EQ(gfx::Display::ROTATE_90, GetActiveDisplayRotation(display1.id()));
213 EXPECT_EQ(gfx::Display::ROTATE_270, GetActiveDisplayRotation(display2_id));
214 magnifier->SetEnabled(false);
216 display_manager->SetDisplayRotation(display1.id(), gfx::Display::ROTATE_180,
217 gfx::Display::ROTATION_SOURCE_ACTIVE);
218 // Move the cursor to the center of the first root window.
219 generator1.MoveMouseToInHost(59, 99);
221 magnifier->SetEnabled(true);
222 EXPECT_EQ("120x200", root_windows[0]->bounds().size().ToString());
223 EXPECT_EQ("200x150", root_windows[1]->bounds().size().ToString());
224 // Dislay must share at least 100, so the x's offset becomes 20.
225 EXPECT_EQ("20,200 200x150",
226 ScreenUtil::GetSecondaryDisplay().bounds().ToString());
227 generator1.MoveMouseToInHost(39, 59);
228 EXPECT_EQ("70,120", event_handler.GetLocationAndReset());
229 EXPECT_EQ(gfx::Display::ROTATE_180, GetActiveDisplayRotation(display1.id()));
230 EXPECT_EQ(gfx::Display::ROTATE_270, GetActiveDisplayRotation(display2_id));
231 magnifier->SetEnabled(false);
233 Shell::GetInstance()->RemovePreTargetHandler(&event_handler);
236 TEST_F(RootWindowTransformersTest, ScaleAndMagnify) {
237 if (!SupportsMultipleDisplays())
238 return;
240 TestEventHandler event_handler;
241 Shell::GetInstance()->AddPreTargetHandler(&event_handler);
243 UpdateDisplay("600x400*2@1.5,500x300");
245 gfx::Display display1 = Shell::GetScreen()->GetPrimaryDisplay();
246 test::DisplayManagerTestApi(Shell::GetInstance()->display_manager())
247 .SetInternalDisplayId(display1.id());
248 gfx::Display display2 = ScreenUtil::GetSecondaryDisplay();
249 aura::Window::Windows root_windows = Shell::GetAllRootWindows();
250 MagnificationController* magnifier =
251 Shell::GetInstance()->magnification_controller();
253 magnifier->SetEnabled(true);
254 EXPECT_EQ(2.0f, magnifier->GetScale());
255 EXPECT_EQ("0,0 450x300", display1.bounds().ToString());
256 EXPECT_EQ("0,0 450x300", root_windows[0]->bounds().ToString());
257 EXPECT_EQ("450,0 500x300", display2.bounds().ToString());
258 EXPECT_EQ(1.5f, GetStoredUIScale(display1.id()));
259 EXPECT_EQ(1.0f, GetStoredUIScale(display2.id()));
261 ui::test::EventGenerator generator(root_windows[0]);
262 generator.MoveMouseToInHost(500, 200);
263 EXPECT_EQ("299,150", event_handler.GetLocationAndReset());
264 magnifier->SetEnabled(false);
266 DisplayManager* display_manager = Shell::GetInstance()->display_manager();
267 display_manager->SetDisplayUIScale(display1.id(), 1.25);
268 display1 = Shell::GetScreen()->GetPrimaryDisplay();
269 display2 = ScreenUtil::GetSecondaryDisplay();
270 magnifier->SetEnabled(true);
271 EXPECT_EQ(2.0f, magnifier->GetScale());
272 EXPECT_EQ("0,0 375x250", display1.bounds().ToString());
273 EXPECT_EQ("0,0 375x250", root_windows[0]->bounds().ToString());
274 EXPECT_EQ("375,0 500x300", display2.bounds().ToString());
275 EXPECT_EQ(1.25f, GetStoredUIScale(display1.id()));
276 EXPECT_EQ(1.0f, GetStoredUIScale(display2.id()));
277 magnifier->SetEnabled(false);
279 Shell::GetInstance()->RemovePreTargetHandler(&event_handler);
282 TEST_F(RootWindowTransformersTest, MAYBE_TouchScaleAndMagnify) {
283 TestEventHandler event_handler;
284 Shell::GetInstance()->AddPreTargetHandler(&event_handler);
286 UpdateDisplay("200x200*2");
287 gfx::Display display = Shell::GetScreen()->GetPrimaryDisplay();
288 aura::Window::Windows root_windows = Shell::GetAllRootWindows();
289 aura::Window* root_window = root_windows[0];
290 ui::test::EventGenerator generator(root_window);
291 MagnificationController* magnifier =
292 Shell::GetInstance()->magnification_controller();
294 magnifier->SetEnabled(true);
295 EXPECT_FLOAT_EQ(2.0f, magnifier->GetScale());
296 magnifier->SetScale(2.5f, false);
297 EXPECT_FLOAT_EQ(2.5f, magnifier->GetScale());
298 generator.PressMoveAndReleaseTouchTo(50, 50);
299 // Default test touches have radius_x/y = 1.0, with device scale
300 // factor = 2, the scaled radius_x/y should be 0.5.
301 EXPECT_FLOAT_EQ(0.2f, event_handler.touch_radius_x());
302 EXPECT_FLOAT_EQ(0.2f, event_handler.touch_radius_y());
304 generator.ScrollSequence(gfx::Point(0,0),
305 base::TimeDelta::FromMilliseconds(100),
306 10.0, 1.0, 5, 1);
308 // ordinal_offset is invariant to the device scale factor.
309 EXPECT_FLOAT_EQ(event_handler.scroll_x_offset(),
310 event_handler.scroll_x_offset_ordinal());
311 EXPECT_FLOAT_EQ(event_handler.scroll_y_offset(),
312 event_handler.scroll_y_offset_ordinal());
313 magnifier->SetEnabled(false);
315 Shell::GetInstance()->RemovePreTargetHandler(&event_handler);
318 TEST_F(RootWindowTransformersTest, MAYBE_ConvertHostToRootCoords) {
319 TestEventHandler event_handler;
320 Shell::GetInstance()->AddPreTargetHandler(&event_handler);
321 MagnificationController* magnifier =
322 Shell::GetInstance()->magnification_controller();
324 // Test 1
325 UpdateDisplay("600x400*2/r@1.5");
327 gfx::Display display1 = Shell::GetScreen()->GetPrimaryDisplay();
328 aura::Window::Windows root_windows = Shell::GetAllRootWindows();
329 EXPECT_EQ("0,0 300x450", display1.bounds().ToString());
330 EXPECT_EQ("0,0 300x450", root_windows[0]->bounds().ToString());
331 EXPECT_EQ(1.5f, GetStoredUIScale(display1.id()));
333 ui::test::EventGenerator generator(root_windows[0]);
334 generator.MoveMouseToInHost(300, 200);
335 magnifier->SetEnabled(true);
336 EXPECT_EQ("150,224", event_handler.GetLocationAndReset());
337 EXPECT_FLOAT_EQ(2.0f, magnifier->GetScale());
339 generator.MoveMouseToInHost(300, 200);
340 EXPECT_EQ("150,224", event_handler.GetLocationAndReset());
341 generator.MoveMouseToInHost(200, 300);
342 EXPECT_EQ("187,261", event_handler.GetLocationAndReset());
343 generator.MoveMouseToInHost(100, 400);
344 EXPECT_EQ("237,299", event_handler.GetLocationAndReset());
345 generator.MoveMouseToInHost(0, 0);
346 EXPECT_EQ("137,348", event_handler.GetLocationAndReset());
348 magnifier->SetEnabled(false);
349 EXPECT_FLOAT_EQ(1.0f, magnifier->GetScale());
351 // Test 2
352 UpdateDisplay("600x400*2/u@1.5");
353 display1 = Shell::GetScreen()->GetPrimaryDisplay();
354 root_windows = Shell::GetAllRootWindows();
355 EXPECT_EQ("0,0 450x300", display1.bounds().ToString());
356 EXPECT_EQ("0,0 450x300", root_windows[0]->bounds().ToString());
357 EXPECT_EQ(1.5f, GetStoredUIScale(display1.id()));
359 generator.MoveMouseToInHost(300, 200);
360 magnifier->SetEnabled(true);
361 EXPECT_EQ("224,149", event_handler.GetLocationAndReset());
362 EXPECT_FLOAT_EQ(2.0f, magnifier->GetScale());
364 generator.MoveMouseToInHost(300, 200);
365 EXPECT_EQ("224,148", event_handler.GetLocationAndReset());
366 generator.MoveMouseToInHost(200, 300);
367 EXPECT_EQ("261,111", event_handler.GetLocationAndReset());
368 generator.MoveMouseToInHost(100, 400);
369 EXPECT_EQ("299,60", event_handler.GetLocationAndReset());
370 generator.MoveMouseToInHost(0, 0);
371 EXPECT_EQ("348,159", event_handler.GetLocationAndReset());
373 magnifier->SetEnabled(false);
374 EXPECT_FLOAT_EQ(1.0f, magnifier->GetScale());
376 // Test 3
377 UpdateDisplay("600x400*2/l@1.5");
378 display1 = Shell::GetScreen()->GetPrimaryDisplay();
379 root_windows = Shell::GetAllRootWindows();
380 EXPECT_EQ("0,0 300x450", display1.bounds().ToString());
381 EXPECT_EQ("0,0 300x450", root_windows[0]->bounds().ToString());
382 EXPECT_EQ(1.5f, GetStoredUIScale(display1.id()));
384 generator.MoveMouseToInHost(300, 200);
385 magnifier->SetEnabled(true);
386 EXPECT_EQ("149,225", event_handler.GetLocationAndReset());
387 EXPECT_FLOAT_EQ(2.0f, magnifier->GetScale());
389 generator.MoveMouseToInHost(300, 200);
390 EXPECT_EQ("148,224", event_handler.GetLocationAndReset());
391 generator.MoveMouseToInHost(200, 300);
392 EXPECT_EQ("111,187", event_handler.GetLocationAndReset());
393 generator.MoveMouseToInHost(100, 400);
394 EXPECT_EQ("60,149", event_handler.GetLocationAndReset());
395 generator.MoveMouseToInHost(0, 0);
396 EXPECT_EQ("159,99", event_handler.GetLocationAndReset());
398 magnifier->SetEnabled(false);
399 EXPECT_FLOAT_EQ(1.0f, magnifier->GetScale());
401 Shell::GetInstance()->RemovePreTargetHandler(&event_handler);
404 TEST_F(RootWindowTransformersTest, LetterBoxPillarBox) {
405 if (!SupportsMultipleDisplays())
406 return;
407 test::MirrorWindowTestApi test_api;
408 DisplayManager* display_manager = Shell::GetInstance()->display_manager();
409 display_manager->SetMultiDisplayMode(DisplayManager::MIRRORING);
410 UpdateDisplay("400x200,500x500");
411 scoped_ptr<RootWindowTransformer> transformer(
412 CreateCurrentRootWindowTransformerForMirroring());
413 // Y margin must be margin is (500 - 500/400 * 200) / 2 = 125.
414 EXPECT_EQ("0,125,0,125", transformer->GetHostInsets().ToString());
416 UpdateDisplay("200x400,500x500");
417 // The aspect ratio is flipped, so X margin is now 125.
418 transformer = CreateCurrentRootWindowTransformerForMirroring();
419 EXPECT_EQ("125,0,125,0", transformer->GetHostInsets().ToString());
422 } // namespace ash