Skip to content

Commit 1d0e4e4

Browse files
FirentisTFWandroidseb
authored andcommitted
[camera_avfoundation] Migrate tests to Swift - part 2 (flutter#8613)
This PR migrates some of Objective-C tests to Swift as a part of Swift migration: [flutter/flutter/issues/119109](flutter/flutter#119109). I kept the names of test cases the same. Tests migrated in this PR: - CameraCaptureSessionQueueRaceConditionTests - CameraMethodChannelTests - CameraOrientationTests - CameraSettingsTests - CameraSessionPresetsTests
1 parent cbaa1a9 commit 1d0e4e4

19 files changed

+738
-982
lines changed

packages/camera/camera_avfoundation/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
## 0.9.18+8
2+
3+
* Migrates unit tests to Swift.
4+
15
## 0.9.18+7
26

37
* Fixes crash when setting `activeFormat` on `FLTCaptureDevice`.

packages/camera/camera_avfoundation/example/ios/Runner.xcodeproj/project.pbxproj

Lines changed: 28 additions & 23 deletions
Large diffs are not rendered by default.

packages/camera/camera_avfoundation/example/ios/RunnerTests/CameraCaptureSessionQueueRaceConditionTests.m

Lines changed: 0 additions & 72 deletions
This file was deleted.
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
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 XCTest
6+
7+
@testable import camera_avfoundation
8+
9+
final class CameraCaptureSessionQueueRaceConditionTests: XCTestCase {
10+
private func createCameraPlugin() -> CameraPlugin {
11+
return CameraPlugin(
12+
registry: MockFlutterTextureRegistry(),
13+
messenger: MockFlutterBinaryMessenger(),
14+
globalAPI: MockGlobalEventApi(),
15+
deviceDiscoverer: MockCameraDeviceDiscoverer(),
16+
deviceFactory: { _ in MockCaptureDevice() },
17+
captureSessionFactory: { MockCaptureSession() },
18+
captureDeviceInputFactory: MockCaptureDeviceInputFactory()
19+
)
20+
}
21+
22+
func testFixForCaptureSessionQueueNullPointerCrashDueToRaceCondition() {
23+
let cameraPlugin = createCameraPlugin()
24+
let disposeExpectation = expectation(description: "dispose's result block must be called")
25+
let createExpectation = expectation(description: "create's result block must be called")
26+
27+
// Mimic a dispose call followed by a create call, which can be triggered by slightly dragging the
28+
// home bar, causing the app to be inactive, and immediately regain active.
29+
cameraPlugin.disposeCamera(0) { error in
30+
disposeExpectation.fulfill()
31+
}
32+
33+
cameraPlugin.createCameraOnSessionQueue(
34+
withName: "acamera",
35+
settings: FCPPlatformMediaSettings.make(
36+
with: .medium,
37+
framesPerSecond: nil,
38+
videoBitrate: nil,
39+
audioBitrate: nil,
40+
enableAudio: true
41+
)
42+
) { result, error in
43+
createExpectation.fulfill()
44+
}
45+
46+
waitForExpectations(timeout: 30, handler: nil)
47+
48+
// `captureSessionQueue` must not be nil after `create` call. Otherwise a nil
49+
// `captureSessionQueue` passed into `AVCaptureVideoDataOutput::setSampleBufferDelegate:queue:`
50+
// API will cause a crash.
51+
XCTAssertNotNil(
52+
cameraPlugin.captureSessionQueue, "captureSessionQueue must not be nil after create method.")
53+
}
54+
}

packages/camera/camera_avfoundation/example/ios/RunnerTests/CameraExposureTests.m

Lines changed: 0 additions & 81 deletions
This file was deleted.

packages/camera/camera_avfoundation/example/ios/RunnerTests/CameraMethodChannelTests.m

Lines changed: 0 additions & 100 deletions
This file was deleted.
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
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 AVFoundation
6+
import XCTest
7+
8+
@testable import camera_avfoundation
9+
10+
final class CameraMethodChannelTests: XCTestCase {
11+
private func createCameraPlugin(with session: MockCaptureSession) -> CameraPlugin {
12+
return CameraPlugin(
13+
registry: MockFlutterTextureRegistry(),
14+
messenger: MockFlutterBinaryMessenger(),
15+
globalAPI: MockGlobalEventApi(),
16+
deviceDiscoverer: MockCameraDeviceDiscoverer(),
17+
deviceFactory: { _ in MockCaptureDevice() },
18+
captureSessionFactory: { session },
19+
captureDeviceInputFactory: MockCaptureDeviceInputFactory()
20+
)
21+
}
22+
23+
func testCreate_ShouldCallResultOnMainThread() {
24+
let avCaptureSessionMock = MockCaptureSession()
25+
avCaptureSessionMock.canSetSessionPreset = true
26+
let camera = createCameraPlugin(with: avCaptureSessionMock)
27+
let expectation = self.expectation(description: "Result finished")
28+
29+
var resultValue: NSNumber?
30+
camera.createCameraOnSessionQueue(
31+
withName: "acamera",
32+
settings: FCPPlatformMediaSettings.make(
33+
with: FCPPlatformResolutionPreset.medium,
34+
framesPerSecond: nil,
35+
videoBitrate: nil,
36+
audioBitrate: nil,
37+
enableAudio: true
38+
)
39+
) { result, error in
40+
resultValue = result
41+
expectation.fulfill()
42+
}
43+
44+
waitForExpectations(timeout: 30, handler: nil)
45+
XCTAssertNotNil(resultValue)
46+
}
47+
48+
func testDisposeShouldDeallocCamera() {
49+
let avCaptureSessionMock = MockCaptureSession()
50+
avCaptureSessionMock.canSetSessionPreset = true
51+
let camera = createCameraPlugin(with: avCaptureSessionMock)
52+
let createExpectation = self.expectation(description: "create's result block must be called")
53+
54+
camera.createCameraOnSessionQueue(
55+
withName: "acamera",
56+
settings: FCPPlatformMediaSettings.make(
57+
with: .medium,
58+
framesPerSecond: nil,
59+
videoBitrate: nil,
60+
audioBitrate: nil,
61+
enableAudio: true
62+
)
63+
) { result, error in
64+
createExpectation.fulfill()
65+
}
66+
67+
waitForExpectations(timeout: 30, handler: nil)
68+
XCTAssertNotNil(camera.camera)
69+
70+
let disposeExpectation = self.expectation(description: "dispose's result block must be called")
71+
camera.disposeCamera(0) { error in
72+
disposeExpectation.fulfill()
73+
}
74+
75+
waitForExpectations(timeout: 30, handler: nil)
76+
XCTAssertNil(camera.camera, "camera should be deallocated after dispose")
77+
}
78+
}

0 commit comments

Comments
 (0)