|
3 | 3 | // found in the LICENSE file. |
4 | 4 |
|
5 | 5 | import AVFoundation |
6 | | -import XCTest |
| 6 | +import Testing |
| 7 | +import UIKit |
7 | 8 |
|
8 | 9 | @testable import camera_avfoundation |
9 | 10 |
|
10 | | -final class CameraSetDeviceOrientationTests: XCTestCase { |
11 | | - private func createCamera() -> (Camera, MockCaptureConnection, MockCaptureConnection) { |
| 11 | +struct CameraSetDeviceOrientationTests { |
| 12 | + private func createCamera() -> (Camera, [MockCaptureConnection]) { |
12 | 13 | let camera = CameraTestUtils.createTestCamera() |
13 | 14 |
|
14 | 15 | let mockCapturePhotoOutput = MockCapturePhotoOutput() |
15 | 16 | let mockPhotoCaptureConnection = MockCaptureConnection() |
16 | 17 | mockPhotoCaptureConnection.isVideoOrientationSupported = true |
| 18 | + mockPhotoCaptureConnection.isVideoRotationAngleSupported = true |
17 | 19 |
|
18 | 20 | mockCapturePhotoOutput.connectionWithMediaTypeStub = { _ in mockPhotoCaptureConnection } |
19 | 21 | camera.capturePhotoOutput = mockCapturePhotoOutput |
20 | 22 |
|
21 | 23 | let mockCaptureVideoDataOutput = MockCaptureVideoDataOutput() |
22 | 24 | let mockVideoCaptureConnection = MockCaptureConnection() |
23 | 25 | mockVideoCaptureConnection.isVideoOrientationSupported = true |
| 26 | + mockVideoCaptureConnection.isVideoRotationAngleSupported = true |
24 | 27 |
|
25 | 28 | mockCaptureVideoDataOutput.connectionWithMediaTypeStub = { _ in mockVideoCaptureConnection } |
26 | 29 | camera.captureVideoOutput = mockCaptureVideoDataOutput |
27 | 30 |
|
28 | | - return (camera, mockPhotoCaptureConnection, mockVideoCaptureConnection) |
| 31 | + return (camera, [mockPhotoCaptureConnection, mockVideoCaptureConnection]) |
29 | 32 | } |
30 | 33 |
|
31 | | - func testSetDeviceOrientation_setsOrientationsOfCaptureConnections() { |
32 | | - let (camera, mockPhotoCaptureConnection, mockVideoCaptureConnection) = createCamera() |
33 | | - var photoSetVideoOrientationCalled = false |
34 | | - mockPhotoCaptureConnection.setVideoOrientationStub = { orientation in |
35 | | - // Device orientation is flipped compared to video orientation. When UIDeviceOrientation |
36 | | - // is landscape left the video orientation should be landscape right. |
37 | | - XCTAssertEqual(orientation, .landscapeRight) |
38 | | - photoSetVideoOrientationCalled = true |
| 34 | + private func stubOrientationUpdates( |
| 35 | + on connection: MockCaptureConnection, |
| 36 | + _ handler: @escaping (AVCaptureVideoOrientation) -> Void |
| 37 | + ) { |
| 38 | + connection.setVideoOrientationStub = handler |
| 39 | + connection.setVideoRotationAngleStub = { angle in |
| 40 | + switch angle { |
| 41 | + case 0: |
| 42 | + handler(.landscapeRight) |
| 43 | + case 90: |
| 44 | + handler(.portrait) |
| 45 | + case 180: |
| 46 | + handler(.landscapeLeft) |
| 47 | + case 270: |
| 48 | + handler(.portraitUpsideDown) |
| 49 | + default: |
| 50 | + Issue.record("Unexpected video rotation angle: \(angle)") |
| 51 | + } |
39 | 52 | } |
| 53 | + } |
40 | 54 |
|
41 | | - var videoSetVideoOrientationCalled = false |
42 | | - mockVideoCaptureConnection.setVideoOrientationStub = { orientation in |
43 | | - // Device orientation is flipped compared to video orientation. When UIDeviceOrientation |
44 | | - // is landscape left the video orientation should be landscape right. |
45 | | - XCTAssertEqual(orientation, .landscapeRight) |
46 | | - videoSetVideoOrientationCalled = true |
| 55 | + @Test func setDeviceOrientation_setsOrientationsOfCaptureConnections() { |
| 56 | + let (camera, mockConnections) = createCamera() |
| 57 | + var setVideoOrientations: [AVCaptureVideoOrientation] = [] |
| 58 | + for connection in mockConnections { |
| 59 | + stubOrientationUpdates(on: connection) { setVideoOrientations.append($0) } |
47 | 60 | } |
48 | 61 |
|
49 | 62 | camera.deviceOrientation = .landscapeLeft |
50 | 63 |
|
51 | | - XCTAssertTrue(photoSetVideoOrientationCalled) |
52 | | - XCTAssertTrue(videoSetVideoOrientationCalled) |
| 64 | + // Device orientation is flipped compared to video orientation. When UIDeviceOrientation |
| 65 | + // is landscape left the video orientation should be landscape right. |
| 66 | + #expect(setVideoOrientations == [.landscapeRight, .landscapeRight]) |
53 | 67 | } |
54 | 68 |
|
55 | | - func |
56 | | - testSetDeviceOrientation_setsLockedOrientationsOfCaptureConnection_ifCaptureOrientationIsLocked() |
| 69 | + @Test |
| 70 | + func setDeviceOrientation_setsLockedOrientationsOfCaptureConnection_ifCaptureOrientationIsLocked() |
57 | 71 | { |
58 | | - let (camera, mockPhotoCaptureConnection, mockVideoCaptureConnection) = createCamera() |
59 | | - var photoSetVideoOrientationCalled = false |
60 | | - mockPhotoCaptureConnection.setVideoOrientationStub = { orientation in |
61 | | - XCTAssertEqual(orientation, .portraitUpsideDown) |
62 | | - photoSetVideoOrientationCalled = true |
63 | | - } |
| 72 | + let (camera, mockConnections) = createCamera() |
| 73 | + camera.lockCaptureOrientation(PlatformDeviceOrientation.portraitDown) |
64 | 74 |
|
65 | | - var videoSetVideoOrientationCalled = false |
66 | | - mockVideoCaptureConnection.setVideoOrientationStub = { orientation in |
67 | | - XCTAssertEqual(orientation, .portraitUpsideDown) |
68 | | - videoSetVideoOrientationCalled = true |
| 75 | + var setVideoOrientations: [AVCaptureVideoOrientation] = [] |
| 76 | + for connection in mockConnections { |
| 77 | + stubOrientationUpdates(on: connection) { setVideoOrientations.append($0) } |
69 | 78 | } |
70 | 79 |
|
71 | | - camera.lockCaptureOrientation(PlatformDeviceOrientation.portraitDown) |
72 | | - |
73 | 80 | camera.deviceOrientation = .landscapeLeft |
74 | 81 |
|
75 | | - XCTAssertTrue(photoSetVideoOrientationCalled) |
76 | | - XCTAssertTrue(videoSetVideoOrientationCalled) |
| 82 | + #expect(setVideoOrientations == [.portraitUpsideDown, .portraitUpsideDown]) |
77 | 83 | } |
78 | 84 |
|
79 | | - func testSetDeviceOrientation_doesNotSetOrientations_ifRecordingIsInProgress() { |
80 | | - let (camera, mockPhotoCaptureConnection, mockVideoCaptureConnection) = createCamera() |
81 | | - |
| 85 | + @Test func setDeviceOrientation_doesNotSetOrientations_ifRecordingIsInProgress() { |
| 86 | + let (camera, mockConnections) = createCamera() |
82 | 87 | camera.startVideoRecording(completion: { _ in }, messengerForStreaming: nil) |
83 | 88 |
|
84 | | - mockPhotoCaptureConnection.setVideoOrientationStub = { _ in XCTFail() } |
85 | | - mockVideoCaptureConnection.setVideoOrientationStub = { _ in XCTFail() } |
| 89 | + for connection in mockConnections { |
| 90 | + stubOrientationUpdates(on: connection) { _ in Issue.record() } |
| 91 | + } |
86 | 92 |
|
87 | 93 | camera.deviceOrientation = .landscapeLeft |
88 | 94 | } |
89 | 95 |
|
90 | | - func testSetDeviceOrientation_doesNotSetOrientations_forDuplicateUpdates() { |
91 | | - let (camera, mockPhotoCaptureConnection, mockVideoCaptureConnection) = createCamera() |
92 | | - var photoSetVideoOrientationCallCount = 0 |
93 | | - mockPhotoCaptureConnection.setVideoOrientationStub = { _ in |
94 | | - photoSetVideoOrientationCallCount += 1 |
95 | | - } |
96 | | - |
97 | | - var videoSetVideoOrientationCallCount = 0 |
98 | | - mockVideoCaptureConnection.setVideoOrientationStub = { _ in |
99 | | - videoSetVideoOrientationCallCount += 1 |
| 96 | + @Test func setDeviceOrientation_doesNotSetOrientations_forDuplicateUpdates() { |
| 97 | + let (camera, mockConnections) = createCamera() |
| 98 | + var setVideoOrientationCallCount = 0 |
| 99 | + for connection in mockConnections { |
| 100 | + stubOrientationUpdates(on: connection) { _ in setVideoOrientationCallCount += 1 } |
100 | 101 | } |
101 | 102 |
|
102 | 103 | camera.deviceOrientation = .landscapeRight |
103 | 104 | camera.deviceOrientation = .landscapeRight |
104 | 105 |
|
105 | | - XCTAssertEqual(photoSetVideoOrientationCallCount, 1) |
106 | | - XCTAssertEqual(videoSetVideoOrientationCallCount, 1) |
| 106 | + #expect(setVideoOrientationCallCount == mockConnections.count) |
| 107 | + } |
| 108 | + |
| 109 | + @Test func setDeviceOrientation_doesNotSetOrientations_ifNotSupportedByConnection() { |
| 110 | + let (camera, mockConnections) = createCamera() |
| 111 | + for connection in mockConnections { |
| 112 | + connection.isVideoOrientationSupported = false |
| 113 | + connection.isVideoRotationAngleSupported = false |
| 114 | + stubOrientationUpdates(on: connection) { _ in Issue.record() } |
| 115 | + } |
| 116 | + |
| 117 | + camera.deviceOrientation = .landscapeLeft |
| 118 | + } |
| 119 | + |
| 120 | + @Test(arguments: [ |
| 121 | + (UIDeviceOrientation.portrait, AVCaptureVideoOrientation.portrait), |
| 122 | + (.portraitUpsideDown, .portraitUpsideDown), |
| 123 | + (.landscapeLeft, .landscapeRight), |
| 124 | + (.landscapeRight, .landscapeLeft), |
| 125 | + // Orientations without a video equivalent fall back to portrait. |
| 126 | + (.faceUp, .portrait), |
| 127 | + ]) |
| 128 | + func setDeviceOrientation_mapsDeviceOrientationToVideoOrientation( |
| 129 | + deviceOrientation: UIDeviceOrientation, expectedVideoOrientation: AVCaptureVideoOrientation |
| 130 | + ) { |
| 131 | + let (camera, mockConnections) = createCamera() |
| 132 | + // Orientation updates are skipped for unchanged values, so start from a non-matching one. |
| 133 | + camera.deviceOrientation = .unknown |
| 134 | + var setVideoOrientations: [AVCaptureVideoOrientation] = [] |
| 135 | + for connection in mockConnections { |
| 136 | + stubOrientationUpdates(on: connection) { setVideoOrientations.append($0) } |
| 137 | + } |
| 138 | + |
| 139 | + camera.deviceOrientation = deviceOrientation |
| 140 | + |
| 141 | + #expect(setVideoOrientations == [expectedVideoOrientation, expectedVideoOrientation]) |
| 142 | + } |
| 143 | + |
| 144 | + @Test func unlockCaptureOrientation_restoresDeviceOrientation() { |
| 145 | + let (camera, mockConnections) = createCamera() |
| 146 | + camera.lockCaptureOrientation(PlatformDeviceOrientation.portraitDown) |
| 147 | + camera.deviceOrientation = .landscapeLeft |
| 148 | + |
| 149 | + var setVideoOrientations: [AVCaptureVideoOrientation] = [] |
| 150 | + for connection in mockConnections { |
| 151 | + stubOrientationUpdates(on: connection) { setVideoOrientations.append($0) } |
| 152 | + } |
| 153 | + |
| 154 | + camera.unlockCaptureOrientation() |
| 155 | + |
| 156 | + #expect(setVideoOrientations == [.landscapeRight, .landscapeRight]) |
107 | 157 | } |
108 | 158 | } |
0 commit comments