Skip to content

Commit

Permalink
fix: Fix JavaScript handlers in InAppBrowser for Android
Browse files Browse the repository at this point in the history
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: pichillilorenzo#1973
  • Loading branch information
fuzzybinary committed Nov 22, 2024
1 parent b176b62 commit 17c3935
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -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<void> handlerFoo = Completer<void>();
final Completer<void> handlerFooWithArgs = Completer<void>();
final List<dynamic> messagesReceived = <dynamic>[];

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<dynamic>?);
messagesReceived
.add(args[3]?.cast<String, String>() as Map<String, String>?);
messagesReceived
.add(args[4]?.cast<String, String>() as Map<String, String>?);
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<dynamic>?, ["bar", 5]), true);
expect(mapEquals(messagesReceived[3], {"foo": "baz"}), true);
expect(
mapEquals(
messagesReceived[4], {"bar": "bar_value", "baz": "baz_value"}),
true);
}, skip: shouldSkip);
}
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -23,5 +27,6 @@ void main() {
setGetSettings();
hideAndShow();
customMenuItems();
javascriptHandler();
}, skip: shouldSkip);
}
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,7 @@ protected void onCreate(Bundle savedInstanceState) {
webView.customSettings = webViewSettings;
webView.contextMenu = contextMenu;

webView.prepareAndAddUserScripts();
List<UserScript> userScripts = new ArrayList<>();
if (initialUserScripts != null) {
for (Map<String, Object> initialUserScript : initialUserScripts) {
Expand Down

0 comments on commit 17c3935

Please sign in to comment.