animations_test.dart 9.18 KB
Newer Older
1 2 3 4 5 6 7 8
// Copyright 2016 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

import 'package:flutter_test/flutter_test.dart';
import 'package:flutter/animation.dart';
import 'package:flutter/widgets.dart';

9 10 11 12 13
class BogusCurve extends Curve {
  @override
  double transform(double t) => 100.0;
}

14 15 16 17 18 19 20
void main() {
  setUp(() {
    WidgetsFlutterBinding.ensureInitialized();
    WidgetsBinding.instance.resetEpoch();
  });

  test('toString control test', () {
21 22 23
    expect(kAlwaysCompleteAnimation, hasOneLineDescription);
    expect(kAlwaysDismissedAnimation, hasOneLineDescription);
    expect(const AlwaysStoppedAnimation<double>(0.5), hasOneLineDescription);
24
    CurvedAnimation curvedAnimation = CurvedAnimation(
25 26 27
      parent: kAlwaysDismissedAnimation,
      curve: Curves.ease
    );
28
    expect(curvedAnimation, hasOneLineDescription);
29
    curvedAnimation.reverseCurve = Curves.elasticOut;
30
    expect(curvedAnimation, hasOneLineDescription);
31
    final AnimationController controller = AnimationController(
32 33
      duration: const Duration(milliseconds: 500),
      vsync: const TestVSync(),
34 35 36 37
    );
    controller
      ..value = 0.5
      ..reverse();
38
    curvedAnimation = CurvedAnimation(
39 40 41 42
      parent: controller,
      curve: Curves.ease,
      reverseCurve: Curves.elasticOut
    );
43
    expect(curvedAnimation, hasOneLineDescription);
44
    controller.stop();
45 46
  });

47
  test('ProxyAnimation.toString control test', () {
48
    final ProxyAnimation animation = ProxyAnimation();
49 50
    expect(animation.value, 0.0);
    expect(animation.status, AnimationStatus.dismissed);
51
    expect(animation, hasOneLineDescription);
52
    animation.parent = kAlwaysDismissedAnimation;
53
    expect(animation, hasOneLineDescription);
54
  });
55 56

  test('ProxyAnimation set parent generates value changed', () {
57
    final AnimationController controller = AnimationController(
58 59
      vsync: const TestVSync(),
    );
60 61
    controller.value = 0.5;
    bool didReceiveCallback = false;
62
    final ProxyAnimation animation = ProxyAnimation()
63 64 65 66 67 68 69 70 71 72 73 74 75
      ..addListener(() {
        didReceiveCallback = true;
      });
    expect(didReceiveCallback, isFalse);
    animation.parent = controller;
    expect(didReceiveCallback, isTrue);
    didReceiveCallback = false;
    expect(didReceiveCallback, isFalse);
    controller.value = 0.6;
    expect(didReceiveCallback, isTrue);
  });

  test('ReverseAnimation calls listeners', () {
76
    final AnimationController controller = AnimationController(
77 78
      vsync: const TestVSync(),
    );
79 80 81 82 83
    controller.value = 0.5;
    bool didReceiveCallback = false;
    void listener() {
      didReceiveCallback = true;
    }
84
    final ReverseAnimation animation = ReverseAnimation(controller)
85 86 87 88 89 90 91 92 93
      ..addListener(listener);
    expect(didReceiveCallback, isFalse);
    controller.value = 0.6;
    expect(didReceiveCallback, isTrue);
    didReceiveCallback = false;
    animation.removeListener(listener);
    expect(didReceiveCallback, isFalse);
    controller.value = 0.7;
    expect(didReceiveCallback, isFalse);
94
    expect(animation, hasOneLineDescription);
95 96 97
  });

  test('TrainHoppingAnimation', () {
98
    final AnimationController currentTrain = AnimationController(
99 100
      vsync: const TestVSync(),
    );
101
    final AnimationController nextTrain = AnimationController(
102 103
      vsync: const TestVSync(),
    );
104 105 106
    currentTrain.value = 0.5;
    nextTrain.value = 0.75;
    bool didSwitchTrains = false;
107
    final TrainHoppingAnimation animation = TrainHoppingAnimation(
108 109 110 111 112
      currentTrain, nextTrain, onSwitchedTrain: () {
        didSwitchTrains = true;
      });
    expect(didSwitchTrains, isFalse);
    expect(animation.value, 0.5);
113
    expect(animation, hasOneLineDescription);
114 115 116
    nextTrain.value = 0.25;
    expect(didSwitchTrains, isTrue);
    expect(animation.value, 0.25);
117
    expect(animation, hasOneLineDescription);
118 119
    expect(animation.toString(), contains('no next'));
  });
120 121

  test('AnimationMean control test', () {
122
    final AnimationController left = AnimationController(
123 124 125
      value: 0.5,
      vsync: const TestVSync(),
    );
126
    final AnimationController right = AnimationController(
127 128 129
      vsync: const TestVSync(),
    );

130
    final AnimationMean mean = AnimationMean(left: left, right: right);
131 132 133 134

    expect(mean, hasOneLineDescription);
    expect(mean.value, equals(0.25));

135
    final List<double> log = <double>[];
136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154
    void logValue() {
      log.add(mean.value);
    }

    mean.addListener(logValue);

    right.value = 1.0;

    expect(mean.value, equals(0.75));
    expect(log, equals(<double>[0.75]));
    log.clear();

    mean.removeListener(logValue);

    left.value = 0.0;

    expect(mean.value, equals(0.50));
    expect(log, isEmpty);
  });
155

156
  test('AnimationMax control test', () {
157
    final AnimationController first = AnimationController(
158 159 160
      value: 0.5,
      vsync: const TestVSync(),
    );
161
    final AnimationController second = AnimationController(
162 163 164
      vsync: const TestVSync(),
    );

165
    final AnimationMax<double> max = AnimationMax<double>(first, second);
166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191

    expect(max, hasOneLineDescription);
    expect(max.value, equals(0.5));

    final List<double> log = <double>[];
    void logValue() {
      log.add(max.value);
    }

    max.addListener(logValue);

    second.value = 1.0;

    expect(max.value, equals(1.0));
    expect(log, equals(<double>[1.0]));
    log.clear();

    max.removeListener(logValue);

    first.value = 0.0;

    expect(max.value, equals(1.0));
    expect(log, isEmpty);
  });

  test('AnimationMin control test', () {
192
    final AnimationController first = AnimationController(
193 194 195
      value: 0.5,
      vsync: const TestVSync(),
    );
196
    final AnimationController second = AnimationController(
197 198 199
      vsync: const TestVSync(),
    );

200
    final AnimationMin<double> min = AnimationMin<double>(first, second);
201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225

    expect(min, hasOneLineDescription);
    expect(min.value, equals(0.0));

    final List<double> log = <double>[];
    void logValue() {
      log.add(min.value);
    }

    min.addListener(logValue);

    second.value = 1.0;

    expect(min.value, equals(0.5));
    expect(log, equals(<double>[0.5]));
    log.clear();

    min.removeListener(logValue);

    first.value = 0.25;

    expect(min.value, equals(0.25));
    expect(log, isEmpty);
  });

226
  test('CurvedAnimation with bogus curve', () {
227
    final AnimationController controller = AnimationController(
228 229
      vsync: const TestVSync(),
    );
230
    final CurvedAnimation curved = CurvedAnimation(parent: controller, curve: BogusCurve());
231 232 233

    expect(() { curved.value; }, throwsFlutterError);
  });
234 235

  test('TweenSequence', () {
236
    final AnimationController controller = AnimationController(
237 238 239
      vsync: const TestVSync(),
    );

240
    final Animation<double> animation = TweenSequence<double>(
241
      <TweenSequenceItem<double>>[
242 243
        TweenSequenceItem<double>(
          tween: Tween<double>(begin: 5.0, end: 10.0),
244 245
          weight: 4.0,
        ),
246 247
        TweenSequenceItem<double>(
          tween: ConstantTween<double>(10.0),
248 249
          weight: 2.0,
        ),
250 251
        TweenSequenceItem<double>(
          tween: Tween<double>(begin: 10.0, end: 5.0),
252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275
          weight: 4.0,
        ),
      ],
    ).animate(controller);

    expect(animation.value, 5.0);

    controller.value = 0.2;
    expect(animation.value, 7.5);

    controller.value = 0.4;
    expect(animation.value, 10.0);

    controller.value = 0.6;
    expect(animation.value, 10.0);

    controller.value = 0.8;
    expect(animation.value, 7.5);

    controller.value = 1.0;
    expect(animation.value, 5.0);
  });

  test('TweenSequence with curves', () {
276
    final AnimationController controller = AnimationController(
277 278 279
      vsync: const TestVSync(),
    );

280
    final Animation<double> animation = TweenSequence<double>(
281
      <TweenSequenceItem<double>>[
282 283 284
        TweenSequenceItem<double>(
          tween: Tween<double>(begin: 5.0, end: 10.0)
            .chain(CurveTween(curve: const Interval(0.5, 1.0))),
285 286
          weight: 4.0,
        ),
287 288 289
        TweenSequenceItem<double>(
          tween: ConstantTween<double>(10.0)
            .chain(CurveTween(curve: Curves.linear)), // linear is a no-op
290 291
          weight: 2.0,
        ),
292 293 294
        TweenSequenceItem<double>(
          tween: Tween<double>(begin: 10.0, end: 5.0)
            .chain(CurveTween(curve: const Interval(0.0, 0.5))),
295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318
          weight: 4.0,
        ),
      ],
    ).animate(controller);

    expect(animation.value, 5.0);

    controller.value = 0.2;
    expect(animation.value, 5.0);

    controller.value = 0.4;
    expect(animation.value, 10.0);

    controller.value = 0.6;
    expect(animation.value, 10.0);

    controller.value = 0.8;
    expect(animation.value, 5.0);

    controller.value = 1.0;
    expect(animation.value, 5.0);
  });

  test('TweenSequence, one tween', () {
319
    final AnimationController controller = AnimationController(
320 321 322
      vsync: const TestVSync(),
    );

323
    final Animation<double> animation = TweenSequence<double>(
324
      <TweenSequenceItem<double>>[
325 326
        TweenSequenceItem<double>(
          tween: Tween<double>(begin: 5.0, end: 10.0),
327 328 329 330 331 332 333 334 335 336 337 338 339 340
          weight: 1.0,
        ),
      ],
    ).animate(controller);

    expect(animation.value, 5.0);

    controller.value = 0.5;
    expect(animation.value, 7.5);

    controller.value = 1.0;
    expect(animation.value, 10.0);
  });

341
}