update tests

This commit is contained in:
Lorenzo Pichilli 2022-04-28 20:34:54 +02:00
parent 6911026d36
commit fa4132e33c
9 changed files with 541 additions and 483 deletions

View File

@ -0,0 +1,75 @@
import 'dart:async';
import 'dart:collection';
import 'package:flutter/foundation.dart';
import 'package:flutter/widgets.dart';
import 'package:flutter_inappwebview/flutter_inappwebview.dart';
import 'package:flutter_test/flutter_test.dart';
import '../constants.dart';
void initialUserScripts() {
final shouldSkip = kIsWeb ||
![
TargetPlatform.android,
TargetPlatform.iOS,
TargetPlatform.macOS,
].contains(defaultTargetPlatform);
testWidgets('initialUserScripts', (WidgetTester tester) async {
final Completer controllerCompleter = Completer<InAppWebViewController>();
final Completer<void> pageLoaded = Completer<void>();
await tester.pumpWidget(
Directionality(
textDirection: TextDirection.ltr,
child: InAppWebView(
key: GlobalKey(),
initialUrlRequest: URLRequest(url: TEST_CROSS_PLATFORM_URL_1),
initialUserScripts: UnmodifiableListView<UserScript>([
UserScript(
source: "var foo = 49;",
injectionTime: UserScriptInjectionTime.AT_DOCUMENT_START),
UserScript(
source: "var foo2 = 19;",
injectionTime: UserScriptInjectionTime.AT_DOCUMENT_START,
contentWorld: ContentWorld.PAGE),
UserScript(
source: "var bar = 2;",
injectionTime: UserScriptInjectionTime.AT_DOCUMENT_END,
contentWorld: ContentWorld.DEFAULT_CLIENT),
UserScript(
source: "var bar2 = 12;",
injectionTime: UserScriptInjectionTime.AT_DOCUMENT_END,
contentWorld: ContentWorld.world(name: "test")),
]),
onWebViewCreated: (controller) {
controllerCompleter.complete(controller);
},
onLoadStop: (controller, url) async {
pageLoaded.complete();
},
),
),
);
final InAppWebViewController controller = await controllerCompleter.future;
await pageLoaded.future;
expect(await controller.evaluateJavascript(source: "foo;"), 49);
expect(await controller.evaluateJavascript(source: "foo2;"), 19);
expect(
await controller.evaluateJavascript(
source: "foo2;", contentWorld: ContentWorld.PAGE),
19);
expect(await controller.evaluateJavascript(source: "bar;"), isNull);
expect(await controller.evaluateJavascript(source: "bar2;"), isNull);
expect(
await controller.evaluateJavascript(
source: "bar;", contentWorld: ContentWorld.DEFAULT_CLIENT),
2);
expect(
await controller.evaluateJavascript(
source: "bar2;", contentWorld: ContentWorld.world(name: "test")),
12);
}, skip: shouldSkip);
}

View File

@ -0,0 +1,78 @@
import 'dart:async';
import 'package:flutter/foundation.dart';
import 'package:flutter/widgets.dart';
import 'package:flutter_inappwebview/flutter_inappwebview.dart';
import 'package:flutter_test/flutter_test.dart';
import '../constants.dart';
void loadData() {
final shouldSkip = !kIsWeb ||
![
TargetPlatform.android,
TargetPlatform.iOS,
TargetPlatform.macOS,
].contains(defaultTargetPlatform);
testWidgets('loadData', (WidgetTester tester) async {
final Completer controllerCompleter = Completer<InAppWebViewController>();
final StreamController<String> pageLoads =
StreamController<String>.broadcast();
await tester.pumpWidget(
Directionality(
textDirection: TextDirection.ltr,
child: InAppWebView(
key: GlobalKey(),
initialUrlRequest: URLRequest(url: TEST_URL_ABOUT_BLANK),
onWebViewCreated: (controller) {
controllerCompleter.complete(controller);
},
onLoadStop: (controller, url) {
pageLoads.add(url!.toString());
},
),
),
);
final InAppWebViewController controller =
await controllerCompleter.future;
await pageLoads.stream.first;
final data = """
<!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">
<link rel="stylesheet" href="https://getbootstrap.com/docs/4.3/dist/css/bootstrap.min.css">
<script src="https://code.jquery.com/jquery-3.3.1.min.js"></script>
</head>
<body>
<img src="https://via.placeholder.com/100x50" alt="placeholder 100x50">
</body>
</html>
""";
final mimeType = 'text/html';
await controller.loadData(
data: data,
encoding: 'utf-8',
mimeType: mimeType,
historyUrl: TEST_CROSS_PLATFORM_URL_1,
baseUrl: TEST_CROSS_PLATFORM_URL_1);
await pageLoads.stream.first;
final String? currentUrl = (await controller.getUrl())?.toString();
if (!kIsWeb) {
expect(currentUrl, TEST_CROSS_PLATFORM_URL_1.toString());
} else {
expect(currentUrl, 'data:$mimeType,' + Uri.encodeFull(data)) ;
}
pageLoads.close();
}, skip: shouldSkip);
}

View File

@ -0,0 +1,55 @@
import 'dart:async';
import 'package:flutter/foundation.dart';
import 'package:flutter/widgets.dart';
import 'package:flutter_inappwebview/flutter_inappwebview.dart';
import 'package:flutter_test/flutter_test.dart';
import '../constants.dart';
void loadFile() {
final shouldSkip = !kIsWeb ||
![
TargetPlatform.android,
TargetPlatform.iOS,
TargetPlatform.macOS,
].contains(defaultTargetPlatform);
testWidgets('loadFile', (WidgetTester tester) async {
final Completer controllerCompleter = Completer<InAppWebViewController>();
final StreamController<String> pageLoads =
StreamController<String>.broadcast();
await tester.pumpWidget(
Directionality(
textDirection: TextDirection.ltr,
child: InAppWebView(
key: GlobalKey(),
initialUrlRequest: URLRequest(url: TEST_URL_ABOUT_BLANK),
onWebViewCreated: (controller) {
controllerCompleter.complete(controller);
},
onLoadStop: (controller, url) {
pageLoads.add(url!.toString());
},
),
),
);
final InAppWebViewController controller =
await controllerCompleter.future;
await pageLoads.stream.first;
await controller.loadFile(
assetFilePath: "test_assets/in_app_webview_initial_file_test.html");
await pageLoads.stream.first;
final Uri? url = await controller.getUrl();
expect(url, isNotNull);
expect(url!.scheme, 'file');
expect(url.path,
endsWith("test_assets/in_app_webview_initial_file_test.html"));
pageLoads.close();
}, skip: shouldSkip);
}

View File

@ -15,100 +15,36 @@ void loadUrl() {
TargetPlatform.macOS,
].contains(defaultTargetPlatform);
group('load url', () {
final shouldSkipTest1 = !kIsWeb ||
![
TargetPlatform.android,
TargetPlatform.iOS,
TargetPlatform.macOS,
].contains(defaultTargetPlatform);
testWidgets('reload', (WidgetTester tester) async {
final Completer controllerCompleter = Completer<InAppWebViewController>();
final StreamController<String> pageLoads =
StreamController<String>.broadcast();
testWidgets('loadUrl', (WidgetTester tester) async {
final Completer controllerCompleter = Completer<InAppWebViewController>();
final StreamController<String> pageLoads =
StreamController<String>.broadcast();
await tester.pumpWidget(
Directionality(
textDirection: TextDirection.ltr,
child: InAppWebView(
key: GlobalKey(),
initialUrlRequest: URLRequest(url: TEST_CROSS_PLATFORM_URL_1),
onWebViewCreated: (controller) {
controllerCompleter.complete(controller);
},
onLoadStop: (controller, url) {
pageLoads.add(url!.toString());
},
),
await tester.pumpWidget(
Directionality(
textDirection: TextDirection.ltr,
child: InAppWebView(
key: GlobalKey(),
initialUrlRequest:
URLRequest(url: Uri.parse('https://github.com/flutter')),
onWebViewCreated: (controller) {
controllerCompleter.complete(controller);
},
onLoadStop: (controller, url) {
pageLoads.add(url!.toString());
},
),
);
final InAppWebViewController controller =
await controllerCompleter.future;
var url = await pageLoads.stream.first;
expect(url, TEST_CROSS_PLATFORM_URL_1.toString());
),
);
final InAppWebViewController controller =
await controllerCompleter.future;
String? url = await pageLoads.stream.first;
expect(url, 'https://github.com/flutter');
await controller.loadUrl(
urlRequest: URLRequest(url: TEST_CROSS_PLATFORM_URL_2));
url = await pageLoads.stream.first;
expect(url, TEST_CROSS_PLATFORM_URL_2.toString());
await controller.reload();
url = await pageLoads.stream.first;
expect(url, 'https://github.com/flutter');
pageLoads.close();
}, skip: shouldSkipTest1);
final shouldSkipTest2 = kIsWeb ||
![
TargetPlatform.android,
TargetPlatform.iOS,
TargetPlatform.macOS,
].contains(defaultTargetPlatform);
testWidgets('loadUrl with headers', (WidgetTester tester) async {
final Completer controllerCompleter = Completer<InAppWebViewController>();
final StreamController<String> pageStarts =
StreamController<String>.broadcast();
final StreamController<String> pageLoads =
StreamController<String>.broadcast();
await tester.pumpWidget(
Directionality(
textDirection: TextDirection.ltr,
child: InAppWebView(
key: GlobalKey(),
initialUrlRequest: URLRequest(url: TEST_URL_1),
onWebViewCreated: (controller) {
controllerCompleter.complete(controller);
},
initialSettings: InAppWebViewSettings(javaScriptEnabled: true),
onLoadStart: (controller, url) {
pageStarts.add(url!.toString());
},
onLoadStop: (controller, url) {
pageLoads.add(url!.toString());
},
),
),
);
final InAppWebViewController controller =
await controllerCompleter.future;
final Map<String, String> headers = <String, String>{
'test_header': 'flutter_test_header'
};
await controller.loadUrl(
urlRequest: URLRequest(
url: Uri.parse('https://flutter-header-echo.herokuapp.com/'),
headers: headers));
final String? currentUrl = (await controller.getUrl())?.toString();
expect(currentUrl, 'https://flutter-header-echo.herokuapp.com/');
await pageStarts.stream.firstWhere((String url) => url == currentUrl);
await pageLoads.stream.firstWhere((String url) => url == currentUrl);
final String? content = await controller.evaluateJavascript(
source: 'document.documentElement.innerText');
expect(content!.contains('flutter_test_header'), isTrue);
pageStarts.close();
pageLoads.close();
}, skip: shouldSkipTest2);
}, skip: shouldSkip);
pageLoads.close();
});
}

View File

@ -5,11 +5,14 @@ import 'content_blocker.dart';
import 'get_title.dart';
import 'http_auth_credential_database.dart';
import 'initial_url_request.dart';
import 'initial_user_scripts.dart';
import 'intercept_ajax_request.dart';
import 'intercept_fetch_request.dart';
import 'javascript_code_evaluation.dart';
import 'javascript_dialogs.dart';
import 'javascript_handler.dart';
import 'load_data.dart';
import 'load_file.dart';
import 'load_file_url.dart';
import 'load_url.dart';
import 'on_console_message.dart';
@ -20,6 +23,7 @@ import 'on_load_error.dart';
import 'on_load_http_error.dart';
import 'on_load_resource.dart';
import 'on_load_resource_custom_scheme.dart';
import 'on_navigation_response.dart';
import 'on_page_commit_visible.dart';
import 'on_permission_request.dart';
import 'on_print.dart';
@ -33,7 +37,9 @@ import 'on_update_visited_history.dart';
import 'on_window_blur.dart';
import 'on_window_focus.dart';
import 'on_zoom_scale_changed.dart';
import 'post_requests.dart';
import 'programmatic_scroll.dart';
import 'reload.dart';
import 'resize_webview.dart';
import 'set_custom_useragent.dart';
import 'set_get_settings.dart';
@ -87,5 +93,11 @@ void main() {
onReceivedIcon();
onReceivedTouchIconUrl();
onJsBeforeUnload();
onNavigationResponse();
initialUserScripts();
postRequests();
loadData();
loadFile();
reload();
});
}

View File

@ -0,0 +1,88 @@
import 'dart:async';
import 'package:flutter/foundation.dart';
import 'package:flutter/widgets.dart';
import 'package:flutter_inappwebview/flutter_inappwebview.dart';
import 'package:flutter_test/flutter_test.dart';
import '../constants.dart';
void onNavigationResponse() {
final shouldSkip = kIsWeb ||
![
TargetPlatform.iOS,
TargetPlatform.macOS,
].contains(defaultTargetPlatform);
group("onNavigationResponse", () {
testWidgets('allow navigation', (WidgetTester tester) async {
final Completer controllerCompleter = Completer<InAppWebViewController>();
final Completer<void> pageLoaded = Completer<void>();
final Completer<String> onNavigationResponseCompleter =
Completer<String>();
await tester.pumpWidget(
Directionality(
textDirection: TextDirection.ltr,
child: InAppWebView(
key: GlobalKey(),
initialUrlRequest:
URLRequest(url: TEST_URL_1),
initialSettings:
InAppWebViewSettings(useOnNavigationResponse: true),
onWebViewCreated: (controller) {
controllerCompleter.complete(controller);
},
onLoadStop: (controller, url) {
pageLoaded.complete();
},
onNavigationResponse: (controller, navigationResponse) async {
onNavigationResponseCompleter
.complete(navigationResponse.response!.url.toString());
return NavigationResponseAction.ALLOW;
},
),
),
);
await pageLoaded.future;
final String url = await onNavigationResponseCompleter.future;
expect(url, TEST_URL_1.toString());
});
testWidgets('cancel navigation', (WidgetTester tester) async {
final Completer controllerCompleter = Completer<InAppWebViewController>();
final Completer<void> pageLoaded = Completer<void>();
final Completer<String> onNavigationResponseCompleter =
Completer<String>();
await tester.pumpWidget(
Directionality(
textDirection: TextDirection.ltr,
child: InAppWebView(
key: GlobalKey(),
initialUrlRequest:
URLRequest(url: TEST_URL_1),
initialSettings:
InAppWebViewSettings(useOnNavigationResponse: true),
onWebViewCreated: (controller) {
controllerCompleter.complete(controller);
},
onLoadStop: (controller, url) {
pageLoaded.complete();
},
onNavigationResponse: (controller, navigationResponse) async {
onNavigationResponseCompleter
.complete(navigationResponse.response!.url.toString());
return NavigationResponseAction.CANCEL;
},
),
),
);
final String url = await onNavigationResponseCompleter.future;
expect(url, TEST_URL_1.toString());
expect(pageLoaded.future, doesNotComplete);
});
}, skip: shouldSkip);
}

View File

@ -0,0 +1,153 @@
import 'dart:async';
import 'dart:convert';
import 'dart:typed_data';
import 'package:flutter/foundation.dart';
import 'package:flutter/widgets.dart';
import 'package:flutter_inappwebview/flutter_inappwebview.dart';
import 'package:flutter_test/flutter_test.dart';
import '../.env.dart';
void postRequests() {
final shouldSkip = kIsWeb ||
![
TargetPlatform.android,
TargetPlatform.iOS,
TargetPlatform.macOS,
].contains(defaultTargetPlatform);
group('POST requests', () {
testWidgets('initialUrlRequest', (WidgetTester tester) async {
final Completer controllerCompleter =
Completer<InAppWebViewController>();
final Completer<void> postPageLoaded = Completer<void>();
await tester.pumpWidget(
Directionality(
textDirection: TextDirection.ltr,
child: InAppWebView(
key: GlobalKey(),
initialUrlRequest: URLRequest(
url: Uri.parse(
"http://${environment["NODE_SERVER_IP"]}:8082/test-post"),
method: 'POST',
body: Uint8List.fromList(utf8.encode("name=FooBar")),
headers: {
'Content-Type': 'application/x-www-form-urlencoded'
}),
onWebViewCreated: (controller) {
controllerCompleter.complete(controller);
},
onLoadStop: (controller, url) {
postPageLoaded.complete();
},
),
),
);
final InAppWebViewController controller =
await controllerCompleter.future;
await postPageLoaded.future;
final String? currentUrl = (await controller.getUrl())?.toString();
expect(currentUrl,
'http://${environment["NODE_SERVER_IP"]}:8082/test-post');
final String? pContent = await controller.evaluateJavascript(
source: "document.querySelector('p').innerHTML;");
expect(pContent, "HELLO FooBar!");
});
testWidgets('loadUrl', (WidgetTester tester) async {
final Completer controllerCompleter =
Completer<InAppWebViewController>();
final Completer<void> postPageLoaded = Completer<void>();
await tester.pumpWidget(
Directionality(
textDirection: TextDirection.ltr,
child: InAppWebView(
key: GlobalKey(),
initialUrlRequest: URLRequest(url: Uri.parse('about:blank')),
onWebViewCreated: (controller) {
controllerCompleter.complete(controller);
},
onLoadStop: (controller, url) {
if (url?.scheme != "about") {
postPageLoaded.complete();
}
},
),
),
);
final InAppWebViewController controller =
await controllerCompleter.future;
var postData = Uint8List.fromList(utf8.encode("name=FooBar"));
await controller.loadUrl(
urlRequest: URLRequest(
url: Uri.parse(
"http://${environment["NODE_SERVER_IP"]}:8082/test-post"),
method: 'POST',
body: postData,
headers: {
'Content-Type': 'application/x-www-form-urlencoded'
}));
await postPageLoaded.future;
final String? currentUrl = (await controller.getUrl())?.toString();
expect(currentUrl,
'http://${environment["NODE_SERVER_IP"]}:8082/test-post');
final String? pContent = await controller.evaluateJavascript(
source: "document.querySelector('p').innerHTML;");
expect(pContent, "HELLO FooBar!");
});
testWidgets('postUrl', (WidgetTester tester) async {
final Completer controllerCompleter =
Completer<InAppWebViewController>();
final Completer<void> postPageLoaded = Completer<void>();
await tester.pumpWidget(
Directionality(
textDirection: TextDirection.ltr,
child: InAppWebView(
key: GlobalKey(),
initialUrlRequest: URLRequest(url: Uri.parse('about:blank')),
onWebViewCreated: (controller) {
controllerCompleter.complete(controller);
},
onLoadStop: (controller, url) {
if (url?.scheme != "about") {
postPageLoaded.complete();
}
},
),
),
);
final InAppWebViewController controller =
await controllerCompleter.future;
var postData = Uint8List.fromList(utf8.encode("name=FooBar"));
await controller.postUrl(
url: Uri.parse(
"http://${environment["NODE_SERVER_IP"]}:8082/test-post"),
postData: postData);
await postPageLoaded.future;
final String? currentUrl = (await controller.getUrl())?.toString();
expect(currentUrl,
'http://${environment["NODE_SERVER_IP"]}:8082/test-post');
final String? pContent = await controller.evaluateJavascript(
source: "document.querySelector('p').innerHTML;");
expect(pContent, "HELLO FooBar!");
});
}, skip: shouldSkip);
}

View File

@ -0,0 +1,52 @@
import 'dart:async';
import 'package:flutter/foundation.dart';
import 'package:flutter/widgets.dart';
import 'package:flutter_inappwebview/flutter_inappwebview.dart';
import 'package:flutter_test/flutter_test.dart';
import '../constants.dart';
void reload() {
final shouldSkip = !kIsWeb ||
![
TargetPlatform.android,
TargetPlatform.iOS,
TargetPlatform.macOS,
].contains(defaultTargetPlatform);
var url = !kIsWeb ? TEST_URL_1 : TEST_WEB_PLATFORM_URL_1;
testWidgets('reload', (WidgetTester tester) async {
final Completer controllerCompleter = Completer<InAppWebViewController>();
final StreamController<String> pageLoads =
StreamController<String>.broadcast();
await tester.pumpWidget(
Directionality(
textDirection: TextDirection.ltr,
child: InAppWebView(
key: GlobalKey(),
initialUrlRequest:
URLRequest(url: url),
onWebViewCreated: (controller) {
controllerCompleter.complete(controller);
},
onLoadStop: (controller, url) {
pageLoads.add(url!.toString());
},
),
),
);
final InAppWebViewController controller =
await controllerCompleter.future;
String? reloadUrl = await pageLoads.stream.first;
expect(reloadUrl, url.toString());
await controller.reload();
reloadUrl = await pageLoads.stream.first;
expect(reloadUrl, url.toString());
pageLoads.close();
}, skip: shouldSkip);
}

View File

@ -28,397 +28,6 @@ void main() {
group('InAppWebView', () {
group("iosOnNavigationResponse", () {
testWidgets('allow navigation', (WidgetTester tester) async {
final Completer controllerCompleter =
Completer<InAppWebViewController>();
final Completer<void> pageLoaded = Completer<void>();
final Completer<String> onNavigationResponseCompleter =
Completer<String>();
await tester.pumpWidget(
Directionality(
textDirection: TextDirection.ltr,
child: InAppWebView(
key: GlobalKey(),
initialUrlRequest:
URLRequest(url: Uri.parse('https://github.com/flutter')),
initialOptions: InAppWebViewGroupOptions(
ios: IOSInAppWebViewOptions(useOnNavigationResponse: true)),
onWebViewCreated: (controller) {
controllerCompleter.complete(controller);
},
onLoadStop: (controller, url) {
pageLoaded.complete();
},
iosOnNavigationResponse: (controller, navigationResponse) async {
onNavigationResponseCompleter
.complete(navigationResponse.response!.url.toString());
return IOSNavigationResponseAction.ALLOW;
},
),
),
);
await pageLoaded.future;
final String url = await onNavigationResponseCompleter.future;
expect(url, 'https://github.com/flutter');
}, skip: defaultTargetPlatform != TargetPlatform.iOS);
testWidgets('cancel navigation', (WidgetTester tester) async {
final Completer controllerCompleter =
Completer<InAppWebViewController>();
final Completer<void> pageLoaded = Completer<void>();
final Completer<String> onNavigationResponseCompleter =
Completer<String>();
await tester.pumpWidget(
Directionality(
textDirection: TextDirection.ltr,
child: InAppWebView(
key: GlobalKey(),
initialUrlRequest:
URLRequest(url: Uri.parse('https://github.com/flutter')),
initialOptions: InAppWebViewGroupOptions(
ios: IOSInAppWebViewOptions(useOnNavigationResponse: true)),
onWebViewCreated: (controller) {
controllerCompleter.complete(controller);
},
onLoadStop: (controller, url) {
pageLoaded.complete();
},
iosOnNavigationResponse: (controller, navigationResponse) async {
onNavigationResponseCompleter
.complete(navigationResponse.response!.url.toString());
return IOSNavigationResponseAction.CANCEL;
},
),
),
);
final String url = await onNavigationResponseCompleter.future;
expect(url, 'https://github.com/flutter');
expect(pageLoaded.future, doesNotComplete);
}, skip: defaultTargetPlatform != TargetPlatform.iOS);
}, skip: defaultTargetPlatform != TargetPlatform.iOS);
testWidgets('initialUserScripts', (WidgetTester tester) async {
final Completer controllerCompleter = Completer<InAppWebViewController>();
final Completer<void> pageLoaded = Completer<void>();
await tester.pumpWidget(
Directionality(
textDirection: TextDirection.ltr,
child: InAppWebView(
key: GlobalKey(),
initialUrlRequest:
URLRequest(url: Uri.parse('https://flutter.dev')),
initialUserScripts: UnmodifiableListView<UserScript>([
UserScript(
source: "var foo = 49;",
injectionTime: UserScriptInjectionTime.AT_DOCUMENT_START),
UserScript(
source: "var foo2 = 19;",
injectionTime: UserScriptInjectionTime.AT_DOCUMENT_START,
contentWorld: ContentWorld.PAGE),
UserScript(
source: "var bar = 2;",
injectionTime: UserScriptInjectionTime.AT_DOCUMENT_END,
contentWorld: ContentWorld.DEFAULT_CLIENT),
UserScript(
source: "var bar2 = 12;",
injectionTime: UserScriptInjectionTime.AT_DOCUMENT_END,
contentWorld: ContentWorld.world(name: "test")),
]),
onWebViewCreated: (controller) {
controllerCompleter.complete(controller);
},
onLoadStop: (controller, url) async {
pageLoaded.complete();
},
),
),
);
final InAppWebViewController controller =
await controllerCompleter.future;
await pageLoaded.future;
expect(await controller.evaluateJavascript(source: "foo;"), 49);
expect(await controller.evaluateJavascript(source: "foo2;"), 19);
expect(
await controller.evaluateJavascript(
source: "foo2;", contentWorld: ContentWorld.PAGE),
19);
expect(await controller.evaluateJavascript(source: "bar;"), isNull);
expect(await controller.evaluateJavascript(source: "bar2;"), isNull);
expect(
await controller.evaluateJavascript(
source: "bar;", contentWorld: ContentWorld.DEFAULT_CLIENT),
2);
expect(
await controller.evaluateJavascript(
source: "bar2;", contentWorld: ContentWorld.world(name: "test")),
12);
});
group('POST requests', () {
testWidgets('initialUrlRequest', (WidgetTester tester) async {
final Completer controllerCompleter =
Completer<InAppWebViewController>();
final Completer<void> postPageLoaded = Completer<void>();
await tester.pumpWidget(
Directionality(
textDirection: TextDirection.ltr,
child: InAppWebView(
key: GlobalKey(),
initialUrlRequest: URLRequest(
url: Uri.parse(
"http://${environment["NODE_SERVER_IP"]}:8082/test-post"),
method: 'POST',
body: Uint8List.fromList(utf8.encode("name=FooBar")),
headers: {
'Content-Type': 'application/x-www-form-urlencoded'
}),
onWebViewCreated: (controller) {
controllerCompleter.complete(controller);
},
onLoadStop: (controller, url) {
postPageLoaded.complete();
},
),
),
);
final InAppWebViewController controller =
await controllerCompleter.future;
await postPageLoaded.future;
final String? currentUrl = (await controller.getUrl())?.toString();
expect(currentUrl,
'http://${environment["NODE_SERVER_IP"]}:8082/test-post');
final String? pContent = await controller.evaluateJavascript(
source: "document.querySelector('p').innerHTML;");
expect(pContent, "HELLO FooBar!");
});
testWidgets('loadUrl', (WidgetTester tester) async {
final Completer controllerCompleter =
Completer<InAppWebViewController>();
final Completer<void> postPageLoaded = Completer<void>();
await tester.pumpWidget(
Directionality(
textDirection: TextDirection.ltr,
child: InAppWebView(
key: GlobalKey(),
initialUrlRequest: URLRequest(url: Uri.parse('about:blank')),
onWebViewCreated: (controller) {
controllerCompleter.complete(controller);
},
onLoadStop: (controller, url) {
if (url?.scheme != "about") {
postPageLoaded.complete();
}
},
),
),
);
final InAppWebViewController controller =
await controllerCompleter.future;
var postData = Uint8List.fromList(utf8.encode("name=FooBar"));
await controller.loadUrl(
urlRequest: URLRequest(
url: Uri.parse(
"http://${environment["NODE_SERVER_IP"]}:8082/test-post"),
method: 'POST',
body: postData,
headers: {
'Content-Type': 'application/x-www-form-urlencoded'
}));
await postPageLoaded.future;
final String? currentUrl = (await controller.getUrl())?.toString();
expect(currentUrl,
'http://${environment["NODE_SERVER_IP"]}:8082/test-post');
final String? pContent = await controller.evaluateJavascript(
source: "document.querySelector('p').innerHTML;");
expect(pContent, "HELLO FooBar!");
});
testWidgets('postUrl', (WidgetTester tester) async {
final Completer controllerCompleter =
Completer<InAppWebViewController>();
final Completer<void> postPageLoaded = Completer<void>();
await tester.pumpWidget(
Directionality(
textDirection: TextDirection.ltr,
child: InAppWebView(
key: GlobalKey(),
initialUrlRequest: URLRequest(url: Uri.parse('about:blank')),
onWebViewCreated: (controller) {
controllerCompleter.complete(controller);
},
onLoadStop: (controller, url) {
if (url?.scheme != "about") {
postPageLoaded.complete();
}
},
),
),
);
final InAppWebViewController controller =
await controllerCompleter.future;
var postData = Uint8List.fromList(utf8.encode("name=FooBar"));
await controller.postUrl(
url: Uri.parse(
"http://${environment["NODE_SERVER_IP"]}:8082/test-post"),
postData: postData);
await postPageLoaded.future;
final String? currentUrl = (await controller.getUrl())?.toString();
expect(currentUrl,
'http://${environment["NODE_SERVER_IP"]}:8082/test-post');
final String? pContent = await controller.evaluateJavascript(
source: "document.querySelector('p').innerHTML;");
expect(pContent, "HELLO FooBar!");
});
});
testWidgets('loadData', (WidgetTester tester) async {
final Completer controllerCompleter = Completer<InAppWebViewController>();
final StreamController<String> pageLoads =
StreamController<String>.broadcast();
await tester.pumpWidget(
Directionality(
textDirection: TextDirection.ltr,
child: InAppWebView(
key: GlobalKey(),
initialUrlRequest: URLRequest(url: Uri.parse('about:blank')),
onWebViewCreated: (controller) {
controllerCompleter.complete(controller);
},
onLoadStop: (controller, url) {
pageLoads.add(url!.toString());
},
),
),
);
final InAppWebViewController controller =
await controllerCompleter.future;
await pageLoads.stream.first;
await controller.loadData(
data: """
<!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">
<link rel="stylesheet" href="https://getbootstrap.com/docs/4.3/dist/css/bootstrap.min.css">
<script src="https://code.jquery.com/jquery-3.3.1.min.js"></script>
</head>
<body>
<img src="https://via.placeholder.com/100x50" alt="placeholder 100x50">
</body>
</html>
""",
encoding: 'utf-8',
mimeType: 'text/html',
historyUrl: Uri.parse("https://flutter.dev"),
baseUrl: Uri.parse("https://flutter.dev"));
await pageLoads.stream.first;
final String? currentUrl = (await controller.getUrl())?.toString();
expect(currentUrl, 'https://flutter.dev/');
pageLoads.close();
});
testWidgets('loadFile', (WidgetTester tester) async {
final Completer controllerCompleter = Completer<InAppWebViewController>();
final StreamController<String> pageLoads =
StreamController<String>.broadcast();
await tester.pumpWidget(
Directionality(
textDirection: TextDirection.ltr,
child: InAppWebView(
key: GlobalKey(),
initialUrlRequest: URLRequest(url: Uri.parse('about:blank')),
onWebViewCreated: (controller) {
controllerCompleter.complete(controller);
},
onLoadStop: (controller, url) {
pageLoads.add(url!.toString());
},
),
),
);
final InAppWebViewController controller =
await controllerCompleter.future;
await pageLoads.stream.first;
await controller.loadFile(
assetFilePath: "test_assets/in_app_webview_initial_file_test.html");
await pageLoads.stream.first;
final Uri? url = await controller.getUrl();
expect(url, isNotNull);
expect(url!.scheme, 'file');
expect(url.path,
endsWith("test_assets/in_app_webview_initial_file_test.html"));
pageLoads.close();
});
testWidgets('reload', (WidgetTester tester) async {
final Completer controllerCompleter = Completer<InAppWebViewController>();
final StreamController<String> pageLoads =
StreamController<String>.broadcast();
await tester.pumpWidget(
Directionality(
textDirection: TextDirection.ltr,
child: InAppWebView(
key: GlobalKey(),
initialUrlRequest:
URLRequest(url: Uri.parse('https://github.com/flutter')),
onWebViewCreated: (controller) {
controllerCompleter.complete(controller);
},
onLoadStop: (controller, url) {
pageLoads.add(url!.toString());
},
),
),
);
final InAppWebViewController controller =
await controllerCompleter.future;
String? url = await pageLoads.stream.first;
expect(url, 'https://github.com/flutter');
await controller.reload();
url = await pageLoads.stream.first;
expect(url, 'https://github.com/flutter');
pageLoads.close();
});
testWidgets('web history - go back and forward',
(WidgetTester tester) async {
final Completer controllerCompleter = Completer<InAppWebViewController>();