tween_animation_builder_test.dart 13.5 KB
Newer Older
Ian Hickson's avatar
Ian Hickson committed
1
// Copyright 2014 The Flutter Authors. All rights reserved.
2 3 4 5
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

import 'package:flutter/widgets.dart';
6
import 'package:flutter_test/flutter_test.dart';
7
import 'package:leak_tracker_flutter_testing/leak_tracker_flutter_testing.dart';
8 9

void main() {
10
  testWidgetsWithLeakTracking('Animates forward when built', (WidgetTester tester) async {
11 12 13 14 15 16
    final List<int> values = <int>[];
    int endCount = 0;
    await tester.pumpWidget(
      TweenAnimationBuilder<int>(
        duration: const Duration(seconds: 1),
        tween: IntTween(begin: 10, end: 110),
17
        builder: (BuildContext context, int i, Widget? child) {
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
          values.add(i);
          return const Placeholder();
        },
        onEnd: () {
          endCount++;
        },
      ),
    );
    expect(endCount, 0);
    expect(values, <int>[10]);

    await tester.pump(const Duration(milliseconds: 500));
    expect(values, <int>[10, 60]);

    await tester.pump(const Duration(milliseconds: 501));
    expect(endCount, 1);
    expect(values, <int>[10, 60, 110]);

    await tester.pump(const Duration(milliseconds: 500));
    expect(endCount, 1);
    expect(values, <int>[10, 60, 110]);
  });

41
  testWidgetsWithLeakTracking('No initial animation when begin=null', (WidgetTester tester) async {
42 43 44 45 46 47
    final List<int> values = <int>[];
    int endCount = 0;
    await tester.pumpWidget(
      TweenAnimationBuilder<int>(
        duration: const Duration(seconds: 1),
        tween: IntTween(end: 100),
48
        builder: (BuildContext context, int i, Widget? child) {
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
          values.add(i);
          return const Placeholder();
        },
        onEnd: () {
          endCount++;
        },
      ),
    );
    expect(endCount, 0);
    expect(values, <int>[100]);
    await tester.pump(const Duration(seconds: 2));
    expect(endCount, 0);
    expect(values, <int>[100]);
  });


65
  testWidgetsWithLeakTracking('No initial animation when begin=end', (WidgetTester tester) async {
66 67 68 69 70 71
    final List<int> values = <int>[];
    int endCount = 0;
    await tester.pumpWidget(
      TweenAnimationBuilder<int>(
        duration: const Duration(seconds: 1),
        tween: IntTween(begin: 100, end: 100),
72
        builder: (BuildContext context, int i, Widget? child) {
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87
          values.add(i);
          return const Placeholder();
        },
        onEnd: () {
          endCount++;
        },
      ),
    );
    expect(endCount, 0);
    expect(values, <int>[100]);
    await tester.pump(const Duration(seconds: 2));
    expect(endCount, 0);
    expect(values, <int>[100]);
  });

88
  testWidgetsWithLeakTracking('Replace tween animates new tween', (WidgetTester tester) async {
89
    final List<int> values = <int>[];
90
    Widget buildWidget({required IntTween tween}) {
91 92 93
      return TweenAnimationBuilder<int>(
        duration: const Duration(seconds: 1),
        tween: tween,
94
        builder: (BuildContext context, int i, Widget? child) {
95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115
          values.add(i);
          return const Placeholder();
        },
      );
    }

    await tester.pumpWidget(buildWidget(tween: IntTween(begin: 0, end: 100)));
    expect(values, <int>[0]);
    await tester.pump(const Duration(seconds: 2)); // finish first animation.
    expect(values, <int>[0, 100]);

    await tester.pumpWidget(buildWidget(tween: IntTween(begin: 100, end: 200)));
    expect(values, <int>[0, 100, 100]);

    await tester.pump(const Duration(milliseconds: 500));
    expect(values, <int>[0, 100, 100, 150]);

    await tester.pump(const Duration(milliseconds: 500));
    expect(values, <int>[0, 100, 100, 150, 200]);
  });

116
  testWidgetsWithLeakTracking('Curve is respected', (WidgetTester tester) async {
117
    final List<int> values = <int>[];
118
    Widget buildWidget({required IntTween tween, required Curve curve}) {
119 120 121 122
      return TweenAnimationBuilder<int>(
        duration: const Duration(seconds: 1),
        tween: tween,
        curve: curve,
123
        builder: (BuildContext context, int i, Widget? child) {
124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145
          values.add(i);
          return const Placeholder();
        },
      );
    }

    await tester.pumpWidget(buildWidget(tween: IntTween(begin: 0, end: 100), curve: Curves.easeInExpo));
    expect(values, <int>[0]);
    await tester.pump(const Duration(milliseconds: 500));
    expect(values.last, lessThan(50));
    expect(values.last, greaterThan(0));

    await tester.pump(const Duration(seconds: 2)); // finish animation.

    values.clear();
    // Update curve (and tween to re-trigger animation).
    await tester.pumpWidget(buildWidget(tween: IntTween(begin: 100, end: 200), curve: Curves.linear));
    expect(values, <int>[100]);
    await tester.pump(const Duration(milliseconds: 500));
    expect(values, <int>[100, 150]);
  });

146
  testWidgetsWithLeakTracking('Duration is respected', (WidgetTester tester) async {
147
    final List<int> values = <int>[];
148
    Widget buildWidget({required IntTween tween, required Duration duration}) {
149 150 151
      return TweenAnimationBuilder<int>(
        tween: tween,
        duration: duration,
152
        builder: (BuildContext context, int i, Widget? child) {
153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173
          values.add(i);
          return const Placeholder();
        },
      );
    }

    await tester.pumpWidget(buildWidget(tween: IntTween(begin: 0, end: 100), duration: const Duration(seconds: 1)));
    expect(values, <int>[0]);
    await tester.pump(const Duration(milliseconds: 500));
    expect(values, <int>[0, 50]);

    await tester.pump(const Duration(seconds: 2)); // finish animation.

    values.clear();
    // Update duration (and tween to re-trigger animation).
    await tester.pumpWidget(buildWidget(tween: IntTween(begin: 100, end: 200), duration: const Duration(seconds: 2)));
    expect(values, <int>[100]);
    await tester.pump(const Duration(milliseconds: 500));
    expect(values, <int>[100, 125]);
  });

174
  testWidgetsWithLeakTracking('Child is integrated into tree', (WidgetTester tester) async {
175 176 177 178 179 180
    await tester.pumpWidget(
      Directionality(
        textDirection: TextDirection.ltr,
        child: TweenAnimationBuilder<int>(
          tween: IntTween(begin: 0, end: 100),
          duration: const Duration(seconds: 1),
181 182
          builder: (BuildContext context, int i, Widget? child) {
            return child!;
183 184 185 186 187 188 189 190 191 192
          },
          child: const Text('Hello World'),
        ),
      ),
    );

    expect(find.text('Hello World'), findsOneWidget);
  });

  group('Change tween gapless while', () {
193
    testWidgetsWithLeakTracking('running forward', (WidgetTester tester) async {
194
      final List<int> values = <int>[];
195
      Widget buildWidget({required IntTween tween}) {
196 197 198
        return TweenAnimationBuilder<int>(
          tween: tween,
          duration: const Duration(seconds: 1),
199
          builder: (BuildContext context, int i, Widget? child) {
200 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 226 227
            values.add(i);
            return const Placeholder();
          },
        );
      }

      await tester.pumpWidget(buildWidget(
        tween: IntTween(begin: 0, end: 100),
      ));
      expect(values, <int>[0]);
      await tester.pump(const Duration(milliseconds: 500));
      expect(values, <int>[0, 50]);

      // Change tween
      await tester.pumpWidget(buildWidget(
        tween: IntTween(begin: 200, end: 300),
      ));
      expect(values, <int>[0, 50, 50]); // gapless: animation continues where it left off.

      await tester.pump(const Duration(milliseconds: 500));
      expect(values, <int>[0, 50, 50, 175]); // 175 = halfway between 50 and new target 300.

      // Run animation to end
      await tester.pump(const Duration(seconds: 2));
      expect(values, <int>[0, 50, 50, 175, 300]);
      values.clear();
    });

228
    testWidgetsWithLeakTracking('running forward and then reverse with same tween instance', (WidgetTester tester) async {
229
      final List<int> values = <int>[];
230
      Widget buildWidget({required IntTween tween}) {
231 232 233
        return TweenAnimationBuilder<int>(
          tween: tween,
          duration: const Duration(seconds: 1),
234
          builder: (BuildContext context, int i, Widget? child) {
235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257
            values.add(i);
            return const Placeholder();
          },
        );
      }

      final IntTween tween1 = IntTween(begin: 0, end: 100);
      final IntTween tween2 = IntTween(begin: 200, end: 300);

      await tester.pumpWidget(buildWidget(
        tween: tween1,
      ));
      await tester.pump(const Duration(milliseconds: 500));
      await tester.pumpWidget(buildWidget(
        tween: tween2,
      ));
      await tester.pump(const Duration(milliseconds: 500));
      await tester.pump(const Duration(seconds: 2));
      expect(values, <int>[0, 50, 50, 175, 300]);
      values.clear();
    });
  });

258
  testWidgetsWithLeakTracking('Changing tween while gapless tween change is in progress', (WidgetTester tester) async {
259
    final List<int> values = <int>[];
260
    Widget buildWidget({required IntTween tween}) {
261 262 263
      return TweenAnimationBuilder<int>(
        tween: tween,
        duration: const Duration(seconds: 1),
264
        builder: (BuildContext context, int i, Widget? child) {
265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297
          values.add(i);
          return const Placeholder();
        },
      );
    }

    final IntTween tween1 = IntTween(begin: 0, end: 100);
    final IntTween tween2 = IntTween(begin: 200, end: 300);
    final IntTween tween3 = IntTween(begin: 400, end: 501);

    await tester.pumpWidget(buildWidget(
      tween: tween1,
    ));
    await tester.pump(const Duration(milliseconds: 500));
    expect(values, <int>[0, 50]);
    values.clear();

    // Change tween
    await tester.pumpWidget(buildWidget(
      tween: tween2,
    ));
    await tester.pump(const Duration(milliseconds: 500));
    expect(values, <int>[50, 175]);
    values.clear();

    await tester.pumpWidget(buildWidget(
      tween: tween3,
    ));
    await tester.pump(const Duration(milliseconds: 500));
    await tester.pump(const Duration(milliseconds: 500));
    expect(values, <int>[175, 338, 501]);
  });

298
  testWidgetsWithLeakTracking('Changing curve while no animation is running does not trigger animation', (WidgetTester tester) async {
299
    final List<int> values = <int>[];
300
    Widget buildWidget({required Curve curve}) {
301 302 303 304
      return TweenAnimationBuilder<int>(
        tween: IntTween(begin: 0, end: 100),
        curve: curve,
        duration: const Duration(seconds: 1),
305
        builder: (BuildContext context, int i, Widget? child) {
306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326
          values.add(i);
          return const Placeholder();
        },
      );
    }

    await tester.pumpWidget(buildWidget(
      curve: Curves.linear,
    ));
    await tester.pump(const Duration(seconds: 2));
    expect(values, <int>[0, 100]);
    values.clear();

    await tester.pumpWidget(buildWidget(
      curve: Curves.easeInExpo,
    ));
    expect(values, <int>[100]);
    await tester.pump(const Duration(seconds: 2));
    expect(values, <int>[100]);
  });

327
  testWidgetsWithLeakTracking('Setting same tween and direction does not trigger animation', (WidgetTester tester) async {
328
    final List<int> values = <int>[];
329
    Widget buildWidget({required IntTween tween}) {
330 331 332
      return TweenAnimationBuilder<int>(
        tween: tween,
        duration: const Duration(seconds: 1),
333
        builder: (BuildContext context, int i, Widget? child) {
334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355
          values.add(i);
          return const Placeholder();
        },
      );
    }

    await tester.pumpWidget(buildWidget(
      tween: IntTween(begin: 0, end: 100),
    ));
    await tester.pump(const Duration(milliseconds: 500));
    await tester.pump(const Duration(milliseconds: 500));
    expect(values, <int>[0, 50, 100]);
    values.clear();

    await tester.pumpWidget(buildWidget(
      tween: IntTween(begin: 0, end: 100),
    ));
    await tester.pump(const Duration(milliseconds: 500));
    await tester.pump(const Duration(milliseconds: 500));
    expect(values, everyElement(100));
  });

356
  testWidgetsWithLeakTracking('Setting same tween and direction while gapless animation is in progress works', (WidgetTester tester) async {
357
    final List<int> values = <int>[];
358
    Widget buildWidget({required IntTween tween}) {
359 360 361
      return TweenAnimationBuilder<int>(
        tween: tween,
        duration: const Duration(seconds: 1),
362
        builder: (BuildContext context, int i, Widget? child) {
363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390
          values.add(i);
          return const Placeholder();
        },
      );
    }

    await tester.pumpWidget(buildWidget(
      tween: IntTween(begin: 0, end: 100),
    ));
    await tester.pump(const Duration(milliseconds: 500));
    expect(values, <int>[0, 50]);
    await tester.pumpWidget(buildWidget(
      tween: IntTween(begin: 200, end: 300),
    ));
    await tester.pump(const Duration(milliseconds: 500));
    expect(values, <int>[0, 50, 50, 175]);

    await tester.pumpWidget(buildWidget(
      tween: IntTween(begin: 200, end: 300),
    ));
    expect(values, <int>[0, 50, 50, 175, 175]);
    await tester.pump(const Duration(milliseconds: 500));
    expect(values, <int>[0, 50, 50, 175, 175, 300]);

    values.clear();
    await tester.pump(const Duration(seconds: 2));
    expect(values, everyElement(300));
  });
391

392
  testWidgetsWithLeakTracking('Works with nullable tweens', (WidgetTester tester) async {
393 394 395 396 397 398 399 400 401 402 403 404 405 406 407
    final List<Size?> values = <Size?>[];
    await tester.pumpWidget(
      TweenAnimationBuilder<Size?>(
        duration: const Duration(seconds: 1),
        tween: SizeTween(end: const Size(10,10)),
        builder: (BuildContext context, Size? s, Widget? child) {
          values.add(s);
          return const Placeholder();
        },
      ),
    );
    expect(values, <Size>[const Size(10,10)]);
    await tester.pump(const Duration(seconds: 2));
    expect(values, <Size>[const Size(10,10)]);
  });
408
}