Implement hover support for mouse pointers. (#24830)

This implements support for hovering mouse pointers, so that mice connected to Android devices, and ChromeOS devices running Android apps will work properly.

It teaches flutter_test about hover events, which required changing how they are created and used.

Also modifies AnnotatedRegion to allow a region that can be located someplace other than just the origin.

Along with tests for all of the above.

Fixes #5504
This commit is contained in:
Greg Spencer
2019-01-29 11:34:26 -08:00
committed by GitHub
parent c9d5b129e2
commit 1811d57445
17 changed files with 1385 additions and 169 deletions

View File

@@ -303,11 +303,11 @@ abstract class TestWidgetsFlutterBinding extends BindingBase
Offset localToGlobal(Offset point) => point;
@override
void dispatchEvent(PointerEvent event, HitTestResult result, {
void dispatchEvent(PointerEvent event, HitTestResult hitTestResult, {
TestBindingEventSource source = TestBindingEventSource.device
}) {
assert(source == TestBindingEventSource.test);
super.dispatchEvent(event, result);
super.dispatchEvent(event, hitTestResult);
}
/// A stub for the system's onscreen keyboard. Callers must set the
@@ -1177,7 +1177,7 @@ class LiveTestWidgetsFlutterBinding extends TestWidgetsFlutterBinding {
HitTestDispatcher deviceEventDispatcher;
@override
void dispatchEvent(PointerEvent event, HitTestResult result, {
void dispatchEvent(PointerEvent event, HitTestResult hitTestResult, {
TestBindingEventSource source = TestBindingEventSource.device
}) {
switch (source) {
@@ -1191,11 +1191,11 @@ class LiveTestWidgetsFlutterBinding extends TestWidgetsFlutterBinding {
renderView._pointers[event.pointer].decay = _kPointerDecay;
}
_handleViewNeedsPaint();
super.dispatchEvent(event, result, source: source);
super.dispatchEvent(event, hitTestResult, source: source);
break;
case TestBindingEventSource.device:
if (deviceEventDispatcher != null)
deviceEventDispatcher.dispatchEvent(event, result);
deviceEventDispatcher.dispatchEvent(event, hitTestResult);
break;
}
}

View File

@@ -38,15 +38,13 @@ abstract class WidgetController {
return finder.evaluate().isNotEmpty;
}
/// All widgets currently in the widget tree (lazy pre-order traversal).
///
/// Can contain duplicates, since widgets can be used in multiple
/// places in the widget tree.
Iterable<Widget> get allWidgets {
TestAsyncUtils.guardSync();
return allElements
.map<Widget>((Element element) => element.widget);
return allElements.map<Widget>((Element element) => element.widget);
}
/// The matching widget in the widget tree.
@@ -84,7 +82,6 @@ abstract class WidgetController {
});
}
/// All elements currently in the widget tree (lazy pre-order traversal).
///
/// The returned iterable is lazy. It does not walk the entire widget tree
@@ -127,7 +124,6 @@ abstract class WidgetController {
return finder.evaluate();
}
/// All states currently in the widget tree (lazy pre-order traversal).
///
/// The returned iterable is lazy. It does not walk the entire widget tree
@@ -135,9 +131,7 @@ abstract class WidgetController {
/// using [Iterator.moveNext].
Iterable<State> get allStates {
TestAsyncUtils.guardSync();
return allElements
.whereType<StatefulElement>()
.map<State>((StatefulElement element) => element.state);
return allElements.whereType<StatefulElement>().map<State>((StatefulElement element) => element.state);
}
/// The matching state in the widget tree.
@@ -183,7 +177,6 @@ abstract class WidgetController {
throw StateError('Widget of type ${element.widget.runtimeType}, with ${finder.description}, is not a StatefulWidget.');
}
/// Render objects of all the widgets currently in the widget tree
/// (lazy pre-order traversal).
///
@@ -193,8 +186,7 @@ abstract class WidgetController {
/// their own render object.
Iterable<RenderObject> get allRenderObjects {
TestAsyncUtils.guardSync();
return allElements
.map<RenderObject>((Element element) => element.renderObject);
return allElements.map<RenderObject>((Element element) => element.renderObject);
}
/// The render object of the matching widget in the widget tree.
@@ -232,7 +224,6 @@ abstract class WidgetController {
});
}
/// Returns a list of all the [Layer] objects in the rendering.
List<Layer> get layers => _walkLayers(binding.renderView.layer).toList();
Iterable<Layer> _walkLayers(Layer layer) sync* {
@@ -248,7 +239,6 @@ abstract class WidgetController {
}
}
// INTERACTION
/// Dispatch a pointer down / pointer up sequence at the center of
@@ -256,12 +246,12 @@ abstract class WidgetController {
///
/// If the center of the widget is not exposed, this might send events to
/// another object.
Future<void> tap(Finder finder, { int pointer }) {
Future<void> tap(Finder finder, {int pointer}) {
return tapAt(getCenter(finder), pointer: pointer);
}
/// Dispatch a pointer down / pointer up sequence at the given location.
Future<void> tapAt(Offset location, { int pointer }) {
Future<void> tapAt(Offset location, {int pointer}) {
return TestAsyncUtils.guard<void>(() async {
final TestGesture gesture = await startGesture(location, pointer: pointer);
await gesture.up();
@@ -273,7 +263,7 @@ abstract class WidgetController {
///
/// If the center of the widget is not exposed, this might send events to
/// another object.
Future<TestGesture> press(Finder finder, { int pointer }) {
Future<TestGesture> press(Finder finder, {int pointer}) {
return TestAsyncUtils.guard<TestGesture>(() {
return startGesture(getCenter(finder), pointer: pointer);
});
@@ -285,13 +275,13 @@ abstract class WidgetController {
///
/// If the center of the widget is not exposed, this might send events to
/// another object.
Future<void> longPress(Finder finder, { int pointer }) {
Future<void> longPress(Finder finder, {int pointer}) {
return longPressAt(getCenter(finder), pointer: pointer);
}
/// Dispatch a pointer down / pointer up sequence at the given location with
/// a delay of [kLongPressTimeout] + [kPressTimeout] between the two events.
Future<void> longPressAt(Offset location, { int pointer }) {
Future<void> longPressAt(Offset location, {int pointer}) {
return TestAsyncUtils.guard<void>(() async {
final TestGesture gesture = await startGesture(location, pointer: pointer);
await pump(kLongPressTimeout + kPressTimeout);
@@ -319,7 +309,10 @@ abstract class WidgetController {
/// opposite direction of the fling (e.g. dragging 200 pixels to the right,
/// then fling to the left over 200 pixels, ending at the exact point that the
/// drag started).
Future<void> fling(Finder finder, Offset offset, double speed, {
Future<void> fling(
Finder finder,
Offset offset,
double speed, {
int pointer,
Duration frameInterval = const Duration(milliseconds: 16),
Offset initialOffset = Offset.zero,
@@ -361,7 +354,10 @@ abstract class WidgetController {
/// opposite direction of the fling (e.g. dragging 200 pixels to the right,
/// then fling to the left over 200 pixels, ending at the exact point that the
/// drag started).
Future<void> flingFrom(Offset startLocation, Offset offset, double speed, {
Future<void> flingFrom(
Offset startLocation,
Offset offset,
double speed, {
int pointer,
Duration frameInterval = const Duration(milliseconds: 16),
Offset initialOffset = Offset.zero,
@@ -414,7 +410,7 @@ abstract class WidgetController {
///
/// If you want the drag to end with a speed so that the gesture recognition
/// system identifies the gesture as a fling, consider using [fling] instead.
Future<void> drag(Finder finder, Offset offset, { int pointer }) {
Future<void> drag(Finder finder, Offset offset, {int pointer}) {
return dragFrom(getCenter(finder), offset, pointer: pointer);
}
@@ -424,7 +420,7 @@ abstract class WidgetController {
/// If you want the drag to end with a speed so that the gesture recognition
/// system identifies the gesture as a fling, consider using [flingFrom]
/// instead.
Future<void> dragFrom(Offset startLocation, Offset offset, { int pointer }) {
Future<void> dragFrom(Offset startLocation, Offset offset, {int pointer}) {
return TestAsyncUtils.guard<void>(() async {
final TestGesture gesture = await startGesture(startLocation, pointer: pointer);
assert(gesture != null);
@@ -445,17 +441,32 @@ abstract class WidgetController {
return result;
}
/// Begins a gesture at a particular point, and returns the
/// [TestGesture] object which you can use to continue the gesture.
Future<TestGesture> startGesture(Offset downLocation, { int pointer }) {
return TestGesture.down(
downLocation,
pointer: pointer ?? _getNextPointer(),
/// Creates gesture and returns the [TestGesture] object which you can use
/// to continue the gesture using calls on the [TestGesture] object.
///
/// You can use [startGesture] instead if your gesture begins with a down
/// event.
Future<TestGesture> createGesture({int pointer, PointerDeviceKind kind = PointerDeviceKind.touch}) async {
return TestGesture(
hitTester: hitTestOnBinding,
dispatcher: sendEventToBinding,
kind: kind,
pointer: pointer ?? _getNextPointer(),
);
}
/// Creates a gesture with an initial down gesture at a particular point, and
/// returns the [TestGesture] object which you can use to continue the
/// gesture.
///
/// You can use [createGesture] if your gesture doesn't begin with an initial
/// down gesture.
Future<TestGesture> startGesture(Offset downLocation, {int pointer}) async {
final TestGesture result = await createGesture(pointer: pointer);
await result.down(downLocation);
return result;
}
/// Forwards the given location to the binding's hitTest logic.
HitTestResult hitTestOnBinding(Offset location) {
final HitTestResult result = HitTestResult();
@@ -470,7 +481,6 @@ abstract class WidgetController {
});
}
// GEOMETRY
/// Returns the point at the center of the given widget.

View File

@@ -13,8 +13,8 @@ export 'dart:ui' show Offset;
/// A class for generating coherent artificial pointer events.
///
/// You can use this to manually simulate individual events, but the
/// simplest way to generate coherent gestures is to use [TestGesture].
/// You can use this to manually simulate individual events, but the simplest
/// way to generate coherent gestures is to use [TestGesture].
class TestPointer {
/// Creates a [TestPointer]. By default, the pointer identifier used is 1,
/// however this can be overridden by providing an argument to the
@@ -22,13 +22,19 @@ class TestPointer {
///
/// Multiple [TestPointer]s created with the same pointer identifier will
/// interfere with each other if they are used in parallel.
TestPointer([ this.pointer = 1 ]);
TestPointer([this.pointer = 1, this.kind = PointerDeviceKind.touch])
: assert(kind != null),
assert(pointer != null);
/// The pointer identifier used for events generated by this object.
///
/// Set when the object is constructed. Defaults to 1.
final int pointer;
/// The kind of pointer device to simulate. Defaults to
/// [PointerDeviceKind.touch].
final PointerDeviceKind kind;
/// Whether the pointer simulated by this object is currently down.
///
/// A pointer is released (goes up) by calling [up] or [cancel].
@@ -64,68 +70,103 @@ class TestPointer {
/// Create a [PointerDownEvent] at the given location.
///
/// By default, the time stamp on the event is [Duration.zero]. You
/// can give a specific time stamp by passing the `timeStamp`
/// argument.
PointerDownEvent down(Offset newLocation, { Duration timeStamp = Duration.zero }) {
/// By default, the time stamp on the event is [Duration.zero]. You can give a
/// specific time stamp by passing the `timeStamp` argument.
PointerDownEvent down(Offset newLocation, {Duration timeStamp = Duration.zero}) {
assert(!isDown);
_isDown = true;
_location = newLocation;
return PointerDownEvent(
timeStamp: timeStamp,
kind: kind,
pointer: pointer,
position: location
position: location,
);
}
/// Create a [PointerMoveEvent] to the given location.
///
/// By default, the time stamp on the event is [Duration.zero]. You
/// can give a specific time stamp by passing the `timeStamp`
/// argument.
PointerMoveEvent move(Offset newLocation, { Duration timeStamp = Duration.zero }) {
assert(isDown);
/// By default, the time stamp on the event is [Duration.zero]. You can give a
/// specific time stamp by passing the `timeStamp` argument.
///
/// [isDown] must be true when this is called, since move events can only
/// be generated when the pointer is down.
PointerMoveEvent move(Offset newLocation, {Duration timeStamp = Duration.zero}) {
assert(
isDown,
'Move events can only be generated when the pointer is down. To '
'create a movement event simulating a pointer move when the pointer is '
'up, use hover() instead.');
final Offset delta = newLocation - location;
_location = newLocation;
return PointerMoveEvent(
timeStamp: timeStamp,
kind: kind,
pointer: pointer,
position: newLocation,
delta: delta
delta: delta,
);
}
/// Create a [PointerUpEvent].
///
/// By default, the time stamp on the event is [Duration.zero]. You
/// can give a specific time stamp by passing the `timeStamp`
/// argument.
/// By default, the time stamp on the event is [Duration.zero]. You can give a
/// specific time stamp by passing the `timeStamp` argument.
///
/// The object is no longer usable after this method has been called.
PointerUpEvent up({ Duration timeStamp = Duration.zero }) {
PointerUpEvent up({Duration timeStamp = Duration.zero}) {
assert(isDown);
_isDown = false;
return PointerUpEvent(
timeStamp: timeStamp,
kind: kind,
pointer: pointer,
position: location
position: location,
);
}
/// Create a [PointerCancelEvent].
///
/// By default, the time stamp on the event is [Duration.zero]. You
/// can give a specific time stamp by passing the `timeStamp`
/// argument.
/// By default, the time stamp on the event is [Duration.zero]. You can give a
/// specific time stamp by passing the `timeStamp` argument.
///
/// The object is no longer usable after this method has been called.
PointerCancelEvent cancel({ Duration timeStamp = Duration.zero }) {
PointerCancelEvent cancel({Duration timeStamp = Duration.zero}) {
assert(isDown);
_isDown = false;
return PointerCancelEvent(
timeStamp: timeStamp,
kind: kind,
pointer: pointer,
position: location
position: location,
);
}
/// Create a [PointerHoverEvent] to the given location.
///
/// By default, the time stamp on the event is [Duration.zero]. You can give a
/// specific time stamp by passing the `timeStamp` argument.
///
/// [isDown] must be false, since hover events can't be sent when the pointer
/// is up.
PointerHoverEvent hover(
Offset newLocation, {
Duration timeStamp = Duration.zero,
}) {
assert(newLocation != null);
assert(timeStamp != null);
assert(
!isDown,
'Hover events can only be generated when the pointer is up. To '
'simulate movement when the pointer is down, use move() instead.');
assert(kind != PointerDeviceKind.touch, "Touch pointers can't generate hover events");
final Offset delta = location != null ? newLocation - location : Offset.zero;
_location = newLocation;
return PointerHoverEvent(
timeStamp: timeStamp,
kind: kind,
position: newLocation,
delta: delta,
);
}
}
@@ -142,77 +183,53 @@ typedef HitTester = HitTestResult Function(Offset location);
/// The simplest way to create a [TestGesture] is to call
/// [WidgetTester.startGesture].
class TestGesture {
TestGesture._(this._dispatcher, this._result, this._pointer);
/// Create a [TestGesture] by starting with a pointerDown at the
/// given point.
/// Create a [TestGesture] without dispatching any events from it.
/// The [TestGesture] can then be manipulated to perform future actions.
///
/// By default, the pointer identifier used is 1. This can be overridden by
/// providing the `pointer` argument.
///
/// A function to use for hit testing should be provided via the `hitTester`
/// argument, and a function to use for dispatching events should be provided
/// A function to use for hit testing must be provided via the `hitTester`
/// argument, and a function to use for dispatching events must be provided
/// via the `dispatcher` argument.
static Future<TestGesture> down(Offset downLocation, {
int pointer = 1,
@required HitTester hitTester,
@required EventDispatcher dispatcher,
}) async {
assert(hitTester != null);
assert(dispatcher != null);
TestGesture result;
return TestAsyncUtils.guard<void>(() async {
// dispatch down event
final HitTestResult hitTestResult = hitTester(downLocation);
final TestPointer testPointer = TestPointer(pointer);
await dispatcher(testPointer.down(downLocation), hitTestResult);
// create a TestGesture
result = TestGesture._(dispatcher, hitTestResult, testPointer);
}).then<TestGesture>((void value) {
return result;
}, onError: (dynamic error, StackTrace stack) {
return Future<TestGesture>.error(error, stack);
});
}
/// Create a [TestGesture] by starting with a custom [PointerDownEvent] at the
/// given point.
///
/// By default, the pointer identifier used is 1. This can be overridden by
/// providing the `pointer` argument.
/// The device `kind` defaults to [PointerDeviceKind.touch], but move events
/// when the pointer is "up" require a kind other than
/// [PointerDeviceKind.touch], like [PointerDeviceKind.mouse], for example,
/// because touch devices can't produce movement events when they are "up".
///
/// A function to use for hit testing should be provided via the `hitTester`
/// argument, and a function to use for dispatching events should be provided
/// via the `dispatcher` argument.
static Future<TestGesture> downWithCustomEvent(Offset downLocation, PointerDownEvent downEvent, {
int pointer = 1,
@required HitTester hitTester,
/// None of the arguments may be null. The `dispatcher` and `hitTester`
/// arguments are required.
TestGesture({
@required EventDispatcher dispatcher,
}) async {
assert(hitTester != null);
assert(dispatcher != null);
TestGesture result;
@required HitTester hitTester,
int pointer = 1,
PointerDeviceKind kind = PointerDeviceKind.touch,
}) : assert(dispatcher != null),
assert(hitTester != null),
assert(pointer != null),
assert(kind != null),
_dispatcher = dispatcher,
_hitTester = hitTester,
_pointer = TestPointer(pointer, kind),
_result = null;
/// Dispatch a pointer down event at the given `downLocation`, caching the
/// hit test result.
Future<void> down(Offset downLocation) async {
return TestAsyncUtils.guard<void>(() async {
// dispatch down event
final HitTestResult hitTestResult = hitTester(downLocation);
final TestPointer testPointer = TestPointer(pointer);
testPointer.setDownInfo(downEvent, downLocation);
await dispatcher(downEvent, hitTestResult);
// create a TestGesture
result = TestGesture._(dispatcher, hitTestResult, testPointer);
}).then<TestGesture>((void value) {
return result;
}, onError: (dynamic error, StackTrace stack) {
return Future<TestGesture>.error(error, stack);
_result = _hitTester(downLocation);
return _dispatcher(_pointer.down(downLocation), _result);
});
}
final EventDispatcher _dispatcher;
final HitTestResult _result;
final HitTester _hitTester;
final TestPointer _pointer;
HitTestResult _result;
/// Send a move event moving the pointer by the given offset.
/// In a test, send a move event that moves the pointer by the given offset.
@visibleForTesting
Future<void> updateWithCustomEvent(PointerEvent event, { Duration timeStamp = Duration.zero }) {
_pointer.setDownInfo(event, event.position);
return TestAsyncUtils.guard<void>(() {
@@ -221,40 +238,53 @@ class TestGesture {
}
/// Send a move event moving the pointer by the given offset.
Future<void> moveBy(Offset offset, { Duration timeStamp = Duration.zero }) {
assert(_pointer._isDown);
///
/// If the pointer is down, then a move event is dispatched. If the pointer is
/// up, then a hover event is dispatched. Touch devices are not able to send
/// hover events.
Future<void> moveBy(Offset offset, {Duration timeStamp = Duration.zero}) {
return moveTo(_pointer.location + offset, timeStamp: timeStamp);
}
/// Send a move event moving the pointer to the given location.
Future<void> moveTo(Offset location, { Duration timeStamp = Duration.zero }) {
///
/// If the pointer is down, then a move event is dispatched. If the pointer is
/// up, then a hover event is dispatched. Touch devices are not able to send
/// hover events.
Future<void> moveTo(Offset location, {Duration timeStamp = Duration.zero}) {
return TestAsyncUtils.guard<void>(() {
assert(_pointer._isDown);
return _dispatcher(_pointer.move(location, timeStamp: timeStamp), _result);
if (_pointer._isDown) {
assert(_result != null,
'Move events with the pointer down must be preceeded by a down '
'event that captures a hit test result.');
return _dispatcher(_pointer.move(location, timeStamp: timeStamp), _result);
} else {
assert(_pointer.kind != PointerDeviceKind.touch,
'Touch device move events can only be sent if the pointer is down.');
return _dispatcher(_pointer.hover(location, timeStamp: timeStamp), null);
}
});
}
/// End the gesture by releasing the pointer.
///
/// The object is no longer usable after this method has been called.
Future<void> up() {
return TestAsyncUtils.guard<void>(() async {
assert(_pointer._isDown);
await _dispatcher(_pointer.up(), _result);
assert(!_pointer._isDown);
_result = null;
});
}
/// End the gesture by canceling the pointer (as would happen if the
/// system showed a modal dialog on top of the Flutter application,
/// for instance).
///
/// The object is no longer usable after this method has been called.
Future<void> cancel() {
return TestAsyncUtils.guard<void>(() async {
assert(_pointer._isDown);
await _dispatcher(_pointer.cancel(), _result);
assert(!_pointer._isDown);
_result = null;
});
}
}