Skip to content

WidgetTest.pump(elapsedTime) doesn't advance time reliably #348

@HansMuller

Description

@HansMuller

Using WidgetTest.pump(elapsedTime) to advance animations and completions in a test can be difficult because just calling the function once doesn't necessarily roll all of the test's state forward by the specified time. The unit test below illustrates a simple version of the problem: it's necessary to call pump(1 second) twice to get the 200ms performance from one end to the other. More complex widgets, see for example snack_bar_test.dart, require more blind test pumping.

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

import 'package:test/test.dart';

import 'widget_tester.dart';

void main() {
  test('Time does not move', () {
    testWidgets((WidgetTester tester) {
      tester.pumpWidget(new Container());

      Performance performance = new Performance(duration: const Duration(milliseconds: 200))
        ..forward();
      print("performance.progress=${performance.progress} expect 0.0");

      tester.pump(new Duration(seconds: 1));
      print("performance.progress=${performance.progress} expect 1.0");

      tester.pump(new Duration(seconds: 1));
      print("performance.progress=${performance.progress} is actually 1.0");

      performance.reverse();
      tester.pump(new Duration(seconds: 1));
      print("performance.progress=${performance.progress} expect 0.0");

      tester.pump(new Duration(seconds: 1));
      print("performance.progress=${performance.progress} is actualy 0.0");
    });
  });
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    a: tests"flutter test", flutter_test, or one of our teststoolAffects the "flutter" command-line tool. See also t: labels.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions