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.
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/test/event_generator.h"
29 #include "ui/aura/window.h"
30 #include "ui/aura/window_event_dispatcher.h"
31 #include "ui/base/models/simple_menu_model.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"
41 using aura::RootWindow
;
47 aura::Window
* GetDefaultContainer() {
48 return Shell::GetContainer(Shell::GetPrimaryRootWindow(),
49 kShellWindowId_DefaultContainer
);
52 aura::Window
* GetAlwaysOnTopContainer() {
53 return Shell::GetContainer(Shell::GetPrimaryRootWindow(),
54 kShellWindowId_AlwaysOnTopContainer
);
57 // Expect ALL the containers!
58 void ExpectAllContainers() {
59 aura::Window
* root_window
= Shell::GetPrimaryRootWindow();
60 EXPECT_TRUE(Shell::GetContainer(root_window
,
61 kShellWindowId_DesktopBackgroundContainer
));
63 Shell::GetContainer(root_window
, kShellWindowId_DefaultContainer
));
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
));
69 Shell::GetContainer(root_window
, kShellWindowId_SystemModalContainer
));
70 EXPECT_TRUE(Shell::GetContainer(
71 root_window
, kShellWindowId_LockScreenBackgroundContainer
));
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
));
81 Shell::GetContainer(root_window
, kShellWindowId_SettingBubbleContainer
));
83 Shell::GetContainer(root_window
, kShellWindowId_OverlayContainer
));
84 EXPECT_TRUE(Shell::GetContainer(
85 root_window
, kShellWindowId_VirtualKeyboardParentContainer
));
86 #if defined(OS_CHROMEOS)
88 Shell::GetContainer(root_window
, kShellWindowId_MouseCursorContainer
));
92 class ModalWindow
: public views::WidgetDelegateView
{
95 virtual ~ModalWindow() {}
97 // Overridden from views::WidgetDelegate:
98 virtual views::View
* GetContentsView() OVERRIDE
{
101 virtual bool CanResize() const OVERRIDE
{
104 virtual base::string16
GetWindowTitle() const OVERRIDE
{
105 return base::ASCIIToUTF16("Modal Window");
107 virtual ui::ModalType
GetModalType() const OVERRIDE
{
108 return ui::MODAL_TYPE_SYSTEM
;
112 DISALLOW_COPY_AND_ASSIGN(ModalWindow
);
115 class SimpleMenuDelegate
: public ui::SimpleMenuModel::Delegate
{
117 SimpleMenuDelegate() {}
118 virtual ~SimpleMenuDelegate() {}
120 virtual bool IsCommandIdChecked(int command_id
) const OVERRIDE
{
124 virtual bool IsCommandIdEnabled(int command_id
) const OVERRIDE
{
128 virtual bool GetAcceleratorForCommandId(
130 ui::Accelerator
* accelerator
) OVERRIDE
{
134 virtual void ExecuteCommand(int command_id
, int event_flags
) OVERRIDE
{
138 DISALLOW_COPY_AND_ASSIGN(SimpleMenuDelegate
);
143 class ShellTest
: public test::AshTestBase
{
145 views::Widget
* CreateTestWindow(views::Widget::InitParams params
) {
146 views::Widget
* widget
= new views::Widget
;
147 params
.context
= CurrentContext();
148 widget
->Init(params
);
152 void TestCreateWindow(views::Widget::InitParams::Type type
,
154 aura::Window
* expected_container
) {
155 views::Widget::InitParams
widget_params(type
);
156 widget_params
.keep_on_top
= always_on_top
;
158 views::Widget
* widget
= CreateTestWindow(widget_params
);
162 expected_container
->Contains(widget
->GetNativeWindow()->parent())) <<
163 "TestCreateWindow: type=" << type
<< ", always_on_top=" <<
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());
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());
187 EXPECT_TRUE(delegate
->IsScreenLocked());
188 EXPECT_TRUE(lock_widget
->GetNativeView()->HasFocus());
190 // Verify menu is closed.
191 EXPECT_NE(views::MenuController::EXIT_NONE
, menu_controller
->exit_type());
192 lock_widget
->Close();
193 delegate
->UnlockScreen();
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();
201 TEST_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());
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());
219 TEST_F(ShellTest
, ChangeAlwaysOnTop
) {
220 views::Widget::InitParams
widget_params(
221 views::Widget::InitParams::TYPE_WINDOW
);
223 // Creates a normal window
224 views::Widget
* widget
= CreateTestWindow(widget_params
);
227 // It should be in default container.
228 EXPECT_TRUE(GetDefaultContainer()->Contains(
229 widget
->GetNativeWindow()->parent()));
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());
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()));
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()));
251 TEST_F(ShellTest
, CreateModalWindow
) {
252 views::Widget::InitParams
widget_params(
253 views::Widget::InitParams::TYPE_WINDOW
);
255 // Create a normal window.
256 views::Widget
* widget
= CreateTestWindow(widget_params
);
259 // It should be in default container.
260 EXPECT_TRUE(GetDefaultContainer()->Contains(
261 widget
->GetNativeWindow()->parent()));
263 // Create a modal window.
264 views::Widget
* modal_widget
= views::Widget::CreateWindowWithParent(
265 new ModalWindow(), widget
->GetNativeView());
266 modal_widget
->Show();
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());
273 modal_widget
->Close();
277 class TestModalDialogDelegate
: public views::DialogDelegateView
{
279 TestModalDialogDelegate() {}
281 // Overridden from views::WidgetDelegate:
282 virtual ui::ModalType
GetModalType() const OVERRIDE
{
283 return ui::MODAL_TYPE_SYSTEM
;
287 TEST_F(ShellTest
, CreateLockScreenModalWindow
) {
288 views::Widget::InitParams
widget_params(
289 views::Widget::InitParams::TYPE_WINDOW
);
291 // Create a normal window.
292 views::Widget
* widget
= CreateTestWindow(widget_params
);
294 EXPECT_TRUE(widget
->GetNativeView()->HasFocus());
296 // It should be in default container.
297 EXPECT_TRUE(GetDefaultContainer()->Contains(
298 widget
->GetNativeWindow()->parent()));
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());
307 EXPECT_TRUE(lock_widget
->GetNativeView()->HasFocus());
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());
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());
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());
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());
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());
340 // Modal dialog without parent, caused crash see crbug.com/226141
341 views::Widget
* modal_dialog
= views::DialogDelegate::CreateDialogWidget(
342 new TestModalDialogDelegate(), CurrentContext(), NULL
);
344 modal_dialog
->Show();
345 EXPECT_FALSE(modal_dialog
->GetNativeView()->HasFocus());
346 EXPECT_TRUE(lock_modal_widget
->GetNativeView()->HasFocus());
348 modal_dialog
->Close();
349 modal_widget
->Close();
350 modal_widget
->Close();
351 lock_modal_widget
->Close();
352 lock_widget
->Close();
356 TEST_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());
365 TEST_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()));
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)));
382 EXPECT_EQ(views::MenuRunner::NORMAL_EXIT
,
383 menu_runner
->RunMenuAt(widget
,
386 views::MENU_ANCHOR_TOPLEFT
,
387 ui::MENU_SOURCE_MOUSE
,
388 views::MenuRunner::CONTEXT_MENU
));
391 TEST_F(ShellTest
, ManagedWindowModeBasics
) {
392 // We start with the usual window containers.
393 ExpectAllContainers();
395 ShelfWidget
* shelf_widget
= Shelf::ForPrimaryDisplay()->shelf_widget();
396 EXPECT_TRUE(shelf_widget
->IsVisible());
397 // Shelf is at bottom-left of screen.
398 EXPECT_EQ(0, shelf_widget
->GetWindowBoundsInScreen().x());
399 EXPECT_EQ(Shell::GetPrimaryRootWindow()->GetHost()->GetBounds().height(),
400 shelf_widget
->GetWindowBoundsInScreen().bottom());
401 // We have a desktop background but not a bare layer.
402 // TODO (antrim): enable once we find out why it fails component build.
403 // DesktopBackgroundWidgetController* background =
404 // Shell::GetPrimaryRootWindow()->
405 // GetProperty(kWindowDesktopComponent);
406 // EXPECT_TRUE(background);
407 // EXPECT_TRUE(background->widget());
408 // EXPECT_FALSE(background->layer());
410 // Create a normal window. It is not maximized.
411 views::Widget::InitParams
widget_params(
412 views::Widget::InitParams::TYPE_WINDOW
);
413 widget_params
.bounds
.SetRect(11, 22, 300, 400);
414 views::Widget
* widget
= CreateTestWindow(widget_params
);
416 EXPECT_FALSE(widget
->IsMaximized());
422 TEST_F(ShellTest
, FullscreenWindowHidesShelf
) {
423 ExpectAllContainers();
425 // Create a normal window. It is not maximized.
426 views::Widget::InitParams
widget_params(
427 views::Widget::InitParams::TYPE_WINDOW
);
428 widget_params
.bounds
.SetRect(11, 22, 300, 400);
429 views::Widget
* widget
= CreateTestWindow(widget_params
);
431 EXPECT_FALSE(widget
->IsMaximized());
433 // Shelf defaults to visible.
436 Shell::GetPrimaryRootWindowController()->
437 GetShelfLayoutManager()->visibility_state());
439 // Fullscreen window hides it.
440 widget
->SetFullscreen(true);
443 Shell::GetPrimaryRootWindowController()->
444 GetShelfLayoutManager()->visibility_state());
446 // Restoring the window restores it.
450 Shell::GetPrimaryRootWindowController()->
451 GetShelfLayoutManager()->visibility_state());
457 // Various assertions around SetShelfAutoHideBehavior() and
458 // GetShelfAutoHideBehavior().
459 TEST_F(ShellTest
, ToggleAutoHide
) {
460 scoped_ptr
<aura::Window
> window(new aura::Window(NULL
));
461 window
->SetProperty(aura::client::kShowStateKey
, ui::SHOW_STATE_NORMAL
);
462 window
->SetType(ui::wm::WINDOW_TYPE_NORMAL
);
463 window
->Init(aura::WINDOW_LAYER_TEXTURED
);
464 ParentWindowInPrimaryRootWindow(window
.get());
466 wm::ActivateWindow(window
.get());
468 Shell
* shell
= Shell::GetInstance();
469 aura::Window
* root_window
= Shell::GetPrimaryRootWindow();
470 shell
->SetShelfAutoHideBehavior(ash::SHELF_AUTO_HIDE_BEHAVIOR_ALWAYS
,
472 EXPECT_EQ(ash::SHELF_AUTO_HIDE_BEHAVIOR_ALWAYS
,
473 shell
->GetShelfAutoHideBehavior(root_window
));
474 shell
->SetShelfAutoHideBehavior(ash::SHELF_AUTO_HIDE_BEHAVIOR_NEVER
,
476 EXPECT_EQ(ash::SHELF_AUTO_HIDE_BEHAVIOR_NEVER
,
477 shell
->GetShelfAutoHideBehavior(root_window
));
478 window
->SetProperty(aura::client::kShowStateKey
, ui::SHOW_STATE_MAXIMIZED
);
479 EXPECT_EQ(ash::SHELF_AUTO_HIDE_BEHAVIOR_NEVER
,
480 shell
->GetShelfAutoHideBehavior(root_window
));
481 shell
->SetShelfAutoHideBehavior(ash::SHELF_AUTO_HIDE_BEHAVIOR_ALWAYS
,
483 EXPECT_EQ(ash::SHELF_AUTO_HIDE_BEHAVIOR_ALWAYS
,
484 shell
->GetShelfAutoHideBehavior(root_window
));
485 shell
->SetShelfAutoHideBehavior(ash::SHELF_AUTO_HIDE_BEHAVIOR_NEVER
,
487 EXPECT_EQ(ash::SHELF_AUTO_HIDE_BEHAVIOR_NEVER
,
488 shell
->GetShelfAutoHideBehavior(root_window
));
491 // Tests that the cursor-filter is ahead of the drag-drop controller in the
493 TEST_F(ShellTest
, TestPreTargetHandlerOrder
) {
494 Shell
* shell
= Shell::GetInstance();
495 ui::EventTargetTestApi
test_api(shell
);
496 test::ShellTestApi
shell_test_api(shell
);
498 const ui::EventHandlerList
& handlers
= test_api
.pre_target_handlers();
499 ui::EventHandlerList::const_iterator cursor_filter
=
500 std::find(handlers
.begin(), handlers
.end(), shell
->mouse_cursor_filter());
501 ui::EventHandlerList::const_iterator drag_drop
=
502 std::find(handlers
.begin(), handlers
.end(),
503 shell_test_api
.drag_drop_controller());
504 EXPECT_NE(handlers
.end(), cursor_filter
);
505 EXPECT_NE(handlers
.end(), drag_drop
);
506 EXPECT_GT(drag_drop
, cursor_filter
);
509 // Verifies an EventHandler added to Env gets notified from EventGenerator.
510 TEST_F(ShellTest
, EnvPreTargetHandler
) {
511 ui::test::TestEventHandler event_handler
;
512 aura::Env::GetInstance()->AddPreTargetHandler(&event_handler
);
513 aura::test::EventGenerator
generator(Shell::GetPrimaryRootWindow());
514 generator
.MoveMouseBy(1, 1);
515 EXPECT_NE(0, event_handler
.num_mouse_events());
516 aura::Env::GetInstance()->RemovePreTargetHandler(&event_handler
);
519 // This verifies WindowObservers are removed when a window is destroyed after
520 // the Shell is destroyed. This scenario (aura::Windows being deleted after the
521 // Shell) occurs if someone is holding a reference to an unparented Window, as
522 // is the case with a RenderWidgetHostViewAura that isn't on screen. As long as
523 // everything is ok, we won't crash. If there is a bug, window's destructor will
524 // notify some deleted object (say VideoDetector or ActivationController) and
526 class ShellTest2
: public test::AshTestBase
{
529 virtual ~ShellTest2() {}
532 scoped_ptr
<aura::Window
> window_
;
535 DISALLOW_COPY_AND_ASSIGN(ShellTest2
);
538 TEST_F(ShellTest2
, DontCrashWhenWindowDeleted
) {
539 window_
.reset(new aura::Window(NULL
));
540 window_
->Init(aura::WINDOW_LAYER_NOT_DRAWN
);