widget_tester_test.dart 8.25 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
    testWidgets('finds exactly one widget', (WidgetTester tester) async {
11
      await tester.pumpWidget(const 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
    testWidgets('fails with a descriptive message', (WidgetTester tester) async {
37
      await tester.pumpWidget(const 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

    testWidgets('fails with a descriptive message when skipping', (WidgetTester tester) async {
55
      await tester.pumpWidget(const Text('foo'));
56 57 58 59 60 61 62 63 64

      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

    testWidgets('pumping', (WidgetTester tester) async {
73
      await tester.pumpWidget(const Text('foo'));
74 75
      int count;

76
      final AnimationController test = new AnimationController(
77 78 79
        duration: const Duration(milliseconds: 5100),
        vsync: tester,
      );
80 81
      count = await tester.pumpAndSettle(const Duration(seconds: 1));
      expect(count, 1); // it always pumps at least one frame
82 83

      test.forward(from: 0.0);
84
      count = await tester.pumpAndSettle(const Duration(seconds: 1));
85 86 87 88 89 90 91 92 93 94 95
      // 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
96
      count = await tester.pumpAndSettle(const Duration(seconds: 1));
97 98 99 100 101
      expect(count, 6);

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

107 108
  group('find.byElementPredicate', () {
    testWidgets('fails with a custom description in the message', (WidgetTester tester) async {
109
      await tester.pumpWidget(const Text('foo'));
110

111
      final String customDescription = 'custom description';
112 113 114 115 116 117 118 119 120 121 122 123 124 125
      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 {
126
      await tester.pumpWidget(const Text('foo'));
127

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

  group('find.descendant', () {
    testWidgets('finds one descendant', (WidgetTester tester) async {
      await tester.pumpWidget(new Row(children: <Widget>[
144
        new Column(children: <Text>[const Text('foo'), const Text('bar')])
145 146 147 148 149 150 151 152 153 154
      ]));

      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>[
155 156
        new Column(children: <Text>[const Text('foo'), const Text('bar')]),
        new Column(children: <Text>[const Text('foo'), const Text('bar')]),
157 158 159 160 161 162 163 164 165 166
      ]));

      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>[
167 168
        new Column(children: <Text>[const Text('foo')]),
        const Text('bar')
169 170 171 172 173 174 175 176 177 178 179 180 181 182
      ]));

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

      expect(failure, isNotNull);
      expect(
183 184 185
        failure.message,
        contains('Actual: ?:<zero widgets with text "bar" that has ancestor(s) with type Column with text "foo"')
      );
186
    });
187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210

    testWidgets('Root not matched by default', (WidgetTester tester) async {
      await tester.pumpWidget(new Row(children: <Widget>[
        new Column(children: <Text>[const Text('foo'), const Text('bar')])
      ]));

      expect(find.descendant(
        of: find.widgetWithText(Row, 'foo'),
        matching: find.byType(Row),
      ), findsNothing);
    });

    testWidgets('Match the root', (WidgetTester tester) async {
      await tester.pumpWidget(new Row(children: <Widget>[
        new Column(children: <Text>[const Text('foo'), const Text('bar')])
      ]));

      expect(find.descendant(
        of: find.widgetWithText(Row, 'foo'),
        matching: find.byType(Row),
        matchRoot: true,
      ), findsOneWidget);
    });

211
  });
212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227

  testWidgets('hasRunningAnimations control test', (WidgetTester tester) async {
    final AnimationController controller = new AnimationController(
      duration: const Duration(seconds: 1),
      vsync: const TestVSync()
    );
    expect(tester.hasRunningAnimations, isFalse);
    controller.forward();
    expect(tester.hasRunningAnimations, isTrue);
    controller.stop();
    expect(tester.hasRunningAnimations, isFalse);
    controller.forward();
    expect(tester.hasRunningAnimations, isTrue);
    await tester.pumpAndSettle();
    expect(tester.hasRunningAnimations, isFalse);
  });
228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247

  testWidgets('pumpAndSettle control test', (WidgetTester tester) async {
    final AnimationController controller = new AnimationController(
      duration: const Duration(minutes: 525600),
      vsync: const TestVSync()
    );
    expect(await tester.pumpAndSettle(), 1);
    controller.forward();
    try {
      await tester.pumpAndSettle();
      expect(true, isFalse);
    } catch (e) {
      expect(e, isFlutterError);
    }
    controller.stop();
    expect(await tester.pumpAndSettle(), 1);
    controller.duration = const Duration(seconds: 1);
    controller.forward();
    expect(await tester.pumpAndSettle(const Duration(milliseconds: 300)), 5); // 0, 300, 600, 900, 1200ms
  });
248
}