test_async_utils_test.dart 13.5 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
Future<Object> _guardedThrower() {
  return TestAsyncUtils.guard<Object>(() async {
34
    throw 'Hello';
35 36 37 38 39
  });
}

void main() {
  test('TestAsyncUtils - one class', () async {
40
    final TestAPI testAPI = TestAPI();
41
    Future<Object?>? f1, f2;
42 43 44 45 46
    f1 = testAPI.testGuard1();
    try {
      f2 = testAPI.testGuard2();
      throw 'unexpectedly did not throw';
    } on FlutterError catch (e) {
47
      final List<String> lines = e.message.split('\n');
48 49 50 51 52 53 54
      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:');
55 56 57 58 59 60 61
      real_test.expect(lines.length, greaterThan(6));
    }
    expect(await f1, isNull);
    expect(f2, isNull);
  });

  test('TestAsyncUtils - two classes, all callers in superclass', () async {
62
    final TestAPI testAPI = TestAPISubclass();
63
    Future<Object?>? f1, f2;
64 65 66 67 68
    f1 = testAPI.testGuard1();
    try {
      f2 = testAPI.testGuard2();
      throw 'unexpectedly did not throw';
    } on FlutterError catch (e) {
69
      final List<String> lines = e.message.split('\n');
70 71 72 73 74 75 76 77
      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));
78 79 80 81 82 83
    }
    expect(await f1, isNull);
    expect(f2, isNull);
  });

  test('TestAsyncUtils - two classes, mixed callers', () async {
84
    final TestAPISubclass testAPI = TestAPISubclass();
85
    Future<Object?>? f1, f2;
86 87 88 89 90
    f1 = testAPI.testGuard1();
    try {
      f2 = testAPI.testGuard3();
      throw 'unexpectedly did not throw';
    } on FlutterError catch (e) {
91
      final List<String> lines = e.message.split('\n');
92 93 94 95 96 97 98 99
      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));
100 101 102 103 104 105
    }
    expect(await f1, isNull);
    expect(f2, isNull);
  });

  test('TestAsyncUtils - expect() catches pending async work', () async {
106
    final TestAPI testAPI = TestAPISubclass();
107
    Future<Object?>? f1;
108 109 110 111 112
    f1 = testAPI.testGuard1();
    try {
      flutter_test.expect(0, 0);
      throw 'unexpectedly did not throw';
    } on FlutterError catch (e) {
113
      final List<String> lines = e.message.split('\n');
114 115 116 117 118 119 120 121
      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:');
122 123 124 125 126 127
      real_test.expect(lines.length, greaterThan(7));
    }
    expect(await f1, isNull);
  });

  testWidgets('TestAsyncUtils - expect() catches pending async work', (WidgetTester tester) async {
128
    Future<Object?>? f1, f2;
129 130 131 132 133
    try {
      f1 = tester.pump();
      f2 = tester.pump();
      throw 'unexpectedly did not throw';
    } on FlutterError catch (e) {
134
      final List<String> lines = e.message.split('\n');
135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154
      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
155 156 157 158 159 160
      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>());
161
      final DiagnosticsStackTrace stackTraceProperty = information[5] as DiagnosticsStackTrace;
162
      real_test.expect(stackTraceProperty.name, '\nWhen the first method was called, this was the stack');
Dan Field's avatar
Dan Field committed
163
      real_test.expect(stackTraceProperty.value, isA<StackTrace>());
164 165 166 167 168 169
    }
    await f1;
    await f2;
  });

  testWidgets('TestAsyncUtils - expect() catches pending async work', (WidgetTester tester) async {
170
    Future<Object?>? f1;
171 172 173 174 175
    try {
      f1 = tester.pump();
      TestAsyncUtils.verifyAllScopesClosed();
      throw 'unexpectedly did not throw';
    } on FlutterError catch (e) {
176
      final List<String> lines = e.message.split('\n');
177 178 179 180 181 182 183 184 185 186 187 188 189
      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);
190 191 192 193 194
    }
    await f1;
  });

  testWidgets('TestAsyncUtils - expect() catches pending async work', (WidgetTester tester) async {
195
    Future<Object?>? f1;
196 197 198 199 200
    try {
      f1 = tester.pump();
      TestAsyncUtils.verifyAllScopesClosed();
      throw 'unexpectedly did not throw';
    } on FlutterError catch (e) {
201
      final List<String> lines = e.message.split('\n');
202 203 204 205 206 207 208 209 210 211 212 213 214
      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);
215 216 217 218
    }
    await f1;
  });

219 220 221 222 223 224 225 226 227
  testWidgets('TestAsyncUtils - guard body can throw', (WidgetTester tester) async {
    try {
      await _guardedThrower();
      expect(false, true); // _guardedThrower should throw and we shouldn't reach here
    } on String catch (s) {
      expect(s, 'Hello');
    }
  });

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