blob: 09af6d380181d13d05ff31fbfc51b5f13b2aa391 [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]2ee2f5d2013-01-10 23:37:1614#include "ash/wm/window_properties.h"
[email protected]f1853122012-06-27 16:21:2615#include "ash/wm/window_util.h"
[email protected]792b9b12012-12-11 03:53:2716#include "ui/aura/client/focus_change_observer.h"
[email protected]8cfb6722012-11-28 03:28:4617#include "ui/aura/client/focus_client.h"
[email protected]f1853122012-06-27 16:21:2618#include "ui/aura/env.h"
19#include "ui/aura/root_window.h"
20#include "ui/aura/test/event_generator.h"
[email protected]dbf835d82012-09-11 18:23:0921#include "ui/aura/test/test_window_delegate.h"
22#include "ui/aura/test/test_windows.h"
[email protected]f1853122012-06-27 16:21:2623#include "ui/aura/window.h"
[email protected]dbf835d82012-09-11 18:23:0924#include "ui/aura/window_tracker.h"
[email protected]f1853122012-06-27 16:21:2625#include "ui/views/controls/menu/menu_controller.h"
26#include "ui/views/widget/widget.h"
27#include "ui/views/widget/widget_delegate.h"
28
[email protected]2ee2f5d2013-01-10 23:37:1629using aura::Window;
30using views::Widget;
31
[email protected]f1853122012-06-27 16:21:2632namespace ash {
33namespace {
34
35class TestDelegate : public views::WidgetDelegateView {
36 public:
37 explicit TestDelegate(bool system_modal) : system_modal_(system_modal) {}
38 virtual ~TestDelegate() {}
39
40 // Overridden from views::WidgetDelegate:
41 virtual views::View* GetContentsView() OVERRIDE {
42 return this;
43 }
44
45 virtual ui::ModalType GetModalType() const OVERRIDE {
46 return system_modal_ ? ui::MODAL_TYPE_SYSTEM : ui::MODAL_TYPE_NONE;
47 }
48
49 private:
50 bool system_modal_;
51 DISALLOW_COPY_AND_ASSIGN(TestDelegate);
52};
53
[email protected]792b9b12012-12-11 03:53:2754class DeleteOnBlurDelegate : public aura::test::TestWindowDelegate,
55 public aura::client::FocusChangeObserver {
[email protected]dbf835d82012-09-11 18:23:0956 public:
57 DeleteOnBlurDelegate() : window_(NULL) {}
58 virtual ~DeleteOnBlurDelegate() {}
59
[email protected]792b9b12012-12-11 03:53:2760 void SetWindow(aura::Window* window) {
61 window_ = window;
62 aura::client::SetFocusChangeObserver(window_, this);
63 }
[email protected]dbf835d82012-09-11 18:23:0964
[email protected]869f6352012-12-06 20:47:1765 private:
[email protected]dbf835d82012-09-11 18:23:0966 // aura::test::TestWindowDelegate overrides:
67 virtual bool CanFocus() OVERRIDE {
68 return true;
69 }
[email protected]dbf835d82012-09-11 18:23:0970
[email protected]792b9b12012-12-11 03:53:2771 // aura::client::FocusChangeObserver implementation:
72 virtual void OnWindowFocused(aura::Window* gained_focus,
73 aura::Window* lost_focus) OVERRIDE {
74 if (window_ == lost_focus)
75 delete window_;
[email protected]869f6352012-12-06 20:47:1776 }
77
[email protected]dbf835d82012-09-11 18:23:0978 aura::Window* window_;
79
80 DISALLOW_COPY_AND_ASSIGN(DeleteOnBlurDelegate);
81};
82
[email protected]f1853122012-06-27 16:21:2683} // namespace
84
85namespace test {
[email protected]f1853122012-06-27 16:21:2686
[email protected]a2e6af12013-01-07 21:40:3587class RootWindowControllerTest : public test::AshTestBase {
88 public:
89 views::Widget* CreateTestWidget(const gfx::Rect& bounds) {
90 views::Widget* widget = views::Widget::CreateWindowWithContextAndBounds(
91 NULL, CurrentContext(), bounds);
92 widget->Show();
93 return widget;
94 }
95
96 views::Widget* CreateModalWidget(const gfx::Rect& bounds) {
97 views::Widget* widget = views::Widget::CreateWindowWithContextAndBounds(
98 new TestDelegate(true), CurrentContext(), bounds);
99 widget->Show();
100 return widget;
101 }
102
103 views::Widget* CreateModalWidgetWithParent(const gfx::Rect& bounds,
104 gfx::NativeWindow parent) {
105 views::Widget* widget =
106 views::Widget::CreateWindowWithParentAndBounds(new TestDelegate(true),
107 parent,
108 bounds);
109 widget->Show();
110 return widget;
111 }
112
113 aura::Window* GetModalContainer(aura::RootWindow* root_window) {
114 return Shell::GetContainer(
115 root_window,
116 ash::internal::kShellWindowId_SystemModalContainer);
117 }
118};
[email protected]f1853122012-06-27 16:21:26119
[email protected]1c3f7002013-01-21 18:46:05120#if defined(OS_WIN)
121// Multiple displays are not supported on Windows Ash. http://crbug.com/165962
122#define MAYBE_MoveWindows_Basic DISABLED_MoveWindows_Basic
123#else
124#define MAYBE_MoveWindows_Basic MoveWindows_Basic
125#endif
126
127TEST_F(RootWindowControllerTest, MAYBE_MoveWindows_Basic) {
[email protected]f634dd32012-07-23 22:49:07128 UpdateDisplay("600x600,500x500");
[email protected]f1853122012-06-27 16:21:26129 Shell::RootWindowList root_windows = Shell::GetAllRootWindows();
[email protected]431552c2012-10-23 00:38:33130 internal::RootWindowController* controller =
131 Shell::GetPrimaryRootWindowController();
132 controller->SetShelfAutoHideBehavior(ash::SHELF_AUTO_HIDE_BEHAVIOR_ALWAYS);
[email protected]f1853122012-06-27 16:21:26133
134 views::Widget* normal = CreateTestWidget(gfx::Rect(650, 10, 100, 100));
135 EXPECT_EQ(root_windows[1], normal->GetNativeView()->GetRootWindow());
[email protected]e2f64d102012-07-19 19:17:04136 EXPECT_EQ("650,10 100x100", normal->GetWindowBoundsInScreen().ToString());
[email protected]8d625fb2012-07-18 16:40:06137 EXPECT_EQ("50,10 100x100",
[email protected]e2f64d102012-07-19 19:17:04138 normal->GetNativeView()->GetBoundsInRootWindow().ToString());
[email protected]f1853122012-06-27 16:21:26139
140 views::Widget* maximized = CreateTestWidget(gfx::Rect(700, 10, 100, 100));
141 maximized->Maximize();
142 EXPECT_EQ(root_windows[1], maximized->GetNativeView()->GetRootWindow());
[email protected]8039e06c2013-01-17 23:34:50143 if (Shell::IsLauncherPerDisplayEnabled()) {
144 EXPECT_EQ("600,0 500x452", maximized->GetWindowBoundsInScreen().ToString());
145 EXPECT_EQ("0,0 500x452",
146 maximized->GetNativeView()->GetBoundsInRootWindow().ToString());
147 } else {
148 EXPECT_EQ("600,0 500x500", maximized->GetWindowBoundsInScreen().ToString());
149 EXPECT_EQ("0,0 500x500",
150 maximized->GetNativeView()->GetBoundsInRootWindow().ToString());
151 }
[email protected]f1853122012-06-27 16:21:26152
153 views::Widget* minimized = CreateTestWidget(gfx::Rect(800, 10, 100, 100));
154 minimized->Minimize();
155 EXPECT_EQ(root_windows[1], minimized->GetNativeView()->GetRootWindow());
[email protected]8d625fb2012-07-18 16:40:06156 EXPECT_EQ("800,10 100x100",
[email protected]e2f64d102012-07-19 19:17:04157 minimized->GetWindowBoundsInScreen().ToString());
[email protected]f1853122012-06-27 16:21:26158
159 views::Widget* fullscreen = CreateTestWidget(gfx::Rect(900, 10, 100, 100));
160 fullscreen->SetFullscreen(true);
161 EXPECT_EQ(root_windows[1], fullscreen->GetNativeView()->GetRootWindow());
[email protected]1a015382012-12-01 19:44:59162
[email protected]8d625fb2012-07-18 16:40:06163 EXPECT_EQ("600,0 500x500",
[email protected]e2f64d102012-07-19 19:17:04164 fullscreen->GetWindowBoundsInScreen().ToString());
[email protected]8d625fb2012-07-18 16:40:06165 EXPECT_EQ("0,0 500x500",
[email protected]e2f64d102012-07-19 19:17:04166 fullscreen->GetNativeView()->GetBoundsInRootWindow().ToString());
[email protected]f1853122012-06-27 16:21:26167
[email protected]8f2f151412013-01-26 03:58:37168 views::Widget* unparented_control = new Widget;
169 Widget::InitParams params;
170 params.bounds = gfx::Rect(650, 10, 100, 100);
171 params.context = CurrentContext();
172 params.type = Widget::InitParams::TYPE_CONTROL;
173 unparented_control->Init(params);
174 EXPECT_EQ(root_windows[1],
175 unparented_control->GetNativeView()->GetRootWindow());
176 EXPECT_EQ(internal::kShellWindowId_UnparentedControlContainer,
177 unparented_control->GetNativeView()->parent()->id());
178
[email protected]dbf835d82012-09-11 18:23:09179 // Make sure a window that will delete itself when losing focus
180 // will not crash.
181 aura::WindowTracker tracker;
182 DeleteOnBlurDelegate delete_on_blur_delegate;
[email protected]5ebe6102012-11-28 21:00:03183 aura::Window* d2 = CreateTestWindowInShellWithDelegate(
184 &delete_on_blur_delegate, 0, gfx::Rect(50, 50, 100, 100));
[email protected]792b9b12012-12-11 03:53:27185 delete_on_blur_delegate.SetWindow(d2);
[email protected]550543e2013-01-11 22:43:44186 aura::client::GetFocusClient(root_windows[0])->FocusWindow(d2);
[email protected]dbf835d82012-09-11 18:23:09187 tracker.Add(d2);
188
[email protected]f634dd32012-07-23 22:49:07189 UpdateDisplay("600x600");
[email protected]f1853122012-06-27 16:21:26190
[email protected]dbf835d82012-09-11 18:23:09191 // d2 must have been deleted.
192 EXPECT_FALSE(tracker.Contains(d2));
193
[email protected]f1853122012-06-27 16:21:26194 EXPECT_EQ(root_windows[0], normal->GetNativeView()->GetRootWindow());
[email protected]e2f64d102012-07-19 19:17:04195 EXPECT_EQ("50,10 100x100", normal->GetWindowBoundsInScreen().ToString());
[email protected]8d625fb2012-07-18 16:40:06196 EXPECT_EQ("50,10 100x100",
[email protected]e2f64d102012-07-19 19:17:04197 normal->GetNativeView()->GetBoundsInRootWindow().ToString());
[email protected]f1853122012-06-27 16:21:26198
[email protected]eefd51b22012-09-25 20:26:24199 // Maximized area on primary display has 3px (given as
[email protected]f1853122012-06-27 16:21:26200 // kAutoHideSize in shelf_layout_manager.cc) inset at the bottom.
201 EXPECT_EQ(root_windows[0], maximized->GetNativeView()->GetRootWindow());
[email protected]7b675df612012-09-16 18:33:20202 EXPECT_EQ("0,0 600x597",
[email protected]e2f64d102012-07-19 19:17:04203 maximized->GetWindowBoundsInScreen().ToString());
[email protected]7b675df612012-09-16 18:33:20204 EXPECT_EQ("0,0 600x597",
[email protected]e2f64d102012-07-19 19:17:04205 maximized->GetNativeView()->GetBoundsInRootWindow().ToString());
[email protected]f1853122012-06-27 16:21:26206
207 EXPECT_EQ(root_windows[0], minimized->GetNativeView()->GetRootWindow());
[email protected]8d625fb2012-07-18 16:40:06208 EXPECT_EQ("200,10 100x100",
[email protected]e2f64d102012-07-19 19:17:04209 minimized->GetWindowBoundsInScreen().ToString());
[email protected]f1853122012-06-27 16:21:26210
211 EXPECT_EQ(root_windows[0], fullscreen->GetNativeView()->GetRootWindow());
212 EXPECT_TRUE(fullscreen->IsFullscreen());
[email protected]8d625fb2012-07-18 16:40:06213 EXPECT_EQ("0,0 600x600",
[email protected]e2f64d102012-07-19 19:17:04214 fullscreen->GetWindowBoundsInScreen().ToString());
[email protected]8d625fb2012-07-18 16:40:06215 EXPECT_EQ("0,0 600x600",
[email protected]e2f64d102012-07-19 19:17:04216 fullscreen->GetNativeView()->GetBoundsInRootWindow().ToString());
[email protected]8d625fb2012-07-18 16:40:06217
218 // Test if the restore bounds are correctly updated.
219 wm::RestoreWindow(maximized->GetNativeView());
[email protected]e2f64d102012-07-19 19:17:04220 EXPECT_EQ("100,10 100x100", maximized->GetWindowBoundsInScreen().ToString());
[email protected]8d625fb2012-07-18 16:40:06221 EXPECT_EQ("100,10 100x100",
[email protected]e2f64d102012-07-19 19:17:04222 maximized->GetNativeView()->GetBoundsInRootWindow().ToString());
[email protected]8d625fb2012-07-18 16:40:06223
224 fullscreen->SetFullscreen(false);
225 EXPECT_EQ("300,10 100x100",
[email protected]e2f64d102012-07-19 19:17:04226 fullscreen->GetWindowBoundsInScreen().ToString());
[email protected]8d625fb2012-07-18 16:40:06227 EXPECT_EQ("300,10 100x100",
[email protected]e2f64d102012-07-19 19:17:04228 fullscreen->GetNativeView()->GetBoundsInRootWindow().ToString());
[email protected]8f2f151412013-01-26 03:58:37229
230 // Test if the unparented widget has moved.
231 EXPECT_EQ(root_windows[0],
232 unparented_control->GetNativeView()->GetRootWindow());
233 EXPECT_EQ(internal::kShellWindowId_UnparentedControlContainer,
234 unparented_control->GetNativeView()->parent()->id());
[email protected]f1853122012-06-27 16:21:26235}
236
[email protected]1c3f7002013-01-21 18:46:05237#if defined(OS_WIN)
238// Multiple displays are not supported on Windows Ash. http://crbug.com/165962
239#define MAYBE_MoveWindows_Modal DISABLED_MoveWindows_Modal
240#else
241#define MAYBE_MoveWindows_Modal MoveWindows_Modal
242#endif
243
244TEST_F(RootWindowControllerTest, MAYBE_MoveWindows_Modal) {
[email protected]f634dd32012-07-23 22:49:07245 UpdateDisplay("500x500,500x500");
[email protected]f1853122012-06-27 16:21:26246
247 Shell::RootWindowList root_windows = Shell::GetAllRootWindows();
248 // Emulate virtual screen coordinate system.
249 root_windows[0]->SetBounds(gfx::Rect(0, 0, 500, 500));
250 root_windows[1]->SetBounds(gfx::Rect(500, 0, 500, 500));
251
252 views::Widget* normal = CreateTestWidget(gfx::Rect(300, 10, 100, 100));
253 EXPECT_EQ(root_windows[0], normal->GetNativeView()->GetRootWindow());
254 EXPECT_TRUE(wm::IsActiveWindow(normal->GetNativeView()));
255
256 views::Widget* modal = CreateModalWidget(gfx::Rect(650, 10, 100, 100));
257 EXPECT_EQ(root_windows[1], modal->GetNativeView()->GetRootWindow());
258 EXPECT_TRUE(GetModalContainer(root_windows[1])->Contains(
259 modal->GetNativeView()));
260 EXPECT_TRUE(wm::IsActiveWindow(modal->GetNativeView()));
261
262 aura::test::EventGenerator generator_1st(root_windows[0]);
263 generator_1st.ClickLeftButton();
264 EXPECT_TRUE(wm::IsActiveWindow(modal->GetNativeView()));
265
[email protected]f634dd32012-07-23 22:49:07266 UpdateDisplay("500x500");
[email protected]f1853122012-06-27 16:21:26267 EXPECT_EQ(root_windows[0], modal->GetNativeView()->GetRootWindow());
268 EXPECT_TRUE(wm::IsActiveWindow(modal->GetNativeView()));
269 generator_1st.ClickLeftButton();
270 EXPECT_TRUE(wm::IsActiveWindow(modal->GetNativeView()));
271}
272
[email protected]8674b312012-10-12 19:02:44273TEST_F(RootWindowControllerTest, ModalContainer) {
274 UpdateDisplay("600x600");
275 Shell* shell = Shell::GetInstance();
276 internal::RootWindowController* controller =
277 shell->GetPrimaryRootWindowController();
278 EXPECT_EQ(user::LOGGED_IN_USER,
[email protected]945f9cae2012-12-12 09:54:29279 shell->system_tray_delegate()->GetUserLoginStatus());
[email protected]8674b312012-10-12 19:02:44280 EXPECT_EQ(Shell::GetContainer(controller->root_window(),
281 internal::kShellWindowId_SystemModalContainer)->layout_manager(),
282 controller->GetSystemModalLayoutManager(NULL));
283
[email protected]3b162e12012-11-09 11:52:35284 views::Widget* session_modal_widget =
285 CreateModalWidget(gfx::Rect(300, 10, 100, 100));
286 EXPECT_EQ(Shell::GetContainer(controller->root_window(),
287 internal::kShellWindowId_SystemModalContainer)->layout_manager(),
288 controller->GetSystemModalLayoutManager(
289 session_modal_widget->GetNativeView()));
290
[email protected]8674b312012-10-12 19:02:44291 shell->delegate()->LockScreen();
292 EXPECT_EQ(user::LOGGED_IN_LOCKED,
[email protected]945f9cae2012-12-12 09:54:29293 shell->system_tray_delegate()->GetUserLoginStatus());
[email protected]8674b312012-10-12 19:02:44294 EXPECT_EQ(Shell::GetContainer(controller->root_window(),
295 internal::kShellWindowId_LockSystemModalContainer)->layout_manager(),
296 controller->GetSystemModalLayoutManager(NULL));
[email protected]3b162e12012-11-09 11:52:35297
298 aura::Window* lock_container =
299 Shell::GetContainer(controller->root_window(),
300 internal::kShellWindowId_LockScreenContainer);
301 views::Widget* lock_modal_widget =
302 CreateModalWidgetWithParent(gfx::Rect(300, 10, 100, 100), lock_container);
303 EXPECT_EQ(Shell::GetContainer(controller->root_window(),
304 internal::kShellWindowId_LockSystemModalContainer)->layout_manager(),
305 controller->GetSystemModalLayoutManager(
306 lock_modal_widget->GetNativeView()));
307 EXPECT_EQ(Shell::GetContainer(controller->root_window(),
308 internal::kShellWindowId_SystemModalContainer)->layout_manager(),
309 controller->GetSystemModalLayoutManager(
310 session_modal_widget->GetNativeView()));
311
[email protected]8674b312012-10-12 19:02:44312 shell->delegate()->UnlockScreen();
313}
314
[email protected]1b219922012-11-13 21:16:43315TEST_F(RootWindowControllerTest, ModalContainerNotLoggedInLoggedIn) {
316 UpdateDisplay("600x600");
317 Shell* shell = Shell::GetInstance();
318
319 // Configure login screen environment.
320 SetUserLoggedIn(false);
321 EXPECT_EQ(user::LOGGED_IN_NONE,
[email protected]945f9cae2012-12-12 09:54:29322 shell->system_tray_delegate()->GetUserLoginStatus());
[email protected]1b219922012-11-13 21:16:43323 EXPECT_FALSE(shell->delegate()->IsUserLoggedIn());
324 EXPECT_FALSE(shell->delegate()->IsSessionStarted());
325
326 internal::RootWindowController* controller =
327 shell->GetPrimaryRootWindowController();
328 EXPECT_EQ(Shell::GetContainer(controller->root_window(),
329 internal::kShellWindowId_LockSystemModalContainer)->layout_manager(),
330 controller->GetSystemModalLayoutManager(NULL));
331
332 aura::Window* lock_container =
333 Shell::GetContainer(controller->root_window(),
334 internal::kShellWindowId_LockScreenContainer);
335 views::Widget* login_modal_widget =
336 CreateModalWidgetWithParent(gfx::Rect(300, 10, 100, 100), lock_container);
337 EXPECT_EQ(Shell::GetContainer(controller->root_window(),
338 internal::kShellWindowId_LockSystemModalContainer)->layout_manager(),
339 controller->GetSystemModalLayoutManager(
340 login_modal_widget->GetNativeView()));
341 login_modal_widget->Close();
342
343 // Configure user session environment.
344 SetUserLoggedIn(true);
345 SetSessionStarted(true);
346 EXPECT_EQ(user::LOGGED_IN_USER,
[email protected]945f9cae2012-12-12 09:54:29347 shell->system_tray_delegate()->GetUserLoginStatus());
[email protected]1b219922012-11-13 21:16:43348 EXPECT_TRUE(shell->delegate()->IsUserLoggedIn());
349 EXPECT_TRUE(shell->delegate()->IsSessionStarted());
350 EXPECT_EQ(Shell::GetContainer(controller->root_window(),
351 internal::kShellWindowId_SystemModalContainer)->layout_manager(),
352 controller->GetSystemModalLayoutManager(NULL));
353
354 views::Widget* session_modal_widget =
355 CreateModalWidget(gfx::Rect(300, 10, 100, 100));
356 EXPECT_EQ(Shell::GetContainer(controller->root_window(),
357 internal::kShellWindowId_SystemModalContainer)->layout_manager(),
358 controller->GetSystemModalLayoutManager(
359 session_modal_widget->GetNativeView()));
360}
361
[email protected]2ee2f5d2013-01-10 23:37:16362// Ensure a workspace with two windows reports immersive mode even if only
363// one has the property set.
364TEST_F(RootWindowControllerTest, ImmersiveMode) {
365 UpdateDisplay("600x600");
366 internal::RootWindowController* controller =
367 Shell::GetInstance()->GetPrimaryRootWindowController();
368
369 // Open a maximized window.
370 Widget* w1 = CreateTestWidget(gfx::Rect(0, 1, 250, 251));
371 w1->Maximize();
372
373 // Immersive mode off by default.
374 EXPECT_FALSE(controller->IsImmersiveMode());
375
376 // Enter immersive mode.
377 w1->GetNativeWindow()->SetProperty(ash::internal::kImmersiveModeKey, true);
378 EXPECT_TRUE(controller->IsImmersiveMode());
379
380 // Add a child, like a print window. Still in immersive mode.
381 Widget* w2 =
382 Widget::CreateWindowWithParentAndBounds(NULL,
383 w1->GetNativeWindow(),
384 gfx::Rect(0, 1, 150, 151));
385 w2->Show();
386 EXPECT_TRUE(controller->IsImmersiveMode());
387}
388
[email protected]f1853122012-06-27 16:21:26389} // namespace test
390} // namespace ash