summaryrefslogtreecommitdiffstats
path: root/ash/wm/cursor_manager_unittest.cc
blob: a29051290d060d1efd2b4397a60b028e163e7166 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "ash/wm/cursor_manager.h"

#include "ash/shell.h"
#include "ash/test/ash_test_base.h"
#include "ash/test/cursor_manager_test_api.h"
#include "ash/wm/image_cursors.h"
#include "ui/aura/root_window.h"
#include "ui/aura/test/test_window_delegate.h"
#include "ui/aura/test/test_windows.h"
#include "ui/aura/window.h"

namespace ash {
namespace test {

namespace {

// A delegate for recording a mouse event location.
class MouseEventLocationDelegate : public aura::test::TestWindowDelegate {
 public:
  MouseEventLocationDelegate() {}
  virtual ~MouseEventLocationDelegate() {}

  const gfx::Point& mouse_event_location() const {
    return mouse_event_location_;
  }

  virtual void OnMouseEvent(ui::MouseEvent* event) OVERRIDE {
    mouse_event_location_ = event->location();
    event->SetHandled();
  }

 private:
  gfx::Point mouse_event_location_;

  DISALLOW_COPY_AND_ASSIGN(MouseEventLocationDelegate);
};

}  // namespace

typedef test::AshTestBase CursorManagerTest;

TEST_F(CursorManagerTest, LockCursor) {
  CursorManager* cursor_manager = Shell::GetInstance()->cursor_manager();
  CursorManagerTestApi test_api(cursor_manager);

  cursor_manager->SetCursor(ui::kCursorCopy);
  EXPECT_EQ(ui::kCursorCopy, test_api.GetCurrentCursor().native_type());
  cursor_manager->SetDeviceScaleFactor(2.0f);
  EXPECT_EQ(2.0f, test_api.GetDeviceScaleFactor());
  EXPECT_TRUE(test_api.GetCurrentCursor().platform());

  cursor_manager->LockCursor();
  EXPECT_TRUE(cursor_manager->is_cursor_locked());

  // Cursor type does not change while cursor is locked.
  cursor_manager->SetCursor(ui::kCursorPointer);
  EXPECT_EQ(ui::kCursorCopy, test_api.GetCurrentCursor().native_type());

  // Device scale factor does change even while cursor is locked.
  cursor_manager->SetDeviceScaleFactor(1.0f);
  EXPECT_EQ(1.0f, test_api.GetDeviceScaleFactor());

  cursor_manager->UnlockCursor();
  EXPECT_FALSE(cursor_manager->is_cursor_locked());

  // Cursor type changes to the one specified while cursor is locked.
  EXPECT_EQ(ui::kCursorPointer, test_api.GetCurrentCursor().native_type());
  EXPECT_EQ(1.0f, test_api.GetDeviceScaleFactor());
  EXPECT_TRUE(test_api.GetCurrentCursor().platform());
}

TEST_F(CursorManagerTest, SetCursor) {
  CursorManager* cursor_manager = Shell::GetInstance()->cursor_manager();
  CursorManagerTestApi test_api(cursor_manager);

  cursor_manager->SetCursor(ui::kCursorCopy);
  EXPECT_EQ(ui::kCursorCopy, test_api.GetCurrentCursor().native_type());
  EXPECT_TRUE(test_api.GetCurrentCursor().platform());
  cursor_manager->SetCursor(ui::kCursorPointer);
  EXPECT_EQ(ui::kCursorPointer, test_api.GetCurrentCursor().native_type());
  EXPECT_TRUE(test_api.GetCurrentCursor().platform());
}

TEST_F(CursorManagerTest, ShowHideCursor) {
  CursorManager* cursor_manager = Shell::GetInstance()->cursor_manager();
  CursorManagerTestApi test_api(cursor_manager);

  cursor_manager->SetCursor(ui::kCursorCopy);
  EXPECT_EQ(ui::kCursorCopy, test_api.GetCurrentCursor().native_type());

  cursor_manager->ShowCursor();
  EXPECT_TRUE(cursor_manager->IsCursorVisible());
  cursor_manager->HideCursor();
  EXPECT_FALSE(cursor_manager->IsCursorVisible());
  // The current cursor does not change even when the cursor is not shown.
  EXPECT_EQ(ui::kCursorCopy, test_api.GetCurrentCursor().native_type());

  // Check if cursor visibility is locked.
  cursor_manager->LockCursor();
  EXPECT_FALSE(cursor_manager->IsCursorVisible());
  cursor_manager->ShowCursor();
  EXPECT_FALSE(cursor_manager->IsCursorVisible());
  cursor_manager->UnlockCursor();
  EXPECT_TRUE(cursor_manager->IsCursorVisible());

  cursor_manager->LockCursor();
  EXPECT_TRUE(cursor_manager->IsCursorVisible());
  cursor_manager->HideCursor();
  EXPECT_TRUE(cursor_manager->IsCursorVisible());
  cursor_manager->UnlockCursor();
  EXPECT_FALSE(cursor_manager->IsCursorVisible());

  // Checks setting visiblity while cursor is locked does not affect the
  // subsequent uses of UnlockCursor.
  cursor_manager->LockCursor();
  cursor_manager->HideCursor();
  cursor_manager->UnlockCursor();
  EXPECT_FALSE(cursor_manager->IsCursorVisible());

  cursor_manager->ShowCursor();
  cursor_manager->LockCursor();
  cursor_manager->UnlockCursor();
  EXPECT_TRUE(cursor_manager->IsCursorVisible());

  cursor_manager->LockCursor();
  cursor_manager->ShowCursor();
  cursor_manager->UnlockCursor();
  EXPECT_TRUE(cursor_manager->IsCursorVisible());

  cursor_manager->HideCursor();
  cursor_manager->LockCursor();
  cursor_manager->UnlockCursor();
  EXPECT_FALSE(cursor_manager->IsCursorVisible());
}

TEST_F(CursorManagerTest, SetDeviceScaleFactor) {
  CursorManager* cursor_manager = Shell::GetInstance()->cursor_manager();
  CursorManagerTestApi test_api(cursor_manager);

  cursor_manager->SetDeviceScaleFactor(2.0f);
  EXPECT_EQ(2.0f, test_api.GetDeviceScaleFactor());
  cursor_manager->SetDeviceScaleFactor(1.0f);
  EXPECT_EQ(1.0f, test_api.GetDeviceScaleFactor());
}

// Verifies that LockCursor/UnlockCursor work correctly with
// EnableMouseEvents and DisableMouseEvents
TEST_F(CursorManagerTest, EnableDisableMouseEvents) {
  CursorManager* cursor_manager = Shell::GetInstance()->cursor_manager();
  CursorManagerTestApi test_api(cursor_manager);

  cursor_manager->SetCursor(ui::kCursorCopy);
  EXPECT_EQ(ui::kCursorCopy, test_api.GetCurrentCursor().native_type());

  cursor_manager->EnableMouseEvents();
  EXPECT_TRUE(cursor_manager->IsMouseEventsEnabled());
  cursor_manager->DisableMouseEvents();
  EXPECT_FALSE(cursor_manager->IsMouseEventsEnabled());
  // The current cursor does not change even when the cursor is not shown.
  EXPECT_EQ(ui::kCursorCopy, test_api.GetCurrentCursor().native_type());

  // Check if cursor enable state is locked.
  cursor_manager->LockCursor();
  EXPECT_FALSE(cursor_manager->IsMouseEventsEnabled());
  cursor_manager->EnableMouseEvents();
  EXPECT_FALSE(cursor_manager->IsMouseEventsEnabled());
  cursor_manager->UnlockCursor();
  EXPECT_TRUE(cursor_manager->IsMouseEventsEnabled());

  cursor_manager->LockCursor();
  EXPECT_TRUE(cursor_manager->IsMouseEventsEnabled());
  cursor_manager->DisableMouseEvents();
  EXPECT_TRUE(cursor_manager->IsMouseEventsEnabled());
  cursor_manager->UnlockCursor();
  EXPECT_FALSE(cursor_manager->IsMouseEventsEnabled());

  // Checks enabling cursor while cursor is locked does not affect the
  // subsequent uses of UnlockCursor.
  cursor_manager->LockCursor();
  cursor_manager->DisableMouseEvents();
  cursor_manager->UnlockCursor();
  EXPECT_FALSE(cursor_manager->IsMouseEventsEnabled());

  cursor_manager->EnableMouseEvents();
  cursor_manager->LockCursor();
  cursor_manager->UnlockCursor();
  EXPECT_TRUE(cursor_manager->IsMouseEventsEnabled());

  cursor_manager->LockCursor();
  cursor_manager->EnableMouseEvents();
  cursor_manager->UnlockCursor();
  EXPECT_TRUE(cursor_manager->IsMouseEventsEnabled());

  cursor_manager->DisableMouseEvents();
  cursor_manager->LockCursor();
  cursor_manager->UnlockCursor();
  EXPECT_FALSE(cursor_manager->IsMouseEventsEnabled());
}

TEST_F(CursorManagerTest, IsMouseEventsEnabled) {
  CursorManager* cursor_manager = Shell::GetInstance()->cursor_manager();
  cursor_manager->EnableMouseEvents();
  EXPECT_TRUE(cursor_manager->IsMouseEventsEnabled());
  cursor_manager->DisableMouseEvents();
  EXPECT_FALSE(cursor_manager->IsMouseEventsEnabled());
}

// Verifies that the mouse events enable state changes correctly when
// ShowCursor/HideCursor and EnableMouseEvents/DisableMouseEvents are used
// together.
TEST_F(CursorManagerTest, ShowAndEnable) {
  CursorManager* cursor_manager = Shell::GetInstance()->cursor_manager();

  // Changing the visibility of the cursor does not affect the enable state.
  cursor_manager->EnableMouseEvents();
  cursor_manager->ShowCursor();
  EXPECT_TRUE(cursor_manager->IsCursorVisible());
  EXPECT_TRUE(cursor_manager->IsMouseEventsEnabled());
  cursor_manager->HideCursor();
  EXPECT_FALSE(cursor_manager->IsCursorVisible());
  EXPECT_TRUE(cursor_manager->IsMouseEventsEnabled());
  cursor_manager->ShowCursor();
  EXPECT_TRUE(cursor_manager->IsCursorVisible());
  EXPECT_TRUE(cursor_manager->IsMouseEventsEnabled());

  // When mouse events are disabled, it also gets invisible.
  EXPECT_TRUE(cursor_manager->IsCursorVisible());
  cursor_manager->DisableMouseEvents();
  EXPECT_FALSE(cursor_manager->IsCursorVisible());
  EXPECT_FALSE(cursor_manager->IsMouseEventsEnabled());

  // When mouse events are enabled, it restores the visibility state.
  cursor_manager->EnableMouseEvents();
  EXPECT_TRUE(cursor_manager->IsCursorVisible());
  EXPECT_TRUE(cursor_manager->IsMouseEventsEnabled());

  cursor_manager->ShowCursor();
  cursor_manager->DisableMouseEvents();
  EXPECT_FALSE(cursor_manager->IsCursorVisible());
  EXPECT_FALSE(cursor_manager->IsMouseEventsEnabled());
  cursor_manager->EnableMouseEvents();
  EXPECT_TRUE(cursor_manager->IsCursorVisible());
  EXPECT_TRUE(cursor_manager->IsMouseEventsEnabled());

  cursor_manager->HideCursor();
  cursor_manager->DisableMouseEvents();
  EXPECT_FALSE(cursor_manager->IsCursorVisible());
  EXPECT_FALSE(cursor_manager->IsMouseEventsEnabled());
  cursor_manager->EnableMouseEvents();
  EXPECT_FALSE(cursor_manager->IsCursorVisible());
  EXPECT_TRUE(cursor_manager->IsMouseEventsEnabled());

  // When mouse events are disabled, ShowCursor is ignored.
  cursor_manager->DisableMouseEvents();
  EXPECT_FALSE(cursor_manager->IsCursorVisible());
  EXPECT_FALSE(cursor_manager->IsMouseEventsEnabled());
  cursor_manager->ShowCursor();
  EXPECT_FALSE(cursor_manager->IsCursorVisible());
  EXPECT_FALSE(cursor_manager->IsMouseEventsEnabled());
  cursor_manager->DisableMouseEvents();
  EXPECT_FALSE(cursor_manager->IsCursorVisible());
  EXPECT_FALSE(cursor_manager->IsMouseEventsEnabled());
}

// Verifies that calling DisableMouseEvents multiple times in a row makes no
// difference compared with calling it once.
// This is a regression test for http://crbug.com/169404.
TEST_F(CursorManagerTest, MultipleDisableMouseEvents) {
  CursorManager* cursor_manager = Shell::GetInstance()->cursor_manager();
  cursor_manager->DisableMouseEvents();
  cursor_manager->DisableMouseEvents();
  cursor_manager->EnableMouseEvents();
  cursor_manager->LockCursor();
  cursor_manager->UnlockCursor();
  EXPECT_TRUE(cursor_manager->IsCursorVisible());
}

// Verifies that calling EnableMouseEvents multiple times in a row makes no
// difference compared with calling it once.
TEST_F(CursorManagerTest, MultipleEnableMouseEvents) {
  CursorManager* cursor_manager = Shell::GetInstance()->cursor_manager();
  cursor_manager->DisableMouseEvents();
  cursor_manager->EnableMouseEvents();
  cursor_manager->EnableMouseEvents();
  cursor_manager->LockCursor();
  cursor_manager->UnlockCursor();
  EXPECT_TRUE(cursor_manager->IsCursorVisible());
}

#if defined(OS_WIN)
// Temporarily disabled for windows. See crbug.com/112222.
#define MAYBE_DisabledMouseEventsLocation DISABLED_DisabledMouseEventsLocation
#else
#define MAYBE_DisabledMouseEventsLocation DisabledMouseEventsLocation
#endif  // defined(OS_WIN)

// Verifies that RootWindow generates a mouse event located outside of a window
// when mouse events are disabled.
TEST_F(CursorManagerTest, MAYBE_DisabledMouseEventsLocation) {
  scoped_ptr<MouseEventLocationDelegate> delegate(
      new MouseEventLocationDelegate());
  const int kWindowWidth = 123;
  const int kWindowHeight = 45;
  gfx::Rect bounds(100, 200, kWindowWidth, kWindowHeight);
  scoped_ptr<aura::Window> window(aura::test::CreateTestWindowWithDelegate(
      delegate.get(), 1, bounds, Shell::GetInstance()->GetPrimaryRootWindow()));

  CursorManager* cursor_manager = Shell::GetInstance()->cursor_manager();
  cursor_manager->EnableMouseEvents();
  // Send a mouse event to window.
  gfx::Point point(101, 201);
  gfx::Point local_point;
  ui::MouseEvent event(ui::ET_MOUSE_MOVED, point, point, 0);
  aura::RootWindow* root_window = window->GetRootWindow();
  root_window->AsRootWindowHostDelegate()->OnHostMouseEvent(&event);

  // Location was in window.
  local_point = delegate->mouse_event_location();
  aura::Window::ConvertPointToTarget(window.get(), root_window, &local_point);
  EXPECT_TRUE(window->bounds().Contains(local_point));

  // Location is now out of window.
  cursor_manager->DisableMouseEvents();
  RunAllPendingInMessageLoop();
  local_point = delegate->mouse_event_location();
  aura::Window::ConvertPointToTarget(window.get(), root_window, &local_point);
  EXPECT_FALSE(window->bounds().Contains(local_point));

  // Location is back in window.
  cursor_manager->EnableMouseEvents();
  RunAllPendingInMessageLoop();
  local_point = delegate->mouse_event_location();
  aura::Window::ConvertPointToTarget(window.get(), root_window, &local_point);
  EXPECT_TRUE(window->bounds().Contains(local_point));
}

#if defined(OS_WIN)
// Disable on Win because RootWindow::MoveCursorTo is not implemented.
#define MAYBE_DisabledQueryMouseLocation DISABLED_DisabledQueryMouseLocation
#else
#define MAYBE_DisabledQueryMouseLocation DisabledQueryMouseLocation
#endif  // defined(OS_WIN)

TEST_F(CursorManagerTest, MAYBE_DisabledQueryMouseLocation) {
  aura::RootWindow* root_window = Shell::GetInstance()->GetPrimaryRootWindow();
  root_window->MoveCursorTo(gfx::Point(10, 10));
  gfx::Point mouse_location;
  EXPECT_TRUE(root_window->QueryMouseLocationForTest(&mouse_location));
  EXPECT_EQ("10,10", mouse_location.ToString());
  Shell::GetInstance()->cursor_manager()->DisableMouseEvents();
  EXPECT_FALSE(root_window->QueryMouseLocationForTest(&mouse_location));
  EXPECT_EQ("0,0", mouse_location.ToString());
}

}  // namespace test
}  // namespace ash