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/session_state_delegate.h"
8 #include "ash/shelf/shelf_layout_manager.h"
10 #include "ash/shell_window_ids.h"
11 #include "ash/system/tray/system_tray_delegate.h"
12 #include "ash/test/ash_test_base.h"
13 #include "ash/wm/system_modal_container_layout_manager.h"
14 #include "ash/wm/window_properties.h"
15 #include "ash/wm/window_util.h"
16 #include "ui/aura/client/focus_change_observer.h"
17 #include "ui/aura/client/focus_client.h"
18 #include "ui/aura/env.h"
19 #include "ui/aura/root_window.h"
20 #include "ui/aura/test/event_generator.h"
21 #include "ui/aura/test/test_window_delegate.h"
22 #include "ui/aura/test/test_windows.h"
23 #include "ui/aura/window.h"
24 #include "ui/aura/window_tracker.h"
25 #include "ui/views/controls/menu/menu_controller.h"
26 #include "ui/views/widget/widget.h"
27 #include "ui/views/widget/widget_delegate.h"
35 class TestDelegate
: public views::WidgetDelegateView
{
37 explicit TestDelegate(bool system_modal
) : system_modal_(system_modal
) {}
38 virtual ~TestDelegate() {}
40 // Overridden from views::WidgetDelegate:
41 virtual views::View
* GetContentsView() OVERRIDE
{
45 virtual ui::ModalType
GetModalType() const OVERRIDE
{
46 return system_modal_
? ui::MODAL_TYPE_SYSTEM
: ui::MODAL_TYPE_NONE
;
51 DISALLOW_COPY_AND_ASSIGN(TestDelegate
);
54 class DeleteOnBlurDelegate
: public aura::test::TestWindowDelegate
,
55 public aura::client::FocusChangeObserver
{
57 DeleteOnBlurDelegate() : window_(NULL
) {}
58 virtual ~DeleteOnBlurDelegate() {}
60 void SetWindow(aura::Window
* window
) {
62 aura::client::SetFocusChangeObserver(window_
, this);
66 // aura::test::TestWindowDelegate overrides:
67 virtual bool CanFocus() OVERRIDE
{
71 // aura::client::FocusChangeObserver implementation:
72 virtual void OnWindowFocused(aura::Window
* gained_focus
,
73 aura::Window
* lost_focus
) OVERRIDE
{
74 if (window_
== lost_focus
)
78 aura::Window
* window_
;
80 DISALLOW_COPY_AND_ASSIGN(DeleteOnBlurDelegate
);
87 class RootWindowControllerTest
: public test::AshTestBase
{
89 views::Widget
* CreateTestWidget(const gfx::Rect
& bounds
) {
90 views::Widget
* widget
= views::Widget::CreateWindowWithContextAndBounds(
91 NULL
, CurrentContext(), bounds
);
96 views::Widget
* CreateModalWidget(const gfx::Rect
& bounds
) {
97 views::Widget
* widget
= views::Widget::CreateWindowWithContextAndBounds(
98 new TestDelegate(true), CurrentContext(), bounds
);
103 views::Widget
* CreateModalWidgetWithParent(const gfx::Rect
& bounds
,
104 gfx::NativeWindow parent
) {
105 views::Widget
* widget
=
106 views::Widget::CreateWindowWithParentAndBounds(new TestDelegate(true),
113 aura::Window
* GetModalContainer(aura::RootWindow
* root_window
) {
114 return Shell::GetContainer(
116 ash::internal::kShellWindowId_SystemModalContainer
);
120 TEST_F(RootWindowControllerTest
, MoveWindows_Basic
) {
121 if (!SupportsMultipleDisplays())
124 UpdateDisplay("600x600,500x500");
125 Shell::RootWindowList root_windows
= Shell::GetAllRootWindows();
126 internal::RootWindowController
* controller
=
127 Shell::GetPrimaryRootWindowController();
128 internal::ShelfLayoutManager
* shelf_layout_manager
=
129 controller
->GetShelfLayoutManager();
130 shelf_layout_manager
->SetAutoHideBehavior(
131 ash::SHELF_AUTO_HIDE_BEHAVIOR_ALWAYS
);
133 views::Widget
* normal
= CreateTestWidget(gfx::Rect(650, 10, 100, 100));
134 EXPECT_EQ(root_windows
[1], normal
->GetNativeView()->GetRootWindow());
135 EXPECT_EQ("650,10 100x100", normal
->GetWindowBoundsInScreen().ToString());
136 EXPECT_EQ("50,10 100x100",
137 normal
->GetNativeView()->GetBoundsInRootWindow().ToString());
139 views::Widget
* maximized
= CreateTestWidget(gfx::Rect(700, 10, 100, 100));
140 maximized
->Maximize();
141 EXPECT_EQ(root_windows
[1], maximized
->GetNativeView()->GetRootWindow());
142 EXPECT_EQ("600,0 500x452", maximized
->GetWindowBoundsInScreen().ToString());
143 EXPECT_EQ("0,0 500x452",
144 maximized
->GetNativeView()->GetBoundsInRootWindow().ToString());
146 views::Widget
* minimized
= CreateTestWidget(gfx::Rect(800, 10, 100, 100));
147 minimized
->Minimize();
148 EXPECT_EQ(root_windows
[1], minimized
->GetNativeView()->GetRootWindow());
149 EXPECT_EQ("800,10 100x100",
150 minimized
->GetWindowBoundsInScreen().ToString());
152 views::Widget
* fullscreen
= CreateTestWidget(gfx::Rect(900, 10, 100, 100));
153 fullscreen
->SetFullscreen(true);
154 EXPECT_EQ(root_windows
[1], fullscreen
->GetNativeView()->GetRootWindow());
156 EXPECT_EQ("600,0 500x500",
157 fullscreen
->GetWindowBoundsInScreen().ToString());
158 EXPECT_EQ("0,0 500x500",
159 fullscreen
->GetNativeView()->GetBoundsInRootWindow().ToString());
161 views::Widget
* unparented_control
= new Widget
;
162 Widget::InitParams params
;
163 params
.bounds
= gfx::Rect(650, 10, 100, 100);
164 params
.context
= CurrentContext();
165 params
.type
= Widget::InitParams::TYPE_CONTROL
;
166 unparented_control
->Init(params
);
167 EXPECT_EQ(root_windows
[1],
168 unparented_control
->GetNativeView()->GetRootWindow());
169 EXPECT_EQ(internal::kShellWindowId_UnparentedControlContainer
,
170 unparented_control
->GetNativeView()->parent()->id());
172 aura::Window
* panel
= CreateTestWindowInShellWithDelegateAndType(
173 NULL
, aura::client::WINDOW_TYPE_PANEL
, 0, gfx::Rect(700, 100, 100, 100));
174 EXPECT_EQ(root_windows
[1], panel
->GetRootWindow());
175 EXPECT_EQ(internal::kShellWindowId_PanelContainer
, panel
->parent()->id());
177 // Make sure a window that will delete itself when losing focus
179 aura::WindowTracker tracker
;
180 DeleteOnBlurDelegate delete_on_blur_delegate
;
181 aura::Window
* d2
= CreateTestWindowInShellWithDelegate(
182 &delete_on_blur_delegate
, 0, gfx::Rect(50, 50, 100, 100));
183 delete_on_blur_delegate
.SetWindow(d2
);
184 aura::client::GetFocusClient(root_windows
[0])->FocusWindow(d2
);
187 UpdateDisplay("600x600");
189 // d2 must have been deleted.
190 EXPECT_FALSE(tracker
.Contains(d2
));
192 EXPECT_EQ(root_windows
[0], normal
->GetNativeView()->GetRootWindow());
193 EXPECT_EQ("50,10 100x100", normal
->GetWindowBoundsInScreen().ToString());
194 EXPECT_EQ("50,10 100x100",
195 normal
->GetNativeView()->GetBoundsInRootWindow().ToString());
197 // Maximized area on primary display has 3px (given as
198 // kAutoHideSize in shelf_layout_manager.cc) inset at the bottom.
199 EXPECT_EQ(root_windows
[0], maximized
->GetNativeView()->GetRootWindow());
200 EXPECT_EQ("0,0 600x597",
201 maximized
->GetWindowBoundsInScreen().ToString());
202 EXPECT_EQ("0,0 600x597",
203 maximized
->GetNativeView()->GetBoundsInRootWindow().ToString());
205 EXPECT_EQ(root_windows
[0], minimized
->GetNativeView()->GetRootWindow());
206 EXPECT_EQ("200,10 100x100",
207 minimized
->GetWindowBoundsInScreen().ToString());
209 EXPECT_EQ(root_windows
[0], fullscreen
->GetNativeView()->GetRootWindow());
210 EXPECT_TRUE(fullscreen
->IsFullscreen());
211 EXPECT_EQ("0,0 600x600",
212 fullscreen
->GetWindowBoundsInScreen().ToString());
213 EXPECT_EQ("0,0 600x600",
214 fullscreen
->GetNativeView()->GetBoundsInRootWindow().ToString());
216 // Test if the restore bounds are correctly updated.
217 wm::RestoreWindow(maximized
->GetNativeView());
218 EXPECT_EQ("100,10 100x100", maximized
->GetWindowBoundsInScreen().ToString());
219 EXPECT_EQ("100,10 100x100",
220 maximized
->GetNativeView()->GetBoundsInRootWindow().ToString());
222 fullscreen
->SetFullscreen(false);
223 EXPECT_EQ("300,10 100x100",
224 fullscreen
->GetWindowBoundsInScreen().ToString());
225 EXPECT_EQ("300,10 100x100",
226 fullscreen
->GetNativeView()->GetBoundsInRootWindow().ToString());
228 // Test if the unparented widget has moved.
229 EXPECT_EQ(root_windows
[0],
230 unparented_control
->GetNativeView()->GetRootWindow());
231 EXPECT_EQ(internal::kShellWindowId_UnparentedControlContainer
,
232 unparented_control
->GetNativeView()->parent()->id());
234 // Test if the panel has moved.
235 EXPECT_EQ(root_windows
[0], panel
->GetRootWindow());
236 EXPECT_EQ(internal::kShellWindowId_PanelContainer
, panel
->parent()->id());
239 TEST_F(RootWindowControllerTest
, MoveWindows_Modal
) {
240 if (!SupportsMultipleDisplays())
243 UpdateDisplay("500x500,500x500");
245 Shell::RootWindowList root_windows
= Shell::GetAllRootWindows();
246 // Emulate virtual screen coordinate system.
247 root_windows
[0]->SetBounds(gfx::Rect(0, 0, 500, 500));
248 root_windows
[1]->SetBounds(gfx::Rect(500, 0, 500, 500));
250 views::Widget
* normal
= CreateTestWidget(gfx::Rect(300, 10, 100, 100));
251 EXPECT_EQ(root_windows
[0], normal
->GetNativeView()->GetRootWindow());
252 EXPECT_TRUE(wm::IsActiveWindow(normal
->GetNativeView()));
254 views::Widget
* modal
= CreateModalWidget(gfx::Rect(650, 10, 100, 100));
255 EXPECT_EQ(root_windows
[1], modal
->GetNativeView()->GetRootWindow());
256 EXPECT_TRUE(GetModalContainer(root_windows
[1])->Contains(
257 modal
->GetNativeView()));
258 EXPECT_TRUE(wm::IsActiveWindow(modal
->GetNativeView()));
260 aura::test::EventGenerator
generator_1st(root_windows
[0]);
261 generator_1st
.ClickLeftButton();
262 EXPECT_TRUE(wm::IsActiveWindow(modal
->GetNativeView()));
264 UpdateDisplay("500x500");
265 EXPECT_EQ(root_windows
[0], modal
->GetNativeView()->GetRootWindow());
266 EXPECT_TRUE(wm::IsActiveWindow(modal
->GetNativeView()));
267 generator_1st
.ClickLeftButton();
268 EXPECT_TRUE(wm::IsActiveWindow(modal
->GetNativeView()));
271 TEST_F(RootWindowControllerTest
, ModalContainer
) {
272 UpdateDisplay("600x600");
273 Shell
* shell
= Shell::GetInstance();
274 internal::RootWindowController
* controller
=
275 shell
->GetPrimaryRootWindowController();
276 EXPECT_EQ(user::LOGGED_IN_USER
,
277 shell
->system_tray_delegate()->GetUserLoginStatus());
278 EXPECT_EQ(Shell::GetContainer(controller
->root_window(),
279 internal::kShellWindowId_SystemModalContainer
)->layout_manager(),
280 controller
->GetSystemModalLayoutManager(NULL
));
282 views::Widget
* session_modal_widget
=
283 CreateModalWidget(gfx::Rect(300, 10, 100, 100));
284 EXPECT_EQ(Shell::GetContainer(controller
->root_window(),
285 internal::kShellWindowId_SystemModalContainer
)->layout_manager(),
286 controller
->GetSystemModalLayoutManager(
287 session_modal_widget
->GetNativeView()));
289 shell
->session_state_delegate()->LockScreen();
290 EXPECT_EQ(user::LOGGED_IN_LOCKED
,
291 shell
->system_tray_delegate()->GetUserLoginStatus());
292 EXPECT_EQ(Shell::GetContainer(controller
->root_window(),
293 internal::kShellWindowId_LockSystemModalContainer
)->layout_manager(),
294 controller
->GetSystemModalLayoutManager(NULL
));
296 aura::Window
* lock_container
=
297 Shell::GetContainer(controller
->root_window(),
298 internal::kShellWindowId_LockScreenContainer
);
299 views::Widget
* lock_modal_widget
=
300 CreateModalWidgetWithParent(gfx::Rect(300, 10, 100, 100), lock_container
);
301 EXPECT_EQ(Shell::GetContainer(controller
->root_window(),
302 internal::kShellWindowId_LockSystemModalContainer
)->layout_manager(),
303 controller
->GetSystemModalLayoutManager(
304 lock_modal_widget
->GetNativeView()));
305 EXPECT_EQ(Shell::GetContainer(controller
->root_window(),
306 internal::kShellWindowId_SystemModalContainer
)->layout_manager(),
307 controller
->GetSystemModalLayoutManager(
308 session_modal_widget
->GetNativeView()));
310 shell
->session_state_delegate()->UnlockScreen();
313 TEST_F(RootWindowControllerTest
, ModalContainerNotLoggedInLoggedIn
) {
314 UpdateDisplay("600x600");
315 Shell
* shell
= Shell::GetInstance();
317 // Configure login screen environment.
318 SetUserLoggedIn(false);
319 EXPECT_EQ(user::LOGGED_IN_NONE
,
320 shell
->system_tray_delegate()->GetUserLoginStatus());
321 EXPECT_EQ(0, shell
->session_state_delegate()->NumberOfLoggedInUsers());
322 EXPECT_FALSE(shell
->session_state_delegate()->IsActiveUserSessionStarted());
324 internal::RootWindowController
* controller
=
325 shell
->GetPrimaryRootWindowController();
326 EXPECT_EQ(Shell::GetContainer(controller
->root_window(),
327 internal::kShellWindowId_LockSystemModalContainer
)->layout_manager(),
328 controller
->GetSystemModalLayoutManager(NULL
));
330 aura::Window
* lock_container
=
331 Shell::GetContainer(controller
->root_window(),
332 internal::kShellWindowId_LockScreenContainer
);
333 views::Widget
* login_modal_widget
=
334 CreateModalWidgetWithParent(gfx::Rect(300, 10, 100, 100), lock_container
);
335 EXPECT_EQ(Shell::GetContainer(controller
->root_window(),
336 internal::kShellWindowId_LockSystemModalContainer
)->layout_manager(),
337 controller
->GetSystemModalLayoutManager(
338 login_modal_widget
->GetNativeView()));
339 login_modal_widget
->Close();
341 // Configure user session environment.
342 SetUserLoggedIn(true);
343 SetSessionStarted(true);
344 EXPECT_EQ(user::LOGGED_IN_USER
,
345 shell
->system_tray_delegate()->GetUserLoginStatus());
346 EXPECT_EQ(1, shell
->session_state_delegate()->NumberOfLoggedInUsers());
347 EXPECT_TRUE(shell
->session_state_delegate()->IsActiveUserSessionStarted());
348 EXPECT_EQ(Shell::GetContainer(controller
->root_window(),
349 internal::kShellWindowId_SystemModalContainer
)->layout_manager(),
350 controller
->GetSystemModalLayoutManager(NULL
));
352 views::Widget
* session_modal_widget
=
353 CreateModalWidget(gfx::Rect(300, 10, 100, 100));
354 EXPECT_EQ(Shell::GetContainer(controller
->root_window(),
355 internal::kShellWindowId_SystemModalContainer
)->layout_manager(),
356 controller
->GetSystemModalLayoutManager(
357 session_modal_widget
->GetNativeView()));
360 // Test that GetFullscreenWindow() returns a fullscreen window only if the
361 // fullscreen window is in the active workspace.
362 TEST_F(RootWindowControllerTest
, GetFullscreenWindow
) {
363 UpdateDisplay("600x600");
364 internal::RootWindowController
* controller
=
365 Shell::GetInstance()->GetPrimaryRootWindowController();
367 Widget
* w1
= CreateTestWidget(gfx::Rect(0, 0, 100, 100));
369 Widget
* w2
= CreateTestWidget(gfx::Rect(0, 0, 100, 100));
370 w2
->SetFullscreen(true);
371 // |w3| is a transient child of |w2|.
372 Widget
* w3
= Widget::CreateWindowWithParentAndBounds(NULL
,
373 w2
->GetNativeWindow(), gfx::Rect(0, 0, 100, 100));
375 // Test that GetFullscreenWindow() finds the fullscreen window when one of
376 // its transient children is active.
378 EXPECT_EQ(w2
->GetNativeWindow(), controller
->GetFullscreenWindow());
380 // Activate the maximized window's workspace. GetFullscreenWindow() should
381 // fail because the fullscreen window's workspace is no longer active.
383 EXPECT_FALSE(controller
->GetFullscreenWindow());
385 // If the fullscreen window is active, GetFullscreenWindow() should find it.
387 EXPECT_EQ(w2
->GetNativeWindow(), controller
->GetFullscreenWindow());