89 lines
3.0 KiB
Dart
89 lines
3.0 KiB
Dart
|
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() {
|
||
|
final shouldSkip = kIsWeb ||
|
||
|
![
|
||
|
TargetPlatform.iOS,
|
||
|
TargetPlatform.macOS,
|
||
|
].contains(defaultTargetPlatform);
|
||
|
|
||
|
group("onNavigationResponse", () {
|
||
|
testWidgets('allow navigation', (WidgetTester tester) async {
|
||
|
final Completer controllerCompleter = Completer<InAppWebViewController>();
|
||
|
final Completer<void> pageLoaded = Completer<void>();
|
||
|
final Completer<String> onNavigationResponseCompleter =
|
||
|
Completer<String>();
|
||
|
|
||
|
await tester.pumpWidget(
|
||
|
Directionality(
|
||
|
textDirection: TextDirection.ltr,
|
||
|
child: InAppWebView(
|
||
|
key: GlobalKey(),
|
||
|
initialUrlRequest:
|
||
|
URLRequest(url: TEST_URL_1),
|
||
|
initialSettings:
|
||
|
InAppWebViewSettings(useOnNavigationResponse: true),
|
||
|
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 {
|
||
|
final Completer controllerCompleter = Completer<InAppWebViewController>();
|
||
|
final Completer<void> pageLoaded = Completer<void>();
|
||
|
final Completer<String> onNavigationResponseCompleter =
|
||
|
Completer<String>();
|
||
|
|
||
|
await tester.pumpWidget(
|
||
|
Directionality(
|
||
|
textDirection: TextDirection.ltr,
|
||
|
child: InAppWebView(
|
||
|
key: GlobalKey(),
|
||
|
initialUrlRequest:
|
||
|
URLRequest(url: TEST_URL_1),
|
||
|
initialSettings:
|
||
|
InAppWebViewSettings(useOnNavigationResponse: true),
|
||
|
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);
|
||
|
}
|