From 1e53d3206ba622f88b126371e16b167f6c91bb18 Mon Sep 17 00:00:00 2001 From: Michael Goderbauer Date: Fri, 4 Aug 2017 16:14:05 -0700 Subject: [PATCH] Adding tests for animationStatus (#11520) * Adding tests for animationStatus * remove comments * animateTo is allways forward * clearify docs * review comments --- .../src/animation/animation_controller.dart | 22 ++- .../animation/animation_controller_test.dart | 132 +++++++++++++++++- 2 files changed, 151 insertions(+), 3 deletions(-) diff --git a/packages/flutter/lib/src/animation/animation_controller.dart b/packages/flutter/lib/src/animation/animation_controller.dart index 54bc9c9c2b..ca04cef9d8 100644 --- a/packages/flutter/lib/src/animation/animation_controller.dart +++ b/packages/flutter/lib/src/animation/animation_controller.dart @@ -270,6 +270,10 @@ class AnimationController extends Animation /// The most recently returned [TickerFuture], if any, is marked as having been /// canceled, meaning the future never completes and its [TickerFuture.orCancel] /// derivative future completes with a [TickerCanceled] error. + /// + /// During the animation, [status] is reported as [AnimationStatus.forward], + /// which switches to [AnimationStatus.completed] when [upperBound] is + /// reached at the end of the animation. TickerFuture forward({ double from }) { assert(() { if (duration == null) { @@ -284,7 +288,7 @@ class AnimationController extends Animation _direction = _AnimationDirection.forward; if (from != null) value = from; - return animateTo(upperBound); + return _animateToInternal(upperBound); } /// Starts running this animation in reverse (towards the beginning). @@ -294,6 +298,10 @@ class AnimationController extends Animation /// The most recently returned [TickerFuture], if any, is marked as having been /// canceled, meaning the future never completes and its [TickerFuture.orCancel] /// derivative future completes with a [TickerCanceled] error. + /// + /// During the animation, [status] is reported as [AnimationStatus.reverse], + /// which switches to [AnimationStatus.dismissed] when [lowerBound] is + /// reached at the end of the animation. TickerFuture reverse({ double from }) { assert(() { if (duration == null) { @@ -308,7 +316,7 @@ class AnimationController extends Animation _direction = _AnimationDirection.reverse; if (from != null) value = from; - return animateTo(lowerBound); + return _animateToInternal(lowerBound); } /// Drives the animation from its current value to target. @@ -318,7 +326,17 @@ class AnimationController extends Animation /// The most recently returned [TickerFuture], if any, is marked as having been /// canceled, meaning the future never completes and its [TickerFuture.orCancel] /// derivative future completes with a [TickerCanceled] error. + /// + /// During the animation, [status] is reported as [AnimationStatus.forward] + /// regardless of whether `target` > [value] or not. At the end of the + /// animation, when `target` is reached, [status] is reported as + /// [AnimationStatus.completed]. TickerFuture animateTo(double target, { Duration duration, Curve curve: Curves.linear }) { + _direction = _AnimationDirection.forward; + return _animateToInternal(target, duration: duration, curve: curve); + } + + TickerFuture _animateToInternal(double target, { Duration duration, Curve curve: Curves.linear }) { Duration simulationDuration = duration; if (simulationDuration == null) { assert(() { diff --git a/packages/flutter/test/animation/animation_controller_test.dart b/packages/flutter/test/animation/animation_controller_test.dart index e55cf07b7d..21c627fad0 100644 --- a/packages/flutter/test/animation/animation_controller_test.dart +++ b/packages/flutter/test/animation/animation_controller_test.dart @@ -379,7 +379,7 @@ void main() { final double currentValue = controller.value; controller.animateTo(currentValue, duration: const Duration(milliseconds: 100)); - expect(statusLog, equals([ AnimationStatus.reverse, AnimationStatus.dismissed ])); + expect(statusLog, equals([ AnimationStatus.reverse, AnimationStatus.completed ])); expect(controller.value, currentValue); }); @@ -395,4 +395,134 @@ void main() { expect(SchedulerBinding.instance.transientCallbackCount, equals(0), reason: 'Expected no animation.'); expect(controller.value, 1.0); }); + + test('setting value directly sets correct status', () { + final AnimationController controller = new AnimationController( + value: 0.0, + lowerBound: 0.0, + upperBound: 1.0, + vsync: const TestVSync(), + ); + + expect(controller.value, 0.0); + expect(controller.status, AnimationStatus.dismissed); + + controller.value = 0.5; + expect(controller.value, 0.5); + expect(controller.status, AnimationStatus.forward); + + controller.value = 1.0; + expect(controller.value, 1.0); + expect(controller.status, AnimationStatus.completed); + + controller.value = 0.5; + expect(controller.value, 0.5); + expect(controller.status, AnimationStatus.forward); + + controller.value = 0.0; + expect(controller.value, 0.0); + expect(controller.status, AnimationStatus.dismissed); + }); + + test('animateTo sets correct status', () { + final List statusLog = []; + final AnimationController controller = new AnimationController( + duration: const Duration(milliseconds: 100), + value: 0.0, + lowerBound: 0.0, + upperBound: 1.0, + vsync: const TestVSync(), + )..addStatusListener(statusLog.add); + + expect(controller.value, 0.0); + expect(controller.status, AnimationStatus.dismissed); + + // Animate from 0.0 to 0.5 + controller.animateTo(0.5); + tick(const Duration(milliseconds: 0)); + tick(const Duration(milliseconds: 150)); + expect(controller.value, 0.5); + expect(statusLog, equals([ AnimationStatus.forward, AnimationStatus.completed ])); + statusLog.clear(); + + // Animate from 0.5 to 1.0 + controller.animateTo(1.0); + tick(const Duration(milliseconds: 0)); + tick(const Duration(milliseconds: 150)); + expect(controller.value, 1.0); + expect(statusLog, equals([ AnimationStatus.forward, AnimationStatus.completed ])); + statusLog.clear(); + + // Animate from 1.0 to 0.5 + controller.animateTo(0.5); + tick(const Duration(milliseconds: 0)); + tick(const Duration(milliseconds: 150)); + expect(controller.value, 0.5); + expect(statusLog, equals([ AnimationStatus.forward, AnimationStatus.completed ])); + statusLog.clear(); + + // Animate from 0.5 to 1.0 + controller.animateTo(0.0); + tick(const Duration(milliseconds: 0)); + tick(const Duration(milliseconds: 150)); + expect(controller.value, 0.0); + expect(statusLog, equals([ AnimationStatus.forward, AnimationStatus.completed ])); + statusLog.clear(); + }); + + test('after a reverse call animateTo sets correct status', () { + final List statusLog = []; + final AnimationController controller = new AnimationController( + duration: const Duration(milliseconds: 100), + value: 1.0, + lowerBound: 0.0, + upperBound: 1.0, + vsync: const TestVSync(), + )..addStatusListener(statusLog.add); + + expect(controller.value, 1.0); + expect(controller.status, AnimationStatus.completed); + + controller.reverse(); + tick(const Duration(milliseconds: 0)); + tick(const Duration(milliseconds: 150)); + expect(controller.value, 0.0); + expect(statusLog, equals([ AnimationStatus.reverse, AnimationStatus.dismissed ])); + statusLog.clear(); + + controller.animateTo(0.5); + tick(const Duration(milliseconds: 0)); + tick(const Duration(milliseconds: 150)); + expect(controller.value, 0.5); + expect(statusLog, equals([ AnimationStatus.forward, AnimationStatus.completed ])); + statusLog.clear(); + }); + + test('after a forward call animateTo sets correct status', () { + final List statusLog = []; + final AnimationController controller = new AnimationController( + duration: const Duration(milliseconds: 100), + value: 0.0, + lowerBound: 0.0, + upperBound: 1.0, + vsync: const TestVSync(), + )..addStatusListener(statusLog.add); + + expect(controller.value, 0.0); + expect(controller.status, AnimationStatus.dismissed); + + controller.forward(); + tick(const Duration(milliseconds: 0)); + tick(const Duration(milliseconds: 150)); + expect(controller.value, 1.0); + expect(statusLog, equals([ AnimationStatus.forward, AnimationStatus.completed ])); + statusLog.clear(); + + controller.animateTo(0.5); + tick(const Duration(milliseconds: 0)); + tick(const Duration(milliseconds: 150)); + expect(controller.value, 0.5); + expect(statusLog, equals([ AnimationStatus.forward, AnimationStatus.completed ])); + statusLog.clear(); + }); }