blob: f155bd118d104db2c136156e455b6471f5d7255d [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
Manu Corneta76b6b02018-08-30 21:02:237#include "ash/shelf/shelf_constants.h"
[email protected]8d625fb2012-07-18 16:40:068#include "ash/shell.h"
estadea23d6ceb2017-02-15 00:47:099#include "ash/test/ash_test_base.h"
[email protected]8d625fb2012-07-18 16:40:0610#include "ash/wm/window_util.h"
11#include "ui/aura/env.h"
[email protected]8d625fb2012-07-18 16:40:0612#include "ui/aura/window.h"
[email protected]fcc51c952014-02-21 21:31:2613#include "ui/aura/window_event_dispatcher.h"
rjkroege72f8154f2016-10-29 00:49:0214#include "ui/display/manager/display_manager.h"
Ahmed Fakhry4f8e3722017-10-31 21:01:5815#include "ui/display/unified_desktop_utils.h"
[email protected]8d625fb2012-07-18 16:40:0616#include "ui/views/widget/widget.h"
17#include "ui/views/widget/widget_delegate.h"
yhanada698af192017-02-23 10:57:0718#include "ui/wm/core/coordinate_conversion.h"
[email protected]8d625fb2012-07-18 16:40:0619
20namespace ash {
[email protected]8d625fb2012-07-18 16:40:0621
estadea23d6ceb2017-02-15 00:47:0922using ScreenUtilTest = AshTestBase;
[email protected]8d625fb2012-07-18 16:40:0623
estadea23d6ceb2017-02-15 00:47:0924TEST_F(ScreenUtilTest, Bounds) {
[email protected]f634dd32012-07-23 22:49:0725 UpdateDisplay("600x600,500x500");
[email protected]a2e6af12013-01-07 21:40:3526 views::Widget* primary = views::Widget::CreateWindowWithContextAndBounds(
27 NULL, CurrentContext(), gfx::Rect(10, 10, 100, 100));
[email protected]8d625fb2012-07-18 16:40:0628 primary->Show();
[email protected]a2e6af12013-01-07 21:40:3529 views::Widget* secondary = views::Widget::CreateWindowWithContextAndBounds(
30 NULL, CurrentContext(), gfx::Rect(610, 10, 100, 100));
[email protected]8d625fb2012-07-18 16:40:0631 secondary->Show();
32
Manu Corneta76b6b02018-08-30 21:02:2333 // Maximized bounds.
34 const int bottom_inset_first = 600 - ShelfConstants::shelf_size();
35 const int bottom_inset_second = 500 - ShelfConstants::shelf_size();
tdanderson0b7905b2016-06-22 21:53:0336 EXPECT_EQ(
Manu Corneta76b6b02018-08-30 21:02:2337 gfx::Rect(0, 0, 600, bottom_inset_first).ToString(),
Qiang Xu07d7c9e32018-01-18 20:25:4438 screen_util::GetMaximizedWindowBoundsInParent(primary->GetNativeView())
tdanderson0b7905b2016-06-22 21:53:0339 .ToString());
40 EXPECT_EQ(
Manu Corneta76b6b02018-08-30 21:02:2341 gfx::Rect(0, 0, 500, bottom_inset_second).ToString(),
Qiang Xu07d7c9e32018-01-18 20:25:4442 screen_util::GetMaximizedWindowBoundsInParent(secondary->GetNativeView())
tdanderson0b7905b2016-06-22 21:53:0343 .ToString());
[email protected]8d625fb2012-07-18 16:40:0644
[email protected]8d625fb2012-07-18 16:40:0645 // Display bounds
46 EXPECT_EQ("0,0 600x600",
Qiang Xu07d7c9e32018-01-18 20:25:4447 screen_util::GetDisplayBoundsInParent(primary->GetNativeView())
jamescookb8dcef522016-06-25 14:42:5548 .ToString());
[email protected]8d625fb2012-07-18 16:40:0649 EXPECT_EQ("0,0 500x500",
Qiang Xu07d7c9e32018-01-18 20:25:4450 screen_util::GetDisplayBoundsInParent(secondary->GetNativeView())
jamescookb8dcef522016-06-25 14:42:5551 .ToString());
[email protected]8d625fb2012-07-18 16:40:0652
53 // Work area bounds
tdanderson0b7905b2016-06-22 21:53:0354 EXPECT_EQ(
Manu Corneta76b6b02018-08-30 21:02:2355 gfx::Rect(0, 0, 600, bottom_inset_first).ToString(),
Qiang Xu07d7c9e32018-01-18 20:25:4456 screen_util::GetDisplayWorkAreaBoundsInParent(primary->GetNativeView())
tdanderson0b7905b2016-06-22 21:53:0357 .ToString());
58 EXPECT_EQ(
Manu Corneta76b6b02018-08-30 21:02:2359 gfx::Rect(0, 0, 500, bottom_inset_second).ToString(),
Qiang Xu07d7c9e32018-01-18 20:25:4460 screen_util::GetDisplayWorkAreaBoundsInParent(secondary->GetNativeView())
tdanderson0b7905b2016-06-22 21:53:0361 .ToString());
[email protected]8d625fb2012-07-18 16:40:0662}
[email protected]8d625fb2012-07-18 16:40:0663
[email protected]805155f2013-04-10 02:11:2064// Test verifies a stable handling of secondary screen widget changes
65// (crbug.com/226132).
estadea23d6ceb2017-02-15 00:47:0966TEST_F(ScreenUtilTest, StabilityTest) {
[email protected]805155f2013-04-10 02:11:2067 UpdateDisplay("600x600,500x500");
68 views::Widget* secondary = views::Widget::CreateWindowWithContextAndBounds(
69 NULL, CurrentContext(), gfx::Rect(610, 10, 100, 100));
70 EXPECT_EQ(Shell::GetAllRootWindows()[1],
jamescookb8dcef522016-06-25 14:42:5571 secondary->GetNativeView()->GetRootWindow());
[email protected]805155f2013-04-10 02:11:2072 secondary->Show();
73 secondary->Maximize();
74 secondary->Show();
75 secondary->SetFullscreen(true);
76 secondary->Hide();
77 secondary->Close();
78}
79
estadea23d6ceb2017-02-15 00:47:0980TEST_F(ScreenUtilTest, ConvertRect) {
[email protected]f634dd32012-07-23 22:49:0781 UpdateDisplay("600x600,500x500");
[email protected]8d625fb2012-07-18 16:40:0682
[email protected]a2e6af12013-01-07 21:40:3583 views::Widget* primary = views::Widget::CreateWindowWithContextAndBounds(
84 NULL, CurrentContext(), gfx::Rect(10, 10, 100, 100));
[email protected]8d625fb2012-07-18 16:40:0685 primary->Show();
[email protected]a2e6af12013-01-07 21:40:3586 views::Widget* secondary = views::Widget::CreateWindowWithContextAndBounds(
87 NULL, CurrentContext(), gfx::Rect(610, 10, 100, 100));
[email protected]8d625fb2012-07-18 16:40:0688 secondary->Show();
89
yhanada698af192017-02-23 10:57:0790 gfx::Rect r1(10, 10, 100, 100);
91 ::wm::ConvertRectFromScreen(primary->GetNativeView(), &r1);
92 EXPECT_EQ("0,0 100x100", r1.ToString());
[email protected]8d625fb2012-07-18 16:40:0693
yhanada698af192017-02-23 10:57:0794 gfx::Rect r2(620, 20, 100, 100);
95 ::wm::ConvertRectFromScreen(secondary->GetNativeView(), &r2);
96 EXPECT_EQ("10,10 100x100", r2.ToString());
97
98 gfx::Rect r3(30, 30, 100, 100);
99 ::wm::ConvertRectToScreen(primary->GetNativeView(), &r3);
100 EXPECT_EQ("40,40 100x100", r3.ToString());
101
102 gfx::Rect r4(40, 40, 100, 100);
103 ::wm::ConvertRectToScreen(secondary->GetNativeView(), &r4);
104 EXPECT_EQ("650,50 100x100", r4.ToString());
[email protected]8d625fb2012-07-18 16:40:06105}
106
estadea23d6ceb2017-02-15 00:47:09107TEST_F(ScreenUtilTest, ShelfDisplayBoundsInUnifiedDesktop) {
rjkroege72f8154f2016-10-29 00:49:02108 display_manager()->SetUnifiedDesktopEnabled(true);
oshima96f6a502015-05-02 08:43:32109
110 views::Widget* widget = views::Widget::CreateWindowWithContextAndBounds(
111 NULL, CurrentContext(), gfx::Rect(10, 10, 100, 100));
sky00f0b892017-05-05 17:06:24112 aura::Window* window = widget->GetNativeWindow();
oshima96f6a502015-05-02 08:43:32113
114 UpdateDisplay("500x400");
sky00f0b892017-05-05 17:06:24115 EXPECT_EQ("0,0 500x400",
Qiang Xu07d7c9e32018-01-18 20:25:44116 screen_util::GetDisplayBoundsWithShelf(window).ToString());
oshima96f6a502015-05-02 08:43:32117
118 UpdateDisplay("500x400,600x400");
sky00f0b892017-05-05 17:06:24119 EXPECT_EQ("0,0 500x400",
Qiang Xu07d7c9e32018-01-18 20:25:44120 screen_util::GetDisplayBoundsWithShelf(window).ToString());
oshima96f6a502015-05-02 08:43:32121
122 // Move to the 2nd physical display. Shelf's display still should be
123 // the first.
124 widget->SetBounds(gfx::Rect(800, 0, 100, 100));
125 ASSERT_EQ("800,0 100x100", widget->GetWindowBoundsInScreen().ToString());
126
sky00f0b892017-05-05 17:06:24127 EXPECT_EQ("0,0 500x400",
Qiang Xu07d7c9e32018-01-18 20:25:44128 screen_util::GetDisplayBoundsWithShelf(window).ToString());
oshima96f6a502015-05-02 08:43:32129
130 UpdateDisplay("600x500");
sky00f0b892017-05-05 17:06:24131 EXPECT_EQ("0,0 600x500",
Qiang Xu07d7c9e32018-01-18 20:25:44132 screen_util::GetDisplayBoundsWithShelf(window).ToString());
oshima96f6a502015-05-02 08:43:32133}
134
Ahmed Fakhry4f8e3722017-10-31 21:01:58135TEST_F(ScreenUtilTest, ShelfDisplayBoundsInUnifiedDesktopGrid) {
Ahmed Fakhry4f8e3722017-10-31 21:01:58136 UpdateDisplay("500x400,400x600,300x600,200x300,600x200,350x400");
137 display_manager()->SetUnifiedDesktopEnabled(true);
138
139 views::Widget* widget = views::Widget::CreateWindowWithContextAndBounds(
140 NULL, CurrentContext(), gfx::Rect(10, 10, 100, 100));
141 aura::Window* window = widget->GetNativeWindow();
142
143 display::DisplayIdList list = display_manager()->GetCurrentDisplayIdList();
144 ASSERT_EQ(6u, list.size());
145 // Create a 3 x 2 vertical layout matrix and set it.
146 // [500 x 400] [400 x 600]
147 // [300 x 600] [200 x 300]
148 // [600 x 200] [350 x 400]
149 display::UnifiedDesktopLayoutMatrix matrix;
150 matrix.resize(3u);
151 matrix[0].emplace_back(list[0]);
152 matrix[0].emplace_back(list[1]);
153 matrix[1].emplace_back(list[2]);
154 matrix[1].emplace_back(list[3]);
155 matrix[2].emplace_back(list[4]);
156 matrix[2].emplace_back(list[5]);
157 display_manager()->SetUnifiedDesktopMatrix(matrix);
158 display::Screen* screen = display::Screen::GetScreen();
159 EXPECT_EQ(gfx::Size(766, 1254), screen->GetPrimaryDisplay().size());
160
161 // Regardless of where the window is, the shelf is always in the top left
162 // display in the matrix.
163 EXPECT_EQ(gfx::Rect(0, 0, 499, 400),
Qiang Xu07d7c9e32018-01-18 20:25:44164 screen_util::GetDisplayBoundsWithShelf(window));
Ahmed Fakhry4f8e3722017-10-31 21:01:58165
166 // Move to the bottom right display.
167 widget->SetBounds(gfx::Rect(620, 940, 100, 100));
168 EXPECT_EQ(gfx::Rect(0, 0, 499, 400),
Qiang Xu07d7c9e32018-01-18 20:25:44169 screen_util::GetDisplayBoundsWithShelf(window));
Ahmed Fakhry4f8e3722017-10-31 21:01:58170}
171
Malay Keshavfd6fd0c2018-07-31 22:07:47172TEST_F(ScreenUtilTest, SnapBoundsToDisplayEdge) {
173 UpdateDisplay("2400x1600*1.5");
174
175 gfx::Rect bounds(1555, 0, 45, 1066);
176 views::Widget* widget = views::Widget::CreateWindowWithContextAndBounds(
177 NULL, CurrentContext(), bounds);
178 aura::Window* window = widget->GetNativeWindow();
179
180 gfx::Rect snapped_bounds =
181 screen_util::SnapBoundsToDisplayEdge(bounds, window);
182
183 EXPECT_EQ(snapped_bounds, gfx::Rect(1555, 0, 45, 1067));
184
185 bounds = gfx::Rect(5, 1000, 1595, 66);
186 snapped_bounds = screen_util::SnapBoundsToDisplayEdge(bounds, window);
187 EXPECT_EQ(snapped_bounds, gfx::Rect(5, 1000, 1595, 67));
188
189 UpdateDisplay("800x600");
190 bounds = gfx::Rect(0, 552, 800, 48);
191 snapped_bounds = screen_util::SnapBoundsToDisplayEdge(bounds, window);
192 EXPECT_EQ(snapped_bounds, gfx::Rect(0, 552, 800, 48));
193}
194
[email protected]8d625fb2012-07-18 16:40:06195} // namespace ash