Commit 2a1e0b82 authored by Lorenzo Pichilli's avatar Lorenzo Pichilli

updated driver tests, updated onTargetBlank event for Android

parent 3106caf4
This diff is collapsed.
......@@ -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))
......
......@@ -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<String, Object> obj = new HashMap<>();
public boolean onCreateWindow(WebView view, boolean isDialog, boolean userGesture, final Message resultMsg) {
final Map<String, Object> 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;
......
......@@ -42,6 +42,7 @@ class _InAppWebViewExampleScreenState extends State<InAppWebViewExampleScreen> {
@override
void dispose() {
super.dispose();
_textFieldController.dispose();
}
@override
......@@ -122,11 +123,11 @@ class _InAppWebViewExampleScreenState extends State<InAppWebViewExampleScreen> {
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(
......
......@@ -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
......
......@@ -29,6 +29,7 @@
<p>
<img src="https://via.placeholder.com/100x50" alt="placeholder 100x50">
</p>
<a id="link" href="https://github.com/pichillilorenzo/flutter_inappbrowser">flutter_inappbrowser</a>
</main>
</div>
</body>
......
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>InAppWebViewOnJsDialogTest</title>
</head>
<body>
<h1>InAppWebViewOnJsDialogTest</h1>
<script>
window.addEventListener("flutterInAppBrowserPlatformReady", function(event) {
alert("alert message");
window.flutter_inappbrowser.callHandler('confirm', confirm("confirm message"));
window.flutter_inappbrowser.callHandler('prompt', prompt("prompt message", "prompt default value"));
});
</script>
</body>
</html>
\ No newline at end of file
......@@ -8,11 +8,9 @@
</head>
<body>
<h1>InAppWebViewOnTargetBlankTest</h1>
<a id="target-blank" href="https://flutter.dev/">target blank</a>
<a id="target-blank" href="https://flutter.dev/" target="_blank">target blank</a>
<script>
window.addEventListener("flutterInAppBrowserPlatformReady", function(event) {
document.querySelector("#target-blank").click();
});
document.querySelector('#target-blank').click();
</script>
</body>
</html>
\ No newline at end of file
......@@ -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
......@@ -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(() {
......
......@@ -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(
......
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: <Widget>[
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<JsAlertResponseAction> createAlertDialog(
BuildContext context, String message) async {
JsAlertResponseAction action;
await showDialog(
context: context,
builder: (BuildContext context) {
return AlertDialog(
content: Text(message),
actions: <Widget>[
FlatButton(
child: Text("Ok"),
key: Key("AlertButtonOk"),
onPressed: () {
action = JsAlertResponseAction.CONFIRM;
Navigator.of(context).pop();
setState(() {
appBarTitle = "alert";
});
},
),
],
);
},
);
return action;
}
Future<JsConfirmResponseAction> createConfirmDialog(
BuildContext context, String message) async {
JsConfirmResponseAction action;
await showDialog(
context: context,
builder: (BuildContext context) {
return AlertDialog(
content: Text(message),
actions: <Widget>[
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<JsPromptResponseAction> 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: <Widget>[
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;
}
}
......@@ -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) {
},
onTargetBlank: (InAppWebViewController controller, String url) {
if (url == "https://flutter.dev/") {
setState(() {
appBarTitle = url;
});
nextTest(context: context, state: this);
}
},
onTargetBlank: (InAppWebViewController controller, String url) {
controller.loadUrl(url: url);
},
),
),
......
......@@ -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) {
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/");
},
),
),
......
......@@ -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<String, WidgetBuilder> 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;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment