blob: 37bd96af9bade29519eeb5030f9816b42e80aa77 [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());
122#if !defined(OS_WIN)
123 // TODO(oshima): Window reports smaller screen size. Investigate why.
[email protected]e2f64d102012-07-19 19:17:04124 EXPECT_EQ("600,0 500x500", maximized->GetWindowBoundsInScreen().ToString());
[email protected]8d625fb2012-07-18 16:40:06125 EXPECT_EQ("0,0 500x500",
[email protected]e2f64d102012-07-19 19:17:04126 maximized->GetNativeView()->GetBoundsInRootWindow().ToString());
[email protected]f1853122012-06-27 16:21:26127#endif
128
129 views::Widget* minimized = CreateTestWidget(gfx::Rect(800, 10, 100, 100));
130 minimized->Minimize();
131 EXPECT_EQ(root_windows[1], minimized->GetNativeView()->GetRootWindow());
[email protected]8d625fb2012-07-18 16:40:06132 EXPECT_EQ("800,10 100x100",
[email protected]e2f64d102012-07-19 19:17:04133 minimized->GetWindowBoundsInScreen().ToString());
[email protected]f1853122012-06-27 16:21:26134
135 views::Widget* fullscreen = CreateTestWidget(gfx::Rect(900, 10, 100, 100));
136 fullscreen->SetFullscreen(true);
137 EXPECT_EQ(root_windows[1], fullscreen->GetNativeView()->GetRootWindow());
138#if !defined(OS_WIN)
139 // TODO(oshima): Window reports smaller screen size. Investigate why.
[email protected]8d625fb2012-07-18 16:40:06140 EXPECT_EQ("600,0 500x500",
[email protected]e2f64d102012-07-19 19:17:04141 fullscreen->GetWindowBoundsInScreen().ToString());
[email protected]8d625fb2012-07-18 16:40:06142 EXPECT_EQ("0,0 500x500",
[email protected]e2f64d102012-07-19 19:17:04143 fullscreen->GetNativeView()->GetBoundsInRootWindow().ToString());
[email protected]f1853122012-06-27 16:21:26144#endif
145
[email protected]dbf835d82012-09-11 18:23:09146 // Make sure a window that will delete itself when losing focus
147 // will not crash.
148 aura::WindowTracker tracker;
149 DeleteOnBlurDelegate delete_on_blur_delegate;
150 aura::Window* d2 = aura::test::CreateTestWindowWithDelegate(
151 &delete_on_blur_delegate, 0, gfx::Rect(50, 50, 100, 100), NULL);
152 delete_on_blur_delegate.set_window(d2);
[email protected]8cfb6722012-11-28 03:28:46153 aura::client::GetFocusClient(root_windows[0])->FocusWindow(d2, NULL);
[email protected]dbf835d82012-09-11 18:23:09154 tracker.Add(d2);
155
[email protected]f634dd32012-07-23 22:49:07156 UpdateDisplay("600x600");
[email protected]f1853122012-06-27 16:21:26157
[email protected]dbf835d82012-09-11 18:23:09158 // d2 must have been deleted.
159 EXPECT_FALSE(tracker.Contains(d2));
160
[email protected]f1853122012-06-27 16:21:26161 EXPECT_EQ(root_windows[0], normal->GetNativeView()->GetRootWindow());
[email protected]e2f64d102012-07-19 19:17:04162 EXPECT_EQ("50,10 100x100", normal->GetWindowBoundsInScreen().ToString());
[email protected]8d625fb2012-07-18 16:40:06163 EXPECT_EQ("50,10 100x100",
[email protected]e2f64d102012-07-19 19:17:04164 normal->GetNativeView()->GetBoundsInRootWindow().ToString());
[email protected]f1853122012-06-27 16:21:26165
[email protected]eefd51b22012-09-25 20:26:24166 // Maximized area on primary display has 3px (given as
[email protected]f1853122012-06-27 16:21:26167 // kAutoHideSize in shelf_layout_manager.cc) inset at the bottom.
168 EXPECT_EQ(root_windows[0], maximized->GetNativeView()->GetRootWindow());
[email protected]7b675df612012-09-16 18:33:20169 EXPECT_EQ("0,0 600x597",
[email protected]e2f64d102012-07-19 19:17:04170 maximized->GetWindowBoundsInScreen().ToString());
[email protected]7b675df612012-09-16 18:33:20171 EXPECT_EQ("0,0 600x597",
[email protected]e2f64d102012-07-19 19:17:04172 maximized->GetNativeView()->GetBoundsInRootWindow().ToString());
[email protected]f1853122012-06-27 16:21:26173
174 EXPECT_EQ(root_windows[0], minimized->GetNativeView()->GetRootWindow());
[email protected]8d625fb2012-07-18 16:40:06175 EXPECT_EQ("200,10 100x100",
[email protected]e2f64d102012-07-19 19:17:04176 minimized->GetWindowBoundsInScreen().ToString());
[email protected]f1853122012-06-27 16:21:26177
178 EXPECT_EQ(root_windows[0], fullscreen->GetNativeView()->GetRootWindow());
179 EXPECT_TRUE(fullscreen->IsFullscreen());
[email protected]8d625fb2012-07-18 16:40:06180 EXPECT_EQ("0,0 600x600",
[email protected]e2f64d102012-07-19 19:17:04181 fullscreen->GetWindowBoundsInScreen().ToString());
[email protected]8d625fb2012-07-18 16:40:06182 EXPECT_EQ("0,0 600x600",
[email protected]e2f64d102012-07-19 19:17:04183 fullscreen->GetNativeView()->GetBoundsInRootWindow().ToString());
[email protected]8d625fb2012-07-18 16:40:06184
185 // Test if the restore bounds are correctly updated.
186 wm::RestoreWindow(maximized->GetNativeView());
[email protected]e2f64d102012-07-19 19:17:04187 EXPECT_EQ("100,10 100x100", maximized->GetWindowBoundsInScreen().ToString());
[email protected]8d625fb2012-07-18 16:40:06188 EXPECT_EQ("100,10 100x100",
[email protected]e2f64d102012-07-19 19:17:04189 maximized->GetNativeView()->GetBoundsInRootWindow().ToString());
[email protected]8d625fb2012-07-18 16:40:06190
191 fullscreen->SetFullscreen(false);
192 EXPECT_EQ("300,10 100x100",
[email protected]e2f64d102012-07-19 19:17:04193 fullscreen->GetWindowBoundsInScreen().ToString());
[email protected]8d625fb2012-07-18 16:40:06194 EXPECT_EQ("300,10 100x100",
[email protected]e2f64d102012-07-19 19:17:04195 fullscreen->GetNativeView()->GetBoundsInRootWindow().ToString());
[email protected]f1853122012-06-27 16:21:26196}
197
198TEST_F(RootWindowControllerTest, MoveWindows_Modal) {
[email protected]f634dd32012-07-23 22:49:07199 UpdateDisplay("500x500,500x500");
[email protected]f1853122012-06-27 16:21:26200
201 Shell::RootWindowList root_windows = Shell::GetAllRootWindows();
202 // Emulate virtual screen coordinate system.
203 root_windows[0]->SetBounds(gfx::Rect(0, 0, 500, 500));
204 root_windows[1]->SetBounds(gfx::Rect(500, 0, 500, 500));
205
206 views::Widget* normal = CreateTestWidget(gfx::Rect(300, 10, 100, 100));
207 EXPECT_EQ(root_windows[0], normal->GetNativeView()->GetRootWindow());
208 EXPECT_TRUE(wm::IsActiveWindow(normal->GetNativeView()));
209
210 views::Widget* modal = CreateModalWidget(gfx::Rect(650, 10, 100, 100));
211 EXPECT_EQ(root_windows[1], modal->GetNativeView()->GetRootWindow());
212 EXPECT_TRUE(GetModalContainer(root_windows[1])->Contains(
213 modal->GetNativeView()));
214 EXPECT_TRUE(wm::IsActiveWindow(modal->GetNativeView()));
215
216 aura::test::EventGenerator generator_1st(root_windows[0]);
217 generator_1st.ClickLeftButton();
218 EXPECT_TRUE(wm::IsActiveWindow(modal->GetNativeView()));
219
[email protected]f634dd32012-07-23 22:49:07220 UpdateDisplay("500x500");
[email protected]f1853122012-06-27 16:21:26221 EXPECT_EQ(root_windows[0], modal->GetNativeView()->GetRootWindow());
222 EXPECT_TRUE(wm::IsActiveWindow(modal->GetNativeView()));
223 generator_1st.ClickLeftButton();
224 EXPECT_TRUE(wm::IsActiveWindow(modal->GetNativeView()));
225}
226
[email protected]8674b312012-10-12 19:02:44227TEST_F(RootWindowControllerTest, ModalContainer) {
228 UpdateDisplay("600x600");
229 Shell* shell = Shell::GetInstance();
230 internal::RootWindowController* controller =
231 shell->GetPrimaryRootWindowController();
232 EXPECT_EQ(user::LOGGED_IN_USER,
233 shell->tray_delegate()->GetUserLoginStatus());
234 EXPECT_EQ(Shell::GetContainer(controller->root_window(),
235 internal::kShellWindowId_SystemModalContainer)->layout_manager(),
236 controller->GetSystemModalLayoutManager(NULL));
237
[email protected]3b162e12012-11-09 11:52:35238 views::Widget* session_modal_widget =
239 CreateModalWidget(gfx::Rect(300, 10, 100, 100));
240 EXPECT_EQ(Shell::GetContainer(controller->root_window(),
241 internal::kShellWindowId_SystemModalContainer)->layout_manager(),
242 controller->GetSystemModalLayoutManager(
243 session_modal_widget->GetNativeView()));
244
[email protected]8674b312012-10-12 19:02:44245 shell->delegate()->LockScreen();
246 EXPECT_EQ(user::LOGGED_IN_LOCKED,
247 shell->tray_delegate()->GetUserLoginStatus());
248 EXPECT_EQ(Shell::GetContainer(controller->root_window(),
249 internal::kShellWindowId_LockSystemModalContainer)->layout_manager(),
250 controller->GetSystemModalLayoutManager(NULL));
[email protected]3b162e12012-11-09 11:52:35251
252 aura::Window* lock_container =
253 Shell::GetContainer(controller->root_window(),
254 internal::kShellWindowId_LockScreenContainer);
255 views::Widget* lock_modal_widget =
256 CreateModalWidgetWithParent(gfx::Rect(300, 10, 100, 100), lock_container);
257 EXPECT_EQ(Shell::GetContainer(controller->root_window(),
258 internal::kShellWindowId_LockSystemModalContainer)->layout_manager(),
259 controller->GetSystemModalLayoutManager(
260 lock_modal_widget->GetNativeView()));
261 EXPECT_EQ(Shell::GetContainer(controller->root_window(),
262 internal::kShellWindowId_SystemModalContainer)->layout_manager(),
263 controller->GetSystemModalLayoutManager(
264 session_modal_widget->GetNativeView()));
265
[email protected]8674b312012-10-12 19:02:44266 shell->delegate()->UnlockScreen();
267}
268
[email protected]1b219922012-11-13 21:16:43269TEST_F(RootWindowControllerTest, ModalContainerNotLoggedInLoggedIn) {
270 UpdateDisplay("600x600");
271 Shell* shell = Shell::GetInstance();
272
273 // Configure login screen environment.
274 SetUserLoggedIn(false);
275 EXPECT_EQ(user::LOGGED_IN_NONE,
276 shell->tray_delegate()->GetUserLoginStatus());
277 EXPECT_FALSE(shell->delegate()->IsUserLoggedIn());
278 EXPECT_FALSE(shell->delegate()->IsSessionStarted());
279
280 internal::RootWindowController* controller =
281 shell->GetPrimaryRootWindowController();
282 EXPECT_EQ(Shell::GetContainer(controller->root_window(),
283 internal::kShellWindowId_LockSystemModalContainer)->layout_manager(),
284 controller->GetSystemModalLayoutManager(NULL));
285
286 aura::Window* lock_container =
287 Shell::GetContainer(controller->root_window(),
288 internal::kShellWindowId_LockScreenContainer);
289 views::Widget* login_modal_widget =
290 CreateModalWidgetWithParent(gfx::Rect(300, 10, 100, 100), lock_container);
291 EXPECT_EQ(Shell::GetContainer(controller->root_window(),
292 internal::kShellWindowId_LockSystemModalContainer)->layout_manager(),
293 controller->GetSystemModalLayoutManager(
294 login_modal_widget->GetNativeView()));
295 login_modal_widget->Close();
296
297 // Configure user session environment.
298 SetUserLoggedIn(true);
299 SetSessionStarted(true);
300 EXPECT_EQ(user::LOGGED_IN_USER,
301 shell->tray_delegate()->GetUserLoginStatus());
302 EXPECT_TRUE(shell->delegate()->IsUserLoggedIn());
303 EXPECT_TRUE(shell->delegate()->IsSessionStarted());
304 EXPECT_EQ(Shell::GetContainer(controller->root_window(),
305 internal::kShellWindowId_SystemModalContainer)->layout_manager(),
306 controller->GetSystemModalLayoutManager(NULL));
307
308 views::Widget* session_modal_widget =
309 CreateModalWidget(gfx::Rect(300, 10, 100, 100));
310 EXPECT_EQ(Shell::GetContainer(controller->root_window(),
311 internal::kShellWindowId_SystemModalContainer)->layout_manager(),
312 controller->GetSystemModalLayoutManager(
313 session_modal_widget->GetNativeView()));
314}
315
[email protected]f1853122012-06-27 16:21:26316} // namespace test
317} // namespace ash