| OLD | NEW |
| (Empty) |
| 1 // Copyright (c) 2012 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 "ash/wm/cursor_manager.h" | |
| 6 | |
| 7 #include "ash/shell.h" | |
| 8 #include "ash/test/ash_test_base.h" | |
| 9 #include "ash/test/cursor_manager_test_api.h" | |
| 10 #include "ash/wm/image_cursors.h" | |
| 11 #include "ui/aura/root_window.h" | |
| 12 #include "ui/aura/test/test_window_delegate.h" | |
| 13 #include "ui/aura/test/test_windows.h" | |
| 14 #include "ui/aura/window.h" | |
| 15 | |
| 16 namespace ash { | |
| 17 namespace test { | |
| 18 | |
| 19 namespace { | |
| 20 | |
| 21 // A delegate for recording a mouse event location. | |
| 22 class MouseEventLocationDelegate : public aura::test::TestWindowDelegate { | |
| 23 public: | |
| 24 MouseEventLocationDelegate() {} | |
| 25 virtual ~MouseEventLocationDelegate() {} | |
| 26 | |
| 27 const gfx::Point& mouse_event_location() const { | |
| 28 return mouse_event_location_; | |
| 29 } | |
| 30 | |
| 31 virtual void OnMouseEvent(ui::MouseEvent* event) OVERRIDE { | |
| 32 mouse_event_location_ = event->location(); | |
| 33 event->SetHandled(); | |
| 34 } | |
| 35 | |
| 36 private: | |
| 37 gfx::Point mouse_event_location_; | |
| 38 | |
| 39 DISALLOW_COPY_AND_ASSIGN(MouseEventLocationDelegate); | |
| 40 }; | |
| 41 | |
| 42 } // namespace | |
| 43 | |
| 44 typedef test::AshTestBase CursorManagerTest; | |
| 45 | |
| 46 TEST_F(CursorManagerTest, LockCursor) { | |
| 47 CursorManager* cursor_manager = Shell::GetInstance()->cursor_manager(); | |
| 48 CursorManagerTestApi test_api(cursor_manager); | |
| 49 | |
| 50 cursor_manager->SetCursor(ui::kCursorCopy); | |
| 51 EXPECT_EQ(ui::kCursorCopy, test_api.GetCurrentCursor().native_type()); | |
| 52 cursor_manager->SetDeviceScaleFactor(2.0f); | |
| 53 EXPECT_EQ(2.0f, test_api.GetDeviceScaleFactor()); | |
| 54 EXPECT_TRUE(test_api.GetCurrentCursor().platform()); | |
| 55 | |
| 56 cursor_manager->LockCursor(); | |
| 57 EXPECT_TRUE(cursor_manager->is_cursor_locked()); | |
| 58 | |
| 59 // Cursor type does not change while cursor is locked. | |
| 60 cursor_manager->SetCursor(ui::kCursorPointer); | |
| 61 EXPECT_EQ(ui::kCursorCopy, test_api.GetCurrentCursor().native_type()); | |
| 62 | |
| 63 // Device scale factor does change even while cursor is locked. | |
| 64 cursor_manager->SetDeviceScaleFactor(1.0f); | |
| 65 EXPECT_EQ(1.0f, test_api.GetDeviceScaleFactor()); | |
| 66 | |
| 67 cursor_manager->UnlockCursor(); | |
| 68 EXPECT_FALSE(cursor_manager->is_cursor_locked()); | |
| 69 | |
| 70 // Cursor type changes to the one specified while cursor is locked. | |
| 71 EXPECT_EQ(ui::kCursorPointer, test_api.GetCurrentCursor().native_type()); | |
| 72 EXPECT_EQ(1.0f, test_api.GetDeviceScaleFactor()); | |
| 73 EXPECT_TRUE(test_api.GetCurrentCursor().platform()); | |
| 74 } | |
| 75 | |
| 76 TEST_F(CursorManagerTest, SetCursor) { | |
| 77 CursorManager* cursor_manager = Shell::GetInstance()->cursor_manager(); | |
| 78 CursorManagerTestApi test_api(cursor_manager); | |
| 79 | |
| 80 cursor_manager->SetCursor(ui::kCursorCopy); | |
| 81 EXPECT_EQ(ui::kCursorCopy, test_api.GetCurrentCursor().native_type()); | |
| 82 EXPECT_TRUE(test_api.GetCurrentCursor().platform()); | |
| 83 cursor_manager->SetCursor(ui::kCursorPointer); | |
| 84 EXPECT_EQ(ui::kCursorPointer, test_api.GetCurrentCursor().native_type()); | |
| 85 EXPECT_TRUE(test_api.GetCurrentCursor().platform()); | |
| 86 } | |
| 87 | |
| 88 TEST_F(CursorManagerTest, ShowHideCursor) { | |
| 89 CursorManager* cursor_manager = Shell::GetInstance()->cursor_manager(); | |
| 90 CursorManagerTestApi test_api(cursor_manager); | |
| 91 | |
| 92 cursor_manager->SetCursor(ui::kCursorCopy); | |
| 93 EXPECT_EQ(ui::kCursorCopy, test_api.GetCurrentCursor().native_type()); | |
| 94 | |
| 95 cursor_manager->ShowCursor(); | |
| 96 EXPECT_TRUE(cursor_manager->IsCursorVisible()); | |
| 97 cursor_manager->HideCursor(); | |
| 98 EXPECT_FALSE(cursor_manager->IsCursorVisible()); | |
| 99 // The current cursor does not change even when the cursor is not shown. | |
| 100 EXPECT_EQ(ui::kCursorCopy, test_api.GetCurrentCursor().native_type()); | |
| 101 | |
| 102 // Check if cursor visibility is locked. | |
| 103 cursor_manager->LockCursor(); | |
| 104 EXPECT_FALSE(cursor_manager->IsCursorVisible()); | |
| 105 cursor_manager->ShowCursor(); | |
| 106 EXPECT_FALSE(cursor_manager->IsCursorVisible()); | |
| 107 cursor_manager->UnlockCursor(); | |
| 108 EXPECT_TRUE(cursor_manager->IsCursorVisible()); | |
| 109 | |
| 110 cursor_manager->LockCursor(); | |
| 111 EXPECT_TRUE(cursor_manager->IsCursorVisible()); | |
| 112 cursor_manager->HideCursor(); | |
| 113 EXPECT_TRUE(cursor_manager->IsCursorVisible()); | |
| 114 cursor_manager->UnlockCursor(); | |
| 115 EXPECT_FALSE(cursor_manager->IsCursorVisible()); | |
| 116 | |
| 117 // Checks setting visiblity while cursor is locked does not affect the | |
| 118 // subsequent uses of UnlockCursor. | |
| 119 cursor_manager->LockCursor(); | |
| 120 cursor_manager->HideCursor(); | |
| 121 cursor_manager->UnlockCursor(); | |
| 122 EXPECT_FALSE(cursor_manager->IsCursorVisible()); | |
| 123 | |
| 124 cursor_manager->ShowCursor(); | |
| 125 cursor_manager->LockCursor(); | |
| 126 cursor_manager->UnlockCursor(); | |
| 127 EXPECT_TRUE(cursor_manager->IsCursorVisible()); | |
| 128 | |
| 129 cursor_manager->LockCursor(); | |
| 130 cursor_manager->ShowCursor(); | |
| 131 cursor_manager->UnlockCursor(); | |
| 132 EXPECT_TRUE(cursor_manager->IsCursorVisible()); | |
| 133 | |
| 134 cursor_manager->HideCursor(); | |
| 135 cursor_manager->LockCursor(); | |
| 136 cursor_manager->UnlockCursor(); | |
| 137 EXPECT_FALSE(cursor_manager->IsCursorVisible()); | |
| 138 } | |
| 139 | |
| 140 TEST_F(CursorManagerTest, SetDeviceScaleFactor) { | |
| 141 CursorManager* cursor_manager = Shell::GetInstance()->cursor_manager(); | |
| 142 CursorManagerTestApi test_api(cursor_manager); | |
| 143 | |
| 144 cursor_manager->SetDeviceScaleFactor(2.0f); | |
| 145 EXPECT_EQ(2.0f, test_api.GetDeviceScaleFactor()); | |
| 146 cursor_manager->SetDeviceScaleFactor(1.0f); | |
| 147 EXPECT_EQ(1.0f, test_api.GetDeviceScaleFactor()); | |
| 148 } | |
| 149 | |
| 150 // Verifies that LockCursor/UnlockCursor work correctly with | |
| 151 // EnableMouseEvents and DisableMouseEvents | |
| 152 TEST_F(CursorManagerTest, EnableDisableMouseEvents) { | |
| 153 CursorManager* cursor_manager = Shell::GetInstance()->cursor_manager(); | |
| 154 CursorManagerTestApi test_api(cursor_manager); | |
| 155 | |
| 156 cursor_manager->SetCursor(ui::kCursorCopy); | |
| 157 EXPECT_EQ(ui::kCursorCopy, test_api.GetCurrentCursor().native_type()); | |
| 158 | |
| 159 cursor_manager->EnableMouseEvents(); | |
| 160 EXPECT_TRUE(cursor_manager->IsMouseEventsEnabled()); | |
| 161 cursor_manager->DisableMouseEvents(); | |
| 162 EXPECT_FALSE(cursor_manager->IsMouseEventsEnabled()); | |
| 163 // The current cursor does not change even when the cursor is not shown. | |
| 164 EXPECT_EQ(ui::kCursorCopy, test_api.GetCurrentCursor().native_type()); | |
| 165 | |
| 166 // Check if cursor enable state is locked. | |
| 167 cursor_manager->LockCursor(); | |
| 168 EXPECT_FALSE(cursor_manager->IsMouseEventsEnabled()); | |
| 169 cursor_manager->EnableMouseEvents(); | |
| 170 EXPECT_FALSE(cursor_manager->IsMouseEventsEnabled()); | |
| 171 cursor_manager->UnlockCursor(); | |
| 172 EXPECT_TRUE(cursor_manager->IsMouseEventsEnabled()); | |
| 173 | |
| 174 cursor_manager->LockCursor(); | |
| 175 EXPECT_TRUE(cursor_manager->IsMouseEventsEnabled()); | |
| 176 cursor_manager->DisableMouseEvents(); | |
| 177 EXPECT_TRUE(cursor_manager->IsMouseEventsEnabled()); | |
| 178 cursor_manager->UnlockCursor(); | |
| 179 EXPECT_FALSE(cursor_manager->IsMouseEventsEnabled()); | |
| 180 | |
| 181 // Checks enabling cursor while cursor is locked does not affect the | |
| 182 // subsequent uses of UnlockCursor. | |
| 183 cursor_manager->LockCursor(); | |
| 184 cursor_manager->DisableMouseEvents(); | |
| 185 cursor_manager->UnlockCursor(); | |
| 186 EXPECT_FALSE(cursor_manager->IsMouseEventsEnabled()); | |
| 187 | |
| 188 cursor_manager->EnableMouseEvents(); | |
| 189 cursor_manager->LockCursor(); | |
| 190 cursor_manager->UnlockCursor(); | |
| 191 EXPECT_TRUE(cursor_manager->IsMouseEventsEnabled()); | |
| 192 | |
| 193 cursor_manager->LockCursor(); | |
| 194 cursor_manager->EnableMouseEvents(); | |
| 195 cursor_manager->UnlockCursor(); | |
| 196 EXPECT_TRUE(cursor_manager->IsMouseEventsEnabled()); | |
| 197 | |
| 198 cursor_manager->DisableMouseEvents(); | |
| 199 cursor_manager->LockCursor(); | |
| 200 cursor_manager->UnlockCursor(); | |
| 201 EXPECT_FALSE(cursor_manager->IsMouseEventsEnabled()); | |
| 202 } | |
| 203 | |
| 204 TEST_F(CursorManagerTest, IsMouseEventsEnabled) { | |
| 205 CursorManager* cursor_manager = Shell::GetInstance()->cursor_manager(); | |
| 206 cursor_manager->EnableMouseEvents(); | |
| 207 EXPECT_TRUE(cursor_manager->IsMouseEventsEnabled()); | |
| 208 cursor_manager->DisableMouseEvents(); | |
| 209 EXPECT_FALSE(cursor_manager->IsMouseEventsEnabled()); | |
| 210 } | |
| 211 | |
| 212 // Verifies that the mouse events enable state changes correctly when | |
| 213 // ShowCursor/HideCursor and EnableMouseEvents/DisableMouseEvents are used | |
| 214 // together. | |
| 215 TEST_F(CursorManagerTest, ShowAndEnable) { | |
| 216 CursorManager* cursor_manager = Shell::GetInstance()->cursor_manager(); | |
| 217 | |
| 218 // Changing the visibility of the cursor does not affect the enable state. | |
| 219 cursor_manager->EnableMouseEvents(); | |
| 220 cursor_manager->ShowCursor(); | |
| 221 EXPECT_TRUE(cursor_manager->IsCursorVisible()); | |
| 222 EXPECT_TRUE(cursor_manager->IsMouseEventsEnabled()); | |
| 223 cursor_manager->HideCursor(); | |
| 224 EXPECT_FALSE(cursor_manager->IsCursorVisible()); | |
| 225 EXPECT_TRUE(cursor_manager->IsMouseEventsEnabled()); | |
| 226 cursor_manager->ShowCursor(); | |
| 227 EXPECT_TRUE(cursor_manager->IsCursorVisible()); | |
| 228 EXPECT_TRUE(cursor_manager->IsMouseEventsEnabled()); | |
| 229 | |
| 230 // When mouse events are disabled, it also gets invisible. | |
| 231 EXPECT_TRUE(cursor_manager->IsCursorVisible()); | |
| 232 cursor_manager->DisableMouseEvents(); | |
| 233 EXPECT_FALSE(cursor_manager->IsCursorVisible()); | |
| 234 EXPECT_FALSE(cursor_manager->IsMouseEventsEnabled()); | |
| 235 | |
| 236 // When mouse events are enabled, it restores the visibility state. | |
| 237 cursor_manager->EnableMouseEvents(); | |
| 238 EXPECT_TRUE(cursor_manager->IsCursorVisible()); | |
| 239 EXPECT_TRUE(cursor_manager->IsMouseEventsEnabled()); | |
| 240 | |
| 241 cursor_manager->ShowCursor(); | |
| 242 cursor_manager->DisableMouseEvents(); | |
| 243 EXPECT_FALSE(cursor_manager->IsCursorVisible()); | |
| 244 EXPECT_FALSE(cursor_manager->IsMouseEventsEnabled()); | |
| 245 cursor_manager->EnableMouseEvents(); | |
| 246 EXPECT_TRUE(cursor_manager->IsCursorVisible()); | |
| 247 EXPECT_TRUE(cursor_manager->IsMouseEventsEnabled()); | |
| 248 | |
| 249 cursor_manager->HideCursor(); | |
| 250 cursor_manager->DisableMouseEvents(); | |
| 251 EXPECT_FALSE(cursor_manager->IsCursorVisible()); | |
| 252 EXPECT_FALSE(cursor_manager->IsMouseEventsEnabled()); | |
| 253 cursor_manager->EnableMouseEvents(); | |
| 254 EXPECT_FALSE(cursor_manager->IsCursorVisible()); | |
| 255 EXPECT_TRUE(cursor_manager->IsMouseEventsEnabled()); | |
| 256 | |
| 257 // When mouse events are disabled, ShowCursor is ignored. | |
| 258 cursor_manager->DisableMouseEvents(); | |
| 259 EXPECT_FALSE(cursor_manager->IsCursorVisible()); | |
| 260 EXPECT_FALSE(cursor_manager->IsMouseEventsEnabled()); | |
| 261 cursor_manager->ShowCursor(); | |
| 262 EXPECT_FALSE(cursor_manager->IsCursorVisible()); | |
| 263 EXPECT_FALSE(cursor_manager->IsMouseEventsEnabled()); | |
| 264 cursor_manager->DisableMouseEvents(); | |
| 265 EXPECT_FALSE(cursor_manager->IsCursorVisible()); | |
| 266 EXPECT_FALSE(cursor_manager->IsMouseEventsEnabled()); | |
| 267 } | |
| 268 | |
| 269 // Verifies that calling DisableMouseEvents multiple times in a row makes no | |
| 270 // difference compared with calling it once. | |
| 271 // This is a regression test for http://crbug.com/169404. | |
| 272 TEST_F(CursorManagerTest, MultipleDisableMouseEvents) { | |
| 273 CursorManager* cursor_manager = Shell::GetInstance()->cursor_manager(); | |
| 274 cursor_manager->DisableMouseEvents(); | |
| 275 cursor_manager->DisableMouseEvents(); | |
| 276 cursor_manager->EnableMouseEvents(); | |
| 277 cursor_manager->LockCursor(); | |
| 278 cursor_manager->UnlockCursor(); | |
| 279 EXPECT_TRUE(cursor_manager->IsCursorVisible()); | |
| 280 } | |
| 281 | |
| 282 // Verifies that calling EnableMouseEvents multiple times in a row makes no | |
| 283 // difference compared with calling it once. | |
| 284 TEST_F(CursorManagerTest, MultipleEnableMouseEvents) { | |
| 285 CursorManager* cursor_manager = Shell::GetInstance()->cursor_manager(); | |
| 286 cursor_manager->DisableMouseEvents(); | |
| 287 cursor_manager->EnableMouseEvents(); | |
| 288 cursor_manager->EnableMouseEvents(); | |
| 289 cursor_manager->LockCursor(); | |
| 290 cursor_manager->UnlockCursor(); | |
| 291 EXPECT_TRUE(cursor_manager->IsCursorVisible()); | |
| 292 } | |
| 293 | |
| 294 #if defined(OS_WIN) | |
| 295 // Temporarily disabled for windows. See crbug.com/112222. | |
| 296 #define MAYBE_DisabledMouseEventsLocation DISABLED_DisabledMouseEventsLocation | |
| 297 #else | |
| 298 #define MAYBE_DisabledMouseEventsLocation DisabledMouseEventsLocation | |
| 299 #endif // defined(OS_WIN) | |
| 300 | |
| 301 // Verifies that RootWindow generates a mouse event located outside of a window | |
| 302 // when mouse events are disabled. | |
| 303 TEST_F(CursorManagerTest, MAYBE_DisabledMouseEventsLocation) { | |
| 304 scoped_ptr<MouseEventLocationDelegate> delegate( | |
| 305 new MouseEventLocationDelegate()); | |
| 306 const int kWindowWidth = 123; | |
| 307 const int kWindowHeight = 45; | |
| 308 gfx::Rect bounds(100, 200, kWindowWidth, kWindowHeight); | |
| 309 scoped_ptr<aura::Window> window(aura::test::CreateTestWindowWithDelegate( | |
| 310 delegate.get(), 1, bounds, Shell::GetInstance()->GetPrimaryRootWindow())); | |
| 311 | |
| 312 CursorManager* cursor_manager = Shell::GetInstance()->cursor_manager(); | |
| 313 cursor_manager->EnableMouseEvents(); | |
| 314 // Send a mouse event to window. | |
| 315 gfx::Point point(101, 201); | |
| 316 gfx::Point local_point; | |
| 317 ui::MouseEvent event(ui::ET_MOUSE_MOVED, point, point, 0); | |
| 318 aura::RootWindow* root_window = window->GetRootWindow(); | |
| 319 root_window->AsRootWindowHostDelegate()->OnHostMouseEvent(&event); | |
| 320 | |
| 321 // Location was in window. | |
| 322 local_point = delegate->mouse_event_location(); | |
| 323 aura::Window::ConvertPointToTarget(window.get(), root_window, &local_point); | |
| 324 EXPECT_TRUE(window->bounds().Contains(local_point)); | |
| 325 | |
| 326 // Location is now out of window. | |
| 327 cursor_manager->DisableMouseEvents(); | |
| 328 RunAllPendingInMessageLoop(); | |
| 329 local_point = delegate->mouse_event_location(); | |
| 330 aura::Window::ConvertPointToTarget(window.get(), root_window, &local_point); | |
| 331 EXPECT_FALSE(window->bounds().Contains(local_point)); | |
| 332 | |
| 333 // Location is back in window. | |
| 334 cursor_manager->EnableMouseEvents(); | |
| 335 RunAllPendingInMessageLoop(); | |
| 336 local_point = delegate->mouse_event_location(); | |
| 337 aura::Window::ConvertPointToTarget(window.get(), root_window, &local_point); | |
| 338 EXPECT_TRUE(window->bounds().Contains(local_point)); | |
| 339 } | |
| 340 | |
| 341 #if defined(OS_WIN) | |
| 342 // Disable on Win because RootWindow::MoveCursorTo is not implemented. | |
| 343 #define MAYBE_DisabledQueryMouseLocation DISABLED_DisabledQueryMouseLocation | |
| 344 #else | |
| 345 #define MAYBE_DisabledQueryMouseLocation DisabledQueryMouseLocation | |
| 346 #endif // defined(OS_WIN) | |
| 347 | |
| 348 TEST_F(CursorManagerTest, MAYBE_DisabledQueryMouseLocation) { | |
| 349 aura::RootWindow* root_window = Shell::GetInstance()->GetPrimaryRootWindow(); | |
| 350 root_window->MoveCursorTo(gfx::Point(10, 10)); | |
| 351 gfx::Point mouse_location; | |
| 352 EXPECT_TRUE(root_window->QueryMouseLocationForTest(&mouse_location)); | |
| 353 EXPECT_EQ("10,10", mouse_location.ToString()); | |
| 354 Shell::GetInstance()->cursor_manager()->DisableMouseEvents(); | |
| 355 EXPECT_FALSE(root_window->QueryMouseLocationForTest(&mouse_location)); | |
| 356 EXPECT_EQ("0,0", mouse_location.ToString()); | |
| 357 } | |
| 358 | |
| 359 } // namespace test | |
| 360 } // namespace ash | |
| OLD | NEW |