widget_tester_test.dart 6.26 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
      } catch(e) {
        failure = e;
      }

      expect(failure, isNotNull);
24
      final String message = failure.message;
25 26 27
      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
      } catch(e) {
        failure = e;
      }

      expect(failure, isNotNull);
47
      final String message = failure.message;
48 49 50 51

      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

    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);
65
      final String message = failure.message;
66 67 68 69 70

      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
      final AnimationController test = new AnimationController(
77 78 79
        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
  group('find.byElementPredicate', () {
    testWidgets('fails with a custom description in the message', (WidgetTester tester) async {
      await tester.pumpWidget(new Text('foo'));

111
      final String customDescription = 'custom description';
112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127
      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'));

128
      final String customDescription = 'custom description';
129 130 131 132 133 134 135 136 137 138 139
      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 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187

  group('find.descendant', () {
    testWidgets('finds one descendant', (WidgetTester tester) async {
      await tester.pumpWidget(new Row(children: <Widget>[
        new Column(children: <Text>[new Text('foo'), new Text('bar')])
      ]));

      expect(find.descendant(
        of: find.widgetWithText(Row, 'foo'),
        matching: find.text('bar')
      ), findsOneWidget);
    });

    testWidgets('finds two descendants with different ancestors', (WidgetTester tester) async {
      await tester.pumpWidget(new Row(children: <Widget>[
        new Column(children: <Text>[new Text('foo'), new Text('bar')]),
        new Column(children: <Text>[new Text('foo'), new Text('bar')]),
      ]));

      expect(find.descendant(
        of: find.widgetWithText(Column, 'foo'),
        matching: find.text('bar')
      ), findsNWidgets(2));
    });

    testWidgets('fails with a descriptive message', (WidgetTester tester) async {
      await tester.pumpWidget(new Row(children: <Widget>[
        new Column(children: <Text>[new Text('foo')]),
        new Text('bar')
      ]));

      TestFailure failure;
      try {
        expect(find.descendant(
          of: find.widgetWithText(Column, 'foo'),
          matching: find.text('bar')
        ), findsOneWidget);
      } catch (e) {
        failure = e;
      }

      expect(failure, isNotNull);
      expect(
          failure.message,
          contains('Actual: ?:<zero widgets with text "bar" that has ancestor(s) with type Column with text "foo"')
        );
    });
  });
188
}