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