[Android] Implement 3-way sensor fallback for Device Orientation.
[chromium-blink-merge.git] / ui / wm / core / cursor_manager_unittest.cc
blob24de948f3d594c62c260af3045161dca865e8fbe
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"
11 namespace {
13 class TestingCursorManager : public wm::NativeCursorManager {
14 public:
15 // Overridden from wm::NativeCursorManager:
16 void SetDisplay(const gfx::Display& display,
17 wm::NativeCursorManagerDelegate* delegate) override {}
19 void SetCursor(gfx::NativeCursor cursor,
20 wm::NativeCursorManagerDelegate* delegate) override {
21 delegate->CommitCursor(cursor);
24 void SetVisibility(bool visible,
25 wm::NativeCursorManagerDelegate* delegate) override {
26 delegate->CommitVisibility(visible);
29 void SetMouseEventsEnabled(
30 bool enabled,
31 wm::NativeCursorManagerDelegate* delegate) override {
32 delegate->CommitMouseEventsEnabled(enabled);
35 void SetCursorSet(ui::CursorSetType cursor_set,
36 wm::NativeCursorManagerDelegate* delegate) override {
37 delegate->CommitCursorSet(cursor_set);
41 } // namespace
43 class CursorManagerTest : public aura::test::AuraTestBase {
44 protected:
45 CursorManagerTest()
46 : delegate_(new TestingCursorManager),
47 cursor_manager_(scoped_ptr<wm::NativeCursorManager>(
48 delegate_)) {
51 TestingCursorManager* delegate_;
52 wm::CursorManager cursor_manager_;
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_; }
64 // Overridden from aura::client::CursorClientObserver:
65 void OnCursorVisibilityChanged(bool is_visible) override {
66 cursor_visibility_ = is_visible;
67 did_visibility_change_ = true;
70 private:
71 bool cursor_visibility_;
72 bool did_visibility_change_;
74 DISALLOW_COPY_AND_ASSIGN(TestingCursorClientObserver);
77 TEST_F(CursorManagerTest, ShowHideCursor) {
78 cursor_manager_.SetCursor(ui::kCursorCopy);
79 EXPECT_EQ(ui::kCursorCopy, cursor_manager_.GetCursor().native_type());
81 cursor_manager_.ShowCursor();
82 EXPECT_TRUE(cursor_manager_.IsCursorVisible());
83 cursor_manager_.HideCursor();
84 EXPECT_FALSE(cursor_manager_.IsCursorVisible());
85 // The current cursor does not change even when the cursor is not shown.
86 EXPECT_EQ(ui::kCursorCopy, cursor_manager_.GetCursor().native_type());
88 // Check if cursor visibility is locked.
89 cursor_manager_.LockCursor();
90 EXPECT_FALSE(cursor_manager_.IsCursorVisible());
91 cursor_manager_.ShowCursor();
92 EXPECT_FALSE(cursor_manager_.IsCursorVisible());
93 cursor_manager_.UnlockCursor();
94 EXPECT_TRUE(cursor_manager_.IsCursorVisible());
96 cursor_manager_.LockCursor();
97 EXPECT_TRUE(cursor_manager_.IsCursorVisible());
98 cursor_manager_.HideCursor();
99 EXPECT_TRUE(cursor_manager_.IsCursorVisible());
100 cursor_manager_.UnlockCursor();
101 EXPECT_FALSE(cursor_manager_.IsCursorVisible());
103 // Checks setting visiblity while cursor is locked does not affect the
104 // subsequent uses of UnlockCursor.
105 cursor_manager_.LockCursor();
106 cursor_manager_.HideCursor();
107 cursor_manager_.UnlockCursor();
108 EXPECT_FALSE(cursor_manager_.IsCursorVisible());
110 cursor_manager_.ShowCursor();
111 cursor_manager_.LockCursor();
112 cursor_manager_.UnlockCursor();
113 EXPECT_TRUE(cursor_manager_.IsCursorVisible());
115 cursor_manager_.LockCursor();
116 cursor_manager_.ShowCursor();
117 cursor_manager_.UnlockCursor();
118 EXPECT_TRUE(cursor_manager_.IsCursorVisible());
120 cursor_manager_.HideCursor();
121 cursor_manager_.LockCursor();
122 cursor_manager_.UnlockCursor();
123 EXPECT_FALSE(cursor_manager_.IsCursorVisible());
126 // Verifies that LockCursor/UnlockCursor work correctly with
127 // EnableMouseEvents and DisableMouseEvents
128 TEST_F(CursorManagerTest, EnableDisableMouseEvents) {
129 cursor_manager_.SetCursor(ui::kCursorCopy);
130 EXPECT_EQ(ui::kCursorCopy, cursor_manager_.GetCursor().native_type());
132 cursor_manager_.EnableMouseEvents();
133 EXPECT_TRUE(cursor_manager_.IsMouseEventsEnabled());
134 cursor_manager_.DisableMouseEvents();
135 EXPECT_FALSE(cursor_manager_.IsMouseEventsEnabled());
136 // The current cursor does not change even when the cursor is not shown.
137 EXPECT_EQ(ui::kCursorCopy, cursor_manager_.GetCursor().native_type());
139 // Check if cursor enable state is locked.
140 cursor_manager_.LockCursor();
141 EXPECT_FALSE(cursor_manager_.IsMouseEventsEnabled());
142 cursor_manager_.EnableMouseEvents();
143 EXPECT_FALSE(cursor_manager_.IsMouseEventsEnabled());
144 cursor_manager_.UnlockCursor();
145 EXPECT_TRUE(cursor_manager_.IsMouseEventsEnabled());
147 cursor_manager_.LockCursor();
148 EXPECT_TRUE(cursor_manager_.IsMouseEventsEnabled());
149 cursor_manager_.DisableMouseEvents();
150 EXPECT_TRUE(cursor_manager_.IsMouseEventsEnabled());
151 cursor_manager_.UnlockCursor();
152 EXPECT_FALSE(cursor_manager_.IsMouseEventsEnabled());
154 // Checks enabling cursor while cursor is locked does not affect the
155 // subsequent uses of UnlockCursor.
156 cursor_manager_.LockCursor();
157 cursor_manager_.DisableMouseEvents();
158 cursor_manager_.UnlockCursor();
159 EXPECT_FALSE(cursor_manager_.IsMouseEventsEnabled());
161 cursor_manager_.EnableMouseEvents();
162 cursor_manager_.LockCursor();
163 cursor_manager_.UnlockCursor();
164 EXPECT_TRUE(cursor_manager_.IsMouseEventsEnabled());
166 cursor_manager_.LockCursor();
167 cursor_manager_.EnableMouseEvents();
168 cursor_manager_.UnlockCursor();
169 EXPECT_TRUE(cursor_manager_.IsMouseEventsEnabled());
171 cursor_manager_.DisableMouseEvents();
172 cursor_manager_.LockCursor();
173 cursor_manager_.UnlockCursor();
174 EXPECT_FALSE(cursor_manager_.IsMouseEventsEnabled());
177 TEST_F(CursorManagerTest, SetCursorSet) {
178 EXPECT_EQ(ui::CURSOR_SET_NORMAL, cursor_manager_.GetCursorSet());
180 cursor_manager_.SetCursorSet(ui::CURSOR_SET_NORMAL);
181 EXPECT_EQ(ui::CURSOR_SET_NORMAL, cursor_manager_.GetCursorSet());
183 cursor_manager_.SetCursorSet(ui::CURSOR_SET_LARGE);
184 EXPECT_EQ(ui::CURSOR_SET_LARGE, cursor_manager_.GetCursorSet());
186 cursor_manager_.SetCursorSet(ui::CURSOR_SET_NORMAL);
187 EXPECT_EQ(ui::CURSOR_SET_NORMAL, cursor_manager_.GetCursorSet());
190 TEST_F(CursorManagerTest, IsMouseEventsEnabled) {
191 cursor_manager_.EnableMouseEvents();
192 EXPECT_TRUE(cursor_manager_.IsMouseEventsEnabled());
193 cursor_manager_.DisableMouseEvents();
194 EXPECT_FALSE(cursor_manager_.IsMouseEventsEnabled());
197 // Verifies that the mouse events enable state changes correctly when
198 // ShowCursor/HideCursor and EnableMouseEvents/DisableMouseEvents are used
199 // together.
200 TEST_F(CursorManagerTest, ShowAndEnable) {
201 // Changing the visibility of the cursor does not affect the enable state.
202 cursor_manager_.EnableMouseEvents();
203 cursor_manager_.ShowCursor();
204 EXPECT_TRUE(cursor_manager_.IsCursorVisible());
205 EXPECT_TRUE(cursor_manager_.IsMouseEventsEnabled());
206 cursor_manager_.HideCursor();
207 EXPECT_FALSE(cursor_manager_.IsCursorVisible());
208 EXPECT_TRUE(cursor_manager_.IsMouseEventsEnabled());
209 cursor_manager_.ShowCursor();
210 EXPECT_TRUE(cursor_manager_.IsCursorVisible());
211 EXPECT_TRUE(cursor_manager_.IsMouseEventsEnabled());
213 // When mouse events are disabled, it also gets invisible.
214 EXPECT_TRUE(cursor_manager_.IsCursorVisible());
215 cursor_manager_.DisableMouseEvents();
216 EXPECT_FALSE(cursor_manager_.IsCursorVisible());
217 EXPECT_FALSE(cursor_manager_.IsMouseEventsEnabled());
219 // When mouse events are enabled, it restores the visibility state.
220 cursor_manager_.EnableMouseEvents();
221 EXPECT_TRUE(cursor_manager_.IsCursorVisible());
222 EXPECT_TRUE(cursor_manager_.IsMouseEventsEnabled());
224 cursor_manager_.ShowCursor();
225 cursor_manager_.DisableMouseEvents();
226 EXPECT_FALSE(cursor_manager_.IsCursorVisible());
227 EXPECT_FALSE(cursor_manager_.IsMouseEventsEnabled());
228 cursor_manager_.EnableMouseEvents();
229 EXPECT_TRUE(cursor_manager_.IsCursorVisible());
230 EXPECT_TRUE(cursor_manager_.IsMouseEventsEnabled());
232 cursor_manager_.HideCursor();
233 cursor_manager_.DisableMouseEvents();
234 EXPECT_FALSE(cursor_manager_.IsCursorVisible());
235 EXPECT_FALSE(cursor_manager_.IsMouseEventsEnabled());
236 cursor_manager_.EnableMouseEvents();
237 EXPECT_FALSE(cursor_manager_.IsCursorVisible());
238 EXPECT_TRUE(cursor_manager_.IsMouseEventsEnabled());
240 // When mouse events are disabled, ShowCursor is ignored.
241 cursor_manager_.DisableMouseEvents();
242 EXPECT_FALSE(cursor_manager_.IsCursorVisible());
243 EXPECT_FALSE(cursor_manager_.IsMouseEventsEnabled());
244 cursor_manager_.ShowCursor();
245 EXPECT_FALSE(cursor_manager_.IsCursorVisible());
246 EXPECT_FALSE(cursor_manager_.IsMouseEventsEnabled());
247 cursor_manager_.DisableMouseEvents();
248 EXPECT_FALSE(cursor_manager_.IsCursorVisible());
249 EXPECT_FALSE(cursor_manager_.IsMouseEventsEnabled());
252 // Verifies that calling DisableMouseEvents multiple times in a row makes no
253 // difference compared with calling it once.
254 // This is a regression test for http://crbug.com/169404.
255 TEST_F(CursorManagerTest, MultipleDisableMouseEvents) {
256 cursor_manager_.DisableMouseEvents();
257 cursor_manager_.DisableMouseEvents();
258 cursor_manager_.EnableMouseEvents();
259 cursor_manager_.LockCursor();
260 cursor_manager_.UnlockCursor();
261 EXPECT_TRUE(cursor_manager_.IsCursorVisible());
264 // Verifies that calling EnableMouseEvents multiple times in a row makes no
265 // difference compared with calling it once.
266 TEST_F(CursorManagerTest, MultipleEnableMouseEvents) {
267 cursor_manager_.DisableMouseEvents();
268 cursor_manager_.EnableMouseEvents();
269 cursor_manager_.EnableMouseEvents();
270 cursor_manager_.LockCursor();
271 cursor_manager_.UnlockCursor();
272 EXPECT_TRUE(cursor_manager_.IsCursorVisible());
275 TEST_F(CursorManagerTest, TestCursorClientObserver) {
276 // Add two observers. Both should have OnCursorVisibilityChanged()
277 // invoked when the visibility of the cursor changes.
278 TestingCursorClientObserver observer_a;
279 TestingCursorClientObserver observer_b;
280 cursor_manager_.AddObserver(&observer_a);
281 cursor_manager_.AddObserver(&observer_b);
283 // Initial state before any events have been sent.
284 observer_a.reset();
285 observer_b.reset();
286 EXPECT_FALSE(observer_a.did_visibility_change());
287 EXPECT_FALSE(observer_b.did_visibility_change());
288 EXPECT_FALSE(observer_a.is_cursor_visible());
289 EXPECT_FALSE(observer_b.is_cursor_visible());
291 // Hide the cursor using HideCursor().
292 cursor_manager_.HideCursor();
293 EXPECT_TRUE(observer_a.did_visibility_change());
294 EXPECT_TRUE(observer_b.did_visibility_change());
295 EXPECT_FALSE(observer_a.is_cursor_visible());
296 EXPECT_FALSE(observer_b.is_cursor_visible());
298 // Show the cursor using ShowCursor().
299 observer_a.reset();
300 observer_b.reset();
301 cursor_manager_.ShowCursor();
302 EXPECT_TRUE(observer_a.did_visibility_change());
303 EXPECT_TRUE(observer_b.did_visibility_change());
304 EXPECT_TRUE(observer_a.is_cursor_visible());
305 EXPECT_TRUE(observer_b.is_cursor_visible());
307 // Remove observer_b. Its OnCursorVisibilityChanged() should
308 // not be invoked past this point.
309 cursor_manager_.RemoveObserver(&observer_b);
311 // Hide the cursor using HideCursor().
312 observer_a.reset();
313 observer_b.reset();
314 cursor_manager_.HideCursor();
315 EXPECT_TRUE(observer_a.did_visibility_change());
316 EXPECT_FALSE(observer_b.did_visibility_change());
317 EXPECT_FALSE(observer_a.is_cursor_visible());
319 // Show the cursor using ShowCursor().
320 observer_a.reset();
321 observer_b.reset();
322 cursor_manager_.ShowCursor();
323 EXPECT_TRUE(observer_a.did_visibility_change());
324 EXPECT_FALSE(observer_b.did_visibility_change());
325 EXPECT_TRUE(observer_a.is_cursor_visible());