// The Camera tool's shot pipeline over a fixture image: the simulator has no // camera, so the surface's half is replaced by a JPEG written here, and the // core and the camera roll by recorders. import CoreGraphics import CottonKit import Foundation import ImageIO import UniformTypeIdentifiers import XCTest final class ShotPipelineTests: XCTestCase { private actor Shooter: CameraShooting { var shots: [URL] = [] func shoot(file url: URL, mime: String, kind: CaptureMediaKind, filename: String?) async throws -> MediaCaptured { XCTAssertTrue(FileManager.default.fileExists(atPath: url.path), "the shot is in Cotton before its file goes") shots.append(url) return MediaCaptured(key: "k/\(shots.count)", recordId: "photo:\(shots.count)") } } private actor Roll: CameraRoll { var saved: [URL] = [] var failing = false func fail() { failing = true } func save(file: URL, kind: CaptureMediaKind) async throws { if failing { throw CocoaError(.fileWriteUnknown) } saved.append(file) } } private func fixture(_ n: Int) throws -> URL { let url = FileManager.default.temporaryDirectory.appendingPathComponent("shot-fixture-\(UUID().uuidString)-\(n).jpg") let context = try XCTUnwrap(CGContext( data: nil, width: 8, height: 8, bitsPerComponent: 8, bytesPerRow: 0, space: CGColorSpaceCreateDeviceRGB(), bitmapInfo: CGImageAlphaInfo.noneSkipLast.rawValue)) context.setFillColor(CGColor(red: 0.9, green: 0.85, blue: 0.7, alpha: 1)) context.fill(CGRect(x: 0, y: 0, width: 8, height: 8)) let image = try XCTUnwrap(context.makeImage()) let destination = try XCTUnwrap(CGImageDestinationCreateWithURL(url as CFURL, UTType.jpeg.identifier as CFString, 1, nil)) CGImageDestinationAddImage(destination, image, nil) XCTAssertTrue(CGImageDestinationFinalize(destination)) return url } /// RULE: With the camera roll switch off, a shot is saved into Cotton and nothing is written to the camera roll. func testWithTheRollOffNothingReachesTheCameraRoll() async throws { let shooter = Shooter(), roll = Roll() let pipeline = ShotPipeline(shooter: shooter, roll: roll, mayAddToRoll: { XCTFail("the roll is never asked for while off"); return true }) let file = try fixture(1) let taken = try await pipeline.take(file: file, kind: .photo, saveToRoll: false) XCTAssertEqual(taken.roll, .off) XCTAssertEqual(taken.captured.recordId, "photo:1") let saved = await roll.saved XCTAssertTrue(saved.isEmpty) let shots = await shooter.shots XCTAssertEqual(shots.count, 1) XCTAssertFalse(FileManager.default.fileExists(atPath: file.path), "the staged file goes") } /// RULE: A camera roll that refuses or fails never loses the shot: it is already in Cotton and the answer says the roll did not take it. func testARefusingRollKeepsTheShotInCotton() async throws { let shooter = Shooter(), roll = Roll() let refused = try await ShotPipeline(shooter: shooter, roll: roll, mayAddToRoll: { false }) .take(file: try fixture(1), kind: .photo, saveToRoll: true) XCTAssertEqual(refused.roll, .refused) await roll.fail() let failed = try await ShotPipeline(shooter: shooter, roll: roll, mayAddToRoll: { true }) .take(file: try fixture(2), kind: .photo, saveToRoll: true) XCTAssertEqual(failed.roll, .failed) let unreachable = try await ShotPipeline(shooter: shooter, roll: nil, mayAddToRoll: { true }) .take(file: try fixture(3), kind: .photo, saveToRoll: true) XCTAssertEqual(unreachable.roll, .unreachable) let shots = await shooter.shots XCTAssertEqual(shots.count, 3) } }