From 8e8c4cd176b253afec456691a4d88a2dd2e8feff Mon Sep 17 00:00:00 2001 From: rekire Date: Sun, 12 Nov 2023 14:02:34 +0100 Subject: [PATCH 1/2] Add missing exports from the platform interface --- .../lib/src/webview_controller.dart | 24 +++++++++++++++++++ .../webview_flutter/lib/webview_flutter.dart | 3 +++ 2 files changed, 27 insertions(+) diff --git a/packages/webview_flutter/webview_flutter/lib/src/webview_controller.dart b/packages/webview_flutter/webview_flutter/lib/src/webview_controller.dart index 0b81978d6fcc..f8f083c9ba90 100644 --- a/packages/webview_flutter/webview_flutter/lib/src/webview_controller.dart +++ b/packages/webview_flutter/webview_flutter/lib/src/webview_controller.dart @@ -370,6 +370,30 @@ class WebViewController { return platform.setOnConsoleMessage(onConsoleMessage); } + /// Sets a callback that notifies the host application that the web page + /// wants to display a JavaScript alert() dialog. + Future setOnJavaScriptAlertDialog( + Future Function(JavaScriptAlertDialogRequest request) + onJavaScriptAlertDialog) async { + return platform.setOnJavaScriptAlertDialog(onJavaScriptAlertDialog); + } + + /// Sets a callback that notifies the host application that the web page + /// wants to display a JavaScript confirm() dialog. + Future setOnJavaScriptConfirmDialog( + Future Function(JavaScriptConfirmDialogRequest request) + onJavaScriptConfirmDialog) async { + return platform.setOnJavaScriptConfirmDialog(onJavaScriptConfirmDialog); + } + + /// Sets a callback that notifies the host application that the web page + /// wants to display a JavaScript prompt() dialog. + Future setOnJavaScriptTextInputDialog( + Future Function(JavaScriptTextInputDialogRequest request) + onJavaScriptTextInputDialog) async { + return platform.setOnJavaScriptTextInputDialog(onJavaScriptTextInputDialog); + } + /// Gets the value used for the HTTP `User-Agent:` request header. Future getUserAgent() { return platform.getUserAgent(); diff --git a/packages/webview_flutter/webview_flutter/lib/webview_flutter.dart b/packages/webview_flutter/webview_flutter/lib/webview_flutter.dart index f4c294a4761a..03449bd480e0 100644 --- a/packages/webview_flutter/webview_flutter/lib/webview_flutter.dart +++ b/packages/webview_flutter/webview_flutter/lib/webview_flutter.dart @@ -4,10 +4,13 @@ export 'package:webview_flutter_platform_interface/webview_flutter_platform_interface.dart' show + JavaScriptAlertDialogRequest, + JavaScriptConfirmDialogRequest, JavaScriptConsoleMessage, JavaScriptLogLevel, JavaScriptMessage, JavaScriptMode, + JavaScriptTextInputDialogRequest, LoadRequestMethod, NavigationDecision, NavigationRequest, From 47b37d8f7f8b9c54cf1bb78cb1f0e777c2261a93 Mon Sep 17 00:00:00 2001 From: jsharp83 Date: Thu, 4 Jan 2024 15:31:09 +0900 Subject: [PATCH 2/2] Add test case for javascript panel interface and update version --- .../webview_flutter/CHANGELOG.md | 3 +- .../webview_flutter/example/pubspec.yaml | 6 +- .../lib/src/webview_controller.dart | 6 +- .../webview_flutter/pubspec.yaml | 8 +-- .../test/webview_controller_test.dart | 56 +++++++++++++++++++ .../test/webview_controller_test.mocks.dart | 39 +++++++++++++ .../test/webview_widget_test.mocks.dart | 39 +++++++++++++ 7 files changed, 146 insertions(+), 11 deletions(-) diff --git a/packages/webview_flutter/webview_flutter/CHANGELOG.md b/packages/webview_flutter/webview_flutter/CHANGELOG.md index dbe14968153d..c0e804104cee 100644 --- a/packages/webview_flutter/webview_flutter/CHANGELOG.md +++ b/packages/webview_flutter/webview_flutter/CHANGELOG.md @@ -1,5 +1,6 @@ -## 4.4.3 +## 4.5.0 +* Adds support to show JavaScript dialog. See `WebViewController.setOnJavaScriptAlertDialog`, `WebViewController.setOnJavaScriptConfirmDialog` and `WebViewController.setOnJavaScriptTextInputDialog`. * Updates minimum supported SDK version to Flutter 3.10/Dart 3.0. * Fixes new lint warnings. diff --git a/packages/webview_flutter/webview_flutter/example/pubspec.yaml b/packages/webview_flutter/webview_flutter/example/pubspec.yaml index efc363539b79..73f35bb97899 100644 --- a/packages/webview_flutter/webview_flutter/example/pubspec.yaml +++ b/packages/webview_flutter/webview_flutter/example/pubspec.yaml @@ -17,8 +17,8 @@ dependencies: # The example app is bundled with the plugin so we use a path dependency on # the parent directory to use the current plugin's version. path: ../ - webview_flutter_android: ^3.12.0 - webview_flutter_wkwebview: ^3.9.0 + webview_flutter_android: ^3.13.0 + webview_flutter_wkwebview: ^3.10.0 dev_dependencies: build_runner: ^2.1.5 @@ -27,7 +27,7 @@ dev_dependencies: sdk: flutter integration_test: sdk: flutter - webview_flutter_platform_interface: ^2.3.0 + webview_flutter_platform_interface: ^2.9.0 flutter: uses-material-design: true diff --git a/packages/webview_flutter/webview_flutter/lib/src/webview_controller.dart b/packages/webview_flutter/webview_flutter/lib/src/webview_controller.dart index f8f083c9ba90..d917d7637c2e 100644 --- a/packages/webview_flutter/webview_flutter/lib/src/webview_controller.dart +++ b/packages/webview_flutter/webview_flutter/lib/src/webview_controller.dart @@ -374,7 +374,7 @@ class WebViewController { /// wants to display a JavaScript alert() dialog. Future setOnJavaScriptAlertDialog( Future Function(JavaScriptAlertDialogRequest request) - onJavaScriptAlertDialog) async { + onJavaScriptAlertDialog) async { return platform.setOnJavaScriptAlertDialog(onJavaScriptAlertDialog); } @@ -382,7 +382,7 @@ class WebViewController { /// wants to display a JavaScript confirm() dialog. Future setOnJavaScriptConfirmDialog( Future Function(JavaScriptConfirmDialogRequest request) - onJavaScriptConfirmDialog) async { + onJavaScriptConfirmDialog) async { return platform.setOnJavaScriptConfirmDialog(onJavaScriptConfirmDialog); } @@ -390,7 +390,7 @@ class WebViewController { /// wants to display a JavaScript prompt() dialog. Future setOnJavaScriptTextInputDialog( Future Function(JavaScriptTextInputDialogRequest request) - onJavaScriptTextInputDialog) async { + onJavaScriptTextInputDialog) async { return platform.setOnJavaScriptTextInputDialog(onJavaScriptTextInputDialog); } diff --git a/packages/webview_flutter/webview_flutter/pubspec.yaml b/packages/webview_flutter/webview_flutter/pubspec.yaml index 2978e8231ea0..a0cdac8330a2 100644 --- a/packages/webview_flutter/webview_flutter/pubspec.yaml +++ b/packages/webview_flutter/webview_flutter/pubspec.yaml @@ -2,7 +2,7 @@ name: webview_flutter description: A Flutter plugin that provides a WebView widget on Android and iOS. repository: https://github.com/flutter/packages/tree/main/packages/webview_flutter/webview_flutter issue_tracker: https://github.com/flutter/flutter/issues?q=is%3Aissue+is%3Aopen+label%3A%22p%3A+webview%22 -version: 4.4.3 +version: 4.5.0 environment: sdk: ">=3.0.0 <4.0.0" @@ -19,9 +19,9 @@ flutter: dependencies: flutter: sdk: flutter - webview_flutter_android: ^3.12.0 - webview_flutter_platform_interface: ^2.6.0 - webview_flutter_wkwebview: ^3.9.0 + webview_flutter_android: ^3.13.0 + webview_flutter_platform_interface: ^2.9.0 + webview_flutter_wkwebview: ^3.10.0 dev_dependencies: build_runner: ^2.1.5 diff --git a/packages/webview_flutter/webview_flutter/test/webview_controller_test.dart b/packages/webview_flutter/webview_flutter/test/webview_controller_test.dart index 695a781738e2..9eb5343f84df 100644 --- a/packages/webview_flutter/webview_flutter/test/webview_controller_test.dart +++ b/packages/webview_flutter/webview_flutter/test/webview_controller_test.dart @@ -403,6 +403,62 @@ void main() { verify(mockPlatformWebViewController.setOnConsoleMessage(onConsoleMessage)); }); + test('setOnJavaScriptAlertDialog', () async { + final MockPlatformWebViewController mockPlatformWebViewController = + MockPlatformWebViewController(); + + final WebViewController webViewController = WebViewController.fromPlatform( + mockPlatformWebViewController, + ); + + Future onJavaScriptAlertDialog( + JavaScriptAlertDialogRequest request) async { + return; + } + + await webViewController.setOnJavaScriptAlertDialog(onJavaScriptAlertDialog); + verify(mockPlatformWebViewController + .setOnJavaScriptAlertDialog(onJavaScriptAlertDialog)); + }); + + test('setOnJavaScriptConfirmDialog', () async { + final MockPlatformWebViewController mockPlatformWebViewController = + MockPlatformWebViewController(); + + final WebViewController webViewController = WebViewController.fromPlatform( + mockPlatformWebViewController, + ); + + Future onJavaScriptConfirmDialog( + JavaScriptConfirmDialogRequest request) async { + return true; + } + + await webViewController + .setOnJavaScriptConfirmDialog(onJavaScriptConfirmDialog); + verify(mockPlatformWebViewController + .setOnJavaScriptConfirmDialog(onJavaScriptConfirmDialog)); + }); + + test('setOnJavaScriptTextInputDialog', () async { + final MockPlatformWebViewController mockPlatformWebViewController = + MockPlatformWebViewController(); + + final WebViewController webViewController = WebViewController.fromPlatform( + mockPlatformWebViewController, + ); + + Future onJavaScriptTextInputDialog( + JavaScriptTextInputDialogRequest request) async { + return 'text'; + } + + await webViewController + .setOnJavaScriptTextInputDialog(onJavaScriptTextInputDialog); + verify(mockPlatformWebViewController + .setOnJavaScriptTextInputDialog(onJavaScriptTextInputDialog)); + }); + test('getUserAgent', () async { final MockPlatformWebViewController mockPlatformWebViewController = MockPlatformWebViewController(); diff --git a/packages/webview_flutter/webview_flutter/test/webview_controller_test.mocks.dart b/packages/webview_flutter/webview_flutter/test/webview_controller_test.mocks.dart index 383751f38bcc..e8fba0e00091 100644 --- a/packages/webview_flutter/webview_flutter/test/webview_controller_test.mocks.dart +++ b/packages/webview_flutter/webview_flutter/test/webview_controller_test.mocks.dart @@ -402,6 +402,45 @@ class MockPlatformWebViewController extends _i1.Mock returnValue: _i5.Future.value(), returnValueForMissingStub: _i5.Future.value(), ) as _i5.Future); + + @override + _i5.Future setOnJavaScriptAlertDialog( + _i5.Future Function(_i2.JavaScriptAlertDialogRequest)? + onJavaScriptAlertDialog) => + (super.noSuchMethod( + Invocation.method( + #setOnJavaScriptAlertDialog, + [onJavaScriptAlertDialog], + ), + returnValue: _i5.Future.value(), + returnValueForMissingStub: _i5.Future.value(), + ) as _i5.Future); + + @override + _i5.Future setOnJavaScriptConfirmDialog( + _i5.Future Function(_i2.JavaScriptConfirmDialogRequest)? + onJavaScriptConfirmDialog) => + (super.noSuchMethod( + Invocation.method( + #setOnJavaScriptConfirmDialog, + [onJavaScriptConfirmDialog], + ), + returnValue: _i5.Future.value(), + returnValueForMissingStub: _i5.Future.value(), + ) as _i5.Future); + + @override + _i5.Future setOnJavaScriptTextInputDialog( + _i5.Future Function(_i2.JavaScriptTextInputDialogRequest)? + onJavaScriptTextInputDialog) => + (super.noSuchMethod( + Invocation.method( + #setOnJavaScriptTextInputDialog, + [onJavaScriptTextInputDialog], + ), + returnValue: _i5.Future.value(), + returnValueForMissingStub: _i5.Future.value(), + ) as _i5.Future); } /// A class which mocks [PlatformNavigationDelegate]. diff --git a/packages/webview_flutter/webview_flutter/test/webview_widget_test.mocks.dart b/packages/webview_flutter/webview_flutter/test/webview_widget_test.mocks.dart index cf1ff7cec255..c6c94309a295 100644 --- a/packages/webview_flutter/webview_flutter/test/webview_widget_test.mocks.dart +++ b/packages/webview_flutter/webview_flutter/test/webview_widget_test.mocks.dart @@ -420,6 +420,45 @@ class MockPlatformWebViewController extends _i1.Mock returnValue: _i7.Future.value(), returnValueForMissingStub: _i7.Future.value(), ) as _i7.Future); + + @override + _i7.Future setOnJavaScriptAlertDialog( + _i7.Future Function(_i2.JavaScriptAlertDialogRequest)? + onJavaScriptAlertDialog) => + (super.noSuchMethod( + Invocation.method( + #setOnJavaScriptAlertDialog, + [onJavaScriptAlertDialog], + ), + returnValue: _i7.Future.value(), + returnValueForMissingStub: _i7.Future.value(), + ) as _i7.Future); + + @override + _i7.Future setOnJavaScriptConfirmDialog( + _i7.Future Function(_i2.JavaScriptConfirmDialogRequest)? + onJavaScriptConfirmDialog) => + (super.noSuchMethod( + Invocation.method( + #setOnJavaScriptConfirmDialog, + [onJavaScriptConfirmDialog], + ), + returnValue: _i7.Future.value(), + returnValueForMissingStub: _i7.Future.value(), + ) as _i7.Future); + + @override + _i7.Future setOnJavaScriptTextInputDialog( + _i7.Future Function(_i2.JavaScriptTextInputDialogRequest)? + onJavaScriptTextInputDialog) => + (super.noSuchMethod( + Invocation.method( + #setOnJavaScriptTextInputDialog, + [onJavaScriptTextInputDialog], + ), + returnValue: _i7.Future.value(), + returnValueForMissingStub: _i7.Future.value(), + ) as _i7.Future); } /// A class which mocks [PlatformWebViewWidget].