Reset framesEnabled to default value at the end of each test (#141844)

Reset `framesEnabled` to `true` at the end of each test as otherwise subsequent tests may fail when pumping a widget

Fixes #141835
This commit is contained in:
Aizat Azhar 2024-01-31 00:04:15 +08:00 committed by GitHub
parent e8cb029583
commit 75a2e5b493
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 23 additions and 7 deletions

View file

@ -391,11 +391,12 @@ mixin SchedulerBinding on BindingBase {
AppLifecycleState? get lifecycleState => _lifecycleState;
AppLifecycleState? _lifecycleState;
/// Allows the test framework to reset the lifecycle state back to its
/// initial value.
/// Allows the test framework to reset the lifecycle state and framesEnabled
/// back to their initial values.
@visibleForTesting
void resetLifecycleState() {
void resetInternalState() {
_lifecycleState = null;
_framesEnabled = true;
}
/// Called when the application lifecycle state changes.

View file

@ -10,7 +10,7 @@ import 'package:flutter_test/flutter_test.dart';
void main() {
testWidgets('initialLifecycleState is used to init state paused', (WidgetTester tester) async {
final TestWidgetsFlutterBinding binding = tester.binding;
binding.resetLifecycleState();
binding.resetInternalState();
// Use paused as the initial state.
binding.platformDispatcher.initialLifecycleStateTestValue = 'AppLifecycleState.paused';
binding.readTestInitialLifecycleStateFromNativeWindow(); // Re-attempt the initialization.
@ -22,7 +22,7 @@ void main() {
testWidgets('Handles all of the allowed states of AppLifecycleState', (WidgetTester tester) async {
final TestWidgetsFlutterBinding binding = tester.binding;
for (final AppLifecycleState state in AppLifecycleState.values) {
binding.resetLifecycleState();
binding.resetInternalState();
binding.platformDispatcher.initialLifecycleStateTestValue = state.toString();
binding.readTestInitialLifecycleStateFromNativeWindow();
expect(ServicesBinding.instance.lifecycleState.toString(), equals(state.toString()));

View file

@ -43,7 +43,7 @@ void main() {
listener?.dispose();
listener = null;
final TestWidgetsFlutterBinding binding = TestWidgetsFlutterBinding.instance;
binding.resetLifecycleState();
binding.resetInternalState();
binding.platformDispatcher.resetInitialLifecycleState();
assert(TestAppLifecycleListener.registerCount == 0,
'There were ${TestAppLifecycleListener.registerCount} listeners that were not disposed of in tests.');

View file

@ -397,6 +397,21 @@ void main() {
await tester.pump();
});
testWidgets('resetInternalState resets lifecycleState and framesEnabled to initial state', (WidgetTester tester) async {
// Initial state
expect(tester.binding.lifecycleState, isNull);
expect(tester.binding.framesEnabled, isTrue);
tester.binding.handleAppLifecycleStateChanged(AppLifecycleState.paused);
expect(tester.binding.lifecycleState, AppLifecycleState.paused);
expect(tester.binding.framesEnabled, isFalse);
tester.binding.resetInternalState();
expect(tester.binding.lifecycleState, isNull);
expect(tester.binding.framesEnabled, isTrue);
});
testWidgets('scheduleFrameCallback error control test', (WidgetTester tester) async {
late FlutterError error;
try {

View file

@ -1177,7 +1177,7 @@ abstract class TestWidgetsFlutterBinding extends BindingBase
// ignore: invalid_use_of_visible_for_testing_member
RendererBinding.instance.initMouseTracker();
// ignore: invalid_use_of_visible_for_testing_member
ServicesBinding.instance.resetLifecycleState();
ServicesBinding.instance.resetInternalState();
}
}