Cotton code
104 lines · 5.9 KB · SwiftRaw
// The in-call telephone: the handset lifted while the call is live and back// on its cradle once it is over; each line key's glyph by the call's state,// lit when it is on and the red key never; each key says its own words in// the cards set's lines; a glyph is laid exactly on its key's painted face;// the face draws in every state it is reviewed in, on the calling desk's// leather.@testable import CottonFacesimport CottonPaperimport CottonValuesimport SwiftUIimport XCTest#if canImport(UIKit)    import UIKit#else    import AppKit#endif@MainActorfinal class CallTelephoneTests: XCTestCase {    typealias Catalogue = CallTelephoneCatalogue    /// RULE: The handset is lifted off the cradle while the call is live and replaced once it is over, and both paintings are in the catalogue.    func testTheHandsetIsLiftedWhileTheCallIsLive() {        XCTAssertEqual(CallTelephoneSetting.painting(Catalogue.ringing), "telephone-lifted")        XCTAssertEqual(CallTelephoneSetting.painting(Catalogue.talking), "telephone-lifted")        XCTAssertEqual(CallTelephoneSetting.painting(Catalogue.ended), "telephone-front")        XCTAssertNotNil(CottonMedia.image("telephone-lifted", scale: 3))        XCTAssertNotNil(CottonMedia.image("telephone-front", scale: 3))    }    /// RULE: A key that is on glows — mute when muted, speaker when on the speaker, camera when this phone's camera is on — and the red key that ends the call never; the mute and camera keys' glyphs say their state.    func testAKeyThatIsOnGlows() {        let talking = Catalogue.talking        XCTAssertTrue(CallTelephoneKey.mute.lit(talking))        XCTAssertTrue(CallTelephoneKey.speaker.lit(talking))        XCTAssertFalse(CallTelephoneKey.video.lit(talking))        XCTAssertFalse(CallTelephoneKey.end.lit(talking))        XCTAssertEqual(CallTelephoneKey.mute.glyph(talking), "mic.slash.fill")        XCTAssertEqual(CallTelephoneKey.mute.glyph(Catalogue.ringing), "mic.fill")        XCTAssertEqual(CallTelephoneKey.video.glyph(Catalogue.video), "video.fill")        XCTAssertEqual(CallTelephoneKey.video.glyph(talking), "video.slash.fill")        XCTAssertEqual(CallTelephoneKey.end.glyph(talking), "phone.down.fill")        XCTAssertTrue(CallTelephoneKey.allCases.allSatisfy { !$0.lit(Catalogue.ringing) })    }    /// RULE: Every key says its own words, the calling set's line and never its path: the mute key what a tap does, the speaker and the camera themselves and whether they are on.    func testEveryKeySaysItsWords() {        let ringing = Catalogue.ringing, talking = Catalogue.talking        XCTAssertEqual(CallTelephoneKey.allCases.map { $0.words(ringing) }, ["Mute", "Speaker", "Camera", "End call"])        XCTAssertEqual(CallTelephoneKey.mute.words(talking), "Unmute")        XCTAssertTrue(CallTelephoneKey.speaker.selected(talking))        XCTAssertFalse(CallTelephoneKey.mute.selected(talking), "the mute key says its state in its words")        XCTAssertTrue(CallTelephoneKey.video.selected(Catalogue.video))        XCTAssertFalse(CallTelephoneKey.end.selected(talking))    }    /// RULE: A key's glyph is laid on its painted face: the box's corners land on the face's upper left, upper right and lower left corners.    func testAGlyphLiesOnItsKeysFace() {        let size = CGSize(width: 390, height: 390 / CallTelephoneKnobs.aspect)        for key in CallTelephoneKey.allCases {            let face = key.face(in: size)            let box = key.box(in: size)            let map = key.onFace(box, in: size)            for (point, corner) in [(CGPoint.zero, face[0]), (CGPoint(x: box.width, y: 0), face[1]), (CGPoint(x: 0, y: box.height), face[3])] {                let laid = point.applying(map)                XCTAssertEqual(laid.x, corner.x, accuracy: 0.001, "\(key)")                XCTAssertEqual(laid.y, corner.y, accuracy: 0.001, "\(key)")            }            XCTAssertLessThan(face[0].x, face[1].x, "\(key) runs left to right")        }        let middles = CallTelephoneKey.allCases.map { key in key.face(in: size).map(\.x).reduce(0, +) / 4 }        XCTAssertEqual(middles, middles.sorted(), "the keys stand mute, speaker, camera, end from the left")    }    /// RULE: A print with no picture is typed with the name's initials, its first word's and its last's.    func testAPrintWithNoPictureCarriesTheInitials() {        XCTAssertEqual(CallTelephoneSetting.initials("Mira Kaul"), "MK")        XCTAssertEqual(CallTelephoneSetting.initials("Arundhati Venkatalakshmi Krishnamurthy"), "AK")        XCTAssertEqual(CallTelephoneSetting.initials("Ravi"), "R")        XCTAssertEqual(CallTelephoneSetting.initials("+1 555 0199"), "")    }    /// RULE: The in-call telephone draws in every state it is reviewed in, with a picture, without one, and with both sides' video, on the calling desk's leather.    func testItDrawsInEveryState() throws {        let out = ProcessInfo.processInfo.environment["COTTON_FACE_SHOTS"].flatMap { $0.isEmpty ? nil : URL(fileURLWithPath: $0, isDirectory: true) }        let portrait = FixturePictures.image("portrait")        for (name, state) in Catalogue.states {            let remote = AnyView(LinearGradient(colors: [CallingMaterial.cream.colour, CallingMaterial.cardInk.colour], startPoint: .top, endPoint: .bottom))            let local = AnyView(CallingMaterial.rule.colour)            let drawn = CallTelephoneFace(state: state, picture: name == "ringing" ? nil : portrait, remote: remote, local: local, acts: .none)                .frame(width: 390, height: 844)            let renderer = ImageRenderer(content: drawn)            renderer.scale = 2            let image = try XCTUnwrap(renderer.cgImage, name)            if let out {                #if canImport(AppKit)                    let png = NSBitmapImageRep(cgImage: image).representation(using: .png, properties: [:])                    try png?.write(to: out.appendingPathComponent("call.telephone-\(name).png"))                #endif            }        }    }}