blob: 24ae5de87e9683afa73ed12b4b1a87e0e3111b36 [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]792b9b12012-12-11 03:53:2715#include "ui/aura/client/focus_change_observer.h"
[email protected]8cfb6722012-11-28 03:28:4616#include "ui/aura/client/focus_client.h"
[email protected]f1853122012-06-27 16:21:2617#include "ui/aura/env.h"
18#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"
[email protected]869f6352012-12-06 20:47:1725#include "ui/views/corewm/focus_change_event.h"
[email protected]f1853122012-06-27 16:21:2626#include "ui/views/widget/widget.h"
27#include "ui/views/widget/widget_delegate.h"
28
29namespace ash {
30namespace {
31
32class TestDelegate : public views::WidgetDelegateView {
33 public:
34 explicit TestDelegate(bool system_modal) : system_modal_(system_modal) {}
35 virtual ~TestDelegate() {}
36
37 // Overridden from views::WidgetDelegate:
38 virtual views::View* GetContentsView() OVERRIDE {
39 return this;
40 }
41
42 virtual ui::ModalType GetModalType() const OVERRIDE {
43 return system_modal_ ? ui::MODAL_TYPE_SYSTEM : ui::MODAL_TYPE_NONE;
44 }
45
46 private:
47 bool system_modal_;
48 DISALLOW_COPY_AND_ASSIGN(TestDelegate);
49};
50
[email protected]792b9b12012-12-11 03:53:2751class DeleteOnBlurDelegate : public aura::test::TestWindowDelegate,
52 public aura::client::FocusChangeObserver {
[email protected]dbf835d82012-09-11 18:23:0953 public:
54 DeleteOnBlurDelegate() : window_(NULL) {}
55 virtual ~DeleteOnBlurDelegate() {}
56
[email protected]792b9b12012-12-11 03:53:2757 void SetWindow(aura::Window* window) {
58 window_ = window;
59 aura::client::SetFocusChangeObserver(window_, this);
60 }
[email protected]dbf835d82012-09-11 18:23:0961
[email protected]869f6352012-12-06 20:47:1762 private:
[email protected]dbf835d82012-09-11 18:23:0963 // aura::test::TestWindowDelegate overrides:
64 virtual bool CanFocus() OVERRIDE {
65 return true;
66 }
[email protected]dbf835d82012-09-11 18:23:0967
[email protected]792b9b12012-12-11 03:53:2768 // aura::client::FocusChangeObserver implementation:
69 virtual void OnWindowFocused(aura::Window* gained_focus,
70 aura::Window* lost_focus) OVERRIDE {
71 if (window_ == lost_focus)
72 delete window_;
[email protected]869f6352012-12-06 20:47:1773 }
74
[email protected]dbf835d82012-09-11 18:23:0975 aura::Window* window_;
76
77 DISALLOW_COPY_AND_ASSIGN(DeleteOnBlurDelegate);
78};
79
[email protected]f1853122012-06-27 16:21:2680views::Widget* CreateTestWidget(const gfx::Rect& bounds) {
81 views::Widget* widget =
82 views::Widget::CreateWindowWithBounds(NULL, bounds);
83 widget->Show();
84 return widget;
85}
86
87views::Widget* CreateModalWidget(const gfx::Rect& bounds) {
88 views::Widget* widget =
89 views::Widget::CreateWindowWithBounds(new TestDelegate(true), bounds);
90 widget->Show();
91 return widget;
92}
93
[email protected]3b162e12012-11-09 11:52:3594views::Widget* CreateModalWidgetWithParent(const gfx::Rect& bounds,
95 gfx::NativeWindow parent) {
96 views::Widget* widget =
97 views::Widget::CreateWindowWithParentAndBounds(new TestDelegate(true),
98 parent,
99 bounds);
100 widget->Show();
101 return widget;
102}
103
[email protected]f1853122012-06-27 16:21:26104aura::Window* GetModalContainer(aura::RootWindow* root_window) {
105 return Shell::GetContainer(
106 root_window,
107 ash::internal::kShellWindowId_SystemModalContainer);
108}
109
110} // namespace
111
112namespace test {
[email protected]f1853122012-06-27 16:21:26113
[email protected]3e4351b2012-08-09 04:04:16114typedef test::AshTestBase RootWindowControllerTest;
[email protected]f1853122012-06-27 16:21:26115
116TEST_F(RootWindowControllerTest, MoveWindows_Basic) {
[email protected]f634dd32012-07-23 22:49:07117 UpdateDisplay("600x600,500x500");
[email protected]f1853122012-06-27 16:21:26118 Shell::RootWindowList root_windows = Shell::GetAllRootWindows();
[email protected]431552c2012-10-23 00:38:33119 internal::RootWindowController* controller =
120 Shell::GetPrimaryRootWindowController();
121 controller->SetShelfAutoHideBehavior(ash::SHELF_AUTO_HIDE_BEHAVIOR_ALWAYS);
[email protected]f1853122012-06-27 16:21:26122
123 views::Widget* normal = CreateTestWidget(gfx::Rect(650, 10, 100, 100));
124 EXPECT_EQ(root_windows[1], normal->GetNativeView()->GetRootWindow());
[email protected]e2f64d102012-07-19 19:17:04125 EXPECT_EQ("650,10 100x100", normal->GetWindowBoundsInScreen().ToString());
[email protected]8d625fb2012-07-18 16:40:06126 EXPECT_EQ("50,10 100x100",
[email protected]e2f64d102012-07-19 19:17:04127 normal->GetNativeView()->GetBoundsInRootWindow().ToString());
[email protected]f1853122012-06-27 16:21:26128
129 views::Widget* maximized = CreateTestWidget(gfx::Rect(700, 10, 100, 100));
130 maximized->Maximize();
131 EXPECT_EQ(root_windows[1], maximized->GetNativeView()->GetRootWindow());
[email protected]1a015382012-12-01 19:44:59132
[email protected]e2f64d102012-07-19 19:17:04133 EXPECT_EQ("600,0 500x500", maximized->GetWindowBoundsInScreen().ToString());
[email protected]8d625fb2012-07-18 16:40:06134 EXPECT_EQ("0,0 500x500",
[email protected]e2f64d102012-07-19 19:17:04135 maximized->GetNativeView()->GetBoundsInRootWindow().ToString());
[email protected]f1853122012-06-27 16:21:26136
137 views::Widget* minimized = CreateTestWidget(gfx::Rect(800, 10, 100, 100));
138 minimized->Minimize();
139 EXPECT_EQ(root_windows[1], minimized->GetNativeView()->GetRootWindow());
[email protected]8d625fb2012-07-18 16:40:06140 EXPECT_EQ("800,10 100x100",
[email protected]e2f64d102012-07-19 19:17:04141 minimized->GetWindowBoundsInScreen().ToString());
[email protected]f1853122012-06-27 16:21:26142
143 views::Widget* fullscreen = CreateTestWidget(gfx::Rect(900, 10, 100, 100));
144 fullscreen->SetFullscreen(true);
145 EXPECT_EQ(root_windows[1], fullscreen->GetNativeView()->GetRootWindow());
[email protected]1a015382012-12-01 19:44:59146
[email protected]8d625fb2012-07-18 16:40:06147 EXPECT_EQ("600,0 500x500",
[email protected]e2f64d102012-07-19 19:17:04148 fullscreen->GetWindowBoundsInScreen().ToString());
[email protected]8d625fb2012-07-18 16:40:06149 EXPECT_EQ("0,0 500x500",
[email protected]e2f64d102012-07-19 19:17:04150 fullscreen->GetNativeView()->GetBoundsInRootWindow().ToString());
[email protected]f1853122012-06-27 16:21:26151
[email protected]dbf835d82012-09-11 18:23:09152 // Make sure a window that will delete itself when losing focus
153 // will not crash.
154 aura::WindowTracker tracker;
155 DeleteOnBlurDelegate delete_on_blur_delegate;
[email protected]5ebe6102012-11-28 21:00:03156 aura::Window* d2 = CreateTestWindowInShellWithDelegate(
157 &delete_on_blur_delegate, 0, gfx::Rect(50, 50, 100, 100));
[email protected]792b9b12012-12-11 03:53:27158 delete_on_blur_delegate.SetWindow(d2);
[email protected]8cfb6722012-11-28 03:28:46159 aura::client::GetFocusClient(root_windows[0])->FocusWindow(d2, NULL);
[email protected]dbf835d82012-09-11 18:23:09160 tracker.Add(d2);
161
[email protected]f634dd32012-07-23 22:49:07162 UpdateDisplay("600x600");
[email protected]f1853122012-06-27 16:21:26163
[email protected]dbf835d82012-09-11 18:23:09164 // d2 must have been deleted.
165 EXPECT_FALSE(tracker.Contains(d2));
166
[email protected]f1853122012-06-27 16:21:26167 EXPECT_EQ(root_windows[0], normal->GetNativeView()->GetRootWindow());
[email protected]e2f64d102012-07-19 19:17:04168 EXPECT_EQ("50,10 100x100", normal->GetWindowBoundsInScreen().ToString());
[email protected]8d625fb2012-07-18 16:40:06169 EXPECT_EQ("50,10 100x100",
[email protected]e2f64d102012-07-19 19:17:04170 normal->GetNativeView()->GetBoundsInRootWindow().ToString());
[email protected]f1853122012-06-27 16:21:26171
[email protected]eefd51b22012-09-25 20:26:24172 // Maximized area on primary display has 3px (given as
[email protected]f1853122012-06-27 16:21:26173 // kAutoHideSize in shelf_layout_manager.cc) inset at the bottom.
174 EXPECT_EQ(root_windows[0], maximized->GetNativeView()->GetRootWindow());
[email protected]7b675df612012-09-16 18:33:20175 EXPECT_EQ("0,0 600x597",
[email protected]e2f64d102012-07-19 19:17:04176 maximized->GetWindowBoundsInScreen().ToString());
[email protected]7b675df612012-09-16 18:33:20177 EXPECT_EQ("0,0 600x597",
[email protected]e2f64d102012-07-19 19:17:04178 maximized->GetNativeView()->GetBoundsInRootWindow().ToString());
[email protected]f1853122012-06-27 16:21:26179
180 EXPECT_EQ(root_windows[0], minimized->GetNativeView()->GetRootWindow());
[email protected]8d625fb2012-07-18 16:40:06181 EXPECT_EQ("200,10 100x100",
[email protected]e2f64d102012-07-19 19:17:04182 minimized->GetWindowBoundsInScreen().ToString());
[email protected]f1853122012-06-27 16:21:26183
184 EXPECT_EQ(root_windows[0], fullscreen->GetNativeView()->GetRootWindow());
185 EXPECT_TRUE(fullscreen->IsFullscreen());
[email protected]8d625fb2012-07-18 16:40:06186 EXPECT_EQ("0,0 600x600",
[email protected]e2f64d102012-07-19 19:17:04187 fullscreen->GetWindowBoundsInScreen().ToString());
[email protected]8d625fb2012-07-18 16:40:06188 EXPECT_EQ("0,0 600x600",
[email protected]e2f64d102012-07-19 19:17:04189 fullscreen->GetNativeView()->GetBoundsInRootWindow().ToString());
[email protected]8d625fb2012-07-18 16:40:06190
191 // Test if the restore bounds are correctly updated.
192 wm::RestoreWindow(maximized->GetNativeView());
[email protected]e2f64d102012-07-19 19:17:04193 EXPECT_EQ("100,10 100x100", maximized->GetWindowBoundsInScreen().ToString());
[email protected]8d625fb2012-07-18 16:40:06194 EXPECT_EQ("100,10 100x100",
[email protected]e2f64d102012-07-19 19:17:04195 maximized->GetNativeView()->GetBoundsInRootWindow().ToString());
[email protected]8d625fb2012-07-18 16:40:06196
197 fullscreen->SetFullscreen(false);
198 EXPECT_EQ("300,10 100x100",
[email protected]e2f64d102012-07-19 19:17:04199 fullscreen->GetWindowBoundsInScreen().ToString());
[email protected]8d625fb2012-07-18 16:40:06200 EXPECT_EQ("300,10 100x100",
[email protected]e2f64d102012-07-19 19:17:04201 fullscreen->GetNativeView()->GetBoundsInRootWindow().ToString());
[email protected]f1853122012-06-27 16:21:26202}
203
204TEST_F(RootWindowControllerTest, MoveWindows_Modal) {
[email protected]f634dd32012-07-23 22:49:07205 UpdateDisplay("500x500,500x500");
[email protected]f1853122012-06-27 16:21:26206
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));
211
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()));
215
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()));
221
222 aura::test::EventGenerator generator_1st(root_windows[0]);
223 generator_1st.ClickLeftButton();
224 EXPECT_TRUE(wm::IsActiveWindow(modal->GetNativeView()));
225
[email protected]f634dd32012-07-23 22:49:07226 UpdateDisplay("500x500");
[email protected]f1853122012-06-27 16:21:26227 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()));
231}
232
[email protected]8674b312012-10-12 19:02:44233TEST_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));
243
[email protected]3b162e12012-11-09 11:52:35244 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()));
250
[email protected]8674b312012-10-12 19:02:44251 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));
[email protected]3b162e12012-11-09 11:52:35257
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()));
271
[email protected]8674b312012-10-12 19:02:44272 shell->delegate()->UnlockScreen();
273}
274
[email protected]1b219922012-11-13 21:16:43275TEST_F(RootWindowControllerTest, ModalContainerNotLoggedInLoggedIn) {
276 UpdateDisplay("600x600");
277 Shell* shell = Shell::GetInstance();
278
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());
285
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));
291
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();
302
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));
313
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()));
320}
321
[email protected]f1853122012-06-27 16:21:26322} // namespace test
323} // namespace ash