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_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(),
375 internal::kShellWindowId_DefaultContainer);
376 default_container->AddChild(window.get());
379 Shell::GetScreen()->GetPrimaryDisplay().work_area());
380 EXPECT_EQ(work_area.ToString(), window->GetBoundsInScreen().ToString());
383 TEST_F(WorkspaceLayoutManagerTest, WindowShouldBeOnScreenWhenAdded) {
384 // Normal window bounds shouldn't be changed.
385 gfx::Rect window_bounds(100, 100, 200, 200);
386 scoped_ptr<aura::Window> window(
387 CreateTestWindowInShellWithBounds(window_bounds));
388 EXPECT_EQ(window_bounds, window->bounds());
390 // If the window is out of the workspace, it would be moved on screen.
391 gfx::Rect root_window_bounds =
392 Shell::GetInstance()->GetPrimaryRootWindow()->bounds();
393 window_bounds.Offset(root_window_bounds.width(), root_window_bounds.height());
394 ASSERT_FALSE(window_bounds.Intersects(root_window_bounds));
395 scoped_ptr<aura::Window> out_window(
396 CreateTestWindowInShellWithBounds(window_bounds));
397 EXPECT_EQ(window_bounds.size(), out_window->bounds().size());
398 gfx::Rect bounds = out_window->bounds();
399 bounds.Intersect(root_window_bounds);
401 // 30% of the window edge must be visible.
402 EXPECT_GT(bounds.width(), out_window->bounds().width() * 0.29);
403 EXPECT_GT(bounds.height(), out_window->bounds().height() * 0.29);
405 aura::Window* parent = out_window->parent();
406 parent->RemoveChild(out_window.get());
407 out_window->SetBounds(gfx::Rect(-200, -200, 200, 200));
408 // UserHasChangedWindowPositionOrSize flag shouldn't turn off this behavior.
409 wm::GetWindowState(window.get())->set_bounds_changed_by_user(true);
410 parent->AddChild(out_window.get());
411 EXPECT_GT(bounds.width(), out_window->bounds().width() * 0.29);
412 EXPECT_GT(bounds.height(), out_window->bounds().height() * 0.29);
414 // Make sure we always make more than 1/3 of the window edge visible even
415 // if the initial bounds intersects with display.
416 window_bounds.SetRect(-150, -150, 200, 200);
417 bounds = window_bounds;
418 bounds.Intersect(root_window_bounds);
420 // Make sure that the initial bounds' visible area is less than 26%
421 // so that the auto adjustment logic kicks in.
422 ASSERT_LT(bounds.width(), out_window->bounds().width() * 0.26);
423 ASSERT_LT(bounds.height(), out_window->bounds().height() * 0.26);
424 ASSERT_TRUE(window_bounds.Intersects(root_window_bounds));
426 scoped_ptr<aura::Window> partially_out_window(
427 CreateTestWindowInShellWithBounds(window_bounds));
428 EXPECT_EQ(window_bounds.size(), partially_out_window->bounds().size());
429 bounds = partially_out_window->bounds();
430 bounds.Intersect(root_window_bounds);
431 EXPECT_GT(bounds.width(), out_window->bounds().width() * 0.29);
432 EXPECT_GT(bounds.height(), out_window->bounds().height() * 0.29);
434 // Make sure the window whose 30% width/height is bigger than display
435 // will be placed correctly.
436 window_bounds.SetRect(-1900, -1900, 3000, 3000);
437 scoped_ptr<aura::Window> window_bigger_than_display(
438 CreateTestWindowInShellWithBounds(window_bounds));
439 EXPECT_GE(root_window_bounds.width(),
440 window_bigger_than_display->bounds().width());
441 EXPECT_GE(root_window_bounds.height(),
442 window_bigger_than_display->bounds().height());
444 bounds = window_bigger_than_display->bounds();
445 bounds.Intersect(root_window_bounds);
446 EXPECT_GT(bounds.width(), out_window->bounds().width() * 0.29);
447 EXPECT_GT(bounds.height(), out_window->bounds().height() * 0.29);
450 // Verifies the size of a window is enforced to be smaller than the work area.
451 TEST_F(WorkspaceLayoutManagerTest, SizeToWorkArea) {
452 // Normal window bounds shouldn't be changed.
454 Shell::GetScreen()->GetPrimaryDisplay().work_area().size());
455 const gfx::Rect window_bounds(
456 100, 101, work_area.width() + 1, work_area.height() + 2);
457 scoped_ptr<aura::Window> window(
458 CreateTestWindowInShellWithBounds(window_bounds));
459 EXPECT_EQ(gfx::Rect(gfx::Point(100, 101), work_area).ToString(),
460 window->bounds().ToString());
462 // Directly setting the bounds triggers a slightly different code path. Verify
464 window->SetBounds(window_bounds);
465 EXPECT_EQ(gfx::Rect(gfx::Point(100, 101), work_area).ToString(),
466 window->bounds().ToString());
469 TEST_F(WorkspaceLayoutManagerTest, NotifyFullscreenChanges) {
470 TestShellObserver observer;
471 scoped_ptr<aura::Window> window1(
472 CreateTestWindowInShellWithBounds(gfx::Rect(1, 2, 30, 40)));
473 scoped_ptr<aura::Window> window2(
474 CreateTestWindowInShellWithBounds(gfx::Rect(1, 2, 30, 40)));
475 wm::WindowState* window_state1 = wm::GetWindowState(window1.get());
476 wm::WindowState* window_state2 = wm::GetWindowState(window2.get());
477 window_state2->Activate();
479 const wm::WMEvent toggle_fullscreen_event(wm::WM_EVENT_TOGGLE_FULLSCREEN);
480 window_state2->OnWMEvent(&toggle_fullscreen_event);
481 EXPECT_EQ(1, observer.call_count());
482 EXPECT_TRUE(observer.is_fullscreen());
484 // When window1 moves to the front the fullscreen state should change.
485 window_state1->Activate();
486 EXPECT_EQ(2, observer.call_count());
487 EXPECT_FALSE(observer.is_fullscreen());
489 // It should change back if window2 becomes active again.
490 window_state2->Activate();
491 EXPECT_EQ(3, observer.call_count());
492 EXPECT_TRUE(observer.is_fullscreen());
494 window_state2->OnWMEvent(&toggle_fullscreen_event);
495 EXPECT_EQ(4, observer.call_count());
496 EXPECT_FALSE(observer.is_fullscreen());
498 window_state2->OnWMEvent(&toggle_fullscreen_event);
499 EXPECT_EQ(5, observer.call_count());
500 EXPECT_TRUE(observer.is_fullscreen());
502 // Closing the window should change the fullscreen state.
504 EXPECT_EQ(6, observer.call_count());
505 EXPECT_FALSE(observer.is_fullscreen());
508 // Following tests were originally written for BaseLayoutManager.
512 class WorkspaceLayoutManagerSoloTest : public test::AshTestBase {
514 WorkspaceLayoutManagerSoloTest() {}
515 virtual ~WorkspaceLayoutManagerSoloTest() {}
517 virtual void SetUp() OVERRIDE {
518 test::AshTestBase::SetUp();
519 UpdateDisplay("800x600");
520 aura::Window* default_container = Shell::GetContainer(
521 Shell::GetPrimaryRootWindow(),
522 internal::kShellWindowId_DefaultContainer);
523 default_container->SetLayoutManager(new internal::WorkspaceLayoutManager(
524 Shell::GetPrimaryRootWindow()));
527 aura::Window* CreateTestWindow(const gfx::Rect& bounds) {
528 return CreateTestWindowInShellWithBounds(bounds);
532 DISALLOW_COPY_AND_ASSIGN(WorkspaceLayoutManagerSoloTest);
537 // Tests normal->maximize->normal.
538 TEST_F(WorkspaceLayoutManagerSoloTest, Maximize) {
539 gfx::Rect bounds(100, 100, 200, 200);
540 scoped_ptr<aura::Window> window(CreateTestWindow(bounds));
541 window->SetProperty(aura::client::kShowStateKey, ui::SHOW_STATE_MAXIMIZED);
542 // Maximized window fills the work area, not the whole display.
544 ScreenUtil::GetMaximizedWindowBoundsInParent(window.get()).ToString(),
545 window->bounds().ToString());
546 window->SetProperty(aura::client::kShowStateKey, ui::SHOW_STATE_NORMAL);
547 EXPECT_EQ(bounds.ToString(), window->bounds().ToString());
550 // Tests normal->minimize->normal.
551 TEST_F(WorkspaceLayoutManagerSoloTest, Minimize) {
552 gfx::Rect bounds(100, 100, 200, 200);
553 scoped_ptr<aura::Window> window(CreateTestWindow(bounds));
554 window->SetProperty(aura::client::kShowStateKey, ui::SHOW_STATE_MINIMIZED);
555 // Note: Currently minimize doesn't do anything except set the state.
556 // See crbug.com/104571.
557 EXPECT_EQ(bounds.ToString(), window->bounds().ToString());
558 window->SetProperty(aura::client::kShowStateKey, ui::SHOW_STATE_NORMAL);
559 EXPECT_EQ(bounds.ToString(), window->bounds().ToString());
562 // A WindowDelegate which sets the focus when the window
564 class FocusDelegate : public aura::test::TestWindowDelegate {
568 show_state_(ui::SHOW_STATE_END) {
570 virtual ~FocusDelegate() {}
572 void set_window(aura::Window* window) { window_ = window; }
574 // aura::test::TestWindowDelegate overrides:
575 virtual void OnWindowTargetVisibilityChanged(bool visible) OVERRIDE {
579 show_state_ = window_->GetProperty(aura::client::kShowStateKey);
583 ui::WindowShowState GetShowStateAndReset() {
584 ui::WindowShowState ret = show_state_;
585 show_state_ = ui::SHOW_STATE_END;
590 aura::Window* window_;
591 ui::WindowShowState show_state_;
593 DISALLOW_COPY_AND_ASSIGN(FocusDelegate);
596 // Make sure that the window's show state is correct in
597 // |WindowDelegate::OnWindowTargetVisibilityChanged|, and setting
598 // focus in this callback doesn't cause DCHECK error. See
600 TEST_F(WorkspaceLayoutManagerSoloTest, FocusDuringUnminimize) {
601 FocusDelegate delegate;
602 scoped_ptr<aura::Window> window(CreateTestWindowInShellWithDelegate(
603 &delegate, 0, gfx::Rect(100, 100, 100, 100)));
604 delegate.set_window(window.get());
605 window->SetProperty(aura::client::kShowStateKey, ui::SHOW_STATE_MINIMIZED);
606 EXPECT_FALSE(window->IsVisible());
607 EXPECT_EQ(ui::SHOW_STATE_MINIMIZED, delegate.GetShowStateAndReset());
609 EXPECT_TRUE(window->IsVisible());
610 EXPECT_EQ(ui::SHOW_STATE_NORMAL, delegate.GetShowStateAndReset());
613 // Tests maximized window size during root window resize.
614 TEST_F(WorkspaceLayoutManagerSoloTest, MaximizeRootWindowResize) {
615 gfx::Rect bounds(100, 100, 200, 200);
616 scoped_ptr<aura::Window> window(CreateTestWindow(bounds));
617 window->SetProperty(aura::client::kShowStateKey, ui::SHOW_STATE_MAXIMIZED);
618 gfx::Rect initial_work_area_bounds =
619 ScreenUtil::GetMaximizedWindowBoundsInParent(window.get());
620 EXPECT_EQ(initial_work_area_bounds.ToString(), window->bounds().ToString());
621 // Enlarge the root window. We should still match the work area size.
622 UpdateDisplay("900x700");
624 ScreenUtil::GetMaximizedWindowBoundsInParent(window.get()).ToString(),
625 window->bounds().ToString());
627 initial_work_area_bounds.ToString(),
628 ScreenUtil::GetMaximizedWindowBoundsInParent(window.get()).ToString());
631 // Tests normal->fullscreen->normal.
632 TEST_F(WorkspaceLayoutManagerSoloTest, Fullscreen) {
633 gfx::Rect bounds(100, 100, 200, 200);
634 scoped_ptr<aura::Window> window(CreateTestWindow(bounds));
635 window->SetProperty(aura::client::kShowStateKey, ui::SHOW_STATE_FULLSCREEN);
636 // Fullscreen window fills the whole display.
637 EXPECT_EQ(Shell::GetScreen()->GetDisplayNearestWindow(
638 window.get()).bounds().ToString(),
639 window->bounds().ToString());
640 window->SetProperty(aura::client::kShowStateKey, ui::SHOW_STATE_NORMAL);
641 EXPECT_EQ(bounds.ToString(), window->bounds().ToString());
644 // Tests fullscreen window size during root window resize.
645 TEST_F(WorkspaceLayoutManagerSoloTest, FullscreenRootWindowResize) {
646 gfx::Rect bounds(100, 100, 200, 200);
647 scoped_ptr<aura::Window> window(CreateTestWindow(bounds));
648 // Fullscreen window fills the whole display.
649 window->SetProperty(aura::client::kShowStateKey, ui::SHOW_STATE_FULLSCREEN);
650 EXPECT_EQ(Shell::GetScreen()->GetDisplayNearestWindow(
651 window.get()).bounds().ToString(),
652 window->bounds().ToString());
653 // Enlarge the root window. We should still match the display size.
654 UpdateDisplay("800x600");
655 EXPECT_EQ(Shell::GetScreen()->GetDisplayNearestWindow(
656 window.get()).bounds().ToString(),
657 window->bounds().ToString());
660 // Tests that when the screen gets smaller the windows aren't bigger than
662 TEST_F(WorkspaceLayoutManagerSoloTest, RootWindowResizeShrinksWindows) {
663 scoped_ptr<aura::Window> window(
664 CreateTestWindow(gfx::Rect(10, 20, 500, 400)));
665 gfx::Rect work_area = Shell::GetScreen()->GetDisplayNearestWindow(
666 window.get()).work_area();
667 // Invariant: Window is smaller than work area.
668 EXPECT_LE(window->bounds().width(), work_area.width());
669 EXPECT_LE(window->bounds().height(), work_area.height());
671 // Make the root window narrower than our window.
672 UpdateDisplay("300x400");
673 work_area = Shell::GetScreen()->GetDisplayNearestWindow(
674 window.get()).work_area();
675 EXPECT_LE(window->bounds().width(), work_area.width());
676 EXPECT_LE(window->bounds().height(), work_area.height());
678 // Make the root window shorter than our window.
679 UpdateDisplay("300x200");
680 work_area = Shell::GetScreen()->GetDisplayNearestWindow(
681 window.get()).work_area();
682 EXPECT_LE(window->bounds().width(), work_area.width());
683 EXPECT_LE(window->bounds().height(), work_area.height());
685 // Enlarging the root window does not change the window bounds.
686 gfx::Rect old_bounds = window->bounds();
687 UpdateDisplay("800x600");
688 EXPECT_EQ(old_bounds.width(), window->bounds().width());
689 EXPECT_EQ(old_bounds.height(), window->bounds().height());
692 // Verifies maximizing sets the restore bounds, and restoring
693 // restores the bounds.
694 TEST_F(WorkspaceLayoutManagerSoloTest, MaximizeSetsRestoreBounds) {
695 scoped_ptr<aura::Window> window(CreateTestWindow(gfx::Rect(10, 20, 30, 40)));
696 wm::WindowState* window_state = wm::GetWindowState(window.get());
698 // Maximize it, which will keep the previous restore bounds.
699 window->SetProperty(aura::client::kShowStateKey, ui::SHOW_STATE_MAXIMIZED);
700 EXPECT_EQ("10,20 30x40", window_state->GetRestoreBoundsInParent().ToString());
702 // Restore it, which should restore bounds and reset restore bounds.
703 window->SetProperty(aura::client::kShowStateKey, ui::SHOW_STATE_NORMAL);
704 EXPECT_EQ("10,20 30x40", window->bounds().ToString());
705 EXPECT_FALSE(window_state->HasRestoreBounds());
708 // Verifies maximizing keeps the restore bounds if set.
709 TEST_F(WorkspaceLayoutManagerSoloTest, MaximizeResetsRestoreBounds) {
710 scoped_ptr<aura::Window> window(CreateTestWindow(gfx::Rect(1, 2, 3, 4)));
712 wm::WindowState* window_state = wm::GetWindowState(window.get());
713 window_state->SetRestoreBoundsInParent(gfx::Rect(10, 11, 12, 13));
715 // Maximize it, which will keep the previous restore bounds.
716 window->SetProperty(aura::client::kShowStateKey, ui::SHOW_STATE_MAXIMIZED);
717 EXPECT_EQ("10,11 12x13", window_state->GetRestoreBoundsInParent().ToString());
720 // Verifies that the restore bounds do not get reset when restoring to a
721 // maximzied state from a minimized state.
722 TEST_F(WorkspaceLayoutManagerSoloTest,
723 BoundsAfterRestoringToMaximizeFromMinimize) {
724 scoped_ptr<aura::Window> window(CreateTestWindow(gfx::Rect(1, 2, 3, 4)));
725 gfx::Rect bounds(10, 15, 25, 35);
726 window->SetBounds(bounds);
728 wm::WindowState* window_state = wm::GetWindowState(window.get());
729 // Maximize it, which should reset restore bounds.
730 window_state->Maximize();
731 EXPECT_EQ(bounds.ToString(),
732 window_state->GetRestoreBoundsInParent().ToString());
733 // Minimize the window. The restore bounds should not change.
734 window_state->Minimize();
735 EXPECT_EQ(bounds.ToString(),
736 window_state->GetRestoreBoundsInParent().ToString());
738 // Show the window again. The window should be maximized, and the restore
739 // bounds should not change.
741 EXPECT_EQ(bounds.ToString(),
742 window_state->GetRestoreBoundsInParent().ToString());
743 EXPECT_TRUE(window_state->IsMaximized());
745 window_state->Restore();
746 EXPECT_EQ(bounds.ToString(), window->bounds().ToString());
749 // Verify if the window is not resized during screen lock. See: crbug.com/173127
750 TEST_F(WorkspaceLayoutManagerSoloTest, NotResizeWhenScreenIsLocked) {
751 SetCanLockScreen(true);
752 scoped_ptr<aura::Window> window(CreateTestWindow(gfx::Rect(1, 2, 3, 4)));
753 // window with AlwaysOnTop will be managed by BaseLayoutManager.
754 window->SetProperty(aura::client::kAlwaysOnTopKey, true);
757 internal::ShelfLayoutManager* shelf =
758 internal::ShelfLayoutManager::ForShelf(window.get());
759 shelf->SetAutoHideBehavior(SHELF_AUTO_HIDE_BEHAVIOR_ALWAYS);
761 window->SetBounds(ScreenUtil::GetMaximizedWindowBoundsInParent(window.get()));
762 gfx::Rect window_bounds = window->bounds();
764 ScreenUtil::GetMaximizedWindowBoundsInParent(window.get()).ToString(),
765 window_bounds.ToString());
767 Shell::GetInstance()->session_state_delegate()->LockScreen();
768 shelf->UpdateVisibilityState();
770 ScreenUtil::GetMaximizedWindowBoundsInParent(window.get()).ToString(),
771 window_bounds.ToString());
773 Shell::GetInstance()->session_state_delegate()->UnlockScreen();
774 shelf->UpdateVisibilityState();
775 EXPECT_EQ(window_bounds.ToString(), window->bounds().ToString());
778 // Following tests are written to test the backdrop functionality.
782 class WorkspaceLayoutManagerBackdropTest : public test::AshTestBase {
784 WorkspaceLayoutManagerBackdropTest() {}
785 virtual ~WorkspaceLayoutManagerBackdropTest() {}
787 virtual void SetUp() OVERRIDE {
788 test::AshTestBase::SetUp();
789 UpdateDisplay("800x600");
790 default_container_ = Shell::GetContainer(
791 Shell::GetPrimaryRootWindow(),
792 internal::kShellWindowId_DefaultContainer);
793 // We set the size to something smaller then the display to avoid resizing
794 // issues with the shelf.
795 default_container_->SetBounds(gfx::Rect(0, 0, 800, 500));
798 aura::Window* CreateTestWindow(const gfx::Rect& bounds) {
799 aura::Window* window = CreateTestWindowInShellWithBounds(bounds);
803 // Turn the top window back drop on / off.
804 void ShowTopWindowBackdrop(bool show) {
805 scoped_ptr<ash::internal::WorkspaceLayoutManagerDelegate> backdrop;
807 backdrop.reset(new ash::internal::WorkspaceBackdropDelegate(
808 default_container_));
810 (static_cast<internal::WorkspaceLayoutManager*>
811 (default_container_->layout_manager()))->SetMaximizeBackdropDelegate(
813 // Closing and / or opening can be a delayed operation.
814 base::MessageLoop::current()->RunUntilIdle();
817 // Return the default container.
818 aura::Window* default_container() { return default_container_; }
820 // Return the order of windows (top most first) as they are in the default
821 // container. If the window is visible it will be a big letter, otherwise a
822 // small one. The backdrop will be an X and unknown windows will be shown as
824 std::string GetWindowOrderAsString(aura::Window* backdrop,
829 for (int i = static_cast<int>(default_container()->children().size()) - 1;
834 if (default_container()->children()[i] == wa)
835 result += default_container()->children()[i]->IsVisible() ? "A" : "a";
836 else if (default_container()->children()[i] == wb)
837 result += default_container()->children()[i]->IsVisible() ? "B" : "b";
838 else if (default_container()->children()[i] == wc)
839 result += default_container()->children()[i]->IsVisible() ? "C" : "c";
840 else if (default_container()->children()[i] == backdrop)
841 result += default_container()->children()[i]->IsVisible() ? "X" : "x";
849 // The default container.
850 aura::Window* default_container_;
852 DISALLOW_COPY_AND_ASSIGN(WorkspaceLayoutManagerBackdropTest);
857 // Check that creating the BackDrop without destroying it does not lead into
859 TEST_F(WorkspaceLayoutManagerBackdropTest, BackdropCrashTest) {
860 ShowTopWindowBackdrop(true);
863 // Verify basic assumptions about the backdrop.
864 TEST_F(WorkspaceLayoutManagerBackdropTest, BasicBackdropTests) {
865 // Create a backdrop and see that there is one window (the backdrop) and
866 // that the size is the same as the default container as well as that it is
868 ShowTopWindowBackdrop(true);
869 ASSERT_EQ(1U, default_container()->children().size());
870 EXPECT_FALSE(default_container()->children()[0]->IsVisible());
873 // Add a window and make sure that the backdrop is the second child.
874 scoped_ptr<aura::Window> window(CreateTestWindow(gfx::Rect(1, 2, 3, 4)));
876 ASSERT_EQ(2U, default_container()->children().size());
877 EXPECT_TRUE(default_container()->children()[0]->IsVisible());
878 EXPECT_TRUE(default_container()->children()[1]->IsVisible());
879 EXPECT_EQ(window.get(), default_container()->children()[1]);
880 EXPECT_EQ(default_container()->bounds().ToString(),
881 default_container()->children()[0]->bounds().ToString());
884 // With the window gone the backdrop should be invisible again.
885 ASSERT_EQ(1U, default_container()->children().size());
886 EXPECT_FALSE(default_container()->children()[0]->IsVisible());
888 // Destroying the Backdrop should empty the container.
889 ShowTopWindowBackdrop(false);
890 ASSERT_EQ(0U, default_container()->children().size());
893 // Verify that the backdrop gets properly created and placed.
894 TEST_F(WorkspaceLayoutManagerBackdropTest, VerifyBackdropAndItsStacking) {
895 scoped_ptr<aura::Window> window1(CreateTestWindow(gfx::Rect(1, 2, 3, 4)));
898 // Get the default container and check that only a single window is in there.
899 ASSERT_EQ(1U, default_container()->children().size());
900 EXPECT_EQ(window1.get(), default_container()->children()[0]);
901 EXPECT_EQ("A", GetWindowOrderAsString(NULL, window1.get(), NULL, NULL));
903 // Create 2 more windows and check that they are also in the container.
904 scoped_ptr<aura::Window> window2(CreateTestWindow(gfx::Rect(10, 2, 3, 4)));
905 scoped_ptr<aura::Window> window3(CreateTestWindow(gfx::Rect(20, 2, 3, 4)));
909 aura::Window* backdrop = NULL;
911 GetWindowOrderAsString(backdrop, window1.get(), window2.get(),
914 // Turn on the backdrop mode and check that the window shows up where it
915 // should be (second highest number).
916 ShowTopWindowBackdrop(true);
917 backdrop = default_container()->children()[2];
919 GetWindowOrderAsString(backdrop, window1.get(), window2.get(),
922 // Switch the order of windows and check that it still remains in that
924 default_container()->StackChildAtTop(window2.get());
926 GetWindowOrderAsString(backdrop, window1.get(), window2.get(),
929 // Make the top window invisible and check.
930 window2.get()->Hide();
932 GetWindowOrderAsString(backdrop, window1.get(), window2.get(),
934 // Then delete window after window and see that everything is in order.
937 GetWindowOrderAsString(backdrop, window1.get(), window2.get(),
941 GetWindowOrderAsString(backdrop, window1.get(), window2.get(),
943 ShowTopWindowBackdrop(false);
945 GetWindowOrderAsString(NULL, window1.get(), window2.get(),