Commit 63964f48 authored by Adam Barth's avatar Adam Barth Committed by GitHub

Port ScrollNotification tests (#8057)

These now test ScrollNotification2.
parent 67462b43
...@@ -6,123 +6,132 @@ import 'dart:async'; ...@@ -6,123 +6,132 @@ import 'dart:async';
import 'package:flutter_test/flutter_test.dart'; import 'package:flutter_test/flutter_test.dart';
import 'package:flutter/widgets.dart'; import 'package:flutter/widgets.dart';
import 'package:meta/meta.dart';
Widget _buildScroller({ List<String> log }) { Widget _buildScroller({ List<String> log }) {
return new ScrollableViewport( return new NotificationListener<ScrollNotification2>(
onScrollStart: (double scrollOffset) { onNotification: (ScrollNotification2 notification) {
log.add('scrollstart'); if (notification is ScrollStartNotification) {
log.add('scroll-start');
} else if (notification is ScrollUpdateNotification) {
log.add('scroll-update');
} else if (notification is ScrollEndNotification) {
log.add('scroll-end');
}
return false;
}, },
onScroll: (double scrollOffset) { child: new SingleChildScrollView(
log.add('scroll'); child: new Container(width: 1000.0, height: 1000.0),
}, ),
onScrollEnd: (double scrollOffset) {
log.add('scrollend');
},
child: new Container(width: 1000.0, height: 1000.0)
); );
} }
void main() { void main() {
Completer<Null> scrollTo(WidgetTester tester, double newScrollOffset, { Duration duration }) { Completer<Null> animateTo(WidgetTester tester, double newScrollOffset, { @required Duration duration }) {
Completer<Null> completer = new Completer<Null>(); Completer<Null> completer = new Completer<Null>();
final ScrollableState scrollable = tester.state(find.byType(Scrollable)); final Scrollable2State scrollable = tester.state(find.byType(Scrollable2));
scrollable.scrollTo(newScrollOffset, duration: duration).whenComplete(completer.complete); scrollable.position.animateTo(newScrollOffset, duration: duration, curve: Curves.linear).whenComplete(completer.complete);
return completer; return completer;
} }
void jumpTo(WidgetTester tester, double newScrollOffset) {
final Scrollable2State scrollable = tester.state(find.byType(Scrollable2));
scrollable.position.jumpTo(newScrollOffset);
}
testWidgets('Scroll event drag', (WidgetTester tester) async { testWidgets('Scroll event drag', (WidgetTester tester) async {
List<String> log = <String>[]; List<String> log = <String>[];
await tester.pumpWidget(_buildScroller(log: log)); await tester.pumpWidget(_buildScroller(log: log));
expect(log, equals(<String>[])); expect(log, equals(<String>[]));
TestGesture gesture = await tester.startGesture(const Point(100.0, 100.0)); TestGesture gesture = await tester.startGesture(const Point(100.0, 100.0));
expect(log, equals(<String>['scrollstart'])); expect(log, equals(<String>['scroll-start']));
await tester.pump(const Duration(seconds: 1)); await tester.pump(const Duration(seconds: 1));
expect(log, equals(<String>['scrollstart'])); expect(log, equals(<String>['scroll-start']));
await gesture.moveBy(const Offset(-10.0, -10.0)); await gesture.moveBy(const Offset(-10.0, -10.0));
expect(log, equals(<String>['scrollstart', 'scroll'])); expect(log, equals(<String>['scroll-start', 'scroll-update']));
await tester.pump(const Duration(seconds: 1)); await tester.pump(const Duration(seconds: 1));
expect(log, equals(<String>['scrollstart', 'scroll'])); expect(log, equals(<String>['scroll-start', 'scroll-update']));
await gesture.up(); await gesture.up();
expect(log, equals(<String>['scrollstart', 'scroll'])); expect(log, equals(<String>['scroll-start', 'scroll-update', 'scroll-end']));
await tester.pump(const Duration(seconds: 1)); await tester.pump(const Duration(seconds: 1));
expect(log, equals(<String>['scrollstart', 'scroll', 'scrollend'])); expect(log, equals(<String>['scroll-start', 'scroll-update', 'scroll-end']));
}); });
testWidgets('Scroll scrollTo animation', (WidgetTester tester) async { testWidgets('Scroll animateTo', (WidgetTester tester) async {
List<String> log = <String>[]; List<String> log = <String>[];
await tester.pumpWidget(_buildScroller(log: log)); await tester.pumpWidget(_buildScroller(log: log));
expect(log, equals(<String>[])); expect(log, equals(<String>[]));
Completer<Null> completer = scrollTo(tester, 100.0, duration: const Duration(seconds: 1)); Completer<Null> completer = animateTo(tester, 100.0, duration: const Duration(seconds: 1));
expect(completer.isCompleted, isFalse); expect(completer.isCompleted, isFalse);
expect(log, equals(<String>['scrollstart'])); expect(log, equals(<String>['scroll-start']));
await tester.pump(const Duration(milliseconds: 100)); await tester.pump(const Duration(milliseconds: 100));
expect(log, equals(<String>['scrollstart'])); expect(log, equals(<String>['scroll-start']));
await tester.pump(const Duration(milliseconds: 100)); await tester.pump(const Duration(milliseconds: 100));
expect(log, equals(<String>['scrollstart', 'scroll'])); expect(log, equals(<String>['scroll-start', 'scroll-update']));
await tester.pump(const Duration(milliseconds: 1500)); await tester.pump(const Duration(milliseconds: 1500));
expect(log, equals(<String>['scrollstart', 'scroll', 'scroll', 'scrollend'])); expect(log, equals(<String>['scroll-start', 'scroll-update', 'scroll-update', 'scroll-end']));
expect(completer.isCompleted, isTrue); expect(completer.isCompleted, isTrue);
}); });
testWidgets('Scroll scrollTo no animation', (WidgetTester tester) async { testWidgets('Scroll jumpTo', (WidgetTester tester) async {
List<String> log = <String>[]; List<String> log = <String>[];
await tester.pumpWidget(_buildScroller(log: log)); await tester.pumpWidget(_buildScroller(log: log));
expect(log, equals(<String>[])); expect(log, equals(<String>[]));
Completer<Null> completer = scrollTo(tester, 100.0); jumpTo(tester, 100.0);
expect(completer.isCompleted, isFalse); expect(log, equals(<String>['scroll-start', 'scroll-update', 'scroll-end']));
expect(log, equals(<String>['scrollstart', 'scroll', 'scrollend']));
await tester.pump(); await tester.pump();
expect(completer.isCompleted, isTrue); expect(log, equals(<String>['scroll-start', 'scroll-update', 'scroll-end']));
}); });
testWidgets('Scroll during animation', (WidgetTester tester) async { testWidgets('Scroll jumpTo during animation', (WidgetTester tester) async {
List<String> log = <String>[]; List<String> log = <String>[];
await tester.pumpWidget(_buildScroller(log: log)); await tester.pumpWidget(_buildScroller(log: log));
expect(log, equals(<String>[])); expect(log, equals(<String>[]));
Completer<Null> completer = scrollTo(tester, 100.0, duration: const Duration(seconds: 1)); Completer<Null> completer = animateTo(tester, 100.0, duration: const Duration(seconds: 1));
expect(completer.isCompleted, isFalse); expect(completer.isCompleted, isFalse);
expect(log, equals(<String>['scrollstart'])); expect(log, equals(<String>['scroll-start']));
await tester.pump(const Duration(milliseconds: 100)); await tester.pump(const Duration(milliseconds: 100));
expect(log, equals(<String>['scrollstart'])); expect(log, equals(<String>['scroll-start']));
await tester.pump(const Duration(milliseconds: 100)); await tester.pump(const Duration(milliseconds: 100));
expect(log, equals(<String>['scrollstart', 'scroll'])); expect(log, equals(<String>['scroll-start', 'scroll-update']));
expect(completer.isCompleted, isFalse); expect(completer.isCompleted, isFalse);
completer = scrollTo(tester, 100.0); jumpTo(tester, 100.0);
expect(completer.isCompleted, isFalse); expect(completer.isCompleted, isFalse);
expect(log, equals(<String>['scrollstart', 'scroll', 'scroll'])); expect(log, equals(<String>['scroll-start', 'scroll-update', 'scroll-end', 'scroll-start', 'scroll-update', 'scroll-end']));
await tester.pump(const Duration(milliseconds: 100)); await tester.pump(const Duration(milliseconds: 100));
expect(log, equals(<String>['scrollstart', 'scroll', 'scroll', 'scrollend'])); expect(log, equals(<String>['scroll-start', 'scroll-update', 'scroll-end', 'scroll-start', 'scroll-update', 'scroll-end']));
expect(completer.isCompleted, isTrue);
await tester.pump(const Duration(milliseconds: 1500)); await tester.pump(const Duration(milliseconds: 1500));
expect(log, equals(<String>['scrollstart', 'scroll', 'scroll', 'scrollend'])); expect(log, equals(<String>['scroll-start', 'scroll-update', 'scroll-end', 'scroll-start', 'scroll-update', 'scroll-end']));
expect(completer.isCompleted, isTrue); expect(completer.isCompleted, isTrue);
}); });
testWidgets('Scroll during animation', (WidgetTester tester) async { testWidgets('Scroll scrollTo during animation', (WidgetTester tester) async {
List<String> log = <String>[]; List<String> log = <String>[];
await tester.pumpWidget(_buildScroller(log: log)); await tester.pumpWidget(_buildScroller(log: log));
expect(log, equals(<String>[])); expect(log, equals(<String>[]));
Completer<Null> completer = scrollTo(tester, 100.0, duration: const Duration(seconds: 1)); Completer<Null> completer = animateTo(tester, 100.0, duration: const Duration(seconds: 1));
expect(completer.isCompleted, isFalse); expect(completer.isCompleted, isFalse);
expect(log, equals(<String>['scrollstart'])); expect(log, equals(<String>['scroll-start']));
await tester.pump(const Duration(milliseconds: 100)); await tester.pump(const Duration(milliseconds: 100));
expect(log, equals(<String>['scrollstart'])); expect(log, equals(<String>['scroll-start']));
await tester.pump(const Duration(milliseconds: 100)); await tester.pump(const Duration(milliseconds: 100));
expect(log, equals(<String>['scrollstart', 'scroll'])); expect(log, equals(<String>['scroll-start', 'scroll-update']));
expect(completer.isCompleted, isFalse); expect(completer.isCompleted, isFalse);
completer = scrollTo(tester, 100.0, duration: const Duration(seconds: 1)); completer = animateTo(tester, 100.0, duration: const Duration(seconds: 1));
expect(completer.isCompleted, isFalse); expect(completer.isCompleted, isFalse);
expect(log, equals(<String>['scrollstart', 'scroll'])); expect(log, equals(<String>['scroll-start', 'scroll-update']));
await tester.pump(const Duration(milliseconds: 100)); await tester.pump(const Duration(milliseconds: 100));
expect(log, equals(<String>['scrollstart', 'scroll'])); expect(log, equals(<String>['scroll-start', 'scroll-update']));
await tester.pump(const Duration(milliseconds: 1500)); await tester.pump(const Duration(milliseconds: 1500));
expect(log, equals(<String>['scrollstart', 'scroll', 'scroll', 'scrollend'])); expect(log, equals(<String>['scroll-start', 'scroll-update', 'scroll-update', 'scroll-end']));
expect(completer.isCompleted, isTrue); expect(completer.isCompleted, isTrue);
}); });
...@@ -133,15 +142,15 @@ void main() { ...@@ -133,15 +142,15 @@ void main() {
expect(log, equals(<String>[])); expect(log, equals(<String>[]));
await tester.flingFrom(const Point(100.0, 100.0), const Offset(-50.0, -50.0), 500.0); await tester.flingFrom(const Point(100.0, 100.0), const Offset(-50.0, -50.0), 500.0);
await tester.pump(const Duration(seconds: 1)); await tester.pump(const Duration(seconds: 1));
log.removeWhere((String value) => value == 'scroll'); log.removeWhere((String value) => value == 'scroll-update');
expect(log, equals(<String>['scrollstart'])); expect(log, equals(<String>['scroll-start']));
await tester.flingFrom(const Point(100.0, 100.0), const Offset(-50.0, -50.0), 500.0); await tester.flingFrom(const Point(100.0, 100.0), const Offset(-50.0, -50.0), 500.0);
log.removeWhere((String value) => value == 'scroll'); log.removeWhere((String value) => value == 'scroll-update');
expect(log, equals(<String>['scrollstart', 'scrollend', 'scrollstart'])); expect(log, equals(<String>['scroll-start', 'scroll-end', 'scroll-start']));
await tester.pump(const Duration(seconds: 1)); await tester.pump(const Duration(seconds: 1));
await tester.pump(const Duration(seconds: 1)); await tester.pump(const Duration(seconds: 1));
log.removeWhere((String value) => value == 'scroll'); log.removeWhere((String value) => value == 'scroll-update');
expect(log, equals(<String>['scrollstart', 'scrollend', 'scrollstart', 'scrollend'])); expect(log, equals(<String>['scroll-start', 'scroll-end', 'scroll-start', 'scroll-end']));
}); });
testWidgets('fling up ends', (WidgetTester tester) async { testWidgets('fling up ends', (WidgetTester tester) async {
...@@ -153,10 +162,10 @@ void main() { ...@@ -153,10 +162,10 @@ void main() {
await tester.pump(const Duration(seconds: 1)); await tester.pump(const Duration(seconds: 1));
await tester.pump(const Duration(seconds: 1)); await tester.pump(const Duration(seconds: 1));
await tester.pump(const Duration(seconds: 1)); await tester.pump(const Duration(seconds: 1));
expect(log.first, equals('scrollstart')); expect(log.first, equals('scroll-start'));
expect(log.last, equals('scrollend')); expect(log.last, equals('scroll-end'));
log.removeWhere((String value) => value == 'scroll'); log.removeWhere((String value) => value == 'scroll-update');
expect(log.length, equals(2)); expect(log.length, equals(2));
expect(tester.state<ScrollableState>(find.byType(Scrollable)).scrollOffset, equals(0.0)); expect(tester.state<Scrollable2State>(find.byType(Scrollable2)).position.pixels, equals(0.0));
}); });
} }
...@@ -7,71 +7,69 @@ import 'package:flutter/widgets.dart'; ...@@ -7,71 +7,69 @@ import 'package:flutter/widgets.dart';
void main() { void main() {
testWidgets('Scroll notification basics', (WidgetTester tester) async { testWidgets('Scroll notification basics', (WidgetTester tester) async {
ScrollNotification notification; ScrollNotification2 notification;
await tester.pumpWidget(new NotificationListener<ScrollNotification>( await tester.pumpWidget(new NotificationListener<ScrollNotification2>(
onNotification: (ScrollNotification value) { onNotification: (ScrollNotification2 value) {
notification = value; if (value is ScrollStartNotification || value is ScrollUpdateNotification || value is ScrollEndNotification)
notification = value;
return false; return false;
}, },
child: new ScrollableViewport( child: new SingleChildScrollView(
child: const SizedBox(height: 1200.0) child: const SizedBox(height: 1200.0)
) )
)); ));
TestGesture gesture = await tester.startGesture(const Point(100.0, 100.0)); TestGesture gesture = await tester.startGesture(const Point(100.0, 100.0));
await tester.pump(const Duration(seconds: 1)); await tester.pump(const Duration(seconds: 1));
expect(notification.kind, equals(ScrollNotificationKind.started)); expect(notification, const isInstanceOf<ScrollStartNotification>());
expect(notification.depth, equals(0)); expect(notification.depth, equals(1));
expect(notification.dragStartDetails, isNotNull); ScrollStartNotification start = notification;
expect(notification.dragStartDetails.globalPosition, equals(const Point(100.0, 100.0))); expect(start.dragDetails, isNotNull);
expect(notification.dragUpdateDetails, isNull); expect(start.dragDetails.globalPosition, equals(const Point(100.0, 100.0)));
expect(notification.dragEndDetails, isNull);
await gesture.moveBy(const Offset(-10.0, -10.0)); await gesture.moveBy(const Offset(-10.0, -10.0));
await tester.pump(const Duration(seconds: 1)); await tester.pump(const Duration(seconds: 1));
expect(notification.kind, equals(ScrollNotificationKind.updated)); expect(notification, const isInstanceOf<ScrollUpdateNotification>());
expect(notification.depth, equals(0)); expect(notification.depth, equals(1));
expect(notification.dragStartDetails, isNull); ScrollUpdateNotification update = notification;
expect(notification.dragUpdateDetails, isNotNull); expect(update.dragDetails, isNotNull);
expect(notification.dragUpdateDetails.globalPosition, equals(const Point(90.0, 90.0))); expect(update.dragDetails.globalPosition, equals(const Point(90.0, 90.0)));
expect(notification.dragUpdateDetails.delta, equals(const Offset(0.0, -10.0))); expect(update.dragDetails.delta, equals(const Offset(0.0, -10.0)));
expect(notification.dragEndDetails, isNull);
await gesture.up(); await gesture.up();
await tester.pump(const Duration(seconds: 1)); await tester.pump(const Duration(seconds: 1));
expect(notification.kind, equals(ScrollNotificationKind.ended)); expect(notification, const isInstanceOf<ScrollEndNotification>());
expect(notification.depth, equals(0)); expect(notification.depth, equals(1));
expect(notification.dragStartDetails, isNull); ScrollEndNotification end = notification;
expect(notification.dragUpdateDetails, isNull); expect(end.dragDetails, isNotNull);
expect(notification.dragEndDetails, isNotNull); expect(end.dragDetails.velocity, equals(Velocity.zero));
expect(notification.dragEndDetails.velocity, equals(Velocity.zero));
}); });
testWidgets('Scroll notification depth', (WidgetTester tester) async { testWidgets('Scroll notification depth', (WidgetTester tester) async {
final List<ScrollNotificationKind> depth0Kinds = <ScrollNotificationKind>[]; final List<Type> depth0Types = <Type>[];
final List<ScrollNotificationKind> depth1Kinds = <ScrollNotificationKind>[]; final List<Type> depth1Types = <Type>[];
final List<int> depth0Values = <int>[]; final List<int> depth0Values = <int>[];
final List<int> depth1Values = <int>[]; final List<int> depth1Values = <int>[];
await tester.pumpWidget(new NotificationListener<ScrollNotification>( await tester.pumpWidget(new NotificationListener<ScrollNotification2>(
onNotification: (ScrollNotification value) { onNotification: (ScrollNotification2 value) {
depth1Kinds.add(value.kind); depth1Types.add(value.runtimeType);
depth1Values.add(value.depth); depth1Values.add(value.depth);
return false; return false;
}, },
child: new ScrollableViewport( child: new SingleChildScrollView(
child: new SizedBox( child: new SizedBox(
height: 1200.0, height: 1200.0,
child: new NotificationListener<ScrollNotification>( child: new NotificationListener<ScrollNotification2>(
onNotification: (ScrollNotification value) { onNotification: (ScrollNotification2 value) {
depth0Kinds.add(value.kind); depth0Types.add(value.runtimeType);
depth0Values.add(value.depth); depth0Values.add(value.depth);
return false; return false;
}, },
child: new Container( child: new Container(
padding: const EdgeInsets.all(50.0), padding: const EdgeInsets.all(50.0),
child: new ScrollableViewport(child: const SizedBox(height: 1200.0)) child: new SingleChildScrollView(child: const SizedBox(height: 1200.0))
) )
) )
) )
...@@ -85,15 +83,19 @@ void main() { ...@@ -85,15 +83,19 @@ void main() {
await gesture.up(); await gesture.up();
await tester.pump(const Duration(seconds: 1)); await tester.pump(const Duration(seconds: 1));
final List<ScrollNotificationKind> kinds = <ScrollNotificationKind>[ final List<Type> types = <Type>[
ScrollNotificationKind.started, ScrollStartNotification,
ScrollNotificationKind.updated, UserScrollNotification,
ScrollNotificationKind.ended ScrollUpdateNotification,
ScrollEndNotification,
UserScrollNotification,
]; ];
expect(depth0Kinds, equals(kinds)); expect(depth0Types, equals(types));
expect(depth1Kinds, equals(kinds)); expect(depth1Types, equals(types));
expect(depth0Values, equals(<int>[0, 0, 0])); // These values might not be what we want in the end.
expect(depth1Values, equals(<int>[1, 1, 1])); // See <https://github.com/flutter/flutter/issues/8017>.
expect(depth0Values, equals(<int>[1, 1, 1, 1, 1]));
expect(depth1Values, equals(<int>[2, 2, 2, 2, 2]));
}); });
} }
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