iosWebViewFix/example/integration_test/in_app_webview/on_permission_request.dart

119 lines
4.2 KiB
Dart
Raw Normal View History

part of 'main.dart';
2022-04-28 17:29:01 +00:00
void onPermissionRequest() {
2022-04-29 11:39:28 +00:00
final shouldSkip = kIsWeb
? true
: ![
TargetPlatform.android,
TargetPlatform.iOS,
TargetPlatform.macOS,
].contains(defaultTargetPlatform);
2022-04-28 17:29:01 +00:00
final expectedValue = [PermissionResourceType.CAMERA];
2022-04-28 17:29:01 +00:00
skippableTestWidgets('onPermissionRequest', (WidgetTester tester) async {
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<List<PermissionResourceType>> onPermissionRequestCompleter =
2022-04-29 11:39:28 +00:00
Completer<List<PermissionResourceType>>();
2022-04-28 17:29:01 +00:00
await tester.pumpWidget(
Directionality(
textDirection: TextDirection.ltr,
child: InAppWebView(
key: GlobalKey(),
initialUrlRequest: URLRequest(url: TEST_PERMISSION_SITE),
onWebViewCreated: (controller) {
controllerCompleter.complete(controller);
},
onLoadStop: (controller, url) {
pageLoaded.complete();
},
onPermissionRequest: (controller, permissionRequest) async {
2022-04-28 17:29:01 +00:00
onPermissionRequestCompleter.complete(permissionRequest.resources);
2022-04-29 11:39:28 +00:00
return PermissionResponse(
resources: permissionRequest.resources,
2022-04-29 00:07:01 +00:00
action: PermissionResponseAction.GRANT);
2022-04-28 17:29:01 +00:00
},
),
),
);
final InAppWebViewController controller = await controllerCompleter.future;
await pageLoaded.future;
await controller.evaluateJavascript(
source: "document.querySelector('#camera').click();");
2022-04-29 00:07:01 +00:00
await tester.pump();
2022-04-28 17:29:01 +00:00
final List<PermissionResourceType> resources =
2022-04-29 11:39:28 +00:00
await onPermissionRequestCompleter.future;
2022-04-28 17:29:01 +00:00
expect(listEquals(resources, expectedValue), true);
}, skip: shouldSkip);
2022-11-02 14:08:09 +00:00
// final shouldSkip2 = kIsWeb
// ? true
// : ![
// TargetPlatform.android,
// ].contains(defaultTargetPlatform);
// TODO: this test is not working
final shouldSkip2 = true;
skippableTestWidgets('onPermissionRequestCanceled',
(WidgetTester tester) async {
final Completer<InAppWebViewController> controllerCompleter =
Completer<InAppWebViewController>();
final Completer<void> pageLoaded = Completer<void>();
final Completer<List<PermissionResourceType>> onPermissionRequestCompleter =
Completer<List<PermissionResourceType>>();
final Completer<List<PermissionResourceType>>
onPermissionRequestCancelCompleter =
Completer<List<PermissionResourceType>>();
await tester.pumpWidget(
Directionality(
textDirection: TextDirection.ltr,
child: InAppWebView(
key: GlobalKey(),
initialUrlRequest: URLRequest(url: TEST_PERMISSION_SITE),
onWebViewCreated: (controller) {
controllerCompleter.complete(controller);
},
onLoadStop: (controller, url) {
if (pageLoaded.isCompleted) {
pageLoaded.complete();
}
},
onPermissionRequest: (controller, permissionRequest) async {
onPermissionRequestCompleter.complete(permissionRequest.resources);
2022-11-02 14:08:09 +00:00
await Future.delayed(Duration(seconds: 5));
return PermissionResponse(
resources: permissionRequest.resources,
action: PermissionResponseAction.GRANT);
},
onPermissionRequestCanceled: (controller, permissionRequest) {
onPermissionRequestCancelCompleter
.complete(permissionRequest.resources);
},
),
),
);
final InAppWebViewController controller = await controllerCompleter.future;
await pageLoaded.future;
await controller.evaluateJavascript(
source: "document.querySelector('#camera').click();");
await tester.pump();
final List<PermissionResourceType> resources =
await onPermissionRequestCompleter.future;
expect(listEquals(resources, expectedValue), true);
controller.reload();
final List<PermissionResourceType> canceledResources =
2022-11-02 14:08:44 +00:00
await onPermissionRequestCancelCompleter.future;
expect(listEquals(canceledResources, expectedValue), true);
}, skip: shouldSkip2);
2022-04-28 17:29:01 +00:00
}