Upstream version 7.36.149.0
[platform/framework/web/crosswalk.git] / src / ui / wm / core / cursor_manager_unittest.cc
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/wm/core/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/wm/core/native_cursor_manager.h"
10
11 namespace {
12
13 class TestingCursorManager : public wm::NativeCursorManager {
14  public:
15   // Overridden from wm::NativeCursorManager:
16   virtual void SetDisplay(
17       const gfx::Display& display,
18       wm::NativeCursorManagerDelegate* delegate) OVERRIDE {}
19
20   virtual void SetCursor(
21       gfx::NativeCursor cursor,
22       wm::NativeCursorManagerDelegate* delegate) OVERRIDE {
23     delegate->CommitCursor(cursor);
24   }
25
26   virtual void SetVisibility(
27       bool visible,
28       wm::NativeCursorManagerDelegate* delegate) OVERRIDE {
29     delegate->CommitVisibility(visible);
30   }
31
32   virtual void SetMouseEventsEnabled(
33       bool enabled,
34       wm::NativeCursorManagerDelegate* delegate) OVERRIDE {
35     delegate->CommitMouseEventsEnabled(enabled);
36   }
37
38   virtual void SetCursorSet(
39       ui::CursorSetType cursor_set,
40       wm::NativeCursorManagerDelegate* delegate) OVERRIDE {
41     delegate->CommitCursorSet(cursor_set);
42   }
43 };
44
45 }  // namespace
46
47 class CursorManagerTest : public aura::test::AuraTestBase {
48  protected:
49   CursorManagerTest()
50       : delegate_(new TestingCursorManager),
51         cursor_manager_(scoped_ptr<wm::NativeCursorManager>(
52             delegate_)) {
53   }
54
55   TestingCursorManager* delegate_;
56   wm::CursorManager cursor_manager_;
57 };
58
59 class TestingCursorClientObserver : public aura::client::CursorClientObserver {
60  public:
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_; }
67
68   // Overridden from aura::client::CursorClientObserver:
69   virtual void OnCursorVisibilityChanged(bool is_visible) OVERRIDE {
70     cursor_visibility_ = is_visible;
71     did_visibility_change_ = true;
72   }
73
74  private:
75   bool cursor_visibility_;
76   bool did_visibility_change_;
77
78   DISALLOW_COPY_AND_ASSIGN(TestingCursorClientObserver);
79 };
80
81 TEST_F(CursorManagerTest, ShowHideCursor) {
82   cursor_manager_.SetCursor(ui::kCursorCopy);
83   EXPECT_EQ(ui::kCursorCopy, cursor_manager_.GetCursor().native_type());
84
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());
91
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());
99
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());
106
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());
113
114   cursor_manager_.ShowCursor();
115   cursor_manager_.LockCursor();
116   cursor_manager_.UnlockCursor();
117   EXPECT_TRUE(cursor_manager_.IsCursorVisible());
118
119   cursor_manager_.LockCursor();
120   cursor_manager_.ShowCursor();
121   cursor_manager_.UnlockCursor();
122   EXPECT_TRUE(cursor_manager_.IsCursorVisible());
123
124   cursor_manager_.HideCursor();
125   cursor_manager_.LockCursor();
126   cursor_manager_.UnlockCursor();
127   EXPECT_FALSE(cursor_manager_.IsCursorVisible());
128 }
129
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());
135
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());
142
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());
150
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());
157
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());
164
165   cursor_manager_.EnableMouseEvents();
166   cursor_manager_.LockCursor();
167   cursor_manager_.UnlockCursor();
168   EXPECT_TRUE(cursor_manager_.IsMouseEventsEnabled());
169
170   cursor_manager_.LockCursor();
171   cursor_manager_.EnableMouseEvents();
172   cursor_manager_.UnlockCursor();
173   EXPECT_TRUE(cursor_manager_.IsMouseEventsEnabled());
174
175   cursor_manager_.DisableMouseEvents();
176   cursor_manager_.LockCursor();
177   cursor_manager_.UnlockCursor();
178   EXPECT_FALSE(cursor_manager_.IsMouseEventsEnabled());
179 }
180
181 TEST_F(CursorManagerTest, SetCursorSet) {
182   EXPECT_EQ(ui::CURSOR_SET_NORMAL, cursor_manager_.GetCursorSet());
183
184   cursor_manager_.SetCursorSet(ui::CURSOR_SET_NORMAL);
185   EXPECT_EQ(ui::CURSOR_SET_NORMAL, cursor_manager_.GetCursorSet());
186
187   cursor_manager_.SetCursorSet(ui::CURSOR_SET_LARGE);
188   EXPECT_EQ(ui::CURSOR_SET_LARGE, cursor_manager_.GetCursorSet());
189
190   cursor_manager_.SetCursorSet(ui::CURSOR_SET_NORMAL);
191   EXPECT_EQ(ui::CURSOR_SET_NORMAL, cursor_manager_.GetCursorSet());
192 }
193
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());
199 }
200
201 // Verifies that the mouse events enable state changes correctly when
202 // ShowCursor/HideCursor and EnableMouseEvents/DisableMouseEvents are used
203 // together.
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());
216
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());
222
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());
227
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());
235
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());
243
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());
254 }
255
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());
266 }
267
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());
277 }
278
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);
286
287   // Initial state before any events have been sent.
288   observer_a.reset();
289   observer_b.reset();
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());
294
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());
301
302   // Show the cursor using ShowCursor().
303   observer_a.reset();
304   observer_b.reset();
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());
310
311   // Remove observer_b. Its OnCursorVisibilityChanged() should
312   // not be invoked past this point.
313   cursor_manager_.RemoveObserver(&observer_b);
314
315   // Hide the cursor using HideCursor().
316   observer_a.reset();
317   observer_b.reset();
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());
322
323   // Show the cursor using ShowCursor().
324   observer_a.reset();
325   observer_b.reset();
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());
330 }