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.
4
5#include "ash/display/screen_position_controller.h"
6
7#include "ash/display/display_manager.h"
8#include "ash/screen_util.h"
9#include "ash/shell.h"
10#include "ash/test/ash_test_base.h"
11#include "ash/test/shell_test_api.h"
12#include "ui/aura/env.h"
13#include "ui/aura/test/test_window_delegate.h"
14#include "ui/aura/window_tree_host.h"
15#include "ui/base/layout.h"
16#include "ui/gfx/screen.h"
17
18#if defined(OS_WIN)
19// TODO(scottmg): RootWindow doesn't get resized immediately on Windows
20// Ash. http://crbug.com/247916.
21#define MAYBE_ConvertHostPointToScreen DISABLED_ConvertHostPointToScreen
22#define MAYBE_ConvertHostPointToScreenHiDPI DISABLED_ConvertHostPointToScreenHiDPI
23#define MAYBE_ConvertHostPointToScreenRotate DISABLED_ConvertHostPointToScreenRotate
24#define MAYBE_ConvertHostPointToScreenUIScale DISABLED_ConvertHostPointToScreenUIScale
25#else
26#define MAYBE_ConvertHostPointToScreen ConvertHostPointToScreen
27#define MAYBE_ConvertHostPointToScreenHiDPI ConvertHostPointToScreenHiDPI
28#define MAYBE_ConvertHostPointToScreenRotate ConvertHostPointToScreenRotate
29#define MAYBE_ConvertHostPointToScreenUIScale ConvertHostPointToScreenUIScale
30#endif
31
32namespace ash {
33namespace test {
34
35namespace {
36void SetSecondaryDisplayLayout(DisplayLayout::Position position) {
37  DisplayLayout layout =
38      Shell::GetInstance()->display_manager()->GetCurrentDisplayLayout();
39  layout.position = position;
40  Shell::GetInstance()->display_manager()->
41      SetLayoutForCurrentDisplays(layout);
42}
43
44ScreenPositionController* GetScreenPositionController() {
45  ShellTestApi test_api(Shell::GetInstance());
46  return test_api.screen_position_controller();
47}
48
49class ScreenPositionControllerTest : public test::AshTestBase {
50 public:
51  ScreenPositionControllerTest() {}
52  virtual ~ScreenPositionControllerTest() {}
53
54  virtual void SetUp() OVERRIDE {
55    AshTestBase::SetUp();
56    window_.reset(new aura::Window(&window_delegate_));
57    window_->SetType(ui::wm::WINDOW_TYPE_NORMAL);
58    window_->Init(aura::WINDOW_LAYER_NOT_DRAWN);
59    ParentWindowInPrimaryRootWindow(window_.get());
60    window_->set_id(1);
61  }
62
63  virtual void TearDown() OVERRIDE {
64    window_.reset();
65    AshTestBase::TearDown();
66  }
67
68  // Converts a point (x, y) in host window's coordinate to screen and
69  // returns its string representation.
70  std::string ConvertHostPointToScreen(int x, int y) const {
71    gfx::Point point(x, y);
72    GetScreenPositionController()->ConvertHostPointToScreen(
73        window_->GetRootWindow(), &point);
74    return point.ToString();
75  }
76
77 protected:
78  scoped_ptr<aura::Window> window_;
79  aura::test::TestWindowDelegate window_delegate_;
80
81 private:
82  DISALLOW_COPY_AND_ASSIGN(ScreenPositionControllerTest);
83};
84
85}  // namespace
86
87TEST_F(ScreenPositionControllerTest, MAYBE_ConvertHostPointToScreen) {
88  UpdateDisplay("100+100-200x200,100+500-200x200");
89
90  aura::Window::Windows root_windows =
91      Shell::GetInstance()->GetAllRootWindows();
92  EXPECT_EQ("100,100",
93            root_windows[0]->GetHost()->GetBounds().origin().ToString());
94  EXPECT_EQ("200x200",
95            root_windows[0]->GetHost()->GetBounds().size().ToString());
96  EXPECT_EQ("100,500",
97            root_windows[1]->GetHost()->GetBounds().origin().ToString());
98  EXPECT_EQ("200x200",
99            root_windows[1]->GetHost()->GetBounds().size().ToString());
100
101  const gfx::Point window_pos(100, 100);
102  window_->SetBoundsInScreen(
103      gfx::Rect(window_pos, gfx::Size(100, 100)),
104      Shell::GetScreen()->GetDisplayNearestPoint(window_pos));
105  SetSecondaryDisplayLayout(DisplayLayout::RIGHT);
106  // The point is on the primary root window.
107  EXPECT_EQ("50,50", ConvertHostPointToScreen(50, 50));
108  // The point is out of the all root windows.
109  EXPECT_EQ("250,250", ConvertHostPointToScreen(250, 250));
110  // The point is on the secondary display.
111  EXPECT_EQ("250,0", ConvertHostPointToScreen(50, 400));
112
113  SetSecondaryDisplayLayout(DisplayLayout::BOTTOM);
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("50,200", ConvertHostPointToScreen(50, 400));
120
121  SetSecondaryDisplayLayout(DisplayLayout::LEFT);
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("-150,0", ConvertHostPointToScreen(50, 400));
128
129  SetSecondaryDisplayLayout(DisplayLayout::TOP);
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("50,-200", ConvertHostPointToScreen(50, 400));
136
137
138  SetSecondaryDisplayLayout(DisplayLayout::RIGHT);
139  const gfx::Point window_pos2(300, 100);
140  window_->SetBoundsInScreen(
141      gfx::Rect(window_pos2, gfx::Size(100, 100)),
142      Shell::GetScreen()->GetDisplayNearestPoint(window_pos2));
143  // The point is on the secondary display.
144  EXPECT_EQ("250,50", ConvertHostPointToScreen(50, 50));
145  // The point is out of the all root windows.
146  EXPECT_EQ("450,250", ConvertHostPointToScreen(250, 250));
147  // The point is on the primary root window.
148  EXPECT_EQ("50,0", ConvertHostPointToScreen(50, -400));
149
150  SetSecondaryDisplayLayout(DisplayLayout::BOTTOM);
151  // The point is on the secondary display.
152  EXPECT_EQ("50,250", ConvertHostPointToScreen(50, 50));
153  // The point is out of the all root windows.
154  EXPECT_EQ("250,450", ConvertHostPointToScreen(250, 250));
155  // The point is on the primary root window.
156  EXPECT_EQ("50,0", ConvertHostPointToScreen(50, -400));
157
158  SetSecondaryDisplayLayout(DisplayLayout::LEFT);
159  // The point is on the secondary display.
160  EXPECT_EQ("-150,50", ConvertHostPointToScreen(50, 50));
161  // The point is out of the all root windows.
162  EXPECT_EQ("50,250", ConvertHostPointToScreen(250, 250));
163  // The point is on the primary root window.
164  EXPECT_EQ("50,0", ConvertHostPointToScreen(50, -400));
165
166  SetSecondaryDisplayLayout(DisplayLayout::TOP);
167  // The point is on the secondary display.
168  EXPECT_EQ("50,-150", ConvertHostPointToScreen(50, 50));
169  // The point is out of the all root windows.
170  EXPECT_EQ("250,50", ConvertHostPointToScreen(250, 250));
171  // The point is on the primary root window.
172  EXPECT_EQ("50,0", ConvertHostPointToScreen(50, -400));
173}
174
175TEST_F(ScreenPositionControllerTest, MAYBE_ConvertHostPointToScreenHiDPI) {
176  UpdateDisplay("100+100-200x200*2,100+500-200x200");
177
178  aura::Window::Windows root_windows =
179      Shell::GetInstance()->GetAllRootWindows();
180  EXPECT_EQ("100,100",
181            root_windows[0]->GetHost()->
182                GetBounds().origin().ToString());
183  EXPECT_EQ("200x200",
184            root_windows[0]->GetHost()->
185                GetBounds().size().ToString());
186  EXPECT_EQ("100,500",
187            root_windows[1]->GetHost()->
188                GetBounds().origin().ToString());
189  EXPECT_EQ("200x200",
190            root_windows[1]->GetHost()->
191                GetBounds().size().ToString());
192
193  // Put |window_| to the primary 2x display.
194  window_->SetBoundsInScreen(gfx::Rect(20, 20, 50, 50),
195                             Shell::GetScreen()->GetPrimaryDisplay());
196  // (30, 30) means the host coordinate, so the point is still on the primary
197  // root window.  Since it's 2x, the specified native point was halved.
198  EXPECT_EQ("15,15", ConvertHostPointToScreen(30, 30));
199  // Similar to above but the point is out of the all root windows.
200  EXPECT_EQ("200,200", ConvertHostPointToScreen(400, 400));
201  // Similar to above but the point is on the secondary display.
202  EXPECT_EQ("100,15", ConvertHostPointToScreen(200, 30));
203
204  // On secondary display. The position on the 2nd host window is (150,50)
205  // so the screen position is (100,0) + (150,50).
206  EXPECT_EQ("250,50", ConvertHostPointToScreen(150, 450));
207
208  // At the edge but still in the primary display.  Remaining of the primary
209  // display is (50, 50) but adding ~100 since it's 2x-display.
210  EXPECT_EQ("79,79", ConvertHostPointToScreen(158, 158));
211  // At the edge of the secondary display.
212  EXPECT_EQ("80,80", ConvertHostPointToScreen(160, 160));
213}
214
215TEST_F(ScreenPositionControllerTest, MAYBE_ConvertHostPointToScreenRotate) {
216  // 1st display is rotated 90 clockise, and 2nd display is rotated
217  // 270 clockwise.
218  UpdateDisplay("100+100-200x200/r,100+500-200x200/l");
219  // Put |window_| to the 1st.
220  window_->SetBoundsInScreen(gfx::Rect(20, 20, 50, 50),
221                             Shell::GetScreen()->GetPrimaryDisplay());
222
223  // The point is on the 1st host.
224  EXPECT_EQ("70,149", ConvertHostPointToScreen(50, 70));
225  // The point is out of the host windows.
226  EXPECT_EQ("250,-51", ConvertHostPointToScreen(250, 250));
227  // The point is on the 2nd host. Point on 2nd host (30,150) -
228  // rotate 270 clockwise -> (149, 30) - layout [+(200,0)] -> (349,30).
229  EXPECT_EQ("349,30", ConvertHostPointToScreen(30, 450));
230
231  // Move |window_| to the 2nd.
232  window_->SetBoundsInScreen(gfx::Rect(300, 20, 50, 50),
233                             ScreenUtil::GetSecondaryDisplay());
234  aura::Window::Windows root_windows =
235      Shell::GetInstance()->GetAllRootWindows();
236  EXPECT_EQ(root_windows[1], window_->GetRootWindow());
237
238  // The point is on the 2nd host. (50,70) on 2n host -
239  // roatate 270 clockwise -> (129,50) -layout [+(200,0)] -> (329,50)
240  EXPECT_EQ("329,50", ConvertHostPointToScreen(50, 70));
241  // The point is out of the host windows.
242  EXPECT_EQ("449,50", ConvertHostPointToScreen(50, -50));
243  // The point is on the 2nd host. Point on 2nd host (50,50) -
244  // rotate 90 clockwise -> (50, 149)
245  EXPECT_EQ("50,149", ConvertHostPointToScreen(50, -350));
246}
247
248TEST_F(ScreenPositionControllerTest, MAYBE_ConvertHostPointToScreenUIScale) {
249  // 1st display is 2x density with 1.5 UI scale.
250  UpdateDisplay("100+100-200x200*2@1.5,100+500-200x200");
251  // Put |window_| to the 1st.
252  window_->SetBoundsInScreen(gfx::Rect(20, 20, 50, 50),
253                             Shell::GetScreen()->GetPrimaryDisplay());
254
255  // The point is on the 1st host.
256  EXPECT_EQ("45,45", ConvertHostPointToScreen(60, 60));
257  // The point is out of the host windows.
258  EXPECT_EQ("45,225", ConvertHostPointToScreen(60, 300));
259  // The point is on the 2nd host. Point on 2nd host (60,150) -
260  // - screen [+(150,0)]
261  EXPECT_EQ("210,49", ConvertHostPointToScreen(60, 450));
262
263  // Move |window_| to the 2nd.
264  window_->SetBoundsInScreen(gfx::Rect(300, 20, 50, 50),
265                             ScreenUtil::GetSecondaryDisplay());
266  aura::Window::Windows root_windows =
267      Shell::GetInstance()->GetAllRootWindows();
268  EXPECT_EQ(root_windows[1], window_->GetRootWindow());
269
270  // The point is on the 2nd host. (50,70) - ro
271  EXPECT_EQ("210,70", ConvertHostPointToScreen(60, 70));
272  // The point is out of the host windows.
273  EXPECT_EQ("210,-50", ConvertHostPointToScreen(60, -50));
274  // The point is on the 2nd host. Point on 1nd host (60, 60)
275  // 1/2 * 1.5 = (45,45)
276  EXPECT_EQ("45,45", ConvertHostPointToScreen(60, -340));
277}
278
279}  // namespace test
280}  // namespace ash
281