test_async_utils_test.dart 13.5 KB
Newer Older
1 2 3 4
// 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.

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
import 'package:test_api/test_api.dart' as real_test show expect;
11 12 13 14 15 16

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

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

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

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

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

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

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

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

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

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

218 219 220 221 222 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
    } on String catch (s) {
      expect(s, 'Hello');
    }
  });

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