fixed plugin dependencies, added getUrl web support

This commit is contained in:
Lorenzo Pichilli 2022-04-27 13:39:00 +02:00
parent 5fb73dae1e
commit 3bad02d6e4
7 changed files with 259 additions and 227 deletions

View File

@ -1,5 +1,6 @@
import 'dart:async'; import 'dart:async';
import 'package:flutter/foundation.dart';
import 'package:flutter/widgets.dart'; import 'package:flutter/widgets.dart';
import 'package:flutter_inappwebview/flutter_inappwebview.dart'; import 'package:flutter_inappwebview/flutter_inappwebview.dart';
import 'package:flutter_test/flutter_test.dart'; import 'package:flutter_test/flutter_test.dart';
@ -24,5 +25,9 @@ void initialUrlRequest() {
await controllerCompleter.future; await controllerCompleter.future;
final String? currentUrl = (await controller.getUrl())?.toString(); final String? currentUrl = (await controller.getUrl())?.toString();
expect(currentUrl, 'https://github.com/flutter'); expect(currentUrl, 'https://github.com/flutter');
}); }, skip: !kIsWeb || ![
TargetPlatform.android,
TargetPlatform.iOS,
TargetPlatform.macOS,
].contains(defaultTargetPlatform));
} }

View File

@ -1,7 +1,7 @@
import 'dart:async'; import 'dart:async';
import 'dart:collection'; import 'dart:collection';
import 'dart:convert'; import 'dart:convert';
import 'dart:io'; // import 'dart:io';
import 'dart:typed_data'; import 'dart:typed_data';
import 'package:flutter/foundation.dart'; import 'package:flutter/foundation.dart';
@ -89,7 +89,7 @@ class MyChromeSafariBrowser extends ChromeSafariBrowser {
void main() { void main() {
IntegrationTestWidgetsFlutterBinding.ensureInitialized(); IntegrationTestWidgetsFlutterBinding.ensureInitialized();
if (Platform.isAndroid) { if (defaultTargetPlatform == TargetPlatform.android) {
InAppWebViewController.setWebContentsDebuggingEnabled(true); InAppWebViewController.setWebContentsDebuggingEnabled(true);
} }
@ -350,141 +350,141 @@ void main() {
pageLoads.close(); pageLoads.close();
}); });
group("iOS loadFileURL", () { // group("iOS loadFileURL", () {
late Directory appSupportDir; // late Directory appSupportDir;
late File fileHtml; // late File fileHtml;
late File fileJs; // late File fileJs;
//
setUpAll(() async { // setUpAll(() async {
appSupportDir = (await getApplicationSupportDirectory()); // appSupportDir = (await getApplicationSupportDirectory());
//
final Directory htmlFolder = Directory('${appSupportDir.path}/html/'); // final Directory htmlFolder = Directory('${appSupportDir.path}/html/');
if (!await htmlFolder.exists()) { // if (!await htmlFolder.exists()) {
await htmlFolder.create(recursive: true); // await htmlFolder.create(recursive: true);
} // }
//
final Directory jsFolder = Directory('${appSupportDir.path}/js/'); // final Directory jsFolder = Directory('${appSupportDir.path}/js/');
if (!await jsFolder.exists()) { // if (!await jsFolder.exists()) {
await jsFolder.create(recursive: true); // await jsFolder.create(recursive: true);
} // }
//
var html = """ // var html = """
<!DOCTYPE html><html> // <!DOCTYPE html><html>
<head> // <head>
<title>file scheme</title> // <title>file scheme</title>
</head> // </head>
<body> // <body>
<script src="../js/main.js"></script> // <script src="../js/main.js"></script>
</body> // </body>
</html> // </html>
"""; // """;
fileHtml = File(htmlFolder.path + "index.html"); // fileHtml = File(htmlFolder.path + "index.html");
fileHtml.writeAsStringSync(html); // fileHtml.writeAsStringSync(html);
//
var js = """ // var js = """
console.log('message'); // console.log('message');
"""; // """;
fileJs = File(jsFolder.path + "main.js"); // fileJs = File(jsFolder.path + "main.js");
fileJs.writeAsStringSync(js); // fileJs.writeAsStringSync(js);
}); // });
//
testWidgets('initialUrl with file:// scheme and allowingReadAccessTo', // testWidgets('initialUrl with file:// scheme and allowingReadAccessTo',
(WidgetTester tester) async { // (WidgetTester tester) async {
final Completer<ConsoleMessage?> consoleMessageShouldNotComplete = // final Completer<ConsoleMessage?> consoleMessageShouldNotComplete =
Completer<ConsoleMessage?>(); // Completer<ConsoleMessage?>();
await tester.pumpWidget( // await tester.pumpWidget(
Directionality( // Directionality(
textDirection: TextDirection.ltr, // textDirection: TextDirection.ltr,
child: InAppWebView( // child: InAppWebView(
key: GlobalKey(), // key: GlobalKey(),
initialUrlRequest: // initialUrlRequest:
URLRequest(url: Uri.parse('file://${fileHtml.path}')), // URLRequest(url: Uri.parse('file://${fileHtml.path}')),
onConsoleMessage: (controller, consoleMessage) { // onConsoleMessage: (controller, consoleMessage) {
consoleMessageShouldNotComplete.complete(consoleMessage); // consoleMessageShouldNotComplete.complete(consoleMessage);
}, // },
), // ),
), // ),
); // );
var result = await consoleMessageShouldNotComplete.future // var result = await consoleMessageShouldNotComplete.future
.timeout(const Duration(seconds: 2), onTimeout: () => null); // .timeout(const Duration(seconds: 2), onTimeout: () => null);
expect(result, null); // expect(result, null);
//
final Completer<ConsoleMessage> consoleMessageCompleter = // final Completer<ConsoleMessage> consoleMessageCompleter =
Completer<ConsoleMessage>(); // Completer<ConsoleMessage>();
await tester.pumpWidget( // await tester.pumpWidget(
Directionality( // Directionality(
textDirection: TextDirection.ltr, // textDirection: TextDirection.ltr,
child: InAppWebView( // child: InAppWebView(
key: GlobalKey(), // key: GlobalKey(),
initialUrlRequest: // initialUrlRequest:
URLRequest(url: Uri.parse('file://${fileHtml.path}')), // URLRequest(url: Uri.parse('file://${fileHtml.path}')),
initialOptions: InAppWebViewGroupOptions( // initialOptions: InAppWebViewGroupOptions(
ios: IOSInAppWebViewOptions( // ios: IOSInAppWebViewOptions(
allowingReadAccessTo: // allowingReadAccessTo:
Uri.parse('file://${appSupportDir.path}/'))), // Uri.parse('file://${appSupportDir.path}/'))),
onConsoleMessage: (controller, consoleMessage) { // onConsoleMessage: (controller, consoleMessage) {
consoleMessageCompleter.complete(consoleMessage); // consoleMessageCompleter.complete(consoleMessage);
}, // },
), // ),
), // ),
); // );
final ConsoleMessage consoleMessage = // final ConsoleMessage consoleMessage =
await consoleMessageCompleter.future; // await consoleMessageCompleter.future;
expect(consoleMessage.messageLevel, ConsoleMessageLevel.LOG); // expect(consoleMessage.messageLevel, ConsoleMessageLevel.LOG);
expect(consoleMessage.message, 'message'); // expect(consoleMessage.message, 'message');
}, skip: !Platform.isIOS); // }, skip: defaultTargetPlatform != TargetPlatform.iOS);
//
testWidgets( // testWidgets(
'loadUrl with file:// scheme and allowingReadAccessTo argument', // 'loadUrl with file:// scheme and allowingReadAccessTo argument',
(WidgetTester tester) async { // (WidgetTester tester) async {
final Completer<ConsoleMessage?> consoleMessageShouldNotComplete = // final Completer<ConsoleMessage?> consoleMessageShouldNotComplete =
Completer<ConsoleMessage?>(); // Completer<ConsoleMessage?>();
await tester.pumpWidget( // await tester.pumpWidget(
Directionality( // Directionality(
textDirection: TextDirection.ltr, // textDirection: TextDirection.ltr,
child: InAppWebView( // child: InAppWebView(
key: GlobalKey(), // key: GlobalKey(),
onWebViewCreated: (controller) { // onWebViewCreated: (controller) {
controller.loadUrl( // controller.loadUrl(
urlRequest: // urlRequest:
URLRequest(url: Uri.parse('file://${fileHtml.path}'))); // URLRequest(url: Uri.parse('file://${fileHtml.path}')));
}, // },
onConsoleMessage: (controller, consoleMessage) { // onConsoleMessage: (controller, consoleMessage) {
consoleMessageShouldNotComplete.complete(consoleMessage); // consoleMessageShouldNotComplete.complete(consoleMessage);
}, // },
), // ),
), // ),
); // );
var result = await consoleMessageShouldNotComplete.future // var result = await consoleMessageShouldNotComplete.future
.timeout(const Duration(seconds: 2), onTimeout: () => null); // .timeout(const Duration(seconds: 2), onTimeout: () => null);
expect(result, null); // expect(result, null);
//
final Completer<ConsoleMessage> consoleMessageCompleter = // final Completer<ConsoleMessage> consoleMessageCompleter =
Completer<ConsoleMessage>(); // Completer<ConsoleMessage>();
await tester.pumpWidget( // await tester.pumpWidget(
Directionality( // Directionality(
textDirection: TextDirection.ltr, // textDirection: TextDirection.ltr,
child: InAppWebView( // child: InAppWebView(
key: GlobalKey(), // key: GlobalKey(),
onWebViewCreated: (controller) { // onWebViewCreated: (controller) {
controller.loadUrl( // controller.loadUrl(
urlRequest: // urlRequest:
URLRequest(url: Uri.parse('file://${fileHtml.path}')), // URLRequest(url: Uri.parse('file://${fileHtml.path}')),
allowingReadAccessTo: // allowingReadAccessTo:
Uri.parse('file://${appSupportDir.path}/')); // Uri.parse('file://${appSupportDir.path}/'));
}, // },
onConsoleMessage: (controller, consoleMessage) { // onConsoleMessage: (controller, consoleMessage) {
consoleMessageCompleter.complete(consoleMessage); // consoleMessageCompleter.complete(consoleMessage);
}, // },
), // ),
), // ),
); // );
final ConsoleMessage consoleMessage = // final ConsoleMessage consoleMessage =
await consoleMessageCompleter.future; // await consoleMessageCompleter.future;
expect(consoleMessage.messageLevel, ConsoleMessageLevel.LOG); // expect(consoleMessage.messageLevel, ConsoleMessageLevel.LOG);
expect(consoleMessage.message, 'message'); // expect(consoleMessage.message, 'message');
}, skip: !Platform.isIOS); // }, skip: defaultTargetPlatform != TargetPlatform.iOS);
}, skip: !Platform.isIOS); // }, skip: defaultTargetPlatform != TargetPlatform.iOS);
testWidgets('JavaScript Handler', (WidgetTester tester) async { testWidgets('JavaScript Handler', (WidgetTester tester) async {
final Completer controllerCompleter = Completer<InAppWebViewController>(); final Completer controllerCompleter = Completer<InAppWebViewController>();
@ -899,7 +899,7 @@ void main() {
await controller.evaluateJavascript(source: "exitFullscreen();"); await controller.evaluateJavascript(source: "exitFullscreen();");
await expectLater(onExitFullscreenCompleter.future, completes); await expectLater(onExitFullscreenCompleter.future, completes);
}, skip: true /*!Platform.isAndroid*/); }, skip: true /*defaultTargetPlatform != TargetPlatform.android*/);
}); });
group('Audio playback policy', () { group('Audio playback policy', () {
@ -1205,8 +1205,8 @@ void main() {
scrollPosY = await controller.getScrollY(); scrollPosY = await controller.getScrollY();
expect(scrollPosX, X_SCROLL * 2); expect(scrollPosX, X_SCROLL * 2);
expect(scrollPosY, Y_SCROLL * 2); expect(scrollPosY, Y_SCROLL * 2);
}, skip: !Platform.isAndroid); }, skip: defaultTargetPlatform != TargetPlatform.android);
}, skip: !Platform.isAndroid); }, skip: defaultTargetPlatform != TargetPlatform.android);
group('shouldOverrideUrlLoading', () { group('shouldOverrideUrlLoading', () {
final String page = final String page =
@ -1318,7 +1318,7 @@ void main() {
'https://github.com/pichillilorenzo/flutter_inappwebview'); 'https://github.com/pichillilorenzo/flutter_inappwebview');
pageLoads.close(); pageLoads.close();
}, skip: !Platform.isIOS); }, skip: defaultTargetPlatform != TargetPlatform.iOS);
testWidgets('can block requests', (WidgetTester tester) async { testWidgets('can block requests', (WidgetTester tester) async {
final Completer controllerCompleter = final Completer controllerCompleter =
@ -1439,9 +1439,9 @@ void main() {
final String url = await errorUrlCompleter.future; final String url = await errorUrlCompleter.future;
final int code = await errorCodeCompleter.future; final int code = await errorCodeCompleter.future;
if (Platform.isAndroid) { if (defaultTargetPlatform == TargetPlatform.android) {
expect(code, -2); expect(code, -2);
} else if (Platform.isIOS) { } else if (defaultTargetPlatform == TargetPlatform.iOS) {
expect(code, -1003); expect(code, -1003);
} }
expect(url, 'https://www.notawebsite..com/'); expect(url, 'https://www.notawebsite..com/');
@ -1502,7 +1502,7 @@ void main() {
await controllerCompleter.future; await controllerCompleter.future;
final String? currentUrl = (await controller.getUrl())?.toString(); final String? currentUrl = (await controller.getUrl())?.toString();
expect(currentUrl, 'https://github.com/flutter'); expect(currentUrl, 'https://github.com/flutter');
}, skip: !Platform.isIOS); }, skip: defaultTargetPlatform != TargetPlatform.iOS);
testWidgets('target _blank opens in same window', testWidgets('target _blank opens in same window',
(WidgetTester tester) async { (WidgetTester tester) async {
@ -1587,7 +1587,7 @@ void main() {
pageLoads.close(); pageLoads.close();
}, },
skip: true /* !Platform.isAndroid */, skip: true /* defaultTargetPlatform != TargetPlatform.android */,
); );
testWidgets( testWidgets(
@ -1663,7 +1663,7 @@ void main() {
completion(null), completion(null),
); );
}, },
skip: !Platform.isAndroid, skip: defaultTargetPlatform != TargetPlatform.android,
); );
group('intercept ajax request', () { group('intercept ajax request', () {
@ -3016,7 +3016,7 @@ setTimeout(function() {
final String url = await pageLoaded.future; final String url = await pageLoaded.future;
expect(url, "chrome://safe-browsing/match?type=malware"); expect(url, "chrome://safe-browsing/match?type=malware");
}, skip: !Platform.isAndroid); }, skip: defaultTargetPlatform != TargetPlatform.android);
testWidgets('onScrollChanged', (WidgetTester tester) async { testWidgets('onScrollChanged', (WidgetTester tester) async {
final Completer controllerCompleter = Completer<InAppWebViewController>(); final Completer controllerCompleter = Completer<InAppWebViewController>();
@ -3295,7 +3295,7 @@ setTimeout(function() {
expect(listEquals(resources, ['android.webkit.resource.VIDEO_CAPTURE']), expect(listEquals(resources, ['android.webkit.resource.VIDEO_CAPTURE']),
true); true);
}, skip: !Platform.isAndroid); }, skip: defaultTargetPlatform != TargetPlatform.android);
testWidgets('androidShouldInterceptRequest', (WidgetTester tester) async { testWidgets('androidShouldInterceptRequest', (WidgetTester tester) async {
List<String> resourceList = [ List<String> resourceList = [
@ -3352,7 +3352,7 @@ setTimeout(function() {
await pageLoaded.future; await pageLoaded.future;
await loadedResourceCompleter.future; await loadedResourceCompleter.future;
expect(resourceLoaded, containsAll(resourceList)); expect(resourceLoaded, containsAll(resourceList));
}, skip: !Platform.isAndroid); }, skip: defaultTargetPlatform != TargetPlatform.android);
testWidgets('androidOnReceivedIcon', (WidgetTester tester) async { testWidgets('androidOnReceivedIcon', (WidgetTester tester) async {
final Completer controllerCompleter = Completer<InAppWebViewController>(); final Completer controllerCompleter = Completer<InAppWebViewController>();
@ -3383,7 +3383,7 @@ setTimeout(function() {
await pageLoaded.future; await pageLoaded.future;
final Uint8List icon = await onReceivedIconCompleter.future; final Uint8List icon = await onReceivedIconCompleter.future;
expect(icon, isNotNull); expect(icon, isNotNull);
}, skip: !Platform.isAndroid); }, skip: defaultTargetPlatform != TargetPlatform.android);
testWidgets('androidOnReceivedTouchIconUrl', (WidgetTester tester) async { testWidgets('androidOnReceivedTouchIconUrl', (WidgetTester tester) async {
final Completer controllerCompleter = Completer<InAppWebViewController>(); final Completer controllerCompleter = Completer<InAppWebViewController>();
@ -3423,7 +3423,7 @@ setTimeout(function() {
final String url = await onReceivedTouchIconUrlCompleter.future; final String url = await onReceivedTouchIconUrlCompleter.future;
expect(url, "https://placehold.it/72x72"); expect(url, "https://placehold.it/72x72");
}, skip: !Platform.isAndroid); }, skip: defaultTargetPlatform != TargetPlatform.android);
testWidgets('androidOnJsBeforeUnload', (WidgetTester tester) async { testWidgets('androidOnJsBeforeUnload', (WidgetTester tester) async {
final Completer controllerCompleter = Completer<InAppWebViewController>(); final Completer controllerCompleter = Completer<InAppWebViewController>();
@ -3466,7 +3466,7 @@ setTimeout(function() {
source: "window.location.href = 'https://github.com/flutter';"); source: "window.location.href = 'https://github.com/flutter';");
final String url = await onJsBeforeUnloadCompleter.future; final String url = await onJsBeforeUnloadCompleter.future;
expect(url, 'https://github.com/flutter'); expect(url, 'https://github.com/flutter');
}, skip: true /*!Platform.isAndroid*/); }, skip: true /*defaultTargetPlatform != TargetPlatform.android*/);
group("iosOnNavigationResponse", () { group("iosOnNavigationResponse", () {
testWidgets('allow navigation', (WidgetTester tester) async { testWidgets('allow navigation', (WidgetTester tester) async {
@ -3503,7 +3503,7 @@ setTimeout(function() {
await pageLoaded.future; await pageLoaded.future;
final String url = await onNavigationResponseCompleter.future; final String url = await onNavigationResponseCompleter.future;
expect(url, 'https://github.com/flutter'); expect(url, 'https://github.com/flutter');
}, skip: !Platform.isIOS); }, skip: defaultTargetPlatform != TargetPlatform.iOS);
testWidgets('cancel navigation', (WidgetTester tester) async { testWidgets('cancel navigation', (WidgetTester tester) async {
final Completer controllerCompleter = final Completer controllerCompleter =
@ -3539,8 +3539,8 @@ setTimeout(function() {
final String url = await onNavigationResponseCompleter.future; final String url = await onNavigationResponseCompleter.future;
expect(url, 'https://github.com/flutter'); expect(url, 'https://github.com/flutter');
expect(pageLoaded.future, doesNotComplete); expect(pageLoaded.future, doesNotComplete);
}, skip: !Platform.isIOS); }, skip: defaultTargetPlatform != TargetPlatform.iOS);
}, skip: !Platform.isIOS); }, skip: defaultTargetPlatform != TargetPlatform.iOS);
testWidgets('initialUserScripts', (WidgetTester tester) async { testWidgets('initialUserScripts', (WidgetTester tester) async {
final Completer controllerCompleter = Completer<InAppWebViewController>(); final Completer controllerCompleter = Completer<InAppWebViewController>();
@ -4104,11 +4104,11 @@ setTimeout(function() {
final InAppWebViewController controller = final InAppWebViewController controller =
await controllerCompleter.future; await controllerCompleter.future;
if (Platform.isAndroid) { if (defaultTargetPlatform == TargetPlatform.android) {
await pageLoaded.future; await pageLoaded.future;
expect(await controller.evaluateJavascript(source: "document.body"), expect(await controller.evaluateJavascript(source: "document.body"),
isNullOrEmpty); isNullOrEmpty);
} else if (Platform.isIOS) { } else if (defaultTargetPlatform == TargetPlatform.iOS) {
expect(pageLoaded.future, doesNotComplete); expect(pageLoaded.future, doesNotComplete);
} }
}); });
@ -4797,52 +4797,52 @@ setTimeout(function() {
pageLoads.close(); pageLoads.close();
}); });
testWidgets('saveWebArchive', (WidgetTester tester) async { // testWidgets('saveWebArchive', (WidgetTester tester) async {
final Completer controllerCompleter = Completer<InAppWebViewController>(); // final Completer controllerCompleter = Completer<InAppWebViewController>();
final Completer<void> pageLoaded = Completer<void>(); // final Completer<void> pageLoaded = Completer<void>();
//
await tester.pumpWidget( // await tester.pumpWidget(
Directionality( // Directionality(
textDirection: TextDirection.ltr, // textDirection: TextDirection.ltr,
child: InAppWebView( // child: InAppWebView(
key: GlobalKey(), // key: GlobalKey(),
initialUrlRequest: // initialUrlRequest:
URLRequest(url: Uri.parse('https://github.com/flutter')), // URLRequest(url: Uri.parse('https://github.com/flutter')),
onWebViewCreated: (controller) { // onWebViewCreated: (controller) {
controllerCompleter.complete(controller); // controllerCompleter.complete(controller);
}, // },
onLoadStop: (controller, url) { // onLoadStop: (controller, url) {
pageLoaded.complete(); // pageLoaded.complete();
}, // },
), // ),
), // ),
); // );
//
final InAppWebViewController controller = // final InAppWebViewController controller =
await controllerCompleter.future; // await controllerCompleter.future;
await pageLoaded.future; // await pageLoaded.future;
//
// wait a little bit after page load otherwise Android will not save the web archive // // wait a little bit after page load otherwise Android will not save the web archive
await Future.delayed(Duration(seconds: 1)); // await Future.delayed(Duration(seconds: 1));
//
var supportDir = await getApplicationSupportDirectory(); // var supportDir = await getApplicationSupportDirectory();
//
var fileName = "flutter-website."; // var fileName = "flutter-website.";
if (Platform.isAndroid) { // if (defaultTargetPlatform == TargetPlatform.android) {
fileName = fileName + WebArchiveFormat.MHT.toValue(); // fileName = fileName + WebArchiveFormat.MHT.toValue();
} else if (Platform.isIOS) { // } else if (defaultTargetPlatform == TargetPlatform.iOS) {
fileName = fileName + WebArchiveFormat.WEBARCHIVE.toValue(); // fileName = fileName + WebArchiveFormat.WEBARCHIVE.toValue();
} // }
//
var fullPath = supportDir.path + Platform.pathSeparator + fileName; // var fullPath = supportDir.path + Platform.pathSeparator + fileName;
var path = await controller.saveWebArchive(filePath: fullPath); // var path = await controller.saveWebArchive(filePath: fullPath);
expect(path, isNotNull); // expect(path, isNotNull);
expect(path, endsWith(fileName)); // expect(path, endsWith(fileName));
//
path = await controller.saveWebArchive( // path = await controller.saveWebArchive(
filePath: supportDir.path, autoname: true); // filePath: supportDir.path, autoname: true);
expect(path, isNotNull); // expect(path, isNotNull);
}); // });
testWidgets('isSecureContext', (WidgetTester tester) async { testWidgets('isSecureContext', (WidgetTester tester) async {
final Completer controllerCompleter = Completer<InAppWebViewController>(); final Completer controllerCompleter = Completer<InAppWebViewController>();
@ -5070,7 +5070,7 @@ setTimeout(function() {
await controllerCompleter.future; await controllerCompleter.future;
await pageLoaded.future; await pageLoaded.future;
await expectLater(controller.android.clearSslPreferences(), completes); await expectLater(controller.android.clearSslPreferences(), completes);
}, skip: !Platform.isAndroid); }, skip: defaultTargetPlatform != TargetPlatform.android);
testWidgets('pause/resume', (WidgetTester tester) async { testWidgets('pause/resume', (WidgetTester tester) async {
final Completer controllerCompleter = final Completer controllerCompleter =
@ -5100,7 +5100,7 @@ setTimeout(function() {
await expectLater(controller.android.pause(), completes); await expectLater(controller.android.pause(), completes);
await Future.delayed(Duration(seconds: 1)); await Future.delayed(Duration(seconds: 1));
await expectLater(controller.android.resume(), completes); await expectLater(controller.android.resume(), completes);
}, skip: !Platform.isAndroid); }, skip: defaultTargetPlatform != TargetPlatform.android);
testWidgets('getOriginalUrl', (WidgetTester tester) async { testWidgets('getOriginalUrl', (WidgetTester tester) async {
final Completer controllerCompleter = final Completer controllerCompleter =
@ -5159,7 +5159,7 @@ setTimeout(function() {
expect(await controller.android.pageDown(bottom: false), true); expect(await controller.android.pageDown(bottom: false), true);
await Future.delayed(Duration(seconds: 1)); await Future.delayed(Duration(seconds: 1));
expect(await controller.android.pageUp(top: false), true); expect(await controller.android.pageUp(top: false), true);
}, skip: !Platform.isAndroid); }, skip: defaultTargetPlatform != TargetPlatform.android);
testWidgets('zoomIn/zoomOut', (WidgetTester tester) async { testWidgets('zoomIn/zoomOut', (WidgetTester tester) async {
final Completer controllerCompleter = final Completer controllerCompleter =
@ -5189,7 +5189,7 @@ setTimeout(function() {
expect(await controller.android.zoomIn(), true); expect(await controller.android.zoomIn(), true);
await Future.delayed(Duration(seconds: 1)); await Future.delayed(Duration(seconds: 1));
expect(await controller.android.zoomOut(), true); expect(await controller.android.zoomOut(), true);
}, skip: !Platform.isAndroid); }, skip: defaultTargetPlatform != TargetPlatform.android);
testWidgets('clearHistory', (WidgetTester tester) async { testWidgets('clearHistory', (WidgetTester tester) async {
final Completer controllerCompleter = final Completer controllerCompleter =
@ -5231,39 +5231,39 @@ setTimeout(function() {
expect(webHistory!.list!.length, 1); expect(webHistory!.list!.length, 1);
pageLoads.close(); pageLoads.close();
}, skip: !Platform.isAndroid); }, skip: defaultTargetPlatform != TargetPlatform.android);
test('clearClientCertPreferences', () async { test('clearClientCertPreferences', () async {
await expectLater( await expectLater(
AndroidInAppWebViewController.clearClientCertPreferences(), AndroidInAppWebViewController.clearClientCertPreferences(),
completes); completes);
}, skip: !Platform.isAndroid); }, skip: defaultTargetPlatform != TargetPlatform.android);
test('getSafeBrowsingPrivacyPolicyUrl', () async { test('getSafeBrowsingPrivacyPolicyUrl', () async {
expect( expect(
await AndroidInAppWebViewController await AndroidInAppWebViewController
.getSafeBrowsingPrivacyPolicyUrl(), .getSafeBrowsingPrivacyPolicyUrl(),
isNotNull); isNotNull);
}, skip: !Platform.isAndroid); }, skip: defaultTargetPlatform != TargetPlatform.android);
test('setSafeBrowsingWhitelist', () async { test('setSafeBrowsingWhitelist', () async {
expect( expect(
await AndroidInAppWebViewController.setSafeBrowsingWhitelist( await AndroidInAppWebViewController.setSafeBrowsingWhitelist(
hosts: ["flutter.dev", "github.com"]), hosts: ["flutter.dev", "github.com"]),
true); true);
}, skip: !Platform.isAndroid); }, skip: defaultTargetPlatform != TargetPlatform.android);
test('getCurrentWebViewPackage', () async { test('getCurrentWebViewPackage', () async {
expect(await AndroidInAppWebViewController.getCurrentWebViewPackage(), expect(await AndroidInAppWebViewController.getCurrentWebViewPackage(),
isNotNull); isNotNull);
}, skip: !Platform.isAndroid); }, skip: defaultTargetPlatform != TargetPlatform.android);
test('setWebContentsDebuggingEnabled', () async { test('setWebContentsDebuggingEnabled', () async {
expect( expect(
AndroidInAppWebViewController.setWebContentsDebuggingEnabled(true), AndroidInAppWebViewController.setWebContentsDebuggingEnabled(true),
completes); completes);
}, skip: !Platform.isAndroid); }, skip: defaultTargetPlatform != TargetPlatform.android);
}, skip: !Platform.isAndroid); }, skip: defaultTargetPlatform != TargetPlatform.android);
group('ios methods', () { group('ios methods', () {
testWidgets('reloadFromOrigin', (WidgetTester tester) async { testWidgets('reloadFromOrigin', (WidgetTester tester) async {
@ -5292,7 +5292,7 @@ setTimeout(function() {
await controllerCompleter.future; await controllerCompleter.future;
await pageLoaded.future; await pageLoaded.future;
await expectLater(controller.ios.reloadFromOrigin(), completes); await expectLater(controller.ios.reloadFromOrigin(), completes);
}, skip: !Platform.isIOS); }, skip: defaultTargetPlatform != TargetPlatform.iOS);
testWidgets('createPdf', (WidgetTester tester) async { testWidgets('createPdf', (WidgetTester tester) async {
final Completer controllerCompleter = final Completer controllerCompleter =
@ -5325,7 +5325,7 @@ setTimeout(function() {
var pdf = await controller.ios var pdf = await controller.ios
.createPdf(iosWKPdfConfiguration: iosWKPdfConfiguration); .createPdf(iosWKPdfConfiguration: iosWKPdfConfiguration);
expect(pdf, isNotNull); expect(pdf, isNotNull);
}, skip: !Platform.isIOS); }, skip: defaultTargetPlatform != TargetPlatform.iOS);
testWidgets('createWebArchiveData', (WidgetTester tester) async { testWidgets('createWebArchiveData', (WidgetTester tester) async {
final Completer controllerCompleter = final Completer controllerCompleter =
@ -5354,7 +5354,7 @@ setTimeout(function() {
await pageLoaded.future; await pageLoaded.future;
expect(await controller.ios.createWebArchiveData(), isNotNull); expect(await controller.ios.createWebArchiveData(), isNotNull);
}, skip: !Platform.isIOS); }, skip: defaultTargetPlatform != TargetPlatform.iOS);
testWidgets('Apple Pay API enabled', (WidgetTester tester) async { testWidgets('Apple Pay API enabled', (WidgetTester tester) async {
final Completer<void> pageLoaded = Completer<void>(); final Completer<void> pageLoaded = Completer<void>();
@ -5398,13 +5398,13 @@ setTimeout(function() {
await pageLoaded.future; await pageLoaded.future;
final message = await alertMessageCompleter.future; final message = await alertMessageCompleter.future;
expect(message, 'true'); expect(message, 'true');
}, skip: !Platform.isIOS); }, skip: defaultTargetPlatform != TargetPlatform.iOS);
test('handlesURLScheme', () async { test('handlesURLScheme', () async {
expect(await IOSInAppWebViewController.handlesURLScheme("http"), true); expect(await IOSInAppWebViewController.handlesURLScheme("http"), true);
expect(await IOSInAppWebViewController.handlesURLScheme("https"), true); expect(await IOSInAppWebViewController.handlesURLScheme("https"), true);
}, skip: !Platform.isIOS); }, skip: defaultTargetPlatform != TargetPlatform.iOS);
}, skip: !Platform.isIOS); }, skip: defaultTargetPlatform != TargetPlatform.iOS);
}); });
group('Service Worker', () { group('Service Worker', () {
@ -5445,7 +5445,7 @@ setTimeout(function() {
); );
expect(completer.future, completes); expect(completer.future, completes);
}, skip: !Platform.isAndroid); }, skip: defaultTargetPlatform != TargetPlatform.android);
testWidgets('setServiceWorkerClient to null', (WidgetTester tester) async { testWidgets('setServiceWorkerClient to null', (WidgetTester tester) async {
final Completer<String> pageLoaded = Completer<String>(); final Completer<String> pageLoaded = Completer<String>();
@ -5478,7 +5478,7 @@ setTimeout(function() {
final String url = await pageLoaded.future; final String url = await pageLoaded.future;
expect(url, "https://mdn.github.io/sw-test/"); expect(url, "https://mdn.github.io/sw-test/");
}, skip: !Platform.isAndroid); }, skip: defaultTargetPlatform != TargetPlatform.android);
}); });
group('Cookie Manager', () { group('Cookie Manager', () {
@ -5924,7 +5924,7 @@ setTimeout(function() {
await chromeSafariBrowser.browserClosed.future; await chromeSafariBrowser.browserClosed.future;
expect(chromeSafariBrowser.isOpened(), false); expect(chromeSafariBrowser.isOpened(), false);
}); });
}, skip: !Platform.isAndroid); }, skip: defaultTargetPlatform != TargetPlatform.android);
}); });
group('InAppLocalhostServer', () { group('InAppLocalhostServer', () {

View File

@ -121,8 +121,9 @@ class _InAppWebViewExampleScreenState extends State<InAppWebViewExampleScreen> {
initialSettings: settings, initialSettings: settings,
// contextMenu: contextMenu, // contextMenu: contextMenu,
pullToRefreshController: pullToRefreshController, pullToRefreshController: pullToRefreshController,
onWebViewCreated: (controller) { onWebViewCreated: (controller) async {
webViewController = controller; webViewController = controller;
print(await controller.getUrl());
}, },
onLoadStart: (controller, url) async { onLoadStart: (controller, url) async {
setState(() { setState(() {

View File

@ -354,7 +354,17 @@ window.flutter_inappwebview = {
console.log(e); console.log(e);
} }
} }
},
getUrl: function () {
var iframe = webView.iframe;
var url = iframe.src;
try {
url = iframe.contentWindow.location.href;
} catch (e) {
console.log(e);
} }
return url;
},
}; };
return webView; return webView;

View File

@ -1179,9 +1179,13 @@ class InAppWebViewController
///Gets the URL for the current page. ///Gets the URL for the current page.
///This is not always the same as the URL passed to [WebView.onLoadStart] because although the load for that URL has begun, the current page may not have changed. ///This is not always the same as the URL passed to [WebView.onLoadStart] because although the load for that URL has begun, the current page may not have changed.
/// ///
///**NOTE for Web**: If `window.location.href` isn't accessible inside the iframe,
///it will return the current value of the `iframe.src` attribute.
///
///**Supported Platforms/Implementations**: ///**Supported Platforms/Implementations**:
///- Android native WebView ([Official API - WebView.getUrl](https://developer.android.com/reference/android/webkit/WebView#getUrl())) ///- Android native WebView ([Official API - WebView.getUrl](https://developer.android.com/reference/android/webkit/WebView#getUrl()))
///- iOS ([Official API - WKWebView.url](https://developer.apple.com/documentation/webkit/wkwebview/1415005-url)) ///- iOS ([Official API - WKWebView.url](https://developer.apple.com/documentation/webkit/wkwebview/1415005-url))
///- Web
Future<Uri?> getUrl() async { Future<Uri?> getUrl() async {
Map<String, dynamic> args = <String, dynamic>{}; Map<String, dynamic> args = <String, dynamic>{};
String? url = await _channel.invokeMethod('getUrl', args); String? url = await _channel.invokeMethod('getUrl', args);

View File

@ -92,6 +92,8 @@ class InAppWebViewWebElement {
call.arguments["settings"].cast<String, dynamic>()); call.arguments["settings"].cast<String, dynamic>());
setSettings(newSettings); setSettings(newSettings);
break; break;
case "getUrl":
return getUrl();
case "dispose": case "dispose":
dispose(); dispose();
break; break;
@ -217,6 +219,14 @@ class InAppWebViewWebElement {
_callMethod("stopLoading"); _callMethod("stopLoading");
} }
Future<String?> getUrl() async {
String? url = _callMethod("getUrl");
if (url == null || url.isEmpty || url == 'about:blank') {
url = iframe.src;
}
return url;
}
Set<Sandbox> getSandbox() { Set<Sandbox> getSandbox() {
var sandbox = iframe.sandbox; var sandbox = iframe.sandbox;
Set<Sandbox> values = Set(); Set<Sandbox> values = Set();

View File

@ -10,12 +10,14 @@ environment:
dependencies: dependencies:
flutter: flutter:
sdk: flutter sdk: flutter
flutter_web_plugins:
sdk: flutter
js: ^0.6.3 js: ^0.6.3
dev_dependencies: dev_dependencies:
flutter_test: flutter_test:
sdk: flutter sdk: flutter
flutter_web_plugins: flutter_driver:
sdk: flutter sdk: flutter
pedantic: ^1.11.1 pedantic: ^1.11.1