|
| 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 FLTCamSetFlashModeTests: XCTestCase { |
| 11 | + private func createCamera() -> (FLTCam, MockCaptureDevice, MockCapturePhotoOutput) { |
| 12 | + let mockDevice = MockCaptureDevice() |
| 13 | + let mockCapturePhotoOutput = MockCapturePhotoOutput() |
| 14 | + |
| 15 | + let configuration = FLTCreateTestCameraConfiguration() |
| 16 | + configuration.captureDeviceFactory = { mockDevice } |
| 17 | + let camera = FLTCreateCamWithConfiguration(configuration) |
| 18 | + camera.capturePhotoOutput = mockCapturePhotoOutput |
| 19 | + |
| 20 | + return (camera, mockDevice, mockCapturePhotoOutput) |
| 21 | + } |
| 22 | + |
| 23 | + func testSetFlashModeWithTorchMode_setsTrochModeOn() { |
| 24 | + let (camera, mockDevice, _) = createCamera() |
| 25 | + |
| 26 | + mockDevice.hasTorch = true |
| 27 | + mockDevice.isTorchAvailable = true |
| 28 | + |
| 29 | + var setTorchModeCalled = false |
| 30 | + mockDevice.setTorchModeStub = { torchMode in |
| 31 | + XCTAssertEqual(torchMode, .on) |
| 32 | + setTorchModeCalled = true |
| 33 | + } |
| 34 | + |
| 35 | + let expectation = expectation(description: "Call completed") |
| 36 | + |
| 37 | + camera.setFlashMode(.torch) { error in |
| 38 | + XCTAssertNil(error) |
| 39 | + expectation.fulfill() |
| 40 | + } |
| 41 | + |
| 42 | + waitForExpectations(timeout: 30) |
| 43 | + |
| 44 | + XCTAssertTrue(setTorchModeCalled) |
| 45 | + } |
| 46 | + |
| 47 | + func testSetFlashModeWithTorchMode_returnError_ifHasNoTorch() { |
| 48 | + let (camera, mockDevice, _) = createCamera() |
| 49 | + |
| 50 | + mockDevice.hasTorch = false |
| 51 | + |
| 52 | + let expectation = expectation(description: "Call completed") |
| 53 | + |
| 54 | + camera.setFlashMode(.torch) { error in |
| 55 | + XCTAssertNotNil(error) |
| 56 | + XCTAssertEqual(error?.code, "setFlashModeFailed") |
| 57 | + XCTAssertEqual(error?.message, "Device does not support torch mode") |
| 58 | + expectation.fulfill() |
| 59 | + } |
| 60 | + |
| 61 | + waitForExpectations(timeout: 30) |
| 62 | + } |
| 63 | + |
| 64 | + func testSetFlashModeWithTorchMode_returnError_ifTorchIsNotAvailable() { |
| 65 | + let (camera, mockDevice, _) = createCamera() |
| 66 | + |
| 67 | + mockDevice.hasTorch = true |
| 68 | + mockDevice.isTorchAvailable = false |
| 69 | + |
| 70 | + let expectation = expectation(description: "Call completed") |
| 71 | + |
| 72 | + camera.setFlashMode(.torch) { error in |
| 73 | + XCTAssertNotNil(error) |
| 74 | + XCTAssertEqual(error?.code, "setFlashModeFailed") |
| 75 | + XCTAssertEqual(error?.message, "Torch mode is currently not available") |
| 76 | + expectation.fulfill() |
| 77 | + } |
| 78 | + |
| 79 | + waitForExpectations(timeout: 30) |
| 80 | + } |
| 81 | + |
| 82 | + func testSetFlashModeWithNonTorchMode_setsTrochModeOff_ifTorchModeIsEnabled() { |
| 83 | + let (camera, mockDevice, mockCapturePhotoOutput) = createCamera() |
| 84 | + |
| 85 | + mockCapturePhotoOutput.supportedFlashModes = [ |
| 86 | + NSNumber(value: AVCaptureDevice.FlashMode.auto.rawValue) |
| 87 | + ] |
| 88 | + |
| 89 | + mockDevice.hasFlash = true |
| 90 | + // Torch mode is enabled |
| 91 | + mockDevice.getTorchModeStub = { .on } |
| 92 | + |
| 93 | + var setTorchModeCalled = false |
| 94 | + mockDevice.setTorchModeStub = { torchMode in |
| 95 | + XCTAssertEqual(torchMode, .off) |
| 96 | + setTorchModeCalled = true |
| 97 | + } |
| 98 | + |
| 99 | + let expectation = expectation(description: "Call completed") |
| 100 | + |
| 101 | + camera.setFlashMode(.auto) { error in |
| 102 | + XCTAssertNil(error) |
| 103 | + expectation.fulfill() |
| 104 | + } |
| 105 | + |
| 106 | + waitForExpectations(timeout: 30) |
| 107 | + |
| 108 | + XCTAssertTrue(setTorchModeCalled) |
| 109 | + } |
| 110 | + |
| 111 | + func testSetFlashModeWithNonTorchMode_returnError_ifHasNoFlash() { |
| 112 | + let (camera, mockDevice, _) = createCamera() |
| 113 | + |
| 114 | + mockDevice.hasFlash = false |
| 115 | + |
| 116 | + let expectation = expectation(description: "Call completed") |
| 117 | + |
| 118 | + camera.setFlashMode(.auto) { error in |
| 119 | + XCTAssertNotNil(error) |
| 120 | + XCTAssertEqual(error?.code, "setFlashModeFailed") |
| 121 | + XCTAssertEqual(error?.message, "Device does not have flash capabilities") |
| 122 | + expectation.fulfill() |
| 123 | + } |
| 124 | + |
| 125 | + waitForExpectations(timeout: 30) |
| 126 | + } |
| 127 | + |
| 128 | + func testSetFlashModeWithNonTorchMode_returnError_ifModeIsNotSupported() { |
| 129 | + let (camera, mockDevice, mockCapturePhotoOutput) = createCamera() |
| 130 | + |
| 131 | + // No flash modes are supported |
| 132 | + mockCapturePhotoOutput.supportedFlashModes = [] |
| 133 | + |
| 134 | + mockDevice.hasFlash = true |
| 135 | + |
| 136 | + let expectation = expectation(description: "Call completed") |
| 137 | + |
| 138 | + camera.setFlashMode(.auto) { error in |
| 139 | + XCTAssertNotNil(error) |
| 140 | + XCTAssertEqual(error?.code, "setFlashModeFailed") |
| 141 | + XCTAssertEqual(error?.message, "Device does not support this specific flash mode") |
| 142 | + expectation.fulfill() |
| 143 | + } |
| 144 | + |
| 145 | + waitForExpectations(timeout: 30) |
| 146 | + } |
| 147 | +} |
0 commit comments