blob: e8f57639ddbf346dbabcf8ce638463161e5a0a9e [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"
[email protected]f1853122012-06-27 16:21:268#include "ash/shell.h"
[email protected]8674b312012-10-12 19:02:449#include "ash/shell_delegate.h"
[email protected]f1853122012-06-27 16:21:2610#include "ash/shell_window_ids.h"
[email protected]8674b312012-10-12 19:02:4411#include "ash/system/tray/system_tray_delegate.h"
[email protected]f1853122012-06-27 16:21:2612#include "ash/test/ash_test_base.h"
[email protected]8674b312012-10-12 19:02:4413#include "ash/wm/system_modal_container_layout_manager.h"
[email protected]f1853122012-06-27 16:21:2614#include "ash/wm/window_util.h"
[email protected]8cfb6722012-11-28 03:28:4615#include "ui/aura/client/focus_client.h"
[email protected]f1853122012-06-27 16:21:2616#include "ui/aura/env.h"
17#include "ui/aura/root_window.h"
18#include "ui/aura/test/event_generator.h"
[email protected]dbf835d82012-09-11 18:23:0919#include "ui/aura/test/test_window_delegate.h"
20#include "ui/aura/test/test_windows.h"
[email protected]f1853122012-06-27 16:21:2621#include "ui/aura/window.h"
[email protected]dbf835d82012-09-11 18:23:0922#include "ui/aura/window_tracker.h"
[email protected]f1853122012-06-27 16:21:2623#include "ui/views/controls/menu/menu_controller.h"
24#include "ui/views/widget/widget.h"
25#include "ui/views/widget/widget_delegate.h"
26
27namespace ash {
28namespace {
29
30class TestDelegate : public views::WidgetDelegateView {
31 public:
32 explicit TestDelegate(bool system_modal) : system_modal_(system_modal) {}
33 virtual ~TestDelegate() {}
34
35 // Overridden from views::WidgetDelegate:
36 virtual views::View* GetContentsView() OVERRIDE {
37 return this;
38 }
39
40 virtual ui::ModalType GetModalType() const OVERRIDE {
41 return system_modal_ ? ui::MODAL_TYPE_SYSTEM : ui::MODAL_TYPE_NONE;
42 }
43
44 private:
45 bool system_modal_;
46 DISALLOW_COPY_AND_ASSIGN(TestDelegate);
47};
48
[email protected]dbf835d82012-09-11 18:23:0949class DeleteOnBlurDelegate : public aura::test::TestWindowDelegate {
50 public:
51 DeleteOnBlurDelegate() : window_(NULL) {}
52 virtual ~DeleteOnBlurDelegate() {}
53
54 void set_window(aura::Window* window) { window_ = window; }
55
56 // aura::test::TestWindowDelegate overrides:
57 virtual bool CanFocus() OVERRIDE {
58 return true;
59 }
60 virtual void OnBlur() OVERRIDE {
61 delete window_;
62 }
63
64 private:
65 aura::Window* window_;
66
67 DISALLOW_COPY_AND_ASSIGN(DeleteOnBlurDelegate);
68};
69
[email protected]f1853122012-06-27 16:21:2670views::Widget* CreateTestWidget(const gfx::Rect& bounds) {
71 views::Widget* widget =
72 views::Widget::CreateWindowWithBounds(NULL, bounds);
73 widget->Show();
74 return widget;
75}
76
77views::Widget* CreateModalWidget(const gfx::Rect& bounds) {
78 views::Widget* widget =
79 views::Widget::CreateWindowWithBounds(new TestDelegate(true), bounds);
80 widget->Show();
81 return widget;
82}
83
[email protected]3b162e12012-11-09 11:52:3584views::Widget* CreateModalWidgetWithParent(const gfx::Rect& bounds,
85 gfx::NativeWindow parent) {
86 views::Widget* widget =
87 views::Widget::CreateWindowWithParentAndBounds(new TestDelegate(true),
88 parent,
89 bounds);
90 widget->Show();
91 return widget;
92}
93
[email protected]f1853122012-06-27 16:21:2694aura::Window* GetModalContainer(aura::RootWindow* root_window) {
95 return Shell::GetContainer(
96 root_window,
97 ash::internal::kShellWindowId_SystemModalContainer);
98}
99
100} // namespace
101
102namespace test {
[email protected]f1853122012-06-27 16:21:26103
[email protected]3e4351b2012-08-09 04:04:16104typedef test::AshTestBase RootWindowControllerTest;
[email protected]f1853122012-06-27 16:21:26105
106TEST_F(RootWindowControllerTest, MoveWindows_Basic) {
[email protected]f634dd32012-07-23 22:49:07107 UpdateDisplay("600x600,500x500");
[email protected]f1853122012-06-27 16:21:26108 Shell::RootWindowList root_windows = Shell::GetAllRootWindows();
[email protected]431552c2012-10-23 00:38:33109 internal::RootWindowController* controller =
110 Shell::GetPrimaryRootWindowController();
111 controller->SetShelfAutoHideBehavior(ash::SHELF_AUTO_HIDE_BEHAVIOR_ALWAYS);
[email protected]f1853122012-06-27 16:21:26112
113 views::Widget* normal = CreateTestWidget(gfx::Rect(650, 10, 100, 100));
114 EXPECT_EQ(root_windows[1], normal->GetNativeView()->GetRootWindow());
[email protected]e2f64d102012-07-19 19:17:04115 EXPECT_EQ("650,10 100x100", normal->GetWindowBoundsInScreen().ToString());
[email protected]8d625fb2012-07-18 16:40:06116 EXPECT_EQ("50,10 100x100",
[email protected]e2f64d102012-07-19 19:17:04117 normal->GetNativeView()->GetBoundsInRootWindow().ToString());
[email protected]f1853122012-06-27 16:21:26118
119 views::Widget* maximized = CreateTestWidget(gfx::Rect(700, 10, 100, 100));
120 maximized->Maximize();
121 EXPECT_EQ(root_windows[1], maximized->GetNativeView()->GetRootWindow());
[email protected]1a015382012-12-01 19:44:59122
[email protected]e2f64d102012-07-19 19:17:04123 EXPECT_EQ("600,0 500x500", maximized->GetWindowBoundsInScreen().ToString());
[email protected]8d625fb2012-07-18 16:40:06124 EXPECT_EQ("0,0 500x500",
[email protected]e2f64d102012-07-19 19:17:04125 maximized->GetNativeView()->GetBoundsInRootWindow().ToString());
[email protected]f1853122012-06-27 16:21:26126
127 views::Widget* minimized = CreateTestWidget(gfx::Rect(800, 10, 100, 100));
128 minimized->Minimize();
129 EXPECT_EQ(root_windows[1], minimized->GetNativeView()->GetRootWindow());
[email protected]8d625fb2012-07-18 16:40:06130 EXPECT_EQ("800,10 100x100",
[email protected]e2f64d102012-07-19 19:17:04131 minimized->GetWindowBoundsInScreen().ToString());
[email protected]f1853122012-06-27 16:21:26132
133 views::Widget* fullscreen = CreateTestWidget(gfx::Rect(900, 10, 100, 100));
134 fullscreen->SetFullscreen(true);
135 EXPECT_EQ(root_windows[1], fullscreen->GetNativeView()->GetRootWindow());
[email protected]1a015382012-12-01 19:44:59136
[email protected]8d625fb2012-07-18 16:40:06137 EXPECT_EQ("600,0 500x500",
[email protected]e2f64d102012-07-19 19:17:04138 fullscreen->GetWindowBoundsInScreen().ToString());
[email protected]8d625fb2012-07-18 16:40:06139 EXPECT_EQ("0,0 500x500",
[email protected]e2f64d102012-07-19 19:17:04140 fullscreen->GetNativeView()->GetBoundsInRootWindow().ToString());
[email protected]f1853122012-06-27 16:21:26141
[email protected]dbf835d82012-09-11 18:23:09142 // Make sure a window that will delete itself when losing focus
143 // will not crash.
144 aura::WindowTracker tracker;
145 DeleteOnBlurDelegate delete_on_blur_delegate;
[email protected]5ebe6102012-11-28 21:00:03146 aura::Window* d2 = CreateTestWindowInShellWithDelegate(
147 &delete_on_blur_delegate, 0, gfx::Rect(50, 50, 100, 100));
[email protected]dbf835d82012-09-11 18:23:09148 delete_on_blur_delegate.set_window(d2);
[email protected]8cfb6722012-11-28 03:28:46149 aura::client::GetFocusClient(root_windows[0])->FocusWindow(d2, NULL);
[email protected]dbf835d82012-09-11 18:23:09150 tracker.Add(d2);
151
[email protected]f634dd32012-07-23 22:49:07152 UpdateDisplay("600x600");
[email protected]f1853122012-06-27 16:21:26153
[email protected]dbf835d82012-09-11 18:23:09154 // d2 must have been deleted.
155 EXPECT_FALSE(tracker.Contains(d2));
156
[email protected]f1853122012-06-27 16:21:26157 EXPECT_EQ(root_windows[0], normal->GetNativeView()->GetRootWindow());
[email protected]e2f64d102012-07-19 19:17:04158 EXPECT_EQ("50,10 100x100", normal->GetWindowBoundsInScreen().ToString());
[email protected]8d625fb2012-07-18 16:40:06159 EXPECT_EQ("50,10 100x100",
[email protected]e2f64d102012-07-19 19:17:04160 normal->GetNativeView()->GetBoundsInRootWindow().ToString());
[email protected]f1853122012-06-27 16:21:26161
[email protected]eefd51b22012-09-25 20:26:24162 // Maximized area on primary display has 3px (given as
[email protected]f1853122012-06-27 16:21:26163 // kAutoHideSize in shelf_layout_manager.cc) inset at the bottom.
164 EXPECT_EQ(root_windows[0], maximized->GetNativeView()->GetRootWindow());
[email protected]7b675df612012-09-16 18:33:20165 EXPECT_EQ("0,0 600x597",
[email protected]e2f64d102012-07-19 19:17:04166 maximized->GetWindowBoundsInScreen().ToString());
[email protected]7b675df612012-09-16 18:33:20167 EXPECT_EQ("0,0 600x597",
[email protected]e2f64d102012-07-19 19:17:04168 maximized->GetNativeView()->GetBoundsInRootWindow().ToString());
[email protected]f1853122012-06-27 16:21:26169
170 EXPECT_EQ(root_windows[0], minimized->GetNativeView()->GetRootWindow());
[email protected]8d625fb2012-07-18 16:40:06171 EXPECT_EQ("200,10 100x100",
[email protected]e2f64d102012-07-19 19:17:04172 minimized->GetWindowBoundsInScreen().ToString());
[email protected]f1853122012-06-27 16:21:26173
174 EXPECT_EQ(root_windows[0], fullscreen->GetNativeView()->GetRootWindow());
175 EXPECT_TRUE(fullscreen->IsFullscreen());
[email protected]8d625fb2012-07-18 16:40:06176 EXPECT_EQ("0,0 600x600",
[email protected]e2f64d102012-07-19 19:17:04177 fullscreen->GetWindowBoundsInScreen().ToString());
[email protected]8d625fb2012-07-18 16:40:06178 EXPECT_EQ("0,0 600x600",
[email protected]e2f64d102012-07-19 19:17:04179 fullscreen->GetNativeView()->GetBoundsInRootWindow().ToString());
[email protected]8d625fb2012-07-18 16:40:06180
181 // Test if the restore bounds are correctly updated.
182 wm::RestoreWindow(maximized->GetNativeView());
[email protected]e2f64d102012-07-19 19:17:04183 EXPECT_EQ("100,10 100x100", maximized->GetWindowBoundsInScreen().ToString());
[email protected]8d625fb2012-07-18 16:40:06184 EXPECT_EQ("100,10 100x100",
[email protected]e2f64d102012-07-19 19:17:04185 maximized->GetNativeView()->GetBoundsInRootWindow().ToString());
[email protected]8d625fb2012-07-18 16:40:06186
187 fullscreen->SetFullscreen(false);
188 EXPECT_EQ("300,10 100x100",
[email protected]e2f64d102012-07-19 19:17:04189 fullscreen->GetWindowBoundsInScreen().ToString());
[email protected]8d625fb2012-07-18 16:40:06190 EXPECT_EQ("300,10 100x100",
[email protected]e2f64d102012-07-19 19:17:04191 fullscreen->GetNativeView()->GetBoundsInRootWindow().ToString());
[email protected]f1853122012-06-27 16:21:26192}
193
194TEST_F(RootWindowControllerTest, MoveWindows_Modal) {
[email protected]f634dd32012-07-23 22:49:07195 UpdateDisplay("500x500,500x500");
[email protected]f1853122012-06-27 16:21:26196
197 Shell::RootWindowList root_windows = Shell::GetAllRootWindows();
198 // Emulate virtual screen coordinate system.
199 root_windows[0]->SetBounds(gfx::Rect(0, 0, 500, 500));
200 root_windows[1]->SetBounds(gfx::Rect(500, 0, 500, 500));
201
202 views::Widget* normal = CreateTestWidget(gfx::Rect(300, 10, 100, 100));
203 EXPECT_EQ(root_windows[0], normal->GetNativeView()->GetRootWindow());
204 EXPECT_TRUE(wm::IsActiveWindow(normal->GetNativeView()));
205
206 views::Widget* modal = CreateModalWidget(gfx::Rect(650, 10, 100, 100));
207 EXPECT_EQ(root_windows[1], modal->GetNativeView()->GetRootWindow());
208 EXPECT_TRUE(GetModalContainer(root_windows[1])->Contains(
209 modal->GetNativeView()));
210 EXPECT_TRUE(wm::IsActiveWindow(modal->GetNativeView()));
211
212 aura::test::EventGenerator generator_1st(root_windows[0]);
213 generator_1st.ClickLeftButton();
214 EXPECT_TRUE(wm::IsActiveWindow(modal->GetNativeView()));
215
[email protected]f634dd32012-07-23 22:49:07216 UpdateDisplay("500x500");
[email protected]f1853122012-06-27 16:21:26217 EXPECT_EQ(root_windows[0], modal->GetNativeView()->GetRootWindow());
218 EXPECT_TRUE(wm::IsActiveWindow(modal->GetNativeView()));
219 generator_1st.ClickLeftButton();
220 EXPECT_TRUE(wm::IsActiveWindow(modal->GetNativeView()));
221}
222
[email protected]8674b312012-10-12 19:02:44223TEST_F(RootWindowControllerTest, ModalContainer) {
224 UpdateDisplay("600x600");
225 Shell* shell = Shell::GetInstance();
226 internal::RootWindowController* controller =
227 shell->GetPrimaryRootWindowController();
228 EXPECT_EQ(user::LOGGED_IN_USER,
229 shell->tray_delegate()->GetUserLoginStatus());
230 EXPECT_EQ(Shell::GetContainer(controller->root_window(),
231 internal::kShellWindowId_SystemModalContainer)->layout_manager(),
232 controller->GetSystemModalLayoutManager(NULL));
233
[email protected]3b162e12012-11-09 11:52:35234 views::Widget* session_modal_widget =
235 CreateModalWidget(gfx::Rect(300, 10, 100, 100));
236 EXPECT_EQ(Shell::GetContainer(controller->root_window(),
237 internal::kShellWindowId_SystemModalContainer)->layout_manager(),
238 controller->GetSystemModalLayoutManager(
239 session_modal_widget->GetNativeView()));
240
[email protected]8674b312012-10-12 19:02:44241 shell->delegate()->LockScreen();
242 EXPECT_EQ(user::LOGGED_IN_LOCKED,
243 shell->tray_delegate()->GetUserLoginStatus());
244 EXPECT_EQ(Shell::GetContainer(controller->root_window(),
245 internal::kShellWindowId_LockSystemModalContainer)->layout_manager(),
246 controller->GetSystemModalLayoutManager(NULL));
[email protected]3b162e12012-11-09 11:52:35247
248 aura::Window* lock_container =
249 Shell::GetContainer(controller->root_window(),
250 internal::kShellWindowId_LockScreenContainer);
251 views::Widget* lock_modal_widget =
252 CreateModalWidgetWithParent(gfx::Rect(300, 10, 100, 100), lock_container);
253 EXPECT_EQ(Shell::GetContainer(controller->root_window(),
254 internal::kShellWindowId_LockSystemModalContainer)->layout_manager(),
255 controller->GetSystemModalLayoutManager(
256 lock_modal_widget->GetNativeView()));
257 EXPECT_EQ(Shell::GetContainer(controller->root_window(),
258 internal::kShellWindowId_SystemModalContainer)->layout_manager(),
259 controller->GetSystemModalLayoutManager(
260 session_modal_widget->GetNativeView()));
261
[email protected]8674b312012-10-12 19:02:44262 shell->delegate()->UnlockScreen();
263}
264
[email protected]1b219922012-11-13 21:16:43265TEST_F(RootWindowControllerTest, ModalContainerNotLoggedInLoggedIn) {
266 UpdateDisplay("600x600");
267 Shell* shell = Shell::GetInstance();
268
269 // Configure login screen environment.
270 SetUserLoggedIn(false);
271 EXPECT_EQ(user::LOGGED_IN_NONE,
272 shell->tray_delegate()->GetUserLoginStatus());
273 EXPECT_FALSE(shell->delegate()->IsUserLoggedIn());
274 EXPECT_FALSE(shell->delegate()->IsSessionStarted());
275
276 internal::RootWindowController* controller =
277 shell->GetPrimaryRootWindowController();
278 EXPECT_EQ(Shell::GetContainer(controller->root_window(),
279 internal::kShellWindowId_LockSystemModalContainer)->layout_manager(),
280 controller->GetSystemModalLayoutManager(NULL));
281
282 aura::Window* lock_container =
283 Shell::GetContainer(controller->root_window(),
284 internal::kShellWindowId_LockScreenContainer);
285 views::Widget* login_modal_widget =
286 CreateModalWidgetWithParent(gfx::Rect(300, 10, 100, 100), lock_container);
287 EXPECT_EQ(Shell::GetContainer(controller->root_window(),
288 internal::kShellWindowId_LockSystemModalContainer)->layout_manager(),
289 controller->GetSystemModalLayoutManager(
290 login_modal_widget->GetNativeView()));
291 login_modal_widget->Close();
292
293 // Configure user session environment.
294 SetUserLoggedIn(true);
295 SetSessionStarted(true);
296 EXPECT_EQ(user::LOGGED_IN_USER,
297 shell->tray_delegate()->GetUserLoginStatus());
298 EXPECT_TRUE(shell->delegate()->IsUserLoggedIn());
299 EXPECT_TRUE(shell->delegate()->IsSessionStarted());
300 EXPECT_EQ(Shell::GetContainer(controller->root_window(),
301 internal::kShellWindowId_SystemModalContainer)->layout_manager(),
302 controller->GetSystemModalLayoutManager(NULL));
303
304 views::Widget* session_modal_widget =
305 CreateModalWidget(gfx::Rect(300, 10, 100, 100));
306 EXPECT_EQ(Shell::GetContainer(controller->root_window(),
307 internal::kShellWindowId_SystemModalContainer)->layout_manager(),
308 controller->GetSystemModalLayoutManager(
309 session_modal_widget->GetNativeView()));
310}
311
[email protected]f1853122012-06-27 16:21:26312} // namespace test
313} // namespace ash