Skip to content

Commit 341d896

Browse files
committed
Add tests for setting camera zoom level
1 parent b535abd commit 341d896

File tree

2 files changed

+106
-0
lines changed

2 files changed

+106
-0
lines changed

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@
5151
E11D6A912D82C7740031E6C5 /* FLTCamExposureTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = E11D6A902D82C7740031E6C5 /* FLTCamExposureTests.swift */; };
5252
E12C4FF62D68C69000515E70 /* CameraPluginDelegatingMethodTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = E12C4FF52D68C69000515E70 /* CameraPluginDelegatingMethodTests.swift */; };
5353
E12C4FF82D68E85500515E70 /* MockFLTCameraPermissionManager.swift in Sources */ = {isa = PBXBuildFile; fileRef = E12C4FF72D68E85500515E70 /* MockFLTCameraPermissionManager.swift */; };
54+
E16602952D8471C0003CFE12 /* FLTCamZoomTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = E16602942D8471C0003CFE12 /* FLTCamZoomTests.swift */; };
5455
E1A5F4E32D80259C0005BA64 /* FLTCamSetFlashModeTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = E1A5F4E22D80259C0005BA64 /* FLTCamSetFlashModeTests.swift */; };
5556
E1FFEAAD2D6C8DD700B14107 /* MockFLTCam.swift in Sources */ = {isa = PBXBuildFile; fileRef = E1FFEAAC2D6C8DD700B14107 /* MockFLTCam.swift */; };
5657
E1FFEAAF2D6CDA8C00B14107 /* CameraPluginCreateCameraTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = E1FFEAAE2D6CDA8C00B14107 /* CameraPluginCreateCameraTests.swift */; };
@@ -149,6 +150,7 @@
149150
E11D6A902D82C7740031E6C5 /* FLTCamExposureTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = FLTCamExposureTests.swift; sourceTree = "<group>"; };
150151
E12C4FF52D68C69000515E70 /* CameraPluginDelegatingMethodTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CameraPluginDelegatingMethodTests.swift; sourceTree = "<group>"; };
151152
E12C4FF72D68E85500515E70 /* MockFLTCameraPermissionManager.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MockFLTCameraPermissionManager.swift; sourceTree = "<group>"; };
153+
E16602942D8471C0003CFE12 /* FLTCamZoomTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = FLTCamZoomTests.swift; sourceTree = "<group>"; };
152154
E1A5F4E22D80259C0005BA64 /* FLTCamSetFlashModeTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = FLTCamSetFlashModeTests.swift; sourceTree = "<group>"; };
153155
E1FFEAAC2D6C8DD700B14107 /* MockFLTCam.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MockFLTCam.swift; sourceTree = "<group>"; };
154156
E1FFEAAE2D6CDA8C00B14107 /* CameraPluginCreateCameraTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CameraPluginCreateCameraTests.swift; sourceTree = "<group>"; };
@@ -209,6 +211,7 @@
209211
E11D6A902D82C7740031E6C5 /* FLTCamExposureTests.swift */,
210212
977A25212D5A49EC00931E34 /* FLTCamFocusTests.swift */,
211213
E1A5F4E22D80259C0005BA64 /* FLTCamSetFlashModeTests.swift */,
214+
E16602942D8471C0003CFE12 /* FLTCamZoomTests.swift */,
212215
);
213216
path = RunnerTests;
214217
sourceTree = "<group>";
@@ -565,6 +568,7 @@
565568
977A25202D5A439300931E34 /* AvailableCamerasTests.swift in Sources */,
566569
E1FFEAAD2D6C8DD700B14107 /* MockFLTCam.swift in Sources */,
567570
7F29EB292D26A59000740257 /* MockCameraDeviceDiscoverer.m in Sources */,
571+
E16602952D8471C0003CFE12 /* FLTCamZoomTests.swift in Sources */,
568572
97BD4A102D5CE13500F857D5 /* CameraSessionPresetsTests.swift in Sources */,
569573
7FD582272D57C020003B1200 /* MockAssetWriter.m in Sources */,
570574
979B3E022D5BA48F009BDE1A /* CameraOrientationTests.swift in Sources */,
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
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 FLTCamZoomTests: XCTestCase {
11+
private func createCamera() -> (FLTCam, MockCaptureDevice) {
12+
let mockDevice = MockCaptureDevice()
13+
14+
let configuration = FLTCreateTestCameraConfiguration()
15+
configuration.captureDeviceFactory = { mockDevice }
16+
let camera = FLTCreateCamWithConfiguration(configuration)
17+
18+
return (camera, mockDevice)
19+
}
20+
21+
func testSetZoomLevel_setVideoZoomFactor() {
22+
let (camera, mockDevice) = createCamera()
23+
24+
mockDevice.maxAvailableVideoZoomFactor = 2.0
25+
mockDevice.minAvailableVideoZoomFactor = 0.0
26+
27+
let targetZoom = CGFloat(1.0)
28+
29+
var setVideoZoomFactorCalled = false
30+
mockDevice.setVideoZoomFactorStub = { zoom in
31+
XCTAssertEqual(zoom, Float(targetZoom))
32+
setVideoZoomFactorCalled = true
33+
}
34+
35+
let expectation = expectation(description: "Call completed")
36+
37+
camera.setZoomLevel(targetZoom) { error in
38+
XCTAssertNil(error)
39+
expectation.fulfill()
40+
}
41+
42+
waitForExpectations(timeout: 30)
43+
44+
XCTAssertTrue(setVideoZoomFactorCalled)
45+
}
46+
47+
func testSetZoomLevel_returnsError_forZoomLevelBlowMinimum() {
48+
let (camera, mockDevice) = createCamera()
49+
50+
// Allowed zoom range between 2.0 and 3.0
51+
mockDevice.maxAvailableVideoZoomFactor = 2.0
52+
mockDevice.minAvailableVideoZoomFactor = 3.0
53+
54+
let expectation = expectation(description: "Call completed")
55+
56+
camera.setZoomLevel(CGFloat(1.0)) { error in
57+
XCTAssertNotNil(error)
58+
XCTAssertEqual(error?.code, "ZOOM_ERROR")
59+
expectation.fulfill()
60+
}
61+
62+
waitForExpectations(timeout: 30)
63+
}
64+
65+
func testSetZoomLevel_returnsError_forZoomLevelAboveMaximum() {
66+
let (camera, mockDevice) = createCamera()
67+
68+
// Allowed zoom range between 0.0 and 1.0
69+
mockDevice.maxAvailableVideoZoomFactor = 0.0
70+
mockDevice.minAvailableVideoZoomFactor = 1.0
71+
72+
let expectation = expectation(description: "Call completed")
73+
74+
camera.setZoomLevel(CGFloat(2.0)) { error in
75+
XCTAssertNotNil(error)
76+
XCTAssertEqual(error?.code, "ZOOM_ERROR")
77+
expectation.fulfill()
78+
}
79+
80+
waitForExpectations(timeout: 30)
81+
}
82+
83+
func testMaximumAvailableZoomFactor_returnsDeviceMaxAvailableVideoZoomFactor() {
84+
let (camera, mockDevice) = createCamera()
85+
86+
let targetZoom = CGFloat(1.0)
87+
88+
mockDevice.maxAvailableVideoZoomFactor = Float(targetZoom)
89+
90+
XCTAssertEqual(camera.maximumAvailableZoomFactor, targetZoom)
91+
}
92+
93+
func testMinimumAvailableZoomFactor_returnsDeviceMinAvailableVideoZoomFactor() {
94+
let (camera, mockDevice) = createCamera()
95+
96+
let targetZoom = CGFloat(1.0)
97+
98+
mockDevice.minAvailableVideoZoomFactor = Float(targetZoom)
99+
100+
XCTAssertEqual(camera.minimumAvailableZoomFactor, targetZoom)
101+
}
102+
}

0 commit comments

Comments
 (0)