app_list_service_mac_unittest.mm revision f2477e01787aa58f445919b809d89e252beef54f
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#import "chrome/browser/ui/app_list/app_list_service_mac.h" 6 7#include "testing/gtest/include/gtest/gtest.h" 8#include "ui/gfx/display.h" 9#include "ui/gfx/point.h" 10#include "ui/gfx/rect.h" 11#include "ui/gfx/size.h" 12 13namespace { 14 15const int kScreenWidth = 800; 16const int kScreenHeight = 600; 17 18const int kWindowWidth = 100; 19const int kWindowHeight = 200; 20 21// Size of the menu bar along the top of the screen. 22const int kMenuBarSize = 22; 23// Size of the normal (non-hidden) dock. 24const int kDockSize = 30; 25// Size of the hidden dock. 26const int kHiddenDockSize = 4; 27 28// Distance to move during the opening animation. 29const int kAnimationOffset = 20; 30 31// The assumed size of the hidden dock. 32const int kExtraDistance = 50; 33 34// A cursor position that is within the dock. This must be < kDockSize. 35const int kCursorOnDock = kDockSize / 2; 36// A cursor position that is within 50 pixels of the dock. 37const int kCursorNearDock = kDockSize + 50; 38// A cursor position that is more than 50 pixels away from the dock. 39const int kCursorAwayFromDock = kCursorNearDock + 1; 40 41// A position for the center of the window that causes the window to overlap the 42// edge of the screen. This must be < kWindowWidth / 2 and < kWindowHeight / 2. 43const int kWindowNearEdge = kWindowWidth / 4; 44// A position for the center of the window that places the window away from all 45// edges of the screen. This must be > kWindowWidth / 2, > kWindowHeight / 2, < 46// kScreenWidth - kWindowWidth / 2 and < kScreenHeight - kWindowHeight / 2. 47const int kWindowAwayFromEdge = 158; 48 49enum DockLocation { 50 DOCK_LOCATION_BOTTOM, 51 DOCK_LOCATION_LEFT, 52 DOCK_LOCATION_RIGHT, 53}; 54 55// Returns |point| offset by (|x_offset|, |y_offset|). 56NSPoint OffsetPoint(const NSPoint& point, CGFloat x_offset, CGFloat y_offset) { 57 return NSMakePoint(point.x + x_offset, point.y + y_offset); 58} 59 60} // namespace 61 62class AppListServiceMacUnitTest : public testing::Test { 63 public: 64 virtual void SetUp() OVERRIDE { 65 display_.set_bounds(gfx::Rect(0, 0, kScreenWidth, kScreenHeight)); 66 display_.set_work_area( 67 gfx::Rect(0, kMenuBarSize, kScreenWidth, kScreenHeight - kMenuBarSize)); 68 window_size_ = gfx::Size(kWindowWidth, kWindowHeight); 69 cursor_is_visible_ = true; 70 cursor_ = gfx::Point(); 71 } 72 73 // Sets up the test environment with the dock in a given location. 74 void PlaceDock(DockLocation location, bool hidden) { 75 int dock_size = hidden ? kHiddenDockSize : kDockSize; 76 switch (location) { 77 case DOCK_LOCATION_BOTTOM: 78 display_.set_work_area( 79 gfx::Rect(0, 80 kMenuBarSize, 81 kScreenWidth, 82 kScreenHeight - kMenuBarSize - dock_size)); 83 break; 84 case DOCK_LOCATION_LEFT: 85 display_.set_work_area(gfx::Rect(dock_size, 86 kMenuBarSize, 87 kScreenWidth - dock_size, 88 kScreenHeight - kMenuBarSize)); 89 break; 90 case DOCK_LOCATION_RIGHT: 91 display_.set_work_area(gfx::Rect(0, 92 kMenuBarSize, 93 kScreenWidth - dock_size, 94 kScreenHeight - kMenuBarSize)); 95 break; 96 } 97 } 98 99 // Set whether the test mouse cursor is visible. 100 void SetCursorVisible(bool visible) { 101 cursor_is_visible_ = visible; 102 } 103 104 // Set up the test mouse cursor in a given location. 105 void PlaceCursor(int x, int y) { 106 cursor_ = gfx::Point(x, y); 107 } 108 109 void DoFindAnchorPoint(NSPoint* target_origin, 110 NSPoint* start_origin) { 111 AppListServiceMac::FindAnchorPoint(window_size_, 112 display_, 113 kScreenHeight, 114 cursor_is_visible_, 115 cursor_, 116 target_origin, 117 start_origin); 118 } 119 120 private: 121 gfx::Display display_; 122 gfx::Size window_size_; 123 bool cursor_is_visible_; 124 gfx::Point cursor_; 125}; 126 127// Tests positioning the app list when there is no visible mouse cursor. 128TEST_F(AppListServiceMacUnitTest, FindAnchorPointNoCursor) { 129 SetCursorVisible(false); 130 PlaceCursor(0, 0); 131 NSPoint target_origin; 132 NSPoint start_origin; 133 134 // Bottom dock. Expect the app list to be in the bottom-left corner, above the 135 // dock. 136 PlaceDock(DOCK_LOCATION_BOTTOM, false); 137 DoFindAnchorPoint(&target_origin, &start_origin); 138 EXPECT_TRUE(NSEqualPoints(NSMakePoint(0, kDockSize), target_origin)); 139 EXPECT_TRUE(NSEqualPoints(target_origin, start_origin)); 140 141 // Left dock. Expect the app list to be in the bottom-left corner, beside the 142 // dock. 143 PlaceDock(DOCK_LOCATION_LEFT, false); 144 DoFindAnchorPoint(&target_origin, &start_origin); 145 EXPECT_TRUE(NSEqualPoints(NSMakePoint(kDockSize, 0), target_origin)); 146 EXPECT_TRUE(NSEqualPoints(target_origin, start_origin)); 147 148 // Right dock. Expect the app list to be in the bottom-left corner. 149 PlaceDock(DOCK_LOCATION_RIGHT, false); 150 DoFindAnchorPoint(&target_origin, &start_origin); 151 EXPECT_TRUE(NSEqualPoints(NSMakePoint(0, 0), target_origin)); 152 EXPECT_TRUE(NSEqualPoints(target_origin, start_origin)); 153} 154 155// Tests positioning the app list when there is no dock on the display. 156TEST_F(AppListServiceMacUnitTest, FindAnchorPointNoDock) { 157 SetCursorVisible(true); 158 PlaceCursor(0, 0); 159 NSPoint target_origin; 160 NSPoint start_origin; 161 162 // Expect the app list to be in the bottom-left corner. 163 DoFindAnchorPoint(&target_origin, &start_origin); 164 EXPECT_TRUE(NSEqualPoints(NSMakePoint(0, 0), target_origin)); 165 EXPECT_TRUE(NSEqualPoints(target_origin, start_origin)); 166} 167 168// Tests positioning the app list when the mouse is away from the dock. 169TEST_F(AppListServiceMacUnitTest, FindAnchorPointMouseOffDock) { 170 // On Mac, this is currently no different from having the mouse on the dock. 171 SetCursorVisible(true); 172 NSPoint target_origin; 173 NSPoint start_origin; 174 175 // Bottom dock. Expect the app list to be at the bottom centered on the mouse 176 // X coordinate. 177 PlaceDock(DOCK_LOCATION_BOTTOM, false); 178 PlaceCursor(kWindowAwayFromEdge, kScreenHeight - kCursorAwayFromDock); 179 DoFindAnchorPoint(&target_origin, &start_origin); 180 EXPECT_TRUE(NSEqualPoints( 181 NSMakePoint(kWindowAwayFromEdge - kWindowWidth / 2, kDockSize), 182 target_origin)); 183 EXPECT_TRUE(NSEqualPoints(OffsetPoint(target_origin, 0, -kAnimationOffset), 184 start_origin)); 185 186 // Left dock. Expect the app list to be at the left centered on the mouse Y 187 // coordinate. 188 PlaceDock(DOCK_LOCATION_LEFT, false); 189 PlaceCursor(kCursorAwayFromDock, kWindowAwayFromEdge); 190 DoFindAnchorPoint(&target_origin, &start_origin); 191 EXPECT_TRUE(NSEqualPoints( 192 NSMakePoint(kDockSize, 193 kScreenHeight - kWindowAwayFromEdge - kWindowHeight / 2), 194 target_origin)); 195 EXPECT_TRUE(NSEqualPoints(OffsetPoint(target_origin, -kAnimationOffset, 0), 196 start_origin)); 197 198 // Right dock. Expect the app list to be at the right centered on the mouse Y 199 // coordinate. 200 PlaceDock(DOCK_LOCATION_RIGHT, false); 201 PlaceCursor(kScreenWidth - kCursorAwayFromDock, kWindowAwayFromEdge); 202 DoFindAnchorPoint(&target_origin, &start_origin); 203 EXPECT_TRUE(NSEqualPoints( 204 NSMakePoint(kScreenWidth - kDockSize - kWindowWidth, 205 kScreenHeight - kWindowAwayFromEdge - kWindowHeight / 2), 206 target_origin)); 207 EXPECT_TRUE(NSEqualPoints(OffsetPoint(target_origin, kAnimationOffset, 0), 208 start_origin)); 209} 210 211// Tests positioning the app list when the dock is hidden. 212TEST_F(AppListServiceMacUnitTest, FindAnchorPointHiddenDock) { 213 SetCursorVisible(true); 214 NSPoint target_origin; 215 NSPoint start_origin; 216 217 // Bottom dock. Expect the app list to be kExtraDistance pixels from the dock 218 // centered on the mouse X coordinate. 219 PlaceDock(DOCK_LOCATION_BOTTOM, true); 220 PlaceCursor(kWindowAwayFromEdge, kScreenHeight - kCursorAwayFromDock); 221 DoFindAnchorPoint(&target_origin, &start_origin); 222 EXPECT_TRUE(NSEqualPoints(NSMakePoint(kWindowAwayFromEdge - kWindowWidth / 2, 223 kHiddenDockSize + kExtraDistance), 224 target_origin)); 225 EXPECT_TRUE(NSEqualPoints(OffsetPoint(target_origin, 0, -kAnimationOffset), 226 start_origin)); 227 228 // Left dock. Expect the app list to be kExtraDistance pixels from the dock 229 // centered on the mouse Y coordinate. 230 PlaceDock(DOCK_LOCATION_LEFT, true); 231 PlaceCursor(kCursorAwayFromDock, kWindowAwayFromEdge); 232 DoFindAnchorPoint(&target_origin, &start_origin); 233 EXPECT_TRUE(NSEqualPoints( 234 NSMakePoint(kHiddenDockSize + kExtraDistance, 235 kScreenHeight - kWindowAwayFromEdge - kWindowHeight / 2), 236 target_origin)); 237 EXPECT_TRUE(NSEqualPoints(OffsetPoint(target_origin, -kAnimationOffset, 0), 238 start_origin)); 239 240 // Right dock. Expect the app list to be kExtraDistance pixels from the dock 241 // centered on the mouse Y coordinate. 242 PlaceDock(DOCK_LOCATION_RIGHT, true); 243 PlaceCursor(kScreenWidth - kCursorAwayFromDock, kWindowAwayFromEdge); 244 DoFindAnchorPoint(&target_origin, &start_origin); 245 EXPECT_TRUE(NSEqualPoints( 246 NSMakePoint( 247 kScreenWidth - kHiddenDockSize - kWindowWidth - kExtraDistance, 248 kScreenHeight - kWindowAwayFromEdge - kWindowHeight / 2), 249 target_origin)); 250 EXPECT_TRUE(NSEqualPoints(OffsetPoint(target_origin, kAnimationOffset, 0), 251 start_origin)); 252} 253 254// Tests positioning the app list when the mouse is over the dock. 255TEST_F(AppListServiceMacUnitTest, FindAnchorPointMouseOnDock) { 256 SetCursorVisible(true); 257 NSPoint target_origin; 258 NSPoint start_origin; 259 260 // Bottom dock (mouse well within dock). Expect the app list to be at the 261 // bottom centered on the mouse X coordinate. 262 PlaceDock(DOCK_LOCATION_BOTTOM, false); 263 PlaceCursor(kWindowAwayFromEdge, kScreenHeight - kCursorOnDock); 264 DoFindAnchorPoint(&target_origin, &start_origin); 265 EXPECT_TRUE(NSEqualPoints( 266 NSMakePoint(kWindowAwayFromEdge - kWindowWidth / 2, kDockSize), 267 target_origin)); 268 EXPECT_TRUE(NSEqualPoints(OffsetPoint(target_origin, 0, -kAnimationOffset), 269 start_origin)); 270 271 // Bottom dock (outside the dock but still close enough). Expect the app list 272 // to be at the bottom centered on the mouse X coordinate. 273 PlaceDock(DOCK_LOCATION_BOTTOM, false); 274 PlaceCursor(kWindowAwayFromEdge, kScreenHeight - kCursorNearDock); 275 DoFindAnchorPoint(&target_origin, &start_origin); 276 EXPECT_TRUE(NSEqualPoints( 277 NSMakePoint(kWindowAwayFromEdge - kWindowWidth / 2, kDockSize), 278 target_origin)); 279 EXPECT_TRUE(NSEqualPoints(OffsetPoint(target_origin, 0, -kAnimationOffset), 280 start_origin)); 281 282 // Left dock. Expect the app list to be at the left centered on the mouse Y 283 // coordinate. 284 PlaceDock(DOCK_LOCATION_LEFT, false); 285 PlaceCursor(kCursorNearDock, kWindowAwayFromEdge); 286 DoFindAnchorPoint(&target_origin, &start_origin); 287 EXPECT_TRUE(NSEqualPoints( 288 NSMakePoint(kDockSize, 289 kScreenHeight - kWindowAwayFromEdge - kWindowHeight / 2), 290 target_origin)); 291 EXPECT_TRUE(NSEqualPoints(OffsetPoint(target_origin, -kAnimationOffset, 0), 292 start_origin)); 293 294 // Right dock. Expect the app list to be at the right centered on the mouse Y 295 // coordinate. 296 PlaceDock(DOCK_LOCATION_RIGHT, false); 297 PlaceCursor(kScreenWidth - kCursorNearDock, kWindowAwayFromEdge); 298 DoFindAnchorPoint(&target_origin, &start_origin); 299 EXPECT_TRUE(NSEqualPoints( 300 NSMakePoint(kScreenWidth - kDockSize - kWindowWidth, 301 kScreenHeight - kWindowAwayFromEdge - kWindowHeight / 2), 302 target_origin)); 303 EXPECT_TRUE(NSEqualPoints(OffsetPoint(target_origin, kAnimationOffset, 0), 304 start_origin)); 305 306 // Bottom dock. Mouse near left edge. App list must not go off screen. 307 PlaceDock(DOCK_LOCATION_BOTTOM, false); 308 PlaceCursor(kWindowNearEdge, kScreenHeight - kCursorOnDock); 309 DoFindAnchorPoint(&target_origin, &start_origin); 310 EXPECT_TRUE(NSEqualPoints(NSMakePoint(0, kDockSize), target_origin)); 311 EXPECT_TRUE(NSEqualPoints(OffsetPoint(target_origin, 0, -kAnimationOffset), 312 start_origin)); 313 314 // Bottom dock. Mouse near right edge. App list must not go off screen. 315 PlaceDock(DOCK_LOCATION_BOTTOM, false); 316 PlaceCursor(kScreenWidth - kWindowNearEdge, kScreenHeight - kCursorOnDock); 317 DoFindAnchorPoint(&target_origin, &start_origin); 318 EXPECT_TRUE(NSEqualPoints(NSMakePoint(kScreenWidth - kWindowWidth, kDockSize), 319 target_origin)); 320 EXPECT_TRUE(NSEqualPoints(OffsetPoint(target_origin, 0, -kAnimationOffset), 321 start_origin)); 322} 323