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 ui::EventResult OnMouseEvent(ui::MouseEvent* event) OVERRIDE { |
| 32 mouse_event_location_ = event->location(); |
| 33 return ui::ER_HANDLED; |
| 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 86 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
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 EnableCursor. |
| 151 TEST_F(CursorManagerTest, EnableCursor) { |
| 152 CursorManager* cursor_manager = Shell::GetInstance()->cursor_manager(); |
| 153 CursorManagerTestApi test_api(cursor_manager); |
| 154 |
| 155 cursor_manager->SetCursor(ui::kCursorCopy); |
| 156 EXPECT_EQ(ui::kCursorCopy, test_api.GetCurrentCursor().native_type()); |
| 157 |
| 158 cursor_manager->EnableCursor(true); |
| 159 EXPECT_TRUE(cursor_manager->IsCursorEnabled()); |
| 160 cursor_manager->EnableCursor(false); |
| 161 EXPECT_FALSE(cursor_manager->IsCursorEnabled()); |
| 162 // The current cursor does not change even when the cursor is not shown. |
| 163 EXPECT_EQ(ui::kCursorCopy, test_api.GetCurrentCursor().native_type()); |
| 164 |
| 165 // Check if cursor enable state is locked. |
| 166 cursor_manager->LockCursor(); |
| 167 EXPECT_FALSE(cursor_manager->IsCursorEnabled()); |
| 168 cursor_manager->EnableCursor(true); |
| 169 EXPECT_FALSE(cursor_manager->IsCursorEnabled()); |
| 170 cursor_manager->UnlockCursor(); |
| 171 EXPECT_TRUE(cursor_manager->IsCursorEnabled()); |
| 172 |
| 173 cursor_manager->LockCursor(); |
| 174 EXPECT_TRUE(cursor_manager->IsCursorEnabled()); |
| 175 cursor_manager->EnableCursor(false); |
| 176 EXPECT_TRUE(cursor_manager->IsCursorEnabled()); |
| 177 cursor_manager->UnlockCursor(); |
| 178 EXPECT_FALSE(cursor_manager->IsCursorEnabled()); |
| 179 |
| 180 // Checks enabling cursor while cursor is locked does not affect the |
| 181 // subsequent uses of UnlockCursor. |
| 182 cursor_manager->LockCursor(); |
| 183 cursor_manager->EnableCursor(false); |
| 184 cursor_manager->UnlockCursor(); |
| 185 EXPECT_FALSE(cursor_manager->IsCursorEnabled()); |
| 186 |
| 187 cursor_manager->EnableCursor(true); |
| 188 cursor_manager->LockCursor(); |
| 189 cursor_manager->UnlockCursor(); |
| 190 EXPECT_TRUE(cursor_manager->IsCursorEnabled()); |
| 191 |
| 192 cursor_manager->LockCursor(); |
| 193 cursor_manager->EnableCursor(true); |
| 194 cursor_manager->UnlockCursor(); |
| 195 EXPECT_TRUE(cursor_manager->IsCursorEnabled()); |
| 196 |
| 197 cursor_manager->EnableCursor(false); |
| 198 cursor_manager->LockCursor(); |
| 199 cursor_manager->UnlockCursor(); |
| 200 EXPECT_FALSE(cursor_manager->IsCursorEnabled()); |
| 201 } |
| 202 |
| 203 TEST_F(CursorManagerTest, IsCursorEnabled) { |
| 204 CursorManager* cursor_manager = Shell::GetInstance()->cursor_manager(); |
| 205 cursor_manager->EnableCursor(true); |
| 206 EXPECT_TRUE(cursor_manager->IsCursorEnabled()); |
| 207 cursor_manager->EnableCursor(false); |
| 208 EXPECT_FALSE(cursor_manager->IsCursorEnabled()); |
| 209 } |
| 210 |
| 211 // Verifies that the cursor state changes correctly when ShowCursor and |
| 212 // EnableCursor are used together. |
| 213 TEST_F(CursorManagerTest, ShowAndEnable) { |
| 214 CursorManager* cursor_manager = Shell::GetInstance()->cursor_manager(); |
| 215 |
| 216 // Changing the visibility of the cursor does not affect the enable state. |
| 217 cursor_manager->EnableCursor(true); |
| 218 cursor_manager->ShowCursor(true); |
| 219 EXPECT_TRUE(cursor_manager->IsCursorVisible()); |
| 220 EXPECT_TRUE(cursor_manager->IsCursorEnabled()); |
| 221 cursor_manager->ShowCursor(false); |
| 222 EXPECT_FALSE(cursor_manager->IsCursorVisible()); |
| 223 EXPECT_TRUE(cursor_manager->IsCursorEnabled()); |
| 224 cursor_manager->ShowCursor(true); |
| 225 EXPECT_TRUE(cursor_manager->IsCursorVisible()); |
| 226 EXPECT_TRUE(cursor_manager->IsCursorEnabled()); |
| 227 |
| 228 // When the cursor is disabled, it also gets invisible. |
| 229 EXPECT_TRUE(cursor_manager->IsCursorVisible()); |
| 230 cursor_manager->EnableCursor(false); |
| 231 EXPECT_FALSE(cursor_manager->IsCursorVisible()); |
| 232 EXPECT_FALSE(cursor_manager->IsCursorEnabled()); |
| 233 |
| 234 // When the cursor is enabled, it restores the visibility state. |
| 235 cursor_manager->EnableCursor(true); |
| 236 EXPECT_TRUE(cursor_manager->IsCursorVisible()); |
| 237 EXPECT_TRUE(cursor_manager->IsCursorEnabled()); |
| 238 |
| 239 cursor_manager->ShowCursor(true); |
| 240 cursor_manager->EnableCursor(false); |
| 241 EXPECT_FALSE(cursor_manager->IsCursorVisible()); |
| 242 EXPECT_FALSE(cursor_manager->IsCursorEnabled()); |
| 243 cursor_manager->EnableCursor(true); |
| 244 EXPECT_TRUE(cursor_manager->IsCursorVisible()); |
| 245 EXPECT_TRUE(cursor_manager->IsCursorEnabled()); |
| 246 |
| 247 cursor_manager->ShowCursor(false); |
| 248 cursor_manager->EnableCursor(false); |
| 249 EXPECT_FALSE(cursor_manager->IsCursorVisible()); |
| 250 EXPECT_FALSE(cursor_manager->IsCursorEnabled()); |
| 251 cursor_manager->EnableCursor(true); |
| 252 EXPECT_FALSE(cursor_manager->IsCursorVisible()); |
| 253 EXPECT_TRUE(cursor_manager->IsCursorEnabled()); |
| 254 |
| 255 // When the cursor is disabled, ShowCursor is ignored. |
| 256 cursor_manager->EnableCursor(false); |
| 257 EXPECT_FALSE(cursor_manager->IsCursorVisible()); |
| 258 EXPECT_FALSE(cursor_manager->IsCursorEnabled()); |
| 259 cursor_manager->ShowCursor(true); |
| 260 EXPECT_FALSE(cursor_manager->IsCursorVisible()); |
| 261 EXPECT_FALSE(cursor_manager->IsCursorEnabled()); |
| 262 cursor_manager->EnableCursor(false); |
| 263 EXPECT_FALSE(cursor_manager->IsCursorVisible()); |
| 264 EXPECT_FALSE(cursor_manager->IsCursorEnabled()); |
| 265 } |
| 266 |
| 267 // Verifies that RootWindow generates a mouse event located outside of a window |
| 268 // when the cursor is disabled. |
| 269 #if defined(OS_WIN) |
| 270 // Temporarily disabled for windows. See crbug.com/112222. |
| 271 TEST_F(CursorManagerTest, DISABLED_DisabledCursorLocation) { |
| 272 #else |
| 273 TEST_F(CursorManagerTest, DisabledCursorLocation) { |
| 274 #endif // defined(OS_WIN) |
| 275 scoped_ptr<MouseEventLocationDelegate> delegate( |
| 276 new MouseEventLocationDelegate()); |
| 277 const int kWindowWidth = 123; |
| 278 const int kWindowHeight = 45; |
| 279 gfx::Rect bounds(100, 200, kWindowWidth, kWindowHeight); |
| 280 scoped_ptr<aura::Window> window(aura::test::CreateTestWindowWithDelegate( |
| 281 delegate.get(), 1, bounds, Shell::GetInstance()->GetPrimaryRootWindow())); |
| 282 |
| 283 CursorManager* cursor_manager = Shell::GetInstance()->cursor_manager(); |
| 284 cursor_manager->EnableCursor(true); |
| 285 // Send a mouse event to window. |
| 286 gfx::Point point(101, 201); |
| 287 gfx::Point local_point; |
| 288 ui::MouseEvent event(ui::ET_MOUSE_MOVED, point, point, 0); |
| 289 aura::RootWindow* root_window = window->GetRootWindow(); |
| 290 root_window->AsRootWindowHostDelegate()->OnHostMouseEvent(&event); |
| 291 |
| 292 // Location was in window. |
| 293 local_point = delegate->mouse_event_location(); |
| 294 aura::Window::ConvertPointToTarget(window.get(), root_window, &local_point); |
| 295 EXPECT_TRUE(window->bounds().Contains(local_point)); |
| 296 |
| 297 // Location is now out of window. |
| 298 cursor_manager->EnableCursor(false); |
| 299 RunAllPendingInMessageLoop(); |
| 300 local_point = delegate->mouse_event_location(); |
| 301 aura::Window::ConvertPointToTarget(window.get(), root_window, &local_point); |
| 302 EXPECT_FALSE(window->bounds().Contains(local_point)); |
| 303 |
| 304 // Location is back in window. |
| 305 cursor_manager->EnableCursor(true); |
| 306 RunAllPendingInMessageLoop(); |
| 307 local_point = delegate->mouse_event_location(); |
| 308 aura::Window::ConvertPointToTarget(window.get(), root_window, &local_point); |
| 309 EXPECT_TRUE(window->bounds().Contains(local_point)); |
| 310 } |
| 311 |
| 312 TEST_F(CursorManagerTest, DisabledQueryMouseLocation) { |
| 313 aura::RootWindow* root_window = Shell::GetInstance()->GetPrimaryRootWindow(); |
| 314 root_window->MoveCursorTo(gfx::Point(10, 10)); |
| 315 gfx::Point mouse_location; |
| 316 EXPECT_TRUE(root_window->QueryMouseLocationForTest(&mouse_location)); |
| 317 EXPECT_EQ("10,10", mouse_location.ToString()); |
| 318 Shell::GetInstance()->cursor_manager()->EnableCursor(false); |
| 319 EXPECT_FALSE(root_window->QueryMouseLocationForTest(&mouse_location)); |
| 320 EXPECT_EQ("0,0", mouse_location.ToString()); |
| 321 } |
| 322 |
121 } // namespace test | 323 } // namespace test |
122 } // namespace ash | 324 } // namespace ash |
OLD | NEW |