Skip to content

Commit

Permalink
[webview_flutter_wkwebview] Repeatedly pump WebViews until one is gar…
Browse files Browse the repository at this point in the history
…bage collected (#4662)

This should improve the chance that a `WKWebView` is garbage collected by repeatedly pumping `WebViewWidgets`.
  • Loading branch information
bparrishMines authored Aug 8, 2023
1 parent 0042da7 commit 269118d
Showing 1 changed file with 32 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -69,47 +69,50 @@ Future<void> main() async {
testWidgets(
'WKWebView is released by garbage collection',
(WidgetTester tester) async {
final Completer<void> webViewGCCompleter = Completer<void>();
bool aWebViewHasBeenGarbageCollected = false;

late final InstanceManager instanceManager;
instanceManager =
InstanceManager(onWeakReferenceRemoved: (int identifier) {
final Copyable instance =
instanceManager.getInstanceWithWeakReference(identifier)!;
if (instance is WKWebView && !webViewGCCompleter.isCompleted) {
webViewGCCompleter.complete();
if (!aWebViewHasBeenGarbageCollected) {
final Copyable instance =
instanceManager.getInstanceWithWeakReference(identifier)!;
if (instance is WKWebView) {
aWebViewHasBeenGarbageCollected = true;
}
}
});

await tester.pumpWidget(
Builder(
builder: (BuildContext context) {
return PlatformWebViewWidget(
WebKitWebViewWidgetCreationParams(
instanceManager: instanceManager,
controller: PlatformWebViewController(
WebKitWebViewControllerCreationParams(
instanceManager: instanceManager,
// Wait for any WebView to be garbage collected.
while (!aWebViewHasBeenGarbageCollected) {
await tester.pumpWidget(
Builder(
builder: (BuildContext context) {
return PlatformWebViewWidget(
WebKitWebViewWidgetCreationParams(
instanceManager: instanceManager,
controller: PlatformWebViewController(
WebKitWebViewControllerCreationParams(
instanceManager: instanceManager,
),
),
),
),
).build(context);
},
),
);
await tester.pumpAndSettle();

await tester.pumpWidget(Container());

// Force garbage collection.
await IntegrationTestWidgetsFlutterBinding.instance
.watchPerformance(() async {
).build(context);
},
),
);
await tester.pumpAndSettle();
});

await expectLater(webViewGCCompleter.future, completes);
await tester.pumpWidget(Container());

// Force garbage collection.
await IntegrationTestWidgetsFlutterBinding.instance
.watchPerformance(() async {
await tester.pumpAndSettle();
});
}
},
timeout: const Timeout(Duration(seconds: 10)),
timeout: const Timeout(Duration(seconds: 30)),
);

testWidgets('loadRequest', (WidgetTester tester) async {
Expand Down

0 comments on commit 269118d

Please sign in to comment.