test_async_utils_test.dart 13.7 KB
Newer Older
Ian Hickson's avatar
Ian Hickson committed
1
// Copyright 2014 The Flutter Authors. All rights reserved.
2 3 4
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

5
import 'package:flutter/foundation.dart';
6 7 8
import 'package:flutter_test/flutter_test.dart';

import 'package:flutter_test/flutter_test.dart' as flutter_test show expect;
9 10

// ignore: deprecated_member_use
11
import 'package:test_api/test_api.dart' as real_test show expect;
12 13 14 15 16 17

// We have to use real_test's expect because the flutter_test expect() goes
// out of its way to check that we're not leaking APIs and the whole point
// of this test is to see how we handle leaking APIs.

class TestAPI {
18 19
  Future<Object?> testGuard1() {
    return TestAsyncUtils.guard<Object?>(() async { return null; });
20
  }
21 22
  Future<Object?> testGuard2() {
    return TestAsyncUtils.guard<Object?>(() async { return null; });
23 24 25 26
  }
}

class TestAPISubclass extends TestAPI {
27 28
  Future<Object?> testGuard3() {
    return TestAsyncUtils.guard<Object?>(() async { return null; });
29 30 31
  }
}

32 33 34 35
class RecognizableTestException implements Exception {
  const RecognizableTestException();
}

36 37
Future<Object> _guardedThrower() {
  return TestAsyncUtils.guard<Object>(() async {
38
    throw const RecognizableTestException();
39 40 41 42 43
  });
}

void main() {
  test('TestAsyncUtils - one class', () async {
44
    final TestAPI testAPI = TestAPI();
45
    Future<Object?>? f1, f2;
46 47 48
    f1 = testAPI.testGuard1();
    try {
      f2 = testAPI.testGuard2();
49
      fail('unexpectedly did not throw');
50
    } on FlutterError catch (e) {
51
      final List<String> lines = e.message.split('\n');
52 53 54 55 56 57 58
      real_test.expect(lines[0], 'Guarded function conflict.');
      real_test.expect(lines[1], 'You must use "await" with all Future-returning test APIs.');
      real_test.expect(lines[2], matches(r'The guarded method "testGuard1" from class TestAPI was called from .*test_async_utils_test.dart on line [0-9]+\.'));
      real_test.expect(lines[3], matches(r'Then, the "testGuard2" method \(also from class TestAPI\) was called from .*test_async_utils_test.dart on line [0-9]+\.'));
      real_test.expect(lines[4], 'The first method (TestAPI.testGuard1) had not yet finished executing at the time that the second method (TestAPI.testGuard2) was called. Since both are guarded, and the second was not a nested call inside the first, the first must complete its execution before the second can be called. Typically, this is achieved by putting an "await" statement in front of the call to the first.');
      real_test.expect(lines[5], '');
      real_test.expect(lines[6], 'When the first method (TestAPI.testGuard1) was called, this was the stack:');
59 60 61 62 63 64 65
      real_test.expect(lines.length, greaterThan(6));
    }
    expect(await f1, isNull);
    expect(f2, isNull);
  });

  test('TestAsyncUtils - two classes, all callers in superclass', () async {
66
    final TestAPI testAPI = TestAPISubclass();
67
    Future<Object?>? f1, f2;
68 69 70
    f1 = testAPI.testGuard1();
    try {
      f2 = testAPI.testGuard2();
71
      fail('unexpectedly did not throw');
72
    } on FlutterError catch (e) {
73
      final List<String> lines = e.message.split('\n');
74 75 76 77 78 79 80 81
      real_test.expect(lines[0], 'Guarded function conflict.');
      real_test.expect(lines[1], 'You must use "await" with all Future-returning test APIs.');
      real_test.expect(lines[2], matches(r'^The guarded method "testGuard1" from class TestAPI was called from .*test_async_utils_test.dart on line [0-9]+\.$'));
      real_test.expect(lines[3], matches(r'^Then, the "testGuard2" method \(also from class TestAPI\) was called from .*test_async_utils_test.dart on line [0-9]+\.$'));
      real_test.expect(lines[4], 'The first method (TestAPI.testGuard1) had not yet finished executing at the time that the second method (TestAPI.testGuard2) was called. Since both are guarded, and the second was not a nested call inside the first, the first must complete its execution before the second can be called. Typically, this is achieved by putting an "await" statement in front of the call to the first.');
      real_test.expect(lines[5], '');
      real_test.expect(lines[6], 'When the first method (TestAPI.testGuard1) was called, this was the stack:');
      real_test.expect(lines.length, greaterThan(7));
82 83 84 85 86 87
    }
    expect(await f1, isNull);
    expect(f2, isNull);
  });

  test('TestAsyncUtils - two classes, mixed callers', () async {
88
    final TestAPISubclass testAPI = TestAPISubclass();
89
    Future<Object?>? f1, f2;
90 91 92
    f1 = testAPI.testGuard1();
    try {
      f2 = testAPI.testGuard3();
93
      fail('unexpectedly did not throw');
94
    } on FlutterError catch (e) {
95
      final List<String> lines = e.message.split('\n');
96 97 98 99 100 101 102 103
      real_test.expect(lines[0], 'Guarded function conflict.');
      real_test.expect(lines[1], 'You must use "await" with all Future-returning test APIs.');
      real_test.expect(lines[2], matches(r'^The guarded method "testGuard1" from class TestAPI was called from .*test_async_utils_test.dart on line [0-9]+\.$'));
      real_test.expect(lines[3], matches(r'^Then, the "testGuard3" method from class TestAPISubclass was called from .*test_async_utils_test.dart on line [0-9]+\.$'));
      real_test.expect(lines[4], 'The first method (TestAPI.testGuard1) had not yet finished executing at the time that the second method (TestAPISubclass.testGuard3) was called. Since both are guarded, and the second was not a nested call inside the first, the first must complete its execution before the second can be called. Typically, this is achieved by putting an "await" statement in front of the call to the first.');
      real_test.expect(lines[5], '');
      real_test.expect(lines[6], 'When the first method (TestAPI.testGuard1) was called, this was the stack:');
      real_test.expect(lines.length, greaterThan(7));
104 105 106 107 108 109
    }
    expect(await f1, isNull);
    expect(f2, isNull);
  });

  test('TestAsyncUtils - expect() catches pending async work', () async {
110
    final TestAPI testAPI = TestAPISubclass();
111
    Future<Object?>? f1;
112 113 114
    f1 = testAPI.testGuard1();
    try {
      flutter_test.expect(0, 0);
115
      fail('unexpectedly did not throw');
116
    } on FlutterError catch (e) {
117
      final List<String> lines = e.message.split('\n');
118 119 120 121 122 123 124 125
      real_test.expect(lines[0], 'Guarded function conflict.');
      real_test.expect(lines[1], 'You must use "await" with all Future-returning test APIs.');
      real_test.expect(lines[2], matches(r'^The guarded method "testGuard1" from class TestAPI was called from .*test_async_utils_test.dart on line [0-9]+\.$'));
      real_test.expect(lines[3], matches(r'^Then, the "expect" function was called from .*test_async_utils_test.dart on line [0-9]+\.$'));
      real_test.expect(lines[4], 'The first method (TestAPI.testGuard1) had not yet finished executing at the time that the second function (expect) was called. Since both are guarded, and the second was not a nested call inside the first, the first must complete its execution before the second can be called. Typically, this is achieved by putting an "await" statement in front of the call to the first.');
      real_test.expect(lines[5], 'If you are confident that all test APIs are being called using "await", and this expect() call is not being called at the top level but is itself being called from some sort of callback registered before the testGuard1 method was called, then consider using expectSync() instead.');
      real_test.expect(lines[6], '');
      real_test.expect(lines[7], 'When the first method (TestAPI.testGuard1) was called, this was the stack:');
126 127 128 129 130 131
      real_test.expect(lines.length, greaterThan(7));
    }
    expect(await f1, isNull);
  });

  testWidgets('TestAsyncUtils - expect() catches pending async work', (WidgetTester tester) async {
132
    Future<Object?>? f1, f2;
133 134 135
    try {
      f1 = tester.pump();
      f2 = tester.pump();
136
      fail('unexpectedly did not throw');
137
    } on FlutterError catch (e) {
138
      final List<String> lines = e.message.split('\n');
139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158
      real_test.expect(lines[0], 'Guarded function conflict.');
      real_test.expect(lines[1], 'You must use "await" with all Future-returning test APIs.');
      real_test.expect(lines[2], matches(r'^The guarded method "pump" from class WidgetTester was called from .*test_async_utils_test.dart on line [0-9]+\.$'));
      real_test.expect(lines[3], matches(r'^Then, it was called from .*test_async_utils_test.dart on line [0-9]+\.$'));
      real_test.expect(lines[4], 'The first method had not yet finished executing at the time that the second method was called. Since both are guarded, and the second was not a nested call inside the first, the first must complete its execution before the second can be called. Typically, this is achieved by putting an "await" statement in front of the call to the first.');
      real_test.expect(lines[5], '');
      real_test.expect(lines[6], 'When the first method was called, this was the stack:');
      real_test.expect(lines.length, greaterThan(7));
      // TODO(jacobr): add more tests like this if they are useful.

      final DiagnosticPropertiesBuilder propertiesBuilder = DiagnosticPropertiesBuilder();
      e.debugFillProperties(propertiesBuilder);
      final List<DiagnosticsNode> information = propertiesBuilder.properties;
      real_test.expect(information.length, 6);
      real_test.expect(information[0].level, DiagnosticLevel.summary);
      real_test.expect(information[1].level, DiagnosticLevel.hint);
      real_test.expect(information[2].level, DiagnosticLevel.info);
      real_test.expect(information[3].level, DiagnosticLevel.info);
      real_test.expect(information[4].level, DiagnosticLevel.info);
      real_test.expect(information[5].level, DiagnosticLevel.info);
Dan Field's avatar
Dan Field committed
159 160 161 162 163 164
      real_test.expect(information[0], isA<DiagnosticsProperty<void>>());
      real_test.expect(information[1], isA<DiagnosticsProperty<void>>());
      real_test.expect(information[2], isA<DiagnosticsProperty<void>>());
      real_test.expect(information[3], isA<DiagnosticsProperty<void>>());
      real_test.expect(information[4], isA<DiagnosticsProperty<void>>());
      real_test.expect(information[5], isA<DiagnosticsStackTrace>());
165
      final DiagnosticsStackTrace stackTraceProperty = information[5] as DiagnosticsStackTrace;
166
      real_test.expect(stackTraceProperty.name, '\nWhen the first method was called, this was the stack');
Dan Field's avatar
Dan Field committed
167
      real_test.expect(stackTraceProperty.value, isA<StackTrace>());
168 169 170 171 172 173
    }
    await f1;
    await f2;
  });

  testWidgets('TestAsyncUtils - expect() catches pending async work', (WidgetTester tester) async {
174
    Future<Object?>? f1;
175 176 177
    try {
      f1 = tester.pump();
      TestAsyncUtils.verifyAllScopesClosed();
178
      fail('unexpectedly did not throw');
179
    } on FlutterError catch (e) {
180
      final List<String> lines = e.message.split('\n');
181 182 183 184 185 186 187 188 189 190 191 192 193
      real_test.expect(lines[0], 'Asynchronous call to guarded function leaked.');
      real_test.expect(lines[1], 'You must use "await" with all Future-returning test APIs.');
      real_test.expect(lines[2], matches(r'^The guarded method "pump" from class WidgetTester was called from .*test_async_utils_test.dart on line [0-9]+, but never completed before its parent scope closed\.$'));
      real_test.expect(lines[3], matches(r'^The guarded method "pump" from class AutomatedTestWidgetsFlutterBinding was called from [^ ]+ on line [0-9]+, but never completed before its parent scope closed\.'));
      real_test.expect(lines.length, 4);
      final DiagnosticPropertiesBuilder propertiesBuilder = DiagnosticPropertiesBuilder();
      e.debugFillProperties(propertiesBuilder);
      final List<DiagnosticsNode> information = propertiesBuilder.properties;
      real_test.expect(information.length, 4);
      real_test.expect(information[0].level, DiagnosticLevel.summary);
      real_test.expect(information[1].level, DiagnosticLevel.hint);
      real_test.expect(information[2].level, DiagnosticLevel.info);
      real_test.expect(information[3].level, DiagnosticLevel.info);
194 195 196 197 198
    }
    await f1;
  });

  testWidgets('TestAsyncUtils - expect() catches pending async work', (WidgetTester tester) async {
199
    Future<Object?>? f1;
200 201 202
    try {
      f1 = tester.pump();
      TestAsyncUtils.verifyAllScopesClosed();
203
      fail('unexpectedly did not throw');
204
    } on FlutterError catch (e) {
205
      final List<String> lines = e.message.split('\n');
206 207 208 209 210 211 212 213 214 215 216 217 218
      real_test.expect(lines[0], 'Asynchronous call to guarded function leaked.');
      real_test.expect(lines[1], 'You must use "await" with all Future-returning test APIs.');
      real_test.expect(lines[2], matches(r'^The guarded method "pump" from class WidgetTester was called from .*test_async_utils_test.dart on line [0-9]+, but never completed before its parent scope closed\.$'));
      real_test.expect(lines[3], matches(r'^The guarded method "pump" from class AutomatedTestWidgetsFlutterBinding was called from [^ ]+ on line [0-9]+, but never completed before its parent scope closed\.'));
      real_test.expect(lines.length, 4);
      final DiagnosticPropertiesBuilder propertiesBuilder = DiagnosticPropertiesBuilder();
      e.debugFillProperties(propertiesBuilder);
      final List<DiagnosticsNode> information = propertiesBuilder.properties;
      real_test.expect(information.length, 4);
      real_test.expect(information[0].level, DiagnosticLevel.summary);
      real_test.expect(information[1].level, DiagnosticLevel.hint);
      real_test.expect(information[2].level, DiagnosticLevel.info);
      real_test.expect(information[3].level, DiagnosticLevel.info);
219 220 221 222
    }
    await f1;
  });

223 224 225 226
  testWidgets('TestAsyncUtils - guard body can throw', (WidgetTester tester) async {
    try {
      await _guardedThrower();
      expect(false, true); // _guardedThrower should throw and we shouldn't reach here
227 228
    } on RecognizableTestException catch (e) {
      expect(e, const RecognizableTestException());
229 230 231
    }
  });

232 233
  // see also dev/manual_tests/test_data which contains tests run by the flutter_tools tests for 'flutter test'
}