- add sources.
[platform/framework/web/crosswalk.git] / src / ash / wm / base_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/base_layout_manager.h"
6
7 #include "ash/screen_ash.h"
8 #include "ash/session_state_delegate.h"
9 #include "ash/shelf/shelf_layout_manager.h"
10 #include "ash/shell.h"
11 #include "ash/shell_window_ids.h"
12 #include "ash/test/ash_test_base.h"
13 #include "ash/wm/window_state.h"
14 #include "ash/wm/window_util.h"
15 #include "ash/wm/workspace/workspace_window_resizer.h"
16 #include "base/basictypes.h"
17 #include "base/compiler_specific.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/base/ui_base_types.h"
23 #include "ui/gfx/insets.h"
24 #include "ui/gfx/screen.h"
25
26 namespace ash {
27
28 namespace {
29
30 class BaseLayoutManagerTest : public test::AshTestBase {
31  public:
32   BaseLayoutManagerTest() {}
33   virtual ~BaseLayoutManagerTest() {}
34
35   virtual void SetUp() OVERRIDE {
36     test::AshTestBase::SetUp();
37     UpdateDisplay("800x600");
38     aura::Window* default_container = Shell::GetContainer(
39         Shell::GetPrimaryRootWindow(),
40         internal::kShellWindowId_DefaultContainer);
41     default_container->SetLayoutManager(new internal::BaseLayoutManager(
42         Shell::GetPrimaryRootWindow()));
43   }
44
45   aura::Window* CreateTestWindow(const gfx::Rect& bounds) {
46     return CreateTestWindowInShellWithBounds(bounds);
47   }
48
49  private:
50   DISALLOW_COPY_AND_ASSIGN(BaseLayoutManagerTest);
51 };
52
53 // Tests normal->maximize->normal.
54 TEST_F(BaseLayoutManagerTest, Maximize) {
55   gfx::Rect bounds(100, 100, 200, 200);
56   scoped_ptr<aura::Window> window(CreateTestWindow(bounds));
57   window->SetProperty(aura::client::kShowStateKey, ui::SHOW_STATE_MAXIMIZED);
58   // Maximized window fills the work area, not the whole display.
59   EXPECT_EQ(
60       ScreenAsh::GetMaximizedWindowBoundsInParent(window.get()).ToString(),
61       window->bounds().ToString());
62   window->SetProperty(aura::client::kShowStateKey, ui::SHOW_STATE_NORMAL);
63   EXPECT_EQ(bounds.ToString(), window->bounds().ToString());
64 }
65
66 // Tests normal->minimize->normal.
67 TEST_F(BaseLayoutManagerTest, Minimize) {
68   gfx::Rect bounds(100, 100, 200, 200);
69   scoped_ptr<aura::Window> window(CreateTestWindow(bounds));
70   window->SetProperty(aura::client::kShowStateKey, ui::SHOW_STATE_MINIMIZED);
71   // Note: Currently minimize doesn't do anything except set the state.
72   // See crbug.com/104571.
73   EXPECT_EQ(bounds.ToString(), window->bounds().ToString());
74   window->SetProperty(aura::client::kShowStateKey, ui::SHOW_STATE_NORMAL);
75   EXPECT_EQ(bounds.ToString(), window->bounds().ToString());
76 }
77
78 // A WindowDelegate which sets the focus when the window
79 // becomes visible.
80 class FocusDelegate : public aura::test::TestWindowDelegate {
81  public:
82   FocusDelegate()
83       : window_(NULL),
84         show_state_(ui::SHOW_STATE_END) {
85   }
86   virtual ~FocusDelegate() {}
87
88   void set_window(aura::Window* window) { window_ = window; }
89
90   // aura::test::TestWindowDelegate overrides:
91   virtual void OnWindowTargetVisibilityChanged(bool visible) OVERRIDE {
92     if (window_) {
93       if (visible)
94         window_->Focus();
95       show_state_ = window_->GetProperty(aura::client::kShowStateKey);
96     }
97   }
98
99   ui::WindowShowState GetShowStateAndReset() {
100     ui::WindowShowState ret = show_state_;
101     show_state_ = ui::SHOW_STATE_END;
102     return ret;
103   }
104
105  private:
106   aura::Window* window_;
107   ui::WindowShowState show_state_;
108
109   DISALLOW_COPY_AND_ASSIGN(FocusDelegate);
110 };
111
112 // Make sure that the window's show state is correct in
113 // |WindowDelegate::OnWindowTargetVisibilityChanged|, and setting
114 // focus in this callback doesn't cause DCHECK error.  See
115 // crbug.com/168383.
116 TEST_F(BaseLayoutManagerTest, FocusDuringUnminimize) {
117   FocusDelegate delegate;
118   scoped_ptr<aura::Window> window(CreateTestWindowInShellWithDelegate(
119       &delegate, 0, gfx::Rect(100, 100, 100, 100)));
120   delegate.set_window(window.get());
121   window->SetProperty(aura::client::kShowStateKey, ui::SHOW_STATE_MINIMIZED);
122   EXPECT_FALSE(window->IsVisible());
123   EXPECT_EQ(ui::SHOW_STATE_MINIMIZED, delegate.GetShowStateAndReset());
124   window->Show();
125   EXPECT_TRUE(window->IsVisible());
126   EXPECT_EQ(ui::SHOW_STATE_DEFAULT, delegate.GetShowStateAndReset());
127 }
128
129 // Tests maximized window size during root window resize.
130 TEST_F(BaseLayoutManagerTest, MaximizeRootWindowResize) {
131   gfx::Rect bounds(100, 100, 200, 200);
132   scoped_ptr<aura::Window> window(CreateTestWindow(bounds));
133   window->SetProperty(aura::client::kShowStateKey, ui::SHOW_STATE_MAXIMIZED);
134   gfx::Rect initial_work_area_bounds =
135       ScreenAsh::GetMaximizedWindowBoundsInParent(window.get());
136   EXPECT_EQ(initial_work_area_bounds.ToString(), window->bounds().ToString());
137   // Enlarge the root window.  We should still match the work area size.
138   UpdateDisplay("900x700");
139   EXPECT_EQ(
140       ScreenAsh::GetMaximizedWindowBoundsInParent(window.get()).ToString(),
141       window->bounds().ToString());
142   EXPECT_NE(
143       initial_work_area_bounds.ToString(),
144       ScreenAsh::GetMaximizedWindowBoundsInParent(window.get()).ToString());
145 }
146
147 // Tests normal->fullscreen->normal.
148 TEST_F(BaseLayoutManagerTest, Fullscreen) {
149   gfx::Rect bounds(100, 100, 200, 200);
150   scoped_ptr<aura::Window> window(CreateTestWindow(bounds));
151   window->SetProperty(aura::client::kShowStateKey, ui::SHOW_STATE_FULLSCREEN);
152   // Fullscreen window fills the whole display.
153   EXPECT_EQ(Shell::GetScreen()->GetDisplayNearestWindow(
154                 window.get()).bounds().ToString(),
155             window->bounds().ToString());
156   window->SetProperty(aura::client::kShowStateKey, ui::SHOW_STATE_NORMAL);
157   EXPECT_EQ(bounds.ToString(), window->bounds().ToString());
158 }
159
160 // Tests fullscreen window size during root window resize.
161 TEST_F(BaseLayoutManagerTest, FullscreenRootWindowResize) {
162   gfx::Rect bounds(100, 100, 200, 200);
163   scoped_ptr<aura::Window> window(CreateTestWindow(bounds));
164   // Fullscreen window fills the whole display.
165   window->SetProperty(aura::client::kShowStateKey, ui::SHOW_STATE_FULLSCREEN);
166   EXPECT_EQ(Shell::GetScreen()->GetDisplayNearestWindow(
167                 window.get()).bounds().ToString(),
168             window->bounds().ToString());
169   // Enlarge the root window.  We should still match the display size.
170   UpdateDisplay("800x600");
171   EXPECT_EQ(Shell::GetScreen()->GetDisplayNearestWindow(
172                 window.get()).bounds().ToString(),
173             window->bounds().ToString());
174 }
175
176 // Tests that when the screen gets smaller the windows aren't bigger than
177 // the screen.
178 TEST_F(BaseLayoutManagerTest, RootWindowResizeShrinksWindows) {
179   scoped_ptr<aura::Window> window(
180       CreateTestWindow(gfx::Rect(10, 20, 500, 400)));
181   gfx::Rect work_area = Shell::GetScreen()->GetDisplayNearestWindow(
182       window.get()).work_area();
183   // Invariant: Window is smaller than work area.
184   EXPECT_LE(window->bounds().width(), work_area.width());
185   EXPECT_LE(window->bounds().height(), work_area.height());
186
187   // Make the root window narrower than our window.
188   UpdateDisplay("300x400");
189   work_area = Shell::GetScreen()->GetDisplayNearestWindow(
190       window.get()).work_area();
191   EXPECT_LE(window->bounds().width(), work_area.width());
192   EXPECT_LE(window->bounds().height(), work_area.height());
193
194   // Make the root window shorter than our window.
195   UpdateDisplay("300x200");
196   work_area = Shell::GetScreen()->GetDisplayNearestWindow(
197       window.get()).work_area();
198   EXPECT_LE(window->bounds().width(), work_area.width());
199   EXPECT_LE(window->bounds().height(), work_area.height());
200
201   // Enlarging the root window does not change the window bounds.
202   gfx::Rect old_bounds = window->bounds();
203   UpdateDisplay("800x600");
204   EXPECT_EQ(old_bounds.width(), window->bounds().width());
205   EXPECT_EQ(old_bounds.height(), window->bounds().height());
206 }
207
208 // Tests that a maximized window with too-large restore bounds will be restored
209 // to smaller than the full work area.
210 TEST_F(BaseLayoutManagerTest, BoundsWithScreenEdgeVisible) {
211   // Create a window with bounds that fill the screen.
212   gfx::Rect bounds = Shell::GetScreen()->GetPrimaryDisplay().bounds();
213   scoped_ptr<aura::Window> window(CreateTestWindow(bounds));
214   // Maximize it, which writes the old bounds to restore bounds.
215   window->SetProperty(aura::client::kShowStateKey, ui::SHOW_STATE_MAXIMIZED);
216   // Restore it.
217   window->SetProperty(aura::client::kShowStateKey, ui::SHOW_STATE_NORMAL);
218   // It should have the default maximized window bounds, inset by the grid size.
219   int grid_size = internal::WorkspaceWindowResizer::kScreenEdgeInset;
220   gfx::Rect max_bounds =
221       ash::ScreenAsh::GetMaximizedWindowBoundsInParent(window.get());
222   max_bounds.Inset(grid_size, grid_size);
223   EXPECT_EQ(max_bounds.ToString(), window->bounds().ToString());
224 }
225
226 // Verifies maximizing sets the restore bounds, and restoring
227 // restores the bounds.
228 TEST_F(BaseLayoutManagerTest, MaximizeSetsRestoreBounds) {
229   scoped_ptr<aura::Window> window(CreateTestWindow(gfx::Rect(1, 2, 3, 4)));
230   wm::WindowState* window_state = wm::GetWindowState(window.get());
231
232   // Maximize it, which will keep the previous restore bounds.
233   window->SetProperty(aura::client::kShowStateKey, ui::SHOW_STATE_MAXIMIZED);
234   EXPECT_EQ("1,2 3x4", window_state->GetRestoreBoundsInParent().ToString());
235
236   // Restore it, which should restore bounds and reset restore bounds.
237   window->SetProperty(aura::client::kShowStateKey, ui::SHOW_STATE_NORMAL);
238   EXPECT_EQ("1,2 3x4", window->bounds().ToString());
239   EXPECT_FALSE(window_state->HasRestoreBounds());
240 }
241
242 // Verifies maximizing keeps the restore bounds if set.
243 TEST_F(BaseLayoutManagerTest, MaximizeResetsRestoreBounds) {
244   scoped_ptr<aura::Window> window(CreateTestWindow(gfx::Rect(1, 2, 3, 4)));
245
246   wm::WindowState* window_state = wm::GetWindowState(window.get());
247   window_state->SetRestoreBoundsInParent(gfx::Rect(10, 11, 12, 13));
248
249   // Maximize it, which will keep the previous restore bounds.
250   window->SetProperty(aura::client::kShowStateKey, ui::SHOW_STATE_MAXIMIZED);
251   EXPECT_EQ("10,11 12x13", window_state->GetRestoreBoundsInParent().ToString());
252 }
253
254 // Verifies that the restore bounds do not get reset when restoring to a
255 // maximzied state from a minimized state.
256 TEST_F(BaseLayoutManagerTest, BoundsAfterRestoringToMaximizeFromMinimize) {
257   scoped_ptr<aura::Window> window(CreateTestWindow(gfx::Rect(1, 2, 3, 4)));
258   gfx::Rect bounds(10, 15, 25, 35);
259   window->SetBounds(bounds);
260
261   wm::WindowState* window_state = wm::GetWindowState(window.get());
262   // Maximize it, which should reset restore bounds.
263   window_state->Maximize();
264   EXPECT_EQ(bounds.ToString(),
265             window_state->GetRestoreBoundsInParent().ToString());
266
267   // Minimize the window. The restore bounds should not change.
268   window_state->Minimize();
269   EXPECT_EQ(bounds.ToString(),
270             window_state->GetRestoreBoundsInParent().ToString());
271
272   // Show the window again. The window should be maximized, and the restore
273   // bounds should not change.
274   window->Show();
275   EXPECT_EQ(bounds.ToString(),
276             window_state->GetRestoreBoundsInParent().ToString());
277   EXPECT_TRUE(window_state->IsMaximized());
278
279   window_state->Restore();
280   EXPECT_EQ(bounds.ToString(), window->bounds().ToString());
281 }
282
283 // Verify if the window is not resized during screen lock. See: crbug.com/173127
284 TEST_F(BaseLayoutManagerTest, NotResizeWhenScreenIsLocked) {
285   SetCanLockScreen(true);
286   scoped_ptr<aura::Window> window(CreateTestWindow(gfx::Rect(1, 2, 3, 4)));
287   // window with AlwaysOnTop will be managed by BaseLayoutManager.
288   window->SetProperty(aura::client::kAlwaysOnTopKey, true);
289   window->Show();
290
291   internal::ShelfLayoutManager* shelf =
292       internal::ShelfLayoutManager::ForLauncher(window.get());
293   shelf->SetAutoHideBehavior(SHELF_AUTO_HIDE_BEHAVIOR_ALWAYS);
294
295   window->SetBounds(ScreenAsh::GetMaximizedWindowBoundsInParent(window.get()));
296   gfx::Rect window_bounds = window->bounds();
297   EXPECT_EQ(
298       ScreenAsh::GetMaximizedWindowBoundsInParent(window.get()).ToString(),
299       window_bounds.ToString());
300
301   Shell::GetInstance()->session_state_delegate()->LockScreen();
302   shelf->UpdateVisibilityState();
303   EXPECT_NE(
304       ScreenAsh::GetMaximizedWindowBoundsInParent(window.get()).ToString(),
305       window_bounds.ToString());
306
307   Shell::GetInstance()->session_state_delegate()->UnlockScreen();
308   shelf->UpdateVisibilityState();
309   EXPECT_EQ(window_bounds.ToString(), window->bounds().ToString());
310 }
311
312 }  // namespace
313 }  // namespace ash