[email protected] | 55ad8c1 | 2014-01-17 18:24:33 | [diff] [blame] | 1 | // Copyright 2014 The Chromium Authors. All rights reserved. |
[email protected] | 8d625fb | 2012-07-18 16:40:06 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
[email protected] | 55ad8c1 | 2014-01-17 18:24:33 | [diff] [blame] | 5 | #include "ash/screen_util.h" |
[email protected] | 8d625fb | 2012-07-18 16:40:06 | [diff] [blame] | 6 | |
Ahmed Fakhry | 5d73bdf7 | 2019-02-27 18:21:07 | [diff] [blame] | 7 | #include <memory> |
| 8 | |
| 9 | #include "ash/magnifier/docked_magnifier_controller.h" |
Ahmed Fakhry | 1ac713b | 2018-11-06 18:57:46 | [diff] [blame] | 10 | #include "ash/root_window_controller.h" |
| 11 | #include "ash/shelf/shelf.h" |
Manu Cornet | a76b6b0 | 2018-08-30 21:02:23 | [diff] [blame] | 12 | #include "ash/shelf/shelf_constants.h" |
[email protected] | 8d625fb | 2012-07-18 16:40:06 | [diff] [blame] | 13 | #include "ash/shell.h" |
estade | a23d6ceb | 2017-02-15 00:47:09 | [diff] [blame] | 14 | #include "ash/test/ash_test_base.h" |
Ahmed Fakhry | f929d5f | 2019-04-11 02:22:48 | [diff] [blame^] | 15 | #include "ash/wm/desks/desks_util.h" |
[email protected] | 8d625fb | 2012-07-18 16:40:06 | [diff] [blame] | 16 | #include "ash/wm/window_util.h" |
Ahmed Fakhry | 5d73bdf7 | 2019-02-27 18:21:07 | [diff] [blame] | 17 | #include "ash/wm/wm_event.h" |
[email protected] | 8d625fb | 2012-07-18 16:40:06 | [diff] [blame] | 18 | #include "ui/aura/env.h" |
[email protected] | 8d625fb | 2012-07-18 16:40:06 | [diff] [blame] | 19 | #include "ui/aura/window.h" |
[email protected] | fcc51c95 | 2014-02-21 21:31:26 | [diff] [blame] | 20 | #include "ui/aura/window_event_dispatcher.h" |
rjkroege | 72f8154f | 2016-10-29 00:49:02 | [diff] [blame] | 21 | #include "ui/display/manager/display_manager.h" |
Ahmed Fakhry | 4f8e372 | 2017-10-31 21:01:58 | [diff] [blame] | 22 | #include "ui/display/unified_desktop_utils.h" |
[email protected] | 8d625fb | 2012-07-18 16:40:06 | [diff] [blame] | 23 | #include "ui/views/widget/widget.h" |
| 24 | #include "ui/views/widget/widget_delegate.h" |
yhanada | 698af19 | 2017-02-23 10:57:07 | [diff] [blame] | 25 | #include "ui/wm/core/coordinate_conversion.h" |
[email protected] | 8d625fb | 2012-07-18 16:40:06 | [diff] [blame] | 26 | |
| 27 | namespace ash { |
[email protected] | 8d625fb | 2012-07-18 16:40:06 | [diff] [blame] | 28 | |
estade | a23d6ceb | 2017-02-15 00:47:09 | [diff] [blame] | 29 | using ScreenUtilTest = AshTestBase; |
[email protected] | 8d625fb | 2012-07-18 16:40:06 | [diff] [blame] | 30 | |
estade | a23d6ceb | 2017-02-15 00:47:09 | [diff] [blame] | 31 | TEST_F(ScreenUtilTest, Bounds) { |
[email protected] | f634dd3 | 2012-07-23 22:49:07 | [diff] [blame] | 32 | UpdateDisplay("600x600,500x500"); |
[email protected] | a2e6af1 | 2013-01-07 21:40:35 | [diff] [blame] | 33 | views::Widget* primary = views::Widget::CreateWindowWithContextAndBounds( |
| 34 | NULL, CurrentContext(), gfx::Rect(10, 10, 100, 100)); |
[email protected] | 8d625fb | 2012-07-18 16:40:06 | [diff] [blame] | 35 | primary->Show(); |
[email protected] | a2e6af1 | 2013-01-07 21:40:35 | [diff] [blame] | 36 | views::Widget* secondary = views::Widget::CreateWindowWithContextAndBounds( |
| 37 | NULL, CurrentContext(), gfx::Rect(610, 10, 100, 100)); |
[email protected] | 8d625fb | 2012-07-18 16:40:06 | [diff] [blame] | 38 | secondary->Show(); |
| 39 | |
Manu Cornet | a76b6b0 | 2018-08-30 21:02:23 | [diff] [blame] | 40 | // Maximized bounds. |
| 41 | const int bottom_inset_first = 600 - ShelfConstants::shelf_size(); |
| 42 | const int bottom_inset_second = 500 - ShelfConstants::shelf_size(); |
tdanderson | 0b7905b | 2016-06-22 21:53:03 | [diff] [blame] | 43 | EXPECT_EQ( |
Manu Cornet | a76b6b0 | 2018-08-30 21:02:23 | [diff] [blame] | 44 | gfx::Rect(0, 0, 600, bottom_inset_first).ToString(), |
Qiang Xu | 07d7c9e3 | 2018-01-18 20:25:44 | [diff] [blame] | 45 | screen_util::GetMaximizedWindowBoundsInParent(primary->GetNativeView()) |
tdanderson | 0b7905b | 2016-06-22 21:53:03 | [diff] [blame] | 46 | .ToString()); |
| 47 | EXPECT_EQ( |
Manu Cornet | a76b6b0 | 2018-08-30 21:02:23 | [diff] [blame] | 48 | gfx::Rect(0, 0, 500, bottom_inset_second).ToString(), |
Qiang Xu | 07d7c9e3 | 2018-01-18 20:25:44 | [diff] [blame] | 49 | screen_util::GetMaximizedWindowBoundsInParent(secondary->GetNativeView()) |
tdanderson | 0b7905b | 2016-06-22 21:53:03 | [diff] [blame] | 50 | .ToString()); |
[email protected] | 8d625fb | 2012-07-18 16:40:06 | [diff] [blame] | 51 | |
[email protected] | 8d625fb | 2012-07-18 16:40:06 | [diff] [blame] | 52 | // Display bounds |
| 53 | EXPECT_EQ("0,0 600x600", |
Qiang Xu | 07d7c9e3 | 2018-01-18 20:25:44 | [diff] [blame] | 54 | screen_util::GetDisplayBoundsInParent(primary->GetNativeView()) |
jamescook | b8dcef52 | 2016-06-25 14:42:55 | [diff] [blame] | 55 | .ToString()); |
[email protected] | 8d625fb | 2012-07-18 16:40:06 | [diff] [blame] | 56 | EXPECT_EQ("0,0 500x500", |
Qiang Xu | 07d7c9e3 | 2018-01-18 20:25:44 | [diff] [blame] | 57 | screen_util::GetDisplayBoundsInParent(secondary->GetNativeView()) |
jamescook | b8dcef52 | 2016-06-25 14:42:55 | [diff] [blame] | 58 | .ToString()); |
[email protected] | 8d625fb | 2012-07-18 16:40:06 | [diff] [blame] | 59 | |
| 60 | // Work area bounds |
tdanderson | 0b7905b | 2016-06-22 21:53:03 | [diff] [blame] | 61 | EXPECT_EQ( |
Manu Cornet | a76b6b0 | 2018-08-30 21:02:23 | [diff] [blame] | 62 | gfx::Rect(0, 0, 600, bottom_inset_first).ToString(), |
Qiang Xu | 07d7c9e3 | 2018-01-18 20:25:44 | [diff] [blame] | 63 | screen_util::GetDisplayWorkAreaBoundsInParent(primary->GetNativeView()) |
tdanderson | 0b7905b | 2016-06-22 21:53:03 | [diff] [blame] | 64 | .ToString()); |
| 65 | EXPECT_EQ( |
Manu Cornet | a76b6b0 | 2018-08-30 21:02:23 | [diff] [blame] | 66 | gfx::Rect(0, 0, 500, bottom_inset_second).ToString(), |
Qiang Xu | 07d7c9e3 | 2018-01-18 20:25:44 | [diff] [blame] | 67 | screen_util::GetDisplayWorkAreaBoundsInParent(secondary->GetNativeView()) |
tdanderson | 0b7905b | 2016-06-22 21:53:03 | [diff] [blame] | 68 | .ToString()); |
[email protected] | 8d625fb | 2012-07-18 16:40:06 | [diff] [blame] | 69 | } |
[email protected] | 8d625fb | 2012-07-18 16:40:06 | [diff] [blame] | 70 | |
[email protected] | 805155f | 2013-04-10 02:11:20 | [diff] [blame] | 71 | // Test verifies a stable handling of secondary screen widget changes |
| 72 | // (crbug.com/226132). |
estade | a23d6ceb | 2017-02-15 00:47:09 | [diff] [blame] | 73 | TEST_F(ScreenUtilTest, StabilityTest) { |
[email protected] | 805155f | 2013-04-10 02:11:20 | [diff] [blame] | 74 | 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], |
jamescook | b8dcef52 | 2016-06-25 14:42:55 | [diff] [blame] | 78 | secondary->GetNativeView()->GetRootWindow()); |
[email protected] | 805155f | 2013-04-10 02:11:20 | [diff] [blame] | 79 | secondary->Show(); |
| 80 | secondary->Maximize(); |
| 81 | secondary->Show(); |
| 82 | secondary->SetFullscreen(true); |
| 83 | secondary->Hide(); |
| 84 | secondary->Close(); |
| 85 | } |
| 86 | |
estade | a23d6ceb | 2017-02-15 00:47:09 | [diff] [blame] | 87 | TEST_F(ScreenUtilTest, ConvertRect) { |
[email protected] | f634dd3 | 2012-07-23 22:49:07 | [diff] [blame] | 88 | UpdateDisplay("600x600,500x500"); |
[email protected] | 8d625fb | 2012-07-18 16:40:06 | [diff] [blame] | 89 | |
[email protected] | a2e6af1 | 2013-01-07 21:40:35 | [diff] [blame] | 90 | views::Widget* primary = views::Widget::CreateWindowWithContextAndBounds( |
| 91 | NULL, CurrentContext(), gfx::Rect(10, 10, 100, 100)); |
[email protected] | 8d625fb | 2012-07-18 16:40:06 | [diff] [blame] | 92 | primary->Show(); |
[email protected] | a2e6af1 | 2013-01-07 21:40:35 | [diff] [blame] | 93 | views::Widget* secondary = views::Widget::CreateWindowWithContextAndBounds( |
| 94 | NULL, CurrentContext(), gfx::Rect(610, 10, 100, 100)); |
[email protected] | 8d625fb | 2012-07-18 16:40:06 | [diff] [blame] | 95 | secondary->Show(); |
| 96 | |
yhanada | 698af19 | 2017-02-23 10:57:07 | [diff] [blame] | 97 | gfx::Rect r1(10, 10, 100, 100); |
| 98 | ::wm::ConvertRectFromScreen(primary->GetNativeView(), &r1); |
| 99 | EXPECT_EQ("0,0 100x100", r1.ToString()); |
[email protected] | 8d625fb | 2012-07-18 16:40:06 | [diff] [blame] | 100 | |
yhanada | 698af19 | 2017-02-23 10:57:07 | [diff] [blame] | 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()); |
[email protected] | 8d625fb | 2012-07-18 16:40:06 | [diff] [blame] | 112 | } |
| 113 | |
estade | a23d6ceb | 2017-02-15 00:47:09 | [diff] [blame] | 114 | TEST_F(ScreenUtilTest, ShelfDisplayBoundsInUnifiedDesktop) { |
rjkroege | 72f8154f | 2016-10-29 00:49:02 | [diff] [blame] | 115 | display_manager()->SetUnifiedDesktopEnabled(true); |
oshima | 96f6a50 | 2015-05-02 08:43:32 | [diff] [blame] | 116 | |
| 117 | views::Widget* widget = views::Widget::CreateWindowWithContextAndBounds( |
| 118 | NULL, CurrentContext(), gfx::Rect(10, 10, 100, 100)); |
sky | 00f0b89 | 2017-05-05 17:06:24 | [diff] [blame] | 119 | aura::Window* window = widget->GetNativeWindow(); |
oshima | 96f6a50 | 2015-05-02 08:43:32 | [diff] [blame] | 120 | |
| 121 | UpdateDisplay("500x400"); |
sky | 00f0b89 | 2017-05-05 17:06:24 | [diff] [blame] | 122 | EXPECT_EQ("0,0 500x400", |
Qiang Xu | 07d7c9e3 | 2018-01-18 20:25:44 | [diff] [blame] | 123 | screen_util::GetDisplayBoundsWithShelf(window).ToString()); |
oshima | 96f6a50 | 2015-05-02 08:43:32 | [diff] [blame] | 124 | |
| 125 | UpdateDisplay("500x400,600x400"); |
sky | 00f0b89 | 2017-05-05 17:06:24 | [diff] [blame] | 126 | EXPECT_EQ("0,0 500x400", |
Qiang Xu | 07d7c9e3 | 2018-01-18 20:25:44 | [diff] [blame] | 127 | screen_util::GetDisplayBoundsWithShelf(window).ToString()); |
oshima | 96f6a50 | 2015-05-02 08:43:32 | [diff] [blame] | 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 | |
sky | 00f0b89 | 2017-05-05 17:06:24 | [diff] [blame] | 134 | EXPECT_EQ("0,0 500x400", |
Qiang Xu | 07d7c9e3 | 2018-01-18 20:25:44 | [diff] [blame] | 135 | screen_util::GetDisplayBoundsWithShelf(window).ToString()); |
oshima | 96f6a50 | 2015-05-02 08:43:32 | [diff] [blame] | 136 | |
| 137 | UpdateDisplay("600x500"); |
sky | 00f0b89 | 2017-05-05 17:06:24 | [diff] [blame] | 138 | EXPECT_EQ("0,0 600x500", |
Qiang Xu | 07d7c9e3 | 2018-01-18 20:25:44 | [diff] [blame] | 139 | screen_util::GetDisplayBoundsWithShelf(window).ToString()); |
oshima | 96f6a50 | 2015-05-02 08:43:32 | [diff] [blame] | 140 | } |
| 141 | |
Ahmed Fakhry | 4f8e372 | 2017-10-31 21:01:58 | [diff] [blame] | 142 | TEST_F(ScreenUtilTest, ShelfDisplayBoundsInUnifiedDesktopGrid) { |
Ahmed Fakhry | 4f8e372 | 2017-10-31 21:01:58 | [diff] [blame] | 143 | 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 Fakhry | 1ac713b | 2018-11-06 18:57:46 | [diff] [blame] | 168 | 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 Xu | 07d7c9e3 | 2018-01-18 20:25:44 | [diff] [blame] | 174 | screen_util::GetDisplayBoundsWithShelf(window)); |
Ahmed Fakhry | 4f8e372 | 2017-10-31 21:01:58 | [diff] [blame] | 175 | |
| 176 | // Move to the bottom right display. |
| 177 | widget->SetBounds(gfx::Rect(620, 940, 100, 100)); |
Ahmed Fakhry | 1ac713b | 2018-11-06 18:57:46 | [diff] [blame] | 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(SHELF_ALIGNMENT_LEFT); |
Ahmed Fakhry | 4f8e372 | 2017-10-31 21:01:58 | [diff] [blame] | 184 | EXPECT_EQ(gfx::Rect(0, 0, 499, 400), |
Qiang Xu | 07d7c9e3 | 2018-01-18 20:25:44 | [diff] [blame] | 185 | screen_util::GetDisplayBoundsWithShelf(window)); |
Ahmed Fakhry | 1ac713b | 2018-11-06 18:57:46 | [diff] [blame] | 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(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 Fakhry | 4f8e372 | 2017-10-31 21:01:58 | [diff] [blame] | 203 | } |
| 204 | |
Malay Keshav | fd6fd0c | 2018-07-31 22:07:47 | [diff] [blame] | 205 | TEST_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 Fakhry | 5d73bdf7 | 2019-02-27 18:21:07 | [diff] [blame] | 228 | // 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. |
| 231 | TEST_F(ScreenUtilTest, FullscreenWindowBoundsWithDockedMagnifier) { |
| 232 | UpdateDisplay("1366x768"); |
| 233 | |
| 234 | std::unique_ptr<aura::Window> window = CreateToplevelTestWindow( |
Ahmed Fakhry | f929d5f | 2019-04-11 02:22:48 | [diff] [blame^] | 235 | gfx::Rect(300, 300, 200, 150), desks_util::GetActiveDeskContainerId()); |
Ahmed Fakhry | 5d73bdf7 | 2019-02-27 18:21:07 | [diff] [blame] | 236 | |
| 237 | auto* docked_magnifier_controller = |
| 238 | Shell::Get()->docked_magnifier_controller(); |
| 239 | docked_magnifier_controller->SetEnabled(true); |
| 240 | |
| 241 | const wm::WMEvent event(wm::WM_EVENT_TOGGLE_FULLSCREEN); |
| 242 | wm::GetWindowState(window.get())->OnWMEvent(&event); |
| 243 | |
| 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] | 8d625fb | 2012-07-18 16:40:06 | [diff] [blame] | 253 | } // namespace ash |