summaryrefslogtreecommitdiff
path: root/chromium/ash/display/mouse_cursor_event_filter_unittest.cc
blob: e4c1f554ed88bbc1064247e5d5d3580ba8b69bcd (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/display/mouse_cursor_event_filter.h"

#include "ash/shell.h"
#include "ash/test/ash_test_base.h"
#include "ash/test/cursor_manager_test_api.h"
#include "ash/display/display_controller.h"
#include "ash/display/display_layout_store.h"
#include "ash/display/display_manager.h"
#include "ui/aura/env.h"
#include "ui/aura/root_window.h"
#include "ui/gfx/display.h"
#include "ui/gfx/screen.h"

namespace ash {
namespace internal {
namespace {

gfx::Display GetPrimaryDisplay() {
  return Shell::GetScreen()->GetDisplayNearestWindow(
      Shell::GetAllRootWindows()[0]);
}

gfx::Display GetSecondaryDisplay() {
  return Shell::GetScreen()->GetDisplayNearestWindow(
      Shell::GetAllRootWindows()[1]);
}

}  // namespace

typedef test::AshTestBase MouseCursorEventFilterTest;

// Verifies if the mouse pointer correctly moves to another display when there
// are two displays.
TEST_F(MouseCursorEventFilterTest, WarpMouse) {
  if (!SupportsMultipleDisplays())
    return;

  UpdateDisplay("500x500,500x500");

  MouseCursorEventFilter* event_filter =
      Shell::GetInstance()->mouse_cursor_filter();
  ASSERT_EQ(
      DisplayLayout::RIGHT,
      Shell::GetInstance()->display_manager()->layout_store()->
          default_display_layout().position);

  Shell::RootWindowList root_windows = Shell::GetAllRootWindows();
  bool is_warped = event_filter->WarpMouseCursorIfNecessary(root_windows[0],
                                                            gfx::Point(11, 11));
  EXPECT_FALSE(is_warped);
  is_warped = event_filter->WarpMouseCursorIfNecessary(root_windows[1],
                                                       gfx::Point(11, 11));
  EXPECT_FALSE(is_warped);

  // Touch the right edge of the primary root window. Pointer should warp.
  is_warped = event_filter->WarpMouseCursorIfNecessary(root_windows[0],
                                                       gfx::Point(499, 11));
  EXPECT_TRUE(is_warped);
  EXPECT_EQ("501,11",  // by 2px.
            aura::Env::GetInstance()->last_mouse_location().ToString());

  // Touch the left edge of the secondary root window. Pointer should warp.
  is_warped = event_filter->WarpMouseCursorIfNecessary(root_windows[1],
                                                       gfx::Point(500, 11));
  EXPECT_TRUE(is_warped);
  EXPECT_EQ("498,11",  // by 2px.
            aura::Env::GetInstance()->last_mouse_location().ToString());

  // Touch the left edge of the primary root window.
  is_warped = event_filter->WarpMouseCursorIfNecessary(root_windows[0],
                                                     gfx::Point(0, 11));
  EXPECT_FALSE(is_warped);
  // Touch the top edge of the primary root window.
  is_warped = event_filter->WarpMouseCursorIfNecessary(root_windows[0],
                                                     gfx::Point(11, 0));
  EXPECT_FALSE(is_warped);
  // Touch the bottom edge of the primary root window.
  is_warped = event_filter->WarpMouseCursorIfNecessary(root_windows[0],
                                                     gfx::Point(11, 499));
  EXPECT_FALSE(is_warped);
  // Touch the right edge of the secondary root window.
  is_warped = event_filter->WarpMouseCursorIfNecessary(root_windows[1],
                                                     gfx::Point(999, 11));
  EXPECT_FALSE(is_warped);
  // Touch the top edge of the secondary root window.
  is_warped = event_filter->WarpMouseCursorIfNecessary(root_windows[1],
                                                     gfx::Point(11, 0));
  EXPECT_FALSE(is_warped);
  // Touch the bottom edge of the secondary root window.
  is_warped = event_filter->WarpMouseCursorIfNecessary(root_windows[1],
                                                     gfx::Point(11, 499));
  EXPECT_FALSE(is_warped);
}

// Verifies if the mouse pointer correctly moves to another display even when
// two displays are not the same size.
TEST_F(MouseCursorEventFilterTest, WarpMouseDifferentSizeDisplays) {
  if (!SupportsMultipleDisplays())
    return;

  UpdateDisplay("500x500,600x600");  // the second one is larger.

  MouseCursorEventFilter* event_filter =
      Shell::GetInstance()->mouse_cursor_filter();
  ASSERT_EQ(
      DisplayLayout::RIGHT,
      Shell::GetInstance()->display_manager()->
          GetCurrentDisplayLayout().position);

  Shell::RootWindowList root_windows = Shell::GetAllRootWindows();
  aura::Env::GetInstance()->set_last_mouse_location(gfx::Point(623, 123));

  // Touch the left edge of the secondary root window. Pointer should NOT warp
  // because 1px left of (0, 500) is outside the primary root window.
  bool is_warped = event_filter->WarpMouseCursorIfNecessary(root_windows[1],
                                                            gfx::Point(0, 500));
  EXPECT_FALSE(is_warped);
  EXPECT_EQ("623,123",  // by 2px.
            aura::Env::GetInstance()->last_mouse_location().ToString());

  // Touch the left edge of the secondary root window. Pointer should warp
  // because 1px left of (0, 499) is inside the primary root window.
  is_warped = event_filter->WarpMouseCursorIfNecessary(root_windows[1],
                                                     gfx::Point(500, 499));
  EXPECT_TRUE(is_warped);
  EXPECT_EQ("498,499",  // by 2px.
            aura::Env::GetInstance()->last_mouse_location().ToString());
}

// Verifies if the mouse pointer correctly moves between displays with
// different scale factors.
TEST_F(MouseCursorEventFilterTest, WarpMouseDifferentScaleDisplays) {
  if (!SupportsMultipleDisplays())
    return;

  UpdateDisplay("500x500,600x600*2");

  MouseCursorEventFilter* event_filter =
      Shell::GetInstance()->mouse_cursor_filter();
  ASSERT_EQ(
      DisplayLayout::RIGHT,
      Shell::GetInstance()->display_manager()->
          GetCurrentDisplayLayout().position);

  Shell::RootWindowList root_windows = Shell::GetAllRootWindows();
  aura::Env::GetInstance()->set_last_mouse_location(gfx::Point(900, 123));

  // This emulates the dragging back to the 2nd display, which has
  // higher scale factor, by having 2nd display's root as target
  // but have the edge of 1st display.
  bool is_warped =
      event_filter->WarpMouseCursorIfNecessary(root_windows[1],
                                               gfx::Point(498, 123));
  EXPECT_TRUE(is_warped);
  EXPECT_EQ("502,123",
            aura::Env::GetInstance()->last_mouse_location().ToString());

  // Touch the edge of 2nd display again and make sure it warps to
  // 1st dislay.
  is_warped = event_filter->WarpMouseCursorIfNecessary(root_windows[1],
                                                       gfx::Point(500, 123));
  EXPECT_TRUE(is_warped);
  EXPECT_EQ("496,123",
            aura::Env::GetInstance()->last_mouse_location().ToString());
}

// Verifies if MouseCursorEventFilter::set_mouse_warp_mode() works as expected.
TEST_F(MouseCursorEventFilterTest, SetMouseWarpModeFlag) {
  if (!SupportsMultipleDisplays())
    return;

  UpdateDisplay("500x500,500x500");

  MouseCursorEventFilter* event_filter =
      Shell::GetInstance()->mouse_cursor_filter();

  Shell::RootWindowList root_windows = Shell::GetAllRootWindows();
  aura::Env::GetInstance()->set_last_mouse_location(gfx::Point(1, 1));

  event_filter->set_mouse_warp_mode(MouseCursorEventFilter::WARP_NONE);
  bool is_warped = event_filter->WarpMouseCursorIfNecessary(root_windows[0],
                                                          gfx::Point(499, 11));
  EXPECT_FALSE(is_warped);
  EXPECT_EQ("1,1",
            aura::Env::GetInstance()->last_mouse_location().ToString());

  event_filter->set_mouse_warp_mode(MouseCursorEventFilter::WARP_ALWAYS);
  is_warped = event_filter->WarpMouseCursorIfNecessary(root_windows[0],
                                                       gfx::Point(499, 11));
  EXPECT_TRUE(is_warped);
  EXPECT_EQ("501,11",
            aura::Env::GetInstance()->last_mouse_location().ToString());
}

// Verifies if MouseCursorEventFilter's bounds calculation works correctly.
TEST_F(MouseCursorEventFilterTest, IndicatorBoundsTestOnRight) {
  if (!SupportsMultipleDisplays())
    return;

  UpdateDisplay("360x360,700x700");
  Shell::RootWindowList root_windows = Shell::GetAllRootWindows();

  DisplayController* controller =
      Shell::GetInstance()->display_controller();
  DisplayLayout layout(DisplayLayout::RIGHT, 0);
  controller->SetLayoutForCurrentDisplays(layout);
  ash::internal::MouseCursorEventFilter* event_filter =
      Shell::GetInstance()->mouse_cursor_filter();
  event_filter->ShowSharedEdgeIndicator(root_windows[0] /* primary */);
  EXPECT_EQ("359,16 1x344", event_filter->src_indicator_bounds_.ToString());
  EXPECT_EQ("360,0 1x360", event_filter->dst_indicator_bounds_.ToString());
  event_filter->ShowSharedEdgeIndicator(root_windows[1] /* secondary */);
  EXPECT_EQ("360,16 1x344", event_filter->src_indicator_bounds_.ToString());
  EXPECT_EQ("359,0 1x360", event_filter->dst_indicator_bounds_.ToString());

  // Move 2nd display downwards a bit.
  layout.offset = 5;
  controller->SetLayoutForCurrentDisplays(layout);
  event_filter->ShowSharedEdgeIndicator(root_windows[0] /* primary */);
  // This is same as before because the 2nd display's y is above
  // the indicator's x.
  EXPECT_EQ("359,16 1x344", event_filter->src_indicator_bounds_.ToString());
  EXPECT_EQ("360,5 1x355", event_filter->dst_indicator_bounds_.ToString());
  event_filter->ShowSharedEdgeIndicator(root_windows[1] /* secondary */);
  EXPECT_EQ("360,21 1x339", event_filter->src_indicator_bounds_.ToString());
  EXPECT_EQ("359,5 1x355", event_filter->dst_indicator_bounds_.ToString());

  // Move it down further so that the shared edge is shorter than
  // minimum hole size (160).
  layout.offset = 200;
  controller->SetLayoutForCurrentDisplays(layout);
  event_filter->ShowSharedEdgeIndicator(root_windows[0] /* primary */);
  EXPECT_EQ("359,200 1x160", event_filter->src_indicator_bounds_.ToString());
  EXPECT_EQ("360,200 1x160", event_filter->dst_indicator_bounds_.ToString());
  event_filter->ShowSharedEdgeIndicator(root_windows[1] /* secondary */);
  EXPECT_EQ("360,200 1x160", event_filter->src_indicator_bounds_.ToString());
  EXPECT_EQ("359,200 1x160", event_filter->dst_indicator_bounds_.ToString());

  // Now move 2nd display upwards
  layout.offset = -5;
  controller->SetLayoutForCurrentDisplays(layout);
  event_filter->ShowSharedEdgeIndicator(root_windows[0] /* primary */);
  EXPECT_EQ("359,16 1x344", event_filter->src_indicator_bounds_.ToString());
  EXPECT_EQ("360,0 1x360", event_filter->dst_indicator_bounds_.ToString());
  event_filter->ShowSharedEdgeIndicator(root_windows[1] /* secondary */);
  // 16 px are reserved on 2nd display from top, so y must be
  // (16 - 5) = 11
  EXPECT_EQ("360,11 1x349", event_filter->src_indicator_bounds_.ToString());
  EXPECT_EQ("359,0 1x360", event_filter->dst_indicator_bounds_.ToString());

  event_filter->HideSharedEdgeIndicator();
}

TEST_F(MouseCursorEventFilterTest, IndicatorBoundsTestOnLeft) {
  if (!SupportsMultipleDisplays())
    return;

  UpdateDisplay("360x360,700x700");
  Shell::RootWindowList root_windows = Shell::GetAllRootWindows();

  DisplayController* controller =
      Shell::GetInstance()->display_controller();
  DisplayLayout layout(DisplayLayout::LEFT, 0);
  controller->SetLayoutForCurrentDisplays(layout);
  ash::internal::MouseCursorEventFilter* event_filter =
      Shell::GetInstance()->mouse_cursor_filter();
  event_filter->ShowSharedEdgeIndicator(root_windows[0] /* primary */);
  EXPECT_EQ("0,16 1x344", event_filter->src_indicator_bounds_.ToString());
  EXPECT_EQ("-1,0 1x360", event_filter->dst_indicator_bounds_.ToString());
  event_filter->ShowSharedEdgeIndicator(root_windows[1] /* secondary */);
  EXPECT_EQ("-1,16 1x344", event_filter->src_indicator_bounds_.ToString());
  EXPECT_EQ("0,0 1x360", event_filter->dst_indicator_bounds_.ToString());

  layout.offset = 250;
  controller->SetLayoutForCurrentDisplays(layout);
  event_filter->ShowSharedEdgeIndicator(root_windows[0] /* primary */);
  EXPECT_EQ("0,250 1x110", event_filter->src_indicator_bounds_.ToString());
  EXPECT_EQ("-1,250 1x110", event_filter->dst_indicator_bounds_.ToString());
  event_filter->ShowSharedEdgeIndicator(root_windows[1] /* secondary */);
  EXPECT_EQ("-1,250 1x110", event_filter->src_indicator_bounds_.ToString());
  EXPECT_EQ("0,250 1x110", event_filter->dst_indicator_bounds_.ToString());
  event_filter->HideSharedEdgeIndicator();
}

TEST_F(MouseCursorEventFilterTest, IndicatorBoundsTestOnTopBottom) {
  if (!SupportsMultipleDisplays())
    return;

  UpdateDisplay("360x360,700x700");
  Shell::RootWindowList root_windows = Shell::GetAllRootWindows();

  DisplayController* controller =
      Shell::GetInstance()->display_controller();
  DisplayLayout layout(DisplayLayout::TOP, 0);
  controller->SetLayoutForCurrentDisplays(layout);
  ash::internal::MouseCursorEventFilter* event_filter =
      Shell::GetInstance()->mouse_cursor_filter();
  event_filter->ShowSharedEdgeIndicator(root_windows[0] /* primary */);
  EXPECT_EQ("0,0 360x1", event_filter->src_indicator_bounds_.ToString());
  EXPECT_EQ("0,-1 360x1", event_filter->dst_indicator_bounds_.ToString());
  event_filter->ShowSharedEdgeIndicator(root_windows[1] /* secondary */);
  EXPECT_EQ("0,-1 360x1", event_filter->src_indicator_bounds_.ToString());
  EXPECT_EQ("0,0 360x1", event_filter->dst_indicator_bounds_.ToString());

  layout.offset = 250;
  controller->SetLayoutForCurrentDisplays(layout);
  event_filter->ShowSharedEdgeIndicator(root_windows[0] /* primary */);
  EXPECT_EQ("250,0 110x1", event_filter->src_indicator_bounds_.ToString());
  EXPECT_EQ("250,-1 110x1", event_filter->dst_indicator_bounds_.ToString());
  event_filter->ShowSharedEdgeIndicator(root_windows[1] /* secondary */);
  EXPECT_EQ("250,-1 110x1", event_filter->src_indicator_bounds_.ToString());
  EXPECT_EQ("250,0 110x1", event_filter->dst_indicator_bounds_.ToString());

  layout.position = DisplayLayout::BOTTOM;
  layout.offset = 0;
  controller->SetLayoutForCurrentDisplays(layout);
  event_filter->ShowSharedEdgeIndicator(root_windows[0] /* primary */);
  EXPECT_EQ("0,359 360x1", event_filter->src_indicator_bounds_.ToString());
  EXPECT_EQ("0,360 360x1", event_filter->dst_indicator_bounds_.ToString());
  event_filter->ShowSharedEdgeIndicator(root_windows[1] /* secondary */);
  EXPECT_EQ("0,360 360x1", event_filter->src_indicator_bounds_.ToString());
  EXPECT_EQ("0,359 360x1", event_filter->dst_indicator_bounds_.ToString());

  event_filter->HideSharedEdgeIndicator();
}

// Verifies cursor's device scale factor is updated when a cursor has moved
// across root windows with different device scale factors
// (http://crbug.com/154183).
TEST_F(MouseCursorEventFilterTest, CursorDeviceScaleFactor) {
  if (!SupportsMultipleDisplays())
    return;

  UpdateDisplay("400x400,800x800*2");
  DisplayController* controller =
      Shell::GetInstance()->display_controller();
  controller->SetLayoutForCurrentDisplays(
      DisplayLayout(DisplayLayout::RIGHT, 0));
  Shell::RootWindowList root_windows = Shell::GetAllRootWindows();
  ASSERT_EQ(2U, root_windows.size());
  test::CursorManagerTestApi cursor_test_api(
      Shell::GetInstance()->cursor_manager());
  MouseCursorEventFilter* event_filter =
      Shell::GetInstance()->mouse_cursor_filter();

  EXPECT_EQ(1.0f, cursor_test_api.GetDisplay().device_scale_factor());
  event_filter->WarpMouseCursorIfNecessary(root_windows[0],
                                           gfx::Point(399, 200));
  EXPECT_EQ(2.0f, cursor_test_api.GetDisplay().device_scale_factor());
  event_filter->WarpMouseCursorIfNecessary(root_windows[1],
                                           gfx::Point(400, 200));
  EXPECT_EQ(1.0f, cursor_test_api.GetDisplay().device_scale_factor());
}

}  // namespace internal
}  // namespace ash