root_window_controller_unittest.cc revision ca12bfac764ba476d6cd062bf1dde12cc64c3f40
1// 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
5#include "ash/root_window_controller.h"
6
7#include "ash/session_state_delegate.h"
8#include "ash/shelf/shelf_layout_manager.h"
9#include "ash/shell.h"
10#include "ash/shell_window_ids.h"
11#include "ash/system/tray/system_tray_delegate.h"
12#include "ash/test/ash_test_base.h"
13#include "ash/wm/system_modal_container_layout_manager.h"
14#include "ash/wm/window_properties.h"
15#include "ash/wm/window_util.h"
16#include "ui/aura/client/focus_change_observer.h"
17#include "ui/aura/client/focus_client.h"
18#include "ui/aura/env.h"
19#include "ui/aura/root_window.h"
20#include "ui/aura/test/event_generator.h"
21#include "ui/aura/test/test_window_delegate.h"
22#include "ui/aura/test/test_windows.h"
23#include "ui/aura/window.h"
24#include "ui/aura/window_tracker.h"
25#include "ui/views/controls/menu/menu_controller.h"
26#include "ui/views/widget/widget.h"
27#include "ui/views/widget/widget_delegate.h"
28
29using aura::Window;
30using views::Widget;
31
32namespace 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
54class DeleteOnBlurDelegate : public aura::test::TestWindowDelegate,
55                             public aura::client::FocusChangeObserver {
56 public:
57  DeleteOnBlurDelegate() : window_(NULL) {}
58  virtual ~DeleteOnBlurDelegate() {}
59
60  void SetWindow(aura::Window* window) {
61    window_ = window;
62    aura::client::SetFocusChangeObserver(window_, this);
63  }
64
65 private:
66  // aura::test::TestWindowDelegate overrides:
67  virtual bool CanFocus() OVERRIDE {
68    return true;
69  }
70
71  // 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_;
76  }
77
78  aura::Window* window_;
79
80  DISALLOW_COPY_AND_ASSIGN(DeleteOnBlurDelegate);
81};
82
83}  // namespace
84
85namespace test {
86
87class 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};
119
120TEST_F(RootWindowControllerTest, MoveWindows_Basic) {
121  if (!SupportsMultipleDisplays())
122    return;
123
124  UpdateDisplay("600x600,500x500");
125  Shell::RootWindowList root_windows = Shell::GetAllRootWindows();
126  internal::RootWindowController* controller =
127      Shell::GetPrimaryRootWindowController();
128  internal::ShelfLayoutManager* shelf_layout_manager =
129      controller->GetShelfLayoutManager();
130  shelf_layout_manager->SetAutoHideBehavior(
131      ash::SHELF_AUTO_HIDE_BEHAVIOR_ALWAYS);
132
133  views::Widget* normal = CreateTestWidget(gfx::Rect(650, 10, 100, 100));
134  EXPECT_EQ(root_windows[1], normal->GetNativeView()->GetRootWindow());
135  EXPECT_EQ("650,10 100x100", normal->GetWindowBoundsInScreen().ToString());
136  EXPECT_EQ("50,10 100x100",
137            normal->GetNativeView()->GetBoundsInRootWindow().ToString());
138
139  views::Widget* maximized = CreateTestWidget(gfx::Rect(700, 10, 100, 100));
140  maximized->Maximize();
141  EXPECT_EQ(root_windows[1], maximized->GetNativeView()->GetRootWindow());
142  EXPECT_EQ("600,0 500x452", maximized->GetWindowBoundsInScreen().ToString());
143  EXPECT_EQ("0,0 500x452",
144            maximized->GetNativeView()->GetBoundsInRootWindow().ToString());
145
146  views::Widget* minimized = CreateTestWidget(gfx::Rect(800, 10, 100, 100));
147  minimized->Minimize();
148  EXPECT_EQ(root_windows[1], minimized->GetNativeView()->GetRootWindow());
149  EXPECT_EQ("800,10 100x100",
150            minimized->GetWindowBoundsInScreen().ToString());
151
152  views::Widget* fullscreen = CreateTestWidget(gfx::Rect(900, 10, 100, 100));
153  fullscreen->SetFullscreen(true);
154  EXPECT_EQ(root_windows[1], fullscreen->GetNativeView()->GetRootWindow());
155
156  EXPECT_EQ("600,0 500x500",
157            fullscreen->GetWindowBoundsInScreen().ToString());
158  EXPECT_EQ("0,0 500x500",
159            fullscreen->GetNativeView()->GetBoundsInRootWindow().ToString());
160
161  views::Widget* unparented_control = new Widget;
162  Widget::InitParams params;
163  params.bounds = gfx::Rect(650, 10, 100, 100);
164  params.context = CurrentContext();
165  params.type = Widget::InitParams::TYPE_CONTROL;
166  unparented_control->Init(params);
167  EXPECT_EQ(root_windows[1],
168            unparented_control->GetNativeView()->GetRootWindow());
169  EXPECT_EQ(internal::kShellWindowId_UnparentedControlContainer,
170            unparented_control->GetNativeView()->parent()->id());
171
172  aura::Window* panel = CreateTestWindowInShellWithDelegateAndType(
173      NULL, aura::client::WINDOW_TYPE_PANEL, 0, gfx::Rect(700, 100, 100, 100));
174  EXPECT_EQ(root_windows[1], panel->GetRootWindow());
175  EXPECT_EQ(internal::kShellWindowId_PanelContainer, panel->parent()->id());
176
177  // Make sure a window that will delete itself when losing focus
178  // will not crash.
179  aura::WindowTracker tracker;
180  DeleteOnBlurDelegate delete_on_blur_delegate;
181  aura::Window* d2 = CreateTestWindowInShellWithDelegate(
182      &delete_on_blur_delegate, 0, gfx::Rect(50, 50, 100, 100));
183  delete_on_blur_delegate.SetWindow(d2);
184  aura::client::GetFocusClient(root_windows[0])->FocusWindow(d2);
185  tracker.Add(d2);
186
187  UpdateDisplay("600x600");
188
189  // d2 must have been deleted.
190  EXPECT_FALSE(tracker.Contains(d2));
191
192  EXPECT_EQ(root_windows[0], normal->GetNativeView()->GetRootWindow());
193  EXPECT_EQ("50,10 100x100", normal->GetWindowBoundsInScreen().ToString());
194  EXPECT_EQ("50,10 100x100",
195            normal->GetNativeView()->GetBoundsInRootWindow().ToString());
196
197  // Maximized area on primary display has 3px (given as
198  // kAutoHideSize in shelf_layout_manager.cc) inset at the bottom.
199  EXPECT_EQ(root_windows[0], maximized->GetNativeView()->GetRootWindow());
200  EXPECT_EQ("0,0 600x597",
201            maximized->GetWindowBoundsInScreen().ToString());
202  EXPECT_EQ("0,0 600x597",
203            maximized->GetNativeView()->GetBoundsInRootWindow().ToString());
204
205  EXPECT_EQ(root_windows[0], minimized->GetNativeView()->GetRootWindow());
206  EXPECT_EQ("200,10 100x100",
207            minimized->GetWindowBoundsInScreen().ToString());
208
209  EXPECT_EQ(root_windows[0], fullscreen->GetNativeView()->GetRootWindow());
210  EXPECT_TRUE(fullscreen->IsFullscreen());
211  EXPECT_EQ("0,0 600x600",
212            fullscreen->GetWindowBoundsInScreen().ToString());
213  EXPECT_EQ("0,0 600x600",
214            fullscreen->GetNativeView()->GetBoundsInRootWindow().ToString());
215
216  // Test if the restore bounds are correctly updated.
217  wm::RestoreWindow(maximized->GetNativeView());
218  EXPECT_EQ("100,10 100x100", maximized->GetWindowBoundsInScreen().ToString());
219  EXPECT_EQ("100,10 100x100",
220            maximized->GetNativeView()->GetBoundsInRootWindow().ToString());
221
222  fullscreen->SetFullscreen(false);
223  EXPECT_EQ("300,10 100x100",
224            fullscreen->GetWindowBoundsInScreen().ToString());
225  EXPECT_EQ("300,10 100x100",
226            fullscreen->GetNativeView()->GetBoundsInRootWindow().ToString());
227
228  // Test if the unparented widget has moved.
229  EXPECT_EQ(root_windows[0],
230            unparented_control->GetNativeView()->GetRootWindow());
231  EXPECT_EQ(internal::kShellWindowId_UnparentedControlContainer,
232            unparented_control->GetNativeView()->parent()->id());
233
234  // Test if the panel has moved.
235  EXPECT_EQ(root_windows[0], panel->GetRootWindow());
236  EXPECT_EQ(internal::kShellWindowId_PanelContainer, panel->parent()->id());
237}
238
239TEST_F(RootWindowControllerTest, MoveWindows_Modal) {
240  if (!SupportsMultipleDisplays())
241    return;
242
243  UpdateDisplay("500x500,500x500");
244
245  Shell::RootWindowList root_windows = Shell::GetAllRootWindows();
246  // Emulate virtual screen coordinate system.
247  root_windows[0]->SetBounds(gfx::Rect(0, 0, 500, 500));
248  root_windows[1]->SetBounds(gfx::Rect(500, 0, 500, 500));
249
250  views::Widget* normal = CreateTestWidget(gfx::Rect(300, 10, 100, 100));
251  EXPECT_EQ(root_windows[0], normal->GetNativeView()->GetRootWindow());
252  EXPECT_TRUE(wm::IsActiveWindow(normal->GetNativeView()));
253
254  views::Widget* modal = CreateModalWidget(gfx::Rect(650, 10, 100, 100));
255  EXPECT_EQ(root_windows[1], modal->GetNativeView()->GetRootWindow());
256  EXPECT_TRUE(GetModalContainer(root_windows[1])->Contains(
257      modal->GetNativeView()));
258  EXPECT_TRUE(wm::IsActiveWindow(modal->GetNativeView()));
259
260  aura::test::EventGenerator generator_1st(root_windows[0]);
261  generator_1st.ClickLeftButton();
262  EXPECT_TRUE(wm::IsActiveWindow(modal->GetNativeView()));
263
264  UpdateDisplay("500x500");
265  EXPECT_EQ(root_windows[0], modal->GetNativeView()->GetRootWindow());
266  EXPECT_TRUE(wm::IsActiveWindow(modal->GetNativeView()));
267  generator_1st.ClickLeftButton();
268  EXPECT_TRUE(wm::IsActiveWindow(modal->GetNativeView()));
269}
270
271TEST_F(RootWindowControllerTest, ModalContainer) {
272  UpdateDisplay("600x600");
273  Shell* shell = Shell::GetInstance();
274  internal::RootWindowController* controller =
275      shell->GetPrimaryRootWindowController();
276  EXPECT_EQ(user::LOGGED_IN_USER,
277            shell->system_tray_delegate()->GetUserLoginStatus());
278  EXPECT_EQ(Shell::GetContainer(controller->root_window(),
279      internal::kShellWindowId_SystemModalContainer)->layout_manager(),
280          controller->GetSystemModalLayoutManager(NULL));
281
282  views::Widget* session_modal_widget =
283      CreateModalWidget(gfx::Rect(300, 10, 100, 100));
284  EXPECT_EQ(Shell::GetContainer(controller->root_window(),
285      internal::kShellWindowId_SystemModalContainer)->layout_manager(),
286          controller->GetSystemModalLayoutManager(
287              session_modal_widget->GetNativeView()));
288
289  shell->session_state_delegate()->LockScreen();
290  EXPECT_EQ(user::LOGGED_IN_LOCKED,
291            shell->system_tray_delegate()->GetUserLoginStatus());
292  EXPECT_EQ(Shell::GetContainer(controller->root_window(),
293      internal::kShellWindowId_LockSystemModalContainer)->layout_manager(),
294          controller->GetSystemModalLayoutManager(NULL));
295
296  aura::Window* lock_container =
297      Shell::GetContainer(controller->root_window(),
298                          internal::kShellWindowId_LockScreenContainer);
299  views::Widget* lock_modal_widget =
300      CreateModalWidgetWithParent(gfx::Rect(300, 10, 100, 100), lock_container);
301  EXPECT_EQ(Shell::GetContainer(controller->root_window(),
302      internal::kShellWindowId_LockSystemModalContainer)->layout_manager(),
303          controller->GetSystemModalLayoutManager(
304              lock_modal_widget->GetNativeView()));
305  EXPECT_EQ(Shell::GetContainer(controller->root_window(),
306        internal::kShellWindowId_SystemModalContainer)->layout_manager(),
307            controller->GetSystemModalLayoutManager(
308                session_modal_widget->GetNativeView()));
309
310  shell->session_state_delegate()->UnlockScreen();
311}
312
313TEST_F(RootWindowControllerTest, ModalContainerNotLoggedInLoggedIn) {
314  UpdateDisplay("600x600");
315  Shell* shell = Shell::GetInstance();
316
317  // Configure login screen environment.
318  SetUserLoggedIn(false);
319  EXPECT_EQ(user::LOGGED_IN_NONE,
320            shell->system_tray_delegate()->GetUserLoginStatus());
321  EXPECT_EQ(0, shell->session_state_delegate()->NumberOfLoggedInUsers());
322  EXPECT_FALSE(shell->session_state_delegate()->IsActiveUserSessionStarted());
323
324  internal::RootWindowController* controller =
325      shell->GetPrimaryRootWindowController();
326  EXPECT_EQ(Shell::GetContainer(controller->root_window(),
327      internal::kShellWindowId_LockSystemModalContainer)->layout_manager(),
328          controller->GetSystemModalLayoutManager(NULL));
329
330  aura::Window* lock_container =
331      Shell::GetContainer(controller->root_window(),
332                          internal::kShellWindowId_LockScreenContainer);
333  views::Widget* login_modal_widget =
334      CreateModalWidgetWithParent(gfx::Rect(300, 10, 100, 100), lock_container);
335  EXPECT_EQ(Shell::GetContainer(controller->root_window(),
336      internal::kShellWindowId_LockSystemModalContainer)->layout_manager(),
337          controller->GetSystemModalLayoutManager(
338              login_modal_widget->GetNativeView()));
339  login_modal_widget->Close();
340
341  // Configure user session environment.
342  SetUserLoggedIn(true);
343  SetSessionStarted(true);
344  EXPECT_EQ(user::LOGGED_IN_USER,
345            shell->system_tray_delegate()->GetUserLoginStatus());
346  EXPECT_EQ(1, shell->session_state_delegate()->NumberOfLoggedInUsers());
347  EXPECT_TRUE(shell->session_state_delegate()->IsActiveUserSessionStarted());
348  EXPECT_EQ(Shell::GetContainer(controller->root_window(),
349      internal::kShellWindowId_SystemModalContainer)->layout_manager(),
350          controller->GetSystemModalLayoutManager(NULL));
351
352  views::Widget* session_modal_widget =
353        CreateModalWidget(gfx::Rect(300, 10, 100, 100));
354  EXPECT_EQ(Shell::GetContainer(controller->root_window(),
355      internal::kShellWindowId_SystemModalContainer)->layout_manager(),
356          controller->GetSystemModalLayoutManager(
357              session_modal_widget->GetNativeView()));
358}
359
360// Test that GetFullscreenWindow() returns a fullscreen window only if the
361// fullscreen window is in the active workspace.
362TEST_F(RootWindowControllerTest, GetFullscreenWindow) {
363  UpdateDisplay("600x600");
364  internal::RootWindowController* controller =
365      Shell::GetInstance()->GetPrimaryRootWindowController();
366
367  Widget* w1 = CreateTestWidget(gfx::Rect(0, 0, 100, 100));
368  w1->Maximize();
369  Widget* w2 = CreateTestWidget(gfx::Rect(0, 0, 100, 100));
370  w2->SetFullscreen(true);
371  // |w3| is a transient child of |w2|.
372  Widget* w3 = Widget::CreateWindowWithParentAndBounds(NULL,
373      w2->GetNativeWindow(), gfx::Rect(0, 0, 100, 100));
374
375  // Test that GetFullscreenWindow() finds the fullscreen window when one of
376  // its transient children is active.
377  w3->Activate();
378  EXPECT_EQ(w2->GetNativeWindow(), controller->GetFullscreenWindow());
379
380  // Activate the maximized window's workspace. GetFullscreenWindow() should
381  // fail because the fullscreen window's workspace is no longer active.
382  w1->Activate();
383  EXPECT_FALSE(controller->GetFullscreenWindow());
384
385  // If the fullscreen window is active, GetFullscreenWindow() should find it.
386  w2->Activate();
387  EXPECT_EQ(w2->GetNativeWindow(), controller->GetFullscreenWindow());
388}
389
390// Test that user session window can't be focused if user session blocked by
391// some overlapping UI.
392TEST_F(RootWindowControllerTest, FocusBlockedWindow) {
393  UpdateDisplay("600x600");
394  internal::RootWindowController* controller =
395      Shell::GetInstance()->GetPrimaryRootWindowController();
396  aura::Window* lock_container =
397      Shell::GetContainer(controller->root_window(),
398                          internal::kShellWindowId_LockScreenContainer);
399  aura::Window* lock_window = Widget::CreateWindowWithParentAndBounds(NULL,
400      lock_container, gfx::Rect(0, 0, 100, 100))->GetNativeView();
401  lock_window->Show();
402  aura::Window* session_window =
403      CreateTestWidget(gfx::Rect(0, 0, 100, 100))->GetNativeView();
404  session_window->Show();
405
406  // Lock screen.
407  Shell::GetInstance()->session_state_delegate()->LockScreen();
408  lock_window->Focus();
409  EXPECT_TRUE(lock_window->HasFocus());
410  session_window->Focus();
411  EXPECT_FALSE(session_window->HasFocus());
412  Shell::GetInstance()->session_state_delegate()->UnlockScreen();
413
414  // Session not started yet.
415  SetSessionStarted(false);
416  lock_window->Focus();
417  EXPECT_TRUE(lock_window->HasFocus());
418  session_window->Focus();
419  EXPECT_FALSE(session_window->HasFocus());
420  SetSessionStarted(true);
421}
422
423}  // namespace test
424}  // namespace ash
425