diff --git a/packages/flutter/test/widgets/scroll_events_test.dart b/packages/flutter/test/widgets/scroll_events_test.dart index cfea1eeea8..c558462756 100644 --- a/packages/flutter/test/widgets/scroll_events_test.dart +++ b/packages/flutter/test/widgets/scroll_events_test.dart @@ -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 log }) { - return new ScrollableViewport( - onScrollStart: (double scrollOffset) { - log.add('scrollstart'); + return new NotificationListener( + 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 scrollTo(WidgetTester tester, double newScrollOffset, { Duration duration }) { + Completer animateTo(WidgetTester tester, double newScrollOffset, { @required Duration duration }) { Completer completer = new Completer(); - 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 log = []; await tester.pumpWidget(_buildScroller(log: log)); expect(log, equals([])); TestGesture gesture = await tester.startGesture(const Point(100.0, 100.0)); - expect(log, equals(['scrollstart'])); + expect(log, equals(['scroll-start'])); await tester.pump(const Duration(seconds: 1)); - expect(log, equals(['scrollstart'])); + expect(log, equals(['scroll-start'])); await gesture.moveBy(const Offset(-10.0, -10.0)); - expect(log, equals(['scrollstart', 'scroll'])); + expect(log, equals(['scroll-start', 'scroll-update'])); await tester.pump(const Duration(seconds: 1)); - expect(log, equals(['scrollstart', 'scroll'])); + expect(log, equals(['scroll-start', 'scroll-update'])); await gesture.up(); - expect(log, equals(['scrollstart', 'scroll'])); + expect(log, equals(['scroll-start', 'scroll-update', 'scroll-end'])); await tester.pump(const Duration(seconds: 1)); - expect(log, equals(['scrollstart', 'scroll', 'scrollend'])); + expect(log, equals(['scroll-start', 'scroll-update', 'scroll-end'])); }); - testWidgets('Scroll scrollTo animation', (WidgetTester tester) async { + testWidgets('Scroll animateTo', (WidgetTester tester) async { List log = []; await tester.pumpWidget(_buildScroller(log: log)); expect(log, equals([])); - Completer completer = scrollTo(tester, 100.0, duration: const Duration(seconds: 1)); + Completer completer = animateTo(tester, 100.0, duration: const Duration(seconds: 1)); expect(completer.isCompleted, isFalse); - expect(log, equals(['scrollstart'])); + expect(log, equals(['scroll-start'])); await tester.pump(const Duration(milliseconds: 100)); - expect(log, equals(['scrollstart'])); + expect(log, equals(['scroll-start'])); await tester.pump(const Duration(milliseconds: 100)); - expect(log, equals(['scrollstart', 'scroll'])); + expect(log, equals(['scroll-start', 'scroll-update'])); await tester.pump(const Duration(milliseconds: 1500)); - expect(log, equals(['scrollstart', 'scroll', 'scroll', 'scrollend'])); + expect(log, equals(['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 log = []; await tester.pumpWidget(_buildScroller(log: log)); expect(log, equals([])); - Completer completer = scrollTo(tester, 100.0); - expect(completer.isCompleted, isFalse); - expect(log, equals(['scrollstart', 'scroll', 'scrollend'])); + jumpTo(tester, 100.0); + expect(log, equals(['scroll-start', 'scroll-update', 'scroll-end'])); await tester.pump(); - expect(completer.isCompleted, isTrue); + expect(log, equals(['scroll-start', 'scroll-update', 'scroll-end'])); }); - testWidgets('Scroll during animation', (WidgetTester tester) async { + testWidgets('Scroll jumpTo during animation', (WidgetTester tester) async { List log = []; await tester.pumpWidget(_buildScroller(log: log)); expect(log, equals([])); - Completer completer = scrollTo(tester, 100.0, duration: const Duration(seconds: 1)); + Completer completer = animateTo(tester, 100.0, duration: const Duration(seconds: 1)); expect(completer.isCompleted, isFalse); - expect(log, equals(['scrollstart'])); + expect(log, equals(['scroll-start'])); await tester.pump(const Duration(milliseconds: 100)); - expect(log, equals(['scrollstart'])); + expect(log, equals(['scroll-start'])); await tester.pump(const Duration(milliseconds: 100)); - expect(log, equals(['scrollstart', 'scroll'])); + expect(log, equals(['scroll-start', 'scroll-update'])); expect(completer.isCompleted, isFalse); - completer = scrollTo(tester, 100.0); + jumpTo(tester, 100.0); expect(completer.isCompleted, isFalse); - expect(log, equals(['scrollstart', 'scroll', 'scroll'])); + expect(log, equals(['scroll-start', 'scroll-update', 'scroll-end', 'scroll-start', 'scroll-update', 'scroll-end'])); await tester.pump(const Duration(milliseconds: 100)); - expect(log, equals(['scrollstart', 'scroll', 'scroll', 'scrollend'])); + expect(log, equals(['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(['scrollstart', 'scroll', 'scroll', 'scrollend'])); + expect(log, equals(['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 log = []; await tester.pumpWidget(_buildScroller(log: log)); expect(log, equals([])); - Completer completer = scrollTo(tester, 100.0, duration: const Duration(seconds: 1)); + Completer completer = animateTo(tester, 100.0, duration: const Duration(seconds: 1)); expect(completer.isCompleted, isFalse); - expect(log, equals(['scrollstart'])); + expect(log, equals(['scroll-start'])); await tester.pump(const Duration(milliseconds: 100)); - expect(log, equals(['scrollstart'])); + expect(log, equals(['scroll-start'])); await tester.pump(const Duration(milliseconds: 100)); - expect(log, equals(['scrollstart', 'scroll'])); + expect(log, equals(['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(['scrollstart', 'scroll'])); + expect(log, equals(['scroll-start', 'scroll-update'])); await tester.pump(const Duration(milliseconds: 100)); - expect(log, equals(['scrollstart', 'scroll'])); + expect(log, equals(['scroll-start', 'scroll-update'])); await tester.pump(const Duration(milliseconds: 1500)); - expect(log, equals(['scrollstart', 'scroll', 'scroll', 'scrollend'])); + expect(log, equals(['scroll-start', 'scroll-update', 'scroll-update', 'scroll-end'])); expect(completer.isCompleted, isTrue); }); @@ -133,15 +142,15 @@ void main() { expect(log, equals([])); 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(['scrollstart'])); + log.removeWhere((String value) => value == 'scroll-update'); + expect(log, equals(['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(['scrollstart', 'scrollend', 'scrollstart'])); + log.removeWhere((String value) => value == 'scroll-update'); + expect(log, equals(['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(['scrollstart', 'scrollend', 'scrollstart', 'scrollend'])); + log.removeWhere((String value) => value == 'scroll-update'); + expect(log, equals(['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(find.byType(Scrollable)).scrollOffset, equals(0.0)); + expect(tester.state(find.byType(Scrollable2)).position.pixels, equals(0.0)); }); } diff --git a/packages/flutter/test/widgets/scroll_notification_test.dart b/packages/flutter/test/widgets/scroll_notification_test.dart index 3d9152933d..49c2761937 100644 --- a/packages/flutter/test/widgets/scroll_notification_test.dart +++ b/packages/flutter/test/widgets/scroll_notification_test.dart @@ -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( - onNotification: (ScrollNotification value) { - notification = value; + await tester.pumpWidget(new NotificationListener( + 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()); + 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()); + 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()); + 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 depth0Kinds = []; - final List depth1Kinds = []; + final List depth0Types = []; + final List depth1Types = []; final List depth0Values = []; final List depth1Values = []; - await tester.pumpWidget(new NotificationListener( - onNotification: (ScrollNotification value) { - depth1Kinds.add(value.kind); + await tester.pumpWidget(new NotificationListener( + 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( - onNotification: (ScrollNotification value) { - depth0Kinds.add(value.kind); + child: new NotificationListener( + 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 kinds = [ - ScrollNotificationKind.started, - ScrollNotificationKind.updated, - ScrollNotificationKind.ended + final List types = [ + ScrollStartNotification, + UserScrollNotification, + ScrollUpdateNotification, + ScrollEndNotification, + UserScrollNotification, ]; - expect(depth0Kinds, equals(kinds)); - expect(depth1Kinds, equals(kinds)); + expect(depth0Types, equals(types)); + expect(depth1Types, equals(types)); - expect(depth0Values, equals([0, 0, 0])); - expect(depth1Values, equals([1, 1, 1])); + // These values might not be what we want in the end. + // See . + expect(depth0Values, equals([1, 1, 1, 1, 1])); + expect(depth1Values, equals([2, 2, 2, 2, 2])); }); }