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
200  // First clear fullscreen status, since both fullscreen and maximized windows
201  // share the same desktop workspace, which cancels the shelf status.
202  fullscreen->SetFullscreen(false);
203  EXPECT_EQ(root_windows[0], maximized->GetNativeView()->GetRootWindow());
204  EXPECT_EQ("0,0 600x597",
205            maximized->GetWindowBoundsInScreen().ToString());
206  EXPECT_EQ("0,0 600x597",
207            maximized->GetNativeView()->GetBoundsInRootWindow().ToString());
208
209  // Set fullscreen to true. In that case the 3px inset becomes invisible so
210  // the maximized window can also use the area fully.
211  fullscreen->SetFullscreen(true);
212  EXPECT_EQ(root_windows[0], maximized->GetNativeView()->GetRootWindow());
213  EXPECT_EQ("0,0 600x600",
214            maximized->GetWindowBoundsInScreen().ToString());
215  EXPECT_EQ("0,0 600x600",
216            maximized->GetNativeView()->GetBoundsInRootWindow().ToString());
217
218  EXPECT_EQ(root_windows[0], minimized->GetNativeView()->GetRootWindow());
219  EXPECT_EQ("200,10 100x100",
220            minimized->GetWindowBoundsInScreen().ToString());
221
222  EXPECT_EQ(root_windows[0], fullscreen->GetNativeView()->GetRootWindow());
223  EXPECT_TRUE(fullscreen->IsFullscreen());
224  EXPECT_EQ("0,0 600x600",
225            fullscreen->GetWindowBoundsInScreen().ToString());
226  EXPECT_EQ("0,0 600x600",
227            fullscreen->GetNativeView()->GetBoundsInRootWindow().ToString());
228
229  // Test if the restore bounds are correctly updated.
230  wm::RestoreWindow(maximized->GetNativeView());
231  EXPECT_EQ("100,10 100x100", maximized->GetWindowBoundsInScreen().ToString());
232  EXPECT_EQ("100,10 100x100",
233            maximized->GetNativeView()->GetBoundsInRootWindow().ToString());
234
235  fullscreen->SetFullscreen(false);
236  EXPECT_EQ("300,10 100x100",
237            fullscreen->GetWindowBoundsInScreen().ToString());
238  EXPECT_EQ("300,10 100x100",
239            fullscreen->GetNativeView()->GetBoundsInRootWindow().ToString());
240
241  // Test if the unparented widget has moved.
242  EXPECT_EQ(root_windows[0],
243            unparented_control->GetNativeView()->GetRootWindow());
244  EXPECT_EQ(internal::kShellWindowId_UnparentedControlContainer,
245            unparented_control->GetNativeView()->parent()->id());
246
247  // Test if the panel has moved.
248  EXPECT_EQ(root_windows[0], panel->GetRootWindow());
249  EXPECT_EQ(internal::kShellWindowId_PanelContainer, panel->parent()->id());
250}
251
252TEST_F(RootWindowControllerTest, MoveWindows_Modal) {
253  if (!SupportsMultipleDisplays())
254    return;
255
256  UpdateDisplay("500x500,500x500");
257
258  Shell::RootWindowList root_windows = Shell::GetAllRootWindows();
259  // Emulate virtual screen coordinate system.
260  root_windows[0]->SetBounds(gfx::Rect(0, 0, 500, 500));
261  root_windows[1]->SetBounds(gfx::Rect(500, 0, 500, 500));
262
263  views::Widget* normal = CreateTestWidget(gfx::Rect(300, 10, 100, 100));
264  EXPECT_EQ(root_windows[0], normal->GetNativeView()->GetRootWindow());
265  EXPECT_TRUE(wm::IsActiveWindow(normal->GetNativeView()));
266
267  views::Widget* modal = CreateModalWidget(gfx::Rect(650, 10, 100, 100));
268  EXPECT_EQ(root_windows[1], modal->GetNativeView()->GetRootWindow());
269  EXPECT_TRUE(GetModalContainer(root_windows[1])->Contains(
270      modal->GetNativeView()));
271  EXPECT_TRUE(wm::IsActiveWindow(modal->GetNativeView()));
272
273  aura::test::EventGenerator generator_1st(root_windows[0]);
274  generator_1st.ClickLeftButton();
275  EXPECT_TRUE(wm::IsActiveWindow(modal->GetNativeView()));
276
277  UpdateDisplay("500x500");
278  EXPECT_EQ(root_windows[0], modal->GetNativeView()->GetRootWindow());
279  EXPECT_TRUE(wm::IsActiveWindow(modal->GetNativeView()));
280  generator_1st.ClickLeftButton();
281  EXPECT_TRUE(wm::IsActiveWindow(modal->GetNativeView()));
282}
283
284TEST_F(RootWindowControllerTest, ModalContainer) {
285  UpdateDisplay("600x600");
286  Shell* shell = Shell::GetInstance();
287  internal::RootWindowController* controller =
288      shell->GetPrimaryRootWindowController();
289  EXPECT_EQ(user::LOGGED_IN_USER,
290            shell->system_tray_delegate()->GetUserLoginStatus());
291  EXPECT_EQ(Shell::GetContainer(controller->root_window(),
292      internal::kShellWindowId_SystemModalContainer)->layout_manager(),
293          controller->GetSystemModalLayoutManager(NULL));
294
295  views::Widget* session_modal_widget =
296      CreateModalWidget(gfx::Rect(300, 10, 100, 100));
297  EXPECT_EQ(Shell::GetContainer(controller->root_window(),
298      internal::kShellWindowId_SystemModalContainer)->layout_manager(),
299          controller->GetSystemModalLayoutManager(
300              session_modal_widget->GetNativeView()));
301
302  shell->session_state_delegate()->LockScreen();
303  EXPECT_EQ(user::LOGGED_IN_LOCKED,
304            shell->system_tray_delegate()->GetUserLoginStatus());
305  EXPECT_EQ(Shell::GetContainer(controller->root_window(),
306      internal::kShellWindowId_LockSystemModalContainer)->layout_manager(),
307          controller->GetSystemModalLayoutManager(NULL));
308
309  aura::Window* lock_container =
310      Shell::GetContainer(controller->root_window(),
311                          internal::kShellWindowId_LockScreenContainer);
312  views::Widget* lock_modal_widget =
313      CreateModalWidgetWithParent(gfx::Rect(300, 10, 100, 100), lock_container);
314  EXPECT_EQ(Shell::GetContainer(controller->root_window(),
315      internal::kShellWindowId_LockSystemModalContainer)->layout_manager(),
316          controller->GetSystemModalLayoutManager(
317              lock_modal_widget->GetNativeView()));
318  EXPECT_EQ(Shell::GetContainer(controller->root_window(),
319        internal::kShellWindowId_SystemModalContainer)->layout_manager(),
320            controller->GetSystemModalLayoutManager(
321                session_modal_widget->GetNativeView()));
322
323  shell->session_state_delegate()->UnlockScreen();
324}
325
326TEST_F(RootWindowControllerTest, ModalContainerNotLoggedInLoggedIn) {
327  UpdateDisplay("600x600");
328  Shell* shell = Shell::GetInstance();
329
330  // Configure login screen environment.
331  SetUserLoggedIn(false);
332  EXPECT_EQ(user::LOGGED_IN_NONE,
333            shell->system_tray_delegate()->GetUserLoginStatus());
334  EXPECT_EQ(0, shell->session_state_delegate()->NumberOfLoggedInUsers());
335  EXPECT_FALSE(shell->session_state_delegate()->IsActiveUserSessionStarted());
336
337  internal::RootWindowController* controller =
338      shell->GetPrimaryRootWindowController();
339  EXPECT_EQ(Shell::GetContainer(controller->root_window(),
340      internal::kShellWindowId_LockSystemModalContainer)->layout_manager(),
341          controller->GetSystemModalLayoutManager(NULL));
342
343  aura::Window* lock_container =
344      Shell::GetContainer(controller->root_window(),
345                          internal::kShellWindowId_LockScreenContainer);
346  views::Widget* login_modal_widget =
347      CreateModalWidgetWithParent(gfx::Rect(300, 10, 100, 100), lock_container);
348  EXPECT_EQ(Shell::GetContainer(controller->root_window(),
349      internal::kShellWindowId_LockSystemModalContainer)->layout_manager(),
350          controller->GetSystemModalLayoutManager(
351              login_modal_widget->GetNativeView()));
352  login_modal_widget->Close();
353
354  // Configure user session environment.
355  SetUserLoggedIn(true);
356  SetSessionStarted(true);
357  EXPECT_EQ(user::LOGGED_IN_USER,
358            shell->system_tray_delegate()->GetUserLoginStatus());
359  EXPECT_EQ(1, shell->session_state_delegate()->NumberOfLoggedInUsers());
360  EXPECT_TRUE(shell->session_state_delegate()->IsActiveUserSessionStarted());
361  EXPECT_EQ(Shell::GetContainer(controller->root_window(),
362      internal::kShellWindowId_SystemModalContainer)->layout_manager(),
363          controller->GetSystemModalLayoutManager(NULL));
364
365  views::Widget* session_modal_widget =
366        CreateModalWidget(gfx::Rect(300, 10, 100, 100));
367  EXPECT_EQ(Shell::GetContainer(controller->root_window(),
368      internal::kShellWindowId_SystemModalContainer)->layout_manager(),
369          controller->GetSystemModalLayoutManager(
370              session_modal_widget->GetNativeView()));
371}
372
373TEST_F(RootWindowControllerTest, ModalContainerBlockedSession) {
374  UpdateDisplay("600x600");
375  Shell* shell = Shell::GetInstance();
376  internal::RootWindowController* controller =
377      shell->GetPrimaryRootWindowController();
378  aura::Window* lock_container =
379      Shell::GetContainer(controller->root_window(),
380                          internal::kShellWindowId_LockScreenContainer);
381  for (int block_reason = FIRST_BLOCK_REASON;
382       block_reason < NUMBER_OF_BLOCK_REASONS;
383       ++block_reason) {
384    views::Widget* session_modal_widget =
385          CreateModalWidget(gfx::Rect(300, 10, 100, 100));
386    EXPECT_EQ(Shell::GetContainer(controller->root_window(),
387        internal::kShellWindowId_SystemModalContainer)->layout_manager(),
388            controller->GetSystemModalLayoutManager(
389                session_modal_widget->GetNativeView()));
390    EXPECT_EQ(Shell::GetContainer(controller->root_window(),
391        internal::kShellWindowId_SystemModalContainer)->layout_manager(),
392            controller->GetSystemModalLayoutManager(NULL));
393    session_modal_widget->Close();
394
395    BlockUserSession(static_cast<UserSessionBlockReason>(block_reason));
396
397    EXPECT_EQ(Shell::GetContainer(controller->root_window(),
398        internal::kShellWindowId_LockSystemModalContainer)->layout_manager(),
399            controller->GetSystemModalLayoutManager(NULL));
400
401    views::Widget* lock_modal_widget =
402        CreateModalWidgetWithParent(gfx::Rect(300, 10, 100, 100),
403                                    lock_container);
404    EXPECT_EQ(Shell::GetContainer(controller->root_window(),
405        internal::kShellWindowId_LockSystemModalContainer)->layout_manager(),
406              controller->GetSystemModalLayoutManager(
407                  lock_modal_widget->GetNativeView()));
408
409    session_modal_widget =
410          CreateModalWidget(gfx::Rect(300, 10, 100, 100));
411    EXPECT_EQ(Shell::GetContainer(controller->root_window(),
412        internal::kShellWindowId_SystemModalContainer)->layout_manager(),
413            controller->GetSystemModalLayoutManager(
414                session_modal_widget->GetNativeView()));
415    session_modal_widget->Close();
416
417    lock_modal_widget->Close();
418    UnblockUserSession();
419  }
420}
421
422// Test that GetFullscreenWindow() returns a fullscreen window only if the
423// fullscreen window is in the active workspace.
424TEST_F(RootWindowControllerTest, GetFullscreenWindow) {
425  UpdateDisplay("600x600");
426  internal::RootWindowController* controller =
427      Shell::GetInstance()->GetPrimaryRootWindowController();
428
429  Widget* w1 = CreateTestWidget(gfx::Rect(0, 0, 100, 100));
430  w1->Maximize();
431  Widget* w2 = CreateTestWidget(gfx::Rect(0, 0, 100, 100));
432  w2->SetFullscreen(true);
433  // |w3| is a transient child of |w2|.
434  Widget* w3 = Widget::CreateWindowWithParentAndBounds(NULL,
435      w2->GetNativeWindow(), gfx::Rect(0, 0, 100, 100));
436
437  // Test that GetFullscreenWindow() finds the fullscreen window when one of
438  // its transient children is active.
439  w3->Activate();
440  EXPECT_EQ(w2->GetNativeWindow(), controller->GetFullscreenWindow());
441
442  // Since there's only one desktop workspace, it always returns the same
443  // fullscreen window.
444  w1->Activate();
445  EXPECT_EQ(w2->GetNativeWindow(), controller->GetFullscreenWindow());
446}
447
448// Test that user session window can't be focused if user session blocked by
449// some overlapping UI.
450TEST_F(RootWindowControllerTest, FocusBlockedWindow) {
451  UpdateDisplay("600x600");
452  internal::RootWindowController* controller =
453      Shell::GetInstance()->GetPrimaryRootWindowController();
454  aura::Window* lock_container =
455      Shell::GetContainer(controller->root_window(),
456                          internal::kShellWindowId_LockScreenContainer);
457  aura::Window* lock_window = Widget::CreateWindowWithParentAndBounds(NULL,
458      lock_container, gfx::Rect(0, 0, 100, 100))->GetNativeView();
459  lock_window->Show();
460  aura::Window* session_window =
461      CreateTestWidget(gfx::Rect(0, 0, 100, 100))->GetNativeView();
462  session_window->Show();
463
464  for (int block_reason = FIRST_BLOCK_REASON;
465       block_reason < NUMBER_OF_BLOCK_REASONS;
466       ++block_reason) {
467    BlockUserSession(static_cast<UserSessionBlockReason>(block_reason));
468    lock_window->Focus();
469    EXPECT_TRUE(lock_window->HasFocus());
470    session_window->Focus();
471    EXPECT_FALSE(session_window->HasFocus());
472    UnblockUserSession();
473  }
474}
475
476typedef test::NoSessionAshTestBase NoSessionRootWindowControllerTest;
477
478// Make sure that an event handler exists for entire display area.
479TEST_F(NoSessionRootWindowControllerTest, Event) {
480  aura::RootWindow* root = Shell::GetPrimaryRootWindow();
481  const gfx::Size size = root->bounds().size();
482  aura::Window* event_target = root->GetEventHandlerForPoint(gfx::Point(0, 0));
483  EXPECT_TRUE(event_target);
484  EXPECT_EQ(event_target,
485            root->GetEventHandlerForPoint(gfx::Point(0, size.height() - 1)));
486  EXPECT_EQ(event_target,
487            root->GetEventHandlerForPoint(gfx::Point(size.width() - 1, 0)));
488  EXPECT_EQ(event_target,
489            root->GetEventHandlerForPoint(gfx::Point(0, size.height() - 1)));
490  EXPECT_EQ(event_target,
491            root->GetEventHandlerForPoint(
492                gfx::Point(size.width() - 1, size.height() - 1)));
493}
494
495}  // namespace test
496}  // namespace ash
497