4e6d1bd178eb90f5c7679dd9c5143b10553b1c55
[platform/framework/web/crosswalk.git] / src / ash / wm / workspace / workspace_layout_manager_unittest.cc
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.
4
5 #include "ash/wm/workspace/workspace_layout_manager.h"
6
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/window_state.h"
18 #include "ash/wm/window_util.h"
19 #include "ash/wm/workspace/workspace_window_resizer.h"
20 #include "base/basictypes.h"
21 #include "base/compiler_specific.h"
22 #include "ui/aura/client/aura_constants.h"
23 #include "ui/aura/root_window.h"
24 #include "ui/aura/test/test_windows.h"
25 #include "ui/aura/window.h"
26 #include "ui/base/ui_base_types.h"
27 #include "ui/gfx/insets.h"
28 #include "ui/gfx/screen.h"
29 #include "ui/views/corewm/window_util.h"
30 #include "ui/views/widget/widget.h"
31 #include "ui/views/widget/widget_delegate.h"
32
33 namespace ash {
34 namespace {
35
36 class MaximizeDelegateView : public views::WidgetDelegateView {
37  public:
38   MaximizeDelegateView(const gfx::Rect& initial_bounds)
39       : initial_bounds_(initial_bounds) {
40   }
41   virtual ~MaximizeDelegateView() {}
42
43   virtual bool GetSavedWindowPlacement(
44       const views::Widget* widget,
45       gfx::Rect* bounds,
46       ui::WindowShowState* show_state) const OVERRIDE {
47     *bounds = initial_bounds_;
48     *show_state = ui::SHOW_STATE_MAXIMIZED;
49     return true;
50   }
51
52  private:
53   const gfx::Rect initial_bounds_;
54
55   DISALLOW_COPY_AND_ASSIGN(MaximizeDelegateView);
56 };
57
58 class TestShellObserver : public ShellObserver {
59  public:
60   TestShellObserver() : call_count_(0),
61                         is_fullscreen_(false) {
62     Shell::GetInstance()->AddShellObserver(this);
63   }
64
65   virtual ~TestShellObserver() {
66     Shell::GetInstance()->RemoveShellObserver(this);
67   }
68
69   virtual void OnFullscreenStateChanged(bool is_fullscreen,
70                                         aura::Window* root_window) OVERRIDE {
71     call_count_++;
72     is_fullscreen_ = is_fullscreen;
73   }
74
75   int call_count() const {
76     return call_count_;
77   }
78
79   bool is_fullscreen() const {
80     return is_fullscreen_;
81   }
82
83  private:
84   int call_count_;
85   bool is_fullscreen_;
86
87   DISALLOW_COPY_AND_ASSIGN(TestShellObserver);
88 };
89
90 }  // namespace
91
92 typedef test::AshTestBase WorkspaceLayoutManagerTest;
93
94 // Verifies that a window containing a restore coordinate will be restored to
95 // to the size prior to minimize, keeping the restore rectangle in tact (if
96 // there is one).
97 TEST_F(WorkspaceLayoutManagerTest, RestoreFromMinimizeKeepsRestore) {
98   scoped_ptr<aura::Window> window(
99       CreateTestWindowInShellWithBounds(gfx::Rect(1, 2, 3, 4)));
100   gfx::Rect bounds(10, 15, 25, 35);
101   window->SetBounds(bounds);
102
103   wm::WindowState* window_state = wm::GetWindowState(window.get());
104
105   // This will not be used for un-minimizing window.
106   window_state->SetRestoreBoundsInScreen(gfx::Rect(0, 0, 100, 100));
107   window_state->Minimize();
108   window_state->Restore();
109   EXPECT_EQ("0,0 100x100", window_state->GetRestoreBoundsInScreen().ToString());
110   EXPECT_EQ("10,15 25x35", window.get()->bounds().ToString());
111
112   if (!SupportsMultipleDisplays())
113     return;
114
115   UpdateDisplay("400x300,500x400");
116   window->SetBoundsInScreen(gfx::Rect(600, 0, 100, 100),
117                             ScreenUtil::GetSecondaryDisplay());
118   EXPECT_EQ(Shell::GetAllRootWindows()[1], window->GetRootWindow());
119   window_state->Minimize();
120   // This will not be used for un-minimizing window.
121   window_state->SetRestoreBoundsInScreen(gfx::Rect(0, 0, 100, 100));
122   window_state->Restore();
123   EXPECT_EQ("600,0 100x100", window->GetBoundsInScreen().ToString());
124
125   // Make sure the unminimized window moves inside the display when
126   // 2nd display is disconnected.
127   window_state->Minimize();
128   UpdateDisplay("400x300");
129   window_state->Restore();
130   EXPECT_EQ(Shell::GetPrimaryRootWindow(), window->GetRootWindow());
131   EXPECT_TRUE(
132       Shell::GetPrimaryRootWindow()->bounds().Intersects(window->bounds()));
133 }
134
135 TEST_F(WorkspaceLayoutManagerTest, KeepMinimumVisibilityInDisplays) {
136   if (!SupportsMultipleDisplays())
137     return;
138
139   UpdateDisplay("300x400,400x500");
140   aura::Window::Windows root_windows = Shell::GetAllRootWindows();
141
142   DisplayLayout layout(DisplayLayout::TOP, 0);
143   Shell::GetInstance()->display_manager()->
144       SetLayoutForCurrentDisplays(layout);
145   EXPECT_EQ("0,-500 400x500", root_windows[1]->GetBoundsInScreen().ToString());
146
147   scoped_ptr<aura::Window> window1(
148       CreateTestWindowInShellWithBounds(gfx::Rect(10, -400, 200, 200)));
149   EXPECT_EQ("10,-400 200x200", window1->GetBoundsInScreen().ToString());
150
151   // Make sure the caption is visible.
152   scoped_ptr<aura::Window> window2(
153       CreateTestWindowInShellWithBounds(gfx::Rect(10, -600, 200, 200)));
154   EXPECT_EQ("10,-500 200x200", window2->GetBoundsInScreen().ToString());
155 }
156
157 TEST_F(WorkspaceLayoutManagerTest, KeepRestoredWindowInDisplay) {
158   if (!SupportsHostWindowResize())
159     return;
160   scoped_ptr<aura::Window> window(
161       CreateTestWindowInShellWithBounds(gfx::Rect(1, 2, 30, 40)));
162   wm::WindowState* window_state = wm::GetWindowState(window.get());
163
164   // Maximized -> Normal transition.
165   window_state->Maximize();
166   window_state->SetRestoreBoundsInScreen(gfx::Rect(-100, -100, 30, 40));
167   window_state->Restore();
168   EXPECT_TRUE(
169       Shell::GetPrimaryRootWindow()->bounds().Intersects(window->bounds()));
170   // Y bounds should not be negative.
171   EXPECT_EQ("-20,0 30x40", window->bounds().ToString());
172
173   // Minimized -> Normal transition.
174   window->SetBounds(gfx::Rect(-100, -100, 30, 40));
175   window_state->Minimize();
176   EXPECT_FALSE(
177       Shell::GetPrimaryRootWindow()->bounds().Intersects(window->bounds()));
178   EXPECT_EQ("-100,-100 30x40", window->bounds().ToString());
179   window->Show();
180   EXPECT_TRUE(
181       Shell::GetPrimaryRootWindow()->bounds().Intersects(window->bounds()));
182   // Y bounds should not be negative.
183   EXPECT_EQ("-20,0 30x40", window->bounds().ToString());
184
185   // Fullscreen -> Normal transition.
186   window->SetBounds(gfx::Rect(0, 0, 30, 40));  // reset bounds.
187   ASSERT_EQ("0,0 30x40", window->bounds().ToString());
188   window->SetProperty(aura::client::kShowStateKey, ui::SHOW_STATE_FULLSCREEN);
189   EXPECT_EQ(window->bounds(), window->GetRootWindow()->bounds());
190   window_state->SetRestoreBoundsInScreen(gfx::Rect(-100, -100, 30, 40));
191   window_state->Restore();
192   EXPECT_TRUE(
193       Shell::GetPrimaryRootWindow()->bounds().Intersects(window->bounds()));
194   // Y bounds should not be negative.
195   EXPECT_EQ("-20,0 30x40", window->bounds().ToString());
196 }
197
198 TEST_F(WorkspaceLayoutManagerTest, MaximizeInDisplayToBeRestored) {
199   if (!SupportsMultipleDisplays())
200     return;
201   UpdateDisplay("300x400,400x500");
202
203   aura::Window::Windows root_windows = Shell::GetAllRootWindows();
204
205   scoped_ptr<aura::Window> window(
206       CreateTestWindowInShellWithBounds(gfx::Rect(1, 2, 30, 40)));
207   EXPECT_EQ(root_windows[0], window->GetRootWindow());
208
209   wm::WindowState* window_state = wm::GetWindowState(window.get());
210   window_state->SetRestoreBoundsInScreen(gfx::Rect(400, 0, 30, 40));
211   // Maximize the window in 2nd display as the restore bounds
212   // is inside 2nd display.
213   window_state->Maximize();
214   EXPECT_EQ(root_windows[1], window->GetRootWindow());
215   EXPECT_EQ("300,0 400x453", window->GetBoundsInScreen().ToString());
216
217   window_state->Restore();
218   EXPECT_EQ(root_windows[1], window->GetRootWindow());
219   EXPECT_EQ("400,0 30x40", window->GetBoundsInScreen().ToString());
220
221   // If the restore bounds intersects with the current display,
222   // don't move.
223   window_state->SetRestoreBoundsInScreen(gfx::Rect(280, 0, 30, 40));
224   window_state->Maximize();
225   EXPECT_EQ(root_windows[1], window->GetRootWindow());
226   EXPECT_EQ("300,0 400x453", window->GetBoundsInScreen().ToString());
227
228   window_state->Restore();
229   EXPECT_EQ(root_windows[1], window->GetRootWindow());
230   EXPECT_EQ("280,0 30x40", window->GetBoundsInScreen().ToString());
231
232   // Restoring widget state.
233   scoped_ptr<views::Widget> w1(new views::Widget);
234   views::Widget::InitParams params;
235   params.ownership = views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET;
236   params.delegate = new MaximizeDelegateView(gfx::Rect(400, 0, 30, 40));
237   params.context = root_windows[0];
238   w1->Init(params);
239   w1->Show();
240   EXPECT_TRUE(w1->IsMaximized());
241   EXPECT_EQ(root_windows[1], w1->GetNativeView()->GetRootWindow());
242   EXPECT_EQ("300,0 400x453", w1->GetWindowBoundsInScreen().ToString());
243   w1->Restore();
244   EXPECT_EQ(root_windows[1], w1->GetNativeView()->GetRootWindow());
245   EXPECT_EQ("400,0 30x40", w1->GetWindowBoundsInScreen().ToString());
246 }
247
248 TEST_F(WorkspaceLayoutManagerTest, FullscreenInDisplayToBeRestored) {
249   if (!SupportsMultipleDisplays())
250     return;
251   UpdateDisplay("300x400,400x500");
252
253   aura::Window::Windows root_windows = Shell::GetAllRootWindows();
254
255   scoped_ptr<aura::Window> window(
256       CreateTestWindowInShellWithBounds(gfx::Rect(1, 2, 30, 40)));
257   EXPECT_EQ(root_windows[0], window->GetRootWindow());
258
259   wm::WindowState* window_state = wm::GetWindowState(window.get());
260   window_state->SetRestoreBoundsInScreen(gfx::Rect(400, 0, 30, 40));
261   // Maximize the window in 2nd display as the restore bounds
262   // is inside 2nd display.
263   window->SetProperty(aura::client::kShowStateKey,
264                       ui::SHOW_STATE_FULLSCREEN);
265   EXPECT_EQ(root_windows[1], window->GetRootWindow());
266   EXPECT_EQ("300,0 400x500", window->GetBoundsInScreen().ToString());
267
268   window_state->Restore();
269   EXPECT_EQ(root_windows[1], window->GetRootWindow());
270   EXPECT_EQ("400,0 30x40", window->GetBoundsInScreen().ToString());
271
272   // If the restore bounds intersects with the current display,
273   // don't move.
274   window_state->SetRestoreBoundsInScreen(gfx::Rect(280, 0, 30, 40));
275   window->SetProperty(aura::client::kShowStateKey,
276                       ui::SHOW_STATE_FULLSCREEN);
277   EXPECT_EQ(root_windows[1], window->GetRootWindow());
278   EXPECT_EQ("300,0 400x500", window->GetBoundsInScreen().ToString());
279
280   window_state->Restore();
281   EXPECT_EQ(root_windows[1], window->GetRootWindow());
282   EXPECT_EQ("280,0 30x40", window->GetBoundsInScreen().ToString());
283 }
284
285 // WindowObserver implementation used by DontClobberRestoreBoundsWindowObserver.
286 // This code mirrors what BrowserFrameAsh does. In particular when this code
287 // sees the window was maximized it changes the bounds of a secondary
288 // window. The secondary window mirrors the status window.
289 class DontClobberRestoreBoundsWindowObserver : public aura::WindowObserver {
290  public:
291   DontClobberRestoreBoundsWindowObserver() : window_(NULL) {}
292
293   void set_window(aura::Window* window) { window_ = window; }
294
295   virtual void OnWindowPropertyChanged(aura::Window* window,
296                                        const void* key,
297                                        intptr_t old) OVERRIDE {
298     if (!window_)
299       return;
300
301     if (wm::GetWindowState(window)->IsMaximized()) {
302       aura::Window* w = window_;
303       window_ = NULL;
304
305       gfx::Rect shelf_bounds(Shell::GetPrimaryRootWindowController()->
306                              GetShelfLayoutManager()->GetIdealBounds());
307       const gfx::Rect& window_bounds(w->bounds());
308       w->SetBounds(gfx::Rect(window_bounds.x(), shelf_bounds.y() - 1,
309                              window_bounds.width(), window_bounds.height()));
310     }
311   }
312
313  private:
314   aura::Window* window_;
315
316   DISALLOW_COPY_AND_ASSIGN(DontClobberRestoreBoundsWindowObserver);
317 };
318
319 // Creates a window, maximized the window and from within the maximized
320 // notification sets the bounds of a window to overlap the shelf. Verifies this
321 // doesn't effect the restore bounds.
322 TEST_F(WorkspaceLayoutManagerTest, DontClobberRestoreBounds) {
323   DontClobberRestoreBoundsWindowObserver window_observer;
324   scoped_ptr<aura::Window> window(new aura::Window(NULL));
325   window->SetType(ui::wm::WINDOW_TYPE_NORMAL);
326   window->Init(aura::WINDOW_LAYER_TEXTURED);
327   window->SetBounds(gfx::Rect(10, 20, 30, 40));
328   // NOTE: for this test to exercise the failure the observer needs to be added
329   // before the parent set. This mimics what BrowserFrameAsh does.
330   window->AddObserver(&window_observer);
331   ParentWindowInPrimaryRootWindow(window.get());
332   window->Show();
333
334   wm::WindowState* window_state = wm::GetWindowState(window.get());
335   window_state->Activate();
336
337   scoped_ptr<aura::Window> window2(
338       CreateTestWindowInShellWithBounds(gfx::Rect(12, 20, 30, 40)));
339   views::corewm::AddTransientChild(window.get(), window2.get());
340   window2->Show();
341
342   window_observer.set_window(window2.get());
343   window_state->Maximize();
344   EXPECT_EQ("10,20 30x40",
345             window_state->GetRestoreBoundsInScreen().ToString());
346   window->RemoveObserver(&window_observer);
347 }
348
349 // Verifies when a window is maximized all descendant windows have a size.
350 TEST_F(WorkspaceLayoutManagerTest, ChildBoundsResetOnMaximize) {
351   scoped_ptr<aura::Window> window(
352       CreateTestWindowInShellWithBounds(gfx::Rect(10, 20, 30, 40)));
353   window->Show();
354   wm::WindowState* window_state = wm::GetWindowState(window.get());
355   window_state->Activate();
356   scoped_ptr<aura::Window> child_window(
357       aura::test::CreateTestWindowWithBounds(gfx::Rect(5, 6, 7, 8),
358                                              window.get()));
359   child_window->Show();
360   window_state->Maximize();
361   EXPECT_EQ("5,6 7x8", child_window->bounds().ToString());
362 }
363
364 TEST_F(WorkspaceLayoutManagerTest, WindowShouldBeOnScreenWhenAdded) {
365   // Normal window bounds shouldn't be changed.
366   gfx::Rect window_bounds(100, 100, 200, 200);
367   scoped_ptr<aura::Window> window(
368       CreateTestWindowInShellWithBounds(window_bounds));
369   EXPECT_EQ(window_bounds, window->bounds());
370
371   // If the window is out of the workspace, it would be moved on screen.
372   gfx::Rect root_window_bounds =
373       Shell::GetInstance()->GetPrimaryRootWindow()->bounds();
374   window_bounds.Offset(root_window_bounds.width(), root_window_bounds.height());
375   ASSERT_FALSE(window_bounds.Intersects(root_window_bounds));
376   scoped_ptr<aura::Window> out_window(
377       CreateTestWindowInShellWithBounds(window_bounds));
378   EXPECT_EQ(window_bounds.size(), out_window->bounds().size());
379   gfx::Rect bounds = out_window->bounds();
380   bounds.Intersect(root_window_bounds);
381
382   // 30% of the window edge must be visible.
383   EXPECT_GT(bounds.width(), out_window->bounds().width() * 0.29);
384   EXPECT_GT(bounds.height(), out_window->bounds().height() * 0.29);
385
386   aura::Window* parent = out_window->parent();
387   parent->RemoveChild(out_window.get());
388   out_window->SetBounds(gfx::Rect(-200, -200, 200, 200));
389   // UserHasChangedWindowPositionOrSize flag shouldn't turn off this behavior.
390   wm::GetWindowState(window.get())->set_bounds_changed_by_user(true);
391   parent->AddChild(out_window.get());
392   EXPECT_GT(bounds.width(), out_window->bounds().width() * 0.29);
393   EXPECT_GT(bounds.height(), out_window->bounds().height() * 0.29);
394
395   // Make sure we always make more than 1/3 of the window edge visible even
396   // if the initial bounds intersects with display.
397   window_bounds.SetRect(-150, -150, 200, 200);
398   bounds = window_bounds;
399   bounds.Intersect(root_window_bounds);
400
401   // Make sure that the initial bounds' visible area is less than 26%
402   // so that the auto adjustment logic kicks in.
403   ASSERT_LT(bounds.width(), out_window->bounds().width() * 0.26);
404   ASSERT_LT(bounds.height(), out_window->bounds().height() * 0.26);
405   ASSERT_TRUE(window_bounds.Intersects(root_window_bounds));
406
407   scoped_ptr<aura::Window> partially_out_window(
408       CreateTestWindowInShellWithBounds(window_bounds));
409   EXPECT_EQ(window_bounds.size(), partially_out_window->bounds().size());
410   bounds = partially_out_window->bounds();
411   bounds.Intersect(root_window_bounds);
412   EXPECT_GT(bounds.width(), out_window->bounds().width() * 0.29);
413   EXPECT_GT(bounds.height(), out_window->bounds().height() * 0.29);
414
415   // Make sure the window whose 30% width/height is bigger than display
416   // will be placed correctly.
417   window_bounds.SetRect(-1900, -1900, 3000, 3000);
418   scoped_ptr<aura::Window> window_bigger_than_display(
419       CreateTestWindowInShellWithBounds(window_bounds));
420   EXPECT_GE(root_window_bounds.width(),
421             window_bigger_than_display->bounds().width());
422   EXPECT_GE(root_window_bounds.height(),
423             window_bigger_than_display->bounds().height());
424
425   bounds = window_bigger_than_display->bounds();
426   bounds.Intersect(root_window_bounds);
427   EXPECT_GT(bounds.width(), out_window->bounds().width() * 0.29);
428   EXPECT_GT(bounds.height(), out_window->bounds().height() * 0.29);
429 }
430
431 // Verifies the size of a window is enforced to be smaller than the work area.
432 TEST_F(WorkspaceLayoutManagerTest, SizeToWorkArea) {
433   // Normal window bounds shouldn't be changed.
434   gfx::Size work_area(
435       Shell::GetScreen()->GetPrimaryDisplay().work_area().size());
436   const gfx::Rect window_bounds(
437       100, 101, work_area.width() + 1, work_area.height() + 2);
438   scoped_ptr<aura::Window> window(
439       CreateTestWindowInShellWithBounds(window_bounds));
440   EXPECT_EQ(gfx::Rect(gfx::Point(100, 101), work_area).ToString(),
441       window->bounds().ToString());
442
443   // Directly setting the bounds triggers a slightly different code path. Verify
444   // that too.
445   window->SetBounds(window_bounds);
446   EXPECT_EQ(gfx::Rect(gfx::Point(100, 101), work_area).ToString(),
447       window->bounds().ToString());
448 }
449
450 TEST_F(WorkspaceLayoutManagerTest, NotifyFullscreenChanges) {
451   TestShellObserver observer;
452   scoped_ptr<aura::Window> window1(
453       CreateTestWindowInShellWithBounds(gfx::Rect(1, 2, 30, 40)));
454   scoped_ptr<aura::Window> window2(
455       CreateTestWindowInShellWithBounds(gfx::Rect(1, 2, 30, 40)));
456   wm::WindowState* window_state1 = wm::GetWindowState(window1.get());
457   wm::WindowState* window_state2 = wm::GetWindowState(window2.get());
458   window_state2->Activate();
459
460   window_state2->ToggleFullscreen();
461   EXPECT_EQ(1, observer.call_count());
462   EXPECT_TRUE(observer.is_fullscreen());
463
464   // When window1 moves to the front the fullscreen state should change.
465   window_state1->Activate();
466   EXPECT_EQ(2, observer.call_count());
467   EXPECT_FALSE(observer.is_fullscreen());
468
469   // It should change back if window2 becomes active again.
470   window_state2->Activate();
471   EXPECT_EQ(3, observer.call_count());
472   EXPECT_TRUE(observer.is_fullscreen());
473
474   window_state2->ToggleFullscreen();
475   EXPECT_EQ(4, observer.call_count());
476   EXPECT_FALSE(observer.is_fullscreen());
477
478   window_state2->ToggleFullscreen();
479   EXPECT_EQ(5, observer.call_count());
480   EXPECT_TRUE(observer.is_fullscreen());
481
482   // Closing the window should change the fullscreen state.
483   window2.reset();
484   EXPECT_EQ(6, observer.call_count());
485   EXPECT_FALSE(observer.is_fullscreen());
486 }
487
488 // Following tests were originally written for BaseLayoutManager.
489
490 namespace {
491
492 class WorkspaceLayoutManagerSoloTest : public test::AshTestBase {
493  public:
494   WorkspaceLayoutManagerSoloTest() {}
495   virtual ~WorkspaceLayoutManagerSoloTest() {}
496
497   virtual void SetUp() OVERRIDE {
498     test::AshTestBase::SetUp();
499     UpdateDisplay("800x600");
500     aura::Window* default_container = Shell::GetContainer(
501         Shell::GetPrimaryRootWindow(),
502         internal::kShellWindowId_DefaultContainer);
503     default_container->SetLayoutManager(new internal::WorkspaceLayoutManager(
504         Shell::GetPrimaryRootWindow()));
505   }
506
507   aura::Window* CreateTestWindow(const gfx::Rect& bounds) {
508     return CreateTestWindowInShellWithBounds(bounds);
509   }
510
511  private:
512   DISALLOW_COPY_AND_ASSIGN(WorkspaceLayoutManagerSoloTest);
513 };
514
515 }  // namespace
516
517 // Tests normal->maximize->normal.
518 TEST_F(WorkspaceLayoutManagerSoloTest, Maximize) {
519   gfx::Rect bounds(100, 100, 200, 200);
520   scoped_ptr<aura::Window> window(CreateTestWindow(bounds));
521   window->SetProperty(aura::client::kShowStateKey, ui::SHOW_STATE_MAXIMIZED);
522   // Maximized window fills the work area, not the whole display.
523   EXPECT_EQ(
524       ScreenUtil::GetMaximizedWindowBoundsInParent(window.get()).ToString(),
525       window->bounds().ToString());
526   window->SetProperty(aura::client::kShowStateKey, ui::SHOW_STATE_NORMAL);
527   EXPECT_EQ(bounds.ToString(), window->bounds().ToString());
528 }
529
530 // Tests normal->minimize->normal.
531 TEST_F(WorkspaceLayoutManagerSoloTest, Minimize) {
532   gfx::Rect bounds(100, 100, 200, 200);
533   scoped_ptr<aura::Window> window(CreateTestWindow(bounds));
534   window->SetProperty(aura::client::kShowStateKey, ui::SHOW_STATE_MINIMIZED);
535   // Note: Currently minimize doesn't do anything except set the state.
536   // See crbug.com/104571.
537   EXPECT_EQ(bounds.ToString(), window->bounds().ToString());
538   window->SetProperty(aura::client::kShowStateKey, ui::SHOW_STATE_NORMAL);
539   EXPECT_EQ(bounds.ToString(), window->bounds().ToString());
540 }
541
542 // A WindowDelegate which sets the focus when the window
543 // becomes visible.
544 class FocusDelegate : public aura::test::TestWindowDelegate {
545  public:
546   FocusDelegate()
547       : window_(NULL),
548         show_state_(ui::SHOW_STATE_END) {
549   }
550   virtual ~FocusDelegate() {}
551
552   void set_window(aura::Window* window) { window_ = window; }
553
554   // aura::test::TestWindowDelegate overrides:
555   virtual void OnWindowTargetVisibilityChanged(bool visible) OVERRIDE {
556     if (window_) {
557       if (visible)
558         window_->Focus();
559       show_state_ = window_->GetProperty(aura::client::kShowStateKey);
560     }
561   }
562
563   ui::WindowShowState GetShowStateAndReset() {
564     ui::WindowShowState ret = show_state_;
565     show_state_ = ui::SHOW_STATE_END;
566     return ret;
567   }
568
569  private:
570   aura::Window* window_;
571   ui::WindowShowState show_state_;
572
573   DISALLOW_COPY_AND_ASSIGN(FocusDelegate);
574 };
575
576 // Make sure that the window's show state is correct in
577 // |WindowDelegate::OnWindowTargetVisibilityChanged|, and setting
578 // focus in this callback doesn't cause DCHECK error.  See
579 // crbug.com/168383.
580 TEST_F(WorkspaceLayoutManagerSoloTest, FocusDuringUnminimize) {
581   FocusDelegate delegate;
582   scoped_ptr<aura::Window> window(CreateTestWindowInShellWithDelegate(
583       &delegate, 0, gfx::Rect(100, 100, 100, 100)));
584   delegate.set_window(window.get());
585   window->SetProperty(aura::client::kShowStateKey, ui::SHOW_STATE_MINIMIZED);
586   EXPECT_FALSE(window->IsVisible());
587   EXPECT_EQ(ui::SHOW_STATE_MINIMIZED, delegate.GetShowStateAndReset());
588   window->Show();
589   EXPECT_TRUE(window->IsVisible());
590   EXPECT_EQ(ui::SHOW_STATE_NORMAL, delegate.GetShowStateAndReset());
591 }
592
593 // Tests maximized window size during root window resize.
594 TEST_F(WorkspaceLayoutManagerSoloTest, MaximizeRootWindowResize) {
595   gfx::Rect bounds(100, 100, 200, 200);
596   scoped_ptr<aura::Window> window(CreateTestWindow(bounds));
597   window->SetProperty(aura::client::kShowStateKey, ui::SHOW_STATE_MAXIMIZED);
598   gfx::Rect initial_work_area_bounds =
599       ScreenUtil::GetMaximizedWindowBoundsInParent(window.get());
600   EXPECT_EQ(initial_work_area_bounds.ToString(), window->bounds().ToString());
601   // Enlarge the root window.  We should still match the work area size.
602   UpdateDisplay("900x700");
603   EXPECT_EQ(
604       ScreenUtil::GetMaximizedWindowBoundsInParent(window.get()).ToString(),
605       window->bounds().ToString());
606   EXPECT_NE(
607       initial_work_area_bounds.ToString(),
608       ScreenUtil::GetMaximizedWindowBoundsInParent(window.get()).ToString());
609 }
610
611 // Tests normal->fullscreen->normal.
612 TEST_F(WorkspaceLayoutManagerSoloTest, Fullscreen) {
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_FULLSCREEN);
616   // Fullscreen window fills the whole display.
617   EXPECT_EQ(Shell::GetScreen()->GetDisplayNearestWindow(
618                 window.get()).bounds().ToString(),
619             window->bounds().ToString());
620   window->SetProperty(aura::client::kShowStateKey, ui::SHOW_STATE_NORMAL);
621   EXPECT_EQ(bounds.ToString(), window->bounds().ToString());
622 }
623
624 // Tests fullscreen window size during root window resize.
625 TEST_F(WorkspaceLayoutManagerSoloTest, FullscreenRootWindowResize) {
626   gfx::Rect bounds(100, 100, 200, 200);
627   scoped_ptr<aura::Window> window(CreateTestWindow(bounds));
628   // Fullscreen window fills the whole display.
629   window->SetProperty(aura::client::kShowStateKey, ui::SHOW_STATE_FULLSCREEN);
630   EXPECT_EQ(Shell::GetScreen()->GetDisplayNearestWindow(
631                 window.get()).bounds().ToString(),
632             window->bounds().ToString());
633   // Enlarge the root window.  We should still match the display size.
634   UpdateDisplay("800x600");
635   EXPECT_EQ(Shell::GetScreen()->GetDisplayNearestWindow(
636                 window.get()).bounds().ToString(),
637             window->bounds().ToString());
638 }
639
640 // Tests that when the screen gets smaller the windows aren't bigger than
641 // the screen.
642 TEST_F(WorkspaceLayoutManagerSoloTest, RootWindowResizeShrinksWindows) {
643   scoped_ptr<aura::Window> window(
644       CreateTestWindow(gfx::Rect(10, 20, 500, 400)));
645   gfx::Rect work_area = Shell::GetScreen()->GetDisplayNearestWindow(
646       window.get()).work_area();
647   // Invariant: Window is smaller than work area.
648   EXPECT_LE(window->bounds().width(), work_area.width());
649   EXPECT_LE(window->bounds().height(), work_area.height());
650
651   // Make the root window narrower than our window.
652   UpdateDisplay("300x400");
653   work_area = Shell::GetScreen()->GetDisplayNearestWindow(
654       window.get()).work_area();
655   EXPECT_LE(window->bounds().width(), work_area.width());
656   EXPECT_LE(window->bounds().height(), work_area.height());
657
658   // Make the root window shorter than our window.
659   UpdateDisplay("300x200");
660   work_area = Shell::GetScreen()->GetDisplayNearestWindow(
661       window.get()).work_area();
662   EXPECT_LE(window->bounds().width(), work_area.width());
663   EXPECT_LE(window->bounds().height(), work_area.height());
664
665   // Enlarging the root window does not change the window bounds.
666   gfx::Rect old_bounds = window->bounds();
667   UpdateDisplay("800x600");
668   EXPECT_EQ(old_bounds.width(), window->bounds().width());
669   EXPECT_EQ(old_bounds.height(), window->bounds().height());
670 }
671
672 // Tests that a maximized window with too-large restore bounds will be restored
673 // to smaller than the full work area.
674 TEST_F(WorkspaceLayoutManagerSoloTest, BoundsWithScreenEdgeVisible) {
675   // Create a window with bounds that fill the screen.
676   gfx::Rect bounds = Shell::GetScreen()->GetPrimaryDisplay().bounds();
677   scoped_ptr<aura::Window> window(CreateTestWindow(bounds));
678   // Maximize it, which writes the old bounds to restore bounds.
679   window->SetProperty(aura::client::kShowStateKey, ui::SHOW_STATE_MAXIMIZED);
680   // Restore it.
681   window->SetProperty(aura::client::kShowStateKey, ui::SHOW_STATE_NORMAL);
682   // It should have the default maximized window bounds, inset by the grid size.
683   int grid_size = internal::WorkspaceWindowResizer::kScreenEdgeInset;
684   gfx::Rect max_bounds =
685       ash::ScreenUtil::GetMaximizedWindowBoundsInParent(window.get());
686   max_bounds.Inset(grid_size, grid_size);
687   EXPECT_EQ(max_bounds.ToString(), window->bounds().ToString());
688 }
689
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());
695
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());
699
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());
704 }
705
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)));
709
710   wm::WindowState* window_state = wm::GetWindowState(window.get());
711   window_state->SetRestoreBoundsInParent(gfx::Rect(10, 11, 12, 13));
712
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());
716 }
717
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);
725
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());
735
736   // Show the window again. The window should be maximized, and the restore
737   // bounds should not change.
738   window->Show();
739   EXPECT_EQ(bounds.ToString(),
740             window_state->GetRestoreBoundsInParent().ToString());
741   EXPECT_TRUE(window_state->IsMaximized());
742
743   window_state->Restore();
744   EXPECT_EQ(bounds.ToString(), window->bounds().ToString());
745 }
746
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);
753   window->Show();
754
755   internal::ShelfLayoutManager* shelf =
756       internal::ShelfLayoutManager::ForShelf(window.get());
757   shelf->SetAutoHideBehavior(SHELF_AUTO_HIDE_BEHAVIOR_ALWAYS);
758
759   window->SetBounds(ScreenUtil::GetMaximizedWindowBoundsInParent(window.get()));
760   gfx::Rect window_bounds = window->bounds();
761   EXPECT_EQ(
762       ScreenUtil::GetMaximizedWindowBoundsInParent(window.get()).ToString(),
763       window_bounds.ToString());
764
765   Shell::GetInstance()->session_state_delegate()->LockScreen();
766   shelf->UpdateVisibilityState();
767   EXPECT_NE(
768       ScreenUtil::GetMaximizedWindowBoundsInParent(window.get()).ToString(),
769       window_bounds.ToString());
770
771   Shell::GetInstance()->session_state_delegate()->UnlockScreen();
772   shelf->UpdateVisibilityState();
773   EXPECT_EQ(window_bounds.ToString(), window->bounds().ToString());
774 }
775
776 }  // namespace ash