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"
7 #include "ash/display/display_layout.h"
8 #include "ash/display/display_manager.h"
9 #include "ash/root_window_controller.h"
10 #include "ash/screen_util.h"
11 #include "ash/session/session_state_delegate.h"
12 #include "ash/shelf/shelf_layout_manager.h"
13 #include "ash/shell.h"
14 #include "ash/shell_observer.h"
15 #include "ash/shell_window_ids.h"
16 #include "ash/test/ash_test_base.h"
17 #include "ash/wm/maximize_mode/workspace_backdrop_delegate.h"
18 #include "ash/wm/window_state.h"
19 #include "ash/wm/window_util.h"
20 #include "ash/wm/wm_event.h"
21 #include "ash/wm/workspace/workspace_window_resizer.h"
22 #include "base/basictypes.h"
23 #include "base/compiler_specific.h"
24 #include "ui/aura/client/aura_constants.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/base/ui_base_types.h"
29 #include "ui/gfx/insets.h"
30 #include "ui/gfx/screen.h"
31 #include "ui/views/widget/widget.h"
32 #include "ui/views/widget/widget_delegate.h"
33 #include "ui/wm/core/window_util.h"
38 class MaximizeDelegateView
: public views::WidgetDelegateView
{
40 MaximizeDelegateView(const gfx::Rect
& initial_bounds
)
41 : initial_bounds_(initial_bounds
) {
43 virtual ~MaximizeDelegateView() {}
45 virtual bool GetSavedWindowPlacement(
46 const views::Widget
* widget
,
48 ui::WindowShowState
* show_state
) const OVERRIDE
{
49 *bounds
= initial_bounds_
;
50 *show_state
= ui::SHOW_STATE_MAXIMIZED
;
55 const gfx::Rect initial_bounds_
;
57 DISALLOW_COPY_AND_ASSIGN(MaximizeDelegateView
);
60 class TestShellObserver
: public ShellObserver
{
62 TestShellObserver() : call_count_(0),
63 is_fullscreen_(false) {
64 Shell::GetInstance()->AddShellObserver(this);
67 virtual ~TestShellObserver() {
68 Shell::GetInstance()->RemoveShellObserver(this);
71 virtual void OnFullscreenStateChanged(bool is_fullscreen
,
72 aura::Window
* root_window
) OVERRIDE
{
74 is_fullscreen_
= is_fullscreen
;
77 int call_count() const {
81 bool is_fullscreen() const {
82 return is_fullscreen_
;
89 DISALLOW_COPY_AND_ASSIGN(TestShellObserver
);
94 typedef test::AshTestBase WorkspaceLayoutManagerTest
;
96 // Verifies that a window containing a restore coordinate will be restored to
97 // to the size prior to minimize, keeping the restore rectangle in tact (if
99 TEST_F(WorkspaceLayoutManagerTest
, RestoreFromMinimizeKeepsRestore
) {
100 scoped_ptr
<aura::Window
> window(
101 CreateTestWindowInShellWithBounds(gfx::Rect(1, 2, 3, 4)));
102 gfx::Rect
bounds(10, 15, 25, 35);
103 window
->SetBounds(bounds
);
105 wm::WindowState
* window_state
= wm::GetWindowState(window
.get());
107 // This will not be used for un-minimizing window.
108 window_state
->SetRestoreBoundsInScreen(gfx::Rect(0, 0, 100, 100));
109 window_state
->Minimize();
110 window_state
->Restore();
111 EXPECT_EQ("0,0 100x100", window_state
->GetRestoreBoundsInScreen().ToString());
112 EXPECT_EQ("10,15 25x35", window
.get()->bounds().ToString());
114 if (!SupportsMultipleDisplays())
117 UpdateDisplay("400x300,500x400");
118 window
->SetBoundsInScreen(gfx::Rect(600, 0, 100, 100),
119 ScreenUtil::GetSecondaryDisplay());
120 EXPECT_EQ(Shell::GetAllRootWindows()[1], window
->GetRootWindow());
121 window_state
->Minimize();
122 // This will not be used for un-minimizing window.
123 window_state
->SetRestoreBoundsInScreen(gfx::Rect(0, 0, 100, 100));
124 window_state
->Restore();
125 EXPECT_EQ("600,0 100x100", window
->GetBoundsInScreen().ToString());
127 // Make sure the unminimized window moves inside the display when
128 // 2nd display is disconnected.
129 window_state
->Minimize();
130 UpdateDisplay("400x300");
131 window_state
->Restore();
132 EXPECT_EQ(Shell::GetPrimaryRootWindow(), window
->GetRootWindow());
134 Shell::GetPrimaryRootWindow()->bounds().Intersects(window
->bounds()));
137 TEST_F(WorkspaceLayoutManagerTest
, KeepMinimumVisibilityInDisplays
) {
138 if (!SupportsMultipleDisplays())
141 UpdateDisplay("300x400,400x500");
142 aura::Window::Windows root_windows
= Shell::GetAllRootWindows();
144 DisplayLayout
layout(DisplayLayout::TOP
, 0);
145 Shell::GetInstance()->display_manager()->
146 SetLayoutForCurrentDisplays(layout
);
147 EXPECT_EQ("0,-500 400x500", root_windows
[1]->GetBoundsInScreen().ToString());
149 scoped_ptr
<aura::Window
> window1(
150 CreateTestWindowInShellWithBounds(gfx::Rect(10, -400, 200, 200)));
151 EXPECT_EQ("10,-400 200x200", window1
->GetBoundsInScreen().ToString());
153 // Make sure the caption is visible.
154 scoped_ptr
<aura::Window
> window2(
155 CreateTestWindowInShellWithBounds(gfx::Rect(10, -600, 200, 200)));
156 EXPECT_EQ("10,-500 200x200", window2
->GetBoundsInScreen().ToString());
159 TEST_F(WorkspaceLayoutManagerTest
, KeepRestoredWindowInDisplay
) {
160 if (!SupportsHostWindowResize())
162 scoped_ptr
<aura::Window
> window(
163 CreateTestWindowInShellWithBounds(gfx::Rect(1, 2, 30, 40)));
164 wm::WindowState
* window_state
= wm::GetWindowState(window
.get());
166 // Maximized -> Normal transition.
167 window_state
->Maximize();
168 window_state
->SetRestoreBoundsInScreen(gfx::Rect(-100, -100, 30, 40));
169 window_state
->Restore();
171 Shell::GetPrimaryRootWindow()->bounds().Intersects(window
->bounds()));
172 // Y bounds should not be negative.
173 EXPECT_EQ("-20,0 30x40", window
->bounds().ToString());
175 // Minimized -> Normal transition.
176 window
->SetBounds(gfx::Rect(-100, -100, 30, 40));
177 window_state
->Minimize();
179 Shell::GetPrimaryRootWindow()->bounds().Intersects(window
->bounds()));
180 EXPECT_EQ("-100,-100 30x40", window
->bounds().ToString());
183 Shell::GetPrimaryRootWindow()->bounds().Intersects(window
->bounds()));
184 // Y bounds should not be negative.
185 EXPECT_EQ("-20,0 30x40", window
->bounds().ToString());
187 // Fullscreen -> Normal transition.
188 window
->SetBounds(gfx::Rect(0, 0, 30, 40)); // reset bounds.
189 ASSERT_EQ("0,0 30x40", window
->bounds().ToString());
190 window
->SetProperty(aura::client::kShowStateKey
, ui::SHOW_STATE_FULLSCREEN
);
191 EXPECT_EQ(window
->bounds(), window
->GetRootWindow()->bounds());
192 window_state
->SetRestoreBoundsInScreen(gfx::Rect(-100, -100, 30, 40));
193 window_state
->Restore();
195 Shell::GetPrimaryRootWindow()->bounds().Intersects(window
->bounds()));
196 // Y bounds should not be negative.
197 EXPECT_EQ("-20,0 30x40", window
->bounds().ToString());
200 TEST_F(WorkspaceLayoutManagerTest
, MaximizeInDisplayToBeRestored
) {
201 if (!SupportsMultipleDisplays())
203 UpdateDisplay("300x400,400x500");
205 aura::Window::Windows root_windows
= Shell::GetAllRootWindows();
207 scoped_ptr
<aura::Window
> window(
208 CreateTestWindowInShellWithBounds(gfx::Rect(1, 2, 30, 40)));
209 EXPECT_EQ(root_windows
[0], window
->GetRootWindow());
211 wm::WindowState
* window_state
= wm::GetWindowState(window
.get());
212 window_state
->SetRestoreBoundsInScreen(gfx::Rect(400, 0, 30, 40));
213 // Maximize the window in 2nd display as the restore bounds
214 // is inside 2nd display.
215 window_state
->Maximize();
216 EXPECT_EQ(root_windows
[1], window
->GetRootWindow());
217 EXPECT_EQ("300,0 400x453", window
->GetBoundsInScreen().ToString());
219 window_state
->Restore();
220 EXPECT_EQ(root_windows
[1], window
->GetRootWindow());
221 EXPECT_EQ("400,0 30x40", window
->GetBoundsInScreen().ToString());
223 // If the restore bounds intersects with the current display,
225 window_state
->SetRestoreBoundsInScreen(gfx::Rect(280, 0, 30, 40));
226 window_state
->Maximize();
227 EXPECT_EQ(root_windows
[1], window
->GetRootWindow());
228 EXPECT_EQ("300,0 400x453", window
->GetBoundsInScreen().ToString());
230 window_state
->Restore();
231 EXPECT_EQ(root_windows
[1], window
->GetRootWindow());
232 EXPECT_EQ("280,0 30x40", window
->GetBoundsInScreen().ToString());
234 // Restoring widget state.
235 scoped_ptr
<views::Widget
> w1(new views::Widget
);
236 views::Widget::InitParams params
;
237 params
.ownership
= views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET
;
238 params
.delegate
= new MaximizeDelegateView(gfx::Rect(400, 0, 30, 40));
239 params
.context
= root_windows
[0];
242 EXPECT_TRUE(w1
->IsMaximized());
243 EXPECT_EQ(root_windows
[1], w1
->GetNativeView()->GetRootWindow());
244 EXPECT_EQ("300,0 400x453", w1
->GetWindowBoundsInScreen().ToString());
246 EXPECT_EQ(root_windows
[1], w1
->GetNativeView()->GetRootWindow());
247 EXPECT_EQ("400,0 30x40", w1
->GetWindowBoundsInScreen().ToString());
250 TEST_F(WorkspaceLayoutManagerTest
, FullscreenInDisplayToBeRestored
) {
251 if (!SupportsMultipleDisplays())
253 UpdateDisplay("300x400,400x500");
255 aura::Window::Windows root_windows
= Shell::GetAllRootWindows();
257 scoped_ptr
<aura::Window
> window(
258 CreateTestWindowInShellWithBounds(gfx::Rect(1, 2, 30, 40)));
259 EXPECT_EQ(root_windows
[0], window
->GetRootWindow());
261 wm::WindowState
* window_state
= wm::GetWindowState(window
.get());
262 window_state
->SetRestoreBoundsInScreen(gfx::Rect(400, 0, 30, 40));
263 // Maximize the window in 2nd display as the restore bounds
264 // is inside 2nd display.
265 window
->SetProperty(aura::client::kShowStateKey
,
266 ui::SHOW_STATE_FULLSCREEN
);
267 EXPECT_EQ(root_windows
[1], window
->GetRootWindow());
268 EXPECT_EQ("300,0 400x500", window
->GetBoundsInScreen().ToString());
270 window_state
->Restore();
271 EXPECT_EQ(root_windows
[1], window
->GetRootWindow());
272 EXPECT_EQ("400,0 30x40", window
->GetBoundsInScreen().ToString());
274 // If the restore bounds intersects with the current display,
276 window_state
->SetRestoreBoundsInScreen(gfx::Rect(280, 0, 30, 40));
277 window
->SetProperty(aura::client::kShowStateKey
,
278 ui::SHOW_STATE_FULLSCREEN
);
279 EXPECT_EQ(root_windows
[1], window
->GetRootWindow());
280 EXPECT_EQ("300,0 400x500", window
->GetBoundsInScreen().ToString());
282 window_state
->Restore();
283 EXPECT_EQ(root_windows
[1], window
->GetRootWindow());
284 EXPECT_EQ("280,0 30x40", window
->GetBoundsInScreen().ToString());
287 // WindowObserver implementation used by DontClobberRestoreBoundsWindowObserver.
288 // This code mirrors what BrowserFrameAsh does. In particular when this code
289 // sees the window was maximized it changes the bounds of a secondary
290 // window. The secondary window mirrors the status window.
291 class DontClobberRestoreBoundsWindowObserver
: public aura::WindowObserver
{
293 DontClobberRestoreBoundsWindowObserver() : window_(NULL
) {}
295 void set_window(aura::Window
* window
) { window_
= window
; }
297 virtual void OnWindowPropertyChanged(aura::Window
* window
,
299 intptr_t old
) OVERRIDE
{
303 if (wm::GetWindowState(window
)->IsMaximized()) {
304 aura::Window
* w
= window_
;
307 gfx::Rect
shelf_bounds(Shell::GetPrimaryRootWindowController()->
308 GetShelfLayoutManager()->GetIdealBounds());
309 const gfx::Rect
& window_bounds(w
->bounds());
310 w
->SetBounds(gfx::Rect(window_bounds
.x(), shelf_bounds
.y() - 1,
311 window_bounds
.width(), window_bounds
.height()));
316 aura::Window
* window_
;
318 DISALLOW_COPY_AND_ASSIGN(DontClobberRestoreBoundsWindowObserver
);
321 // Creates a window, maximized the window and from within the maximized
322 // notification sets the bounds of a window to overlap the shelf. Verifies this
323 // doesn't effect the restore bounds.
324 TEST_F(WorkspaceLayoutManagerTest
, DontClobberRestoreBounds
) {
325 DontClobberRestoreBoundsWindowObserver window_observer
;
326 scoped_ptr
<aura::Window
> window(new aura::Window(NULL
));
327 window
->SetType(ui::wm::WINDOW_TYPE_NORMAL
);
328 window
->Init(aura::WINDOW_LAYER_TEXTURED
);
329 window
->SetBounds(gfx::Rect(10, 20, 30, 40));
330 // NOTE: for this test to exercise the failure the observer needs to be added
331 // before the parent set. This mimics what BrowserFrameAsh does.
332 window
->AddObserver(&window_observer
);
333 ParentWindowInPrimaryRootWindow(window
.get());
336 wm::WindowState
* window_state
= wm::GetWindowState(window
.get());
337 window_state
->Activate();
339 scoped_ptr
<aura::Window
> window2(
340 CreateTestWindowInShellWithBounds(gfx::Rect(12, 20, 30, 40)));
341 ::wm::AddTransientChild(window
.get(), window2
.get());
344 window_observer
.set_window(window2
.get());
345 window_state
->Maximize();
346 EXPECT_EQ("10,20 30x40",
347 window_state
->GetRestoreBoundsInScreen().ToString());
348 window
->RemoveObserver(&window_observer
);
351 // Verifies when a window is maximized all descendant windows have a size.
352 TEST_F(WorkspaceLayoutManagerTest
, ChildBoundsResetOnMaximize
) {
353 scoped_ptr
<aura::Window
> window(
354 CreateTestWindowInShellWithBounds(gfx::Rect(10, 20, 30, 40)));
356 wm::WindowState
* window_state
= wm::GetWindowState(window
.get());
357 window_state
->Activate();
358 scoped_ptr
<aura::Window
> child_window(
359 aura::test::CreateTestWindowWithBounds(gfx::Rect(5, 6, 7, 8),
361 child_window
->Show();
362 window_state
->Maximize();
363 EXPECT_EQ("5,6 7x8", child_window
->bounds().ToString());
366 // Verifies a window created with maximized state has the maximized
368 TEST_F(WorkspaceLayoutManagerTest
, MaximizeWithEmptySize
) {
369 scoped_ptr
<aura::Window
> window(
370 aura::test::CreateTestWindowWithBounds(gfx::Rect(0, 0, 0, 0),
372 wm::GetWindowState(window
.get())->Maximize();
373 aura::Window
* default_container
= Shell::GetContainer(
374 Shell::GetPrimaryRootWindow(), kShellWindowId_DefaultContainer
);
375 default_container
->AddChild(window
.get());
378 Shell::GetScreen()->GetPrimaryDisplay().work_area());
379 EXPECT_EQ(work_area
.ToString(), window
->GetBoundsInScreen().ToString());
382 TEST_F(WorkspaceLayoutManagerTest
, WindowShouldBeOnScreenWhenAdded
) {
383 // Normal window bounds shouldn't be changed.
384 gfx::Rect
window_bounds(100, 100, 200, 200);
385 scoped_ptr
<aura::Window
> window(
386 CreateTestWindowInShellWithBounds(window_bounds
));
387 EXPECT_EQ(window_bounds
, window
->bounds());
389 // If the window is out of the workspace, it would be moved on screen.
390 gfx::Rect root_window_bounds
=
391 Shell::GetInstance()->GetPrimaryRootWindow()->bounds();
392 window_bounds
.Offset(root_window_bounds
.width(), root_window_bounds
.height());
393 ASSERT_FALSE(window_bounds
.Intersects(root_window_bounds
));
394 scoped_ptr
<aura::Window
> out_window(
395 CreateTestWindowInShellWithBounds(window_bounds
));
396 EXPECT_EQ(window_bounds
.size(), out_window
->bounds().size());
397 gfx::Rect bounds
= out_window
->bounds();
398 bounds
.Intersect(root_window_bounds
);
400 // 30% of the window edge must be visible.
401 EXPECT_GT(bounds
.width(), out_window
->bounds().width() * 0.29);
402 EXPECT_GT(bounds
.height(), out_window
->bounds().height() * 0.29);
404 aura::Window
* parent
= out_window
->parent();
405 parent
->RemoveChild(out_window
.get());
406 out_window
->SetBounds(gfx::Rect(-200, -200, 200, 200));
407 // UserHasChangedWindowPositionOrSize flag shouldn't turn off this behavior.
408 wm::GetWindowState(window
.get())->set_bounds_changed_by_user(true);
409 parent
->AddChild(out_window
.get());
410 EXPECT_GT(bounds
.width(), out_window
->bounds().width() * 0.29);
411 EXPECT_GT(bounds
.height(), out_window
->bounds().height() * 0.29);
413 // Make sure we always make more than 1/3 of the window edge visible even
414 // if the initial bounds intersects with display.
415 window_bounds
.SetRect(-150, -150, 200, 200);
416 bounds
= window_bounds
;
417 bounds
.Intersect(root_window_bounds
);
419 // Make sure that the initial bounds' visible area is less than 26%
420 // so that the auto adjustment logic kicks in.
421 ASSERT_LT(bounds
.width(), out_window
->bounds().width() * 0.26);
422 ASSERT_LT(bounds
.height(), out_window
->bounds().height() * 0.26);
423 ASSERT_TRUE(window_bounds
.Intersects(root_window_bounds
));
425 scoped_ptr
<aura::Window
> partially_out_window(
426 CreateTestWindowInShellWithBounds(window_bounds
));
427 EXPECT_EQ(window_bounds
.size(), partially_out_window
->bounds().size());
428 bounds
= partially_out_window
->bounds();
429 bounds
.Intersect(root_window_bounds
);
430 EXPECT_GT(bounds
.width(), out_window
->bounds().width() * 0.29);
431 EXPECT_GT(bounds
.height(), out_window
->bounds().height() * 0.29);
433 // Make sure the window whose 30% width/height is bigger than display
434 // will be placed correctly.
435 window_bounds
.SetRect(-1900, -1900, 3000, 3000);
436 scoped_ptr
<aura::Window
> window_bigger_than_display(
437 CreateTestWindowInShellWithBounds(window_bounds
));
438 EXPECT_GE(root_window_bounds
.width(),
439 window_bigger_than_display
->bounds().width());
440 EXPECT_GE(root_window_bounds
.height(),
441 window_bigger_than_display
->bounds().height());
443 bounds
= window_bigger_than_display
->bounds();
444 bounds
.Intersect(root_window_bounds
);
445 EXPECT_GT(bounds
.width(), out_window
->bounds().width() * 0.29);
446 EXPECT_GT(bounds
.height(), out_window
->bounds().height() * 0.29);
449 // Verifies the size of a window is enforced to be smaller than the work area.
450 TEST_F(WorkspaceLayoutManagerTest
, SizeToWorkArea
) {
451 // Normal window bounds shouldn't be changed.
453 Shell::GetScreen()->GetPrimaryDisplay().work_area().size());
454 const gfx::Rect
window_bounds(
455 100, 101, work_area
.width() + 1, work_area
.height() + 2);
456 scoped_ptr
<aura::Window
> window(
457 CreateTestWindowInShellWithBounds(window_bounds
));
458 EXPECT_EQ(gfx::Rect(gfx::Point(100, 101), work_area
).ToString(),
459 window
->bounds().ToString());
461 // Directly setting the bounds triggers a slightly different code path. Verify
463 window
->SetBounds(window_bounds
);
464 EXPECT_EQ(gfx::Rect(gfx::Point(100, 101), work_area
).ToString(),
465 window
->bounds().ToString());
468 TEST_F(WorkspaceLayoutManagerTest
, NotifyFullscreenChanges
) {
469 TestShellObserver observer
;
470 scoped_ptr
<aura::Window
> window1(
471 CreateTestWindowInShellWithBounds(gfx::Rect(1, 2, 30, 40)));
472 scoped_ptr
<aura::Window
> window2(
473 CreateTestWindowInShellWithBounds(gfx::Rect(1, 2, 30, 40)));
474 wm::WindowState
* window_state1
= wm::GetWindowState(window1
.get());
475 wm::WindowState
* window_state2
= wm::GetWindowState(window2
.get());
476 window_state2
->Activate();
478 const wm::WMEvent
toggle_fullscreen_event(wm::WM_EVENT_TOGGLE_FULLSCREEN
);
479 window_state2
->OnWMEvent(&toggle_fullscreen_event
);
480 EXPECT_EQ(1, observer
.call_count());
481 EXPECT_TRUE(observer
.is_fullscreen());
483 // When window1 moves to the front the fullscreen state should change.
484 window_state1
->Activate();
485 EXPECT_EQ(2, observer
.call_count());
486 EXPECT_FALSE(observer
.is_fullscreen());
488 // It should change back if window2 becomes active again.
489 window_state2
->Activate();
490 EXPECT_EQ(3, observer
.call_count());
491 EXPECT_TRUE(observer
.is_fullscreen());
493 window_state2
->OnWMEvent(&toggle_fullscreen_event
);
494 EXPECT_EQ(4, observer
.call_count());
495 EXPECT_FALSE(observer
.is_fullscreen());
497 window_state2
->OnWMEvent(&toggle_fullscreen_event
);
498 EXPECT_EQ(5, observer
.call_count());
499 EXPECT_TRUE(observer
.is_fullscreen());
501 // Closing the window should change the fullscreen state.
503 EXPECT_EQ(6, observer
.call_count());
504 EXPECT_FALSE(observer
.is_fullscreen());
507 // Following tests were originally written for BaseLayoutManager.
511 class WorkspaceLayoutManagerSoloTest
: public test::AshTestBase
{
513 WorkspaceLayoutManagerSoloTest() {}
514 virtual ~WorkspaceLayoutManagerSoloTest() {}
516 virtual void SetUp() OVERRIDE
{
517 test::AshTestBase::SetUp();
518 UpdateDisplay("800x600");
519 aura::Window
* default_container
= Shell::GetContainer(
520 Shell::GetPrimaryRootWindow(), kShellWindowId_DefaultContainer
);
521 default_container
->SetLayoutManager(
522 new WorkspaceLayoutManager(Shell::GetPrimaryRootWindow()));
525 aura::Window
* CreateTestWindow(const gfx::Rect
& bounds
) {
526 return CreateTestWindowInShellWithBounds(bounds
);
530 DISALLOW_COPY_AND_ASSIGN(WorkspaceLayoutManagerSoloTest
);
535 // Tests normal->maximize->normal.
536 TEST_F(WorkspaceLayoutManagerSoloTest
, Maximize
) {
537 gfx::Rect
bounds(100, 100, 200, 200);
538 scoped_ptr
<aura::Window
> window(CreateTestWindow(bounds
));
539 window
->SetProperty(aura::client::kShowStateKey
, ui::SHOW_STATE_MAXIMIZED
);
540 // Maximized window fills the work area, not the whole display.
542 ScreenUtil::GetMaximizedWindowBoundsInParent(window
.get()).ToString(),
543 window
->bounds().ToString());
544 window
->SetProperty(aura::client::kShowStateKey
, ui::SHOW_STATE_NORMAL
);
545 EXPECT_EQ(bounds
.ToString(), window
->bounds().ToString());
548 // Tests normal->minimize->normal.
549 TEST_F(WorkspaceLayoutManagerSoloTest
, Minimize
) {
550 gfx::Rect
bounds(100, 100, 200, 200);
551 scoped_ptr
<aura::Window
> window(CreateTestWindow(bounds
));
552 window
->SetProperty(aura::client::kShowStateKey
, ui::SHOW_STATE_MINIMIZED
);
553 // Note: Currently minimize doesn't do anything except set the state.
554 // See crbug.com/104571.
555 EXPECT_EQ(bounds
.ToString(), window
->bounds().ToString());
556 window
->SetProperty(aura::client::kShowStateKey
, ui::SHOW_STATE_NORMAL
);
557 EXPECT_EQ(bounds
.ToString(), window
->bounds().ToString());
560 // A WindowDelegate which sets the focus when the window
562 class FocusDelegate
: public aura::test::TestWindowDelegate
{
566 show_state_(ui::SHOW_STATE_END
) {
568 virtual ~FocusDelegate() {}
570 void set_window(aura::Window
* window
) { window_
= window
; }
572 // aura::test::TestWindowDelegate overrides:
573 virtual void OnWindowTargetVisibilityChanged(bool visible
) OVERRIDE
{
577 show_state_
= window_
->GetProperty(aura::client::kShowStateKey
);
581 ui::WindowShowState
GetShowStateAndReset() {
582 ui::WindowShowState ret
= show_state_
;
583 show_state_
= ui::SHOW_STATE_END
;
588 aura::Window
* window_
;
589 ui::WindowShowState show_state_
;
591 DISALLOW_COPY_AND_ASSIGN(FocusDelegate
);
594 // Make sure that the window's show state is correct in
595 // |WindowDelegate::OnWindowTargetVisibilityChanged|, and setting
596 // focus in this callback doesn't cause DCHECK error. See
598 TEST_F(WorkspaceLayoutManagerSoloTest
, FocusDuringUnminimize
) {
599 FocusDelegate delegate
;
600 scoped_ptr
<aura::Window
> window(CreateTestWindowInShellWithDelegate(
601 &delegate
, 0, gfx::Rect(100, 100, 100, 100)));
602 delegate
.set_window(window
.get());
603 window
->SetProperty(aura::client::kShowStateKey
, ui::SHOW_STATE_MINIMIZED
);
604 EXPECT_FALSE(window
->IsVisible());
605 EXPECT_EQ(ui::SHOW_STATE_MINIMIZED
, delegate
.GetShowStateAndReset());
607 EXPECT_TRUE(window
->IsVisible());
608 EXPECT_EQ(ui::SHOW_STATE_NORMAL
, delegate
.GetShowStateAndReset());
611 // Tests maximized window size during root window resize.
612 TEST_F(WorkspaceLayoutManagerSoloTest
, MaximizeRootWindowResize
) {
613 gfx::Rect
bounds(100, 100, 200, 200);
614 scoped_ptr
<aura::Window
> window(CreateTestWindow(bounds
));
615 window
->SetProperty(aura::client::kShowStateKey
, ui::SHOW_STATE_MAXIMIZED
);
616 gfx::Rect initial_work_area_bounds
=
617 ScreenUtil::GetMaximizedWindowBoundsInParent(window
.get());
618 EXPECT_EQ(initial_work_area_bounds
.ToString(), window
->bounds().ToString());
619 // Enlarge the root window. We should still match the work area size.
620 UpdateDisplay("900x700");
622 ScreenUtil::GetMaximizedWindowBoundsInParent(window
.get()).ToString(),
623 window
->bounds().ToString());
625 initial_work_area_bounds
.ToString(),
626 ScreenUtil::GetMaximizedWindowBoundsInParent(window
.get()).ToString());
629 // Tests normal->fullscreen->normal.
630 TEST_F(WorkspaceLayoutManagerSoloTest
, Fullscreen
) {
631 gfx::Rect
bounds(100, 100, 200, 200);
632 scoped_ptr
<aura::Window
> window(CreateTestWindow(bounds
));
633 window
->SetProperty(aura::client::kShowStateKey
, ui::SHOW_STATE_FULLSCREEN
);
634 // Fullscreen window fills the whole display.
635 EXPECT_EQ(Shell::GetScreen()->GetDisplayNearestWindow(
636 window
.get()).bounds().ToString(),
637 window
->bounds().ToString());
638 window
->SetProperty(aura::client::kShowStateKey
, ui::SHOW_STATE_NORMAL
);
639 EXPECT_EQ(bounds
.ToString(), window
->bounds().ToString());
642 // Tests fullscreen window size during root window resize.
643 TEST_F(WorkspaceLayoutManagerSoloTest
, FullscreenRootWindowResize
) {
644 gfx::Rect
bounds(100, 100, 200, 200);
645 scoped_ptr
<aura::Window
> window(CreateTestWindow(bounds
));
646 // Fullscreen window fills the whole display.
647 window
->SetProperty(aura::client::kShowStateKey
, ui::SHOW_STATE_FULLSCREEN
);
648 EXPECT_EQ(Shell::GetScreen()->GetDisplayNearestWindow(
649 window
.get()).bounds().ToString(),
650 window
->bounds().ToString());
651 // Enlarge the root window. We should still match the display size.
652 UpdateDisplay("800x600");
653 EXPECT_EQ(Shell::GetScreen()->GetDisplayNearestWindow(
654 window
.get()).bounds().ToString(),
655 window
->bounds().ToString());
658 // Tests that when the screen gets smaller the windows aren't bigger than
660 TEST_F(WorkspaceLayoutManagerSoloTest
, RootWindowResizeShrinksWindows
) {
661 scoped_ptr
<aura::Window
> window(
662 CreateTestWindow(gfx::Rect(10, 20, 500, 400)));
663 gfx::Rect work_area
= Shell::GetScreen()->GetDisplayNearestWindow(
664 window
.get()).work_area();
665 // Invariant: Window is smaller than work area.
666 EXPECT_LE(window
->bounds().width(), work_area
.width());
667 EXPECT_LE(window
->bounds().height(), work_area
.height());
669 // Make the root window narrower than our window.
670 UpdateDisplay("300x400");
671 work_area
= Shell::GetScreen()->GetDisplayNearestWindow(
672 window
.get()).work_area();
673 EXPECT_LE(window
->bounds().width(), work_area
.width());
674 EXPECT_LE(window
->bounds().height(), work_area
.height());
676 // Make the root window shorter than our window.
677 UpdateDisplay("300x200");
678 work_area
= Shell::GetScreen()->GetDisplayNearestWindow(
679 window
.get()).work_area();
680 EXPECT_LE(window
->bounds().width(), work_area
.width());
681 EXPECT_LE(window
->bounds().height(), work_area
.height());
683 // Enlarging the root window does not change the window bounds.
684 gfx::Rect old_bounds
= window
->bounds();
685 UpdateDisplay("800x600");
686 EXPECT_EQ(old_bounds
.width(), window
->bounds().width());
687 EXPECT_EQ(old_bounds
.height(), window
->bounds().height());
690 // Verifies maximizing sets the restore bounds, and restoring
691 // restores the bounds.
692 TEST_F(WorkspaceLayoutManagerSoloTest
, MaximizeSetsRestoreBounds
) {
693 scoped_ptr
<aura::Window
> window(CreateTestWindow(gfx::Rect(10, 20, 30, 40)));
694 wm::WindowState
* window_state
= wm::GetWindowState(window
.get());
696 // Maximize it, which will keep the previous restore bounds.
697 window
->SetProperty(aura::client::kShowStateKey
, ui::SHOW_STATE_MAXIMIZED
);
698 EXPECT_EQ("10,20 30x40", window_state
->GetRestoreBoundsInParent().ToString());
700 // Restore it, which should restore bounds and reset restore bounds.
701 window
->SetProperty(aura::client::kShowStateKey
, ui::SHOW_STATE_NORMAL
);
702 EXPECT_EQ("10,20 30x40", window
->bounds().ToString());
703 EXPECT_FALSE(window_state
->HasRestoreBounds());
706 // Verifies maximizing keeps the restore bounds if set.
707 TEST_F(WorkspaceLayoutManagerSoloTest
, MaximizeResetsRestoreBounds
) {
708 scoped_ptr
<aura::Window
> window(CreateTestWindow(gfx::Rect(1, 2, 3, 4)));
710 wm::WindowState
* window_state
= wm::GetWindowState(window
.get());
711 window_state
->SetRestoreBoundsInParent(gfx::Rect(10, 11, 12, 13));
713 // Maximize it, which will keep the previous restore bounds.
714 window
->SetProperty(aura::client::kShowStateKey
, ui::SHOW_STATE_MAXIMIZED
);
715 EXPECT_EQ("10,11 12x13", window_state
->GetRestoreBoundsInParent().ToString());
718 // Verifies that the restore bounds do not get reset when restoring to a
719 // maximzied state from a minimized state.
720 TEST_F(WorkspaceLayoutManagerSoloTest
,
721 BoundsAfterRestoringToMaximizeFromMinimize
) {
722 scoped_ptr
<aura::Window
> window(CreateTestWindow(gfx::Rect(1, 2, 3, 4)));
723 gfx::Rect
bounds(10, 15, 25, 35);
724 window
->SetBounds(bounds
);
726 wm::WindowState
* window_state
= wm::GetWindowState(window
.get());
727 // Maximize it, which should reset restore bounds.
728 window_state
->Maximize();
729 EXPECT_EQ(bounds
.ToString(),
730 window_state
->GetRestoreBoundsInParent().ToString());
731 // Minimize the window. The restore bounds should not change.
732 window_state
->Minimize();
733 EXPECT_EQ(bounds
.ToString(),
734 window_state
->GetRestoreBoundsInParent().ToString());
736 // Show the window again. The window should be maximized, and the restore
737 // bounds should not change.
739 EXPECT_EQ(bounds
.ToString(),
740 window_state
->GetRestoreBoundsInParent().ToString());
741 EXPECT_TRUE(window_state
->IsMaximized());
743 window_state
->Restore();
744 EXPECT_EQ(bounds
.ToString(), window
->bounds().ToString());
747 // Verify if the window is not resized during screen lock. See: crbug.com/173127
748 TEST_F(WorkspaceLayoutManagerSoloTest
, NotResizeWhenScreenIsLocked
) {
749 SetCanLockScreen(true);
750 scoped_ptr
<aura::Window
> window(CreateTestWindow(gfx::Rect(1, 2, 3, 4)));
751 // window with AlwaysOnTop will be managed by BaseLayoutManager.
752 window
->SetProperty(aura::client::kAlwaysOnTopKey
, true);
755 ShelfLayoutManager
* shelf
= ShelfLayoutManager::ForShelf(window
.get());
756 shelf
->SetAutoHideBehavior(SHELF_AUTO_HIDE_BEHAVIOR_ALWAYS
);
758 window
->SetBounds(ScreenUtil::GetMaximizedWindowBoundsInParent(window
.get()));
759 gfx::Rect window_bounds
= window
->bounds();
761 ScreenUtil::GetMaximizedWindowBoundsInParent(window
.get()).ToString(),
762 window_bounds
.ToString());
764 Shell::GetInstance()->session_state_delegate()->LockScreen();
765 shelf
->UpdateVisibilityState();
767 ScreenUtil::GetMaximizedWindowBoundsInParent(window
.get()).ToString(),
768 window_bounds
.ToString());
770 Shell::GetInstance()->session_state_delegate()->UnlockScreen();
771 shelf
->UpdateVisibilityState();
772 EXPECT_EQ(window_bounds
.ToString(), window
->bounds().ToString());
775 // Following tests are written to test the backdrop functionality.
779 class WorkspaceLayoutManagerBackdropTest
: public test::AshTestBase
{
781 WorkspaceLayoutManagerBackdropTest() {}
782 virtual ~WorkspaceLayoutManagerBackdropTest() {}
784 virtual void SetUp() OVERRIDE
{
785 test::AshTestBase::SetUp();
786 UpdateDisplay("800x600");
787 default_container_
= Shell::GetContainer(Shell::GetPrimaryRootWindow(),
788 kShellWindowId_DefaultContainer
);
789 // We set the size to something smaller then the display to avoid resizing
790 // issues with the shelf.
791 default_container_
->SetBounds(gfx::Rect(0, 0, 800, 500));
794 aura::Window
* CreateTestWindow(const gfx::Rect
& bounds
) {
795 aura::Window
* window
= CreateTestWindowInShellWithBounds(bounds
);
799 // Turn the top window back drop on / off.
800 void ShowTopWindowBackdrop(bool show
) {
801 scoped_ptr
<ash::WorkspaceLayoutManagerDelegate
> backdrop
;
803 backdrop
.reset(new ash::WorkspaceBackdropDelegate(default_container_
));
805 (static_cast<WorkspaceLayoutManager
*>(default_container_
->layout_manager()))
806 ->SetMaximizeBackdropDelegate(backdrop
.Pass());
807 // Closing and / or opening can be a delayed operation.
808 base::MessageLoop::current()->RunUntilIdle();
811 // Return the default container.
812 aura::Window
* default_container() { return default_container_
; }
814 // Return the order of windows (top most first) as they are in the default
815 // container. If the window is visible it will be a big letter, otherwise a
816 // small one. The backdrop will be an X and unknown windows will be shown as
818 std::string
GetWindowOrderAsString(aura::Window
* backdrop
,
823 for (int i
= static_cast<int>(default_container()->children().size()) - 1;
828 if (default_container()->children()[i
] == wa
)
829 result
+= default_container()->children()[i
]->IsVisible() ? "A" : "a";
830 else if (default_container()->children()[i
] == wb
)
831 result
+= default_container()->children()[i
]->IsVisible() ? "B" : "b";
832 else if (default_container()->children()[i
] == wc
)
833 result
+= default_container()->children()[i
]->IsVisible() ? "C" : "c";
834 else if (default_container()->children()[i
] == backdrop
)
835 result
+= default_container()->children()[i
]->IsVisible() ? "X" : "x";
843 // The default container.
844 aura::Window
* default_container_
;
846 DISALLOW_COPY_AND_ASSIGN(WorkspaceLayoutManagerBackdropTest
);
851 // Check that creating the BackDrop without destroying it does not lead into
853 TEST_F(WorkspaceLayoutManagerBackdropTest
, BackdropCrashTest
) {
854 ShowTopWindowBackdrop(true);
857 // Verify basic assumptions about the backdrop.
858 TEST_F(WorkspaceLayoutManagerBackdropTest
, BasicBackdropTests
) {
859 // Create a backdrop and see that there is one window (the backdrop) and
860 // that the size is the same as the default container as well as that it is
862 ShowTopWindowBackdrop(true);
863 ASSERT_EQ(1U, default_container()->children().size());
864 EXPECT_FALSE(default_container()->children()[0]->IsVisible());
867 // Add a window and make sure that the backdrop is the second child.
868 scoped_ptr
<aura::Window
> window(CreateTestWindow(gfx::Rect(1, 2, 3, 4)));
870 ASSERT_EQ(2U, default_container()->children().size());
871 EXPECT_TRUE(default_container()->children()[0]->IsVisible());
872 EXPECT_TRUE(default_container()->children()[1]->IsVisible());
873 EXPECT_EQ(window
.get(), default_container()->children()[1]);
874 EXPECT_EQ(default_container()->bounds().ToString(),
875 default_container()->children()[0]->bounds().ToString());
878 // With the window gone the backdrop should be invisible again.
879 ASSERT_EQ(1U, default_container()->children().size());
880 EXPECT_FALSE(default_container()->children()[0]->IsVisible());
882 // Destroying the Backdrop should empty the container.
883 ShowTopWindowBackdrop(false);
884 ASSERT_EQ(0U, default_container()->children().size());
887 // Verify that the backdrop gets properly created and placed.
888 TEST_F(WorkspaceLayoutManagerBackdropTest
, VerifyBackdropAndItsStacking
) {
889 scoped_ptr
<aura::Window
> window1(CreateTestWindow(gfx::Rect(1, 2, 3, 4)));
892 // Get the default container and check that only a single window is in there.
893 ASSERT_EQ(1U, default_container()->children().size());
894 EXPECT_EQ(window1
.get(), default_container()->children()[0]);
895 EXPECT_EQ("A", GetWindowOrderAsString(NULL
, window1
.get(), NULL
, NULL
));
897 // Create 2 more windows and check that they are also in the container.
898 scoped_ptr
<aura::Window
> window2(CreateTestWindow(gfx::Rect(10, 2, 3, 4)));
899 scoped_ptr
<aura::Window
> window3(CreateTestWindow(gfx::Rect(20, 2, 3, 4)));
903 aura::Window
* backdrop
= NULL
;
905 GetWindowOrderAsString(backdrop
, window1
.get(), window2
.get(),
908 // Turn on the backdrop mode and check that the window shows up where it
909 // should be (second highest number).
910 ShowTopWindowBackdrop(true);
911 backdrop
= default_container()->children()[2];
913 GetWindowOrderAsString(backdrop
, window1
.get(), window2
.get(),
916 // Switch the order of windows and check that it still remains in that
918 default_container()->StackChildAtTop(window2
.get());
920 GetWindowOrderAsString(backdrop
, window1
.get(), window2
.get(),
923 // Make the top window invisible and check.
924 window2
.get()->Hide();
926 GetWindowOrderAsString(backdrop
, window1
.get(), window2
.get(),
928 // Then delete window after window and see that everything is in order.
931 GetWindowOrderAsString(backdrop
, window1
.get(), window2
.get(),
935 GetWindowOrderAsString(backdrop
, window1
.get(), window2
.get(),
937 ShowTopWindowBackdrop(false);
939 GetWindowOrderAsString(NULL
, window1
.get(), window2
.get(),