2022-04-28 17:29:01 +00:00
|
|
|
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';
|
|
|
|
|
|
|
|
void shouldInterceptRequest() {
|
2022-04-29 11:39:28 +00:00
|
|
|
final shouldSkip = kIsWeb
|
|
|
|
? true
|
|
|
|
: ![
|
|
|
|
TargetPlatform.android,
|
|
|
|
].contains(defaultTargetPlatform);
|
2022-04-28 17:29:01 +00:00
|
|
|
|
|
|
|
testWidgets('shouldInterceptRequest', (WidgetTester tester) async {
|
|
|
|
List<String> resourceList = [
|
|
|
|
"https://getbootstrap.com/docs/4.3/dist/css/bootstrap.min.css",
|
|
|
|
"https://code.jquery.com/jquery-3.3.1.min.js",
|
|
|
|
"https://via.placeholder.com/100x50"
|
|
|
|
];
|
|
|
|
List<String> resourceLoaded = [];
|
|
|
|
|
2022-10-14 00:28:23 +00:00
|
|
|
final Completer<InAppWebViewController> controllerCompleter =
|
|
|
|
Completer<InAppWebViewController>();
|
2022-04-28 17:29:01 +00:00
|
|
|
final Completer<void> pageLoaded = Completer<void>();
|
|
|
|
final Completer<void> loadedResourceCompleter = Completer<void>();
|
|
|
|
|
|
|
|
await tester.pumpWidget(
|
|
|
|
Directionality(
|
|
|
|
textDirection: TextDirection.ltr,
|
|
|
|
child: InAppWebView(
|
|
|
|
key: GlobalKey(),
|
|
|
|
initialData: InAppWebViewInitialData(data: """
|
|
|
|
<!doctype html>
|
|
|
|
<html lang="en">
|
|
|
|
<head>
|
|
|
|
<meta charset="UTF-8">
|
|
|
|
<meta name="viewport" content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
|
|
|
|
<meta http-equiv="X-UA-Compatible" content="ie=edge">
|
|
|
|
<link rel="stylesheet" href="https://getbootstrap.com/docs/4.3/dist/css/bootstrap.min.css">
|
|
|
|
<script src="https://code.jquery.com/jquery-3.3.1.min.js"></script>
|
|
|
|
</head>
|
|
|
|
<body>
|
|
|
|
<img src="https://via.placeholder.com/100x50" alt="placeholder 100x50">
|
|
|
|
</body>
|
|
|
|
</html>
|
|
|
|
"""),
|
|
|
|
initialSettings:
|
All PrintJobSettings properties are optionals, All PullToRefreshSettings properties are optionals, All WebAuthenticationSessionSettings properties are optionals, Automatically infer useShouldOverrideUrlLoading, useOnLoadResource, useOnDownloadStart, useShouldInterceptAjaxRequest, useShouldInterceptFetchRequest, useShouldInterceptRequest, useOnRenderProcessGone, useOnNavigationResponse settings if their value is null and the corresponding event is implemented by the WebView (InAppWebView and HeadlessInAppWebView, not InAppBrowser) before it's native initialization
2022-11-22 21:40:45 +00:00
|
|
|
InAppWebViewSettings(),
|
2022-04-28 17:29:01 +00:00
|
|
|
onWebViewCreated: (controller) {
|
|
|
|
controllerCompleter.complete(controller);
|
|
|
|
},
|
|
|
|
onLoadStop: (controller, url) {
|
|
|
|
pageLoaded.complete();
|
|
|
|
},
|
|
|
|
shouldInterceptRequest: (controller, request) async {
|
|
|
|
resourceLoaded.add(request.url.toString());
|
|
|
|
if (resourceLoaded.length == resourceList.length) {
|
|
|
|
loadedResourceCompleter.complete();
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
},
|
|
|
|
),
|
|
|
|
),
|
|
|
|
);
|
|
|
|
|
|
|
|
await pageLoaded.future;
|
|
|
|
await loadedResourceCompleter.future;
|
|
|
|
expect(resourceLoaded, containsAll(resourceList));
|
|
|
|
}, skip: shouldSkip);
|
|
|
|
}
|