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