@capgo/camera-preview 8.12.3 → 8.12.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +43 -39
- package/android/src/main/java/app/capgo/capacitor/camera/preview/CameraXView.java +26 -28
- package/android/src/main/java/app/capgo/capacitor/camera/preview/PhysicalOrientationResolver.java +72 -0
- package/android/src/test/java/app/capgo/capacitor/camera/preview/PhysicalOrientationResolverTest.java +90 -0
- package/dist/docs.json +8 -4
- package/dist/esm/definitions.d.ts +20 -8
- package/dist/esm/definitions.js.map +1 -1
- package/dist/esm/web.js +6 -3
- package/dist/esm/web.js.map +1 -1
- package/dist/plugin.cjs.js +6 -3
- package/dist/plugin.cjs.js.map +1 -1
- package/dist/plugin.js +6 -3
- package/dist/plugin.js.map +1 -1
- package/ios/Sources/CapgoCameraPreviewPlugin/ApplicationAudioSessionRestoreCoordinator.swift +136 -0
- package/ios/Sources/CapgoCameraPreviewPlugin/ApplicationAudioSessionSnapshot.swift +28 -0
- package/ios/Sources/CapgoCameraPreviewPlugin/AspectRatioLayout.swift +40 -0
- package/ios/Sources/CapgoCameraPreviewPlugin/CameraController.swift +204 -34
- package/ios/Sources/CapgoCameraPreviewPlugin/CapturePhotoProcessingQueue.swift +20 -0
- package/ios/Sources/CapgoCameraPreviewPlugin/CaptureResolutionSupport.swift +133 -0
- package/ios/Sources/CapgoCameraPreviewPlugin/ImageIOJpegEncoding.swift +28 -0
- package/ios/Sources/CapgoCameraPreviewPlugin/PhysicalOrientationResolver.swift +59 -0
- package/ios/Sources/CapgoCameraPreviewPlugin/Plugin.swift +59 -63
- package/ios/Tests/CameraPreviewPluginTests/ApplicationAudioSessionRestoreCoordinatorTests.swift +279 -0
- package/ios/Tests/CameraPreviewPluginTests/ApplicationAudioSessionSnapshotTests.swift +25 -0
- package/ios/Tests/CameraPreviewPluginTests/CameraPreviewPluginTests.swift +26 -0
- package/ios/Tests/CameraPreviewPluginTests/CaptureResolutionSupportTests.swift +133 -0
- package/ios/Tests/CameraPreviewPluginTests/ImageIOJpegEncodingTests.swift +42 -0
- package/ios/Tests/CameraPreviewPluginTests/PhysicalOrientationResolverTests.swift +89 -0
- package/package.json +5 -3
package/ios/Tests/CameraPreviewPluginTests/ApplicationAudioSessionRestoreCoordinatorTests.swift
ADDED
|
@@ -0,0 +1,279 @@
|
|
|
1
|
+
import AVFoundation
|
|
2
|
+
import XCTest
|
|
3
|
+
@testable import CapgoCameraPreview
|
|
4
|
+
|
|
5
|
+
private extension ApplicationAudioSessionRestoreCoordinator {
|
|
6
|
+
func registerCleanup(isRecording: Bool, pendingRecordingIds: Set<UInt64> = []) {
|
|
7
|
+
if self.beginCaptureSessionTeardown(isRecording: isRecording, pendingRecordingIds: pendingRecordingIds) {
|
|
8
|
+
self.restoreAfterCaptureSessionTeardown()
|
|
9
|
+
} else {
|
|
10
|
+
self.completeCaptureSessionTeardown()
|
|
11
|
+
}
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
func registerCleanupAndRestoreIfNeeded(isRecording: Bool, pendingRecordingIds: Set<UInt64> = []) {
|
|
15
|
+
registerCleanup(isRecording: isRecording, pendingRecordingIds: pendingRecordingIds)
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
func beginDeferredCleanup(isRecording: Bool, pendingRecordingIds: Set<UInt64> = []) {
|
|
19
|
+
XCTAssertFalse(
|
|
20
|
+
self.beginCaptureSessionTeardown(isRecording: isRecording, pendingRecordingIds: pendingRecordingIds)
|
|
21
|
+
)
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
final class ApplicationAudioSessionRestoreCoordinatorTests: XCTestCase {
|
|
26
|
+
func testDoubleCleanupWhileRecordingActiveDefersSingleRestoreUntilRecordingFinishes() throws {
|
|
27
|
+
let session = AVAudioSession.sharedInstance()
|
|
28
|
+
let priorCategory = session.category
|
|
29
|
+
let priorMode = session.mode
|
|
30
|
+
let priorOptions = session.categoryOptions
|
|
31
|
+
defer { try? session.setCategory(priorCategory, mode: priorMode, options: priorOptions) }
|
|
32
|
+
|
|
33
|
+
try session.setCategory(.playback, mode: .default, options: [.mixWithOthers])
|
|
34
|
+
|
|
35
|
+
let coordinator = ApplicationAudioSessionRestoreCoordinator()
|
|
36
|
+
coordinator.beginPreviewSession()
|
|
37
|
+
coordinator.captureSnapshotIfNeeded()
|
|
38
|
+
|
|
39
|
+
try session.setCategory(.playAndRecord, mode: .videoRecording, options: [.defaultToSpeaker])
|
|
40
|
+
|
|
41
|
+
let recordingId = coordinator.beginRecording()
|
|
42
|
+
coordinator.registerCleanup(isRecording: true)
|
|
43
|
+
XCTAssertTrue(coordinator.isDeferredRestorePending)
|
|
44
|
+
|
|
45
|
+
coordinator.registerCleanup(isRecording: true)
|
|
46
|
+
XCTAssertTrue(coordinator.isDeferredRestorePending)
|
|
47
|
+
XCTAssertEqual(session.category, .playAndRecord)
|
|
48
|
+
|
|
49
|
+
coordinator.finishRecording(recordingId: recordingId)
|
|
50
|
+
XCTAssertFalse(coordinator.isDeferredRestorePending)
|
|
51
|
+
XCTAssertEqual(session.category, .playback)
|
|
52
|
+
XCTAssertEqual(session.mode, .default)
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
func testRepeatedCleanupDuringRecordingKeepsDeferralUntilRecordingFinishes() {
|
|
56
|
+
let coordinator = ApplicationAudioSessionRestoreCoordinator()
|
|
57
|
+
coordinator.beginPreviewSession()
|
|
58
|
+
coordinator.captureSnapshotIfNeeded()
|
|
59
|
+
|
|
60
|
+
let recordingId = coordinator.beginRecording()
|
|
61
|
+
coordinator.registerCleanup(isRecording: true)
|
|
62
|
+
XCTAssertTrue(coordinator.isDeferredRestorePending)
|
|
63
|
+
|
|
64
|
+
coordinator.registerCleanup(isRecording: false)
|
|
65
|
+
XCTAssertTrue(coordinator.isDeferredRestorePending)
|
|
66
|
+
|
|
67
|
+
coordinator.finishRecording(recordingId: recordingId)
|
|
68
|
+
XCTAssertFalse(coordinator.isDeferredRestorePending)
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
func testCleanupDefersWhenPendingRecordingIdProvidedAfterOutputStops() throws {
|
|
72
|
+
let session = AVAudioSession.sharedInstance()
|
|
73
|
+
let priorCategory = session.category
|
|
74
|
+
let priorMode = session.mode
|
|
75
|
+
let priorOptions = session.categoryOptions
|
|
76
|
+
defer { try? session.setCategory(priorCategory, mode: priorMode, options: priorOptions) }
|
|
77
|
+
|
|
78
|
+
try session.setCategory(.playback, mode: .default, options: [.mixWithOthers])
|
|
79
|
+
|
|
80
|
+
let coordinator = ApplicationAudioSessionRestoreCoordinator()
|
|
81
|
+
coordinator.beginPreviewSession()
|
|
82
|
+
coordinator.captureSnapshotIfNeeded()
|
|
83
|
+
|
|
84
|
+
try session.setCategory(.playAndRecord, mode: .videoRecording, options: [.defaultToSpeaker])
|
|
85
|
+
|
|
86
|
+
let recordingId = coordinator.beginRecording()
|
|
87
|
+
coordinator.registerCleanup(isRecording: false, pendingRecordingIds: [recordingId])
|
|
88
|
+
XCTAssertTrue(coordinator.isDeferredRestorePending)
|
|
89
|
+
XCTAssertEqual(session.category, .playAndRecord)
|
|
90
|
+
|
|
91
|
+
coordinator.finishRecording(recordingId: recordingId)
|
|
92
|
+
XCTAssertEqual(session.category, .playback)
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
func testNewPreviewBeforeRecordingCompletionSkipsLateRestore() throws {
|
|
96
|
+
let session = AVAudioSession.sharedInstance()
|
|
97
|
+
let priorCategory = session.category
|
|
98
|
+
let priorMode = session.mode
|
|
99
|
+
let priorOptions = session.categoryOptions
|
|
100
|
+
defer { try? session.setCategory(priorCategory, mode: priorMode, options: priorOptions) }
|
|
101
|
+
|
|
102
|
+
try session.setCategory(.playback, mode: .default, options: [.mixWithOthers])
|
|
103
|
+
|
|
104
|
+
let coordinator = ApplicationAudioSessionRestoreCoordinator()
|
|
105
|
+
coordinator.beginPreviewSession()
|
|
106
|
+
coordinator.captureSnapshotIfNeeded()
|
|
107
|
+
|
|
108
|
+
let recordingId = coordinator.beginRecording()
|
|
109
|
+
coordinator.registerCleanup(isRecording: true)
|
|
110
|
+
|
|
111
|
+
try session.setCategory(.playAndRecord, mode: .videoRecording, options: [.defaultToSpeaker])
|
|
112
|
+
coordinator.beginPreviewSession()
|
|
113
|
+
|
|
114
|
+
coordinator.finishRecording(recordingId: recordingId)
|
|
115
|
+
XCTAssertEqual(session.category, .playAndRecord)
|
|
116
|
+
XCTAssertEqual(session.mode, .videoRecording)
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
func testNewPreviewBeforeAsyncRecordingCompletionSkipsLateRestore() throws {
|
|
120
|
+
let session = AVAudioSession.sharedInstance()
|
|
121
|
+
let priorCategory = session.category
|
|
122
|
+
let priorMode = session.mode
|
|
123
|
+
let priorOptions = session.categoryOptions
|
|
124
|
+
defer { try? session.setCategory(priorCategory, mode: priorMode, options: priorOptions) }
|
|
125
|
+
|
|
126
|
+
try session.setCategory(.playback, mode: .default, options: [.mixWithOthers])
|
|
127
|
+
|
|
128
|
+
let coordinator = ApplicationAudioSessionRestoreCoordinator()
|
|
129
|
+
coordinator.beginPreviewSession()
|
|
130
|
+
coordinator.captureSnapshotIfNeeded()
|
|
131
|
+
|
|
132
|
+
let recordingId = coordinator.beginRecording()
|
|
133
|
+
coordinator.registerCleanup(isRecording: true)
|
|
134
|
+
|
|
135
|
+
try session.setCategory(.playAndRecord, mode: .videoRecording, options: [.defaultToSpeaker])
|
|
136
|
+
coordinator.beginPreviewSession()
|
|
137
|
+
|
|
138
|
+
coordinator.finishRecordingAsync(recordingId: recordingId)
|
|
139
|
+
coordinator.syncWithQueueForTesting()
|
|
140
|
+
|
|
141
|
+
XCTAssertEqual(session.category, .playAndRecord)
|
|
142
|
+
XCTAssertEqual(session.mode, .videoRecording)
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
func testCaptureSnapshotDoesNotReplaceExistingBaselineOnNewPreview() throws {
|
|
146
|
+
let session = AVAudioSession.sharedInstance()
|
|
147
|
+
let priorCategory = session.category
|
|
148
|
+
let priorMode = session.mode
|
|
149
|
+
let priorOptions = session.categoryOptions
|
|
150
|
+
defer { try? session.setCategory(priorCategory, mode: priorMode, options: priorOptions) }
|
|
151
|
+
|
|
152
|
+
try session.setCategory(.playback, mode: .default, options: [.mixWithOthers])
|
|
153
|
+
|
|
154
|
+
let coordinator = ApplicationAudioSessionRestoreCoordinator()
|
|
155
|
+
coordinator.beginPreviewSession()
|
|
156
|
+
coordinator.captureSnapshotIfNeeded()
|
|
157
|
+
|
|
158
|
+
let recordingId = coordinator.beginRecording()
|
|
159
|
+
coordinator.registerCleanup(isRecording: true)
|
|
160
|
+
|
|
161
|
+
try session.setCategory(.playAndRecord, mode: .videoRecording, options: [.defaultToSpeaker])
|
|
162
|
+
coordinator.beginPreviewSession()
|
|
163
|
+
coordinator.captureSnapshotIfNeeded()
|
|
164
|
+
|
|
165
|
+
coordinator.finishRecording(recordingId: recordingId)
|
|
166
|
+
coordinator.registerCleanupAndRestoreIfNeeded(isRecording: false)
|
|
167
|
+
XCTAssertEqual(session.category, .playback)
|
|
168
|
+
XCTAssertEqual(session.mode, .default)
|
|
169
|
+
XCTAssertEqual(session.categoryOptions, [.mixWithOthers])
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
func testBeginPreviewDuringDeferredRestoreKeepsSnapshotForLaterCleanup() throws {
|
|
173
|
+
let session = AVAudioSession.sharedInstance()
|
|
174
|
+
let priorCategory = session.category
|
|
175
|
+
let priorMode = session.mode
|
|
176
|
+
let priorOptions = session.categoryOptions
|
|
177
|
+
defer { try? session.setCategory(priorCategory, mode: priorMode, options: priorOptions) }
|
|
178
|
+
|
|
179
|
+
try session.setCategory(.playback, mode: .default, options: [.mixWithOthers])
|
|
180
|
+
|
|
181
|
+
let coordinator = ApplicationAudioSessionRestoreCoordinator()
|
|
182
|
+
coordinator.beginPreviewSession()
|
|
183
|
+
coordinator.captureSnapshotIfNeeded()
|
|
184
|
+
|
|
185
|
+
let recordingId = coordinator.beginRecording()
|
|
186
|
+
coordinator.registerCleanup(isRecording: true)
|
|
187
|
+
|
|
188
|
+
try session.setCategory(.playAndRecord, mode: .videoRecording, options: [.defaultToSpeaker])
|
|
189
|
+
coordinator.beginPreviewSession()
|
|
190
|
+
coordinator.finishRecording(recordingId: recordingId)
|
|
191
|
+
XCTAssertEqual(session.category, .playAndRecord)
|
|
192
|
+
|
|
193
|
+
coordinator.registerCleanupAndRestoreIfNeeded(isRecording: false)
|
|
194
|
+
XCTAssertEqual(session.category, .playback)
|
|
195
|
+
XCTAssertEqual(session.mode, .default)
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
func testRecordingFinishesBeforeTeardownCompleteDoesNotRestoreUntilTeardownDone() throws {
|
|
199
|
+
let session = AVAudioSession.sharedInstance()
|
|
200
|
+
let priorCategory = session.category
|
|
201
|
+
let priorMode = session.mode
|
|
202
|
+
let priorOptions = session.categoryOptions
|
|
203
|
+
defer { try? session.setCategory(priorCategory, mode: priorMode, options: priorOptions) }
|
|
204
|
+
|
|
205
|
+
try session.setCategory(.playback, mode: .default, options: [.mixWithOthers])
|
|
206
|
+
|
|
207
|
+
let coordinator = ApplicationAudioSessionRestoreCoordinator()
|
|
208
|
+
coordinator.beginPreviewSession()
|
|
209
|
+
coordinator.captureSnapshotIfNeeded()
|
|
210
|
+
|
|
211
|
+
try session.setCategory(.playAndRecord, mode: .videoRecording, options: [.defaultToSpeaker])
|
|
212
|
+
|
|
213
|
+
let recordingId = coordinator.beginRecording()
|
|
214
|
+
coordinator.beginDeferredCleanup(isRecording: true)
|
|
215
|
+
XCTAssertTrue(coordinator.isDeferredRestorePending)
|
|
216
|
+
|
|
217
|
+
coordinator.finishRecording(recordingId: recordingId)
|
|
218
|
+
coordinator.syncWithQueueForTesting()
|
|
219
|
+
XCTAssertEqual(session.category, .playAndRecord)
|
|
220
|
+
|
|
221
|
+
coordinator.completeCaptureSessionTeardown()
|
|
222
|
+
XCTAssertFalse(coordinator.isDeferredRestorePending)
|
|
223
|
+
XCTAssertEqual(session.category, .playback)
|
|
224
|
+
XCTAssertEqual(session.mode, .default)
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
func testTeardownCompletesBeforeRecordingFinishesRestoresOnRecordingFinish() throws {
|
|
228
|
+
let session = AVAudioSession.sharedInstance()
|
|
229
|
+
let priorCategory = session.category
|
|
230
|
+
let priorMode = session.mode
|
|
231
|
+
let priorOptions = session.categoryOptions
|
|
232
|
+
defer { try? session.setCategory(priorCategory, mode: priorMode, options: priorOptions) }
|
|
233
|
+
|
|
234
|
+
try session.setCategory(.playback, mode: .default, options: [.mixWithOthers])
|
|
235
|
+
|
|
236
|
+
let coordinator = ApplicationAudioSessionRestoreCoordinator()
|
|
237
|
+
coordinator.beginPreviewSession()
|
|
238
|
+
coordinator.captureSnapshotIfNeeded()
|
|
239
|
+
|
|
240
|
+
try session.setCategory(.playAndRecord, mode: .videoRecording, options: [.defaultToSpeaker])
|
|
241
|
+
|
|
242
|
+
let recordingId = coordinator.beginRecording()
|
|
243
|
+
coordinator.beginDeferredCleanup(isRecording: true)
|
|
244
|
+
coordinator.completeCaptureSessionTeardown()
|
|
245
|
+
XCTAssertEqual(session.category, .playAndRecord)
|
|
246
|
+
|
|
247
|
+
coordinator.finishRecording(recordingId: recordingId)
|
|
248
|
+
XCTAssertFalse(coordinator.isDeferredRestorePending)
|
|
249
|
+
XCTAssertEqual(session.category, .playback)
|
|
250
|
+
XCTAssertEqual(session.mode, .default)
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
func testNewPreviewDuringDeferredRestoreBeforeTeardownCompleteSkipsRestore() throws {
|
|
254
|
+
let session = AVAudioSession.sharedInstance()
|
|
255
|
+
let priorCategory = session.category
|
|
256
|
+
let priorMode = session.mode
|
|
257
|
+
let priorOptions = session.categoryOptions
|
|
258
|
+
defer { try? session.setCategory(priorCategory, mode: priorMode, options: priorOptions) }
|
|
259
|
+
|
|
260
|
+
try session.setCategory(.playback, mode: .default, options: [.mixWithOthers])
|
|
261
|
+
|
|
262
|
+
let coordinator = ApplicationAudioSessionRestoreCoordinator()
|
|
263
|
+
coordinator.beginPreviewSession()
|
|
264
|
+
coordinator.captureSnapshotIfNeeded()
|
|
265
|
+
|
|
266
|
+
let recordingId = coordinator.beginRecording()
|
|
267
|
+
coordinator.beginDeferredCleanup(isRecording: true)
|
|
268
|
+
|
|
269
|
+
try session.setCategory(.playAndRecord, mode: .videoRecording, options: [.defaultToSpeaker])
|
|
270
|
+
coordinator.beginPreviewSession()
|
|
271
|
+
|
|
272
|
+
coordinator.finishRecording(recordingId: recordingId)
|
|
273
|
+
coordinator.completeCaptureSessionTeardown()
|
|
274
|
+
coordinator.syncWithQueueForTesting()
|
|
275
|
+
|
|
276
|
+
XCTAssertEqual(session.category, .playAndRecord)
|
|
277
|
+
XCTAssertEqual(session.mode, .videoRecording)
|
|
278
|
+
}
|
|
279
|
+
}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import AVFoundation
|
|
2
|
+
import XCTest
|
|
3
|
+
@testable import CapgoCameraPreview
|
|
4
|
+
|
|
5
|
+
final class ApplicationAudioSessionSnapshotTests: XCTestCase {
|
|
6
|
+
func testSnapshotAndRestoreCategoryModeAndOptions() throws {
|
|
7
|
+
let session = AVAudioSession.sharedInstance()
|
|
8
|
+
let priorCategory = session.category
|
|
9
|
+
let priorMode = session.mode
|
|
10
|
+
let priorOptions = session.categoryOptions
|
|
11
|
+
defer { try? session.setCategory(priorCategory, mode: priorMode, options: priorOptions) }
|
|
12
|
+
|
|
13
|
+
try session.setCategory(.playback, mode: .default, options: [.mixWithOthers])
|
|
14
|
+
let snapshot = ApplicationAudioSessionSnapshot.capture(from: session)
|
|
15
|
+
|
|
16
|
+
try session.setCategory(.playAndRecord, mode: .videoRecording, options: [.defaultToSpeaker])
|
|
17
|
+
XCTAssertEqual(session.category, .playAndRecord)
|
|
18
|
+
XCTAssertEqual(session.mode, .videoRecording)
|
|
19
|
+
|
|
20
|
+
XCTAssertTrue(snapshot.apply(to: session))
|
|
21
|
+
XCTAssertEqual(session.category, .playback)
|
|
22
|
+
XCTAssertEqual(session.mode, .default)
|
|
23
|
+
XCTAssertEqual(session.categoryOptions, [.mixWithOthers])
|
|
24
|
+
}
|
|
25
|
+
}
|
|
@@ -43,6 +43,32 @@ final class AspectRatioLayoutTests: XCTestCase {
|
|
|
43
43
|
XCTAssertGreaterThan(dimensions.height, 500)
|
|
44
44
|
}
|
|
45
45
|
|
|
46
|
+
func testContainContentRectLetterboxesFourThreeInTallContainer() {
|
|
47
|
+
let container = CGRect(x: 0, y: 0, width: 390, height: 700)
|
|
48
|
+
let content = AspectRatioLayout.containContentRect(
|
|
49
|
+
contentSize: CGSize(width: 3, height: 4),
|
|
50
|
+
inside: container
|
|
51
|
+
)
|
|
52
|
+
|
|
53
|
+
XCTAssertEqual(content.width, 390, accuracy: 0.5)
|
|
54
|
+
XCTAssertEqual(content.height, 520, accuracy: 0.5)
|
|
55
|
+
XCTAssertEqual(content.origin.x, 0, accuracy: 0.5)
|
|
56
|
+
XCTAssertEqual(content.origin.y, 90, accuracy: 0.5)
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
func testPreviewContentRectInWebViewCoordinatesOffsetsByPreviewViewOrigin() {
|
|
60
|
+
let content = CGRect(x: 0, y: 90, width: 390, height: 520)
|
|
61
|
+
let mapped = AspectRatioLayout.previewContentRectInWebViewCoordinates(
|
|
62
|
+
contentRectInPreviewView: content,
|
|
63
|
+
previewViewFrame: CGRect(x: 12, y: 48, width: 390, height: 700)
|
|
64
|
+
)
|
|
65
|
+
|
|
66
|
+
XCTAssertEqual(mapped.origin.x, 12, accuracy: 0.5)
|
|
67
|
+
XCTAssertEqual(mapped.origin.y, 138, accuracy: 0.5)
|
|
68
|
+
XCTAssertEqual(mapped.width, 390, accuracy: 0.5)
|
|
69
|
+
XCTAssertEqual(mapped.height, 520, accuracy: 0.5)
|
|
70
|
+
}
|
|
71
|
+
|
|
46
72
|
func testMalformedRatioFallsBackToFullViewport() {
|
|
47
73
|
XCTAssertNil(AspectRatioLayout.parseViewportAspectRatio("4:bad:3", isPortrait: true))
|
|
48
74
|
|
|
@@ -0,0 +1,133 @@
|
|
|
1
|
+
import AVFoundation
|
|
2
|
+
import XCTest
|
|
3
|
+
@testable import CapgoCameraPreview
|
|
4
|
+
|
|
5
|
+
final class CaptureResolutionSupportTests: XCTestCase {
|
|
6
|
+
func testParseCaptureResolutionNullObject() {
|
|
7
|
+
let result = CaptureResolutionSupport.parseStartCaptureResolution(nil)
|
|
8
|
+
XCTAssertFalse(result.isError)
|
|
9
|
+
XCTAssertNil(result.width)
|
|
10
|
+
XCTAssertNil(result.height)
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
func testParseCaptureResolutionMissingWidthOrHeight() {
|
|
14
|
+
let missingWidth = CaptureResolutionSupport.parseStartCaptureResolution(["height": 1080])
|
|
15
|
+
XCTAssertTrue(missingWidth.isError)
|
|
16
|
+
XCTAssertEqual(missingWidth.errorMessage, "captureResolution requires both width and height")
|
|
17
|
+
|
|
18
|
+
let missingHeight = CaptureResolutionSupport.parseStartCaptureResolution(["width": 1920])
|
|
19
|
+
XCTAssertTrue(missingHeight.isError)
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
func testParseCaptureResolutionInvalidTypesAndValues() {
|
|
23
|
+
XCTAssertTrue(CaptureResolutionSupport.parseStartCaptureResolution("not-an-object").isError)
|
|
24
|
+
|
|
25
|
+
let zero = CaptureResolutionSupport.parseStartCaptureResolution(["width": 0, "height": 1080])
|
|
26
|
+
XCTAssertTrue(zero.isError)
|
|
27
|
+
|
|
28
|
+
let negative = CaptureResolutionSupport.parseStartCaptureResolution(["width": -1, "height": 1080])
|
|
29
|
+
XCTAssertTrue(negative.isError)
|
|
30
|
+
|
|
31
|
+
let nanWidth = CaptureResolutionSupport.parseStartCaptureResolution(["width": "NaN", "height": 1080])
|
|
32
|
+
XCTAssertTrue(nanWidth.isError)
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
func testParseCaptureResolutionStringNumbers() {
|
|
36
|
+
let result = CaptureResolutionSupport.parseStartCaptureResolution(["width": "1920", "height": "1080"])
|
|
37
|
+
XCTAssertFalse(result.isError)
|
|
38
|
+
XCTAssertEqual(result.width, 1920)
|
|
39
|
+
XCTAssertEqual(result.height, 1080)
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
func testNormalizedCaptureTargetResolutionSwapsToLandscapeMajor() {
|
|
43
|
+
let normalized = CaptureResolutionSupport.normalizedCaptureTargetResolution(configuredWidth: 1080, configuredHeight: 1920)
|
|
44
|
+
XCTAssertEqual(normalized.width, 1920)
|
|
45
|
+
XCTAssertEqual(normalized.height, 1080)
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
func testPhotoMaxDimensionsSelectsSupportedValueWithinCap() {
|
|
49
|
+
let deviceMax = CMVideoDimensions(width: 4032, height: 3024)
|
|
50
|
+
let supported = [
|
|
51
|
+
CMVideoDimensions(width: 4032, height: 3024),
|
|
52
|
+
CMVideoDimensions(width: 1920, height: 1080),
|
|
53
|
+
CMVideoDimensions(width: 1280, height: 720)
|
|
54
|
+
]
|
|
55
|
+
let dimensions = CaptureResolutionSupport.photoMaxDimensions(
|
|
56
|
+
configuredWidth: 1080,
|
|
57
|
+
configuredHeight: 1920,
|
|
58
|
+
supportedDimensions: supported,
|
|
59
|
+
deviceMax: deviceMax
|
|
60
|
+
)
|
|
61
|
+
XCTAssertEqual(dimensions.width, 1920)
|
|
62
|
+
XCTAssertEqual(dimensions.height, 1080)
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
func testPhotoMaxDimensionsClampsToConfiguredCapWhenNoSupportedSizeFits() {
|
|
66
|
+
let deviceMax = CMVideoDimensions(width: 4032, height: 3024)
|
|
67
|
+
let supported = [
|
|
68
|
+
CMVideoDimensions(width: 4032, height: 3024),
|
|
69
|
+
CMVideoDimensions(width: 1920, height: 1080)
|
|
70
|
+
]
|
|
71
|
+
let dimensions = CaptureResolutionSupport.photoMaxDimensions(
|
|
72
|
+
configuredWidth: 640,
|
|
73
|
+
configuredHeight: 480,
|
|
74
|
+
supportedDimensions: supported,
|
|
75
|
+
deviceMax: deviceMax
|
|
76
|
+
)
|
|
77
|
+
XCTAssertEqual(dimensions.width, 640)
|
|
78
|
+
XCTAssertEqual(dimensions.height, 480)
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
func testParseCaptureResolutionRejectsInt32Overflow() {
|
|
82
|
+
let overflow = CaptureResolutionSupport.parseStartCaptureResolution([
|
|
83
|
+
"width": Int64(Int32.max) + 1,
|
|
84
|
+
"height": 1080
|
|
85
|
+
])
|
|
86
|
+
XCTAssertTrue(overflow.isError)
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
func testShouldEnableHighResolutionPhotoWhenCaptureResolutionSet() {
|
|
90
|
+
XCTAssertFalse(
|
|
91
|
+
CaptureResolutionSupport.shouldEnableHighResolutionPhoto(
|
|
92
|
+
captureResolutionWidth: 1920,
|
|
93
|
+
captureResolutionHeight: 1080,
|
|
94
|
+
captureMaxWidth: nil,
|
|
95
|
+
captureMaxHeight: nil
|
|
96
|
+
)
|
|
97
|
+
)
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
func testShouldEnableHighResolutionPhotoWhenCaptureMaxDimensionsLarge() {
|
|
101
|
+
XCTAssertTrue(
|
|
102
|
+
CaptureResolutionSupport.shouldEnableHighResolutionPhoto(
|
|
103
|
+
captureResolutionWidth: nil,
|
|
104
|
+
captureResolutionHeight: nil,
|
|
105
|
+
captureMaxWidth: 2048,
|
|
106
|
+
captureMaxHeight: nil
|
|
107
|
+
)
|
|
108
|
+
)
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
final class CapturePhotoProcessingQueueTests: XCTestCase {
|
|
113
|
+
func testProcessingQueueIsNotMainThread() {
|
|
114
|
+
let expectation = expectation(description: "background processing")
|
|
115
|
+
var ranOnMain = true
|
|
116
|
+
CapturePhotoProcessingQueue.async {
|
|
117
|
+
ranOnMain = Thread.isMainThread
|
|
118
|
+
expectation.fulfill()
|
|
119
|
+
}
|
|
120
|
+
waitForExpectations(timeout: 5)
|
|
121
|
+
XCTAssertFalse(ranOnMain)
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
func testIsCurrentQueueOnlyOnProcessingQueue() {
|
|
125
|
+
XCTAssertFalse(CapturePhotoProcessingQueue.isCurrentQueue)
|
|
126
|
+
let expectation = expectation(description: "queue marker")
|
|
127
|
+
CapturePhotoProcessingQueue.async {
|
|
128
|
+
XCTAssertTrue(CapturePhotoProcessingQueue.isCurrentQueue)
|
|
129
|
+
expectation.fulfill()
|
|
130
|
+
}
|
|
131
|
+
waitForExpectations(timeout: 5)
|
|
132
|
+
}
|
|
133
|
+
}
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import XCTest
|
|
2
|
+
@testable import CapgoCameraPreview
|
|
3
|
+
|
|
4
|
+
final class ImageIOJpegEncodingTests: XCTestCase {
|
|
5
|
+
func testApplyingCompressionQualitySetsImageIODestinationKey() {
|
|
6
|
+
let properties = ImageIOJpegEncoding.applyingCompressionQuality(42, to: [:])
|
|
7
|
+
let quality = properties[kCGImageDestinationLossyCompressionQuality as String] as? CGFloat
|
|
8
|
+
XCTAssertEqual(quality ?? -1, 0.42, accuracy: 0.0001)
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
func testEncodeProducesSmallerOutputAtLowerQuality() throws {
|
|
12
|
+
let size = CGSize(width: 800, height: 600)
|
|
13
|
+
let renderer = UIGraphicsImageRenderer(size: size)
|
|
14
|
+
let image = renderer.image { context in
|
|
15
|
+
UIColor.red.setFill()
|
|
16
|
+
context.fill(CGRect(origin: .zero, size: size))
|
|
17
|
+
UIColor.blue.setFill()
|
|
18
|
+
context.fill(CGRect(x: 100, y: 100, width: 600, height: 400))
|
|
19
|
+
}
|
|
20
|
+
guard let cgImage = image.cgImage else {
|
|
21
|
+
XCTFail("Failed to create test CGImage")
|
|
22
|
+
return
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
let sourceJPEG = image.jpegData(compressionQuality: 1.0)
|
|
26
|
+
XCTAssertNotNil(sourceJPEG)
|
|
27
|
+
guard let sourceJPEG,
|
|
28
|
+
let imageSource = CGImageSourceCreateWithData(sourceJPEG as CFData, nil),
|
|
29
|
+
let metadata = CGImageSourceCopyPropertiesAtIndex(imageSource, 0, nil) as? [String: Any] else {
|
|
30
|
+
XCTFail("Failed to read JPEG metadata for test fixture")
|
|
31
|
+
return
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
guard let lowQualityData = ImageIOJpegEncoding.encode(cgImage: cgImage, properties: metadata, qualityPercent: 20),
|
|
35
|
+
let highQualityData = ImageIOJpegEncoding.encode(cgImage: cgImage, properties: metadata, qualityPercent: 95) else {
|
|
36
|
+
XCTFail("ImageIO JPEG encode failed")
|
|
37
|
+
return
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
XCTAssertLessThan(lowQualityData.count, highQualityData.count)
|
|
41
|
+
}
|
|
42
|
+
}
|
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
import AVFoundation
|
|
2
|
+
import XCTest
|
|
3
|
+
@testable import CapgoCameraPreview
|
|
4
|
+
|
|
5
|
+
final class PhysicalOrientationResolverTests: XCTestCase {
|
|
6
|
+
private func accel(x: Double, y: Double, z: Double) -> PhysicalOrientationResolver.Acceleration {
|
|
7
|
+
PhysicalOrientationResolver.Acceleration(x: x, y: y, z: z)
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
func testNearFlatUsesFallbackWhenNoLastReliable() {
|
|
11
|
+
let result = PhysicalOrientationResolver.resolve(
|
|
12
|
+
acceleration: accel(x: 0.04, y: -0.03, z: -0.99),
|
|
13
|
+
lastReliableOrientation: nil,
|
|
14
|
+
fallbackOrientation: .portrait
|
|
15
|
+
)
|
|
16
|
+
XCTAssertEqual(result.orientation, .portrait)
|
|
17
|
+
XCTAssertFalse(result.isReliable)
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
func testNearFlatKeepsLastReliableOrientation() {
|
|
21
|
+
let result = PhysicalOrientationResolver.resolve(
|
|
22
|
+
acceleration: accel(x: 0.05, y: 0.02, z: 0.92),
|
|
23
|
+
lastReliableOrientation: .landscapeLeft,
|
|
24
|
+
fallbackOrientation: .portrait
|
|
25
|
+
)
|
|
26
|
+
XCTAssertEqual(result.orientation, .landscapeLeft)
|
|
27
|
+
XCTAssertFalse(result.isReliable)
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
func testClearPortrait() {
|
|
31
|
+
let result = PhysicalOrientationResolver.resolve(
|
|
32
|
+
acceleration: accel(x: 0.08, y: -0.95, z: 0.15),
|
|
33
|
+
lastReliableOrientation: nil,
|
|
34
|
+
fallbackOrientation: .portrait
|
|
35
|
+
)
|
|
36
|
+
XCTAssertEqual(result.orientation, .portrait)
|
|
37
|
+
XCTAssertTrue(result.isReliable)
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
func testClearLandscapeLeft() {
|
|
41
|
+
let result = PhysicalOrientationResolver.resolve(
|
|
42
|
+
acceleration: accel(x: 0.92, y: 0.08, z: 0.05),
|
|
43
|
+
lastReliableOrientation: nil,
|
|
44
|
+
fallbackOrientation: .portrait
|
|
45
|
+
)
|
|
46
|
+
XCTAssertEqual(result.orientation, .landscapeLeft)
|
|
47
|
+
XCTAssertTrue(result.isReliable)
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
func testClearLandscapeRight() {
|
|
51
|
+
let result = PhysicalOrientationResolver.resolve(
|
|
52
|
+
acceleration: accel(x: -0.9, y: 0.1, z: 0.05),
|
|
53
|
+
lastReliableOrientation: nil,
|
|
54
|
+
fallbackOrientation: .portrait
|
|
55
|
+
)
|
|
56
|
+
XCTAssertEqual(result.orientation, .landscapeRight)
|
|
57
|
+
XCTAssertTrue(result.isReliable)
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
func testUpsideDownPortrait() {
|
|
61
|
+
let result = PhysicalOrientationResolver.resolve(
|
|
62
|
+
acceleration: accel(x: 0.05, y: 0.94, z: 0.1),
|
|
63
|
+
lastReliableOrientation: nil,
|
|
64
|
+
fallbackOrientation: .portrait
|
|
65
|
+
)
|
|
66
|
+
XCTAssertEqual(result.orientation, .portraitUpsideDown)
|
|
67
|
+
XCTAssertTrue(result.isReliable)
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
func testAmbiguousAxisUsesLastReliable() {
|
|
71
|
+
let result = PhysicalOrientationResolver.resolve(
|
|
72
|
+
acceleration: accel(x: 0.52, y: -0.48, z: 0.2),
|
|
73
|
+
lastReliableOrientation: .portrait,
|
|
74
|
+
fallbackOrientation: .landscapeLeft
|
|
75
|
+
)
|
|
76
|
+
XCTAssertEqual(result.orientation, .portrait)
|
|
77
|
+
XCTAssertFalse(result.isReliable)
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
func testWeakHorizontalGravityTreatedAsFlat() {
|
|
81
|
+
let result = PhysicalOrientationResolver.resolve(
|
|
82
|
+
acceleration: accel(x: 0.12, y: -0.18, z: 0.55),
|
|
83
|
+
lastReliableOrientation: .landscapeRight,
|
|
84
|
+
fallbackOrientation: .portrait
|
|
85
|
+
)
|
|
86
|
+
XCTAssertEqual(result.orientation, .landscapeRight)
|
|
87
|
+
XCTAssertFalse(result.isReliable)
|
|
88
|
+
}
|
|
89
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@capgo/camera-preview",
|
|
3
|
-
"version": "8.12.
|
|
3
|
+
"version": "8.12.5",
|
|
4
4
|
"description": "Camera preview",
|
|
5
5
|
"license": "MPL-2.0",
|
|
6
6
|
"repository": {
|
|
@@ -44,7 +44,8 @@
|
|
|
44
44
|
"verify:ios": "xcodebuild -scheme CapgoCameraPreview -destination generic/platform=iOS && bun run test:ios",
|
|
45
45
|
"test:ios": "bash ./scripts/test-ios.sh",
|
|
46
46
|
"verify:android": "cd android && ./gradlew clean build test && cd ..",
|
|
47
|
-
"verify:web": "bun run build",
|
|
47
|
+
"verify:web": "bun run build && bun run test:web",
|
|
48
|
+
"test:web": "bun test src/web.stop.test.ts",
|
|
48
49
|
"lint": "bun run eslint && bun run prettier -- --check && bun run swiftlint -- lint",
|
|
49
50
|
"fmt": "bun run eslint -- --fix && bun run prettier -- --write && bun run swiftlint -- --fix --format",
|
|
50
51
|
"eslint": "eslint .",
|
|
@@ -82,7 +83,8 @@
|
|
|
82
83
|
"rollup": "^4.53.2",
|
|
83
84
|
"swiftlint": "^2.0.0",
|
|
84
85
|
"typescript": "^6.0.3",
|
|
85
|
-
"prettier-pretty-check": "^0.2.0"
|
|
86
|
+
"prettier-pretty-check": "^0.2.0",
|
|
87
|
+
"happy-dom": "^20.0.0"
|
|
86
88
|
},
|
|
87
89
|
"peerDependencies": {
|
|
88
90
|
"@capacitor/core": ">=8.0.0"
|