Commit e2bd3076 authored by Lorenzo Pichilli's avatar Lorenzo Pichilli

updated flutter driver tests, fixed javascript code for fetch request interception

parent 3869f893
This diff is collapsed.
......@@ -445,22 +445,44 @@ final public class InAppWebView extends InputAwareWebView {
" controller.abort();" +
" break;" +
" }" +
" var resultResource = (result.url != null) ? result.url : resource;" +
" var resultInit = init;" +
" if (result.init != null) {" +
" resultInit.method = result.method;" +
" resultInit.headers = convertJsonToHeaders(result.headers);" +
" resultInit.body = convertArrayIntBodyToUint8Array(result.body);" +
" resultInit.mode = result.mode;" +
" resultInit.credentials = convertJsonToCredential(result.credentials);" +
" resultInit.cache = result.cache;" +
" resultInit.redirect = result.redirect;" +
" resultInit.referrer = result.referrer;" +
" resultInit.referrerPolicy = result.referrerPolicy;" +
" resultInit.integrity = result.integrity;" +
" resultInit.keepalive = result.keepalive;" +
" resource = (result.url != null) ? result.url : resource;" +
" if (init == null) {" +
" init = {};" +
" }" +
" return fetch(resultResource, resultInit);" +
" if (result.method != null && result.method.length > 0) {" +
" init.method = result.method;" +
" }" +
" if (result.headers != null && Object.keys(result.headers).length > 0) {" +
" init.headers = convertJsonToHeaders(result.headers);" +
" }" +
" if (result.body != null && result.body.length > 0) {" +
" init.body = convertArrayIntBodyToUint8Array(result.body);" +
" }" +
" if (result.mode != null && result.mode.length > 0) {" +
" init.mode = result.mode;" +
" }" +
" if (result.credentials != null) {" +
" init.credentials = convertJsonToCredential(result.credentials);" +
" }" +
" if (result.cache != null && result.cache.length > 0) {" +
" init.cache = result.cache;" +
" }" +
" if (result.redirect != null && result.redirect.length > 0) {" +
" init.redirect = result.redirect;" +
" }" +
" if (result.referrer != null && result.referrer.length > 0) {" +
" init.referrer = result.referrer;" +
" }" +
" if (result.referrerPolicy != null && result.referrerPolicy.length > 0) {" +
" init.referrerPolicy = result.referrerPolicy;" +
" }" +
" if (result.integrity != null && result.integrity.length > 0) {" +
" init.integrity = result.integrity;" +
" }" +
" if (result.keepalive != null) {" +
" init.keepalive = result.keepalive;" +
" }" +
" return fetch(resource, init);" +
" }" +
" return fetch(resource, init);" +
" });" +
......
......@@ -54,6 +54,8 @@ flutter:
- test_assets/in_app_webview_on_load_resource_test.html
- test_assets/in_app_webview_javascript_handler_test.html
- 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/css/
- test_assets/images/
- test_assets/favicon.ico
......
<!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>InAppWebViewFetchTest</title>
</head>
<body>
<h1>InAppWebViewFetchTest</h1>
<script>
window.addEventListener('flutterInAppBrowserPlatformReady', function(event) {
fetch(new Request("http://192.168.1.20:8082/test-download-file")).then(function(response) {
window.flutter_inappbrowser.callHandler('fetchGet', response.status);
}).catch(function(error) {
window.flutter_inappbrowser.callHandler('fetchGet', "ERROR: " + error);
});
fetch("http://192.168.1.20:8082/test-ajax-post", {
method: 'POST',
body: JSON.stringify({
firstname: 'Foo',
lastname: 'Bar'
}),
headers: {
'Content-Type': 'application/json'
}
}).then(function(response) {
response.json().then(function(value) {
window.flutter_inappbrowser.callHandler('fetchPost', value);
}).catch(function(error) {
window.flutter_inappbrowser.callHandler('fetchPost', "ERROR: " + error);
});
}).catch(function(error) {
window.flutter_inappbrowser.callHandler('fetchPost', "ERROR: " + error);
});
});
</script>
</body>
</html>
\ No newline at end of file
<!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>InAppWebViewOnLoadResourceCustomSchemeTest</title>
</head>
<body class="text-center">
<div class="cover-container d-flex w-100 h-100 p-3 mx-auto flex-column">
<main role="main" class="inner cover">
<h1 class="cover-heading">InAppWebViewOnLoadResourceCustomSchemeTest</h1>
<img id="image" src="my-special-custom-scheme://images/flutter-logo.svg" alt="flutter logo">
</main>
</div>
</body>
<script>
var image = document.querySelector("#image");
image.addEventListener('load', function() {
window.flutter_inappbrowser.callHandler('imageLoaded');
});
</script>
</html>
\ No newline at end of file
// Imports the Flutter Driver API.
import 'dart:async';
import 'package:flutter/cupertino.dart';
import 'package:flutter_driver/flutter_driver.dart';
import 'package:test/test.dart';
......@@ -22,21 +21,14 @@ void main() {
}
});
myTest({@required String name, @required Function callback, Timeout timeout}) {
timeout = (timeout == null) ? new Timeout(new Duration(minutes: 5)) : timeout;
test(name, () async {
await Future.delayed(const Duration(milliseconds: 2000));
callback();
}, timeout: timeout);
}
//
// IMPORTANT NOTE!!!
// These tests need to follow the same order of "var routes" in "buildRoutes()" function
// defined in main_test.dart
//
myTest(name: 'InAppWebViewInitialUrlTest', callback: () async {
test('InAppWebViewInitialUrlTest', () async {
await Future.delayed(const Duration(milliseconds: 2000));
final appBarTitle = find.byValueKey('AppBarTitle');
while((await driver.getText(appBarTitle)) == "InAppWebViewInitialUrlTest") {
......@@ -45,9 +37,10 @@ void main() {
String url = await driver.getText(appBarTitle);
expect(url, "https://flutter.dev/");
});
}, timeout: new Timeout(new Duration(minutes: 5)));
myTest(name: 'InAppWebViewInitialFileTest', callback: () async {
test('InAppWebViewInitialFileTest', () async {
await Future.delayed(const Duration(milliseconds: 2000));
final appBarTitle = find.byValueKey('AppBarTitle');
while((await driver.getText(appBarTitle)) == "InAppWebViewInitialFileTest") {
......@@ -56,9 +49,10 @@ void main() {
String title = await driver.getText(appBarTitle);
expect(title, "true");
});
}, timeout: new Timeout(new Duration(minutes: 5)));
myTest(name: 'InAppWebViewOnLoadResourceTest', callback: () async {
test('InAppWebViewOnLoadResourceTest', () async {
await Future.delayed(const Duration(milliseconds: 2000));
List<String> resourceList = [
"https://getbootstrap.com/docs/4.3/dist/css/bootstrap.min.css",
"https://code.jquery.com/jquery-3.3.1.min.js",
......@@ -71,13 +65,13 @@ void main() {
}
String title = await driver.getText(appBarTitle);
print(title);
for (String resource in resourceList) {
expect(true, title.contains(resource));
}
});
}, timeout: new Timeout(new Duration(minutes: 5)));
myTest(name: 'InAppWebViewJavaScriptHandlerTest', callback: () async {
test('InAppWebViewJavaScriptHandlerTest', () async {
await Future.delayed(const Duration(milliseconds: 2000));
final appBarTitle = find.byValueKey('AppBarTitle');
while((await driver.getText(appBarTitle)) == "InAppWebViewJavaScriptHandlerTest") {
......@@ -86,9 +80,10 @@ void main() {
String title = await driver.getText(appBarTitle);
expect(true, !title.contains("false"));
});
}, timeout: new Timeout(new Duration(minutes: 5)));
myTest(name: 'InAppWebViewAjaxTest', callback: () async {
test('InAppWebViewAjaxTest', () async {
await Future.delayed(const Duration(milliseconds: 2000));
final appBarTitle = find.byValueKey('AppBarTitle');
while((await driver.getText(appBarTitle)) == "InAppWebViewAjaxTest") {
......@@ -97,6 +92,31 @@ void main() {
String title = await driver.getText(appBarTitle);
expect(title, "Lorenzo Pichilli Lorenzo Pichilli");
});
}, timeout: new Timeout(new Duration(minutes: 5)));
test('InAppWebViewOnLoadResourceCustomSchemeTest', () async {
await Future.delayed(const Duration(milliseconds: 2000));
final appBarTitle = find.byValueKey('AppBarTitle');
while((await driver.getText(appBarTitle)) == "InAppWebViewOnLoadResourceCustomSchemeTest") {
await Future.delayed(const Duration(milliseconds: 1000));
}
String title = await driver.getText(appBarTitle);
expect(title, "true");
}, timeout: new Timeout(new Duration(minutes: 5)));
test('InAppWebViewFetchTest', () async {
await Future.delayed(const Duration(milliseconds: 2000));
final appBarTitle = find.byValueKey('AppBarTitle');
while((await driver.getText(appBarTitle)) == "InAppWebViewFetchTest") {
await Future.delayed(const Duration(milliseconds: 1000));
}
String title = await driver.getText(appBarTitle);
expect(true, title.contains("Lorenzo Pichilli") && title.contains("200"));
}, timeout: new Timeout(new Duration(minutes: 5)));
});
}
\ No newline at end of file
import 'dart:convert';
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 InAppWebViewFetchTest extends WidgetTest {
final InAppWebViewFetchTestState state = InAppWebViewFetchTestState();
@override
InAppWebViewFetchTestState createState() => state;
}
class InAppWebViewFetchTestState extends WidgetTestState {
String appBarTitle = "InAppWebViewFetchTest";
int totTests = 2;
int testsDone = 0;
@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_fetch_test.html",
initialHeaders: {},
initialOptions: InAppWebViewWidgetOptions(
inAppWebViewOptions: InAppWebViewOptions(
clearCache: true,
debuggingEnabled: true,
useShouldInterceptFetchRequest: true,
)
),
onWebViewCreated: (InAppWebViewController controller) {
webView = controller;
webView.addJavaScriptHandler(handlerName: "fetchGet", callback: (args) {
appBarTitle = (appBarTitle == "InAppWebViewFetchTest") ? args[0].toString() : appBarTitle + " " + args[0].toString();
updateCountTest(context: context);
});
webView.addJavaScriptHandler(handlerName: "fetchPost", callback: (args) {
appBarTitle = (appBarTitle == "InAppWebViewFetchTest") ? args[0]["fullname"] : appBarTitle + " " + args[0]["fullname"];
updateCountTest(context: context);
});
},
onLoadStart: (InAppWebViewController controller, String url) {
},
onLoadStop: (InAppWebViewController controller, String url) {
},
shouldInterceptFetchRequest: (InAppWebViewController controller, FetchRequest fetchRequest) async {
if (fetchRequest.url.endsWith("/test-ajax-post")) {
fetchRequest.body = utf8.encode("""{
"firstname": "Lorenzo",
"lastname": "Pichilli"
}
""");
}
return fetchRequest;
},
),
),
),
])
)
);
}
void updateCountTest({@required BuildContext context}) {
testsDone++;
if (testsDone == totTests) {
setState(() { });
nextTest(context: context, state: this);
}
}
}
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_inappbrowser/flutter_inappbrowser.dart';
import 'main_test.dart';
import 'util_test.dart';
import 'custom_widget_test.dart';
class InAppWebViewOnLoadResourceCustomSchemeTest extends WidgetTest {
final InAppWebViewOnLoadResourceCustomSchemeTestState state = InAppWebViewOnLoadResourceCustomSchemeTestState();
@override
InAppWebViewOnLoadResourceCustomSchemeTestState createState() => state;
}
class InAppWebViewOnLoadResourceCustomSchemeTestState extends WidgetTestState {
String appBarTitle = "InAppWebViewOnLoadResourceCustomSchemeTest";
@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_load_resource_custom_scheme_test.html",
initialHeaders: {},
initialOptions: InAppWebViewWidgetOptions(
inAppWebViewOptions: InAppWebViewOptions(
clearCache: true,
debuggingEnabled: true,
resourceCustomSchemes: ["my-special-custom-scheme"]
)
),
onWebViewCreated: (InAppWebViewController controller) {
webView = controller;
webView.addJavaScriptHandler(handlerName: "imageLoaded", callback: (args) {
setState(() {
appBarTitle = "true";
});
nextTest(context: context, state: this);
});
},
onLoadStart: (InAppWebViewController controller, String url) {
},
onLoadStop: (InAppWebViewController controller, String url) {
},
onLoadResourceCustomScheme: (InAppWebViewController controller, String scheme, String url) async {
if (scheme == "my-special-custom-scheme") {
var bytes = await rootBundle.load("test_assets/" + url.replaceFirst("my-special-custom-scheme://", "", 0));
var response = new CustomSchemeResponse(data: bytes.buffer.asUint8List(), contentType: "image/svg+xml", contentEnconding: "utf-8");
return response;
}
return null;
},
),
),
),
])
)
);
}
}
......@@ -4,9 +4,11 @@ import 'package:flutter/material.dart';
import 'custom_widget_test.dart';
import 'in_app_webview_ajax_test.dart';
import 'in_app_webview_fetch_test.dart';
import 'in_app_webview_initial_file_test.dart';
import 'in_app_webview_initial_url_test.dart';
import 'in_app_webview_javascript_handler_test.dart';
import 'in_app_webview_on_load_resource_custom_scheme_test.dart';
import 'in_app_webview_on_load_resource_test.dart';
List<String> testRoutes = [];
......@@ -17,6 +19,8 @@ Map<String, WidgetBuilder> buildRoutes({@required BuildContext context}) {
'/InAppWebViewOnLoadResourceTest': (context) => InAppWebViewOnLoadResourceTest(),
'/InAppWebViewJavaScriptHandlerTest': (context) => InAppWebViewJavaScriptHandlerTest(),
'/InAppWebViewAjaxTest': (context) => InAppWebViewAjaxTest(),
'/InAppWebViewOnLoadResourceCustomSchemeTest': (context) => InAppWebViewOnLoadResourceCustomSchemeTest(),
'/InAppWebViewFetchTest': (context) => InAppWebViewFetchTest(),
};
routes.forEach((k, v) => testRoutes.add(k));
return routes;
......
......@@ -597,22 +597,44 @@ let interceptFetchRequestsJS = """
controller.abort();
break;
}
var resultResource = (result.url != null) ? result.url : resource;
var resultInit = init;
if (result.init != null) {
resultInit.method = result.method;
resultInit.headers = convertJsonToHeaders(result.headers);
resultInit.body = convertArrayIntBodyToUint8Array(result.body);
resultInit.mode = result.mode;
resultInit.credentials = convertJsonToCredential(result.credentials);
resultInit.cache = result.cache;
resultInit.redirect = result.redirect;
resultInit.referrer = result.referrer;
resultInit.referrerPolicy = result.referrerPolicy;
resultInit.integrity = result.integrity;
resultInit.keepalive = result.keepalive;
}
return fetch(resultResource, resultInit);
resource = (result.url != null) ? result.url : resource;
if (init == null) {
init = {};
}
if (result.method != null && result.method.length > 0) {
init.method = result.method;
}
if (result.headers != null && Object.keys(result.headers).length > 0) {
init.headers = convertJsonToHeaders(result.headers);
}
if (result.body != null && result.body.length > 0) {
init.body = convertArrayIntBodyToUint8Array(result.body);
}
if (result.mode != null && result.mode.length > 0) {
init.mode = result.mode;
}
if (result.credentials != null) {
init.credentials = convertJsonToCredential(result.credentials);
}
if (result.cache != null && result.cache.length > 0) {
init.cache = result.cache;
}
if (result.redirect != null && result.redirect.length > 0) {
init.redirect = result.redirect;
}
if (result.referrer != null && result.referrer.length > 0) {
init.referrer = result.referrer;
}
if (result.referrerPolicy != null && result.referrerPolicy.length > 0) {
init.referrerPolicy = result.referrerPolicy;
}
if (result.integrity != null && result.integrity.length > 0) {
init.integrity = result.integrity;
}
if (result.keepalive != null) {
init.keepalive = result.keepalive;
}
return fetch(resource, init);
}
return fetch(resource, init);
});
......
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