app_list_linux_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/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    shelf_edge_ = AppListPositioner::SCREEN_EDGE_UNKNOWN;
56  }
57
58  // Set the display work area.
59  void SetWorkArea(int x, int y, int width, int height) {
60    display_.set_work_area(gfx::Rect(x, y, width, height));
61  }
62
63  // Sets up the test environment with the shelf along a given edge of the
64  // work area.
65  void PlaceShelf(AppListPositioner::ScreenEdge edge) {
66    shelf_edge_ = edge;
67    switch (edge) {
68      case AppListPositioner::SCREEN_EDGE_LEFT:
69        display_.set_work_area(gfx::Rect(kShelfSize,
70                                         kMenuBarSize,
71                                         kScreenWidth - kShelfSize,
72                                         kScreenHeight - kMenuBarSize));
73        break;
74      case AppListPositioner::SCREEN_EDGE_RIGHT:
75        display_.set_work_area(gfx::Rect(0,
76                                         kMenuBarSize,
77                                         kScreenWidth - kShelfSize,
78                                         kScreenHeight - kMenuBarSize));
79        break;
80      case AppListPositioner::SCREEN_EDGE_TOP:
81        display_.set_work_area(
82            gfx::Rect(0,
83                      kMenuBarSize + kShelfSize,
84                      kScreenWidth,
85                      kScreenHeight - kMenuBarSize - kShelfSize));
86        break;
87      case AppListPositioner::SCREEN_EDGE_BOTTOM:
88        display_.set_work_area(
89            gfx::Rect(0,
90                      kMenuBarSize,
91                      kScreenWidth,
92                      kScreenHeight - kMenuBarSize - kShelfSize));
93        break;
94      case AppListPositioner::SCREEN_EDGE_UNKNOWN:
95        NOTREACHED();
96        break;
97    }
98  }
99
100  // Set up the test mouse cursor in a given location.
101  void PlaceCursor(int x, int y) {
102    cursor_ = gfx::Point(x, y);
103  }
104
105  gfx::Point DoFindAnchorPoint() const {
106    return AppListLinux::FindAnchorPoint(gfx::Size(kWindowWidth, kWindowHeight),
107                                         display_,
108                                         cursor_,
109                                         shelf_edge_);
110  }
111
112 private:
113  gfx::Display display_;
114  gfx::Point cursor_;
115  AppListPositioner::ScreenEdge shelf_edge_;
116};
117
118TEST_F(AppListLinuxUnitTest, FindAnchorPointNoShelf) {
119  // Position the app list when there is no shelf on the display.
120  PlaceCursor(0, 0);
121  // Expect the app list to be in the top-left corner.
122  EXPECT_EQ(gfx::Point(kWindowWidth / 2, kMenuBarSize + kWindowHeight / 2),
123            DoFindAnchorPoint());
124}
125
126TEST_F(AppListLinuxUnitTest, FindAnchorPointMouseOffShelf) {
127  // Position the app list when the mouse is away from the shelf.
128
129  // Bottom shelf. Expect the app list to be in the bottom-left corner.
130  PlaceShelf(AppListPositioner::SCREEN_EDGE_BOTTOM);
131  PlaceCursor(kWindowAwayFromEdge, kScreenHeight - kCursorAwayFromShelfY);
132  EXPECT_EQ(gfx::Point(kWindowWidth / 2,
133                       kScreenHeight - kShelfSize - kWindowHeight / 2),
134            DoFindAnchorPoint());
135
136  // Top shelf. Expect the app list to be in the top-left corner.
137  PlaceShelf(AppListPositioner::SCREEN_EDGE_TOP);
138  PlaceCursor(kWindowAwayFromEdge, kMenuBarSize + kCursorAwayFromShelfY);
139  EXPECT_EQ(gfx::Point(kWindowWidth / 2,
140                       kMenuBarSize + kShelfSize + kWindowHeight / 2),
141            DoFindAnchorPoint());
142
143  // Left shelf. Expect the app list to be in the top-left corner.
144  PlaceShelf(AppListPositioner::SCREEN_EDGE_LEFT);
145  PlaceCursor(kCursorAwayFromShelfX, kWindowAwayFromEdge);
146  EXPECT_EQ(gfx::Point(kShelfSize + kWindowWidth / 2,
147                       kMenuBarSize + kWindowHeight / 2),
148            DoFindAnchorPoint());
149
150  // Right shelf. Expect the app list to be in the top-right corner.
151  PlaceShelf(AppListPositioner::SCREEN_EDGE_RIGHT);
152  PlaceCursor(kScreenWidth - kCursorAwayFromShelfX, kWindowAwayFromEdge);
153  EXPECT_EQ(gfx::Point(kScreenWidth - kShelfSize - kWindowWidth / 2,
154                       kMenuBarSize + kWindowHeight / 2),
155            DoFindAnchorPoint());
156}
157
158TEST_F(AppListLinuxUnitTest, FindAnchorPointMouseOnShelf) {
159  // Position the app list when the mouse is over the shelf.
160
161  // Bottom shelf (mouse well within shelf). Expect the app list to be at
162  // the bottom centered on the mouse X coordinate.
163  PlaceShelf(AppListPositioner::SCREEN_EDGE_BOTTOM);
164  PlaceCursor(kWindowAwayFromEdge, kScreenHeight - kCursorOnShelf);
165  EXPECT_EQ(gfx::Point(kWindowAwayFromEdge,
166                       kScreenHeight - kShelfSize - kWindowHeight / 2),
167            DoFindAnchorPoint());
168
169  // Bottom shelf (outside the shelf but still close enough).
170  // Expect the app list to be at the bottom centered on the mouse X coordinate.
171  PlaceShelf(AppListPositioner::SCREEN_EDGE_BOTTOM);
172  PlaceCursor(kWindowAwayFromEdge, kScreenHeight - kCursorNearShelfY);
173  EXPECT_EQ(gfx::Point(kWindowAwayFromEdge,
174                       kScreenHeight - kShelfSize - kWindowHeight / 2),
175            DoFindAnchorPoint());
176
177  // Top shelf. Expect the app list to be at the top centered on the
178  // mouse X coordinate.
179  PlaceShelf(AppListPositioner::SCREEN_EDGE_TOP);
180  PlaceCursor(kWindowAwayFromEdge, kMenuBarSize + kCursorNearShelfY);
181  EXPECT_EQ(gfx::Point(kWindowAwayFromEdge,
182                       kMenuBarSize + kShelfSize + kWindowHeight / 2),
183            DoFindAnchorPoint());
184
185  // Left shelf. Expect the app list to be at the left centered on the
186  // mouse Y coordinate.
187  PlaceShelf(AppListPositioner::SCREEN_EDGE_LEFT);
188  PlaceCursor(kCursorNearShelfX, kWindowAwayFromEdge);
189  EXPECT_EQ(gfx::Point(kShelfSize + kWindowWidth / 2, kWindowAwayFromEdge),
190            DoFindAnchorPoint());
191
192  // Right shelf. Expect the app list to be at the right centered on the
193  // mouse Y coordinate.
194  PlaceShelf(AppListPositioner::SCREEN_EDGE_RIGHT);
195  PlaceCursor(kScreenWidth - kCursorNearShelfX, kWindowAwayFromEdge);
196  EXPECT_EQ(gfx::Point(kScreenWidth - kShelfSize - kWindowWidth / 2,
197                       kWindowAwayFromEdge),
198            DoFindAnchorPoint());
199
200  // Bottom shelf. Mouse near left edge. App list must not go off screen.
201  PlaceShelf(AppListPositioner::SCREEN_EDGE_BOTTOM);
202  PlaceCursor(kWindowNearEdge, kScreenHeight - kCursorOnShelf);
203  EXPECT_EQ(gfx::Point(kWindowWidth / 2,
204                       kScreenHeight - kShelfSize - kWindowHeight / 2),
205            DoFindAnchorPoint());
206
207  // Bottom shelf. Mouse near right edge. App list must not go off screen.
208  PlaceShelf(AppListPositioner::SCREEN_EDGE_BOTTOM);
209  PlaceCursor(kScreenWidth - kWindowNearEdge, kScreenHeight - kCursorOnShelf);
210  EXPECT_EQ(gfx::Point(kScreenWidth - kWindowWidth / 2,
211                       kScreenHeight - kShelfSize - kWindowHeight / 2),
212            DoFindAnchorPoint());
213}
214