blob: c40a069f637213c9af54a09ad0c63ca4227467e6 [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
Jun Mukaif701ffa82019-05-15 20:45:259#include "ash/magnifier/docked_magnifier_controller_impl.h"
Matthew Mourgos75327562019-09-09 21:23:5710#include "ash/public/cpp/shelf_config.h"
Ahmed Fakhry1ac713b2018-11-06 18:57:4611#include "ash/root_window_controller.h"
12#include "ash/shelf/shelf.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"
Ahmed Fakhryf929d5f2019-04-11 02:22:4815#include "ash/wm/desks/desks_util.h"
[email protected]8d625fb2012-07-18 16:40:0616#include "ash/wm/window_util.h"
Ahmed Fakhry5d73bdf72019-02-27 18:21:0717#include "ash/wm/wm_event.h"
[email protected]8d625fb2012-07-18 16:40:0618#include "ui/aura/env.h"
[email protected]8d625fb2012-07-18 16:40:0619#include "ui/aura/window.h"
[email protected]fcc51c952014-02-21 21:31:2620#include "ui/aura/window_event_dispatcher.h"
rjkroege72f8154f2016-10-29 00:49:0221#include "ui/display/manager/display_manager.h"
Ahmed Fakhry4f8e3722017-10-31 21:01:5822#include "ui/display/unified_desktop_utils.h"
[email protected]8d625fb2012-07-18 16:40:0623#include "ui/views/widget/widget.h"
24#include "ui/views/widget/widget_delegate.h"
yhanada698af192017-02-23 10:57:0725#include "ui/wm/core/coordinate_conversion.h"
[email protected]8d625fb2012-07-18 16:40:0626
27namespace ash {
[email protected]8d625fb2012-07-18 16:40:0628
estadea23d6ceb2017-02-15 00:47:0929using ScreenUtilTest = AshTestBase;
[email protected]8d625fb2012-07-18 16:40:0630
estadea23d6ceb2017-02-15 00:47:0931TEST_F(ScreenUtilTest, Bounds) {
[email protected]f634dd32012-07-23 22:49:0732 UpdateDisplay("600x600,500x500");
[email protected]a2e6af12013-01-07 21:40:3533 views::Widget* primary = views::Widget::CreateWindowWithContextAndBounds(
34 NULL, CurrentContext(), gfx::Rect(10, 10, 100, 100));
[email protected]8d625fb2012-07-18 16:40:0635 primary->Show();
[email protected]a2e6af12013-01-07 21:40:3536 views::Widget* secondary = views::Widget::CreateWindowWithContextAndBounds(
37 NULL, CurrentContext(), gfx::Rect(610, 10, 100, 100));
[email protected]8d625fb2012-07-18 16:40:0638 secondary->Show();
39
Manu Corneta76b6b02018-08-30 21:02:2340 // Maximized bounds.
Matthew Mourgos75327562019-09-09 21:23:5741 const int bottom_inset_first = 600 - ShelfConfig::Get()->shelf_size();
42 const int bottom_inset_second = 500 - ShelfConfig::Get()->shelf_size();
tdanderson0b7905b2016-06-22 21:53:0343 EXPECT_EQ(
Manu Corneta76b6b02018-08-30 21:02:2344 gfx::Rect(0, 0, 600, bottom_inset_first).ToString(),
Qiang Xu07d7c9e32018-01-18 20:25:4445 screen_util::GetMaximizedWindowBoundsInParent(primary->GetNativeView())
tdanderson0b7905b2016-06-22 21:53:0346 .ToString());
47 EXPECT_EQ(
Manu Corneta76b6b02018-08-30 21:02:2348 gfx::Rect(0, 0, 500, bottom_inset_second).ToString(),
Qiang Xu07d7c9e32018-01-18 20:25:4449 screen_util::GetMaximizedWindowBoundsInParent(secondary->GetNativeView())
tdanderson0b7905b2016-06-22 21:53:0350 .ToString());
[email protected]8d625fb2012-07-18 16:40:0651
[email protected]8d625fb2012-07-18 16:40:0652 // Display bounds
53 EXPECT_EQ("0,0 600x600",
Qiang Xu07d7c9e32018-01-18 20:25:4454 screen_util::GetDisplayBoundsInParent(primary->GetNativeView())
jamescookb8dcef522016-06-25 14:42:5555 .ToString());
[email protected]8d625fb2012-07-18 16:40:0656 EXPECT_EQ("0,0 500x500",
Qiang Xu07d7c9e32018-01-18 20:25:4457 screen_util::GetDisplayBoundsInParent(secondary->GetNativeView())
jamescookb8dcef522016-06-25 14:42:5558 .ToString());
[email protected]8d625fb2012-07-18 16:40:0659
60 // Work area bounds
tdanderson0b7905b2016-06-22 21:53:0361 EXPECT_EQ(
Manu Corneta76b6b02018-08-30 21:02:2362 gfx::Rect(0, 0, 600, bottom_inset_first).ToString(),
Qiang Xu07d7c9e32018-01-18 20:25:4463 screen_util::GetDisplayWorkAreaBoundsInParent(primary->GetNativeView())
tdanderson0b7905b2016-06-22 21:53:0364 .ToString());
65 EXPECT_EQ(
Manu Corneta76b6b02018-08-30 21:02:2366 gfx::Rect(0, 0, 500, bottom_inset_second).ToString(),
Qiang Xu07d7c9e32018-01-18 20:25:4467 screen_util::GetDisplayWorkAreaBoundsInParent(secondary->GetNativeView())
tdanderson0b7905b2016-06-22 21:53:0368 .ToString());
[email protected]8d625fb2012-07-18 16:40:0669}
[email protected]8d625fb2012-07-18 16:40:0670
[email protected]805155f2013-04-10 02:11:2071// Test verifies a stable handling of secondary screen widget changes
72// (crbug.com/226132).
estadea23d6ceb2017-02-15 00:47:0973TEST_F(ScreenUtilTest, StabilityTest) {
[email protected]805155f2013-04-10 02:11:2074 UpdateDisplay("600x600,500x500");
75 views::Widget* secondary = views::Widget::CreateWindowWithContextAndBounds(
76 NULL, CurrentContext(), gfx::Rect(610, 10, 100, 100));
77 EXPECT_EQ(Shell::GetAllRootWindows()[1],
jamescookb8dcef522016-06-25 14:42:5578 secondary->GetNativeView()->GetRootWindow());
[email protected]805155f2013-04-10 02:11:2079 secondary->Show();
80 secondary->Maximize();
81 secondary->Show();
82 secondary->SetFullscreen(true);
83 secondary->Hide();
84 secondary->Close();
85}
86
estadea23d6ceb2017-02-15 00:47:0987TEST_F(ScreenUtilTest, ConvertRect) {
[email protected]f634dd32012-07-23 22:49:0788 UpdateDisplay("600x600,500x500");
[email protected]8d625fb2012-07-18 16:40:0689
[email protected]a2e6af12013-01-07 21:40:3590 views::Widget* primary = views::Widget::CreateWindowWithContextAndBounds(
91 NULL, CurrentContext(), gfx::Rect(10, 10, 100, 100));
[email protected]8d625fb2012-07-18 16:40:0692 primary->Show();
[email protected]a2e6af12013-01-07 21:40:3593 views::Widget* secondary = views::Widget::CreateWindowWithContextAndBounds(
94 NULL, CurrentContext(), gfx::Rect(610, 10, 100, 100));
[email protected]8d625fb2012-07-18 16:40:0695 secondary->Show();
96
yhanada698af192017-02-23 10:57:0797 gfx::Rect r1(10, 10, 100, 100);
98 ::wm::ConvertRectFromScreen(primary->GetNativeView(), &r1);
99 EXPECT_EQ("0,0 100x100", r1.ToString());
[email protected]8d625fb2012-07-18 16:40:06100
yhanada698af192017-02-23 10:57:07101 gfx::Rect r2(620, 20, 100, 100);
102 ::wm::ConvertRectFromScreen(secondary->GetNativeView(), &r2);
103 EXPECT_EQ("10,10 100x100", r2.ToString());
104
105 gfx::Rect r3(30, 30, 100, 100);
106 ::wm::ConvertRectToScreen(primary->GetNativeView(), &r3);
107 EXPECT_EQ("40,40 100x100", r3.ToString());
108
109 gfx::Rect r4(40, 40, 100, 100);
110 ::wm::ConvertRectToScreen(secondary->GetNativeView(), &r4);
111 EXPECT_EQ("650,50 100x100", r4.ToString());
[email protected]8d625fb2012-07-18 16:40:06112}
113
estadea23d6ceb2017-02-15 00:47:09114TEST_F(ScreenUtilTest, ShelfDisplayBoundsInUnifiedDesktop) {
rjkroege72f8154f2016-10-29 00:49:02115 display_manager()->SetUnifiedDesktopEnabled(true);
oshima96f6a502015-05-02 08:43:32116
117 views::Widget* widget = views::Widget::CreateWindowWithContextAndBounds(
118 NULL, CurrentContext(), gfx::Rect(10, 10, 100, 100));
sky00f0b892017-05-05 17:06:24119 aura::Window* window = widget->GetNativeWindow();
oshima96f6a502015-05-02 08:43:32120
121 UpdateDisplay("500x400");
sky00f0b892017-05-05 17:06:24122 EXPECT_EQ("0,0 500x400",
Qiang Xu07d7c9e32018-01-18 20:25:44123 screen_util::GetDisplayBoundsWithShelf(window).ToString());
oshima96f6a502015-05-02 08:43:32124
125 UpdateDisplay("500x400,600x400");
sky00f0b892017-05-05 17:06:24126 EXPECT_EQ("0,0 500x400",
Qiang Xu07d7c9e32018-01-18 20:25:44127 screen_util::GetDisplayBoundsWithShelf(window).ToString());
oshima96f6a502015-05-02 08:43:32128
129 // Move to the 2nd physical display. Shelf's display still should be
130 // the first.
131 widget->SetBounds(gfx::Rect(800, 0, 100, 100));
132 ASSERT_EQ("800,0 100x100", widget->GetWindowBoundsInScreen().ToString());
133
sky00f0b892017-05-05 17:06:24134 EXPECT_EQ("0,0 500x400",
Qiang Xu07d7c9e32018-01-18 20:25:44135 screen_util::GetDisplayBoundsWithShelf(window).ToString());
oshima96f6a502015-05-02 08:43:32136
137 UpdateDisplay("600x500");
sky00f0b892017-05-05 17:06:24138 EXPECT_EQ("0,0 600x500",
Qiang Xu07d7c9e32018-01-18 20:25:44139 screen_util::GetDisplayBoundsWithShelf(window).ToString());
oshima96f6a502015-05-02 08:43:32140}
141
Ahmed Fakhry4f8e3722017-10-31 21:01:58142TEST_F(ScreenUtilTest, ShelfDisplayBoundsInUnifiedDesktopGrid) {
Ahmed Fakhry4f8e3722017-10-31 21:01:58143 UpdateDisplay("500x400,400x600,300x600,200x300,600x200,350x400");
144 display_manager()->SetUnifiedDesktopEnabled(true);
145
146 views::Widget* widget = views::Widget::CreateWindowWithContextAndBounds(
147 NULL, CurrentContext(), gfx::Rect(10, 10, 100, 100));
148 aura::Window* window = widget->GetNativeWindow();
149
150 display::DisplayIdList list = display_manager()->GetCurrentDisplayIdList();
151 ASSERT_EQ(6u, list.size());
152 // Create a 3 x 2 vertical layout matrix and set it.
153 // [500 x 400] [400 x 600]
154 // [300 x 600] [200 x 300]
155 // [600 x 200] [350 x 400]
156 display::UnifiedDesktopLayoutMatrix matrix;
157 matrix.resize(3u);
158 matrix[0].emplace_back(list[0]);
159 matrix[0].emplace_back(list[1]);
160 matrix[1].emplace_back(list[2]);
161 matrix[1].emplace_back(list[3]);
162 matrix[2].emplace_back(list[4]);
163 matrix[2].emplace_back(list[5]);
164 display_manager()->SetUnifiedDesktopMatrix(matrix);
165 display::Screen* screen = display::Screen::GetScreen();
166 EXPECT_EQ(gfx::Size(766, 1254), screen->GetPrimaryDisplay().size());
167
Ahmed Fakhry1ac713b2018-11-06 18:57:46168 Shelf* shelf = Shell::GetPrimaryRootWindowController()->shelf();
169 EXPECT_EQ(shelf->alignment(), SHELF_ALIGNMENT_BOTTOM);
170
171 // Regardless of where the window is, the shelf with a bottom alignment is
172 // always in the bottom left display in the matrix.
173 EXPECT_EQ(gfx::Rect(0, 1057, 593, 198),
Qiang Xu07d7c9e32018-01-18 20:25:44174 screen_util::GetDisplayBoundsWithShelf(window));
Ahmed Fakhry4f8e3722017-10-31 21:01:58175
176 // Move to the bottom right display.
177 widget->SetBounds(gfx::Rect(620, 940, 100, 100));
Ahmed Fakhry1ac713b2018-11-06 18:57:46178 EXPECT_EQ(gfx::Rect(0, 1057, 593, 198),
179 screen_util::GetDisplayBoundsWithShelf(window));
180
181 // Change the shelf alignment to left, and expect that it now resides in the
182 // top left display in the matrix.
183 shelf->SetAlignment(SHELF_ALIGNMENT_LEFT);
Ahmed Fakhry4f8e3722017-10-31 21:01:58184 EXPECT_EQ(gfx::Rect(0, 0, 499, 400),
Qiang Xu07d7c9e32018-01-18 20:25:44185 screen_util::GetDisplayBoundsWithShelf(window));
Ahmed Fakhry1ac713b2018-11-06 18:57:46186
187 // Change the shelf alignment to right, and expect that it now resides in the
188 // top right display in the matrix.
189 shelf->SetAlignment(SHELF_ALIGNMENT_RIGHT);
190 EXPECT_EQ(gfx::Rect(499, 0, 267, 400),
191 screen_util::GetDisplayBoundsWithShelf(window));
192
193 // Change alignment back to bottom and change the unified display zoom factor.
194 // Expect that the display with shelf bounds will take into account the zoom
195 // factor.
196 shelf->SetAlignment(SHELF_ALIGNMENT_BOTTOM);
197 display_manager()->UpdateZoomFactor(display::kUnifiedDisplayId, 3.f);
198 const display::Display unified_display =
199 display_manager()->GetDisplayForId(display::kUnifiedDisplayId);
200 EXPECT_FLOAT_EQ(unified_display.device_scale_factor(), 3.f);
201 EXPECT_EQ(gfx::Rect(0, 352, 198, 67),
202 screen_util::GetDisplayBoundsWithShelf(window));
Ahmed Fakhry4f8e3722017-10-31 21:01:58203}
204
Malay Keshavfd6fd0c2018-07-31 22:07:47205TEST_F(ScreenUtilTest, SnapBoundsToDisplayEdge) {
206 UpdateDisplay("2400x1600*1.5");
207
208 gfx::Rect bounds(1555, 0, 45, 1066);
209 views::Widget* widget = views::Widget::CreateWindowWithContextAndBounds(
210 NULL, CurrentContext(), bounds);
211 aura::Window* window = widget->GetNativeWindow();
212
213 gfx::Rect snapped_bounds =
214 screen_util::SnapBoundsToDisplayEdge(bounds, window);
215
216 EXPECT_EQ(snapped_bounds, gfx::Rect(1555, 0, 45, 1067));
217
218 bounds = gfx::Rect(5, 1000, 1595, 66);
219 snapped_bounds = screen_util::SnapBoundsToDisplayEdge(bounds, window);
220 EXPECT_EQ(snapped_bounds, gfx::Rect(5, 1000, 1595, 67));
221
222 UpdateDisplay("800x600");
223 bounds = gfx::Rect(0, 552, 800, 48);
224 snapped_bounds = screen_util::SnapBoundsToDisplayEdge(bounds, window);
225 EXPECT_EQ(snapped_bounds, gfx::Rect(0, 552, 800, 48));
226}
227
Ahmed Fakhry5d73bdf72019-02-27 18:21:07228// Tests that making a window fullscreen while the Docked Magnifier is enabled
229// won't make its bounds occupy the entire screen bounds, but will take into
230// account the Docked Magnifier height.
231TEST_F(ScreenUtilTest, FullscreenWindowBoundsWithDockedMagnifier) {
232 UpdateDisplay("1366x768");
233
234 std::unique_ptr<aura::Window> window = CreateToplevelTestWindow(
Ahmed Fakhryf929d5f2019-04-11 02:22:48235 gfx::Rect(300, 300, 200, 150), desks_util::GetActiveDeskContainerId());
Ahmed Fakhry5d73bdf72019-02-27 18:21:07236
237 auto* docked_magnifier_controller =
238 Shell::Get()->docked_magnifier_controller();
239 docked_magnifier_controller->SetEnabled(true);
240
James Cook00e65e92019-07-25 03:19:08241 const WMEvent event(WM_EVENT_TOGGLE_FULLSCREEN);
242 WindowState::Get(window.get())->OnWMEvent(&event);
Ahmed Fakhry5d73bdf72019-02-27 18:21:07243
244 constexpr gfx::Rect kDisplayBounds{1366, 768};
245 EXPECT_NE(window->bounds(), kDisplayBounds);
246
247 gfx::Rect expected_bounds = kDisplayBounds;
248 expected_bounds.Inset(
249 0, docked_magnifier_controller->GetTotalMagnifierHeight(), 0, 0);
250 EXPECT_EQ(expected_bounds, window->bounds());
251}
252
[email protected]8d625fb2012-07-18 16:40:06253} // namespace ash