Popular sites on the NTP: Try to keep the ordering constant
[chromium-blink-merge.git] / ash / root_window_controller_unittest.cc
blob923fdce697cdaf4d77c610eb3c50e2dd9f4d5096
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/events/test/event_generator.h"
34 #include "ui/events/test/test_event_handler.h"
35 #include "ui/keyboard/keyboard_controller_proxy.h"
36 #include "ui/keyboard/keyboard_switches.h"
37 #include "ui/keyboard/keyboard_util.h"
38 #include "ui/views/controls/menu/menu_controller.h"
39 #include "ui/views/widget/widget.h"
40 #include "ui/views/widget/widget_delegate.h"
42 using aura::Window;
43 using views::Widget;
45 namespace ash {
46 namespace {
48 class TestDelegate : public views::WidgetDelegateView {
49 public:
50 explicit TestDelegate(bool system_modal) : system_modal_(system_modal) {}
51 ~TestDelegate() override {}
53 // Overridden from views::WidgetDelegate:
54 views::View* GetContentsView() override { return this; }
56 ui::ModalType GetModalType() const override {
57 return system_modal_ ? ui::MODAL_TYPE_SYSTEM : ui::MODAL_TYPE_NONE;
60 private:
61 bool system_modal_;
63 DISALLOW_COPY_AND_ASSIGN(TestDelegate);
66 class DeleteOnBlurDelegate : public aura::test::TestWindowDelegate,
67 public aura::client::FocusChangeObserver {
68 public:
69 DeleteOnBlurDelegate() : window_(NULL) {}
70 ~DeleteOnBlurDelegate() override {}
72 void SetWindow(aura::Window* window) {
73 window_ = window;
74 aura::client::SetFocusChangeObserver(window_, this);
77 private:
78 // aura::test::TestWindowDelegate overrides:
79 bool CanFocus() override { return true; }
81 // aura::client::FocusChangeObserver implementation:
82 void OnWindowFocused(aura::Window* gained_focus,
83 aura::Window* lost_focus) override {
84 if (window_ == lost_focus)
85 delete window_;
88 aura::Window* window_;
90 DISALLOW_COPY_AND_ASSIGN(DeleteOnBlurDelegate);
93 } // namespace
95 namespace test {
97 class RootWindowControllerTest : public test::AshTestBase {
98 public:
99 views::Widget* CreateTestWidget(const gfx::Rect& bounds) {
100 views::Widget* widget = views::Widget::CreateWindowWithContextAndBounds(
101 NULL, CurrentContext(), bounds);
102 widget->Show();
103 return widget;
106 views::Widget* CreateModalWidget(const gfx::Rect& bounds) {
107 views::Widget* widget = views::Widget::CreateWindowWithContextAndBounds(
108 new TestDelegate(true), CurrentContext(), bounds);
109 widget->Show();
110 return widget;
113 views::Widget* CreateModalWidgetWithParent(const gfx::Rect& bounds,
114 gfx::NativeWindow parent) {
115 views::Widget* widget =
116 views::Widget::CreateWindowWithParentAndBounds(new TestDelegate(true),
117 parent,
118 bounds);
119 widget->Show();
120 return widget;
123 aura::Window* GetModalContainer(aura::Window* root_window) {
124 return Shell::GetContainer(root_window,
125 ash::kShellWindowId_SystemModalContainer);
129 TEST_F(RootWindowControllerTest, MoveWindows_Basic) {
130 if (!SupportsMultipleDisplays())
131 return;
132 // Windows origin should be doubled when moved to the 1st display.
133 UpdateDisplay("600x600,300x300");
134 aura::Window::Windows root_windows = Shell::GetAllRootWindows();
135 RootWindowController* controller = Shell::GetPrimaryRootWindowController();
136 ShelfLayoutManager* shelf_layout_manager =
137 controller->GetShelfLayoutManager();
138 shelf_layout_manager->SetAutoHideBehavior(
139 ash::SHELF_AUTO_HIDE_BEHAVIOR_ALWAYS);
141 views::Widget* normal = CreateTestWidget(gfx::Rect(650, 10, 100, 100));
142 EXPECT_EQ(root_windows[1], normal->GetNativeView()->GetRootWindow());
143 EXPECT_EQ("650,10 100x100", normal->GetWindowBoundsInScreen().ToString());
144 EXPECT_EQ("50,10 100x100",
145 normal->GetNativeView()->GetBoundsInRootWindow().ToString());
147 views::Widget* maximized = CreateTestWidget(gfx::Rect(700, 10, 100, 100));
148 maximized->Maximize();
149 EXPECT_EQ(root_windows[1], maximized->GetNativeView()->GetRootWindow());
150 EXPECT_EQ("600,0 300x253", maximized->GetWindowBoundsInScreen().ToString());
151 EXPECT_EQ("0,0 300x253",
152 maximized->GetNativeView()->GetBoundsInRootWindow().ToString());
154 views::Widget* minimized = CreateTestWidget(gfx::Rect(800, 10, 100, 100));
155 minimized->Minimize();
156 EXPECT_EQ(root_windows[1], minimized->GetNativeView()->GetRootWindow());
157 EXPECT_EQ("800,10 100x100",
158 minimized->GetWindowBoundsInScreen().ToString());
160 views::Widget* fullscreen = CreateTestWidget(gfx::Rect(850, 10, 100, 100));
161 fullscreen->SetFullscreen(true);
162 EXPECT_EQ(root_windows[1], fullscreen->GetNativeView()->GetRootWindow());
164 EXPECT_EQ("600,0 300x300",
165 fullscreen->GetWindowBoundsInScreen().ToString());
166 EXPECT_EQ("0,0 300x300",
167 fullscreen->GetNativeView()->GetBoundsInRootWindow().ToString());
169 views::Widget* unparented_control = new Widget;
170 Widget::InitParams params;
171 params.bounds = gfx::Rect(650, 10, 100, 100);
172 params.context = CurrentContext();
173 params.type = Widget::InitParams::TYPE_CONTROL;
174 unparented_control->Init(params);
175 EXPECT_EQ(root_windows[1],
176 unparented_control->GetNativeView()->GetRootWindow());
177 EXPECT_EQ(kShellWindowId_UnparentedControlContainer,
178 unparented_control->GetNativeView()->parent()->id());
180 aura::Window* panel = CreateTestWindowInShellWithDelegateAndType(
181 NULL, ui::wm::WINDOW_TYPE_PANEL, 0, gfx::Rect(700, 100, 100, 100));
182 EXPECT_EQ(root_windows[1], panel->GetRootWindow());
183 EXPECT_EQ(kShellWindowId_PanelContainer, panel->parent()->id());
185 // Make sure a window that will delete itself when losing focus
186 // will not crash.
187 aura::WindowTracker tracker;
188 DeleteOnBlurDelegate delete_on_blur_delegate;
189 aura::Window* d2 = CreateTestWindowInShellWithDelegate(
190 &delete_on_blur_delegate, 0, gfx::Rect(50, 50, 100, 100));
191 delete_on_blur_delegate.SetWindow(d2);
192 aura::client::GetFocusClient(root_windows[0])->FocusWindow(d2);
193 tracker.Add(d2);
195 UpdateDisplay("600x600");
197 // d2 must have been deleted.
198 EXPECT_FALSE(tracker.Contains(d2));
200 EXPECT_EQ(root_windows[0], normal->GetNativeView()->GetRootWindow());
201 EXPECT_EQ("100,20 100x100", normal->GetWindowBoundsInScreen().ToString());
202 EXPECT_EQ("100,20 100x100",
203 normal->GetNativeView()->GetBoundsInRootWindow().ToString());
205 // Maximized area on primary display has 3px (given as
206 // kAutoHideSize in shelf_layout_manager.cc) inset at the bottom.
208 // First clear fullscreen status, since both fullscreen and maximized windows
209 // share the same desktop workspace, which cancels the shelf status.
210 fullscreen->SetFullscreen(false);
211 EXPECT_EQ(root_windows[0], maximized->GetNativeView()->GetRootWindow());
212 EXPECT_EQ("0,0 600x597",
213 maximized->GetWindowBoundsInScreen().ToString());
214 EXPECT_EQ("0,0 600x597",
215 maximized->GetNativeView()->GetBoundsInRootWindow().ToString());
217 // Set fullscreen to true. In that case the 3px inset becomes invisible so
218 // the maximized window can also use the area fully.
219 fullscreen->SetFullscreen(true);
220 EXPECT_EQ(root_windows[0], maximized->GetNativeView()->GetRootWindow());
221 EXPECT_EQ("0,0 600x600",
222 maximized->GetWindowBoundsInScreen().ToString());
223 EXPECT_EQ("0,0 600x600",
224 maximized->GetNativeView()->GetBoundsInRootWindow().ToString());
226 EXPECT_EQ(root_windows[0], minimized->GetNativeView()->GetRootWindow());
227 EXPECT_EQ("400,20 100x100",
228 minimized->GetWindowBoundsInScreen().ToString());
230 EXPECT_EQ(root_windows[0], fullscreen->GetNativeView()->GetRootWindow());
231 EXPECT_TRUE(fullscreen->IsFullscreen());
232 EXPECT_EQ("0,0 600x600",
233 fullscreen->GetWindowBoundsInScreen().ToString());
234 EXPECT_EQ("0,0 600x600",
235 fullscreen->GetNativeView()->GetBoundsInRootWindow().ToString());
237 // Test if the restore bounds are correctly updated.
238 wm::GetWindowState(maximized->GetNativeView())->Restore();
239 EXPECT_EQ("200,20 100x100", maximized->GetWindowBoundsInScreen().ToString());
240 EXPECT_EQ("200,20 100x100",
241 maximized->GetNativeView()->GetBoundsInRootWindow().ToString());
243 fullscreen->SetFullscreen(false);
244 EXPECT_EQ("500,20 100x100",
245 fullscreen->GetWindowBoundsInScreen().ToString());
246 EXPECT_EQ("500,20 100x100",
247 fullscreen->GetNativeView()->GetBoundsInRootWindow().ToString());
249 // Test if the unparented widget has moved.
250 EXPECT_EQ(root_windows[0],
251 unparented_control->GetNativeView()->GetRootWindow());
252 EXPECT_EQ(kShellWindowId_UnparentedControlContainer,
253 unparented_control->GetNativeView()->parent()->id());
255 // Test if the panel has moved.
256 EXPECT_EQ(root_windows[0], panel->GetRootWindow());
257 EXPECT_EQ(kShellWindowId_PanelContainer, panel->parent()->id());
260 TEST_F(RootWindowControllerTest, MoveWindows_Modal) {
261 if (!SupportsMultipleDisplays())
262 return;
264 UpdateDisplay("500x500,500x500");
266 aura::Window::Windows root_windows = Shell::GetAllRootWindows();
267 // Emulate virtual screen coordinate system.
268 root_windows[0]->SetBounds(gfx::Rect(0, 0, 500, 500));
269 root_windows[1]->SetBounds(gfx::Rect(500, 0, 500, 500));
271 views::Widget* normal = CreateTestWidget(gfx::Rect(300, 10, 100, 100));
272 EXPECT_EQ(root_windows[0], normal->GetNativeView()->GetRootWindow());
273 EXPECT_TRUE(wm::IsActiveWindow(normal->GetNativeView()));
275 views::Widget* modal = CreateModalWidget(gfx::Rect(650, 10, 100, 100));
276 EXPECT_EQ(root_windows[1], modal->GetNativeView()->GetRootWindow());
277 EXPECT_TRUE(GetModalContainer(root_windows[1])->Contains(
278 modal->GetNativeView()));
279 EXPECT_TRUE(wm::IsActiveWindow(modal->GetNativeView()));
281 ui::test::EventGenerator generator_1st(root_windows[0]);
282 generator_1st.ClickLeftButton();
283 EXPECT_TRUE(wm::IsActiveWindow(modal->GetNativeView()));
285 UpdateDisplay("500x500");
286 EXPECT_EQ(root_windows[0], modal->GetNativeView()->GetRootWindow());
287 EXPECT_TRUE(wm::IsActiveWindow(modal->GetNativeView()));
288 generator_1st.ClickLeftButton();
289 EXPECT_TRUE(wm::IsActiveWindow(modal->GetNativeView()));
292 // Make sure lock related windows moves.
293 TEST_F(RootWindowControllerTest, MoveWindows_LockWindowsInUnified) {
294 if (!SupportsMultipleDisplays())
295 return;
296 Shell::GetInstance()->display_manager()->SetUnifiedDesktopEnabled(true);
298 UpdateDisplay("500x500");
299 const int kLockScreenWindowId = 1000;
300 const int kLockBackgroundWindowId = 1001;
302 RootWindowController* controller =
303 Shell::GetInstance()->GetPrimaryRootWindowController();
305 aura::Window* lock_container =
306 controller->GetContainer(kShellWindowId_LockScreenContainer);
307 aura::Window* lock_background_container =
308 controller->GetContainer(kShellWindowId_LockScreenBackgroundContainer);
310 views::Widget* lock_screen =
311 CreateModalWidgetWithParent(gfx::Rect(10, 10, 100, 100), lock_container);
312 lock_screen->GetNativeWindow()->set_id(kLockScreenWindowId);
313 lock_screen->SetFullscreen(true);
315 views::Widget* lock_background = CreateModalWidgetWithParent(
316 gfx::Rect(10, 10, 100, 100), lock_background_container);
317 lock_background->GetNativeWindow()->set_id(kLockBackgroundWindowId);
319 ASSERT_EQ(lock_screen->GetNativeWindow(),
320 controller->GetRootWindow()->GetChildById(kLockScreenWindowId));
321 ASSERT_EQ(lock_background->GetNativeWindow(),
322 controller->GetRootWindow()->GetChildById(kLockBackgroundWindowId));
323 EXPECT_EQ("0,0 500x500", lock_screen->GetNativeWindow()->bounds().ToString());
325 // Switch to unified.
326 UpdateDisplay("500x500,500x500");
328 // In unified mode, RWC is created
329 controller = Shell::GetInstance()->GetPrimaryRootWindowController();
331 ASSERT_EQ(lock_screen->GetNativeWindow(),
332 controller->GetRootWindow()->GetChildById(kLockScreenWindowId));
333 ASSERT_EQ(lock_background->GetNativeWindow(),
334 controller->GetRootWindow()->GetChildById(kLockBackgroundWindowId));
335 EXPECT_EQ("0,0 500x500", lock_screen->GetNativeWindow()->bounds().ToString());
337 // Switch to mirror.
338 DisplayManager* display_manager = Shell::GetInstance()->display_manager();
339 display_manager->SetMirrorMode(true);
340 EXPECT_TRUE(display_manager->IsInMirrorMode());
342 controller = Shell::GetInstance()->GetPrimaryRootWindowController();
343 ASSERT_EQ(lock_screen->GetNativeWindow(),
344 controller->GetRootWindow()->GetChildById(kLockScreenWindowId));
345 ASSERT_EQ(lock_background->GetNativeWindow(),
346 controller->GetRootWindow()->GetChildById(kLockBackgroundWindowId));
347 EXPECT_EQ("0,0 500x500", lock_screen->GetNativeWindow()->bounds().ToString());
349 // Switch to unified.
350 display_manager->SetMirrorMode(false);
351 EXPECT_TRUE(display_manager->IsInUnifiedMode());
353 controller = Shell::GetInstance()->GetPrimaryRootWindowController();
355 ASSERT_EQ(lock_screen->GetNativeWindow(),
356 controller->GetRootWindow()->GetChildById(kLockScreenWindowId));
357 ASSERT_EQ(lock_background->GetNativeWindow(),
358 controller->GetRootWindow()->GetChildById(kLockBackgroundWindowId));
359 EXPECT_EQ("0,0 500x500", lock_screen->GetNativeWindow()->bounds().ToString());
361 // Switch to single display.
362 UpdateDisplay("600x500");
363 EXPECT_FALSE(display_manager->IsInUnifiedMode());
364 EXPECT_FALSE(display_manager->IsInMirrorMode());
366 controller = Shell::GetInstance()->GetPrimaryRootWindowController();
368 ASSERT_EQ(lock_screen->GetNativeWindow(),
369 controller->GetRootWindow()->GetChildById(kLockScreenWindowId));
370 ASSERT_EQ(lock_background->GetNativeWindow(),
371 controller->GetRootWindow()->GetChildById(kLockBackgroundWindowId));
372 EXPECT_EQ("0,0 600x500", lock_screen->GetNativeWindow()->bounds().ToString());
375 TEST_F(RootWindowControllerTest, ModalContainer) {
376 UpdateDisplay("600x600");
377 Shell* shell = Shell::GetInstance();
378 RootWindowController* controller = shell->GetPrimaryRootWindowController();
379 EXPECT_EQ(user::LOGGED_IN_USER,
380 shell->system_tray_delegate()->GetUserLoginStatus());
381 EXPECT_EQ(controller->GetContainer(kShellWindowId_SystemModalContainer)
382 ->layout_manager(),
383 controller->GetSystemModalLayoutManager(NULL));
385 views::Widget* session_modal_widget =
386 CreateModalWidget(gfx::Rect(300, 10, 100, 100));
387 EXPECT_EQ(controller->GetContainer(kShellWindowId_SystemModalContainer)
388 ->layout_manager(),
389 controller->GetSystemModalLayoutManager(
390 session_modal_widget->GetNativeView()));
392 shell->session_state_delegate()->LockScreen();
393 EXPECT_EQ(user::LOGGED_IN_LOCKED,
394 shell->system_tray_delegate()->GetUserLoginStatus());
395 EXPECT_EQ(controller->GetContainer(kShellWindowId_LockSystemModalContainer)
396 ->layout_manager(),
397 controller->GetSystemModalLayoutManager(NULL));
399 aura::Window* lock_container =
400 controller->GetContainer(kShellWindowId_LockScreenContainer);
401 views::Widget* lock_modal_widget =
402 CreateModalWidgetWithParent(gfx::Rect(300, 10, 100, 100), lock_container);
403 EXPECT_EQ(controller->GetContainer(kShellWindowId_LockSystemModalContainer)
404 ->layout_manager(),
405 controller->GetSystemModalLayoutManager(
406 lock_modal_widget->GetNativeView()));
407 EXPECT_EQ(controller->GetContainer(kShellWindowId_SystemModalContainer)
408 ->layout_manager(),
409 controller->GetSystemModalLayoutManager(
410 session_modal_widget->GetNativeView()));
412 shell->session_state_delegate()->UnlockScreen();
415 TEST_F(RootWindowControllerTest, ModalContainerNotLoggedInLoggedIn) {
416 UpdateDisplay("600x600");
417 Shell* shell = Shell::GetInstance();
419 // Configure login screen environment.
420 SetUserLoggedIn(false);
421 EXPECT_EQ(user::LOGGED_IN_NONE,
422 shell->system_tray_delegate()->GetUserLoginStatus());
423 EXPECT_EQ(0, shell->session_state_delegate()->NumberOfLoggedInUsers());
424 EXPECT_FALSE(shell->session_state_delegate()->IsActiveUserSessionStarted());
426 RootWindowController* controller = shell->GetPrimaryRootWindowController();
427 EXPECT_EQ(controller->GetContainer(kShellWindowId_LockSystemModalContainer)
428 ->layout_manager(),
429 controller->GetSystemModalLayoutManager(NULL));
431 aura::Window* lock_container =
432 controller->GetContainer(kShellWindowId_LockScreenContainer);
433 views::Widget* login_modal_widget =
434 CreateModalWidgetWithParent(gfx::Rect(300, 10, 100, 100), lock_container);
435 EXPECT_EQ(controller->GetContainer(kShellWindowId_LockSystemModalContainer)
436 ->layout_manager(),
437 controller->GetSystemModalLayoutManager(
438 login_modal_widget->GetNativeView()));
439 login_modal_widget->Close();
441 // Configure user session environment.
442 SetUserLoggedIn(true);
443 SetSessionStarted(true);
444 EXPECT_EQ(user::LOGGED_IN_USER,
445 shell->system_tray_delegate()->GetUserLoginStatus());
446 EXPECT_EQ(1, shell->session_state_delegate()->NumberOfLoggedInUsers());
447 EXPECT_TRUE(shell->session_state_delegate()->IsActiveUserSessionStarted());
448 EXPECT_EQ(controller->GetContainer(kShellWindowId_SystemModalContainer)
449 ->layout_manager(),
450 controller->GetSystemModalLayoutManager(NULL));
452 views::Widget* session_modal_widget =
453 CreateModalWidget(gfx::Rect(300, 10, 100, 100));
454 EXPECT_EQ(controller->GetContainer(kShellWindowId_SystemModalContainer)
455 ->layout_manager(),
456 controller->GetSystemModalLayoutManager(
457 session_modal_widget->GetNativeView()));
460 TEST_F(RootWindowControllerTest, ModalContainerBlockedSession) {
461 UpdateDisplay("600x600");
462 RootWindowController* controller = Shell::GetPrimaryRootWindowController();
463 aura::Window* lock_container =
464 controller->GetContainer(kShellWindowId_LockScreenContainer);
465 for (int block_reason = FIRST_BLOCK_REASON;
466 block_reason < NUMBER_OF_BLOCK_REASONS;
467 ++block_reason) {
468 views::Widget* session_modal_widget =
469 CreateModalWidget(gfx::Rect(300, 10, 100, 100));
470 EXPECT_EQ(controller->GetContainer(kShellWindowId_SystemModalContainer)
471 ->layout_manager(),
472 controller->GetSystemModalLayoutManager(
473 session_modal_widget->GetNativeView()));
474 EXPECT_EQ(controller->GetContainer(kShellWindowId_SystemModalContainer)
475 ->layout_manager(),
476 controller->GetSystemModalLayoutManager(NULL));
477 session_modal_widget->Close();
479 BlockUserSession(static_cast<UserSessionBlockReason>(block_reason));
481 EXPECT_EQ(controller->GetContainer(kShellWindowId_LockSystemModalContainer)
482 ->layout_manager(),
483 controller->GetSystemModalLayoutManager(NULL));
485 views::Widget* lock_modal_widget =
486 CreateModalWidgetWithParent(gfx::Rect(300, 10, 100, 100),
487 lock_container);
488 EXPECT_EQ(controller->GetContainer(kShellWindowId_LockSystemModalContainer)
489 ->layout_manager(),
490 controller->GetSystemModalLayoutManager(
491 lock_modal_widget->GetNativeView()));
493 session_modal_widget =
494 CreateModalWidget(gfx::Rect(300, 10, 100, 100));
495 EXPECT_EQ(controller->GetContainer(kShellWindowId_SystemModalContainer)
496 ->layout_manager(),
497 controller->GetSystemModalLayoutManager(
498 session_modal_widget->GetNativeView()));
499 session_modal_widget->Close();
501 lock_modal_widget->Close();
502 UnblockUserSession();
506 TEST_F(RootWindowControllerTest, GetWindowForFullscreenMode) {
507 UpdateDisplay("600x600");
508 RootWindowController* controller =
509 Shell::GetInstance()->GetPrimaryRootWindowController();
511 Widget* w1 = CreateTestWidget(gfx::Rect(0, 0, 100, 100));
512 w1->Maximize();
513 Widget* w2 = CreateTestWidget(gfx::Rect(0, 0, 100, 100));
514 w2->SetFullscreen(true);
515 // |w3| is a transient child of |w2|.
516 Widget* w3 = Widget::CreateWindowWithParentAndBounds(NULL,
517 w2->GetNativeWindow(), gfx::Rect(0, 0, 100, 100));
519 // Test that GetWindowForFullscreenMode() finds the fullscreen window when one
520 // of its transient children is active.
521 w3->Activate();
522 EXPECT_EQ(w2->GetNativeWindow(), controller->GetWindowForFullscreenMode());
524 // If the topmost window is not fullscreen, it returns NULL.
525 w1->Activate();
526 EXPECT_EQ(NULL, controller->GetWindowForFullscreenMode());
527 w1->Close();
528 w3->Close();
530 // Only w2 remains, if minimized GetWindowForFullscreenMode should return
531 // NULL.
532 w2->Activate();
533 EXPECT_EQ(w2->GetNativeWindow(), controller->GetWindowForFullscreenMode());
534 w2->Minimize();
535 EXPECT_EQ(NULL, controller->GetWindowForFullscreenMode());
538 TEST_F(RootWindowControllerTest, MultipleDisplaysGetWindowForFullscreenMode) {
539 if (!SupportsMultipleDisplays())
540 return;
542 UpdateDisplay("600x600,600x600");
543 Shell::RootWindowControllerList controllers =
544 Shell::GetInstance()->GetAllRootWindowControllers();
546 Widget* w1 = CreateTestWidget(gfx::Rect(0, 0, 100, 100));
547 w1->Maximize();
548 Widget* w2 = CreateTestWidget(gfx::Rect(0, 0, 100, 100));
549 w2->SetFullscreen(true);
550 Widget* w3 = CreateTestWidget(gfx::Rect(600, 0, 100, 100));
552 EXPECT_EQ(w1->GetNativeWindow()->GetRootWindow(),
553 controllers[0]->GetRootWindow());
554 EXPECT_EQ(w2->GetNativeWindow()->GetRootWindow(),
555 controllers[0]->GetRootWindow());
556 EXPECT_EQ(w3->GetNativeWindow()->GetRootWindow(),
557 controllers[1]->GetRootWindow());
559 w1->Activate();
560 EXPECT_EQ(NULL, controllers[0]->GetWindowForFullscreenMode());
561 EXPECT_EQ(NULL, controllers[1]->GetWindowForFullscreenMode());
563 w2->Activate();
564 EXPECT_EQ(w2->GetNativeWindow(),
565 controllers[0]->GetWindowForFullscreenMode());
566 EXPECT_EQ(NULL, controllers[1]->GetWindowForFullscreenMode());
568 // Verify that the first root window controller remains in fullscreen mode
569 // when a window on the other display is activated.
570 w3->Activate();
571 EXPECT_EQ(w2->GetNativeWindow(),
572 controllers[0]->GetWindowForFullscreenMode());
573 EXPECT_EQ(NULL, controllers[1]->GetWindowForFullscreenMode());
576 // Test that user session window can't be focused if user session blocked by
577 // some overlapping UI.
578 TEST_F(RootWindowControllerTest, FocusBlockedWindow) {
579 UpdateDisplay("600x600");
580 RootWindowController* controller =
581 Shell::GetInstance()->GetPrimaryRootWindowController();
582 aura::Window* lock_container =
583 controller->GetContainer(kShellWindowId_LockScreenContainer);
584 aura::Window* lock_window = Widget::CreateWindowWithParentAndBounds(NULL,
585 lock_container, gfx::Rect(0, 0, 100, 100))->GetNativeView();
586 lock_window->Show();
587 aura::Window* session_window =
588 CreateTestWidget(gfx::Rect(0, 0, 100, 100))->GetNativeView();
589 session_window->Show();
591 for (int block_reason = FIRST_BLOCK_REASON;
592 block_reason < NUMBER_OF_BLOCK_REASONS;
593 ++block_reason) {
594 BlockUserSession(static_cast<UserSessionBlockReason>(block_reason));
595 lock_window->Focus();
596 EXPECT_TRUE(lock_window->HasFocus());
597 session_window->Focus();
598 EXPECT_FALSE(session_window->HasFocus());
599 UnblockUserSession();
603 // Tracks whether OnWindowDestroying() has been invoked.
604 class DestroyedWindowObserver : public aura::WindowObserver {
605 public:
606 DestroyedWindowObserver() : destroyed_(false), window_(NULL) {}
607 ~DestroyedWindowObserver() override { Shutdown(); }
609 void SetWindow(Window* window) {
610 window_ = window;
611 window->AddObserver(this);
614 bool destroyed() const { return destroyed_; }
616 // WindowObserver overrides:
617 void OnWindowDestroying(Window* window) override {
618 destroyed_ = true;
619 Shutdown();
622 private:
623 void Shutdown() {
624 if (!window_)
625 return;
626 window_->RemoveObserver(this);
627 window_ = NULL;
630 bool destroyed_;
631 Window* window_;
633 DISALLOW_COPY_AND_ASSIGN(DestroyedWindowObserver);
636 // Verifies shutdown doesn't delete windows that are not owned by the parent.
637 TEST_F(RootWindowControllerTest, DontDeleteWindowsNotOwnedByParent) {
638 DestroyedWindowObserver observer1;
639 aura::test::TestWindowDelegate delegate1;
640 aura::Window* window1 = new aura::Window(&delegate1);
641 window1->SetType(ui::wm::WINDOW_TYPE_CONTROL);
642 window1->set_owned_by_parent(false);
643 observer1.SetWindow(window1);
644 window1->Init(ui::LAYER_NOT_DRAWN);
645 aura::client::ParentWindowWithContext(
646 window1, Shell::GetInstance()->GetPrimaryRootWindow(), gfx::Rect());
648 DestroyedWindowObserver observer2;
649 aura::Window* window2 = new aura::Window(NULL);
650 window2->set_owned_by_parent(false);
651 observer2.SetWindow(window2);
652 window2->Init(ui::LAYER_NOT_DRAWN);
653 Shell::GetInstance()->GetPrimaryRootWindow()->AddChild(window2);
655 Shell::GetInstance()->GetPrimaryRootWindowController()->CloseChildWindows();
657 ASSERT_FALSE(observer1.destroyed());
658 delete window1;
660 ASSERT_FALSE(observer2.destroyed());
661 delete window2;
664 typedef test::NoSessionAshTestBase NoSessionRootWindowControllerTest;
666 // Make sure that an event handler exists for entire display area.
667 TEST_F(NoSessionRootWindowControllerTest, Event) {
668 // Hide the shelf since it might otherwise get an event target.
669 RootWindowController* controller = Shell::GetPrimaryRootWindowController();
670 ShelfLayoutManager* shelf_layout_manager =
671 controller->GetShelfLayoutManager();
672 shelf_layout_manager->SetAutoHideBehavior(
673 ash::SHELF_AUTO_HIDE_ALWAYS_HIDDEN);
675 aura::Window* root = Shell::GetPrimaryRootWindow();
676 const gfx::Size size = root->bounds().size();
677 aura::Window* event_target = root->GetEventHandlerForPoint(gfx::Point(0, 0));
678 EXPECT_TRUE(event_target);
679 EXPECT_EQ(event_target,
680 root->GetEventHandlerForPoint(gfx::Point(0, size.height() - 1)));
681 EXPECT_EQ(event_target,
682 root->GetEventHandlerForPoint(gfx::Point(size.width() - 1, 0)));
683 EXPECT_EQ(event_target,
684 root->GetEventHandlerForPoint(gfx::Point(0, size.height() - 1)));
685 EXPECT_EQ(event_target,
686 root->GetEventHandlerForPoint(
687 gfx::Point(size.width() - 1, size.height() - 1)));
690 class VirtualKeyboardRootWindowControllerTest
691 : public RootWindowControllerTest {
692 public:
693 VirtualKeyboardRootWindowControllerTest() {}
694 ~VirtualKeyboardRootWindowControllerTest() override {}
696 void SetUp() override {
697 base::CommandLine::ForCurrentProcess()->AppendSwitch(
698 keyboard::switches::kEnableVirtualKeyboard);
699 test::AshTestBase::SetUp();
700 Shell::GetPrimaryRootWindowController()->ActivateKeyboard(
701 keyboard::KeyboardController::GetInstance());
704 private:
705 DISALLOW_COPY_AND_ASSIGN(VirtualKeyboardRootWindowControllerTest);
708 class MockTextInputClient : public ui::DummyTextInputClient {
709 public:
710 MockTextInputClient() :
711 ui::DummyTextInputClient(ui::TEXT_INPUT_TYPE_TEXT) {}
713 void EnsureCaretInRect(const gfx::Rect& rect) override {
714 visible_rect_ = rect;
717 const gfx::Rect& visible_rect() const {
718 return visible_rect_;
721 private:
722 gfx::Rect visible_rect_;
724 DISALLOW_COPY_AND_ASSIGN(MockTextInputClient);
727 class TargetHitTestEventHandler : public ui::test::TestEventHandler {
728 public:
729 TargetHitTestEventHandler() {}
731 // ui::test::TestEventHandler overrides.
732 void OnMouseEvent(ui::MouseEvent* event) override {
733 if (event->type() == ui::ET_MOUSE_PRESSED)
734 ui::test::TestEventHandler::OnMouseEvent(event);
735 event->StopPropagation();
738 private:
739 DISALLOW_COPY_AND_ASSIGN(TargetHitTestEventHandler);
742 // Test for http://crbug.com/297858. Virtual keyboard container should only show
743 // on primary root window.
744 TEST_F(VirtualKeyboardRootWindowControllerTest,
745 VirtualKeyboardOnPrimaryRootWindowOnly) {
746 if (!SupportsMultipleDisplays())
747 return;
749 UpdateDisplay("500x500,500x500");
751 aura::Window::Windows root_windows = Shell::GetAllRootWindows();
752 aura::Window* primary_root_window = Shell::GetPrimaryRootWindow();
753 aura::Window* secondary_root_window =
754 root_windows[0] == primary_root_window ?
755 root_windows[1] : root_windows[0];
757 ASSERT_TRUE(Shell::GetContainer(primary_root_window,
758 kShellWindowId_VirtualKeyboardContainer));
759 ASSERT_FALSE(Shell::GetContainer(secondary_root_window,
760 kShellWindowId_VirtualKeyboardContainer));
763 // Test for http://crbug.com/263599. Virtual keyboard should be able to receive
764 // events at blocked user session.
765 TEST_F(VirtualKeyboardRootWindowControllerTest,
766 ClickVirtualKeyboardInBlockedWindow) {
767 aura::Window* root_window = Shell::GetPrimaryRootWindow();
768 aura::Window* keyboard_container =
769 Shell::GetContainer(root_window, kShellWindowId_VirtualKeyboardContainer);
770 ASSERT_TRUE(keyboard_container);
771 keyboard_container->Show();
773 aura::Window* keyboard_window = keyboard::KeyboardController::GetInstance()->
774 proxy()->GetKeyboardWindow();
775 keyboard_container->AddChild(keyboard_window);
776 keyboard_window->set_owned_by_parent(false);
777 keyboard_window->SetBounds(gfx::Rect());
778 keyboard_window->Show();
780 ui::test::TestEventHandler handler;
781 root_window->AddPreTargetHandler(&handler);
783 ui::test::EventGenerator event_generator(root_window, keyboard_window);
784 event_generator.ClickLeftButton();
785 int expected_mouse_presses = 1;
786 EXPECT_EQ(expected_mouse_presses, handler.num_mouse_events() / 2);
788 for (int block_reason = FIRST_BLOCK_REASON;
789 block_reason < NUMBER_OF_BLOCK_REASONS;
790 ++block_reason) {
791 BlockUserSession(static_cast<UserSessionBlockReason>(block_reason));
792 event_generator.ClickLeftButton();
793 expected_mouse_presses++;
794 EXPECT_EQ(expected_mouse_presses, handler.num_mouse_events() / 2);
795 UnblockUserSession();
797 root_window->RemovePreTargetHandler(&handler);
800 // Test for http://crbug.com/299787. RootWindowController should delete
801 // the old container since the keyboard controller creates a new window in
802 // GetWindowContainer().
803 TEST_F(VirtualKeyboardRootWindowControllerTest,
804 DeleteOldContainerOnVirtualKeyboardInit) {
805 aura::Window* root_window = Shell::GetPrimaryRootWindow();
806 aura::Window* keyboard_container =
807 Shell::GetContainer(root_window, kShellWindowId_VirtualKeyboardContainer);
808 ASSERT_TRUE(keyboard_container);
809 // Track the keyboard container window.
810 aura::WindowTracker tracker;
811 tracker.Add(keyboard_container);
812 // Mock a login user profile change to reinitialize the keyboard.
813 ash::Shell::GetInstance()->OnLoginUserProfilePrepared();
814 // keyboard_container should no longer be present.
815 EXPECT_FALSE(tracker.Contains(keyboard_container));
818 // Test for crbug.com/342524. After user login, the work space should restore to
819 // full screen.
820 TEST_F(VirtualKeyboardRootWindowControllerTest, RestoreWorkspaceAfterLogin) {
821 aura::Window* root_window = Shell::GetPrimaryRootWindow();
822 aura::Window* keyboard_container =
823 Shell::GetContainer(root_window, kShellWindowId_VirtualKeyboardContainer);
824 keyboard_container->Show();
825 keyboard::KeyboardController* controller =
826 keyboard::KeyboardController::GetInstance();
827 aura::Window* keyboard_window = controller->proxy()->GetKeyboardWindow();
828 keyboard_container->AddChild(keyboard_window);
829 keyboard_window->set_owned_by_parent(false);
830 keyboard_window->SetBounds(keyboard::FullWidthKeyboardBoundsFromRootBounds(
831 root_window->bounds(), 100));
832 keyboard_window->Show();
834 gfx::Rect before = ash::Shell::GetScreen()->GetPrimaryDisplay().work_area();
836 // Notify keyboard bounds changing.
837 controller->NotifyKeyboardBoundsChanging(keyboard_container->bounds());
839 if (!keyboard::IsKeyboardOverscrollEnabled()) {
840 gfx::Rect after = ash::Shell::GetScreen()->GetPrimaryDisplay().work_area();
841 EXPECT_LT(after, before);
844 // Mock a login user profile change to reinitialize the keyboard.
845 ash::Shell::GetInstance()->OnLoginUserProfilePrepared();
846 EXPECT_EQ(ash::Shell::GetScreen()->GetPrimaryDisplay().work_area(), before);
849 // Ensure that system modal dialogs do not block events targeted at the virtual
850 // keyboard.
851 TEST_F(VirtualKeyboardRootWindowControllerTest, ClickWithActiveModalDialog) {
852 aura::Window* root_window = Shell::GetPrimaryRootWindow();
853 aura::Window* keyboard_container =
854 Shell::GetContainer(root_window, kShellWindowId_VirtualKeyboardContainer);
855 ASSERT_TRUE(keyboard_container);
856 keyboard_container->Show();
858 aura::Window* keyboard_window = keyboard::KeyboardController::GetInstance()->
859 proxy()->GetKeyboardWindow();
860 keyboard_container->AddChild(keyboard_window);
861 keyboard_window->set_owned_by_parent(false);
862 keyboard_window->SetBounds(keyboard::FullWidthKeyboardBoundsFromRootBounds(
863 root_window->bounds(), 100));
865 ui::test::TestEventHandler handler;
866 root_window->AddPreTargetHandler(&handler);
867 ui::test::EventGenerator root_window_event_generator(root_window);
868 ui::test::EventGenerator keyboard_event_generator(root_window,
869 keyboard_window);
871 views::Widget* modal_widget =
872 CreateModalWidget(gfx::Rect(300, 10, 100, 100));
874 // Verify that mouse events to the root window are block with a visble modal
875 // dialog.
876 root_window_event_generator.ClickLeftButton();
877 EXPECT_EQ(0, handler.num_mouse_events());
879 // Verify that event dispatch to the virtual keyboard is unblocked.
880 keyboard_event_generator.ClickLeftButton();
881 EXPECT_EQ(1, handler.num_mouse_events() / 2);
883 modal_widget->Close();
885 // Verify that mouse events are now unblocked to the root window.
886 root_window_event_generator.ClickLeftButton();
887 EXPECT_EQ(2, handler.num_mouse_events() / 2);
888 root_window->RemovePreTargetHandler(&handler);
891 // Ensure that the visible area for scrolling the text caret excludes the
892 // region occluded by the on-screen keyboard.
893 TEST_F(VirtualKeyboardRootWindowControllerTest, EnsureCaretInWorkArea) {
894 keyboard::KeyboardController* keyboard_controller =
895 keyboard::KeyboardController::GetInstance();
896 keyboard::KeyboardControllerProxy* proxy = keyboard_controller->proxy();
898 MockTextInputClient text_input_client;
899 ui::InputMethod* input_method = proxy->GetInputMethod();
900 ASSERT_TRUE(input_method);
901 input_method->SetFocusedTextInputClient(&text_input_client);
903 aura::Window* root_window = Shell::GetPrimaryRootWindow();
904 aura::Window* keyboard_container =
905 Shell::GetContainer(root_window, kShellWindowId_VirtualKeyboardContainer);
906 ASSERT_TRUE(keyboard_container);
907 keyboard_container->Show();
909 const int keyboard_height = 100;
910 aura::Window* keyboard_window =proxy->GetKeyboardWindow();
911 keyboard_container->AddChild(keyboard_window);
912 keyboard_window->set_owned_by_parent(false);
913 keyboard_window->SetBounds(keyboard::FullWidthKeyboardBoundsFromRootBounds(
914 root_window->bounds(), keyboard_height));
916 proxy->EnsureCaretInWorkArea();
917 ASSERT_EQ(root_window->bounds().width(),
918 text_input_client.visible_rect().width());
919 ASSERT_EQ(root_window->bounds().height() - keyboard_height,
920 text_input_client.visible_rect().height());
922 input_method->SetFocusedTextInputClient(NULL);
925 // Tests that the virtual keyboard does not block context menus. The virtual
926 // keyboard should appear in front of most content, but not context menus. See
927 // crbug/377180.
928 TEST_F(VirtualKeyboardRootWindowControllerTest, ZOrderTest) {
929 UpdateDisplay("800x600");
930 keyboard::KeyboardController* keyboard_controller =
931 keyboard::KeyboardController::GetInstance();
932 keyboard::KeyboardControllerProxy* proxy = keyboard_controller->proxy();
934 aura::Window* root_window = Shell::GetPrimaryRootWindow();
935 aura::Window* keyboard_container =
936 Shell::GetContainer(root_window, kShellWindowId_VirtualKeyboardContainer);
937 ASSERT_TRUE(keyboard_container);
938 keyboard_container->Show();
940 const int keyboard_height = 200;
941 aura::Window* keyboard_window = proxy->GetKeyboardWindow();
942 keyboard_container->AddChild(keyboard_window);
943 keyboard_window->set_owned_by_parent(false);
944 gfx::Rect keyboard_bounds = keyboard::FullWidthKeyboardBoundsFromRootBounds(
945 root_window->bounds(), keyboard_height);
946 keyboard_window->SetBounds(keyboard_bounds);
947 keyboard_window->Show();
949 ui::test::EventGenerator generator(root_window);
951 // Cover the screen with two windows: a normal window on the left side and a
952 // context menu on the right side. When the virtual keyboard is displayed it
953 // partially occludes the normal window, but not the context menu. Compute
954 // positions for generating synthetic click events to perform hit tests,
955 // ensuring the correct window layering. 'top' is above the VK, whereas
956 // 'bottom' lies within the VK. 'left' is centered in the normal window, and
957 // 'right' is centered in the context menu.
958 int window_height = keyboard_bounds.bottom();
959 int window_width = keyboard_bounds.width() / 2;
960 int left = window_width / 2;
961 int right = 3 * window_width / 2;
962 int top = keyboard_bounds.y() / 2;
963 int bottom = window_height - keyboard_height / 2;
965 // Normal window is partially occluded by the virtual keyboard.
966 aura::test::TestWindowDelegate delegate;
967 scoped_ptr<aura::Window> normal(CreateTestWindowInShellWithDelegateAndType(
968 &delegate,
969 ui::wm::WINDOW_TYPE_NORMAL,
971 gfx::Rect(0, 0, window_width, window_height)));
972 normal->set_owned_by_parent(false);
973 normal->Show();
974 TargetHitTestEventHandler normal_handler;
975 normal->AddPreTargetHandler(&normal_handler);
977 // Test that only the click on the top portion of the window is picked up. The
978 // click on the bottom hits the virtual keyboard instead.
979 generator.MoveMouseTo(left, top);
980 generator.ClickLeftButton();
981 EXPECT_EQ(1, normal_handler.num_mouse_events());
982 generator.MoveMouseTo(left, bottom);
983 generator.ClickLeftButton();
984 EXPECT_EQ(1, normal_handler.num_mouse_events());
986 // Menu overlaps virtual keyboard.
987 aura::test::TestWindowDelegate delegate2;
988 scoped_ptr<aura::Window> menu(CreateTestWindowInShellWithDelegateAndType(
989 &delegate2,
990 ui::wm::WINDOW_TYPE_MENU,
992 gfx::Rect(window_width, 0, window_width, window_height)));
993 menu->set_owned_by_parent(false);
994 menu->Show();
995 TargetHitTestEventHandler menu_handler;
996 menu->AddPreTargetHandler(&menu_handler);
998 // Test that both clicks register.
999 generator.MoveMouseTo(right, top);
1000 generator.ClickLeftButton();
1001 EXPECT_EQ(1, menu_handler.num_mouse_events());
1002 generator.MoveMouseTo(right, bottom);
1003 generator.ClickLeftButton();
1004 EXPECT_EQ(2, menu_handler.num_mouse_events());
1006 // Cleanup to ensure that the test windows are destroyed before their
1007 // delegates.
1008 normal.reset();
1009 menu.reset();
1012 // Resolution in UpdateDisplay is not being respected on Windows 8.
1013 #if defined(OS_WIN)
1014 #define MAYBE_DisplayRotation DISABLED_DisplayRotation
1015 #else
1016 #define MAYBE_DisplayRotation DisplayRotation
1017 #endif
1019 // Tests that the virtual keyboard correctly resizes with a change to display
1020 // orientation. See crbug/417612.
1021 TEST_F(VirtualKeyboardRootWindowControllerTest, MAYBE_DisplayRotation) {
1022 UpdateDisplay("800x600");
1023 aura::Window* root_window = Shell::GetPrimaryRootWindow();
1024 aura::Window* keyboard_container =
1025 Shell::GetContainer(root_window, kShellWindowId_VirtualKeyboardContainer);
1026 ASSERT_TRUE(keyboard_container);
1027 keyboard::KeyboardController* keyboard_controller =
1028 keyboard::KeyboardController::GetInstance();
1029 keyboard_controller->ShowKeyboard(false);
1030 keyboard_controller->proxy()->GetKeyboardWindow()->SetBounds(
1031 gfx::Rect(0, 400, 800, 200));
1032 EXPECT_EQ("0,400 800x200", keyboard_container->bounds().ToString());
1034 UpdateDisplay("600x800");
1035 EXPECT_EQ("0,600 600x200", keyboard_container->bounds().ToString());
1038 } // namespace test
1039 } // namespace ash