From 55a2a41a04a3b0b9a9b012071aa5b2bcfaf8f650 Mon Sep 17 00:00:00 2001 From: Lorenzo Pichilli Date: Wed, 27 Apr 2022 18:59:19 +0200 Subject: [PATCH] Updated tests --- example/integration_test/constants.dart | 6 + .../in_app_webview/initial_url_request.dart | 20 +- .../javascript_code_evaluation.dart | 210 ++++++++++++ .../in_app_webview/load_url.dart | 117 +++++++ .../integration_test/in_app_webview/main.dart | 4 + .../in_app_webview/set_get_settings.dart | 25 +- example/integration_test/util.dart | 75 ++++ .../webview_flutter_test.dart | 321 +----------------- .../ios/Flutter/flutter_export_environment.sh | 7 +- example/lib/in_app_webiew_example.screen.dart | 7 +- example/lib/main.dart | 2 + 11 files changed, 458 insertions(+), 336 deletions(-) create mode 100644 example/integration_test/constants.dart create mode 100644 example/integration_test/in_app_webview/javascript_code_evaluation.dart create mode 100644 example/integration_test/in_app_webview/load_url.dart create mode 100644 example/integration_test/util.dart diff --git a/example/integration_test/constants.dart b/example/integration_test/constants.dart new file mode 100644 index 00000000..781bc2ee --- /dev/null +++ b/example/integration_test/constants.dart @@ -0,0 +1,6 @@ +final TEST_URL_ABOUT_BLANK = Uri.parse('about:blank'); +final TEST_CROSS_PLATFORM_URL_1 = Uri.parse('https://flutter.dev'); +final TEST_CROSS_PLATFORM_URL_2 = Uri.parse('https://www.bing.com/'); +final TEST_URL_1 = Uri.parse('https://github.com/flutter'); +final TEST_URL_2 = Uri.parse('https://www.google.com/'); +final TEST_WEB_PLATFORM_URL_1 = Uri.parse(Uri.base.toString().replaceFirst("/#/", "/") + 'page.html'); \ No newline at end of file diff --git a/example/integration_test/in_app_webview/initial_url_request.dart b/example/integration_test/in_app_webview/initial_url_request.dart index 12279d12..c6eaa934 100644 --- a/example/integration_test/in_app_webview/initial_url_request.dart +++ b/example/integration_test/in_app_webview/initial_url_request.dart @@ -5,7 +5,15 @@ import 'package:flutter/widgets.dart'; import 'package:flutter_inappwebview/flutter_inappwebview.dart'; import 'package:flutter_test/flutter_test.dart'; +import '../constants.dart'; + void initialUrlRequest() { + final shouldSkip = !kIsWeb || ![ + TargetPlatform.android, + TargetPlatform.iOS, + TargetPlatform.macOS, + ].contains(defaultTargetPlatform); + testWidgets('initialUrlRequest', (WidgetTester tester) async { final Completer controllerCompleter = Completer(); await tester.pumpWidget( @@ -14,20 +22,18 @@ void initialUrlRequest() { child: InAppWebView( key: GlobalKey(), initialUrlRequest: - URLRequest(url: Uri.parse('https://github.com/flutter')), + URLRequest(url: TEST_CROSS_PLATFORM_URL_1), onWebViewCreated: (controller) { controllerCompleter.complete(controller); }, ), ), ); + final InAppWebViewController controller = await controllerCompleter.future; final String? currentUrl = (await controller.getUrl())?.toString(); - expect(currentUrl, 'https://github.com/flutter'); - }, skip: !kIsWeb || ![ - TargetPlatform.android, - TargetPlatform.iOS, - TargetPlatform.macOS, - ].contains(defaultTargetPlatform)); + + expect(currentUrl, TEST_CROSS_PLATFORM_URL_1.toString()); + }, skip: shouldSkip); } \ No newline at end of file diff --git a/example/integration_test/in_app_webview/javascript_code_evaluation.dart b/example/integration_test/in_app_webview/javascript_code_evaluation.dart new file mode 100644 index 00000000..e2efc808 --- /dev/null +++ b/example/integration_test/in_app_webview/javascript_code_evaluation.dart @@ -0,0 +1,210 @@ +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 javascriptCodeEvaluation() { + final shouldSkip = !kIsWeb || + ![ + TargetPlatform.android, + TargetPlatform.iOS, + TargetPlatform.macOS, + ].contains(defaultTargetPlatform); + + group('javascript code evaluation', () { + final shouldSkipTest1 = !kIsWeb || + ![ + TargetPlatform.android, + TargetPlatform.iOS, + TargetPlatform.macOS, + ].contains(defaultTargetPlatform); + + testWidgets('evaluateJavascript', (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_URL_ABOUT_BLANK), + onWebViewCreated: (controller) { + controllerCompleter.complete(controller); + }, + onLoadStop: (controller, url) { + pageLoaded.complete(); + }, + ), + ), + ); + final InAppWebViewController controller = + await controllerCompleter.future; + await pageLoaded.future; + + var result = await controller.evaluateJavascript(source: """ + [1, true, ["bar", 5], {"foo": "baz"}]; + """); + expect(result, isNotNull); + expect(result[0], 1); + expect(result[1], true); + expect(listEquals(result[2] as List?, ["bar", 5]), true); + expect( + mapEquals(result[3]?.cast(), {"foo": "baz"}), true); + }, skip: shouldSkipTest1); + + final shouldSkipTest2 = kIsWeb || + ![ + TargetPlatform.android, + TargetPlatform.iOS, + TargetPlatform.macOS, + ].contains(defaultTargetPlatform); + + testWidgets('evaluateJavascript with content world', + (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_URL_ABOUT_BLANK), + onWebViewCreated: (controller) { + controllerCompleter.complete(controller); + }, + onLoadStop: (controller, url) { + pageLoaded.complete(); + }, + ), + ), + ); + final InAppWebViewController controller = + await controllerCompleter.future; + await pageLoaded.future; + + await controller.evaluateJavascript( + source: "var foo = 49;", + contentWorld: ContentWorld.world(name: "custom-world")); + var result = await controller.evaluateJavascript(source: "foo"); + expect(result, isNull); + + result = await controller.evaluateJavascript( + source: "foo", + contentWorld: ContentWorld.world(name: "custom-world")); + expect(result, 49); + }, skip: shouldSkipTest2); + + final shouldSkipTest3 = kIsWeb || + ![ + TargetPlatform.android, + TargetPlatform.iOS, + TargetPlatform.macOS, + ].contains(defaultTargetPlatform); + + testWidgets('callAsyncJavaScript', (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_URL_ABOUT_BLANK), + onWebViewCreated: (controller) { + controllerCompleter.complete(controller); + }, + onLoadStop: (controller, url) { + pageLoaded.complete(); + }, + ), + ), + ); + final InAppWebViewController controller = + await controllerCompleter.future; + await pageLoaded.future; + + final String functionBody = """ + var p = new Promise(function (resolve, reject) { + window.setTimeout(function() { + if (x >= 0) { + resolve(x); + } else { + reject(y); + } + }, 1000); + }); + await p; + return p; + """; + + var result = await controller.callAsyncJavaScript( + functionBody: functionBody, + arguments: {'x': 49, 'y': 'error message'}); + expect(result, isNotNull); + expect(result!.error, isNull); + expect(result.value, 49); + + result = await controller.callAsyncJavaScript( + functionBody: functionBody, + arguments: {'x': -49, 'y': 'error message'}); + expect(result, isNotNull); + expect(result!.value, isNull); + expect(result.error, 'error message'); + }, skip: shouldSkipTest3); + + final shouldSkipTest4 = kIsWeb || + ![ + TargetPlatform.android, + TargetPlatform.iOS, + TargetPlatform.macOS, + ].contains(defaultTargetPlatform); + + testWidgets('callAsyncJavaScript with content world', + (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_URL_ABOUT_BLANK), + onWebViewCreated: (controller) { + controllerCompleter.complete(controller); + }, + onLoadStop: (controller, url) { + pageLoaded.complete(); + }, + ), + ), + ); + final InAppWebViewController controller = + await controllerCompleter.future; + await pageLoaded.future; + + await controller.callAsyncJavaScript( + functionBody: "window.foo = 49;", + contentWorld: ContentWorld.world(name: "custom-world")); + var result = await controller.callAsyncJavaScript( + functionBody: "return window.foo;"); + expect(result, isNotNull); + expect(result!.error, isNull); + expect(result.value, isNull); + + result = await controller.callAsyncJavaScript( + functionBody: "return window.foo;", + contentWorld: ContentWorld.world(name: "custom-world")); + expect(result, isNotNull); + expect(result!.error, isNull); + expect(result.value, 49); + }, skip: shouldSkipTest4); + }, skip: shouldSkip); +} diff --git a/example/integration_test/in_app_webview/load_url.dart b/example/integration_test/in_app_webview/load_url.dart new file mode 100644 index 00000000..c2f071f3 --- /dev/null +++ b/example/integration_test/in_app_webview/load_url.dart @@ -0,0 +1,117 @@ +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 loadUrl() { + final shouldSkip = !kIsWeb || ![ + TargetPlatform.android, + TargetPlatform.iOS, + TargetPlatform.macOS, + ].contains(defaultTargetPlatform); + + group('load url', () { + final shouldSkipTest1 = !kIsWeb || + ![ + TargetPlatform.android, + TargetPlatform.iOS, + TargetPlatform.macOS, + ].contains(defaultTargetPlatform); + + 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()); + }, + ), + ), + ); + final InAppWebViewController controller = + await controllerCompleter.future; + var url = await pageLoads.stream.first; + expect(url, TEST_CROSS_PLATFORM_URL_1.toString()); + + await controller.loadUrl( + urlRequest: URLRequest(url: TEST_CROSS_PLATFORM_URL_2)); + url = await pageLoads.stream.first; + expect(url, TEST_CROSS_PLATFORM_URL_2.toString()); + + 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); +} \ No newline at end of file diff --git a/example/integration_test/in_app_webview/main.dart b/example/integration_test/in_app_webview/main.dart index 8392cd0b..fc3d2a6e 100644 --- a/example/integration_test/in_app_webview/main.dart +++ b/example/integration_test/in_app_webview/main.dart @@ -2,10 +2,14 @@ import 'package:flutter_test/flutter_test.dart'; import 'initial_url_request.dart'; import 'set_get_settings.dart'; +import 'javascript_code_evaluation.dart'; +import 'load_url.dart'; void main() { group('InAppWebView', () { initialUrlRequest(); setGetSettings(); + javascriptCodeEvaluation(); + loadUrl(); }); } \ No newline at end of file diff --git a/example/integration_test/in_app_webview/set_get_settings.dart b/example/integration_test/in_app_webview/set_get_settings.dart index ff714ec9..1001089f 100644 --- a/example/integration_test/in_app_webview/set_get_settings.dart +++ b/example/integration_test/in_app_webview/set_get_settings.dart @@ -1,10 +1,21 @@ 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 setGetSettings() { + final shouldSkip = !kIsWeb || ![ + TargetPlatform.android, + TargetPlatform.iOS, + TargetPlatform.macOS, + ].contains(defaultTargetPlatform); + + final url = !kIsWeb ? TEST_CROSS_PLATFORM_URL_1 : TEST_WEB_PLATFORM_URL_1; + testWidgets('set/get settings', (WidgetTester tester) async { final Completer controllerCompleter = Completer(); final Completer pageLoaded = Completer(); @@ -15,7 +26,7 @@ void setGetSettings() { child: InAppWebView( key: GlobalKey(), initialUrlRequest: - URLRequest(url: Uri.parse('https://github.com/flutter')), + URLRequest(url: url), initialSettings: InAppWebViewSettings( javaScriptEnabled: false ), @@ -36,11 +47,21 @@ void setGetSettings() { expect(settings, isNotNull); expect(settings!.javaScriptEnabled, false); + if (kIsWeb) { + expect(settings.iframeSandbox, isNotNull); + expect(settings.iframeSandbox!.contains(Sandbox.ALLOW_SCRIPTS), false); + } + await controller.setSettings(settings: InAppWebViewSettings( javaScriptEnabled: true)); settings = await controller.getSettings(); expect(settings, isNotNull); expect(settings!.javaScriptEnabled, true); - }); + + if (kIsWeb) { + expect(settings.iframeSandbox, isNotNull); + expect(settings.iframeSandbox!.contains(Sandbox.ALLOW_SCRIPTS), true); + } + }, skip: shouldSkip); } \ No newline at end of file diff --git a/example/integration_test/util.dart b/example/integration_test/util.dart new file mode 100644 index 00000000..0a8d688e --- /dev/null +++ b/example/integration_test/util.dart @@ -0,0 +1,75 @@ +import 'dart:async'; +import 'dart:collection'; + +import 'package:flutter_inappwebview/flutter_inappwebview.dart'; +import 'package:flutter_test/flutter_test.dart'; + +/// Returns a matcher that matches the isNullOrEmpty property. +const Matcher isNullOrEmpty = _NullOrEmpty(); + +class _NullOrEmpty extends Matcher { + const _NullOrEmpty(); + + @override + bool matches(Object? item, Map matchState) => + item == null || (item as dynamic).isEmpty; + + @override + Description describe(Description description) => + description.add('null or empty'); +} + +class Foo { + String? bar; + String? baz; + + Foo({this.bar, this.baz}); + + Map toJson() { + return {'bar': this.bar, 'baz': this.baz}; + } +} + +class MyInAppBrowser extends InAppBrowser { + final Completer browserCreated = Completer(); + final Completer firstPageLoaded = Completer(); + + MyInAppBrowser( + {int? windowId, UnmodifiableListView? initialUserScripts}) + : super(windowId: windowId, initialUserScripts: initialUserScripts); + + @override + Future onBrowserCreated() async { + browserCreated.complete(); + } + + @override + void onLoadStop(Uri? url) { + super.onLoadStop(url); + + if (!firstPageLoaded.isCompleted) { + firstPageLoaded.complete(); + } + } +} + +class MyChromeSafariBrowser extends ChromeSafariBrowser { + final Completer browserCreated = Completer(); + final Completer firstPageLoaded = Completer(); + final Completer browserClosed = Completer(); + + @override + void onOpened() { + browserCreated.complete(); + } + + @override + void onCompletedInitialLoad() { + firstPageLoaded.complete(); + } + + @override + void onClosed() { + browserClosed.complete(); + } +} \ No newline at end of file diff --git a/example/integration_test/webview_flutter_test.dart b/example/integration_test/webview_flutter_test.dart index 871a671f..0fb93521 100644 --- a/example/integration_test/webview_flutter_test.dart +++ b/example/integration_test/webview_flutter_test.dart @@ -17,75 +17,7 @@ import 'in_app_webview/main.dart' as in_app_webview_test; import '.env.dart'; -/// Returns a matcher that matches the isNullOrEmpty property. -const Matcher isNullOrEmpty = _NullOrEmpty(); - -class _NullOrEmpty extends Matcher { - const _NullOrEmpty(); - - @override - bool matches(Object? item, Map matchState) => - item == null || (item as dynamic).isEmpty; - - @override - Description describe(Description description) => - description.add('null or empty'); -} - -class Foo { - String? bar; - String? baz; - - Foo({this.bar, this.baz}); - - Map toJson() { - return {'bar': this.bar, 'baz': this.baz}; - } -} - -class MyInAppBrowser extends InAppBrowser { - final Completer browserCreated = Completer(); - final Completer firstPageLoaded = Completer(); - - MyInAppBrowser( - {int? windowId, UnmodifiableListView? initialUserScripts}) - : super(windowId: windowId, initialUserScripts: initialUserScripts); - - @override - Future onBrowserCreated() async { - browserCreated.complete(); - } - - @override - void onLoadStop(Uri? url) { - super.onLoadStop(url); - - if (!firstPageLoaded.isCompleted) { - firstPageLoaded.complete(); - } - } -} - -class MyChromeSafariBrowser extends ChromeSafariBrowser { - final Completer browserCreated = Completer(); - final Completer firstPageLoaded = Completer(); - final Completer browserClosed = Completer(); - - @override - void onOpened() { - browserCreated.complete(); - } - - @override - void onCompletedInitialLoad() { - firstPageLoaded.complete(); - } - - @override - void onClosed() { - browserClosed.complete(); - } -} +import 'util.dart'; void main() { IntegrationTestWidgetsFlutterBinding.ensureInitialized(); @@ -97,258 +29,7 @@ void main() { group('OLD InAppWebView', () { - group('javascript code evaluation', () { - testWidgets('evaluateJavascript', (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('about:blank')), - onWebViewCreated: (controller) { - controllerCompleter.complete(controller); - }, - onLoadStop: (controller, url) { - pageLoaded.complete(); - }, - ), - ), - ); - final InAppWebViewController controller = - await controllerCompleter.future; - await pageLoaded.future; - - var result = await controller.evaluateJavascript(source: """ - [1, true, ["bar", 5], {"foo": "baz"}]; - """); - expect(result, isNotNull); - expect(result[0], 1); - expect(result[1], true); - expect(listEquals(result[2] as List?, ["bar", 5]), true); - expect( - mapEquals(result[3]?.cast(), {"foo": "baz"}), true); - }); - - testWidgets('evaluateJavascript with content world', - (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('about:blank')), - onWebViewCreated: (controller) { - controllerCompleter.complete(controller); - }, - onLoadStop: (controller, url) { - pageLoaded.complete(); - }, - ), - ), - ); - final InAppWebViewController controller = - await controllerCompleter.future; - await pageLoaded.future; - - await controller.evaluateJavascript( - source: "var foo = 49;", - contentWorld: ContentWorld.world(name: "custom-world")); - var result = await controller.evaluateJavascript(source: "foo"); - expect(result, isNull); - - result = await controller.evaluateJavascript( - source: "foo", - contentWorld: ContentWorld.world(name: "custom-world")); - expect(result, 49); - }); - - testWidgets('callAsyncJavaScript', (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('about:blank')), - onWebViewCreated: (controller) { - controllerCompleter.complete(controller); - }, - onLoadStop: (controller, url) { - pageLoaded.complete(); - }, - ), - ), - ); - final InAppWebViewController controller = - await controllerCompleter.future; - await pageLoaded.future; - - final String functionBody = """ - var p = new Promise(function (resolve, reject) { - window.setTimeout(function() { - if (x >= 0) { - resolve(x); - } else { - reject(y); - } - }, 1000); - }); - await p; - return p; - """; - - var result = await controller.callAsyncJavaScript( - functionBody: functionBody, - arguments: {'x': 49, 'y': 'error message'}); - expect(result, isNotNull); - expect(result!.error, isNull); - expect(result.value, 49); - - result = await controller.callAsyncJavaScript( - functionBody: functionBody, - arguments: {'x': -49, 'y': 'error message'}); - expect(result, isNotNull); - expect(result!.value, isNull); - expect(result.error, 'error message'); - }); - - testWidgets('callAsyncJavaScript with content world', - (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('about:blank')), - onWebViewCreated: (controller) { - controllerCompleter.complete(controller); - }, - onLoadStop: (controller, url) { - pageLoaded.complete(); - }, - ), - ), - ); - final InAppWebViewController controller = - await controllerCompleter.future; - await pageLoaded.future; - - await controller.callAsyncJavaScript( - functionBody: "window.foo = 49;", - contentWorld: ContentWorld.world(name: "custom-world")); - var result = await controller.callAsyncJavaScript( - functionBody: "return window.foo;"); - expect(result, isNotNull); - expect(result!.error, isNull); - expect(result.value, isNull); - - result = await controller.callAsyncJavaScript( - functionBody: "return window.foo;", - contentWorld: ContentWorld.world(name: "custom-world")); - expect(result, isNotNull); - expect(result!.error, isNull); - expect(result.value, 49); - }); - }); - - 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: 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, 'https://github.com/flutter'); - - await controller.loadUrl( - urlRequest: URLRequest(url: Uri.parse('https://www.google.com/'))); - url = await pageLoads.stream.first; - expect(url, 'https://www.google.com/'); - - pageLoads.close(); - }); - - 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: Uri.parse('https://github.com/flutter')), - onWebViewCreated: (controller) { - controllerCompleter.complete(controller); - }, - initialOptions: InAppWebViewGroupOptions( - crossPlatform: InAppWebViewOptions(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(); - }); // group("iOS loadFileURL", () { // late Directory appSupportDir; diff --git a/example/ios/Flutter/flutter_export_environment.sh b/example/ios/Flutter/flutter_export_environment.sh index dad54015..9e98dd5e 100755 --- a/example/ios/Flutter/flutter_export_environment.sh +++ b/example/ios/Flutter/flutter_export_environment.sh @@ -3,11 +3,12 @@ export "FLUTTER_ROOT=/Users/lorenzopichilli/fvm/versions/2.10.4" export "FLUTTER_APPLICATION_PATH=/Users/lorenzopichilli/Desktop/flutter_inappwebview/example" export "COCOAPODS_PARALLEL_CODE_SIGN=true" -export "FLUTTER_TARGET=lib/main.dart" +export "FLUTTER_TARGET=/Users/lorenzopichilli/Desktop/flutter_inappwebview/example/lib/main.dart" export "FLUTTER_BUILD_DIR=build" export "FLUTTER_BUILD_NAME=1.0.0" export "FLUTTER_BUILD_NUMBER=1" +export "DART_DEFINES=Zmx1dHRlci5pbnNwZWN0b3Iuc3RydWN0dXJlZEVycm9ycz10cnVl,RkxVVFRFUl9XRUJfQVVUT19ERVRFQ1Q9dHJ1ZQ==" export "DART_OBFUSCATION=false" -export "TRACK_WIDGET_CREATION=false" +export "TRACK_WIDGET_CREATION=true" export "TREE_SHAKE_ICONS=false" -export "PACKAGE_CONFIG=.packages" +export "PACKAGE_CONFIG=/Users/lorenzopichilli/Desktop/flutter_inappwebview/example/.dart_tool/package_config.json" diff --git a/example/lib/in_app_webiew_example.screen.dart b/example/lib/in_app_webiew_example.screen.dart index 18a5607a..11a59e7c 100755 --- a/example/lib/in_app_webiew_example.screen.dart +++ b/example/lib/in_app_webiew_example.screen.dart @@ -115,10 +115,10 @@ class _InAppWebViewExampleScreenState extends State { children: [ InAppWebView( key: webViewKey, - // initialUrlRequest: - // URLRequest(url: Uri.parse("https://flutter.dev")), initialUrlRequest: - URLRequest(url: Uri.parse(Uri.base.toString().replaceFirst("/#/", "/") + 'page.html')), + URLRequest(url: Uri.parse("https://flutter.dev")), + // initialUrlRequest: + // URLRequest(url: Uri.parse(Uri.base.toString().replaceFirst("/#/", "/") + 'page.html')), // initialFile: "assets/index.html", initialUserScripts: UnmodifiableListView([]), initialSettings: settings, @@ -169,7 +169,6 @@ class _InAppWebViewExampleScreenState extends State { this.url = url.toString(); urlController.text = this.url; }); - }, onLoadError: (controller, url, code, message) { pullToRefreshController?.endRefreshing(); diff --git a/example/lib/main.dart b/example/lib/main.dart index 3078a4a1..f453b613 100755 --- a/example/lib/main.dart +++ b/example/lib/main.dart @@ -19,6 +19,8 @@ Future main() async { // await Permission.microphone.request(); // await Permission.storage.request(); + WebView.debugLogging = true; + if (defaultTargetPlatform == TargetPlatform.android) { await InAppWebViewController.setWebContentsDebuggingEnabled(true);