Cotton code
43 lines · 2.0 KB · SwiftRaw
import ImageIOimport SwiftUIimport UniformTypeIdentifiersimport XCTest@testable import CottonUI/// A capture utility, not a guard: with `COTTON_FACE_SHOTS=<dir>` set (through/// xcodebuild, `TEST_RUNNER_COTTON_FACE_SHOTS`) it draws the gallery's deck/// specimens — each face on its stock at the deck's card size, as the deck/// lays the front card — into `<dir>` as PNGs at the phone's scale, so a face/// is looked at before it ships. Without the variable it skips.@MainActorfinal class FaceShotsTests: XCTestCase {    func testDrawTheDeckSpecimens() throws {        guard let dir = ProcessInfo.processInfo.environment["COTTON_FACE_SHOTS"], !dir.isEmpty else {            throw XCTSkip("set COTTON_FACE_SHOTS=<dir> to draw the specimens")        }        let out = URL(fileURLWithPath: dir, isDirectory: true)        try FileManager.default.createDirectory(at: out, withIntermediateDirectories: true)        let faces = GalleryFaces.self        shoot("session", faces.sessionCards, to: out)        shoot("session-tasks", faces.stage(faces.card, SessionFaceCatalogue.runningSession) {            SessionFace(card: SessionFaceCatalogue.runningSession, pages: faces.runningTasks, opened: 1)        }, to: out)        shoot("memory", faces.memoryCards, to: out)        shoot("mood", faces.moodCards, to: out)        shoot("meeting", faces.meetingCards, to: out)    }    private func shoot(_ name: String, _ view: some View, to out: URL) {        let renderer = ImageRenderer(content: view            .environment(\.colorScheme, .light)            .frame(width: 393)            .background(Color(white: 0.93)))        renderer.scale = 2        guard let image = renderer.cgImage,              let dest = CGImageDestinationCreateWithURL(out.appendingPathComponent("\(name).png") as CFURL, UTType.png.identifier as CFString, 1, nil)        else { return XCTFail("\(name) drew nothing") }        CGImageDestinationAddImage(dest, image, nil)        XCTAssertTrue(CGImageDestinationFinalize(dest), "\(name) was not written")    }}