1 // Copyright 2013 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/window_positioner.h"
10 #include "ash/shell/toplevel_window.h"
11 #include "ash/test/ash_test_base.h"
12 #include "ash/test/test_shell_delegate.h"
13 #include "ash/wm/window_positioner.h"
14 #include "ash/wm/window_state.h"
15 #include "base/strings/string_number_conversions.h"
16 #include "ui/aura/window_event_dispatcher.h"
17 #include "ui/gfx/screen.h"
18 #include "ui/views/widget/widget.h"
19 #include "ui/views/widget/widget_delegate.h"
23 typedef test::AshTestBase WindowPositionerTest
;
25 TEST_F(WindowPositionerTest
, OpenMaximizedWindowOnSecondDisplay
) {
26 if (!SupportsMultipleDisplays())
28 // Tests that for a screen that is narrower than kForceMaximizeWidthLimit
29 // a new window gets maximized.
30 UpdateDisplay("400x400,500x500");
31 Shell::GetInstance()->set_target_root_window(
32 Shell::GetAllRootWindows()[1]);
33 shell::ToplevelWindow::CreateParams params
;
34 params
.can_resize
= true;
35 params
.can_maximize
= true;
36 views::Widget
* widget
=
37 shell::ToplevelWindow::CreateToplevelWindow(params
);
38 EXPECT_EQ("400,0 500x453", widget
->GetWindowBoundsInScreen().ToString());
41 TEST_F(WindowPositionerTest
, OpenDefaultWindowOnSecondDisplay
) {
42 if (!SupportsMultipleDisplays())
45 ash::WindowPositioner::SetMaximizeFirstWindow(true);
47 UpdateDisplay("400x400,1400x900");
48 aura::Window
* second_root_window
= Shell::GetAllRootWindows()[1];
49 Shell::GetInstance()->set_target_root_window(
51 shell::ToplevelWindow::CreateParams params
;
52 params
.can_resize
= true;
53 params
.can_maximize
= true;
54 views::Widget
* widget
=
55 shell::ToplevelWindow::CreateToplevelWindow(params
);
56 gfx::Rect bounds
= widget
->GetWindowBoundsInScreen();
58 EXPECT_TRUE(widget
->IsMaximized());
60 // The window should be in the 2nd display with the default size.
61 EXPECT_EQ("300x300", bounds
.size().ToString());
63 EXPECT_TRUE(Shell::GetScreen()->GetDisplayNearestWindow(
64 second_root_window
).bounds().Contains(bounds
));
67 // Tests that second window inherits first window's maximized state as well as
68 // its restore bounds.
69 TEST_F(WindowPositionerTest
, SecondMaximizedWindowHasProperRestoreSize
) {
71 ash::WindowPositioner::SetMaximizeFirstWindow(true);
73 UpdateDisplay("1400x900");
74 shell::ToplevelWindow::CreateParams params
;
75 params
.can_resize
= true;
76 params
.can_maximize
= true;
77 views::Widget
* widget1
=
78 shell::ToplevelWindow::CreateToplevelWindow(params
);
79 gfx::Rect bounds
= widget1
->GetWindowBoundsInScreen();
82 // The window should have default size.
83 EXPECT_FALSE(widget1
->IsMaximized());
84 EXPECT_EQ("300x300", bounds
.size().ToString());
87 // The window should be maximized.
88 bounds
= widget1
->GetWindowBoundsInScreen();
89 EXPECT_TRUE(widget1
->IsMaximized());
90 EXPECT_EQ("0,0 1400x853", bounds
.ToString());
92 // Create another window
93 views::Widget
* widget2
=
94 shell::ToplevelWindow::CreateToplevelWindow(params
);
96 // The second window should be maximized.
97 bounds
= widget2
->GetWindowBoundsInScreen();
98 EXPECT_TRUE(widget2
->IsMaximized());
99 EXPECT_EQ("0,0 1400x853", bounds
.ToString());
102 // Second window's restored size should be set to default size.
103 bounds
= widget2
->GetWindowBoundsInScreen();
104 EXPECT_EQ("300x300", bounds
.size().ToString());
109 // A WidgetDelegate that returns the out of display saved bounds.
110 class OutOfDisplayDelegate
: public views::WidgetDelegate
{
112 explicit OutOfDisplayDelegate(views::Widget
* widget
) : widget_(widget
) {}
113 ~OutOfDisplayDelegate() override
{}
115 // Overridden from WidgetDelegate:
116 void DeleteDelegate() override
{ delete this; }
117 views::Widget
* GetWidget() override
{ return widget_
; }
118 const views::Widget
* GetWidget() const override
{ return widget_
; }
119 bool GetSavedWindowPlacement(const views::Widget
* widget
,
121 ui::WindowShowState
* show_state
) const override
{
122 bounds
->SetRect(450, 10, 100, 100);
123 *show_state
= ui::SHOW_STATE_NORMAL
;
128 views::Widget
* widget_
;
130 DISALLOW_COPY_AND_ASSIGN(OutOfDisplayDelegate
);
135 TEST_F(WindowPositionerTest
, EnsureMinimumVisibility
) {
136 if (!SupportsHostWindowResize())
139 UpdateDisplay("400x400");
140 views::Widget
* widget
= new views::Widget();
141 views::Widget::InitParams
params(views::Widget::InitParams::TYPE_WINDOW
);
142 params
.delegate
= new OutOfDisplayDelegate(widget
);
143 params
.context
= Shell::GetPrimaryRootWindow();
144 widget
->Init(params
);
145 widget
->SetBounds(gfx::Rect(450,10, 100, 100));
146 wm::GetWindowState(widget
->GetNativeView())->set_minimum_visibility(true);
148 // Make sure the bounds is adjusted to be inside the work area.
149 EXPECT_EQ("375,10 100x100", widget
->GetWindowBoundsInScreen().ToString());
153 // In general case on first run the browser window will be maximized only for
154 // low resolution screens (width < 1366). In case of big screens the browser is
155 // opened being not maximized. To enforce maximization for all screen
156 // resolutions, one can set "ForceMaximizeBrowserWindowOnFirstRun"
157 // policy. In the following tests we check if the window will be opened in
158 // maximized mode for low and high resolution when this policy is set.
159 TEST_F(WindowPositionerTest
, FirstRunMaximizeWindowHighResloution
) {
160 const int width
= ash::WindowPositioner::GetForceMaximizedWidthLimit() + 100;
161 // Set resolution to 1466x300.
162 const std::string resolution
= base::IntToString(width
) + "x300";
163 UpdateDisplay(resolution
);
164 gfx::Rect
bounds_in_out(0, 0, 320, 240); // Random bounds.
165 ui::WindowShowState show_state_out
= ui::SHOW_STATE_DEFAULT
;
167 test::TestShellDelegate
* const delegate
=
168 static_cast<test::TestShellDelegate
*>(Shell::GetInstance()->delegate());
169 delegate
->SetForceMaximizeOnFirstRun(true);
171 WindowPositioner::GetBoundsAndShowStateForNewWindow(
172 Shell::GetScreen(), nullptr, false, ui::SHOW_STATE_DEFAULT
,
173 &bounds_in_out
, &show_state_out
);
175 EXPECT_EQ(show_state_out
, ui::SHOW_STATE_MAXIMIZED
);
178 // For detail see description of FirstRunMaximizeWindowHighResloution.
179 TEST_F(WindowPositionerTest
, FirstRunMaximizeWindowLowResolution
) {
180 const int width
= ash::WindowPositioner::GetForceMaximizedWidthLimit() - 100;
181 // Set resolution to 1266x300.
182 const std::string resolution
= base::IntToString(width
) + "x300";
183 UpdateDisplay(resolution
);
184 gfx::Rect
bounds_in_out(0, 0, 320, 240); // Random bounds.
185 ui::WindowShowState show_state_out
= ui::SHOW_STATE_DEFAULT
;
187 test::TestShellDelegate
* const delegate
=
188 static_cast<test::TestShellDelegate
*>(Shell::GetInstance()->delegate());
189 delegate
->SetForceMaximizeOnFirstRun(true);
191 WindowPositioner::GetBoundsAndShowStateForNewWindow(
192 Shell::GetScreen(), nullptr, false, ui::SHOW_STATE_DEFAULT
,
193 &bounds_in_out
, &show_state_out
);
195 EXPECT_EQ(show_state_out
, ui::SHOW_STATE_MAXIMIZED
);