| OLD | NEW |
| 1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2013 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 "ui/wm/core/cursor_manager.h" | 5 #include "ui/wm/core/cursor_manager.h" |
| 6 | 6 |
| 7 #include "base/macros.h" | 7 #include "base/macros.h" |
| 8 #include "base/memory/ptr_util.h" | 8 #include "base/memory/ptr_util.h" |
| 9 #include "ui/aura/client/cursor_client_observer.h" | 9 #include "ui/aura/client/cursor_client_observer.h" |
| 10 #include "ui/aura/test/aura_test_base.h" | 10 #include "ui/aura/test/aura_test_base.h" |
| 11 #include "ui/wm/core/native_cursor_manager.h" | 11 #include "ui/wm/core/native_cursor_manager.h" |
| 12 #include "ui/wm/test/testing_cursor_client_observer.h" |
| 12 | 13 |
| 13 namespace { | 14 namespace { |
| 14 | 15 |
| 15 class TestingCursorManager : public wm::NativeCursorManager { | 16 class TestingCursorManager : public wm::NativeCursorManager { |
| 16 public: | 17 public: |
| 17 // Overridden from wm::NativeCursorManager: | 18 // Overridden from wm::NativeCursorManager: |
| 18 void SetDisplay(const display::Display& display, | 19 void SetDisplay(const display::Display& display, |
| 19 wm::NativeCursorManagerDelegate* delegate) override {} | 20 wm::NativeCursorManagerDelegate* delegate) override {} |
| 20 | 21 |
| 21 void SetCursor(gfx::NativeCursor cursor, | 22 void SetCursor(gfx::NativeCursor cursor, |
| (...skipping 23 matching lines...) Expand all Loading... |
| 45 class CursorManagerTest : public aura::test::AuraTestBase { | 46 class CursorManagerTest : public aura::test::AuraTestBase { |
| 46 protected: | 47 protected: |
| 47 CursorManagerTest() | 48 CursorManagerTest() |
| 48 : delegate_(new TestingCursorManager), | 49 : delegate_(new TestingCursorManager), |
| 49 cursor_manager_(base::WrapUnique(delegate_)) {} | 50 cursor_manager_(base::WrapUnique(delegate_)) {} |
| 50 | 51 |
| 51 TestingCursorManager* delegate_; | 52 TestingCursorManager* delegate_; |
| 52 wm::CursorManager cursor_manager_; | 53 wm::CursorManager cursor_manager_; |
| 53 }; | 54 }; |
| 54 | 55 |
| 55 class TestingCursorClientObserver : public aura::client::CursorClientObserver { | |
| 56 public: | |
| 57 TestingCursorClientObserver() | |
| 58 : cursor_visibility_(false), | |
| 59 did_visibility_change_(false) {} | |
| 60 void reset() { cursor_visibility_ = did_visibility_change_ = false; } | |
| 61 bool is_cursor_visible() const { return cursor_visibility_; } | |
| 62 bool did_visibility_change() const { return did_visibility_change_; } | |
| 63 | |
| 64 // Overridden from aura::client::CursorClientObserver: | |
| 65 void OnCursorVisibilityChanged(bool is_visible) override { | |
| 66 cursor_visibility_ = is_visible; | |
| 67 did_visibility_change_ = true; | |
| 68 } | |
| 69 | |
| 70 private: | |
| 71 bool cursor_visibility_; | |
| 72 bool did_visibility_change_; | |
| 73 | |
| 74 DISALLOW_COPY_AND_ASSIGN(TestingCursorClientObserver); | |
| 75 }; | |
| 76 | |
| 77 TEST_F(CursorManagerTest, ShowHideCursor) { | 56 TEST_F(CursorManagerTest, ShowHideCursor) { |
| 78 cursor_manager_.SetCursor(ui::kCursorCopy); | 57 cursor_manager_.SetCursor(ui::kCursorCopy); |
| 79 EXPECT_EQ(ui::kCursorCopy, cursor_manager_.GetCursor().native_type()); | 58 EXPECT_EQ(ui::kCursorCopy, cursor_manager_.GetCursor().native_type()); |
| 80 | 59 |
| 81 cursor_manager_.ShowCursor(); | 60 cursor_manager_.ShowCursor(); |
| 82 EXPECT_TRUE(cursor_manager_.IsCursorVisible()); | 61 EXPECT_TRUE(cursor_manager_.IsCursorVisible()); |
| 83 cursor_manager_.HideCursor(); | 62 cursor_manager_.HideCursor(); |
| 84 EXPECT_FALSE(cursor_manager_.IsCursorVisible()); | 63 EXPECT_FALSE(cursor_manager_.IsCursorVisible()); |
| 85 // The current cursor does not change even when the cursor is not shown. | 64 // The current cursor does not change even when the cursor is not shown. |
| 86 EXPECT_EQ(ui::kCursorCopy, cursor_manager_.GetCursor().native_type()); | 65 EXPECT_EQ(ui::kCursorCopy, cursor_manager_.GetCursor().native_type()); |
| (...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 168 cursor_manager_.UnlockCursor(); | 147 cursor_manager_.UnlockCursor(); |
| 169 EXPECT_TRUE(cursor_manager_.IsMouseEventsEnabled()); | 148 EXPECT_TRUE(cursor_manager_.IsMouseEventsEnabled()); |
| 170 | 149 |
| 171 cursor_manager_.DisableMouseEvents(); | 150 cursor_manager_.DisableMouseEvents(); |
| 172 cursor_manager_.LockCursor(); | 151 cursor_manager_.LockCursor(); |
| 173 cursor_manager_.UnlockCursor(); | 152 cursor_manager_.UnlockCursor(); |
| 174 EXPECT_FALSE(cursor_manager_.IsMouseEventsEnabled()); | 153 EXPECT_FALSE(cursor_manager_.IsMouseEventsEnabled()); |
| 175 } | 154 } |
| 176 | 155 |
| 177 TEST_F(CursorManagerTest, SetCursorSet) { | 156 TEST_F(CursorManagerTest, SetCursorSet) { |
| 157 wm::TestingCursorClientObserver observer; |
| 158 cursor_manager_.AddObserver(&observer); |
| 159 |
| 178 EXPECT_EQ(ui::CURSOR_SET_NORMAL, cursor_manager_.GetCursorSet()); | 160 EXPECT_EQ(ui::CURSOR_SET_NORMAL, cursor_manager_.GetCursorSet()); |
| 161 EXPECT_EQ(ui::CURSOR_SET_NORMAL, observer.cursor_set()); |
| 179 | 162 |
| 180 cursor_manager_.SetCursorSet(ui::CURSOR_SET_NORMAL); | 163 cursor_manager_.SetCursorSet(ui::CURSOR_SET_NORMAL); |
| 181 EXPECT_EQ(ui::CURSOR_SET_NORMAL, cursor_manager_.GetCursorSet()); | 164 EXPECT_EQ(ui::CURSOR_SET_NORMAL, cursor_manager_.GetCursorSet()); |
| 165 EXPECT_EQ(ui::CURSOR_SET_NORMAL, observer.cursor_set()); |
| 182 | 166 |
| 183 cursor_manager_.SetCursorSet(ui::CURSOR_SET_LARGE); | 167 cursor_manager_.SetCursorSet(ui::CURSOR_SET_LARGE); |
| 184 EXPECT_EQ(ui::CURSOR_SET_LARGE, cursor_manager_.GetCursorSet()); | 168 EXPECT_EQ(ui::CURSOR_SET_LARGE, cursor_manager_.GetCursorSet()); |
| 169 EXPECT_EQ(ui::CURSOR_SET_LARGE, observer.cursor_set()); |
| 185 | 170 |
| 186 cursor_manager_.SetCursorSet(ui::CURSOR_SET_NORMAL); | 171 cursor_manager_.SetCursorSet(ui::CURSOR_SET_NORMAL); |
| 187 EXPECT_EQ(ui::CURSOR_SET_NORMAL, cursor_manager_.GetCursorSet()); | 172 EXPECT_EQ(ui::CURSOR_SET_NORMAL, cursor_manager_.GetCursorSet()); |
| 173 EXPECT_EQ(ui::CURSOR_SET_NORMAL, observer.cursor_set()); |
| 188 } | 174 } |
| 189 | 175 |
| 190 TEST_F(CursorManagerTest, IsMouseEventsEnabled) { | 176 TEST_F(CursorManagerTest, IsMouseEventsEnabled) { |
| 191 cursor_manager_.EnableMouseEvents(); | 177 cursor_manager_.EnableMouseEvents(); |
| 192 EXPECT_TRUE(cursor_manager_.IsMouseEventsEnabled()); | 178 EXPECT_TRUE(cursor_manager_.IsMouseEventsEnabled()); |
| 193 cursor_manager_.DisableMouseEvents(); | 179 cursor_manager_.DisableMouseEvents(); |
| 194 EXPECT_FALSE(cursor_manager_.IsMouseEventsEnabled()); | 180 EXPECT_FALSE(cursor_manager_.IsMouseEventsEnabled()); |
| 195 } | 181 } |
| 196 | 182 |
| 197 // Verifies that the mouse events enable state changes correctly when | 183 // Verifies that the mouse events enable state changes correctly when |
| (...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 268 cursor_manager_.EnableMouseEvents(); | 254 cursor_manager_.EnableMouseEvents(); |
| 269 cursor_manager_.EnableMouseEvents(); | 255 cursor_manager_.EnableMouseEvents(); |
| 270 cursor_manager_.LockCursor(); | 256 cursor_manager_.LockCursor(); |
| 271 cursor_manager_.UnlockCursor(); | 257 cursor_manager_.UnlockCursor(); |
| 272 EXPECT_TRUE(cursor_manager_.IsCursorVisible()); | 258 EXPECT_TRUE(cursor_manager_.IsCursorVisible()); |
| 273 } | 259 } |
| 274 | 260 |
| 275 TEST_F(CursorManagerTest, TestCursorClientObserver) { | 261 TEST_F(CursorManagerTest, TestCursorClientObserver) { |
| 276 // Add two observers. Both should have OnCursorVisibilityChanged() | 262 // Add two observers. Both should have OnCursorVisibilityChanged() |
| 277 // invoked when the visibility of the cursor changes. | 263 // invoked when the visibility of the cursor changes. |
| 278 TestingCursorClientObserver observer_a; | 264 wm::TestingCursorClientObserver observer_a; |
| 279 TestingCursorClientObserver observer_b; | 265 wm::TestingCursorClientObserver observer_b; |
| 280 cursor_manager_.AddObserver(&observer_a); | 266 cursor_manager_.AddObserver(&observer_a); |
| 281 cursor_manager_.AddObserver(&observer_b); | 267 cursor_manager_.AddObserver(&observer_b); |
| 282 | 268 |
| 283 // Initial state before any events have been sent. | 269 // Initial state before any events have been sent. |
| 284 observer_a.reset(); | 270 observer_a.reset(); |
| 285 observer_b.reset(); | 271 observer_b.reset(); |
| 286 EXPECT_FALSE(observer_a.did_visibility_change()); | 272 EXPECT_FALSE(observer_a.did_visibility_change()); |
| 287 EXPECT_FALSE(observer_b.did_visibility_change()); | 273 EXPECT_FALSE(observer_b.did_visibility_change()); |
| 288 EXPECT_FALSE(observer_a.is_cursor_visible()); | 274 EXPECT_FALSE(observer_a.is_cursor_visible()); |
| 289 EXPECT_FALSE(observer_b.is_cursor_visible()); | 275 EXPECT_FALSE(observer_b.is_cursor_visible()); |
| 276 EXPECT_FALSE(observer_a.did_cursor_set_change()); |
| 277 EXPECT_FALSE(observer_b.did_cursor_set_change()); |
| 290 | 278 |
| 291 // Hide the cursor using HideCursor(). | 279 // Hide the cursor using HideCursor(). |
| 292 cursor_manager_.HideCursor(); | 280 cursor_manager_.HideCursor(); |
| 293 EXPECT_TRUE(observer_a.did_visibility_change()); | 281 EXPECT_TRUE(observer_a.did_visibility_change()); |
| 294 EXPECT_TRUE(observer_b.did_visibility_change()); | 282 EXPECT_TRUE(observer_b.did_visibility_change()); |
| 295 EXPECT_FALSE(observer_a.is_cursor_visible()); | 283 EXPECT_FALSE(observer_a.is_cursor_visible()); |
| 296 EXPECT_FALSE(observer_b.is_cursor_visible()); | 284 EXPECT_FALSE(observer_b.is_cursor_visible()); |
| 297 | 285 |
| 286 // Set the cursor set. |
| 287 cursor_manager_.SetCursorSet(ui::CURSOR_SET_LARGE); |
| 288 EXPECT_TRUE(observer_a.did_cursor_set_change()); |
| 289 EXPECT_EQ(ui::CURSOR_SET_LARGE, observer_a.cursor_set()); |
| 290 EXPECT_TRUE(observer_b.did_cursor_set_change()); |
| 291 EXPECT_EQ(ui::CURSOR_SET_LARGE, observer_b.cursor_set()); |
| 292 |
| 298 // Show the cursor using ShowCursor(). | 293 // Show the cursor using ShowCursor(). |
| 299 observer_a.reset(); | 294 observer_a.reset(); |
| 300 observer_b.reset(); | 295 observer_b.reset(); |
| 301 cursor_manager_.ShowCursor(); | 296 cursor_manager_.ShowCursor(); |
| 302 EXPECT_TRUE(observer_a.did_visibility_change()); | 297 EXPECT_TRUE(observer_a.did_visibility_change()); |
| 303 EXPECT_TRUE(observer_b.did_visibility_change()); | 298 EXPECT_TRUE(observer_b.did_visibility_change()); |
| 304 EXPECT_TRUE(observer_a.is_cursor_visible()); | 299 EXPECT_TRUE(observer_a.is_cursor_visible()); |
| 305 EXPECT_TRUE(observer_b.is_cursor_visible()); | 300 EXPECT_TRUE(observer_b.is_cursor_visible()); |
| 306 | 301 |
| 307 // Remove observer_b. Its OnCursorVisibilityChanged() should | 302 // Remove observer_b. Its OnCursorVisibilityChanged() should |
| 308 // not be invoked past this point. | 303 // not be invoked past this point. |
| 309 cursor_manager_.RemoveObserver(&observer_b); | 304 cursor_manager_.RemoveObserver(&observer_b); |
| 310 | 305 |
| 311 // Hide the cursor using HideCursor(). | 306 // Hide the cursor using HideCursor(). |
| 312 observer_a.reset(); | 307 observer_a.reset(); |
| 313 observer_b.reset(); | 308 observer_b.reset(); |
| 314 cursor_manager_.HideCursor(); | 309 cursor_manager_.HideCursor(); |
| 315 EXPECT_TRUE(observer_a.did_visibility_change()); | 310 EXPECT_TRUE(observer_a.did_visibility_change()); |
| 316 EXPECT_FALSE(observer_b.did_visibility_change()); | 311 EXPECT_FALSE(observer_b.did_visibility_change()); |
| 317 EXPECT_FALSE(observer_a.is_cursor_visible()); | 312 EXPECT_FALSE(observer_a.is_cursor_visible()); |
| 318 | 313 |
| 314 // Set back the cursor set to normal. |
| 315 cursor_manager_.SetCursorSet(ui::CURSOR_SET_NORMAL); |
| 316 EXPECT_TRUE(observer_a.did_cursor_set_change()); |
| 317 EXPECT_EQ(ui::CURSOR_SET_NORMAL, observer_a.cursor_set()); |
| 318 EXPECT_FALSE(observer_b.did_cursor_set_change()); |
| 319 |
| 319 // Show the cursor using ShowCursor(). | 320 // Show the cursor using ShowCursor(). |
| 320 observer_a.reset(); | 321 observer_a.reset(); |
| 321 observer_b.reset(); | 322 observer_b.reset(); |
| 322 cursor_manager_.ShowCursor(); | 323 cursor_manager_.ShowCursor(); |
| 323 EXPECT_TRUE(observer_a.did_visibility_change()); | 324 EXPECT_TRUE(observer_a.did_visibility_change()); |
| 324 EXPECT_FALSE(observer_b.did_visibility_change()); | 325 EXPECT_FALSE(observer_b.did_visibility_change()); |
| 325 EXPECT_TRUE(observer_a.is_cursor_visible()); | 326 EXPECT_TRUE(observer_a.is_cursor_visible()); |
| 326 } | 327 } |
| 327 | 328 |
| 328 // This test validates that the cursor visiblity state is restored when a | 329 // This test validates that the cursor visiblity state is restored when a |
| (...skipping 23 matching lines...) Expand all Loading... |
| 352 // This block validates that the cursor is visible initially. It then | 353 // This block validates that the cursor is visible initially. It then |
| 353 // performs normal cursor visibility operations. | 354 // performs normal cursor visibility operations. |
| 354 { | 355 { |
| 355 wm::CursorManager cursor_manager3( | 356 wm::CursorManager cursor_manager3( |
| 356 base::WrapUnique(new TestingCursorManager)); | 357 base::WrapUnique(new TestingCursorManager)); |
| 357 EXPECT_TRUE(cursor_manager3.IsCursorVisible()); | 358 EXPECT_TRUE(cursor_manager3.IsCursorVisible()); |
| 358 cursor_manager3.HideCursor(); | 359 cursor_manager3.HideCursor(); |
| 359 EXPECT_FALSE(cursor_manager3.IsCursorVisible()); | 360 EXPECT_FALSE(cursor_manager3.IsCursorVisible()); |
| 360 } | 361 } |
| 361 } | 362 } |
| OLD | NEW |