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/wm/workspace/workspace_layout_manager.h"
9 #include "ash/display/display_layout.h"
10 #include "ash/display/display_manager.h"
11 #include "ash/root_window_controller.h"
12 #include "ash/screen_util.h"
13 #include "ash/session/session_state_delegate.h"
14 #include "ash/shelf/shelf_layout_manager.h"
15 #include "ash/shell.h"
16 #include "ash/shell_observer.h"
17 #include "ash/shell_window_ids.h"
18 #include "ash/test/ash_test_base.h"
19 #include "ash/wm/maximize_mode/workspace_backdrop_delegate.h"
20 #include "ash/wm/window_state.h"
21 #include "ash/wm/window_util.h"
22 #include "ash/wm/wm_event.h"
23 #include "ash/wm/workspace/workspace_window_resizer.h"
24 #include "base/basictypes.h"
25 #include "base/compiler_specific.h"
26 #include "ui/aura/client/aura_constants.h"
27 #include "ui/aura/test/test_windows.h"
28 #include "ui/aura/window.h"
29 #include "ui/aura/window_event_dispatcher.h"
30 #include "ui/base/ime/dummy_text_input_client.h"
31 #include "ui/base/ime/input_method.h"
32 #include "ui/base/ime/text_input_focus_manager.h"
33 #include "ui/base/ui_base_switches_util.h"
34 #include "ui/base/ui_base_types.h"
35 #include "ui/gfx/insets.h"
36 #include "ui/gfx/screen.h"
37 #include "ui/views/widget/widget.h"
38 #include "ui/views/widget/widget_delegate.h"
39 #include "ui/wm/core/window_util.h"
44 class MaximizeDelegateView
: public views::WidgetDelegateView
{
46 explicit MaximizeDelegateView(const gfx::Rect
& initial_bounds
)
47 : initial_bounds_(initial_bounds
) {
49 virtual ~MaximizeDelegateView() {}
51 virtual bool GetSavedWindowPlacement(
52 const views::Widget
* widget
,
54 ui::WindowShowState
* show_state
) const OVERRIDE
{
55 *bounds
= initial_bounds_
;
56 *show_state
= ui::SHOW_STATE_MAXIMIZED
;
61 const gfx::Rect initial_bounds_
;
63 DISALLOW_COPY_AND_ASSIGN(MaximizeDelegateView
);
66 class TestShellObserver
: public ShellObserver
{
68 TestShellObserver() : call_count_(0),
69 is_fullscreen_(false) {
70 Shell::GetInstance()->AddShellObserver(this);
73 virtual ~TestShellObserver() {
74 Shell::GetInstance()->RemoveShellObserver(this);
77 virtual void OnFullscreenStateChanged(bool is_fullscreen
,
78 aura::Window
* root_window
) OVERRIDE
{
80 is_fullscreen_
= is_fullscreen
;
83 int call_count() const {
87 bool is_fullscreen() const {
88 return is_fullscreen_
;
95 DISALLOW_COPY_AND_ASSIGN(TestShellObserver
);
100 typedef test::AshTestBase WorkspaceLayoutManagerTest
;
102 // Verifies that a window containing a restore coordinate will be restored to
103 // to the size prior to minimize, keeping the restore rectangle in tact (if
105 TEST_F(WorkspaceLayoutManagerTest
, RestoreFromMinimizeKeepsRestore
) {
106 scoped_ptr
<aura::Window
> window(
107 CreateTestWindowInShellWithBounds(gfx::Rect(1, 2, 3, 4)));
108 gfx::Rect
bounds(10, 15, 25, 35);
109 window
->SetBounds(bounds
);
111 wm::WindowState
* window_state
= wm::GetWindowState(window
.get());
113 // This will not be used for un-minimizing window.
114 window_state
->SetRestoreBoundsInScreen(gfx::Rect(0, 0, 100, 100));
115 window_state
->Minimize();
116 window_state
->Restore();
117 EXPECT_EQ("0,0 100x100", window_state
->GetRestoreBoundsInScreen().ToString());
118 EXPECT_EQ("10,15 25x35", window
.get()->bounds().ToString());
120 if (!SupportsMultipleDisplays())
123 UpdateDisplay("400x300,500x400");
124 window
->SetBoundsInScreen(gfx::Rect(600, 0, 100, 100),
125 ScreenUtil::GetSecondaryDisplay());
126 EXPECT_EQ(Shell::GetAllRootWindows()[1], window
->GetRootWindow());
127 window_state
->Minimize();
128 // This will not be used for un-minimizing window.
129 window_state
->SetRestoreBoundsInScreen(gfx::Rect(0, 0, 100, 100));
130 window_state
->Restore();
131 EXPECT_EQ("600,0 100x100", window
->GetBoundsInScreen().ToString());
133 // Make sure the unminimized window moves inside the display when
134 // 2nd display is disconnected.
135 window_state
->Minimize();
136 UpdateDisplay("400x300");
137 window_state
->Restore();
138 EXPECT_EQ(Shell::GetPrimaryRootWindow(), window
->GetRootWindow());
140 Shell::GetPrimaryRootWindow()->bounds().Intersects(window
->bounds()));
143 TEST_F(WorkspaceLayoutManagerTest
, KeepMinimumVisibilityInDisplays
) {
144 if (!SupportsMultipleDisplays())
147 UpdateDisplay("300x400,400x500");
148 aura::Window::Windows root_windows
= Shell::GetAllRootWindows();
150 DisplayLayout
layout(DisplayLayout::TOP
, 0);
151 Shell::GetInstance()->display_manager()->
152 SetLayoutForCurrentDisplays(layout
);
153 EXPECT_EQ("0,-500 400x500", root_windows
[1]->GetBoundsInScreen().ToString());
155 scoped_ptr
<aura::Window
> window1(
156 CreateTestWindowInShellWithBounds(gfx::Rect(10, -400, 200, 200)));
157 EXPECT_EQ("10,-400 200x200", window1
->GetBoundsInScreen().ToString());
159 // Make sure the caption is visible.
160 scoped_ptr
<aura::Window
> window2(
161 CreateTestWindowInShellWithBounds(gfx::Rect(10, -600, 200, 200)));
162 EXPECT_EQ("10,-500 200x200", window2
->GetBoundsInScreen().ToString());
165 TEST_F(WorkspaceLayoutManagerTest
, NoMinimumVisibilityForPopupWindows
) {
166 UpdateDisplay("300x400");
168 // Create a popup window out of display boundaries and make sure it is not
169 // moved to have minimum visibility.
170 scoped_ptr
<aura::Window
> window(
171 CreateTestWindowInShellWithDelegateAndType(NULL
,
172 ui::wm::WINDOW_TYPE_POPUP
,
174 gfx::Rect(400, 100, 50, 50)));
175 EXPECT_EQ("400,100 50x50", window
->GetBoundsInScreen().ToString());
178 TEST_F(WorkspaceLayoutManagerTest
, KeepRestoredWindowInDisplay
) {
179 if (!SupportsHostWindowResize())
181 scoped_ptr
<aura::Window
> window(
182 CreateTestWindowInShellWithBounds(gfx::Rect(1, 2, 30, 40)));
183 wm::WindowState
* window_state
= wm::GetWindowState(window
.get());
185 // Maximized -> Normal transition.
186 window_state
->Maximize();
187 window_state
->SetRestoreBoundsInScreen(gfx::Rect(-100, -100, 30, 40));
188 window_state
->Restore();
190 Shell::GetPrimaryRootWindow()->bounds().Intersects(window
->bounds()));
191 // Y bounds should not be negative.
192 EXPECT_EQ("-20,0 30x40", window
->bounds().ToString());
194 // Minimized -> Normal transition.
195 window
->SetBounds(gfx::Rect(-100, -100, 30, 40));
196 window_state
->Minimize();
198 Shell::GetPrimaryRootWindow()->bounds().Intersects(window
->bounds()));
199 EXPECT_EQ("-100,-100 30x40", window
->bounds().ToString());
202 Shell::GetPrimaryRootWindow()->bounds().Intersects(window
->bounds()));
203 // Y bounds should not be negative.
204 EXPECT_EQ("-20,0 30x40", window
->bounds().ToString());
206 // Fullscreen -> Normal transition.
207 window
->SetBounds(gfx::Rect(0, 0, 30, 40)); // reset bounds.
208 ASSERT_EQ("0,0 30x40", window
->bounds().ToString());
209 window
->SetProperty(aura::client::kShowStateKey
, ui::SHOW_STATE_FULLSCREEN
);
210 EXPECT_EQ(window
->bounds(), window
->GetRootWindow()->bounds());
211 window_state
->SetRestoreBoundsInScreen(gfx::Rect(-100, -100, 30, 40));
212 window_state
->Restore();
214 Shell::GetPrimaryRootWindow()->bounds().Intersects(window
->bounds()));
215 // Y bounds should not be negative.
216 EXPECT_EQ("-20,0 30x40", window
->bounds().ToString());
219 TEST_F(WorkspaceLayoutManagerTest
, MaximizeInDisplayToBeRestored
) {
220 if (!SupportsMultipleDisplays())
222 UpdateDisplay("300x400,400x500");
224 aura::Window::Windows root_windows
= Shell::GetAllRootWindows();
226 scoped_ptr
<aura::Window
> window(
227 CreateTestWindowInShellWithBounds(gfx::Rect(1, 2, 30, 40)));
228 EXPECT_EQ(root_windows
[0], window
->GetRootWindow());
230 wm::WindowState
* window_state
= wm::GetWindowState(window
.get());
231 window_state
->SetRestoreBoundsInScreen(gfx::Rect(400, 0, 30, 40));
232 // Maximize the window in 2nd display as the restore bounds
233 // is inside 2nd display.
234 window_state
->Maximize();
235 EXPECT_EQ(root_windows
[1], window
->GetRootWindow());
236 EXPECT_EQ("300,0 400x453", window
->GetBoundsInScreen().ToString());
238 window_state
->Restore();
239 EXPECT_EQ(root_windows
[1], window
->GetRootWindow());
240 EXPECT_EQ("400,0 30x40", window
->GetBoundsInScreen().ToString());
242 // If the restore bounds intersects with the current display,
244 window_state
->SetRestoreBoundsInScreen(gfx::Rect(280, 0, 30, 40));
245 window_state
->Maximize();
246 EXPECT_EQ(root_windows
[1], window
->GetRootWindow());
247 EXPECT_EQ("300,0 400x453", window
->GetBoundsInScreen().ToString());
249 window_state
->Restore();
250 EXPECT_EQ(root_windows
[1], window
->GetRootWindow());
251 EXPECT_EQ("280,0 30x40", window
->GetBoundsInScreen().ToString());
253 // Restoring widget state.
254 scoped_ptr
<views::Widget
> w1(new views::Widget
);
255 views::Widget::InitParams params
;
256 params
.ownership
= views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET
;
257 params
.delegate
= new MaximizeDelegateView(gfx::Rect(400, 0, 30, 40));
258 params
.context
= root_windows
[0];
261 EXPECT_TRUE(w1
->IsMaximized());
262 EXPECT_EQ(root_windows
[1], w1
->GetNativeView()->GetRootWindow());
263 EXPECT_EQ("300,0 400x453", w1
->GetWindowBoundsInScreen().ToString());
265 EXPECT_EQ(root_windows
[1], w1
->GetNativeView()->GetRootWindow());
266 EXPECT_EQ("400,0 30x40", w1
->GetWindowBoundsInScreen().ToString());
269 TEST_F(WorkspaceLayoutManagerTest
, FullscreenInDisplayToBeRestored
) {
270 if (!SupportsMultipleDisplays())
272 UpdateDisplay("300x400,400x500");
274 aura::Window::Windows root_windows
= Shell::GetAllRootWindows();
276 scoped_ptr
<aura::Window
> window(
277 CreateTestWindowInShellWithBounds(gfx::Rect(1, 2, 30, 40)));
278 EXPECT_EQ(root_windows
[0], window
->GetRootWindow());
280 wm::WindowState
* window_state
= wm::GetWindowState(window
.get());
281 window_state
->SetRestoreBoundsInScreen(gfx::Rect(400, 0, 30, 40));
282 // Maximize the window in 2nd display as the restore bounds
283 // is inside 2nd display.
284 window
->SetProperty(aura::client::kShowStateKey
,
285 ui::SHOW_STATE_FULLSCREEN
);
286 EXPECT_EQ(root_windows
[1], window
->GetRootWindow());
287 EXPECT_EQ("300,0 400x500", window
->GetBoundsInScreen().ToString());
289 window_state
->Restore();
290 EXPECT_EQ(root_windows
[1], window
->GetRootWindow());
291 EXPECT_EQ("400,0 30x40", window
->GetBoundsInScreen().ToString());
293 // If the restore bounds intersects with the current display,
295 window_state
->SetRestoreBoundsInScreen(gfx::Rect(280, 0, 30, 40));
296 window
->SetProperty(aura::client::kShowStateKey
,
297 ui::SHOW_STATE_FULLSCREEN
);
298 EXPECT_EQ(root_windows
[1], window
->GetRootWindow());
299 EXPECT_EQ("300,0 400x500", window
->GetBoundsInScreen().ToString());
301 window_state
->Restore();
302 EXPECT_EQ(root_windows
[1], window
->GetRootWindow());
303 EXPECT_EQ("280,0 30x40", window
->GetBoundsInScreen().ToString());
306 // WindowObserver implementation used by DontClobberRestoreBoundsWindowObserver.
307 // This code mirrors what BrowserFrameAsh does. In particular when this code
308 // sees the window was maximized it changes the bounds of a secondary
309 // window. The secondary window mirrors the status window.
310 class DontClobberRestoreBoundsWindowObserver
: public aura::WindowObserver
{
312 DontClobberRestoreBoundsWindowObserver() : window_(NULL
) {}
314 void set_window(aura::Window
* window
) { window_
= window
; }
316 virtual void OnWindowPropertyChanged(aura::Window
* window
,
318 intptr_t old
) OVERRIDE
{
322 if (wm::GetWindowState(window
)->IsMaximized()) {
323 aura::Window
* w
= window_
;
326 gfx::Rect
shelf_bounds(Shell::GetPrimaryRootWindowController()->
327 GetShelfLayoutManager()->GetIdealBounds());
328 const gfx::Rect
& window_bounds(w
->bounds());
329 w
->SetBounds(gfx::Rect(window_bounds
.x(), shelf_bounds
.y() - 1,
330 window_bounds
.width(), window_bounds
.height()));
335 aura::Window
* window_
;
337 DISALLOW_COPY_AND_ASSIGN(DontClobberRestoreBoundsWindowObserver
);
340 // Creates a window, maximized the window and from within the maximized
341 // notification sets the bounds of a window to overlap the shelf. Verifies this
342 // doesn't effect the restore bounds.
343 TEST_F(WorkspaceLayoutManagerTest
, DontClobberRestoreBounds
) {
344 DontClobberRestoreBoundsWindowObserver window_observer
;
345 scoped_ptr
<aura::Window
> window(new aura::Window(NULL
));
346 window
->SetType(ui::wm::WINDOW_TYPE_NORMAL
);
347 window
->Init(aura::WINDOW_LAYER_TEXTURED
);
348 window
->SetBounds(gfx::Rect(10, 20, 30, 40));
349 // NOTE: for this test to exercise the failure the observer needs to be added
350 // before the parent set. This mimics what BrowserFrameAsh does.
351 window
->AddObserver(&window_observer
);
352 ParentWindowInPrimaryRootWindow(window
.get());
355 wm::WindowState
* window_state
= wm::GetWindowState(window
.get());
356 window_state
->Activate();
358 scoped_ptr
<aura::Window
> window2(
359 CreateTestWindowInShellWithBounds(gfx::Rect(12, 20, 30, 40)));
360 ::wm::AddTransientChild(window
.get(), window2
.get());
363 window_observer
.set_window(window2
.get());
364 window_state
->Maximize();
365 EXPECT_EQ("10,20 30x40",
366 window_state
->GetRestoreBoundsInScreen().ToString());
367 window
->RemoveObserver(&window_observer
);
370 // Verifies when a window is maximized all descendant windows have a size.
371 TEST_F(WorkspaceLayoutManagerTest
, ChildBoundsResetOnMaximize
) {
372 scoped_ptr
<aura::Window
> window(
373 CreateTestWindowInShellWithBounds(gfx::Rect(10, 20, 30, 40)));
375 wm::WindowState
* window_state
= wm::GetWindowState(window
.get());
376 window_state
->Activate();
377 scoped_ptr
<aura::Window
> child_window(
378 aura::test::CreateTestWindowWithBounds(gfx::Rect(5, 6, 7, 8),
380 child_window
->Show();
381 window_state
->Maximize();
382 EXPECT_EQ("5,6 7x8", child_window
->bounds().ToString());
385 // Verifies a window created with maximized state has the maximized
387 TEST_F(WorkspaceLayoutManagerTest
, MaximizeWithEmptySize
) {
388 scoped_ptr
<aura::Window
> window(
389 aura::test::CreateTestWindowWithBounds(gfx::Rect(0, 0, 0, 0),
391 wm::GetWindowState(window
.get())->Maximize();
392 aura::Window
* default_container
= Shell::GetContainer(
393 Shell::GetPrimaryRootWindow(), kShellWindowId_DefaultContainer
);
394 default_container
->AddChild(window
.get());
397 Shell::GetScreen()->GetPrimaryDisplay().work_area());
398 EXPECT_EQ(work_area
.ToString(), window
->GetBoundsInScreen().ToString());
401 TEST_F(WorkspaceLayoutManagerTest
, WindowShouldBeOnScreenWhenAdded
) {
402 // Normal window bounds shouldn't be changed.
403 gfx::Rect
window_bounds(100, 100, 200, 200);
404 scoped_ptr
<aura::Window
> window(
405 CreateTestWindowInShellWithBounds(window_bounds
));
406 EXPECT_EQ(window_bounds
, window
->bounds());
408 // If the window is out of the workspace, it would be moved on screen.
409 gfx::Rect root_window_bounds
=
410 Shell::GetInstance()->GetPrimaryRootWindow()->bounds();
411 window_bounds
.Offset(root_window_bounds
.width(), root_window_bounds
.height());
412 ASSERT_FALSE(window_bounds
.Intersects(root_window_bounds
));
413 scoped_ptr
<aura::Window
> out_window(
414 CreateTestWindowInShellWithBounds(window_bounds
));
415 EXPECT_EQ(window_bounds
.size(), out_window
->bounds().size());
416 gfx::Rect bounds
= out_window
->bounds();
417 bounds
.Intersect(root_window_bounds
);
419 // 30% of the window edge must be visible.
420 EXPECT_GT(bounds
.width(), out_window
->bounds().width() * 0.29);
421 EXPECT_GT(bounds
.height(), out_window
->bounds().height() * 0.29);
423 aura::Window
* parent
= out_window
->parent();
424 parent
->RemoveChild(out_window
.get());
425 out_window
->SetBounds(gfx::Rect(-200, -200, 200, 200));
426 // UserHasChangedWindowPositionOrSize flag shouldn't turn off this behavior.
427 wm::GetWindowState(window
.get())->set_bounds_changed_by_user(true);
428 parent
->AddChild(out_window
.get());
429 EXPECT_GT(bounds
.width(), out_window
->bounds().width() * 0.29);
430 EXPECT_GT(bounds
.height(), out_window
->bounds().height() * 0.29);
432 // Make sure we always make more than 1/3 of the window edge visible even
433 // if the initial bounds intersects with display.
434 window_bounds
.SetRect(-150, -150, 200, 200);
435 bounds
= window_bounds
;
436 bounds
.Intersect(root_window_bounds
);
438 // Make sure that the initial bounds' visible area is less than 26%
439 // so that the auto adjustment logic kicks in.
440 ASSERT_LT(bounds
.width(), out_window
->bounds().width() * 0.26);
441 ASSERT_LT(bounds
.height(), out_window
->bounds().height() * 0.26);
442 ASSERT_TRUE(window_bounds
.Intersects(root_window_bounds
));
444 scoped_ptr
<aura::Window
> partially_out_window(
445 CreateTestWindowInShellWithBounds(window_bounds
));
446 EXPECT_EQ(window_bounds
.size(), partially_out_window
->bounds().size());
447 bounds
= partially_out_window
->bounds();
448 bounds
.Intersect(root_window_bounds
);
449 EXPECT_GT(bounds
.width(), out_window
->bounds().width() * 0.29);
450 EXPECT_GT(bounds
.height(), out_window
->bounds().height() * 0.29);
452 // Make sure the window whose 30% width/height is bigger than display
453 // will be placed correctly.
454 window_bounds
.SetRect(-1900, -1900, 3000, 3000);
455 scoped_ptr
<aura::Window
> window_bigger_than_display(
456 CreateTestWindowInShellWithBounds(window_bounds
));
457 EXPECT_GE(root_window_bounds
.width(),
458 window_bigger_than_display
->bounds().width());
459 EXPECT_GE(root_window_bounds
.height(),
460 window_bigger_than_display
->bounds().height());
462 bounds
= window_bigger_than_display
->bounds();
463 bounds
.Intersect(root_window_bounds
);
464 EXPECT_GT(bounds
.width(), out_window
->bounds().width() * 0.29);
465 EXPECT_GT(bounds
.height(), out_window
->bounds().height() * 0.29);
468 // Verifies the size of a window is enforced to be smaller than the work area.
469 TEST_F(WorkspaceLayoutManagerTest
, SizeToWorkArea
) {
470 // Normal window bounds shouldn't be changed.
472 Shell::GetScreen()->GetPrimaryDisplay().work_area().size());
473 const gfx::Rect
window_bounds(
474 100, 101, work_area
.width() + 1, work_area
.height() + 2);
475 scoped_ptr
<aura::Window
> window(
476 CreateTestWindowInShellWithBounds(window_bounds
));
477 EXPECT_EQ(gfx::Rect(gfx::Point(100, 101), work_area
).ToString(),
478 window
->bounds().ToString());
480 // Directly setting the bounds triggers a slightly different code path. Verify
482 window
->SetBounds(window_bounds
);
483 EXPECT_EQ(gfx::Rect(gfx::Point(100, 101), work_area
).ToString(),
484 window
->bounds().ToString());
487 TEST_F(WorkspaceLayoutManagerTest
, NotifyFullscreenChanges
) {
488 TestShellObserver observer
;
489 scoped_ptr
<aura::Window
> window1(
490 CreateTestWindowInShellWithBounds(gfx::Rect(1, 2, 30, 40)));
491 scoped_ptr
<aura::Window
> window2(
492 CreateTestWindowInShellWithBounds(gfx::Rect(1, 2, 30, 40)));
493 wm::WindowState
* window_state1
= wm::GetWindowState(window1
.get());
494 wm::WindowState
* window_state2
= wm::GetWindowState(window2
.get());
495 window_state2
->Activate();
497 const wm::WMEvent
toggle_fullscreen_event(wm::WM_EVENT_TOGGLE_FULLSCREEN
);
498 window_state2
->OnWMEvent(&toggle_fullscreen_event
);
499 EXPECT_EQ(1, observer
.call_count());
500 EXPECT_TRUE(observer
.is_fullscreen());
502 // When window1 moves to the front the fullscreen state should change.
503 window_state1
->Activate();
504 EXPECT_EQ(2, observer
.call_count());
505 EXPECT_FALSE(observer
.is_fullscreen());
507 // It should change back if window2 becomes active again.
508 window_state2
->Activate();
509 EXPECT_EQ(3, observer
.call_count());
510 EXPECT_TRUE(observer
.is_fullscreen());
512 window_state2
->OnWMEvent(&toggle_fullscreen_event
);
513 EXPECT_EQ(4, observer
.call_count());
514 EXPECT_FALSE(observer
.is_fullscreen());
516 window_state2
->OnWMEvent(&toggle_fullscreen_event
);
517 EXPECT_EQ(5, observer
.call_count());
518 EXPECT_TRUE(observer
.is_fullscreen());
520 // Closing the window should change the fullscreen state.
522 EXPECT_EQ(6, observer
.call_count());
523 EXPECT_FALSE(observer
.is_fullscreen());
526 // Following tests were originally written for BaseLayoutManager.
530 class WorkspaceLayoutManagerSoloTest
: public test::AshTestBase
{
532 WorkspaceLayoutManagerSoloTest() {}
533 virtual ~WorkspaceLayoutManagerSoloTest() {}
535 virtual void SetUp() OVERRIDE
{
536 test::AshTestBase::SetUp();
537 UpdateDisplay("800x600");
538 aura::Window
* default_container
= Shell::GetContainer(
539 Shell::GetPrimaryRootWindow(), kShellWindowId_DefaultContainer
);
540 default_container
->SetLayoutManager(
541 new WorkspaceLayoutManager(Shell::GetPrimaryRootWindow()));
544 aura::Window
* CreateTestWindow(const gfx::Rect
& bounds
) {
545 return CreateTestWindowInShellWithBounds(bounds
);
549 DISALLOW_COPY_AND_ASSIGN(WorkspaceLayoutManagerSoloTest
);
554 // Tests normal->maximize->normal.
555 TEST_F(WorkspaceLayoutManagerSoloTest
, Maximize
) {
556 gfx::Rect
bounds(100, 100, 200, 200);
557 scoped_ptr
<aura::Window
> window(CreateTestWindow(bounds
));
558 window
->SetProperty(aura::client::kShowStateKey
, ui::SHOW_STATE_MAXIMIZED
);
559 // Maximized window fills the work area, not the whole display.
561 ScreenUtil::GetMaximizedWindowBoundsInParent(window
.get()).ToString(),
562 window
->bounds().ToString());
563 window
->SetProperty(aura::client::kShowStateKey
, ui::SHOW_STATE_NORMAL
);
564 EXPECT_EQ(bounds
.ToString(), window
->bounds().ToString());
567 // Tests normal->minimize->normal.
568 TEST_F(WorkspaceLayoutManagerSoloTest
, Minimize
) {
569 gfx::Rect
bounds(100, 100, 200, 200);
570 scoped_ptr
<aura::Window
> window(CreateTestWindow(bounds
));
571 window
->SetProperty(aura::client::kShowStateKey
, ui::SHOW_STATE_MINIMIZED
);
572 // Note: Currently minimize doesn't do anything except set the state.
573 // See crbug.com/104571.
574 EXPECT_EQ(bounds
.ToString(), window
->bounds().ToString());
575 window
->SetProperty(aura::client::kShowStateKey
, ui::SHOW_STATE_NORMAL
);
576 EXPECT_EQ(bounds
.ToString(), window
->bounds().ToString());
579 // A WindowDelegate which sets the focus when the window
581 class FocusDelegate
: public aura::test::TestWindowDelegate
{
585 show_state_(ui::SHOW_STATE_END
) {
587 virtual ~FocusDelegate() {}
589 void set_window(aura::Window
* window
) { window_
= window
; }
591 // aura::test::TestWindowDelegate overrides:
592 virtual void OnWindowTargetVisibilityChanged(bool visible
) OVERRIDE
{
596 show_state_
= window_
->GetProperty(aura::client::kShowStateKey
);
600 ui::WindowShowState
GetShowStateAndReset() {
601 ui::WindowShowState ret
= show_state_
;
602 show_state_
= ui::SHOW_STATE_END
;
607 aura::Window
* window_
;
608 ui::WindowShowState show_state_
;
610 DISALLOW_COPY_AND_ASSIGN(FocusDelegate
);
613 // Make sure that the window's show state is correct in
614 // |WindowDelegate::OnWindowTargetVisibilityChanged|, and setting
615 // focus in this callback doesn't cause DCHECK error. See
617 TEST_F(WorkspaceLayoutManagerSoloTest
, FocusDuringUnminimize
) {
618 FocusDelegate delegate
;
619 scoped_ptr
<aura::Window
> window(CreateTestWindowInShellWithDelegate(
620 &delegate
, 0, gfx::Rect(100, 100, 100, 100)));
621 delegate
.set_window(window
.get());
622 window
->SetProperty(aura::client::kShowStateKey
, ui::SHOW_STATE_MINIMIZED
);
623 EXPECT_FALSE(window
->IsVisible());
624 EXPECT_EQ(ui::SHOW_STATE_MINIMIZED
, delegate
.GetShowStateAndReset());
626 EXPECT_TRUE(window
->IsVisible());
627 EXPECT_EQ(ui::SHOW_STATE_NORMAL
, delegate
.GetShowStateAndReset());
630 // Tests maximized window size during root window resize.
631 TEST_F(WorkspaceLayoutManagerSoloTest
, MaximizeRootWindowResize
) {
632 gfx::Rect
bounds(100, 100, 200, 200);
633 scoped_ptr
<aura::Window
> window(CreateTestWindow(bounds
));
634 window
->SetProperty(aura::client::kShowStateKey
, ui::SHOW_STATE_MAXIMIZED
);
635 gfx::Rect initial_work_area_bounds
=
636 ScreenUtil::GetMaximizedWindowBoundsInParent(window
.get());
637 EXPECT_EQ(initial_work_area_bounds
.ToString(), window
->bounds().ToString());
638 // Enlarge the root window. We should still match the work area size.
639 UpdateDisplay("900x700");
641 ScreenUtil::GetMaximizedWindowBoundsInParent(window
.get()).ToString(),
642 window
->bounds().ToString());
644 initial_work_area_bounds
.ToString(),
645 ScreenUtil::GetMaximizedWindowBoundsInParent(window
.get()).ToString());
648 // Tests normal->fullscreen->normal.
649 TEST_F(WorkspaceLayoutManagerSoloTest
, Fullscreen
) {
650 gfx::Rect
bounds(100, 100, 200, 200);
651 scoped_ptr
<aura::Window
> window(CreateTestWindow(bounds
));
652 window
->SetProperty(aura::client::kShowStateKey
, ui::SHOW_STATE_FULLSCREEN
);
653 // Fullscreen window fills the whole display.
654 EXPECT_EQ(Shell::GetScreen()->GetDisplayNearestWindow(
655 window
.get()).bounds().ToString(),
656 window
->bounds().ToString());
657 window
->SetProperty(aura::client::kShowStateKey
, ui::SHOW_STATE_NORMAL
);
658 EXPECT_EQ(bounds
.ToString(), window
->bounds().ToString());
661 // Tests fullscreen window size during root window resize.
662 TEST_F(WorkspaceLayoutManagerSoloTest
, FullscreenRootWindowResize
) {
663 gfx::Rect
bounds(100, 100, 200, 200);
664 scoped_ptr
<aura::Window
> window(CreateTestWindow(bounds
));
665 // Fullscreen window fills the whole display.
666 window
->SetProperty(aura::client::kShowStateKey
, ui::SHOW_STATE_FULLSCREEN
);
667 EXPECT_EQ(Shell::GetScreen()->GetDisplayNearestWindow(
668 window
.get()).bounds().ToString(),
669 window
->bounds().ToString());
670 // Enlarge the root window. We should still match the display size.
671 UpdateDisplay("800x600");
672 EXPECT_EQ(Shell::GetScreen()->GetDisplayNearestWindow(
673 window
.get()).bounds().ToString(),
674 window
->bounds().ToString());
677 // Tests that when the screen gets smaller the windows aren't bigger than
679 TEST_F(WorkspaceLayoutManagerSoloTest
, RootWindowResizeShrinksWindows
) {
680 scoped_ptr
<aura::Window
> window(
681 CreateTestWindow(gfx::Rect(10, 20, 500, 400)));
682 gfx::Rect work_area
= Shell::GetScreen()->GetDisplayNearestWindow(
683 window
.get()).work_area();
684 // Invariant: Window is smaller than work area.
685 EXPECT_LE(window
->bounds().width(), work_area
.width());
686 EXPECT_LE(window
->bounds().height(), work_area
.height());
688 // Make the root window narrower than our window.
689 UpdateDisplay("300x400");
690 work_area
= Shell::GetScreen()->GetDisplayNearestWindow(
691 window
.get()).work_area();
692 EXPECT_LE(window
->bounds().width(), work_area
.width());
693 EXPECT_LE(window
->bounds().height(), work_area
.height());
695 // Make the root window shorter than our window.
696 UpdateDisplay("300x200");
697 work_area
= Shell::GetScreen()->GetDisplayNearestWindow(
698 window
.get()).work_area();
699 EXPECT_LE(window
->bounds().width(), work_area
.width());
700 EXPECT_LE(window
->bounds().height(), work_area
.height());
702 // Enlarging the root window does not change the window bounds.
703 gfx::Rect old_bounds
= window
->bounds();
704 UpdateDisplay("800x600");
705 EXPECT_EQ(old_bounds
.width(), window
->bounds().width());
706 EXPECT_EQ(old_bounds
.height(), window
->bounds().height());
709 // Verifies maximizing sets the restore bounds, and restoring
710 // restores the bounds.
711 TEST_F(WorkspaceLayoutManagerSoloTest
, MaximizeSetsRestoreBounds
) {
712 scoped_ptr
<aura::Window
> window(CreateTestWindow(gfx::Rect(10, 20, 30, 40)));
713 wm::WindowState
* window_state
= wm::GetWindowState(window
.get());
715 // Maximize it, which will keep the previous restore bounds.
716 window
->SetProperty(aura::client::kShowStateKey
, ui::SHOW_STATE_MAXIMIZED
);
717 EXPECT_EQ("10,20 30x40", window_state
->GetRestoreBoundsInParent().ToString());
719 // Restore it, which should restore bounds and reset restore bounds.
720 window
->SetProperty(aura::client::kShowStateKey
, ui::SHOW_STATE_NORMAL
);
721 EXPECT_EQ("10,20 30x40", window
->bounds().ToString());
722 EXPECT_FALSE(window_state
->HasRestoreBounds());
725 // Verifies maximizing keeps the restore bounds if set.
726 TEST_F(WorkspaceLayoutManagerSoloTest
, MaximizeResetsRestoreBounds
) {
727 scoped_ptr
<aura::Window
> window(CreateTestWindow(gfx::Rect(1, 2, 3, 4)));
729 wm::WindowState
* window_state
= wm::GetWindowState(window
.get());
730 window_state
->SetRestoreBoundsInParent(gfx::Rect(10, 11, 12, 13));
732 // Maximize it, which will keep the previous restore bounds.
733 window
->SetProperty(aura::client::kShowStateKey
, ui::SHOW_STATE_MAXIMIZED
);
734 EXPECT_EQ("10,11 12x13", window_state
->GetRestoreBoundsInParent().ToString());
737 // Verifies that the restore bounds do not get reset when restoring to a
738 // maximzied state from a minimized state.
739 TEST_F(WorkspaceLayoutManagerSoloTest
,
740 BoundsAfterRestoringToMaximizeFromMinimize
) {
741 scoped_ptr
<aura::Window
> window(CreateTestWindow(gfx::Rect(1, 2, 3, 4)));
742 gfx::Rect
bounds(10, 15, 25, 35);
743 window
->SetBounds(bounds
);
745 wm::WindowState
* window_state
= wm::GetWindowState(window
.get());
746 // Maximize it, which should reset restore bounds.
747 window_state
->Maximize();
748 EXPECT_EQ(bounds
.ToString(),
749 window_state
->GetRestoreBoundsInParent().ToString());
750 // Minimize the window. The restore bounds should not change.
751 window_state
->Minimize();
752 EXPECT_EQ(bounds
.ToString(),
753 window_state
->GetRestoreBoundsInParent().ToString());
755 // Show the window again. The window should be maximized, and the restore
756 // bounds should not change.
758 EXPECT_EQ(bounds
.ToString(),
759 window_state
->GetRestoreBoundsInParent().ToString());
760 EXPECT_TRUE(window_state
->IsMaximized());
762 window_state
->Restore();
763 EXPECT_EQ(bounds
.ToString(), window
->bounds().ToString());
766 // Verify if the window is not resized during screen lock. See: crbug.com/173127
767 TEST_F(WorkspaceLayoutManagerSoloTest
, NotResizeWhenScreenIsLocked
) {
768 SetCanLockScreen(true);
769 scoped_ptr
<aura::Window
> window(CreateTestWindow(gfx::Rect(1, 2, 3, 4)));
770 // window with AlwaysOnTop will be managed by BaseLayoutManager.
771 window
->SetProperty(aura::client::kAlwaysOnTopKey
, true);
774 ShelfLayoutManager
* shelf
= ShelfLayoutManager::ForShelf(window
.get());
775 shelf
->SetAutoHideBehavior(SHELF_AUTO_HIDE_BEHAVIOR_ALWAYS
);
777 window
->SetBounds(ScreenUtil::GetMaximizedWindowBoundsInParent(window
.get()));
778 gfx::Rect window_bounds
= window
->bounds();
780 ScreenUtil::GetMaximizedWindowBoundsInParent(window
.get()).ToString(),
781 window_bounds
.ToString());
783 Shell::GetInstance()->session_state_delegate()->LockScreen();
784 shelf
->UpdateVisibilityState();
786 ScreenUtil::GetMaximizedWindowBoundsInParent(window
.get()).ToString(),
787 window_bounds
.ToString());
789 Shell::GetInstance()->session_state_delegate()->UnlockScreen();
790 shelf
->UpdateVisibilityState();
791 EXPECT_EQ(window_bounds
.ToString(), window
->bounds().ToString());
794 // Following tests are written to test the backdrop functionality.
798 class WorkspaceLayoutManagerBackdropTest
: public test::AshTestBase
{
800 WorkspaceLayoutManagerBackdropTest() {}
801 virtual ~WorkspaceLayoutManagerBackdropTest() {}
803 virtual void SetUp() OVERRIDE
{
804 test::AshTestBase::SetUp();
805 UpdateDisplay("800x600");
806 default_container_
= Shell::GetContainer(Shell::GetPrimaryRootWindow(),
807 kShellWindowId_DefaultContainer
);
810 aura::Window
* CreateTestWindow(const gfx::Rect
& bounds
) {
811 aura::Window
* window
= CreateTestWindowInShellWithBounds(bounds
);
815 // Turn the top window back drop on / off.
816 void ShowTopWindowBackdrop(bool show
) {
817 scoped_ptr
<ash::WorkspaceLayoutManagerDelegate
> backdrop
;
819 backdrop
.reset(new ash::WorkspaceBackdropDelegate(default_container_
));
821 (static_cast<WorkspaceLayoutManager
*>(default_container_
->layout_manager()))
822 ->SetMaximizeBackdropDelegate(backdrop
.Pass());
823 // Closing and / or opening can be a delayed operation.
824 base::MessageLoop::current()->RunUntilIdle();
827 // Return the default container.
828 aura::Window
* default_container() { return default_container_
; }
830 // Return the order of windows (top most first) as they are in the default
831 // container. If the window is visible it will be a big letter, otherwise a
832 // small one. The backdrop will be an X and unknown windows will be shown as
834 std::string
GetWindowOrderAsString(aura::Window
* backdrop
,
839 for (int i
= static_cast<int>(default_container()->children().size()) - 1;
844 if (default_container()->children()[i
] == wa
)
845 result
+= default_container()->children()[i
]->IsVisible() ? "A" : "a";
846 else if (default_container()->children()[i
] == wb
)
847 result
+= default_container()->children()[i
]->IsVisible() ? "B" : "b";
848 else if (default_container()->children()[i
] == wc
)
849 result
+= default_container()->children()[i
]->IsVisible() ? "C" : "c";
850 else if (default_container()->children()[i
] == backdrop
)
851 result
+= default_container()->children()[i
]->IsVisible() ? "X" : "x";
859 // The default container.
860 aura::Window
* default_container_
;
862 DISALLOW_COPY_AND_ASSIGN(WorkspaceLayoutManagerBackdropTest
);
867 // Check that creating the BackDrop without destroying it does not lead into
869 TEST_F(WorkspaceLayoutManagerBackdropTest
, BackdropCrashTest
) {
870 ShowTopWindowBackdrop(true);
873 // Verify basic assumptions about the backdrop.
874 TEST_F(WorkspaceLayoutManagerBackdropTest
, BasicBackdropTests
) {
875 // Create a backdrop and see that there is one window (the backdrop) and
876 // that the size is the same as the default container as well as that it is
878 ShowTopWindowBackdrop(true);
879 ASSERT_EQ(1U, default_container()->children().size());
880 EXPECT_FALSE(default_container()->children()[0]->IsVisible());
883 // Add a window and make sure that the backdrop is the second child.
884 scoped_ptr
<aura::Window
> window(CreateTestWindow(gfx::Rect(1, 2, 3, 4)));
886 ASSERT_EQ(2U, default_container()->children().size());
887 EXPECT_TRUE(default_container()->children()[0]->IsVisible());
888 EXPECT_TRUE(default_container()->children()[1]->IsVisible());
889 EXPECT_EQ(window
.get(), default_container()->children()[1]);
890 EXPECT_EQ(default_container()->bounds().ToString(),
891 default_container()->children()[0]->bounds().ToString());
894 // With the window gone the backdrop should be invisible again.
895 ASSERT_EQ(1U, default_container()->children().size());
896 EXPECT_FALSE(default_container()->children()[0]->IsVisible());
898 // Destroying the Backdrop should empty the container.
899 ShowTopWindowBackdrop(false);
900 ASSERT_EQ(0U, default_container()->children().size());
903 // Verify that the backdrop gets properly created and placed.
904 TEST_F(WorkspaceLayoutManagerBackdropTest
, VerifyBackdropAndItsStacking
) {
905 scoped_ptr
<aura::Window
> window1(CreateTestWindow(gfx::Rect(1, 2, 3, 4)));
908 // Get the default container and check that only a single window is in there.
909 ASSERT_EQ(1U, default_container()->children().size());
910 EXPECT_EQ(window1
.get(), default_container()->children()[0]);
911 EXPECT_EQ("A", GetWindowOrderAsString(NULL
, window1
.get(), NULL
, NULL
));
913 // Create 2 more windows and check that they are also in the container.
914 scoped_ptr
<aura::Window
> window2(CreateTestWindow(gfx::Rect(10, 2, 3, 4)));
915 scoped_ptr
<aura::Window
> window3(CreateTestWindow(gfx::Rect(20, 2, 3, 4)));
919 aura::Window
* backdrop
= NULL
;
921 GetWindowOrderAsString(backdrop
, window1
.get(), window2
.get(),
924 // Turn on the backdrop mode and check that the window shows up where it
925 // should be (second highest number).
926 ShowTopWindowBackdrop(true);
927 backdrop
= default_container()->children()[2];
929 GetWindowOrderAsString(backdrop
, window1
.get(), window2
.get(),
932 // Switch the order of windows and check that it still remains in that
934 default_container()->StackChildAtTop(window2
.get());
936 GetWindowOrderAsString(backdrop
, window1
.get(), window2
.get(),
939 // Make the top window invisible and check.
940 window2
.get()->Hide();
942 GetWindowOrderAsString(backdrop
, window1
.get(), window2
.get(),
944 // Then delete window after window and see that everything is in order.
947 GetWindowOrderAsString(backdrop
, window1
.get(), window2
.get(),
951 GetWindowOrderAsString(backdrop
, window1
.get(), window2
.get(),
953 ShowTopWindowBackdrop(false);
955 GetWindowOrderAsString(NULL
, window1
.get(), window2
.get(),
959 // Tests that when hidding the shelf, that the backdrop resizes to fill the
960 // entire workspace area.
961 TEST_F(WorkspaceLayoutManagerBackdropTest
, ShelfVisibilityChangesBounds
) {
962 ShelfLayoutManager
* shelf_layout_manager
=
963 Shell::GetPrimaryRootWindowController()->GetShelfLayoutManager();
964 ShowTopWindowBackdrop(true);
965 RunAllPendingInMessageLoop();
967 ASSERT_EQ(SHELF_VISIBLE
, shelf_layout_manager
->visibility_state());
968 gfx::Rect initial_bounds
= default_container()->children()[0]->bounds();
969 shelf_layout_manager
->SetAutoHideBehavior(SHELF_AUTO_HIDE_ALWAYS_HIDDEN
);
970 shelf_layout_manager
->UpdateVisibilityState();
972 // When the shelf is re-shown WorkspaceLayoutManager shrinks all children
973 // including the backdrop.
974 shelf_layout_manager
->SetAutoHideBehavior(SHELF_AUTO_HIDE_BEHAVIOR_NEVER
);
975 shelf_layout_manager
->UpdateVisibilityState();
976 gfx::Rect reduced_bounds
= default_container()->children()[0]->bounds();
977 EXPECT_LT(reduced_bounds
.height(), initial_bounds
.height());
979 shelf_layout_manager
->SetAutoHideBehavior(SHELF_AUTO_HIDE_ALWAYS_HIDDEN
);
980 shelf_layout_manager
->UpdateVisibilityState();
982 EXPECT_GT(default_container()->children()[0]->bounds().height(),
983 reduced_bounds
.height());
986 class WorkspaceLayoutManagerKeyboardTest
: public test::AshTestBase
{
988 WorkspaceLayoutManagerKeyboardTest() {}
989 virtual ~WorkspaceLayoutManagerKeyboardTest() {}
991 virtual void SetUp() OVERRIDE
{
992 test::AshTestBase::SetUp();
993 UpdateDisplay("800x600");
994 aura::Window
* default_container
= Shell::GetContainer(
995 Shell::GetPrimaryRootWindow(), kShellWindowId_DefaultContainer
);
996 layout_manager_
= new WorkspaceLayoutManager(Shell::GetPrimaryRootWindow());
997 default_container
->SetLayoutManager(layout_manager_
);
1000 aura::Window
* CreateTestWindow(const gfx::Rect
& bounds
) {
1001 return CreateTestWindowInShellWithBounds(bounds
);
1004 void ShowKeyboard() {
1005 restore_work_area_insets_
= Shell::GetScreen()->GetPrimaryDisplay().
1006 GetWorkAreaInsets();
1007 Shell::GetInstance()->SetDisplayWorkAreaInsets(
1008 Shell::GetPrimaryRootWindow(),
1009 gfx::Insets(0, 0, keyboard_bounds_
.height(), 0));
1010 layout_manager_
->OnKeyboardBoundsChanging(keyboard_bounds_
);
1013 void HideKeyboard() {
1014 Shell::GetInstance()->SetDisplayWorkAreaInsets(
1015 Shell::GetPrimaryRootWindow(),
1016 restore_work_area_insets_
);
1017 layout_manager_
->OnKeyboardBoundsChanging(gfx::Rect());
1020 void SetKeyboardBounds(const gfx::Rect
& bounds
) {
1021 keyboard_bounds_
= bounds
;
1025 gfx::Insets restore_work_area_insets_
;
1026 gfx::Rect keyboard_bounds_
;
1027 WorkspaceLayoutManager
* layout_manager_
;
1029 DISALLOW_COPY_AND_ASSIGN(WorkspaceLayoutManagerKeyboardTest
);
1032 class FakeTextInputClient
: public ui::DummyTextInputClient
{
1034 explicit FakeTextInputClient(gfx::NativeWindow window
) : window_(window
) {}
1035 virtual ~FakeTextInputClient() {}
1037 virtual gfx::NativeWindow
GetAttachedWindow() const OVERRIDE
{
1042 gfx::NativeWindow window_
;
1044 DISALLOW_COPY_AND_ASSIGN(FakeTextInputClient
);
1047 TEST_F(WorkspaceLayoutManagerKeyboardTest
, AdjustWindowForA11yKeyboard
) {
1048 gfx::Rect
work_area(
1049 Shell::GetScreen()->GetPrimaryDisplay().work_area());
1050 gfx::Rect
keyboard_bounds(work_area
.x(),
1051 work_area
.y() + work_area
.height() / 2,
1053 work_area
.height() / 2);
1055 SetKeyboardBounds(keyboard_bounds
);
1056 scoped_ptr
<aura::Window
> window(
1057 CreateTestWindowInShellWithBounds(work_area
));
1059 aura::Window
* root_window
= ash::Shell::GetInstance()->GetPrimaryRootWindow();
1060 FakeTextInputClient
text_input_client(window
.get());
1061 ui::InputMethod
* input_method
=
1062 root_window
->GetProperty(aura::client::kRootWindowInputMethodKey
);
1063 if (switches::IsTextInputFocusManagerEnabled()) {
1064 ui::TextInputFocusManager::GetInstance()->FocusTextInputClient(
1065 &text_input_client
);
1067 input_method
->SetFocusedTextInputClient(&text_input_client
);
1070 int available_height
=
1071 Shell::GetScreen()->GetPrimaryDisplay().bounds().height() -
1072 keyboard_bounds
.height();
1074 EXPECT_EQ(gfx::Rect(work_area
).ToString(),
1075 window
->bounds().ToString());
1077 EXPECT_EQ(gfx::Rect(work_area
.origin(),
1078 gfx::Size(work_area
.width(), available_height
)).ToString(),
1079 window
->bounds().ToString());
1082 window
->SetBounds(gfx::Rect(50, 50, 100, 500));
1083 EXPECT_EQ("50,50 100x500", window
->bounds().ToString());
1085 EXPECT_EQ(gfx::Rect(50, 0, 100, available_height
).ToString(),
1086 window
->bounds().ToString());
1088 if (switches::IsTextInputFocusManagerEnabled()) {
1089 ui::TextInputFocusManager::GetInstance()->BlurTextInputClient(
1090 &text_input_client
);
1092 input_method
->SetFocusedTextInputClient(NULL
);