|
| 1 | +import AVFoundation |
| 2 | +import CoreMedia |
| 3 | +import Testing |
| 4 | +@testable import OpenClaw |
| 5 | + |
| 6 | +struct CameraDeviceResolverTests { |
| 7 | + @Test func `external portrait photos select the transposed format with the same encoding`() throws { |
| 8 | + let portrait = try Self.format(width: 1080, height: 1920) |
| 9 | + let formats = try [ |
| 10 | + Self.format(width: 1920, height: 1080, subtype: kCVPixelFormatType_422YpCbCr8_yuvs), |
| 11 | + Self.format(width: 3840, height: 2160), |
| 12 | + Self.format(width: 1920, height: 1080), |
| 13 | + ] |
| 14 | + #expect(CameraDeviceResolver.landscapePhotoFormatIndex( |
| 15 | + deviceType: .external, activeFormat: portrait, formats: formats) == 2) |
| 16 | + } |
| 17 | + |
| 18 | + @Test(arguments: [AVCaptureDevice.DeviceType.builtInWideAngleCamera, .continuityCamera]) |
| 19 | + func `non-external cameras retain portrait formats`(deviceType: AVCaptureDevice.DeviceType) throws { |
| 20 | + #expect(try CameraDeviceResolver.landscapePhotoFormatIndex( |
| 21 | + deviceType: deviceType, |
| 22 | + activeFormat: Self.format(width: 1080, height: 1920), |
| 23 | + formats: [Self.format(width: 1920, height: 1080)]) == nil) |
| 24 | + } |
| 25 | + |
| 26 | + @Test(arguments: [(1920, 1080), (1552, 1552), (1088, 1920)]) |
| 27 | + func `landscape square and unmatched portrait formats remain unchanged`(width: Int32, height: Int32) throws { |
| 28 | + #expect(try CameraDeviceResolver.landscapePhotoFormatIndex( |
| 29 | + deviceType: .external, |
| 30 | + activeFormat: Self.format(width: width, height: height), |
| 31 | + formats: [ |
| 32 | + Self.format(width: height, height: width, subtype: kCVPixelFormatType_422YpCbCr8_yuvs), |
| 33 | + Self.format(width: 1920, height: 1080), |
| 34 | + ]) == nil) |
| 35 | + } |
| 36 | + |
| 37 | + @Test func `external cameras without a matching encoding or format remain unchanged`() throws { |
| 38 | + let portrait = try Self.format(width: 1080, height: 1920) |
| 39 | + let wrongEncoding = try Self.format(width: 1920, height: 1080, subtype: kCVPixelFormatType_422YpCbCr8_yuvs) |
| 40 | + for formats in [[], [wrongEncoding]] { |
| 41 | + #expect(CameraDeviceResolver.landscapePhotoFormatIndex( |
| 42 | + deviceType: .external, activeFormat: portrait, formats: formats) == nil) |
| 43 | + } |
| 44 | + } |
| 45 | + |
| 46 | + private static func format( |
| 47 | + width: Int32, |
| 48 | + height: Int32, |
| 49 | + subtype: FourCharCode = kCVPixelFormatType_420YpCbCr8BiPlanarVideoRange) throws -> CMFormatDescription |
| 50 | + { |
| 51 | + var description: CMVideoFormatDescription? |
| 52 | + let result = CMVideoFormatDescriptionCreate( |
| 53 | + allocator: kCFAllocatorDefault, |
| 54 | + codecType: subtype, |
| 55 | + width: width, |
| 56 | + height: height, |
| 57 | + extensions: nil, |
| 58 | + formatDescriptionOut: &description) |
| 59 | + #expect(result == noErr) |
| 60 | + return try #require(description) |
| 61 | + } |
| 62 | +} |
0 commit comments