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/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/geometry/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(root_window
,
85 kShellWindowId_ImeWindowParentContainer
));
86 #if defined(OS_CHROMEOS)
88 Shell::GetContainer(root_window
, kShellWindowId_MouseCursorContainer
));
92 class ModalWindow
: public views::WidgetDelegateView
{
95 ~ModalWindow() override
{}
97 // Overridden from views::WidgetDelegate:
98 views::View
* GetContentsView() override
{ return this; }
99 bool CanResize() const override
{ return true; }
100 base::string16
GetWindowTitle() const override
{
101 return base::ASCIIToUTF16("Modal Window");
103 ui::ModalType
GetModalType() const override
{ return ui::MODAL_TYPE_SYSTEM
; }
106 DISALLOW_COPY_AND_ASSIGN(ModalWindow
);
109 class SimpleMenuDelegate
: public ui::SimpleMenuModel::Delegate
{
111 SimpleMenuDelegate() {}
112 ~SimpleMenuDelegate() override
{}
114 bool IsCommandIdChecked(int command_id
) const override
{ return false; }
116 bool IsCommandIdEnabled(int command_id
) const override
{ return true; }
118 bool GetAcceleratorForCommandId(int command_id
,
119 ui::Accelerator
* accelerator
) override
{
123 void ExecuteCommand(int command_id
, int event_flags
) override
{}
126 DISALLOW_COPY_AND_ASSIGN(SimpleMenuDelegate
);
131 class ShellTest
: public test::AshTestBase
{
133 views::Widget
* CreateTestWindow(views::Widget::InitParams params
) {
134 views::Widget
* widget
= new views::Widget
;
135 params
.context
= CurrentContext();
136 widget
->Init(params
);
140 void TestCreateWindow(views::Widget::InitParams::Type type
,
142 aura::Window
* expected_container
) {
143 views::Widget::InitParams
widget_params(type
);
144 widget_params
.keep_on_top
= always_on_top
;
146 views::Widget
* widget
= CreateTestWindow(widget_params
);
150 expected_container
->Contains(widget
->GetNativeWindow()->parent())) <<
151 "TestCreateWindow: type=" << type
<< ", always_on_top=" <<
157 void LockScreenAndVerifyMenuClosed() {
158 // Verify a menu is open before locking.
159 views::MenuController
* menu_controller
=
160 views::MenuController::GetActiveInstance();
161 DCHECK(menu_controller
);
162 EXPECT_EQ(views::MenuController::EXIT_NONE
, menu_controller
->exit_type());
164 // Create a LockScreen window.
165 views::Widget::InitParams
widget_params(
166 views::Widget::InitParams::TYPE_WINDOW
);
167 SessionStateDelegate
* delegate
=
168 Shell::GetInstance()->session_state_delegate();
169 delegate
->LockScreen();
170 views::Widget
* lock_widget
= CreateTestWindow(widget_params
);
171 ash::Shell::GetContainer(Shell::GetPrimaryRootWindow(),
172 ash::kShellWindowId_LockScreenContainer
)
173 ->AddChild(lock_widget
->GetNativeView());
175 EXPECT_TRUE(delegate
->IsScreenLocked());
176 EXPECT_TRUE(lock_widget
->GetNativeView()->HasFocus());
178 // Verify menu is closed.
179 EXPECT_NE(views::MenuController::EXIT_NONE
, menu_controller
->exit_type());
180 lock_widget
->Close();
181 delegate
->UnlockScreen();
183 // In case the menu wasn't closed, cancel the menu to exit the nested menu
184 // run loop so that the test will not time out.
185 menu_controller
->CancelAll();
189 TEST_F(ShellTest
, CreateWindow
) {
190 // Normal window should be created in default container.
191 TestCreateWindow(views::Widget::InitParams::TYPE_WINDOW
,
192 false, // always_on_top
193 GetDefaultContainer());
194 TestCreateWindow(views::Widget::InitParams::TYPE_POPUP
,
195 false, // always_on_top
196 GetDefaultContainer());
198 // Always-on-top window and popup are created in always-on-top container.
199 TestCreateWindow(views::Widget::InitParams::TYPE_WINDOW
,
200 true, // always_on_top
201 GetAlwaysOnTopContainer());
202 TestCreateWindow(views::Widget::InitParams::TYPE_POPUP
,
203 true, // always_on_top
204 GetAlwaysOnTopContainer());
207 TEST_F(ShellTest
, ChangeAlwaysOnTop
) {
208 views::Widget::InitParams
widget_params(
209 views::Widget::InitParams::TYPE_WINDOW
);
211 // Creates a normal window
212 views::Widget
* widget
= CreateTestWindow(widget_params
);
215 // It should be in default container.
216 EXPECT_TRUE(GetDefaultContainer()->Contains(
217 widget
->GetNativeWindow()->parent()));
219 // Flip always-on-top flag.
220 widget
->SetAlwaysOnTop(true);
221 // And it should in always on top container now.
222 EXPECT_EQ(GetAlwaysOnTopContainer(), widget
->GetNativeWindow()->parent());
224 // Flip always-on-top flag.
225 widget
->SetAlwaysOnTop(false);
226 // It should go back to default container.
227 EXPECT_TRUE(GetDefaultContainer()->Contains(
228 widget
->GetNativeWindow()->parent()));
230 // Set the same always-on-top flag again.
231 widget
->SetAlwaysOnTop(false);
232 // Should have no effect and we are still in the default container.
233 EXPECT_TRUE(GetDefaultContainer()->Contains(
234 widget
->GetNativeWindow()->parent()));
239 TEST_F(ShellTest
, CreateModalWindow
) {
240 views::Widget::InitParams
widget_params(
241 views::Widget::InitParams::TYPE_WINDOW
);
243 // Create a normal window.
244 views::Widget
* widget
= CreateTestWindow(widget_params
);
247 // It should be in default container.
248 EXPECT_TRUE(GetDefaultContainer()->Contains(
249 widget
->GetNativeWindow()->parent()));
251 // Create a modal window.
252 views::Widget
* modal_widget
= views::Widget::CreateWindowWithParent(
253 new ModalWindow(), widget
->GetNativeView());
254 modal_widget
->Show();
256 // It should be in modal container.
257 aura::Window
* modal_container
= Shell::GetContainer(
258 Shell::GetPrimaryRootWindow(), kShellWindowId_SystemModalContainer
);
259 EXPECT_EQ(modal_container
, modal_widget
->GetNativeWindow()->parent());
261 modal_widget
->Close();
265 class TestModalDialogDelegate
: public views::DialogDelegateView
{
267 TestModalDialogDelegate() {}
269 // Overridden from views::WidgetDelegate:
270 ui::ModalType
GetModalType() const override
{ return ui::MODAL_TYPE_SYSTEM
; }
273 TEST_F(ShellTest
, CreateLockScreenModalWindow
) {
274 views::Widget::InitParams
widget_params(
275 views::Widget::InitParams::TYPE_WINDOW
);
277 // Create a normal window.
278 views::Widget
* widget
= CreateTestWindow(widget_params
);
280 EXPECT_TRUE(widget
->GetNativeView()->HasFocus());
282 // It should be in default container.
283 EXPECT_TRUE(GetDefaultContainer()->Contains(
284 widget
->GetNativeWindow()->parent()));
286 Shell::GetInstance()->session_state_delegate()->LockScreen();
287 // Create a LockScreen window.
288 views::Widget
* lock_widget
= CreateTestWindow(widget_params
);
289 ash::Shell::GetContainer(Shell::GetPrimaryRootWindow(),
290 ash::kShellWindowId_LockScreenContainer
)
291 ->AddChild(lock_widget
->GetNativeView());
293 EXPECT_TRUE(lock_widget
->GetNativeView()->HasFocus());
295 // It should be in LockScreen container.
296 aura::Window
* lock_screen
= Shell::GetContainer(
297 Shell::GetPrimaryRootWindow(), ash::kShellWindowId_LockScreenContainer
);
298 EXPECT_EQ(lock_screen
, lock_widget
->GetNativeWindow()->parent());
300 // Create a modal window with a lock window as parent.
301 views::Widget
* lock_modal_widget
= views::Widget::CreateWindowWithParent(
302 new ModalWindow(), lock_widget
->GetNativeView());
303 lock_modal_widget
->Show();
304 EXPECT_TRUE(lock_modal_widget
->GetNativeView()->HasFocus());
306 // It should be in LockScreen modal container.
307 aura::Window
* lock_modal_container
=
308 Shell::GetContainer(Shell::GetPrimaryRootWindow(),
309 ash::kShellWindowId_LockSystemModalContainer
);
310 EXPECT_EQ(lock_modal_container
,
311 lock_modal_widget
->GetNativeWindow()->parent());
313 // Create a modal window with a normal window as parent.
314 views::Widget
* modal_widget
= views::Widget::CreateWindowWithParent(
315 new ModalWindow(), widget
->GetNativeView());
316 modal_widget
->Show();
317 // Window on lock screen shouldn't lost focus.
318 EXPECT_FALSE(modal_widget
->GetNativeView()->HasFocus());
319 EXPECT_TRUE(lock_modal_widget
->GetNativeView()->HasFocus());
321 // It should be in non-LockScreen modal container.
322 aura::Window
* modal_container
= Shell::GetContainer(
323 Shell::GetPrimaryRootWindow(), ash::kShellWindowId_SystemModalContainer
);
324 EXPECT_EQ(modal_container
, modal_widget
->GetNativeWindow()->parent());
326 // Modal dialog without parent, caused crash see crbug.com/226141
327 views::Widget
* modal_dialog
= views::DialogDelegate::CreateDialogWidget(
328 new TestModalDialogDelegate(), CurrentContext(), NULL
);
330 modal_dialog
->Show();
331 EXPECT_FALSE(modal_dialog
->GetNativeView()->HasFocus());
332 EXPECT_TRUE(lock_modal_widget
->GetNativeView()->HasFocus());
334 modal_dialog
->Close();
335 modal_widget
->Close();
336 modal_widget
->Close();
337 lock_modal_widget
->Close();
338 lock_widget
->Close();
342 TEST_F(ShellTest
, IsScreenLocked
) {
343 SessionStateDelegate
* delegate
=
344 Shell::GetInstance()->session_state_delegate();
345 delegate
->LockScreen();
346 EXPECT_TRUE(delegate
->IsScreenLocked());
347 delegate
->UnlockScreen();
348 EXPECT_FALSE(delegate
->IsScreenLocked());
351 TEST_F(ShellTest
, LockScreenClosesActiveMenu
) {
352 SimpleMenuDelegate menu_delegate
;
353 scoped_ptr
<ui::SimpleMenuModel
> menu_model(
354 new ui::SimpleMenuModel(&menu_delegate
));
355 menu_model
->AddItem(0, base::ASCIIToUTF16("Menu item"));
356 views::Widget
* widget
= ash::Shell::GetPrimaryRootWindowController()->
357 wallpaper_controller()->widget();
358 scoped_ptr
<views::MenuRunner
> menu_runner(
359 new views::MenuRunner(menu_model
.get(), views::MenuRunner::CONTEXT_MENU
));
361 // When MenuRunner runs a nested loop the LockScreenAndVerifyMenuClosed
362 // command will fire, check the menu state and ensure the nested menu loop
363 // is exited so that the test will terminate.
364 base::MessageLoopForUI::current()->PostTask(FROM_HERE
,
365 base::Bind(&ShellTest::LockScreenAndVerifyMenuClosed
,
366 base::Unretained(this)));
368 EXPECT_EQ(views::MenuRunner::NORMAL_EXIT
,
369 menu_runner
->RunMenuAt(widget
,
372 views::MENU_ANCHOR_TOPLEFT
,
373 ui::MENU_SOURCE_MOUSE
));
376 TEST_F(ShellTest
, ManagedWindowModeBasics
) {
377 // We start with the usual window containers.
378 ExpectAllContainers();
380 ShelfWidget
* shelf_widget
= Shelf::ForPrimaryDisplay()->shelf_widget();
381 EXPECT_TRUE(shelf_widget
->IsVisible());
382 // Shelf is at bottom-left of screen.
383 EXPECT_EQ(0, shelf_widget
->GetWindowBoundsInScreen().x());
384 EXPECT_EQ(Shell::GetPrimaryRootWindow()->GetHost()->GetBounds().height(),
385 shelf_widget
->GetWindowBoundsInScreen().bottom());
386 // We have a desktop background but not a bare layer.
387 // TODO (antrim): enable once we find out why it fails component build.
388 // DesktopBackgroundWidgetController* background =
389 // Shell::GetPrimaryRootWindow()->
390 // GetProperty(kWindowDesktopComponent);
391 // EXPECT_TRUE(background);
392 // EXPECT_TRUE(background->widget());
393 // EXPECT_FALSE(background->layer());
395 // Create a normal window. It is not maximized.
396 views::Widget::InitParams
widget_params(
397 views::Widget::InitParams::TYPE_WINDOW
);
398 widget_params
.bounds
.SetRect(11, 22, 300, 400);
399 views::Widget
* widget
= CreateTestWindow(widget_params
);
401 EXPECT_FALSE(widget
->IsMaximized());
407 TEST_F(ShellTest
, FullscreenWindowHidesShelf
) {
408 ExpectAllContainers();
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());
418 // Shelf defaults to visible.
421 Shell::GetPrimaryRootWindowController()->
422 GetShelfLayoutManager()->visibility_state());
424 // Fullscreen window hides it.
425 widget
->SetFullscreen(true);
428 Shell::GetPrimaryRootWindowController()->
429 GetShelfLayoutManager()->visibility_state());
431 // Restoring the window restores it.
435 Shell::GetPrimaryRootWindowController()->
436 GetShelfLayoutManager()->visibility_state());
442 // Various assertions around SetShelfAutoHideBehavior() and
443 // GetShelfAutoHideBehavior().
444 TEST_F(ShellTest
, ToggleAutoHide
) {
445 scoped_ptr
<aura::Window
> window(new aura::Window(NULL
));
446 window
->SetProperty(aura::client::kShowStateKey
, ui::SHOW_STATE_NORMAL
);
447 window
->SetType(ui::wm::WINDOW_TYPE_NORMAL
);
448 window
->Init(ui::LAYER_TEXTURED
);
449 ParentWindowInPrimaryRootWindow(window
.get());
451 wm::ActivateWindow(window
.get());
453 Shell
* shell
= Shell::GetInstance();
454 aura::Window
* root_window
= Shell::GetPrimaryRootWindow();
455 shell
->SetShelfAutoHideBehavior(ash::SHELF_AUTO_HIDE_BEHAVIOR_ALWAYS
,
457 EXPECT_EQ(ash::SHELF_AUTO_HIDE_BEHAVIOR_ALWAYS
,
458 shell
->GetShelfAutoHideBehavior(root_window
));
459 shell
->SetShelfAutoHideBehavior(ash::SHELF_AUTO_HIDE_BEHAVIOR_NEVER
,
461 EXPECT_EQ(ash::SHELF_AUTO_HIDE_BEHAVIOR_NEVER
,
462 shell
->GetShelfAutoHideBehavior(root_window
));
463 window
->SetProperty(aura::client::kShowStateKey
, ui::SHOW_STATE_MAXIMIZED
);
464 EXPECT_EQ(ash::SHELF_AUTO_HIDE_BEHAVIOR_NEVER
,
465 shell
->GetShelfAutoHideBehavior(root_window
));
466 shell
->SetShelfAutoHideBehavior(ash::SHELF_AUTO_HIDE_BEHAVIOR_ALWAYS
,
468 EXPECT_EQ(ash::SHELF_AUTO_HIDE_BEHAVIOR_ALWAYS
,
469 shell
->GetShelfAutoHideBehavior(root_window
));
470 shell
->SetShelfAutoHideBehavior(ash::SHELF_AUTO_HIDE_BEHAVIOR_NEVER
,
472 EXPECT_EQ(ash::SHELF_AUTO_HIDE_BEHAVIOR_NEVER
,
473 shell
->GetShelfAutoHideBehavior(root_window
));
476 // Tests that the cursor-filter is ahead of the drag-drop controller in the
478 TEST_F(ShellTest
, TestPreTargetHandlerOrder
) {
479 Shell
* shell
= Shell::GetInstance();
480 ui::EventTargetTestApi
test_api(shell
);
481 test::ShellTestApi
shell_test_api(shell
);
483 const ui::EventHandlerList
& handlers
= test_api
.pre_target_handlers();
484 ui::EventHandlerList::const_iterator cursor_filter
=
485 std::find(handlers
.begin(), handlers
.end(), shell
->mouse_cursor_filter());
486 ui::EventHandlerList::const_iterator drag_drop
=
487 std::find(handlers
.begin(), handlers
.end(),
488 shell_test_api
.drag_drop_controller());
489 EXPECT_NE(handlers
.end(), cursor_filter
);
490 EXPECT_NE(handlers
.end(), drag_drop
);
491 EXPECT_GT(drag_drop
, cursor_filter
);
494 // Verifies an EventHandler added to Env gets notified from EventGenerator.
495 TEST_F(ShellTest
, EnvPreTargetHandler
) {
496 ui::test::TestEventHandler event_handler
;
497 aura::Env::GetInstance()->AddPreTargetHandler(&event_handler
);
498 ui::test::EventGenerator
generator(Shell::GetPrimaryRootWindow());
499 generator
.MoveMouseBy(1, 1);
500 EXPECT_NE(0, event_handler
.num_mouse_events());
501 aura::Env::GetInstance()->RemovePreTargetHandler(&event_handler
);
504 // This verifies WindowObservers are removed when a window is destroyed after
505 // the Shell is destroyed. This scenario (aura::Windows being deleted after the
506 // Shell) occurs if someone is holding a reference to an unparented Window, as
507 // is the case with a RenderWidgetHostViewAura that isn't on screen. As long as
508 // everything is ok, we won't crash. If there is a bug, window's destructor will
509 // notify some deleted object (say VideoDetector or ActivationController) and
511 class ShellTest2
: public test::AshTestBase
{
514 ~ShellTest2() override
{}
517 scoped_ptr
<aura::Window
> window_
;
520 DISALLOW_COPY_AND_ASSIGN(ShellTest2
);
523 TEST_F(ShellTest2
, DontCrashWhenWindowDeleted
) {
524 window_
.reset(new aura::Window(NULL
));
525 window_
->Init(ui::LAYER_NOT_DRAWN
);