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

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

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

// 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 {
19 20
  Future<Object?> testGuard1() {
    return TestAsyncUtils.guard<Object?>(() async { return null; });
21
  }
22 23
  Future<Object?> testGuard2() {
    return TestAsyncUtils.guard<Object?>(() async { return null; });
24 25 26 27
  }
}

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

33 34
Future<Object> _guardedThrower() {
  return TestAsyncUtils.guard<Object>(() async {
35
    throw 'Hello';
36 37 38 39 40
  });
}

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

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

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

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

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

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

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

220 221 222 223 224 225 226 227 228
  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');
    }
  });

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