Skip to content

Commit

Permalink
Fix error when resetting configurations in tear down phase (#114468)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
fzyzcjy authored Feb 15, 2023
1 parent c102bf4 commit 577ad2e
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 0 deletions.
14 changes: 14 additions & 0 deletions packages/flutter_test/lib/src/binding.dart
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import 'package:flutter/services.dart';
import 'package:flutter/widgets.dart';
import 'package:stack_trace/stack_trace.dart' as stack_trace;
import 'package:test_api/expect.dart' show fail;
import 'package:test_api/scaffolding.dart'; // ignore: deprecated_member_use
import 'package:test_api/test_api.dart' as test_package show Timeout; // ignore: deprecated_member_use
import 'package:vector_math/vector_math_64.dart';

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

bool shouldTearDownVerifyInvariants = false;
addTearDown(() {
if (shouldTearDownVerifyInvariants) {
_verifyTearDownInvariants();
}
});

runApp(Container(key: UniqueKey(), child: _preTestMessage)); // Reset the tree to a known state.
await pump();
// Pretend that the first frame produced in the test body is the first frame
Expand Down Expand Up @@ -949,6 +957,7 @@ abstract class TestWidgetsFlutterBinding extends BindingBase
_verifyErrorWidgetBuilderUnset(errorWidgetBuilderBeforeTest);
_verifyShouldPropagateDevicePointerEventsUnset(shouldPropagateDevicePointerEventsBeforeTest);
_verifyInvariants();
shouldTearDownVerifyInvariants = true;
}

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

void _verifyInvariants() {
// subclasses such as AutomatedTestWidgetsFlutterBinding overrides this
// to perform more verifications.
}

void _verifyTearDownInvariants() {
assert(debugAssertNoTransientCallbacks(
'An animation is still running even after the widget tree was disposed.'
));
Expand Down
13 changes: 13 additions & 0 deletions packages/flutter_test/test/bindings_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ library;
import 'dart:async';
import 'dart:io';

import 'package:flutter/scheduler.dart';
import 'package:flutter/services.dart';
import 'package:flutter/widgets.dart';
import 'package:flutter_test/flutter_test.dart';
Expand Down Expand Up @@ -102,4 +103,16 @@ void main() {
});
expect(responded, true);
});

group('should be able to reset values in either tearDown or end of function', () {
testWidgets('addTearDown should work', (WidgetTester tester) async {
timeDilation = 2;
addTearDown(() => timeDilation = 1);
});

testWidgets('directly reset should work', (WidgetTester tester) async {
timeDilation = 2;
timeDilation = 1;
});
});
}

0 comments on commit 577ad2e

Please sign in to comment.