blob: 7b1605b170aa891042277b35aca61c9f1e88934c [file] [log] [blame]
[email protected]f1853122012-06-27 16:21:261// 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.
4
[email protected]8674b312012-10-12 19:02:445#include "ash/root_window_controller.h"
6
[email protected]2e236a52012-06-27 22:21:477#include "ash/display/display_controller.h"
8#include "ash/display/multi_display_manager.h"
[email protected]f1853122012-06-27 16:21:269#include "ash/shell.h"
[email protected]8674b312012-10-12 19:02:4410#include "ash/shell_delegate.h"
[email protected]f1853122012-06-27 16:21:2611#include "ash/shell_window_ids.h"
[email protected]8674b312012-10-12 19:02:4412#include "ash/system/tray/system_tray_delegate.h"
[email protected]f1853122012-06-27 16:21:2613#include "ash/test/ash_test_base.h"
[email protected]8674b312012-10-12 19:02:4414#include "ash/wm/system_modal_container_layout_manager.h"
[email protected]f1853122012-06-27 16:21:2615#include "ash/wm/window_util.h"
16#include "ui/aura/env.h"
[email protected]dbf835d82012-09-11 18:23:0917#include "ui/aura/focus_manager.h"
[email protected]f1853122012-06-27 16:21:2618#include "ui/aura/root_window.h"
19#include "ui/aura/test/event_generator.h"
[email protected]dbf835d82012-09-11 18:23:0920#include "ui/aura/test/test_window_delegate.h"
21#include "ui/aura/test/test_windows.h"
[email protected]f1853122012-06-27 16:21:2622#include "ui/aura/window.h"
[email protected]dbf835d82012-09-11 18:23:0923#include "ui/aura/window_tracker.h"
[email protected]f1853122012-06-27 16:21:2624#include "ui/views/controls/menu/menu_controller.h"
25#include "ui/views/widget/widget.h"
26#include "ui/views/widget/widget_delegate.h"
27
28namespace ash {
29namespace {
30
31class TestDelegate : public views::WidgetDelegateView {
32 public:
33 explicit TestDelegate(bool system_modal) : system_modal_(system_modal) {}
34 virtual ~TestDelegate() {}
35
36 // Overridden from views::WidgetDelegate:
37 virtual views::View* GetContentsView() OVERRIDE {
38 return this;
39 }
40
41 virtual ui::ModalType GetModalType() const OVERRIDE {
42 return system_modal_ ? ui::MODAL_TYPE_SYSTEM : ui::MODAL_TYPE_NONE;
43 }
44
45 private:
46 bool system_modal_;
47 DISALLOW_COPY_AND_ASSIGN(TestDelegate);
48};
49
[email protected]dbf835d82012-09-11 18:23:0950class DeleteOnBlurDelegate : public aura::test::TestWindowDelegate {
51 public:
52 DeleteOnBlurDelegate() : window_(NULL) {}
53 virtual ~DeleteOnBlurDelegate() {}
54
55 void set_window(aura::Window* window) { window_ = window; }
56
57 // aura::test::TestWindowDelegate overrides:
58 virtual bool CanFocus() OVERRIDE {
59 return true;
60 }
61 virtual void OnBlur() OVERRIDE {
62 delete window_;
63 }
64
65 private:
66 aura::Window* window_;
67
68 DISALLOW_COPY_AND_ASSIGN(DeleteOnBlurDelegate);
69};
70
[email protected]f1853122012-06-27 16:21:2671views::Widget* CreateTestWidget(const gfx::Rect& bounds) {
72 views::Widget* widget =
73 views::Widget::CreateWindowWithBounds(NULL, bounds);
74 widget->Show();
75 return widget;
76}
77
78views::Widget* CreateModalWidget(const gfx::Rect& bounds) {
79 views::Widget* widget =
80 views::Widget::CreateWindowWithBounds(new TestDelegate(true), bounds);
81 widget->Show();
82 return widget;
83}
84
[email protected]3b162e12012-11-09 11:52:3585views::Widget* CreateModalWidgetWithParent(const gfx::Rect& bounds,
86 gfx::NativeWindow parent) {
87 views::Widget* widget =
88 views::Widget::CreateWindowWithParentAndBounds(new TestDelegate(true),
89 parent,
90 bounds);
91 widget->Show();
92 return widget;
93}
94
[email protected]f1853122012-06-27 16:21:2695aura::Window* GetModalContainer(aura::RootWindow* root_window) {
96 return Shell::GetContainer(
97 root_window,
98 ash::internal::kShellWindowId_SystemModalContainer);
99}
100
101} // namespace
102
103namespace test {
[email protected]f1853122012-06-27 16:21:26104
[email protected]3e4351b2012-08-09 04:04:16105typedef test::AshTestBase RootWindowControllerTest;
[email protected]f1853122012-06-27 16:21:26106
107TEST_F(RootWindowControllerTest, MoveWindows_Basic) {
[email protected]f634dd32012-07-23 22:49:07108 UpdateDisplay("600x600,500x500");
[email protected]f1853122012-06-27 16:21:26109 Shell::RootWindowList root_windows = Shell::GetAllRootWindows();
[email protected]431552c2012-10-23 00:38:33110 internal::RootWindowController* controller =
111 Shell::GetPrimaryRootWindowController();
112 controller->SetShelfAutoHideBehavior(ash::SHELF_AUTO_HIDE_BEHAVIOR_ALWAYS);
[email protected]f1853122012-06-27 16:21:26113
114 views::Widget* normal = CreateTestWidget(gfx::Rect(650, 10, 100, 100));
115 EXPECT_EQ(root_windows[1], normal->GetNativeView()->GetRootWindow());
[email protected]e2f64d102012-07-19 19:17:04116 EXPECT_EQ("650,10 100x100", normal->GetWindowBoundsInScreen().ToString());
[email protected]8d625fb2012-07-18 16:40:06117 EXPECT_EQ("50,10 100x100",
[email protected]e2f64d102012-07-19 19:17:04118 normal->GetNativeView()->GetBoundsInRootWindow().ToString());
[email protected]f1853122012-06-27 16:21:26119
120 views::Widget* maximized = CreateTestWidget(gfx::Rect(700, 10, 100, 100));
121 maximized->Maximize();
122 EXPECT_EQ(root_windows[1], maximized->GetNativeView()->GetRootWindow());
123#if !defined(OS_WIN)
124 // TODO(oshima): Window reports smaller screen size. Investigate why.
[email protected]e2f64d102012-07-19 19:17:04125 EXPECT_EQ("600,0 500x500", maximized->GetWindowBoundsInScreen().ToString());
[email protected]8d625fb2012-07-18 16:40:06126 EXPECT_EQ("0,0 500x500",
[email protected]e2f64d102012-07-19 19:17:04127 maximized->GetNativeView()->GetBoundsInRootWindow().ToString());
[email protected]f1853122012-06-27 16:21:26128#endif
129
130 views::Widget* minimized = CreateTestWidget(gfx::Rect(800, 10, 100, 100));
131 minimized->Minimize();
132 EXPECT_EQ(root_windows[1], minimized->GetNativeView()->GetRootWindow());
[email protected]8d625fb2012-07-18 16:40:06133 EXPECT_EQ("800,10 100x100",
[email protected]e2f64d102012-07-19 19:17:04134 minimized->GetWindowBoundsInScreen().ToString());
[email protected]f1853122012-06-27 16:21:26135
136 views::Widget* fullscreen = CreateTestWidget(gfx::Rect(900, 10, 100, 100));
137 fullscreen->SetFullscreen(true);
138 EXPECT_EQ(root_windows[1], fullscreen->GetNativeView()->GetRootWindow());
139#if !defined(OS_WIN)
140 // TODO(oshima): Window reports smaller screen size. Investigate why.
[email protected]8d625fb2012-07-18 16:40:06141 EXPECT_EQ("600,0 500x500",
[email protected]e2f64d102012-07-19 19:17:04142 fullscreen->GetWindowBoundsInScreen().ToString());
[email protected]8d625fb2012-07-18 16:40:06143 EXPECT_EQ("0,0 500x500",
[email protected]e2f64d102012-07-19 19:17:04144 fullscreen->GetNativeView()->GetBoundsInRootWindow().ToString());
[email protected]f1853122012-06-27 16:21:26145#endif
146
[email protected]dbf835d82012-09-11 18:23:09147 // Make sure a window that will delete itself when losing focus
148 // will not crash.
149 aura::WindowTracker tracker;
150 DeleteOnBlurDelegate delete_on_blur_delegate;
151 aura::Window* d2 = aura::test::CreateTestWindowWithDelegate(
152 &delete_on_blur_delegate, 0, gfx::Rect(50, 50, 100, 100), NULL);
153 delete_on_blur_delegate.set_window(d2);
154 root_windows[0]->GetFocusManager()->SetFocusedWindow(
155 d2, NULL);
156 tracker.Add(d2);
157
[email protected]f634dd32012-07-23 22:49:07158 UpdateDisplay("600x600");
[email protected]f1853122012-06-27 16:21:26159
[email protected]dbf835d82012-09-11 18:23:09160 // d2 must have been deleted.
161 EXPECT_FALSE(tracker.Contains(d2));
162
[email protected]f1853122012-06-27 16:21:26163 EXPECT_EQ(root_windows[0], normal->GetNativeView()->GetRootWindow());
[email protected]e2f64d102012-07-19 19:17:04164 EXPECT_EQ("50,10 100x100", normal->GetWindowBoundsInScreen().ToString());
[email protected]8d625fb2012-07-18 16:40:06165 EXPECT_EQ("50,10 100x100",
[email protected]e2f64d102012-07-19 19:17:04166 normal->GetNativeView()->GetBoundsInRootWindow().ToString());
[email protected]f1853122012-06-27 16:21:26167
[email protected]eefd51b22012-09-25 20:26:24168 // Maximized area on primary display has 3px (given as
[email protected]f1853122012-06-27 16:21:26169 // kAutoHideSize in shelf_layout_manager.cc) inset at the bottom.
170 EXPECT_EQ(root_windows[0], maximized->GetNativeView()->GetRootWindow());
[email protected]7b675df612012-09-16 18:33:20171 EXPECT_EQ("0,0 600x597",
[email protected]e2f64d102012-07-19 19:17:04172 maximized->GetWindowBoundsInScreen().ToString());
[email protected]7b675df612012-09-16 18:33:20173 EXPECT_EQ("0,0 600x597",
[email protected]e2f64d102012-07-19 19:17:04174 maximized->GetNativeView()->GetBoundsInRootWindow().ToString());
[email protected]f1853122012-06-27 16:21:26175
176 EXPECT_EQ(root_windows[0], minimized->GetNativeView()->GetRootWindow());
[email protected]8d625fb2012-07-18 16:40:06177 EXPECT_EQ("200,10 100x100",
[email protected]e2f64d102012-07-19 19:17:04178 minimized->GetWindowBoundsInScreen().ToString());
[email protected]f1853122012-06-27 16:21:26179
180 EXPECT_EQ(root_windows[0], fullscreen->GetNativeView()->GetRootWindow());
181 EXPECT_TRUE(fullscreen->IsFullscreen());
[email protected]8d625fb2012-07-18 16:40:06182 EXPECT_EQ("0,0 600x600",
[email protected]e2f64d102012-07-19 19:17:04183 fullscreen->GetWindowBoundsInScreen().ToString());
[email protected]8d625fb2012-07-18 16:40:06184 EXPECT_EQ("0,0 600x600",
[email protected]e2f64d102012-07-19 19:17:04185 fullscreen->GetNativeView()->GetBoundsInRootWindow().ToString());
[email protected]8d625fb2012-07-18 16:40:06186
187 // Test if the restore bounds are correctly updated.
188 wm::RestoreWindow(maximized->GetNativeView());
[email protected]e2f64d102012-07-19 19:17:04189 EXPECT_EQ("100,10 100x100", maximized->GetWindowBoundsInScreen().ToString());
[email protected]8d625fb2012-07-18 16:40:06190 EXPECT_EQ("100,10 100x100",
[email protected]e2f64d102012-07-19 19:17:04191 maximized->GetNativeView()->GetBoundsInRootWindow().ToString());
[email protected]8d625fb2012-07-18 16:40:06192
193 fullscreen->SetFullscreen(false);
194 EXPECT_EQ("300,10 100x100",
[email protected]e2f64d102012-07-19 19:17:04195 fullscreen->GetWindowBoundsInScreen().ToString());
[email protected]8d625fb2012-07-18 16:40:06196 EXPECT_EQ("300,10 100x100",
[email protected]e2f64d102012-07-19 19:17:04197 fullscreen->GetNativeView()->GetBoundsInRootWindow().ToString());
[email protected]f1853122012-06-27 16:21:26198}
199
200TEST_F(RootWindowControllerTest, MoveWindows_Modal) {
[email protected]f634dd32012-07-23 22:49:07201 UpdateDisplay("500x500,500x500");
[email protected]f1853122012-06-27 16:21:26202
203 Shell::RootWindowList root_windows = Shell::GetAllRootWindows();
204 // Emulate virtual screen coordinate system.
205 root_windows[0]->SetBounds(gfx::Rect(0, 0, 500, 500));
206 root_windows[1]->SetBounds(gfx::Rect(500, 0, 500, 500));
207
208 views::Widget* normal = CreateTestWidget(gfx::Rect(300, 10, 100, 100));
209 EXPECT_EQ(root_windows[0], normal->GetNativeView()->GetRootWindow());
210 EXPECT_TRUE(wm::IsActiveWindow(normal->GetNativeView()));
211
212 views::Widget* modal = CreateModalWidget(gfx::Rect(650, 10, 100, 100));
213 EXPECT_EQ(root_windows[1], modal->GetNativeView()->GetRootWindow());
214 EXPECT_TRUE(GetModalContainer(root_windows[1])->Contains(
215 modal->GetNativeView()));
216 EXPECT_TRUE(wm::IsActiveWindow(modal->GetNativeView()));
217
218 aura::test::EventGenerator generator_1st(root_windows[0]);
219 generator_1st.ClickLeftButton();
220 EXPECT_TRUE(wm::IsActiveWindow(modal->GetNativeView()));
221
[email protected]f634dd32012-07-23 22:49:07222 UpdateDisplay("500x500");
[email protected]f1853122012-06-27 16:21:26223 EXPECT_EQ(root_windows[0], modal->GetNativeView()->GetRootWindow());
224 EXPECT_TRUE(wm::IsActiveWindow(modal->GetNativeView()));
225 generator_1st.ClickLeftButton();
226 EXPECT_TRUE(wm::IsActiveWindow(modal->GetNativeView()));
227}
228
[email protected]8674b312012-10-12 19:02:44229TEST_F(RootWindowControllerTest, ModalContainer) {
230 UpdateDisplay("600x600");
231 Shell* shell = Shell::GetInstance();
232 internal::RootWindowController* controller =
233 shell->GetPrimaryRootWindowController();
234 EXPECT_EQ(user::LOGGED_IN_USER,
235 shell->tray_delegate()->GetUserLoginStatus());
236 EXPECT_EQ(Shell::GetContainer(controller->root_window(),
237 internal::kShellWindowId_SystemModalContainer)->layout_manager(),
238 controller->GetSystemModalLayoutManager(NULL));
239
[email protected]3b162e12012-11-09 11:52:35240 views::Widget* session_modal_widget =
241 CreateModalWidget(gfx::Rect(300, 10, 100, 100));
242 EXPECT_EQ(Shell::GetContainer(controller->root_window(),
243 internal::kShellWindowId_SystemModalContainer)->layout_manager(),
244 controller->GetSystemModalLayoutManager(
245 session_modal_widget->GetNativeView()));
246
[email protected]8674b312012-10-12 19:02:44247 shell->delegate()->LockScreen();
248 EXPECT_EQ(user::LOGGED_IN_LOCKED,
249 shell->tray_delegate()->GetUserLoginStatus());
250 EXPECT_EQ(Shell::GetContainer(controller->root_window(),
251 internal::kShellWindowId_LockSystemModalContainer)->layout_manager(),
252 controller->GetSystemModalLayoutManager(NULL));
[email protected]3b162e12012-11-09 11:52:35253
254 aura::Window* lock_container =
255 Shell::GetContainer(controller->root_window(),
256 internal::kShellWindowId_LockScreenContainer);
257 views::Widget* lock_modal_widget =
258 CreateModalWidgetWithParent(gfx::Rect(300, 10, 100, 100), lock_container);
259 EXPECT_EQ(Shell::GetContainer(controller->root_window(),
260 internal::kShellWindowId_LockSystemModalContainer)->layout_manager(),
261 controller->GetSystemModalLayoutManager(
262 lock_modal_widget->GetNativeView()));
263 EXPECT_EQ(Shell::GetContainer(controller->root_window(),
264 internal::kShellWindowId_SystemModalContainer)->layout_manager(),
265 controller->GetSystemModalLayoutManager(
266 session_modal_widget->GetNativeView()));
267
[email protected]8674b312012-10-12 19:02:44268 shell->delegate()->UnlockScreen();
269}
270
[email protected]1b219922012-11-13 21:16:43271TEST_F(RootWindowControllerTest, ModalContainerNotLoggedInLoggedIn) {
272 UpdateDisplay("600x600");
273 Shell* shell = Shell::GetInstance();
274
275 // Configure login screen environment.
276 SetUserLoggedIn(false);
277 EXPECT_EQ(user::LOGGED_IN_NONE,
278 shell->tray_delegate()->GetUserLoginStatus());
279 EXPECT_FALSE(shell->delegate()->IsUserLoggedIn());
280 EXPECT_FALSE(shell->delegate()->IsSessionStarted());
281
282 internal::RootWindowController* controller =
283 shell->GetPrimaryRootWindowController();
284 EXPECT_EQ(Shell::GetContainer(controller->root_window(),
285 internal::kShellWindowId_LockSystemModalContainer)->layout_manager(),
286 controller->GetSystemModalLayoutManager(NULL));
287
288 aura::Window* lock_container =
289 Shell::GetContainer(controller->root_window(),
290 internal::kShellWindowId_LockScreenContainer);
291 views::Widget* login_modal_widget =
292 CreateModalWidgetWithParent(gfx::Rect(300, 10, 100, 100), lock_container);
293 EXPECT_EQ(Shell::GetContainer(controller->root_window(),
294 internal::kShellWindowId_LockSystemModalContainer)->layout_manager(),
295 controller->GetSystemModalLayoutManager(
296 login_modal_widget->GetNativeView()));
297 login_modal_widget->Close();
298
299 // Configure user session environment.
300 SetUserLoggedIn(true);
301 SetSessionStarted(true);
302 EXPECT_EQ(user::LOGGED_IN_USER,
303 shell->tray_delegate()->GetUserLoginStatus());
304 EXPECT_TRUE(shell->delegate()->IsUserLoggedIn());
305 EXPECT_TRUE(shell->delegate()->IsSessionStarted());
306 EXPECT_EQ(Shell::GetContainer(controller->root_window(),
307 internal::kShellWindowId_SystemModalContainer)->layout_manager(),
308 controller->GetSystemModalLayoutManager(NULL));
309
310 views::Widget* session_modal_widget =
311 CreateModalWidget(gfx::Rect(300, 10, 100, 100));
312 EXPECT_EQ(Shell::GetContainer(controller->root_window(),
313 internal::kShellWindowId_SystemModalContainer)->layout_manager(),
314 controller->GetSystemModalLayoutManager(
315 session_modal_widget->GetNativeView()));
316}
317
[email protected]f1853122012-06-27 16:21:26318} // namespace test
319} // namespace ash