Video Player: Remove unnecessary script file
[chromium-blink-merge.git] / ash / display / display_controller_unittest.cc
blob26e01ccf66a6313d0387ea60100adbbe84eccf66
1 // Copyright (c) 2012 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/display_controller.h"
7 #include "ash/ash_switches.h"
8 #include "ash/display/display_info.h"
9 #include "ash/display/display_layout_store.h"
10 #include "ash/display/display_manager.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/ash_test_helper.h"
17 #include "ash/test/cursor_manager_test_api.h"
18 #include "ash/test/display_manager_test_api.h"
19 #include "ash/test/test_shell_delegate.h"
20 #include "ash/wm/window_state.h"
21 #include "ash/wm/wm_event.h"
22 #include "base/command_line.h"
23 #include "ui/aura/client/focus_change_observer.h"
24 #include "ui/aura/client/focus_client.h"
25 #include "ui/aura/env.h"
26 #include "ui/aura/test/event_generator.h"
27 #include "ui/aura/window_tracker.h"
28 #include "ui/aura/window_tree_host.h"
29 #include "ui/events/event_handler.h"
30 #include "ui/gfx/display.h"
31 #include "ui/gfx/screen.h"
32 #include "ui/views/widget/widget.h"
33 #include "ui/wm/public/activation_change_observer.h"
34 #include "ui/wm/public/activation_client.h"
36 #if defined(USE_X11)
37 #include <X11/Xlib.h>
38 #include "ui/gfx/x/x11_types.h"
39 #undef RootWindow
40 #endif
42 namespace ash {
43 namespace {
45 const char kDesktopBackgroundView[] = "DesktopBackgroundView";
47 template<typename T>
48 class Resetter {
49 public:
50 explicit Resetter(T* value) : value_(*value) {
51 *value = 0;
53 ~Resetter() { }
54 T value() { return value_; }
56 private:
57 T value_;
58 DISALLOW_COPY_AND_ASSIGN(Resetter);
61 class TestObserver : public DisplayController::Observer,
62 public gfx::DisplayObserver,
63 public aura::client::FocusChangeObserver,
64 public aura::client::ActivationChangeObserver {
65 public:
66 TestObserver()
67 : changing_count_(0),
68 changed_count_(0),
69 bounds_changed_count_(0),
70 rotation_changed_count_(0),
71 workarea_changed_count_(0),
72 changed_display_id_(0),
73 focus_changed_count_(0),
74 activation_changed_count_(0) {
75 Shell::GetInstance()->display_controller()->AddObserver(this);
76 Shell::GetScreen()->AddObserver(this);
77 aura::client::GetFocusClient(Shell::GetPrimaryRootWindow())->
78 AddObserver(this);
79 aura::client::GetActivationClient(Shell::GetPrimaryRootWindow())->
80 AddObserver(this);
83 virtual ~TestObserver() {
84 Shell::GetInstance()->display_controller()->RemoveObserver(this);
85 Shell::GetScreen()->RemoveObserver(this);
86 aura::client::GetFocusClient(Shell::GetPrimaryRootWindow())->
87 RemoveObserver(this);
88 aura::client::GetActivationClient(Shell::GetPrimaryRootWindow())->
89 RemoveObserver(this);
92 // Overridden from DisplayController::Observer
93 virtual void OnDisplayConfigurationChanging() OVERRIDE {
94 ++changing_count_;
96 virtual void OnDisplayConfigurationChanged() OVERRIDE {
97 ++changed_count_;
100 // Overrideen from gfx::DisplayObserver
101 virtual void OnDisplayMetricsChanged(const gfx::Display& display,
102 uint32_t metrics) OVERRIDE {
103 changed_display_id_ = display.id();
104 if (metrics & DISPLAY_METRIC_BOUNDS)
105 ++bounds_changed_count_;
106 if (metrics & DISPLAY_METRIC_ROTATION)
107 ++rotation_changed_count_;
108 if (metrics & DISPLAY_METRIC_WORK_AREA)
109 ++workarea_changed_count_;
111 virtual void OnDisplayAdded(const gfx::Display& new_display) OVERRIDE {
113 virtual void OnDisplayRemoved(const gfx::Display& old_display) OVERRIDE {
116 // Overridden from aura::client::FocusChangeObserver
117 virtual void OnWindowFocused(aura::Window* gained_focus,
118 aura::Window* lost_focus) OVERRIDE {
119 focus_changed_count_++;
122 // Overridden from aura::client::ActivationChangeObserver
123 virtual void OnWindowActivated(aura::Window* gained_active,
124 aura::Window* lost_active) OVERRIDE {
125 activation_changed_count_++;
127 virtual void OnAttemptToReactivateWindow(
128 aura::Window* request_active,
129 aura::Window* actual_active) OVERRIDE {
132 int CountAndReset() {
133 EXPECT_EQ(changing_count_, changed_count_);
134 changed_count_ = 0;
135 return Resetter<int>(&changing_count_).value();
138 int64 GetBoundsChangedCountAndReset() {
139 return Resetter<int>(&bounds_changed_count_).value();
142 int64 GetRotationChangedCountAndReset() {
143 return Resetter<int>(&rotation_changed_count_).value();
146 int64 GetWorkareaChangedCountAndReset() {
147 return Resetter<int>(&workarea_changed_count_).value();
150 int64 GetChangedDisplayIdAndReset() {
151 return Resetter<int64>(&changed_display_id_).value();
154 int GetFocusChangedCountAndReset() {
155 return Resetter<int>(&focus_changed_count_).value();
158 int GetActivationChangedCountAndReset() {
159 return Resetter<int>(&activation_changed_count_).value();
162 private:
163 int changing_count_;
164 int changed_count_;
166 int bounds_changed_count_;
167 int rotation_changed_count_;
168 int workarea_changed_count_;
169 int64 changed_display_id_;
171 int focus_changed_count_;
172 int activation_changed_count_;
174 DISALLOW_COPY_AND_ASSIGN(TestObserver);
177 gfx::Display GetPrimaryDisplay() {
178 return Shell::GetScreen()->GetDisplayNearestWindow(
179 Shell::GetAllRootWindows()[0]);
182 gfx::Display GetSecondaryDisplay() {
183 return Shell::GetScreen()->GetDisplayNearestWindow(
184 Shell::GetAllRootWindows()[1]);
187 void SetSecondaryDisplayLayoutAndOffset(DisplayLayout::Position position,
188 int offset) {
189 DisplayLayout layout(position, offset);
190 ASSERT_GT(Shell::GetScreen()->GetNumDisplays(), 1);
191 Shell::GetInstance()->display_manager()->
192 SetLayoutForCurrentDisplays(layout);
195 void SetSecondaryDisplayLayout(DisplayLayout::Position position) {
196 SetSecondaryDisplayLayoutAndOffset(position, 0);
199 void SetDefaultDisplayLayout(DisplayLayout::Position position) {
200 Shell::GetInstance()->display_manager()->layout_store()->
201 SetDefaultDisplayLayout(DisplayLayout(position, 0));
204 class DisplayControllerShutdownTest : public test::AshTestBase {
205 public:
206 DisplayControllerShutdownTest() {}
207 virtual ~DisplayControllerShutdownTest() {}
209 virtual void TearDown() OVERRIDE {
210 test::AshTestBase::TearDown();
211 if (!SupportsMultipleDisplays())
212 return;
214 // Make sure that primary display is accessible after shutdown.
215 gfx::Display primary = Shell::GetScreen()->GetPrimaryDisplay();
216 EXPECT_EQ("0,0 444x333", primary.bounds().ToString());
217 EXPECT_EQ(2, Shell::GetScreen()->GetNumDisplays());
220 private:
221 DISALLOW_COPY_AND_ASSIGN(DisplayControllerShutdownTest);
224 class StartupHelper : public test::TestShellDelegate,
225 public DisplayController::Observer {
226 public:
227 StartupHelper() : displays_initialized_(false) {}
228 virtual ~StartupHelper() {}
230 // ash::ShellSelegate:
231 virtual void PreInit() OVERRIDE {
232 Shell::GetInstance()->display_controller()->AddObserver(this);
235 // ash::DisplayController::Observer:
236 virtual void OnDisplaysInitialized() OVERRIDE {
237 DCHECK(!displays_initialized_);
238 displays_initialized_ = true;
241 const bool displays_initialized() const {
242 return displays_initialized_;
245 private:
246 bool displays_initialized_;
248 DISALLOW_COPY_AND_ASSIGN(StartupHelper);
251 class DisplayControllerStartupTest : public test::AshTestBase {
252 public:
253 DisplayControllerStartupTest() : startup_helper_(new StartupHelper) {}
254 virtual ~DisplayControllerStartupTest() {}
256 // ash::test::AshTestBase:
257 virtual void SetUp() OVERRIDE {
258 ash_test_helper()->set_test_shell_delegate(startup_helper_);
259 test::AshTestBase::SetUp();
261 virtual void TearDown() OVERRIDE {
262 Shell::GetInstance()->display_controller()->RemoveObserver(startup_helper_);
263 test::AshTestBase::TearDown();
266 const StartupHelper* startup_helper() const { return startup_helper_; }
268 private:
269 StartupHelper* startup_helper_; // Owned by ash::Shell.
271 DISALLOW_COPY_AND_ASSIGN(DisplayControllerStartupTest);
274 class TestEventHandler : public ui::EventHandler {
275 public:
276 TestEventHandler() : target_root_(NULL),
277 touch_radius_x_(0.0),
278 touch_radius_y_(0.0),
279 scroll_x_offset_(0.0),
280 scroll_y_offset_(0.0),
281 scroll_x_offset_ordinal_(0.0),
282 scroll_y_offset_ordinal_(0.0) {}
283 virtual ~TestEventHandler() {}
285 virtual void OnMouseEvent(ui::MouseEvent* event) OVERRIDE {
286 if (event->flags() & ui::EF_IS_SYNTHESIZED &&
287 event->type() != ui::ET_MOUSE_EXITED &&
288 event->type() != ui::ET_MOUSE_ENTERED) {
289 return;
291 aura::Window* target = static_cast<aura::Window*>(event->target());
292 mouse_location_ = event->root_location();
293 target_root_ = target->GetRootWindow();
294 event->StopPropagation();
297 virtual void OnTouchEvent(ui::TouchEvent* event) OVERRIDE {
298 aura::Window* target = static_cast<aura::Window*>(event->target());
299 // Only record when the target is the background which covers
300 // entire root window.
301 if (target->name() != kDesktopBackgroundView)
302 return;
303 touch_radius_x_ = event->radius_x();
304 touch_radius_y_ = event->radius_y();
305 event->StopPropagation();
308 virtual void OnScrollEvent(ui::ScrollEvent* event) OVERRIDE {
309 aura::Window* target = static_cast<aura::Window*>(event->target());
310 // Only record when the target is the background which covers
311 // entire root window.
312 if (target->name() != kDesktopBackgroundView)
313 return;
315 if (event->type() == ui::ET_SCROLL) {
316 scroll_x_offset_ = event->x_offset();
317 scroll_y_offset_ = event->y_offset();
318 scroll_x_offset_ordinal_ = event->x_offset_ordinal();
319 scroll_y_offset_ordinal_ = event->y_offset_ordinal();
321 event->StopPropagation();
324 std::string GetLocationAndReset() {
325 std::string result = mouse_location_.ToString();
326 mouse_location_.SetPoint(0, 0);
327 target_root_ = NULL;
328 return result;
331 float touch_radius_x() { return touch_radius_x_; }
332 float touch_radius_y() { return touch_radius_y_; }
333 float scroll_x_offset() { return scroll_x_offset_; }
334 float scroll_y_offset() { return scroll_y_offset_; }
335 float scroll_x_offset_ordinal() { return scroll_x_offset_ordinal_; }
336 float scroll_y_offset_ordinal() { return scroll_y_offset_ordinal_; }
338 private:
339 gfx::Point mouse_location_;
340 aura::Window* target_root_;
342 float touch_radius_x_;
343 float touch_radius_y_;
344 float scroll_x_offset_;
345 float scroll_y_offset_;
346 float scroll_x_offset_ordinal_;
347 float scroll_y_offset_ordinal_;
349 DISALLOW_COPY_AND_ASSIGN(TestEventHandler);
352 gfx::Display::Rotation GetStoredRotation(int64 id) {
353 return Shell::GetInstance()->display_manager()->GetDisplayInfo(id).rotation();
356 float GetStoredUIScale(int64 id) {
357 return Shell::GetInstance()->display_manager()->GetDisplayInfo(id).
358 GetEffectiveUIScale();
361 #if defined(USE_X11)
362 void GetPrimaryAndSeconary(aura::Window** primary,
363 aura::Window** secondary) {
364 *primary = Shell::GetPrimaryRootWindow();
365 aura::Window::Windows root_windows = Shell::GetAllRootWindows();
366 *secondary = root_windows[0] == *primary ? root_windows[1] : root_windows[0];
369 std::string GetXWindowName(aura::WindowTreeHost* host) {
370 char* name = NULL;
371 XFetchName(gfx::GetXDisplay(), host->GetAcceleratedWidget(), &name);
372 std::string ret(name);
373 XFree(name);
374 return ret;
376 #endif
378 } // namespace
380 typedef test::AshTestBase DisplayControllerTest;
382 TEST_F(DisplayControllerShutdownTest, Shutdown) {
383 if (!SupportsMultipleDisplays())
384 return;
386 UpdateDisplay("444x333, 200x200");
389 TEST_F(DisplayControllerStartupTest, Startup) {
390 if (!SupportsMultipleDisplays())
391 return;
393 EXPECT_TRUE(startup_helper()->displays_initialized());
396 TEST_F(DisplayControllerTest, SecondaryDisplayLayout) {
397 if (!SupportsMultipleDisplays())
398 return;
400 // Creates windows to catch activation change event.
401 scoped_ptr<aura::Window> w1(CreateTestWindowInShellWithId(1));
402 w1->Focus();
404 TestObserver observer;
405 UpdateDisplay("500x500,400x400");
406 EXPECT_EQ(1, observer.CountAndReset()); // resize and add
407 EXPECT_EQ(1, observer.GetBoundsChangedCountAndReset());
408 EXPECT_EQ(1, observer.GetWorkareaChangedCountAndReset());
409 EXPECT_EQ(0, observer.GetFocusChangedCountAndReset());
410 EXPECT_EQ(0, observer.GetActivationChangedCountAndReset());
411 gfx::Insets insets(5, 5, 5, 5);
412 int64 secondary_display_id = ScreenUtil::GetSecondaryDisplay().id();
413 Shell::GetInstance()->display_manager()->UpdateWorkAreaOfDisplay(
414 secondary_display_id, insets);
416 // Default layout is RIGHT.
417 EXPECT_EQ("0,0 500x500", GetPrimaryDisplay().bounds().ToString());
418 EXPECT_EQ("500,0 400x400", GetSecondaryDisplay().bounds().ToString());
419 EXPECT_EQ("505,5 390x390", GetSecondaryDisplay().work_area().ToString());
420 EXPECT_EQ(0, observer.GetFocusChangedCountAndReset());
421 EXPECT_EQ(0, observer.GetActivationChangedCountAndReset());
423 // Layout the secondary display to the bottom of the primary.
424 SetSecondaryDisplayLayout(DisplayLayout::BOTTOM);
425 EXPECT_EQ(1, observer.CountAndReset());
426 EXPECT_EQ(1, observer.GetBoundsChangedCountAndReset());
427 EXPECT_EQ(1, observer.GetWorkareaChangedCountAndReset());
428 EXPECT_EQ(0, observer.GetFocusChangedCountAndReset());
429 EXPECT_EQ(0, observer.GetActivationChangedCountAndReset());
430 EXPECT_EQ(secondary_display_id, observer.GetChangedDisplayIdAndReset());
431 EXPECT_EQ("0,0 500x500", GetPrimaryDisplay().bounds().ToString());
432 EXPECT_EQ("0,500 400x400", GetSecondaryDisplay().bounds().ToString());
433 EXPECT_EQ("5,505 390x390", GetSecondaryDisplay().work_area().ToString());
435 // Layout the secondary display to the left of the primary.
436 SetSecondaryDisplayLayout(DisplayLayout::LEFT);
437 EXPECT_EQ(1, observer.CountAndReset());
438 EXPECT_EQ(1, observer.GetBoundsChangedCountAndReset());
439 EXPECT_EQ(1, observer.GetWorkareaChangedCountAndReset());
440 EXPECT_EQ(0, observer.GetFocusChangedCountAndReset());
441 EXPECT_EQ(0, observer.GetActivationChangedCountAndReset());
442 EXPECT_EQ(secondary_display_id, observer.GetChangedDisplayIdAndReset());
443 EXPECT_EQ("0,0 500x500", GetPrimaryDisplay().bounds().ToString());
444 EXPECT_EQ("-400,0 400x400", GetSecondaryDisplay().bounds().ToString());
445 EXPECT_EQ("-395,5 390x390", GetSecondaryDisplay().work_area().ToString());
447 // Layout the secondary display to the top of the primary.
448 SetSecondaryDisplayLayout(DisplayLayout::TOP);
449 EXPECT_EQ(1, observer.CountAndReset());
450 EXPECT_EQ(1, observer.GetBoundsChangedCountAndReset());
451 EXPECT_EQ(1, observer.GetWorkareaChangedCountAndReset());
452 EXPECT_EQ(0, observer.GetFocusChangedCountAndReset());
453 EXPECT_EQ(0, observer.GetActivationChangedCountAndReset());
454 EXPECT_EQ(secondary_display_id, observer.GetChangedDisplayIdAndReset());
455 EXPECT_EQ("0,0 500x500", GetPrimaryDisplay().bounds().ToString());
456 EXPECT_EQ("0,-400 400x400", GetSecondaryDisplay().bounds().ToString());
457 EXPECT_EQ("5,-395 390x390", GetSecondaryDisplay().work_area().ToString());
459 // Layout to the right with an offset.
460 SetSecondaryDisplayLayoutAndOffset(DisplayLayout::RIGHT, 300);
461 EXPECT_EQ(1, observer.CountAndReset()); // resize and add
462 EXPECT_EQ(1, observer.GetBoundsChangedCountAndReset());
463 EXPECT_EQ(1, observer.GetWorkareaChangedCountAndReset());
464 EXPECT_EQ(0, observer.GetFocusChangedCountAndReset());
465 EXPECT_EQ(0, observer.GetActivationChangedCountAndReset());
466 EXPECT_EQ(secondary_display_id, observer.GetChangedDisplayIdAndReset());
467 EXPECT_EQ("0,0 500x500", GetPrimaryDisplay().bounds().ToString());
468 EXPECT_EQ("500,300 400x400", GetSecondaryDisplay().bounds().ToString());
470 // Keep the minimum 100.
471 SetSecondaryDisplayLayoutAndOffset(DisplayLayout::RIGHT, 490);
472 EXPECT_EQ(1, observer.CountAndReset()); // resize and add
473 EXPECT_EQ(1, observer.GetBoundsChangedCountAndReset());
474 EXPECT_EQ(1, observer.GetWorkareaChangedCountAndReset());
475 EXPECT_EQ(0, observer.GetFocusChangedCountAndReset());
476 EXPECT_EQ(0, observer.GetActivationChangedCountAndReset());
477 EXPECT_EQ(secondary_display_id, observer.GetChangedDisplayIdAndReset());
478 EXPECT_EQ("0,0 500x500", GetPrimaryDisplay().bounds().ToString());
479 EXPECT_EQ("500,400 400x400", GetSecondaryDisplay().bounds().ToString());
481 SetSecondaryDisplayLayoutAndOffset(DisplayLayout::RIGHT, -400);
482 EXPECT_EQ(secondary_display_id, observer.GetChangedDisplayIdAndReset());
483 EXPECT_EQ(1, observer.GetBoundsChangedCountAndReset());
484 EXPECT_EQ(1, observer.GetWorkareaChangedCountAndReset());
485 EXPECT_EQ(1, observer.CountAndReset()); // resize and add
486 EXPECT_EQ(0, observer.GetFocusChangedCountAndReset());
487 EXPECT_EQ(0, observer.GetActivationChangedCountAndReset());
488 EXPECT_EQ("0,0 500x500", GetPrimaryDisplay().bounds().ToString());
489 EXPECT_EQ("500,-300 400x400", GetSecondaryDisplay().bounds().ToString());
491 // Layout to the bottom with an offset.
492 SetSecondaryDisplayLayoutAndOffset(DisplayLayout::BOTTOM, -200);
493 EXPECT_EQ(secondary_display_id, observer.GetChangedDisplayIdAndReset());
494 EXPECT_EQ(1, observer.GetBoundsChangedCountAndReset());
495 EXPECT_EQ(1, observer.GetWorkareaChangedCountAndReset());
496 EXPECT_EQ(1, observer.CountAndReset()); // resize and add
497 EXPECT_EQ(0, observer.GetFocusChangedCountAndReset());
498 EXPECT_EQ(0, observer.GetActivationChangedCountAndReset());
499 EXPECT_EQ("0,0 500x500", GetPrimaryDisplay().bounds().ToString());
500 EXPECT_EQ("-200,500 400x400", GetSecondaryDisplay().bounds().ToString());
502 // Keep the minimum 100.
503 SetSecondaryDisplayLayoutAndOffset(DisplayLayout::BOTTOM, 490);
504 EXPECT_EQ(secondary_display_id, observer.GetChangedDisplayIdAndReset());
505 EXPECT_EQ(1, observer.GetBoundsChangedCountAndReset());
506 EXPECT_EQ(1, observer.GetWorkareaChangedCountAndReset());
507 EXPECT_EQ(1, observer.CountAndReset()); // resize and add
508 EXPECT_EQ(0, observer.GetFocusChangedCountAndReset());
509 EXPECT_EQ(0, observer.GetActivationChangedCountAndReset());
510 EXPECT_EQ("0,0 500x500", GetPrimaryDisplay().bounds().ToString());
511 EXPECT_EQ("400,500 400x400", GetSecondaryDisplay().bounds().ToString());
513 SetSecondaryDisplayLayoutAndOffset(DisplayLayout::BOTTOM, -400);
514 EXPECT_EQ(secondary_display_id, observer.GetChangedDisplayIdAndReset());
515 EXPECT_EQ(1, observer.GetBoundsChangedCountAndReset());
516 EXPECT_EQ(1, observer.GetWorkareaChangedCountAndReset());
517 EXPECT_EQ(1, observer.CountAndReset()); // resize and add
518 EXPECT_EQ(0, observer.GetFocusChangedCountAndReset());
519 EXPECT_EQ(0, observer.GetActivationChangedCountAndReset());
520 EXPECT_EQ("0,0 500x500", GetPrimaryDisplay().bounds().ToString());
521 EXPECT_EQ("-300,500 400x400", GetSecondaryDisplay().bounds().ToString());
523 // Setting the same layout shouldn't invoke observers.
524 SetSecondaryDisplayLayoutAndOffset(DisplayLayout::BOTTOM, -400);
525 EXPECT_EQ(0, observer.GetChangedDisplayIdAndReset());
526 EXPECT_EQ(0, observer.GetBoundsChangedCountAndReset());
527 EXPECT_EQ(0, observer.GetWorkareaChangedCountAndReset());
528 EXPECT_EQ(0, observer.CountAndReset()); // resize and add
529 EXPECT_EQ(0, observer.GetFocusChangedCountAndReset());
530 EXPECT_EQ(0, observer.GetActivationChangedCountAndReset());
531 EXPECT_EQ("0,0 500x500", GetPrimaryDisplay().bounds().ToString());
532 EXPECT_EQ("-300,500 400x400", GetSecondaryDisplay().bounds().ToString());
534 UpdateDisplay("500x500");
535 EXPECT_LE(1, observer.GetFocusChangedCountAndReset());
536 EXPECT_LE(1, observer.GetActivationChangedCountAndReset());
539 namespace {
541 DisplayInfo CreateDisplayInfo(int64 id,
542 const gfx::Rect& bounds,
543 float device_scale_factor) {
544 DisplayInfo info(id, "", false);
545 info.SetBounds(bounds);
546 info.set_device_scale_factor(device_scale_factor);
547 return info;
550 } // namespace
552 TEST_F(DisplayControllerTest, MirrorToDockedWithFullscreen) {
553 if (!SupportsMultipleDisplays())
554 return;
556 // Creates windows to catch activation change event.
557 scoped_ptr<aura::Window> w1(CreateTestWindowInShellWithId(1));
558 w1->Focus();
560 // Docked mode.
561 DisplayManager* display_manager = Shell::GetInstance()->display_manager();
563 const DisplayInfo internal_display_info =
564 CreateDisplayInfo(1, gfx::Rect(0, 0, 500, 500), 2.0f);
565 const DisplayInfo external_display_info =
566 CreateDisplayInfo(2, gfx::Rect(0, 0, 500, 500), 1.0f);
568 std::vector<DisplayInfo> display_info_list;
569 // Mirror.
570 display_info_list.push_back(internal_display_info);
571 display_info_list.push_back(external_display_info);
572 display_manager->OnNativeDisplaysChanged(display_info_list);
573 const int64 internal_display_id =
574 test::DisplayManagerTestApi(display_manager).
575 SetFirstDisplayAsInternalDisplay();
576 EXPECT_EQ(1, internal_display_id);
577 EXPECT_EQ(2U, display_manager->num_connected_displays());
578 EXPECT_EQ(1U, display_manager->GetNumDisplays());
580 wm::WindowState* window_state = wm::GetWindowState(w1.get());
581 const wm::WMEvent toggle_fullscreen_event(wm::WM_EVENT_TOGGLE_FULLSCREEN);
582 window_state->OnWMEvent(&toggle_fullscreen_event);
583 EXPECT_TRUE(window_state->IsFullscreen());
584 EXPECT_EQ("0,0 250x250", w1->bounds().ToString());
585 // Dock mode.
586 TestObserver observer;
587 display_info_list.clear();
588 display_info_list.push_back(external_display_info);
589 display_manager->OnNativeDisplaysChanged(display_info_list);
590 EXPECT_EQ(1U, display_manager->GetNumDisplays());
591 EXPECT_EQ(1U, display_manager->num_connected_displays());
592 EXPECT_EQ(0, observer.GetChangedDisplayIdAndReset());
593 EXPECT_EQ(0, observer.GetBoundsChangedCountAndReset());
594 EXPECT_EQ(0, observer.GetWorkareaChangedCountAndReset());
595 EXPECT_EQ(1, observer.CountAndReset());
596 EXPECT_EQ(0, observer.GetFocusChangedCountAndReset());
597 EXPECT_EQ(0, observer.GetActivationChangedCountAndReset());
599 EXPECT_TRUE(window_state->IsFullscreen());
600 EXPECT_EQ("0,0 500x500", w1->bounds().ToString());
603 TEST_F(DisplayControllerTest, BoundsUpdated) {
604 if (!SupportsMultipleDisplays())
605 return;
607 // Creates windows to catch activation change event.
608 scoped_ptr<aura::Window> w1(CreateTestWindowInShellWithId(1));
609 w1->Focus();
611 TestObserver observer;
612 SetDefaultDisplayLayout(DisplayLayout::BOTTOM);
613 UpdateDisplay("200x200,300x300"); // layout, resize and add.
614 EXPECT_EQ(1, observer.CountAndReset());
615 EXPECT_EQ(0, observer.GetFocusChangedCountAndReset());
616 EXPECT_EQ(0, observer.GetActivationChangedCountAndReset());
618 DisplayManager* display_manager = Shell::GetInstance()->display_manager();
619 gfx::Insets insets(5, 5, 5, 5);
620 display_manager->UpdateWorkAreaOfDisplay(
621 ScreenUtil::GetSecondaryDisplay().id(), insets);
623 EXPECT_EQ("0,0 200x200", GetPrimaryDisplay().bounds().ToString());
624 EXPECT_EQ("0,200 300x300", GetSecondaryDisplay().bounds().ToString());
625 EXPECT_EQ("5,205 290x290", GetSecondaryDisplay().work_area().ToString());
627 UpdateDisplay("400x400,200x200");
628 EXPECT_EQ(1, observer.CountAndReset()); // two resizes
629 EXPECT_EQ(0, observer.GetFocusChangedCountAndReset());
630 EXPECT_EQ(0, observer.GetActivationChangedCountAndReset());
631 EXPECT_EQ("0,0 400x400", GetPrimaryDisplay().bounds().ToString());
632 EXPECT_EQ("0,400 200x200", GetSecondaryDisplay().bounds().ToString());
634 UpdateDisplay("400x400,300x300");
635 EXPECT_EQ(1, observer.CountAndReset());
636 EXPECT_EQ(0, observer.GetFocusChangedCountAndReset());
637 EXPECT_EQ(0, observer.GetActivationChangedCountAndReset());
638 EXPECT_EQ("0,0 400x400", GetPrimaryDisplay().bounds().ToString());
639 EXPECT_EQ("0,400 300x300", GetSecondaryDisplay().bounds().ToString());
641 UpdateDisplay("400x400");
642 EXPECT_EQ(1, observer.CountAndReset());
643 EXPECT_LE(1, observer.GetFocusChangedCountAndReset());
644 EXPECT_LE(1, observer.GetActivationChangedCountAndReset());
645 EXPECT_EQ("0,0 400x400", GetPrimaryDisplay().bounds().ToString());
646 EXPECT_EQ(1, Shell::GetScreen()->GetNumDisplays());
648 UpdateDisplay("400x500*2,300x300");
649 EXPECT_EQ(1, observer.CountAndReset());
650 EXPECT_EQ(0, observer.GetFocusChangedCountAndReset());
651 EXPECT_EQ(0, observer.GetActivationChangedCountAndReset());
652 ASSERT_EQ(2, Shell::GetScreen()->GetNumDisplays());
653 EXPECT_EQ("0,0 200x250", GetPrimaryDisplay().bounds().ToString());
654 EXPECT_EQ("0,250 300x300", GetSecondaryDisplay().bounds().ToString());
656 // No change
657 UpdateDisplay("400x500*2,300x300");
658 EXPECT_EQ(0, observer.CountAndReset());
659 EXPECT_EQ(0, observer.GetFocusChangedCountAndReset());
660 EXPECT_EQ(0, observer.GetActivationChangedCountAndReset());
662 // Rotation
663 observer.GetRotationChangedCountAndReset(); // we only want to reset.
664 int64 primary_id = GetPrimaryDisplay().id();
665 display_manager->SetDisplayRotation(primary_id, gfx::Display::ROTATE_90);
666 EXPECT_EQ(1, observer.GetRotationChangedCountAndReset());
667 EXPECT_EQ(1, observer.CountAndReset());
668 EXPECT_EQ(0, observer.GetFocusChangedCountAndReset());
669 EXPECT_EQ(0, observer.GetActivationChangedCountAndReset());
670 display_manager->SetDisplayRotation(primary_id, gfx::Display::ROTATE_90);
671 EXPECT_EQ(0, observer.GetRotationChangedCountAndReset());
672 EXPECT_EQ(0, observer.CountAndReset());
673 EXPECT_EQ(0, observer.GetFocusChangedCountAndReset());
674 EXPECT_EQ(0, observer.GetActivationChangedCountAndReset());
676 // UI scale is eanbled only on internal display.
677 int64 secondary_id = GetSecondaryDisplay().id();
678 gfx::Display::SetInternalDisplayId(secondary_id);
679 display_manager->SetDisplayUIScale(secondary_id, 1.125f);
680 EXPECT_EQ(1, observer.CountAndReset());
681 EXPECT_EQ(0, observer.GetFocusChangedCountAndReset());
682 EXPECT_EQ(0, observer.GetActivationChangedCountAndReset());
683 display_manager->SetDisplayUIScale(secondary_id, 1.125f);
684 EXPECT_EQ(0, observer.CountAndReset());
685 EXPECT_EQ(0, observer.GetFocusChangedCountAndReset());
686 EXPECT_EQ(0, observer.GetActivationChangedCountAndReset());
687 display_manager->SetDisplayUIScale(primary_id, 1.125f);
688 EXPECT_EQ(0, observer.CountAndReset());
689 EXPECT_EQ(0, observer.GetFocusChangedCountAndReset());
690 EXPECT_EQ(0, observer.GetActivationChangedCountAndReset());
691 display_manager->SetDisplayUIScale(primary_id, 1.125f);
692 EXPECT_EQ(0, observer.CountAndReset());
693 EXPECT_EQ(0, observer.GetFocusChangedCountAndReset());
694 EXPECT_EQ(0, observer.GetActivationChangedCountAndReset());
697 TEST_F(DisplayControllerTest, SwapPrimary) {
698 if (!SupportsMultipleDisplays())
699 return;
701 DisplayController* display_controller =
702 Shell::GetInstance()->display_controller();
703 DisplayManager* display_manager = Shell::GetInstance()->display_manager();
705 UpdateDisplay("200x200,300x300");
706 gfx::Display primary_display = Shell::GetScreen()->GetPrimaryDisplay();
707 gfx::Display secondary_display = ScreenUtil::GetSecondaryDisplay();
709 DisplayLayout display_layout(DisplayLayout::RIGHT, 50);
710 display_manager->SetLayoutForCurrentDisplays(display_layout);
712 EXPECT_NE(primary_display.id(), secondary_display.id());
713 aura::Window* primary_root =
714 display_controller->GetRootWindowForDisplayId(primary_display.id());
715 aura::Window* secondary_root =
716 display_controller->GetRootWindowForDisplayId(secondary_display.id());
717 EXPECT_NE(primary_root, secondary_root);
718 aura::Window* shelf_window =
719 Shelf::ForPrimaryDisplay()->shelf_widget()->GetNativeView();
720 EXPECT_TRUE(primary_root->Contains(shelf_window));
721 EXPECT_FALSE(secondary_root->Contains(shelf_window));
722 EXPECT_EQ(primary_display.id(),
723 Shell::GetScreen()->GetDisplayNearestPoint(
724 gfx::Point(-100, -100)).id());
725 EXPECT_EQ(primary_display.id(),
726 Shell::GetScreen()->GetDisplayNearestWindow(NULL).id());
728 EXPECT_EQ("0,0 200x200", primary_display.bounds().ToString());
729 EXPECT_EQ("0,0 200x153", primary_display.work_area().ToString());
730 EXPECT_EQ("200,0 300x300", secondary_display.bounds().ToString());
731 EXPECT_EQ("200,0 300x253", secondary_display.work_area().ToString());
732 EXPECT_EQ("right, 50",
733 display_manager->GetCurrentDisplayLayout().ToString());
735 // Switch primary and secondary
736 display_controller->SetPrimaryDisplay(secondary_display);
737 const DisplayLayout& inverted_layout =
738 display_manager->GetCurrentDisplayLayout();
739 EXPECT_EQ("left, -50", inverted_layout.ToString());
741 EXPECT_EQ(secondary_display.id(),
742 Shell::GetScreen()->GetPrimaryDisplay().id());
743 EXPECT_EQ(primary_display.id(), ScreenUtil::GetSecondaryDisplay().id());
744 EXPECT_EQ(primary_display.id(),
745 Shell::GetScreen()->GetDisplayNearestPoint(
746 gfx::Point(-100, -100)).id());
747 EXPECT_EQ(secondary_display.id(),
748 Shell::GetScreen()->GetDisplayNearestWindow(NULL).id());
750 EXPECT_EQ(
751 primary_root,
752 display_controller->GetRootWindowForDisplayId(secondary_display.id()));
753 EXPECT_EQ(
754 secondary_root,
755 display_controller->GetRootWindowForDisplayId(primary_display.id()));
756 EXPECT_TRUE(primary_root->Contains(shelf_window));
757 EXPECT_FALSE(secondary_root->Contains(shelf_window));
759 // Test if the bounds are correctly swapped.
760 gfx::Display swapped_primary = Shell::GetScreen()->GetPrimaryDisplay();
761 gfx::Display swapped_secondary = ScreenUtil::GetSecondaryDisplay();
762 EXPECT_EQ("0,0 300x300", swapped_primary.bounds().ToString());
763 EXPECT_EQ("0,0 300x253", swapped_primary.work_area().ToString());
764 EXPECT_EQ("-200,-50 200x200", swapped_secondary.bounds().ToString());
766 EXPECT_EQ("-200,-50 200x153", swapped_secondary.work_area().ToString());
768 aura::WindowTracker tracker;
769 tracker.Add(primary_root);
770 tracker.Add(secondary_root);
772 // Deleting 2nd display should move the primary to original primary display.
773 UpdateDisplay("200x200");
774 RunAllPendingInMessageLoop(); // RootWindow is deleted in a posted task.
775 EXPECT_EQ(1, Shell::GetScreen()->GetNumDisplays());
776 EXPECT_EQ(primary_display.id(), Shell::GetScreen()->GetPrimaryDisplay().id());
777 EXPECT_EQ(primary_display.id(),
778 Shell::GetScreen()->GetDisplayNearestPoint(
779 gfx::Point(-100, -100)).id());
780 EXPECT_EQ(primary_display.id(),
781 Shell::GetScreen()->GetDisplayNearestWindow(NULL).id());
782 EXPECT_TRUE(tracker.Contains(primary_root));
783 EXPECT_FALSE(tracker.Contains(secondary_root));
784 EXPECT_TRUE(primary_root->Contains(shelf_window));
787 TEST_F(DisplayControllerTest, FindNearestDisplay) {
788 if (!SupportsMultipleDisplays())
789 return;
791 DisplayController* display_controller =
792 Shell::GetInstance()->display_controller();
793 DisplayManager* display_manager = Shell::GetInstance()->display_manager();
795 UpdateDisplay("200x200,300x300");
796 DisplayLayout display_layout(DisplayLayout::RIGHT, 50);
797 display_manager->SetLayoutForCurrentDisplays(display_layout);
799 gfx::Display primary_display = Shell::GetScreen()->GetPrimaryDisplay();
800 gfx::Display secondary_display = ScreenUtil::GetSecondaryDisplay();
801 EXPECT_NE(primary_display.id(), secondary_display.id());
802 aura::Window* primary_root =
803 display_controller->GetRootWindowForDisplayId(primary_display.id());
804 aura::Window* secondary_root =
805 display_controller->GetRootWindowForDisplayId(secondary_display.id());
806 EXPECT_NE(primary_root, secondary_root);
808 // Test that points outside of any display return the nearest display.
809 EXPECT_EQ(primary_display.id(), Shell::GetScreen()->GetDisplayNearestPoint(
810 gfx::Point(-100, 0)).id());
811 EXPECT_EQ(primary_display.id(), Shell::GetScreen()->GetDisplayNearestPoint(
812 gfx::Point(0, -100)).id());
813 EXPECT_EQ(primary_display.id(), Shell::GetScreen()->GetDisplayNearestPoint(
814 gfx::Point(100, 100)).id());
815 EXPECT_EQ(primary_display.id(), Shell::GetScreen()->GetDisplayNearestPoint(
816 gfx::Point(224, 25)).id());
817 EXPECT_EQ(secondary_display.id(), Shell::GetScreen()->GetDisplayNearestPoint(
818 gfx::Point(226, 25)).id());
819 EXPECT_EQ(secondary_display.id(), Shell::GetScreen()->GetDisplayNearestPoint(
820 gfx::Point(600, 100)).id());
821 EXPECT_EQ(primary_display.id(), Shell::GetScreen()->GetDisplayNearestPoint(
822 gfx::Point(174, 225)).id());
823 EXPECT_EQ(secondary_display.id(), Shell::GetScreen()->GetDisplayNearestPoint(
824 gfx::Point(176, 225)).id());
825 EXPECT_EQ(secondary_display.id(), Shell::GetScreen()->GetDisplayNearestPoint(
826 gfx::Point(300, 400)).id());
829 TEST_F(DisplayControllerTest, SwapPrimaryById) {
830 if (!SupportsMultipleDisplays())
831 return;
833 DisplayController* display_controller =
834 Shell::GetInstance()->display_controller();
835 DisplayManager* display_manager = Shell::GetInstance()->display_manager();
837 UpdateDisplay("200x200,300x300");
838 gfx::Display primary_display = Shell::GetScreen()->GetPrimaryDisplay();
839 gfx::Display secondary_display = ScreenUtil::GetSecondaryDisplay();
841 DisplayLayout display_layout(DisplayLayout::RIGHT, 50);
842 display_manager->SetLayoutForCurrentDisplays(display_layout);
844 EXPECT_NE(primary_display.id(), secondary_display.id());
845 aura::Window* primary_root =
846 display_controller->GetRootWindowForDisplayId(primary_display.id());
847 aura::Window* secondary_root =
848 display_controller->GetRootWindowForDisplayId(secondary_display.id());
849 aura::Window* shelf_window =
850 Shelf::ForPrimaryDisplay()->shelf_widget()->GetNativeView();
851 EXPECT_TRUE(primary_root->Contains(shelf_window));
852 EXPECT_FALSE(secondary_root->Contains(shelf_window));
853 EXPECT_NE(primary_root, secondary_root);
854 EXPECT_EQ(primary_display.id(),
855 Shell::GetScreen()->GetDisplayNearestPoint(
856 gfx::Point(-100, -100)).id());
857 EXPECT_EQ(primary_display.id(),
858 Shell::GetScreen()->GetDisplayNearestWindow(NULL).id());
860 // Switch primary and secondary by display ID.
861 TestObserver observer;
862 display_controller->SetPrimaryDisplayId(secondary_display.id());
863 EXPECT_EQ(secondary_display.id(),
864 Shell::GetScreen()->GetPrimaryDisplay().id());
865 EXPECT_EQ(primary_display.id(), ScreenUtil::GetSecondaryDisplay().id());
866 EXPECT_LT(0, observer.CountAndReset());
868 EXPECT_EQ(
869 primary_root,
870 display_controller->GetRootWindowForDisplayId(secondary_display.id()));
871 EXPECT_EQ(
872 secondary_root,
873 display_controller->GetRootWindowForDisplayId(primary_display.id()));
874 EXPECT_TRUE(primary_root->Contains(shelf_window));
875 EXPECT_FALSE(secondary_root->Contains(shelf_window));
877 const DisplayLayout& inverted_layout =
878 display_manager->GetCurrentDisplayLayout();
880 EXPECT_EQ("left, -50", inverted_layout.ToString());
882 // Calling the same ID don't do anything.
883 display_controller->SetPrimaryDisplayId(secondary_display.id());
884 EXPECT_EQ(0, observer.CountAndReset());
886 aura::WindowTracker tracker;
887 tracker.Add(primary_root);
888 tracker.Add(secondary_root);
890 // Deleting 2nd display should move the primary to original primary display.
891 UpdateDisplay("200x200");
892 RunAllPendingInMessageLoop(); // RootWindow is deleted in a posted task.
893 EXPECT_EQ(1, Shell::GetScreen()->GetNumDisplays());
894 EXPECT_EQ(primary_display.id(), Shell::GetScreen()->GetPrimaryDisplay().id());
895 EXPECT_EQ(primary_display.id(),
896 Shell::GetScreen()->GetDisplayNearestPoint(
897 gfx::Point(-100, -100)).id());
898 EXPECT_EQ(primary_display.id(),
899 Shell::GetScreen()->GetDisplayNearestWindow(NULL).id());
900 EXPECT_TRUE(tracker.Contains(primary_root));
901 EXPECT_FALSE(tracker.Contains(secondary_root));
902 EXPECT_TRUE(primary_root->Contains(shelf_window));
904 // Adding 2nd display with the same ID. The 2nd display should become primary
905 // since secondary id is still stored as desirable_primary_id.
906 std::vector<DisplayInfo> display_info_list;
907 display_info_list.push_back(
908 display_manager->GetDisplayInfo(primary_display.id()));
909 display_info_list.push_back(
910 display_manager->GetDisplayInfo(secondary_display.id()));
911 display_manager->OnNativeDisplaysChanged(display_info_list);
913 EXPECT_EQ(2, Shell::GetScreen()->GetNumDisplays());
914 EXPECT_EQ(secondary_display.id(),
915 Shell::GetScreen()->GetPrimaryDisplay().id());
916 EXPECT_EQ(primary_display.id(), ScreenUtil::GetSecondaryDisplay().id());
917 EXPECT_EQ(
918 primary_root,
919 display_controller->GetRootWindowForDisplayId(secondary_display.id()));
920 EXPECT_NE(
921 primary_root,
922 display_controller->GetRootWindowForDisplayId(primary_display.id()));
923 EXPECT_TRUE(primary_root->Contains(shelf_window));
925 // Deleting 2nd display and adding 2nd display with a different ID. The 2nd
926 // display shouldn't become primary.
927 UpdateDisplay("200x200");
928 DisplayInfo third_display_info(
929 secondary_display.id() + 1, std::string(), false);
930 third_display_info.SetBounds(secondary_display.bounds());
931 ASSERT_NE(primary_display.id(), third_display_info.id());
933 const DisplayInfo& primary_display_info =
934 display_manager->GetDisplayInfo(primary_display.id());
935 std::vector<DisplayInfo> display_info_list2;
936 display_info_list2.push_back(primary_display_info);
937 display_info_list2.push_back(third_display_info);
938 display_manager->OnNativeDisplaysChanged(display_info_list2);
939 EXPECT_EQ(2, Shell::GetScreen()->GetNumDisplays());
940 EXPECT_EQ(primary_display.id(),
941 Shell::GetScreen()->GetPrimaryDisplay().id());
942 EXPECT_EQ(third_display_info.id(), ScreenUtil::GetSecondaryDisplay().id());
943 EXPECT_EQ(
944 primary_root,
945 display_controller->GetRootWindowForDisplayId(primary_display.id()));
946 EXPECT_NE(
947 primary_root,
948 display_controller->GetRootWindowForDisplayId(third_display_info.id()));
949 EXPECT_TRUE(primary_root->Contains(shelf_window));
952 TEST_F(DisplayControllerTest, CursorDeviceScaleFactorSwapPrimary) {
953 if (!SupportsMultipleDisplays())
954 return;
956 DisplayController* display_controller =
957 Shell::GetInstance()->display_controller();
959 UpdateDisplay("200x200,200x200*2");
960 gfx::Display primary_display = Shell::GetScreen()->GetPrimaryDisplay();
961 gfx::Display secondary_display = ScreenUtil::GetSecondaryDisplay();
963 aura::Window* primary_root =
964 display_controller->GetRootWindowForDisplayId(primary_display.id());
965 aura::Window* secondary_root =
966 display_controller->GetRootWindowForDisplayId(secondary_display.id());
967 EXPECT_NE(primary_root, secondary_root);
969 test::CursorManagerTestApi test_api(Shell::GetInstance()->cursor_manager());
971 EXPECT_EQ(1.0f, primary_root->GetHost()->compositor()->
972 device_scale_factor());
973 primary_root->MoveCursorTo(gfx::Point(50, 50));
974 EXPECT_EQ(1.0f, test_api.GetCurrentCursor().device_scale_factor());
975 EXPECT_EQ(2.0f, secondary_root->GetHost()->compositor()->
976 device_scale_factor());
977 secondary_root->MoveCursorTo(gfx::Point(50, 50));
978 EXPECT_EQ(2.0f, test_api.GetCurrentCursor().device_scale_factor());
980 // Switch primary and secondary
981 display_controller->SetPrimaryDisplay(secondary_display);
983 // Cursor's device scale factor should be updated accroding to the swap of
984 // primary and secondary.
985 EXPECT_EQ(1.0f, secondary_root->GetHost()->compositor()->
986 device_scale_factor());
987 secondary_root->MoveCursorTo(gfx::Point(50, 50));
988 EXPECT_EQ(1.0f, test_api.GetCurrentCursor().device_scale_factor());
989 primary_root->MoveCursorTo(gfx::Point(50, 50));
990 EXPECT_EQ(2.0f, primary_root->GetHost()->compositor()->
991 device_scale_factor());
992 EXPECT_EQ(2.0f, test_api.GetCurrentCursor().device_scale_factor());
994 // Deleting 2nd display.
995 UpdateDisplay("200x200");
996 RunAllPendingInMessageLoop(); // RootWindow is deleted in a posted task.
998 // Cursor's device scale factor should be updated even without moving cursor.
999 EXPECT_EQ(1.0f, test_api.GetCurrentCursor().device_scale_factor());
1001 primary_root->MoveCursorTo(gfx::Point(50, 50));
1002 EXPECT_EQ(1.0f, primary_root->GetHost()->compositor()->
1003 device_scale_factor());
1004 EXPECT_EQ(1.0f, test_api.GetCurrentCursor().device_scale_factor());
1007 TEST_F(DisplayControllerTest, OverscanInsets) {
1008 if (!SupportsMultipleDisplays())
1009 return;
1011 DisplayController* display_controller =
1012 Shell::GetInstance()->display_controller();
1013 TestEventHandler event_handler;
1014 Shell::GetInstance()->AddPreTargetHandler(&event_handler);
1016 UpdateDisplay("120x200,300x400*2");
1017 gfx::Display display1 = Shell::GetScreen()->GetPrimaryDisplay();
1018 aura::Window::Windows root_windows = Shell::GetAllRootWindows();
1020 display_controller->SetOverscanInsets(display1.id(),
1021 gfx::Insets(10, 15, 20, 25));
1022 EXPECT_EQ("0,0 80x170", root_windows[0]->bounds().ToString());
1023 EXPECT_EQ("150x200", root_windows[1]->bounds().size().ToString());
1024 EXPECT_EQ("80,0 150x200",
1025 ScreenUtil::GetSecondaryDisplay().bounds().ToString());
1027 aura::test::EventGenerator generator(root_windows[0]);
1028 generator.MoveMouseToInHost(20, 25);
1029 EXPECT_EQ("5,15", event_handler.GetLocationAndReset());
1031 display_controller->SetOverscanInsets(display1.id(), gfx::Insets());
1032 EXPECT_EQ("0,0 120x200", root_windows[0]->bounds().ToString());
1033 EXPECT_EQ("120,0 150x200",
1034 ScreenUtil::GetSecondaryDisplay().bounds().ToString());
1036 generator.MoveMouseToInHost(30, 20);
1037 EXPECT_EQ("30,20", event_handler.GetLocationAndReset());
1039 // Make sure the root window transformer uses correct scale
1040 // factor when swapping display. Test crbug.com/253690.
1041 UpdateDisplay("400x300*2,600x400/o");
1042 root_windows = Shell::GetAllRootWindows();
1043 gfx::Point point;
1044 Shell::GetAllRootWindows()[1]->GetHost()->
1045 GetRootTransform().TransformPoint(&point);
1046 EXPECT_EQ("15,10", point.ToString());
1048 display_controller->SwapPrimaryDisplay();
1049 point.SetPoint(0, 0);
1050 Shell::GetAllRootWindows()[1]->GetHost()->
1051 GetRootTransform().TransformPoint(&point);
1052 EXPECT_EQ("15,10", point.ToString());
1054 Shell::GetInstance()->RemovePreTargetHandler(&event_handler);
1057 TEST_F(DisplayControllerTest, Rotate) {
1058 if (!SupportsMultipleDisplays())
1059 return;
1061 DisplayManager* display_manager = Shell::GetInstance()->display_manager();
1062 TestEventHandler event_handler;
1063 Shell::GetInstance()->AddPreTargetHandler(&event_handler);
1065 UpdateDisplay("120x200,300x400*2");
1066 gfx::Display display1 = Shell::GetScreen()->GetPrimaryDisplay();
1067 int64 display2_id = ScreenUtil::GetSecondaryDisplay().id();
1068 aura::Window::Windows root_windows = Shell::GetAllRootWindows();
1069 aura::test::EventGenerator generator1(root_windows[0]);
1071 TestObserver observer;
1072 EXPECT_EQ("120x200", root_windows[0]->bounds().size().ToString());
1073 EXPECT_EQ("150x200", root_windows[1]->bounds().size().ToString());
1074 EXPECT_EQ("120,0 150x200",
1075 ScreenUtil::GetSecondaryDisplay().bounds().ToString());
1076 generator1.MoveMouseToInHost(50, 40);
1077 EXPECT_EQ("50,40", event_handler.GetLocationAndReset());
1078 EXPECT_EQ(gfx::Display::ROTATE_0, GetStoredRotation(display1.id()));
1079 EXPECT_EQ(gfx::Display::ROTATE_0, GetStoredRotation(display2_id));
1080 EXPECT_EQ(0, observer.GetRotationChangedCountAndReset());
1082 display_manager->SetDisplayRotation(display1.id(),
1083 gfx::Display::ROTATE_90);
1084 EXPECT_EQ("200x120", root_windows[0]->bounds().size().ToString());
1085 EXPECT_EQ("150x200", root_windows[1]->bounds().size().ToString());
1086 EXPECT_EQ("200,0 150x200",
1087 ScreenUtil::GetSecondaryDisplay().bounds().ToString());
1088 generator1.MoveMouseToInHost(50, 40);
1089 EXPECT_EQ("40,69", event_handler.GetLocationAndReset());
1090 EXPECT_EQ(gfx::Display::ROTATE_90, GetStoredRotation(display1.id()));
1091 EXPECT_EQ(gfx::Display::ROTATE_0, GetStoredRotation(display2_id));
1092 EXPECT_EQ(1, observer.GetRotationChangedCountAndReset());
1094 DisplayLayout display_layout(DisplayLayout::BOTTOM, 50);
1095 display_manager->SetLayoutForCurrentDisplays(display_layout);
1096 EXPECT_EQ("50,120 150x200",
1097 ScreenUtil::GetSecondaryDisplay().bounds().ToString());
1099 display_manager->SetDisplayRotation(display2_id,
1100 gfx::Display::ROTATE_270);
1101 EXPECT_EQ("200x120", root_windows[0]->bounds().size().ToString());
1102 EXPECT_EQ("200x150", root_windows[1]->bounds().size().ToString());
1103 EXPECT_EQ("50,120 200x150",
1104 ScreenUtil::GetSecondaryDisplay().bounds().ToString());
1105 EXPECT_EQ(gfx::Display::ROTATE_90, GetStoredRotation(display1.id()));
1106 EXPECT_EQ(gfx::Display::ROTATE_270, GetStoredRotation(display2_id));
1107 EXPECT_EQ(1, observer.GetRotationChangedCountAndReset());
1109 #if !defined(OS_WIN)
1110 aura::test::EventGenerator generator2(root_windows[1]);
1111 generator2.MoveMouseToInHost(50, 40);
1112 EXPECT_EQ("179,25", event_handler.GetLocationAndReset());
1113 display_manager->SetDisplayRotation(display1.id(),
1114 gfx::Display::ROTATE_180);
1116 EXPECT_EQ("120x200", root_windows[0]->bounds().size().ToString());
1117 EXPECT_EQ("200x150", root_windows[1]->bounds().size().ToString());
1118 // Dislay must share at least 100, so the x's offset becomes 20.
1119 EXPECT_EQ("20,200 200x150",
1120 ScreenUtil::GetSecondaryDisplay().bounds().ToString());
1121 EXPECT_EQ(gfx::Display::ROTATE_180, GetStoredRotation(display1.id()));
1122 EXPECT_EQ(gfx::Display::ROTATE_270, GetStoredRotation(display2_id));
1123 EXPECT_EQ(1, observer.GetRotationChangedCountAndReset());
1125 generator1.MoveMouseToInHost(50, 40);
1126 EXPECT_EQ("69,159", event_handler.GetLocationAndReset());
1127 #endif
1129 Shell::GetInstance()->RemovePreTargetHandler(&event_handler);
1132 TEST_F(DisplayControllerTest, ScaleRootWindow) {
1133 if (!SupportsMultipleDisplays())
1134 return;
1136 TestEventHandler event_handler;
1137 Shell::GetInstance()->AddPreTargetHandler(&event_handler);
1139 UpdateDisplay("600x400*2@1.5,500x300");
1141 gfx::Display display1 = Shell::GetScreen()->GetPrimaryDisplay();
1142 gfx::Display::SetInternalDisplayId(display1.id());
1144 gfx::Display display2 = ScreenUtil::GetSecondaryDisplay();
1145 aura::Window::Windows root_windows = Shell::GetAllRootWindows();
1146 EXPECT_EQ("0,0 450x300", display1.bounds().ToString());
1147 EXPECT_EQ("0,0 450x300", root_windows[0]->bounds().ToString());
1148 EXPECT_EQ("450,0 500x300", display2.bounds().ToString());
1149 EXPECT_EQ(1.5f, GetStoredUIScale(display1.id()));
1150 EXPECT_EQ(1.0f, GetStoredUIScale(display2.id()));
1152 aura::test::EventGenerator generator(root_windows[0]);
1153 generator.MoveMouseToInHost(599, 200);
1154 EXPECT_EQ("449,150", event_handler.GetLocationAndReset());
1156 DisplayManager* display_manager = Shell::GetInstance()->display_manager();
1157 display_manager->SetDisplayUIScale(display1.id(), 1.25f);
1158 display1 = Shell::GetScreen()->GetPrimaryDisplay();
1159 display2 = ScreenUtil::GetSecondaryDisplay();
1160 EXPECT_EQ("0,0 375x250", display1.bounds().ToString());
1161 EXPECT_EQ("0,0 375x250", root_windows[0]->bounds().ToString());
1162 EXPECT_EQ("375,0 500x300", display2.bounds().ToString());
1163 EXPECT_EQ(1.25f, GetStoredUIScale(display1.id()));
1164 EXPECT_EQ(1.0f, GetStoredUIScale(display2.id()));
1166 Shell::GetInstance()->RemovePreTargetHandler(&event_handler);
1169 TEST_F(DisplayControllerTest, TouchScale) {
1170 if (!SupportsMultipleDisplays())
1171 return;
1173 TestEventHandler event_handler;
1174 Shell::GetInstance()->AddPreTargetHandler(&event_handler);
1176 UpdateDisplay("200x200*2");
1177 gfx::Display display = Shell::GetScreen()->GetPrimaryDisplay();
1178 aura::Window::Windows root_windows = Shell::GetAllRootWindows();
1179 aura::Window* root_window = root_windows[0];
1180 aura::test::EventGenerator generator(root_window);
1182 generator.PressMoveAndReleaseTouchTo(50, 50);
1183 // Default test touches have radius_x/y = 1.0, with device scale
1184 // factor = 2, the scaled radius_x/y should be 0.5.
1185 EXPECT_EQ(0.5, event_handler.touch_radius_x());
1186 EXPECT_EQ(0.5, event_handler.touch_radius_y());
1188 generator.ScrollSequence(gfx::Point(0,0),
1189 base::TimeDelta::FromMilliseconds(100),
1190 10.0, 1.0, 5, 1);
1192 // ordinal_offset is invariant to the device scale factor.
1193 EXPECT_EQ(event_handler.scroll_x_offset(),
1194 event_handler.scroll_x_offset_ordinal());
1195 EXPECT_EQ(event_handler.scroll_y_offset(),
1196 event_handler.scroll_y_offset_ordinal());
1198 Shell::GetInstance()->RemovePreTargetHandler(&event_handler);
1201 TEST_F(DisplayControllerTest, ConvertHostToRootCoords) {
1202 if (!SupportsMultipleDisplays())
1203 return;
1205 TestEventHandler event_handler;
1206 Shell::GetInstance()->AddPreTargetHandler(&event_handler);
1208 UpdateDisplay("600x400*2/r@1.5");
1210 gfx::Display display1 = Shell::GetScreen()->GetPrimaryDisplay();
1211 aura::Window::Windows root_windows = Shell::GetAllRootWindows();
1212 EXPECT_EQ("0,0 300x450", display1.bounds().ToString());
1213 EXPECT_EQ("0,0 300x450", root_windows[0]->bounds().ToString());
1214 EXPECT_EQ(1.5f, GetStoredUIScale(display1.id()));
1216 aura::test::EventGenerator generator(root_windows[0]);
1217 generator.MoveMouseToInHost(0, 0);
1218 EXPECT_EQ("0,449", event_handler.GetLocationAndReset());
1219 generator.MoveMouseToInHost(599, 0);
1220 EXPECT_EQ("0,0", event_handler.GetLocationAndReset());
1221 generator.MoveMouseToInHost(599, 399);
1222 EXPECT_EQ("299,0", event_handler.GetLocationAndReset());
1223 generator.MoveMouseToInHost(0, 399);
1224 EXPECT_EQ("299,449", event_handler.GetLocationAndReset());
1226 UpdateDisplay("600x400*2/u@1.5");
1227 display1 = Shell::GetScreen()->GetPrimaryDisplay();
1228 root_windows = Shell::GetAllRootWindows();
1229 EXPECT_EQ("0,0 450x300", display1.bounds().ToString());
1230 EXPECT_EQ("0,0 450x300", root_windows[0]->bounds().ToString());
1231 EXPECT_EQ(1.5f, GetStoredUIScale(display1.id()));
1233 generator.MoveMouseToInHost(0, 0);
1234 EXPECT_EQ("449,299", event_handler.GetLocationAndReset());
1235 generator.MoveMouseToInHost(599, 0);
1236 EXPECT_EQ("0,299", event_handler.GetLocationAndReset());
1237 generator.MoveMouseToInHost(599, 399);
1238 EXPECT_EQ("0,0", event_handler.GetLocationAndReset());
1239 generator.MoveMouseToInHost(0, 399);
1240 EXPECT_EQ("449,0", event_handler.GetLocationAndReset());
1242 UpdateDisplay("600x400*2/l@1.5");
1243 display1 = Shell::GetScreen()->GetPrimaryDisplay();
1244 root_windows = Shell::GetAllRootWindows();
1245 EXPECT_EQ("0,0 300x450", display1.bounds().ToString());
1246 EXPECT_EQ("0,0 300x450", root_windows[0]->bounds().ToString());
1247 EXPECT_EQ(1.5f, GetStoredUIScale(display1.id()));
1249 generator.MoveMouseToInHost(0, 0);
1250 EXPECT_EQ("299,0", event_handler.GetLocationAndReset());
1251 generator.MoveMouseToInHost(599, 0);
1252 EXPECT_EQ("299,449", event_handler.GetLocationAndReset());
1253 generator.MoveMouseToInHost(599, 399);
1254 EXPECT_EQ("0,449", event_handler.GetLocationAndReset());
1255 generator.MoveMouseToInHost(0, 399);
1256 EXPECT_EQ("0,0", event_handler.GetLocationAndReset());
1258 Shell::GetInstance()->RemovePreTargetHandler(&event_handler);
1261 namespace {
1263 DisplayInfo CreateDisplayInfo(int64 id,
1264 int y,
1265 gfx::Display::Rotation rotation) {
1266 DisplayInfo info(id, "", false);
1267 info.SetBounds(gfx::Rect(0, y, 500, 500));
1268 info.set_rotation(rotation);
1269 return info;
1272 } // namespace
1274 // Make sure that the compositor based mirroring can switch
1275 // from/to dock mode.
1276 TEST_F(DisplayControllerTest, DockToSingle) {
1277 if (!SupportsMultipleDisplays())
1278 return;
1280 DisplayManager* display_manager = Shell::GetInstance()->display_manager();
1282 const int64 internal_id = 1;
1284 const DisplayInfo internal_display_info =
1285 CreateDisplayInfo(internal_id, 0, gfx::Display::ROTATE_0);
1286 const DisplayInfo external_display_info =
1287 CreateDisplayInfo(2, 1, gfx::Display::ROTATE_90);
1289 std::vector<DisplayInfo> display_info_list;
1290 // Extended
1291 display_info_list.push_back(internal_display_info);
1292 display_info_list.push_back(external_display_info);
1293 display_manager->OnNativeDisplaysChanged(display_info_list);
1294 const int64 internal_display_id =
1295 test::DisplayManagerTestApi(display_manager).
1296 SetFirstDisplayAsInternalDisplay();
1297 EXPECT_EQ(internal_id, internal_display_id);
1298 EXPECT_EQ(2U, display_manager->GetNumDisplays());
1300 // Dock mode.
1301 display_info_list.clear();
1302 display_info_list.push_back(external_display_info);
1303 display_manager->OnNativeDisplaysChanged(display_info_list);
1304 EXPECT_EQ(1U, display_manager->GetNumDisplays());
1305 EXPECT_FALSE(Shell::GetPrimaryRootWindow()->GetHost()->
1306 GetRootTransform().IsIdentityOrIntegerTranslation());
1308 // Switch to single mode and make sure the transform is the one
1309 // for the internal display.
1310 display_info_list.clear();
1311 display_info_list.push_back(internal_display_info);
1312 display_manager->OnNativeDisplaysChanged(display_info_list);
1313 EXPECT_TRUE(Shell::GetPrimaryRootWindow()->GetHost()->
1314 GetRootTransform().IsIdentityOrIntegerTranslation());
1317 #if defined(USE_X11)
1318 TEST_F(DisplayControllerTest, XWidowNameForRootWindow) {
1319 EXPECT_EQ("aura_root_0", GetXWindowName(
1320 Shell::GetPrimaryRootWindow()->GetHost()));
1322 // Multiple display.
1323 UpdateDisplay("200x200,300x300");
1324 aura::Window* primary, *secondary;
1325 GetPrimaryAndSeconary(&primary, &secondary);
1326 EXPECT_EQ("aura_root_0", GetXWindowName(primary->GetHost()));
1327 EXPECT_EQ("aura_root_x", GetXWindowName(secondary->GetHost()));
1329 // Swap primary.
1330 primary = secondary = NULL;
1331 Shell::GetInstance()->display_controller()->SwapPrimaryDisplay();
1332 GetPrimaryAndSeconary(&primary, &secondary);
1333 EXPECT_EQ("aura_root_0", GetXWindowName(primary->GetHost()));
1334 EXPECT_EQ("aura_root_x", GetXWindowName(secondary->GetHost()));
1336 // Switching back to single display.
1337 UpdateDisplay("300x400");
1338 EXPECT_EQ("aura_root_0", GetXWindowName(
1339 Shell::GetPrimaryRootWindow()->GetHost()));
1341 #endif
1343 } // namespace ash