1// Copyright 2014 The Flutter Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5import 'dart:async';
6
7import 'package:flutter/foundation.dart';
8import 'package:flutter_test/flutter_test.dart';
9
10final List<FlutterErrorDetails> errors = <FlutterErrorDetails>[];
11
12Future<void> testExecutable(FutureOr<void> Function() testMain) async {
13 reportTestException = (FlutterErrorDetails details, String testDescription) {
14 errors.add(details);
15 };
16
17 // The error that the test throws in their run methods below will be forwarded
18 // to our exception handler above and do not cause the test to fail. The
19 // tearDown method then checks that the test threw the expected exception.
20 await testMain();
21}
22
23void pipelineOwnerTestRun() {
24 testWidgets('open SemanticsHandle from PipelineOwner fails test', (WidgetTester tester) async {
25 final int outstandingHandles = tester.binding.debugOutstandingSemanticsHandles;
26 tester.binding.ensureSemantics();
27 expect(tester.binding.debugOutstandingSemanticsHandles, outstandingHandles + 1);
28 // SemanticsHandle is not disposed on purpose to verify in tearDown that
29 // the test failed due to an active SemanticsHandle.
30 });
31
32 tearDown(() {
33 expect(errors, hasLength(1));
34 expect(errors.single.toString(), contains('SemanticsHandle was active at the end of the test'));
35 });
36}
37
38void semanticsBindingTestRun() {
39 testWidgets('open SemanticsHandle from SemanticsBinding fails test', (WidgetTester tester) async {
40 final int outstandingHandles = tester.binding.debugOutstandingSemanticsHandles;
41 tester.binding.ensureSemantics();
42 expect(tester.binding.debugOutstandingSemanticsHandles, outstandingHandles + 1);
43 // SemanticsHandle is not disposed on purpose to verify in tearDown that
44 // the test failed due to an active SemanticsHandle.
45 });
46
47 tearDown(() {
48 expect(errors, hasLength(1));
49 expect(errors.single.toString(), contains('SemanticsHandle was active at the end of the test'));
50 });
51}
52
53void failingTestTestRun() {
54 testWidgets('open SemanticsHandle from SemanticsBinding fails test', (WidgetTester tester) async {
55 final int outstandingHandles = tester.binding.debugOutstandingSemanticsHandles;
56 tester.binding.ensureSemantics();
57 expect(tester.binding.debugOutstandingSemanticsHandles, outstandingHandles + 1);
58
59 // Failing expectation to verify that an open semantics handle doesn't
60 // cause any cascading failures and only the failing expectation is
61 // reported.
62 expect(1, equals(2));
63 fail('The test should never have gotten this far.');
64 });
65
66 tearDown(() {
67 expect(errors, hasLength(1));
68 expect(errors.single.toString(), contains('Expected: <2>'));
69 expect(errors.single.toString(), contains('Actual: <1>'));
70 });
71}
72