// Copyright (c) 2012 The Chromium Authors. All rights reserved. // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. #include "ash/display/display_controller.h" #include "ash/shell.h" #include "ash/test/ash_test_base.h" #include "ui/aura/display_manager.h" #include "ui/aura/env.h" #include "ui/aura/root_window.h" #include "ui/gfx/display.h" #include "ui/gfx/screen.h" namespace ash { namespace test { namespace { class TestObserver : public DisplayController::Observer { public: TestObserver() : count_(0) { Shell::GetInstance()->display_controller()->AddObserver(this); } virtual ~TestObserver() { Shell::GetInstance()->display_controller()->RemoveObserver(this); } virtual void OnDisplayConfigurationChanging() OVERRIDE { ++count_; } int CountAndReset() { int c = count_; count_ = 0; return c; } private: int count_; DISALLOW_COPY_AND_ASSIGN(TestObserver); }; gfx::Display GetPrimaryDisplay() { return gfx::Screen::GetDisplayNearestWindow( Shell::GetAllRootWindows()[0]); } gfx::Display GetSecondaryDisplay() { return gfx::Screen::GetDisplayNearestWindow( Shell::GetAllRootWindows()[1]); } void SetSecondaryDisplayLayout(DisplayLayout::Position position) { DisplayController* display_controller = Shell::GetInstance()->display_controller(); DisplayLayout layout = display_controller->default_display_layout(); layout.position = position; display_controller->SetDefaultDisplayLayout(layout); } } // namespace typedef test::AshTestBase DisplayControllerTest; #if defined(OS_WIN) // TOD(oshima): Windows creates a window with smaller client area. // Fix this and enable tests. #define MAYBE_SecondaryDisplayLayout DISABLED_SecondaryDisplayLayout #define MAYBE_BoundsUpdated DISABLED_BoundsUpdated #else #define MAYBE_SecondaryDisplayLayout SecondaryDisplayLayout #define MAYBE_BoundsUpdated BoundsUpdated #endif TEST_F(DisplayControllerTest, MAYBE_SecondaryDisplayLayout) { TestObserver observer; UpdateDisplay("500x500,400x400"); EXPECT_EQ(2, observer.CountAndReset()); // resize and add gfx::Display* secondary_display = aura::Env::GetInstance()->display_manager()->GetDisplayAt(1); gfx::Insets insets(5, 5, 5, 5); secondary_display->UpdateWorkAreaFromInsets(insets); // Default layout is LEFT. EXPECT_EQ("0,0 500x500", GetPrimaryDisplay().bounds().ToString()); EXPECT_EQ("500,0 400x400", GetSecondaryDisplay().bounds().ToString()); EXPECT_EQ("505,5 390x390", GetSecondaryDisplay().work_area().ToString()); // Layout the secondary display to the bottom of the primary. SetSecondaryDisplayLayout(DisplayLayout::BOTTOM); EXPECT_EQ(1, observer.CountAndReset()); EXPECT_EQ("0,0 500x500", GetPrimaryDisplay().bounds().ToString()); EXPECT_EQ("0,500 400x400", GetSecondaryDisplay().bounds().ToString()); EXPECT_EQ("5,505 390x390", GetSecondaryDisplay().work_area().ToString()); // Layout the secondary display to the left of the primary. SetSecondaryDisplayLayout(DisplayLayout::LEFT); EXPECT_EQ(1, observer.CountAndReset()); EXPECT_EQ("0,0 500x500", GetPrimaryDisplay().bounds().ToString()); EXPECT_EQ("-400,0 400x400", GetSecondaryDisplay().bounds().ToString()); EXPECT_EQ("-395,5 390x390", GetSecondaryDisplay().work_area().ToString()); // Layout the secondary display to the top of the primary. SetSecondaryDisplayLayout(DisplayLayout::TOP); EXPECT_EQ(1, observer.CountAndReset()); EXPECT_EQ("0,0 500x500", GetPrimaryDisplay().bounds().ToString()); EXPECT_EQ("0,-400 400x400", GetSecondaryDisplay().bounds().ToString()); EXPECT_EQ("5,-395 390x390", GetSecondaryDisplay().work_area().ToString()); } TEST_F(DisplayControllerTest, MAYBE_BoundsUpdated) { TestObserver observer; SetSecondaryDisplayLayout(DisplayLayout::BOTTOM); UpdateDisplay("200x200,300x300"); // layout, resize and add. EXPECT_EQ(3, observer.CountAndReset()); gfx::Display* secondary_display = aura::Env::GetInstance()->display_manager()->GetDisplayAt(1); gfx::Insets insets(5, 5, 5, 5); secondary_display->UpdateWorkAreaFromInsets(insets); EXPECT_EQ("0,0 200x200", GetPrimaryDisplay().bounds().ToString()); EXPECT_EQ("0,200 300x300", GetSecondaryDisplay().bounds().ToString()); EXPECT_EQ("5,205 290x290", GetSecondaryDisplay().work_area().ToString()); UpdateDisplay("400x400,200x200"); EXPECT_EQ(2, observer.CountAndReset()); // two resizes EXPECT_EQ("0,0 400x400", GetPrimaryDisplay().bounds().ToString()); EXPECT_EQ("0,400 200x200", GetSecondaryDisplay().bounds().ToString()); EXPECT_EQ("5,405 190x190", GetSecondaryDisplay().work_area().ToString()); UpdateDisplay("400x400,300x300"); EXPECT_EQ(1, observer.CountAndReset()); EXPECT_EQ("0,0 400x400", GetPrimaryDisplay().bounds().ToString()); EXPECT_EQ("0,400 300x300", GetSecondaryDisplay().bounds().ToString()); EXPECT_EQ("5,405 290x290", GetSecondaryDisplay().work_area().ToString()); UpdateDisplay("400x400"); EXPECT_EQ(1, observer.CountAndReset()); EXPECT_EQ("0,0 400x400", GetPrimaryDisplay().bounds().ToString()); EXPECT_EQ(1, gfx::Screen::GetNumDisplays()); UpdateDisplay("500x500,700x700"); EXPECT_EQ(2, observer.CountAndReset()); ASSERT_EQ(2, gfx::Screen::GetNumDisplays()); EXPECT_EQ("0,0 500x500", GetPrimaryDisplay().bounds().ToString()); EXPECT_EQ("0,500 700x700", GetSecondaryDisplay().bounds().ToString()); } } // namespace test } // namespace ash