Expose GestureBinding.handlePointerEvent, replacing dispatchEvent as the preferred way to dispatch events (#64846)
This commit is contained in:
@@ -196,6 +196,7 @@ abstract class TestWidgetsFlutterBinding extends BindingBase
|
||||
/// prepare the binding for the next test.
|
||||
void reset() {
|
||||
_restorationManager = createRestorationManager();
|
||||
resetGestureBinding();
|
||||
}
|
||||
|
||||
@override
|
||||
@@ -488,19 +489,25 @@ abstract class TestWidgetsFlutterBinding extends BindingBase
|
||||
/// events from the device).
|
||||
Offset localToGlobal(Offset point) => point;
|
||||
|
||||
// The source of the current pointer event.
|
||||
//
|
||||
// The [pointerEventSource] is set as the `source` parameter of
|
||||
// [handlePointerEvent] and can be used in the immediate enclosing
|
||||
// [dispatchEvent].
|
||||
TestBindingEventSource _pointerEventSource = TestBindingEventSource.device;
|
||||
|
||||
@override
|
||||
void dispatchEvent(
|
||||
PointerEvent event,
|
||||
HitTestResult hitTestResult, {
|
||||
void handlePointerEvent(
|
||||
PointerEvent event, {
|
||||
TestBindingEventSource source = TestBindingEventSource.device,
|
||||
}) {
|
||||
// This override disables calling this method from base class
|
||||
// [GestureBinding] when the runtime type is [TestWidgetsFlutterBinding],
|
||||
// while enables sub class [LiveTestWidgetsFlutterBinding] to override
|
||||
// this behavior and use this argument to determine the souce of the event
|
||||
// especially when the test app is running on a device.
|
||||
assert(source == TestBindingEventSource.test);
|
||||
super.dispatchEvent(event, hitTestResult);
|
||||
final TestBindingEventSource previousSource = source;
|
||||
_pointerEventSource = source;
|
||||
try {
|
||||
super.handlePointerEvent(event);
|
||||
} finally {
|
||||
_pointerEventSource = previousSource;
|
||||
}
|
||||
}
|
||||
|
||||
/// A stub for the system's onscreen keyboard. Callers must set the
|
||||
@@ -1486,14 +1493,13 @@ class LiveTestWidgetsFlutterBinding extends TestWidgetsFlutterBinding {
|
||||
HitTestDispatcher deviceEventDispatcher;
|
||||
|
||||
|
||||
/// Dispatch an event to a hit test result's path.
|
||||
/// Dispatch an event to the targets found by a hit test on its position.
|
||||
///
|
||||
/// Apart from forwarding the event to [GestureBinding.dispatchEvent],
|
||||
/// This also paint all events that's down on the screen.
|
||||
@override
|
||||
void dispatchEvent(
|
||||
PointerEvent event,
|
||||
HitTestResult hitTestResult, {
|
||||
void handlePointerEvent(
|
||||
PointerEvent event, {
|
||||
TestBindingEventSource source = TestBindingEventSource.device,
|
||||
}) {
|
||||
switch (source) {
|
||||
@@ -1510,11 +1516,23 @@ class LiveTestWidgetsFlutterBinding extends TestWidgetsFlutterBinding {
|
||||
);
|
||||
_handleViewNeedsPaint();
|
||||
}
|
||||
super.dispatchEvent(event, hitTestResult, source: source);
|
||||
super.handlePointerEvent(event, source: TestBindingEventSource.test);
|
||||
break;
|
||||
case TestBindingEventSource.device:
|
||||
if (deviceEventDispatcher != null)
|
||||
deviceEventDispatcher.dispatchEvent(event, hitTestResult);
|
||||
super.handlePointerEvent(event, source: TestBindingEventSource.device);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
void dispatchEvent(PointerEvent event, HitTestResult hitTestResult) {
|
||||
switch (_pointerEventSource) {
|
||||
case TestBindingEventSource.test:
|
||||
super.dispatchEvent(event, hitTestResult);
|
||||
break;
|
||||
case TestBindingEventSource.device:
|
||||
deviceEventDispatcher.dispatchEvent(event, hitTestResult);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -378,27 +378,26 @@ abstract class WidgetController {
|
||||
assert(speed > 0.0); // speed is pixels/second
|
||||
return TestAsyncUtils.guard<void>(() async {
|
||||
final TestPointer testPointer = TestPointer(pointer ?? _getNextPointer(), PointerDeviceKind.touch, null, buttons);
|
||||
final HitTestResult result = hitTestOnBinding(startLocation);
|
||||
const int kMoveCount = 50; // Needs to be >= kHistorySize, see _LeastSquaresVelocityTrackerStrategy
|
||||
final double timeStampDelta = 1000000.0 * offset.distance / (kMoveCount * speed);
|
||||
double timeStamp = 0.0;
|
||||
double lastTimeStamp = timeStamp;
|
||||
await sendEventToBinding(testPointer.down(startLocation, timeStamp: Duration(microseconds: timeStamp.round())), result);
|
||||
await sendEventToBinding(testPointer.down(startLocation, timeStamp: Duration(microseconds: timeStamp.round())));
|
||||
if (initialOffset.distance > 0.0) {
|
||||
await sendEventToBinding(testPointer.move(startLocation + initialOffset, timeStamp: Duration(microseconds: timeStamp.round())), result);
|
||||
await sendEventToBinding(testPointer.move(startLocation + initialOffset, timeStamp: Duration(microseconds: timeStamp.round())));
|
||||
timeStamp += initialOffsetDelay.inMicroseconds;
|
||||
await pump(initialOffsetDelay);
|
||||
}
|
||||
for (int i = 0; i <= kMoveCount; i += 1) {
|
||||
final Offset location = startLocation + initialOffset + Offset.lerp(Offset.zero, offset, i / kMoveCount);
|
||||
await sendEventToBinding(testPointer.move(location, timeStamp: Duration(microseconds: timeStamp.round())), result);
|
||||
await sendEventToBinding(testPointer.move(location, timeStamp: Duration(microseconds: timeStamp.round())));
|
||||
timeStamp += timeStampDelta;
|
||||
if (timeStamp - lastTimeStamp > frameInterval.inMicroseconds) {
|
||||
await pump(Duration(microseconds: (timeStamp - lastTimeStamp).truncate()));
|
||||
lastTimeStamp = timeStamp;
|
||||
}
|
||||
}
|
||||
await sendEventToBinding(testPointer.up(timeStamp: Duration(microseconds: timeStamp.round())), result);
|
||||
await sendEventToBinding(testPointer.up(timeStamp: Duration(microseconds: timeStamp.round())));
|
||||
});
|
||||
}
|
||||
|
||||
@@ -739,7 +738,6 @@ abstract class WidgetController {
|
||||
int buttons = kPrimaryButton,
|
||||
}) async {
|
||||
return TestGesture(
|
||||
hitTester: hitTestOnBinding,
|
||||
dispatcher: sendEventToBinding,
|
||||
kind: kind,
|
||||
pointer: pointer ?? _getNextPointer(),
|
||||
@@ -777,9 +775,9 @@ abstract class WidgetController {
|
||||
}
|
||||
|
||||
/// Forwards the given pointer event to the binding.
|
||||
Future<void> sendEventToBinding(PointerEvent event, HitTestResult result) {
|
||||
Future<void> sendEventToBinding(PointerEvent event) {
|
||||
return TestAsyncUtils.guard<void>(() async {
|
||||
binding.dispatchEvent(event, result);
|
||||
binding.handlePointerEvent(event);
|
||||
});
|
||||
}
|
||||
|
||||
@@ -1087,9 +1085,7 @@ class LiveWidgetController extends WidgetController {
|
||||
// processing of the events.
|
||||
// Flush all past events
|
||||
handleTimeStampDiff.add(-timeDiff);
|
||||
for (final PointerEvent event in record.events) {
|
||||
_handlePointerEvent(event, hitTestHistory);
|
||||
}
|
||||
record.events.forEach(binding.handlePointerEvent);
|
||||
} else {
|
||||
await Future<void>.delayed(timeDiff);
|
||||
handleTimeStampDiff.add(
|
||||
@@ -1098,9 +1094,7 @@ class LiveWidgetController extends WidgetController {
|
||||
// fake async this new diff should be zero.
|
||||
clock.now().difference(startTime) - record.timeDelay,
|
||||
);
|
||||
for (final PointerEvent event in record.events) {
|
||||
_handlePointerEvent(event, hitTestHistory);
|
||||
}
|
||||
record.events.forEach(binding.handlePointerEvent);
|
||||
}
|
||||
}
|
||||
// This makes sure that a gesture is completed, with no more pointers
|
||||
@@ -1109,46 +1103,4 @@ class LiveWidgetController extends WidgetController {
|
||||
return handleTimeStampDiff;
|
||||
});
|
||||
}
|
||||
|
||||
// This method is almost identical to [GestureBinding._handlePointerEvent]
|
||||
// to replicate the behavior of the real binding.
|
||||
void _handlePointerEvent(
|
||||
PointerEvent event,
|
||||
Map<int, HitTestResult> _hitTests
|
||||
) {
|
||||
HitTestResult hitTestResult;
|
||||
if (event is PointerDownEvent || event is PointerSignalEvent) {
|
||||
assert(!_hitTests.containsKey(event.pointer));
|
||||
hitTestResult = HitTestResult();
|
||||
binding.hitTest(hitTestResult, event.position);
|
||||
if (event is PointerDownEvent) {
|
||||
_hitTests[event.pointer] = hitTestResult;
|
||||
}
|
||||
assert(() {
|
||||
if (debugPrintHitTestResults)
|
||||
debugPrint('$event: $hitTestResult');
|
||||
return true;
|
||||
}());
|
||||
} else if (event is PointerUpEvent || event is PointerCancelEvent) {
|
||||
hitTestResult = _hitTests.remove(event.pointer);
|
||||
} else if (event.down) {
|
||||
// Because events that occur with the pointer down (like
|
||||
// PointerMoveEvents) should be dispatched to the same place that their
|
||||
// initial PointerDownEvent was, we want to re-use the path we found when
|
||||
// the pointer went down, rather than do hit detection each time we get
|
||||
// such an event.
|
||||
hitTestResult = _hitTests[event.pointer];
|
||||
}
|
||||
assert(() {
|
||||
if (debugPrintMouseHoverEvents && event is PointerHoverEvent)
|
||||
debugPrint('$event');
|
||||
return true;
|
||||
}());
|
||||
if (hitTestResult != null ||
|
||||
event is PointerHoverEvent ||
|
||||
event is PointerAddedEvent ||
|
||||
event is PointerRemovedEvent) {
|
||||
binding.dispatchEvent(event, hitTestResult);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -295,7 +295,7 @@ class TestPointer {
|
||||
|
||||
/// Signature for a callback that can dispatch events and returns a future that
|
||||
/// completes when the event dispatch is complete.
|
||||
typedef EventDispatcher = Future<void> Function(PointerEvent event, HitTestResult result);
|
||||
typedef EventDispatcher = Future<void> Function(PointerEvent event);
|
||||
|
||||
/// Signature for callbacks that perform hit-testing at a given location.
|
||||
typedef HitTester = HitTestResult Function(Offset location);
|
||||
@@ -324,27 +324,22 @@ class TestGesture {
|
||||
/// arguments are required.
|
||||
TestGesture({
|
||||
@required EventDispatcher dispatcher,
|
||||
@required HitTester hitTester,
|
||||
int pointer = 1,
|
||||
PointerDeviceKind kind = PointerDeviceKind.touch,
|
||||
int device,
|
||||
int buttons = kPrimaryButton,
|
||||
}) : assert(dispatcher != null),
|
||||
assert(hitTester != null),
|
||||
assert(pointer != null),
|
||||
assert(kind != null),
|
||||
assert(buttons != null),
|
||||
_dispatcher = dispatcher,
|
||||
_hitTester = hitTester,
|
||||
_pointer = TestPointer(pointer, kind, device, buttons),
|
||||
_result = null;
|
||||
_pointer = TestPointer(pointer, kind, device, buttons);
|
||||
|
||||
/// Dispatch a pointer down event at the given `downLocation`, caching the
|
||||
/// hit test result.
|
||||
Future<void> down(Offset downLocation, { Duration timeStamp = Duration.zero }) async {
|
||||
return TestAsyncUtils.guard<void>(() async {
|
||||
_result = _hitTester(downLocation);
|
||||
return _dispatcher(_pointer.down(downLocation, timeStamp: timeStamp), _result);
|
||||
return _dispatcher(_pointer.down(downLocation, timeStamp: timeStamp));
|
||||
});
|
||||
}
|
||||
|
||||
@@ -353,36 +348,33 @@ class TestGesture {
|
||||
Future<void> downWithCustomEvent(Offset downLocation, PointerDownEvent event) async {
|
||||
_pointer.setDownInfo(event, downLocation);
|
||||
return TestAsyncUtils.guard<void>(() async {
|
||||
_result = _hitTester(downLocation);
|
||||
return _dispatcher(event, _result);
|
||||
return _dispatcher(event);
|
||||
});
|
||||
}
|
||||
|
||||
final EventDispatcher _dispatcher;
|
||||
final HitTester _hitTester;
|
||||
final TestPointer _pointer;
|
||||
HitTestResult _result;
|
||||
|
||||
/// 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>(() {
|
||||
return _dispatcher(event, _result);
|
||||
return _dispatcher(event);
|
||||
});
|
||||
}
|
||||
|
||||
/// In a test, send a pointer add event for this pointer.
|
||||
Future<void> addPointer({ Duration timeStamp = Duration.zero, Offset location }) {
|
||||
return TestAsyncUtils.guard<void>(() {
|
||||
return _dispatcher(_pointer.addPointer(timeStamp: timeStamp, location: location ?? _pointer.location), null);
|
||||
return _dispatcher(_pointer.addPointer(timeStamp: timeStamp, location: location ?? _pointer.location));
|
||||
});
|
||||
}
|
||||
|
||||
/// In a test, send a pointer remove event for this pointer.
|
||||
Future<void> removePointer({ Duration timeStamp = Duration.zero, Offset location }) {
|
||||
return TestAsyncUtils.guard<void>(() {
|
||||
return _dispatcher(_pointer.removePointer(timeStamp: timeStamp, location: location ?? _pointer.location), null);
|
||||
return _dispatcher(_pointer.removePointer(timeStamp: timeStamp, location: location ?? _pointer.location));
|
||||
});
|
||||
}
|
||||
|
||||
@@ -403,14 +395,11 @@ class TestGesture {
|
||||
Future<void> moveTo(Offset location, { Duration timeStamp = Duration.zero }) {
|
||||
return TestAsyncUtils.guard<void>(() {
|
||||
if (_pointer._isDown) {
|
||||
assert(_result != null,
|
||||
'Move events with the pointer down must be preceded by a down '
|
||||
'event that captures a hit test result.');
|
||||
return _dispatcher(_pointer.move(location, timeStamp: timeStamp), _result);
|
||||
return _dispatcher(_pointer.move(location, timeStamp: timeStamp));
|
||||
} 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);
|
||||
return _dispatcher(_pointer.hover(location, timeStamp: timeStamp));
|
||||
}
|
||||
});
|
||||
}
|
||||
@@ -419,9 +408,8 @@ class TestGesture {
|
||||
Future<void> up({ Duration timeStamp = Duration.zero }) {
|
||||
return TestAsyncUtils.guard<void>(() async {
|
||||
assert(_pointer._isDown);
|
||||
await _dispatcher(_pointer.up(timeStamp: timeStamp), _result);
|
||||
await _dispatcher(_pointer.up(timeStamp: timeStamp));
|
||||
assert(!_pointer._isDown);
|
||||
_result = null;
|
||||
});
|
||||
}
|
||||
|
||||
@@ -431,9 +419,8 @@ class TestGesture {
|
||||
Future<void> cancel({ Duration timeStamp = Duration.zero }) {
|
||||
return TestAsyncUtils.guard<void>(() async {
|
||||
assert(_pointer._isDown);
|
||||
await _dispatcher(_pointer.cancel(timeStamp: timeStamp), _result);
|
||||
await _dispatcher(_pointer.cancel(timeStamp: timeStamp));
|
||||
assert(!_pointer._isDown);
|
||||
_result = null;
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
@@ -543,7 +543,7 @@ class WidgetTester extends WidgetController implements HitTestDispatcher, Ticker
|
||||
// Flush all past events
|
||||
handleTimeStampDiff.add(-timeDiff);
|
||||
for (final PointerEvent event in record.events) {
|
||||
_handlePointerEvent(event, hitTestHistory);
|
||||
binding.handlePointerEvent(event, source: TestBindingEventSource.test);
|
||||
}
|
||||
} else {
|
||||
// TODO(CareF): reconsider the pumping strategy after
|
||||
@@ -554,7 +554,7 @@ class WidgetTester extends WidgetController implements HitTestDispatcher, Ticker
|
||||
binding.clock.now().difference(startTime) - record.timeDelay,
|
||||
);
|
||||
for (final PointerEvent event in record.events) {
|
||||
_handlePointerEvent(event, hitTestHistory);
|
||||
binding.handlePointerEvent(event, source: TestBindingEventSource.test);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -566,48 +566,6 @@ class WidgetTester extends WidgetController implements HitTestDispatcher, Ticker
|
||||
});
|
||||
}
|
||||
|
||||
// This is a parallel implementation of [GestureBinding._handlePointerEvent]
|
||||
// to make compatible with test bindings.
|
||||
void _handlePointerEvent(
|
||||
PointerEvent event,
|
||||
Map<int, HitTestResult> _hitTests
|
||||
) {
|
||||
HitTestResult hitTestResult;
|
||||
if (event is PointerDownEvent || event is PointerSignalEvent) {
|
||||
assert(!_hitTests.containsKey(event.pointer));
|
||||
hitTestResult = HitTestResult();
|
||||
binding.hitTest(hitTestResult, event.position);
|
||||
if (event is PointerDownEvent) {
|
||||
_hitTests[event.pointer] = hitTestResult;
|
||||
}
|
||||
assert(() {
|
||||
if (debugPrintHitTestResults)
|
||||
debugPrint('$event: $hitTestResult');
|
||||
return true;
|
||||
}());
|
||||
} else if (event is PointerUpEvent || event is PointerCancelEvent) {
|
||||
hitTestResult = _hitTests.remove(event.pointer);
|
||||
} else if (event.down) {
|
||||
// Because events that occur with the pointer down (like
|
||||
// PointerMoveEvents) should be dispatched to the same place that their
|
||||
// initial PointerDownEvent was, we want to re-use the path we found when
|
||||
// the pointer went down, rather than do hit detection each time we get
|
||||
// such an event.
|
||||
hitTestResult = _hitTests[event.pointer];
|
||||
}
|
||||
assert(() {
|
||||
if (debugPrintMouseHoverEvents && event is PointerHoverEvent)
|
||||
debugPrint('$event');
|
||||
return true;
|
||||
}());
|
||||
if (hitTestResult != null ||
|
||||
event is PointerHoverEvent ||
|
||||
event is PointerAddedEvent ||
|
||||
event is PointerRemovedEvent) {
|
||||
binding.dispatchEvent(event, hitTestResult, source: TestBindingEventSource.test);
|
||||
}
|
||||
}
|
||||
|
||||
/// Triggers a frame after `duration` amount of time.
|
||||
///
|
||||
/// This makes the framework act as if the application had janked (missed
|
||||
@@ -824,9 +782,9 @@ class WidgetTester extends WidgetController implements HitTestDispatcher, Ticker
|
||||
}
|
||||
|
||||
@override
|
||||
Future<void> sendEventToBinding(PointerEvent event, HitTestResult result) {
|
||||
Future<void> sendEventToBinding(PointerEvent event) {
|
||||
return TestAsyncUtils.guard<void>(() async {
|
||||
binding.dispatchEvent(event, result, source: TestBindingEventSource.test);
|
||||
binding.handlePointerEvent(event, source: TestBindingEventSource.test);
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user