Unverified Commit 54405bfa authored by chunhtai's avatar chunhtai Committed by GitHub

fixes PointerEventConverter to handle malformed scrolling event (#118124)

* fixes PointerEventConverter to handle malformed scrolling event

* update
parent 458b298f
...@@ -52,7 +52,7 @@ class PointerEventConverter { ...@@ -52,7 +52,7 @@ class PointerEventConverter {
static Iterable<PointerEvent> expand(Iterable<ui.PointerData> data, double devicePixelRatio) { static Iterable<PointerEvent> expand(Iterable<ui.PointerData> data, double devicePixelRatio) {
return data return data
.where((ui.PointerData datum) => datum.signalKind != ui.PointerSignalKind.unknown) .where((ui.PointerData datum) => datum.signalKind != ui.PointerSignalKind.unknown)
.map((ui.PointerData datum) { .map<PointerEvent?>((ui.PointerData datum) {
final Offset position = Offset(datum.physicalX, datum.physicalY) / devicePixelRatio; final Offset position = Offset(datum.physicalX, datum.physicalY) / devicePixelRatio;
final Offset delta = Offset(datum.physicalDeltaX, datum.physicalDeltaY) / devicePixelRatio; final Offset delta = Offset(datum.physicalDeltaX, datum.physicalDeltaY) / devicePixelRatio;
final double radiusMinor = _toLogicalPixels(datum.radiusMinor, devicePixelRatio); final double radiusMinor = _toLogicalPixels(datum.radiusMinor, devicePixelRatio);
...@@ -247,6 +247,9 @@ class PointerEventConverter { ...@@ -247,6 +247,9 @@ class PointerEventConverter {
); );
} }
case ui.PointerSignalKind.scroll: case ui.PointerSignalKind.scroll:
if (!datum.scrollDeltaX.isFinite || !datum.scrollDeltaY.isFinite || devicePixelRatio <= 0) {
return null;
}
final Offset scrollDelta = final Offset scrollDelta =
Offset(datum.scrollDeltaX, datum.scrollDeltaY) / devicePixelRatio; Offset(datum.scrollDeltaX, datum.scrollDeltaY) / devicePixelRatio;
return PointerScrollEvent( return PointerScrollEvent(
...@@ -280,7 +283,7 @@ class PointerEventConverter { ...@@ -280,7 +283,7 @@ class PointerEventConverter {
// enumeration to PointerSignalKind. // enumeration to PointerSignalKind.
throw StateError('Unreachable'); throw StateError('Unreachable');
} }
}); }).whereType<PointerEvent>();
} }
static double _toLogicalPixels(double physicalPixels, double devicePixelRatio) => physicalPixels / devicePixelRatio; static double _toLogicalPixels(double physicalPixels, double devicePixelRatio) => physicalPixels / devicePixelRatio;
......
...@@ -177,6 +177,47 @@ void main() { ...@@ -177,6 +177,47 @@ void main() {
expect(events[4], isA<PointerHoverEvent>()); expect(events[4], isA<PointerHoverEvent>());
}); });
test('Can handle malformed scrolling event.', () {
ui.PointerDataPacket packet = const ui.PointerDataPacket(
data: <ui.PointerData>[
ui.PointerData(change: ui.PointerChange.add, device: 24),
],
);
List<PointerEvent> events = PointerEventConverter.expand(packet.data, GestureBinding.instance.window.devicePixelRatio).toList();
expect(events.length, 1);
expect(events[0], isA<PointerAddedEvent>());
// Send packet contains malformed scroll events.
packet = const ui.PointerDataPacket(
data: <ui.PointerData>[
ui.PointerData(signalKind: ui.PointerSignalKind.scroll, device: 24, scrollDeltaX: double.infinity, scrollDeltaY: 10),
ui.PointerData(signalKind: ui.PointerSignalKind.scroll, device: 24, scrollDeltaX: double.nan, scrollDeltaY: 10),
ui.PointerData(signalKind: ui.PointerSignalKind.scroll, device: 24, scrollDeltaX: double.negativeInfinity, scrollDeltaY: 10),
ui.PointerData(signalKind: ui.PointerSignalKind.scroll, device: 24, scrollDeltaY: double.infinity, scrollDeltaX: 10),
ui.PointerData(signalKind: ui.PointerSignalKind.scroll, device: 24, scrollDeltaY: double.nan, scrollDeltaX: 10),
ui.PointerData(signalKind: ui.PointerSignalKind.scroll, device: 24, scrollDeltaY: double.negativeInfinity, scrollDeltaX: 10),
],
);
events = PointerEventConverter.expand(packet.data, GestureBinding.instance.window.devicePixelRatio).toList();
expect(events.length, 0);
// Send packet with a valid scroll event.
packet = const ui.PointerDataPacket(
data: <ui.PointerData>[
ui.PointerData(signalKind: ui.PointerSignalKind.scroll, device: 24, scrollDeltaX: 10, scrollDeltaY: 10),
],
);
// Make sure PointerEventConverter can expand when device pixel ratio is valid.
events = PointerEventConverter.expand(packet.data, GestureBinding.instance.window.devicePixelRatio).toList();
expect(events.length, 1);
expect(events[0], isA<PointerScrollEvent>());
// Make sure PointerEventConverter returns none when device pixel ratio is invalid.
events = PointerEventConverter.expand(packet.data, 0).toList();
expect(events.length, 0);
});
test('Can expand pointer scroll events', () { test('Can expand pointer scroll events', () {
const ui.PointerDataPacket packet = ui.PointerDataPacket( const ui.PointerDataPacket packet = ui.PointerDataPacket(
data: <ui.PointerData>[ data: <ui.PointerData>[
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment