From 17c39355bd7061fe6d708029b52ea5d8cf008fd6 Mon Sep 17 00:00:00 2001 From: Jeff Ward Date: Fri, 22 Nov 2024 10:59:49 -0500 Subject: [PATCH] fix: Fix JavaScript handlers in InAppBrowser for Android Android InAppBrowserActivity would not call `prepareAndAddUserScripts` when `WebViewFeature.DOCUMENT_START_SCRIPT`, which would prevent shims like `callHandler` from being injected. Fix this by calling `prepareAndAddUserScripts` during OnCreate. Add an integration test for JavaScript handlers in in_app_browser. refs: #1973 --- .../in_app_browser/javascript_handler.dart | 54 +++++++++++++++++++ .../integration_test/in_app_browser/main.dart | 9 +++- .../in_app_browser/InAppBrowserActivity.java | 1 + 3 files changed, 62 insertions(+), 2 deletions(-) create mode 100644 flutter_inappwebview/example/integration_test/in_app_browser/javascript_handler.dart diff --git a/flutter_inappwebview/example/integration_test/in_app_browser/javascript_handler.dart b/flutter_inappwebview/example/integration_test/in_app_browser/javascript_handler.dart new file mode 100644 index 000000000..6f3db4e98 --- /dev/null +++ b/flutter_inappwebview/example/integration_test/in_app_browser/javascript_handler.dart @@ -0,0 +1,54 @@ +part of 'main.dart'; + +void javascriptHandler() { + final shouldSkip = kIsWeb + ? true + : ![ + TargetPlatform.android, + TargetPlatform.iOS, + TargetPlatform.macOS, + ].contains(defaultTargetPlatform); + + skippableTest('JavaScript Handler', () async { + final Completer handlerFoo = Completer(); + final Completer handlerFooWithArgs = Completer(); + final List messagesReceived = []; + + MyInAppBrowser inAppBrowser = MyInAppBrowser(); + inAppBrowser.browserCreated.future.then((_) { + inAppBrowser.webViewController!.addJavaScriptHandler( + handlerName: 'handlerFoo', + callback: (args) { + handlerFoo.complete(); + return Foo(bar: 'bar_value', baz: 'baz_value'); + }); + inAppBrowser.webViewController!.addJavaScriptHandler( + handlerName: 'handlerFooWithArgs', + callback: (args) { + messagesReceived.add(args[0] as int); + messagesReceived.add(args[1] as bool); + messagesReceived.add(args[2] as List?); + messagesReceived + .add(args[3]?.cast() as Map?); + messagesReceived + .add(args[4]?.cast() as Map?); + handlerFooWithArgs.complete(); + }); + }); + await inAppBrowser.openFile( + assetFilePath: + 'test_assets/in_app_webview_javascript_handler_test.html'); + + await handlerFoo.future; + await handlerFooWithArgs.future; + + expect(messagesReceived[0], 1); + expect(messagesReceived[1], true); + expect(listEquals(messagesReceived[2] as List?, ["bar", 5]), true); + expect(mapEquals(messagesReceived[3], {"foo": "baz"}), true); + expect( + mapEquals( + messagesReceived[4], {"bar": "bar_value", "baz": "baz_value"}), + true); + }, skip: shouldSkip); +} diff --git a/flutter_inappwebview/example/integration_test/in_app_browser/main.dart b/flutter_inappwebview/example/integration_test/in_app_browser/main.dart index a1437e6b6..6ad5233cc 100644 --- a/flutter_inappwebview/example/integration_test/in_app_browser/main.dart +++ b/flutter_inappwebview/example/integration_test/in_app_browser/main.dart @@ -1,17 +1,21 @@ +import 'dart:async'; + import 'package:flutter/foundation.dart'; import 'package:flutter/material.dart'; import 'package:flutter/services.dart'; import 'package:flutter_inappwebview/flutter_inappwebview.dart'; import 'package:flutter_test/flutter_test.dart'; + import '../constants.dart'; import '../util.dart'; +part 'custom_menu_items.dart'; +part 'hide_and_show.dart'; +part 'javascript_handler.dart'; part 'open_data_and_close.dart'; part 'open_file_and_close.dart'; part 'open_url_and_close.dart'; part 'set_get_settings.dart'; -part 'hide_and_show.dart'; -part 'custom_menu_items.dart'; void main() { final shouldSkip = kIsWeb; @@ -23,5 +27,6 @@ void main() { setGetSettings(); hideAndShow(); customMenuItems(); + javascriptHandler(); }, skip: shouldSkip); } diff --git a/flutter_inappwebview_android/android/src/main/java/com/pichillilorenzo/flutter_inappwebview_android/in_app_browser/InAppBrowserActivity.java b/flutter_inappwebview_android/android/src/main/java/com/pichillilorenzo/flutter_inappwebview_android/in_app_browser/InAppBrowserActivity.java index 0fee8e3cf..c79df2332 100755 --- a/flutter_inappwebview_android/android/src/main/java/com/pichillilorenzo/flutter_inappwebview_android/in_app_browser/InAppBrowserActivity.java +++ b/flutter_inappwebview_android/android/src/main/java/com/pichillilorenzo/flutter_inappwebview_android/in_app_browser/InAppBrowserActivity.java @@ -145,6 +145,7 @@ protected void onCreate(Bundle savedInstanceState) { webView.customSettings = webViewSettings; webView.contextMenu = contextMenu; + webView.prepareAndAddUserScripts(); List userScripts = new ArrayList<>(); if (initialUserScripts != null) { for (Map initialUserScript : initialUserScripts) {