OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "ash/wm/cursor_manager.h" | 5 #include "ash/wm/cursor_manager.h" |
6 | 6 |
7 #include "ash/shell.h" | 7 #include "ash/shell.h" |
8 #include "ash/test/ash_test_base.h" | 8 #include "ash/test/ash_test_base.h" |
9 #include "ash/test/cursor_manager_test_api.h" | 9 #include "ash/test/cursor_manager_test_api.h" |
10 #include "ash/wm/image_cursors.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" |
11 | 15 |
12 namespace ash { | 16 namespace ash { |
13 namespace test { | 17 namespace test { |
14 | 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 |
15 typedef test::AshTestBase CursorManagerTest; | 44 typedef test::AshTestBase CursorManagerTest; |
16 | 45 |
17 TEST_F(CursorManagerTest, LockCursor) { | 46 TEST_F(CursorManagerTest, LockCursor) { |
18 CursorManager* cursor_manager = Shell::GetInstance()->cursor_manager(); | 47 CursorManager* cursor_manager = Shell::GetInstance()->cursor_manager(); |
19 CursorManagerTestApi test_api(cursor_manager); | 48 CursorManagerTestApi test_api(cursor_manager); |
20 | 49 |
21 cursor_manager->SetCursor(ui::kCursorCopy); | 50 cursor_manager->SetCursor(ui::kCursorCopy); |
22 EXPECT_EQ(ui::kCursorCopy, test_api.GetCurrentCursor().native_type()); | 51 EXPECT_EQ(ui::kCursorCopy, test_api.GetCurrentCursor().native_type()); |
23 cursor_manager->SetDeviceScaleFactor(2.0f); | 52 cursor_manager->SetDeviceScaleFactor(2.0f); |
24 EXPECT_EQ(2.0f, test_api.GetDeviceScaleFactor()); | 53 EXPECT_EQ(2.0f, test_api.GetDeviceScaleFactor()); |
(...skipping 24 matching lines...) Expand all Loading... |
49 CursorManagerTestApi test_api(cursor_manager); | 78 CursorManagerTestApi test_api(cursor_manager); |
50 | 79 |
51 cursor_manager->SetCursor(ui::kCursorCopy); | 80 cursor_manager->SetCursor(ui::kCursorCopy); |
52 EXPECT_EQ(ui::kCursorCopy, test_api.GetCurrentCursor().native_type()); | 81 EXPECT_EQ(ui::kCursorCopy, test_api.GetCurrentCursor().native_type()); |
53 EXPECT_TRUE(test_api.GetCurrentCursor().platform()); | 82 EXPECT_TRUE(test_api.GetCurrentCursor().platform()); |
54 cursor_manager->SetCursor(ui::kCursorPointer); | 83 cursor_manager->SetCursor(ui::kCursorPointer); |
55 EXPECT_EQ(ui::kCursorPointer, test_api.GetCurrentCursor().native_type()); | 84 EXPECT_EQ(ui::kCursorPointer, test_api.GetCurrentCursor().native_type()); |
56 EXPECT_TRUE(test_api.GetCurrentCursor().platform()); | 85 EXPECT_TRUE(test_api.GetCurrentCursor().platform()); |
57 } | 86 } |
58 | 87 |
59 TEST_F(CursorManagerTest, ShowCursor) { | 88 TEST_F(CursorManagerTest, ShowHideCursor) { |
60 CursorManager* cursor_manager = Shell::GetInstance()->cursor_manager(); | 89 CursorManager* cursor_manager = Shell::GetInstance()->cursor_manager(); |
61 CursorManagerTestApi test_api(cursor_manager); | 90 CursorManagerTestApi test_api(cursor_manager); |
62 | 91 |
63 cursor_manager->SetCursor(ui::kCursorCopy); | 92 cursor_manager->SetCursor(ui::kCursorCopy); |
64 EXPECT_EQ(ui::kCursorCopy, test_api.GetCurrentCursor().native_type()); | 93 EXPECT_EQ(ui::kCursorCopy, test_api.GetCurrentCursor().native_type()); |
65 | 94 |
66 cursor_manager->ShowCursor(true); | 95 cursor_manager->ShowCursor(); |
67 EXPECT_TRUE(cursor_manager->IsCursorVisible()); | 96 EXPECT_TRUE(cursor_manager->IsCursorVisible()); |
68 cursor_manager->ShowCursor(false); | 97 cursor_manager->HideCursor(); |
69 EXPECT_FALSE(cursor_manager->IsCursorVisible()); | 98 EXPECT_FALSE(cursor_manager->IsCursorVisible()); |
70 // The current cursor does not change even when the cursor is not shown. | 99 // The current cursor does not change even when the cursor is not shown. |
71 EXPECT_EQ(ui::kCursorCopy, test_api.GetCurrentCursor().native_type()); | 100 EXPECT_EQ(ui::kCursorCopy, test_api.GetCurrentCursor().native_type()); |
72 | 101 |
73 // Check if cursor visibility is locked. | 102 // Check if cursor visibility is locked. |
74 cursor_manager->LockCursor(); | 103 cursor_manager->LockCursor(); |
75 EXPECT_FALSE(cursor_manager->IsCursorVisible()); | 104 EXPECT_FALSE(cursor_manager->IsCursorVisible()); |
76 cursor_manager->ShowCursor(true); | 105 cursor_manager->ShowCursor(); |
77 EXPECT_FALSE(cursor_manager->IsCursorVisible()); | 106 EXPECT_FALSE(cursor_manager->IsCursorVisible()); |
78 cursor_manager->UnlockCursor(); | 107 cursor_manager->UnlockCursor(); |
79 EXPECT_TRUE(cursor_manager->IsCursorVisible()); | 108 EXPECT_TRUE(cursor_manager->IsCursorVisible()); |
80 | 109 |
81 cursor_manager->LockCursor(); | 110 cursor_manager->LockCursor(); |
82 EXPECT_TRUE(cursor_manager->IsCursorVisible()); | 111 EXPECT_TRUE(cursor_manager->IsCursorVisible()); |
83 cursor_manager->ShowCursor(false); | 112 cursor_manager->HideCursor(); |
84 EXPECT_TRUE(cursor_manager->IsCursorVisible()); | 113 EXPECT_TRUE(cursor_manager->IsCursorVisible()); |
85 cursor_manager->UnlockCursor(); | 114 cursor_manager->UnlockCursor(); |
86 EXPECT_FALSE(cursor_manager->IsCursorVisible()); | 115 EXPECT_FALSE(cursor_manager->IsCursorVisible()); |
87 | 116 |
88 // Checks setting visiblity while cursor is locked does not affect the | 117 // Checks setting visiblity while cursor is locked does not affect the |
89 // subsequent uses of UnlockCursor. | 118 // subsequent uses of UnlockCursor. |
90 cursor_manager->LockCursor(); | 119 cursor_manager->LockCursor(); |
91 cursor_manager->ShowCursor(false); | 120 cursor_manager->HideCursor(); |
92 cursor_manager->UnlockCursor(); | 121 cursor_manager->UnlockCursor(); |
93 EXPECT_FALSE(cursor_manager->IsCursorVisible()); | 122 EXPECT_FALSE(cursor_manager->IsCursorVisible()); |
94 | 123 |
95 cursor_manager->ShowCursor(true); | 124 cursor_manager->ShowCursor(); |
96 cursor_manager->LockCursor(); | 125 cursor_manager->LockCursor(); |
97 cursor_manager->UnlockCursor(); | 126 cursor_manager->UnlockCursor(); |
98 EXPECT_TRUE(cursor_manager->IsCursorVisible()); | 127 EXPECT_TRUE(cursor_manager->IsCursorVisible()); |
99 | 128 |
100 cursor_manager->LockCursor(); | 129 cursor_manager->LockCursor(); |
101 cursor_manager->ShowCursor(true); | 130 cursor_manager->ShowCursor(); |
102 cursor_manager->UnlockCursor(); | 131 cursor_manager->UnlockCursor(); |
103 EXPECT_TRUE(cursor_manager->IsCursorVisible()); | 132 EXPECT_TRUE(cursor_manager->IsCursorVisible()); |
104 | 133 |
105 cursor_manager->ShowCursor(false); | 134 cursor_manager->HideCursor(); |
106 cursor_manager->LockCursor(); | 135 cursor_manager->LockCursor(); |
107 cursor_manager->UnlockCursor(); | 136 cursor_manager->UnlockCursor(); |
108 EXPECT_FALSE(cursor_manager->IsCursorVisible()); | 137 EXPECT_FALSE(cursor_manager->IsCursorVisible()); |
109 } | 138 } |
110 | 139 |
111 TEST_F(CursorManagerTest, SetDeviceScaleFactor) { | 140 TEST_F(CursorManagerTest, SetDeviceScaleFactor) { |
112 CursorManager* cursor_manager = Shell::GetInstance()->cursor_manager(); | 141 CursorManager* cursor_manager = Shell::GetInstance()->cursor_manager(); |
113 CursorManagerTestApi test_api(cursor_manager); | 142 CursorManagerTestApi test_api(cursor_manager); |
114 | 143 |
115 cursor_manager->SetDeviceScaleFactor(2.0f); | 144 cursor_manager->SetDeviceScaleFactor(2.0f); |
116 EXPECT_EQ(2.0f, test_api.GetDeviceScaleFactor()); | 145 EXPECT_EQ(2.0f, test_api.GetDeviceScaleFactor()); |
117 cursor_manager->SetDeviceScaleFactor(1.0f); | 146 cursor_manager->SetDeviceScaleFactor(1.0f); |
118 EXPECT_EQ(1.0f, test_api.GetDeviceScaleFactor()); | 147 EXPECT_EQ(1.0f, test_api.GetDeviceScaleFactor()); |
119 } | 148 } |
120 | 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 #if defined(OS_WIN) |
| 270 // Temporarily disabled for windows. See crbug.com/112222. |
| 271 #define MAYBE_DisabledMouseEventsLocation DISABLED_DisabledMouseEventsLocation |
| 272 #else |
| 273 #define MAYBE_DisabledMouseEventsLocation DisabledMouseEventsLocation |
| 274 #endif // defined(OS_WIN) |
| 275 |
| 276 // Verifies that RootWindow generates a mouse event located outside of a window |
| 277 // when mouse events are disabled. |
| 278 TEST_F(CursorManagerTest, MAYBE_DisabledMouseEventsLocation) { |
| 279 scoped_ptr<MouseEventLocationDelegate> delegate( |
| 280 new MouseEventLocationDelegate()); |
| 281 const int kWindowWidth = 123; |
| 282 const int kWindowHeight = 45; |
| 283 gfx::Rect bounds(100, 200, kWindowWidth, kWindowHeight); |
| 284 scoped_ptr<aura::Window> window(aura::test::CreateTestWindowWithDelegate( |
| 285 delegate.get(), 1, bounds, Shell::GetInstance()->GetPrimaryRootWindow())); |
| 286 |
| 287 CursorManager* cursor_manager = Shell::GetInstance()->cursor_manager(); |
| 288 cursor_manager->EnableMouseEvents(); |
| 289 // Send a mouse event to window. |
| 290 gfx::Point point(101, 201); |
| 291 gfx::Point local_point; |
| 292 ui::MouseEvent event(ui::ET_MOUSE_MOVED, point, point, 0); |
| 293 aura::RootWindow* root_window = window->GetRootWindow(); |
| 294 root_window->AsRootWindowHostDelegate()->OnHostMouseEvent(&event); |
| 295 |
| 296 // Location was in window. |
| 297 local_point = delegate->mouse_event_location(); |
| 298 aura::Window::ConvertPointToTarget(window.get(), root_window, &local_point); |
| 299 EXPECT_TRUE(window->bounds().Contains(local_point)); |
| 300 |
| 301 // Location is now out of window. |
| 302 cursor_manager->DisableMouseEvents(); |
| 303 RunAllPendingInMessageLoop(); |
| 304 local_point = delegate->mouse_event_location(); |
| 305 aura::Window::ConvertPointToTarget(window.get(), root_window, &local_point); |
| 306 EXPECT_FALSE(window->bounds().Contains(local_point)); |
| 307 |
| 308 // Location is back in window. |
| 309 cursor_manager->EnableMouseEvents(); |
| 310 RunAllPendingInMessageLoop(); |
| 311 local_point = delegate->mouse_event_location(); |
| 312 aura::Window::ConvertPointToTarget(window.get(), root_window, &local_point); |
| 313 EXPECT_TRUE(window->bounds().Contains(local_point)); |
| 314 } |
| 315 |
| 316 #if defined(OS_WIN) |
| 317 // Disable on Win because RootWindow::MoveCursorTo is not implemented. |
| 318 #define MAYBE_DisabledQueryMouseLocation DISABLED_DisabledQueryMouseLocation |
| 319 #else |
| 320 #define MAYBE_DisabledQueryMouseLocation DisabledQueryMouseLocation |
| 321 #endif // defined(OS_WIN) |
| 322 |
| 323 TEST_F(CursorManagerTest, MAYBE_DisabledQueryMouseLocation) { |
| 324 aura::RootWindow* root_window = Shell::GetInstance()->GetPrimaryRootWindow(); |
| 325 root_window->MoveCursorTo(gfx::Point(10, 10)); |
| 326 gfx::Point mouse_location; |
| 327 EXPECT_TRUE(root_window->QueryMouseLocationForTest(&mouse_location)); |
| 328 EXPECT_EQ("10,10", mouse_location.ToString()); |
| 329 Shell::GetInstance()->cursor_manager()->DisableMouseEvents(); |
| 330 EXPECT_FALSE(root_window->QueryMouseLocationForTest(&mouse_location)); |
| 331 EXPECT_EQ("0,0", mouse_location.ToString()); |
| 332 } |
| 333 |
121 } // namespace test | 334 } // namespace test |
122 } // namespace ash | 335 } // namespace ash |
OLD | NEW |