Skip to content

Commit c97791d

Browse files
authored
Reset flutter engine internal state in framework test postTest (flutter#193842)
Part of flutter#192992 Follow-up to flutter#193497. In flutter#193497, I introduced `PlatformDispatcher.resetInternalState()` to allow test harnesses to purge internal engine caches (such as Impeller's offscreen `RenderTargetCache`) between test cases to ensure test isolation. Now that the engine changes have rolled into the framework, this PR connects the framework test harness: 1. Implements `TestPlatformDispatcher.resetInternalState()` in `packages/flutter_test/lib/src/window.dart` to delegate to the underlying `PlatformDispatcher`. 2. Invokes `platformDispatcher.resetInternalState()` in `TestWidgetsFlutterBinding.postTest` alongside `ServicesBinding.instance.resetInternalState()`. 3. Adds a unit test in `packages/flutter_test/test/platform_dispatcher_test.dart` to verify delegation. ## Pre-launch Checklist - [x] I read the [Contributor Guide] and followed the process outlined there for submitting PRs. - [x] I read the [AI contribution guidelines] and understand my responsibilities, or I am not using AI tools. - [x] I read the [Tree Hygiene] wiki page, which explains my responsibilities. - [x] I read and followed the [Flutter Style Guide], including [Features we expect every widget to implement]. - [x] I signed the [CLA]. - [x] I listed at least one issue that this PR fixes in the description above. - [x] I updated/added relevant in-code documentation (doc comments with `///`). - [x] If this PR introduces a new feature or capability, I created and linked a website documentation issue or PR in [flutter/website] (or verified none is needed). - [x] I added new tests to check the change I am making, or this PR is [test-exempt]. - [x] I followed the [breaking change policy] and added [Data Driven Fixes] where supported. - [x] All existing and new tests are passing. <!-- Links --> [Contributor Guide]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#overview [AI contribution guidelines]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#ai-contribution-guidelines [Tree Hygiene]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md [test-exempt]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#tests [Flutter Style Guide]: https://github.com/flutter/flutter/blob/main/docs/contributing/Style-guide-for-Flutter-repo.md [Features we expect every widget to implement]: https://github.com/flutter/flutter/blob/main/docs/contributing/Style-guide-for-Flutter-repo.md#features-we-expect-every-widget-to-implement [CLA]: https://cla.developers.google.com/ [flutter/tests]: https://github.com/flutter/tests [flutter/website]: https://github.com/flutter/website [breaking change policy]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#handling-breaking-changes [Discord]: https://github.com/flutter/flutter/blob/main/docs/contributing/Chat.md [Data Driven Fixes]: https://github.com/flutter/flutter/blob/main/docs/contributing/Data-driven-Fixes.md
1 parent 5c138bc commit c97791d

3 files changed

Lines changed: 32 additions & 0 deletions

File tree

‎packages/flutter_test/lib/src/binding.dart‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2268,6 +2268,8 @@ abstract class TestWidgetsFlutterBinding extends BindingBase
22682268
// ignore: invalid_use_of_visible_for_testing_member
22692269
ServicesBinding.instance.resetInternalState();
22702270

2271+
platformDispatcher.resetInternalState();
2272+
22712273
if (invariantError != null) {
22722274
debugPrint = debugPrintOverride; // just in case the test overrides it -- otherwise we won't see the error!
22732275
reportTestException(invariantError, testDescription);

‎packages/flutter_test/lib/src/window.dart‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -702,6 +702,16 @@ class TestPlatformDispatcher implements PlatformDispatcher {
702702
_platformDispatcher.scheduleFrame();
703703
}
704704

705+
/// Resets the engine's internal caches.
706+
///
707+
/// This is primarily intended for test harnesses to ensure test isolation
708+
/// and prevent cached offscreen render targets or other transient engine
709+
/// state from leaking across consecutive tests.
710+
@override
711+
void resetInternalState() {
712+
_platformDispatcher.resetInternalState();
713+
}
714+
705715
@override
706716
bool get semanticsEnabled => _semanticsEnabledTestValue ?? _platformDispatcher.semanticsEnabled;
707717
bool? _semanticsEnabledTestValue;

‎packages/flutter_test/test/platform_dispatcher_test.dart‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,19 @@ void main() {
2727
expect(testPlatformDispatcher.someNewProperty, null);
2828
});
2929

30+
test('TestPlatformDispatcher delegates resetInternalState', () {
31+
final fakePlatformDispatcher = _FakePlatformDispatcher(
32+
displays: const <Display>[],
33+
views: const <FlutterView>[],
34+
);
35+
final testPlatformDispatcher = TestPlatformDispatcher(
36+
platformDispatcher: fakePlatformDispatcher,
37+
);
38+
expect(fakePlatformDispatcher.resetInternalStateCalled, isFalse);
39+
testPlatformDispatcher.resetInternalState();
40+
expect(fakePlatformDispatcher.resetInternalStateCalled, isTrue);
41+
});
42+
3043
testWidgets('TestPlatformDispatcher can fake locale', (WidgetTester tester) async {
3144
verifyPropertyFaked<Locale>(
3245
tester: tester,
@@ -344,4 +357,11 @@ class _FakePlatformDispatcher extends Fake implements PlatformDispatcher {
344357

345358
@override
346359
double get textScaleFactor => 1.0;
360+
361+
bool resetInternalStateCalled = false;
362+
363+
@override
364+
void resetInternalState() {
365+
resetInternalStateCalled = true;
366+
}
347367
}

0 commit comments

Comments
 (0)