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_controller.h"
9 #include "ash/test/ash_test_base.h"
10 #include "ash/test/shell_test_api.h"
11 #include "ui/aura/env.h"
12 #include "ui/aura/root_window.h"
13 #include "ui/aura/test/test_window_delegate.h"
14 #include "ui/base/layout.h"
15 #include "ui/gfx/screen.h"
18 // TOD(mazda): UpdateDisplay does not work properly on Win.
19 // Fix this and enable tests.
20 #define MAYBE_ConvertNativePointToScreen DISABLED_ConvertNativePointToScreen
21 #define MAYBE_ConvertNativePointToScreenHiDPI DISABLED_ConvertNativePointToScreenHiDPI
23 #define MAYBE_ConvertNativePointToScreen ConvertNativePointToScreen
24 #define MAYBE_ConvertNativePointToScreenHiDPI ConvertNativePointToScreenHiDPI
31 void SetSecondaryDisplayLayout(DisplayLayout::Position position
) {
32 DisplayController
* display_controller
=
33 Shell::GetInstance()->display_controller();
34 DisplayLayout layout
= display_controller
->default_display_layout();
35 layout
.position
= position
;
36 display_controller
->SetDefaultDisplayLayout(layout
);
39 internal::ScreenPositionController
* GetScreenPositionController() {
40 ShellTestApi
test_api(Shell::GetInstance());
41 return test_api
.screen_position_controller();
44 class ScreenPositionControllerTest
: public test::AshTestBase
{
46 ScreenPositionControllerTest() : window_(NULL
) {}
47 virtual ~ScreenPositionControllerTest() {}
49 virtual void SetUp() OVERRIDE
{
51 window_
.reset(new aura::Window(&window_delegate_
));
52 window_
->SetType(aura::client::WINDOW_TYPE_NORMAL
);
53 window_
->Init(ui::LAYER_NOT_DRAWN
);
54 SetDefaultParentByPrimaryRootWindow(window_
.get());
58 virtual void TearDown() OVERRIDE
{
60 AshTestBase::TearDown();
63 // Converts a native point (x, y) to screen and returns its string
65 std::string
ConvertNativePointToScreen(int x
, int y
) const {
66 gfx::Point
point(x
, y
);
67 GetScreenPositionController()->ConvertNativePointToScreen(
68 window_
.get(), &point
);
69 return point
.ToString();
73 scoped_ptr
<aura::Window
> window_
;
74 aura::test::TestWindowDelegate window_delegate_
;
77 DISALLOW_COPY_AND_ASSIGN(ScreenPositionControllerTest
);
82 TEST_F(ScreenPositionControllerTest
, MAYBE_ConvertNativePointToScreen
) {
83 UpdateDisplay("100+100-200x200,100+500-200x200");
85 Shell::RootWindowList root_windows
=
86 Shell::GetInstance()->GetAllRootWindows();
87 EXPECT_EQ("100,100", root_windows
[0]->GetHostOrigin().ToString());
88 EXPECT_EQ("200x200", root_windows
[0]->GetHostSize().ToString());
89 EXPECT_EQ("100,500", root_windows
[1]->GetHostOrigin().ToString());
90 EXPECT_EQ("200x200", root_windows
[1]->GetHostSize().ToString());
92 const gfx::Point
window_pos(100, 100);
93 window_
->SetBoundsInScreen(
94 gfx::Rect(window_pos
, gfx::Size(100, 100)),
95 Shell::GetScreen()->GetDisplayNearestPoint(window_pos
));
96 SetSecondaryDisplayLayout(DisplayLayout::RIGHT
);
97 // The point is on the primary root window.
98 EXPECT_EQ("150,150", ConvertNativePointToScreen(50, 50));
99 // The point is out of the all root windows.
100 EXPECT_EQ("350,350", ConvertNativePointToScreen(250, 250));
101 // The point is on the secondary display.
102 EXPECT_EQ("350,100", ConvertNativePointToScreen(50, 400));
104 SetSecondaryDisplayLayout(DisplayLayout::BOTTOM
);
105 // The point is on the primary root window.
106 EXPECT_EQ("150,150", ConvertNativePointToScreen(50, 50));
107 // The point is out of the all root windows.
108 EXPECT_EQ("350,350", ConvertNativePointToScreen(250, 250));
109 // The point is on the secondary display.
110 EXPECT_EQ("150,300", ConvertNativePointToScreen(50, 400));
112 SetSecondaryDisplayLayout(DisplayLayout::LEFT
);
113 // The point is on the primary root window.
114 EXPECT_EQ("150,150", ConvertNativePointToScreen(50, 50));
115 // The point is out of the all root windows.
116 EXPECT_EQ("350,350", ConvertNativePointToScreen(250, 250));
117 // The point is on the secondary display.
118 EXPECT_EQ("-50,100", ConvertNativePointToScreen(50, 400));
120 SetSecondaryDisplayLayout(DisplayLayout::TOP
);
121 // The point is on the primary root window.
122 EXPECT_EQ("150,150", ConvertNativePointToScreen(50, 50));
123 // The point is out of the all root windows.
124 EXPECT_EQ("350,350", ConvertNativePointToScreen(250, 250));
125 // The point is on the secondary display.
126 EXPECT_EQ("150,-100", ConvertNativePointToScreen(50, 400));
129 SetSecondaryDisplayLayout(DisplayLayout::RIGHT
);
130 const gfx::Point
window_pos2(300, 100);
131 window_
->SetBoundsInScreen(
132 gfx::Rect(window_pos2
, gfx::Size(100, 100)),
133 Shell::GetScreen()->GetDisplayNearestPoint(window_pos2
));
134 // The point is on the secondary display.
135 EXPECT_EQ("350,150", ConvertNativePointToScreen(50, 50));
136 // The point is out of the all root windows.
137 EXPECT_EQ("550,350", ConvertNativePointToScreen(250, 250));
138 // The point is on the primary root window.
139 EXPECT_EQ("150,100", ConvertNativePointToScreen(50, -400));
141 SetSecondaryDisplayLayout(DisplayLayout::BOTTOM
);
142 // The point is on the secondary display.
143 EXPECT_EQ("150,350", ConvertNativePointToScreen(50, 50));
144 // The point is out of the all root windows.
145 EXPECT_EQ("350,550", ConvertNativePointToScreen(250, 250));
146 // The point is on the primary root window.
147 EXPECT_EQ("150,100", ConvertNativePointToScreen(50, -400));
149 SetSecondaryDisplayLayout(DisplayLayout::LEFT
);
150 // The point is on the secondary display.
151 EXPECT_EQ("-50,150", ConvertNativePointToScreen(50, 50));
152 // The point is out of the all root windows.
153 EXPECT_EQ("150,350", ConvertNativePointToScreen(250, 250));
154 // The point is on the primary root window.
155 EXPECT_EQ("150,100", ConvertNativePointToScreen(50, -400));
157 SetSecondaryDisplayLayout(DisplayLayout::TOP
);
158 // The point is on the secondary display.
159 EXPECT_EQ("150,-50", ConvertNativePointToScreen(50, 50));
160 // The point is out of the all root windows.
161 EXPECT_EQ("350,150", ConvertNativePointToScreen(250, 250));
162 // The point is on the primary root window.
163 EXPECT_EQ("150,100", ConvertNativePointToScreen(50, -400));
166 TEST_F(ScreenPositionControllerTest
, MAYBE_ConvertNativePointToScreenHiDPI
) {
167 UpdateDisplay("100+100-200x200*2,100+500-200x200");
169 Shell::RootWindowList root_windows
=
170 Shell::GetInstance()->GetAllRootWindows();
171 EXPECT_EQ("100,100", root_windows
[0]->GetHostOrigin().ToString());
172 EXPECT_EQ("200x200", root_windows
[0]->GetHostSize().ToString());
173 EXPECT_EQ("100,500", root_windows
[1]->GetHostOrigin().ToString());
174 EXPECT_EQ("200x200", root_windows
[1]->GetHostSize().ToString());
176 ash::DisplayController
* display_controller
=
177 Shell::GetInstance()->display_controller();
178 // Put |window_| to the primary 2x display.
179 window_
->SetBoundsInScreen(gfx::Rect(20, 20, 50, 50),
180 display_controller
->GetPrimaryDisplay());
181 // (30, 30) means the native coordinate, so the point is still on the primary
182 // root window. Since it's 2x, the specified native point was halved.
183 EXPECT_EQ("35,35", ConvertNativePointToScreen(30, 30));
184 // Similar to above but the point is out of the all root windows.
185 EXPECT_EQ("220,220", ConvertNativePointToScreen(400, 400));
186 // Similar to above but the point is on the secondary display.
187 EXPECT_EQ("120,35", ConvertNativePointToScreen(200, 30));
188 // At the edge but still in the primary display. Remaining of the primary
189 // display is (50, 50) but adding ~100 since it's 2x-display.
190 EXPECT_EQ("99,99", ConvertNativePointToScreen(159, 159));
191 // At the edge of the secondary display.
192 EXPECT_EQ("100,100", ConvertNativePointToScreen(160, 160));