Adding tests for animationStatus (#11520)
* Adding tests for animationStatus * remove comments * animateTo is allways forward * clearify docs * review comments
This commit is contained in:
parent
a5c4680e72
commit
1e53d3206b
@ -270,6 +270,10 @@ class AnimationController extends Animation<double>
|
||||
/// 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<double>
|
||||
_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<double>
|
||||
/// 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<double>
|
||||
_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<double>
|
||||
/// 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(() {
|
||||
|
@ -379,7 +379,7 @@ void main() {
|
||||
|
||||
final double currentValue = controller.value;
|
||||
controller.animateTo(currentValue, duration: const Duration(milliseconds: 100));
|
||||
expect(statusLog, equals(<AnimationStatus>[ AnimationStatus.reverse, AnimationStatus.dismissed ]));
|
||||
expect(statusLog, equals(<AnimationStatus>[ 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<AnimationStatus> statusLog = <AnimationStatus>[];
|
||||
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>[ 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>[ 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>[ 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>[ AnimationStatus.forward, AnimationStatus.completed ]));
|
||||
statusLog.clear();
|
||||
});
|
||||
|
||||
test('after a reverse call animateTo sets correct status', () {
|
||||
final List<AnimationStatus> statusLog = <AnimationStatus>[];
|
||||
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>[ 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>[ AnimationStatus.forward, AnimationStatus.completed ]));
|
||||
statusLog.clear();
|
||||
});
|
||||
|
||||
test('after a forward call animateTo sets correct status', () {
|
||||
final List<AnimationStatus> statusLog = <AnimationStatus>[];
|
||||
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>[ 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>[ AnimationStatus.forward, AnimationStatus.completed ]));
|
||||
statusLog.clear();
|
||||
});
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user