Skip to content

Commit 577ad2e

Browse files
authored
Fix error when resetting configurations in tear down phase (#114468)
* move _verifyInvariants * fix * fix (mimic import test_api.dart) * fix * Update binding.dart * add tests * try to move * Revert "try to move" This reverts commit d3c466d226cc1abe195af83a2630c70f08e25d7d. * Update binding.dart
1 parent c102bf4 commit 577ad2e

File tree

2 files changed

+27
-0
lines changed

2 files changed

+27
-0
lines changed

packages/flutter_test/lib/src/binding.dart

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import 'package:flutter/services.dart';
1515
import 'package:flutter/widgets.dart';
1616
import 'package:stack_trace/stack_trace.dart' as stack_trace;
1717
import 'package:test_api/expect.dart' show fail;
18+
import 'package:test_api/scaffolding.dart'; // ignore: deprecated_member_use
1819
import 'package:test_api/test_api.dart' as test_package show Timeout; // ignore: deprecated_member_use
1920
import 'package:vector_math/vector_math_64.dart';
2021

@@ -919,6 +920,13 @@ abstract class TestWidgetsFlutterBinding extends BindingBase
919920
// So that we can assert that it remains the same after the test finishes.
920921
_beforeTestCheckIntrinsicSizes = debugCheckIntrinsicSizes;
921922

923+
bool shouldTearDownVerifyInvariants = false;
924+
addTearDown(() {
925+
if (shouldTearDownVerifyInvariants) {
926+
_verifyTearDownInvariants();
927+
}
928+
});
929+
922930
runApp(Container(key: UniqueKey(), child: _preTestMessage)); // Reset the tree to a known state.
923931
await pump();
924932
// Pretend that the first frame produced in the test body is the first frame
@@ -949,6 +957,7 @@ abstract class TestWidgetsFlutterBinding extends BindingBase
949957
_verifyErrorWidgetBuilderUnset(errorWidgetBuilderBeforeTest);
950958
_verifyShouldPropagateDevicePointerEventsUnset(shouldPropagateDevicePointerEventsBeforeTest);
951959
_verifyInvariants();
960+
shouldTearDownVerifyInvariants = true;
952961
}
953962

954963
assert(inTest);
@@ -958,6 +967,11 @@ abstract class TestWidgetsFlutterBinding extends BindingBase
958967
late bool _beforeTestCheckIntrinsicSizes;
959968

960969
void _verifyInvariants() {
970+
// subclasses such as AutomatedTestWidgetsFlutterBinding overrides this
971+
// to perform more verifications.
972+
}
973+
974+
void _verifyTearDownInvariants() {
961975
assert(debugAssertNoTransientCallbacks(
962976
'An animation is still running even after the widget tree was disposed.'
963977
));

packages/flutter_test/test/bindings_test.dart

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ library;
1212
import 'dart:async';
1313
import 'dart:io';
1414

15+
import 'package:flutter/scheduler.dart';
1516
import 'package:flutter/services.dart';
1617
import 'package:flutter/widgets.dart';
1718
import 'package:flutter_test/flutter_test.dart';
@@ -102,4 +103,16 @@ void main() {
102103
});
103104
expect(responded, true);
104105
});
106+
107+
group('should be able to reset values in either tearDown or end of function', () {
108+
testWidgets('addTearDown should work', (WidgetTester tester) async {
109+
timeDilation = 2;
110+
addTearDown(() => timeDilation = 1);
111+
});
112+
113+
testWidgets('directly reset should work', (WidgetTester tester) async {
114+
timeDilation = 2;
115+
timeDilation = 1;
116+
});
117+
});
105118
}

0 commit comments

Comments
 (0)