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