2022-04-28 18:34:54 +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';
|
|
|
|
|
|
|
|
import '../constants.dart';
|
|
|
|
|
|
|
|
void onNavigationResponse() {
|
2022-04-29 11:39:28 +00:00
|
|
|
final shouldSkip = kIsWeb
|
|
|
|
? true
|
|
|
|
: ![
|
|
|
|
TargetPlatform.iOS,
|
|
|
|
TargetPlatform.macOS,
|
|
|
|
].contains(defaultTargetPlatform);
|
2022-04-28 18:34:54 +00:00
|
|
|
|
|
|
|
group("onNavigationResponse", () {
|
|
|
|
testWidgets('allow navigation', (WidgetTester tester) async {
|
2022-10-14 00:28:23 +00:00
|
|
|
final Completer<InAppWebViewController> controllerCompleter =
|
|
|
|
Completer<InAppWebViewController>();
|
2022-04-28 18:34:54 +00:00
|
|
|
final Completer<void> pageLoaded = Completer<void>();
|
|
|
|
final Completer<String> onNavigationResponseCompleter =
|
|
|
|
Completer<String>();
|
|
|
|
|
|
|
|
await tester.pumpWidget(
|
|
|
|
Directionality(
|
|
|
|
textDirection: TextDirection.ltr,
|
|
|
|
child: InAppWebView(
|
|
|
|
key: GlobalKey(),
|
2022-04-29 11:39:28 +00:00
|
|
|
initialUrlRequest: URLRequest(url: TEST_URL_1),
|
2022-04-28 18:34:54 +00:00
|
|
|
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 18:34:54 +00:00
|
|
|
onWebViewCreated: (controller) {
|
|
|
|
controllerCompleter.complete(controller);
|
|
|
|
},
|
|
|
|
onLoadStop: (controller, url) {
|
|
|
|
pageLoaded.complete();
|
|
|
|
},
|
|
|
|
onNavigationResponse: (controller, navigationResponse) async {
|
|
|
|
onNavigationResponseCompleter
|
|
|
|
.complete(navigationResponse.response!.url.toString());
|
|
|
|
return NavigationResponseAction.ALLOW;
|
|
|
|
},
|
|
|
|
),
|
|
|
|
),
|
|
|
|
);
|
|
|
|
|
|
|
|
await pageLoaded.future;
|
|
|
|
final String url = await onNavigationResponseCompleter.future;
|
|
|
|
expect(url, TEST_URL_1.toString());
|
|
|
|
});
|
|
|
|
|
|
|
|
testWidgets('cancel navigation', (WidgetTester tester) async {
|
2022-10-14 00:28:23 +00:00
|
|
|
final Completer<InAppWebViewController> controllerCompleter =
|
|
|
|
Completer<InAppWebViewController>();
|
2022-04-28 18:34:54 +00:00
|
|
|
final Completer<void> pageLoaded = Completer<void>();
|
|
|
|
final Completer<String> onNavigationResponseCompleter =
|
|
|
|
Completer<String>();
|
|
|
|
|
|
|
|
await tester.pumpWidget(
|
|
|
|
Directionality(
|
|
|
|
textDirection: TextDirection.ltr,
|
|
|
|
child: InAppWebView(
|
|
|
|
key: GlobalKey(),
|
2022-04-29 11:39:28 +00:00
|
|
|
initialUrlRequest: URLRequest(url: TEST_URL_1),
|
2022-04-28 18:34:54 +00:00
|
|
|
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 18:34:54 +00:00
|
|
|
onWebViewCreated: (controller) {
|
|
|
|
controllerCompleter.complete(controller);
|
|
|
|
},
|
|
|
|
onLoadStop: (controller, url) {
|
|
|
|
pageLoaded.complete();
|
|
|
|
},
|
|
|
|
onNavigationResponse: (controller, navigationResponse) async {
|
|
|
|
onNavigationResponseCompleter
|
|
|
|
.complete(navigationResponse.response!.url.toString());
|
|
|
|
return NavigationResponseAction.CANCEL;
|
|
|
|
},
|
|
|
|
),
|
|
|
|
),
|
|
|
|
);
|
|
|
|
|
|
|
|
final String url = await onNavigationResponseCompleter.future;
|
|
|
|
expect(url, TEST_URL_1.toString());
|
|
|
|
expect(pageLoaded.future, doesNotComplete);
|
|
|
|
});
|
|
|
|
}, skip: shouldSkip);
|
|
|
|
}
|