Updated tests
This commit is contained in:
parent
2dd1999a63
commit
55a2a41a04
|
@ -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');
|
|
@ -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<InAppWebViewController>();
|
||||
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);
|
||||
}
|
|
@ -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<InAppWebViewController>();
|
||||
final Completer<void> pageLoaded = Completer<void>();
|
||||
|
||||
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<dynamic>?, ["bar", 5]), true);
|
||||
expect(
|
||||
mapEquals(result[3]?.cast<String, String>(), {"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<InAppWebViewController>();
|
||||
final Completer<void> pageLoaded = Completer<void>();
|
||||
|
||||
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<InAppWebViewController>();
|
||||
final Completer<void> pageLoaded = Completer<void>();
|
||||
|
||||
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<InAppWebViewController>();
|
||||
final Completer<void> pageLoaded = Completer<void>();
|
||||
|
||||
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);
|
||||
}
|
|
@ -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<InAppWebViewController>();
|
||||
final StreamController<String> pageLoads =
|
||||
StreamController<String>.broadcast();
|
||||
|
||||
await tester.pumpWidget(
|
||||
Directionality(
|
||||
textDirection: TextDirection.ltr,
|
||||
child: InAppWebView(
|
||||
key: GlobalKey(),
|
||||
initialUrlRequest:
|
||||
URLRequest(url: TEST_CROSS_PLATFORM_URL_1),
|
||||
onWebViewCreated: (controller) {
|
||||
controllerCompleter.complete(controller);
|
||||
},
|
||||
onLoadStop: (controller, url) {
|
||||
pageLoads.add(url!.toString());
|
||||
},
|
||||
),
|
||||
),
|
||||
);
|
||||
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<InAppWebViewController>();
|
||||
final StreamController<String> pageStarts =
|
||||
StreamController<String>.broadcast();
|
||||
final StreamController<String> pageLoads =
|
||||
StreamController<String>.broadcast();
|
||||
await tester.pumpWidget(
|
||||
Directionality(
|
||||
textDirection: TextDirection.ltr,
|
||||
child: InAppWebView(
|
||||
key: GlobalKey(),
|
||||
initialUrlRequest:
|
||||
URLRequest(url: TEST_URL_1),
|
||||
onWebViewCreated: (controller) {
|
||||
controllerCompleter.complete(controller);
|
||||
},
|
||||
initialSettings: InAppWebViewSettings(
|
||||
javaScriptEnabled: true
|
||||
),
|
||||
onLoadStart: (controller, url) {
|
||||
pageStarts.add(url!.toString());
|
||||
},
|
||||
onLoadStop: (controller, url) {
|
||||
pageLoads.add(url!.toString());
|
||||
},
|
||||
),
|
||||
),
|
||||
);
|
||||
final InAppWebViewController controller =
|
||||
await controllerCompleter.future;
|
||||
final Map<String, String> headers = <String, String>{
|
||||
'test_header': 'flutter_test_header'
|
||||
};
|
||||
await controller.loadUrl(
|
||||
urlRequest: URLRequest(
|
||||
url: Uri.parse('https://flutter-header-echo.herokuapp.com/'),
|
||||
headers: headers));
|
||||
final String? currentUrl = (await controller.getUrl())?.toString();
|
||||
expect(currentUrl, 'https://flutter-header-echo.herokuapp.com/');
|
||||
|
||||
await pageStarts.stream.firstWhere((String url) => url == currentUrl);
|
||||
await pageLoads.stream.firstWhere((String url) => url == currentUrl);
|
||||
|
||||
final String? content = await controller.evaluateJavascript(
|
||||
source: 'document.documentElement.innerText');
|
||||
expect(content!.contains('flutter_test_header'), isTrue);
|
||||
|
||||
pageStarts.close();
|
||||
pageLoads.close();
|
||||
}, skip: shouldSkipTest2);
|
||||
}, skip: shouldSkip);
|
||||
}
|
|
@ -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();
|
||||
});
|
||||
}
|
|
@ -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<InAppWebViewController>();
|
||||
final Completer<void> pageLoaded = Completer<void>();
|
||||
|
@ -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);
|
||||
}
|
|
@ -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<String, dynamic> toJson() {
|
||||
return {'bar': this.bar, 'baz': this.baz};
|
||||
}
|
||||
}
|
||||
|
||||
class MyInAppBrowser extends InAppBrowser {
|
||||
final Completer<void> browserCreated = Completer<void>();
|
||||
final Completer<void> firstPageLoaded = Completer<void>();
|
||||
|
||||
MyInAppBrowser(
|
||||
{int? windowId, UnmodifiableListView<UserScript>? 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<void> browserCreated = Completer<void>();
|
||||
final Completer<void> firstPageLoaded = Completer<void>();
|
||||
final Completer<void> browserClosed = Completer<void>();
|
||||
|
||||
@override
|
||||
void onOpened() {
|
||||
browserCreated.complete();
|
||||
}
|
||||
|
||||
@override
|
||||
void onCompletedInitialLoad() {
|
||||
firstPageLoaded.complete();
|
||||
}
|
||||
|
||||
@override
|
||||
void onClosed() {
|
||||
browserClosed.complete();
|
||||
}
|
||||
}
|
|
@ -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<String, dynamic> toJson() {
|
||||
return {'bar': this.bar, 'baz': this.baz};
|
||||
}
|
||||
}
|
||||
|
||||
class MyInAppBrowser extends InAppBrowser {
|
||||
final Completer<void> browserCreated = Completer<void>();
|
||||
final Completer<void> firstPageLoaded = Completer<void>();
|
||||
|
||||
MyInAppBrowser(
|
||||
{int? windowId, UnmodifiableListView<UserScript>? 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<void> browserCreated = Completer<void>();
|
||||
final Completer<void> firstPageLoaded = Completer<void>();
|
||||
final Completer<void> browserClosed = Completer<void>();
|
||||
|
||||
@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<InAppWebViewController>();
|
||||
final Completer<void> pageLoaded = Completer<void>();
|
||||
|
||||
await tester.pumpWidget(
|
||||
Directionality(
|
||||
textDirection: TextDirection.ltr,
|
||||
child: InAppWebView(
|
||||
key: GlobalKey(),
|
||||
initialUrlRequest: URLRequest(url: Uri.parse('about:blank')),
|
||||
onWebViewCreated: (controller) {
|
||||
controllerCompleter.complete(controller);
|
||||
},
|
||||
onLoadStop: (controller, url) {
|
||||
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<dynamic>?, ["bar", 5]), true);
|
||||
expect(
|
||||
mapEquals(result[3]?.cast<String, String>(), {"foo": "baz"}), true);
|
||||
});
|
||||
|
||||
testWidgets('evaluateJavascript with content world',
|
||||
(WidgetTester tester) async {
|
||||
final Completer controllerCompleter =
|
||||
Completer<InAppWebViewController>();
|
||||
final Completer<void> pageLoaded = Completer<void>();
|
||||
|
||||
await tester.pumpWidget(
|
||||
Directionality(
|
||||
textDirection: TextDirection.ltr,
|
||||
child: InAppWebView(
|
||||
key: GlobalKey(),
|
||||
initialUrlRequest: URLRequest(url: Uri.parse('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<InAppWebViewController>();
|
||||
final Completer<void> pageLoaded = Completer<void>();
|
||||
|
||||
await tester.pumpWidget(
|
||||
Directionality(
|
||||
textDirection: TextDirection.ltr,
|
||||
child: InAppWebView(
|
||||
key: GlobalKey(),
|
||||
initialUrlRequest: URLRequest(url: Uri.parse('about:blank')),
|
||||
onWebViewCreated: (controller) {
|
||||
controllerCompleter.complete(controller);
|
||||
},
|
||||
onLoadStop: (controller, url) {
|
||||
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<InAppWebViewController>();
|
||||
final Completer<void> pageLoaded = Completer<void>();
|
||||
|
||||
await tester.pumpWidget(
|
||||
Directionality(
|
||||
textDirection: TextDirection.ltr,
|
||||
child: InAppWebView(
|
||||
key: GlobalKey(),
|
||||
initialUrlRequest: URLRequest(url: Uri.parse('about:blank')),
|
||||
onWebViewCreated: (controller) {
|
||||
controllerCompleter.complete(controller);
|
||||
},
|
||||
onLoadStop: (controller, url) {
|
||||
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<InAppWebViewController>();
|
||||
final StreamController<String> pageLoads =
|
||||
StreamController<String>.broadcast();
|
||||
|
||||
await tester.pumpWidget(
|
||||
Directionality(
|
||||
textDirection: TextDirection.ltr,
|
||||
child: InAppWebView(
|
||||
key: GlobalKey(),
|
||||
initialUrlRequest:
|
||||
URLRequest(url: Uri.parse('https://github.com/flutter')),
|
||||
onWebViewCreated: (controller) {
|
||||
controllerCompleter.complete(controller);
|
||||
},
|
||||
onLoadStop: (controller, url) {
|
||||
pageLoads.add(url!.toString());
|
||||
},
|
||||
),
|
||||
),
|
||||
);
|
||||
final InAppWebViewController controller =
|
||||
await controllerCompleter.future;
|
||||
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<InAppWebViewController>();
|
||||
final StreamController<String> pageStarts =
|
||||
StreamController<String>.broadcast();
|
||||
final StreamController<String> pageLoads =
|
||||
StreamController<String>.broadcast();
|
||||
await tester.pumpWidget(
|
||||
Directionality(
|
||||
textDirection: TextDirection.ltr,
|
||||
child: InAppWebView(
|
||||
key: GlobalKey(),
|
||||
initialUrlRequest:
|
||||
URLRequest(url: 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<String, String> headers = <String, String>{
|
||||
'test_header': 'flutter_test_header'
|
||||
};
|
||||
await controller.loadUrl(
|
||||
urlRequest: URLRequest(
|
||||
url: Uri.parse('https://flutter-header-echo.herokuapp.com/'),
|
||||
headers: headers));
|
||||
final String? currentUrl = (await controller.getUrl())?.toString();
|
||||
expect(currentUrl, 'https://flutter-header-echo.herokuapp.com/');
|
||||
|
||||
await pageStarts.stream.firstWhere((String url) => url == currentUrl);
|
||||
await pageLoads.stream.firstWhere((String url) => url == currentUrl);
|
||||
|
||||
final String? content = await controller.evaluateJavascript(
|
||||
source: 'document.documentElement.innerText');
|
||||
expect(content!.contains('flutter_test_header'), isTrue);
|
||||
|
||||
pageStarts.close();
|
||||
pageLoads.close();
|
||||
});
|
||||
|
||||
// group("iOS loadFileURL", () {
|
||||
// late Directory appSupportDir;
|
||||
|
|
|
@ -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"
|
||||
|
|
|
@ -115,10 +115,10 @@ class _InAppWebViewExampleScreenState extends State<InAppWebViewExampleScreen> {
|
|||
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<UserScript>([]),
|
||||
initialSettings: settings,
|
||||
|
@ -169,7 +169,6 @@ class _InAppWebViewExampleScreenState extends State<InAppWebViewExampleScreen> {
|
|||
this.url = url.toString();
|
||||
urlController.text = this.url;
|
||||
});
|
||||
|
||||
},
|
||||
onLoadError: (controller, url, code, message) {
|
||||
pullToRefreshController?.endRefreshing();
|
||||
|
|
|
@ -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);
|
||||
|
||||
|
|
Loading…
Reference in New Issue