Call InstanceID::DeleteID to delete InstanceID data when the app or
[chromium-blink-merge.git] / ash / root_window_controller_unittest.cc
blob412fecdd6161f0d7fbb8aafffba8d98f48dfe95e
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/root_window_controller.h"
7 #include "ash/display/display_manager.h"
8 #include "ash/session/session_state_delegate.h"
9 #include "ash/shelf/shelf_layout_manager.h"
10 #include "ash/shell.h"
11 #include "ash/shell_window_ids.h"
12 #include "ash/system/tray/system_tray_delegate.h"
13 #include "ash/test/ash_test_base.h"
14 #include "ash/test/display_manager_test_api.h"
15 #include "ash/wm/system_modal_container_layout_manager.h"
16 #include "ash/wm/window_properties.h"
17 #include "ash/wm/window_state.h"
18 #include "ash/wm/window_util.h"
19 #include "base/command_line.h"
20 #include "base/memory/scoped_ptr.h"
21 #include "ui/aura/client/focus_change_observer.h"
22 #include "ui/aura/client/focus_client.h"
23 #include "ui/aura/client/window_tree_client.h"
24 #include "ui/aura/env.h"
25 #include "ui/aura/test/test_window_delegate.h"
26 #include "ui/aura/test/test_windows.h"
27 #include "ui/aura/window.h"
28 #include "ui/aura/window_event_dispatcher.h"
29 #include "ui/aura/window_tracker.h"
30 #include "ui/base/ime/dummy_text_input_client.h"
31 #include "ui/base/ime/input_method.h"
32 #include "ui/base/ime/text_input_client.h"
33 #include "ui/base/ime/text_input_focus_manager.h"
34 #include "ui/base/ui_base_switches_util.h"
35 #include "ui/events/test/event_generator.h"
36 #include "ui/events/test/test_event_handler.h"
37 #include "ui/keyboard/keyboard_controller_proxy.h"
38 #include "ui/keyboard/keyboard_switches.h"
39 #include "ui/keyboard/keyboard_util.h"
40 #include "ui/views/controls/menu/menu_controller.h"
41 #include "ui/views/widget/widget.h"
42 #include "ui/views/widget/widget_delegate.h"
44 using aura::Window;
45 using views::Widget;
47 namespace ash {
48 namespace {
50 class TestDelegate : public views::WidgetDelegateView {
51 public:
52 explicit TestDelegate(bool system_modal) : system_modal_(system_modal) {}
53 ~TestDelegate() override {}
55 // Overridden from views::WidgetDelegate:
56 views::View* GetContentsView() override { return this; }
58 ui::ModalType GetModalType() const override {
59 return system_modal_ ? ui::MODAL_TYPE_SYSTEM : ui::MODAL_TYPE_NONE;
62 private:
63 bool system_modal_;
65 DISALLOW_COPY_AND_ASSIGN(TestDelegate);
68 class DeleteOnBlurDelegate : public aura::test::TestWindowDelegate,
69 public aura::client::FocusChangeObserver {
70 public:
71 DeleteOnBlurDelegate() : window_(NULL) {}
72 ~DeleteOnBlurDelegate() override {}
74 void SetWindow(aura::Window* window) {
75 window_ = window;
76 aura::client::SetFocusChangeObserver(window_, this);
79 private:
80 // aura::test::TestWindowDelegate overrides:
81 bool CanFocus() override { return true; }
83 // aura::client::FocusChangeObserver implementation:
84 void OnWindowFocused(aura::Window* gained_focus,
85 aura::Window* lost_focus) override {
86 if (window_ == lost_focus)
87 delete window_;
90 aura::Window* window_;
92 DISALLOW_COPY_AND_ASSIGN(DeleteOnBlurDelegate);
95 } // namespace
97 namespace test {
99 class RootWindowControllerTest : public test::AshTestBase {
100 public:
101 views::Widget* CreateTestWidget(const gfx::Rect& bounds) {
102 views::Widget* widget = views::Widget::CreateWindowWithContextAndBounds(
103 NULL, CurrentContext(), bounds);
104 widget->Show();
105 return widget;
108 views::Widget* CreateModalWidget(const gfx::Rect& bounds) {
109 views::Widget* widget = views::Widget::CreateWindowWithContextAndBounds(
110 new TestDelegate(true), CurrentContext(), bounds);
111 widget->Show();
112 return widget;
115 views::Widget* CreateModalWidgetWithParent(const gfx::Rect& bounds,
116 gfx::NativeWindow parent) {
117 views::Widget* widget =
118 views::Widget::CreateWindowWithParentAndBounds(new TestDelegate(true),
119 parent,
120 bounds);
121 widget->Show();
122 return widget;
125 aura::Window* GetModalContainer(aura::Window* root_window) {
126 return Shell::GetContainer(root_window,
127 ash::kShellWindowId_SystemModalContainer);
131 TEST_F(RootWindowControllerTest, MoveWindows_Basic) {
132 if (!SupportsMultipleDisplays())
133 return;
134 // Windows origin should be doubled when moved to the 1st display.
135 UpdateDisplay("600x600,300x300");
136 aura::Window::Windows root_windows = Shell::GetAllRootWindows();
137 RootWindowController* controller = Shell::GetPrimaryRootWindowController();
138 ShelfLayoutManager* shelf_layout_manager =
139 controller->GetShelfLayoutManager();
140 shelf_layout_manager->SetAutoHideBehavior(
141 ash::SHELF_AUTO_HIDE_BEHAVIOR_ALWAYS);
143 views::Widget* normal = CreateTestWidget(gfx::Rect(650, 10, 100, 100));
144 EXPECT_EQ(root_windows[1], normal->GetNativeView()->GetRootWindow());
145 EXPECT_EQ("650,10 100x100", normal->GetWindowBoundsInScreen().ToString());
146 EXPECT_EQ("50,10 100x100",
147 normal->GetNativeView()->GetBoundsInRootWindow().ToString());
149 views::Widget* maximized = CreateTestWidget(gfx::Rect(700, 10, 100, 100));
150 maximized->Maximize();
151 EXPECT_EQ(root_windows[1], maximized->GetNativeView()->GetRootWindow());
152 EXPECT_EQ("600,0 300x253", maximized->GetWindowBoundsInScreen().ToString());
153 EXPECT_EQ("0,0 300x253",
154 maximized->GetNativeView()->GetBoundsInRootWindow().ToString());
156 views::Widget* minimized = CreateTestWidget(gfx::Rect(800, 10, 100, 100));
157 minimized->Minimize();
158 EXPECT_EQ(root_windows[1], minimized->GetNativeView()->GetRootWindow());
159 EXPECT_EQ("800,10 100x100",
160 minimized->GetWindowBoundsInScreen().ToString());
162 views::Widget* fullscreen = CreateTestWidget(gfx::Rect(850, 10, 100, 100));
163 fullscreen->SetFullscreen(true);
164 EXPECT_EQ(root_windows[1], fullscreen->GetNativeView()->GetRootWindow());
166 EXPECT_EQ("600,0 300x300",
167 fullscreen->GetWindowBoundsInScreen().ToString());
168 EXPECT_EQ("0,0 300x300",
169 fullscreen->GetNativeView()->GetBoundsInRootWindow().ToString());
171 views::Widget* unparented_control = new Widget;
172 Widget::InitParams params;
173 params.bounds = gfx::Rect(650, 10, 100, 100);
174 params.context = CurrentContext();
175 params.type = Widget::InitParams::TYPE_CONTROL;
176 unparented_control->Init(params);
177 EXPECT_EQ(root_windows[1],
178 unparented_control->GetNativeView()->GetRootWindow());
179 EXPECT_EQ(kShellWindowId_UnparentedControlContainer,
180 unparented_control->GetNativeView()->parent()->id());
182 aura::Window* panel = CreateTestWindowInShellWithDelegateAndType(
183 NULL, ui::wm::WINDOW_TYPE_PANEL, 0, gfx::Rect(700, 100, 100, 100));
184 EXPECT_EQ(root_windows[1], panel->GetRootWindow());
185 EXPECT_EQ(kShellWindowId_PanelContainer, panel->parent()->id());
187 // Make sure a window that will delete itself when losing focus
188 // will not crash.
189 aura::WindowTracker tracker;
190 DeleteOnBlurDelegate delete_on_blur_delegate;
191 aura::Window* d2 = CreateTestWindowInShellWithDelegate(
192 &delete_on_blur_delegate, 0, gfx::Rect(50, 50, 100, 100));
193 delete_on_blur_delegate.SetWindow(d2);
194 aura::client::GetFocusClient(root_windows[0])->FocusWindow(d2);
195 tracker.Add(d2);
197 UpdateDisplay("600x600");
199 // d2 must have been deleted.
200 EXPECT_FALSE(tracker.Contains(d2));
202 EXPECT_EQ(root_windows[0], normal->GetNativeView()->GetRootWindow());
203 EXPECT_EQ("100,20 100x100", normal->GetWindowBoundsInScreen().ToString());
204 EXPECT_EQ("100,20 100x100",
205 normal->GetNativeView()->GetBoundsInRootWindow().ToString());
207 // Maximized area on primary display has 3px (given as
208 // kAutoHideSize in shelf_layout_manager.cc) inset at the bottom.
210 // First clear fullscreen status, since both fullscreen and maximized windows
211 // share the same desktop workspace, which cancels the shelf status.
212 fullscreen->SetFullscreen(false);
213 EXPECT_EQ(root_windows[0], maximized->GetNativeView()->GetRootWindow());
214 EXPECT_EQ("0,0 600x597",
215 maximized->GetWindowBoundsInScreen().ToString());
216 EXPECT_EQ("0,0 600x597",
217 maximized->GetNativeView()->GetBoundsInRootWindow().ToString());
219 // Set fullscreen to true. In that case the 3px inset becomes invisible so
220 // the maximized window can also use the area fully.
221 fullscreen->SetFullscreen(true);
222 EXPECT_EQ(root_windows[0], maximized->GetNativeView()->GetRootWindow());
223 EXPECT_EQ("0,0 600x600",
224 maximized->GetWindowBoundsInScreen().ToString());
225 EXPECT_EQ("0,0 600x600",
226 maximized->GetNativeView()->GetBoundsInRootWindow().ToString());
228 EXPECT_EQ(root_windows[0], minimized->GetNativeView()->GetRootWindow());
229 EXPECT_EQ("400,20 100x100",
230 minimized->GetWindowBoundsInScreen().ToString());
232 EXPECT_EQ(root_windows[0], fullscreen->GetNativeView()->GetRootWindow());
233 EXPECT_TRUE(fullscreen->IsFullscreen());
234 EXPECT_EQ("0,0 600x600",
235 fullscreen->GetWindowBoundsInScreen().ToString());
236 EXPECT_EQ("0,0 600x600",
237 fullscreen->GetNativeView()->GetBoundsInRootWindow().ToString());
239 // Test if the restore bounds are correctly updated.
240 wm::GetWindowState(maximized->GetNativeView())->Restore();
241 EXPECT_EQ("200,20 100x100", maximized->GetWindowBoundsInScreen().ToString());
242 EXPECT_EQ("200,20 100x100",
243 maximized->GetNativeView()->GetBoundsInRootWindow().ToString());
245 fullscreen->SetFullscreen(false);
246 EXPECT_EQ("500,20 100x100",
247 fullscreen->GetWindowBoundsInScreen().ToString());
248 EXPECT_EQ("500,20 100x100",
249 fullscreen->GetNativeView()->GetBoundsInRootWindow().ToString());
251 // Test if the unparented widget has moved.
252 EXPECT_EQ(root_windows[0],
253 unparented_control->GetNativeView()->GetRootWindow());
254 EXPECT_EQ(kShellWindowId_UnparentedControlContainer,
255 unparented_control->GetNativeView()->parent()->id());
257 // Test if the panel has moved.
258 EXPECT_EQ(root_windows[0], panel->GetRootWindow());
259 EXPECT_EQ(kShellWindowId_PanelContainer, panel->parent()->id());
262 TEST_F(RootWindowControllerTest, MoveWindows_Modal) {
263 if (!SupportsMultipleDisplays())
264 return;
266 UpdateDisplay("500x500,500x500");
268 aura::Window::Windows root_windows = Shell::GetAllRootWindows();
269 // Emulate virtual screen coordinate system.
270 root_windows[0]->SetBounds(gfx::Rect(0, 0, 500, 500));
271 root_windows[1]->SetBounds(gfx::Rect(500, 0, 500, 500));
273 views::Widget* normal = CreateTestWidget(gfx::Rect(300, 10, 100, 100));
274 EXPECT_EQ(root_windows[0], normal->GetNativeView()->GetRootWindow());
275 EXPECT_TRUE(wm::IsActiveWindow(normal->GetNativeView()));
277 views::Widget* modal = CreateModalWidget(gfx::Rect(650, 10, 100, 100));
278 EXPECT_EQ(root_windows[1], modal->GetNativeView()->GetRootWindow());
279 EXPECT_TRUE(GetModalContainer(root_windows[1])->Contains(
280 modal->GetNativeView()));
281 EXPECT_TRUE(wm::IsActiveWindow(modal->GetNativeView()));
283 ui::test::EventGenerator generator_1st(root_windows[0]);
284 generator_1st.ClickLeftButton();
285 EXPECT_TRUE(wm::IsActiveWindow(modal->GetNativeView()));
287 UpdateDisplay("500x500");
288 EXPECT_EQ(root_windows[0], modal->GetNativeView()->GetRootWindow());
289 EXPECT_TRUE(wm::IsActiveWindow(modal->GetNativeView()));
290 generator_1st.ClickLeftButton();
291 EXPECT_TRUE(wm::IsActiveWindow(modal->GetNativeView()));
294 // Make sure lock related windows moves.
295 TEST_F(RootWindowControllerTest, MoveWindows_LockWindowsInUnified) {
296 if (!SupportsMultipleDisplays())
297 return;
298 test::DisplayManagerTestApi::EnableUnifiedDesktopForTest();
300 DisplayManager* display_manager = Shell::GetInstance()->display_manager();
301 display_manager->SetDefaultMultiDisplayMode(DisplayManager::UNIFIED);
302 display_manager->SetMultiDisplayMode(DisplayManager::UNIFIED);
303 UpdateDisplay("500x500");
304 const int kLockScreenWindowId = 1000;
305 const int kLockBackgroundWindowId = 1001;
307 RootWindowController* controller =
308 Shell::GetInstance()->GetPrimaryRootWindowController();
310 aura::Window* lock_container =
311 controller->GetContainer(kShellWindowId_LockScreenContainer);
312 aura::Window* lock_background_container =
313 controller->GetContainer(kShellWindowId_LockScreenBackgroundContainer);
315 views::Widget* lock_screen =
316 CreateModalWidgetWithParent(gfx::Rect(10, 10, 100, 100), lock_container);
317 lock_screen->GetNativeWindow()->set_id(kLockScreenWindowId);
318 lock_screen->SetFullscreen(true);
320 views::Widget* lock_background = CreateModalWidgetWithParent(
321 gfx::Rect(10, 10, 100, 100), lock_background_container);
322 lock_background->GetNativeWindow()->set_id(kLockBackgroundWindowId);
324 ASSERT_EQ(lock_screen->GetNativeWindow(),
325 controller->GetRootWindow()->GetChildById(kLockScreenWindowId));
326 ASSERT_EQ(lock_background->GetNativeWindow(),
327 controller->GetRootWindow()->GetChildById(kLockBackgroundWindowId));
328 EXPECT_EQ("0,0 500x500", lock_screen->GetNativeWindow()->bounds().ToString());
330 // Switch to unified.
331 UpdateDisplay("500x500,500x500");
333 // In unified mode, RWC is created
334 controller = Shell::GetInstance()->GetPrimaryRootWindowController();
336 ASSERT_EQ(lock_screen->GetNativeWindow(),
337 controller->GetRootWindow()->GetChildById(kLockScreenWindowId));
338 ASSERT_EQ(lock_background->GetNativeWindow(),
339 controller->GetRootWindow()->GetChildById(kLockBackgroundWindowId));
340 EXPECT_EQ("0,0 500x500", lock_screen->GetNativeWindow()->bounds().ToString());
342 // Switch to mirror.
343 display_manager->SetMirrorMode(true);
344 EXPECT_TRUE(display_manager->IsInMirrorMode());
346 controller = Shell::GetInstance()->GetPrimaryRootWindowController();
347 ASSERT_EQ(lock_screen->GetNativeWindow(),
348 controller->GetRootWindow()->GetChildById(kLockScreenWindowId));
349 ASSERT_EQ(lock_background->GetNativeWindow(),
350 controller->GetRootWindow()->GetChildById(kLockBackgroundWindowId));
351 EXPECT_EQ("0,0 500x500", lock_screen->GetNativeWindow()->bounds().ToString());
353 // Switch to unified.
354 display_manager->SetMirrorMode(false);
355 EXPECT_TRUE(display_manager->IsInUnifiedMode());
357 controller = Shell::GetInstance()->GetPrimaryRootWindowController();
359 ASSERT_EQ(lock_screen->GetNativeWindow(),
360 controller->GetRootWindow()->GetChildById(kLockScreenWindowId));
361 ASSERT_EQ(lock_background->GetNativeWindow(),
362 controller->GetRootWindow()->GetChildById(kLockBackgroundWindowId));
363 EXPECT_EQ("0,0 500x500", lock_screen->GetNativeWindow()->bounds().ToString());
365 // Switch to single display.
366 UpdateDisplay("600x500");
367 EXPECT_FALSE(display_manager->IsInUnifiedMode());
368 EXPECT_FALSE(display_manager->IsInMirrorMode());
370 controller = Shell::GetInstance()->GetPrimaryRootWindowController();
372 ASSERT_EQ(lock_screen->GetNativeWindow(),
373 controller->GetRootWindow()->GetChildById(kLockScreenWindowId));
374 ASSERT_EQ(lock_background->GetNativeWindow(),
375 controller->GetRootWindow()->GetChildById(kLockBackgroundWindowId));
376 EXPECT_EQ("0,0 600x500", lock_screen->GetNativeWindow()->bounds().ToString());
379 TEST_F(RootWindowControllerTest, ModalContainer) {
380 UpdateDisplay("600x600");
381 Shell* shell = Shell::GetInstance();
382 RootWindowController* controller = shell->GetPrimaryRootWindowController();
383 EXPECT_EQ(user::LOGGED_IN_USER,
384 shell->system_tray_delegate()->GetUserLoginStatus());
385 EXPECT_EQ(controller->GetContainer(kShellWindowId_SystemModalContainer)
386 ->layout_manager(),
387 controller->GetSystemModalLayoutManager(NULL));
389 views::Widget* session_modal_widget =
390 CreateModalWidget(gfx::Rect(300, 10, 100, 100));
391 EXPECT_EQ(controller->GetContainer(kShellWindowId_SystemModalContainer)
392 ->layout_manager(),
393 controller->GetSystemModalLayoutManager(
394 session_modal_widget->GetNativeView()));
396 shell->session_state_delegate()->LockScreen();
397 EXPECT_EQ(user::LOGGED_IN_LOCKED,
398 shell->system_tray_delegate()->GetUserLoginStatus());
399 EXPECT_EQ(controller->GetContainer(kShellWindowId_LockSystemModalContainer)
400 ->layout_manager(),
401 controller->GetSystemModalLayoutManager(NULL));
403 aura::Window* lock_container =
404 controller->GetContainer(kShellWindowId_LockScreenContainer);
405 views::Widget* lock_modal_widget =
406 CreateModalWidgetWithParent(gfx::Rect(300, 10, 100, 100), lock_container);
407 EXPECT_EQ(controller->GetContainer(kShellWindowId_LockSystemModalContainer)
408 ->layout_manager(),
409 controller->GetSystemModalLayoutManager(
410 lock_modal_widget->GetNativeView()));
411 EXPECT_EQ(controller->GetContainer(kShellWindowId_SystemModalContainer)
412 ->layout_manager(),
413 controller->GetSystemModalLayoutManager(
414 session_modal_widget->GetNativeView()));
416 shell->session_state_delegate()->UnlockScreen();
419 TEST_F(RootWindowControllerTest, ModalContainerNotLoggedInLoggedIn) {
420 UpdateDisplay("600x600");
421 Shell* shell = Shell::GetInstance();
423 // Configure login screen environment.
424 SetUserLoggedIn(false);
425 EXPECT_EQ(user::LOGGED_IN_NONE,
426 shell->system_tray_delegate()->GetUserLoginStatus());
427 EXPECT_EQ(0, shell->session_state_delegate()->NumberOfLoggedInUsers());
428 EXPECT_FALSE(shell->session_state_delegate()->IsActiveUserSessionStarted());
430 RootWindowController* controller = shell->GetPrimaryRootWindowController();
431 EXPECT_EQ(controller->GetContainer(kShellWindowId_LockSystemModalContainer)
432 ->layout_manager(),
433 controller->GetSystemModalLayoutManager(NULL));
435 aura::Window* lock_container =
436 controller->GetContainer(kShellWindowId_LockScreenContainer);
437 views::Widget* login_modal_widget =
438 CreateModalWidgetWithParent(gfx::Rect(300, 10, 100, 100), lock_container);
439 EXPECT_EQ(controller->GetContainer(kShellWindowId_LockSystemModalContainer)
440 ->layout_manager(),
441 controller->GetSystemModalLayoutManager(
442 login_modal_widget->GetNativeView()));
443 login_modal_widget->Close();
445 // Configure user session environment.
446 SetUserLoggedIn(true);
447 SetSessionStarted(true);
448 EXPECT_EQ(user::LOGGED_IN_USER,
449 shell->system_tray_delegate()->GetUserLoginStatus());
450 EXPECT_EQ(1, shell->session_state_delegate()->NumberOfLoggedInUsers());
451 EXPECT_TRUE(shell->session_state_delegate()->IsActiveUserSessionStarted());
452 EXPECT_EQ(controller->GetContainer(kShellWindowId_SystemModalContainer)
453 ->layout_manager(),
454 controller->GetSystemModalLayoutManager(NULL));
456 views::Widget* session_modal_widget =
457 CreateModalWidget(gfx::Rect(300, 10, 100, 100));
458 EXPECT_EQ(controller->GetContainer(kShellWindowId_SystemModalContainer)
459 ->layout_manager(),
460 controller->GetSystemModalLayoutManager(
461 session_modal_widget->GetNativeView()));
464 TEST_F(RootWindowControllerTest, ModalContainerBlockedSession) {
465 UpdateDisplay("600x600");
466 RootWindowController* controller = Shell::GetPrimaryRootWindowController();
467 aura::Window* lock_container =
468 controller->GetContainer(kShellWindowId_LockScreenContainer);
469 for (int block_reason = FIRST_BLOCK_REASON;
470 block_reason < NUMBER_OF_BLOCK_REASONS;
471 ++block_reason) {
472 views::Widget* session_modal_widget =
473 CreateModalWidget(gfx::Rect(300, 10, 100, 100));
474 EXPECT_EQ(controller->GetContainer(kShellWindowId_SystemModalContainer)
475 ->layout_manager(),
476 controller->GetSystemModalLayoutManager(
477 session_modal_widget->GetNativeView()));
478 EXPECT_EQ(controller->GetContainer(kShellWindowId_SystemModalContainer)
479 ->layout_manager(),
480 controller->GetSystemModalLayoutManager(NULL));
481 session_modal_widget->Close();
483 BlockUserSession(static_cast<UserSessionBlockReason>(block_reason));
485 EXPECT_EQ(controller->GetContainer(kShellWindowId_LockSystemModalContainer)
486 ->layout_manager(),
487 controller->GetSystemModalLayoutManager(NULL));
489 views::Widget* lock_modal_widget =
490 CreateModalWidgetWithParent(gfx::Rect(300, 10, 100, 100),
491 lock_container);
492 EXPECT_EQ(controller->GetContainer(kShellWindowId_LockSystemModalContainer)
493 ->layout_manager(),
494 controller->GetSystemModalLayoutManager(
495 lock_modal_widget->GetNativeView()));
497 session_modal_widget =
498 CreateModalWidget(gfx::Rect(300, 10, 100, 100));
499 EXPECT_EQ(controller->GetContainer(kShellWindowId_SystemModalContainer)
500 ->layout_manager(),
501 controller->GetSystemModalLayoutManager(
502 session_modal_widget->GetNativeView()));
503 session_modal_widget->Close();
505 lock_modal_widget->Close();
506 UnblockUserSession();
510 TEST_F(RootWindowControllerTest, GetWindowForFullscreenMode) {
511 UpdateDisplay("600x600");
512 RootWindowController* controller =
513 Shell::GetInstance()->GetPrimaryRootWindowController();
515 Widget* w1 = CreateTestWidget(gfx::Rect(0, 0, 100, 100));
516 w1->Maximize();
517 Widget* w2 = CreateTestWidget(gfx::Rect(0, 0, 100, 100));
518 w2->SetFullscreen(true);
519 // |w3| is a transient child of |w2|.
520 Widget* w3 = Widget::CreateWindowWithParentAndBounds(NULL,
521 w2->GetNativeWindow(), gfx::Rect(0, 0, 100, 100));
523 // Test that GetWindowForFullscreenMode() finds the fullscreen window when one
524 // of its transient children is active.
525 w3->Activate();
526 EXPECT_EQ(w2->GetNativeWindow(), controller->GetWindowForFullscreenMode());
528 // If the topmost window is not fullscreen, it returns NULL.
529 w1->Activate();
530 EXPECT_EQ(NULL, controller->GetWindowForFullscreenMode());
531 w1->Close();
532 w3->Close();
534 // Only w2 remains, if minimized GetWindowForFullscreenMode should return
535 // NULL.
536 w2->Activate();
537 EXPECT_EQ(w2->GetNativeWindow(), controller->GetWindowForFullscreenMode());
538 w2->Minimize();
539 EXPECT_EQ(NULL, controller->GetWindowForFullscreenMode());
542 TEST_F(RootWindowControllerTest, MultipleDisplaysGetWindowForFullscreenMode) {
543 if (!SupportsMultipleDisplays())
544 return;
546 UpdateDisplay("600x600,600x600");
547 Shell::RootWindowControllerList controllers =
548 Shell::GetInstance()->GetAllRootWindowControllers();
550 Widget* w1 = CreateTestWidget(gfx::Rect(0, 0, 100, 100));
551 w1->Maximize();
552 Widget* w2 = CreateTestWidget(gfx::Rect(0, 0, 100, 100));
553 w2->SetFullscreen(true);
554 Widget* w3 = CreateTestWidget(gfx::Rect(600, 0, 100, 100));
556 EXPECT_EQ(w1->GetNativeWindow()->GetRootWindow(),
557 controllers[0]->GetRootWindow());
558 EXPECT_EQ(w2->GetNativeWindow()->GetRootWindow(),
559 controllers[0]->GetRootWindow());
560 EXPECT_EQ(w3->GetNativeWindow()->GetRootWindow(),
561 controllers[1]->GetRootWindow());
563 w1->Activate();
564 EXPECT_EQ(NULL, controllers[0]->GetWindowForFullscreenMode());
565 EXPECT_EQ(NULL, controllers[1]->GetWindowForFullscreenMode());
567 w2->Activate();
568 EXPECT_EQ(w2->GetNativeWindow(),
569 controllers[0]->GetWindowForFullscreenMode());
570 EXPECT_EQ(NULL, controllers[1]->GetWindowForFullscreenMode());
572 // Verify that the first root window controller remains in fullscreen mode
573 // when a window on the other display is activated.
574 w3->Activate();
575 EXPECT_EQ(w2->GetNativeWindow(),
576 controllers[0]->GetWindowForFullscreenMode());
577 EXPECT_EQ(NULL, controllers[1]->GetWindowForFullscreenMode());
580 // Test that user session window can't be focused if user session blocked by
581 // some overlapping UI.
582 TEST_F(RootWindowControllerTest, FocusBlockedWindow) {
583 UpdateDisplay("600x600");
584 RootWindowController* controller =
585 Shell::GetInstance()->GetPrimaryRootWindowController();
586 aura::Window* lock_container =
587 controller->GetContainer(kShellWindowId_LockScreenContainer);
588 aura::Window* lock_window = Widget::CreateWindowWithParentAndBounds(NULL,
589 lock_container, gfx::Rect(0, 0, 100, 100))->GetNativeView();
590 lock_window->Show();
591 aura::Window* session_window =
592 CreateTestWidget(gfx::Rect(0, 0, 100, 100))->GetNativeView();
593 session_window->Show();
595 for (int block_reason = FIRST_BLOCK_REASON;
596 block_reason < NUMBER_OF_BLOCK_REASONS;
597 ++block_reason) {
598 BlockUserSession(static_cast<UserSessionBlockReason>(block_reason));
599 lock_window->Focus();
600 EXPECT_TRUE(lock_window->HasFocus());
601 session_window->Focus();
602 EXPECT_FALSE(session_window->HasFocus());
603 UnblockUserSession();
607 // Tracks whether OnWindowDestroying() has been invoked.
608 class DestroyedWindowObserver : public aura::WindowObserver {
609 public:
610 DestroyedWindowObserver() : destroyed_(false), window_(NULL) {}
611 ~DestroyedWindowObserver() override { Shutdown(); }
613 void SetWindow(Window* window) {
614 window_ = window;
615 window->AddObserver(this);
618 bool destroyed() const { return destroyed_; }
620 // WindowObserver overrides:
621 void OnWindowDestroying(Window* window) override {
622 destroyed_ = true;
623 Shutdown();
626 private:
627 void Shutdown() {
628 if (!window_)
629 return;
630 window_->RemoveObserver(this);
631 window_ = NULL;
634 bool destroyed_;
635 Window* window_;
637 DISALLOW_COPY_AND_ASSIGN(DestroyedWindowObserver);
640 // Verifies shutdown doesn't delete windows that are not owned by the parent.
641 TEST_F(RootWindowControllerTest, DontDeleteWindowsNotOwnedByParent) {
642 DestroyedWindowObserver observer1;
643 aura::test::TestWindowDelegate delegate1;
644 aura::Window* window1 = new aura::Window(&delegate1);
645 window1->SetType(ui::wm::WINDOW_TYPE_CONTROL);
646 window1->set_owned_by_parent(false);
647 observer1.SetWindow(window1);
648 window1->Init(ui::LAYER_NOT_DRAWN);
649 aura::client::ParentWindowWithContext(
650 window1, Shell::GetInstance()->GetPrimaryRootWindow(), gfx::Rect());
652 DestroyedWindowObserver observer2;
653 aura::Window* window2 = new aura::Window(NULL);
654 window2->set_owned_by_parent(false);
655 observer2.SetWindow(window2);
656 window2->Init(ui::LAYER_NOT_DRAWN);
657 Shell::GetInstance()->GetPrimaryRootWindow()->AddChild(window2);
659 Shell::GetInstance()->GetPrimaryRootWindowController()->CloseChildWindows();
661 ASSERT_FALSE(observer1.destroyed());
662 delete window1;
664 ASSERT_FALSE(observer2.destroyed());
665 delete window2;
668 typedef test::NoSessionAshTestBase NoSessionRootWindowControllerTest;
670 // Make sure that an event handler exists for entire display area.
671 TEST_F(NoSessionRootWindowControllerTest, Event) {
672 // Hide the shelf since it might otherwise get an event target.
673 RootWindowController* controller = Shell::GetPrimaryRootWindowController();
674 ShelfLayoutManager* shelf_layout_manager =
675 controller->GetShelfLayoutManager();
676 shelf_layout_manager->SetAutoHideBehavior(
677 ash::SHELF_AUTO_HIDE_ALWAYS_HIDDEN);
679 aura::Window* root = Shell::GetPrimaryRootWindow();
680 const gfx::Size size = root->bounds().size();
681 aura::Window* event_target = root->GetEventHandlerForPoint(gfx::Point(0, 0));
682 EXPECT_TRUE(event_target);
683 EXPECT_EQ(event_target,
684 root->GetEventHandlerForPoint(gfx::Point(0, size.height() - 1)));
685 EXPECT_EQ(event_target,
686 root->GetEventHandlerForPoint(gfx::Point(size.width() - 1, 0)));
687 EXPECT_EQ(event_target,
688 root->GetEventHandlerForPoint(gfx::Point(0, size.height() - 1)));
689 EXPECT_EQ(event_target,
690 root->GetEventHandlerForPoint(
691 gfx::Point(size.width() - 1, size.height() - 1)));
694 class VirtualKeyboardRootWindowControllerTest
695 : public RootWindowControllerTest {
696 public:
697 VirtualKeyboardRootWindowControllerTest() {}
698 ~VirtualKeyboardRootWindowControllerTest() override {}
700 void SetUp() override {
701 base::CommandLine::ForCurrentProcess()->AppendSwitch(
702 keyboard::switches::kEnableVirtualKeyboard);
703 test::AshTestBase::SetUp();
704 Shell::GetPrimaryRootWindowController()->ActivateKeyboard(
705 keyboard::KeyboardController::GetInstance());
708 private:
709 DISALLOW_COPY_AND_ASSIGN(VirtualKeyboardRootWindowControllerTest);
712 class MockTextInputClient : public ui::DummyTextInputClient {
713 public:
714 MockTextInputClient() :
715 ui::DummyTextInputClient(ui::TEXT_INPUT_TYPE_TEXT) {}
717 void EnsureCaretInRect(const gfx::Rect& rect) override {
718 visible_rect_ = rect;
721 const gfx::Rect& visible_rect() const {
722 return visible_rect_;
725 private:
726 gfx::Rect visible_rect_;
728 DISALLOW_COPY_AND_ASSIGN(MockTextInputClient);
731 class TargetHitTestEventHandler : public ui::test::TestEventHandler {
732 public:
733 TargetHitTestEventHandler() {}
735 // ui::test::TestEventHandler overrides.
736 void OnMouseEvent(ui::MouseEvent* event) override {
737 if (event->type() == ui::ET_MOUSE_PRESSED)
738 ui::test::TestEventHandler::OnMouseEvent(event);
739 event->StopPropagation();
742 private:
743 DISALLOW_COPY_AND_ASSIGN(TargetHitTestEventHandler);
746 // Test for http://crbug.com/297858. Virtual keyboard container should only show
747 // on primary root window.
748 TEST_F(VirtualKeyboardRootWindowControllerTest,
749 VirtualKeyboardOnPrimaryRootWindowOnly) {
750 if (!SupportsMultipleDisplays())
751 return;
753 UpdateDisplay("500x500,500x500");
755 aura::Window::Windows root_windows = Shell::GetAllRootWindows();
756 aura::Window* primary_root_window = Shell::GetPrimaryRootWindow();
757 aura::Window* secondary_root_window =
758 root_windows[0] == primary_root_window ?
759 root_windows[1] : root_windows[0];
761 ASSERT_TRUE(Shell::GetContainer(primary_root_window,
762 kShellWindowId_VirtualKeyboardContainer));
763 ASSERT_FALSE(Shell::GetContainer(secondary_root_window,
764 kShellWindowId_VirtualKeyboardContainer));
767 // Test for http://crbug.com/263599. Virtual keyboard should be able to receive
768 // events at blocked user session.
769 TEST_F(VirtualKeyboardRootWindowControllerTest,
770 ClickVirtualKeyboardInBlockedWindow) {
771 aura::Window* root_window = Shell::GetPrimaryRootWindow();
772 aura::Window* keyboard_container =
773 Shell::GetContainer(root_window, kShellWindowId_VirtualKeyboardContainer);
774 ASSERT_TRUE(keyboard_container);
775 keyboard_container->Show();
777 aura::Window* keyboard_window = keyboard::KeyboardController::GetInstance()->
778 proxy()->GetKeyboardWindow();
779 keyboard_container->AddChild(keyboard_window);
780 keyboard_window->set_owned_by_parent(false);
781 keyboard_window->SetBounds(gfx::Rect());
782 keyboard_window->Show();
784 ui::test::TestEventHandler handler;
785 root_window->AddPreTargetHandler(&handler);
787 ui::test::EventGenerator event_generator(root_window, keyboard_window);
788 event_generator.ClickLeftButton();
789 int expected_mouse_presses = 1;
790 EXPECT_EQ(expected_mouse_presses, handler.num_mouse_events() / 2);
792 for (int block_reason = FIRST_BLOCK_REASON;
793 block_reason < NUMBER_OF_BLOCK_REASONS;
794 ++block_reason) {
795 BlockUserSession(static_cast<UserSessionBlockReason>(block_reason));
796 event_generator.ClickLeftButton();
797 expected_mouse_presses++;
798 EXPECT_EQ(expected_mouse_presses, handler.num_mouse_events() / 2);
799 UnblockUserSession();
801 root_window->RemovePreTargetHandler(&handler);
804 // Test for http://crbug.com/299787. RootWindowController should delete
805 // the old container since the keyboard controller creates a new window in
806 // GetWindowContainer().
807 TEST_F(VirtualKeyboardRootWindowControllerTest,
808 DeleteOldContainerOnVirtualKeyboardInit) {
809 aura::Window* root_window = Shell::GetPrimaryRootWindow();
810 aura::Window* keyboard_container =
811 Shell::GetContainer(root_window, kShellWindowId_VirtualKeyboardContainer);
812 ASSERT_TRUE(keyboard_container);
813 // Track the keyboard container window.
814 aura::WindowTracker tracker;
815 tracker.Add(keyboard_container);
816 // Mock a login user profile change to reinitialize the keyboard.
817 ash::Shell::GetInstance()->OnLoginUserProfilePrepared();
818 // keyboard_container should no longer be present.
819 EXPECT_FALSE(tracker.Contains(keyboard_container));
822 // Test for crbug.com/342524. After user login, the work space should restore to
823 // full screen.
824 TEST_F(VirtualKeyboardRootWindowControllerTest, RestoreWorkspaceAfterLogin) {
825 aura::Window* root_window = Shell::GetPrimaryRootWindow();
826 aura::Window* keyboard_container =
827 Shell::GetContainer(root_window, kShellWindowId_VirtualKeyboardContainer);
828 keyboard_container->Show();
829 keyboard::KeyboardController* controller =
830 keyboard::KeyboardController::GetInstance();
831 aura::Window* keyboard_window = controller->proxy()->GetKeyboardWindow();
832 keyboard_container->AddChild(keyboard_window);
833 keyboard_window->set_owned_by_parent(false);
834 keyboard_window->SetBounds(keyboard::FullWidthKeyboardBoundsFromRootBounds(
835 root_window->bounds(), 100));
836 keyboard_window->Show();
838 gfx::Rect before = ash::Shell::GetScreen()->GetPrimaryDisplay().work_area();
840 // Notify keyboard bounds changing.
841 controller->NotifyKeyboardBoundsChanging(keyboard_container->bounds());
843 if (!keyboard::IsKeyboardOverscrollEnabled()) {
844 gfx::Rect after = ash::Shell::GetScreen()->GetPrimaryDisplay().work_area();
845 EXPECT_LT(after, before);
848 // Mock a login user profile change to reinitialize the keyboard.
849 ash::Shell::GetInstance()->OnLoginUserProfilePrepared();
850 EXPECT_EQ(ash::Shell::GetScreen()->GetPrimaryDisplay().work_area(), before);
853 // Ensure that system modal dialogs do not block events targeted at the virtual
854 // keyboard.
855 TEST_F(VirtualKeyboardRootWindowControllerTest, ClickWithActiveModalDialog) {
856 aura::Window* root_window = Shell::GetPrimaryRootWindow();
857 aura::Window* keyboard_container =
858 Shell::GetContainer(root_window, kShellWindowId_VirtualKeyboardContainer);
859 ASSERT_TRUE(keyboard_container);
860 keyboard_container->Show();
862 aura::Window* keyboard_window = keyboard::KeyboardController::GetInstance()->
863 proxy()->GetKeyboardWindow();
864 keyboard_container->AddChild(keyboard_window);
865 keyboard_window->set_owned_by_parent(false);
866 keyboard_window->SetBounds(keyboard::FullWidthKeyboardBoundsFromRootBounds(
867 root_window->bounds(), 100));
869 ui::test::TestEventHandler handler;
870 root_window->AddPreTargetHandler(&handler);
871 ui::test::EventGenerator root_window_event_generator(root_window);
872 ui::test::EventGenerator keyboard_event_generator(root_window,
873 keyboard_window);
875 views::Widget* modal_widget =
876 CreateModalWidget(gfx::Rect(300, 10, 100, 100));
878 // Verify that mouse events to the root window are block with a visble modal
879 // dialog.
880 root_window_event_generator.ClickLeftButton();
881 EXPECT_EQ(0, handler.num_mouse_events());
883 // Verify that event dispatch to the virtual keyboard is unblocked.
884 keyboard_event_generator.ClickLeftButton();
885 EXPECT_EQ(1, handler.num_mouse_events() / 2);
887 modal_widget->Close();
889 // Verify that mouse events are now unblocked to the root window.
890 root_window_event_generator.ClickLeftButton();
891 EXPECT_EQ(2, handler.num_mouse_events() / 2);
892 root_window->RemovePreTargetHandler(&handler);
895 // Ensure that the visible area for scrolling the text caret excludes the
896 // region occluded by the on-screen keyboard.
897 TEST_F(VirtualKeyboardRootWindowControllerTest, EnsureCaretInWorkArea) {
898 keyboard::KeyboardController* keyboard_controller =
899 keyboard::KeyboardController::GetInstance();
900 keyboard::KeyboardControllerProxy* proxy = keyboard_controller->proxy();
902 MockTextInputClient text_input_client;
903 ui::InputMethod* input_method = proxy->GetInputMethod();
904 ASSERT_TRUE(input_method);
905 if (switches::IsTextInputFocusManagerEnabled()) {
906 ui::TextInputFocusManager::GetInstance()->FocusTextInputClient(
907 &text_input_client);
908 } else {
909 input_method->SetFocusedTextInputClient(&text_input_client);
912 aura::Window* root_window = Shell::GetPrimaryRootWindow();
913 aura::Window* keyboard_container =
914 Shell::GetContainer(root_window, kShellWindowId_VirtualKeyboardContainer);
915 ASSERT_TRUE(keyboard_container);
916 keyboard_container->Show();
918 const int keyboard_height = 100;
919 aura::Window* keyboard_window =proxy->GetKeyboardWindow();
920 keyboard_container->AddChild(keyboard_window);
921 keyboard_window->set_owned_by_parent(false);
922 keyboard_window->SetBounds(keyboard::FullWidthKeyboardBoundsFromRootBounds(
923 root_window->bounds(), keyboard_height));
925 proxy->EnsureCaretInWorkArea();
926 ASSERT_EQ(root_window->bounds().width(),
927 text_input_client.visible_rect().width());
928 ASSERT_EQ(root_window->bounds().height() - keyboard_height,
929 text_input_client.visible_rect().height());
931 if (switches::IsTextInputFocusManagerEnabled()) {
932 ui::TextInputFocusManager::GetInstance()->BlurTextInputClient(
933 &text_input_client);
934 } else {
935 input_method->SetFocusedTextInputClient(NULL);
939 // Tests that the virtual keyboard does not block context menus. The virtual
940 // keyboard should appear in front of most content, but not context menus. See
941 // crbug/377180.
942 TEST_F(VirtualKeyboardRootWindowControllerTest, ZOrderTest) {
943 UpdateDisplay("800x600");
944 keyboard::KeyboardController* keyboard_controller =
945 keyboard::KeyboardController::GetInstance();
946 keyboard::KeyboardControllerProxy* proxy = keyboard_controller->proxy();
948 aura::Window* root_window = Shell::GetPrimaryRootWindow();
949 aura::Window* keyboard_container =
950 Shell::GetContainer(root_window, kShellWindowId_VirtualKeyboardContainer);
951 ASSERT_TRUE(keyboard_container);
952 keyboard_container->Show();
954 const int keyboard_height = 200;
955 aura::Window* keyboard_window = proxy->GetKeyboardWindow();
956 keyboard_container->AddChild(keyboard_window);
957 keyboard_window->set_owned_by_parent(false);
958 gfx::Rect keyboard_bounds = keyboard::FullWidthKeyboardBoundsFromRootBounds(
959 root_window->bounds(), keyboard_height);
960 keyboard_window->SetBounds(keyboard_bounds);
961 keyboard_window->Show();
963 ui::test::EventGenerator generator(root_window);
965 // Cover the screen with two windows: a normal window on the left side and a
966 // context menu on the right side. When the virtual keyboard is displayed it
967 // partially occludes the normal window, but not the context menu. Compute
968 // positions for generating synthetic click events to perform hit tests,
969 // ensuring the correct window layering. 'top' is above the VK, whereas
970 // 'bottom' lies within the VK. 'left' is centered in the normal window, and
971 // 'right' is centered in the context menu.
972 int window_height = keyboard_bounds.bottom();
973 int window_width = keyboard_bounds.width() / 2;
974 int left = window_width / 2;
975 int right = 3 * window_width / 2;
976 int top = keyboard_bounds.y() / 2;
977 int bottom = window_height - keyboard_height / 2;
979 // Normal window is partially occluded by the virtual keyboard.
980 aura::test::TestWindowDelegate delegate;
981 scoped_ptr<aura::Window> normal(CreateTestWindowInShellWithDelegateAndType(
982 &delegate,
983 ui::wm::WINDOW_TYPE_NORMAL,
985 gfx::Rect(0, 0, window_width, window_height)));
986 normal->set_owned_by_parent(false);
987 normal->Show();
988 TargetHitTestEventHandler normal_handler;
989 normal->AddPreTargetHandler(&normal_handler);
991 // Test that only the click on the top portion of the window is picked up. The
992 // click on the bottom hits the virtual keyboard instead.
993 generator.MoveMouseTo(left, top);
994 generator.ClickLeftButton();
995 EXPECT_EQ(1, normal_handler.num_mouse_events());
996 generator.MoveMouseTo(left, bottom);
997 generator.ClickLeftButton();
998 EXPECT_EQ(1, normal_handler.num_mouse_events());
1000 // Menu overlaps virtual keyboard.
1001 aura::test::TestWindowDelegate delegate2;
1002 scoped_ptr<aura::Window> menu(CreateTestWindowInShellWithDelegateAndType(
1003 &delegate2,
1004 ui::wm::WINDOW_TYPE_MENU,
1006 gfx::Rect(window_width, 0, window_width, window_height)));
1007 menu->set_owned_by_parent(false);
1008 menu->Show();
1009 TargetHitTestEventHandler menu_handler;
1010 menu->AddPreTargetHandler(&menu_handler);
1012 // Test that both clicks register.
1013 generator.MoveMouseTo(right, top);
1014 generator.ClickLeftButton();
1015 EXPECT_EQ(1, menu_handler.num_mouse_events());
1016 generator.MoveMouseTo(right, bottom);
1017 generator.ClickLeftButton();
1018 EXPECT_EQ(2, menu_handler.num_mouse_events());
1020 // Cleanup to ensure that the test windows are destroyed before their
1021 // delegates.
1022 normal.reset();
1023 menu.reset();
1026 // Resolution in UpdateDisplay is not being respected on Windows 8.
1027 #if defined(OS_WIN)
1028 #define MAYBE_DisplayRotation DISABLED_DisplayRotation
1029 #else
1030 #define MAYBE_DisplayRotation DisplayRotation
1031 #endif
1033 // Tests that the virtual keyboard correctly resizes with a change to display
1034 // orientation. See crbug/417612.
1035 TEST_F(VirtualKeyboardRootWindowControllerTest, MAYBE_DisplayRotation) {
1036 UpdateDisplay("800x600");
1037 aura::Window* root_window = Shell::GetPrimaryRootWindow();
1038 aura::Window* keyboard_container =
1039 Shell::GetContainer(root_window, kShellWindowId_VirtualKeyboardContainer);
1040 ASSERT_TRUE(keyboard_container);
1041 keyboard::KeyboardController* keyboard_controller =
1042 keyboard::KeyboardController::GetInstance();
1043 keyboard_controller->ShowKeyboard(false);
1044 keyboard_controller->proxy()->GetKeyboardWindow()->SetBounds(
1045 gfx::Rect(0, 400, 800, 200));
1046 EXPECT_EQ("0,400 800x200", keyboard_container->bounds().ToString());
1048 UpdateDisplay("600x800");
1049 EXPECT_EQ("0,600 600x200", keyboard_container->bounds().ToString());
1052 } // namespace test
1053 } // namespace ash