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/aura/client/cursor_client_observer.h" | |
8 #include "ui/aura/test/aura_test_base.h" | |
9 #include "ui/views/corewm/native_cursor_manager.h" | |
10 | |
11 namespace { | |
12 | |
13 class TestingCursorManager : public views::corewm::NativeCursorManager { | |
14 public: | |
15 // Overridden from views::corewm::NativeCursorManager: | |
16 virtual void SetDisplay( | |
17 const gfx::Display& display, | |
18 views::corewm::NativeCursorManagerDelegate* delegate) OVERRIDE {} | |
19 | |
20 virtual void SetCursor( | |
21 gfx::NativeCursor cursor, | |
22 views::corewm::NativeCursorManagerDelegate* delegate) OVERRIDE { | |
23 delegate->CommitCursor(cursor); | |
24 } | |
25 | |
26 virtual void SetVisibility( | |
27 bool visible, | |
28 views::corewm::NativeCursorManagerDelegate* delegate) OVERRIDE { | |
29 delegate->CommitVisibility(visible); | |
30 } | |
31 | |
32 virtual void SetMouseEventsEnabled( | |
33 bool enabled, | |
34 views::corewm::NativeCursorManagerDelegate* delegate) OVERRIDE { | |
35 delegate->CommitMouseEventsEnabled(enabled); | |
36 } | |
37 | |
38 virtual void SetCursorSet( | |
39 ui::CursorSetType cursor_set, | |
40 views::corewm::NativeCursorManagerDelegate* delegate) OVERRIDE { | |
41 delegate->CommitCursorSet(cursor_set); | |
42 } | |
43 | |
44 virtual void SetScale( | |
45 float scale, | |
46 views::corewm::NativeCursorManagerDelegate* delegate) OVERRIDE { | |
47 delegate->CommitScale(scale); | |
48 } | |
49 }; | |
50 | |
51 } // namespace | |
52 | |
53 class CursorManagerTest : public aura::test::AuraTestBase { | |
54 protected: | |
55 CursorManagerTest() | |
56 : delegate_(new TestingCursorManager), | |
57 cursor_manager_(scoped_ptr<views::corewm::NativeCursorManager>( | |
58 delegate_)) { | |
59 } | |
60 | |
61 TestingCursorManager* delegate_; | |
62 views::corewm::CursorManager cursor_manager_; | |
63 }; | |
64 | |
65 class TestingCursorClientObserver : public aura::client::CursorClientObserver { | |
66 public: | |
67 TestingCursorClientObserver() | |
68 : cursor_visibility_(false), | |
69 did_visibility_change_(false) {} | |
70 void reset() { cursor_visibility_ = did_visibility_change_ = false; } | |
71 bool is_cursor_visible() const { return cursor_visibility_; } | |
72 bool did_visibility_change() const { return did_visibility_change_; } | |
73 | |
74 // Overridden from aura::client::CursorClientObserver: | |
75 virtual void OnCursorVisibilityChanged(bool is_visible) OVERRIDE { | |
76 cursor_visibility_ = is_visible; | |
77 did_visibility_change_ = true; | |
78 } | |
79 | |
80 private: | |
81 bool cursor_visibility_; | |
82 bool did_visibility_change_; | |
83 | |
84 DISALLOW_COPY_AND_ASSIGN(TestingCursorClientObserver); | |
85 }; | |
86 | |
87 TEST_F(CursorManagerTest, ShowHideCursor) { | |
88 cursor_manager_.SetCursor(ui::kCursorCopy); | |
89 EXPECT_EQ(ui::kCursorCopy, cursor_manager_.GetCursor().native_type()); | |
90 | |
91 cursor_manager_.ShowCursor(); | |
92 EXPECT_TRUE(cursor_manager_.IsCursorVisible()); | |
93 cursor_manager_.HideCursor(); | |
94 EXPECT_FALSE(cursor_manager_.IsCursorVisible()); | |
95 // The current cursor does not change even when the cursor is not shown. | |
96 EXPECT_EQ(ui::kCursorCopy, cursor_manager_.GetCursor().native_type()); | |
97 | |
98 // Check if cursor visibility is locked. | |
99 cursor_manager_.LockCursor(); | |
100 EXPECT_FALSE(cursor_manager_.IsCursorVisible()); | |
101 cursor_manager_.ShowCursor(); | |
102 EXPECT_FALSE(cursor_manager_.IsCursorVisible()); | |
103 cursor_manager_.UnlockCursor(); | |
104 EXPECT_TRUE(cursor_manager_.IsCursorVisible()); | |
105 | |
106 cursor_manager_.LockCursor(); | |
107 EXPECT_TRUE(cursor_manager_.IsCursorVisible()); | |
108 cursor_manager_.HideCursor(); | |
109 EXPECT_TRUE(cursor_manager_.IsCursorVisible()); | |
110 cursor_manager_.UnlockCursor(); | |
111 EXPECT_FALSE(cursor_manager_.IsCursorVisible()); | |
112 | |
113 // Checks setting visiblity while cursor is locked does not affect the | |
114 // subsequent uses of UnlockCursor. | |
115 cursor_manager_.LockCursor(); | |
116 cursor_manager_.HideCursor(); | |
117 cursor_manager_.UnlockCursor(); | |
118 EXPECT_FALSE(cursor_manager_.IsCursorVisible()); | |
119 | |
120 cursor_manager_.ShowCursor(); | |
121 cursor_manager_.LockCursor(); | |
122 cursor_manager_.UnlockCursor(); | |
123 EXPECT_TRUE(cursor_manager_.IsCursorVisible()); | |
124 | |
125 cursor_manager_.LockCursor(); | |
126 cursor_manager_.ShowCursor(); | |
127 cursor_manager_.UnlockCursor(); | |
128 EXPECT_TRUE(cursor_manager_.IsCursorVisible()); | |
129 | |
130 cursor_manager_.HideCursor(); | |
131 cursor_manager_.LockCursor(); | |
132 cursor_manager_.UnlockCursor(); | |
133 EXPECT_FALSE(cursor_manager_.IsCursorVisible()); | |
134 } | |
135 | |
136 // Verifies that LockCursor/UnlockCursor work correctly with | |
137 // EnableMouseEvents and DisableMouseEvents | |
138 TEST_F(CursorManagerTest, EnableDisableMouseEvents) { | |
139 cursor_manager_.SetCursor(ui::kCursorCopy); | |
140 EXPECT_EQ(ui::kCursorCopy, cursor_manager_.GetCursor().native_type()); | |
141 | |
142 cursor_manager_.EnableMouseEvents(); | |
143 EXPECT_TRUE(cursor_manager_.IsMouseEventsEnabled()); | |
144 cursor_manager_.DisableMouseEvents(); | |
145 EXPECT_FALSE(cursor_manager_.IsMouseEventsEnabled()); | |
146 // The current cursor does not change even when the cursor is not shown. | |
147 EXPECT_EQ(ui::kCursorCopy, cursor_manager_.GetCursor().native_type()); | |
148 | |
149 // Check if cursor enable state is locked. | |
150 cursor_manager_.LockCursor(); | |
151 EXPECT_FALSE(cursor_manager_.IsMouseEventsEnabled()); | |
152 cursor_manager_.EnableMouseEvents(); | |
153 EXPECT_FALSE(cursor_manager_.IsMouseEventsEnabled()); | |
154 cursor_manager_.UnlockCursor(); | |
155 EXPECT_TRUE(cursor_manager_.IsMouseEventsEnabled()); | |
156 | |
157 cursor_manager_.LockCursor(); | |
158 EXPECT_TRUE(cursor_manager_.IsMouseEventsEnabled()); | |
159 cursor_manager_.DisableMouseEvents(); | |
160 EXPECT_TRUE(cursor_manager_.IsMouseEventsEnabled()); | |
161 cursor_manager_.UnlockCursor(); | |
162 EXPECT_FALSE(cursor_manager_.IsMouseEventsEnabled()); | |
163 | |
164 // Checks enabling cursor while cursor is locked does not affect the | |
165 // subsequent uses of UnlockCursor. | |
166 cursor_manager_.LockCursor(); | |
167 cursor_manager_.DisableMouseEvents(); | |
168 cursor_manager_.UnlockCursor(); | |
169 EXPECT_FALSE(cursor_manager_.IsMouseEventsEnabled()); | |
170 | |
171 cursor_manager_.EnableMouseEvents(); | |
172 cursor_manager_.LockCursor(); | |
173 cursor_manager_.UnlockCursor(); | |
174 EXPECT_TRUE(cursor_manager_.IsMouseEventsEnabled()); | |
175 | |
176 cursor_manager_.LockCursor(); | |
177 cursor_manager_.EnableMouseEvents(); | |
178 cursor_manager_.UnlockCursor(); | |
179 EXPECT_TRUE(cursor_manager_.IsMouseEventsEnabled()); | |
180 | |
181 cursor_manager_.DisableMouseEvents(); | |
182 cursor_manager_.LockCursor(); | |
183 cursor_manager_.UnlockCursor(); | |
184 EXPECT_FALSE(cursor_manager_.IsMouseEventsEnabled()); | |
185 } | |
186 | |
187 TEST_F(CursorManagerTest, SetCursorSet) { | |
188 EXPECT_EQ(ui::CURSOR_SET_NORMAL, cursor_manager_.GetCursorSet()); | |
189 | |
190 cursor_manager_.SetCursorSet(ui::CURSOR_SET_NORMAL); | |
191 EXPECT_EQ(ui::CURSOR_SET_NORMAL, cursor_manager_.GetCursorSet()); | |
192 | |
193 cursor_manager_.SetCursorSet(ui::CURSOR_SET_LARGE); | |
194 EXPECT_EQ(ui::CURSOR_SET_LARGE, cursor_manager_.GetCursorSet()); | |
195 | |
196 cursor_manager_.SetCursorSet(ui::CURSOR_SET_NORMAL); | |
197 EXPECT_EQ(ui::CURSOR_SET_NORMAL, cursor_manager_.GetCursorSet()); | |
198 } | |
199 | |
200 TEST_F(CursorManagerTest, SetScale) { | |
201 EXPECT_EQ(1.f, cursor_manager_.GetScale()); | |
202 cursor_manager_.SetScale(2.f); | |
203 EXPECT_EQ(2.f, cursor_manager_.GetScale()); | |
204 | |
205 // Cusror scale does change even while cursor is locked. | |
206 cursor_manager_.LockCursor(); | |
207 EXPECT_EQ(2.f, cursor_manager_.GetScale()); | |
208 cursor_manager_.SetScale(2.5f); | |
209 EXPECT_EQ(2.5f, cursor_manager_.GetScale()); | |
210 cursor_manager_.UnlockCursor(); | |
211 | |
212 EXPECT_EQ(2.5f, cursor_manager_.GetScale()); | |
213 cursor_manager_.SetScale(1.f); | |
214 EXPECT_EQ(1.f, cursor_manager_.GetScale()); | |
215 } | |
216 | |
217 TEST_F(CursorManagerTest, IsMouseEventsEnabled) { | |
218 cursor_manager_.EnableMouseEvents(); | |
219 EXPECT_TRUE(cursor_manager_.IsMouseEventsEnabled()); | |
220 cursor_manager_.DisableMouseEvents(); | |
221 EXPECT_FALSE(cursor_manager_.IsMouseEventsEnabled()); | |
222 } | |
223 | |
224 // Verifies that the mouse events enable state changes correctly when | |
225 // ShowCursor/HideCursor and EnableMouseEvents/DisableMouseEvents are used | |
226 // together. | |
227 TEST_F(CursorManagerTest, ShowAndEnable) { | |
228 // Changing the visibility of the cursor does not affect the enable state. | |
229 cursor_manager_.EnableMouseEvents(); | |
230 cursor_manager_.ShowCursor(); | |
231 EXPECT_TRUE(cursor_manager_.IsCursorVisible()); | |
232 EXPECT_TRUE(cursor_manager_.IsMouseEventsEnabled()); | |
233 cursor_manager_.HideCursor(); | |
234 EXPECT_FALSE(cursor_manager_.IsCursorVisible()); | |
235 EXPECT_TRUE(cursor_manager_.IsMouseEventsEnabled()); | |
236 cursor_manager_.ShowCursor(); | |
237 EXPECT_TRUE(cursor_manager_.IsCursorVisible()); | |
238 EXPECT_TRUE(cursor_manager_.IsMouseEventsEnabled()); | |
239 | |
240 // When mouse events are disabled, it also gets invisible. | |
241 EXPECT_TRUE(cursor_manager_.IsCursorVisible()); | |
242 cursor_manager_.DisableMouseEvents(); | |
243 EXPECT_FALSE(cursor_manager_.IsCursorVisible()); | |
244 EXPECT_FALSE(cursor_manager_.IsMouseEventsEnabled()); | |
245 | |
246 // When mouse events are enabled, it restores the visibility state. | |
247 cursor_manager_.EnableMouseEvents(); | |
248 EXPECT_TRUE(cursor_manager_.IsCursorVisible()); | |
249 EXPECT_TRUE(cursor_manager_.IsMouseEventsEnabled()); | |
250 | |
251 cursor_manager_.ShowCursor(); | |
252 cursor_manager_.DisableMouseEvents(); | |
253 EXPECT_FALSE(cursor_manager_.IsCursorVisible()); | |
254 EXPECT_FALSE(cursor_manager_.IsMouseEventsEnabled()); | |
255 cursor_manager_.EnableMouseEvents(); | |
256 EXPECT_TRUE(cursor_manager_.IsCursorVisible()); | |
257 EXPECT_TRUE(cursor_manager_.IsMouseEventsEnabled()); | |
258 | |
259 cursor_manager_.HideCursor(); | |
260 cursor_manager_.DisableMouseEvents(); | |
261 EXPECT_FALSE(cursor_manager_.IsCursorVisible()); | |
262 EXPECT_FALSE(cursor_manager_.IsMouseEventsEnabled()); | |
263 cursor_manager_.EnableMouseEvents(); | |
264 EXPECT_FALSE(cursor_manager_.IsCursorVisible()); | |
265 EXPECT_TRUE(cursor_manager_.IsMouseEventsEnabled()); | |
266 | |
267 // When mouse events are disabled, ShowCursor is ignored. | |
268 cursor_manager_.DisableMouseEvents(); | |
269 EXPECT_FALSE(cursor_manager_.IsCursorVisible()); | |
270 EXPECT_FALSE(cursor_manager_.IsMouseEventsEnabled()); | |
271 cursor_manager_.ShowCursor(); | |
272 EXPECT_FALSE(cursor_manager_.IsCursorVisible()); | |
273 EXPECT_FALSE(cursor_manager_.IsMouseEventsEnabled()); | |
274 cursor_manager_.DisableMouseEvents(); | |
275 EXPECT_FALSE(cursor_manager_.IsCursorVisible()); | |
276 EXPECT_FALSE(cursor_manager_.IsMouseEventsEnabled()); | |
277 } | |
278 | |
279 // Verifies that calling DisableMouseEvents multiple times in a row makes no | |
280 // difference compared with calling it once. | |
281 // This is a regression test for http://crbug.com/169404. | |
282 TEST_F(CursorManagerTest, MultipleDisableMouseEvents) { | |
283 cursor_manager_.DisableMouseEvents(); | |
284 cursor_manager_.DisableMouseEvents(); | |
285 cursor_manager_.EnableMouseEvents(); | |
286 cursor_manager_.LockCursor(); | |
287 cursor_manager_.UnlockCursor(); | |
288 EXPECT_TRUE(cursor_manager_.IsCursorVisible()); | |
289 } | |
290 | |
291 // Verifies that calling EnableMouseEvents multiple times in a row makes no | |
292 // difference compared with calling it once. | |
293 TEST_F(CursorManagerTest, MultipleEnableMouseEvents) { | |
294 cursor_manager_.DisableMouseEvents(); | |
295 cursor_manager_.EnableMouseEvents(); | |
296 cursor_manager_.EnableMouseEvents(); | |
297 cursor_manager_.LockCursor(); | |
298 cursor_manager_.UnlockCursor(); | |
299 EXPECT_TRUE(cursor_manager_.IsCursorVisible()); | |
300 } | |
301 | |
302 TEST_F(CursorManagerTest, TestCursorClientObserver) { | |
303 // Add two observers. Both should have OnCursorVisibilityChanged() | |
304 // invoked when the visibility of the cursor changes. | |
305 TestingCursorClientObserver observer_a; | |
306 TestingCursorClientObserver observer_b; | |
307 cursor_manager_.AddObserver(&observer_a); | |
308 cursor_manager_.AddObserver(&observer_b); | |
309 | |
310 // Initial state before any events have been sent. | |
311 observer_a.reset(); | |
312 observer_b.reset(); | |
313 EXPECT_FALSE(observer_a.did_visibility_change()); | |
314 EXPECT_FALSE(observer_b.did_visibility_change()); | |
315 EXPECT_FALSE(observer_a.is_cursor_visible()); | |
316 EXPECT_FALSE(observer_b.is_cursor_visible()); | |
317 | |
318 // Hide the cursor using HideCursor(). | |
319 cursor_manager_.HideCursor(); | |
320 EXPECT_TRUE(observer_a.did_visibility_change()); | |
321 EXPECT_TRUE(observer_b.did_visibility_change()); | |
322 EXPECT_FALSE(observer_a.is_cursor_visible()); | |
323 EXPECT_FALSE(observer_b.is_cursor_visible()); | |
324 | |
325 // Show the cursor using ShowCursor(). | |
326 observer_a.reset(); | |
327 observer_b.reset(); | |
328 cursor_manager_.ShowCursor(); | |
329 EXPECT_TRUE(observer_a.did_visibility_change()); | |
330 EXPECT_TRUE(observer_b.did_visibility_change()); | |
331 EXPECT_TRUE(observer_a.is_cursor_visible()); | |
332 EXPECT_TRUE(observer_b.is_cursor_visible()); | |
333 | |
334 // Remove observer_b. Its OnCursorVisibilityChanged() should | |
335 // not be invoked past this point. | |
336 cursor_manager_.RemoveObserver(&observer_b); | |
337 | |
338 // Hide the cursor using HideCursor(). | |
339 observer_a.reset(); | |
340 observer_b.reset(); | |
341 cursor_manager_.HideCursor(); | |
342 EXPECT_TRUE(observer_a.did_visibility_change()); | |
343 EXPECT_FALSE(observer_b.did_visibility_change()); | |
344 EXPECT_FALSE(observer_a.is_cursor_visible()); | |
345 | |
346 // Show the cursor using ShowCursor(). | |
347 observer_a.reset(); | |
348 observer_b.reset(); | |
349 cursor_manager_.ShowCursor(); | |
350 EXPECT_TRUE(observer_a.did_visibility_change()); | |
351 EXPECT_FALSE(observer_b.did_visibility_change()); | |
352 EXPECT_TRUE(observer_a.is_cursor_visible()); | |
353 } | |
OLD | NEW |