app_list_win_unittest.cc revision a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7
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/win/app_list_win.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 normal (non-hidden) taskbar.
23const int kTaskbarSize = 30;
24
25// The distance the taskbar will appear from the edge of the screen.
26const int kMinDistanceFromEdge = 3;
27
28// Max distance the mouse can be from taskbar to count as "near" the taskbar.
29const int kSnapDistance = 50;
30// A cursor position that is within the taskbar. This must be < kTaskbarSize.
31const int kCursorOnTaskbar = kTaskbarSize / 2;
32// A cursor position that is within 50 pixels of the taskbar.
33const int kCursorNearTaskbar = kTaskbarSize + kSnapDistance;
34// A cursor position that is more than 50 pixels away from the taskbar.
35const int kCursorAwayFromTaskbar = kCursorNearTaskbar + 1;
36
37// A position for the center of the window that causes the window to overlap the
38// edge of the screen. This must be < kWindowWidth / 2 and < kWindowHeight / 2.
39const int kWindowNearEdge = kWindowWidth / 4;
40// A position for the center of the window that places the window away from all
41// edges of the screen. This must be > kWindowWidth / 2, > kWindowHeight / 2, <
42// kScreenWidth - kWindowWidth / 2 and < kScreenHeight - kWindowHeight / 2.
43const int kWindowAwayFromEdge = 158;
44
45// Horizontal offset of the simulated Windows 8 split point.
46const int kWin8SplitPoint = 200;
47
48}  // namespace
49
50class AppListWinUnitTest : public testing::Test {
51 public:
52  virtual void SetUp() OVERRIDE {
53    display_.set_bounds(gfx::Rect(0, 0, kScreenWidth, kScreenHeight));
54    display_.set_work_area(gfx::Rect(0, 0, kScreenWidth, kScreenHeight));
55    cursor_ = gfx::Point();
56    taskbar_rect_ = gfx::Rect();
57  }
58
59  // Set the display work area.
60  void SetWorkArea(int x, int y, int width, int height) {
61    display_.set_work_area(gfx::Rect(x, y, width, height));
62  }
63
64  // Sets up the test environment with the taskbar along a given edge of the
65  // work area.
66  void PlaceTaskbar(AppListPositioner::ScreenEdge edge) {
67    const gfx::Rect& work_area = display_.work_area();
68    switch (edge) {
69      case AppListPositioner::SCREEN_EDGE_UNKNOWN:
70        taskbar_rect_ = gfx::Rect();
71        break;
72      case AppListPositioner::SCREEN_EDGE_LEFT:
73        taskbar_rect_ = gfx::Rect(
74            work_area.x(), work_area.y(), kTaskbarSize, work_area.height());
75        break;
76      case AppListPositioner::SCREEN_EDGE_RIGHT:
77        taskbar_rect_ =
78            gfx::Rect(work_area.x() + work_area.width() - kTaskbarSize,
79                      work_area.y(),
80                      kTaskbarSize,
81                      work_area.height());
82        break;
83      case AppListPositioner::SCREEN_EDGE_TOP:
84        taskbar_rect_ = gfx::Rect(
85            work_area.x(), work_area.y(), work_area.width(), kTaskbarSize);
86        break;
87      case AppListPositioner::SCREEN_EDGE_BOTTOM:
88        taskbar_rect_ =
89            gfx::Rect(work_area.x(),
90                      work_area.y() + work_area.height() - kTaskbarSize,
91                      work_area.width(),
92                      kTaskbarSize);
93        break;
94    }
95  }
96
97  // Set up the test mouse cursor in a given location.
98  void PlaceCursor(int x, int y) {
99    cursor_ = gfx::Point(x, y);
100  }
101
102  gfx::Point DoFindAnchorPoint() const {
103    return AppListWin::FindAnchorPoint(gfx::Size(kWindowWidth, kWindowHeight),
104                                       display_,
105                                       cursor_,
106                                       taskbar_rect_);
107  }
108
109 private:
110  gfx::Display display_;
111  gfx::Point cursor_;
112  gfx::Rect taskbar_rect_;
113};
114
115TEST_F(AppListWinUnitTest, FindAnchorPointNoTaskbar) {
116  // Position the app list when there is no taskbar on the display.
117  PlaceCursor(0, 0);
118  // Expect the app list to be in the bottom-left corner.
119  EXPECT_EQ(
120      gfx::Point(kWindowWidth / 2 + kMinDistanceFromEdge,
121                 kScreenHeight - kWindowHeight / 2 - kMinDistanceFromEdge),
122      DoFindAnchorPoint());
123}
124
125TEST_F(AppListWinUnitTest, FindAnchorPointMouseOffTaskbar) {
126  // Position the app list when the mouse is away from the taskbar.
127
128  // Bottom taskbar. Expect the app list to be in the bottom-left corner.
129  PlaceTaskbar(AppListPositioner::SCREEN_EDGE_BOTTOM);
130  PlaceCursor(kWindowAwayFromEdge, kScreenHeight - kCursorAwayFromTaskbar);
131  EXPECT_EQ(gfx::Point(kWindowWidth / 2 + kMinDistanceFromEdge,
132                       kScreenHeight - kTaskbarSize - kWindowHeight / 2 -
133                           kMinDistanceFromEdge),
134            DoFindAnchorPoint());
135
136  // Top taskbar. Expect the app list to be in the top-left corner.
137  PlaceTaskbar(AppListPositioner::SCREEN_EDGE_TOP);
138  PlaceCursor(kWindowAwayFromEdge, kCursorAwayFromTaskbar);
139  EXPECT_EQ(gfx::Point(kWindowWidth / 2 + kMinDistanceFromEdge,
140                       kTaskbarSize + kWindowHeight / 2 + kMinDistanceFromEdge),
141            DoFindAnchorPoint());
142
143  // Left taskbar. Expect the app list to be in the top-left corner.
144  PlaceTaskbar(AppListPositioner::SCREEN_EDGE_LEFT);
145  PlaceCursor(kCursorAwayFromTaskbar, kWindowAwayFromEdge);
146  EXPECT_EQ(gfx::Point(kTaskbarSize + kWindowWidth / 2 + kMinDistanceFromEdge,
147                       kWindowHeight / 2 + kMinDistanceFromEdge),
148            DoFindAnchorPoint());
149
150  // Right taskbar. Expect the app list to be in the top-right corner.
151  PlaceTaskbar(AppListPositioner::SCREEN_EDGE_RIGHT);
152  PlaceCursor(kScreenWidth - kCursorAwayFromTaskbar, kWindowAwayFromEdge);
153  EXPECT_EQ(gfx::Point(kScreenWidth - kTaskbarSize - kWindowWidth / 2 -
154                           kMinDistanceFromEdge,
155                       kWindowHeight / 2 + kMinDistanceFromEdge),
156            DoFindAnchorPoint());
157}
158
159TEST_F(AppListWinUnitTest, FindAnchorPointMouseOnTaskbar) {
160  // Position the app list when the mouse is over the taskbar.
161
162  // Bottom taskbar (mouse well within taskbar). Expect the app list to be at
163  // the bottom centered on the mouse X coordinate.
164  PlaceTaskbar(AppListPositioner::SCREEN_EDGE_BOTTOM);
165  PlaceCursor(kWindowAwayFromEdge, kScreenHeight - kCursorOnTaskbar);
166  EXPECT_EQ(gfx::Point(kWindowAwayFromEdge,
167                       kScreenHeight - kTaskbarSize - kWindowHeight / 2 -
168                           kMinDistanceFromEdge),
169            DoFindAnchorPoint());
170
171  // Bottom taskbar (outside the taskbar but still close enough).
172  // Expect the app list to be at the bottom centered on the mouse X coordinate.
173  PlaceTaskbar(AppListPositioner::SCREEN_EDGE_BOTTOM);
174  PlaceCursor(kWindowAwayFromEdge, kScreenHeight - kCursorNearTaskbar);
175  EXPECT_EQ(gfx::Point(kWindowAwayFromEdge,
176                       kScreenHeight - kTaskbarSize - kWindowHeight / 2 -
177                           kMinDistanceFromEdge),
178            DoFindAnchorPoint());
179
180  // Top taskbar. Expect the app list to be at the top centered on the
181  // mouse X coordinate.
182  PlaceTaskbar(AppListPositioner::SCREEN_EDGE_TOP);
183  PlaceCursor(kWindowAwayFromEdge, kCursorNearTaskbar);
184  EXPECT_EQ(gfx::Point(kWindowAwayFromEdge,
185                       kTaskbarSize + kWindowHeight / 2 + kMinDistanceFromEdge),
186            DoFindAnchorPoint());
187
188  // Left taskbar. Expect the app list to be at the left centered on the
189  // mouse Y coordinate.
190  PlaceTaskbar(AppListPositioner::SCREEN_EDGE_LEFT);
191  PlaceCursor(kCursorNearTaskbar, kWindowAwayFromEdge);
192  EXPECT_EQ(gfx::Point(kTaskbarSize + kWindowWidth / 2 + kMinDistanceFromEdge,
193                       kWindowAwayFromEdge),
194            DoFindAnchorPoint());
195
196  // Right taskbar. Expect the app list to be at the right centered on the
197  // mouse Y coordinate.
198  PlaceTaskbar(AppListPositioner::SCREEN_EDGE_RIGHT);
199  PlaceCursor(kScreenWidth - kCursorNearTaskbar, kWindowAwayFromEdge);
200  EXPECT_EQ(gfx::Point(kScreenWidth - kTaskbarSize - kWindowWidth / 2 -
201                           kMinDistanceFromEdge,
202                       kWindowAwayFromEdge),
203            DoFindAnchorPoint());
204
205  // Bottom taskbar. Mouse near left edge. App list must not go off screen.
206  PlaceTaskbar(AppListPositioner::SCREEN_EDGE_BOTTOM);
207  PlaceCursor(kWindowNearEdge, kScreenHeight - kCursorOnTaskbar);
208  EXPECT_EQ(gfx::Point(kWindowWidth / 2 + kMinDistanceFromEdge,
209                       kScreenHeight - kTaskbarSize - kWindowHeight / 2 -
210                           kMinDistanceFromEdge),
211            DoFindAnchorPoint());
212
213  // Bottom taskbar. Mouse near right edge. App list must not go off screen.
214  PlaceTaskbar(AppListPositioner::SCREEN_EDGE_BOTTOM);
215  PlaceCursor(kScreenWidth - kWindowNearEdge, kScreenHeight - kCursorOnTaskbar);
216  EXPECT_EQ(gfx::Point(kScreenWidth - kWindowWidth / 2 - kMinDistanceFromEdge,
217                       kScreenHeight - kTaskbarSize - kWindowHeight / 2 -
218                           kMinDistanceFromEdge),
219            DoFindAnchorPoint());
220}
221
222TEST_F(AppListWinUnitTest, FindAnchorPointWin8SplitScreen) {
223  // Make the work area smaller than the screen, as you would get in Windows 8
224  // when there is a split screen and the Desktop is in one side of the screen.
225  SetWorkArea(
226      kWin8SplitPoint, 0, kScreenWidth - kWin8SplitPoint, kScreenHeight);
227
228  // No taskbar. Expect the app list to be in the bottom-left corner of the work
229  // area.
230  EXPECT_EQ(
231      gfx::Point(kWin8SplitPoint + kWindowWidth / 2 + kMinDistanceFromEdge,
232                 kScreenHeight - kWindowHeight / 2 - kMinDistanceFromEdge),
233      DoFindAnchorPoint());
234
235  // Bottom taskbar (mouse off-screen to the left). Expect the app list to be
236  // in the bottom-left corner of the work area.
237  PlaceTaskbar(AppListPositioner::SCREEN_EDGE_BOTTOM);
238  PlaceCursor(kWindowAwayFromEdge, kScreenHeight - kCursorOnTaskbar);
239  EXPECT_EQ(
240      gfx::Point(kWin8SplitPoint + kWindowWidth / 2 + kMinDistanceFromEdge,
241                 kScreenHeight - kTaskbarSize - kWindowHeight / 2 -
242                     kMinDistanceFromEdge),
243      DoFindAnchorPoint());
244}
245