|
| 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