app_list_linux_unittest.cc revision 5c02ac1a9c1b504631c0a3d2b6e737b5d738bae1
1// Copyright 2013 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 "chrome/browser/ui/views/app_list/linux/app_list_linux.h"
6
7#include "chrome/browser/ui/app_list/app_list_positioner.h"
8#include "testing/gtest/include/gtest/gtest.h"
9#include "ui/gfx/display.h"
10#include "ui/gfx/point.h"
11#include "ui/gfx/rect.h"
12#include "ui/gfx/size.h"
13
14namespace {
15
16const int kScreenWidth = 800;
17const int kScreenHeight = 600;
18
19const int kWindowWidth = 100;
20const int kWindowHeight = 200;
21
22// Size of the menu bar along the top of the screen.
23const int kMenuBarSize = 22;
24// Size of the normal (non-hidden) shelf.
25const int kShelfSize = 30;
26
27// A cursor position that is within the shelf. This must be < kShelfSize.
28const int kCursorOnShelf = kShelfSize / 2;
29// A cursor position that is within kWindowWidth pixels of the shelf.
30const int kCursorNearShelfX = kShelfSize + kWindowWidth;
31// A cursor position that is more than kWindowWidth pixels away from the shelf.
32const int kCursorAwayFromShelfX = kCursorNearShelfX + 1;
33// A cursor position that is within kWindowHeight pixels of the shelf.
34const int kCursorNearShelfY = kShelfSize + kWindowHeight;
35// A cursor position that is more than kWindowHeight pixels away from the shelf.
36const int kCursorAwayFromShelfY = kCursorNearShelfY + 1;
37
38// A position for the center of the window that causes the window to overlap the
39// edge of the screen. This must be < kWindowWidth / 2 and < kWindowHeight / 2.
40const int kWindowNearEdge = kWindowWidth / 4;
41// A position for the center of the window that places the window away from all
42// edges of the screen. This must be > kWindowWidth / 2, > kWindowHeight / 2, <
43// kScreenWidth - kWindowWidth / 2 and < kScreenHeight - kWindowHeight / 2.
44const int kWindowAwayFromEdge = 158;
45
46}  // namespace
47
48class AppListLinuxUnitTest : public testing::Test {
49 public:
50  virtual void SetUp() OVERRIDE {
51    display_.set_bounds(gfx::Rect(0, 0, kScreenWidth, kScreenHeight));
52    display_.set_work_area(
53        gfx::Rect(0, kMenuBarSize, kScreenWidth, kScreenHeight - kMenuBarSize));
54    cursor_ = gfx::Point();
55  }
56
57  // Set the display work area.
58  void SetWorkArea(int x, int y, int width, int height) {
59    display_.set_work_area(gfx::Rect(x, y, width, height));
60  }
61
62  // Sets up the test environment with the shelf along a given edge of the
63  // work area.
64  void PlaceShelf(AppListPositioner::ScreenEdge edge) {
65    switch (edge) {
66      case AppListPositioner::SCREEN_EDGE_LEFT:
67        display_.set_work_area(gfx::Rect(kShelfSize,
68                                         kMenuBarSize,
69                                         kScreenWidth - kShelfSize,
70                                         kScreenHeight - kMenuBarSize));
71        break;
72      case AppListPositioner::SCREEN_EDGE_RIGHT:
73        display_.set_work_area(gfx::Rect(0,
74                                         kMenuBarSize,
75                                         kScreenWidth - kShelfSize,
76                                         kScreenHeight - kMenuBarSize));
77        break;
78      case AppListPositioner::SCREEN_EDGE_TOP:
79        display_.set_work_area(
80            gfx::Rect(0,
81                      kMenuBarSize + kShelfSize,
82                      kScreenWidth,
83                      kScreenHeight - kMenuBarSize - kShelfSize));
84        break;
85      case AppListPositioner::SCREEN_EDGE_BOTTOM:
86        display_.set_work_area(
87            gfx::Rect(0,
88                      kMenuBarSize,
89                      kScreenWidth,
90                      kScreenHeight - kMenuBarSize - kShelfSize));
91        break;
92      case AppListPositioner::SCREEN_EDGE_UNKNOWN:
93        NOTREACHED();
94        break;
95    }
96  }
97
98  // Set up the test mouse cursor in a given location.
99  void PlaceCursor(int x, int y) {
100    cursor_ = gfx::Point(x, y);
101  }
102
103  AppListPositioner::ScreenEdge ShelfEdge() const {
104    return AppListLinux::ShelfLocationInDisplay(display_);
105  }
106
107  gfx::Point DoFindAnchorPoint() const {
108    return AppListLinux::FindAnchorPoint(gfx::Size(kWindowWidth, kWindowHeight),
109                                         display_,
110                                         cursor_,
111                                         ShelfEdge());
112  }
113
114 private:
115  gfx::Display display_;
116  gfx::Point cursor_;
117};
118
119TEST_F(AppListLinuxUnitTest, ShelfLocationInDisplay) {
120  SetWorkArea(0, 0, kScreenWidth, kScreenHeight);
121  EXPECT_EQ(AppListPositioner::SCREEN_EDGE_UNKNOWN, ShelfEdge());
122
123  // The BOTTOM, LEFT and RIGHT tests test the case where there are two bars:
124  // one at the top and one at the bottom/left/right. The bigger one should be
125  // chosen, so TOP should not win in these cases.
126  PlaceShelf(AppListPositioner::SCREEN_EDGE_BOTTOM);
127  EXPECT_EQ(AppListPositioner::SCREEN_EDGE_BOTTOM, ShelfEdge());
128
129  PlaceShelf(AppListPositioner::SCREEN_EDGE_TOP);
130  EXPECT_EQ(AppListPositioner::SCREEN_EDGE_TOP, ShelfEdge());
131
132  PlaceShelf(AppListPositioner::SCREEN_EDGE_LEFT);
133  EXPECT_EQ(AppListPositioner::SCREEN_EDGE_LEFT, ShelfEdge());
134
135  PlaceShelf(AppListPositioner::SCREEN_EDGE_RIGHT);
136  EXPECT_EQ(AppListPositioner::SCREEN_EDGE_RIGHT, ShelfEdge());
137
138  // Bar at top and bottom, same size. Top should win.
139  SetWorkArea(0,
140              kMenuBarSize,
141              kScreenWidth,
142              kScreenHeight - kMenuBarSize - kMenuBarSize);
143  EXPECT_EQ(AppListPositioner::SCREEN_EDGE_TOP, ShelfEdge());
144}
145
146TEST_F(AppListLinuxUnitTest, FindAnchorPointNoShelf) {
147  // Position the app list when there is no shelf on the display.
148  PlaceCursor(0, 0);
149  // Expect the app list to be in the top-left corner.
150  EXPECT_EQ(gfx::Point(kWindowWidth / 2, kMenuBarSize + kWindowHeight / 2),
151            DoFindAnchorPoint());
152}
153
154TEST_F(AppListLinuxUnitTest, FindAnchorPointMouseOffShelf) {
155  // Position the app list when the mouse is away from the shelf.
156
157  // Bottom shelf. Expect the app list to be in the bottom-left corner.
158  PlaceShelf(AppListPositioner::SCREEN_EDGE_BOTTOM);
159  PlaceCursor(kWindowAwayFromEdge, kScreenHeight - kCursorAwayFromShelfY);
160  EXPECT_EQ(gfx::Point(kWindowWidth / 2,
161                       kScreenHeight - kShelfSize - kWindowHeight / 2),
162            DoFindAnchorPoint());
163
164  // Top shelf. Expect the app list to be in the top-left corner.
165  PlaceShelf(AppListPositioner::SCREEN_EDGE_TOP);
166  PlaceCursor(kWindowAwayFromEdge, kMenuBarSize + kCursorAwayFromShelfY);
167  EXPECT_EQ(gfx::Point(kWindowWidth / 2,
168                       kMenuBarSize + kShelfSize + kWindowHeight / 2),
169            DoFindAnchorPoint());
170
171  // Left shelf. Expect the app list to be in the top-left corner.
172  PlaceShelf(AppListPositioner::SCREEN_EDGE_LEFT);
173  PlaceCursor(kCursorAwayFromShelfX, kWindowAwayFromEdge);
174  EXPECT_EQ(gfx::Point(kShelfSize + kWindowWidth / 2,
175                       kMenuBarSize + kWindowHeight / 2),
176            DoFindAnchorPoint());
177
178  // Right shelf. Expect the app list to be in the top-right corner.
179  PlaceShelf(AppListPositioner::SCREEN_EDGE_RIGHT);
180  PlaceCursor(kScreenWidth - kCursorAwayFromShelfX, kWindowAwayFromEdge);
181  EXPECT_EQ(gfx::Point(kScreenWidth - kShelfSize - kWindowWidth / 2,
182                       kMenuBarSize + kWindowHeight / 2),
183            DoFindAnchorPoint());
184}
185
186TEST_F(AppListLinuxUnitTest, FindAnchorPointMouseOnShelf) {
187  // Position the app list when the mouse is over the shelf.
188
189  // Bottom shelf (mouse well within shelf). Expect the app list to be at
190  // the bottom centered on the mouse X coordinate.
191  PlaceShelf(AppListPositioner::SCREEN_EDGE_BOTTOM);
192  PlaceCursor(kWindowAwayFromEdge, kScreenHeight - kCursorOnShelf);
193  EXPECT_EQ(gfx::Point(kWindowAwayFromEdge,
194                       kScreenHeight - kShelfSize - kWindowHeight / 2),
195            DoFindAnchorPoint());
196
197  // Bottom shelf (outside the shelf but still close enough).
198  // Expect the app list to be at the bottom centered on the mouse X coordinate.
199  PlaceShelf(AppListPositioner::SCREEN_EDGE_BOTTOM);
200  PlaceCursor(kWindowAwayFromEdge, kScreenHeight - kCursorNearShelfY);
201  EXPECT_EQ(gfx::Point(kWindowAwayFromEdge,
202                       kScreenHeight - kShelfSize - kWindowHeight / 2),
203            DoFindAnchorPoint());
204
205  // Top shelf. Expect the app list to be at the top centered on the
206  // mouse X coordinate.
207  PlaceShelf(AppListPositioner::SCREEN_EDGE_TOP);
208  PlaceCursor(kWindowAwayFromEdge, kMenuBarSize + kCursorNearShelfY);
209  EXPECT_EQ(gfx::Point(kWindowAwayFromEdge,
210                       kMenuBarSize + kShelfSize + kWindowHeight / 2),
211            DoFindAnchorPoint());
212
213  // Left shelf. Expect the app list to be at the left centered on the
214  // mouse Y coordinate.
215  PlaceShelf(AppListPositioner::SCREEN_EDGE_LEFT);
216  PlaceCursor(kCursorNearShelfX, kWindowAwayFromEdge);
217  EXPECT_EQ(gfx::Point(kShelfSize + kWindowWidth / 2, kWindowAwayFromEdge),
218            DoFindAnchorPoint());
219
220  // Right shelf. Expect the app list to be at the right centered on the
221  // mouse Y coordinate.
222  PlaceShelf(AppListPositioner::SCREEN_EDGE_RIGHT);
223  PlaceCursor(kScreenWidth - kCursorNearShelfX, kWindowAwayFromEdge);
224  EXPECT_EQ(gfx::Point(kScreenWidth - kShelfSize - kWindowWidth / 2,
225                       kWindowAwayFromEdge),
226            DoFindAnchorPoint());
227
228  // Bottom shelf. Mouse near left edge. App list must not go off screen.
229  PlaceShelf(AppListPositioner::SCREEN_EDGE_BOTTOM);
230  PlaceCursor(kWindowNearEdge, kScreenHeight - kCursorOnShelf);
231  EXPECT_EQ(gfx::Point(kWindowWidth / 2,
232                       kScreenHeight - kShelfSize - kWindowHeight / 2),
233            DoFindAnchorPoint());
234
235  // Bottom shelf. Mouse near right edge. App list must not go off screen.
236  PlaceShelf(AppListPositioner::SCREEN_EDGE_BOTTOM);
237  PlaceCursor(kScreenWidth - kWindowNearEdge, kScreenHeight - kCursorOnShelf);
238  EXPECT_EQ(gfx::Point(kScreenWidth - kWindowWidth / 2,
239                       kScreenHeight - kShelfSize - kWindowHeight / 2),
240            DoFindAnchorPoint());
241}
242