Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
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
Next Next commit
Add tests for setting camera exposure mode
  • Loading branch information
RobertOdrowaz committed Mar 14, 2025
commit cbc5559d449ad60edabfe6d5df2bed90707a30b0
5 changes: 5 additions & 0 deletions packages/camera/camera_avfoundation/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
## 0.9.18+10

* Backfills unit tests for the `FLTCam` class.
* Makes `exposureMode` property private.

## 0.9.18+9

* Backfills unit tests for `CameraPlugin` class.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
970ADAC02D6764CC00EFDCD9 /* MockEventChannel.swift in Sources */ = {isa = PBXBuildFile; fileRef = 970ADABF2D6764CC00EFDCD9 /* MockEventChannel.swift */; };
972CA92B2D5A1D8C004B846F /* CameraPropertiesTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 972CA92A2D5A1D8C004B846F /* CameraPropertiesTests.swift */; };
972CA92D2D5A28C4004B846F /* QueueUtilsTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 972CA92C2D5A28C4004B846F /* QueueUtilsTests.swift */; };
972CA9312D5A366C004B846F /* CameraExposureTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 972CA9302D5A366C004B846F /* CameraExposureTests.swift */; };
977A25202D5A439300931E34 /* AvailableCamerasTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 977A251F2D5A439300931E34 /* AvailableCamerasTests.swift */; };
977A25222D5A49EC00931E34 /* CameraFocusTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 977A25212D5A49EC00931E34 /* CameraFocusTests.swift */; };
977A25242D5A511600931E34 /* CameraPermissionTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 977A25232D5A511600931E34 /* CameraPermissionTests.swift */; };
Expand All @@ -49,6 +48,7 @@
97C147011CF9000F007C117D /* LaunchScreen.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 97C146FF1CF9000F007C117D /* LaunchScreen.storyboard */; };
97DB234D2D566D0700CEFE66 /* CameraPreviewPauseTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 97DB234C2D566D0700CEFE66 /* CameraPreviewPauseTests.swift */; };
E0CDBAC227CD9729002561D9 /* CameraTestUtils.m in Sources */ = {isa = PBXBuildFile; fileRef = E0CDBAC127CD9729002561D9 /* CameraTestUtils.m */; };
E11D6A912D82C7740031E6C5 /* FLTCamExposureTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = E11D6A902D82C7740031E6C5 /* FLTCamExposureTests.swift */; };
E12C4FF62D68C69000515E70 /* CameraPluginDelegatingMethodTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = E12C4FF52D68C69000515E70 /* CameraPluginDelegatingMethodTests.swift */; };
E12C4FF82D68E85500515E70 /* MockFLTCameraPermissionManager.swift in Sources */ = {isa = PBXBuildFile; fileRef = E12C4FF72D68E85500515E70 /* MockFLTCameraPermissionManager.swift */; };
E1A5F4E32D80259C0005BA64 /* FLTCamSetFlashModeTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = E1A5F4E22D80259C0005BA64 /* FLTCamSetFlashModeTests.swift */; };
Expand Down Expand Up @@ -116,7 +116,6 @@
970ADABF2D6764CC00EFDCD9 /* MockEventChannel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MockEventChannel.swift; sourceTree = "<group>"; };
972CA92A2D5A1D8C004B846F /* CameraPropertiesTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CameraPropertiesTests.swift; sourceTree = "<group>"; };
972CA92C2D5A28C4004B846F /* QueueUtilsTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = QueueUtilsTests.swift; sourceTree = "<group>"; };
972CA9302D5A366C004B846F /* CameraExposureTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CameraExposureTests.swift; sourceTree = "<group>"; };
9740EEB21CF90195004384FC /* Debug.xcconfig */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.xcconfig; name = Debug.xcconfig; path = Flutter/Debug.xcconfig; sourceTree = "<group>"; };
9740EEB31CF90195004384FC /* Generated.xcconfig */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.xcconfig; name = Generated.xcconfig; path = Flutter/Generated.xcconfig; sourceTree = "<group>"; };
977A251F2D5A439300931E34 /* AvailableCamerasTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AvailableCamerasTests.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -147,6 +146,7 @@
B61D98BBC8FB276D1C4A7BB2 /* Pods-RunnerTests.debug.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-RunnerTests.debug.xcconfig"; path = "Target Support Files/Pods-RunnerTests/Pods-RunnerTests.debug.xcconfig"; sourceTree = "<group>"; };
E0CDBAC027CD9729002561D9 /* CameraTestUtils.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = CameraTestUtils.h; sourceTree = "<group>"; };
E0CDBAC127CD9729002561D9 /* CameraTestUtils.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = CameraTestUtils.m; sourceTree = "<group>"; };
E11D6A902D82C7740031E6C5 /* FLTCamExposureTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = FLTCamExposureTests.swift; sourceTree = "<group>"; };
E12C4FF52D68C69000515E70 /* CameraPluginDelegatingMethodTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CameraPluginDelegatingMethodTests.swift; sourceTree = "<group>"; };
E12C4FF72D68E85500515E70 /* MockFLTCameraPermissionManager.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MockFLTCameraPermissionManager.swift; sourceTree = "<group>"; };
E1A5F4E22D80259C0005BA64 /* FLTCamSetFlashModeTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = FLTCamSetFlashModeTests.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -196,7 +196,6 @@
97DB234C2D566D0700CEFE66 /* CameraPreviewPauseTests.swift */,
972CA92A2D5A1D8C004B846F /* CameraPropertiesTests.swift */,
972CA92C2D5A28C4004B846F /* QueueUtilsTests.swift */,
972CA9302D5A366C004B846F /* CameraExposureTests.swift */,
977A251F2D5A439300931E34 /* AvailableCamerasTests.swift */,
977A25212D5A49EC00931E34 /* CameraFocusTests.swift */,
977A25232D5A511600931E34 /* CameraPermissionTests.swift */,
Expand All @@ -208,6 +207,7 @@
E12C4FF52D68C69000515E70 /* CameraPluginDelegatingMethodTests.swift */,
E1FFEAAE2D6CDA8C00B14107 /* CameraPluginCreateCameraTests.swift */,
E1FFEAB02D6CDE5B00B14107 /* CameraPluginInitializeCameraTests.swift */,
E11D6A902D82C7740031E6C5 /* FLTCamExposureTests.swift */,
E1A5F4E22D80259C0005BA64 /* FLTCamSetFlashModeTests.swift */,
);
path = RunnerTests;
Expand Down Expand Up @@ -539,6 +539,7 @@
isa = PBXSourcesBuildPhase;
buildActionMask = 2147483647;
files = (
E11D6A912D82C7740031E6C5 /* FLTCamExposureTests.swift in Sources */,
97BD4A0E2D5CC5AE00F857D5 /* CameraSettingsTests.swift in Sources */,
972CA92D2D5A28C4004B846F /* QueueUtilsTests.swift in Sources */,
E1FFEAB12D6CDE5B00B14107 /* CameraPluginInitializeCameraTests.swift in Sources */,
Expand All @@ -562,7 +563,6 @@
97DB234D2D566D0700CEFE66 /* CameraPreviewPauseTests.swift in Sources */,
970ADAC02D6764CC00EFDCD9 /* MockEventChannel.swift in Sources */,
977A25202D5A439300931E34 /* AvailableCamerasTests.swift in Sources */,
972CA9312D5A366C004B846F /* CameraExposureTests.swift in Sources */,
E1FFEAAD2D6C8DD700B14107 /* MockFLTCam.swift in Sources */,
7F29EB292D26A59000740257 /* MockCameraDeviceDiscoverer.m in Sources */,
97BD4A102D5CE13500F857D5 /* CameraSessionPresetsTests.swift in Sources */,
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
// 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 XCTest

@testable import camera_avfoundation

final class FLTCamExposureTests: XCTestCase {
private func createCamera() -> (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 testSetExposureModeLocked_setsAuthExposeMode() {
let (camera, mockDevice, _) = createCamera()

mockDevice.setExposureModeStub = { mode in
// AVCaptureExposureModeAutoExpose automatically adjusts the exposure one time, and then
// locks exposure for the device
XCTAssertEqual(mode, .autoExpose)
}

camera.setExposureMode(.locked)
}

func testSetExposureModeAuto_setsContinousAutoExposureMode_ifSupported() {
let (camera, mockDevice, _) = createCamera()

// All exposure modes are supported
mockDevice.isExposureModeSupportedStub = { _ in true }

mockDevice.setExposureModeStub = { mode in
XCTAssertEqual(mode, .continuousAutoExposure)
}

camera.setExposureMode(.auto)
}

func testSetExposureModeAuto_setsAutoExposeMode_ifContinousAutoIsNotSupported() {
let (camera, mockDevice, _) = createCamera()

// Continous auto exposure is not supported
mockDevice.isExposureModeSupportedStub = { mode in
mode != .continuousAutoExposure
}

mockDevice.setExposureModeStub = { mode in
XCTAssertEqual(mode, .autoExpose)
}

camera.setExposureMode(.auto)
}

func testSetExposurePoint_setsExposurePointOfInterest() {
let (camera, mockDevice, mockDeviceOrientationProvider) = createCamera()
// 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 expectation = expectation(description: "Completion called")
camera.setExposurePoint(FCPPlatformPoint.makeWith(x: 1, y: 1)) { error in
XCTAssertNil(error)
expectation.fulfill()
}

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

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

let expectation = 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)
}

func testSetExposureOffset_setsExposureTargetBias() {
let (camera, mockDevice, _) = createCamera()

let targetOffset = CGFloat(1.0)

var setExposureTargetBiasCalled = false
mockDevice.setExposureTargetBiasStub = { bias, handler in
XCTAssertEqual(bias, Float(targetOffset))
setExposureTargetBiasCalled = true
}

camera.setExposureOffset(targetOffset)

XCTAssertTrue(setExposureTargetBiasCalled)
}

func testMaximumExposureOffset_returnsDeviceMaxExposureTargetBias() {
let (camera, mockDevice, _) = createCamera()

let targetOffset = CGFloat(1.0)

mockDevice.maxExposureTargetBias = Float(targetOffset)

XCTAssertEqual(camera.maximumExposureOffset, targetOffset)
}

func testMinimumExposureOffset_returnsDeviceMinExposureTargetBias() {
let (camera, mockDevice, _) = createCamera()

let targetOffset = CGFloat(1.0)

mockDevice.minExposureTargetBias = Float(targetOffset)

XCTAssertEqual(camera.minimumExposureOffset, targetOffset)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ NS_ASSUME_NONNULL_BEGIN
// Exposure
@property(nonatomic, assign) BOOL exposurePointOfInterestSupported;
@property(nonatomic, assign) AVCaptureExposureMode exposureMode;
@property(nonatomic, assign) BOOL exposureModeSupported;
@property(nonatomic, copy) BOOL (^isExposureModeSupportedStub)(AVCaptureExposureMode mode);
/// Overrides the default implementation of setting exposure mode.
/// @param mode The exposure mode being set
@property(nonatomic, copy) void (^setExposureModeStub)(AVCaptureExposureMode mode);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,11 @@ - (void)setActiveVideoMaxFrameDuration:(CMTime)duration {
}

- (BOOL)isExposureModeSupported:(AVCaptureExposureMode)mode {
return self.exposureModeSupported;
if (self.isExposureModeSupportedStub) {
return self.isExposureModeSupportedStub(mode);
} else {
return NO;
}
}

@synthesize device;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
final class MockFLTCam: FLTCam {
var setOnFrameAvailableStub: ((() -> Void) -> Void)?
var setDartApiStub: ((FCPCameraEventApi) -> Void)?
var setExposureModeStub: ((FCPPlatformExposureMode) -> Void)?
var setFocusModeStub: ((FCPPlatformFocusMode) -> Void)?
var getMinimumAvailableZoomFactorStub: (() -> CGFloat)?
var getMaximumAvailableZoomFactorStub: (() -> CGFloat)?
Expand All @@ -24,6 +23,7 @@ final class MockFLTCam: FLTCam {
var lockCaptureStub: ((FCPPlatformDeviceOrientation) -> Void)?
var unlockCaptureOrientationStub: (() -> Void)?
var setFlashModeStub: ((FCPPlatformFlashMode, ((FlutterError?) -> Void)?) -> Void)?
var setExposureModeStub: ((FCPPlatformExposureMode) -> Void)?
var receivedImageStreamDataStub: (() -> Void)?
var pausePreviewStub: (() -> Void)?
var resumePreviewStub: (() -> Void)?
Expand Down Expand Up @@ -54,16 +54,6 @@ final class MockFLTCam: FLTCam {
}
}

/// The `setExposureMode` ObjC method is converted to property accessor in Swift translation
override var exposureMode: FCPPlatformExposureMode {
get {
return super.exposureMode
}
set {
setExposureModeStub?(newValue)
}
}

/// The `setFocusMode` ObjC method is converted to property accessor in Swift translation
override var focusMode: FCPPlatformFocusMode {
get {
Expand Down Expand Up @@ -159,6 +149,10 @@ final class MockFLTCam: FLTCam {
setFlashModeStub?(mode, completion)
}

override func setExposureMode(_ mode: FCPPlatformExposureMode) {
setExposureModeStub?(mode)
}

override func receivedImageStreamData() {
receivedImageStreamDataStub?()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ @interface FLTCam () <AVCaptureVideoDataOutputSampleBufferDelegate,
/// A wrapper for AVCaptureDevice creation to allow for dependency injection in tests.
@property(nonatomic, copy) CaptureDeviceFactory captureDeviceFactory;
@property(readonly, nonatomic) NSObject<FLTCaptureDeviceInputFactory> *captureDeviceInputFactory;
@property(assign, nonatomic) FCPPlatformExposureMode exposureMode;
@property(readonly, nonatomic) NSObject<FLTDeviceOrientationProviding> *deviceOrientationProvider;
@property(nonatomic, copy) AssetWriterFactory assetWriterFactory;
@property(nonatomic, copy) InputPixelBufferAdaptorFactory inputPixelBufferAdaptorFactory;
Expand Down Expand Up @@ -987,8 +988,10 @@ - (void)setExposureMode:(FCPPlatformExposureMode)mode {

- (void)applyExposureMode {
[_captureDevice lockForConfiguration:nil];
switch (_exposureMode) {
switch (self.exposureMode) {
case FCPPlatformExposureModeLocked:
// AVCaptureExposureModeAutoExpose automatically adjusts the exposure one time, and then
// locks exposure for the device
[_captureDevice setExposureMode:AVCaptureExposureModeAutoExpose];
break;
case FCPPlatformExposureModeAuto:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ NS_ASSUME_NONNULL_BEGIN
/// The API instance used to communicate with the Dart side of the plugin. Once initially set, this
/// should only ever be accessed on the main thread.
@property(nonatomic) FCPCameraEventApi *dartAPI;
@property(assign, nonatomic) FCPPlatformExposureMode exposureMode;
@property(assign, nonatomic) FCPPlatformFocusMode focusMode;
@property(assign, nonatomic) FCPPlatformFlashMode flashMode;
// Format used for video and image streaming.
Expand Down
2 changes: 1 addition & 1 deletion packages/camera/camera_avfoundation/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ name: camera_avfoundation
description: iOS implementation of the camera plugin.
repository: https://github.com/flutter/packages/tree/main/packages/camera/camera_avfoundation
issue_tracker: https://github.com/flutter/flutter/issues?q=is%3Aissue+is%3Aopen+label%3A%22p%3A+camera%22
version: 0.9.18+9
version: 0.9.18+10

environment:
sdk: ^3.4.0
Expand Down