Restrict use of hardware-secure codecs based on the RendererPreference.
[chromium-blink-merge.git] / ash / root_window_controller_unittest.cc
blobe5e47aeb2a71e40551816cf09400861c06982c63
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/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"
43 using aura::Window;
44 using views::Widget;
46 namespace ash {
47 namespace {
49 class TestDelegate : public views::WidgetDelegateView {
50 public:
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;
61 private:
62 bool system_modal_;
64 DISALLOW_COPY_AND_ASSIGN(TestDelegate);
67 class DeleteOnBlurDelegate : public aura::test::TestWindowDelegate,
68 public aura::client::FocusChangeObserver {
69 public:
70 DeleteOnBlurDelegate() : window_(NULL) {}
71 ~DeleteOnBlurDelegate() override {}
73 void SetWindow(aura::Window* window) {
74 window_ = window;
75 aura::client::SetFocusChangeObserver(window_, this);
78 private:
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)
86 delete window_;
89 aura::Window* window_;
91 DISALLOW_COPY_AND_ASSIGN(DeleteOnBlurDelegate);
94 } // namespace
96 namespace test {
98 class RootWindowControllerTest : public test::AshTestBase {
99 public:
100 views::Widget* CreateTestWidget(const gfx::Rect& bounds) {
101 views::Widget* widget = views::Widget::CreateWindowWithContextAndBounds(
102 NULL, CurrentContext(), bounds);
103 widget->Show();
104 return widget;
107 views::Widget* CreateModalWidget(const gfx::Rect& bounds) {
108 views::Widget* widget = views::Widget::CreateWindowWithContextAndBounds(
109 new TestDelegate(true), CurrentContext(), bounds);
110 widget->Show();
111 return widget;
114 views::Widget* CreateModalWidgetWithParent(const gfx::Rect& bounds,
115 gfx::NativeWindow parent) {
116 views::Widget* widget =
117 views::Widget::CreateWindowWithParentAndBounds(new TestDelegate(true),
118 parent,
119 bounds);
120 widget->Show();
121 return widget;
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())
132 return;
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
187 // will not crash.
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);
194 tracker.Add(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())
263 return;
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())
296 return;
297 DisplayManager* display_manager = Shell::GetInstance()->display_manager();
298 display_manager->SetDefaultMultiDisplayMode(DisplayManager::UNIFIED);
299 display_manager->SetMultiDisplayMode(DisplayManager::UNIFIED);
300 UpdateDisplay("500x500");
301 const int kLockScreenWindowId = 1000;
302 const int kLockBackgroundWindowId = 1001;
304 RootWindowController* controller =
305 Shell::GetInstance()->GetPrimaryRootWindowController();
307 aura::Window* lock_container =
308 controller->GetContainer(kShellWindowId_LockScreenContainer);
309 aura::Window* lock_background_container =
310 controller->GetContainer(kShellWindowId_LockScreenBackgroundContainer);
312 views::Widget* lock_screen =
313 CreateModalWidgetWithParent(gfx::Rect(10, 10, 100, 100), lock_container);
314 lock_screen->GetNativeWindow()->set_id(kLockScreenWindowId);
315 lock_screen->SetFullscreen(true);
317 views::Widget* lock_background = CreateModalWidgetWithParent(
318 gfx::Rect(10, 10, 100, 100), lock_background_container);
319 lock_background->GetNativeWindow()->set_id(kLockBackgroundWindowId);
321 ASSERT_EQ(lock_screen->GetNativeWindow(),
322 controller->GetRootWindow()->GetChildById(kLockScreenWindowId));
323 ASSERT_EQ(lock_background->GetNativeWindow(),
324 controller->GetRootWindow()->GetChildById(kLockBackgroundWindowId));
325 EXPECT_EQ("0,0 500x500", lock_screen->GetNativeWindow()->bounds().ToString());
327 // Switch to unified.
328 UpdateDisplay("500x500,500x500");
330 // In unified mode, RWC is created
331 controller = Shell::GetInstance()->GetPrimaryRootWindowController();
333 ASSERT_EQ(lock_screen->GetNativeWindow(),
334 controller->GetRootWindow()->GetChildById(kLockScreenWindowId));
335 ASSERT_EQ(lock_background->GetNativeWindow(),
336 controller->GetRootWindow()->GetChildById(kLockBackgroundWindowId));
337 EXPECT_EQ("0,0 500x500", lock_screen->GetNativeWindow()->bounds().ToString());
339 // Switch to mirror.
340 display_manager->SetMirrorMode(true);
341 EXPECT_TRUE(display_manager->IsInMirrorMode());
343 controller = Shell::GetInstance()->GetPrimaryRootWindowController();
344 ASSERT_EQ(lock_screen->GetNativeWindow(),
345 controller->GetRootWindow()->GetChildById(kLockScreenWindowId));
346 ASSERT_EQ(lock_background->GetNativeWindow(),
347 controller->GetRootWindow()->GetChildById(kLockBackgroundWindowId));
348 EXPECT_EQ("0,0 500x500", lock_screen->GetNativeWindow()->bounds().ToString());
350 // Switch to unified.
351 display_manager->SetMirrorMode(false);
352 EXPECT_TRUE(display_manager->IsInUnifiedMode());
354 controller = Shell::GetInstance()->GetPrimaryRootWindowController();
356 ASSERT_EQ(lock_screen->GetNativeWindow(),
357 controller->GetRootWindow()->GetChildById(kLockScreenWindowId));
358 ASSERT_EQ(lock_background->GetNativeWindow(),
359 controller->GetRootWindow()->GetChildById(kLockBackgroundWindowId));
360 EXPECT_EQ("0,0 500x500", lock_screen->GetNativeWindow()->bounds().ToString());
362 // Switch to single display.
363 UpdateDisplay("600x500");
364 EXPECT_FALSE(display_manager->IsInUnifiedMode());
365 EXPECT_FALSE(display_manager->IsInMirrorMode());
367 controller = Shell::GetInstance()->GetPrimaryRootWindowController();
369 ASSERT_EQ(lock_screen->GetNativeWindow(),
370 controller->GetRootWindow()->GetChildById(kLockScreenWindowId));
371 ASSERT_EQ(lock_background->GetNativeWindow(),
372 controller->GetRootWindow()->GetChildById(kLockBackgroundWindowId));
373 EXPECT_EQ("0,0 600x500", lock_screen->GetNativeWindow()->bounds().ToString());
376 TEST_F(RootWindowControllerTest, ModalContainer) {
377 UpdateDisplay("600x600");
378 Shell* shell = Shell::GetInstance();
379 RootWindowController* controller = shell->GetPrimaryRootWindowController();
380 EXPECT_EQ(user::LOGGED_IN_USER,
381 shell->system_tray_delegate()->GetUserLoginStatus());
382 EXPECT_EQ(controller->GetContainer(kShellWindowId_SystemModalContainer)
383 ->layout_manager(),
384 controller->GetSystemModalLayoutManager(NULL));
386 views::Widget* session_modal_widget =
387 CreateModalWidget(gfx::Rect(300, 10, 100, 100));
388 EXPECT_EQ(controller->GetContainer(kShellWindowId_SystemModalContainer)
389 ->layout_manager(),
390 controller->GetSystemModalLayoutManager(
391 session_modal_widget->GetNativeView()));
393 shell->session_state_delegate()->LockScreen();
394 EXPECT_EQ(user::LOGGED_IN_LOCKED,
395 shell->system_tray_delegate()->GetUserLoginStatus());
396 EXPECT_EQ(controller->GetContainer(kShellWindowId_LockSystemModalContainer)
397 ->layout_manager(),
398 controller->GetSystemModalLayoutManager(NULL));
400 aura::Window* lock_container =
401 controller->GetContainer(kShellWindowId_LockScreenContainer);
402 views::Widget* lock_modal_widget =
403 CreateModalWidgetWithParent(gfx::Rect(300, 10, 100, 100), lock_container);
404 EXPECT_EQ(controller->GetContainer(kShellWindowId_LockSystemModalContainer)
405 ->layout_manager(),
406 controller->GetSystemModalLayoutManager(
407 lock_modal_widget->GetNativeView()));
408 EXPECT_EQ(controller->GetContainer(kShellWindowId_SystemModalContainer)
409 ->layout_manager(),
410 controller->GetSystemModalLayoutManager(
411 session_modal_widget->GetNativeView()));
413 shell->session_state_delegate()->UnlockScreen();
416 TEST_F(RootWindowControllerTest, ModalContainerNotLoggedInLoggedIn) {
417 UpdateDisplay("600x600");
418 Shell* shell = Shell::GetInstance();
420 // Configure login screen environment.
421 SetUserLoggedIn(false);
422 EXPECT_EQ(user::LOGGED_IN_NONE,
423 shell->system_tray_delegate()->GetUserLoginStatus());
424 EXPECT_EQ(0, shell->session_state_delegate()->NumberOfLoggedInUsers());
425 EXPECT_FALSE(shell->session_state_delegate()->IsActiveUserSessionStarted());
427 RootWindowController* controller = shell->GetPrimaryRootWindowController();
428 EXPECT_EQ(controller->GetContainer(kShellWindowId_LockSystemModalContainer)
429 ->layout_manager(),
430 controller->GetSystemModalLayoutManager(NULL));
432 aura::Window* lock_container =
433 controller->GetContainer(kShellWindowId_LockScreenContainer);
434 views::Widget* login_modal_widget =
435 CreateModalWidgetWithParent(gfx::Rect(300, 10, 100, 100), lock_container);
436 EXPECT_EQ(controller->GetContainer(kShellWindowId_LockSystemModalContainer)
437 ->layout_manager(),
438 controller->GetSystemModalLayoutManager(
439 login_modal_widget->GetNativeView()));
440 login_modal_widget->Close();
442 // Configure user session environment.
443 SetUserLoggedIn(true);
444 SetSessionStarted(true);
445 EXPECT_EQ(user::LOGGED_IN_USER,
446 shell->system_tray_delegate()->GetUserLoginStatus());
447 EXPECT_EQ(1, shell->session_state_delegate()->NumberOfLoggedInUsers());
448 EXPECT_TRUE(shell->session_state_delegate()->IsActiveUserSessionStarted());
449 EXPECT_EQ(controller->GetContainer(kShellWindowId_SystemModalContainer)
450 ->layout_manager(),
451 controller->GetSystemModalLayoutManager(NULL));
453 views::Widget* session_modal_widget =
454 CreateModalWidget(gfx::Rect(300, 10, 100, 100));
455 EXPECT_EQ(controller->GetContainer(kShellWindowId_SystemModalContainer)
456 ->layout_manager(),
457 controller->GetSystemModalLayoutManager(
458 session_modal_widget->GetNativeView()));
461 TEST_F(RootWindowControllerTest, ModalContainerBlockedSession) {
462 UpdateDisplay("600x600");
463 RootWindowController* controller = Shell::GetPrimaryRootWindowController();
464 aura::Window* lock_container =
465 controller->GetContainer(kShellWindowId_LockScreenContainer);
466 for (int block_reason = FIRST_BLOCK_REASON;
467 block_reason < NUMBER_OF_BLOCK_REASONS;
468 ++block_reason) {
469 views::Widget* session_modal_widget =
470 CreateModalWidget(gfx::Rect(300, 10, 100, 100));
471 EXPECT_EQ(controller->GetContainer(kShellWindowId_SystemModalContainer)
472 ->layout_manager(),
473 controller->GetSystemModalLayoutManager(
474 session_modal_widget->GetNativeView()));
475 EXPECT_EQ(controller->GetContainer(kShellWindowId_SystemModalContainer)
476 ->layout_manager(),
477 controller->GetSystemModalLayoutManager(NULL));
478 session_modal_widget->Close();
480 BlockUserSession(static_cast<UserSessionBlockReason>(block_reason));
482 EXPECT_EQ(controller->GetContainer(kShellWindowId_LockSystemModalContainer)
483 ->layout_manager(),
484 controller->GetSystemModalLayoutManager(NULL));
486 views::Widget* lock_modal_widget =
487 CreateModalWidgetWithParent(gfx::Rect(300, 10, 100, 100),
488 lock_container);
489 EXPECT_EQ(controller->GetContainer(kShellWindowId_LockSystemModalContainer)
490 ->layout_manager(),
491 controller->GetSystemModalLayoutManager(
492 lock_modal_widget->GetNativeView()));
494 session_modal_widget =
495 CreateModalWidget(gfx::Rect(300, 10, 100, 100));
496 EXPECT_EQ(controller->GetContainer(kShellWindowId_SystemModalContainer)
497 ->layout_manager(),
498 controller->GetSystemModalLayoutManager(
499 session_modal_widget->GetNativeView()));
500 session_modal_widget->Close();
502 lock_modal_widget->Close();
503 UnblockUserSession();
507 TEST_F(RootWindowControllerTest, GetWindowForFullscreenMode) {
508 UpdateDisplay("600x600");
509 RootWindowController* controller =
510 Shell::GetInstance()->GetPrimaryRootWindowController();
512 Widget* w1 = CreateTestWidget(gfx::Rect(0, 0, 100, 100));
513 w1->Maximize();
514 Widget* w2 = CreateTestWidget(gfx::Rect(0, 0, 100, 100));
515 w2->SetFullscreen(true);
516 // |w3| is a transient child of |w2|.
517 Widget* w3 = Widget::CreateWindowWithParentAndBounds(NULL,
518 w2->GetNativeWindow(), gfx::Rect(0, 0, 100, 100));
520 // Test that GetWindowForFullscreenMode() finds the fullscreen window when one
521 // of its transient children is active.
522 w3->Activate();
523 EXPECT_EQ(w2->GetNativeWindow(), controller->GetWindowForFullscreenMode());
525 // If the topmost window is not fullscreen, it returns NULL.
526 w1->Activate();
527 EXPECT_EQ(NULL, controller->GetWindowForFullscreenMode());
528 w1->Close();
529 w3->Close();
531 // Only w2 remains, if minimized GetWindowForFullscreenMode should return
532 // NULL.
533 w2->Activate();
534 EXPECT_EQ(w2->GetNativeWindow(), controller->GetWindowForFullscreenMode());
535 w2->Minimize();
536 EXPECT_EQ(NULL, controller->GetWindowForFullscreenMode());
539 TEST_F(RootWindowControllerTest, MultipleDisplaysGetWindowForFullscreenMode) {
540 if (!SupportsMultipleDisplays())
541 return;
543 UpdateDisplay("600x600,600x600");
544 Shell::RootWindowControllerList controllers =
545 Shell::GetInstance()->GetAllRootWindowControllers();
547 Widget* w1 = CreateTestWidget(gfx::Rect(0, 0, 100, 100));
548 w1->Maximize();
549 Widget* w2 = CreateTestWidget(gfx::Rect(0, 0, 100, 100));
550 w2->SetFullscreen(true);
551 Widget* w3 = CreateTestWidget(gfx::Rect(600, 0, 100, 100));
553 EXPECT_EQ(w1->GetNativeWindow()->GetRootWindow(),
554 controllers[0]->GetRootWindow());
555 EXPECT_EQ(w2->GetNativeWindow()->GetRootWindow(),
556 controllers[0]->GetRootWindow());
557 EXPECT_EQ(w3->GetNativeWindow()->GetRootWindow(),
558 controllers[1]->GetRootWindow());
560 w1->Activate();
561 EXPECT_EQ(NULL, controllers[0]->GetWindowForFullscreenMode());
562 EXPECT_EQ(NULL, controllers[1]->GetWindowForFullscreenMode());
564 w2->Activate();
565 EXPECT_EQ(w2->GetNativeWindow(),
566 controllers[0]->GetWindowForFullscreenMode());
567 EXPECT_EQ(NULL, controllers[1]->GetWindowForFullscreenMode());
569 // Verify that the first root window controller remains in fullscreen mode
570 // when a window on the other display is activated.
571 w3->Activate();
572 EXPECT_EQ(w2->GetNativeWindow(),
573 controllers[0]->GetWindowForFullscreenMode());
574 EXPECT_EQ(NULL, controllers[1]->GetWindowForFullscreenMode());
577 // Test that user session window can't be focused if user session blocked by
578 // some overlapping UI.
579 TEST_F(RootWindowControllerTest, FocusBlockedWindow) {
580 UpdateDisplay("600x600");
581 RootWindowController* controller =
582 Shell::GetInstance()->GetPrimaryRootWindowController();
583 aura::Window* lock_container =
584 controller->GetContainer(kShellWindowId_LockScreenContainer);
585 aura::Window* lock_window = Widget::CreateWindowWithParentAndBounds(NULL,
586 lock_container, gfx::Rect(0, 0, 100, 100))->GetNativeView();
587 lock_window->Show();
588 aura::Window* session_window =
589 CreateTestWidget(gfx::Rect(0, 0, 100, 100))->GetNativeView();
590 session_window->Show();
592 for (int block_reason = FIRST_BLOCK_REASON;
593 block_reason < NUMBER_OF_BLOCK_REASONS;
594 ++block_reason) {
595 BlockUserSession(static_cast<UserSessionBlockReason>(block_reason));
596 lock_window->Focus();
597 EXPECT_TRUE(lock_window->HasFocus());
598 session_window->Focus();
599 EXPECT_FALSE(session_window->HasFocus());
600 UnblockUserSession();
604 // Tracks whether OnWindowDestroying() has been invoked.
605 class DestroyedWindowObserver : public aura::WindowObserver {
606 public:
607 DestroyedWindowObserver() : destroyed_(false), window_(NULL) {}
608 ~DestroyedWindowObserver() override { Shutdown(); }
610 void SetWindow(Window* window) {
611 window_ = window;
612 window->AddObserver(this);
615 bool destroyed() const { return destroyed_; }
617 // WindowObserver overrides:
618 void OnWindowDestroying(Window* window) override {
619 destroyed_ = true;
620 Shutdown();
623 private:
624 void Shutdown() {
625 if (!window_)
626 return;
627 window_->RemoveObserver(this);
628 window_ = NULL;
631 bool destroyed_;
632 Window* window_;
634 DISALLOW_COPY_AND_ASSIGN(DestroyedWindowObserver);
637 // Verifies shutdown doesn't delete windows that are not owned by the parent.
638 TEST_F(RootWindowControllerTest, DontDeleteWindowsNotOwnedByParent) {
639 DestroyedWindowObserver observer1;
640 aura::test::TestWindowDelegate delegate1;
641 aura::Window* window1 = new aura::Window(&delegate1);
642 window1->SetType(ui::wm::WINDOW_TYPE_CONTROL);
643 window1->set_owned_by_parent(false);
644 observer1.SetWindow(window1);
645 window1->Init(ui::LAYER_NOT_DRAWN);
646 aura::client::ParentWindowWithContext(
647 window1, Shell::GetInstance()->GetPrimaryRootWindow(), gfx::Rect());
649 DestroyedWindowObserver observer2;
650 aura::Window* window2 = new aura::Window(NULL);
651 window2->set_owned_by_parent(false);
652 observer2.SetWindow(window2);
653 window2->Init(ui::LAYER_NOT_DRAWN);
654 Shell::GetInstance()->GetPrimaryRootWindow()->AddChild(window2);
656 Shell::GetInstance()->GetPrimaryRootWindowController()->CloseChildWindows();
658 ASSERT_FALSE(observer1.destroyed());
659 delete window1;
661 ASSERT_FALSE(observer2.destroyed());
662 delete window2;
665 typedef test::NoSessionAshTestBase NoSessionRootWindowControllerTest;
667 // Make sure that an event handler exists for entire display area.
668 TEST_F(NoSessionRootWindowControllerTest, Event) {
669 // Hide the shelf since it might otherwise get an event target.
670 RootWindowController* controller = Shell::GetPrimaryRootWindowController();
671 ShelfLayoutManager* shelf_layout_manager =
672 controller->GetShelfLayoutManager();
673 shelf_layout_manager->SetAutoHideBehavior(
674 ash::SHELF_AUTO_HIDE_ALWAYS_HIDDEN);
676 aura::Window* root = Shell::GetPrimaryRootWindow();
677 const gfx::Size size = root->bounds().size();
678 aura::Window* event_target = root->GetEventHandlerForPoint(gfx::Point(0, 0));
679 EXPECT_TRUE(event_target);
680 EXPECT_EQ(event_target,
681 root->GetEventHandlerForPoint(gfx::Point(0, size.height() - 1)));
682 EXPECT_EQ(event_target,
683 root->GetEventHandlerForPoint(gfx::Point(size.width() - 1, 0)));
684 EXPECT_EQ(event_target,
685 root->GetEventHandlerForPoint(gfx::Point(0, size.height() - 1)));
686 EXPECT_EQ(event_target,
687 root->GetEventHandlerForPoint(
688 gfx::Point(size.width() - 1, size.height() - 1)));
691 class VirtualKeyboardRootWindowControllerTest
692 : public RootWindowControllerTest {
693 public:
694 VirtualKeyboardRootWindowControllerTest() {}
695 ~VirtualKeyboardRootWindowControllerTest() override {}
697 void SetUp() override {
698 base::CommandLine::ForCurrentProcess()->AppendSwitch(
699 keyboard::switches::kEnableVirtualKeyboard);
700 test::AshTestBase::SetUp();
701 Shell::GetPrimaryRootWindowController()->ActivateKeyboard(
702 keyboard::KeyboardController::GetInstance());
705 private:
706 DISALLOW_COPY_AND_ASSIGN(VirtualKeyboardRootWindowControllerTest);
709 class MockTextInputClient : public ui::DummyTextInputClient {
710 public:
711 MockTextInputClient() :
712 ui::DummyTextInputClient(ui::TEXT_INPUT_TYPE_TEXT) {}
714 void EnsureCaretInRect(const gfx::Rect& rect) override {
715 visible_rect_ = rect;
718 const gfx::Rect& visible_rect() const {
719 return visible_rect_;
722 private:
723 gfx::Rect visible_rect_;
725 DISALLOW_COPY_AND_ASSIGN(MockTextInputClient);
728 class TargetHitTestEventHandler : public ui::test::TestEventHandler {
729 public:
730 TargetHitTestEventHandler() {}
732 // ui::test::TestEventHandler overrides.
733 void OnMouseEvent(ui::MouseEvent* event) override {
734 if (event->type() == ui::ET_MOUSE_PRESSED)
735 ui::test::TestEventHandler::OnMouseEvent(event);
736 event->StopPropagation();
739 private:
740 DISALLOW_COPY_AND_ASSIGN(TargetHitTestEventHandler);
743 // Test for http://crbug.com/297858. Virtual keyboard container should only show
744 // on primary root window.
745 TEST_F(VirtualKeyboardRootWindowControllerTest,
746 VirtualKeyboardOnPrimaryRootWindowOnly) {
747 if (!SupportsMultipleDisplays())
748 return;
750 UpdateDisplay("500x500,500x500");
752 aura::Window::Windows root_windows = Shell::GetAllRootWindows();
753 aura::Window* primary_root_window = Shell::GetPrimaryRootWindow();
754 aura::Window* secondary_root_window =
755 root_windows[0] == primary_root_window ?
756 root_windows[1] : root_windows[0];
758 ASSERT_TRUE(Shell::GetContainer(primary_root_window,
759 kShellWindowId_VirtualKeyboardContainer));
760 ASSERT_FALSE(Shell::GetContainer(secondary_root_window,
761 kShellWindowId_VirtualKeyboardContainer));
764 // Test for http://crbug.com/263599. Virtual keyboard should be able to receive
765 // events at blocked user session.
766 TEST_F(VirtualKeyboardRootWindowControllerTest,
767 ClickVirtualKeyboardInBlockedWindow) {
768 aura::Window* root_window = Shell::GetPrimaryRootWindow();
769 aura::Window* keyboard_container =
770 Shell::GetContainer(root_window, kShellWindowId_VirtualKeyboardContainer);
771 ASSERT_TRUE(keyboard_container);
772 keyboard_container->Show();
774 aura::Window* keyboard_window = keyboard::KeyboardController::GetInstance()->
775 proxy()->GetKeyboardWindow();
776 keyboard_container->AddChild(keyboard_window);
777 keyboard_window->set_owned_by_parent(false);
778 keyboard_window->SetBounds(gfx::Rect());
779 keyboard_window->Show();
781 ui::test::TestEventHandler handler;
782 root_window->AddPreTargetHandler(&handler);
784 ui::test::EventGenerator event_generator(root_window, keyboard_window);
785 event_generator.ClickLeftButton();
786 int expected_mouse_presses = 1;
787 EXPECT_EQ(expected_mouse_presses, handler.num_mouse_events() / 2);
789 for (int block_reason = FIRST_BLOCK_REASON;
790 block_reason < NUMBER_OF_BLOCK_REASONS;
791 ++block_reason) {
792 BlockUserSession(static_cast<UserSessionBlockReason>(block_reason));
793 event_generator.ClickLeftButton();
794 expected_mouse_presses++;
795 EXPECT_EQ(expected_mouse_presses, handler.num_mouse_events() / 2);
796 UnblockUserSession();
798 root_window->RemovePreTargetHandler(&handler);
801 // Test for http://crbug.com/299787. RootWindowController should delete
802 // the old container since the keyboard controller creates a new window in
803 // GetWindowContainer().
804 TEST_F(VirtualKeyboardRootWindowControllerTest,
805 DeleteOldContainerOnVirtualKeyboardInit) {
806 aura::Window* root_window = Shell::GetPrimaryRootWindow();
807 aura::Window* keyboard_container =
808 Shell::GetContainer(root_window, kShellWindowId_VirtualKeyboardContainer);
809 ASSERT_TRUE(keyboard_container);
810 // Track the keyboard container window.
811 aura::WindowTracker tracker;
812 tracker.Add(keyboard_container);
813 // Mock a login user profile change to reinitialize the keyboard.
814 ash::Shell::GetInstance()->OnLoginUserProfilePrepared();
815 // keyboard_container should no longer be present.
816 EXPECT_FALSE(tracker.Contains(keyboard_container));
819 // Test for crbug.com/342524. After user login, the work space should restore to
820 // full screen.
821 TEST_F(VirtualKeyboardRootWindowControllerTest, RestoreWorkspaceAfterLogin) {
822 aura::Window* root_window = Shell::GetPrimaryRootWindow();
823 aura::Window* keyboard_container =
824 Shell::GetContainer(root_window, kShellWindowId_VirtualKeyboardContainer);
825 keyboard_container->Show();
826 keyboard::KeyboardController* controller =
827 keyboard::KeyboardController::GetInstance();
828 aura::Window* keyboard_window = controller->proxy()->GetKeyboardWindow();
829 keyboard_container->AddChild(keyboard_window);
830 keyboard_window->set_owned_by_parent(false);
831 keyboard_window->SetBounds(keyboard::FullWidthKeyboardBoundsFromRootBounds(
832 root_window->bounds(), 100));
833 keyboard_window->Show();
835 gfx::Rect before = ash::Shell::GetScreen()->GetPrimaryDisplay().work_area();
837 // Notify keyboard bounds changing.
838 controller->NotifyKeyboardBoundsChanging(keyboard_container->bounds());
840 if (!keyboard::IsKeyboardOverscrollEnabled()) {
841 gfx::Rect after = ash::Shell::GetScreen()->GetPrimaryDisplay().work_area();
842 EXPECT_LT(after, before);
845 // Mock a login user profile change to reinitialize the keyboard.
846 ash::Shell::GetInstance()->OnLoginUserProfilePrepared();
847 EXPECT_EQ(ash::Shell::GetScreen()->GetPrimaryDisplay().work_area(), before);
850 // Ensure that system modal dialogs do not block events targeted at the virtual
851 // keyboard.
852 TEST_F(VirtualKeyboardRootWindowControllerTest, ClickWithActiveModalDialog) {
853 aura::Window* root_window = Shell::GetPrimaryRootWindow();
854 aura::Window* keyboard_container =
855 Shell::GetContainer(root_window, kShellWindowId_VirtualKeyboardContainer);
856 ASSERT_TRUE(keyboard_container);
857 keyboard_container->Show();
859 aura::Window* keyboard_window = keyboard::KeyboardController::GetInstance()->
860 proxy()->GetKeyboardWindow();
861 keyboard_container->AddChild(keyboard_window);
862 keyboard_window->set_owned_by_parent(false);
863 keyboard_window->SetBounds(keyboard::FullWidthKeyboardBoundsFromRootBounds(
864 root_window->bounds(), 100));
866 ui::test::TestEventHandler handler;
867 root_window->AddPreTargetHandler(&handler);
868 ui::test::EventGenerator root_window_event_generator(root_window);
869 ui::test::EventGenerator keyboard_event_generator(root_window,
870 keyboard_window);
872 views::Widget* modal_widget =
873 CreateModalWidget(gfx::Rect(300, 10, 100, 100));
875 // Verify that mouse events to the root window are block with a visble modal
876 // dialog.
877 root_window_event_generator.ClickLeftButton();
878 EXPECT_EQ(0, handler.num_mouse_events());
880 // Verify that event dispatch to the virtual keyboard is unblocked.
881 keyboard_event_generator.ClickLeftButton();
882 EXPECT_EQ(1, handler.num_mouse_events() / 2);
884 modal_widget->Close();
886 // Verify that mouse events are now unblocked to the root window.
887 root_window_event_generator.ClickLeftButton();
888 EXPECT_EQ(2, handler.num_mouse_events() / 2);
889 root_window->RemovePreTargetHandler(&handler);
892 // Ensure that the visible area for scrolling the text caret excludes the
893 // region occluded by the on-screen keyboard.
894 TEST_F(VirtualKeyboardRootWindowControllerTest, EnsureCaretInWorkArea) {
895 keyboard::KeyboardController* keyboard_controller =
896 keyboard::KeyboardController::GetInstance();
897 keyboard::KeyboardControllerProxy* proxy = keyboard_controller->proxy();
899 MockTextInputClient text_input_client;
900 ui::InputMethod* input_method = proxy->GetInputMethod();
901 ASSERT_TRUE(input_method);
902 if (switches::IsTextInputFocusManagerEnabled()) {
903 ui::TextInputFocusManager::GetInstance()->FocusTextInputClient(
904 &text_input_client);
905 } else {
906 input_method->SetFocusedTextInputClient(&text_input_client);
909 aura::Window* root_window = Shell::GetPrimaryRootWindow();
910 aura::Window* keyboard_container =
911 Shell::GetContainer(root_window, kShellWindowId_VirtualKeyboardContainer);
912 ASSERT_TRUE(keyboard_container);
913 keyboard_container->Show();
915 const int keyboard_height = 100;
916 aura::Window* keyboard_window =proxy->GetKeyboardWindow();
917 keyboard_container->AddChild(keyboard_window);
918 keyboard_window->set_owned_by_parent(false);
919 keyboard_window->SetBounds(keyboard::FullWidthKeyboardBoundsFromRootBounds(
920 root_window->bounds(), keyboard_height));
922 proxy->EnsureCaretInWorkArea();
923 ASSERT_EQ(root_window->bounds().width(),
924 text_input_client.visible_rect().width());
925 ASSERT_EQ(root_window->bounds().height() - keyboard_height,
926 text_input_client.visible_rect().height());
928 if (switches::IsTextInputFocusManagerEnabled()) {
929 ui::TextInputFocusManager::GetInstance()->BlurTextInputClient(
930 &text_input_client);
931 } else {
932 input_method->SetFocusedTextInputClient(NULL);
936 // Tests that the virtual keyboard does not block context menus. The virtual
937 // keyboard should appear in front of most content, but not context menus. See
938 // crbug/377180.
939 TEST_F(VirtualKeyboardRootWindowControllerTest, ZOrderTest) {
940 UpdateDisplay("800x600");
941 keyboard::KeyboardController* keyboard_controller =
942 keyboard::KeyboardController::GetInstance();
943 keyboard::KeyboardControllerProxy* proxy = keyboard_controller->proxy();
945 aura::Window* root_window = Shell::GetPrimaryRootWindow();
946 aura::Window* keyboard_container =
947 Shell::GetContainer(root_window, kShellWindowId_VirtualKeyboardContainer);
948 ASSERT_TRUE(keyboard_container);
949 keyboard_container->Show();
951 const int keyboard_height = 200;
952 aura::Window* keyboard_window = proxy->GetKeyboardWindow();
953 keyboard_container->AddChild(keyboard_window);
954 keyboard_window->set_owned_by_parent(false);
955 gfx::Rect keyboard_bounds = keyboard::FullWidthKeyboardBoundsFromRootBounds(
956 root_window->bounds(), keyboard_height);
957 keyboard_window->SetBounds(keyboard_bounds);
958 keyboard_window->Show();
960 ui::test::EventGenerator generator(root_window);
962 // Cover the screen with two windows: a normal window on the left side and a
963 // context menu on the right side. When the virtual keyboard is displayed it
964 // partially occludes the normal window, but not the context menu. Compute
965 // positions for generating synthetic click events to perform hit tests,
966 // ensuring the correct window layering. 'top' is above the VK, whereas
967 // 'bottom' lies within the VK. 'left' is centered in the normal window, and
968 // 'right' is centered in the context menu.
969 int window_height = keyboard_bounds.bottom();
970 int window_width = keyboard_bounds.width() / 2;
971 int left = window_width / 2;
972 int right = 3 * window_width / 2;
973 int top = keyboard_bounds.y() / 2;
974 int bottom = window_height - keyboard_height / 2;
976 // Normal window is partially occluded by the virtual keyboard.
977 aura::test::TestWindowDelegate delegate;
978 scoped_ptr<aura::Window> normal(CreateTestWindowInShellWithDelegateAndType(
979 &delegate,
980 ui::wm::WINDOW_TYPE_NORMAL,
982 gfx::Rect(0, 0, window_width, window_height)));
983 normal->set_owned_by_parent(false);
984 normal->Show();
985 TargetHitTestEventHandler normal_handler;
986 normal->AddPreTargetHandler(&normal_handler);
988 // Test that only the click on the top portion of the window is picked up. The
989 // click on the bottom hits the virtual keyboard instead.
990 generator.MoveMouseTo(left, top);
991 generator.ClickLeftButton();
992 EXPECT_EQ(1, normal_handler.num_mouse_events());
993 generator.MoveMouseTo(left, bottom);
994 generator.ClickLeftButton();
995 EXPECT_EQ(1, normal_handler.num_mouse_events());
997 // Menu overlaps virtual keyboard.
998 aura::test::TestWindowDelegate delegate2;
999 scoped_ptr<aura::Window> menu(CreateTestWindowInShellWithDelegateAndType(
1000 &delegate2,
1001 ui::wm::WINDOW_TYPE_MENU,
1003 gfx::Rect(window_width, 0, window_width, window_height)));
1004 menu->set_owned_by_parent(false);
1005 menu->Show();
1006 TargetHitTestEventHandler menu_handler;
1007 menu->AddPreTargetHandler(&menu_handler);
1009 // Test that both clicks register.
1010 generator.MoveMouseTo(right, top);
1011 generator.ClickLeftButton();
1012 EXPECT_EQ(1, menu_handler.num_mouse_events());
1013 generator.MoveMouseTo(right, bottom);
1014 generator.ClickLeftButton();
1015 EXPECT_EQ(2, menu_handler.num_mouse_events());
1017 // Cleanup to ensure that the test windows are destroyed before their
1018 // delegates.
1019 normal.reset();
1020 menu.reset();
1023 // Resolution in UpdateDisplay is not being respected on Windows 8.
1024 #if defined(OS_WIN)
1025 #define MAYBE_DisplayRotation DISABLED_DisplayRotation
1026 #else
1027 #define MAYBE_DisplayRotation DisplayRotation
1028 #endif
1030 // Tests that the virtual keyboard correctly resizes with a change to display
1031 // orientation. See crbug/417612.
1032 TEST_F(VirtualKeyboardRootWindowControllerTest, MAYBE_DisplayRotation) {
1033 UpdateDisplay("800x600");
1034 aura::Window* root_window = Shell::GetPrimaryRootWindow();
1035 aura::Window* keyboard_container =
1036 Shell::GetContainer(root_window, kShellWindowId_VirtualKeyboardContainer);
1037 ASSERT_TRUE(keyboard_container);
1038 keyboard::KeyboardController* keyboard_controller =
1039 keyboard::KeyboardController::GetInstance();
1040 keyboard_controller->ShowKeyboard(false);
1041 keyboard_controller->proxy()->GetKeyboardWindow()->SetBounds(
1042 gfx::Rect(0, 400, 800, 200));
1043 EXPECT_EQ("0,400 800x200", keyboard_container->bounds().ToString());
1045 UpdateDisplay("600x800");
1046 EXPECT_EQ("0,600 600x200", keyboard_container->bounds().ToString());
1049 } // namespace test
1050 } // namespace ash