OLD | NEW |
| (Empty) |
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/app_list/app_list_positioner.h" | |
6 | |
7 #include <memory> | |
8 | |
9 #include "testing/gtest/include/gtest/gtest.h" | |
10 | |
11 namespace { | |
12 | |
13 const int kScreenWidth = 800; | |
14 const int kScreenHeight = 600; | |
15 | |
16 const int kWindowWidth = 100; | |
17 const int kWindowHeight = 200; | |
18 | |
19 // Size of the menu bar along the top of the screen. | |
20 const int kMenuBarSize = 22; | |
21 // Size of the normal (non-hidden) shelf. | |
22 const int kShelfSize = 30; | |
23 | |
24 // The distance the shelf will appear from the edge of the screen. | |
25 const int kMinDistanceFromEdge = 3; | |
26 | |
27 // A cursor position that is within the shelf. This must be < kShelfSize. | |
28 const int kCursorOnShelf = kShelfSize / 2; | |
29 // A cursor position that should be ignored. | |
30 const int kCursorIgnore = -300; | |
31 | |
32 // A position for the center of the window that causes the window to overlap the | |
33 // edge of the screen. This must be < kWindowWidth / 2 and < kWindowHeight / 2. | |
34 const int kWindowNearEdge = kWindowWidth / 4; | |
35 // A position for the center of the window that places the window away from all | |
36 // edges of the screen. This must be > kWindowWidth / 2, > kWindowHeight / 2, < | |
37 // kScreenWidth - kWindowWidth / 2 and < kScreenHeight - kWindowHeight / 2. | |
38 const int kWindowAwayFromEdge = 158; | |
39 | |
40 } // namespace | |
41 | |
42 class AppListPositionerUnitTest : public testing::Test { | |
43 public: | |
44 void ResetPositioner() { | |
45 gfx::Size view_size(kWindowWidth, kWindowHeight); | |
46 positioner_.reset( | |
47 new AppListPositioner(display_, view_size, kMinDistanceFromEdge)); | |
48 } | |
49 | |
50 void SetUp() override { | |
51 display_.set_bounds(gfx::Rect(0, 0, kScreenWidth, kScreenHeight)); | |
52 // Assume there is a menu bar at the top of the screen, as on Mac and Unity. | |
53 // This is for cases where the work area does not fill the entire screen. | |
54 display_.set_work_area( | |
55 gfx::Rect(0, kMenuBarSize, kScreenWidth, kScreenHeight - kMenuBarSize)); | |
56 ResetPositioner(); | |
57 cursor_ = gfx::Point(); | |
58 } | |
59 | |
60 // Sets up the test environment with the shelf along a given edge of the work | |
61 // area. | |
62 void PlaceShelf(AppListPositioner::ScreenEdge edge) { | |
63 ResetPositioner(); | |
64 switch (edge) { | |
65 case AppListPositioner::SCREEN_EDGE_UNKNOWN: | |
66 break; | |
67 case AppListPositioner::SCREEN_EDGE_LEFT: | |
68 positioner_->WorkAreaInset(kShelfSize, 0, 0, 0); | |
69 break; | |
70 case AppListPositioner::SCREEN_EDGE_RIGHT: | |
71 positioner_->WorkAreaInset(0, 0, kShelfSize, 0); | |
72 break; | |
73 case AppListPositioner::SCREEN_EDGE_TOP: | |
74 positioner_->WorkAreaInset(0, kShelfSize, 0, 0); | |
75 break; | |
76 case AppListPositioner::SCREEN_EDGE_BOTTOM: | |
77 positioner_->WorkAreaInset(0, 0, 0, kShelfSize); | |
78 break; | |
79 } | |
80 } | |
81 | |
82 // Set up the test mouse cursor in a given location. | |
83 void PlaceCursor(int x, int y) { | |
84 cursor_ = gfx::Point(x, y); | |
85 } | |
86 | |
87 gfx::Point DoGetAnchorPointForScreenCorner( | |
88 AppListPositioner::ScreenCorner corner) const { | |
89 return positioner_->GetAnchorPointForScreenCorner(corner); | |
90 } | |
91 | |
92 gfx::Point DoGetAnchorPointForShelfCorner( | |
93 AppListPositioner::ScreenEdge shelf_edge) const { | |
94 return positioner_->GetAnchorPointForShelfCorner(shelf_edge); | |
95 } | |
96 | |
97 gfx::Point DoGetAnchorPointForShelfCenter( | |
98 AppListPositioner::ScreenEdge shelf_edge) const { | |
99 return positioner_->GetAnchorPointForShelfCenter(shelf_edge); | |
100 } | |
101 | |
102 gfx::Point DoGetAnchorPointForShelfCursor( | |
103 AppListPositioner::ScreenEdge shelf_edge) const { | |
104 return positioner_->GetAnchorPointForShelfCursor(shelf_edge, cursor_); | |
105 } | |
106 | |
107 AppListPositioner::ScreenEdge DoGetShelfEdge( | |
108 const gfx::Rect& shelf_rect) const { | |
109 return positioner_->GetShelfEdge(shelf_rect); | |
110 } | |
111 | |
112 int DoGetCursorDistanceFromShelf( | |
113 AppListPositioner::ScreenEdge shelf_edge) const { | |
114 return positioner_->GetCursorDistanceFromShelf(shelf_edge, cursor_); | |
115 } | |
116 | |
117 private: | |
118 display::Display display_; | |
119 std::unique_ptr<AppListPositioner> positioner_; | |
120 gfx::Point cursor_; | |
121 }; | |
122 | |
123 TEST_F(AppListPositionerUnitTest, ScreenCorner) { | |
124 // Position the app list in a corner of the screen. | |
125 // Top-left corner. | |
126 EXPECT_EQ(gfx::Point(kWindowWidth / 2 + kMinDistanceFromEdge, | |
127 kMenuBarSize + kWindowHeight / 2 + kMinDistanceFromEdge), | |
128 DoGetAnchorPointForScreenCorner( | |
129 AppListPositioner::SCREEN_CORNER_TOP_LEFT)); | |
130 | |
131 // Top-right corner. | |
132 EXPECT_EQ(gfx::Point(kScreenWidth - kWindowWidth / 2 - kMinDistanceFromEdge, | |
133 kMenuBarSize + kWindowHeight / 2 + kMinDistanceFromEdge), | |
134 DoGetAnchorPointForScreenCorner( | |
135 AppListPositioner::SCREEN_CORNER_TOP_RIGHT)); | |
136 | |
137 // Bottom-left corner. | |
138 EXPECT_EQ( | |
139 gfx::Point(kWindowWidth / 2 + kMinDistanceFromEdge, | |
140 kScreenHeight - kWindowHeight / 2 - kMinDistanceFromEdge), | |
141 DoGetAnchorPointForScreenCorner( | |
142 AppListPositioner::SCREEN_CORNER_BOTTOM_LEFT)); | |
143 | |
144 // Bottom-right corner. | |
145 EXPECT_EQ( | |
146 gfx::Point(kScreenWidth - kWindowWidth / 2 - kMinDistanceFromEdge, | |
147 kScreenHeight - kWindowHeight / 2 - kMinDistanceFromEdge), | |
148 DoGetAnchorPointForScreenCorner( | |
149 AppListPositioner::SCREEN_CORNER_BOTTOM_RIGHT)); | |
150 } | |
151 | |
152 TEST_F(AppListPositionerUnitTest, ShelfCorner) { | |
153 // Position the app list on the shelf, aligned with the top or left corner. | |
154 // Shelf on left. Expect app list in top-left corner. | |
155 PlaceShelf(AppListPositioner::SCREEN_EDGE_LEFT); | |
156 EXPECT_EQ( | |
157 gfx::Point(kShelfSize + kWindowWidth / 2 + kMinDistanceFromEdge, | |
158 kMenuBarSize + kWindowHeight / 2 + kMinDistanceFromEdge), | |
159 DoGetAnchorPointForShelfCorner(AppListPositioner::SCREEN_EDGE_LEFT)); | |
160 | |
161 // Shelf on right. Expect app list in top-right corner. | |
162 PlaceShelf(AppListPositioner::SCREEN_EDGE_RIGHT); | |
163 EXPECT_EQ( | |
164 gfx::Point( | |
165 kScreenWidth - kShelfSize - kWindowWidth / 2 - kMinDistanceFromEdge, | |
166 kMenuBarSize + kWindowHeight / 2 + kMinDistanceFromEdge), | |
167 DoGetAnchorPointForShelfCorner(AppListPositioner::SCREEN_EDGE_RIGHT)); | |
168 | |
169 // Shelf on top. Expect app list in top-left corner. | |
170 PlaceShelf(AppListPositioner::SCREEN_EDGE_TOP); | |
171 EXPECT_EQ(gfx::Point(kWindowWidth / 2 + kMinDistanceFromEdge, | |
172 kMenuBarSize + kShelfSize + kWindowHeight / 2 + | |
173 kMinDistanceFromEdge), | |
174 DoGetAnchorPointForShelfCorner(AppListPositioner::SCREEN_EDGE_TOP)); | |
175 | |
176 // Shelf on bottom. Expect app list in bottom-left corner. | |
177 PlaceShelf(AppListPositioner::SCREEN_EDGE_BOTTOM); | |
178 EXPECT_EQ( | |
179 gfx::Point(kWindowWidth / 2 + kMinDistanceFromEdge, | |
180 kScreenHeight - kShelfSize - kWindowHeight / 2 - | |
181 kMinDistanceFromEdge), | |
182 DoGetAnchorPointForShelfCorner(AppListPositioner::SCREEN_EDGE_BOTTOM)); | |
183 } | |
184 | |
185 TEST_F(AppListPositionerUnitTest, ShelfCenter) { | |
186 // Position the app list on the shelf, aligned with the shelf center. | |
187 PlaceShelf(AppListPositioner::SCREEN_EDGE_LEFT); | |
188 // Shelf on left. Expect app list to be center-left. | |
189 EXPECT_EQ( | |
190 gfx::Point(kShelfSize + kWindowWidth / 2 + kMinDistanceFromEdge, | |
191 (kMenuBarSize + kScreenHeight) / 2), | |
192 DoGetAnchorPointForShelfCenter(AppListPositioner::SCREEN_EDGE_LEFT)); | |
193 | |
194 // Shelf on right. Expect app list to be center-right. | |
195 PlaceShelf(AppListPositioner::SCREEN_EDGE_RIGHT); | |
196 EXPECT_EQ( | |
197 gfx::Point( | |
198 kScreenWidth - kShelfSize - kWindowWidth / 2 - kMinDistanceFromEdge, | |
199 (kMenuBarSize + kScreenHeight) / 2), | |
200 DoGetAnchorPointForShelfCenter(AppListPositioner::SCREEN_EDGE_RIGHT)); | |
201 | |
202 // Shelf on top. Expect app list to be top-center. | |
203 PlaceShelf(AppListPositioner::SCREEN_EDGE_TOP); | |
204 EXPECT_EQ(gfx::Point(kScreenWidth / 2, | |
205 kMenuBarSize + kShelfSize + kWindowHeight / 2 + | |
206 kMinDistanceFromEdge), | |
207 DoGetAnchorPointForShelfCenter(AppListPositioner::SCREEN_EDGE_TOP)); | |
208 | |
209 // Shelf on bottom. Expect app list to be bottom-center. | |
210 PlaceShelf(AppListPositioner::SCREEN_EDGE_BOTTOM); | |
211 EXPECT_EQ( | |
212 gfx::Point(kScreenWidth / 2, | |
213 kScreenHeight - kShelfSize - kWindowHeight / 2 - | |
214 kMinDistanceFromEdge), | |
215 DoGetAnchorPointForShelfCenter(AppListPositioner::SCREEN_EDGE_BOTTOM)); | |
216 } | |
217 | |
218 TEST_F(AppListPositionerUnitTest, ShelfCursor) { | |
219 // Position the app list on the shelf, aligned with the mouse cursor. | |
220 | |
221 // Shelf on left. Expect app list in top-left corner. | |
222 PlaceShelf(AppListPositioner::SCREEN_EDGE_LEFT); | |
223 PlaceCursor(kCursorIgnore, kWindowAwayFromEdge); | |
224 EXPECT_EQ( | |
225 gfx::Point(kShelfSize + kWindowWidth / 2 + kMinDistanceFromEdge, | |
226 kWindowAwayFromEdge), | |
227 DoGetAnchorPointForShelfCursor(AppListPositioner::SCREEN_EDGE_LEFT)); | |
228 | |
229 // Shelf on right. Expect app list in top-right corner. | |
230 PlaceShelf(AppListPositioner::SCREEN_EDGE_RIGHT); | |
231 PlaceCursor(kCursorIgnore, kWindowAwayFromEdge); | |
232 EXPECT_EQ( | |
233 gfx::Point( | |
234 kScreenWidth - kShelfSize - kWindowWidth / 2 - kMinDistanceFromEdge, | |
235 kWindowAwayFromEdge), | |
236 DoGetAnchorPointForShelfCursor(AppListPositioner::SCREEN_EDGE_RIGHT)); | |
237 | |
238 // Shelf on top. Expect app list in top-left corner. | |
239 PlaceShelf(AppListPositioner::SCREEN_EDGE_TOP); | |
240 PlaceCursor(kWindowAwayFromEdge, kCursorIgnore); | |
241 EXPECT_EQ(gfx::Point(kWindowAwayFromEdge, | |
242 kMenuBarSize + kShelfSize + kWindowHeight / 2 + | |
243 kMinDistanceFromEdge), | |
244 DoGetAnchorPointForShelfCursor(AppListPositioner::SCREEN_EDGE_TOP)); | |
245 | |
246 // Shelf on bottom. Expect app list in bottom-left corner. | |
247 PlaceShelf(AppListPositioner::SCREEN_EDGE_BOTTOM); | |
248 PlaceCursor(kWindowAwayFromEdge, kCursorIgnore); | |
249 EXPECT_EQ( | |
250 gfx::Point(kWindowAwayFromEdge, | |
251 kScreenHeight - kShelfSize - kWindowHeight / 2 - | |
252 kMinDistanceFromEdge), | |
253 DoGetAnchorPointForShelfCursor(AppListPositioner::SCREEN_EDGE_BOTTOM)); | |
254 | |
255 // Shelf on bottom. Mouse near left edge. App list must not go off screen. | |
256 PlaceShelf(AppListPositioner::SCREEN_EDGE_BOTTOM); | |
257 PlaceCursor(kWindowNearEdge, kCursorIgnore); | |
258 EXPECT_EQ( | |
259 gfx::Point(kWindowWidth / 2 + kMinDistanceFromEdge, | |
260 kScreenHeight - kShelfSize - kWindowHeight / 2 - | |
261 kMinDistanceFromEdge), | |
262 DoGetAnchorPointForShelfCursor(AppListPositioner::SCREEN_EDGE_BOTTOM)); | |
263 | |
264 // Shelf on bottom. Mouse near right edge. App list must not go off screen. | |
265 PlaceShelf(AppListPositioner::SCREEN_EDGE_BOTTOM); | |
266 PlaceCursor(kScreenWidth - kWindowNearEdge, kCursorIgnore); | |
267 EXPECT_EQ( | |
268 gfx::Point(kScreenWidth - kWindowWidth / 2 - kMinDistanceFromEdge, | |
269 kScreenHeight - kShelfSize - kWindowHeight / 2 - | |
270 kMinDistanceFromEdge), | |
271 DoGetAnchorPointForShelfCursor(AppListPositioner::SCREEN_EDGE_BOTTOM)); | |
272 } | |
273 | |
274 TEST_F(AppListPositionerUnitTest, GetShelfEdge) { | |
275 gfx::Rect shelf_rect; | |
276 // Shelf on left. | |
277 shelf_rect = | |
278 gfx::Rect(0, kMenuBarSize, kShelfSize, kScreenHeight - kMenuBarSize); | |
279 EXPECT_EQ(AppListPositioner::SCREEN_EDGE_LEFT, DoGetShelfEdge(shelf_rect)); | |
280 | |
281 // Shelf on right. | |
282 shelf_rect = gfx::Rect(kScreenWidth - kShelfSize, | |
283 kMenuBarSize, | |
284 kShelfSize, | |
285 kScreenHeight - kMenuBarSize); | |
286 EXPECT_EQ(AppListPositioner::SCREEN_EDGE_RIGHT, DoGetShelfEdge(shelf_rect)); | |
287 | |
288 // Shelf on top. | |
289 shelf_rect = gfx::Rect(0, 0, kScreenWidth, kShelfSize); | |
290 EXPECT_EQ(AppListPositioner::SCREEN_EDGE_TOP, DoGetShelfEdge(shelf_rect)); | |
291 | |
292 // Shelf on bottom. | |
293 shelf_rect = | |
294 gfx::Rect(0, kScreenHeight - kShelfSize, kScreenWidth, kShelfSize); | |
295 EXPECT_EQ(AppListPositioner::SCREEN_EDGE_BOTTOM, DoGetShelfEdge(shelf_rect)); | |
296 | |
297 // A couple of inconclusive cases, which should return unknown. | |
298 shelf_rect = gfx::Rect(); | |
299 EXPECT_EQ(AppListPositioner::SCREEN_EDGE_UNKNOWN, DoGetShelfEdge(shelf_rect)); | |
300 shelf_rect = gfx::Rect(-10, 0, kScreenWidth, kShelfSize); | |
301 EXPECT_EQ(AppListPositioner::SCREEN_EDGE_UNKNOWN, DoGetShelfEdge(shelf_rect)); | |
302 shelf_rect = gfx::Rect(10, 0, kScreenWidth - 20, kShelfSize); | |
303 EXPECT_EQ(AppListPositioner::SCREEN_EDGE_UNKNOWN, DoGetShelfEdge(shelf_rect)); | |
304 shelf_rect = gfx::Rect(0, kShelfSize, kScreenWidth, 60); | |
305 EXPECT_EQ(AppListPositioner::SCREEN_EDGE_UNKNOWN, DoGetShelfEdge(shelf_rect)); | |
306 } | |
307 | |
308 TEST_F(AppListPositionerUnitTest, GetCursorDistanceFromShelf) { | |
309 // Shelf on left. | |
310 PlaceShelf(AppListPositioner::SCREEN_EDGE_LEFT); | |
311 PlaceCursor(kWindowAwayFromEdge, kCursorIgnore); | |
312 EXPECT_EQ(kWindowAwayFromEdge - kShelfSize, | |
313 DoGetCursorDistanceFromShelf(AppListPositioner::SCREEN_EDGE_LEFT)); | |
314 | |
315 // Shelf on right. | |
316 PlaceShelf(AppListPositioner::SCREEN_EDGE_RIGHT); | |
317 PlaceCursor(kScreenWidth - kWindowAwayFromEdge, kCursorIgnore); | |
318 EXPECT_EQ(kWindowAwayFromEdge - kShelfSize, | |
319 DoGetCursorDistanceFromShelf(AppListPositioner::SCREEN_EDGE_RIGHT)); | |
320 | |
321 // Shelf on top. | |
322 PlaceShelf(AppListPositioner::SCREEN_EDGE_TOP); | |
323 PlaceCursor(kCursorIgnore, kMenuBarSize + kWindowAwayFromEdge); | |
324 EXPECT_EQ(kWindowAwayFromEdge - kShelfSize, | |
325 DoGetCursorDistanceFromShelf(AppListPositioner::SCREEN_EDGE_TOP)); | |
326 | |
327 // Shelf on bottom. | |
328 PlaceShelf(AppListPositioner::SCREEN_EDGE_BOTTOM); | |
329 PlaceCursor(kCursorIgnore, kScreenHeight - kWindowAwayFromEdge); | |
330 EXPECT_EQ( | |
331 kWindowAwayFromEdge - kShelfSize, | |
332 DoGetCursorDistanceFromShelf(AppListPositioner::SCREEN_EDGE_BOTTOM)); | |
333 | |
334 // Shelf on bottom. Cursor inside shelf; expect 0. | |
335 PlaceShelf(AppListPositioner::SCREEN_EDGE_BOTTOM); | |
336 PlaceCursor(kCursorIgnore, kScreenHeight - kCursorOnShelf); | |
337 EXPECT_EQ( | |
338 0, DoGetCursorDistanceFromShelf(AppListPositioner::SCREEN_EDGE_BOTTOM)); | |
339 } | |
OLD | NEW |