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 UpdateDisplay("100+100-200x200,100+500-200x200");
98 aura::Window::Windows root_windows
=
99 Shell::GetInstance()->GetAllRootWindows();
101 root_windows
[0]->GetHost()->GetBounds().origin().ToString());
103 root_windows
[0]->GetHost()->GetBounds().size().ToString());
105 root_windows
[1]->GetHost()->GetBounds().origin().ToString());
107 root_windows
[1]->GetHost()->GetBounds().size().ToString());
109 const gfx::Point
window_pos(100, 100);
110 window_
->SetBoundsInScreen(
111 gfx::Rect(window_pos
, gfx::Size(100, 100)),
112 Shell::GetScreen()->GetDisplayNearestPoint(window_pos
));
113 SetSecondaryDisplayLayout(DisplayLayout::RIGHT
);
114 // The point is on the primary root window.
115 EXPECT_EQ("50,50", ConvertHostPointToScreen(50, 50));
116 // The point is out of the all root windows.
117 EXPECT_EQ("250,250", ConvertHostPointToScreen(250, 250));
118 // The point is on the secondary display.
119 EXPECT_EQ("250,0", ConvertHostPointToScreen(50, 400));
121 SetSecondaryDisplayLayout(DisplayLayout::BOTTOM
);
122 // The point is on the primary root window.
123 EXPECT_EQ("50,50", ConvertHostPointToScreen(50, 50));
124 // The point is out of the all root windows.
125 EXPECT_EQ("250,250", ConvertHostPointToScreen(250, 250));
126 // The point is on the secondary display.
127 EXPECT_EQ("50,200", ConvertHostPointToScreen(50, 400));
129 SetSecondaryDisplayLayout(DisplayLayout::LEFT
);
130 // The point is on the primary root window.
131 EXPECT_EQ("50,50", ConvertHostPointToScreen(50, 50));
132 // The point is out of the all root windows.
133 EXPECT_EQ("250,250", ConvertHostPointToScreen(250, 250));
134 // The point is on the secondary display.
135 EXPECT_EQ("-150,0", ConvertHostPointToScreen(50, 400));
137 SetSecondaryDisplayLayout(DisplayLayout::TOP
);
138 // The point is on the primary root window.
139 EXPECT_EQ("50,50", ConvertHostPointToScreen(50, 50));
140 // The point is out of the all root windows.
141 EXPECT_EQ("250,250", ConvertHostPointToScreen(250, 250));
142 // The point is on the secondary display.
143 EXPECT_EQ("50,-200", ConvertHostPointToScreen(50, 400));
146 SetSecondaryDisplayLayout(DisplayLayout::RIGHT
);
147 const gfx::Point
window_pos2(300, 100);
148 window_
->SetBoundsInScreen(
149 gfx::Rect(window_pos2
, gfx::Size(100, 100)),
150 Shell::GetScreen()->GetDisplayNearestPoint(window_pos2
));
151 // The point is on the secondary display.
152 EXPECT_EQ("250,50", ConvertHostPointToScreen(50, 50));
153 // The point is out of the all root windows.
154 EXPECT_EQ("450,250", ConvertHostPointToScreen(250, 250));
155 // The point is on the primary root window.
156 EXPECT_EQ("50,0", ConvertHostPointToScreen(50, -400));
158 SetSecondaryDisplayLayout(DisplayLayout::BOTTOM
);
159 // The point is on the secondary display.
160 EXPECT_EQ("50,250", ConvertHostPointToScreen(50, 50));
161 // The point is out of the all root windows.
162 EXPECT_EQ("250,450", ConvertHostPointToScreen(250, 250));
163 // The point is on the primary root window.
164 EXPECT_EQ("50,0", ConvertHostPointToScreen(50, -400));
166 SetSecondaryDisplayLayout(DisplayLayout::LEFT
);
167 // The point is on the secondary display.
168 EXPECT_EQ("-150,50", ConvertHostPointToScreen(50, 50));
169 // The point is out of the all root windows.
170 EXPECT_EQ("50,250", ConvertHostPointToScreen(250, 250));
171 // The point is on the primary root window.
172 EXPECT_EQ("50,0", ConvertHostPointToScreen(50, -400));
174 SetSecondaryDisplayLayout(DisplayLayout::TOP
);
175 // The point is on the secondary display.
176 EXPECT_EQ("50,-150", ConvertHostPointToScreen(50, 50));
177 // The point is out of the all root windows.
178 EXPECT_EQ("250,50", ConvertHostPointToScreen(250, 250));
179 // The point is on the primary root window.
180 EXPECT_EQ("50,0", ConvertHostPointToScreen(50, -400));
183 TEST_F(ScreenPositionControllerTest
, MAYBE_ConvertHostPointToScreenHiDPI
) {
184 UpdateDisplay("100+100-200x200*2,100+500-200x200");
186 aura::Window::Windows root_windows
=
187 Shell::GetInstance()->GetAllRootWindows();
189 root_windows
[0]->GetHost()->
190 GetBounds().origin().ToString());
192 root_windows
[0]->GetHost()->
193 GetBounds().size().ToString());
195 root_windows
[1]->GetHost()->
196 GetBounds().origin().ToString());
198 root_windows
[1]->GetHost()->
199 GetBounds().size().ToString());
201 // Put |window_| to the primary 2x display.
202 window_
->SetBoundsInScreen(gfx::Rect(20, 20, 50, 50),
203 Shell::GetScreen()->GetPrimaryDisplay());
204 // (30, 30) means the host coordinate, so the point is still on the primary
205 // root window. Since it's 2x, the specified native point was halved.
206 EXPECT_EQ("15,15", ConvertHostPointToScreen(30, 30));
207 // Similar to above but the point is out of the all root windows.
208 EXPECT_EQ("200,200", ConvertHostPointToScreen(400, 400));
209 // Similar to above but the point is on the secondary display.
210 EXPECT_EQ("100,15", ConvertHostPointToScreen(200, 30));
212 // On secondary display. The position on the 2nd host window is (150,50)
213 // so the screen position is (100,0) + (150,50).
214 EXPECT_EQ("250,50", ConvertHostPointToScreen(150, 450));
216 // At the edge but still in the primary display. Remaining of the primary
217 // display is (50, 50) but adding ~100 since it's 2x-display.
218 EXPECT_EQ("79,79", ConvertHostPointToScreen(158, 158));
219 // At the edge of the secondary display.
220 EXPECT_EQ("80,80", ConvertHostPointToScreen(160, 160));
223 TEST_F(ScreenPositionControllerTest
, MAYBE_ConvertHostPointToScreenRotate
) {
224 // 1st display is rotated 90 clockise, and 2nd display is rotated
226 UpdateDisplay("100+100-200x200/r,100+500-200x200/l");
227 // Put |window_| to the 1st.
228 window_
->SetBoundsInScreen(gfx::Rect(20, 20, 50, 50),
229 Shell::GetScreen()->GetPrimaryDisplay());
231 // The point is on the 1st host.
232 EXPECT_EQ("70,149", ConvertHostPointToScreen(50, 70));
233 // The point is out of the host windows.
234 EXPECT_EQ("250,-51", ConvertHostPointToScreen(250, 250));
235 // The point is on the 2nd host. Point on 2nd host (30,150) -
236 // rotate 270 clockwise -> (149, 30) - layout [+(200,0)] -> (349,30).
237 EXPECT_EQ("349,30", ConvertHostPointToScreen(30, 450));
239 // Move |window_| to the 2nd.
240 window_
->SetBoundsInScreen(gfx::Rect(300, 20, 50, 50),
241 ScreenUtil::GetSecondaryDisplay());
242 aura::Window::Windows root_windows
=
243 Shell::GetInstance()->GetAllRootWindows();
244 EXPECT_EQ(root_windows
[1], window_
->GetRootWindow());
246 // The point is on the 2nd host. (50,70) on 2n host -
247 // roatate 270 clockwise -> (129,50) -layout [+(200,0)] -> (329,50)
248 EXPECT_EQ("329,50", ConvertHostPointToScreen(50, 70));
249 // The point is out of the host windows.
250 EXPECT_EQ("449,50", ConvertHostPointToScreen(50, -50));
251 // The point is on the 2nd host. Point on 2nd host (50,50) -
252 // rotate 90 clockwise -> (50, 149)
253 EXPECT_EQ("50,149", ConvertHostPointToScreen(50, -350));
256 TEST_F(ScreenPositionControllerTest
, MAYBE_ConvertHostPointToScreenUIScale
) {
257 // 1st display is 2x density with 1.5 UI scale.
258 UpdateDisplay("100+100-200x200*2@1.5,100+500-200x200");
259 // Put |window_| to the 1st.
260 window_
->SetBoundsInScreen(gfx::Rect(20, 20, 50, 50),
261 Shell::GetScreen()->GetPrimaryDisplay());
263 // The point is on the 1st host.
264 EXPECT_EQ("45,45", ConvertHostPointToScreen(60, 60));
265 // The point is out of the host windows.
266 EXPECT_EQ("45,225", ConvertHostPointToScreen(60, 300));
267 // The point is on the 2nd host. Point on 2nd host (60,150) -
268 // - screen [+(150,0)]
269 EXPECT_EQ("210,49", ConvertHostPointToScreen(60, 450));
271 // Move |window_| to the 2nd.
272 window_
->SetBoundsInScreen(gfx::Rect(300, 20, 50, 50),
273 ScreenUtil::GetSecondaryDisplay());
274 aura::Window::Windows root_windows
=
275 Shell::GetInstance()->GetAllRootWindows();
276 EXPECT_EQ(root_windows
[1], window_
->GetRootWindow());
278 // The point is on the 2nd host. (50,70) - ro
279 EXPECT_EQ("210,70", ConvertHostPointToScreen(60, 70));
280 // The point is out of the host windows.
281 EXPECT_EQ("210,-50", ConvertHostPointToScreen(60, -50));
282 // The point is on the 2nd host. Point on 1nd host (60, 60)
283 // 1/2 * 1.5 = (45,45)
284 EXPECT_EQ("45,45", ConvertHostPointToScreen(60, -340));
289 // EventHandler which tracks whether it got any MouseEvents whose location could
290 // not be converted to screen coordinates.
291 class ConvertToScreenEventHandler
: public ui::EventHandler
{
293 ConvertToScreenEventHandler() : could_convert_to_screen_(true) {
294 aura::Env::GetInstance()->AddPreTargetHandler(this);
296 ~ConvertToScreenEventHandler() override
{
297 aura::Env::GetInstance()->RemovePreTargetHandler(this);
300 bool could_convert_to_screen() const { return could_convert_to_screen_
; }
303 void OnMouseEvent(ui::MouseEvent
* event
) override
{
304 if (event
->type() == ui::ET_MOUSE_CAPTURE_CHANGED
)
308 static_cast<aura::Window
*>(event
->target())->GetRootWindow();
309 if (!aura::client::GetScreenPositionClient(root
))
310 could_convert_to_screen_
= false;
313 bool could_convert_to_screen_
;
315 DISALLOW_COPY_AND_ASSIGN(ConvertToScreenEventHandler
);
320 // Test that events are only dispatched when a ScreenPositionClient is available
321 // to convert the event to screen coordinates. The ScreenPositionClient is
322 // detached from the root window prior to the root window being destroyed. Test
323 // that no events are dispatched at this time.
324 TEST_F(ScreenPositionControllerTest
,
325 MAYBE_ConvertToScreenWhileRemovingSecondaryDisplay
) {
326 UpdateDisplay("600x600,600x600");
327 RunAllPendingInMessageLoop();
329 // Create a window on the secondary display.
330 window_
->SetBoundsInScreen(gfx::Rect(600, 0, 400, 400),
331 ScreenUtil::GetSecondaryDisplay());
333 // Move the mouse cursor over |window_|. Synthetic mouse moves are dispatched
334 // asynchronously when a window which contains the mouse cursor is destroyed.
335 // We want to check that none of these synthetic events are dispatched after
336 // ScreenPositionClient has been detached from the root window.
337 GetEventGenerator().MoveMouseTo(800, 200);
338 EXPECT_TRUE(window_
->GetBoundsInScreen().Contains(
339 aura::Env::GetInstance()->last_mouse_location()));
341 aura::Window::Windows root_windows
=
342 Shell::GetInstance()->GetAllRootWindows();
343 aura::WindowTracker tracker
;
344 tracker
.Add(root_windows
[1]);
345 scoped_ptr
<ConvertToScreenEventHandler
> event_handler(
346 new ConvertToScreenEventHandler
);
348 // Remove the secondary monitor.
349 UpdateDisplay("600x600");
351 // The secondary root window is not immediately destroyed.
352 EXPECT_TRUE(tracker
.Contains(root_windows
[1]));
354 RunAllPendingInMessageLoop();
356 // Check that we waited long enough and that the secondary root window was
358 EXPECT_FALSE(tracker
.Contains(root_windows
[1]));
360 // Check that we could convert all of the mouse events we got to screen
362 EXPECT_TRUE(event_handler
->could_convert_to_screen());