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/base_layout_manager.h"
7 #include "ash/screen_ash.h"
9 #include "ash/shell_window_ids.h"
10 #include "ash/test/ash_test_base.h"
11 #include "ash/wm/property_util.h"
12 #include "ash/wm/window_util.h"
13 #include "ash/wm/workspace/workspace_window_resizer.h"
14 #include "base/basictypes.h"
15 #include "base/compiler_specific.h"
16 #include "ui/aura/client/aura_constants.h"
17 #include "ui/aura/root_window.h"
18 #include "ui/aura/test/test_windows.h"
19 #include "ui/aura/window.h"
20 #include "ui/base/ui_base_types.h"
21 #include "ui/gfx/insets.h"
22 #include "ui/gfx/screen.h"
28 class BaseLayoutManagerTest
: public test::AshTestBase
{
30 BaseLayoutManagerTest() {}
31 virtual ~BaseLayoutManagerTest() {}
33 virtual void SetUp() OVERRIDE
{
34 test::AshTestBase::SetUp();
35 Shell::GetInstance()->SetDisplayWorkAreaInsets(
36 Shell::GetPrimaryRootWindow(),
37 gfx::Insets(1, 2, 3, 4));
38 Shell::GetPrimaryRootWindow()->SetHostSize(gfx::Size(800, 600));
39 aura::Window
* default_container
= Shell::GetContainer(
40 Shell::GetPrimaryRootWindow(),
41 internal::kShellWindowId_DefaultContainer
);
42 default_container
->SetLayoutManager(new internal::BaseLayoutManager(
43 Shell::GetPrimaryRootWindow()));
46 aura::Window
* CreateTestWindow(const gfx::Rect
& bounds
) {
47 return CreateTestWindowInShellWithBounds(bounds
);
51 DISALLOW_COPY_AND_ASSIGN(BaseLayoutManagerTest
);
54 // Tests normal->maximize->normal.
55 TEST_F(BaseLayoutManagerTest
, Maximize
) {
56 gfx::Rect
bounds(100, 100, 200, 200);
57 scoped_ptr
<aura::Window
> window(CreateTestWindow(bounds
));
58 window
->SetProperty(aura::client::kShowStateKey
, ui::SHOW_STATE_MAXIMIZED
);
59 // Maximized window fills the work area, not the whole display.
61 ScreenAsh::GetMaximizedWindowBoundsInParent(window
.get()).ToString(),
62 window
->bounds().ToString());
63 window
->SetProperty(aura::client::kShowStateKey
, ui::SHOW_STATE_NORMAL
);
64 EXPECT_EQ(bounds
.ToString(), window
->bounds().ToString());
67 // Tests normal->minimize->normal.
68 TEST_F(BaseLayoutManagerTest
, Minimize
) {
69 gfx::Rect
bounds(100, 100, 200, 200);
70 scoped_ptr
<aura::Window
> window(CreateTestWindow(bounds
));
71 window
->SetProperty(aura::client::kShowStateKey
, ui::SHOW_STATE_MINIMIZED
);
72 // Note: Currently minimize doesn't do anything except set the state.
73 // See crbug.com/104571.
74 EXPECT_EQ(bounds
.ToString(), window
->bounds().ToString());
75 window
->SetProperty(aura::client::kShowStateKey
, ui::SHOW_STATE_NORMAL
);
76 EXPECT_EQ(bounds
.ToString(), window
->bounds().ToString());
79 // A WindowDelegate which sets the focus when the window
81 class FocusDelegate
: public aura::test::TestWindowDelegate
{
85 show_state_(ui::SHOW_STATE_END
) {
87 virtual ~FocusDelegate() {}
89 void set_window(aura::Window
* window
) { window_
= window
; }
91 // aura::test::TestWindowDelegate overrides:
92 virtual void OnWindowTargetVisibilityChanged(bool visible
) OVERRIDE
{
96 show_state_
= window_
->GetProperty(aura::client::kShowStateKey
);
100 ui::WindowShowState
GetShowStateAndReset() {
101 ui::WindowShowState ret
= show_state_
;
102 show_state_
= ui::SHOW_STATE_END
;
107 aura::Window
* window_
;
108 ui::WindowShowState show_state_
;
110 DISALLOW_COPY_AND_ASSIGN(FocusDelegate
);
113 // Make sure that the window's show state is correct in
114 // |WindowDelegate::OnWindowTargetVisibilityChanged|, and setting
115 // focus in this callback doesn't cause DCHECK error. See
117 TEST_F(BaseLayoutManagerTest
, FocusDuringUnminimize
) {
118 FocusDelegate delegate
;
119 scoped_ptr
<aura::Window
> window(CreateTestWindowInShellWithDelegate(
120 &delegate
, 0, gfx::Rect(100, 100, 100, 100)));
121 delegate
.set_window(window
.get());
122 window
->SetProperty(aura::client::kShowStateKey
, ui::SHOW_STATE_MINIMIZED
);
123 EXPECT_FALSE(window
->IsVisible());
124 EXPECT_EQ(ui::SHOW_STATE_MINIMIZED
, delegate
.GetShowStateAndReset());
126 EXPECT_TRUE(window
->IsVisible());
127 EXPECT_EQ(ui::SHOW_STATE_DEFAULT
, delegate
.GetShowStateAndReset());
131 // RootWindow and Display can't resize on Windows Ash. http://crbug.com/165962
132 #define MAYBE_MaximizeRootWindowResize DISABLED_MaximizeRootWindowResize
134 #define MAYBE_MaximizeRootWindowResize MaximizeRootWindowResize
137 // Tests maximized window size during root window resize.
138 TEST_F(BaseLayoutManagerTest
, MAYBE_MaximizeRootWindowResize
) {
139 gfx::Rect
bounds(100, 100, 200, 200);
140 scoped_ptr
<aura::Window
> window(CreateTestWindow(bounds
));
141 window
->SetProperty(aura::client::kShowStateKey
, ui::SHOW_STATE_MAXIMIZED
);
142 gfx::Rect initial_work_area_bounds
=
143 ScreenAsh::GetMaximizedWindowBoundsInParent(window
.get());
144 EXPECT_EQ(initial_work_area_bounds
.ToString(), window
->bounds().ToString());
145 // Enlarge the root window. We should still match the work area size.
146 Shell::GetPrimaryRootWindow()->SetHostSize(gfx::Size(900, 700));
148 ScreenAsh::GetMaximizedWindowBoundsInParent(window
.get()).ToString(),
149 window
->bounds().ToString());
151 initial_work_area_bounds
.ToString(),
152 ScreenAsh::GetMaximizedWindowBoundsInParent(window
.get()).ToString());
155 // Tests normal->fullscreen->normal.
156 TEST_F(BaseLayoutManagerTest
, Fullscreen
) {
157 gfx::Rect
bounds(100, 100, 200, 200);
158 scoped_ptr
<aura::Window
> window(CreateTestWindow(bounds
));
159 window
->SetProperty(aura::client::kShowStateKey
, ui::SHOW_STATE_FULLSCREEN
);
160 // Fullscreen window fills the whole display.
161 EXPECT_EQ(Shell::GetScreen()->GetDisplayNearestWindow(
162 window
.get()).bounds().ToString(),
163 window
->bounds().ToString());
164 window
->SetProperty(aura::client::kShowStateKey
, ui::SHOW_STATE_NORMAL
);
165 EXPECT_EQ(bounds
.ToString(), window
->bounds().ToString());
168 // Tests fullscreen window size during root window resize.
169 TEST_F(BaseLayoutManagerTest
, FullscreenRootWindowResize
) {
170 gfx::Rect
bounds(100, 100, 200, 200);
171 scoped_ptr
<aura::Window
> window(CreateTestWindow(bounds
));
172 // Fullscreen window fills the whole display.
173 window
->SetProperty(aura::client::kShowStateKey
, ui::SHOW_STATE_FULLSCREEN
);
174 EXPECT_EQ(Shell::GetScreen()->GetDisplayNearestWindow(
175 window
.get()).bounds().ToString(),
176 window
->bounds().ToString());
177 // Enlarge the root window. We should still match the display size.
178 Shell::GetPrimaryRootWindow()->SetHostSize(gfx::Size(800, 600));
179 EXPECT_EQ(Shell::GetScreen()->GetDisplayNearestWindow(
180 window
.get()).bounds().ToString(),
181 window
->bounds().ToString());
184 // Fails on Mac only. Need to be implemented. http://crbug.com/111279.
185 #if defined(OS_MACOSX)
186 #define MAYBE_RootWindowResizeShrinksWindows \
187 DISABLED_RootWindowResizeShrinksWindows
189 #define MAYBE_RootWindowResizeShrinksWindows RootWindowResizeShrinksWindows
191 // Tests that when the screen gets smaller the windows aren't bigger than
193 TEST_F(BaseLayoutManagerTest
, MAYBE_RootWindowResizeShrinksWindows
) {
194 scoped_ptr
<aura::Window
> window(
195 CreateTestWindow(gfx::Rect(10, 20, 500, 400)));
196 gfx::Rect work_area
= Shell::GetScreen()->GetDisplayNearestWindow(
197 window
.get()).work_area();
198 // Invariant: Window is smaller than work area.
199 EXPECT_LE(window
->bounds().width(), work_area
.width());
200 EXPECT_LE(window
->bounds().height(), work_area
.height());
202 // Make the root window narrower than our window.
203 Shell::GetPrimaryRootWindow()->SetHostSize(gfx::Size(300, 400));
204 work_area
= Shell::GetScreen()->GetDisplayNearestWindow(
205 window
.get()).work_area();
206 EXPECT_LE(window
->bounds().width(), work_area
.width());
207 EXPECT_LE(window
->bounds().height(), work_area
.height());
209 // Make the root window shorter than our window.
210 Shell::GetPrimaryRootWindow()->SetHostSize(gfx::Size(300, 200));
211 work_area
= Shell::GetScreen()->GetDisplayNearestWindow(
212 window
.get()).work_area();
213 EXPECT_LE(window
->bounds().width(), work_area
.width());
214 EXPECT_LE(window
->bounds().height(), work_area
.height());
216 // Enlarging the root window does not change the window bounds.
217 gfx::Rect old_bounds
= window
->bounds();
218 Shell::GetPrimaryRootWindow()->SetHostSize(gfx::Size(800, 600));
219 EXPECT_EQ(old_bounds
.width(), window
->bounds().width());
220 EXPECT_EQ(old_bounds
.height(), window
->bounds().height());
223 // Tests that a maximized window with too-large restore bounds will be restored
224 // to smaller than the full work area.
225 TEST_F(BaseLayoutManagerTest
, BoundsWithScreenEdgeVisible
) {
226 // Create a window with bounds that fill the screen.
227 gfx::Rect bounds
= Shell::GetScreen()->GetPrimaryDisplay().bounds();
228 scoped_ptr
<aura::Window
> window(CreateTestWindow(bounds
));
229 // Maximize it, which writes the old bounds to restore bounds.
230 window
->SetProperty(aura::client::kShowStateKey
, ui::SHOW_STATE_MAXIMIZED
);
232 window
->SetProperty(aura::client::kShowStateKey
, ui::SHOW_STATE_NORMAL
);
233 // It should have the default maximized window bounds, inset by the grid size.
234 int grid_size
= internal::WorkspaceWindowResizer::kScreenEdgeInset
;
235 gfx::Rect max_bounds
=
236 ash::ScreenAsh::GetMaximizedWindowBoundsInParent(window
.get());
237 max_bounds
.Inset(grid_size
, grid_size
);
238 EXPECT_EQ(max_bounds
.ToString(), window
->bounds().ToString());
241 // Verifies maximizing sets the restore bounds, and restoring
242 // restores the bounds.
243 TEST_F(BaseLayoutManagerTest
, MaximizeSetsRestoreBounds
) {
244 scoped_ptr
<aura::Window
> window(CreateTestWindow(gfx::Rect(1, 2, 3, 4)));
246 // Maximize it, which will keep the previous restore bounds.
247 window
->SetProperty(aura::client::kShowStateKey
, ui::SHOW_STATE_MAXIMIZED
);
248 EXPECT_EQ("1,2 3x4", GetRestoreBoundsInParent(window
.get()).ToString());
250 // Restore it, which should restore bounds and reset restore bounds.
251 window
->SetProperty(aura::client::kShowStateKey
, ui::SHOW_STATE_NORMAL
);
252 EXPECT_EQ("1,2 3x4", window
->bounds().ToString());
253 EXPECT_TRUE(GetRestoreBoundsInScreen(window
.get()) == NULL
);
256 // Verifies maximizing keeps the restore bounds if set.
257 TEST_F(BaseLayoutManagerTest
, MaximizeResetsRestoreBounds
) {
258 scoped_ptr
<aura::Window
> window(CreateTestWindow(gfx::Rect(1, 2, 3, 4)));
259 SetRestoreBoundsInParent(window
.get(), gfx::Rect(10, 11, 12, 13));
261 // Maximize it, which will keep the previous restore bounds.
262 window
->SetProperty(aura::client::kShowStateKey
, ui::SHOW_STATE_MAXIMIZED
);
263 EXPECT_EQ("10,11 12x13", GetRestoreBoundsInParent(window
.get()).ToString());
266 // Verifies that the restore bounds do not get reset when restoring to a
267 // maximzied state from a minimized state.
268 TEST_F(BaseLayoutManagerTest
, BoundsAfterRestoringToMaximizeFromMinimize
) {
269 scoped_ptr
<aura::Window
> window(CreateTestWindow(gfx::Rect(1, 2, 3, 4)));
270 gfx::Rect
bounds(10, 15, 25, 35);
271 window
->SetBounds(bounds
);
273 // Maximize it, which should reset restore bounds.
274 wm::MaximizeWindow(window
.get());
275 EXPECT_EQ(bounds
.ToString(),
276 GetRestoreBoundsInParent(window
.get()).ToString());
278 // Minimize the window. The restore bounds should not change.
279 wm::MinimizeWindow(window
.get());
280 EXPECT_EQ(bounds
.ToString(),
281 GetRestoreBoundsInParent(window
.get()).ToString());
283 // Show the window again. The window should be maximized, and the restore
284 // bounds should not change.
286 EXPECT_EQ(bounds
.ToString(),
287 GetRestoreBoundsInParent(window
.get()).ToString());
288 EXPECT_TRUE(wm::IsWindowMaximized(window
.get()));
290 wm::RestoreWindow(window
.get());
291 EXPECT_EQ(bounds
.ToString(), window
->bounds().ToString());