summaryrefslogtreecommitdiffstats
path: root/ash/display/screen_position_controller_unittest.cc
blob: 440ae632b566e138e15f218ae46a1cd092194ef3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "ash/display/screen_position_controller.h"

#include "ash/display/display_controller.h"
#include "ash/shell.h"
#include "ash/test/ash_test_base.h"
#include "ui/aura/env.h"
#include "ui/aura/root_window.h"
#include "ui/aura/test/test_window_delegate.h"
#include "ui/base/layout.h"
#include "ui/gfx/screen.h"

#if defined(OS_WIN)
// TOD(mazda): UpdateDisplay does not work properly on Win.
// Fix this and enable tests.
#define MAYBE_ConvertNativePointToScreen DISABLED_ConvertNativePointToScreen
#define MAYBE_ConvertNativePointToScreenHiDPI DISABLED_ConvertNativePointToScreenHiDPI
#else
#define MAYBE_ConvertNativePointToScreen ConvertNativePointToScreen
#define MAYBE_ConvertNativePointToScreenHiDPI ConvertNativePointToScreenHiDPI
#endif

namespace ash {
namespace test {

namespace {
void SetSecondaryDisplayLayout(DisplayLayout::Position position) {
  DisplayController* display_controller =
      Shell::GetInstance()->display_controller();
  DisplayLayout layout = display_controller->default_display_layout();
  layout.position = position;
  display_controller->SetDefaultDisplayLayout(layout);
}

internal::ScreenPositionController* GetScreenPositionController() {
  Shell::TestApi test_api(Shell::GetInstance());
  return test_api.screen_position_controller();
}

class ScreenPositionControllerTest : public test::AshTestBase {
 public:
  ScreenPositionControllerTest() : window_(NULL) {}
  virtual ~ScreenPositionControllerTest() {}

  virtual void SetUp() OVERRIDE {
    AshTestBase::SetUp();
    window_.reset(new aura::Window(&window_delegate_));
    window_->SetType(aura::client::WINDOW_TYPE_NORMAL);
    window_->Init(ui::LAYER_NOT_DRAWN);
    window_->SetParent(NULL);
    window_->set_id(1);
  }

  virtual void TearDown() OVERRIDE {
    window_.reset();
    AshTestBase::TearDown();
  }

  // Converts a native point (x, y) to screen and returns its string
  // representation.
  std::string ConvertNativePointToScreen(int x, int y) const {
    gfx::Point point(x, y);
    GetScreenPositionController()->ConvertNativePointToScreen(
        window_.get(), &point);
    return point.ToString();
  }

 protected:
  scoped_ptr<aura::Window> window_;
  aura::test::TestWindowDelegate window_delegate_;

 private:
  DISALLOW_COPY_AND_ASSIGN(ScreenPositionControllerTest);
};

}  // namespace

TEST_F(ScreenPositionControllerTest, MAYBE_ConvertNativePointToScreen) {
  UpdateDisplay("100+100-200x200,100+500-200x200");

  Shell::RootWindowList root_windows =
      Shell::GetInstance()->GetAllRootWindows();
  EXPECT_EQ("100,100", root_windows[0]->GetHostOrigin().ToString());
  EXPECT_EQ("200x200", root_windows[0]->GetHostSize().ToString());
  EXPECT_EQ("100,500", root_windows[1]->GetHostOrigin().ToString());
  EXPECT_EQ("200x200", root_windows[1]->GetHostSize().ToString());

  const gfx::Point window_pos(100, 100);
  window_->SetBoundsInScreen(
      gfx::Rect(window_pos, gfx::Size(100, 100)),
      Shell::GetScreen()->GetDisplayNearestPoint(window_pos));
  SetSecondaryDisplayLayout(DisplayLayout::RIGHT);
  // The point is on the primary root window.
  EXPECT_EQ("150,150", ConvertNativePointToScreen(50, 50));
  // The point is out of the all root windows.
  EXPECT_EQ("350,350", ConvertNativePointToScreen(250, 250));
  // The point is on the secondary display.
  EXPECT_EQ("350,100", ConvertNativePointToScreen(50, 400));

  SetSecondaryDisplayLayout(DisplayLayout::BOTTOM);
  // The point is on the primary root window.
  EXPECT_EQ("150,150", ConvertNativePointToScreen(50, 50));
  // The point is out of the all root windows.
  EXPECT_EQ("350,350", ConvertNativePointToScreen(250, 250));
  // The point is on the secondary display.
  EXPECT_EQ("150,300", ConvertNativePointToScreen(50, 400));

  SetSecondaryDisplayLayout(DisplayLayout::LEFT);
  // The point is on the primary root window.
  EXPECT_EQ("150,150", ConvertNativePointToScreen(50, 50));
  // The point is out of the all root windows.
  EXPECT_EQ("350,350", ConvertNativePointToScreen(250, 250));
  // The point is on the secondary display.
  EXPECT_EQ("-50,100", ConvertNativePointToScreen(50, 400));

  SetSecondaryDisplayLayout(DisplayLayout::TOP);
  // The point is on the primary root window.
  EXPECT_EQ("150,150", ConvertNativePointToScreen(50, 50));
  // The point is out of the all root windows.
  EXPECT_EQ("350,350", ConvertNativePointToScreen(250, 250));
  // The point is on the secondary display.
  EXPECT_EQ("150,-100", ConvertNativePointToScreen(50, 400));


  SetSecondaryDisplayLayout(DisplayLayout::RIGHT);
  const gfx::Point window_pos2(300, 100);
  window_->SetBoundsInScreen(
      gfx::Rect(window_pos2, gfx::Size(100, 100)),
      Shell::GetScreen()->GetDisplayNearestPoint(window_pos2));
  // The point is on the secondary display.
  EXPECT_EQ("350,150", ConvertNativePointToScreen(50, 50));
  // The point is out of the all root windows.
  EXPECT_EQ("550,350", ConvertNativePointToScreen(250, 250));
  // The point is on the primary root window.
  EXPECT_EQ("150,100", ConvertNativePointToScreen(50, -400));

  SetSecondaryDisplayLayout(DisplayLayout::BOTTOM);
  // The point is on the secondary display.
  EXPECT_EQ("150,350", ConvertNativePointToScreen(50, 50));
  // The point is out of the all root windows.
  EXPECT_EQ("350,550", ConvertNativePointToScreen(250, 250));
  // The point is on the primary root window.
  EXPECT_EQ("150,100", ConvertNativePointToScreen(50, -400));

  SetSecondaryDisplayLayout(DisplayLayout::LEFT);
  // The point is on the secondary display.
  EXPECT_EQ("-50,150", ConvertNativePointToScreen(50, 50));
  // The point is out of the all root windows.
  EXPECT_EQ("150,350", ConvertNativePointToScreen(250, 250));
  // The point is on the primary root window.
  EXPECT_EQ("150,100", ConvertNativePointToScreen(50, -400));

  SetSecondaryDisplayLayout(DisplayLayout::TOP);
  // The point is on the secondary display.
  EXPECT_EQ("150,-50", ConvertNativePointToScreen(50, 50));
  // The point is out of the all root windows.
  EXPECT_EQ("350,150", ConvertNativePointToScreen(250, 250));
  // The point is on the primary root window.
  EXPECT_EQ("150,100", ConvertNativePointToScreen(50, -400));
}

TEST_F(ScreenPositionControllerTest, MAYBE_ConvertNativePointToScreenHiDPI) {
  UpdateDisplay("100+100-200x200*2,100+500-200x200");

  Shell::RootWindowList root_windows =
      Shell::GetInstance()->GetAllRootWindows();
  EXPECT_EQ("100,100", root_windows[0]->GetHostOrigin().ToString());
  EXPECT_EQ("200x200", root_windows[0]->GetHostSize().ToString());
  EXPECT_EQ("100,500", root_windows[1]->GetHostOrigin().ToString());
  EXPECT_EQ("200x200", root_windows[1]->GetHostSize().ToString());

  ash::DisplayController* display_controller =
      Shell::GetInstance()->display_controller();
  // Put |window_| to the primary 2x display.
  window_->SetBoundsInScreen(gfx::Rect(20, 20, 50, 50),
                             display_controller->GetPrimaryDisplay());
  // (30, 30) means the native coordinate, so the point is still on the primary
  // root window.  Since it's 2x, the specified native point was halved.
  EXPECT_EQ("35,35", ConvertNativePointToScreen(30, 30));
  // Similar to above but the point is out of the all root windows.
  EXPECT_EQ("220,220", ConvertNativePointToScreen(400, 400));
  // Similar to above but the point is on the secondary display.
  EXPECT_EQ("120,35", ConvertNativePointToScreen(200, 30));
  // At the edge but still in the primary display.  Remaining of the primary
  // display is (50, 50) but adding ~100 since it's 2x-display.
  EXPECT_EQ("99,99", ConvertNativePointToScreen(159, 159));
  // At the edge of the secondary display.
  EXPECT_EQ("100,100", ConvertNativePointToScreen(160, 160));
}

}  // namespace test
}  // namespace ash