Stop running PPAPITest and PPAPINaClGLibcTest Pepper tests.
[chromium-blink-merge.git] / ash / root_window_controller_unittest.cc
blob0c321df8e234b0fa1189100258c442f59680988b
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"
9 #include "ash/shell.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/wm/system_modal_container_layout_manager.h"
14 #include "ash/wm/window_properties.h"
15 #include "ash/wm/window_state.h"
16 #include "ash/wm/window_util.h"
17 #include "base/command_line.h"
18 #include "base/memory/scoped_ptr.h"
19 #include "ui/aura/client/focus_change_observer.h"
20 #include "ui/aura/client/focus_client.h"
21 #include "ui/aura/client/window_tree_client.h"
22 #include "ui/aura/env.h"
23 #include "ui/aura/test/test_window_delegate.h"
24 #include "ui/aura/test/test_windows.h"
25 #include "ui/aura/window.h"
26 #include "ui/aura/window_event_dispatcher.h"
27 #include "ui/aura/window_tracker.h"
28 #include "ui/base/ime/dummy_text_input_client.h"
29 #include "ui/base/ime/input_method.h"
30 #include "ui/base/ime/text_input_client.h"
31 #include "ui/base/ime/text_input_focus_manager.h"
32 #include "ui/base/ui_base_switches_util.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 TEST_F(RootWindowControllerTest, ModalContainer) {
293 UpdateDisplay("600x600");
294 Shell* shell = Shell::GetInstance();
295 RootWindowController* controller = shell->GetPrimaryRootWindowController();
296 EXPECT_EQ(user::LOGGED_IN_USER,
297 shell->system_tray_delegate()->GetUserLoginStatus());
298 EXPECT_EQ(controller->GetContainer(kShellWindowId_SystemModalContainer)
299 ->layout_manager(),
300 controller->GetSystemModalLayoutManager(NULL));
302 views::Widget* session_modal_widget =
303 CreateModalWidget(gfx::Rect(300, 10, 100, 100));
304 EXPECT_EQ(controller->GetContainer(kShellWindowId_SystemModalContainer)
305 ->layout_manager(),
306 controller->GetSystemModalLayoutManager(
307 session_modal_widget->GetNativeView()));
309 shell->session_state_delegate()->LockScreen();
310 EXPECT_EQ(user::LOGGED_IN_LOCKED,
311 shell->system_tray_delegate()->GetUserLoginStatus());
312 EXPECT_EQ(controller->GetContainer(kShellWindowId_LockSystemModalContainer)
313 ->layout_manager(),
314 controller->GetSystemModalLayoutManager(NULL));
316 aura::Window* lock_container =
317 controller->GetContainer(kShellWindowId_LockScreenContainer);
318 views::Widget* lock_modal_widget =
319 CreateModalWidgetWithParent(gfx::Rect(300, 10, 100, 100), lock_container);
320 EXPECT_EQ(controller->GetContainer(kShellWindowId_LockSystemModalContainer)
321 ->layout_manager(),
322 controller->GetSystemModalLayoutManager(
323 lock_modal_widget->GetNativeView()));
324 EXPECT_EQ(controller->GetContainer(kShellWindowId_SystemModalContainer)
325 ->layout_manager(),
326 controller->GetSystemModalLayoutManager(
327 session_modal_widget->GetNativeView()));
329 shell->session_state_delegate()->UnlockScreen();
332 TEST_F(RootWindowControllerTest, ModalContainerNotLoggedInLoggedIn) {
333 UpdateDisplay("600x600");
334 Shell* shell = Shell::GetInstance();
336 // Configure login screen environment.
337 SetUserLoggedIn(false);
338 EXPECT_EQ(user::LOGGED_IN_NONE,
339 shell->system_tray_delegate()->GetUserLoginStatus());
340 EXPECT_EQ(0, shell->session_state_delegate()->NumberOfLoggedInUsers());
341 EXPECT_FALSE(shell->session_state_delegate()->IsActiveUserSessionStarted());
343 RootWindowController* controller = shell->GetPrimaryRootWindowController();
344 EXPECT_EQ(controller->GetContainer(kShellWindowId_LockSystemModalContainer)
345 ->layout_manager(),
346 controller->GetSystemModalLayoutManager(NULL));
348 aura::Window* lock_container =
349 controller->GetContainer(kShellWindowId_LockScreenContainer);
350 views::Widget* login_modal_widget =
351 CreateModalWidgetWithParent(gfx::Rect(300, 10, 100, 100), lock_container);
352 EXPECT_EQ(controller->GetContainer(kShellWindowId_LockSystemModalContainer)
353 ->layout_manager(),
354 controller->GetSystemModalLayoutManager(
355 login_modal_widget->GetNativeView()));
356 login_modal_widget->Close();
358 // Configure user session environment.
359 SetUserLoggedIn(true);
360 SetSessionStarted(true);
361 EXPECT_EQ(user::LOGGED_IN_USER,
362 shell->system_tray_delegate()->GetUserLoginStatus());
363 EXPECT_EQ(1, shell->session_state_delegate()->NumberOfLoggedInUsers());
364 EXPECT_TRUE(shell->session_state_delegate()->IsActiveUserSessionStarted());
365 EXPECT_EQ(controller->GetContainer(kShellWindowId_SystemModalContainer)
366 ->layout_manager(),
367 controller->GetSystemModalLayoutManager(NULL));
369 views::Widget* session_modal_widget =
370 CreateModalWidget(gfx::Rect(300, 10, 100, 100));
371 EXPECT_EQ(controller->GetContainer(kShellWindowId_SystemModalContainer)
372 ->layout_manager(),
373 controller->GetSystemModalLayoutManager(
374 session_modal_widget->GetNativeView()));
377 TEST_F(RootWindowControllerTest, ModalContainerBlockedSession) {
378 UpdateDisplay("600x600");
379 RootWindowController* controller = Shell::GetPrimaryRootWindowController();
380 aura::Window* lock_container =
381 controller->GetContainer(kShellWindowId_LockScreenContainer);
382 for (int block_reason = FIRST_BLOCK_REASON;
383 block_reason < NUMBER_OF_BLOCK_REASONS;
384 ++block_reason) {
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()));
391 EXPECT_EQ(controller->GetContainer(kShellWindowId_SystemModalContainer)
392 ->layout_manager(),
393 controller->GetSystemModalLayoutManager(NULL));
394 session_modal_widget->Close();
396 BlockUserSession(static_cast<UserSessionBlockReason>(block_reason));
398 EXPECT_EQ(controller->GetContainer(kShellWindowId_LockSystemModalContainer)
399 ->layout_manager(),
400 controller->GetSystemModalLayoutManager(NULL));
402 views::Widget* lock_modal_widget =
403 CreateModalWidgetWithParent(gfx::Rect(300, 10, 100, 100),
404 lock_container);
405 EXPECT_EQ(controller->GetContainer(kShellWindowId_LockSystemModalContainer)
406 ->layout_manager(),
407 controller->GetSystemModalLayoutManager(
408 lock_modal_widget->GetNativeView()));
410 session_modal_widget =
411 CreateModalWidget(gfx::Rect(300, 10, 100, 100));
412 EXPECT_EQ(controller->GetContainer(kShellWindowId_SystemModalContainer)
413 ->layout_manager(),
414 controller->GetSystemModalLayoutManager(
415 session_modal_widget->GetNativeView()));
416 session_modal_widget->Close();
418 lock_modal_widget->Close();
419 UnblockUserSession();
423 TEST_F(RootWindowControllerTest, GetWindowForFullscreenMode) {
424 UpdateDisplay("600x600");
425 RootWindowController* controller =
426 Shell::GetInstance()->GetPrimaryRootWindowController();
428 Widget* w1 = CreateTestWidget(gfx::Rect(0, 0, 100, 100));
429 w1->Maximize();
430 Widget* w2 = CreateTestWidget(gfx::Rect(0, 0, 100, 100));
431 w2->SetFullscreen(true);
432 // |w3| is a transient child of |w2|.
433 Widget* w3 = Widget::CreateWindowWithParentAndBounds(NULL,
434 w2->GetNativeWindow(), gfx::Rect(0, 0, 100, 100));
436 // Test that GetWindowForFullscreenMode() finds the fullscreen window when one
437 // of its transient children is active.
438 w3->Activate();
439 EXPECT_EQ(w2->GetNativeWindow(), controller->GetWindowForFullscreenMode());
441 // If the topmost window is not fullscreen, it returns NULL.
442 w1->Activate();
443 EXPECT_EQ(NULL, controller->GetWindowForFullscreenMode());
444 w1->Close();
445 w3->Close();
447 // Only w2 remains, if minimized GetWindowForFullscreenMode should return
448 // NULL.
449 w2->Activate();
450 EXPECT_EQ(w2->GetNativeWindow(), controller->GetWindowForFullscreenMode());
451 w2->Minimize();
452 EXPECT_EQ(NULL, controller->GetWindowForFullscreenMode());
455 TEST_F(RootWindowControllerTest, MultipleDisplaysGetWindowForFullscreenMode) {
456 if (!SupportsMultipleDisplays())
457 return;
459 UpdateDisplay("600x600,600x600");
460 Shell::RootWindowControllerList controllers =
461 Shell::GetInstance()->GetAllRootWindowControllers();
463 Widget* w1 = CreateTestWidget(gfx::Rect(0, 0, 100, 100));
464 w1->Maximize();
465 Widget* w2 = CreateTestWidget(gfx::Rect(0, 0, 100, 100));
466 w2->SetFullscreen(true);
467 Widget* w3 = CreateTestWidget(gfx::Rect(600, 0, 100, 100));
469 EXPECT_EQ(w1->GetNativeWindow()->GetRootWindow(),
470 controllers[0]->GetRootWindow());
471 EXPECT_EQ(w2->GetNativeWindow()->GetRootWindow(),
472 controllers[0]->GetRootWindow());
473 EXPECT_EQ(w3->GetNativeWindow()->GetRootWindow(),
474 controllers[1]->GetRootWindow());
476 w1->Activate();
477 EXPECT_EQ(NULL, controllers[0]->GetWindowForFullscreenMode());
478 EXPECT_EQ(NULL, controllers[1]->GetWindowForFullscreenMode());
480 w2->Activate();
481 EXPECT_EQ(w2->GetNativeWindow(),
482 controllers[0]->GetWindowForFullscreenMode());
483 EXPECT_EQ(NULL, controllers[1]->GetWindowForFullscreenMode());
485 // Verify that the first root window controller remains in fullscreen mode
486 // when a window on the other display is activated.
487 w3->Activate();
488 EXPECT_EQ(w2->GetNativeWindow(),
489 controllers[0]->GetWindowForFullscreenMode());
490 EXPECT_EQ(NULL, controllers[1]->GetWindowForFullscreenMode());
493 // Test that user session window can't be focused if user session blocked by
494 // some overlapping UI.
495 TEST_F(RootWindowControllerTest, FocusBlockedWindow) {
496 UpdateDisplay("600x600");
497 RootWindowController* controller =
498 Shell::GetInstance()->GetPrimaryRootWindowController();
499 aura::Window* lock_container =
500 controller->GetContainer(kShellWindowId_LockScreenContainer);
501 aura::Window* lock_window = Widget::CreateWindowWithParentAndBounds(NULL,
502 lock_container, gfx::Rect(0, 0, 100, 100))->GetNativeView();
503 lock_window->Show();
504 aura::Window* session_window =
505 CreateTestWidget(gfx::Rect(0, 0, 100, 100))->GetNativeView();
506 session_window->Show();
508 for (int block_reason = FIRST_BLOCK_REASON;
509 block_reason < NUMBER_OF_BLOCK_REASONS;
510 ++block_reason) {
511 BlockUserSession(static_cast<UserSessionBlockReason>(block_reason));
512 lock_window->Focus();
513 EXPECT_TRUE(lock_window->HasFocus());
514 session_window->Focus();
515 EXPECT_FALSE(session_window->HasFocus());
516 UnblockUserSession();
520 // Tracks whether OnWindowDestroying() has been invoked.
521 class DestroyedWindowObserver : public aura::WindowObserver {
522 public:
523 DestroyedWindowObserver() : destroyed_(false), window_(NULL) {}
524 ~DestroyedWindowObserver() override { Shutdown(); }
526 void SetWindow(Window* window) {
527 window_ = window;
528 window->AddObserver(this);
531 bool destroyed() const { return destroyed_; }
533 // WindowObserver overrides:
534 void OnWindowDestroying(Window* window) override {
535 destroyed_ = true;
536 Shutdown();
539 private:
540 void Shutdown() {
541 if (!window_)
542 return;
543 window_->RemoveObserver(this);
544 window_ = NULL;
547 bool destroyed_;
548 Window* window_;
550 DISALLOW_COPY_AND_ASSIGN(DestroyedWindowObserver);
553 // Verifies shutdown doesn't delete windows that are not owned by the parent.
554 TEST_F(RootWindowControllerTest, DontDeleteWindowsNotOwnedByParent) {
555 DestroyedWindowObserver observer1;
556 aura::test::TestWindowDelegate delegate1;
557 aura::Window* window1 = new aura::Window(&delegate1);
558 window1->SetType(ui::wm::WINDOW_TYPE_CONTROL);
559 window1->set_owned_by_parent(false);
560 observer1.SetWindow(window1);
561 window1->Init(aura::WINDOW_LAYER_NOT_DRAWN);
562 aura::client::ParentWindowWithContext(
563 window1, Shell::GetInstance()->GetPrimaryRootWindow(), gfx::Rect());
565 DestroyedWindowObserver observer2;
566 aura::Window* window2 = new aura::Window(NULL);
567 window2->set_owned_by_parent(false);
568 observer2.SetWindow(window2);
569 window2->Init(aura::WINDOW_LAYER_NOT_DRAWN);
570 Shell::GetInstance()->GetPrimaryRootWindow()->AddChild(window2);
572 Shell::GetInstance()->GetPrimaryRootWindowController()->CloseChildWindows();
574 ASSERT_FALSE(observer1.destroyed());
575 delete window1;
577 ASSERT_FALSE(observer2.destroyed());
578 delete window2;
581 typedef test::NoSessionAshTestBase NoSessionRootWindowControllerTest;
583 // Make sure that an event handler exists for entire display area.
584 TEST_F(NoSessionRootWindowControllerTest, Event) {
585 // Hide the shelf since it might otherwise get an event target.
586 RootWindowController* controller = Shell::GetPrimaryRootWindowController();
587 ShelfLayoutManager* shelf_layout_manager =
588 controller->GetShelfLayoutManager();
589 shelf_layout_manager->SetAutoHideBehavior(
590 ash::SHELF_AUTO_HIDE_ALWAYS_HIDDEN);
592 aura::Window* root = Shell::GetPrimaryRootWindow();
593 const gfx::Size size = root->bounds().size();
594 aura::Window* event_target = root->GetEventHandlerForPoint(gfx::Point(0, 0));
595 EXPECT_TRUE(event_target);
596 EXPECT_EQ(event_target,
597 root->GetEventHandlerForPoint(gfx::Point(0, size.height() - 1)));
598 EXPECT_EQ(event_target,
599 root->GetEventHandlerForPoint(gfx::Point(size.width() - 1, 0)));
600 EXPECT_EQ(event_target,
601 root->GetEventHandlerForPoint(gfx::Point(0, size.height() - 1)));
602 EXPECT_EQ(event_target,
603 root->GetEventHandlerForPoint(
604 gfx::Point(size.width() - 1, size.height() - 1)));
607 class VirtualKeyboardRootWindowControllerTest
608 : public RootWindowControllerTest {
609 public:
610 VirtualKeyboardRootWindowControllerTest() {}
611 ~VirtualKeyboardRootWindowControllerTest() override {}
613 void SetUp() override {
614 base::CommandLine::ForCurrentProcess()->AppendSwitch(
615 keyboard::switches::kEnableVirtualKeyboard);
616 test::AshTestBase::SetUp();
617 Shell::GetPrimaryRootWindowController()->ActivateKeyboard(
618 keyboard::KeyboardController::GetInstance());
621 private:
622 DISALLOW_COPY_AND_ASSIGN(VirtualKeyboardRootWindowControllerTest);
625 class MockTextInputClient : public ui::DummyTextInputClient {
626 public:
627 MockTextInputClient() :
628 ui::DummyTextInputClient(ui::TEXT_INPUT_TYPE_TEXT) {}
630 void EnsureCaretInRect(const gfx::Rect& rect) override {
631 visible_rect_ = rect;
634 const gfx::Rect& visible_rect() const {
635 return visible_rect_;
638 private:
639 gfx::Rect visible_rect_;
641 DISALLOW_COPY_AND_ASSIGN(MockTextInputClient);
644 class TargetHitTestEventHandler : public ui::test::TestEventHandler {
645 public:
646 TargetHitTestEventHandler() {}
648 // ui::test::TestEventHandler overrides.
649 void OnMouseEvent(ui::MouseEvent* event) override {
650 if (event->type() == ui::ET_MOUSE_PRESSED)
651 ui::test::TestEventHandler::OnMouseEvent(event);
652 event->StopPropagation();
655 private:
656 DISALLOW_COPY_AND_ASSIGN(TargetHitTestEventHandler);
659 // Test for http://crbug.com/297858. Virtual keyboard container should only show
660 // on primary root window.
661 TEST_F(VirtualKeyboardRootWindowControllerTest,
662 VirtualKeyboardOnPrimaryRootWindowOnly) {
663 if (!SupportsMultipleDisplays())
664 return;
666 UpdateDisplay("500x500,500x500");
668 aura::Window::Windows root_windows = Shell::GetAllRootWindows();
669 aura::Window* primary_root_window = Shell::GetPrimaryRootWindow();
670 aura::Window* secondary_root_window =
671 root_windows[0] == primary_root_window ?
672 root_windows[1] : root_windows[0];
674 ASSERT_TRUE(Shell::GetContainer(primary_root_window,
675 kShellWindowId_VirtualKeyboardContainer));
676 ASSERT_FALSE(Shell::GetContainer(secondary_root_window,
677 kShellWindowId_VirtualKeyboardContainer));
680 // Test for http://crbug.com/263599. Virtual keyboard should be able to receive
681 // events at blocked user session.
682 TEST_F(VirtualKeyboardRootWindowControllerTest,
683 ClickVirtualKeyboardInBlockedWindow) {
684 aura::Window* root_window = Shell::GetPrimaryRootWindow();
685 aura::Window* keyboard_container =
686 Shell::GetContainer(root_window, kShellWindowId_VirtualKeyboardContainer);
687 ASSERT_TRUE(keyboard_container);
688 keyboard_container->Show();
690 aura::Window* keyboard_window = keyboard::KeyboardController::GetInstance()->
691 proxy()->GetKeyboardWindow();
692 keyboard_container->AddChild(keyboard_window);
693 keyboard_window->set_owned_by_parent(false);
694 keyboard_window->SetBounds(gfx::Rect());
695 keyboard_window->Show();
697 ui::test::TestEventHandler handler;
698 root_window->AddPreTargetHandler(&handler);
700 ui::test::EventGenerator event_generator(root_window, keyboard_window);
701 event_generator.ClickLeftButton();
702 int expected_mouse_presses = 1;
703 EXPECT_EQ(expected_mouse_presses, handler.num_mouse_events() / 2);
705 for (int block_reason = FIRST_BLOCK_REASON;
706 block_reason < NUMBER_OF_BLOCK_REASONS;
707 ++block_reason) {
708 BlockUserSession(static_cast<UserSessionBlockReason>(block_reason));
709 event_generator.ClickLeftButton();
710 expected_mouse_presses++;
711 EXPECT_EQ(expected_mouse_presses, handler.num_mouse_events() / 2);
712 UnblockUserSession();
714 root_window->RemovePreTargetHandler(&handler);
717 // Test for http://crbug.com/299787. RootWindowController should delete
718 // the old container since the keyboard controller creates a new window in
719 // GetWindowContainer().
720 TEST_F(VirtualKeyboardRootWindowControllerTest,
721 DeleteOldContainerOnVirtualKeyboardInit) {
722 aura::Window* root_window = Shell::GetPrimaryRootWindow();
723 aura::Window* keyboard_container =
724 Shell::GetContainer(root_window, kShellWindowId_VirtualKeyboardContainer);
725 ASSERT_TRUE(keyboard_container);
726 // Track the keyboard container window.
727 aura::WindowTracker tracker;
728 tracker.Add(keyboard_container);
729 // Mock a login user profile change to reinitialize the keyboard.
730 ash::Shell::GetInstance()->OnLoginUserProfilePrepared();
731 // keyboard_container should no longer be present.
732 EXPECT_FALSE(tracker.Contains(keyboard_container));
735 // Test for crbug.com/342524. After user login, the work space should restore to
736 // full screen.
737 TEST_F(VirtualKeyboardRootWindowControllerTest, RestoreWorkspaceAfterLogin) {
738 aura::Window* root_window = Shell::GetPrimaryRootWindow();
739 aura::Window* keyboard_container =
740 Shell::GetContainer(root_window, kShellWindowId_VirtualKeyboardContainer);
741 keyboard_container->Show();
742 keyboard::KeyboardController* controller =
743 keyboard::KeyboardController::GetInstance();
744 aura::Window* keyboard_window = controller->proxy()->GetKeyboardWindow();
745 keyboard_container->AddChild(keyboard_window);
746 keyboard_window->set_owned_by_parent(false);
747 keyboard_window->SetBounds(keyboard::KeyboardBoundsFromWindowBounds(
748 keyboard_container->bounds(), 100));
749 keyboard_window->Show();
751 gfx::Rect before = ash::Shell::GetScreen()->GetPrimaryDisplay().work_area();
753 // Notify keyboard bounds changing.
754 controller->NotifyKeyboardBoundsChanging(
755 controller->proxy()->GetKeyboardWindow()->bounds());
757 if (!keyboard::IsKeyboardOverscrollEnabled()) {
758 gfx::Rect after = ash::Shell::GetScreen()->GetPrimaryDisplay().work_area();
759 EXPECT_LT(after, before);
762 // Mock a login user profile change to reinitialize the keyboard.
763 ash::Shell::GetInstance()->OnLoginUserProfilePrepared();
764 EXPECT_EQ(ash::Shell::GetScreen()->GetPrimaryDisplay().work_area(), before);
767 // Ensure that system modal dialogs do not block events targeted at the virtual
768 // keyboard.
769 TEST_F(VirtualKeyboardRootWindowControllerTest, ClickWithActiveModalDialog) {
770 aura::Window* root_window = Shell::GetPrimaryRootWindow();
771 aura::Window* keyboard_container =
772 Shell::GetContainer(root_window, kShellWindowId_VirtualKeyboardContainer);
773 ASSERT_TRUE(keyboard_container);
774 keyboard_container->Show();
776 aura::Window* keyboard_window = keyboard::KeyboardController::GetInstance()->
777 proxy()->GetKeyboardWindow();
778 keyboard_container->AddChild(keyboard_window);
779 keyboard_window->set_owned_by_parent(false);
780 keyboard_window->SetBounds(keyboard::KeyboardBoundsFromWindowBounds(
781 keyboard_container->bounds(), 100));
783 ui::test::TestEventHandler handler;
784 root_window->AddPreTargetHandler(&handler);
785 ui::test::EventGenerator root_window_event_generator(root_window);
786 ui::test::EventGenerator keyboard_event_generator(root_window,
787 keyboard_window);
789 views::Widget* modal_widget =
790 CreateModalWidget(gfx::Rect(300, 10, 100, 100));
792 // Verify that mouse events to the root window are block with a visble modal
793 // dialog.
794 root_window_event_generator.ClickLeftButton();
795 EXPECT_EQ(0, handler.num_mouse_events());
797 // Verify that event dispatch to the virtual keyboard is unblocked.
798 keyboard_event_generator.ClickLeftButton();
799 EXPECT_EQ(1, handler.num_mouse_events() / 2);
801 modal_widget->Close();
803 // Verify that mouse events are now unblocked to the root window.
804 root_window_event_generator.ClickLeftButton();
805 EXPECT_EQ(2, handler.num_mouse_events() / 2);
806 root_window->RemovePreTargetHandler(&handler);
809 // Ensure that the visible area for scrolling the text caret excludes the
810 // region occluded by the on-screen keyboard.
811 TEST_F(VirtualKeyboardRootWindowControllerTest, EnsureCaretInWorkArea) {
812 keyboard::KeyboardController* keyboard_controller =
813 keyboard::KeyboardController::GetInstance();
814 keyboard::KeyboardControllerProxy* proxy = keyboard_controller->proxy();
816 MockTextInputClient text_input_client;
817 ui::InputMethod* input_method = proxy->GetInputMethod();
818 ASSERT_TRUE(input_method);
819 if (switches::IsTextInputFocusManagerEnabled()) {
820 ui::TextInputFocusManager::GetInstance()->FocusTextInputClient(
821 &text_input_client);
822 } else {
823 input_method->SetFocusedTextInputClient(&text_input_client);
826 aura::Window* root_window = Shell::GetPrimaryRootWindow();
827 aura::Window* keyboard_container =
828 Shell::GetContainer(root_window, kShellWindowId_VirtualKeyboardContainer);
829 ASSERT_TRUE(keyboard_container);
830 keyboard_container->Show();
832 const int keyboard_height = 100;
833 aura::Window* keyboard_window =proxy->GetKeyboardWindow();
834 keyboard_container->AddChild(keyboard_window);
835 keyboard_window->set_owned_by_parent(false);
836 keyboard_window->SetBounds(keyboard::KeyboardBoundsFromWindowBounds(
837 keyboard_container->bounds(), keyboard_height));
839 proxy->EnsureCaretInWorkArea();
840 ASSERT_EQ(keyboard_container->bounds().width(),
841 text_input_client.visible_rect().width());
842 ASSERT_EQ(keyboard_container->bounds().height() - keyboard_height,
843 text_input_client.visible_rect().height());
845 if (switches::IsTextInputFocusManagerEnabled()) {
846 ui::TextInputFocusManager::GetInstance()->BlurTextInputClient(
847 &text_input_client);
848 } else {
849 input_method->SetFocusedTextInputClient(NULL);
853 // Tests that the virtual keyboard does not block context menus. The virtual
854 // keyboard should appear in front of most content, but not context menus. See
855 // crbug/377180.
856 TEST_F(VirtualKeyboardRootWindowControllerTest, ZOrderTest) {
857 UpdateDisplay("800x600");
858 keyboard::KeyboardController* keyboard_controller =
859 keyboard::KeyboardController::GetInstance();
860 keyboard::KeyboardControllerProxy* proxy = keyboard_controller->proxy();
862 aura::Window* root_window = Shell::GetPrimaryRootWindow();
863 aura::Window* keyboard_container =
864 Shell::GetContainer(root_window, kShellWindowId_VirtualKeyboardContainer);
865 ASSERT_TRUE(keyboard_container);
866 keyboard_container->Show();
868 const int keyboard_height = 200;
869 aura::Window* keyboard_window = proxy->GetKeyboardWindow();
870 keyboard_container->AddChild(keyboard_window);
871 keyboard_window->set_owned_by_parent(false);
872 gfx::Rect keyboard_bounds = keyboard::KeyboardBoundsFromWindowBounds(
873 keyboard_container->bounds(), keyboard_height);
874 keyboard_window->SetBounds(keyboard_bounds);
875 keyboard_window->Show();
877 ui::test::EventGenerator generator(root_window);
879 // Cover the screen with two windows: a normal window on the left side and a
880 // context menu on the right side. When the virtual keyboard is displayed it
881 // partially occludes the normal window, but not the context menu. Compute
882 // positions for generating synthetic click events to perform hit tests,
883 // ensuring the correct window layering. 'top' is above the VK, whereas
884 // 'bottom' lies within the VK. 'left' is centered in the normal window, and
885 // 'right' is centered in the context menu.
886 int window_height = keyboard_bounds.bottom();
887 int window_width = keyboard_bounds.width() / 2;
888 int left = window_width / 2;
889 int right = 3 * window_width / 2;
890 int top = keyboard_bounds.y() / 2;
891 int bottom = window_height - keyboard_height / 2;
893 // Normal window is partially occluded by the virtual keyboard.
894 aura::test::TestWindowDelegate delegate;
895 scoped_ptr<aura::Window> normal(CreateTestWindowInShellWithDelegateAndType(
896 &delegate,
897 ui::wm::WINDOW_TYPE_NORMAL,
899 gfx::Rect(0, 0, window_width, window_height)));
900 normal->set_owned_by_parent(false);
901 normal->Show();
902 TargetHitTestEventHandler normal_handler;
903 normal->AddPreTargetHandler(&normal_handler);
905 // Test that only the click on the top portion of the window is picked up. The
906 // click on the bottom hits the virtual keyboard instead.
907 generator.MoveMouseTo(left, top);
908 generator.ClickLeftButton();
909 EXPECT_EQ(1, normal_handler.num_mouse_events());
910 generator.MoveMouseTo(left, bottom);
911 generator.ClickLeftButton();
912 EXPECT_EQ(1, normal_handler.num_mouse_events());
914 // Menu overlaps virtual keyboard.
915 aura::test::TestWindowDelegate delegate2;
916 scoped_ptr<aura::Window> menu(CreateTestWindowInShellWithDelegateAndType(
917 &delegate2,
918 ui::wm::WINDOW_TYPE_MENU,
920 gfx::Rect(window_width, 0, window_width, window_height)));
921 menu->set_owned_by_parent(false);
922 menu->Show();
923 TargetHitTestEventHandler menu_handler;
924 menu->AddPreTargetHandler(&menu_handler);
926 // Test that both clicks register.
927 generator.MoveMouseTo(right, top);
928 generator.ClickLeftButton();
929 EXPECT_EQ(1, menu_handler.num_mouse_events());
930 generator.MoveMouseTo(right, bottom);
931 generator.ClickLeftButton();
932 EXPECT_EQ(2, menu_handler.num_mouse_events());
934 // Cleanup to ensure that the test windows are destroyed before their
935 // delegates.
936 normal.reset();
937 menu.reset();
940 // Resolution in UpdateDisplay is not being respected on Windows 8.
941 #if defined(OS_WIN)
942 #define MAYBE_DisplayRotation DISABLED_DisplayRotation
943 #else
944 #define MAYBE_DisplayRotation DisplayRotation
945 #endif
947 // Tests that the virtual keyboard correctly resizes with a change to display
948 // orientation. See crbug/417612.
949 TEST_F(VirtualKeyboardRootWindowControllerTest, MAYBE_DisplayRotation) {
950 UpdateDisplay("800x600");
951 aura::Window* root_window = Shell::GetPrimaryRootWindow();
952 aura::Window* keyboard_container =
953 Shell::GetContainer(root_window, kShellWindowId_VirtualKeyboardContainer);
954 ASSERT_TRUE(keyboard_container);
955 keyboard_container->Show();
956 EXPECT_EQ("0,0 800x600", keyboard_container->bounds().ToString());
958 UpdateDisplay("600x800");
959 EXPECT_EQ("0,0 600x800", keyboard_container->bounds().ToString());
962 } // namespace test
963 } // namespace ash