Skip to content
This repository has been archived by the owner on Feb 22, 2023. It is now read-only.

[android_webview_controller] Fixes bug where an AndroidController couldn't be reused #6910

Merged
merged 3 commits into from
Jan 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 3.1.2

* Fixes bug where an `AndroidWebViewController` couldn't be reused with a new `WebViewWidget`.

## 3.1.1

* Fixes bug where a `AndroidNavigationDelegate` was required to load a request.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ public JavaObjectHostApiImpl(InstanceManager instanceManager) {

@Override
public void dispose(@NonNull Long identifier) {
final Object instance = instanceManager.getInstance(identifier);
if (instance instanceof WebViewHostApiImpl.WebViewPlatformView) {
((WebViewHostApiImpl.WebViewPlatformView) instance).destroy();
}
instanceManager.remove(identifier);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -102,9 +102,7 @@ public View getView() {
}

@Override
public void dispose() {
destroy();
}
public void dispose() {}

@Override
public void setWebViewClient(WebViewClient webViewClient) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -281,4 +281,37 @@ public void defaultWebChromeClientDoesNotAttemptToCommunicateWithDart() {
// This shouldn't throw an Exception.
Objects.requireNonNull(webView.getWebChromeClient()).onProgressChanged(webView, 0);
}

@Test
public void disposeDoesNotCallDestroy() {
final boolean[] destroyCalled = {false};
final WebViewPlatformView webView =
new WebViewPlatformView(mockContext, null, null) {
@Override
public void destroy() {
destroyCalled[0] = true;
}
};
webView.dispose();

assertFalse(destroyCalled[0]);
}

@Test
public void destroyWebViewWhenDisposedFromJavaObjectHostApi() {
final boolean[] destroyCalled = {false};
final WebViewPlatformView webView =
new WebViewPlatformView(mockContext, null, null) {
@Override
public void destroy() {
destroyCalled[0] = true;
}
};

testInstanceManager.addDartCreatedInstance(webView, 0);
final JavaObjectHostApiImpl javaObjectHostApi = new JavaObjectHostApiImpl(testInstanceManager);
javaObjectHostApi.dispose(0L);

assertTrue(destroyCalled[0]);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -1018,6 +1018,51 @@ Future<void> main() async {
expect(elementText, 'null');
},
);

testWidgets(
'`AndroidWebViewController` can be reused with a new `AndroidWebViewWidget`',
(WidgetTester tester) async {
Completer<void> pageLoaded = Completer<void>();

final PlatformWebViewController controller = PlatformWebViewController(
const PlatformWebViewControllerCreationParams(),
)
..setPlatformNavigationDelegate(PlatformNavigationDelegate(
const PlatformNavigationDelegateCreationParams(),
)..setOnPageFinished((_) => pageLoaded.complete()))
..loadRequest(LoadRequestParams(uri: Uri.parse(primaryUrl)));

await tester.pumpWidget(Builder(
builder: (BuildContext context) {
return PlatformWebViewWidget(
PlatformWebViewWidgetCreationParams(controller: controller),
).build(context);
},
));

await pageLoaded.future;

await tester.pumpWidget(Container());
await tester.pumpAndSettle();

await tester.pumpWidget(Builder(
builder: (BuildContext context) {
return PlatformWebViewWidget(
PlatformWebViewWidgetCreationParams(controller: controller),
).build(context);
},
));

pageLoaded = Completer<void>();
await controller.loadRequest(
LoadRequestParams(uri: Uri.parse(primaryUrl)),
);
await expectLater(
pageLoaded.future,
completes,
);
},
);
}

/// Returns the value used for the HTTP User-Agent: request header in subsequent HTTP requests.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ name: webview_flutter_android
description: A Flutter plugin that provides a WebView widget on Android.
repository: https://github.com/flutter/plugins/tree/main/packages/webview_flutter/webview_flutter_android
issue_tracker: https://github.com/flutter/flutter/issues?q=is%3Aissue+is%3Aopen+label%3A%22p%3A+webview%22
version: 3.1.1
version: 3.1.2

environment:
sdk: ">=2.17.0 <3.0.0"
Expand Down