widget_tester_test.dart 4.73 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/widgets.dart';
import 'package:flutter_test/flutter_test.dart';

void main() {
9
  group('findsOneWidget', () {
10 11
    testWidgets('finds exactly one widget', (WidgetTester tester) async {
      await tester.pumpWidget(new Text('foo'));
12
      expect(find.text('foo'), findsOneWidget);
13 14
    });

15
    testWidgets('fails with a descriptive message', (WidgetTester tester) async {
16 17
      TestFailure failure;
      try {
18
        expect(find.text('foo', skipOffstage: false), findsOneWidget);
19 20 21 22 23 24 25 26 27
      } catch(e) {
        failure = e;
      }

      expect(failure, isNotNull);
      String message = failure.message;
      expect(message, contains('Expected: exactly one matching node in the widget tree\n'));
      expect(message, contains('Actual: ?:<zero widgets with text "foo">\n'));
      expect(message, contains('Which: means none were found but one was expected\n'));
28 29 30
    });
  });

31
  group('findsNothing', () {
32
    testWidgets('finds no widgets', (WidgetTester tester) async {
33
      expect(find.text('foo'), findsNothing);
34 35
    });

36 37
    testWidgets('fails with a descriptive message', (WidgetTester tester) async {
      await tester.pumpWidget(new Text('foo'));
38 39 40

      TestFailure failure;
      try {
41
        expect(find.text('foo', skipOffstage: false), findsNothing);
42 43 44 45 46 47 48 49 50 51
      } catch(e) {
        failure = e;
      }

      expect(failure, isNotNull);
      String message = failure.message;

      expect(message, contains('Expected: no matching nodes in the widget tree\n'));
      expect(message, contains('Actual: ?:<exactly one widget with text "foo": Text("foo")>\n'));
      expect(message, contains('Which: means one was found but none were expected\n'));
52
    });
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70

    testWidgets('fails with a descriptive message when skipping', (WidgetTester tester) async {
      await tester.pumpWidget(new Text('foo'));

      TestFailure failure;
      try {
        expect(find.text('foo'), findsNothing);
      } catch(e) {
        failure = e;
      }

      expect(failure, isNotNull);
      String message = failure.message;

      expect(message, contains('Expected: no matching nodes in the widget tree\n'));
      expect(message, contains('Actual: ?:<exactly one widget with text "foo" (ignoring offstage widgets): Text("foo")>\n'));
      expect(message, contains('Which: means one was found but none were expected\n'));
    });
71 72 73 74 75

    testWidgets('pumping', (WidgetTester tester) async {
      await tester.pumpWidget(new Text('foo'));
      int count;

76 77 78 79
      AnimationController test = new AnimationController(
        duration: const Duration(milliseconds: 5100),
        vsync: tester,
      );
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104
      count = await tester.pumpUntilNoTransientCallbacks(const Duration(seconds: 1));
      expect(count, 0);

      test.forward(from: 0.0);
      count = await tester.pumpUntilNoTransientCallbacks(const Duration(seconds: 1));
      // 1 frame at t=0, starting the animation
      // 1 frame at t=1
      // 1 frame at t=2
      // 1 frame at t=3
      // 1 frame at t=4
      // 1 frame at t=5
      // 1 frame at t=6, ending the animation
      expect(count, 7);

      test.forward(from: 0.0);
      await tester.pump(); // starts the animation
      count = await tester.pumpUntilNoTransientCallbacks(const Duration(seconds: 1));
      expect(count, 6);

      test.forward(from: 0.0);
      await tester.pump(); // starts the animation
      await tester.pump(); // has no effect
      count = await tester.pumpUntilNoTransientCallbacks(const Duration(seconds: 1));
      expect(count, 6);
    });
105
  });
106

107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139
  group('find.byElementPredicate', () {
    testWidgets('fails with a custom description in the message', (WidgetTester tester) async {
      await tester.pumpWidget(new Text('foo'));

      String customDescription = 'custom description';
      TestFailure failure;
      try {
        expect(find.byElementPredicate((_) => false, description: customDescription), findsOneWidget);
      } catch(e) {
        failure = e;
      }

      expect(failure, isNotNull);
      expect(failure.message, contains('Actual: ?:<zero widgets with $customDescription'));
    });
  });

  group('find.byWidgetPredicate', () {
    testWidgets('fails with a custom description in the message', (WidgetTester tester) async {
      await tester.pumpWidget(new Text('foo'));

      String customDescription = 'custom description';
      TestFailure failure;
      try {
        expect(find.byWidgetPredicate((_) => false, description: customDescription), findsOneWidget);
      } catch(e) {
        failure = e;
      }

      expect(failure, isNotNull);
      expect(failure.message, contains('Actual: ?:<zero widgets with $customDescription'));
    });
  });
140
}