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/shell.h"
6
7#include <algorithm>
8#include <vector>
9
10#include "ash/ash_switches.h"
11#include "ash/desktop_background/desktop_background_widget_controller.h"
12#include "ash/display/mouse_cursor_event_filter.h"
13#include "ash/drag_drop/drag_drop_controller.h"
14#include "ash/root_window_controller.h"
15#include "ash/session/session_state_delegate.h"
16#include "ash/shelf/shelf.h"
17#include "ash/shelf/shelf_layout_manager.h"
18#include "ash/shelf/shelf_widget.h"
19#include "ash/shell_delegate.h"
20#include "ash/shell_window_ids.h"
21#include "ash/test/ash_test_base.h"
22#include "ash/test/shell_test_api.h"
23#include "ash/wm/root_window_layout_manager.h"
24#include "ash/wm/window_util.h"
25#include "base/strings/utf_string_conversions.h"
26#include "ui/aura/client/aura_constants.h"
27#include "ui/aura/env.h"
28#include "ui/aura/window.h"
29#include "ui/aura/window_event_dispatcher.h"
30#include "ui/base/models/simple_menu_model.h"
31#include "ui/events/test/event_generator.h"
32#include "ui/events/test/events_test_utils.h"
33#include "ui/events/test/test_event_handler.h"
34#include "ui/gfx/size.h"
35#include "ui/views/controls/menu/menu_controller.h"
36#include "ui/views/controls/menu/menu_runner.h"
37#include "ui/views/widget/widget.h"
38#include "ui/views/widget/widget_delegate.h"
39#include "ui/views/window/dialog_delegate.h"
40
41using aura::RootWindow;
42
43namespace ash {
44
45namespace {
46
47aura::Window* GetDefaultContainer() {
48  return Shell::GetContainer(Shell::GetPrimaryRootWindow(),
49                             kShellWindowId_DefaultContainer);
50}
51
52aura::Window* GetAlwaysOnTopContainer() {
53  return Shell::GetContainer(Shell::GetPrimaryRootWindow(),
54                             kShellWindowId_AlwaysOnTopContainer);
55}
56
57// Expect ALL the containers!
58void ExpectAllContainers() {
59  aura::Window* root_window = Shell::GetPrimaryRootWindow();
60  EXPECT_TRUE(Shell::GetContainer(root_window,
61                                  kShellWindowId_DesktopBackgroundContainer));
62  EXPECT_TRUE(
63      Shell::GetContainer(root_window, kShellWindowId_DefaultContainer));
64  EXPECT_TRUE(
65      Shell::GetContainer(root_window, kShellWindowId_AlwaysOnTopContainer));
66  EXPECT_TRUE(Shell::GetContainer(root_window, kShellWindowId_PanelContainer));
67  EXPECT_TRUE(Shell::GetContainer(root_window, kShellWindowId_ShelfContainer));
68  EXPECT_TRUE(
69      Shell::GetContainer(root_window, kShellWindowId_SystemModalContainer));
70  EXPECT_TRUE(Shell::GetContainer(
71      root_window, kShellWindowId_LockScreenBackgroundContainer));
72  EXPECT_TRUE(
73      Shell::GetContainer(root_window, kShellWindowId_LockScreenContainer));
74  EXPECT_TRUE(Shell::GetContainer(root_window,
75                                  kShellWindowId_LockSystemModalContainer));
76  EXPECT_TRUE(Shell::GetContainer(root_window, kShellWindowId_StatusContainer));
77  EXPECT_TRUE(Shell::GetContainer(root_window, kShellWindowId_MenuContainer));
78  EXPECT_TRUE(Shell::GetContainer(root_window,
79                                  kShellWindowId_DragImageAndTooltipContainer));
80  EXPECT_TRUE(
81      Shell::GetContainer(root_window, kShellWindowId_SettingBubbleContainer));
82  EXPECT_TRUE(
83      Shell::GetContainer(root_window, kShellWindowId_OverlayContainer));
84  EXPECT_TRUE(Shell::GetContainer(
85      root_window, kShellWindowId_VirtualKeyboardParentContainer));
86#if defined(OS_CHROMEOS)
87  EXPECT_TRUE(
88      Shell::GetContainer(root_window, kShellWindowId_MouseCursorContainer));
89#endif
90}
91
92class ModalWindow : public views::WidgetDelegateView {
93 public:
94  ModalWindow() {}
95  virtual ~ModalWindow() {}
96
97  // Overridden from views::WidgetDelegate:
98  virtual views::View* GetContentsView() OVERRIDE {
99    return this;
100  }
101  virtual bool CanResize() const OVERRIDE {
102    return true;
103  }
104  virtual base::string16 GetWindowTitle() const OVERRIDE {
105    return base::ASCIIToUTF16("Modal Window");
106  }
107  virtual ui::ModalType GetModalType() const OVERRIDE {
108    return ui::MODAL_TYPE_SYSTEM;
109  }
110
111 private:
112  DISALLOW_COPY_AND_ASSIGN(ModalWindow);
113};
114
115class SimpleMenuDelegate : public ui::SimpleMenuModel::Delegate {
116 public:
117  SimpleMenuDelegate() {}
118  virtual ~SimpleMenuDelegate() {}
119
120  virtual bool IsCommandIdChecked(int command_id) const OVERRIDE {
121    return false;
122  }
123
124  virtual bool IsCommandIdEnabled(int command_id) const OVERRIDE {
125    return true;
126  }
127
128  virtual bool GetAcceleratorForCommandId(
129      int command_id,
130      ui::Accelerator* accelerator) OVERRIDE {
131    return false;
132  }
133
134  virtual void ExecuteCommand(int command_id, int event_flags) OVERRIDE {
135  }
136
137 private:
138  DISALLOW_COPY_AND_ASSIGN(SimpleMenuDelegate);
139};
140
141}  // namespace
142
143class ShellTest : public test::AshTestBase {
144 public:
145  views::Widget* CreateTestWindow(views::Widget::InitParams params) {
146    views::Widget* widget = new views::Widget;
147    params.context = CurrentContext();
148    widget->Init(params);
149    return widget;
150  }
151
152  void TestCreateWindow(views::Widget::InitParams::Type type,
153                        bool always_on_top,
154                        aura::Window* expected_container) {
155    views::Widget::InitParams widget_params(type);
156    widget_params.keep_on_top = always_on_top;
157
158    views::Widget* widget = CreateTestWindow(widget_params);
159    widget->Show();
160
161    EXPECT_TRUE(
162        expected_container->Contains(widget->GetNativeWindow()->parent())) <<
163        "TestCreateWindow: type=" << type << ", always_on_top=" <<
164        always_on_top;
165
166    widget->Close();
167  }
168
169  void LockScreenAndVerifyMenuClosed() {
170    // Verify a menu is open before locking.
171    views::MenuController* menu_controller =
172        views::MenuController::GetActiveInstance();
173    DCHECK(menu_controller);
174    EXPECT_EQ(views::MenuController::EXIT_NONE, menu_controller->exit_type());
175
176    // Create a LockScreen window.
177    views::Widget::InitParams widget_params(
178        views::Widget::InitParams::TYPE_WINDOW);
179    SessionStateDelegate* delegate =
180        Shell::GetInstance()->session_state_delegate();
181    delegate->LockScreen();
182    views::Widget* lock_widget = CreateTestWindow(widget_params);
183    ash::Shell::GetContainer(Shell::GetPrimaryRootWindow(),
184                             ash::kShellWindowId_LockScreenContainer)
185        ->AddChild(lock_widget->GetNativeView());
186    lock_widget->Show();
187    EXPECT_TRUE(delegate->IsScreenLocked());
188    EXPECT_TRUE(lock_widget->GetNativeView()->HasFocus());
189
190    // Verify menu is closed.
191    EXPECT_NE(views::MenuController::EXIT_NONE, menu_controller->exit_type());
192    lock_widget->Close();
193    delegate->UnlockScreen();
194
195    // In case the menu wasn't closed, cancel the menu to exit the nested menu
196    // run loop so that the test will not time out.
197    menu_controller->CancelAll();
198  }
199};
200
201TEST_F(ShellTest, CreateWindow) {
202  // Normal window should be created in default container.
203  TestCreateWindow(views::Widget::InitParams::TYPE_WINDOW,
204                   false,  // always_on_top
205                   GetDefaultContainer());
206  TestCreateWindow(views::Widget::InitParams::TYPE_POPUP,
207                   false,  // always_on_top
208                   GetDefaultContainer());
209
210  // Always-on-top window and popup are created in always-on-top container.
211  TestCreateWindow(views::Widget::InitParams::TYPE_WINDOW,
212                   true,  // always_on_top
213                   GetAlwaysOnTopContainer());
214  TestCreateWindow(views::Widget::InitParams::TYPE_POPUP,
215                   true,  // always_on_top
216                   GetAlwaysOnTopContainer());
217}
218
219TEST_F(ShellTest, ChangeAlwaysOnTop) {
220  views::Widget::InitParams widget_params(
221      views::Widget::InitParams::TYPE_WINDOW);
222
223  // Creates a normal window
224  views::Widget* widget = CreateTestWindow(widget_params);
225  widget->Show();
226
227  // It should be in default container.
228  EXPECT_TRUE(GetDefaultContainer()->Contains(
229                  widget->GetNativeWindow()->parent()));
230
231  // Flip always-on-top flag.
232  widget->SetAlwaysOnTop(true);
233  // And it should in always on top container now.
234  EXPECT_EQ(GetAlwaysOnTopContainer(), widget->GetNativeWindow()->parent());
235
236  // Flip always-on-top flag.
237  widget->SetAlwaysOnTop(false);
238  // It should go back to default container.
239  EXPECT_TRUE(GetDefaultContainer()->Contains(
240                  widget->GetNativeWindow()->parent()));
241
242  // Set the same always-on-top flag again.
243  widget->SetAlwaysOnTop(false);
244  // Should have no effect and we are still in the default container.
245  EXPECT_TRUE(GetDefaultContainer()->Contains(
246                  widget->GetNativeWindow()->parent()));
247
248  widget->Close();
249}
250
251TEST_F(ShellTest, CreateModalWindow) {
252  views::Widget::InitParams widget_params(
253      views::Widget::InitParams::TYPE_WINDOW);
254
255  // Create a normal window.
256  views::Widget* widget = CreateTestWindow(widget_params);
257  widget->Show();
258
259  // It should be in default container.
260  EXPECT_TRUE(GetDefaultContainer()->Contains(
261                  widget->GetNativeWindow()->parent()));
262
263  // Create a modal window.
264  views::Widget* modal_widget = views::Widget::CreateWindowWithParent(
265      new ModalWindow(), widget->GetNativeView());
266  modal_widget->Show();
267
268  // It should be in modal container.
269  aura::Window* modal_container = Shell::GetContainer(
270      Shell::GetPrimaryRootWindow(), kShellWindowId_SystemModalContainer);
271  EXPECT_EQ(modal_container, modal_widget->GetNativeWindow()->parent());
272
273  modal_widget->Close();
274  widget->Close();
275}
276
277class TestModalDialogDelegate : public views::DialogDelegateView {
278 public:
279  TestModalDialogDelegate() {}
280
281  // Overridden from views::WidgetDelegate:
282  virtual ui::ModalType GetModalType() const OVERRIDE {
283    return ui::MODAL_TYPE_SYSTEM;
284  }
285};
286
287TEST_F(ShellTest, CreateLockScreenModalWindow) {
288  views::Widget::InitParams widget_params(
289      views::Widget::InitParams::TYPE_WINDOW);
290
291  // Create a normal window.
292  views::Widget* widget = CreateTestWindow(widget_params);
293  widget->Show();
294  EXPECT_TRUE(widget->GetNativeView()->HasFocus());
295
296  // It should be in default container.
297  EXPECT_TRUE(GetDefaultContainer()->Contains(
298                  widget->GetNativeWindow()->parent()));
299
300  Shell::GetInstance()->session_state_delegate()->LockScreen();
301  // Create a LockScreen window.
302  views::Widget* lock_widget = CreateTestWindow(widget_params);
303  ash::Shell::GetContainer(Shell::GetPrimaryRootWindow(),
304                           ash::kShellWindowId_LockScreenContainer)
305      ->AddChild(lock_widget->GetNativeView());
306  lock_widget->Show();
307  EXPECT_TRUE(lock_widget->GetNativeView()->HasFocus());
308
309  // It should be in LockScreen container.
310  aura::Window* lock_screen = Shell::GetContainer(
311      Shell::GetPrimaryRootWindow(), ash::kShellWindowId_LockScreenContainer);
312  EXPECT_EQ(lock_screen, lock_widget->GetNativeWindow()->parent());
313
314  // Create a modal window with a lock window as parent.
315  views::Widget* lock_modal_widget = views::Widget::CreateWindowWithParent(
316      new ModalWindow(), lock_widget->GetNativeView());
317  lock_modal_widget->Show();
318  EXPECT_TRUE(lock_modal_widget->GetNativeView()->HasFocus());
319
320  // It should be in LockScreen modal container.
321  aura::Window* lock_modal_container =
322      Shell::GetContainer(Shell::GetPrimaryRootWindow(),
323                          ash::kShellWindowId_LockSystemModalContainer);
324  EXPECT_EQ(lock_modal_container,
325            lock_modal_widget->GetNativeWindow()->parent());
326
327  // Create a modal window with a normal window as parent.
328  views::Widget* modal_widget = views::Widget::CreateWindowWithParent(
329      new ModalWindow(), widget->GetNativeView());
330  modal_widget->Show();
331  // Window on lock screen shouldn't lost focus.
332  EXPECT_FALSE(modal_widget->GetNativeView()->HasFocus());
333  EXPECT_TRUE(lock_modal_widget->GetNativeView()->HasFocus());
334
335  // It should be in non-LockScreen modal container.
336  aura::Window* modal_container = Shell::GetContainer(
337      Shell::GetPrimaryRootWindow(), ash::kShellWindowId_SystemModalContainer);
338  EXPECT_EQ(modal_container, modal_widget->GetNativeWindow()->parent());
339
340  // Modal dialog without parent, caused crash see crbug.com/226141
341  views::Widget* modal_dialog = views::DialogDelegate::CreateDialogWidget(
342      new TestModalDialogDelegate(), CurrentContext(), NULL);
343
344  modal_dialog->Show();
345  EXPECT_FALSE(modal_dialog->GetNativeView()->HasFocus());
346  EXPECT_TRUE(lock_modal_widget->GetNativeView()->HasFocus());
347
348  modal_dialog->Close();
349  modal_widget->Close();
350  modal_widget->Close();
351  lock_modal_widget->Close();
352  lock_widget->Close();
353  widget->Close();
354}
355
356TEST_F(ShellTest, IsScreenLocked) {
357  SessionStateDelegate* delegate =
358      Shell::GetInstance()->session_state_delegate();
359  delegate->LockScreen();
360  EXPECT_TRUE(delegate->IsScreenLocked());
361  delegate->UnlockScreen();
362  EXPECT_FALSE(delegate->IsScreenLocked());
363}
364
365TEST_F(ShellTest, LockScreenClosesActiveMenu) {
366  SimpleMenuDelegate menu_delegate;
367  scoped_ptr<ui::SimpleMenuModel> menu_model(
368      new ui::SimpleMenuModel(&menu_delegate));
369  menu_model->AddItem(0, base::ASCIIToUTF16("Menu item"));
370  views::Widget* widget = ash::Shell::GetPrimaryRootWindowController()->
371      wallpaper_controller()->widget();
372  scoped_ptr<views::MenuRunner> menu_runner(
373      new views::MenuRunner(menu_model.get(), views::MenuRunner::CONTEXT_MENU));
374
375  // When MenuRunner runs a nested loop the LockScreenAndVerifyMenuClosed
376  // command will fire, check the menu state and ensure the nested menu loop
377  // is exited so that the test will terminate.
378  base::MessageLoopForUI::current()->PostTask(FROM_HERE,
379      base::Bind(&ShellTest::LockScreenAndVerifyMenuClosed,
380                 base::Unretained(this)));
381
382  EXPECT_EQ(views::MenuRunner::NORMAL_EXIT,
383            menu_runner->RunMenuAt(widget,
384                                   NULL,
385                                   gfx::Rect(),
386                                   views::MENU_ANCHOR_TOPLEFT,
387                                   ui::MENU_SOURCE_MOUSE));
388}
389
390TEST_F(ShellTest, ManagedWindowModeBasics) {
391  // We start with the usual window containers.
392  ExpectAllContainers();
393  // Shelf is visible.
394  ShelfWidget* shelf_widget = Shelf::ForPrimaryDisplay()->shelf_widget();
395  EXPECT_TRUE(shelf_widget->IsVisible());
396  // Shelf is at bottom-left of screen.
397  EXPECT_EQ(0, shelf_widget->GetWindowBoundsInScreen().x());
398  EXPECT_EQ(Shell::GetPrimaryRootWindow()->GetHost()->GetBounds().height(),
399            shelf_widget->GetWindowBoundsInScreen().bottom());
400  // We have a desktop background but not a bare layer.
401  // TODO (antrim): enable once we find out why it fails component build.
402  //  DesktopBackgroundWidgetController* background =
403  //      Shell::GetPrimaryRootWindow()->
404  //          GetProperty(kWindowDesktopComponent);
405  //  EXPECT_TRUE(background);
406  //  EXPECT_TRUE(background->widget());
407  //  EXPECT_FALSE(background->layer());
408
409  // Create a normal window.  It is not maximized.
410  views::Widget::InitParams widget_params(
411      views::Widget::InitParams::TYPE_WINDOW);
412  widget_params.bounds.SetRect(11, 22, 300, 400);
413  views::Widget* widget = CreateTestWindow(widget_params);
414  widget->Show();
415  EXPECT_FALSE(widget->IsMaximized());
416
417  // Clean up.
418  widget->Close();
419}
420
421TEST_F(ShellTest, FullscreenWindowHidesShelf) {
422  ExpectAllContainers();
423
424  // Create a normal window.  It is not maximized.
425  views::Widget::InitParams widget_params(
426      views::Widget::InitParams::TYPE_WINDOW);
427  widget_params.bounds.SetRect(11, 22, 300, 400);
428  views::Widget* widget = CreateTestWindow(widget_params);
429  widget->Show();
430  EXPECT_FALSE(widget->IsMaximized());
431
432  // Shelf defaults to visible.
433  EXPECT_EQ(
434      SHELF_VISIBLE,
435      Shell::GetPrimaryRootWindowController()->
436          GetShelfLayoutManager()->visibility_state());
437
438  // Fullscreen window hides it.
439  widget->SetFullscreen(true);
440  EXPECT_EQ(
441      SHELF_HIDDEN,
442      Shell::GetPrimaryRootWindowController()->
443          GetShelfLayoutManager()->visibility_state());
444
445  // Restoring the window restores it.
446  widget->Restore();
447  EXPECT_EQ(
448      SHELF_VISIBLE,
449      Shell::GetPrimaryRootWindowController()->
450          GetShelfLayoutManager()->visibility_state());
451
452  // Clean up.
453  widget->Close();
454}
455
456// Various assertions around SetShelfAutoHideBehavior() and
457// GetShelfAutoHideBehavior().
458TEST_F(ShellTest, ToggleAutoHide) {
459  scoped_ptr<aura::Window> window(new aura::Window(NULL));
460  window->SetProperty(aura::client::kShowStateKey, ui::SHOW_STATE_NORMAL);
461  window->SetType(ui::wm::WINDOW_TYPE_NORMAL);
462  window->Init(aura::WINDOW_LAYER_TEXTURED);
463  ParentWindowInPrimaryRootWindow(window.get());
464  window->Show();
465  wm::ActivateWindow(window.get());
466
467  Shell* shell = Shell::GetInstance();
468  aura::Window* root_window = Shell::GetPrimaryRootWindow();
469  shell->SetShelfAutoHideBehavior(ash::SHELF_AUTO_HIDE_BEHAVIOR_ALWAYS,
470                                  root_window);
471  EXPECT_EQ(ash::SHELF_AUTO_HIDE_BEHAVIOR_ALWAYS,
472            shell->GetShelfAutoHideBehavior(root_window));
473  shell->SetShelfAutoHideBehavior(ash::SHELF_AUTO_HIDE_BEHAVIOR_NEVER,
474                                  root_window);
475  EXPECT_EQ(ash::SHELF_AUTO_HIDE_BEHAVIOR_NEVER,
476            shell->GetShelfAutoHideBehavior(root_window));
477  window->SetProperty(aura::client::kShowStateKey, ui::SHOW_STATE_MAXIMIZED);
478  EXPECT_EQ(ash::SHELF_AUTO_HIDE_BEHAVIOR_NEVER,
479            shell->GetShelfAutoHideBehavior(root_window));
480  shell->SetShelfAutoHideBehavior(ash::SHELF_AUTO_HIDE_BEHAVIOR_ALWAYS,
481                                  root_window);
482  EXPECT_EQ(ash::SHELF_AUTO_HIDE_BEHAVIOR_ALWAYS,
483            shell->GetShelfAutoHideBehavior(root_window));
484  shell->SetShelfAutoHideBehavior(ash::SHELF_AUTO_HIDE_BEHAVIOR_NEVER,
485                                  root_window);
486  EXPECT_EQ(ash::SHELF_AUTO_HIDE_BEHAVIOR_NEVER,
487            shell->GetShelfAutoHideBehavior(root_window));
488}
489
490// Tests that the cursor-filter is ahead of the drag-drop controller in the
491// pre-target list.
492TEST_F(ShellTest, TestPreTargetHandlerOrder) {
493  Shell* shell = Shell::GetInstance();
494  ui::EventTargetTestApi test_api(shell);
495  test::ShellTestApi shell_test_api(shell);
496
497  const ui::EventHandlerList& handlers = test_api.pre_target_handlers();
498  ui::EventHandlerList::const_iterator cursor_filter =
499      std::find(handlers.begin(), handlers.end(), shell->mouse_cursor_filter());
500  ui::EventHandlerList::const_iterator drag_drop =
501      std::find(handlers.begin(), handlers.end(),
502                shell_test_api.drag_drop_controller());
503  EXPECT_NE(handlers.end(), cursor_filter);
504  EXPECT_NE(handlers.end(), drag_drop);
505  EXPECT_GT(drag_drop, cursor_filter);
506}
507
508// Verifies an EventHandler added to Env gets notified from EventGenerator.
509TEST_F(ShellTest, EnvPreTargetHandler) {
510  ui::test::TestEventHandler event_handler;
511  aura::Env::GetInstance()->AddPreTargetHandler(&event_handler);
512  ui::test::EventGenerator generator(Shell::GetPrimaryRootWindow());
513  generator.MoveMouseBy(1, 1);
514  EXPECT_NE(0, event_handler.num_mouse_events());
515  aura::Env::GetInstance()->RemovePreTargetHandler(&event_handler);
516}
517
518// This verifies WindowObservers are removed when a window is destroyed after
519// the Shell is destroyed. This scenario (aura::Windows being deleted after the
520// Shell) occurs if someone is holding a reference to an unparented Window, as
521// is the case with a RenderWidgetHostViewAura that isn't on screen. As long as
522// everything is ok, we won't crash. If there is a bug, window's destructor will
523// notify some deleted object (say VideoDetector or ActivationController) and
524// this will crash.
525class ShellTest2 : public test::AshTestBase {
526 public:
527  ShellTest2() {}
528  virtual ~ShellTest2() {}
529
530 protected:
531  scoped_ptr<aura::Window> window_;
532
533 private:
534  DISALLOW_COPY_AND_ASSIGN(ShellTest2);
535};
536
537TEST_F(ShellTest2, DontCrashWhenWindowDeleted) {
538  window_.reset(new aura::Window(NULL));
539  window_->Init(aura::WINDOW_LAYER_NOT_DRAWN);
540}
541
542}  // namespace ash
543