Avoid unnecessary writes in ShellWindowGeometryCache
[chromium-blink-merge.git] / ash / root_window_controller_unittest.cc
blobe7ff23047fd8859a5c0253951ac447f3a509d35d
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/root_window_controller.h"
7 #include "ash/display/display_controller.h"
8 #include "ash/display/multi_display_manager.h"
9 #include "ash/shell.h"
10 #include "ash/shell_delegate.h"
11 #include "ash/shell_window_ids.h"
12 #include "ash/system/tray/system_tray_delegate.h"
13 #include "ash/test/ash_test_base.h"
14 #include "ash/wm/system_modal_container_layout_manager.h"
15 #include "ash/wm/window_util.h"
16 #include "ui/aura/env.h"
17 #include "ui/aura/focus_manager.h"
18 #include "ui/aura/root_window.h"
19 #include "ui/aura/test/event_generator.h"
20 #include "ui/aura/test/test_window_delegate.h"
21 #include "ui/aura/test/test_windows.h"
22 #include "ui/aura/window.h"
23 #include "ui/aura/window_tracker.h"
24 #include "ui/views/controls/menu/menu_controller.h"
25 #include "ui/views/widget/widget.h"
26 #include "ui/views/widget/widget_delegate.h"
28 namespace ash {
29 namespace {
31 class TestDelegate : public views::WidgetDelegateView {
32 public:
33 explicit TestDelegate(bool system_modal) : system_modal_(system_modal) {}
34 virtual ~TestDelegate() {}
36 // Overridden from views::WidgetDelegate:
37 virtual views::View* GetContentsView() OVERRIDE {
38 return this;
41 virtual ui::ModalType GetModalType() const OVERRIDE {
42 return system_modal_ ? ui::MODAL_TYPE_SYSTEM : ui::MODAL_TYPE_NONE;
45 private:
46 bool system_modal_;
47 DISALLOW_COPY_AND_ASSIGN(TestDelegate);
50 class DeleteOnBlurDelegate : public aura::test::TestWindowDelegate {
51 public:
52 DeleteOnBlurDelegate() : window_(NULL) {}
53 virtual ~DeleteOnBlurDelegate() {}
55 void set_window(aura::Window* window) { window_ = window; }
57 // aura::test::TestWindowDelegate overrides:
58 virtual bool CanFocus() OVERRIDE {
59 return true;
61 virtual void OnBlur() OVERRIDE {
62 delete window_;
65 private:
66 aura::Window* window_;
68 DISALLOW_COPY_AND_ASSIGN(DeleteOnBlurDelegate);
71 views::Widget* CreateTestWidget(const gfx::Rect& bounds) {
72 views::Widget* widget =
73 views::Widget::CreateWindowWithBounds(NULL, bounds);
74 widget->Show();
75 return widget;
78 views::Widget* CreateModalWidget(const gfx::Rect& bounds) {
79 views::Widget* widget =
80 views::Widget::CreateWindowWithBounds(new TestDelegate(true), bounds);
81 widget->Show();
82 return widget;
85 aura::Window* GetModalContainer(aura::RootWindow* root_window) {
86 return Shell::GetContainer(
87 root_window,
88 ash::internal::kShellWindowId_SystemModalContainer);
91 } // namespace
93 namespace test {
95 typedef test::AshTestBase RootWindowControllerTest;
97 TEST_F(RootWindowControllerTest, MoveWindows_Basic) {
98 UpdateDisplay("600x600,500x500");
99 Shell::RootWindowList root_windows = Shell::GetAllRootWindows();
100 internal::RootWindowController* controller =
101 Shell::GetPrimaryRootWindowController();
102 controller->SetShelfAutoHideBehavior(ash::SHELF_AUTO_HIDE_BEHAVIOR_ALWAYS);
104 views::Widget* normal = CreateTestWidget(gfx::Rect(650, 10, 100, 100));
105 EXPECT_EQ(root_windows[1], normal->GetNativeView()->GetRootWindow());
106 EXPECT_EQ("650,10 100x100", normal->GetWindowBoundsInScreen().ToString());
107 EXPECT_EQ("50,10 100x100",
108 normal->GetNativeView()->GetBoundsInRootWindow().ToString());
110 views::Widget* maximized = CreateTestWidget(gfx::Rect(700, 10, 100, 100));
111 maximized->Maximize();
112 EXPECT_EQ(root_windows[1], maximized->GetNativeView()->GetRootWindow());
113 #if !defined(OS_WIN)
114 // TODO(oshima): Window reports smaller screen size. Investigate why.
115 EXPECT_EQ("600,0 500x500", maximized->GetWindowBoundsInScreen().ToString());
116 EXPECT_EQ("0,0 500x500",
117 maximized->GetNativeView()->GetBoundsInRootWindow().ToString());
118 #endif
120 views::Widget* minimized = CreateTestWidget(gfx::Rect(800, 10, 100, 100));
121 minimized->Minimize();
122 EXPECT_EQ(root_windows[1], minimized->GetNativeView()->GetRootWindow());
123 EXPECT_EQ("800,10 100x100",
124 minimized->GetWindowBoundsInScreen().ToString());
126 views::Widget* fullscreen = CreateTestWidget(gfx::Rect(900, 10, 100, 100));
127 fullscreen->SetFullscreen(true);
128 EXPECT_EQ(root_windows[1], fullscreen->GetNativeView()->GetRootWindow());
129 #if !defined(OS_WIN)
130 // TODO(oshima): Window reports smaller screen size. Investigate why.
131 EXPECT_EQ("600,0 500x500",
132 fullscreen->GetWindowBoundsInScreen().ToString());
133 EXPECT_EQ("0,0 500x500",
134 fullscreen->GetNativeView()->GetBoundsInRootWindow().ToString());
135 #endif
137 // Make sure a window that will delete itself when losing focus
138 // will not crash.
139 aura::WindowTracker tracker;
140 DeleteOnBlurDelegate delete_on_blur_delegate;
141 aura::Window* d2 = aura::test::CreateTestWindowWithDelegate(
142 &delete_on_blur_delegate, 0, gfx::Rect(50, 50, 100, 100), NULL);
143 delete_on_blur_delegate.set_window(d2);
144 root_windows[0]->GetFocusManager()->SetFocusedWindow(
145 d2, NULL);
146 tracker.Add(d2);
148 UpdateDisplay("600x600");
150 // d2 must have been deleted.
151 EXPECT_FALSE(tracker.Contains(d2));
153 EXPECT_EQ(root_windows[0], normal->GetNativeView()->GetRootWindow());
154 EXPECT_EQ("50,10 100x100", normal->GetWindowBoundsInScreen().ToString());
155 EXPECT_EQ("50,10 100x100",
156 normal->GetNativeView()->GetBoundsInRootWindow().ToString());
158 // Maximized area on primary display has 3px (given as
159 // kAutoHideSize in shelf_layout_manager.cc) inset at the bottom.
160 EXPECT_EQ(root_windows[0], maximized->GetNativeView()->GetRootWindow());
161 EXPECT_EQ("0,0 600x597",
162 maximized->GetWindowBoundsInScreen().ToString());
163 EXPECT_EQ("0,0 600x597",
164 maximized->GetNativeView()->GetBoundsInRootWindow().ToString());
166 EXPECT_EQ(root_windows[0], minimized->GetNativeView()->GetRootWindow());
167 EXPECT_EQ("200,10 100x100",
168 minimized->GetWindowBoundsInScreen().ToString());
170 EXPECT_EQ(root_windows[0], fullscreen->GetNativeView()->GetRootWindow());
171 EXPECT_TRUE(fullscreen->IsFullscreen());
172 EXPECT_EQ("0,0 600x600",
173 fullscreen->GetWindowBoundsInScreen().ToString());
174 EXPECT_EQ("0,0 600x600",
175 fullscreen->GetNativeView()->GetBoundsInRootWindow().ToString());
177 // Test if the restore bounds are correctly updated.
178 wm::RestoreWindow(maximized->GetNativeView());
179 EXPECT_EQ("100,10 100x100", maximized->GetWindowBoundsInScreen().ToString());
180 EXPECT_EQ("100,10 100x100",
181 maximized->GetNativeView()->GetBoundsInRootWindow().ToString());
183 fullscreen->SetFullscreen(false);
184 EXPECT_EQ("300,10 100x100",
185 fullscreen->GetWindowBoundsInScreen().ToString());
186 EXPECT_EQ("300,10 100x100",
187 fullscreen->GetNativeView()->GetBoundsInRootWindow().ToString());
190 TEST_F(RootWindowControllerTest, MoveWindows_Modal) {
191 UpdateDisplay("500x500,500x500");
193 Shell::RootWindowList root_windows = Shell::GetAllRootWindows();
194 // Emulate virtual screen coordinate system.
195 root_windows[0]->SetBounds(gfx::Rect(0, 0, 500, 500));
196 root_windows[1]->SetBounds(gfx::Rect(500, 0, 500, 500));
198 views::Widget* normal = CreateTestWidget(gfx::Rect(300, 10, 100, 100));
199 EXPECT_EQ(root_windows[0], normal->GetNativeView()->GetRootWindow());
200 EXPECT_TRUE(wm::IsActiveWindow(normal->GetNativeView()));
202 views::Widget* modal = CreateModalWidget(gfx::Rect(650, 10, 100, 100));
203 EXPECT_EQ(root_windows[1], modal->GetNativeView()->GetRootWindow());
204 EXPECT_TRUE(GetModalContainer(root_windows[1])->Contains(
205 modal->GetNativeView()));
206 EXPECT_TRUE(wm::IsActiveWindow(modal->GetNativeView()));
208 aura::test::EventGenerator generator_1st(root_windows[0]);
209 generator_1st.ClickLeftButton();
210 EXPECT_TRUE(wm::IsActiveWindow(modal->GetNativeView()));
212 UpdateDisplay("500x500");
213 EXPECT_EQ(root_windows[0], modal->GetNativeView()->GetRootWindow());
214 EXPECT_TRUE(wm::IsActiveWindow(modal->GetNativeView()));
215 generator_1st.ClickLeftButton();
216 EXPECT_TRUE(wm::IsActiveWindow(modal->GetNativeView()));
219 TEST_F(RootWindowControllerTest, ModalContainer) {
220 UpdateDisplay("600x600");
221 Shell* shell = Shell::GetInstance();
222 internal::RootWindowController* controller =
223 shell->GetPrimaryRootWindowController();
224 EXPECT_EQ(user::LOGGED_IN_USER,
225 shell->tray_delegate()->GetUserLoginStatus());
226 EXPECT_EQ(Shell::GetContainer(controller->root_window(),
227 internal::kShellWindowId_SystemModalContainer)->layout_manager(),
228 controller->GetSystemModalLayoutManager(NULL));
230 shell->delegate()->LockScreen();
231 EXPECT_EQ(user::LOGGED_IN_LOCKED,
232 shell->tray_delegate()->GetUserLoginStatus());
233 EXPECT_EQ(Shell::GetContainer(controller->root_window(),
234 internal::kShellWindowId_LockSystemModalContainer)->layout_manager(),
235 controller->GetSystemModalLayoutManager(NULL));
236 shell->delegate()->UnlockScreen();
239 } // namespace test
240 } // namespace ash