From 2a1e0b82045f6ee2e9dd79a4bddca537f787fe51 Mon Sep 17 00:00:00 2001 From: Lorenzo Pichilli Date: Wed, 20 Nov 2019 02:30:16 +0100 Subject: [PATCH] updated driver tests, updated onTargetBlank event for Android --- .idea/workspace.xml | 930 +++++++++--------- CHANGELOG.md | 5 + .../InAppWebView/InAppWebChromeClient.java | 38 +- example/lib/in_app_webiew_example.screen.dart | 5 +- example/pubspec.yaml | 4 + .../in_app_webview_initial_file_test.html | 1 + .../in_app_webview_on_js_dialog_test.html | 19 + .../in_app_webview_on_target_blank_test.html | 6 +- example/test_driver/app_test.dart | 40 + ...n_app_webview_on_console_message_test.dart | 5 +- ...in_app_webview_on_download_start_test.dart | 2 +- .../in_app_webview_on_js_dialog_test.dart | 200 ++++ .../in_app_webview_on_target_blank_test.dart | 15 +- ...view_should_override_url_loading_test.dart | 17 +- example/test_driver/main_test.dart | 2 + 15 files changed, 809 insertions(+), 480 deletions(-) create mode 100644 example/test_assets/in_app_webview_on_js_dialog_test.html create mode 100644 example/test_driver/in_app_webview_on_js_dialog_test.dart diff --git a/.idea/workspace.xml b/.idea/workspace.xml index 6b9fd3fa..177cf086 100644 --- a/.idea/workspace.xml +++ b/.idea/workspace.xml @@ -15,16 +15,21 @@ - + + - - + - - - - + + + + + + + + + @@ -45,13 +50,10 @@ - + - - - - - + + @@ -59,62 +61,53 @@ - - + + + + + + + + + + + + + + + + + + + + + + + + + + - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + @@ -125,59 +118,50 @@ - _onPlatformViewCreated - callback - evaluate - onLoadReso - reload - t-rex - ajaxReq - window. - shouldInterceptFetchRequest - onconsole - shouldInterceptAjaxRequest - console - getHT - getFavicons - getHtml - loadUrl - evaluateJavascript - isAsync - "headers": headers - shouldIn - onZaboHandlerCallback - AndroidView - scalesPageToFit - cookie_manager - scroll - scrolltO - removeF - onScroll + onConsoleM + return this + return await + onCallJsHandler + ListenerCallback + jsonDecode + CallBac + dart + ``` + JavaScriptHandlerCallback + addJavaScriptHandler + indexTest + removeJavaScriptHandler + __safeBrowsingEnabled__ + __safe + __textZoom + hidden + builtInZoomControls + __builtInZoomControls__ + android.support.v4 + __transparentBackground__ dispose - _getDomainName + _dispose + รน + setC + _textFieldController + in_app_webview_on_target_blank_test + createPromptDialog + houldOverrideUrlLoad + InAppWebViewShouldOverrideUrlLoadingTest activity.getPreferences(0) Log. com.pichillilorenzo.flutter_inappbrowser flutter_inappbrowser - throwIsNotOpened - ChannelManager - Function - ?.toMap() ?? {}); - javascriptEnabled - [InAppWebView]\ $PROJECT_DIR$/example/android - $PROJECT_DIR$/lib - $PROJECT_DIR$/lib/src @@ -186,61 +170,61 @@ - + @@ -330,26 +337,23 @@ - + - + - - - - - - - - - - + + + + + + + @@ -380,6 +384,12 @@ - + - - + + @@ -475,23 +485,22 @@ - - - + + + - + - - - - - - - - + + + + + + + + - @@ -506,351 +515,368 @@ - - + - - - - - - - - - - + - + + + + + - - + + - + - - + + - + - - + + - - + - - + + - + - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + - + - - - - - + + - - - - - + - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + - + - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + - + + + + + + + + + - - + - + - + - - + + + + + + + + + - + - + - - - - - - - - - - - - - - - - - - - - - - - + - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + - - + + diff --git a/CHANGELOG.md b/CHANGELOG.md index 1a7f1e7c..1d7245f0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -46,6 +46,11 @@ - Renamed `injectStyleCode` to `injectCSSCode` - Renamed `injectStyleFile` to `injectCSSFileFromUrl` +## 1.2.2 + +- Merge "added a shared WKProcessPool for webview instances" [#198](https://github.com/pichillilorenzo/flutter_inappbrowser/pull/198) (thanks to [robertcnst](https://github.com/robertcnst)) +- Fixed iOS setCookie. + ## 1.2.1 - Merge "Add new option to control the contentMode in Android platform" [#101](https://github.com/pichillilorenzo/flutter_inappbrowser/pull/101) (thanks to [DreamBuddy](https://github.com/DreamBuddy)) diff --git a/android/src/main/java/com/pichillilorenzo/flutter_inappbrowser/InAppWebView/InAppWebChromeClient.java b/android/src/main/java/com/pichillilorenzo/flutter_inappbrowser/InAppWebView/InAppWebChromeClient.java index d3071b04..5f784045 100644 --- a/android/src/main/java/com/pichillilorenzo/flutter_inappbrowser/InAppWebView/InAppWebChromeClient.java +++ b/android/src/main/java/com/pichillilorenzo/flutter_inappbrowser/InAppWebView/InAppWebChromeClient.java @@ -9,6 +9,7 @@ import android.graphics.BitmapFactory; import android.graphics.Color; import android.net.Uri; import android.os.Build; +import android.os.Message; import android.util.Log; import android.view.View; import android.webkit.ConsoleMessage; @@ -18,6 +19,7 @@ import android.webkit.JsResult; import android.webkit.ValueCallback; import android.webkit.WebChromeClient; import android.webkit.WebView; +import android.webkit.WebViewClient; import android.widget.EditText; import android.widget.FrameLayout; import android.widget.LinearLayout; @@ -395,12 +397,40 @@ public class InAppWebChromeClient extends WebChromeClient implements PluginRegis } @Override - public boolean onCreateWindow(WebView view, boolean dialog, boolean userGesture, android.os.Message resultMsg) { - WebView.HitTestResult result = view.getHitTestResult(); - String data = result.getExtra(); - Map obj = new HashMap<>(); + public boolean onCreateWindow(WebView view, boolean isDialog, boolean userGesture, final Message resultMsg) { + final Map obj = new HashMap<>(); if (inAppBrowserActivity != null) obj.put("uuid", inAppBrowserActivity.uuid); + + WebView.HitTestResult result = view.getHitTestResult(); + String data = result.getExtra(); + + if (data == null) { + // to get the URL, create a temp weview + final WebView newWebView = new WebView(view.getContext()); + // disable javascript + newWebView.getSettings().setJavaScriptEnabled(false); + newWebView.setWebViewClient(new WebViewClient(){ + @Override + public void onPageStarted(WebView v, String url, Bitmap favicon) { + super.onPageStarted(v, url, favicon); + + obj.put("url", url); + getChannel().invokeMethod("onTargetBlank", obj); + + // stop webview loading + v.stopLoading(); + + // this will throw the error "Application attempted to call on a destroyed AwAutofillManager" that will kill the webview. + // that's ok. + v.destroy(); + } + }); + ((WebView.WebViewTransport)resultMsg.obj).setWebView(newWebView); + resultMsg.sendToTarget(); + return true; + } + obj.put("url", data); getChannel().invokeMethod("onTargetBlank", obj); return false; diff --git a/example/lib/in_app_webiew_example.screen.dart b/example/lib/in_app_webiew_example.screen.dart index 6482554f..645565f5 100755 --- a/example/lib/in_app_webiew_example.screen.dart +++ b/example/lib/in_app_webiew_example.screen.dart @@ -42,6 +42,7 @@ class _InAppWebViewExampleScreenState extends State { @override void dispose() { super.dispose(); + _textFieldController.dispose(); } @override @@ -122,11 +123,11 @@ class _InAppWebViewExampleScreenState extends State { BoxDecoration(border: Border.all(color: Colors.blueAccent)), child: InAppWebView( //initialUrl: "https://www.youtube.com/embed/M7lc1UVf-VE?playsinline=1", - initialUrl: "https://github.com", + //initialUrl: "https://github.com", //initialUrl: "chrome://safe-browsing/match?type=malware", //initialUrl: "http://192.168.1.20:8081/", //initialUrl: "https://192.168.1.20:4433/", - //initialFile: "assets/index.html", + initialFile: "assets/index.html", initialHeaders: {}, initialOptions: InAppWebViewWidgetOptions( inAppWebViewOptions: InAppWebViewOptions( diff --git a/example/pubspec.yaml b/example/pubspec.yaml index ac51e7e8..8fb8e5f6 100644 --- a/example/pubspec.yaml +++ b/example/pubspec.yaml @@ -56,6 +56,10 @@ flutter: - test_assets/in_app_webview_ajax_test.html - test_assets/in_app_webview_fetch_test.html - test_assets/in_app_webview_on_load_resource_custom_scheme_test.html + - test_assets/in_app_webview_on_console_message_test.html + - test_assets/in_app_webview_on_download_start_test.html + - test_assets/in_app_webview_on_target_blank_test.html + - test_assets/in_app_webview_on_js_dialog_test.html - test_assets/css/ - test_assets/images/ - test_assets/favicon.ico diff --git a/example/test_assets/in_app_webview_initial_file_test.html b/example/test_assets/in_app_webview_initial_file_test.html index 35047153..443d98e9 100644 --- a/example/test_assets/in_app_webview_initial_file_test.html +++ b/example/test_assets/in_app_webview_initial_file_test.html @@ -29,6 +29,7 @@

placeholder 100x50

+ flutter_inappbrowser diff --git a/example/test_assets/in_app_webview_on_js_dialog_test.html b/example/test_assets/in_app_webview_on_js_dialog_test.html new file mode 100644 index 00000000..a234f0ef --- /dev/null +++ b/example/test_assets/in_app_webview_on_js_dialog_test.html @@ -0,0 +1,19 @@ + + + + + + + InAppWebViewOnJsDialogTest + + +

InAppWebViewOnJsDialogTest

+ + + \ No newline at end of file diff --git a/example/test_assets/in_app_webview_on_target_blank_test.html b/example/test_assets/in_app_webview_on_target_blank_test.html index ad5f4918..997016b6 100644 --- a/example/test_assets/in_app_webview_on_target_blank_test.html +++ b/example/test_assets/in_app_webview_on_target_blank_test.html @@ -8,11 +8,9 @@

InAppWebViewOnTargetBlankTest

- target blank + target blank \ No newline at end of file diff --git a/example/test_driver/app_test.dart b/example/test_driver/app_test.dart index 2ecd7d1b..779d15a5 100644 --- a/example/test_driver/app_test.dart +++ b/example/test_driver/app_test.dart @@ -12,6 +12,7 @@ void main() { // Connect to the Flutter driver before running any tests. setUpAll(() async { driver = await FlutterDriver.connect(); + await driver.setTextEntryEmulation(enabled: true); }); // Close the connection to the driver after the tests have completed. @@ -166,5 +167,44 @@ void main() { expect(url, "https://flutter.dev/"); }, timeout: new Timeout(new Duration(minutes: 5))); + test('InAppWebViewOnJsDialogTest', () async { + await Future.delayed(const Duration(milliseconds: 2000)); + final appBarTitle = find.byValueKey('AppBarTitle'); + final alertButtonOk = find.byValueKey('AlertButtonOk'); + final confirmButtonCancel = find.byValueKey('ConfirmButtonCancel'); + final confirmButtonOk = find.byValueKey('ConfirmButtonOk'); + final promptTextField = find.byValueKey('PromptTextField'); + final promptButtonCancel = find.byValueKey('PromptButtonCancel'); + final promptButtonOk = find.byValueKey('PromptButtonOk'); + + while((await driver.getText(appBarTitle)) == "InAppWebViewOnJsDialogTest") { + await Future.delayed(const Duration(milliseconds: 1000)); + } + + await driver.tap(alertButtonOk); + + String title = await driver.getText(appBarTitle); + expect(title, "alert"); + + await Future.delayed(const Duration(milliseconds: 500)); + + await driver.tap(confirmButtonOk); + + title = await driver.getText(appBarTitle); + expect(title, "confirm true"); + + await Future.delayed(const Duration(milliseconds: 500)); + + await driver.tap(promptTextField); + await driver.enterText("new value"); + await driver.waitFor(find.text("new value")); + + await driver.tap(promptButtonOk); + + title = await driver.getText(appBarTitle); + expect(title, "prompt new value"); + + }, timeout: new Timeout(new Duration(minutes: 5))); + }); } \ No newline at end of file diff --git a/example/test_driver/in_app_webview_on_console_message_test.dart b/example/test_driver/in_app_webview_on_console_message_test.dart index 9783c8f3..4e0cb620 100644 --- a/example/test_driver/in_app_webview_on_console_message_test.dart +++ b/example/test_driver/in_app_webview_on_console_message_test.dart @@ -40,10 +40,7 @@ class InAppWebViewOnConsoleMessageTestState extends WidgetTestState { }, onLoadStop: (InAppWebViewController controller, String url) { - setState(() { - appBarTitle = "true"; - }); - nextTest(context: context, state: this); + }, onConsoleMessage: (InAppWebViewController controller, ConsoleMessage consoleMessage) { setState(() { diff --git a/example/test_driver/in_app_webview_on_download_start_test.dart b/example/test_driver/in_app_webview_on_download_start_test.dart index 0183ac98..b09ee2f0 100644 --- a/example/test_driver/in_app_webview_on_download_start_test.dart +++ b/example/test_driver/in_app_webview_on_download_start_test.dart @@ -25,7 +25,7 @@ class InAppWebViewOnDownloadStartTestState extends WidgetTestState { Expanded( child: Container( child: InAppWebView( - initialFile: "test_assets/in_app_webview_on_downlaod_start_test.html", + initialFile: "test_assets/in_app_webview_on_download_start_test.html", initialHeaders: {}, initialOptions: InAppWebViewWidgetOptions( inAppWebViewOptions: InAppWebViewOptions( diff --git a/example/test_driver/in_app_webview_on_js_dialog_test.dart b/example/test_driver/in_app_webview_on_js_dialog_test.dart new file mode 100644 index 00000000..35415f6a --- /dev/null +++ b/example/test_driver/in_app_webview_on_js_dialog_test.dart @@ -0,0 +1,200 @@ +import 'package:flutter/material.dart'; + +import 'package:flutter_inappbrowser/flutter_inappbrowser.dart'; + +import 'main_test.dart'; +import 'util_test.dart'; +import 'custom_widget_test.dart'; + +class InAppWebViewOnJsDialogTest extends WidgetTest { + final InAppWebViewOnJsDialogTestState state = InAppWebViewOnJsDialogTestState(); + + @override + InAppWebViewOnJsDialogTestState createState() => state; +} + +class InAppWebViewOnJsDialogTestState extends WidgetTestState { + String appBarTitle = "InAppWebViewOnJsDialogTest"; + + TextEditingController _textFieldController = TextEditingController(); + + @override + void dispose() { + super.dispose(); + _textFieldController.dispose(); + } + + @override + Widget build(BuildContext context) { + return Scaffold( + appBar: myAppBar(state: this, title: appBarTitle), + body: Container( + child: Column(children: [ + Expanded( + child: Container( + child: InAppWebView( + initialFile: "test_assets/in_app_webview_on_js_dialog_test.html", + initialHeaders: {}, + initialOptions: InAppWebViewWidgetOptions( + inAppWebViewOptions: InAppWebViewOptions( + clearCache: true, + debuggingEnabled: true + ) + ), + onWebViewCreated: (InAppWebViewController controller) { + webView = controller; + + controller.addJavaScriptHandler(handlerName: 'confirm', callback: (args) { + setState(() { + appBarTitle = "confirm " + ((args[0] is bool && args[0]) ? "true" : "false"); + }); + }); + + controller.addJavaScriptHandler(handlerName: 'prompt', callback: (args) { + setState(() { + appBarTitle = "prompt " + args[0]; + }); + nextTest(context: context, state: this); + }); + }, + onLoadStart: (InAppWebViewController controller, String url) { + + }, + onLoadStop: (InAppWebViewController controller, String url) { + setState(() { + appBarTitle = "loaded"; + }); + }, + onJsAlert: + (InAppWebViewController controller, String message) async { + JsAlertResponseAction action = + await createAlertDialog(context, message); + return JsAlertResponse( + handledByClient: true, action: action); + }, + onJsConfirm: + (InAppWebViewController controller, String message) async { + JsConfirmResponseAction action = + await createConfirmDialog(context, message); + return JsConfirmResponse( + handledByClient: true, action: action); + }, + onJsPrompt: (InAppWebViewController controller, String message, + String defaultValue) async { + _textFieldController.text = defaultValue; + JsPromptResponseAction action = + await createPromptDialog(context, message); + return JsPromptResponse( + handledByClient: true, + action: action, + value: _textFieldController.text); + }, + ), + ), + ), + ]) + ) + ); + } + + Future createAlertDialog( + BuildContext context, String message) async { + JsAlertResponseAction action; + + await showDialog( + context: context, + builder: (BuildContext context) { + return AlertDialog( + content: Text(message), + actions: [ + FlatButton( + child: Text("Ok"), + key: Key("AlertButtonOk"), + onPressed: () { + action = JsAlertResponseAction.CONFIRM; + Navigator.of(context).pop(); + setState(() { + appBarTitle = "alert"; + }); + }, + ), + ], + ); + }, + ); + + return action; + } + + Future createConfirmDialog( + BuildContext context, String message) async { + JsConfirmResponseAction action; + + await showDialog( + context: context, + builder: (BuildContext context) { + return AlertDialog( + content: Text(message), + actions: [ + FlatButton( + child: Text("Cancel"), + key: Key("ConfirmButtonCancel"), + onPressed: () { + action = JsConfirmResponseAction.CANCEL; + Navigator.of(context).pop(); + }, + ), + FlatButton( + child: Text("Ok"), + key: Key("ConfirmButtonOk"), + onPressed: () { + action = JsConfirmResponseAction.CONFIRM; + Navigator.of(context).pop(); + }, + ), + ], + ); + }, + ); + + return action; + } + + Future createPromptDialog( + BuildContext context, String message) async { + JsPromptResponseAction action; + + await showDialog( + context: context, + builder: (BuildContext context) { + return AlertDialog( + title: Text(message), + content: TextField( + key: Key("PromptTextField"), + controller: _textFieldController, + ), + actions: [ + FlatButton( + child: Text("Cancel"), + key: Key("PromptButtonCancel"), + onPressed: () { + action = JsPromptResponseAction.CANCEL; + Navigator.of(context).pop(); + }, + ), + FlatButton( + child: Text("Ok"), + key: Key("PromptButtonOk"), + onPressed: () { + action = JsPromptResponseAction.CONFIRM; + Navigator.of(context).pop(); + }, + ), + ], + ); + }, + ); + + return action; + } +} diff --git a/example/test_driver/in_app_webview_on_target_blank_test.dart b/example/test_driver/in_app_webview_on_target_blank_test.dart index 8feabf99..e63e754a 100644 --- a/example/test_driver/in_app_webview_on_target_blank_test.dart +++ b/example/test_driver/in_app_webview_on_target_blank_test.dart @@ -31,7 +31,8 @@ class InAppWebViewOnTargetBlankTestState extends WidgetTestState { inAppWebViewOptions: InAppWebViewOptions( clearCache: true, debuggingEnabled: true, - useOnTargetBlank: true + useOnTargetBlank: true, + javaScriptCanOpenWindowsAutomatically: true, ) ), onWebViewCreated: (InAppWebViewController controller) { @@ -41,13 +42,15 @@ class InAppWebViewOnTargetBlankTestState extends WidgetTestState { }, onLoadStop: (InAppWebViewController controller, String url) { - + if (url == "https://flutter.dev/") { + setState(() { + appBarTitle = url; + }); + nextTest(context: context, state: this); + } }, onTargetBlank: (InAppWebViewController controller, String url) { - setState(() { - appBarTitle = url; - }); - nextTest(context: context, state: this); + controller.loadUrl(url: url); }, ), ), diff --git a/example/test_driver/in_app_webview_should_override_url_loading_test.dart b/example/test_driver/in_app_webview_should_override_url_loading_test.dart index c56b491d..45203d62 100644 --- a/example/test_driver/in_app_webview_should_override_url_loading_test.dart +++ b/example/test_driver/in_app_webview_should_override_url_loading_test.dart @@ -14,7 +14,6 @@ class InAppWebViewShouldOverrideUrlLoadingTest extends WidgetTest { } class InAppWebViewShouldOverrideUrlLoadingTestState extends WidgetTestState { - String url = "https://flutter.dev/"; String appBarTitle = "InAppWebViewShouldOverrideUrlLoadingTest"; @override @@ -26,7 +25,7 @@ class InAppWebViewShouldOverrideUrlLoadingTestState extends WidgetTestState { Expanded( child: Container( child: InAppWebView( - initialUrl: "https://www.google.com/", + initialFile: "test_assets/in_app_webview_initial_file_test.html", initialHeaders: {}, initialOptions: InAppWebViewWidgetOptions( inAppWebViewOptions: InAppWebViewOptions( @@ -42,13 +41,17 @@ class InAppWebViewShouldOverrideUrlLoadingTestState extends WidgetTestState { }, onLoadStop: (InAppWebViewController controller, String url) { - setState(() { - appBarTitle = url; - }); - nextTest(context: context, state: this); + if (url == "https://flutter.dev/") { + setState(() { + appBarTitle = url; + }); + nextTest(context: context, state: this); + } else { + controller.evaluateJavascript(source: "document.querySelector('#link').click();"); + } }, shouldOverrideUrlLoading: (InAppWebViewController controller, String url) { - controller.loadUrl(url: url); + controller.loadUrl(url: "https://flutter.dev/"); }, ), ), diff --git a/example/test_driver/main_test.dart b/example/test_driver/main_test.dart index 3746cf3c..37687ad7 100644 --- a/example/test_driver/main_test.dart +++ b/example/test_driver/main_test.dart @@ -10,6 +10,7 @@ import 'in_app_webview_initial_url_test.dart'; import 'in_app_webview_javascript_handler_test.dart'; import 'in_app_webview_on_console_message_test.dart'; import 'in_app_webview_on_download_start_test.dart'; +import 'in_app_webview_on_js_dialog_test.dart'; import 'in_app_webview_on_load_resource_custom_scheme_test.dart'; import 'in_app_webview_on_load_resource_test.dart'; import 'in_app_webview_on_target_blank_test.dart'; @@ -30,6 +31,7 @@ Map buildRoutes({@required BuildContext context}) { '/InAppWebViewOnConsoleMessageTest': (context) => InAppWebViewOnConsoleMessageTest(), '/InAppWebViewOnDownloadStartTest': (context) => InAppWebViewOnDownloadStartTest(), '/InAppWebViewOnTargetBlankTest': (context) => InAppWebViewOnTargetBlankTest(), + '/InAppWebViewOnJsDialogTest': (context) => InAppWebViewOnJsDialogTest(), }; routes.forEach((k, v) => testRoutes.add(k)); return routes;