Make Scrollable's free scroll initial velocity matches that of iOS (#60501)

This commit is contained in:
LongCatIsLooong
2020-08-12 13:31:05 -07:00
committed by GitHub
parent 54f21ec7aa
commit cea055ef00
11 changed files with 314 additions and 35 deletions

View File

@@ -380,25 +380,25 @@ abstract class WidgetController {
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 = 1000.0 * offset.distance / (kMoveCount * speed);
final double timeStampDelta = 1000000.0 * offset.distance / (kMoveCount * speed);
double timeStamp = 0.0;
double lastTimeStamp = timeStamp;
await sendEventToBinding(testPointer.down(startLocation, timeStamp: Duration(milliseconds: timeStamp.round())), result);
await sendEventToBinding(testPointer.down(startLocation, timeStamp: Duration(microseconds: timeStamp.round())), result);
if (initialOffset.distance > 0.0) {
await sendEventToBinding(testPointer.move(startLocation + initialOffset, timeStamp: Duration(milliseconds: timeStamp.round())), result);
timeStamp += initialOffsetDelay.inMilliseconds;
await sendEventToBinding(testPointer.move(startLocation + initialOffset, timeStamp: Duration(microseconds: timeStamp.round())), result);
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(milliseconds: timeStamp.round())), result);
await sendEventToBinding(testPointer.move(location, timeStamp: Duration(microseconds: timeStamp.round())), result);
timeStamp += timeStampDelta;
if (timeStamp - lastTimeStamp > frameInterval.inMilliseconds) {
await pump(Duration(milliseconds: (timeStamp - lastTimeStamp).truncate()));
if (timeStamp - lastTimeStamp > frameInterval.inMicroseconds) {
await pump(Duration(microseconds: (timeStamp - lastTimeStamp).truncate()));
lastTimeStamp = timeStamp;
}
}
await sendEventToBinding(testPointer.up(timeStamp: Duration(milliseconds: timeStamp.round())), result);
await sendEventToBinding(testPointer.up(timeStamp: Duration(microseconds: timeStamp.round())), result);
});
}

View File

@@ -550,6 +550,33 @@ void main() {
},
);
testWidgets(
'WidgetTester.fling produces strictly monotonically increasing timestamps, '
'when given a large velocity',
(WidgetTester tester) async {
// Velocity trackers may misbehave if the `PointerMoveEvent`s' have the
// same timestamp. This is more likely to happen when the velocity tracker
// has a small sample size.
final List<Duration> logs = <Duration>[];
await tester.pumpWidget(
Directionality(
textDirection: TextDirection.ltr,
child: Listener(
onPointerMove: (PointerMoveEvent event) => logs.add(event.timeStamp),
child: const Text('test'),
),
),
);
await tester.fling(find.text('test'), const Offset(0.0, -50.0), 10000.0);
await tester.pumpAndSettle();
for (int i = 0; i + 1 < logs.length; i += 1) {
expect(logs[i + 1], greaterThan(logs[i]));
}
});
testWidgets(
'ensureVisible: scrolls to make widget visible',
(WidgetTester tester) async {