File size: 16,010 Bytes
88b9d23
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b0336a8
 
 
88b9d23
b0336a8
88b9d23
b0336a8
 
 
88b9d23
 
 
 
 
b0336a8
 
 
88b9d23
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b0336a8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
88b9d23
b0336a8
 
 
 
88b9d23
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b0336a8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
88b9d23
b0336a8
 
 
 
88b9d23
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b0336a8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
88b9d23
b0336a8
 
 
 
88b9d23
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b0336a8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
88b9d23
b0336a8
 
 
 
88b9d23
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b0336a8
 
 
 
 
 
 
 
 
 
 
 
 
 
88b9d23
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b0336a8
 
 
 
88b9d23
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b0336a8
 
 
 
 
 
 
 
 
 
 
 
88b9d23
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b0336a8
 
 
 
 
 
 
 
 
 
 
 
88b9d23
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
// Copyright 2014 The Flutter 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/foundation.dart';
import 'package:flutter_test/flutter_test.dart';

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

import 'package:matcher/expect.dart' as matcher show expect;

// We have to use matcher'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 {
  Future<Object?> testGuard1() {
    return TestAsyncUtils.guard<Object?>(() async {
      return null;
    });
  }

  Future<Object?> testGuard2() {
    return TestAsyncUtils.guard<Object?>(() async {
      return null;
    });
  }
}

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

class RecognizableTestException implements Exception {
  const RecognizableTestException();
}

Future<Object> _guardedThrower() {
  return TestAsyncUtils.guard<Object>(() async {
    throw const RecognizableTestException();
  });
}

void main() {
  test('TestAsyncUtils - one class', () async {
    final TestAPI testAPI = TestAPI();
    Future<Object?>? f1, f2;
    f1 = testAPI.testGuard1();
    try {
      f2 = testAPI.testGuard2();
      fail('unexpectedly did not throw');
    } on FlutterError catch (e) {
      final List<String> lines = e.message.split('\n');
      matcher.expect(lines[0], 'Guarded function conflict.');
      matcher.expect(lines[1], 'You must use "await" with all Future-returning test APIs.');
      matcher.expect(
        lines[2],
        matches(
          r'The guarded method "testGuard1" from class TestAPI was called from .*test_async_utils_test.dart on line [0-9]+\.',
        ),
      );
      matcher.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]+\.',
        ),
      );
      matcher.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.',
      );
      matcher.expect(lines[5], '');
      matcher.expect(
        lines[6],
        'When the first method (TestAPI.testGuard1) was called, this was the stack:',
      );
      matcher.expect(lines.length, greaterThan(6));
    }
    expect(await f1, isNull);
    expect(f2, isNull);
  }, skip: kIsWeb); // [intended] depends on platform-specific stack traces.

  test('TestAsyncUtils - two classes, all callers in superclass', () async {
    final TestAPI testAPI = TestAPISubclass();
    Future<Object?>? f1, f2;
    f1 = testAPI.testGuard1();
    try {
      f2 = testAPI.testGuard2();
      fail('unexpectedly did not throw');
    } on FlutterError catch (e) {
      final List<String> lines = e.message.split('\n');
      matcher.expect(lines[0], 'Guarded function conflict.');
      matcher.expect(lines[1], 'You must use "await" with all Future-returning test APIs.');
      matcher.expect(
        lines[2],
        matches(
          r'^The guarded method "testGuard1" from class TestAPI was called from .*test_async_utils_test.dart on line [0-9]+\.$',
        ),
      );
      matcher.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]+\.$',
        ),
      );
      matcher.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.',
      );
      matcher.expect(lines[5], '');
      matcher.expect(
        lines[6],
        'When the first method (TestAPI.testGuard1) was called, this was the stack:',
      );
      matcher.expect(lines.length, greaterThan(7));
    }
    expect(await f1, isNull);
    expect(f2, isNull);
  }, skip: kIsWeb); // [intended] depends on platform-specific stack traces.

  test('TestAsyncUtils - two classes, mixed callers', () async {
    final TestAPISubclass testAPI = TestAPISubclass();
    Future<Object?>? f1, f2;
    f1 = testAPI.testGuard1();
    try {
      f2 = testAPI.testGuard3();
      fail('unexpectedly did not throw');
    } on FlutterError catch (e) {
      final List<String> lines = e.message.split('\n');
      matcher.expect(lines[0], 'Guarded function conflict.');
      matcher.expect(lines[1], 'You must use "await" with all Future-returning test APIs.');
      matcher.expect(
        lines[2],
        matches(
          r'^The guarded method "testGuard1" from class TestAPI was called from .*test_async_utils_test.dart on line [0-9]+\.$',
        ),
      );
      matcher.expect(
        lines[3],
        matches(
          r'^Then, the "testGuard3" method from class TestAPISubclass was called from .*test_async_utils_test.dart on line [0-9]+\.$',
        ),
      );
      matcher.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.',
      );
      matcher.expect(lines[5], '');
      matcher.expect(
        lines[6],
        'When the first method (TestAPI.testGuard1) was called, this was the stack:',
      );
      matcher.expect(lines.length, greaterThan(7));
    }
    expect(await f1, isNull);
    expect(f2, isNull);
  }, skip: kIsWeb); // [intended] depends on platform-specific stack traces.

  test('TestAsyncUtils - expect() catches pending async work', () async {
    final TestAPI testAPI = TestAPISubclass();
    Future<Object?>? f1;
    f1 = testAPI.testGuard1();
    try {
      flutter_test.expect(0, 0);
      fail('unexpectedly did not throw');
    } on FlutterError catch (e) {
      final List<String> lines = e.message.split('\n');
      matcher.expect(lines[0], 'Guarded function conflict.');
      matcher.expect(lines[1], 'You must use "await" with all Future-returning test APIs.');
      matcher.expect(
        lines[2],
        matches(
          r'^The guarded method "testGuard1" from class TestAPI was called from .*test_async_utils_test.dart on line [0-9]+\.$',
        ),
      );
      matcher.expect(
        lines[3],
        matches(
          r'^Then, the "expect" function was called from .*test_async_utils_test.dart on line [0-9]+\.$',
        ),
      );
      matcher.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.',
      );
      matcher.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.',
      );
      matcher.expect(lines[6], '');
      matcher.expect(
        lines[7],
        'When the first method (TestAPI.testGuard1) was called, this was the stack:',
      );
      matcher.expect(lines.length, greaterThan(7));
    }
    expect(await f1, isNull);
  }, skip: kIsWeb); // [intended] depends on platform-specific stack traces.

  testWidgets('TestAsyncUtils - expect() catches pending async work', (WidgetTester tester) async {
    Future<Object?>? f1, f2;
    try {
      f1 = tester.pump();
      f2 = tester.pump();
      fail('unexpectedly did not throw');
    } on FlutterError catch (e) {
      final List<String> lines = e.message.split('\n');
      matcher.expect(lines[0], 'Guarded function conflict.');
      matcher.expect(lines[1], 'You must use "await" with all Future-returning test APIs.');
      matcher.expect(
        lines[2],
        matches(
          r'^The guarded method "pump" from class WidgetTester was called from .*test_async_utils_test.dart on line [0-9]+\.$',
        ),
      );
      matcher.expect(
        lines[3],
        matches(r'^Then, it was called from .*test_async_utils_test.dart on line [0-9]+\.$'),
      );
      matcher.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.',
      );
      matcher.expect(lines[5], '');
      matcher.expect(lines[6], 'When the first method was called, this was the stack:');
      matcher.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;
      matcher.expect(information.length, 6);
      matcher.expect(information[0].level, DiagnosticLevel.summary);
      matcher.expect(information[1].level, DiagnosticLevel.hint);
      matcher.expect(information[2].level, DiagnosticLevel.info);
      matcher.expect(information[3].level, DiagnosticLevel.info);
      matcher.expect(information[4].level, DiagnosticLevel.info);
      matcher.expect(information[5].level, DiagnosticLevel.info);
      matcher.expect(information[0], isA<DiagnosticsProperty<void>>());
      matcher.expect(information[1], isA<DiagnosticsProperty<void>>());
      matcher.expect(information[2], isA<DiagnosticsProperty<void>>());
      matcher.expect(information[3], isA<DiagnosticsProperty<void>>());
      matcher.expect(information[4], isA<DiagnosticsProperty<void>>());
      matcher.expect(information[5], isA<DiagnosticsStackTrace>());
      final DiagnosticsStackTrace stackTraceProperty = information[5] as DiagnosticsStackTrace;
      matcher.expect(
        stackTraceProperty.name,
        '\nWhen the first method was called, this was the stack',
      );
      matcher.expect(stackTraceProperty.value, isA<StackTrace>());
    }
    await f1;
    await f2;
  }, skip: kIsWeb); // [intended] depends on platform-specific stack traces.

  testWidgets('TestAsyncUtils - expect() catches pending async work', (WidgetTester tester) async {
    Future<Object?>? f1;
    try {
      f1 = tester.pump();
      TestAsyncUtils.verifyAllScopesClosed();
      fail('unexpectedly did not throw');
    } on FlutterError catch (e) {
      final List<String> lines = e.message.split('\n');
      matcher.expect(lines[0], 'Asynchronous call to guarded function leaked.');
      matcher.expect(lines[1], 'You must use "await" with all Future-returning test APIs.');
      matcher.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\.$',
        ),
      );
      matcher.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\.',
        ),
      );
      matcher.expect(lines.length, 4);
      final DiagnosticPropertiesBuilder propertiesBuilder = DiagnosticPropertiesBuilder();
      e.debugFillProperties(propertiesBuilder);
      final List<DiagnosticsNode> information = propertiesBuilder.properties;
      matcher.expect(information.length, 4);
      matcher.expect(information[0].level, DiagnosticLevel.summary);
      matcher.expect(information[1].level, DiagnosticLevel.hint);
      matcher.expect(information[2].level, DiagnosticLevel.info);
      matcher.expect(information[3].level, DiagnosticLevel.info);
    }
    await f1;
  }, skip: kIsWeb); // [intended] depends on platform-specific stack traces.

  testWidgets('TestAsyncUtils - expect() catches pending async work', (WidgetTester tester) async {
    Future<Object?>? f1;
    try {
      f1 = tester.pump();
      TestAsyncUtils.verifyAllScopesClosed();
      fail('unexpectedly did not throw');
    } on FlutterError catch (e) {
      final List<String> lines = e.message.split('\n');
      matcher.expect(lines[0], 'Asynchronous call to guarded function leaked.');
      matcher.expect(lines[1], 'You must use "await" with all Future-returning test APIs.');
      matcher.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\.$',
        ),
      );
      matcher.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\.',
        ),
      );
      matcher.expect(lines.length, 4);
      final DiagnosticPropertiesBuilder propertiesBuilder = DiagnosticPropertiesBuilder();
      e.debugFillProperties(propertiesBuilder);
      final List<DiagnosticsNode> information = propertiesBuilder.properties;
      matcher.expect(information.length, 4);
      matcher.expect(information[0].level, DiagnosticLevel.summary);
      matcher.expect(information[1].level, DiagnosticLevel.hint);
      matcher.expect(information[2].level, DiagnosticLevel.info);
      matcher.expect(information[3].level, DiagnosticLevel.info);
    }
    await f1;
  }, skip: kIsWeb); // [intended] depends on platform-specific stack traces.

  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 RecognizableTestException catch (e) {
      expect(e, const RecognizableTestException());
    }
  });

  test('TestAsyncUtils - web', () async {
    final TestAPI testAPI = TestAPI();
    Future<Object?>? f1, f2;
    f1 = testAPI.testGuard1();
    try {
      f2 = testAPI.testGuard2();
      fail('unexpectedly did not throw');
    } on FlutterError catch (e) {
      final List<String> lines = e.message.split('\n');
      matcher.expect(lines[0], 'Guarded function conflict.');
      matcher.expect(lines[1], 'You must use "await" with all Future-returning test APIs.');
      matcher.expect(lines[2], '');
      matcher.expect(lines[3], 'When the first function was called, this was the stack:');
      matcher.expect(lines.length, greaterThan(3));
    }
    expect(await f1, isNull);
    expect(f2, isNull);
  }, skip: !kIsWeb); // [intended] depends on platform-specific stack traces.

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