status_transitions_test.dart 2.62 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

class TestStatusTransitionWidget extends StatusTransitionWidget {
10
  const TestStatusTransitionWidget({
11
    super.key,
12
    required this.builder,
13 14
    required super.animation,
  });
15 16 17 18 19 20 21 22

  final WidgetBuilder builder;

  @override
  Widget build(BuildContext context) => builder(context);
}

void main() {
23
  testWidgetsWithLeakTracking('Status transition control test', (WidgetTester tester) async {
24
    bool didBuild = false;
25
    final AnimationController controller = AnimationController(
26 27 28
      duration: const Duration(seconds: 1),
      vsync: const TestVSync(),
    );
29
    addTearDown(controller.dispose);
30

31
    await tester.pumpWidget(TestStatusTransitionWidget(
32 33 34 35
      animation: controller,
      builder: (BuildContext context) {
        expect(didBuild, isFalse);
        didBuild = true;
36
        return Container();
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62
      },
    ));

    expect(didBuild, isTrue);
    didBuild = false;

    controller.forward();

    expect(didBuild, isFalse);
    await tester.pump();
    expect(didBuild, isTrue);
    didBuild = false;
    await tester.pump(const Duration(milliseconds: 100));
    expect(didBuild, isFalse);
    await tester.pump(const Duration(milliseconds: 850));
    expect(didBuild, isFalse);
    await tester.pump(const Duration(milliseconds: 100));
    expect(didBuild, isTrue);
    didBuild = false;
    controller.forward();
    await tester.pump(const Duration(milliseconds: 100));
    expect(didBuild, isFalse);
    controller.stop();
    await tester.pump(const Duration(milliseconds: 100));
    expect(didBuild, isFalse);

63
    final AnimationController anotherController = AnimationController(
64 65 66
      duration: const Duration(seconds: 1),
      vsync: const TestVSync(),
    );
67
    addTearDown(anotherController.dispose);
68

69
    await tester.pumpWidget(TestStatusTransitionWidget(
70 71 72 73
      animation: anotherController,
      builder: (BuildContext context) {
        expect(didBuild, isFalse);
        didBuild = true;
74
        return Container();
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91
      },
    ));

    expect(didBuild, isTrue);
    didBuild = false;
    controller.reverse();
    await tester.pump(const Duration(milliseconds: 100));
    expect(didBuild, isFalse);
    anotherController.forward();
    await tester.pump(const Duration(milliseconds: 100));
    expect(didBuild, isTrue);
    didBuild = false;

    controller.stop();
    anotherController.stop();
  });
}