forked from pichillilorenzo/flutter_inappwebview
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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: pichillilorenzo#1973
- Loading branch information
1 parent
b176b62
commit 17c3935
Showing
3 changed files
with
62 additions
and
2 deletions.
There are no files selected for viewing
54 changes: 54 additions & 0 deletions
54
flutter_inappwebview/example/integration_test/in_app_browser/javascript_handler.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters