Skip to content

Commit

Permalink
Fix nits
Browse files Browse the repository at this point in the history
  • Loading branch information
mchudy committed Feb 6, 2025
1 parent 40cce59 commit fc0e826
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,49 +20,52 @@ @interface CameraCaptureSessionQueueRaceConditionTests : XCTestCase

@implementation CameraCaptureSessionQueueRaceConditionTests

- (void)testFixForCaptureSessionQueueNullPointerCrashDueToRaceCondition {
- (CameraPlugin *)createCameraPlugin {
MockCaptureDevice *captureDevice = [[MockCaptureDevice alloc] init];

CameraPlugin *camera =
[[CameraPlugin alloc] initWithRegistry:[[MockFlutterTextureRegistry alloc] init]
messenger:[[MockFlutterBinaryMessenger alloc] init]
globalAPI:[[MockGlobalEventApi alloc] init]
deviceDiscoverer:[[MockCameraDeviceDiscoverer alloc] init]
deviceFactory:^NSObject<FLTCaptureDevice> *(NSString *name) {
return captureDevice;
}
captureSessionFactory:^NSObject<FLTCaptureSession> * {
return [[MockCaptureSession alloc] init];
}
captureDeviceInputFactory:[[MockCaptureDeviceInputFactory alloc] init]];
return [[CameraPlugin alloc] initWithRegistry:[[MockFlutterTextureRegistry alloc] init]
messenger:[[MockFlutterBinaryMessenger alloc] init]
globalAPI:[[MockGlobalEventApi alloc] init]
deviceDiscoverer:[[MockCameraDeviceDiscoverer alloc] init]
deviceFactory:^NSObject<FLTCaptureDevice> *(NSString *name) {
return captureDevice;
}
captureSessionFactory:^NSObject<FLTCaptureSession> * {
return [[MockCaptureSession alloc] init];
}
captureDeviceInputFactory:[[MockCaptureDeviceInputFactory alloc] init]];
}

- (void)testFixForCaptureSessionQueueNullPointerCrashDueToRaceCondition {
CameraPlugin *cameraPlugin = [self createCameraPlugin];

XCTestExpectation *disposeExpectation =
[self expectationWithDescription:@"dispose's result block must be called"];
XCTestExpectation *createExpectation =
[self expectationWithDescription:@"create's result block must be called"];
// Mimic a dispose call followed by a create call, which can be triggered by slightly dragging the
// home bar, causing the app to be inactive, and immediately regain active.
[camera disposeCamera:0
completion:^(FlutterError *_Nullable error) {
[disposeExpectation fulfill];
}];
[camera createCameraOnSessionQueueWithName:@"acamera"
settings:[FCPPlatformMediaSettings
makeWithResolutionPreset:
FCPPlatformResolutionPresetMedium
framesPerSecond:nil
videoBitrate:nil
audioBitrate:nil
enableAudio:YES]
completion:^(NSNumber *_Nullable result,
FlutterError *_Nullable error) {
[createExpectation fulfill];
}];
[cameraPlugin disposeCamera:0
completion:^(FlutterError *_Nullable error) {
[disposeExpectation fulfill];
}];
[cameraPlugin createCameraOnSessionQueueWithName:@"acamera"
settings:[FCPPlatformMediaSettings
makeWithResolutionPreset:
FCPPlatformResolutionPresetMedium
framesPerSecond:nil
videoBitrate:nil
audioBitrate:nil
enableAudio:YES]
completion:^(NSNumber *_Nullable result,
FlutterError *_Nullable error) {
[createExpectation fulfill];
}];
[self waitForExpectationsWithTimeout:30 handler:nil];
// `captureSessionQueue` must not be nil after `create` call. Otherwise a nil
// `captureSessionQueue` passed into `AVCaptureVideoDataOutput::setSampleBufferDelegate:queue:`
// API will cause a crash.
XCTAssertNotNil(camera.captureSessionQueue,
XCTAssertNotNil(cameraPlugin.captureSessionQueue,
@"captureSessionQueue must not be nil after create method. ");
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ @implementation FLTDefaultCameraDeviceDiscoverer
NSArray<AVCaptureDevice *> *devices = discoverySession.devices;
NSMutableArray<NSObject<FLTCaptureDevice> *> *deviceControllers =
[NSMutableArray arrayWithCapacity:devices.count];

for (AVCaptureDevice *device in devices) {
[deviceControllers addObject:[[FLTDefaultCaptureDevice alloc] initWithDevice:device]];
}
Expand Down

0 comments on commit fc0e826

Please sign in to comment.