From fa4132e33cc4af840374a512d17236a20f1c7ebe Mon Sep 17 00:00:00 2001 From: Lorenzo Pichilli Date: Thu, 28 Apr 2022 20:34:54 +0200 Subject: [PATCH] update tests --- .../in_app_webview/initial_user_scripts.dart | 75 ++++ .../in_app_webview/load_data.dart | 78 ++++ .../in_app_webview/load_file.dart | 55 +++ .../in_app_webview/load_url.dart | 120 ++---- .../integration_test/in_app_webview/main.dart | 12 + .../on_navigation_response.dart | 88 ++++ .../in_app_webview/post_requests.dart | 153 +++++++ .../in_app_webview/reload.dart | 52 +++ .../webview_flutter_test.dart | 391 ------------------ 9 files changed, 541 insertions(+), 483 deletions(-) create mode 100644 example/integration_test/in_app_webview/initial_user_scripts.dart create mode 100644 example/integration_test/in_app_webview/load_data.dart create mode 100644 example/integration_test/in_app_webview/load_file.dart create mode 100644 example/integration_test/in_app_webview/on_navigation_response.dart create mode 100644 example/integration_test/in_app_webview/post_requests.dart create mode 100644 example/integration_test/in_app_webview/reload.dart diff --git a/example/integration_test/in_app_webview/initial_user_scripts.dart b/example/integration_test/in_app_webview/initial_user_scripts.dart new file mode 100644 index 00000000..2e9a349b --- /dev/null +++ b/example/integration_test/in_app_webview/initial_user_scripts.dart @@ -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(); + final Completer pageLoaded = Completer(); + + await tester.pumpWidget( + Directionality( + textDirection: TextDirection.ltr, + child: InAppWebView( + key: GlobalKey(), + initialUrlRequest: URLRequest(url: TEST_CROSS_PLATFORM_URL_1), + initialUserScripts: UnmodifiableListView([ + 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); +} diff --git a/example/integration_test/in_app_webview/load_data.dart b/example/integration_test/in_app_webview/load_data.dart new file mode 100644 index 00000000..e05e40e3 --- /dev/null +++ b/example/integration_test/in_app_webview/load_data.dart @@ -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(); + final StreamController pageLoads = + StreamController.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 = """ + + + + + + + + + + + placeholder 100x50 + + +"""; + 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); +} diff --git a/example/integration_test/in_app_webview/load_file.dart b/example/integration_test/in_app_webview/load_file.dart new file mode 100644 index 00000000..f9f46801 --- /dev/null +++ b/example/integration_test/in_app_webview/load_file.dart @@ -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(); + final StreamController pageLoads = + StreamController.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); +} diff --git a/example/integration_test/in_app_webview/load_url.dart b/example/integration_test/in_app_webview/load_url.dart index 0b9d7553..09eca075 100644 --- a/example/integration_test/in_app_webview/load_url.dart +++ b/example/integration_test/in_app_webview/load_url.dart @@ -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(); + final StreamController pageLoads = + StreamController.broadcast(); - testWidgets('loadUrl', (WidgetTester tester) async { - final Completer controllerCompleter = Completer(); - final StreamController pageLoads = - StreamController.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(); - final StreamController pageStarts = - StreamController.broadcast(); - final StreamController pageLoads = - StreamController.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 headers = { - '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(); + }); } diff --git a/example/integration_test/in_app_webview/main.dart b/example/integration_test/in_app_webview/main.dart index c20a2257..3cd61cd0 100644 --- a/example/integration_test/in_app_webview/main.dart +++ b/example/integration_test/in_app_webview/main.dart @@ -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(); }); } \ No newline at end of file diff --git a/example/integration_test/in_app_webview/on_navigation_response.dart b/example/integration_test/in_app_webview/on_navigation_response.dart new file mode 100644 index 00000000..99a09497 --- /dev/null +++ b/example/integration_test/in_app_webview/on_navigation_response.dart @@ -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(); + final Completer pageLoaded = Completer(); + final Completer onNavigationResponseCompleter = + Completer(); + + 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(); + final Completer pageLoaded = Completer(); + final Completer onNavigationResponseCompleter = + Completer(); + + 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); +} diff --git a/example/integration_test/in_app_webview/post_requests.dart b/example/integration_test/in_app_webview/post_requests.dart new file mode 100644 index 00000000..7bb59076 --- /dev/null +++ b/example/integration_test/in_app_webview/post_requests.dart @@ -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(); + final Completer postPageLoaded = Completer(); + + 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(); + final Completer postPageLoaded = Completer(); + + 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(); + final Completer postPageLoaded = Completer(); + + 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); +} diff --git a/example/integration_test/in_app_webview/reload.dart b/example/integration_test/in_app_webview/reload.dart new file mode 100644 index 00000000..64163c4f --- /dev/null +++ b/example/integration_test/in_app_webview/reload.dart @@ -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(); + final StreamController pageLoads = + StreamController.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); +} diff --git a/example/integration_test/webview_flutter_test.dart b/example/integration_test/webview_flutter_test.dart index 7b423351..22af6eb8 100644 --- a/example/integration_test/webview_flutter_test.dart +++ b/example/integration_test/webview_flutter_test.dart @@ -28,397 +28,6 @@ void main() { group('InAppWebView', () { - group("iosOnNavigationResponse", () { - testWidgets('allow navigation', (WidgetTester tester) async { - final Completer controllerCompleter = - Completer(); - final Completer pageLoaded = Completer(); - final Completer onNavigationResponseCompleter = - Completer(); - - 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(); - final Completer pageLoaded = Completer(); - final Completer onNavigationResponseCompleter = - Completer(); - - 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(); - final Completer pageLoaded = Completer(); - - await tester.pumpWidget( - Directionality( - textDirection: TextDirection.ltr, - child: InAppWebView( - key: GlobalKey(), - initialUrlRequest: - URLRequest(url: Uri.parse('https://flutter.dev')), - initialUserScripts: UnmodifiableListView([ - 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(); - final Completer postPageLoaded = Completer(); - - 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(); - final Completer postPageLoaded = Completer(); - - 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(); - final Completer postPageLoaded = Completer(); - - 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(); - final StreamController pageLoads = - StreamController.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: """ - - - - - - - - - - - placeholder 100x50 - - -""", - 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(); - final StreamController pageLoads = - StreamController.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(); - final StreamController pageLoads = - StreamController.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();