Elim cr-checkbox
[chromium-blink-merge.git] / ash / shell_unittest.cc
blobf74fdb0f07a9c3d95ed04bd2796d810f45ce3fdf
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.
5 #include "ash/shell.h"
7 #include <algorithm>
8 #include <vector>
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;
43 namespace ash {
45 namespace {
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));
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(root_window,
85 kShellWindowId_ImeWindowParentContainer));
86 #if defined(OS_CHROMEOS)
87 EXPECT_TRUE(
88 Shell::GetContainer(root_window, kShellWindowId_MouseCursorContainer));
89 #endif
92 class ModalWindow : public views::WidgetDelegateView {
93 public:
94 ModalWindow() {}
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; }
105 private:
106 DISALLOW_COPY_AND_ASSIGN(ModalWindow);
109 class SimpleMenuDelegate : public ui::SimpleMenuModel::Delegate {
110 public:
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 {
120 return false;
123 void ExecuteCommand(int command_id, int event_flags) override {}
125 private:
126 DISALLOW_COPY_AND_ASSIGN(SimpleMenuDelegate);
129 } // namespace
131 class ShellTest : public test::AshTestBase {
132 public:
133 views::Widget* CreateTestWindow(views::Widget::InitParams params) {
134 views::Widget* widget = new views::Widget;
135 params.context = CurrentContext();
136 widget->Init(params);
137 return widget;
140 void TestCreateWindow(views::Widget::InitParams::Type type,
141 bool always_on_top,
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);
147 widget->Show();
149 EXPECT_TRUE(
150 expected_container->Contains(widget->GetNativeWindow()->parent())) <<
151 "TestCreateWindow: type=" << type << ", always_on_top=" <<
152 always_on_top;
154 widget->Close();
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());
174 lock_widget->Show();
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);
213 widget->Show();
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()));
236 widget->Close();
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);
245 widget->Show();
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();
262 widget->Close();
265 class TestModalDialogDelegate : public views::DialogDelegateView {
266 public:
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);
279 widget->Show();
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());
292 lock_widget->Show();
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();
339 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,
370 NULL,
371 gfx::Rect(),
372 views::MENU_ANCHOR_TOPLEFT,
373 ui::MENU_SOURCE_MOUSE));
376 TEST_F(ShellTest, ManagedWindowModeBasics) {
377 if (!SupportsHostWindowResize())
378 return;
380 // We start with the usual window containers.
381 ExpectAllContainers();
382 // Shelf is visible.
383 ShelfWidget* shelf_widget = Shelf::ForPrimaryDisplay()->shelf_widget();
384 EXPECT_TRUE(shelf_widget->IsVisible());
385 // Shelf is at bottom-left of screen.
386 EXPECT_EQ(0, shelf_widget->GetWindowBoundsInScreen().x());
387 EXPECT_EQ(Shell::GetPrimaryRootWindow()->GetHost()->GetBounds().height(),
388 shelf_widget->GetWindowBoundsInScreen().bottom());
389 // We have a desktop background but not a bare layer.
390 // TODO (antrim): enable once we find out why it fails component build.
391 // DesktopBackgroundWidgetController* background =
392 // Shell::GetPrimaryRootWindow()->
393 // GetProperty(kWindowDesktopComponent);
394 // EXPECT_TRUE(background);
395 // EXPECT_TRUE(background->widget());
396 // EXPECT_FALSE(background->layer());
398 // Create a normal window. It is not maximized.
399 views::Widget::InitParams widget_params(
400 views::Widget::InitParams::TYPE_WINDOW);
401 widget_params.bounds.SetRect(11, 22, 300, 400);
402 views::Widget* widget = CreateTestWindow(widget_params);
403 widget->Show();
404 EXPECT_FALSE(widget->IsMaximized());
406 // Clean up.
407 widget->Close();
410 TEST_F(ShellTest, FullscreenWindowHidesShelf) {
411 ExpectAllContainers();
413 // Create a normal window. It is not maximized.
414 views::Widget::InitParams widget_params(
415 views::Widget::InitParams::TYPE_WINDOW);
416 widget_params.bounds.SetRect(11, 22, 300, 400);
417 views::Widget* widget = CreateTestWindow(widget_params);
418 widget->Show();
419 EXPECT_FALSE(widget->IsMaximized());
421 // Shelf defaults to visible.
422 EXPECT_EQ(
423 SHELF_VISIBLE,
424 Shell::GetPrimaryRootWindowController()->
425 GetShelfLayoutManager()->visibility_state());
427 // Fullscreen window hides it.
428 widget->SetFullscreen(true);
429 EXPECT_EQ(
430 SHELF_HIDDEN,
431 Shell::GetPrimaryRootWindowController()->
432 GetShelfLayoutManager()->visibility_state());
434 // Restoring the window restores it.
435 widget->Restore();
436 EXPECT_EQ(
437 SHELF_VISIBLE,
438 Shell::GetPrimaryRootWindowController()->
439 GetShelfLayoutManager()->visibility_state());
441 // Clean up.
442 widget->Close();
445 // Various assertions around SetShelfAutoHideBehavior() and
446 // GetShelfAutoHideBehavior().
447 TEST_F(ShellTest, ToggleAutoHide) {
448 scoped_ptr<aura::Window> window(new aura::Window(NULL));
449 window->SetProperty(aura::client::kShowStateKey, ui::SHOW_STATE_NORMAL);
450 window->SetType(ui::wm::WINDOW_TYPE_NORMAL);
451 window->Init(ui::LAYER_TEXTURED);
452 ParentWindowInPrimaryRootWindow(window.get());
453 window->Show();
454 wm::ActivateWindow(window.get());
456 Shell* shell = Shell::GetInstance();
457 aura::Window* root_window = Shell::GetPrimaryRootWindow();
458 shell->SetShelfAutoHideBehavior(ash::SHELF_AUTO_HIDE_BEHAVIOR_ALWAYS,
459 root_window);
460 EXPECT_EQ(ash::SHELF_AUTO_HIDE_BEHAVIOR_ALWAYS,
461 shell->GetShelfAutoHideBehavior(root_window));
462 shell->SetShelfAutoHideBehavior(ash::SHELF_AUTO_HIDE_BEHAVIOR_NEVER,
463 root_window);
464 EXPECT_EQ(ash::SHELF_AUTO_HIDE_BEHAVIOR_NEVER,
465 shell->GetShelfAutoHideBehavior(root_window));
466 window->SetProperty(aura::client::kShowStateKey, ui::SHOW_STATE_MAXIMIZED);
467 EXPECT_EQ(ash::SHELF_AUTO_HIDE_BEHAVIOR_NEVER,
468 shell->GetShelfAutoHideBehavior(root_window));
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));
479 // Tests that the cursor-filter is ahead of the drag-drop controller in the
480 // pre-target list.
481 TEST_F(ShellTest, TestPreTargetHandlerOrder) {
482 Shell* shell = Shell::GetInstance();
483 ui::EventTargetTestApi test_api(shell);
484 test::ShellTestApi shell_test_api(shell);
486 const ui::EventHandlerList& handlers = test_api.pre_target_handlers();
487 ui::EventHandlerList::const_iterator cursor_filter =
488 std::find(handlers.begin(), handlers.end(), shell->mouse_cursor_filter());
489 ui::EventHandlerList::const_iterator drag_drop =
490 std::find(handlers.begin(), handlers.end(),
491 shell_test_api.drag_drop_controller());
492 EXPECT_NE(handlers.end(), cursor_filter);
493 EXPECT_NE(handlers.end(), drag_drop);
494 EXPECT_GT(drag_drop, cursor_filter);
497 // Verifies an EventHandler added to Env gets notified from EventGenerator.
498 TEST_F(ShellTest, EnvPreTargetHandler) {
499 ui::test::TestEventHandler event_handler;
500 aura::Env::GetInstance()->AddPreTargetHandler(&event_handler);
501 ui::test::EventGenerator generator(Shell::GetPrimaryRootWindow());
502 generator.MoveMouseBy(1, 1);
503 EXPECT_NE(0, event_handler.num_mouse_events());
504 aura::Env::GetInstance()->RemovePreTargetHandler(&event_handler);
507 // This verifies WindowObservers are removed when a window is destroyed after
508 // the Shell is destroyed. This scenario (aura::Windows being deleted after the
509 // Shell) occurs if someone is holding a reference to an unparented Window, as
510 // is the case with a RenderWidgetHostViewAura that isn't on screen. As long as
511 // everything is ok, we won't crash. If there is a bug, window's destructor will
512 // notify some deleted object (say VideoDetector or ActivationController) and
513 // this will crash.
514 class ShellTest2 : public test::AshTestBase {
515 public:
516 ShellTest2() {}
517 ~ShellTest2() override {}
519 protected:
520 scoped_ptr<aura::Window> window_;
522 private:
523 DISALLOW_COPY_AND_ASSIGN(ShellTest2);
526 TEST_F(ShellTest2, DontCrashWhenWindowDeleted) {
527 window_.reset(new aura::Window(NULL));
528 window_->Init(ui::LAYER_NOT_DRAWN);
531 } // namespace ash