Replace the deprecated __attribute__((no_address_safety_analysis))
[chromium-blink-merge.git] / ash / shell_unittest.cc
blobc41163b313051e0941e735d84326d24c22dfe51e
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/launcher/launcher.h"
15 #include "ash/root_window_controller.h"
16 #include "ash/session_state_delegate.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/root_window.h"
29 #include "ui/aura/test/event_generator.h"
30 #include "ui/aura/test/test_event_handler.h"
31 #include "ui/aura/window.h"
32 #include "ui/base/models/simple_menu_model.h"
33 #include "ui/gfx/size.h"
34 #include "ui/views/controls/menu/menu_controller.h"
35 #include "ui/views/controls/menu/menu_runner.h"
36 #include "ui/views/widget/widget.h"
37 #include "ui/views/widget/widget_delegate.h"
38 #include "ui/views/window/dialog_delegate.h"
40 using aura::RootWindow;
42 namespace ash {
44 namespace {
46 aura::Window* GetDefaultContainer() {
47 return Shell::GetContainer(
48 Shell::GetPrimaryRootWindow(),
49 internal::kShellWindowId_DefaultContainer);
52 aura::Window* GetAlwaysOnTopContainer() {
53 return Shell::GetContainer(
54 Shell::GetPrimaryRootWindow(),
55 internal::kShellWindowId_AlwaysOnTopContainer);
58 // Expect ALL the containers!
59 void ExpectAllContainers() {
60 aura::Window* root_window = Shell::GetPrimaryRootWindow();
61 EXPECT_TRUE(Shell::GetContainer(
62 root_window, internal::kShellWindowId_DesktopBackgroundContainer));
63 EXPECT_TRUE(Shell::GetContainer(
64 root_window, internal::kShellWindowId_DefaultContainer));
65 EXPECT_TRUE(Shell::GetContainer(
66 root_window, internal::kShellWindowId_AlwaysOnTopContainer));
67 EXPECT_TRUE(Shell::GetContainer(
68 root_window, internal::kShellWindowId_PanelContainer));
69 EXPECT_TRUE(Shell::GetContainer(
70 root_window, internal::kShellWindowId_ShelfContainer));
71 EXPECT_TRUE(Shell::GetContainer(
72 root_window, internal::kShellWindowId_SystemModalContainer));
73 EXPECT_TRUE(Shell::GetContainer(
74 root_window, internal::kShellWindowId_LockScreenBackgroundContainer));
75 EXPECT_TRUE(Shell::GetContainer(
76 root_window, internal::kShellWindowId_LockScreenContainer));
77 EXPECT_TRUE(Shell::GetContainer(
78 root_window, internal::kShellWindowId_LockSystemModalContainer));
79 EXPECT_TRUE(Shell::GetContainer(
80 root_window, internal::kShellWindowId_StatusContainer));
81 EXPECT_TRUE(Shell::GetContainer(
82 root_window, internal::kShellWindowId_MenuContainer));
83 EXPECT_TRUE(Shell::GetContainer(
84 root_window, internal::kShellWindowId_DragImageAndTooltipContainer));
85 EXPECT_TRUE(Shell::GetContainer(
86 root_window, internal::kShellWindowId_SettingBubbleContainer));
87 EXPECT_TRUE(Shell::GetContainer(
88 root_window, internal::kShellWindowId_OverlayContainer));
91 class ModalWindow : public views::WidgetDelegateView {
92 public:
93 ModalWindow() {}
94 virtual ~ModalWindow() {}
96 // Overridden from views::WidgetDelegate:
97 virtual views::View* GetContentsView() OVERRIDE {
98 return this;
100 virtual bool CanResize() const OVERRIDE {
101 return true;
103 virtual base::string16 GetWindowTitle() const OVERRIDE {
104 return ASCIIToUTF16("Modal Window");
106 virtual ui::ModalType GetModalType() const OVERRIDE {
107 return ui::MODAL_TYPE_SYSTEM;
110 private:
111 DISALLOW_COPY_AND_ASSIGN(ModalWindow);
114 class SimpleMenuDelegate : public ui::SimpleMenuModel::Delegate {
115 public:
116 SimpleMenuDelegate() {}
117 virtual ~SimpleMenuDelegate() {}
119 virtual bool IsCommandIdChecked(int command_id) const OVERRIDE {
120 return false;
123 virtual bool IsCommandIdEnabled(int command_id) const OVERRIDE {
124 return true;
127 virtual bool GetAcceleratorForCommandId(
128 int command_id,
129 ui::Accelerator* accelerator) OVERRIDE {
130 return false;
133 virtual void ExecuteCommand(int command_id, int event_flags) OVERRIDE {
136 private:
137 DISALLOW_COPY_AND_ASSIGN(SimpleMenuDelegate);
140 } // namespace
142 class ShellTest : public test::AshTestBase {
143 public:
144 views::Widget* CreateTestWindow(views::Widget::InitParams params) {
145 views::Widget* widget = new views::Widget;
146 params.context = CurrentContext();
147 widget->Init(params);
148 return widget;
151 void TestCreateWindow(views::Widget::InitParams::Type type,
152 bool always_on_top,
153 aura::Window* expected_container) {
154 views::Widget::InitParams widget_params(type);
155 widget_params.keep_on_top = always_on_top;
157 views::Widget* widget = CreateTestWindow(widget_params);
158 widget->Show();
160 EXPECT_TRUE(
161 expected_container->Contains(widget->GetNativeWindow()->parent())) <<
162 "TestCreateWindow: type=" << type << ", always_on_top=" <<
163 always_on_top;
165 widget->Close();
168 void LockScreenAndVerifyMenuClosed() {
169 // Verify a menu is open before locking.
170 views::MenuController* menu_controller =
171 views::MenuController::GetActiveInstance();
172 DCHECK(menu_controller);
173 EXPECT_EQ(views::MenuController::EXIT_NONE, menu_controller->exit_type());
175 // Create a LockScreen window.
176 views::Widget::InitParams widget_params(
177 views::Widget::InitParams::TYPE_WINDOW);
178 SessionStateDelegate* delegate =
179 Shell::GetInstance()->session_state_delegate();
180 delegate->LockScreen();
181 views::Widget* lock_widget = CreateTestWindow(widget_params);
182 ash::Shell::GetContainer(
183 Shell::GetPrimaryRootWindow(),
184 ash::internal::kShellWindowId_LockScreenContainer)->
185 AddChild(lock_widget->GetNativeView());
186 lock_widget->Show();
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);
225 widget->Show();
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()));
248 widget->Close();
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);
257 widget->Show();
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(),
271 internal::kShellWindowId_SystemModalContainer);
272 EXPECT_EQ(modal_container, modal_widget->GetNativeWindow()->parent());
274 modal_widget->Close();
275 widget->Close();
278 class TestModalDialogDelegate : public views::DialogDelegateView {
279 public:
280 TestModalDialogDelegate() {}
282 // Overridden from views::WidgetDelegate:
283 virtual ui::ModalType GetModalType() const OVERRIDE {
284 return ui::MODAL_TYPE_SYSTEM;
288 TEST_F(ShellTest, CreateLockScreenModalWindow) {
289 views::Widget::InitParams widget_params(
290 views::Widget::InitParams::TYPE_WINDOW);
292 // Create a normal window.
293 views::Widget* widget = CreateTestWindow(widget_params);
294 widget->Show();
295 EXPECT_TRUE(widget->GetNativeView()->HasFocus());
297 // It should be in default container.
298 EXPECT_TRUE(GetDefaultContainer()->Contains(
299 widget->GetNativeWindow()->parent()));
301 Shell::GetInstance()->session_state_delegate()->LockScreen();
302 // Create a LockScreen window.
303 views::Widget* lock_widget = CreateTestWindow(widget_params);
304 ash::Shell::GetContainer(
305 Shell::GetPrimaryRootWindow(),
306 ash::internal::kShellWindowId_LockScreenContainer)->
307 AddChild(lock_widget->GetNativeView());
308 lock_widget->Show();
309 EXPECT_TRUE(lock_widget->GetNativeView()->HasFocus());
311 // It should be in LockScreen container.
312 aura::Window* lock_screen = Shell::GetContainer(
313 Shell::GetPrimaryRootWindow(),
314 ash::internal::kShellWindowId_LockScreenContainer);
315 EXPECT_EQ(lock_screen, lock_widget->GetNativeWindow()->parent());
317 // Create a modal window with a lock window as parent.
318 views::Widget* lock_modal_widget = views::Widget::CreateWindowWithParent(
319 new ModalWindow(), lock_widget->GetNativeView());
320 lock_modal_widget->Show();
321 EXPECT_TRUE(lock_modal_widget->GetNativeView()->HasFocus());
323 // It should be in LockScreen modal container.
324 aura::Window* lock_modal_container = Shell::GetContainer(
325 Shell::GetPrimaryRootWindow(),
326 ash::internal::kShellWindowId_LockSystemModalContainer);
327 EXPECT_EQ(lock_modal_container,
328 lock_modal_widget->GetNativeWindow()->parent());
330 // Create a modal window with a normal window as parent.
331 views::Widget* modal_widget = views::Widget::CreateWindowWithParent(
332 new ModalWindow(), widget->GetNativeView());
333 modal_widget->Show();
334 // Window on lock screen shouldn't lost focus.
335 EXPECT_FALSE(modal_widget->GetNativeView()->HasFocus());
336 EXPECT_TRUE(lock_modal_widget->GetNativeView()->HasFocus());
338 // It should be in non-LockScreen modal container.
339 aura::Window* modal_container = Shell::GetContainer(
340 Shell::GetPrimaryRootWindow(),
341 ash::internal::kShellWindowId_SystemModalContainer);
342 EXPECT_EQ(modal_container, modal_widget->GetNativeWindow()->parent());
344 // Modal dialog without parent, caused crash see crbug.com/226141
345 views::Widget* modal_dialog = views::DialogDelegate::CreateDialogWidget(
346 new TestModalDialogDelegate(), CurrentContext(), NULL);
348 modal_dialog->Show();
349 EXPECT_FALSE(modal_dialog->GetNativeView()->HasFocus());
350 EXPECT_TRUE(lock_modal_widget->GetNativeView()->HasFocus());
352 modal_dialog->Close();
353 modal_widget->Close();
354 modal_widget->Close();
355 lock_modal_widget->Close();
356 lock_widget->Close();
357 widget->Close();
360 TEST_F(ShellTest, IsScreenLocked) {
361 SessionStateDelegate* delegate =
362 Shell::GetInstance()->session_state_delegate();
363 delegate->LockScreen();
364 EXPECT_TRUE(delegate->IsScreenLocked());
365 delegate->UnlockScreen();
366 EXPECT_FALSE(delegate->IsScreenLocked());
369 TEST_F(ShellTest, LockScreenClosesActiveMenu) {
370 SimpleMenuDelegate menu_delegate;
371 scoped_ptr<ui::SimpleMenuModel> menu_model(
372 new ui::SimpleMenuModel(&menu_delegate));
373 menu_model->AddItem(0, ASCIIToUTF16("Menu item"));
374 views::Widget* widget = ash::Shell::GetPrimaryRootWindowController()->
375 wallpaper_controller()->widget();
376 scoped_ptr<views::MenuRunner> menu_runner(
377 new views::MenuRunner(menu_model.get()));
379 // When MenuRunner runs a nested loop the LockScreenAndVerifyMenuClosed
380 // command will fire, check the menu state and ensure the nested menu loop
381 // is exited so that the test will terminate.
382 base::MessageLoopForUI::current()->PostTask(FROM_HERE,
383 base::Bind(&ShellTest::LockScreenAndVerifyMenuClosed,
384 base::Unretained(this)));
386 EXPECT_EQ(views::MenuRunner::NORMAL_EXIT,
387 menu_runner->RunMenuAt(widget, NULL, gfx::Rect(),
388 views::MenuItemView::TOPLEFT, ui::MENU_SOURCE_MOUSE,
389 views::MenuRunner::CONTEXT_MENU));
392 TEST_F(ShellTest, ManagedWindowModeBasics) {
393 Shell* shell = Shell::GetInstance();
394 Shell::TestApi test_api(shell);
396 // We start with the usual window containers.
397 ExpectAllContainers();
398 // Shelf is visible.
399 ShelfWidget* launcher_widget = Launcher::ForPrimaryDisplay()->shelf_widget();
400 EXPECT_TRUE(launcher_widget->IsVisible());
401 // Shelf is at bottom-left of screen.
402 EXPECT_EQ(0, launcher_widget->GetWindowBoundsInScreen().x());
403 EXPECT_EQ(Shell::GetPrimaryRootWindow()->GetDispatcher()->host()->
404 GetBounds().height(),
405 launcher_widget->GetWindowBoundsInScreen().bottom());
406 // We have a desktop background but not a bare layer.
407 // TODO (antrim): enable once we find out why it fails component build.
408 // internal::DesktopBackgroundWidgetController* background =
409 // Shell::GetPrimaryRootWindow()->
410 // GetProperty(internal::kWindowDesktopComponent);
411 // EXPECT_TRUE(background);
412 // EXPECT_TRUE(background->widget());
413 // EXPECT_FALSE(background->layer());
415 // Create a normal window. It is not maximized.
416 views::Widget::InitParams widget_params(
417 views::Widget::InitParams::TYPE_WINDOW);
418 widget_params.bounds.SetRect(11, 22, 300, 400);
419 views::Widget* widget = CreateTestWindow(widget_params);
420 widget->Show();
421 EXPECT_FALSE(widget->IsMaximized());
423 // Clean up.
424 widget->Close();
427 TEST_F(ShellTest, FullscreenWindowHidesShelf) {
428 ExpectAllContainers();
430 // Create a normal window. It is not maximized.
431 views::Widget::InitParams widget_params(
432 views::Widget::InitParams::TYPE_WINDOW);
433 widget_params.bounds.SetRect(11, 22, 300, 400);
434 views::Widget* widget = CreateTestWindow(widget_params);
435 widget->Show();
436 EXPECT_FALSE(widget->IsMaximized());
438 // Shelf defaults to visible.
439 EXPECT_EQ(
440 SHELF_VISIBLE,
441 Shell::GetPrimaryRootWindowController()->
442 GetShelfLayoutManager()->visibility_state());
444 // Fullscreen window hides it.
445 widget->SetFullscreen(true);
446 EXPECT_EQ(
447 SHELF_HIDDEN,
448 Shell::GetPrimaryRootWindowController()->
449 GetShelfLayoutManager()->visibility_state());
451 // Restoring the window restores it.
452 widget->Restore();
453 EXPECT_EQ(
454 SHELF_VISIBLE,
455 Shell::GetPrimaryRootWindowController()->
456 GetShelfLayoutManager()->visibility_state());
458 // Clean up.
459 widget->Close();
462 // Various assertions around SetShelfAutoHideBehavior() and
463 // GetShelfAutoHideBehavior().
464 TEST_F(ShellTest, ToggleAutoHide) {
465 scoped_ptr<aura::Window> window(new aura::Window(NULL));
466 window->SetProperty(aura::client::kShowStateKey, ui::SHOW_STATE_NORMAL);
467 window->SetType(aura::client::WINDOW_TYPE_NORMAL);
468 window->Init(ui::LAYER_TEXTURED);
469 ParentWindowInPrimaryRootWindow(window.get());
470 window->Show();
471 wm::ActivateWindow(window.get());
473 Shell* shell = Shell::GetInstance();
474 aura::Window* root_window = Shell::GetPrimaryRootWindow();
475 shell->SetShelfAutoHideBehavior(ash::SHELF_AUTO_HIDE_BEHAVIOR_ALWAYS,
476 root_window);
477 EXPECT_EQ(ash::SHELF_AUTO_HIDE_BEHAVIOR_ALWAYS,
478 shell->GetShelfAutoHideBehavior(root_window));
479 shell->SetShelfAutoHideBehavior(ash::SHELF_AUTO_HIDE_BEHAVIOR_NEVER,
480 root_window);
481 EXPECT_EQ(ash::SHELF_AUTO_HIDE_BEHAVIOR_NEVER,
482 shell->GetShelfAutoHideBehavior(root_window));
483 window->SetProperty(aura::client::kShowStateKey, ui::SHOW_STATE_MAXIMIZED);
484 EXPECT_EQ(ash::SHELF_AUTO_HIDE_BEHAVIOR_NEVER,
485 shell->GetShelfAutoHideBehavior(root_window));
486 shell->SetShelfAutoHideBehavior(ash::SHELF_AUTO_HIDE_BEHAVIOR_ALWAYS,
487 root_window);
488 EXPECT_EQ(ash::SHELF_AUTO_HIDE_BEHAVIOR_ALWAYS,
489 shell->GetShelfAutoHideBehavior(root_window));
490 shell->SetShelfAutoHideBehavior(ash::SHELF_AUTO_HIDE_BEHAVIOR_NEVER,
491 root_window);
492 EXPECT_EQ(ash::SHELF_AUTO_HIDE_BEHAVIOR_NEVER,
493 shell->GetShelfAutoHideBehavior(root_window));
496 TEST_F(ShellTest, TestPreTargetHandlerOrder) {
497 Shell* shell = Shell::GetInstance();
498 Shell::TestApi test_api(shell);
499 test::ShellTestApi shell_test_api(shell);
501 const ui::EventHandlerList& handlers = test_api.pre_target_handlers();
502 EXPECT_EQ(handlers[0], shell->mouse_cursor_filter());
503 EXPECT_EQ(handlers[1], shell_test_api.drag_drop_controller());
506 // Verifies an EventHandler added to Env gets notified from EventGenerator.
507 TEST_F(ShellTest, EnvPreTargetHandler) {
508 aura::test::TestEventHandler event_handler;
509 aura::Env::GetInstance()->AddPreTargetHandler(&event_handler);
510 aura::test::EventGenerator generator(Shell::GetPrimaryRootWindow());
511 generator.MoveMouseBy(1, 1);
512 EXPECT_NE(0, event_handler.num_mouse_events());
513 aura::Env::GetInstance()->RemovePreTargetHandler(&event_handler);
516 // This verifies WindowObservers are removed when a window is destroyed after
517 // the Shell is destroyed. This scenario (aura::Windows being deleted after the
518 // Shell) occurs if someone is holding a reference to an unparented Window, as
519 // is the case with a RenderWidgetHostViewAura that isn't on screen. As long as
520 // everything is ok, we won't crash. If there is a bug, window's destructor will
521 // notify some deleted object (say VideoDetector or ActivationController) and
522 // this will crash.
523 class ShellTest2 : public test::AshTestBase {
524 public:
525 ShellTest2() {}
526 virtual ~ShellTest2() {}
528 protected:
529 scoped_ptr<aura::Window> window_;
531 private:
532 DISALLOW_COPY_AND_ASSIGN(ShellTest2);
535 TEST_F(ShellTest2, DontCrashWhenWindowDeleted) {
536 window_.reset(new aura::Window(NULL));
537 window_->Init(ui::LAYER_NOT_DRAWN);
540 } // namespace ash