41e067fefaa9af4e9524ca02be8ff2edec782e21
[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/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"
26
27 namespace ash {
28 namespace {
29
30 class MaximizeDelegateView : public views::WidgetDelegateView {
31  public:
32   MaximizeDelegateView(const gfx::Rect& initial_bounds)
33       : initial_bounds_(initial_bounds) {
34   }
35   virtual ~MaximizeDelegateView() {}
36
37   virtual bool GetSavedWindowPlacement(
38       const views::Widget* widget,
39       gfx::Rect* bounds,
40       ui::WindowShowState* show_state) const OVERRIDE {
41     *bounds = initial_bounds_;
42     *show_state = ui::SHOW_STATE_MAXIMIZED;
43     return true;
44   }
45
46  private:
47   const gfx::Rect initial_bounds_;
48
49   DISALLOW_COPY_AND_ASSIGN(MaximizeDelegateView);
50 };
51
52 class TestShellObserver : public ShellObserver {
53  public:
54   TestShellObserver() : call_count_(0),
55                         is_fullscreen_(false) {
56     Shell::GetInstance()->AddShellObserver(this);
57   }
58
59   virtual ~TestShellObserver() {
60     Shell::GetInstance()->RemoveShellObserver(this);
61   }
62
63   virtual void OnFullscreenStateChanged(bool is_fullscreen,
64                                         aura::Window* root_window) OVERRIDE {
65     call_count_++;
66     is_fullscreen_ = is_fullscreen;
67   }
68
69   int call_count() const {
70     return call_count_;
71   }
72
73   bool is_fullscreen() const {
74     return is_fullscreen_;
75   }
76
77  private:
78   int call_count_;
79   bool is_fullscreen_;
80
81   DISALLOW_COPY_AND_ASSIGN(TestShellObserver);
82 };
83
84 }  // namespace
85
86 typedef test::AshTestBase WorkspaceLayoutManagerTest;
87
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
90 // there is one).
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);
96
97   wm::WindowState* window_state = wm::GetWindowState(window.get());
98
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());
105
106   if (!SupportsMultipleDisplays())
107     return;
108
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());
118
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());
125   EXPECT_TRUE(
126       Shell::GetPrimaryRootWindow()->bounds().Intersects(window->bounds()));
127 }
128
129 TEST_F(WorkspaceLayoutManagerTest, KeepMinimumVisibilityInDisplays) {
130   if (!SupportsMultipleDisplays())
131     return;
132
133   UpdateDisplay("300x400,400x500");
134   aura::Window::Windows root_windows = Shell::GetAllRootWindows();
135
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());
140
141   scoped_ptr<aura::Window> window1(
142       CreateTestWindowInShellWithBounds(gfx::Rect(10, -400, 200, 200)));
143   EXPECT_EQ("10,-400 200x200", window1->GetBoundsInScreen().ToString());
144
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());
149 }
150
151 TEST_F(WorkspaceLayoutManagerTest, KeepRestoredWindowInDisplay) {
152   if (!SupportsHostWindowResize())
153     return;
154   scoped_ptr<aura::Window> window(
155       CreateTestWindowInShellWithBounds(gfx::Rect(1, 2, 30, 40)));
156   wm::WindowState* window_state = wm::GetWindowState(window.get());
157
158   // Maximized -> Normal transition.
159   window_state->Maximize();
160   window_state->SetRestoreBoundsInScreen(gfx::Rect(-100, -100, 30, 40));
161   window_state->Restore();
162   EXPECT_TRUE(
163       Shell::GetPrimaryRootWindow()->bounds().Intersects(window->bounds()));
164   // Y bounds should not be negative.
165   EXPECT_EQ("-20,0 30x40", window->bounds().ToString());
166
167   // Minimized -> Normal transition.
168   window->SetBounds(gfx::Rect(-100, -100, 30, 40));
169   window_state->Minimize();
170   EXPECT_FALSE(
171       Shell::GetPrimaryRootWindow()->bounds().Intersects(window->bounds()));
172   EXPECT_EQ("-100,-100 30x40", window->bounds().ToString());
173   window->Show();
174   EXPECT_TRUE(
175       Shell::GetPrimaryRootWindow()->bounds().Intersects(window->bounds()));
176   // Y bounds should not be negative.
177   EXPECT_EQ("-20,0 30x40", window->bounds().ToString());
178
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();
186   EXPECT_TRUE(
187       Shell::GetPrimaryRootWindow()->bounds().Intersects(window->bounds()));
188   // Y bounds should not be negative.
189   EXPECT_EQ("-20,0 30x40", window->bounds().ToString());
190 }
191
192 TEST_F(WorkspaceLayoutManagerTest, MaximizeInDisplayToBeRestored) {
193   if (!SupportsMultipleDisplays())
194     return;
195   UpdateDisplay("300x400,400x500");
196
197   aura::Window::Windows root_windows = Shell::GetAllRootWindows();
198
199   scoped_ptr<aura::Window> window(
200       CreateTestWindowInShellWithBounds(gfx::Rect(1, 2, 30, 40)));
201   EXPECT_EQ(root_windows[0], window->GetRootWindow());
202
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());
210
211   window_state->Restore();
212   EXPECT_EQ(root_windows[1], window->GetRootWindow());
213   EXPECT_EQ("400,0 30x40", window->GetBoundsInScreen().ToString());
214
215   // If the restore bounds intersects with the current display,
216   // don't move.
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());
221
222   window_state->Restore();
223   EXPECT_EQ(root_windows[1], window->GetRootWindow());
224   EXPECT_EQ("280,0 30x40", window->GetBoundsInScreen().ToString());
225
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];
232   w1->Init(params);
233   w1->Show();
234   EXPECT_TRUE(w1->IsMaximized());
235   EXPECT_EQ(root_windows[1], w1->GetNativeView()->GetRootWindow());
236   EXPECT_EQ("300,0 400x453", w1->GetWindowBoundsInScreen().ToString());
237   w1->Restore();
238   EXPECT_EQ(root_windows[1], w1->GetNativeView()->GetRootWindow());
239   EXPECT_EQ("400,0 30x40", w1->GetWindowBoundsInScreen().ToString());
240 }
241
242 TEST_F(WorkspaceLayoutManagerTest, FullscreenInDisplayToBeRestored) {
243   if (!SupportsMultipleDisplays())
244     return;
245   UpdateDisplay("300x400,400x500");
246
247   aura::Window::Windows root_windows = Shell::GetAllRootWindows();
248
249   scoped_ptr<aura::Window> window(
250       CreateTestWindowInShellWithBounds(gfx::Rect(1, 2, 30, 40)));
251   EXPECT_EQ(root_windows[0], window->GetRootWindow());
252
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());
261
262   window_state->Restore();
263   EXPECT_EQ(root_windows[1], window->GetRootWindow());
264   EXPECT_EQ("400,0 30x40", window->GetBoundsInScreen().ToString());
265
266   // If the restore bounds intersects with the current display,
267   // don't move.
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());
273
274   window_state->Restore();
275   EXPECT_EQ(root_windows[1], window->GetRootWindow());
276   EXPECT_EQ("280,0 30x40", window->GetBoundsInScreen().ToString());
277 }
278
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 {
284  public:
285   DontClobberRestoreBoundsWindowObserver() : window_(NULL) {}
286
287   void set_window(aura::Window* window) { window_ = window; }
288
289   virtual void OnWindowPropertyChanged(aura::Window* window,
290                                        const void* key,
291                                        intptr_t old) OVERRIDE {
292     if (!window_)
293       return;
294
295     if (wm::GetWindowState(window)->IsMaximized()) {
296       aura::Window* w = window_;
297       window_ = NULL;
298
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()));
304     }
305   }
306
307  private:
308   aura::Window* window_;
309
310   DISALLOW_COPY_AND_ASSIGN(DontClobberRestoreBoundsWindowObserver);
311 };
312
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());
326   window->Show();
327
328   wm::WindowState* window_state = wm::GetWindowState(window.get());
329   window_state->Activate();
330
331   scoped_ptr<aura::Window> window2(
332       CreateTestWindowInShellWithBounds(gfx::Rect(12, 20, 30, 40)));
333   views::corewm::AddTransientChild(window.get(), window2.get());
334   window2->Show();
335
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);
341 }
342
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)));
347   window->Show();
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),
352                                              window.get()));
353   child_window->Show();
354   window_state->Maximize();
355   EXPECT_EQ("5,6 7x8", child_window->bounds().ToString());
356 }
357
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());
364
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);
375
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);
379
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);
388
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);
394
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));
400
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);
408
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());
418
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);
423 }
424
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.
428   gfx::Size work_area(
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());
436
437   // Directly setting the bounds triggers a slightly different code path. Verify
438   // that too.
439   window->SetBounds(window_bounds);
440   EXPECT_EQ(gfx::Rect(gfx::Point(100, 101), work_area).ToString(),
441       window->bounds().ToString());
442 }
443
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();
453
454   window_state2->ToggleFullscreen();
455   EXPECT_EQ(1, observer.call_count());
456   EXPECT_TRUE(observer.is_fullscreen());
457
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());
462
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());
467
468   window_state2->ToggleFullscreen();
469   EXPECT_EQ(4, observer.call_count());
470   EXPECT_FALSE(observer.is_fullscreen());
471
472   window_state2->ToggleFullscreen();
473   EXPECT_EQ(5, observer.call_count());
474   EXPECT_TRUE(observer.is_fullscreen());
475
476   // Closing the window should change the fullscreen state.
477   window2.reset();
478   EXPECT_EQ(6, observer.call_count());
479   EXPECT_FALSE(observer.is_fullscreen());
480 }
481
482 }  // namespace ash