1 // Copyright (c) 2012 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.
5 #include "ash/display/screen_position_controller.h"
7 #include "ash/display/display_manager.h"
8 #include "ash/screen_util.h"
10 #include "ash/shell_window_ids.h"
11 #include "ash/test/ash_test_base.h"
12 #include "ash/test/shell_test_api.h"
13 #include "ui/aura/env.h"
14 #include "ui/aura/test/test_window_delegate.h"
15 #include "ui/aura/window_tracker.h"
16 #include "ui/aura/window_tree_host.h"
17 #include "ui/base/layout.h"
18 #include "ui/events/test/event_generator.h"
19 #include "ui/gfx/screen.h"
22 // TODO(scottmg): RootWindow doesn't get resized immediately on Windows
23 // Ash. http://crbug.com/247916.
24 #define MAYBE_ConvertHostPointToScreen DISABLED_ConvertHostPointToScreen
25 #define MAYBE_ConvertHostPointToScreenHiDPI DISABLED_ConvertHostPointToScreenHiDPI
26 #define MAYBE_ConvertHostPointToScreenRotate DISABLED_ConvertHostPointToScreenRotate
27 #define MAYBE_ConvertHostPointToScreenUIScale DISABLED_ConvertHostPointToScreenUIScale
28 #define MAYBE_ConvertToScreenWhileRemovingSecondaryDisplay \
29 DISABLED_ConvertToScreenWhileRemovingSecondaryDisplay
31 #define MAYBE_ConvertHostPointToScreen ConvertHostPointToScreen
32 #define MAYBE_ConvertHostPointToScreenHiDPI ConvertHostPointToScreenHiDPI
33 #define MAYBE_ConvertHostPointToScreenRotate ConvertHostPointToScreenRotate
34 #define MAYBE_ConvertHostPointToScreenUIScale ConvertHostPointToScreenUIScale
35 #define MAYBE_ConvertToScreenWhileRemovingSecondaryDisplay \
36 ConvertToScreenWhileRemovingSecondaryDisplay
44 void SetSecondaryDisplayLayout(DisplayLayout::Position position
) {
45 DisplayLayout layout
=
46 Shell::GetInstance()->display_manager()->GetCurrentDisplayLayout();
47 layout
.position
= position
;
48 Shell::GetInstance()->display_manager()->
49 SetLayoutForCurrentDisplays(layout
);
52 ScreenPositionController
* GetScreenPositionController() {
53 ShellTestApi
test_api(Shell::GetInstance());
54 return test_api
.screen_position_controller();
57 class ScreenPositionControllerTest
: public test::AshTestBase
{
59 ScreenPositionControllerTest() {}
60 ~ScreenPositionControllerTest() override
{}
62 void SetUp() override
{
64 window_
.reset(new aura::Window(&window_delegate_
));
65 window_
->SetType(ui::wm::WINDOW_TYPE_NORMAL
);
66 window_
->Init(ui::LAYER_NOT_DRAWN
);
67 ParentWindowInPrimaryRootWindow(window_
.get());
71 void TearDown() override
{
73 AshTestBase::TearDown();
76 // Converts a point (x, y) in host window's coordinate to screen and
77 // returns its string representation.
78 std::string
ConvertHostPointToScreen(int x
, int y
) const {
79 gfx::Point
point(x
, y
);
80 GetScreenPositionController()->ConvertHostPointToScreen(
81 window_
->GetRootWindow(), &point
);
82 return point
.ToString();
86 scoped_ptr
<aura::Window
> window_
;
87 aura::test::TestWindowDelegate window_delegate_
;
90 DISALLOW_COPY_AND_ASSIGN(ScreenPositionControllerTest
);
95 TEST_F(ScreenPositionControllerTest
, MAYBE_ConvertHostPointToScreen
) {
96 // Make sure that the point is in host coordinates. (crbug.com/521919)
97 UpdateDisplay("100+100-200x200,100+300-200x200");
98 // The point 150,210 should be in host coords, and detected as outside.
99 EXPECT_EQ("350,10", ConvertHostPointToScreen(150, 210));
101 UpdateDisplay("100+100-200x200,100+500-200x200");
103 aura::Window::Windows root_windows
=
104 Shell::GetInstance()->GetAllRootWindows();
106 root_windows
[0]->GetHost()->GetBounds().origin().ToString());
108 root_windows
[0]->GetHost()->GetBounds().size().ToString());
110 root_windows
[1]->GetHost()->GetBounds().origin().ToString());
112 root_windows
[1]->GetHost()->GetBounds().size().ToString());
114 const gfx::Point
window_pos(100, 100);
115 window_
->SetBoundsInScreen(
116 gfx::Rect(window_pos
, gfx::Size(100, 100)),
117 Shell::GetScreen()->GetDisplayNearestPoint(window_pos
));
118 SetSecondaryDisplayLayout(DisplayLayout::RIGHT
);
119 // The point is on the primary root window.
120 EXPECT_EQ("50,50", ConvertHostPointToScreen(50, 50));
121 // The point is out of the all root windows.
122 EXPECT_EQ("250,250", ConvertHostPointToScreen(250, 250));
123 // The point is on the secondary display.
124 EXPECT_EQ("250,0", ConvertHostPointToScreen(50, 400));
126 SetSecondaryDisplayLayout(DisplayLayout::BOTTOM
);
127 // The point is on the primary root window.
128 EXPECT_EQ("50,50", ConvertHostPointToScreen(50, 50));
129 // The point is out of the all root windows.
130 EXPECT_EQ("250,250", ConvertHostPointToScreen(250, 250));
131 // The point is on the secondary display.
132 EXPECT_EQ("50,200", ConvertHostPointToScreen(50, 400));
134 SetSecondaryDisplayLayout(DisplayLayout::LEFT
);
135 // The point is on the primary root window.
136 EXPECT_EQ("50,50", ConvertHostPointToScreen(50, 50));
137 // The point is out of the all root windows.
138 EXPECT_EQ("250,250", ConvertHostPointToScreen(250, 250));
139 // The point is on the secondary display.
140 EXPECT_EQ("-150,0", ConvertHostPointToScreen(50, 400));
142 SetSecondaryDisplayLayout(DisplayLayout::TOP
);
143 // The point is on the primary root window.
144 EXPECT_EQ("50,50", ConvertHostPointToScreen(50, 50));
145 // The point is out of the all root windows.
146 EXPECT_EQ("250,250", ConvertHostPointToScreen(250, 250));
147 // The point is on the secondary display.
148 EXPECT_EQ("50,-200", ConvertHostPointToScreen(50, 400));
151 SetSecondaryDisplayLayout(DisplayLayout::RIGHT
);
152 const gfx::Point
window_pos2(300, 100);
153 window_
->SetBoundsInScreen(
154 gfx::Rect(window_pos2
, gfx::Size(100, 100)),
155 Shell::GetScreen()->GetDisplayNearestPoint(window_pos2
));
156 // The point is on the secondary display.
157 EXPECT_EQ("250,50", ConvertHostPointToScreen(50, 50));
158 // The point is out of the all root windows.
159 EXPECT_EQ("450,250", ConvertHostPointToScreen(250, 250));
160 // The point is on the primary root window.
161 EXPECT_EQ("50,0", ConvertHostPointToScreen(50, -400));
163 SetSecondaryDisplayLayout(DisplayLayout::BOTTOM
);
164 // The point is on the secondary display.
165 EXPECT_EQ("50,250", ConvertHostPointToScreen(50, 50));
166 // The point is out of the all root windows.
167 EXPECT_EQ("250,450", ConvertHostPointToScreen(250, 250));
168 // The point is on the primary root window.
169 EXPECT_EQ("50,0", ConvertHostPointToScreen(50, -400));
171 SetSecondaryDisplayLayout(DisplayLayout::LEFT
);
172 // The point is on the secondary display.
173 EXPECT_EQ("-150,50", ConvertHostPointToScreen(50, 50));
174 // The point is out of the all root windows.
175 EXPECT_EQ("50,250", ConvertHostPointToScreen(250, 250));
176 // The point is on the primary root window.
177 EXPECT_EQ("50,0", ConvertHostPointToScreen(50, -400));
179 SetSecondaryDisplayLayout(DisplayLayout::TOP
);
180 // The point is on the secondary display.
181 EXPECT_EQ("50,-150", ConvertHostPointToScreen(50, 50));
182 // The point is out of the all root windows.
183 EXPECT_EQ("250,50", ConvertHostPointToScreen(250, 250));
184 // The point is on the primary root window.
185 EXPECT_EQ("50,0", ConvertHostPointToScreen(50, -400));
188 TEST_F(ScreenPositionControllerTest
, MAYBE_ConvertHostPointToScreenHiDPI
) {
189 UpdateDisplay("50+50-200x200*2,50+300-300x300");
191 aura::Window::Windows root_windows
=
192 Shell::GetInstance()->GetAllRootWindows();
193 EXPECT_EQ("50,50 200x200",
194 root_windows
[0]->GetHost()->GetBounds().ToString());
195 EXPECT_EQ("50,300 300x300",
196 root_windows
[1]->GetHost()->GetBounds().ToString());
198 // Put |window_| to the primary 2x display.
199 window_
->SetBoundsInScreen(gfx::Rect(20, 20, 50, 50),
200 Shell::GetScreen()->GetPrimaryDisplay());
201 // (30, 30) means the host coordinate, so the point is still on the primary
202 // root window. Since it's 2x, the specified native point was halved.
203 EXPECT_EQ("15,15", ConvertHostPointToScreen(30, 30));
204 // Similar to above but the point is out of the all root windows.
205 EXPECT_EQ("200,200", ConvertHostPointToScreen(400, 400));
206 // Similar to above but the point is on the secondary display.
207 EXPECT_EQ("100,15", ConvertHostPointToScreen(200, 30));
209 // On secondary display. The position on the 2nd host window is (150,200)
210 // so the screen position is (100,0) + (150,200).
211 EXPECT_EQ("250,200", ConvertHostPointToScreen(150, 450));
213 // At the edge but still in the primary display. Remaining of the primary
214 // display is (50, 50) but adding ~100 since it's 2x-display.
215 EXPECT_EQ("79,79", ConvertHostPointToScreen(158, 158));
216 // At the edge of the secondary display.
217 EXPECT_EQ("80,80", ConvertHostPointToScreen(160, 160));
220 TEST_F(ScreenPositionControllerTest
, MAYBE_ConvertHostPointToScreenRotate
) {
221 // 1st display is rotated 90 clockise, and 2nd display is rotated
223 UpdateDisplay("100+100-200x200/r,100+500-200x200/l");
224 // Put |window_| to the 1st.
225 window_
->SetBoundsInScreen(gfx::Rect(20, 20, 50, 50),
226 Shell::GetScreen()->GetPrimaryDisplay());
228 // The point is on the 1st host.
229 EXPECT_EQ("70,149", ConvertHostPointToScreen(50, 70));
230 // The point is out of the host windows.
231 EXPECT_EQ("250,-51", ConvertHostPointToScreen(250, 250));
232 // The point is on the 2nd host. Point on 2nd host (30,150) -
233 // rotate 270 clockwise -> (149, 30) - layout [+(200,0)] -> (349,30).
234 EXPECT_EQ("349,30", ConvertHostPointToScreen(30, 450));
236 // Move |window_| to the 2nd.
237 window_
->SetBoundsInScreen(gfx::Rect(300, 20, 50, 50),
238 ScreenUtil::GetSecondaryDisplay());
239 aura::Window::Windows root_windows
=
240 Shell::GetInstance()->GetAllRootWindows();
241 EXPECT_EQ(root_windows
[1], window_
->GetRootWindow());
243 // The point is on the 2nd host. (50,70) on 2n host -
244 // roatate 270 clockwise -> (129,50) -layout [+(200,0)] -> (329,50)
245 EXPECT_EQ("329,50", ConvertHostPointToScreen(50, 70));
246 // The point is out of the host windows.
247 EXPECT_EQ("449,50", ConvertHostPointToScreen(50, -50));
248 // The point is on the 2nd host. Point on 2nd host (50,50) -
249 // rotate 90 clockwise -> (50, 149)
250 EXPECT_EQ("50,149", ConvertHostPointToScreen(50, -350));
253 TEST_F(ScreenPositionControllerTest
, MAYBE_ConvertHostPointToScreenUIScale
) {
254 // 1st display is 2x density with 1.5 UI scale.
255 UpdateDisplay("100+100-200x200*2@1.5,100+500-200x200");
256 // Put |window_| to the 1st.
257 window_
->SetBoundsInScreen(gfx::Rect(20, 20, 50, 50),
258 Shell::GetScreen()->GetPrimaryDisplay());
260 // The point is on the 1st host.
261 EXPECT_EQ("45,45", ConvertHostPointToScreen(60, 60));
262 // The point is out of the host windows.
263 EXPECT_EQ("45,225", ConvertHostPointToScreen(60, 300));
264 // The point is on the 2nd host. Point on 2nd host (60,150) -
265 // - screen [+(150,0)]
266 EXPECT_EQ("210,49", ConvertHostPointToScreen(60, 450));
268 // Move |window_| to the 2nd.
269 window_
->SetBoundsInScreen(gfx::Rect(300, 20, 50, 50),
270 ScreenUtil::GetSecondaryDisplay());
271 aura::Window::Windows root_windows
=
272 Shell::GetInstance()->GetAllRootWindows();
273 EXPECT_EQ(root_windows
[1], window_
->GetRootWindow());
275 // The point is on the 2nd host. (50,70) - ro
276 EXPECT_EQ("210,70", ConvertHostPointToScreen(60, 70));
277 // The point is out of the host windows.
278 EXPECT_EQ("210,-50", ConvertHostPointToScreen(60, -50));
279 // The point is on the 2nd host. Point on 1nd host (60, 60)
280 // 1/2 * 1.5 = (45,45)
281 EXPECT_EQ("45,45", ConvertHostPointToScreen(60, -340));
286 // EventHandler which tracks whether it got any MouseEvents whose location could
287 // not be converted to screen coordinates.
288 class ConvertToScreenEventHandler
: public ui::EventHandler
{
290 ConvertToScreenEventHandler() : could_convert_to_screen_(true) {
291 aura::Env::GetInstance()->AddPreTargetHandler(this);
293 ~ConvertToScreenEventHandler() override
{
294 aura::Env::GetInstance()->RemovePreTargetHandler(this);
297 bool could_convert_to_screen() const { return could_convert_to_screen_
; }
300 void OnMouseEvent(ui::MouseEvent
* event
) override
{
301 if (event
->type() == ui::ET_MOUSE_CAPTURE_CHANGED
)
305 static_cast<aura::Window
*>(event
->target())->GetRootWindow();
306 if (!aura::client::GetScreenPositionClient(root
))
307 could_convert_to_screen_
= false;
310 bool could_convert_to_screen_
;
312 DISALLOW_COPY_AND_ASSIGN(ConvertToScreenEventHandler
);
317 // Test that events are only dispatched when a ScreenPositionClient is available
318 // to convert the event to screen coordinates. The ScreenPositionClient is
319 // detached from the root window prior to the root window being destroyed. Test
320 // that no events are dispatched at this time.
321 TEST_F(ScreenPositionControllerTest
,
322 MAYBE_ConvertToScreenWhileRemovingSecondaryDisplay
) {
323 UpdateDisplay("600x600,600x600");
324 RunAllPendingInMessageLoop();
326 // Create a window on the secondary display.
327 window_
->SetBoundsInScreen(gfx::Rect(600, 0, 400, 400),
328 ScreenUtil::GetSecondaryDisplay());
330 // Move the mouse cursor over |window_|. Synthetic mouse moves are dispatched
331 // asynchronously when a window which contains the mouse cursor is destroyed.
332 // We want to check that none of these synthetic events are dispatched after
333 // ScreenPositionClient has been detached from the root window.
334 GetEventGenerator().MoveMouseTo(800, 200);
335 EXPECT_TRUE(window_
->GetBoundsInScreen().Contains(
336 aura::Env::GetInstance()->last_mouse_location()));
338 aura::Window::Windows root_windows
=
339 Shell::GetInstance()->GetAllRootWindows();
340 aura::WindowTracker tracker
;
341 tracker
.Add(root_windows
[1]);
342 scoped_ptr
<ConvertToScreenEventHandler
> event_handler(
343 new ConvertToScreenEventHandler
);
345 // Remove the secondary monitor.
346 UpdateDisplay("600x600");
348 // The secondary root window is not immediately destroyed.
349 EXPECT_TRUE(tracker
.Contains(root_windows
[1]));
351 RunAllPendingInMessageLoop();
353 // Check that we waited long enough and that the secondary root window was
355 EXPECT_FALSE(tracker
.Contains(root_windows
[1]));
357 // Check that we could convert all of the mouse events we got to screen
359 EXPECT_TRUE(event_handler
->could_convert_to_screen());