Improve our scroll physics on iOS (#5340)

Changes in this patch:
- iOS now uses a different scrollDrag constant than Android.
   - ScrollConfigurationDelegate now knows about target platforms.
   - ScrollBehaviors now know about target platforms.
   - RawInputLine now has to be told what platform it's targetting.
   - PageableList now has a concept of target platform.
- make debugPrintStack filter its stack.
   - move debugPrintStack to `assertions.dart`.
- add support for limiting the number of frames to debugPrintStack.
- make defaultTargetPlatform default to android in test environments.
- remove OverscrollStyle and MaterialApp's overscrollStyle argument. You
  can now control the overscroll style using Theme.platform.
- the default scroll configuration is now private to avoid people
  relying on the defaultTargetPlatform getter in their subclasses (since
  they really should use Theme.of(context).platform).
- fix some typos I noticed in some tests.
- added a test for flinging scrollables, that checks that the behavior
  differs on the two target platforms.
- made flingFrom and fling in the test API pump the frames.
- added more docs to the test API.
- made the TestAsyncUtils.guard() method report uncaught errors to help
  debug errors when using that API.
This commit is contained in:
Ian Hickson
2016-08-11 11:31:30 -07:00
committed by GitHub
parent cf2b2078d4
commit 17cdc88958
19 changed files with 397 additions and 110 deletions

View File

@@ -264,13 +264,29 @@ class WidgetController {
///
/// If the middle of the widget is not exposed, this might send
/// events to another object.
Future<Null> fling(Finder finder, Offset offset, double velocity, { int pointer: 1 }) {
return flingFrom(getCenter(finder), offset, velocity, pointer: pointer);
///
/// This can pump frames. See [flingFrom] for a discussion of how the
/// `offset`, `velocity` and `frameInterval` arguments affect this.
Future<Null> fling(Finder finder, Offset offset, double velocity, { int pointer: 1, Duration frameInterval: const Duration(milliseconds: 16) }) {
return flingFrom(getCenter(finder), offset, velocity, pointer: pointer, frameInterval: frameInterval);
}
/// Attempts a fling gesture starting from the given location,
/// moving the given distance, reaching the given velocity.
Future<Null> flingFrom(Point startLocation, Offset offset, double velocity, { int pointer: 1 }) {
///
/// Exactly 50 pointer events are synthesized.
///
/// The offset and velocity control the interval between each pointer event.
/// For example, if the offset is 200 pixels, and the velocity is 800 pixels
/// per second, the pointer events will be sent for each increment of 4 pixels
/// (200/50), over 250ms (200/800), meaning events will be sent every 1.25ms
/// (250/200).
///
/// To make tests more realistic, frames may be pumped during this time (using
/// calls to [pump]). If the total duration is longer than `frameInterval`,
/// then one frame is pumped each time that amount of time elapses while
/// sending events, or each time an event is synthesised, whichever is rarer.
Future<Null> flingFrom(Point startLocation, Offset offset, double velocity, { int pointer: 1, Duration frameInterval: const Duration(milliseconds: 16) }) {
return TestAsyncUtils.guard(() async {
assert(offset.distance > 0.0);
assert(velocity != 0.0); // velocity is pixels/second
@@ -279,17 +295,32 @@ class WidgetController {
const int kMoveCount = 50; // Needs to be >= kHistorySize, see _LeastSquaresVelocityTrackerStrategy
final double timeStampDelta = 1000.0 * offset.distance / (kMoveCount * velocity);
double timeStamp = 0.0;
double lastTimeStamp = timeStamp;
await sendEventToBinding(p.down(startLocation, timeStamp: new Duration(milliseconds: timeStamp.round())), result);
for (int i = 0; i <= kMoveCount; i++) {
for (int i = 0; i <= kMoveCount; i += 1) {
final Point location = startLocation + Offset.lerp(Offset.zero, offset, i / kMoveCount);
await sendEventToBinding(p.move(location, timeStamp: new Duration(milliseconds: timeStamp.round())), result);
timeStamp += timeStampDelta;
if (timeStamp - lastTimeStamp > frameInterval.inMilliseconds) {
await pump(new Duration(milliseconds: (timeStamp - lastTimeStamp).truncate()));
lastTimeStamp = timeStamp;
}
}
await sendEventToBinding(p.up(timeStamp: new Duration(milliseconds: timeStamp.round())), result);
return null;
});
}
/// Called to indicate that time should advance.
///
/// This is invoked by [flingFrom], for instance, so that the sequence of
/// pointer events occurs over time.
///
/// The default implementation does nothing.
///
/// The [WidgetTester] subclass implements this by deferring to the [binding].
Future<Null> pump(Duration duration) => new Future<Null>.value(null);
/// Attempts to drag the given widget by the given offset, by
/// starting a drag in the middle of the widget.
///

View File

@@ -67,7 +67,7 @@ class TestAsyncUtils {
_AsyncScope scope = new _AsyncScope(StackTrace.current, zone);
_scopeStack.add(scope);
Future<Null> result = scope.zone.run(body);
result = result.whenComplete(() {
void completionHandler(dynamic error, StackTrace stack) {
assert(_scopeStack.isNotEmpty);
assert(_scopeStack.contains(scope));
bool leaked = false;
@@ -77,8 +77,10 @@ class TestAsyncUtils {
closedScope = _scopeStack.removeLast();
if (closedScope == scope)
break;
leaked = true;
message.writeln('Asynchronous call to guarded function leaked. You must use "await" with all Future-returning test APIs.');
if (!leaked) {
message.writeln('Asynchronous call to guarded function leaked. You must use "await" with all Future-returning test APIs.');
leaked = true;
}
final _StackEntry originalGuarder = _findResponsibleMethod(closedScope.creationStack, 'guard', message);
if (originalGuarder != null) {
message.writeln(
@@ -90,10 +92,20 @@ class TestAsyncUtils {
);
}
}
if (leaked)
if (leaked) {
if (error != null) {
message.writeln('An uncaught exception may have caused the guarded function leak. The exception was:');
message.writeln('$error');
message.writeln('The stack trace associated with this exception was:');
FlutterError.defaultStackFilter(stack.toString().trimRight().split('\n')).forEach(message.writeln);
}
throw new FlutterError(message.toString().trimRight());
});
return result;
}
}
return result.then(
(Null value) => completionHandler(null, null),
onError: completionHandler
);
}
static Zone get _currentScopeZone {

View File

@@ -36,9 +36,9 @@ typedef Future<Null> WidgetTesterCallback(WidgetTester widgetTester);
///
/// Example:
///
/// testWidgets('MyWidget', (WidgetTester tester) {
/// tester.pumpWidget(new MyWidget());
/// tester.tap(find.text('Save'));
/// testWidgets('MyWidget', (WidgetTester tester) async {
/// await tester.pumpWidget(new MyWidget());
/// await tester.tap(find.text('Save'));
/// expect(tester, hasWidget(find.text('Success')));
/// });
void testWidgets(String description, WidgetTesterCallback callback, {
@@ -77,12 +77,12 @@ void testWidgets(String description, WidgetTesterCallback callback, {
///
/// main() async {
/// assert(false); // fail in checked mode
/// await benchmarkWidgets((WidgetTester tester) {
/// tester.pumpWidget(new MyWidget());
/// await benchmarkWidgets((WidgetTester tester) async {
/// await tester.pumpWidget(new MyWidget());
/// final Stopwatch timer = new Stopwatch()..start();
/// for (int index = 0; index < 10000; index += 1) {
/// tester.tap(find.text('Tap me'));
/// tester.pump();
/// await tester.tap(find.text('Tap me'));
/// await tester.pump();
/// }
/// timer.stop();
/// debugPrint('Time taken: ${timer.elapsedMilliseconds}ms');
@@ -99,7 +99,7 @@ Future<Null> benchmarkWidgets(WidgetTesterCallback callback) {
print('│ enabled will not accurately reflect the performance │');
print('│ that will be experienced by end users using release ╎');
print('│ builds. Benchmarks should be run using this command ┆');
print('│ line: flutter run --release -t benchmark.dart ┊');
print('│ line: flutter run --release benchmark.dart ┊');
print('│ ');
print('└─────────────────────────────────────────────────╌┄┈ 🐢');
return true;
@@ -172,6 +172,7 @@ class WidgetTester extends WidgetController implements HitTestDispatcher {
///
/// This is a convenience function that just calls
/// [TestWidgetsFlutterBinding.pump].
@override
Future<Null> pump([
Duration duration,
EnginePhase phase = EnginePhase.sendSemanticsTree