blob: bbc931bb000f45579fe21a640ae4fec3952c1e35 [file] [log] [blame]
[email protected]55ad8c12014-01-17 18:24:331// Copyright 2014 The Chromium Authors. All rights reserved.
[email protected]8d625fb2012-07-18 16:40:062// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
[email protected]55ad8c12014-01-17 18:24:335#include "ash/screen_util.h"
[email protected]8d625fb2012-07-18 16:40:066
Ahmed Fakhry5d73bdf72019-02-27 18:21:077#include <memory>
8
9#include "ash/magnifier/docked_magnifier_controller.h"
Ahmed Fakhry1ac713b2018-11-06 18:57:4610#include "ash/root_window_controller.h"
11#include "ash/shelf/shelf.h"
Manu Corneta76b6b02018-08-30 21:02:2312#include "ash/shelf/shelf_constants.h"
[email protected]8d625fb2012-07-18 16:40:0613#include "ash/shell.h"
estadea23d6ceb2017-02-15 00:47:0914#include "ash/test/ash_test_base.h"
[email protected]8d625fb2012-07-18 16:40:0615#include "ash/wm/window_util.h"
Ahmed Fakhry5d73bdf72019-02-27 18:21:0716#include "ash/wm/wm_event.h"
[email protected]8d625fb2012-07-18 16:40:0617#include "ui/aura/env.h"
[email protected]8d625fb2012-07-18 16:40:0618#include "ui/aura/window.h"
[email protected]fcc51c952014-02-21 21:31:2619#include "ui/aura/window_event_dispatcher.h"
rjkroege72f8154f2016-10-29 00:49:0220#include "ui/display/manager/display_manager.h"
Ahmed Fakhry4f8e3722017-10-31 21:01:5821#include "ui/display/unified_desktop_utils.h"
[email protected]8d625fb2012-07-18 16:40:0622#include "ui/views/widget/widget.h"
23#include "ui/views/widget/widget_delegate.h"
yhanada698af192017-02-23 10:57:0724#include "ui/wm/core/coordinate_conversion.h"
[email protected]8d625fb2012-07-18 16:40:0625
26namespace ash {
[email protected]8d625fb2012-07-18 16:40:0627
estadea23d6ceb2017-02-15 00:47:0928using ScreenUtilTest = AshTestBase;
[email protected]8d625fb2012-07-18 16:40:0629
estadea23d6ceb2017-02-15 00:47:0930TEST_F(ScreenUtilTest, Bounds) {
[email protected]f634dd32012-07-23 22:49:0731 UpdateDisplay("600x600,500x500");
[email protected]a2e6af12013-01-07 21:40:3532 views::Widget* primary = views::Widget::CreateWindowWithContextAndBounds(
33 NULL, CurrentContext(), gfx::Rect(10, 10, 100, 100));
[email protected]8d625fb2012-07-18 16:40:0634 primary->Show();
[email protected]a2e6af12013-01-07 21:40:3535 views::Widget* secondary = views::Widget::CreateWindowWithContextAndBounds(
36 NULL, CurrentContext(), gfx::Rect(610, 10, 100, 100));
[email protected]8d625fb2012-07-18 16:40:0637 secondary->Show();
38
Manu Corneta76b6b02018-08-30 21:02:2339 // Maximized bounds.
40 const int bottom_inset_first = 600 - ShelfConstants::shelf_size();
41 const int bottom_inset_second = 500 - ShelfConstants::shelf_size();
tdanderson0b7905b2016-06-22 21:53:0342 EXPECT_EQ(
Manu Corneta76b6b02018-08-30 21:02:2343 gfx::Rect(0, 0, 600, bottom_inset_first).ToString(),
Qiang Xu07d7c9e32018-01-18 20:25:4444 screen_util::GetMaximizedWindowBoundsInParent(primary->GetNativeView())
tdanderson0b7905b2016-06-22 21:53:0345 .ToString());
46 EXPECT_EQ(
Manu Corneta76b6b02018-08-30 21:02:2347 gfx::Rect(0, 0, 500, bottom_inset_second).ToString(),
Qiang Xu07d7c9e32018-01-18 20:25:4448 screen_util::GetMaximizedWindowBoundsInParent(secondary->GetNativeView())
tdanderson0b7905b2016-06-22 21:53:0349 .ToString());
[email protected]8d625fb2012-07-18 16:40:0650
[email protected]8d625fb2012-07-18 16:40:0651 // Display bounds
52 EXPECT_EQ("0,0 600x600",
Qiang Xu07d7c9e32018-01-18 20:25:4453 screen_util::GetDisplayBoundsInParent(primary->GetNativeView())
jamescookb8dcef522016-06-25 14:42:5554 .ToString());
[email protected]8d625fb2012-07-18 16:40:0655 EXPECT_EQ("0,0 500x500",
Qiang Xu07d7c9e32018-01-18 20:25:4456 screen_util::GetDisplayBoundsInParent(secondary->GetNativeView())
jamescookb8dcef522016-06-25 14:42:5557 .ToString());
[email protected]8d625fb2012-07-18 16:40:0658
59 // Work area bounds
tdanderson0b7905b2016-06-22 21:53:0360 EXPECT_EQ(
Manu Corneta76b6b02018-08-30 21:02:2361 gfx::Rect(0, 0, 600, bottom_inset_first).ToString(),
Qiang Xu07d7c9e32018-01-18 20:25:4462 screen_util::GetDisplayWorkAreaBoundsInParent(primary->GetNativeView())
tdanderson0b7905b2016-06-22 21:53:0363 .ToString());
64 EXPECT_EQ(
Manu Corneta76b6b02018-08-30 21:02:2365 gfx::Rect(0, 0, 500, bottom_inset_second).ToString(),
Qiang Xu07d7c9e32018-01-18 20:25:4466 screen_util::GetDisplayWorkAreaBoundsInParent(secondary->GetNativeView())
tdanderson0b7905b2016-06-22 21:53:0367 .ToString());
[email protected]8d625fb2012-07-18 16:40:0668}
[email protected]8d625fb2012-07-18 16:40:0669
[email protected]805155f2013-04-10 02:11:2070// Test verifies a stable handling of secondary screen widget changes
71// (crbug.com/226132).
estadea23d6ceb2017-02-15 00:47:0972TEST_F(ScreenUtilTest, StabilityTest) {
[email protected]805155f2013-04-10 02:11:2073 UpdateDisplay("600x600,500x500");
74 views::Widget* secondary = views::Widget::CreateWindowWithContextAndBounds(
75 NULL, CurrentContext(), gfx::Rect(610, 10, 100, 100));
76 EXPECT_EQ(Shell::GetAllRootWindows()[1],
jamescookb8dcef522016-06-25 14:42:5577 secondary->GetNativeView()->GetRootWindow());
[email protected]805155f2013-04-10 02:11:2078 secondary->Show();
79 secondary->Maximize();
80 secondary->Show();
81 secondary->SetFullscreen(true);
82 secondary->Hide();
83 secondary->Close();
84}
85
estadea23d6ceb2017-02-15 00:47:0986TEST_F(ScreenUtilTest, ConvertRect) {
[email protected]f634dd32012-07-23 22:49:0787 UpdateDisplay("600x600,500x500");
[email protected]8d625fb2012-07-18 16:40:0688
[email protected]a2e6af12013-01-07 21:40:3589 views::Widget* primary = views::Widget::CreateWindowWithContextAndBounds(
90 NULL, CurrentContext(), gfx::Rect(10, 10, 100, 100));
[email protected]8d625fb2012-07-18 16:40:0691 primary->Show();
[email protected]a2e6af12013-01-07 21:40:3592 views::Widget* secondary = views::Widget::CreateWindowWithContextAndBounds(
93 NULL, CurrentContext(), gfx::Rect(610, 10, 100, 100));
[email protected]8d625fb2012-07-18 16:40:0694 secondary->Show();
95
yhanada698af192017-02-23 10:57:0796 gfx::Rect r1(10, 10, 100, 100);
97 ::wm::ConvertRectFromScreen(primary->GetNativeView(), &r1);
98 EXPECT_EQ("0,0 100x100", r1.ToString());
[email protected]8d625fb2012-07-18 16:40:0699
yhanada698af192017-02-23 10:57:07100 gfx::Rect r2(620, 20, 100, 100);
101 ::wm::ConvertRectFromScreen(secondary->GetNativeView(), &r2);
102 EXPECT_EQ("10,10 100x100", r2.ToString());
103
104 gfx::Rect r3(30, 30, 100, 100);
105 ::wm::ConvertRectToScreen(primary->GetNativeView(), &r3);
106 EXPECT_EQ("40,40 100x100", r3.ToString());
107
108 gfx::Rect r4(40, 40, 100, 100);
109 ::wm::ConvertRectToScreen(secondary->GetNativeView(), &r4);
110 EXPECT_EQ("650,50 100x100", r4.ToString());
[email protected]8d625fb2012-07-18 16:40:06111}
112
estadea23d6ceb2017-02-15 00:47:09113TEST_F(ScreenUtilTest, ShelfDisplayBoundsInUnifiedDesktop) {
rjkroege72f8154f2016-10-29 00:49:02114 display_manager()->SetUnifiedDesktopEnabled(true);
oshima96f6a502015-05-02 08:43:32115
116 views::Widget* widget = views::Widget::CreateWindowWithContextAndBounds(
117 NULL, CurrentContext(), gfx::Rect(10, 10, 100, 100));
sky00f0b892017-05-05 17:06:24118 aura::Window* window = widget->GetNativeWindow();
oshima96f6a502015-05-02 08:43:32119
120 UpdateDisplay("500x400");
sky00f0b892017-05-05 17:06:24121 EXPECT_EQ("0,0 500x400",
Qiang Xu07d7c9e32018-01-18 20:25:44122 screen_util::GetDisplayBoundsWithShelf(window).ToString());
oshima96f6a502015-05-02 08:43:32123
124 UpdateDisplay("500x400,600x400");
sky00f0b892017-05-05 17:06:24125 EXPECT_EQ("0,0 500x400",
Qiang Xu07d7c9e32018-01-18 20:25:44126 screen_util::GetDisplayBoundsWithShelf(window).ToString());
oshima96f6a502015-05-02 08:43:32127
128 // Move to the 2nd physical display. Shelf's display still should be
129 // the first.
130 widget->SetBounds(gfx::Rect(800, 0, 100, 100));
131 ASSERT_EQ("800,0 100x100", widget->GetWindowBoundsInScreen().ToString());
132
sky00f0b892017-05-05 17:06:24133 EXPECT_EQ("0,0 500x400",
Qiang Xu07d7c9e32018-01-18 20:25:44134 screen_util::GetDisplayBoundsWithShelf(window).ToString());
oshima96f6a502015-05-02 08:43:32135
136 UpdateDisplay("600x500");
sky00f0b892017-05-05 17:06:24137 EXPECT_EQ("0,0 600x500",
Qiang Xu07d7c9e32018-01-18 20:25:44138 screen_util::GetDisplayBoundsWithShelf(window).ToString());
oshima96f6a502015-05-02 08:43:32139}
140
Ahmed Fakhry4f8e3722017-10-31 21:01:58141TEST_F(ScreenUtilTest, ShelfDisplayBoundsInUnifiedDesktopGrid) {
Ahmed Fakhry4f8e3722017-10-31 21:01:58142 UpdateDisplay("500x400,400x600,300x600,200x300,600x200,350x400");
143 display_manager()->SetUnifiedDesktopEnabled(true);
144
145 views::Widget* widget = views::Widget::CreateWindowWithContextAndBounds(
146 NULL, CurrentContext(), gfx::Rect(10, 10, 100, 100));
147 aura::Window* window = widget->GetNativeWindow();
148
149 display::DisplayIdList list = display_manager()->GetCurrentDisplayIdList();
150 ASSERT_EQ(6u, list.size());
151 // Create a 3 x 2 vertical layout matrix and set it.
152 // [500 x 400] [400 x 600]
153 // [300 x 600] [200 x 300]
154 // [600 x 200] [350 x 400]
155 display::UnifiedDesktopLayoutMatrix matrix;
156 matrix.resize(3u);
157 matrix[0].emplace_back(list[0]);
158 matrix[0].emplace_back(list[1]);
159 matrix[1].emplace_back(list[2]);
160 matrix[1].emplace_back(list[3]);
161 matrix[2].emplace_back(list[4]);
162 matrix[2].emplace_back(list[5]);
163 display_manager()->SetUnifiedDesktopMatrix(matrix);
164 display::Screen* screen = display::Screen::GetScreen();
165 EXPECT_EQ(gfx::Size(766, 1254), screen->GetPrimaryDisplay().size());
166
Ahmed Fakhry1ac713b2018-11-06 18:57:46167 Shelf* shelf = Shell::GetPrimaryRootWindowController()->shelf();
168 EXPECT_EQ(shelf->alignment(), SHELF_ALIGNMENT_BOTTOM);
169
170 // Regardless of where the window is, the shelf with a bottom alignment is
171 // always in the bottom left display in the matrix.
172 EXPECT_EQ(gfx::Rect(0, 1057, 593, 198),
Qiang Xu07d7c9e32018-01-18 20:25:44173 screen_util::GetDisplayBoundsWithShelf(window));
Ahmed Fakhry4f8e3722017-10-31 21:01:58174
175 // Move to the bottom right display.
176 widget->SetBounds(gfx::Rect(620, 940, 100, 100));
Ahmed Fakhry1ac713b2018-11-06 18:57:46177 EXPECT_EQ(gfx::Rect(0, 1057, 593, 198),
178 screen_util::GetDisplayBoundsWithShelf(window));
179
180 // Change the shelf alignment to left, and expect that it now resides in the
181 // top left display in the matrix.
182 shelf->SetAlignment(SHELF_ALIGNMENT_LEFT);
Ahmed Fakhry4f8e3722017-10-31 21:01:58183 EXPECT_EQ(gfx::Rect(0, 0, 499, 400),
Qiang Xu07d7c9e32018-01-18 20:25:44184 screen_util::GetDisplayBoundsWithShelf(window));
Ahmed Fakhry1ac713b2018-11-06 18:57:46185
186 // Change the shelf alignment to right, and expect that it now resides in the
187 // top right display in the matrix.
188 shelf->SetAlignment(SHELF_ALIGNMENT_RIGHT);
189 EXPECT_EQ(gfx::Rect(499, 0, 267, 400),
190 screen_util::GetDisplayBoundsWithShelf(window));
191
192 // Change alignment back to bottom and change the unified display zoom factor.
193 // Expect that the display with shelf bounds will take into account the zoom
194 // factor.
195 shelf->SetAlignment(SHELF_ALIGNMENT_BOTTOM);
196 display_manager()->UpdateZoomFactor(display::kUnifiedDisplayId, 3.f);
197 const display::Display unified_display =
198 display_manager()->GetDisplayForId(display::kUnifiedDisplayId);
199 EXPECT_FLOAT_EQ(unified_display.device_scale_factor(), 3.f);
200 EXPECT_EQ(gfx::Rect(0, 352, 198, 67),
201 screen_util::GetDisplayBoundsWithShelf(window));
Ahmed Fakhry4f8e3722017-10-31 21:01:58202}
203
Malay Keshavfd6fd0c2018-07-31 22:07:47204TEST_F(ScreenUtilTest, SnapBoundsToDisplayEdge) {
205 UpdateDisplay("2400x1600*1.5");
206
207 gfx::Rect bounds(1555, 0, 45, 1066);
208 views::Widget* widget = views::Widget::CreateWindowWithContextAndBounds(
209 NULL, CurrentContext(), bounds);
210 aura::Window* window = widget->GetNativeWindow();
211
212 gfx::Rect snapped_bounds =
213 screen_util::SnapBoundsToDisplayEdge(bounds, window);
214
215 EXPECT_EQ(snapped_bounds, gfx::Rect(1555, 0, 45, 1067));
216
217 bounds = gfx::Rect(5, 1000, 1595, 66);
218 snapped_bounds = screen_util::SnapBoundsToDisplayEdge(bounds, window);
219 EXPECT_EQ(snapped_bounds, gfx::Rect(5, 1000, 1595, 67));
220
221 UpdateDisplay("800x600");
222 bounds = gfx::Rect(0, 552, 800, 48);
223 snapped_bounds = screen_util::SnapBoundsToDisplayEdge(bounds, window);
224 EXPECT_EQ(snapped_bounds, gfx::Rect(0, 552, 800, 48));
225}
226
Ahmed Fakhry5d73bdf72019-02-27 18:21:07227// Tests that making a window fullscreen while the Docked Magnifier is enabled
228// won't make its bounds occupy the entire screen bounds, but will take into
229// account the Docked Magnifier height.
230TEST_F(ScreenUtilTest, FullscreenWindowBoundsWithDockedMagnifier) {
231 UpdateDisplay("1366x768");
232
233 std::unique_ptr<aura::Window> window = CreateToplevelTestWindow(
234 gfx::Rect(300, 300, 200, 150), kShellWindowId_DefaultContainer);
235
236 auto* docked_magnifier_controller =
237 Shell::Get()->docked_magnifier_controller();
238 docked_magnifier_controller->SetEnabled(true);
239
240 const wm::WMEvent event(wm::WM_EVENT_TOGGLE_FULLSCREEN);
241 wm::GetWindowState(window.get())->OnWMEvent(&event);
242
243 constexpr gfx::Rect kDisplayBounds{1366, 768};
244 EXPECT_NE(window->bounds(), kDisplayBounds);
245
246 gfx::Rect expected_bounds = kDisplayBounds;
247 expected_bounds.Inset(
248 0, docked_magnifier_controller->GetTotalMagnifierHeight(), 0, 0);
249 EXPECT_EQ(expected_bounds, window->bounds());
250}
251
[email protected]8d625fb2012-07-18 16:40:06252} // namespace ash