OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "ui/views/corewm/cursor_manager.h" |
| 6 |
| 7 #include "ui/views/corewm/native_cursor_manager.h" |
| 8 #include "ui/views/test/views_test_base.h" |
| 9 |
| 10 namespace { |
| 11 |
| 12 class TestingCursorManager : public views::corewm::NativeCursorManager { |
| 13 public: |
| 14 gfx::NativeCursor current_cursor() { return cursor_; } |
| 15 |
| 16 // Overridden from views::corewm::NativeCursorManager: |
| 17 virtual void SetDeviceScaleFactor( |
| 18 float device_scale_factor, |
| 19 views::corewm::NativeCursorManagerDelegate* delegate) OVERRIDE {} |
| 20 |
| 21 virtual void SetCursor( |
| 22 gfx::NativeCursor cursor, |
| 23 views::corewm::NativeCursorManagerDelegate* delegate) OVERRIDE { |
| 24 cursor_ = cursor; |
| 25 delegate->CommitCursor(cursor); |
| 26 } |
| 27 |
| 28 virtual void SetVisibility( |
| 29 bool visible, |
| 30 views::corewm::NativeCursorManagerDelegate* delegate) OVERRIDE { |
| 31 delegate->CommitVisibility(visible); |
| 32 } |
| 33 |
| 34 virtual void SetMouseEventsEnabled( |
| 35 bool enabled, |
| 36 views::corewm::NativeCursorManagerDelegate* delegate) OVERRIDE { |
| 37 delegate->CommitMouseEventsEnabled(enabled); |
| 38 } |
| 39 |
| 40 private: |
| 41 gfx::NativeCursor cursor_; |
| 42 }; |
| 43 |
| 44 } // namespace |
| 45 |
| 46 class CursorManagerTest : public views::ViewsTestBase { |
| 47 protected: |
| 48 CursorManagerTest() |
| 49 : delegate_(new TestingCursorManager), |
| 50 cursor_manager_(scoped_ptr<views::corewm::NativeCursorManager>( |
| 51 delegate_)) { |
| 52 } |
| 53 |
| 54 gfx::NativeCursor current_cursor() { return delegate_->current_cursor(); } |
| 55 |
| 56 TestingCursorManager* delegate_; |
| 57 views::corewm::CursorManager cursor_manager_; |
| 58 }; |
| 59 |
| 60 TEST_F(CursorManagerTest, ShowHideCursor) { |
| 61 cursor_manager_.SetCursor(ui::kCursorCopy); |
| 62 EXPECT_EQ(ui::kCursorCopy, current_cursor().native_type()); |
| 63 |
| 64 cursor_manager_.ShowCursor(); |
| 65 EXPECT_TRUE(cursor_manager_.IsCursorVisible()); |
| 66 cursor_manager_.HideCursor(); |
| 67 EXPECT_FALSE(cursor_manager_.IsCursorVisible()); |
| 68 // The current cursor does not change even when the cursor is not shown. |
| 69 EXPECT_EQ(ui::kCursorCopy, current_cursor().native_type()); |
| 70 |
| 71 // Check if cursor visibility is locked. |
| 72 cursor_manager_.LockCursor(); |
| 73 EXPECT_FALSE(cursor_manager_.IsCursorVisible()); |
| 74 cursor_manager_.ShowCursor(); |
| 75 EXPECT_FALSE(cursor_manager_.IsCursorVisible()); |
| 76 cursor_manager_.UnlockCursor(); |
| 77 EXPECT_TRUE(cursor_manager_.IsCursorVisible()); |
| 78 |
| 79 cursor_manager_.LockCursor(); |
| 80 EXPECT_TRUE(cursor_manager_.IsCursorVisible()); |
| 81 cursor_manager_.HideCursor(); |
| 82 EXPECT_TRUE(cursor_manager_.IsCursorVisible()); |
| 83 cursor_manager_.UnlockCursor(); |
| 84 EXPECT_FALSE(cursor_manager_.IsCursorVisible()); |
| 85 |
| 86 // Checks setting visiblity while cursor is locked does not affect the |
| 87 // subsequent uses of UnlockCursor. |
| 88 cursor_manager_.LockCursor(); |
| 89 cursor_manager_.HideCursor(); |
| 90 cursor_manager_.UnlockCursor(); |
| 91 EXPECT_FALSE(cursor_manager_.IsCursorVisible()); |
| 92 |
| 93 cursor_manager_.ShowCursor(); |
| 94 cursor_manager_.LockCursor(); |
| 95 cursor_manager_.UnlockCursor(); |
| 96 EXPECT_TRUE(cursor_manager_.IsCursorVisible()); |
| 97 |
| 98 cursor_manager_.LockCursor(); |
| 99 cursor_manager_.ShowCursor(); |
| 100 cursor_manager_.UnlockCursor(); |
| 101 EXPECT_TRUE(cursor_manager_.IsCursorVisible()); |
| 102 |
| 103 cursor_manager_.HideCursor(); |
| 104 cursor_manager_.LockCursor(); |
| 105 cursor_manager_.UnlockCursor(); |
| 106 EXPECT_FALSE(cursor_manager_.IsCursorVisible()); |
| 107 } |
| 108 |
| 109 // Verifies that LockCursor/UnlockCursor work correctly with |
| 110 // EnableMouseEvents and DisableMouseEvents |
| 111 TEST_F(CursorManagerTest, EnableDisableMouseEvents) { |
| 112 cursor_manager_.SetCursor(ui::kCursorCopy); |
| 113 EXPECT_EQ(ui::kCursorCopy, current_cursor().native_type()); |
| 114 |
| 115 cursor_manager_.EnableMouseEvents(); |
| 116 EXPECT_TRUE(cursor_manager_.IsMouseEventsEnabled()); |
| 117 cursor_manager_.DisableMouseEvents(); |
| 118 EXPECT_FALSE(cursor_manager_.IsMouseEventsEnabled()); |
| 119 // The current cursor does not change even when the cursor is not shown. |
| 120 EXPECT_EQ(ui::kCursorCopy, current_cursor().native_type()); |
| 121 |
| 122 // Check if cursor enable state is locked. |
| 123 cursor_manager_.LockCursor(); |
| 124 EXPECT_FALSE(cursor_manager_.IsMouseEventsEnabled()); |
| 125 cursor_manager_.EnableMouseEvents(); |
| 126 EXPECT_FALSE(cursor_manager_.IsMouseEventsEnabled()); |
| 127 cursor_manager_.UnlockCursor(); |
| 128 EXPECT_TRUE(cursor_manager_.IsMouseEventsEnabled()); |
| 129 |
| 130 cursor_manager_.LockCursor(); |
| 131 EXPECT_TRUE(cursor_manager_.IsMouseEventsEnabled()); |
| 132 cursor_manager_.DisableMouseEvents(); |
| 133 EXPECT_TRUE(cursor_manager_.IsMouseEventsEnabled()); |
| 134 cursor_manager_.UnlockCursor(); |
| 135 EXPECT_FALSE(cursor_manager_.IsMouseEventsEnabled()); |
| 136 |
| 137 // Checks enabling cursor while cursor is locked does not affect the |
| 138 // subsequent uses of UnlockCursor. |
| 139 cursor_manager_.LockCursor(); |
| 140 cursor_manager_.DisableMouseEvents(); |
| 141 cursor_manager_.UnlockCursor(); |
| 142 EXPECT_FALSE(cursor_manager_.IsMouseEventsEnabled()); |
| 143 |
| 144 cursor_manager_.EnableMouseEvents(); |
| 145 cursor_manager_.LockCursor(); |
| 146 cursor_manager_.UnlockCursor(); |
| 147 EXPECT_TRUE(cursor_manager_.IsMouseEventsEnabled()); |
| 148 |
| 149 cursor_manager_.LockCursor(); |
| 150 cursor_manager_.EnableMouseEvents(); |
| 151 cursor_manager_.UnlockCursor(); |
| 152 EXPECT_TRUE(cursor_manager_.IsMouseEventsEnabled()); |
| 153 |
| 154 cursor_manager_.DisableMouseEvents(); |
| 155 cursor_manager_.LockCursor(); |
| 156 cursor_manager_.UnlockCursor(); |
| 157 EXPECT_FALSE(cursor_manager_.IsMouseEventsEnabled()); |
| 158 } |
| 159 |
| 160 TEST_F(CursorManagerTest, IsMouseEventsEnabled) { |
| 161 cursor_manager_.EnableMouseEvents(); |
| 162 EXPECT_TRUE(cursor_manager_.IsMouseEventsEnabled()); |
| 163 cursor_manager_.DisableMouseEvents(); |
| 164 EXPECT_FALSE(cursor_manager_.IsMouseEventsEnabled()); |
| 165 } |
| 166 |
| 167 // Verifies that the mouse events enable state changes correctly when |
| 168 // ShowCursor/HideCursor and EnableMouseEvents/DisableMouseEvents are used |
| 169 // together. |
| 170 TEST_F(CursorManagerTest, ShowAndEnable) { |
| 171 // Changing the visibility of the cursor does not affect the enable state. |
| 172 cursor_manager_.EnableMouseEvents(); |
| 173 cursor_manager_.ShowCursor(); |
| 174 EXPECT_TRUE(cursor_manager_.IsCursorVisible()); |
| 175 EXPECT_TRUE(cursor_manager_.IsMouseEventsEnabled()); |
| 176 cursor_manager_.HideCursor(); |
| 177 EXPECT_FALSE(cursor_manager_.IsCursorVisible()); |
| 178 EXPECT_TRUE(cursor_manager_.IsMouseEventsEnabled()); |
| 179 cursor_manager_.ShowCursor(); |
| 180 EXPECT_TRUE(cursor_manager_.IsCursorVisible()); |
| 181 EXPECT_TRUE(cursor_manager_.IsMouseEventsEnabled()); |
| 182 |
| 183 // When mouse events are disabled, it also gets invisible. |
| 184 EXPECT_TRUE(cursor_manager_.IsCursorVisible()); |
| 185 cursor_manager_.DisableMouseEvents(); |
| 186 EXPECT_FALSE(cursor_manager_.IsCursorVisible()); |
| 187 EXPECT_FALSE(cursor_manager_.IsMouseEventsEnabled()); |
| 188 |
| 189 // When mouse events are enabled, it restores the visibility state. |
| 190 cursor_manager_.EnableMouseEvents(); |
| 191 EXPECT_TRUE(cursor_manager_.IsCursorVisible()); |
| 192 EXPECT_TRUE(cursor_manager_.IsMouseEventsEnabled()); |
| 193 |
| 194 cursor_manager_.ShowCursor(); |
| 195 cursor_manager_.DisableMouseEvents(); |
| 196 EXPECT_FALSE(cursor_manager_.IsCursorVisible()); |
| 197 EXPECT_FALSE(cursor_manager_.IsMouseEventsEnabled()); |
| 198 cursor_manager_.EnableMouseEvents(); |
| 199 EXPECT_TRUE(cursor_manager_.IsCursorVisible()); |
| 200 EXPECT_TRUE(cursor_manager_.IsMouseEventsEnabled()); |
| 201 |
| 202 cursor_manager_.HideCursor(); |
| 203 cursor_manager_.DisableMouseEvents(); |
| 204 EXPECT_FALSE(cursor_manager_.IsCursorVisible()); |
| 205 EXPECT_FALSE(cursor_manager_.IsMouseEventsEnabled()); |
| 206 cursor_manager_.EnableMouseEvents(); |
| 207 EXPECT_FALSE(cursor_manager_.IsCursorVisible()); |
| 208 EXPECT_TRUE(cursor_manager_.IsMouseEventsEnabled()); |
| 209 |
| 210 // When mouse events are disabled, ShowCursor is ignored. |
| 211 cursor_manager_.DisableMouseEvents(); |
| 212 EXPECT_FALSE(cursor_manager_.IsCursorVisible()); |
| 213 EXPECT_FALSE(cursor_manager_.IsMouseEventsEnabled()); |
| 214 cursor_manager_.ShowCursor(); |
| 215 EXPECT_FALSE(cursor_manager_.IsCursorVisible()); |
| 216 EXPECT_FALSE(cursor_manager_.IsMouseEventsEnabled()); |
| 217 cursor_manager_.DisableMouseEvents(); |
| 218 EXPECT_FALSE(cursor_manager_.IsCursorVisible()); |
| 219 EXPECT_FALSE(cursor_manager_.IsMouseEventsEnabled()); |
| 220 } |
| 221 |
| 222 // Verifies that calling DisableMouseEvents multiple times in a row makes no |
| 223 // difference compared with calling it once. |
| 224 // This is a regression test for http://crbug.com/169404. |
| 225 TEST_F(CursorManagerTest, MultipleDisableMouseEvents) { |
| 226 cursor_manager_.DisableMouseEvents(); |
| 227 cursor_manager_.DisableMouseEvents(); |
| 228 cursor_manager_.EnableMouseEvents(); |
| 229 cursor_manager_.LockCursor(); |
| 230 cursor_manager_.UnlockCursor(); |
| 231 EXPECT_TRUE(cursor_manager_.IsCursorVisible()); |
| 232 } |
| 233 |
| 234 // Verifies that calling EnableMouseEvents multiple times in a row makes no |
| 235 // difference compared with calling it once. |
| 236 TEST_F(CursorManagerTest, MultipleEnableMouseEvents) { |
| 237 cursor_manager_.DisableMouseEvents(); |
| 238 cursor_manager_.EnableMouseEvents(); |
| 239 cursor_manager_.EnableMouseEvents(); |
| 240 cursor_manager_.LockCursor(); |
| 241 cursor_manager_.UnlockCursor(); |
| 242 EXPECT_TRUE(cursor_manager_.IsCursorVisible()); |
| 243 } |
OLD | NEW |