Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[camera_avfoundation] Migrate tests to Swift - part 1 #8603

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
f476931
Add bridging header file
FirentisTFW Feb 7, 2025
b0decfb
Migrate CameraPreviewPauseTests to Swift
FirentisTFW Feb 7, 2025
75dca24
Migrate CameraPropertiesTests to Swift, format code
FirentisTFW Feb 10, 2025
4a98112
Migrate QueueUtilsTests to Swift
FirentisTFW Feb 10, 2025
2a4168c
Migrate CameraExposureTests to Swift
FirentisTFW Feb 10, 2025
164095e
Migrate CameraFocusTests and AvailableCamerasTest to Swift
FirentisTFW Feb 10, 2025
ef0c0c0
Migrate CameraPermissionTests to Swift
FirentisTFW Feb 10, 2025
aab1d5e
Format file
FirentisTFW Feb 11, 2025
06a7bd6
Bump version and update changelog
FirentisTFW Feb 11, 2025
cb937d6
Format Swift files
FirentisTFW Feb 11, 2025
8d10061
Handle catching Objective-C errors in Swift tests
FirentisTFW Feb 11, 2025
a314c5b
Fix expectation timeout after rebase
FirentisTFW Feb 11, 2025
6d4bf8d
Remove redundant comments
FirentisTFW Feb 11, 2025
fc34006
Add more context to changelog entry
FirentisTFW Feb 11, 2025
2f7c9fd
Refactor code according to style guide
FirentisTFW Feb 11, 2025
8701be9
Start changelog message with a verb
FirentisTFW Feb 11, 2025
85ef5a1
Make method private
FirentisTFW Feb 12, 2025
5ba3781
Compare CGPoints directly
FirentisTFW Feb 12, 2025
cacdf4c
Make helper class fileprivate
FirentisTFW Feb 12, 2025
7c589d1
Do not use setUp for a file with only two tests
FirentisTFW Feb 12, 2025
605b4df
Remove shared state from tests
FirentisTFW Feb 12, 2025
1453630
Remove redundant empty lines
FirentisTFW Feb 12, 2025
4f9cbdd
Add a todo to use errors when migrating to Swift
FirentisTFW Feb 12, 2025
a10c4ac
Format files
FirentisTFW Feb 12, 2025
20682dc
Add more information to a todo
FirentisTFW Feb 12, 2025
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions packages/camera/camera_avfoundation/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 0.9.18+5

* Migrates unit tests to Swift.

## 0.9.18+4

* Refactors implementations to reduce usage of OCMock in internal testing.
Expand Down

Large diffs are not rendered by default.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
// Copyright 2013 The Flutter Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

import AVFoundation
import XCTest

@testable import camera_avfoundation

final class AvailableCamerasTest: XCTestCase {
private func createCameraPlugin(with deviceDiscoverer: MockCameraDeviceDiscoverer) -> CameraPlugin
{
return CameraPlugin(
registry: MockFlutterTextureRegistry(),
messenger: MockFlutterBinaryMessenger(),
globalAPI: MockGlobalEventApi(),
deviceDiscoverer: deviceDiscoverer,
deviceFactory: { _ in MockCaptureDevice() },
captureSessionFactory: { MockCaptureSession() },
captureDeviceInputFactory: MockCaptureDeviceInputFactory()
)
}

func testAvailableCamerasShouldReturnAllCamerasOnMultiCameraIPhone() {
let mockDeviceDiscoverer = MockCameraDeviceDiscoverer()
let cameraPlugin = createCameraPlugin(with: mockDeviceDiscoverer)
let expectation = self.expectation(description: "Result finished")

// iPhone 13 Cameras:
let wideAngleCamera = MockCaptureDevice()
wideAngleCamera.uniqueID = "0"
wideAngleCamera.position = .back

let frontFacingCamera = MockCaptureDevice()
frontFacingCamera.uniqueID = "1"
frontFacingCamera.position = .front

let ultraWideCamera = MockCaptureDevice()
ultraWideCamera.uniqueID = "2"
ultraWideCamera.position = .back

let telephotoCamera = MockCaptureDevice()
telephotoCamera.uniqueID = "3"
telephotoCamera.position = .back

var requiredTypes: [AVCaptureDevice.DeviceType] = [
.builtInWideAngleCamera, .builtInTelephotoCamera,
]
if #available(iOS 13.0, *) {
requiredTypes.append(.builtInUltraWideCamera)
}
var cameras: [MockCaptureDevice] = [wideAngleCamera, frontFacingCamera, telephotoCamera]
if #available(iOS 13.0, *) {
cameras.append(ultraWideCamera)
}

mockDeviceDiscoverer.discoverySessionStub = { deviceTypes, mediaType, position in
XCTAssertEqual(deviceTypes, requiredTypes)
XCTAssertEqual(mediaType, .video)
XCTAssertEqual(position, .unspecified)
return cameras
}

var resultValue: [FCPPlatformCameraDescription]?
cameraPlugin.availableCameras { result, error in
XCTAssertNil(error)
resultValue = result
expectation.fulfill()
}
waitForExpectations(timeout: 30, handler: nil)

// Verify the result.
if #available(iOS 13.0, *) {
XCTAssertEqual(resultValue?.count, 4)
} else {
XCTAssertEqual(resultValue?.count, 3)
}
}

func testAvailableCamerasShouldReturnOneCameraOnSingleCameraIPhone() {
let mockDeviceDiscoverer = MockCameraDeviceDiscoverer()
let cameraPlugin = createCameraPlugin(with: mockDeviceDiscoverer)
let expectation = self.expectation(description: "Result finished")

// iPhone 8 Cameras:
let wideAngleCamera = MockCaptureDevice()
wideAngleCamera.uniqueID = "0"
wideAngleCamera.position = .back

let frontFacingCamera = MockCaptureDevice()
frontFacingCamera.uniqueID = "1"
frontFacingCamera.position = .front

var requiredTypes: [AVCaptureDevice.DeviceType] = [
.builtInWideAngleCamera, .builtInTelephotoCamera,
]
if #available(iOS 13.0, *) {
requiredTypes.append(.builtInUltraWideCamera)
}
let cameras: [MockCaptureDevice] = [wideAngleCamera, frontFacingCamera]

mockDeviceDiscoverer.discoverySessionStub = { deviceTypes, mediaType, position in
XCTAssertEqual(deviceTypes, requiredTypes)
XCTAssertEqual(mediaType, .video)
XCTAssertEqual(position, .unspecified)
return cameras
}

var resultValue: [FCPPlatformCameraDescription]?
cameraPlugin.availableCameras { result, error in
XCTAssertNil(error)
resultValue = result
expectation.fulfill()
}
waitForExpectations(timeout: 30, handler: nil)

// Verify the result.
XCTAssertEqual(resultValue?.count, 2)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
// Copyright 2013 The Flutter Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

import AVFoundation
import XCTest

@testable import camera_avfoundation

final class CameraExposureTests: XCTestCase {
private func createSutAndMocks() -> (FLTCam, MockCaptureDevice, MockDeviceOrientationProvider) {
let mockDevice = MockCaptureDevice()
let mockDeviceOrientationProvider = MockDeviceOrientationProvider()

let configuration = FLTCreateTestCameraConfiguration()
configuration.captureDeviceFactory = { mockDevice }
configuration.deviceOrientationProvider = mockDeviceOrientationProvider
let camera = FLTCreateCamWithConfiguration(configuration)

return (camera, mockDevice, mockDeviceOrientationProvider)
}

func testSetExposurePointWithResult_SetsExposurePointOfInterest() {
let (camera, mockDevice, mockDeviceOrientationProvider) = createSutAndMocks()
// UI is currently in landscape left orientation.
mockDeviceOrientationProvider.orientation = .landscapeLeft
// Exposure point of interest is supported.
mockDevice.exposurePointOfInterestSupported = true

// Verify the focus point of interest has been set.
var setPoint = CGPoint.zero
mockDevice.setExposurePointOfInterestStub = { point in
if point == CGPoint(x: 1, y: 1) {
setPoint = point
}
}

let completionExpectation = expectation(description: "Completion called")
camera.setExposurePoint(FCPPlatformPoint.makeWith(x: 1, y: 1)) { error in
XCTAssertNil(error)
completionExpectation.fulfill()
}

waitForExpectations(timeout: 30, handler: nil)
XCTAssertEqual(setPoint, CGPoint(x: 1.0, y: 1.0))
}

func testSetExposurePoint_WhenNotSupported_ReturnsError() {
let (camera, mockDevice, mockDeviceOrientationProvider) = createSutAndMocks()
// UI is currently in landscape left orientation.
mockDeviceOrientationProvider.orientation = .landscapeLeft
// Exposure point of interest is not supported.
mockDevice.exposurePointOfInterestSupported = false

let expectation = self.expectation(description: "Completion with error")

camera.setExposurePoint(FCPPlatformPoint.makeWith(x: 1, y: 1)) { error in
XCTAssertNotNil(error)
XCTAssertEqual(error?.code, "setExposurePointFailed")
XCTAssertEqual(error?.message, "Device does not have exposure point capabilities")
expectation.fulfill()
}

waitForExpectations(timeout: 30, handler: nil)
}
}
Loading