|
| 1 | +// Copyright 2013 The Flutter Authors. All rights reserved. |
| 2 | +// Use of this source code is governed by a BSD-style license that can be |
| 3 | +// found in the LICENSE file. |
| 4 | + |
| 5 | +@import camera; |
| 6 | +@import camera.Test; |
| 7 | +@import AVFoundation; |
| 8 | +@import XCTest; |
| 9 | +#import <OCMock/OCMock.h> |
| 10 | +#import "CameraTestUtils.h" |
| 11 | + |
| 12 | +@interface CameraPermissionTests : XCTestCase |
| 13 | + |
| 14 | +@end |
| 15 | + |
| 16 | +@implementation CameraPermissionTests |
| 17 | + |
| 18 | +- (void)testRequestCameraPermission_completeWithoutErrorIfPrevoiuslyAuthorized { |
| 19 | + XCTestExpectation *expectation = |
| 20 | + [self expectationWithDescription: |
| 21 | + @"Must copmlete without error if camera access was previously authorized."]; |
| 22 | + |
| 23 | + id mockDevice = OCMClassMock([AVCaptureDevice class]); |
| 24 | + OCMStub([mockDevice authorizationStatusForMediaType:AVMediaTypeVideo]) |
| 25 | + .andReturn(AVAuthorizationStatusAuthorized); |
| 26 | + |
| 27 | + FLTRequestCameraPermissionWithCompletionHandler(^(FlutterError *error) { |
| 28 | + if (error == nil) { |
| 29 | + [expectation fulfill]; |
| 30 | + } |
| 31 | + }); |
| 32 | + [self waitForExpectationsWithTimeout:1 handler:nil]; |
| 33 | +} |
| 34 | +- (void)testRequestCameraPermission_completeWithErrorIfPreviouslyDenied { |
| 35 | + XCTestExpectation *expectation = |
| 36 | + [self expectationWithDescription: |
| 37 | + @"Must complete with error if camera access was previously denied."]; |
| 38 | + FlutterError *expectedError = |
| 39 | + [FlutterError errorWithCode:@"CameraAccessDeniedWithoutPrompt" |
| 40 | + message:@"User has previously denied the camera access request. Go to " |
| 41 | + @"Settings to enable camera access." |
| 42 | + details:nil]; |
| 43 | + |
| 44 | + id mockDevice = OCMClassMock([AVCaptureDevice class]); |
| 45 | + OCMStub([mockDevice authorizationStatusForMediaType:AVMediaTypeVideo]) |
| 46 | + .andReturn(AVAuthorizationStatusDenied); |
| 47 | + FLTRequestCameraPermissionWithCompletionHandler(^(FlutterError *error) { |
| 48 | + if ([error isEqual:expectedError]) { |
| 49 | + [expectation fulfill]; |
| 50 | + } |
| 51 | + }); |
| 52 | + [self waitForExpectationsWithTimeout:1 handler:nil]; |
| 53 | +} |
| 54 | + |
| 55 | +- (void)testRequestCameraPermission_completeWithErrorIfRestricted { |
| 56 | + XCTestExpectation *expectation = |
| 57 | + [self expectationWithDescription:@"Must complete with error if camera access is restricted."]; |
| 58 | + FlutterError *expectedError = [FlutterError errorWithCode:@"CameraAccessRestricted" |
| 59 | + message:@"Camera access is restricted. " |
| 60 | + details:nil]; |
| 61 | + |
| 62 | + id mockDevice = OCMClassMock([AVCaptureDevice class]); |
| 63 | + OCMStub([mockDevice authorizationStatusForMediaType:AVMediaTypeVideo]) |
| 64 | + .andReturn(AVAuthorizationStatusRestricted); |
| 65 | + |
| 66 | + FLTRequestCameraPermissionWithCompletionHandler(^(FlutterError *error) { |
| 67 | + if ([error isEqual:expectedError]) { |
| 68 | + [expectation fulfill]; |
| 69 | + } |
| 70 | + }); |
| 71 | + [self waitForExpectationsWithTimeout:1 handler:nil]; |
| 72 | +} |
| 73 | + |
| 74 | +- (void)testRequestCameraPermission_completeWithoutErrorIfUserGrantAccess { |
| 75 | + XCTestExpectation *grantedExpectation = [self |
| 76 | + expectationWithDescription:@"Must complete without error if user choose to grant access"]; |
| 77 | + |
| 78 | + id mockDevice = OCMClassMock([AVCaptureDevice class]); |
| 79 | + OCMStub([mockDevice authorizationStatusForMediaType:AVMediaTypeVideo]) |
| 80 | + .andReturn(AVAuthorizationStatusNotDetermined); |
| 81 | + // Mimic user choosing "allow" in permission dialog. |
| 82 | + OCMStub([mockDevice requestAccessForMediaType:AVMediaTypeVideo |
| 83 | + completionHandler:[OCMArg checkWithBlock:^BOOL(void (^block)(BOOL)) { |
| 84 | + block(YES); |
| 85 | + return YES; |
| 86 | + }]]); |
| 87 | + |
| 88 | + FLTRequestCameraPermissionWithCompletionHandler(^(FlutterError *error) { |
| 89 | + if (error == nil) { |
| 90 | + [grantedExpectation fulfill]; |
| 91 | + } |
| 92 | + }); |
| 93 | + [self waitForExpectationsWithTimeout:1 handler:nil]; |
| 94 | +} |
| 95 | + |
| 96 | +- (void)testRequestCameraPermission_completeWithErrorIfUserDenyAccess { |
| 97 | + XCTestExpectation *expectation = |
| 98 | + [self expectationWithDescription:@"Must complete with error if user choose to deny access"]; |
| 99 | + FlutterError *expectedError = |
| 100 | + [FlutterError errorWithCode:@"CameraAccessDenied" |
| 101 | + message:@"User denied the camera access request." |
| 102 | + details:nil]; |
| 103 | + |
| 104 | + id mockDevice = OCMClassMock([AVCaptureDevice class]); |
| 105 | + OCMStub([mockDevice authorizationStatusForMediaType:AVMediaTypeVideo]) |
| 106 | + .andReturn(AVAuthorizationStatusNotDetermined); |
| 107 | + |
| 108 | + // Mimic user choosing "deny" in permission dialog. |
| 109 | + OCMStub([mockDevice requestAccessForMediaType:AVMediaTypeVideo |
| 110 | + completionHandler:[OCMArg checkWithBlock:^BOOL(void (^block)(BOOL)) { |
| 111 | + block(NO); |
| 112 | + return YES; |
| 113 | + }]]); |
| 114 | + FLTRequestCameraPermissionWithCompletionHandler(^(FlutterError *error) { |
| 115 | + if ([error isEqual:expectedError]) { |
| 116 | + [expectation fulfill]; |
| 117 | + } |
| 118 | + }); |
| 119 | + |
| 120 | + [self waitForExpectationsWithTimeout:1 handler:nil]; |
| 121 | +} |
| 122 | + |
| 123 | +@end |
0 commit comments