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_controller.h"
8#include "ash/screen_ash.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/root_window.h"
14#include "ui/aura/test/test_window_delegate.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  DisplayController* display_controller =
38      Shell::GetInstance()->display_controller();
39  DisplayLayout layout = display_controller->GetCurrentDisplayLayout();
40  layout.position = position;
41  display_controller->SetLayoutForCurrentDisplays(layout);
42}
43
44internal::ScreenPositionController* 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(aura::client::WINDOW_TYPE_NORMAL);
58    window_->Init(ui::LAYER_NOT_DRAWN);
59    SetDefaultParentByPrimaryRootWindow(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  Shell::RootWindowList root_windows =
91      Shell::GetInstance()->GetAllRootWindows();
92  EXPECT_EQ("100,100", root_windows[0]->GetHostOrigin().ToString());
93  EXPECT_EQ("200x200", root_windows[0]->GetHostSize().ToString());
94  EXPECT_EQ("100,500", root_windows[1]->GetHostOrigin().ToString());
95  EXPECT_EQ("200x200", root_windows[1]->GetHostSize().ToString());
96
97  const gfx::Point window_pos(100, 100);
98  window_->SetBoundsInScreen(
99      gfx::Rect(window_pos, gfx::Size(100, 100)),
100      Shell::GetScreen()->GetDisplayNearestPoint(window_pos));
101  SetSecondaryDisplayLayout(DisplayLayout::RIGHT);
102  // The point is on the primary root window.
103  EXPECT_EQ("50,50", ConvertHostPointToScreen(50, 50));
104  // The point is out of the all root windows.
105  EXPECT_EQ("250,250", ConvertHostPointToScreen(250, 250));
106  // The point is on the secondary display.
107  EXPECT_EQ("250,0", ConvertHostPointToScreen(50, 400));
108
109  SetSecondaryDisplayLayout(DisplayLayout::BOTTOM);
110  // The point is on the primary root window.
111  EXPECT_EQ("50,50", ConvertHostPointToScreen(50, 50));
112  // The point is out of the all root windows.
113  EXPECT_EQ("250,250", ConvertHostPointToScreen(250, 250));
114  // The point is on the secondary display.
115  EXPECT_EQ("50,200", ConvertHostPointToScreen(50, 400));
116
117  SetSecondaryDisplayLayout(DisplayLayout::LEFT);
118  // The point is on the primary root window.
119  EXPECT_EQ("50,50", ConvertHostPointToScreen(50, 50));
120  // The point is out of the all root windows.
121  EXPECT_EQ("250,250", ConvertHostPointToScreen(250, 250));
122  // The point is on the secondary display.
123  EXPECT_EQ("-150,0", ConvertHostPointToScreen(50, 400));
124
125  SetSecondaryDisplayLayout(DisplayLayout::TOP);
126  // The point is on the primary root window.
127  EXPECT_EQ("50,50", ConvertHostPointToScreen(50, 50));
128  // The point is out of the all root windows.
129  EXPECT_EQ("250,250", ConvertHostPointToScreen(250, 250));
130  // The point is on the secondary display.
131  EXPECT_EQ("50,-200", ConvertHostPointToScreen(50, 400));
132
133
134  SetSecondaryDisplayLayout(DisplayLayout::RIGHT);
135  const gfx::Point window_pos2(300, 100);
136  window_->SetBoundsInScreen(
137      gfx::Rect(window_pos2, gfx::Size(100, 100)),
138      Shell::GetScreen()->GetDisplayNearestPoint(window_pos2));
139  // The point is on the secondary display.
140  EXPECT_EQ("250,50", ConvertHostPointToScreen(50, 50));
141  // The point is out of the all root windows.
142  EXPECT_EQ("450,250", ConvertHostPointToScreen(250, 250));
143  // The point is on the primary root window.
144  EXPECT_EQ("50,0", ConvertHostPointToScreen(50, -400));
145
146  SetSecondaryDisplayLayout(DisplayLayout::BOTTOM);
147  // The point is on the secondary display.
148  EXPECT_EQ("50,250", ConvertHostPointToScreen(50, 50));
149  // The point is out of the all root windows.
150  EXPECT_EQ("250,450", ConvertHostPointToScreen(250, 250));
151  // The point is on the primary root window.
152  EXPECT_EQ("50,0", ConvertHostPointToScreen(50, -400));
153
154  SetSecondaryDisplayLayout(DisplayLayout::LEFT);
155  // The point is on the secondary display.
156  EXPECT_EQ("-150,50", ConvertHostPointToScreen(50, 50));
157  // The point is out of the all root windows.
158  EXPECT_EQ("50,250", ConvertHostPointToScreen(250, 250));
159  // The point is on the primary root window.
160  EXPECT_EQ("50,0", ConvertHostPointToScreen(50, -400));
161
162  SetSecondaryDisplayLayout(DisplayLayout::TOP);
163  // The point is on the secondary display.
164  EXPECT_EQ("50,-150", ConvertHostPointToScreen(50, 50));
165  // The point is out of the all root windows.
166  EXPECT_EQ("250,50", ConvertHostPointToScreen(250, 250));
167  // The point is on the primary root window.
168  EXPECT_EQ("50,0", ConvertHostPointToScreen(50, -400));
169}
170
171TEST_F(ScreenPositionControllerTest, MAYBE_ConvertHostPointToScreenHiDPI) {
172  UpdateDisplay("100+100-200x200*2,100+500-200x200");
173
174  Shell::RootWindowList root_windows =
175      Shell::GetInstance()->GetAllRootWindows();
176  EXPECT_EQ("100,100", root_windows[0]->GetHostOrigin().ToString());
177  EXPECT_EQ("200x200", root_windows[0]->GetHostSize().ToString());
178  EXPECT_EQ("100,500", root_windows[1]->GetHostOrigin().ToString());
179  EXPECT_EQ("200x200", root_windows[1]->GetHostSize().ToString());
180
181  // Put |window_| to the primary 2x display.
182  window_->SetBoundsInScreen(gfx::Rect(20, 20, 50, 50),
183                             Shell::GetScreen()->GetPrimaryDisplay());
184  // (30, 30) means the host coordinate, so the point is still on the primary
185  // root window.  Since it's 2x, the specified native point was halved.
186  EXPECT_EQ("15,15", ConvertHostPointToScreen(30, 30));
187  // Similar to above but the point is out of the all root windows.
188  EXPECT_EQ("200,200", ConvertHostPointToScreen(400, 400));
189  // Similar to above but the point is on the secondary display.
190  EXPECT_EQ("100,15", ConvertHostPointToScreen(200, 30));
191
192  // On secondary display. The position on the 2nd host window is (150,50)
193  // so the screen position is (100,0) + (150,50).
194  EXPECT_EQ("250,50", ConvertHostPointToScreen(150, 450));
195
196  // At the edge but still in the primary display.  Remaining of the primary
197  // display is (50, 50) but adding ~100 since it's 2x-display.
198  EXPECT_EQ("79,79", ConvertHostPointToScreen(158, 158));
199  // At the edge of the secondary display.
200  EXPECT_EQ("80,80", ConvertHostPointToScreen(160, 160));
201}
202
203TEST_F(ScreenPositionControllerTest, MAYBE_ConvertHostPointToScreenRotate) {
204  // 1st display is rotated 90 clockise, and 2nd display is rotated
205  // 270 clockwise.
206  UpdateDisplay("100+100-200x200/r,100+500-200x200/l");
207  // Put |window_| to the 1st.
208  window_->SetBoundsInScreen(gfx::Rect(20, 20, 50, 50),
209                             Shell::GetScreen()->GetPrimaryDisplay());
210
211  // The point is on the 1st host.
212  EXPECT_EQ("70,149", ConvertHostPointToScreen(50, 70));
213  // The point is out of the host windows.
214  EXPECT_EQ("250,-51", ConvertHostPointToScreen(250, 250));
215  // The point is on the 2nd host. Point on 2nd host (30,150) -
216  // rotate 270 clockwise -> (149, 30) - layout [+(200,0)] -> (349,30).
217  EXPECT_EQ("349,30", ConvertHostPointToScreen(30, 450));
218
219  // Move |window_| to the 2nd.
220  window_->SetBoundsInScreen(gfx::Rect(300, 20, 50, 50),
221                             ScreenAsh::GetSecondaryDisplay());
222  Shell::RootWindowList root_windows =
223      Shell::GetInstance()->GetAllRootWindows();
224  EXPECT_EQ(root_windows[1], window_->GetRootWindow());
225
226  // The point is on the 2nd host. (50,70) on 2n host -
227  // roatate 270 clockwise -> (129,50) -layout [+(200,0)] -> (329,50)
228  EXPECT_EQ("329,50", ConvertHostPointToScreen(50, 70));
229  // The point is out of the host windows.
230  EXPECT_EQ("449,50", ConvertHostPointToScreen(50, -50));
231  // The point is on the 2nd host. Point on 2nd host (50,50) -
232  // rotate 90 clockwise -> (50, 149)
233  EXPECT_EQ("50,149", ConvertHostPointToScreen(50, -350));
234}
235
236TEST_F(ScreenPositionControllerTest, MAYBE_ConvertHostPointToScreenUIScale) {
237  // 1st display is 2x density with 1.5 UI scale.
238  UpdateDisplay("100+100-200x200*2@1.5,100+500-200x200");
239  // Put |window_| to the 1st.
240  window_->SetBoundsInScreen(gfx::Rect(20, 20, 50, 50),
241                             Shell::GetScreen()->GetPrimaryDisplay());
242
243  // The point is on the 1st host.
244  EXPECT_EQ("45,45", ConvertHostPointToScreen(60, 60));
245  // The point is out of the host windows.
246  EXPECT_EQ("45,225", ConvertHostPointToScreen(60, 300));
247  // The point is on the 2nd host. Point on 2nd host (60,150) -
248  // - screen [+(150,0)]
249  EXPECT_EQ("210,49", ConvertHostPointToScreen(60, 450));
250
251  // Move |window_| to the 2nd.
252  window_->SetBoundsInScreen(gfx::Rect(300, 20, 50, 50),
253                             ScreenAsh::GetSecondaryDisplay());
254  Shell::RootWindowList root_windows =
255      Shell::GetInstance()->GetAllRootWindows();
256  EXPECT_EQ(root_windows[1], window_->GetRootWindow());
257
258  // The point is on the 2nd host. (50,70) - ro
259  EXPECT_EQ("210,70", ConvertHostPointToScreen(60, 70));
260  // The point is out of the host windows.
261  EXPECT_EQ("210,-50", ConvertHostPointToScreen(60, -50));
262  // The point is on the 2nd host. Point on 1nd host (60, 60)
263  // 1/2 * 1.5 = (45,45)
264  EXPECT_EQ("45,45", ConvertHostPointToScreen(60, -340));
265}
266
267}  // namespace test
268}  // namespace ash
269