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"
9 #include "ash/shell_delegate.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_util.h"
15 #include "ui/aura/client/focus_change_observer.h"
16 #include "ui/aura/client/focus_client.h"
17 #include "ui/aura/env.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/corewm/focus_change_event.h"
26 #include "ui/views/widget/widget.h"
27 #include "ui/views/widget/widget_delegate.h"
32 class TestDelegate
: public views::WidgetDelegateView
{
34 explicit TestDelegate(bool system_modal
) : system_modal_(system_modal
) {}
35 virtual ~TestDelegate() {}
37 // Overridden from views::WidgetDelegate:
38 virtual views::View
* GetContentsView() OVERRIDE
{
42 virtual ui::ModalType
GetModalType() const OVERRIDE
{
43 return system_modal_
? ui::MODAL_TYPE_SYSTEM
: ui::MODAL_TYPE_NONE
;
48 DISALLOW_COPY_AND_ASSIGN(TestDelegate
);
51 class DeleteOnBlurDelegate
: public aura::test::TestWindowDelegate
,
52 public aura::client::FocusChangeObserver
{
54 DeleteOnBlurDelegate() : window_(NULL
) {}
55 virtual ~DeleteOnBlurDelegate() {}
57 void SetWindow(aura::Window
* window
) {
59 aura::client::SetFocusChangeObserver(window_
, this);
63 // aura::test::TestWindowDelegate overrides:
64 virtual bool CanFocus() OVERRIDE
{
68 // aura::client::FocusChangeObserver implementation:
69 virtual void OnWindowFocused(aura::Window
* gained_focus
,
70 aura::Window
* lost_focus
) OVERRIDE
{
71 if (window_
== lost_focus
)
75 aura::Window
* window_
;
77 DISALLOW_COPY_AND_ASSIGN(DeleteOnBlurDelegate
);
80 views::Widget
* CreateTestWidget(const gfx::Rect
& bounds
) {
81 views::Widget
* widget
=
82 views::Widget::CreateWindowWithBounds(NULL
, bounds
);
87 views::Widget
* CreateModalWidget(const gfx::Rect
& bounds
) {
88 views::Widget
* widget
=
89 views::Widget::CreateWindowWithBounds(new TestDelegate(true), bounds
);
94 views::Widget
* CreateModalWidgetWithParent(const gfx::Rect
& bounds
,
95 gfx::NativeWindow parent
) {
96 views::Widget
* widget
=
97 views::Widget::CreateWindowWithParentAndBounds(new TestDelegate(true),
104 aura::Window
* GetModalContainer(aura::RootWindow
* root_window
) {
105 return Shell::GetContainer(
107 ash::internal::kShellWindowId_SystemModalContainer
);
114 typedef test::AshTestBase RootWindowControllerTest
;
116 TEST_F(RootWindowControllerTest
, MoveWindows_Basic
) {
117 UpdateDisplay("600x600,500x500");
118 Shell::RootWindowList root_windows
= Shell::GetAllRootWindows();
119 internal::RootWindowController
* controller
=
120 Shell::GetPrimaryRootWindowController();
121 controller
->SetShelfAutoHideBehavior(ash::SHELF_AUTO_HIDE_BEHAVIOR_ALWAYS
);
123 views::Widget
* normal
= CreateTestWidget(gfx::Rect(650, 10, 100, 100));
124 EXPECT_EQ(root_windows
[1], normal
->GetNativeView()->GetRootWindow());
125 EXPECT_EQ("650,10 100x100", normal
->GetWindowBoundsInScreen().ToString());
126 EXPECT_EQ("50,10 100x100",
127 normal
->GetNativeView()->GetBoundsInRootWindow().ToString());
129 views::Widget
* maximized
= CreateTestWidget(gfx::Rect(700, 10, 100, 100));
130 maximized
->Maximize();
131 EXPECT_EQ(root_windows
[1], maximized
->GetNativeView()->GetRootWindow());
133 EXPECT_EQ("600,0 500x500", maximized
->GetWindowBoundsInScreen().ToString());
134 EXPECT_EQ("0,0 500x500",
135 maximized
->GetNativeView()->GetBoundsInRootWindow().ToString());
137 views::Widget
* minimized
= CreateTestWidget(gfx::Rect(800, 10, 100, 100));
138 minimized
->Minimize();
139 EXPECT_EQ(root_windows
[1], minimized
->GetNativeView()->GetRootWindow());
140 EXPECT_EQ("800,10 100x100",
141 minimized
->GetWindowBoundsInScreen().ToString());
143 views::Widget
* fullscreen
= CreateTestWidget(gfx::Rect(900, 10, 100, 100));
144 fullscreen
->SetFullscreen(true);
145 EXPECT_EQ(root_windows
[1], fullscreen
->GetNativeView()->GetRootWindow());
147 EXPECT_EQ("600,0 500x500",
148 fullscreen
->GetWindowBoundsInScreen().ToString());
149 EXPECT_EQ("0,0 500x500",
150 fullscreen
->GetNativeView()->GetBoundsInRootWindow().ToString());
152 // Make sure a window that will delete itself when losing focus
154 aura::WindowTracker tracker
;
155 DeleteOnBlurDelegate delete_on_blur_delegate
;
156 aura::Window
* d2
= CreateTestWindowInShellWithDelegate(
157 &delete_on_blur_delegate
, 0, gfx::Rect(50, 50, 100, 100));
158 delete_on_blur_delegate
.SetWindow(d2
);
159 aura::client::GetFocusClient(root_windows
[0])->FocusWindow(d2
, NULL
);
162 UpdateDisplay("600x600");
164 // d2 must have been deleted.
165 EXPECT_FALSE(tracker
.Contains(d2
));
167 EXPECT_EQ(root_windows
[0], normal
->GetNativeView()->GetRootWindow());
168 EXPECT_EQ("50,10 100x100", normal
->GetWindowBoundsInScreen().ToString());
169 EXPECT_EQ("50,10 100x100",
170 normal
->GetNativeView()->GetBoundsInRootWindow().ToString());
172 // Maximized area on primary display has 3px (given as
173 // kAutoHideSize in shelf_layout_manager.cc) inset at the bottom.
174 EXPECT_EQ(root_windows
[0], maximized
->GetNativeView()->GetRootWindow());
175 EXPECT_EQ("0,0 600x597",
176 maximized
->GetWindowBoundsInScreen().ToString());
177 EXPECT_EQ("0,0 600x597",
178 maximized
->GetNativeView()->GetBoundsInRootWindow().ToString());
180 EXPECT_EQ(root_windows
[0], minimized
->GetNativeView()->GetRootWindow());
181 EXPECT_EQ("200,10 100x100",
182 minimized
->GetWindowBoundsInScreen().ToString());
184 EXPECT_EQ(root_windows
[0], fullscreen
->GetNativeView()->GetRootWindow());
185 EXPECT_TRUE(fullscreen
->IsFullscreen());
186 EXPECT_EQ("0,0 600x600",
187 fullscreen
->GetWindowBoundsInScreen().ToString());
188 EXPECT_EQ("0,0 600x600",
189 fullscreen
->GetNativeView()->GetBoundsInRootWindow().ToString());
191 // Test if the restore bounds are correctly updated.
192 wm::RestoreWindow(maximized
->GetNativeView());
193 EXPECT_EQ("100,10 100x100", maximized
->GetWindowBoundsInScreen().ToString());
194 EXPECT_EQ("100,10 100x100",
195 maximized
->GetNativeView()->GetBoundsInRootWindow().ToString());
197 fullscreen
->SetFullscreen(false);
198 EXPECT_EQ("300,10 100x100",
199 fullscreen
->GetWindowBoundsInScreen().ToString());
200 EXPECT_EQ("300,10 100x100",
201 fullscreen
->GetNativeView()->GetBoundsInRootWindow().ToString());
204 TEST_F(RootWindowControllerTest
, MoveWindows_Modal
) {
205 UpdateDisplay("500x500,500x500");
207 Shell::RootWindowList root_windows
= Shell::GetAllRootWindows();
208 // Emulate virtual screen coordinate system.
209 root_windows
[0]->SetBounds(gfx::Rect(0, 0, 500, 500));
210 root_windows
[1]->SetBounds(gfx::Rect(500, 0, 500, 500));
212 views::Widget
* normal
= CreateTestWidget(gfx::Rect(300, 10, 100, 100));
213 EXPECT_EQ(root_windows
[0], normal
->GetNativeView()->GetRootWindow());
214 EXPECT_TRUE(wm::IsActiveWindow(normal
->GetNativeView()));
216 views::Widget
* modal
= CreateModalWidget(gfx::Rect(650, 10, 100, 100));
217 EXPECT_EQ(root_windows
[1], modal
->GetNativeView()->GetRootWindow());
218 EXPECT_TRUE(GetModalContainer(root_windows
[1])->Contains(
219 modal
->GetNativeView()));
220 EXPECT_TRUE(wm::IsActiveWindow(modal
->GetNativeView()));
222 aura::test::EventGenerator
generator_1st(root_windows
[0]);
223 generator_1st
.ClickLeftButton();
224 EXPECT_TRUE(wm::IsActiveWindow(modal
->GetNativeView()));
226 UpdateDisplay("500x500");
227 EXPECT_EQ(root_windows
[0], modal
->GetNativeView()->GetRootWindow());
228 EXPECT_TRUE(wm::IsActiveWindow(modal
->GetNativeView()));
229 generator_1st
.ClickLeftButton();
230 EXPECT_TRUE(wm::IsActiveWindow(modal
->GetNativeView()));
233 TEST_F(RootWindowControllerTest
, ModalContainer
) {
234 UpdateDisplay("600x600");
235 Shell
* shell
= Shell::GetInstance();
236 internal::RootWindowController
* controller
=
237 shell
->GetPrimaryRootWindowController();
238 EXPECT_EQ(user::LOGGED_IN_USER
,
239 shell
->tray_delegate()->GetUserLoginStatus());
240 EXPECT_EQ(Shell::GetContainer(controller
->root_window(),
241 internal::kShellWindowId_SystemModalContainer
)->layout_manager(),
242 controller
->GetSystemModalLayoutManager(NULL
));
244 views::Widget
* session_modal_widget
=
245 CreateModalWidget(gfx::Rect(300, 10, 100, 100));
246 EXPECT_EQ(Shell::GetContainer(controller
->root_window(),
247 internal::kShellWindowId_SystemModalContainer
)->layout_manager(),
248 controller
->GetSystemModalLayoutManager(
249 session_modal_widget
->GetNativeView()));
251 shell
->delegate()->LockScreen();
252 EXPECT_EQ(user::LOGGED_IN_LOCKED
,
253 shell
->tray_delegate()->GetUserLoginStatus());
254 EXPECT_EQ(Shell::GetContainer(controller
->root_window(),
255 internal::kShellWindowId_LockSystemModalContainer
)->layout_manager(),
256 controller
->GetSystemModalLayoutManager(NULL
));
258 aura::Window
* lock_container
=
259 Shell::GetContainer(controller
->root_window(),
260 internal::kShellWindowId_LockScreenContainer
);
261 views::Widget
* lock_modal_widget
=
262 CreateModalWidgetWithParent(gfx::Rect(300, 10, 100, 100), lock_container
);
263 EXPECT_EQ(Shell::GetContainer(controller
->root_window(),
264 internal::kShellWindowId_LockSystemModalContainer
)->layout_manager(),
265 controller
->GetSystemModalLayoutManager(
266 lock_modal_widget
->GetNativeView()));
267 EXPECT_EQ(Shell::GetContainer(controller
->root_window(),
268 internal::kShellWindowId_SystemModalContainer
)->layout_manager(),
269 controller
->GetSystemModalLayoutManager(
270 session_modal_widget
->GetNativeView()));
272 shell
->delegate()->UnlockScreen();
275 TEST_F(RootWindowControllerTest
, ModalContainerNotLoggedInLoggedIn
) {
276 UpdateDisplay("600x600");
277 Shell
* shell
= Shell::GetInstance();
279 // Configure login screen environment.
280 SetUserLoggedIn(false);
281 EXPECT_EQ(user::LOGGED_IN_NONE
,
282 shell
->tray_delegate()->GetUserLoginStatus());
283 EXPECT_FALSE(shell
->delegate()->IsUserLoggedIn());
284 EXPECT_FALSE(shell
->delegate()->IsSessionStarted());
286 internal::RootWindowController
* controller
=
287 shell
->GetPrimaryRootWindowController();
288 EXPECT_EQ(Shell::GetContainer(controller
->root_window(),
289 internal::kShellWindowId_LockSystemModalContainer
)->layout_manager(),
290 controller
->GetSystemModalLayoutManager(NULL
));
292 aura::Window
* lock_container
=
293 Shell::GetContainer(controller
->root_window(),
294 internal::kShellWindowId_LockScreenContainer
);
295 views::Widget
* login_modal_widget
=
296 CreateModalWidgetWithParent(gfx::Rect(300, 10, 100, 100), lock_container
);
297 EXPECT_EQ(Shell::GetContainer(controller
->root_window(),
298 internal::kShellWindowId_LockSystemModalContainer
)->layout_manager(),
299 controller
->GetSystemModalLayoutManager(
300 login_modal_widget
->GetNativeView()));
301 login_modal_widget
->Close();
303 // Configure user session environment.
304 SetUserLoggedIn(true);
305 SetSessionStarted(true);
306 EXPECT_EQ(user::LOGGED_IN_USER
,
307 shell
->tray_delegate()->GetUserLoginStatus());
308 EXPECT_TRUE(shell
->delegate()->IsUserLoggedIn());
309 EXPECT_TRUE(shell
->delegate()->IsSessionStarted());
310 EXPECT_EQ(Shell::GetContainer(controller
->root_window(),
311 internal::kShellWindowId_SystemModalContainer
)->layout_manager(),
312 controller
->GetSystemModalLayoutManager(NULL
));
314 views::Widget
* session_modal_widget
=
315 CreateModalWidget(gfx::Rect(300, 10, 100, 100));
316 EXPECT_EQ(Shell::GetContainer(controller
->root_window(),
317 internal::kShellWindowId_SystemModalContainer
)->layout_manager(),
318 controller
->GetSystemModalLayoutManager(
319 session_modal_widget
->GetNativeView()));