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
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
// Copyright 2014 The Flutter Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
import 'dart:ui' as ui;
import 'dart:ui' show PointerChange;
import 'package:flutter/gestures.dart';
import 'package:flutter/rendering.dart';
import 'package:flutter/services.dart';
import 'package:flutter_test/flutter_test.dart';
import 'mouse_tracker_test_utils.dart';
typedef MethodCallHandler = Future<dynamic> Function(MethodCall call);
typedef SimpleAnnotationFinder = Iterable<HitTestTarget> Function(Offset offset);
void main() {
final TestMouseTrackerFlutterBinding binding = TestMouseTrackerFlutterBinding();
MethodCallHandler? methodCallHandler;
// Only one of `logCursors` and `cursorHandler` should be specified.
void setUpMouseTracker({
required SimpleAnnotationFinder annotationFinder,
List<_CursorUpdateDetails>? logCursors,
MethodCallHandler? cursorHandler,
}) {
assert(logCursors == null || cursorHandler == null);
methodCallHandler = logCursors != null
? (MethodCall call) async {
logCursors.add(_CursorUpdateDetails.wrap(call));
return;
}
: cursorHandler;
binding.setHitTest((BoxHitTestResult result, Offset position) {
for (final HitTestTarget target in annotationFinder(position)) {
result.addWithRawTransform(
transform: Matrix4.identity(),
position: position,
hitTest: (BoxHitTestResult result, Offset position) {
result.add(HitTestEntry(target));
return true;
},
);
}
return true;
});
}
void dispatchRemoveDevice([int device = 0]) {
RendererBinding.instance.platformDispatcher.onPointerDataPacket!(ui.PointerDataPacket(data: <ui.PointerData>[
_pointerData(PointerChange.remove, Offset.zero, device: device),
]));
}
setUp(() {
binding.postFrameCallbacks.clear();
binding.defaultBinaryMessenger.setMockMethodCallHandler(SystemChannels.mouseCursor, (MethodCall call) async {
if (methodCallHandler != null) {
return methodCallHandler!(call);
}
return null;
});
});
tearDown(() {
binding.defaultBinaryMessenger.setMockMethodCallHandler(SystemChannels.mouseCursor, null);
});
test('Should work on platforms that does not support mouse cursor', () async {
const TestAnnotationTarget annotation = TestAnnotationTarget(cursor: SystemMouseCursors.grabbing);
setUpMouseTracker(
annotationFinder: (Offset position) => <TestAnnotationTarget>[annotation],
cursorHandler: (MethodCall call) async {
return null;
},
);
RendererBinding.instance.platformDispatcher.onPointerDataPacket!(ui.PointerDataPacket(data: <ui.PointerData>[
_pointerData(PointerChange.add, Offset.zero),
]));
addTearDown(dispatchRemoveDevice);
// Passes if no errors are thrown
});
test('pointer is added and removed out of any annotations', () {
final List<_CursorUpdateDetails> logCursors = <_CursorUpdateDetails>[];
TestAnnotationTarget? annotation;
setUpMouseTracker(
annotationFinder: (Offset position) => <TestAnnotationTarget>[if (annotation != null) annotation],
logCursors: logCursors,
);
// Pointer is added outside of the annotation.
RendererBinding.instance.platformDispatcher.onPointerDataPacket!(ui.PointerDataPacket(data: <ui.PointerData>[
_pointerData(PointerChange.add, Offset.zero),
]));
expect(logCursors, <_CursorUpdateDetails>[
_CursorUpdateDetails.activateSystemCursor(device: 0, kind: SystemMouseCursors.basic.kind),
]);
logCursors.clear();
// Pointer moves into the annotation
annotation = const TestAnnotationTarget(cursor: SystemMouseCursors.grabbing);
RendererBinding.instance.platformDispatcher.onPointerDataPacket!(ui.PointerDataPacket(data: <ui.PointerData>[
_pointerData(PointerChange.hover, const Offset(5.0, 0.0)),
]));
expect(logCursors, <_CursorUpdateDetails>[
_CursorUpdateDetails.activateSystemCursor(device: 0, kind: SystemMouseCursors.grabbing.kind),
]);
logCursors.clear();
// Pointer moves within the annotation
annotation = const TestAnnotationTarget(cursor: SystemMouseCursors.grabbing);
RendererBinding.instance.platformDispatcher.onPointerDataPacket!(ui.PointerDataPacket(data: <ui.PointerData>[
_pointerData(PointerChange.hover, const Offset(10.0, 0.0)),
]));
expect(logCursors, <_CursorUpdateDetails>[]);
logCursors.clear();
// Pointer moves out of the annotation
annotation = null;
RendererBinding.instance.platformDispatcher.onPointerDataPacket!(ui.PointerDataPacket(data: <ui.PointerData>[
_pointerData(PointerChange.hover, Offset.zero),
]));
expect(logCursors, <_CursorUpdateDetails>[
_CursorUpdateDetails.activateSystemCursor(device: 0, kind: SystemMouseCursors.basic.kind),
]);
logCursors.clear();
// Pointer is removed outside of the annotation.
RendererBinding.instance.platformDispatcher.onPointerDataPacket!(ui.PointerDataPacket(data: <ui.PointerData>[
_pointerData(PointerChange.remove, Offset.zero),
]));
expect(logCursors, const <_CursorUpdateDetails>[]);
});
test('pointer is added and removed in an annotation', () {
final List<_CursorUpdateDetails> logCursors = <_CursorUpdateDetails>[];
TestAnnotationTarget? annotation;
setUpMouseTracker(
annotationFinder: (Offset position) => <TestAnnotationTarget>[if (annotation != null) annotation],
logCursors: logCursors,
);
// Pointer is added in the annotation.
annotation = const TestAnnotationTarget(cursor: SystemMouseCursors.grabbing);
RendererBinding.instance.platformDispatcher.onPointerDataPacket!(ui.PointerDataPacket(data: <ui.PointerData>[
_pointerData(PointerChange.add, Offset.zero),
]));
expect(logCursors, <_CursorUpdateDetails>[
_CursorUpdateDetails.activateSystemCursor(device: 0, kind: SystemMouseCursors.grabbing.kind),
]);
logCursors.clear();
// Pointer moves out of the annotation
annotation = null;
RendererBinding.instance.platformDispatcher.onPointerDataPacket!(ui.PointerDataPacket(data: <ui.PointerData>[
_pointerData(PointerChange.hover, const Offset(5.0, 0.0)),
]));
expect(logCursors, <_CursorUpdateDetails>[
_CursorUpdateDetails.activateSystemCursor(device: 0, kind: SystemMouseCursors.basic.kind),
]);
logCursors.clear();
// Pointer moves around out of the annotation
annotation = null;
RendererBinding.instance.platformDispatcher.onPointerDataPacket!(ui.PointerDataPacket(data: <ui.PointerData>[
_pointerData(PointerChange.hover, const Offset(10.0, 0.0)),
]));
expect(logCursors, <_CursorUpdateDetails>[]);
logCursors.clear();
// Pointer moves back into the annotation
annotation = const TestAnnotationTarget(cursor: SystemMouseCursors.grabbing);
RendererBinding.instance.platformDispatcher.onPointerDataPacket!(ui.PointerDataPacket(data: <ui.PointerData>[
_pointerData(PointerChange.hover, Offset.zero),
]));
expect(logCursors, <_CursorUpdateDetails>[
_CursorUpdateDetails.activateSystemCursor(device: 0, kind: SystemMouseCursors.grabbing.kind),
]);
logCursors.clear();
// Pointer is removed within the annotation.
RendererBinding.instance.platformDispatcher.onPointerDataPacket!(ui.PointerDataPacket(data: <ui.PointerData>[
_pointerData(PointerChange.remove, Offset.zero),
]));
expect(logCursors, <_CursorUpdateDetails>[]);
});
test('pointer change caused by new frames', () {
final List<_CursorUpdateDetails> logCursors = <_CursorUpdateDetails>[];
TestAnnotationTarget? annotation;
setUpMouseTracker(
annotationFinder: (Offset position) => <TestAnnotationTarget>[if (annotation != null) annotation],
logCursors: logCursors,
);
// Pointer is added outside of the annotation.
RendererBinding.instance.platformDispatcher.onPointerDataPacket!(ui.PointerDataPacket(data: <ui.PointerData>[
_pointerData(PointerChange.add, Offset.zero),
]));
expect(logCursors, <_CursorUpdateDetails>[
_CursorUpdateDetails.activateSystemCursor(device: 0, kind: SystemMouseCursors.basic.kind),
]);
logCursors.clear();
// Synthesize a new frame while changing annotation
annotation = const TestAnnotationTarget(cursor: SystemMouseCursors.grabbing);
binding.scheduleMouseTrackerPostFrameCheck();
binding.flushPostFrameCallbacks(Duration.zero);
expect(logCursors, <_CursorUpdateDetails>[
_CursorUpdateDetails.activateSystemCursor(device: 0, kind: SystemMouseCursors.grabbing.kind),
]);
logCursors.clear();
// Synthesize a new frame without changing annotation
annotation = const TestAnnotationTarget(cursor: SystemMouseCursors.grabbing);
binding.scheduleMouseTrackerPostFrameCheck();
expect(logCursors, <_CursorUpdateDetails>[]);
logCursors.clear();
// Pointer is removed outside of the annotation.
RendererBinding.instance.platformDispatcher.onPointerDataPacket!(ui.PointerDataPacket(data: <ui.PointerData>[
_pointerData(PointerChange.remove, Offset.zero),
]));
expect(logCursors, <_CursorUpdateDetails>[]);
});
test('The first annotation with non-deferring cursor is used', () {
final List<_CursorUpdateDetails> logCursors = <_CursorUpdateDetails>[];
late List<TestAnnotationTarget> annotations;
setUpMouseTracker(
annotationFinder: (Offset position) sync* { yield* annotations; },
logCursors: logCursors,
);
annotations = <TestAnnotationTarget>[
const TestAnnotationTarget(),
const TestAnnotationTarget(cursor: SystemMouseCursors.click),
const TestAnnotationTarget(cursor: SystemMouseCursors.grabbing),
];
RendererBinding.instance.platformDispatcher.onPointerDataPacket!(ui.PointerDataPacket(data: <ui.PointerData>[
_pointerData(PointerChange.add, Offset.zero),
]));
expect(logCursors, <_CursorUpdateDetails>[
_CursorUpdateDetails.activateSystemCursor(device: 0, kind: SystemMouseCursors.click.kind),
]);
logCursors.clear();
// Remove
RendererBinding.instance.platformDispatcher.onPointerDataPacket!(ui.PointerDataPacket(data: <ui.PointerData>[
_pointerData(PointerChange.remove, const Offset(5.0, 0.0)),
]));
});
test('Annotations with deferring cursors are ignored', () {
final List<_CursorUpdateDetails> logCursors = <_CursorUpdateDetails>[];
late List<TestAnnotationTarget> annotations;
setUpMouseTracker(
annotationFinder: (Offset position) sync* { yield* annotations; },
logCursors: logCursors,
);
annotations = <TestAnnotationTarget>[
const TestAnnotationTarget(),
const TestAnnotationTarget(),
const TestAnnotationTarget(cursor: SystemMouseCursors.grabbing),
];
RendererBinding.instance.platformDispatcher.onPointerDataPacket!(ui.PointerDataPacket(data: <ui.PointerData>[
_pointerData(PointerChange.add, Offset.zero),
]));
expect(logCursors, <_CursorUpdateDetails>[
_CursorUpdateDetails.activateSystemCursor(device: 0, kind: SystemMouseCursors.grabbing.kind),
]);
logCursors.clear();
// Remove
RendererBinding.instance.platformDispatcher.onPointerDataPacket!(ui.PointerDataPacket(data: <ui.PointerData>[
_pointerData(PointerChange.remove, const Offset(5.0, 0.0)),
]));
});
test('Finding no annotation is equivalent to specifying default cursor', () {
final List<_CursorUpdateDetails> logCursors = <_CursorUpdateDetails>[];
TestAnnotationTarget? annotation;
setUpMouseTracker(
annotationFinder: (Offset position) => <TestAnnotationTarget>[if (annotation != null) annotation],
logCursors: logCursors,
);
// Pointer is added outside of the annotation.
RendererBinding.instance.platformDispatcher.onPointerDataPacket!(ui.PointerDataPacket(data: <ui.PointerData>[
_pointerData(PointerChange.add, Offset.zero),
]));
expect(logCursors, <_CursorUpdateDetails>[
_CursorUpdateDetails.activateSystemCursor(device: 0, kind: SystemMouseCursors.basic.kind),
]);
logCursors.clear();
// Pointer moved to an annotation specified with the default cursor
annotation = const TestAnnotationTarget(cursor: SystemMouseCursors.basic);
RendererBinding.instance.platformDispatcher.onPointerDataPacket!(ui.PointerDataPacket(data: <ui.PointerData>[
_pointerData(PointerChange.hover, const Offset(5.0, 0.0)),
]));
expect(logCursors, <_CursorUpdateDetails>[]);
logCursors.clear();
// Pointer moved to no annotations
annotation = null;
RendererBinding.instance.platformDispatcher.onPointerDataPacket!(ui.PointerDataPacket(data: <ui.PointerData>[
_pointerData(PointerChange.hover, Offset.zero),
]));
expect(logCursors, <_CursorUpdateDetails>[]);
logCursors.clear();
// Remove
RendererBinding.instance.platformDispatcher.onPointerDataPacket!(ui.PointerDataPacket(data: <ui.PointerData>[
_pointerData(PointerChange.remove, Offset.zero),
]));
});
test('Removing a pointer resets it back to the default cursor', () {
final List<_CursorUpdateDetails> logCursors = <_CursorUpdateDetails>[];
TestAnnotationTarget? annotation;
setUpMouseTracker(
annotationFinder: (Offset position) => <TestAnnotationTarget>[if (annotation != null) annotation],
logCursors: logCursors,
);
// Pointer is added to the annotation, then removed
annotation = const TestAnnotationTarget(cursor: SystemMouseCursors.click);
RendererBinding.instance.platformDispatcher.onPointerDataPacket!(ui.PointerDataPacket(data: <ui.PointerData>[
_pointerData(PointerChange.add, Offset.zero),
_pointerData(PointerChange.hover, const Offset(5.0, 0.0)),
_pointerData(PointerChange.remove, const Offset(5.0, 0.0)),
]));
logCursors.clear();
// Pointer is added out of the annotation
annotation = null;
RendererBinding.instance.platformDispatcher.onPointerDataPacket!(ui.PointerDataPacket(data: <ui.PointerData>[
_pointerData(PointerChange.add, Offset.zero),
]));
addTearDown(dispatchRemoveDevice);
expect(logCursors, <_CursorUpdateDetails>[
_CursorUpdateDetails.activateSystemCursor(device: 0, kind: SystemMouseCursors.basic.kind),
]);
logCursors.clear();
});
test('Pointing devices display cursors separately', () {
final List<_CursorUpdateDetails> logCursors = <_CursorUpdateDetails>[];
setUpMouseTracker(
annotationFinder: (Offset position) sync* {
if (position.dx > 200) {
yield const TestAnnotationTarget(cursor: SystemMouseCursors.forbidden);
} else if (position.dx > 100) {
yield const TestAnnotationTarget(cursor: SystemMouseCursors.click);
} else {}
},
logCursors: logCursors,
);
// Pointers are added outside of the annotation.
RendererBinding.instance.platformDispatcher.onPointerDataPacket!(ui.PointerDataPacket(data: <ui.PointerData>[
_pointerData(PointerChange.add, Offset.zero, device: 1),
_pointerData(PointerChange.add, Offset.zero, device: 2),
]));
addTearDown(() => dispatchRemoveDevice(1));
addTearDown(() => dispatchRemoveDevice(2));
expect(logCursors, <_CursorUpdateDetails>[
_CursorUpdateDetails.activateSystemCursor(device: 1, kind: SystemMouseCursors.basic.kind),
_CursorUpdateDetails.activateSystemCursor(device: 2, kind: SystemMouseCursors.basic.kind),
]);
logCursors.clear();
// Pointer 1 moved to cursor "click"
RendererBinding.instance.platformDispatcher.onPointerDataPacket!(ui.PointerDataPacket(data: <ui.PointerData>[
_pointerData(PointerChange.hover, const Offset(101.0, 0.0), device: 1),
]));
expect(logCursors, <_CursorUpdateDetails>[
_CursorUpdateDetails.activateSystemCursor(device: 1, kind: SystemMouseCursors.click.kind),
]);
logCursors.clear();
// Pointer 2 moved to cursor "click"
RendererBinding.instance.platformDispatcher.onPointerDataPacket!(ui.PointerDataPacket(data: <ui.PointerData>[
_pointerData(PointerChange.hover, const Offset(102.0, 0.0), device: 2),
]));
expect(logCursors, <_CursorUpdateDetails>[
_CursorUpdateDetails.activateSystemCursor(device: 2, kind: SystemMouseCursors.click.kind),
]);
logCursors.clear();
// Pointer 2 moved to cursor "forbidden"
RendererBinding.instance.platformDispatcher.onPointerDataPacket!(ui.PointerDataPacket(data: <ui.PointerData>[
_pointerData(PointerChange.hover, const Offset(202.0, 0.0), device: 2),
]));
expect(logCursors, <_CursorUpdateDetails>[
_CursorUpdateDetails.activateSystemCursor(device: 2, kind: SystemMouseCursors.forbidden.kind),
]);
logCursors.clear();
});
}
ui.PointerData _pointerData(
PointerChange change,
Offset logicalPosition, {
int device = 0,
PointerDeviceKind kind = PointerDeviceKind.mouse,
}) {
return ui.PointerData(
change: change,
physicalX: logicalPosition.dx * RendererBinding.instance.window.devicePixelRatio,
physicalY: logicalPosition.dy * RendererBinding.instance.window.devicePixelRatio,
kind: kind,
device: device,
);
}
class _CursorUpdateDetails extends MethodCall {
const _CursorUpdateDetails(super.method, Map<String, dynamic> super.arguments);
_CursorUpdateDetails.wrap(MethodCall call)
: super(call.method, Map<String, dynamic>.from(call.arguments as Map<dynamic, dynamic>));
_CursorUpdateDetails.activateSystemCursor({
required int device,
required String kind,
}) : this('activateSystemCursor', <String, dynamic>{'device': device, 'kind': kind});
@override
Map<String, dynamic> get arguments => super.arguments as Map<String, dynamic>;
@override
bool operator ==(Object other) {
if (identical(other, this)) {
return true;
}
if (other.runtimeType != runtimeType) {
return false;
}
return other is _CursorUpdateDetails
&& other.method == method
&& other.arguments.length == arguments.length
&& other.arguments.entries.every(
(MapEntry<String, dynamic> entry) =>
arguments.containsKey(entry.key) && arguments[entry.key] == entry.value,
);
}
@override
int get hashCode => Object.hash(method, arguments);
@override
String toString() {
return '_CursorUpdateDetails(method: $method, arguments: $arguments)';
}
}