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/shelf/shelf_layout_manager.h"
12 #include "ash/shelf/shelf_widget.h"
13 #include "ash/shell.h"
14 #include "ash/shell_observer.h"
15 #include "ash/test/ash_test_base.h"
16 #include "ash/wm/window_state.h"
17 #include "ash/wm/window_util.h"
18 #include "ui/aura/client/aura_constants.h"
19 #include "ui/aura/root_window.h"
20 #include "ui/aura/test/test_windows.h"
21 #include "ui/aura/window.h"
22 #include "ui/gfx/insets.h"
23 #include "ui/views/corewm/window_util.h"
24 #include "ui/views/widget/widget.h"
25 #include "ui/views/widget/widget_delegate.h"
30 class MaximizeDelegateView : public views::WidgetDelegateView {
32 MaximizeDelegateView(const gfx::Rect& initial_bounds)
33 : initial_bounds_(initial_bounds) {
35 virtual ~MaximizeDelegateView() {}
37 virtual bool GetSavedWindowPlacement(
38 const views::Widget* widget,
40 ui::WindowShowState* show_state) const OVERRIDE {
41 *bounds = initial_bounds_;
42 *show_state = ui::SHOW_STATE_MAXIMIZED;
47 const gfx::Rect initial_bounds_;
49 DISALLOW_COPY_AND_ASSIGN(MaximizeDelegateView);
52 class TestShellObserver : public ShellObserver {
54 TestShellObserver() : call_count_(0),
55 is_fullscreen_(false) {
56 Shell::GetInstance()->AddShellObserver(this);
59 virtual ~TestShellObserver() {
60 Shell::GetInstance()->RemoveShellObserver(this);
63 virtual void OnFullscreenStateChanged(bool is_fullscreen,
64 aura::Window* root_window) OVERRIDE {
66 is_fullscreen_ = is_fullscreen;
69 int call_count() const {
73 bool is_fullscreen() const {
74 return is_fullscreen_;
81 DISALLOW_COPY_AND_ASSIGN(TestShellObserver);
86 typedef test::AshTestBase WorkspaceLayoutManagerTest;
88 // Verifies that a window containing a restore coordinate will be restored to
89 // to the size prior to minimize, keeping the restore rectangle in tact (if
91 TEST_F(WorkspaceLayoutManagerTest, RestoreFromMinimizeKeepsRestore) {
92 scoped_ptr<aura::Window> window(
93 CreateTestWindowInShellWithBounds(gfx::Rect(1, 2, 3, 4)));
94 gfx::Rect bounds(10, 15, 25, 35);
95 window->SetBounds(bounds);
97 wm::WindowState* window_state = wm::GetWindowState(window.get());
99 // This will not be used for un-minimizing window.
100 window_state->SetRestoreBoundsInScreen(gfx::Rect(0, 0, 100, 100));
101 window_state->Minimize();
102 window_state->Restore();
103 EXPECT_EQ("0,0 100x100", window_state->GetRestoreBoundsInScreen().ToString());
104 EXPECT_EQ("10,15 25x35", window.get()->bounds().ToString());
106 if (!SupportsMultipleDisplays())
109 UpdateDisplay("400x300,500x400");
110 window->SetBoundsInScreen(gfx::Rect(600, 0, 100, 100),
111 ScreenUtil::GetSecondaryDisplay());
112 EXPECT_EQ(Shell::GetAllRootWindows()[1], window->GetRootWindow());
113 window_state->Minimize();
114 // This will not be used for un-minimizing window.
115 window_state->SetRestoreBoundsInScreen(gfx::Rect(0, 0, 100, 100));
116 window_state->Restore();
117 EXPECT_EQ("600,0 100x100", window->GetBoundsInScreen().ToString());
119 // Make sure the unminimized window moves inside the display when
120 // 2nd display is disconnected.
121 window_state->Minimize();
122 UpdateDisplay("400x300");
123 window_state->Restore();
124 EXPECT_EQ(Shell::GetPrimaryRootWindow(), window->GetRootWindow());
126 Shell::GetPrimaryRootWindow()->bounds().Intersects(window->bounds()));
129 TEST_F(WorkspaceLayoutManagerTest, KeepMinimumVisibilityInDisplays) {
130 if (!SupportsMultipleDisplays())
133 UpdateDisplay("300x400,400x500");
134 aura::Window::Windows root_windows = Shell::GetAllRootWindows();
136 DisplayLayout layout(DisplayLayout::TOP, 0);
137 Shell::GetInstance()->display_manager()->
138 SetLayoutForCurrentDisplays(layout);
139 EXPECT_EQ("0,-500 400x500", root_windows[1]->GetBoundsInScreen().ToString());
141 scoped_ptr<aura::Window> window1(
142 CreateTestWindowInShellWithBounds(gfx::Rect(10, -400, 200, 200)));
143 EXPECT_EQ("10,-400 200x200", window1->GetBoundsInScreen().ToString());
145 // Make sure the caption is visible.
146 scoped_ptr<aura::Window> window2(
147 CreateTestWindowInShellWithBounds(gfx::Rect(10, -600, 200, 200)));
148 EXPECT_EQ("10,-500 200x200", window2->GetBoundsInScreen().ToString());
151 TEST_F(WorkspaceLayoutManagerTest, KeepRestoredWindowInDisplay) {
152 if (!SupportsHostWindowResize())
154 scoped_ptr<aura::Window> window(
155 CreateTestWindowInShellWithBounds(gfx::Rect(1, 2, 30, 40)));
156 wm::WindowState* window_state = wm::GetWindowState(window.get());
158 // Maximized -> Normal transition.
159 window_state->Maximize();
160 window_state->SetRestoreBoundsInScreen(gfx::Rect(-100, -100, 30, 40));
161 window_state->Restore();
163 Shell::GetPrimaryRootWindow()->bounds().Intersects(window->bounds()));
164 // Y bounds should not be negative.
165 EXPECT_EQ("-20,0 30x40", window->bounds().ToString());
167 // Minimized -> Normal transition.
168 window->SetBounds(gfx::Rect(-100, -100, 30, 40));
169 window_state->Minimize();
171 Shell::GetPrimaryRootWindow()->bounds().Intersects(window->bounds()));
172 EXPECT_EQ("-100,-100 30x40", window->bounds().ToString());
175 Shell::GetPrimaryRootWindow()->bounds().Intersects(window->bounds()));
176 // Y bounds should not be negative.
177 EXPECT_EQ("-20,0 30x40", window->bounds().ToString());
179 // Fullscreen -> Normal transition.
180 window->SetBounds(gfx::Rect(0, 0, 30, 40)); // reset bounds.
181 ASSERT_EQ("0,0 30x40", window->bounds().ToString());
182 window->SetProperty(aura::client::kShowStateKey, ui::SHOW_STATE_FULLSCREEN);
183 EXPECT_EQ(window->bounds(), window->GetRootWindow()->bounds());
184 window_state->SetRestoreBoundsInScreen(gfx::Rect(-100, -100, 30, 40));
185 window_state->Restore();
187 Shell::GetPrimaryRootWindow()->bounds().Intersects(window->bounds()));
188 // Y bounds should not be negative.
189 EXPECT_EQ("-20,0 30x40", window->bounds().ToString());
192 TEST_F(WorkspaceLayoutManagerTest, MaximizeInDisplayToBeRestored) {
193 if (!SupportsMultipleDisplays())
195 UpdateDisplay("300x400,400x500");
197 aura::Window::Windows root_windows = Shell::GetAllRootWindows();
199 scoped_ptr<aura::Window> window(
200 CreateTestWindowInShellWithBounds(gfx::Rect(1, 2, 30, 40)));
201 EXPECT_EQ(root_windows[0], window->GetRootWindow());
203 wm::WindowState* window_state = wm::GetWindowState(window.get());
204 window_state->SetRestoreBoundsInScreen(gfx::Rect(400, 0, 30, 40));
205 // Maximize the window in 2nd display as the restore bounds
206 // is inside 2nd display.
207 window_state->Maximize();
208 EXPECT_EQ(root_windows[1], window->GetRootWindow());
209 EXPECT_EQ("300,0 400x453", window->GetBoundsInScreen().ToString());
211 window_state->Restore();
212 EXPECT_EQ(root_windows[1], window->GetRootWindow());
213 EXPECT_EQ("400,0 30x40", window->GetBoundsInScreen().ToString());
215 // If the restore bounds intersects with the current display,
217 window_state->SetRestoreBoundsInScreen(gfx::Rect(280, 0, 30, 40));
218 window_state->Maximize();
219 EXPECT_EQ(root_windows[1], window->GetRootWindow());
220 EXPECT_EQ("300,0 400x453", window->GetBoundsInScreen().ToString());
222 window_state->Restore();
223 EXPECT_EQ(root_windows[1], window->GetRootWindow());
224 EXPECT_EQ("280,0 30x40", window->GetBoundsInScreen().ToString());
226 // Restoring widget state.
227 scoped_ptr<views::Widget> w1(new views::Widget);
228 views::Widget::InitParams params;
229 params.ownership = views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET;
230 params.delegate = new MaximizeDelegateView(gfx::Rect(400, 0, 30, 40));
231 params.context = root_windows[0];
234 EXPECT_TRUE(w1->IsMaximized());
235 EXPECT_EQ(root_windows[1], w1->GetNativeView()->GetRootWindow());
236 EXPECT_EQ("300,0 400x453", w1->GetWindowBoundsInScreen().ToString());
238 EXPECT_EQ(root_windows[1], w1->GetNativeView()->GetRootWindow());
239 EXPECT_EQ("400,0 30x40", w1->GetWindowBoundsInScreen().ToString());
242 TEST_F(WorkspaceLayoutManagerTest, FullscreenInDisplayToBeRestored) {
243 if (!SupportsMultipleDisplays())
245 UpdateDisplay("300x400,400x500");
247 aura::Window::Windows root_windows = Shell::GetAllRootWindows();
249 scoped_ptr<aura::Window> window(
250 CreateTestWindowInShellWithBounds(gfx::Rect(1, 2, 30, 40)));
251 EXPECT_EQ(root_windows[0], window->GetRootWindow());
253 wm::WindowState* window_state = wm::GetWindowState(window.get());
254 window_state->SetRestoreBoundsInScreen(gfx::Rect(400, 0, 30, 40));
255 // Maximize the window in 2nd display as the restore bounds
256 // is inside 2nd display.
257 window->SetProperty(aura::client::kShowStateKey,
258 ui::SHOW_STATE_FULLSCREEN);
259 EXPECT_EQ(root_windows[1], window->GetRootWindow());
260 EXPECT_EQ("300,0 400x500", window->GetBoundsInScreen().ToString());
262 window_state->Restore();
263 EXPECT_EQ(root_windows[1], window->GetRootWindow());
264 EXPECT_EQ("400,0 30x40", window->GetBoundsInScreen().ToString());
266 // If the restore bounds intersects with the current display,
268 window_state->SetRestoreBoundsInScreen(gfx::Rect(280, 0, 30, 40));
269 window->SetProperty(aura::client::kShowStateKey,
270 ui::SHOW_STATE_FULLSCREEN);
271 EXPECT_EQ(root_windows[1], window->GetRootWindow());
272 EXPECT_EQ("300,0 400x500", window->GetBoundsInScreen().ToString());
274 window_state->Restore();
275 EXPECT_EQ(root_windows[1], window->GetRootWindow());
276 EXPECT_EQ("280,0 30x40", window->GetBoundsInScreen().ToString());
279 // WindowObserver implementation used by DontClobberRestoreBoundsWindowObserver.
280 // This code mirrors what BrowserFrameAsh does. In particular when this code
281 // sees the window was maximized it changes the bounds of a secondary
282 // window. The secondary window mirrors the status window.
283 class DontClobberRestoreBoundsWindowObserver : public aura::WindowObserver {
285 DontClobberRestoreBoundsWindowObserver() : window_(NULL) {}
287 void set_window(aura::Window* window) { window_ = window; }
289 virtual void OnWindowPropertyChanged(aura::Window* window,
291 intptr_t old) OVERRIDE {
295 if (wm::GetWindowState(window)->IsMaximized()) {
296 aura::Window* w = window_;
299 gfx::Rect shelf_bounds(Shell::GetPrimaryRootWindowController()->
300 GetShelfLayoutManager()->GetIdealBounds());
301 const gfx::Rect& window_bounds(w->bounds());
302 w->SetBounds(gfx::Rect(window_bounds.x(), shelf_bounds.y() - 1,
303 window_bounds.width(), window_bounds.height()));
308 aura::Window* window_;
310 DISALLOW_COPY_AND_ASSIGN(DontClobberRestoreBoundsWindowObserver);
313 // Creates a window, maximized the window and from within the maximized
314 // notification sets the bounds of a window to overlap the shelf. Verifies this
315 // doesn't effect the restore bounds.
316 TEST_F(WorkspaceLayoutManagerTest, DontClobberRestoreBounds) {
317 DontClobberRestoreBoundsWindowObserver window_observer;
318 scoped_ptr<aura::Window> window(new aura::Window(NULL));
319 window->SetType(ui::wm::WINDOW_TYPE_NORMAL);
320 window->Init(aura::WINDOW_LAYER_TEXTURED);
321 window->SetBounds(gfx::Rect(10, 20, 30, 40));
322 // NOTE: for this test to exercise the failure the observer needs to be added
323 // before the parent set. This mimics what BrowserFrameAsh does.
324 window->AddObserver(&window_observer);
325 ParentWindowInPrimaryRootWindow(window.get());
328 wm::WindowState* window_state = wm::GetWindowState(window.get());
329 window_state->Activate();
331 scoped_ptr<aura::Window> window2(
332 CreateTestWindowInShellWithBounds(gfx::Rect(12, 20, 30, 40)));
333 views::corewm::AddTransientChild(window.get(), window2.get());
336 window_observer.set_window(window2.get());
337 window_state->Maximize();
338 EXPECT_EQ("10,20 30x40",
339 window_state->GetRestoreBoundsInScreen().ToString());
340 window->RemoveObserver(&window_observer);
343 // Verifies when a window is maximized all descendant windows have a size.
344 TEST_F(WorkspaceLayoutManagerTest, ChildBoundsResetOnMaximize) {
345 scoped_ptr<aura::Window> window(
346 CreateTestWindowInShellWithBounds(gfx::Rect(10, 20, 30, 40)));
348 wm::WindowState* window_state = wm::GetWindowState(window.get());
349 window_state->Activate();
350 scoped_ptr<aura::Window> child_window(
351 aura::test::CreateTestWindowWithBounds(gfx::Rect(5, 6, 7, 8),
353 child_window->Show();
354 window_state->Maximize();
355 EXPECT_EQ("5,6 7x8", child_window->bounds().ToString());
358 TEST_F(WorkspaceLayoutManagerTest, WindowShouldBeOnScreenWhenAdded) {
359 // Normal window bounds shouldn't be changed.
360 gfx::Rect window_bounds(100, 100, 200, 200);
361 scoped_ptr<aura::Window> window(
362 CreateTestWindowInShellWithBounds(window_bounds));
363 EXPECT_EQ(window_bounds, window->bounds());
365 // If the window is out of the workspace, it would be moved on screen.
366 gfx::Rect root_window_bounds =
367 Shell::GetInstance()->GetPrimaryRootWindow()->bounds();
368 window_bounds.Offset(root_window_bounds.width(), root_window_bounds.height());
369 ASSERT_FALSE(window_bounds.Intersects(root_window_bounds));
370 scoped_ptr<aura::Window> out_window(
371 CreateTestWindowInShellWithBounds(window_bounds));
372 EXPECT_EQ(window_bounds.size(), out_window->bounds().size());
373 gfx::Rect bounds = out_window->bounds();
374 bounds.Intersect(root_window_bounds);
376 // 30% of the window edge must be visible.
377 EXPECT_GT(bounds.width(), out_window->bounds().width() * 0.29);
378 EXPECT_GT(bounds.height(), out_window->bounds().height() * 0.29);
380 aura::Window* parent = out_window->parent();
381 parent->RemoveChild(out_window.get());
382 out_window->SetBounds(gfx::Rect(-200, -200, 200, 200));
383 // UserHasChangedWindowPositionOrSize flag shouldn't turn off this behavior.
384 wm::GetWindowState(window.get())->set_bounds_changed_by_user(true);
385 parent->AddChild(out_window.get());
386 EXPECT_GT(bounds.width(), out_window->bounds().width() * 0.29);
387 EXPECT_GT(bounds.height(), out_window->bounds().height() * 0.29);
389 // Make sure we always make more than 1/3 of the window edge visible even
390 // if the initial bounds intersects with display.
391 window_bounds.SetRect(-150, -150, 200, 200);
392 bounds = window_bounds;
393 bounds.Intersect(root_window_bounds);
395 // Make sure that the initial bounds' visible area is less than 26%
396 // so that the auto adjustment logic kicks in.
397 ASSERT_LT(bounds.width(), out_window->bounds().width() * 0.26);
398 ASSERT_LT(bounds.height(), out_window->bounds().height() * 0.26);
399 ASSERT_TRUE(window_bounds.Intersects(root_window_bounds));
401 scoped_ptr<aura::Window> partially_out_window(
402 CreateTestWindowInShellWithBounds(window_bounds));
403 EXPECT_EQ(window_bounds.size(), partially_out_window->bounds().size());
404 bounds = partially_out_window->bounds();
405 bounds.Intersect(root_window_bounds);
406 EXPECT_GT(bounds.width(), out_window->bounds().width() * 0.29);
407 EXPECT_GT(bounds.height(), out_window->bounds().height() * 0.29);
409 // Make sure the window whose 30% width/height is bigger than display
410 // will be placed correctly.
411 window_bounds.SetRect(-1900, -1900, 3000, 3000);
412 scoped_ptr<aura::Window> window_bigger_than_display(
413 CreateTestWindowInShellWithBounds(window_bounds));
414 EXPECT_GE(root_window_bounds.width(),
415 window_bigger_than_display->bounds().width());
416 EXPECT_GE(root_window_bounds.height(),
417 window_bigger_than_display->bounds().height());
419 bounds = window_bigger_than_display->bounds();
420 bounds.Intersect(root_window_bounds);
421 EXPECT_GT(bounds.width(), out_window->bounds().width() * 0.29);
422 EXPECT_GT(bounds.height(), out_window->bounds().height() * 0.29);
425 // Verifies the size of a window is enforced to be smaller than the work area.
426 TEST_F(WorkspaceLayoutManagerTest, SizeToWorkArea) {
427 // Normal window bounds shouldn't be changed.
429 Shell::GetScreen()->GetPrimaryDisplay().work_area().size());
430 const gfx::Rect window_bounds(
431 100, 101, work_area.width() + 1, work_area.height() + 2);
432 scoped_ptr<aura::Window> window(
433 CreateTestWindowInShellWithBounds(window_bounds));
434 EXPECT_EQ(gfx::Rect(gfx::Point(100, 101), work_area).ToString(),
435 window->bounds().ToString());
437 // Directly setting the bounds triggers a slightly different code path. Verify
439 window->SetBounds(window_bounds);
440 EXPECT_EQ(gfx::Rect(gfx::Point(100, 101), work_area).ToString(),
441 window->bounds().ToString());
444 TEST_F(WorkspaceLayoutManagerTest, NotifyFullscreenChanges) {
445 TestShellObserver observer;
446 scoped_ptr<aura::Window> window1(
447 CreateTestWindowInShellWithBounds(gfx::Rect(1, 2, 30, 40)));
448 scoped_ptr<aura::Window> window2(
449 CreateTestWindowInShellWithBounds(gfx::Rect(1, 2, 30, 40)));
450 wm::WindowState* window_state1 = wm::GetWindowState(window1.get());
451 wm::WindowState* window_state2 = wm::GetWindowState(window2.get());
452 window_state2->Activate();
454 window_state2->ToggleFullscreen();
455 EXPECT_EQ(1, observer.call_count());
456 EXPECT_TRUE(observer.is_fullscreen());
458 // When window1 moves to the front the fullscreen state should change.
459 window_state1->Activate();
460 EXPECT_EQ(2, observer.call_count());
461 EXPECT_FALSE(observer.is_fullscreen());
463 // It should change back if window2 becomes active again.
464 window_state2->Activate();
465 EXPECT_EQ(3, observer.call_count());
466 EXPECT_TRUE(observer.is_fullscreen());
468 window_state2->ToggleFullscreen();
469 EXPECT_EQ(4, observer.call_count());
470 EXPECT_FALSE(observer.is_fullscreen());
472 window_state2->ToggleFullscreen();
473 EXPECT_EQ(5, observer.call_count());
474 EXPECT_TRUE(observer.is_fullscreen());
476 // Closing the window should change the fullscreen state.
478 EXPECT_EQ(6, observer.call_count());
479 EXPECT_FALSE(observer.is_fullscreen());