|
| 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 | + |
| 11 | +@interface FLTCamPhotoCaptureTests : XCTestCase |
| 12 | + |
| 13 | +@end |
| 14 | + |
| 15 | +@implementation FLTCamPhotoCaptureTests |
| 16 | + |
| 17 | +- (void)testCaptureToFile_savePhotoDelegateReferencesMustBeAccessedOnCaptureSessionQueue { |
| 18 | + XCTestExpectation *setReferenceExpectation = |
| 19 | + [self expectationWithDescription: |
| 20 | + @"SavePhotoDelegate references must be set on capture session queue."]; |
| 21 | + XCTestExpectation *clearReferenceExpectation = |
| 22 | + [self expectationWithDescription: |
| 23 | + @"SavePhotoDelegate references must be cleared on capture session queue."]; |
| 24 | + |
| 25 | + dispatch_queue_t captureSessionQueue = dispatch_queue_create("capture_session_queue", NULL); |
| 26 | + const char *captureSessionQueueSpecific = "capture_session_queue"; |
| 27 | + dispatch_queue_set_specific(captureSessionQueue, captureSessionQueueSpecific, |
| 28 | + (void *)captureSessionQueueSpecific, NULL); |
| 29 | + FLTCam *cam = [self createFLTCamWithCaptureSessionQueue:captureSessionQueue]; |
| 30 | + |
| 31 | + // settings.uniqueID is used as the key for `inProgressSavePhotoDelegates` dictionary |
| 32 | + AVCapturePhotoSettings *settings = [AVCapturePhotoSettings photoSettings]; |
| 33 | + id mockSettings = OCMClassMock([AVCapturePhotoSettings class]); |
| 34 | + OCMStub([mockSettings photoSettings]).andReturn(settings); |
| 35 | + |
| 36 | + // We need to make sure the delegate reference is actually saved in a real dictionary, so that we |
| 37 | + // can call its completion handler later. Must use a partial mock, in order to forward invocation |
| 38 | + // to the real object. |
| 39 | + id mockDelegates = OCMPartialMock([NSMutableDictionary dictionary]); |
| 40 | + OCMStub([mockDelegates setObject:OCMOCK_ANY forKeyedSubscript:OCMOCK_ANY]) |
| 41 | + .andDo(^(NSInvocation *invocation) { |
| 42 | + if (dispatch_get_specific(captureSessionQueueSpecific)) { |
| 43 | + FLTSavePhotoDelegate *delegate; |
| 44 | + // Index 0 and 1 are `self` and `_cmd`. |
| 45 | + [invocation getArgument:&delegate atIndex:2]; |
| 46 | + if (delegate) { |
| 47 | + [setReferenceExpectation fulfill]; |
| 48 | + } else { |
| 49 | + [clearReferenceExpectation fulfill]; |
| 50 | + } |
| 51 | + } |
| 52 | + }) |
| 53 | + .andForwardToRealObject(); |
| 54 | + cam.inProgressSavePhotoDelegates = mockDelegates; |
| 55 | + |
| 56 | + id mockOutput = OCMClassMock([AVCapturePhotoOutput class]); |
| 57 | + OCMStub([mockOutput capturePhotoWithSettings:OCMOCK_ANY delegate:OCMOCK_ANY]) |
| 58 | + .andDo(^(NSInvocation *invocation) { |
| 59 | + FLTSavePhotoDelegate *delegate = cam.inProgressSavePhotoDelegates[@(settings.uniqueID)]; |
| 60 | + XCTAssertNotNil(delegate, @"Delegate reference must be saved to the dictionary."); |
| 61 | + // Completion runs on IO queue. |
| 62 | + dispatch_queue_t ioQueue = dispatch_queue_create("io_queue", NULL); |
| 63 | + dispatch_async(ioQueue, ^{ |
| 64 | + NSString *filePath = @"test"; |
| 65 | + delegate.completionHandler(nil, filePath); |
| 66 | + }); |
| 67 | + }); |
| 68 | + cam.capturePhotoOutput = mockOutput; |
| 69 | + |
| 70 | + // `FLTCam::captureToFile` runs on capture session queue. |
| 71 | + dispatch_async(captureSessionQueue, ^{ |
| 72 | + [cam captureToFile:OCMClassMock([FLTThreadSafeFlutterResult class])]; |
| 73 | + }); |
| 74 | + |
| 75 | + [self waitForExpectationsWithTimeout:1 handler:nil]; |
| 76 | +} |
| 77 | + |
| 78 | +- (void)testCaptureToFile_mustReportErrorToResultIfSavePhotoDelegateCompletionsWithError { |
| 79 | + XCTestExpectation *errorExpectation = |
| 80 | + [self expectationWithDescription: |
| 81 | + @"Must send error to result if save photo delegate completes with error."]; |
| 82 | + |
| 83 | + dispatch_queue_t captureSessionQueue = dispatch_queue_create("capture_session_queue", NULL); |
| 84 | + FLTCam *cam = [self createFLTCamWithCaptureSessionQueue:captureSessionQueue]; |
| 85 | + |
| 86 | + AVCapturePhotoSettings *settings = [AVCapturePhotoSettings photoSettings]; |
| 87 | + id mockSettings = OCMClassMock([AVCapturePhotoSettings class]); |
| 88 | + OCMStub([mockSettings photoSettings]).andReturn(settings); |
| 89 | + |
| 90 | + NSError *error = [NSError errorWithDomain:@"test" code:0 userInfo:nil]; |
| 91 | + id mockResult = OCMClassMock([FLTThreadSafeFlutterResult class]); |
| 92 | + OCMStub([mockResult sendError:error]).andDo(^(NSInvocation *invocation) { |
| 93 | + [errorExpectation fulfill]; |
| 94 | + }); |
| 95 | + |
| 96 | + id mockOutput = OCMClassMock([AVCapturePhotoOutput class]); |
| 97 | + OCMStub([mockOutput capturePhotoWithSettings:OCMOCK_ANY delegate:OCMOCK_ANY]) |
| 98 | + .andDo(^(NSInvocation *invocation) { |
| 99 | + FLTSavePhotoDelegate *delegate = cam.inProgressSavePhotoDelegates[@(settings.uniqueID)]; |
| 100 | + // Completion runs on IO queue. |
| 101 | + dispatch_queue_t ioQueue = dispatch_queue_create("io_queue", NULL); |
| 102 | + dispatch_async(ioQueue, ^{ |
| 103 | + delegate.completionHandler(error, nil); |
| 104 | + }); |
| 105 | + }); |
| 106 | + cam.capturePhotoOutput = mockOutput; |
| 107 | + |
| 108 | + // `FLTCam::captureToFile` runs on capture session queue. |
| 109 | + dispatch_async(captureSessionQueue, ^{ |
| 110 | + [cam captureToFile:mockResult]; |
| 111 | + }); |
| 112 | + |
| 113 | + [self waitForExpectationsWithTimeout:1 handler:nil]; |
| 114 | +} |
| 115 | + |
| 116 | +- (void)testCaptureToFile_mustReportPathToResultIfSavePhotoDelegateCompletionsWithPath { |
| 117 | + XCTestExpectation *pathExpectation = |
| 118 | + [self expectationWithDescription: |
| 119 | + @"Must send file path to result if save photo delegate completes with file path."]; |
| 120 | + |
| 121 | + dispatch_queue_t captureSessionQueue = dispatch_queue_create("capture_session_queue", NULL); |
| 122 | + FLTCam *cam = [self createFLTCamWithCaptureSessionQueue:captureSessionQueue]; |
| 123 | + |
| 124 | + AVCapturePhotoSettings *settings = [AVCapturePhotoSettings photoSettings]; |
| 125 | + id mockSettings = OCMClassMock([AVCapturePhotoSettings class]); |
| 126 | + OCMStub([mockSettings photoSettings]).andReturn(settings); |
| 127 | + |
| 128 | + NSString *filePath = @"test"; |
| 129 | + id mockResult = OCMClassMock([FLTThreadSafeFlutterResult class]); |
| 130 | + OCMStub([mockResult sendSuccessWithData:filePath]).andDo(^(NSInvocation *invocation) { |
| 131 | + [pathExpectation fulfill]; |
| 132 | + }); |
| 133 | + |
| 134 | + id mockOutput = OCMClassMock([AVCapturePhotoOutput class]); |
| 135 | + OCMStub([mockOutput capturePhotoWithSettings:OCMOCK_ANY delegate:OCMOCK_ANY]) |
| 136 | + .andDo(^(NSInvocation *invocation) { |
| 137 | + FLTSavePhotoDelegate *delegate = cam.inProgressSavePhotoDelegates[@(settings.uniqueID)]; |
| 138 | + // Completion runs on IO queue. |
| 139 | + dispatch_queue_t ioQueue = dispatch_queue_create("io_queue", NULL); |
| 140 | + dispatch_async(ioQueue, ^{ |
| 141 | + delegate.completionHandler(nil, filePath); |
| 142 | + }); |
| 143 | + }); |
| 144 | + cam.capturePhotoOutput = mockOutput; |
| 145 | + |
| 146 | + // `FLTCam::captureToFile` runs on capture session queue. |
| 147 | + dispatch_async(captureSessionQueue, ^{ |
| 148 | + [cam captureToFile:mockResult]; |
| 149 | + }); |
| 150 | + [self waitForExpectationsWithTimeout:1 handler:nil]; |
| 151 | +} |
| 152 | + |
| 153 | +/// Creates an `FLTCam` that runs its operations on a given capture session queue. |
| 154 | +- (FLTCam *)createFLTCamWithCaptureSessionQueue:(dispatch_queue_t)captureSessionQueue { |
| 155 | + id inputMock = OCMClassMock([AVCaptureDeviceInput class]); |
| 156 | + OCMStub([inputMock deviceInputWithDevice:[OCMArg any] error:[OCMArg setTo:nil]]) |
| 157 | + .andReturn(inputMock); |
| 158 | + |
| 159 | + id sessionMock = OCMClassMock([AVCaptureSession class]); |
| 160 | + OCMStub([sessionMock alloc]).andReturn(sessionMock); |
| 161 | + OCMStub([sessionMock addInputWithNoConnections:[OCMArg any]]); // no-op |
| 162 | + OCMStub([sessionMock canSetSessionPreset:[OCMArg any]]).andReturn(YES); |
| 163 | + |
| 164 | + return [[FLTCam alloc] initWithCameraName:@"camera" |
| 165 | + resolutionPreset:@"medium" |
| 166 | + enableAudio:true |
| 167 | + orientation:UIDeviceOrientationPortrait |
| 168 | + captureSessionQueue:captureSessionQueue |
| 169 | + error:nil]; |
| 170 | +} |
| 171 | + |
| 172 | +@end |
0 commit comments