From 4b037fd65f8e284346bc7b7fda318b2409002a78 Mon Sep 17 00:00:00 2001 From: Adam Barth Date: Mon, 31 Oct 2016 16:46:54 -0700 Subject: [PATCH] Simplify and test TransitionRoute.completed (#6601) We now create the Completer inside TransitionRoute, as with the popped completer. Also, test that this future resolves at the appropriate time. --- packages/flutter/lib/src/widgets/routes.dart | 18 +---- .../test/widget/page_transitions_test.dart | 77 +++++++++++++++++++ 2 files changed, 81 insertions(+), 14 deletions(-) diff --git a/packages/flutter/lib/src/widgets/routes.dart b/packages/flutter/lib/src/widgets/routes.dart index c6af3a0f45..8db62389e9 100644 --- a/packages/flutter/lib/src/widgets/routes.dart +++ b/packages/flutter/lib/src/widgets/routes.dart @@ -74,24 +74,14 @@ abstract class OverlayRoute extends Route { /// A route with entrance and exit transitions. abstract class TransitionRoute extends OverlayRoute { - /// Creates a route with entrance and exit transitions. - TransitionRoute({ - Completer transitionCompleter - }) : _transitionCompleter = transitionCompleter; - - /// The same as the default constructor but callable with mixins. - TransitionRoute.explicit( - Completer transitionCompleter - ) : this(transitionCompleter: transitionCompleter); - /// This future completes only once the transition itself has finished, after /// the overlay entries have been removed from the navigator's overlay. /// /// This future completes once the animation has been dismissed. That will be /// after [popped], because [popped] completes before the animation even /// starts, as soon as the route is popped. - Future get completed => _transitionCompleter?.future; - final Completer _transitionCompleter; + Future get completed => _transitionCompleter.future; + final Completer _transitionCompleter = new Completer(); /// The duration the transition lasts. Duration get transitionDuration; @@ -252,7 +242,7 @@ abstract class TransitionRoute extends OverlayRoute { @override void finished() { super.finished(); - _transitionCompleter?.complete(_result); + _transitionCompleter.complete(_result); } @override @@ -456,7 +446,7 @@ abstract class ModalRoute extends TransitionRoute with LocalHistoryRoute routes = { + '/': (_) => new Center(child: new Text('home')), + '/next': (_) => new Center(child: new Text('next')), + }; + + await tester.pumpWidget(new MaterialApp(routes: routes)); + + PageRoute route = new MaterialPageRoute( + settings: new RouteSettings(name: '/page'), + builder: (BuildContext context) => new Center(child: new Text('page')), + ); + + int popCount = 0; + route.popped.then((_) { + ++popCount; + }); + + int completeCount = 0; + route.completed.then((_) { + ++completeCount; + }); + + expect(popCount, 0); + expect(completeCount, 0); + + Navigator.push(tester.element(find.text('home')), route); + + expect(popCount, 0); + expect(completeCount, 0); + + await tester.pump(); + + expect(popCount, 0); + expect(completeCount, 0); + + await tester.pump(const Duration(milliseconds: 100)); + + expect(popCount, 0); + expect(completeCount, 0); + + await tester.pump(const Duration(milliseconds: 100)); + + expect(popCount, 0); + expect(completeCount, 0); + + await tester.pump(const Duration(seconds: 1)); + + expect(popCount, 0); + expect(completeCount, 0); + + Navigator.pop(tester.element(find.text('page'))); + + expect(popCount, 0); + expect(completeCount, 0); + + await tester.pump(); + + expect(popCount, 1); + expect(completeCount, 0); + + await tester.pump(const Duration(milliseconds: 100)); + + expect(popCount, 1); + expect(completeCount, 0); + + await tester.pump(const Duration(milliseconds: 100)); + + expect(popCount, 1); + expect(completeCount, 0); + + await tester.pump(const Duration(seconds: 1)); + + expect(popCount, 1); + expect(completeCount, 1); + }); }