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/session/session_state_delegate.h"
8 #include "ash/shelf/shelf_layout_manager.h"
10 #include "ash/shell_window_ids.h"
11 #include "ash/system/tray/system_tray_delegate.h"
12 #include "ash/test/ash_test_base.h"
13 #include "ash/test/display_manager_test_api.h"
14 #include "ash/wm/system_modal_container_layout_manager.h"
15 #include "ash/wm/window_properties.h"
16 #include "ash/wm/window_state.h"
17 #include "ash/wm/window_util.h"
18 #include "base/command_line.h"
19 #include "base/memory/scoped_ptr.h"
20 #include "ui/aura/client/focus_change_observer.h"
21 #include "ui/aura/client/focus_client.h"
22 #include "ui/aura/client/window_tree_client.h"
23 #include "ui/aura/env.h"
24 #include "ui/aura/test/test_window_delegate.h"
25 #include "ui/aura/test/test_windows.h"
26 #include "ui/aura/window.h"
27 #include "ui/aura/window_event_dispatcher.h"
28 #include "ui/aura/window_tracker.h"
29 #include "ui/base/ime/dummy_text_input_client.h"
30 #include "ui/base/ime/input_method.h"
31 #include "ui/base/ime/text_input_client.h"
32 #include "ui/base/ime/text_input_focus_manager.h"
33 #include "ui/base/ui_base_switches_util.h"
34 #include "ui/events/test/event_generator.h"
35 #include "ui/events/test/test_event_handler.h"
36 #include "ui/keyboard/keyboard_controller_proxy.h"
37 #include "ui/keyboard/keyboard_switches.h"
38 #include "ui/keyboard/keyboard_util.h"
39 #include "ui/views/controls/menu/menu_controller.h"
40 #include "ui/views/widget/widget.h"
41 #include "ui/views/widget/widget_delegate.h"
49 class TestDelegate
: public views::WidgetDelegateView
{
51 explicit TestDelegate(bool system_modal
) : system_modal_(system_modal
) {}
52 ~TestDelegate() override
{}
54 // Overridden from views::WidgetDelegate:
55 views::View
* GetContentsView() override
{ return this; }
57 ui::ModalType
GetModalType() const override
{
58 return system_modal_
? ui::MODAL_TYPE_SYSTEM
: ui::MODAL_TYPE_NONE
;
64 DISALLOW_COPY_AND_ASSIGN(TestDelegate
);
67 class DeleteOnBlurDelegate
: public aura::test::TestWindowDelegate
,
68 public aura::client::FocusChangeObserver
{
70 DeleteOnBlurDelegate() : window_(NULL
) {}
71 ~DeleteOnBlurDelegate() override
{}
73 void SetWindow(aura::Window
* window
) {
75 aura::client::SetFocusChangeObserver(window_
, this);
79 // aura::test::TestWindowDelegate overrides:
80 bool CanFocus() override
{ return true; }
82 // aura::client::FocusChangeObserver implementation:
83 void OnWindowFocused(aura::Window
* gained_focus
,
84 aura::Window
* lost_focus
) override
{
85 if (window_
== lost_focus
)
89 aura::Window
* window_
;
91 DISALLOW_COPY_AND_ASSIGN(DeleteOnBlurDelegate
);
98 class RootWindowControllerTest
: public test::AshTestBase
{
100 views::Widget
* CreateTestWidget(const gfx::Rect
& bounds
) {
101 views::Widget
* widget
= views::Widget::CreateWindowWithContextAndBounds(
102 NULL
, CurrentContext(), bounds
);
107 views::Widget
* CreateModalWidget(const gfx::Rect
& bounds
) {
108 views::Widget
* widget
= views::Widget::CreateWindowWithContextAndBounds(
109 new TestDelegate(true), CurrentContext(), bounds
);
114 views::Widget
* CreateModalWidgetWithParent(const gfx::Rect
& bounds
,
115 gfx::NativeWindow parent
) {
116 views::Widget
* widget
=
117 views::Widget::CreateWindowWithParentAndBounds(new TestDelegate(true),
124 aura::Window
* GetModalContainer(aura::Window
* root_window
) {
125 return Shell::GetContainer(root_window
,
126 ash::kShellWindowId_SystemModalContainer
);
130 TEST_F(RootWindowControllerTest
, MoveWindows_Basic
) {
131 if (!SupportsMultipleDisplays())
133 // Windows origin should be doubled when moved to the 1st display.
134 UpdateDisplay("600x600,300x300");
135 aura::Window::Windows root_windows
= Shell::GetAllRootWindows();
136 RootWindowController
* controller
= Shell::GetPrimaryRootWindowController();
137 ShelfLayoutManager
* shelf_layout_manager
=
138 controller
->GetShelfLayoutManager();
139 shelf_layout_manager
->SetAutoHideBehavior(
140 ash::SHELF_AUTO_HIDE_BEHAVIOR_ALWAYS
);
142 views::Widget
* normal
= CreateTestWidget(gfx::Rect(650, 10, 100, 100));
143 EXPECT_EQ(root_windows
[1], normal
->GetNativeView()->GetRootWindow());
144 EXPECT_EQ("650,10 100x100", normal
->GetWindowBoundsInScreen().ToString());
145 EXPECT_EQ("50,10 100x100",
146 normal
->GetNativeView()->GetBoundsInRootWindow().ToString());
148 views::Widget
* maximized
= CreateTestWidget(gfx::Rect(700, 10, 100, 100));
149 maximized
->Maximize();
150 EXPECT_EQ(root_windows
[1], maximized
->GetNativeView()->GetRootWindow());
151 EXPECT_EQ("600,0 300x253", maximized
->GetWindowBoundsInScreen().ToString());
152 EXPECT_EQ("0,0 300x253",
153 maximized
->GetNativeView()->GetBoundsInRootWindow().ToString());
155 views::Widget
* minimized
= CreateTestWidget(gfx::Rect(800, 10, 100, 100));
156 minimized
->Minimize();
157 EXPECT_EQ(root_windows
[1], minimized
->GetNativeView()->GetRootWindow());
158 EXPECT_EQ("800,10 100x100",
159 minimized
->GetWindowBoundsInScreen().ToString());
161 views::Widget
* fullscreen
= CreateTestWidget(gfx::Rect(850, 10, 100, 100));
162 fullscreen
->SetFullscreen(true);
163 EXPECT_EQ(root_windows
[1], fullscreen
->GetNativeView()->GetRootWindow());
165 EXPECT_EQ("600,0 300x300",
166 fullscreen
->GetWindowBoundsInScreen().ToString());
167 EXPECT_EQ("0,0 300x300",
168 fullscreen
->GetNativeView()->GetBoundsInRootWindow().ToString());
170 views::Widget
* unparented_control
= new Widget
;
171 Widget::InitParams params
;
172 params
.bounds
= gfx::Rect(650, 10, 100, 100);
173 params
.context
= CurrentContext();
174 params
.type
= Widget::InitParams::TYPE_CONTROL
;
175 unparented_control
->Init(params
);
176 EXPECT_EQ(root_windows
[1],
177 unparented_control
->GetNativeView()->GetRootWindow());
178 EXPECT_EQ(kShellWindowId_UnparentedControlContainer
,
179 unparented_control
->GetNativeView()->parent()->id());
181 aura::Window
* panel
= CreateTestWindowInShellWithDelegateAndType(
182 NULL
, ui::wm::WINDOW_TYPE_PANEL
, 0, gfx::Rect(700, 100, 100, 100));
183 EXPECT_EQ(root_windows
[1], panel
->GetRootWindow());
184 EXPECT_EQ(kShellWindowId_PanelContainer
, panel
->parent()->id());
186 // Make sure a window that will delete itself when losing focus
188 aura::WindowTracker tracker
;
189 DeleteOnBlurDelegate delete_on_blur_delegate
;
190 aura::Window
* d2
= CreateTestWindowInShellWithDelegate(
191 &delete_on_blur_delegate
, 0, gfx::Rect(50, 50, 100, 100));
192 delete_on_blur_delegate
.SetWindow(d2
);
193 aura::client::GetFocusClient(root_windows
[0])->FocusWindow(d2
);
196 UpdateDisplay("600x600");
198 // d2 must have been deleted.
199 EXPECT_FALSE(tracker
.Contains(d2
));
201 EXPECT_EQ(root_windows
[0], normal
->GetNativeView()->GetRootWindow());
202 EXPECT_EQ("100,20 100x100", normal
->GetWindowBoundsInScreen().ToString());
203 EXPECT_EQ("100,20 100x100",
204 normal
->GetNativeView()->GetBoundsInRootWindow().ToString());
206 // Maximized area on primary display has 3px (given as
207 // kAutoHideSize in shelf_layout_manager.cc) inset at the bottom.
209 // First clear fullscreen status, since both fullscreen and maximized windows
210 // share the same desktop workspace, which cancels the shelf status.
211 fullscreen
->SetFullscreen(false);
212 EXPECT_EQ(root_windows
[0], maximized
->GetNativeView()->GetRootWindow());
213 EXPECT_EQ("0,0 600x597",
214 maximized
->GetWindowBoundsInScreen().ToString());
215 EXPECT_EQ("0,0 600x597",
216 maximized
->GetNativeView()->GetBoundsInRootWindow().ToString());
218 // Set fullscreen to true. In that case the 3px inset becomes invisible so
219 // the maximized window can also use the area fully.
220 fullscreen
->SetFullscreen(true);
221 EXPECT_EQ(root_windows
[0], maximized
->GetNativeView()->GetRootWindow());
222 EXPECT_EQ("0,0 600x600",
223 maximized
->GetWindowBoundsInScreen().ToString());
224 EXPECT_EQ("0,0 600x600",
225 maximized
->GetNativeView()->GetBoundsInRootWindow().ToString());
227 EXPECT_EQ(root_windows
[0], minimized
->GetNativeView()->GetRootWindow());
228 EXPECT_EQ("400,20 100x100",
229 minimized
->GetWindowBoundsInScreen().ToString());
231 EXPECT_EQ(root_windows
[0], fullscreen
->GetNativeView()->GetRootWindow());
232 EXPECT_TRUE(fullscreen
->IsFullscreen());
233 EXPECT_EQ("0,0 600x600",
234 fullscreen
->GetWindowBoundsInScreen().ToString());
235 EXPECT_EQ("0,0 600x600",
236 fullscreen
->GetNativeView()->GetBoundsInRootWindow().ToString());
238 // Test if the restore bounds are correctly updated.
239 wm::GetWindowState(maximized
->GetNativeView())->Restore();
240 EXPECT_EQ("200,20 100x100", maximized
->GetWindowBoundsInScreen().ToString());
241 EXPECT_EQ("200,20 100x100",
242 maximized
->GetNativeView()->GetBoundsInRootWindow().ToString());
244 fullscreen
->SetFullscreen(false);
245 EXPECT_EQ("500,20 100x100",
246 fullscreen
->GetWindowBoundsInScreen().ToString());
247 EXPECT_EQ("500,20 100x100",
248 fullscreen
->GetNativeView()->GetBoundsInRootWindow().ToString());
250 // Test if the unparented widget has moved.
251 EXPECT_EQ(root_windows
[0],
252 unparented_control
->GetNativeView()->GetRootWindow());
253 EXPECT_EQ(kShellWindowId_UnparentedControlContainer
,
254 unparented_control
->GetNativeView()->parent()->id());
256 // Test if the panel has moved.
257 EXPECT_EQ(root_windows
[0], panel
->GetRootWindow());
258 EXPECT_EQ(kShellWindowId_PanelContainer
, panel
->parent()->id());
261 TEST_F(RootWindowControllerTest
, MoveWindows_Modal
) {
262 if (!SupportsMultipleDisplays())
265 UpdateDisplay("500x500,500x500");
267 aura::Window::Windows root_windows
= Shell::GetAllRootWindows();
268 // Emulate virtual screen coordinate system.
269 root_windows
[0]->SetBounds(gfx::Rect(0, 0, 500, 500));
270 root_windows
[1]->SetBounds(gfx::Rect(500, 0, 500, 500));
272 views::Widget
* normal
= CreateTestWidget(gfx::Rect(300, 10, 100, 100));
273 EXPECT_EQ(root_windows
[0], normal
->GetNativeView()->GetRootWindow());
274 EXPECT_TRUE(wm::IsActiveWindow(normal
->GetNativeView()));
276 views::Widget
* modal
= CreateModalWidget(gfx::Rect(650, 10, 100, 100));
277 EXPECT_EQ(root_windows
[1], modal
->GetNativeView()->GetRootWindow());
278 EXPECT_TRUE(GetModalContainer(root_windows
[1])->Contains(
279 modal
->GetNativeView()));
280 EXPECT_TRUE(wm::IsActiveWindow(modal
->GetNativeView()));
282 ui::test::EventGenerator
generator_1st(root_windows
[0]);
283 generator_1st
.ClickLeftButton();
284 EXPECT_TRUE(wm::IsActiveWindow(modal
->GetNativeView()));
286 UpdateDisplay("500x500");
287 EXPECT_EQ(root_windows
[0], modal
->GetNativeView()->GetRootWindow());
288 EXPECT_TRUE(wm::IsActiveWindow(modal
->GetNativeView()));
289 generator_1st
.ClickLeftButton();
290 EXPECT_TRUE(wm::IsActiveWindow(modal
->GetNativeView()));
293 // Make sure lock related windows moves.
294 TEST_F(RootWindowControllerTest
, MoveWindows_LockWindowsInUnified
) {
295 if (!SupportsMultipleDisplays())
297 DisplayManager
* display_manager
= Shell::GetInstance()->display_manager();
298 DisplayManagerTestApi
test_api(display_manager
);
299 test_api
.SetDefaultMultiDisplayMode(DisplayManager::UNIFIED
);
300 display_manager
->SetMultiDisplayMode(DisplayManager::UNIFIED
);
301 UpdateDisplay("500x500");
302 const int kLockScreenWindowId
= 1000;
303 const int kLockBackgroundWindowId
= 1001;
305 RootWindowController
* controller
=
306 Shell::GetInstance()->GetPrimaryRootWindowController();
308 aura::Window
* lock_container
=
309 controller
->GetContainer(kShellWindowId_LockScreenContainer
);
310 aura::Window
* lock_background_container
=
311 controller
->GetContainer(kShellWindowId_LockScreenBackgroundContainer
);
313 views::Widget
* lock_screen
=
314 CreateModalWidgetWithParent(gfx::Rect(10, 10, 100, 100), lock_container
);
315 lock_screen
->GetNativeWindow()->set_id(kLockScreenWindowId
);
316 lock_screen
->SetFullscreen(true);
318 views::Widget
* lock_background
= CreateModalWidgetWithParent(
319 gfx::Rect(10, 10, 100, 100), lock_background_container
);
320 lock_background
->GetNativeWindow()->set_id(kLockBackgroundWindowId
);
322 ASSERT_EQ(lock_screen
->GetNativeWindow(),
323 controller
->GetRootWindow()->GetChildById(kLockScreenWindowId
));
324 ASSERT_EQ(lock_background
->GetNativeWindow(),
325 controller
->GetRootWindow()->GetChildById(kLockBackgroundWindowId
));
326 EXPECT_EQ("0,0 500x500", lock_screen
->GetNativeWindow()->bounds().ToString());
328 // Switch to unified.
329 UpdateDisplay("500x500,500x500");
331 // In unified mode, RWC is created
332 controller
= Shell::GetInstance()->GetPrimaryRootWindowController();
334 ASSERT_EQ(lock_screen
->GetNativeWindow(),
335 controller
->GetRootWindow()->GetChildById(kLockScreenWindowId
));
336 ASSERT_EQ(lock_background
->GetNativeWindow(),
337 controller
->GetRootWindow()->GetChildById(kLockBackgroundWindowId
));
338 EXPECT_EQ("0,0 500x500", lock_screen
->GetNativeWindow()->bounds().ToString());
341 display_manager
->SetMirrorMode(true);
342 EXPECT_TRUE(display_manager
->IsInMirrorMode());
344 controller
= Shell::GetInstance()->GetPrimaryRootWindowController();
345 ASSERT_EQ(lock_screen
->GetNativeWindow(),
346 controller
->GetRootWindow()->GetChildById(kLockScreenWindowId
));
347 ASSERT_EQ(lock_background
->GetNativeWindow(),
348 controller
->GetRootWindow()->GetChildById(kLockBackgroundWindowId
));
349 EXPECT_EQ("0,0 500x500", lock_screen
->GetNativeWindow()->bounds().ToString());
351 // Switch to unified.
352 display_manager
->SetMirrorMode(false);
353 EXPECT_TRUE(display_manager
->IsInUnifiedMode());
355 controller
= Shell::GetInstance()->GetPrimaryRootWindowController();
357 ASSERT_EQ(lock_screen
->GetNativeWindow(),
358 controller
->GetRootWindow()->GetChildById(kLockScreenWindowId
));
359 ASSERT_EQ(lock_background
->GetNativeWindow(),
360 controller
->GetRootWindow()->GetChildById(kLockBackgroundWindowId
));
361 EXPECT_EQ("0,0 500x500", lock_screen
->GetNativeWindow()->bounds().ToString());
363 // Switch to single display.
364 UpdateDisplay("600x500");
365 EXPECT_FALSE(display_manager
->IsInUnifiedMode());
366 EXPECT_FALSE(display_manager
->IsInMirrorMode());
368 controller
= Shell::GetInstance()->GetPrimaryRootWindowController();
370 ASSERT_EQ(lock_screen
->GetNativeWindow(),
371 controller
->GetRootWindow()->GetChildById(kLockScreenWindowId
));
372 ASSERT_EQ(lock_background
->GetNativeWindow(),
373 controller
->GetRootWindow()->GetChildById(kLockBackgroundWindowId
));
374 EXPECT_EQ("0,0 600x500", lock_screen
->GetNativeWindow()->bounds().ToString());
377 TEST_F(RootWindowControllerTest
, ModalContainer
) {
378 UpdateDisplay("600x600");
379 Shell
* shell
= Shell::GetInstance();
380 RootWindowController
* controller
= shell
->GetPrimaryRootWindowController();
381 EXPECT_EQ(user::LOGGED_IN_USER
,
382 shell
->system_tray_delegate()->GetUserLoginStatus());
383 EXPECT_EQ(controller
->GetContainer(kShellWindowId_SystemModalContainer
)
385 controller
->GetSystemModalLayoutManager(NULL
));
387 views::Widget
* session_modal_widget
=
388 CreateModalWidget(gfx::Rect(300, 10, 100, 100));
389 EXPECT_EQ(controller
->GetContainer(kShellWindowId_SystemModalContainer
)
391 controller
->GetSystemModalLayoutManager(
392 session_modal_widget
->GetNativeView()));
394 shell
->session_state_delegate()->LockScreen();
395 EXPECT_EQ(user::LOGGED_IN_LOCKED
,
396 shell
->system_tray_delegate()->GetUserLoginStatus());
397 EXPECT_EQ(controller
->GetContainer(kShellWindowId_LockSystemModalContainer
)
399 controller
->GetSystemModalLayoutManager(NULL
));
401 aura::Window
* lock_container
=
402 controller
->GetContainer(kShellWindowId_LockScreenContainer
);
403 views::Widget
* lock_modal_widget
=
404 CreateModalWidgetWithParent(gfx::Rect(300, 10, 100, 100), lock_container
);
405 EXPECT_EQ(controller
->GetContainer(kShellWindowId_LockSystemModalContainer
)
407 controller
->GetSystemModalLayoutManager(
408 lock_modal_widget
->GetNativeView()));
409 EXPECT_EQ(controller
->GetContainer(kShellWindowId_SystemModalContainer
)
411 controller
->GetSystemModalLayoutManager(
412 session_modal_widget
->GetNativeView()));
414 shell
->session_state_delegate()->UnlockScreen();
417 TEST_F(RootWindowControllerTest
, ModalContainerNotLoggedInLoggedIn
) {
418 UpdateDisplay("600x600");
419 Shell
* shell
= Shell::GetInstance();
421 // Configure login screen environment.
422 SetUserLoggedIn(false);
423 EXPECT_EQ(user::LOGGED_IN_NONE
,
424 shell
->system_tray_delegate()->GetUserLoginStatus());
425 EXPECT_EQ(0, shell
->session_state_delegate()->NumberOfLoggedInUsers());
426 EXPECT_FALSE(shell
->session_state_delegate()->IsActiveUserSessionStarted());
428 RootWindowController
* controller
= shell
->GetPrimaryRootWindowController();
429 EXPECT_EQ(controller
->GetContainer(kShellWindowId_LockSystemModalContainer
)
431 controller
->GetSystemModalLayoutManager(NULL
));
433 aura::Window
* lock_container
=
434 controller
->GetContainer(kShellWindowId_LockScreenContainer
);
435 views::Widget
* login_modal_widget
=
436 CreateModalWidgetWithParent(gfx::Rect(300, 10, 100, 100), lock_container
);
437 EXPECT_EQ(controller
->GetContainer(kShellWindowId_LockSystemModalContainer
)
439 controller
->GetSystemModalLayoutManager(
440 login_modal_widget
->GetNativeView()));
441 login_modal_widget
->Close();
443 // Configure user session environment.
444 SetUserLoggedIn(true);
445 SetSessionStarted(true);
446 EXPECT_EQ(user::LOGGED_IN_USER
,
447 shell
->system_tray_delegate()->GetUserLoginStatus());
448 EXPECT_EQ(1, shell
->session_state_delegate()->NumberOfLoggedInUsers());
449 EXPECT_TRUE(shell
->session_state_delegate()->IsActiveUserSessionStarted());
450 EXPECT_EQ(controller
->GetContainer(kShellWindowId_SystemModalContainer
)
452 controller
->GetSystemModalLayoutManager(NULL
));
454 views::Widget
* session_modal_widget
=
455 CreateModalWidget(gfx::Rect(300, 10, 100, 100));
456 EXPECT_EQ(controller
->GetContainer(kShellWindowId_SystemModalContainer
)
458 controller
->GetSystemModalLayoutManager(
459 session_modal_widget
->GetNativeView()));
462 TEST_F(RootWindowControllerTest
, ModalContainerBlockedSession
) {
463 UpdateDisplay("600x600");
464 RootWindowController
* controller
= Shell::GetPrimaryRootWindowController();
465 aura::Window
* lock_container
=
466 controller
->GetContainer(kShellWindowId_LockScreenContainer
);
467 for (int block_reason
= FIRST_BLOCK_REASON
;
468 block_reason
< NUMBER_OF_BLOCK_REASONS
;
470 views::Widget
* session_modal_widget
=
471 CreateModalWidget(gfx::Rect(300, 10, 100, 100));
472 EXPECT_EQ(controller
->GetContainer(kShellWindowId_SystemModalContainer
)
474 controller
->GetSystemModalLayoutManager(
475 session_modal_widget
->GetNativeView()));
476 EXPECT_EQ(controller
->GetContainer(kShellWindowId_SystemModalContainer
)
478 controller
->GetSystemModalLayoutManager(NULL
));
479 session_modal_widget
->Close();
481 BlockUserSession(static_cast<UserSessionBlockReason
>(block_reason
));
483 EXPECT_EQ(controller
->GetContainer(kShellWindowId_LockSystemModalContainer
)
485 controller
->GetSystemModalLayoutManager(NULL
));
487 views::Widget
* lock_modal_widget
=
488 CreateModalWidgetWithParent(gfx::Rect(300, 10, 100, 100),
490 EXPECT_EQ(controller
->GetContainer(kShellWindowId_LockSystemModalContainer
)
492 controller
->GetSystemModalLayoutManager(
493 lock_modal_widget
->GetNativeView()));
495 session_modal_widget
=
496 CreateModalWidget(gfx::Rect(300, 10, 100, 100));
497 EXPECT_EQ(controller
->GetContainer(kShellWindowId_SystemModalContainer
)
499 controller
->GetSystemModalLayoutManager(
500 session_modal_widget
->GetNativeView()));
501 session_modal_widget
->Close();
503 lock_modal_widget
->Close();
504 UnblockUserSession();
508 TEST_F(RootWindowControllerTest
, GetWindowForFullscreenMode
) {
509 UpdateDisplay("600x600");
510 RootWindowController
* controller
=
511 Shell::GetInstance()->GetPrimaryRootWindowController();
513 Widget
* w1
= CreateTestWidget(gfx::Rect(0, 0, 100, 100));
515 Widget
* w2
= CreateTestWidget(gfx::Rect(0, 0, 100, 100));
516 w2
->SetFullscreen(true);
517 // |w3| is a transient child of |w2|.
518 Widget
* w3
= Widget::CreateWindowWithParentAndBounds(NULL
,
519 w2
->GetNativeWindow(), gfx::Rect(0, 0, 100, 100));
521 // Test that GetWindowForFullscreenMode() finds the fullscreen window when one
522 // of its transient children is active.
524 EXPECT_EQ(w2
->GetNativeWindow(), controller
->GetWindowForFullscreenMode());
526 // If the topmost window is not fullscreen, it returns NULL.
528 EXPECT_EQ(NULL
, controller
->GetWindowForFullscreenMode());
532 // Only w2 remains, if minimized GetWindowForFullscreenMode should return
535 EXPECT_EQ(w2
->GetNativeWindow(), controller
->GetWindowForFullscreenMode());
537 EXPECT_EQ(NULL
, controller
->GetWindowForFullscreenMode());
540 TEST_F(RootWindowControllerTest
, MultipleDisplaysGetWindowForFullscreenMode
) {
541 if (!SupportsMultipleDisplays())
544 UpdateDisplay("600x600,600x600");
545 Shell::RootWindowControllerList controllers
=
546 Shell::GetInstance()->GetAllRootWindowControllers();
548 Widget
* w1
= CreateTestWidget(gfx::Rect(0, 0, 100, 100));
550 Widget
* w2
= CreateTestWidget(gfx::Rect(0, 0, 100, 100));
551 w2
->SetFullscreen(true);
552 Widget
* w3
= CreateTestWidget(gfx::Rect(600, 0, 100, 100));
554 EXPECT_EQ(w1
->GetNativeWindow()->GetRootWindow(),
555 controllers
[0]->GetRootWindow());
556 EXPECT_EQ(w2
->GetNativeWindow()->GetRootWindow(),
557 controllers
[0]->GetRootWindow());
558 EXPECT_EQ(w3
->GetNativeWindow()->GetRootWindow(),
559 controllers
[1]->GetRootWindow());
562 EXPECT_EQ(NULL
, controllers
[0]->GetWindowForFullscreenMode());
563 EXPECT_EQ(NULL
, controllers
[1]->GetWindowForFullscreenMode());
566 EXPECT_EQ(w2
->GetNativeWindow(),
567 controllers
[0]->GetWindowForFullscreenMode());
568 EXPECT_EQ(NULL
, controllers
[1]->GetWindowForFullscreenMode());
570 // Verify that the first root window controller remains in fullscreen mode
571 // when a window on the other display is activated.
573 EXPECT_EQ(w2
->GetNativeWindow(),
574 controllers
[0]->GetWindowForFullscreenMode());
575 EXPECT_EQ(NULL
, controllers
[1]->GetWindowForFullscreenMode());
578 // Test that user session window can't be focused if user session blocked by
579 // some overlapping UI.
580 TEST_F(RootWindowControllerTest
, FocusBlockedWindow
) {
581 UpdateDisplay("600x600");
582 RootWindowController
* controller
=
583 Shell::GetInstance()->GetPrimaryRootWindowController();
584 aura::Window
* lock_container
=
585 controller
->GetContainer(kShellWindowId_LockScreenContainer
);
586 aura::Window
* lock_window
= Widget::CreateWindowWithParentAndBounds(NULL
,
587 lock_container
, gfx::Rect(0, 0, 100, 100))->GetNativeView();
589 aura::Window
* session_window
=
590 CreateTestWidget(gfx::Rect(0, 0, 100, 100))->GetNativeView();
591 session_window
->Show();
593 for (int block_reason
= FIRST_BLOCK_REASON
;
594 block_reason
< NUMBER_OF_BLOCK_REASONS
;
596 BlockUserSession(static_cast<UserSessionBlockReason
>(block_reason
));
597 lock_window
->Focus();
598 EXPECT_TRUE(lock_window
->HasFocus());
599 session_window
->Focus();
600 EXPECT_FALSE(session_window
->HasFocus());
601 UnblockUserSession();
605 // Tracks whether OnWindowDestroying() has been invoked.
606 class DestroyedWindowObserver
: public aura::WindowObserver
{
608 DestroyedWindowObserver() : destroyed_(false), window_(NULL
) {}
609 ~DestroyedWindowObserver() override
{ Shutdown(); }
611 void SetWindow(Window
* window
) {
613 window
->AddObserver(this);
616 bool destroyed() const { return destroyed_
; }
618 // WindowObserver overrides:
619 void OnWindowDestroying(Window
* window
) override
{
628 window_
->RemoveObserver(this);
635 DISALLOW_COPY_AND_ASSIGN(DestroyedWindowObserver
);
638 // Verifies shutdown doesn't delete windows that are not owned by the parent.
639 TEST_F(RootWindowControllerTest
, DontDeleteWindowsNotOwnedByParent
) {
640 DestroyedWindowObserver observer1
;
641 aura::test::TestWindowDelegate delegate1
;
642 aura::Window
* window1
= new aura::Window(&delegate1
);
643 window1
->SetType(ui::wm::WINDOW_TYPE_CONTROL
);
644 window1
->set_owned_by_parent(false);
645 observer1
.SetWindow(window1
);
646 window1
->Init(ui::LAYER_NOT_DRAWN
);
647 aura::client::ParentWindowWithContext(
648 window1
, Shell::GetInstance()->GetPrimaryRootWindow(), gfx::Rect());
650 DestroyedWindowObserver observer2
;
651 aura::Window
* window2
= new aura::Window(NULL
);
652 window2
->set_owned_by_parent(false);
653 observer2
.SetWindow(window2
);
654 window2
->Init(ui::LAYER_NOT_DRAWN
);
655 Shell::GetInstance()->GetPrimaryRootWindow()->AddChild(window2
);
657 Shell::GetInstance()->GetPrimaryRootWindowController()->CloseChildWindows();
659 ASSERT_FALSE(observer1
.destroyed());
662 ASSERT_FALSE(observer2
.destroyed());
666 typedef test::NoSessionAshTestBase NoSessionRootWindowControllerTest
;
668 // Make sure that an event handler exists for entire display area.
669 TEST_F(NoSessionRootWindowControllerTest
, Event
) {
670 // Hide the shelf since it might otherwise get an event target.
671 RootWindowController
* controller
= Shell::GetPrimaryRootWindowController();
672 ShelfLayoutManager
* shelf_layout_manager
=
673 controller
->GetShelfLayoutManager();
674 shelf_layout_manager
->SetAutoHideBehavior(
675 ash::SHELF_AUTO_HIDE_ALWAYS_HIDDEN
);
677 aura::Window
* root
= Shell::GetPrimaryRootWindow();
678 const gfx::Size size
= root
->bounds().size();
679 aura::Window
* event_target
= root
->GetEventHandlerForPoint(gfx::Point(0, 0));
680 EXPECT_TRUE(event_target
);
681 EXPECT_EQ(event_target
,
682 root
->GetEventHandlerForPoint(gfx::Point(0, size
.height() - 1)));
683 EXPECT_EQ(event_target
,
684 root
->GetEventHandlerForPoint(gfx::Point(size
.width() - 1, 0)));
685 EXPECT_EQ(event_target
,
686 root
->GetEventHandlerForPoint(gfx::Point(0, size
.height() - 1)));
687 EXPECT_EQ(event_target
,
688 root
->GetEventHandlerForPoint(
689 gfx::Point(size
.width() - 1, size
.height() - 1)));
692 class VirtualKeyboardRootWindowControllerTest
693 : public RootWindowControllerTest
{
695 VirtualKeyboardRootWindowControllerTest() {}
696 ~VirtualKeyboardRootWindowControllerTest() override
{}
698 void SetUp() override
{
699 base::CommandLine::ForCurrentProcess()->AppendSwitch(
700 keyboard::switches::kEnableVirtualKeyboard
);
701 test::AshTestBase::SetUp();
702 Shell::GetPrimaryRootWindowController()->ActivateKeyboard(
703 keyboard::KeyboardController::GetInstance());
707 DISALLOW_COPY_AND_ASSIGN(VirtualKeyboardRootWindowControllerTest
);
710 class MockTextInputClient
: public ui::DummyTextInputClient
{
712 MockTextInputClient() :
713 ui::DummyTextInputClient(ui::TEXT_INPUT_TYPE_TEXT
) {}
715 void EnsureCaretInRect(const gfx::Rect
& rect
) override
{
716 visible_rect_
= rect
;
719 const gfx::Rect
& visible_rect() const {
720 return visible_rect_
;
724 gfx::Rect visible_rect_
;
726 DISALLOW_COPY_AND_ASSIGN(MockTextInputClient
);
729 class TargetHitTestEventHandler
: public ui::test::TestEventHandler
{
731 TargetHitTestEventHandler() {}
733 // ui::test::TestEventHandler overrides.
734 void OnMouseEvent(ui::MouseEvent
* event
) override
{
735 if (event
->type() == ui::ET_MOUSE_PRESSED
)
736 ui::test::TestEventHandler::OnMouseEvent(event
);
737 event
->StopPropagation();
741 DISALLOW_COPY_AND_ASSIGN(TargetHitTestEventHandler
);
744 // Test for http://crbug.com/297858. Virtual keyboard container should only show
745 // on primary root window.
746 TEST_F(VirtualKeyboardRootWindowControllerTest
,
747 VirtualKeyboardOnPrimaryRootWindowOnly
) {
748 if (!SupportsMultipleDisplays())
751 UpdateDisplay("500x500,500x500");
753 aura::Window::Windows root_windows
= Shell::GetAllRootWindows();
754 aura::Window
* primary_root_window
= Shell::GetPrimaryRootWindow();
755 aura::Window
* secondary_root_window
=
756 root_windows
[0] == primary_root_window
?
757 root_windows
[1] : root_windows
[0];
759 ASSERT_TRUE(Shell::GetContainer(primary_root_window
,
760 kShellWindowId_VirtualKeyboardContainer
));
761 ASSERT_FALSE(Shell::GetContainer(secondary_root_window
,
762 kShellWindowId_VirtualKeyboardContainer
));
765 // Test for http://crbug.com/263599. Virtual keyboard should be able to receive
766 // events at blocked user session.
767 TEST_F(VirtualKeyboardRootWindowControllerTest
,
768 ClickVirtualKeyboardInBlockedWindow
) {
769 aura::Window
* root_window
= Shell::GetPrimaryRootWindow();
770 aura::Window
* keyboard_container
=
771 Shell::GetContainer(root_window
, kShellWindowId_VirtualKeyboardContainer
);
772 ASSERT_TRUE(keyboard_container
);
773 keyboard_container
->Show();
775 aura::Window
* keyboard_window
= keyboard::KeyboardController::GetInstance()->
776 proxy()->GetKeyboardWindow();
777 keyboard_container
->AddChild(keyboard_window
);
778 keyboard_window
->set_owned_by_parent(false);
779 keyboard_window
->SetBounds(gfx::Rect());
780 keyboard_window
->Show();
782 ui::test::TestEventHandler handler
;
783 root_window
->AddPreTargetHandler(&handler
);
785 ui::test::EventGenerator
event_generator(root_window
, keyboard_window
);
786 event_generator
.ClickLeftButton();
787 int expected_mouse_presses
= 1;
788 EXPECT_EQ(expected_mouse_presses
, handler
.num_mouse_events() / 2);
790 for (int block_reason
= FIRST_BLOCK_REASON
;
791 block_reason
< NUMBER_OF_BLOCK_REASONS
;
793 BlockUserSession(static_cast<UserSessionBlockReason
>(block_reason
));
794 event_generator
.ClickLeftButton();
795 expected_mouse_presses
++;
796 EXPECT_EQ(expected_mouse_presses
, handler
.num_mouse_events() / 2);
797 UnblockUserSession();
799 root_window
->RemovePreTargetHandler(&handler
);
802 // Test for http://crbug.com/299787. RootWindowController should delete
803 // the old container since the keyboard controller creates a new window in
804 // GetWindowContainer().
805 TEST_F(VirtualKeyboardRootWindowControllerTest
,
806 DeleteOldContainerOnVirtualKeyboardInit
) {
807 aura::Window
* root_window
= Shell::GetPrimaryRootWindow();
808 aura::Window
* keyboard_container
=
809 Shell::GetContainer(root_window
, kShellWindowId_VirtualKeyboardContainer
);
810 ASSERT_TRUE(keyboard_container
);
811 // Track the keyboard container window.
812 aura::WindowTracker tracker
;
813 tracker
.Add(keyboard_container
);
814 // Mock a login user profile change to reinitialize the keyboard.
815 ash::Shell::GetInstance()->OnLoginUserProfilePrepared();
816 // keyboard_container should no longer be present.
817 EXPECT_FALSE(tracker
.Contains(keyboard_container
));
820 // Test for crbug.com/342524. After user login, the work space should restore to
822 TEST_F(VirtualKeyboardRootWindowControllerTest
, RestoreWorkspaceAfterLogin
) {
823 aura::Window
* root_window
= Shell::GetPrimaryRootWindow();
824 aura::Window
* keyboard_container
=
825 Shell::GetContainer(root_window
, kShellWindowId_VirtualKeyboardContainer
);
826 keyboard_container
->Show();
827 keyboard::KeyboardController
* controller
=
828 keyboard::KeyboardController::GetInstance();
829 aura::Window
* keyboard_window
= controller
->proxy()->GetKeyboardWindow();
830 keyboard_container
->AddChild(keyboard_window
);
831 keyboard_window
->set_owned_by_parent(false);
832 keyboard_window
->SetBounds(keyboard::FullWidthKeyboardBoundsFromRootBounds(
833 root_window
->bounds(), 100));
834 keyboard_window
->Show();
836 gfx::Rect before
= ash::Shell::GetScreen()->GetPrimaryDisplay().work_area();
838 // Notify keyboard bounds changing.
839 controller
->NotifyKeyboardBoundsChanging(keyboard_container
->bounds());
841 if (!keyboard::IsKeyboardOverscrollEnabled()) {
842 gfx::Rect after
= ash::Shell::GetScreen()->GetPrimaryDisplay().work_area();
843 EXPECT_LT(after
, before
);
846 // Mock a login user profile change to reinitialize the keyboard.
847 ash::Shell::GetInstance()->OnLoginUserProfilePrepared();
848 EXPECT_EQ(ash::Shell::GetScreen()->GetPrimaryDisplay().work_area(), before
);
851 // Ensure that system modal dialogs do not block events targeted at the virtual
853 TEST_F(VirtualKeyboardRootWindowControllerTest
, ClickWithActiveModalDialog
) {
854 aura::Window
* root_window
= Shell::GetPrimaryRootWindow();
855 aura::Window
* keyboard_container
=
856 Shell::GetContainer(root_window
, kShellWindowId_VirtualKeyboardContainer
);
857 ASSERT_TRUE(keyboard_container
);
858 keyboard_container
->Show();
860 aura::Window
* keyboard_window
= keyboard::KeyboardController::GetInstance()->
861 proxy()->GetKeyboardWindow();
862 keyboard_container
->AddChild(keyboard_window
);
863 keyboard_window
->set_owned_by_parent(false);
864 keyboard_window
->SetBounds(keyboard::FullWidthKeyboardBoundsFromRootBounds(
865 root_window
->bounds(), 100));
867 ui::test::TestEventHandler handler
;
868 root_window
->AddPreTargetHandler(&handler
);
869 ui::test::EventGenerator
root_window_event_generator(root_window
);
870 ui::test::EventGenerator
keyboard_event_generator(root_window
,
873 views::Widget
* modal_widget
=
874 CreateModalWidget(gfx::Rect(300, 10, 100, 100));
876 // Verify that mouse events to the root window are block with a visble modal
878 root_window_event_generator
.ClickLeftButton();
879 EXPECT_EQ(0, handler
.num_mouse_events());
881 // Verify that event dispatch to the virtual keyboard is unblocked.
882 keyboard_event_generator
.ClickLeftButton();
883 EXPECT_EQ(1, handler
.num_mouse_events() / 2);
885 modal_widget
->Close();
887 // Verify that mouse events are now unblocked to the root window.
888 root_window_event_generator
.ClickLeftButton();
889 EXPECT_EQ(2, handler
.num_mouse_events() / 2);
890 root_window
->RemovePreTargetHandler(&handler
);
893 // Ensure that the visible area for scrolling the text caret excludes the
894 // region occluded by the on-screen keyboard.
895 TEST_F(VirtualKeyboardRootWindowControllerTest
, EnsureCaretInWorkArea
) {
896 keyboard::KeyboardController
* keyboard_controller
=
897 keyboard::KeyboardController::GetInstance();
898 keyboard::KeyboardControllerProxy
* proxy
= keyboard_controller
->proxy();
900 MockTextInputClient text_input_client
;
901 ui::InputMethod
* input_method
= proxy
->GetInputMethod();
902 ASSERT_TRUE(input_method
);
903 if (switches::IsTextInputFocusManagerEnabled()) {
904 ui::TextInputFocusManager::GetInstance()->FocusTextInputClient(
907 input_method
->SetFocusedTextInputClient(&text_input_client
);
910 aura::Window
* root_window
= Shell::GetPrimaryRootWindow();
911 aura::Window
* keyboard_container
=
912 Shell::GetContainer(root_window
, kShellWindowId_VirtualKeyboardContainer
);
913 ASSERT_TRUE(keyboard_container
);
914 keyboard_container
->Show();
916 const int keyboard_height
= 100;
917 aura::Window
* keyboard_window
=proxy
->GetKeyboardWindow();
918 keyboard_container
->AddChild(keyboard_window
);
919 keyboard_window
->set_owned_by_parent(false);
920 keyboard_window
->SetBounds(keyboard::FullWidthKeyboardBoundsFromRootBounds(
921 root_window
->bounds(), keyboard_height
));
923 proxy
->EnsureCaretInWorkArea();
924 ASSERT_EQ(root_window
->bounds().width(),
925 text_input_client
.visible_rect().width());
926 ASSERT_EQ(root_window
->bounds().height() - keyboard_height
,
927 text_input_client
.visible_rect().height());
929 if (switches::IsTextInputFocusManagerEnabled()) {
930 ui::TextInputFocusManager::GetInstance()->BlurTextInputClient(
933 input_method
->SetFocusedTextInputClient(NULL
);
937 // Tests that the virtual keyboard does not block context menus. The virtual
938 // keyboard should appear in front of most content, but not context menus. See
940 TEST_F(VirtualKeyboardRootWindowControllerTest
, ZOrderTest
) {
941 UpdateDisplay("800x600");
942 keyboard::KeyboardController
* keyboard_controller
=
943 keyboard::KeyboardController::GetInstance();
944 keyboard::KeyboardControllerProxy
* proxy
= keyboard_controller
->proxy();
946 aura::Window
* root_window
= Shell::GetPrimaryRootWindow();
947 aura::Window
* keyboard_container
=
948 Shell::GetContainer(root_window
, kShellWindowId_VirtualKeyboardContainer
);
949 ASSERT_TRUE(keyboard_container
);
950 keyboard_container
->Show();
952 const int keyboard_height
= 200;
953 aura::Window
* keyboard_window
= proxy
->GetKeyboardWindow();
954 keyboard_container
->AddChild(keyboard_window
);
955 keyboard_window
->set_owned_by_parent(false);
956 gfx::Rect keyboard_bounds
= keyboard::FullWidthKeyboardBoundsFromRootBounds(
957 root_window
->bounds(), keyboard_height
);
958 keyboard_window
->SetBounds(keyboard_bounds
);
959 keyboard_window
->Show();
961 ui::test::EventGenerator
generator(root_window
);
963 // Cover the screen with two windows: a normal window on the left side and a
964 // context menu on the right side. When the virtual keyboard is displayed it
965 // partially occludes the normal window, but not the context menu. Compute
966 // positions for generating synthetic click events to perform hit tests,
967 // ensuring the correct window layering. 'top' is above the VK, whereas
968 // 'bottom' lies within the VK. 'left' is centered in the normal window, and
969 // 'right' is centered in the context menu.
970 int window_height
= keyboard_bounds
.bottom();
971 int window_width
= keyboard_bounds
.width() / 2;
972 int left
= window_width
/ 2;
973 int right
= 3 * window_width
/ 2;
974 int top
= keyboard_bounds
.y() / 2;
975 int bottom
= window_height
- keyboard_height
/ 2;
977 // Normal window is partially occluded by the virtual keyboard.
978 aura::test::TestWindowDelegate delegate
;
979 scoped_ptr
<aura::Window
> normal(CreateTestWindowInShellWithDelegateAndType(
981 ui::wm::WINDOW_TYPE_NORMAL
,
983 gfx::Rect(0, 0, window_width
, window_height
)));
984 normal
->set_owned_by_parent(false);
986 TargetHitTestEventHandler normal_handler
;
987 normal
->AddPreTargetHandler(&normal_handler
);
989 // Test that only the click on the top portion of the window is picked up. The
990 // click on the bottom hits the virtual keyboard instead.
991 generator
.MoveMouseTo(left
, top
);
992 generator
.ClickLeftButton();
993 EXPECT_EQ(1, normal_handler
.num_mouse_events());
994 generator
.MoveMouseTo(left
, bottom
);
995 generator
.ClickLeftButton();
996 EXPECT_EQ(1, normal_handler
.num_mouse_events());
998 // Menu overlaps virtual keyboard.
999 aura::test::TestWindowDelegate delegate2
;
1000 scoped_ptr
<aura::Window
> menu(CreateTestWindowInShellWithDelegateAndType(
1002 ui::wm::WINDOW_TYPE_MENU
,
1004 gfx::Rect(window_width
, 0, window_width
, window_height
)));
1005 menu
->set_owned_by_parent(false);
1007 TargetHitTestEventHandler menu_handler
;
1008 menu
->AddPreTargetHandler(&menu_handler
);
1010 // Test that both clicks register.
1011 generator
.MoveMouseTo(right
, top
);
1012 generator
.ClickLeftButton();
1013 EXPECT_EQ(1, menu_handler
.num_mouse_events());
1014 generator
.MoveMouseTo(right
, bottom
);
1015 generator
.ClickLeftButton();
1016 EXPECT_EQ(2, menu_handler
.num_mouse_events());
1018 // Cleanup to ensure that the test windows are destroyed before their
1024 // Resolution in UpdateDisplay is not being respected on Windows 8.
1026 #define MAYBE_DisplayRotation DISABLED_DisplayRotation
1028 #define MAYBE_DisplayRotation DisplayRotation
1031 // Tests that the virtual keyboard correctly resizes with a change to display
1032 // orientation. See crbug/417612.
1033 TEST_F(VirtualKeyboardRootWindowControllerTest
, MAYBE_DisplayRotation
) {
1034 UpdateDisplay("800x600");
1035 aura::Window
* root_window
= Shell::GetPrimaryRootWindow();
1036 aura::Window
* keyboard_container
=
1037 Shell::GetContainer(root_window
, kShellWindowId_VirtualKeyboardContainer
);
1038 ASSERT_TRUE(keyboard_container
);
1039 keyboard::KeyboardController
* keyboard_controller
=
1040 keyboard::KeyboardController::GetInstance();
1041 keyboard_controller
->ShowKeyboard(false);
1042 keyboard_controller
->proxy()->GetKeyboardWindow()->SetBounds(
1043 gfx::Rect(0, 400, 800, 200));
1044 EXPECT_EQ("0,400 800x200", keyboard_container
->bounds().ToString());
1046 UpdateDisplay("600x800");
1047 EXPECT_EQ("0,600 600x200", keyboard_container
->bounds().ToString());