1 // Copyright 2014 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/screen_util.h"
6 
7 #include <memory>
8 
9 #include "ash/magnifier/docked_magnifier_controller_impl.h"
10 #include "ash/public/cpp/shelf_config.h"
11 #include "ash/root_window_controller.h"
12 #include "ash/shelf/shelf.h"
13 #include "ash/shell.h"
14 #include "ash/test/ash_test_base.h"
15 #include "ash/wm/desks/desks_util.h"
16 #include "ash/wm/window_util.h"
17 #include "ash/wm/wm_event.h"
18 #include "ui/aura/env.h"
19 #include "ui/aura/window.h"
20 #include "ui/aura/window_event_dispatcher.h"
21 #include "ui/display/manager/display_manager.h"
22 #include "ui/display/unified_desktop_utils.h"
23 #include "ui/views/widget/widget.h"
24 #include "ui/views/widget/widget_delegate.h"
25 #include "ui/wm/core/coordinate_conversion.h"
26 
27 namespace ash {
28 
29 using ScreenUtilTest = AshTestBase;
30 
TEST_F(ScreenUtilTest,Bounds)31 TEST_F(ScreenUtilTest, Bounds) {
32   UpdateDisplay("600x600,500x500");
33   views::Widget* primary = views::Widget::CreateWindowWithContext(
34       nullptr, GetContext(), gfx::Rect(10, 10, 100, 100));
35   primary->Show();
36   views::Widget* secondary = views::Widget::CreateWindowWithContext(
37       nullptr, GetContext(), gfx::Rect(610, 10, 100, 100));
38   secondary->Show();
39 
40   // Maximized bounds.
41   const int bottom_inset_first = 600 - ShelfConfig::Get()->shelf_size();
42   const int bottom_inset_second = 500 - ShelfConfig::Get()->shelf_size();
43   EXPECT_EQ(
44       gfx::Rect(0, 0, 600, bottom_inset_first).ToString(),
45       screen_util::GetMaximizedWindowBoundsInParent(primary->GetNativeView())
46           .ToString());
47   EXPECT_EQ(
48       gfx::Rect(0, 0, 500, bottom_inset_second).ToString(),
49       screen_util::GetMaximizedWindowBoundsInParent(secondary->GetNativeView())
50           .ToString());
51 
52   // Display bounds
53   EXPECT_EQ("0,0 600x600",
54             screen_util::GetDisplayBoundsInParent(primary->GetNativeView())
55                 .ToString());
56   EXPECT_EQ("0,0 500x500",
57             screen_util::GetDisplayBoundsInParent(secondary->GetNativeView())
58                 .ToString());
59 
60   // Work area bounds
61   EXPECT_EQ(
62       gfx::Rect(0, 0, 600, bottom_inset_first).ToString(),
63       screen_util::GetDisplayWorkAreaBoundsInParent(primary->GetNativeView())
64           .ToString());
65   EXPECT_EQ(
66       gfx::Rect(0, 0, 500, bottom_inset_second).ToString(),
67       screen_util::GetDisplayWorkAreaBoundsInParent(secondary->GetNativeView())
68           .ToString());
69 }
70 
71 // Test verifies a stable handling of secondary screen widget changes
72 // (crbug.com/226132).
TEST_F(ScreenUtilTest,StabilityTest)73 TEST_F(ScreenUtilTest, StabilityTest) {
74   UpdateDisplay("600x600,500x500");
75   views::Widget* secondary = views::Widget::CreateWindowWithContext(
76       nullptr, GetContext(), gfx::Rect(610, 10, 100, 100));
77   EXPECT_EQ(Shell::GetAllRootWindows()[1],
78             secondary->GetNativeView()->GetRootWindow());
79   secondary->Show();
80   secondary->Maximize();
81   secondary->Show();
82   secondary->SetFullscreen(true);
83   secondary->Hide();
84   secondary->Close();
85 }
86 
TEST_F(ScreenUtilTest,ConvertRect)87 TEST_F(ScreenUtilTest, ConvertRect) {
88   UpdateDisplay("600x600,500x500");
89 
90   views::Widget* primary = views::Widget::CreateWindowWithContext(
91       nullptr, GetContext(), gfx::Rect(10, 10, 100, 100));
92   primary->Show();
93   views::Widget* secondary = views::Widget::CreateWindowWithContext(
94       nullptr, GetContext(), gfx::Rect(610, 10, 100, 100));
95   secondary->Show();
96 
97   gfx::Rect r1(10, 10, 100, 100);
98   ::wm::ConvertRectFromScreen(primary->GetNativeView(), &r1);
99   EXPECT_EQ("0,0 100x100", r1.ToString());
100 
101   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());
112 }
113 
TEST_F(ScreenUtilTest,ShelfDisplayBoundsInUnifiedDesktop)114 TEST_F(ScreenUtilTest, ShelfDisplayBoundsInUnifiedDesktop) {
115   display_manager()->SetUnifiedDesktopEnabled(true);
116 
117   views::Widget* widget = views::Widget::CreateWindowWithContext(
118       nullptr, GetContext(), gfx::Rect(10, 10, 100, 100));
119   aura::Window* window = widget->GetNativeWindow();
120 
121   UpdateDisplay("500x400");
122   EXPECT_EQ("0,0 500x400",
123             screen_util::GetDisplayBoundsWithShelf(window).ToString());
124 
125   UpdateDisplay("500x400,600x400");
126   EXPECT_EQ("0,0 500x400",
127             screen_util::GetDisplayBoundsWithShelf(window).ToString());
128 
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 
134   EXPECT_EQ("0,0 500x400",
135             screen_util::GetDisplayBoundsWithShelf(window).ToString());
136 
137   UpdateDisplay("600x500");
138   EXPECT_EQ("0,0 600x500",
139             screen_util::GetDisplayBoundsWithShelf(window).ToString());
140 }
141 
TEST_F(ScreenUtilTest,ShelfDisplayBoundsInUnifiedDesktopGrid)142 TEST_F(ScreenUtilTest, ShelfDisplayBoundsInUnifiedDesktopGrid) {
143   UpdateDisplay("500x400,400x600,300x600,200x300,600x200,350x400");
144   display_manager()->SetUnifiedDesktopEnabled(true);
145 
146   views::Widget* widget = views::Widget::CreateWindowWithContext(
147       nullptr, GetContext(), 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 
168   Shelf* shelf = Shell::GetPrimaryRootWindowController()->shelf();
169   EXPECT_EQ(shelf->alignment(), ShelfAlignment::kBottom);
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),
174             screen_util::GetDisplayBoundsWithShelf(window));
175 
176   // Move to the bottom right display.
177   widget->SetBounds(gfx::Rect(620, 940, 100, 100));
178   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(ShelfAlignment::kLeft);
184   EXPECT_EQ(gfx::Rect(0, 0, 499, 400),
185             screen_util::GetDisplayBoundsWithShelf(window));
186 
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(ShelfAlignment::kRight);
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(ShelfAlignment::kBottom);
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));
203 }
204 
TEST_F(ScreenUtilTest,SnapBoundsToDisplayEdge)205 TEST_F(ScreenUtilTest, SnapBoundsToDisplayEdge) {
206   UpdateDisplay("2400x1600*1.5");
207 
208   gfx::Rect bounds(1555, 0, 45, 1066);
209   views::Widget* widget =
210       views::Widget::CreateWindowWithContext(nullptr, GetContext(), 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   UpdateDisplay("2400x1800*1.8/r");
228   EXPECT_EQ(gfx::Size(1000, 1333),
229             display::Screen::GetScreen()->GetPrimaryDisplay().size());
230   bounds = gfx::Rect(950, 0, 50, 1333);
231   snapped_bounds = screen_util::SnapBoundsToDisplayEdge(bounds, window);
232   EXPECT_EQ(snapped_bounds, gfx::Rect(950, 0, 50, 1334));
233 }
234 
235 // Tests that making a window fullscreen while the Docked Magnifier is enabled
236 // won't make its bounds occupy the entire screen bounds, but will take into
237 // account the Docked Magnifier height.
TEST_F(ScreenUtilTest,FullscreenWindowBoundsWithDockedMagnifier)238 TEST_F(ScreenUtilTest, FullscreenWindowBoundsWithDockedMagnifier) {
239   UpdateDisplay("1366x768");
240 
241   std::unique_ptr<aura::Window> window = CreateToplevelTestWindow(
242       gfx::Rect(300, 300, 200, 150), desks_util::GetActiveDeskContainerId());
243 
244   auto* docked_magnifier_controller =
245       Shell::Get()->docked_magnifier_controller();
246   docked_magnifier_controller->SetEnabled(true);
247 
248   const WMEvent event(WM_EVENT_TOGGLE_FULLSCREEN);
249   WindowState::Get(window.get())->OnWMEvent(&event);
250 
251   constexpr gfx::Rect kDisplayBounds{1366, 768};
252   EXPECT_NE(window->bounds(), kDisplayBounds);
253 
254   gfx::Rect expected_bounds = kDisplayBounds;
255   expected_bounds.Inset(
256       0, docked_magnifier_controller->GetTotalMagnifierHeight(), 0, 0);
257   EXPECT_EQ(expected_bounds, window->bounds());
258 }
259 
260 }  // namespace ash
261