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.
5 #include "ui/wm/core/cursor_manager.h"
7 #include "ui/aura/client/cursor_client_observer.h"
8 #include "ui/aura/test/aura_test_base.h"
9 #include "ui/wm/core/native_cursor_manager.h"
13 class TestingCursorManager : public wm::NativeCursorManager {
15 // Overridden from wm::NativeCursorManager:
16 virtual void SetDisplay(
17 const gfx::Display& display,
18 wm::NativeCursorManagerDelegate* delegate) OVERRIDE {}
20 virtual void SetCursor(
21 gfx::NativeCursor cursor,
22 wm::NativeCursorManagerDelegate* delegate) OVERRIDE {
23 delegate->CommitCursor(cursor);
26 virtual void SetVisibility(
28 wm::NativeCursorManagerDelegate* delegate) OVERRIDE {
29 delegate->CommitVisibility(visible);
32 virtual void SetMouseEventsEnabled(
34 wm::NativeCursorManagerDelegate* delegate) OVERRIDE {
35 delegate->CommitMouseEventsEnabled(enabled);
38 virtual void SetCursorSet(
39 ui::CursorSetType cursor_set,
40 wm::NativeCursorManagerDelegate* delegate) OVERRIDE {
41 delegate->CommitCursorSet(cursor_set);
47 class CursorManagerTest : public aura::test::AuraTestBase {
50 : delegate_(new TestingCursorManager),
51 cursor_manager_(scoped_ptr<wm::NativeCursorManager>(
55 TestingCursorManager* delegate_;
56 wm::CursorManager cursor_manager_;
59 class TestingCursorClientObserver : public aura::client::CursorClientObserver {
61 TestingCursorClientObserver()
62 : cursor_visibility_(false),
63 did_visibility_change_(false) {}
64 void reset() { cursor_visibility_ = did_visibility_change_ = false; }
65 bool is_cursor_visible() const { return cursor_visibility_; }
66 bool did_visibility_change() const { return did_visibility_change_; }
68 // Overridden from aura::client::CursorClientObserver:
69 virtual void OnCursorVisibilityChanged(bool is_visible) OVERRIDE {
70 cursor_visibility_ = is_visible;
71 did_visibility_change_ = true;
75 bool cursor_visibility_;
76 bool did_visibility_change_;
78 DISALLOW_COPY_AND_ASSIGN(TestingCursorClientObserver);
81 TEST_F(CursorManagerTest, ShowHideCursor) {
82 cursor_manager_.SetCursor(ui::kCursorCopy);
83 EXPECT_EQ(ui::kCursorCopy, cursor_manager_.GetCursor().native_type());
85 cursor_manager_.ShowCursor();
86 EXPECT_TRUE(cursor_manager_.IsCursorVisible());
87 cursor_manager_.HideCursor();
88 EXPECT_FALSE(cursor_manager_.IsCursorVisible());
89 // The current cursor does not change even when the cursor is not shown.
90 EXPECT_EQ(ui::kCursorCopy, cursor_manager_.GetCursor().native_type());
92 // Check if cursor visibility is locked.
93 cursor_manager_.LockCursor();
94 EXPECT_FALSE(cursor_manager_.IsCursorVisible());
95 cursor_manager_.ShowCursor();
96 EXPECT_FALSE(cursor_manager_.IsCursorVisible());
97 cursor_manager_.UnlockCursor();
98 EXPECT_TRUE(cursor_manager_.IsCursorVisible());
100 cursor_manager_.LockCursor();
101 EXPECT_TRUE(cursor_manager_.IsCursorVisible());
102 cursor_manager_.HideCursor();
103 EXPECT_TRUE(cursor_manager_.IsCursorVisible());
104 cursor_manager_.UnlockCursor();
105 EXPECT_FALSE(cursor_manager_.IsCursorVisible());
107 // Checks setting visiblity while cursor is locked does not affect the
108 // subsequent uses of UnlockCursor.
109 cursor_manager_.LockCursor();
110 cursor_manager_.HideCursor();
111 cursor_manager_.UnlockCursor();
112 EXPECT_FALSE(cursor_manager_.IsCursorVisible());
114 cursor_manager_.ShowCursor();
115 cursor_manager_.LockCursor();
116 cursor_manager_.UnlockCursor();
117 EXPECT_TRUE(cursor_manager_.IsCursorVisible());
119 cursor_manager_.LockCursor();
120 cursor_manager_.ShowCursor();
121 cursor_manager_.UnlockCursor();
122 EXPECT_TRUE(cursor_manager_.IsCursorVisible());
124 cursor_manager_.HideCursor();
125 cursor_manager_.LockCursor();
126 cursor_manager_.UnlockCursor();
127 EXPECT_FALSE(cursor_manager_.IsCursorVisible());
130 // Verifies that LockCursor/UnlockCursor work correctly with
131 // EnableMouseEvents and DisableMouseEvents
132 TEST_F(CursorManagerTest, EnableDisableMouseEvents) {
133 cursor_manager_.SetCursor(ui::kCursorCopy);
134 EXPECT_EQ(ui::kCursorCopy, cursor_manager_.GetCursor().native_type());
136 cursor_manager_.EnableMouseEvents();
137 EXPECT_TRUE(cursor_manager_.IsMouseEventsEnabled());
138 cursor_manager_.DisableMouseEvents();
139 EXPECT_FALSE(cursor_manager_.IsMouseEventsEnabled());
140 // The current cursor does not change even when the cursor is not shown.
141 EXPECT_EQ(ui::kCursorCopy, cursor_manager_.GetCursor().native_type());
143 // Check if cursor enable state is locked.
144 cursor_manager_.LockCursor();
145 EXPECT_FALSE(cursor_manager_.IsMouseEventsEnabled());
146 cursor_manager_.EnableMouseEvents();
147 EXPECT_FALSE(cursor_manager_.IsMouseEventsEnabled());
148 cursor_manager_.UnlockCursor();
149 EXPECT_TRUE(cursor_manager_.IsMouseEventsEnabled());
151 cursor_manager_.LockCursor();
152 EXPECT_TRUE(cursor_manager_.IsMouseEventsEnabled());
153 cursor_manager_.DisableMouseEvents();
154 EXPECT_TRUE(cursor_manager_.IsMouseEventsEnabled());
155 cursor_manager_.UnlockCursor();
156 EXPECT_FALSE(cursor_manager_.IsMouseEventsEnabled());
158 // Checks enabling cursor while cursor is locked does not affect the
159 // subsequent uses of UnlockCursor.
160 cursor_manager_.LockCursor();
161 cursor_manager_.DisableMouseEvents();
162 cursor_manager_.UnlockCursor();
163 EXPECT_FALSE(cursor_manager_.IsMouseEventsEnabled());
165 cursor_manager_.EnableMouseEvents();
166 cursor_manager_.LockCursor();
167 cursor_manager_.UnlockCursor();
168 EXPECT_TRUE(cursor_manager_.IsMouseEventsEnabled());
170 cursor_manager_.LockCursor();
171 cursor_manager_.EnableMouseEvents();
172 cursor_manager_.UnlockCursor();
173 EXPECT_TRUE(cursor_manager_.IsMouseEventsEnabled());
175 cursor_manager_.DisableMouseEvents();
176 cursor_manager_.LockCursor();
177 cursor_manager_.UnlockCursor();
178 EXPECT_FALSE(cursor_manager_.IsMouseEventsEnabled());
181 TEST_F(CursorManagerTest, SetCursorSet) {
182 EXPECT_EQ(ui::CURSOR_SET_NORMAL, cursor_manager_.GetCursorSet());
184 cursor_manager_.SetCursorSet(ui::CURSOR_SET_NORMAL);
185 EXPECT_EQ(ui::CURSOR_SET_NORMAL, cursor_manager_.GetCursorSet());
187 cursor_manager_.SetCursorSet(ui::CURSOR_SET_LARGE);
188 EXPECT_EQ(ui::CURSOR_SET_LARGE, cursor_manager_.GetCursorSet());
190 cursor_manager_.SetCursorSet(ui::CURSOR_SET_NORMAL);
191 EXPECT_EQ(ui::CURSOR_SET_NORMAL, cursor_manager_.GetCursorSet());
194 TEST_F(CursorManagerTest, IsMouseEventsEnabled) {
195 cursor_manager_.EnableMouseEvents();
196 EXPECT_TRUE(cursor_manager_.IsMouseEventsEnabled());
197 cursor_manager_.DisableMouseEvents();
198 EXPECT_FALSE(cursor_manager_.IsMouseEventsEnabled());
201 // Verifies that the mouse events enable state changes correctly when
202 // ShowCursor/HideCursor and EnableMouseEvents/DisableMouseEvents are used
204 TEST_F(CursorManagerTest, ShowAndEnable) {
205 // Changing the visibility of the cursor does not affect the enable state.
206 cursor_manager_.EnableMouseEvents();
207 cursor_manager_.ShowCursor();
208 EXPECT_TRUE(cursor_manager_.IsCursorVisible());
209 EXPECT_TRUE(cursor_manager_.IsMouseEventsEnabled());
210 cursor_manager_.HideCursor();
211 EXPECT_FALSE(cursor_manager_.IsCursorVisible());
212 EXPECT_TRUE(cursor_manager_.IsMouseEventsEnabled());
213 cursor_manager_.ShowCursor();
214 EXPECT_TRUE(cursor_manager_.IsCursorVisible());
215 EXPECT_TRUE(cursor_manager_.IsMouseEventsEnabled());
217 // When mouse events are disabled, it also gets invisible.
218 EXPECT_TRUE(cursor_manager_.IsCursorVisible());
219 cursor_manager_.DisableMouseEvents();
220 EXPECT_FALSE(cursor_manager_.IsCursorVisible());
221 EXPECT_FALSE(cursor_manager_.IsMouseEventsEnabled());
223 // When mouse events are enabled, it restores the visibility state.
224 cursor_manager_.EnableMouseEvents();
225 EXPECT_TRUE(cursor_manager_.IsCursorVisible());
226 EXPECT_TRUE(cursor_manager_.IsMouseEventsEnabled());
228 cursor_manager_.ShowCursor();
229 cursor_manager_.DisableMouseEvents();
230 EXPECT_FALSE(cursor_manager_.IsCursorVisible());
231 EXPECT_FALSE(cursor_manager_.IsMouseEventsEnabled());
232 cursor_manager_.EnableMouseEvents();
233 EXPECT_TRUE(cursor_manager_.IsCursorVisible());
234 EXPECT_TRUE(cursor_manager_.IsMouseEventsEnabled());
236 cursor_manager_.HideCursor();
237 cursor_manager_.DisableMouseEvents();
238 EXPECT_FALSE(cursor_manager_.IsCursorVisible());
239 EXPECT_FALSE(cursor_manager_.IsMouseEventsEnabled());
240 cursor_manager_.EnableMouseEvents();
241 EXPECT_FALSE(cursor_manager_.IsCursorVisible());
242 EXPECT_TRUE(cursor_manager_.IsMouseEventsEnabled());
244 // When mouse events are disabled, ShowCursor is ignored.
245 cursor_manager_.DisableMouseEvents();
246 EXPECT_FALSE(cursor_manager_.IsCursorVisible());
247 EXPECT_FALSE(cursor_manager_.IsMouseEventsEnabled());
248 cursor_manager_.ShowCursor();
249 EXPECT_FALSE(cursor_manager_.IsCursorVisible());
250 EXPECT_FALSE(cursor_manager_.IsMouseEventsEnabled());
251 cursor_manager_.DisableMouseEvents();
252 EXPECT_FALSE(cursor_manager_.IsCursorVisible());
253 EXPECT_FALSE(cursor_manager_.IsMouseEventsEnabled());
256 // Verifies that calling DisableMouseEvents multiple times in a row makes no
257 // difference compared with calling it once.
258 // This is a regression test for http://crbug.com/169404.
259 TEST_F(CursorManagerTest, MultipleDisableMouseEvents) {
260 cursor_manager_.DisableMouseEvents();
261 cursor_manager_.DisableMouseEvents();
262 cursor_manager_.EnableMouseEvents();
263 cursor_manager_.LockCursor();
264 cursor_manager_.UnlockCursor();
265 EXPECT_TRUE(cursor_manager_.IsCursorVisible());
268 // Verifies that calling EnableMouseEvents multiple times in a row makes no
269 // difference compared with calling it once.
270 TEST_F(CursorManagerTest, MultipleEnableMouseEvents) {
271 cursor_manager_.DisableMouseEvents();
272 cursor_manager_.EnableMouseEvents();
273 cursor_manager_.EnableMouseEvents();
274 cursor_manager_.LockCursor();
275 cursor_manager_.UnlockCursor();
276 EXPECT_TRUE(cursor_manager_.IsCursorVisible());
279 TEST_F(CursorManagerTest, TestCursorClientObserver) {
280 // Add two observers. Both should have OnCursorVisibilityChanged()
281 // invoked when the visibility of the cursor changes.
282 TestingCursorClientObserver observer_a;
283 TestingCursorClientObserver observer_b;
284 cursor_manager_.AddObserver(&observer_a);
285 cursor_manager_.AddObserver(&observer_b);
287 // Initial state before any events have been sent.
290 EXPECT_FALSE(observer_a.did_visibility_change());
291 EXPECT_FALSE(observer_b.did_visibility_change());
292 EXPECT_FALSE(observer_a.is_cursor_visible());
293 EXPECT_FALSE(observer_b.is_cursor_visible());
295 // Hide the cursor using HideCursor().
296 cursor_manager_.HideCursor();
297 EXPECT_TRUE(observer_a.did_visibility_change());
298 EXPECT_TRUE(observer_b.did_visibility_change());
299 EXPECT_FALSE(observer_a.is_cursor_visible());
300 EXPECT_FALSE(observer_b.is_cursor_visible());
302 // Show the cursor using ShowCursor().
305 cursor_manager_.ShowCursor();
306 EXPECT_TRUE(observer_a.did_visibility_change());
307 EXPECT_TRUE(observer_b.did_visibility_change());
308 EXPECT_TRUE(observer_a.is_cursor_visible());
309 EXPECT_TRUE(observer_b.is_cursor_visible());
311 // Remove observer_b. Its OnCursorVisibilityChanged() should
312 // not be invoked past this point.
313 cursor_manager_.RemoveObserver(&observer_b);
315 // Hide the cursor using HideCursor().
318 cursor_manager_.HideCursor();
319 EXPECT_TRUE(observer_a.did_visibility_change());
320 EXPECT_FALSE(observer_b.did_visibility_change());
321 EXPECT_FALSE(observer_a.is_cursor_visible());
323 // Show the cursor using ShowCursor().
326 cursor_manager_.ShowCursor();
327 EXPECT_TRUE(observer_a.did_visibility_change());
328 EXPECT_FALSE(observer_b.did_visibility_change());
329 EXPECT_TRUE(observer_a.is_cursor_visible());