88#endif
99@import AVFoundation;
1010@import XCTest;
11- # import < OCMock/OCMock.h >
11+
1212#import " CameraTestUtils.h"
13+ #import " MockCaptureDevice.h"
14+ #import " MockCapturePhotoOutput.h"
1315
1416// / Includes test cases related to photo capture operations for FLTCam class.
1517@interface FLTCamPhotoCaptureTests : XCTestCase
@@ -27,22 +29,19 @@ - (void)testCaptureToFile_mustReportErrorToResultIfSavePhotoDelegateCompletionsW
2729 dispatch_queue_set_specific (captureSessionQueue, FLTCaptureSessionQueueSpecific,
2830 (void *)FLTCaptureSessionQueueSpecific, NULL );
2931 FLTCam *cam = FLTCreateCamWithCaptureSessionQueue (captureSessionQueue);
30- AVCapturePhotoSettings *settings = [AVCapturePhotoSettings photoSettings ];
31- id mockSettings = OCMClassMock ([AVCapturePhotoSettings class ]);
32- OCMStub ([mockSettings photoSettings ]).andReturn (settings);
3332
3433 NSError *error = [NSError errorWithDomain: @" test" code: 0 userInfo: nil ];
3534
36- id mockOutput = OCMClassMock ([AVCapturePhotoOutput class ]) ;
37- OCMStub ([ mockOutput capturePhotoWithSettings: OCMOCK_ANY delegate: OCMOCK_ANY])
38- . andDo (^( NSInvocation *invocation ) {
35+ MockCapturePhotoOutput * mockOutput = [[MockCapturePhotoOutput alloc ] init ] ;
36+ mockOutput. capturePhotoWithSettingsStub =
37+ ^(AVCapturePhotoSettings *settings, NSObject <AVCapturePhotoCaptureDelegate> *photoDelegate ) {
3938 FLTSavePhotoDelegate *delegate = cam.inProgressSavePhotoDelegates [@(settings.uniqueID)];
4039 // Completion runs on IO queue.
4140 dispatch_queue_t ioQueue = dispatch_queue_create (" io_queue" , NULL );
4241 dispatch_async (ioQueue, ^{
4342 delegate.completionHandler (nil , error);
4443 });
45- }) ;
44+ };
4645 cam.capturePhotoOutput = mockOutput;
4746
4847 // `FLTCam::captureToFile` runs on capture session queue.
@@ -67,22 +66,18 @@ - (void)testCaptureToFile_mustReportPathToResultIfSavePhotoDelegateCompletionsWi
6766 (void *)FLTCaptureSessionQueueSpecific, NULL );
6867 FLTCam *cam = FLTCreateCamWithCaptureSessionQueue (captureSessionQueue);
6968
70- AVCapturePhotoSettings *settings = [AVCapturePhotoSettings photoSettings ];
71- id mockSettings = OCMClassMock ([AVCapturePhotoSettings class ]);
72- OCMStub ([mockSettings photoSettings ]).andReturn (settings);
73-
7469 NSString *filePath = @" test" ;
7570
76- id mockOutput = OCMClassMock ([AVCapturePhotoOutput class ]) ;
77- OCMStub ([ mockOutput capturePhotoWithSettings: OCMOCK_ANY delegate: OCMOCK_ANY])
78- . andDo (^( NSInvocation *invocation ) {
71+ MockCapturePhotoOutput * mockOutput = [[MockCapturePhotoOutput alloc ] init ] ;
72+ mockOutput. capturePhotoWithSettingsStub =
73+ ^(AVCapturePhotoSettings *settings, NSObject <AVCapturePhotoCaptureDelegate> *photoDelegate ) {
7974 FLTSavePhotoDelegate *delegate = cam.inProgressSavePhotoDelegates [@(settings.uniqueID)];
8075 // Completion runs on IO queue.
8176 dispatch_queue_t ioQueue = dispatch_queue_create (" io_queue" , NULL );
8277 dispatch_async (ioQueue, ^{
8378 delegate.completionHandler (filePath, nil );
8479 });
85- }) ;
80+ };
8681 cam.capturePhotoOutput = mockOutput;
8782
8883 // `FLTCam::captureToFile` runs on capture session queue.
@@ -105,27 +100,19 @@ - (void)testCaptureToFile_mustReportFileExtensionWithHeifWhenHEVCIsAvailableAndF
105100 FLTCam *cam = FLTCreateCamWithCaptureSessionQueue (captureSessionQueue);
106101 [cam setImageFileFormat: FCPPlatformImageFileFormatHeif];
107102
108- AVCapturePhotoSettings *settings =
109- [AVCapturePhotoSettings photoSettingsWithFormat: @{AVVideoCodecKey : AVVideoCodecTypeHEVC}];
110-
111- id mockSettings = OCMClassMock ([AVCapturePhotoSettings class ]);
112- OCMStub ([mockSettings photoSettingsWithFormat: OCMOCK_ANY]).andReturn (settings);
113-
114- id mockOutput = OCMClassMock ([AVCapturePhotoOutput class ]);
115- // Set availablePhotoCodecTypes to HEVC
116- NSArray *codecTypes = @[ AVVideoCodecTypeHEVC ];
117- OCMStub ([mockOutput availablePhotoCodecTypes ]).andReturn (codecTypes);
118-
119- OCMStub ([mockOutput capturePhotoWithSettings: OCMOCK_ANY delegate: OCMOCK_ANY])
120- .andDo (^(NSInvocation *invocation) {
103+ MockCapturePhotoOutput *mockOutput = [[MockCapturePhotoOutput alloc ] init ];
104+ mockOutput.availablePhotoCodecTypes = @[ AVVideoCodecTypeHEVC ];
105+ mockOutput.capturePhotoWithSettingsStub =
106+ ^(AVCapturePhotoSettings *settings, NSObject <AVCapturePhotoCaptureDelegate> *photoDelegate) {
121107 FLTSavePhotoDelegate *delegate = cam.inProgressSavePhotoDelegates [@(settings.uniqueID)];
122108 // Completion runs on IO queue.
123109 dispatch_queue_t ioQueue = dispatch_queue_create (" io_queue" , NULL );
124110 dispatch_async (ioQueue, ^{
125111 delegate.completionHandler (delegate.filePath , nil );
126112 });
127- }) ;
113+ };
128114 cam.capturePhotoOutput = mockOutput;
115+
129116 // `FLTCam::captureToFile` runs on capture session queue.
130117 dispatch_async (captureSessionQueue, ^{
131118 [cam captureToFileWithCompletion: ^(NSString *filePath, FlutterError *error) {
@@ -146,22 +133,18 @@ - (void)testCaptureToFile_mustReportFileExtensionWithJpgWhenHEVCNotAvailableAndF
146133 FLTCam *cam = FLTCreateCamWithCaptureSessionQueue (captureSessionQueue);
147134 [cam setImageFileFormat: FCPPlatformImageFileFormatHeif];
148135
149- AVCapturePhotoSettings *settings = [AVCapturePhotoSettings photoSettings ];
150- id mockSettings = OCMClassMock ([AVCapturePhotoSettings class ]);
151- OCMStub ([mockSettings photoSettings ]).andReturn (settings);
152-
153- id mockOutput = OCMClassMock ([AVCapturePhotoOutput class ]);
154-
155- OCMStub ([mockOutput capturePhotoWithSettings: OCMOCK_ANY delegate: OCMOCK_ANY])
156- .andDo (^(NSInvocation *invocation) {
136+ MockCapturePhotoOutput *mockOutput = [[MockCapturePhotoOutput alloc ] init ];
137+ mockOutput.capturePhotoWithSettingsStub =
138+ ^(AVCapturePhotoSettings *settings, NSObject <AVCapturePhotoCaptureDelegate> *photoDelegate) {
157139 FLTSavePhotoDelegate *delegate = cam.inProgressSavePhotoDelegates [@(settings.uniqueID)];
158140 // Completion runs on IO queue.
159141 dispatch_queue_t ioQueue = dispatch_queue_create (" io_queue" , NULL );
160142 dispatch_async (ioQueue, ^{
161143 delegate.completionHandler (delegate.filePath , nil );
162144 });
163- }) ;
145+ };
164146 cam.capturePhotoOutput = mockOutput;
147+
165148 // `FLTCam::captureToFile` runs on capture session queue.
166149 dispatch_async (captureSessionQueue, ^{
167150 [cam captureToFileWithCompletion: ^(NSString *filePath, FlutterError *error) {
@@ -176,12 +159,18 @@ - (void)testCaptureToFile_handlesTorchMode {
176159 XCTestExpectation *pathExpectation =
177160 [self expectationWithDescription:
178161 @" Must send file path to result if save photo delegate completes with file path." ];
179-
180- id captureDeviceMock = OCMProtocolMock (@protocol (FLTCaptureDevice));
181- OCMStub ([captureDeviceMock hasTorch ]).andReturn (YES );
182- OCMStub ([captureDeviceMock isTorchAvailable ]).andReturn (YES );
183- OCMStub ([captureDeviceMock torchMode ]).andReturn (AVCaptureTorchModeAuto);
184- OCMExpect ([captureDeviceMock setTorchMode: AVCaptureTorchModeOn]);
162+ XCTestExpectation *setTorchExpectation =
163+ [self expectationWithDescription: @" Should set torch mode to AVCaptureTorchModeOn." ];
164+
165+ MockCaptureDevice *captureDeviceMock = [[MockCaptureDevice alloc ] init ];
166+ captureDeviceMock.hasTorch = YES ;
167+ captureDeviceMock.isTorchAvailable = YES ;
168+ captureDeviceMock.torchMode = AVCaptureTorchModeAuto;
169+ captureDeviceMock.setTorchModeStub = ^(AVCaptureTorchMode mode) {
170+ if (mode == AVCaptureTorchModeOn) {
171+ [setTorchExpectation fulfill ];
172+ }
173+ };
185174
186175 dispatch_queue_t captureSessionQueue = dispatch_queue_create (" capture_session_queue" , NULL );
187176 dispatch_queue_set_specific (captureSessionQueue, FLTCaptureSessionQueueSpecific,
@@ -194,22 +183,18 @@ - (void)testCaptureToFile_handlesTorchMode {
194183 },
195184 nil );
196185
197- AVCapturePhotoSettings *settings = [AVCapturePhotoSettings photoSettings ];
198- id mockSettings = OCMClassMock ([AVCapturePhotoSettings class ]);
199- OCMStub ([mockSettings photoSettings ]).andReturn (settings);
200-
201186 NSString *filePath = @" test" ;
202187
203- id mockOutput = OCMClassMock ([AVCapturePhotoOutput class ]) ;
204- OCMStub ([ mockOutput capturePhotoWithSettings: OCMOCK_ANY delegate: OCMOCK_ANY])
205- . andDo (^( NSInvocation *invocation ) {
188+ MockCapturePhotoOutput * mockOutput = [[MockCapturePhotoOutput alloc ] init ] ;
189+ mockOutput. capturePhotoWithSettingsStub =
190+ ^(AVCapturePhotoSettings *settings, NSObject <AVCapturePhotoCaptureDelegate> *photoDelegate ) {
206191 FLTSavePhotoDelegate *delegate = cam.inProgressSavePhotoDelegates [@(settings.uniqueID)];
207192 // Completion runs on IO queue.
208193 dispatch_queue_t ioQueue = dispatch_queue_create (" io_queue" , NULL );
209194 dispatch_async (ioQueue, ^{
210195 delegate.completionHandler (filePath, nil );
211196 });
212- }) ;
197+ };
213198 cam.capturePhotoOutput = mockOutput;
214199
215200 // `FLTCam::captureToFile` runs on capture session queue.
@@ -223,6 +208,5 @@ - (void)testCaptureToFile_handlesTorchMode {
223208 }];
224209 });
225210 [self waitForExpectationsWithTimeout: 1 handler: nil ];
226- OCMVerifyAll (captureDeviceMock);
227211}
228212@end
0 commit comments