// The party: the turntable in the party's look, on the dance floor under the // drifting lights. Its record, arm, turn and gestures are the turntable's // (`SongTurntableTests` holds them); here, what is its own: its label's // words, red name and cream capitals, set once into a picture of their own; // its label inside the run-out groove; its ground, the floor with no lamp and // the lights drifting half a point a second and wrapping, still under Reduce // Motion; and its specimens. With `COTTON_FACE_SHOTS=` set, the party in // each state is written there beside the face shots. @testable import CottonFaces import CottonPaper import CottonSpecimens import CottonValues import SwiftUI import XCTest #if canImport(UIKit) import UIKit #else import AppKit #endif @MainActor final class SongPartyTests: XCTestCase { private typealias Knobs = SongPartyFaceKnobs /// RULE: The party's label says the card's words: the song's name in capitals above the spindle, its artist and its album in capitals under it; nothing the card does not hold. func testTheLabelIsTheCards() { let harbour = SongPartyFaceCatalogue.harbour XCTAssertEqual(PartyLabel.title(harbour), "HARBOUR LIGHTS") XCTAssertEqual(PartyLabel.artist(harbour), "THE LOW TIDE") XCTAssertEqual(PartyLabel.album(harbour), "NIGHT FERRY") let bare = DeckCard(face: "song.party", corner: nil, pressing: false, pace: nil, plate: nil, fields: [:]) XCTAssertNil(PartyLabel.title(bare)) XCTAssertNil(PartyLabel.artist(bare)) XCTAssertNil(PartyLabel.album(bare)) XCTAssertEqual(TurntableDeck.times(134, of: 242), ["2:14", "\u{2212}1:48", "4:02"], "the turntable's three times at its foot") } /// RULE: The name is printed in the label's red and the artist and album in its cream, each read off the painted rings; the floor's colour is its tile's. func testItsInksAreTheRings() { XCTAssertNotEqual(MusicMaterial.partyRed.colour, MusicMaterial.partyCream.colour) XCTAssertNotEqual(MusicMaterial.floor.colour, MusicMaterial.tolex.colour) XCTAssertEqual(FaceGround.partyFloor.colour, MusicMaterial.floor.colour) } /// RULE: The party's label lies over the record's middle, centred on the spindle, its painted round inside the run-out groove; its name above the spindle hole and its other words under it, all inside the rings; a cover goes round just inside the red ring. func testTheLabelLiesInsideTheRunOut() { let record = SongTurntableFaceKnobs.self XCTAssertEqual(Knobs.label.midX, record.centre.x, accuracy: 1e-9) XCTAssertEqual(Knobs.label.midY, record.centre.y, accuracy: 1e-9) let round = Knobs.round * Knobs.label.width / Knobs.painted XCTAssertLessThan(Double(round), record.grooves.inner, "the label lies inside the run-out groove") XCTAssertLessThan(Knobs.coverRound, 456, "a cover lies inside the red ring") let centre = Knobs.painted / 2 XCTAssertLessThan(Knobs.titleBox.maxY, centre - Knobs.hole, "the name is above the spindle") for box in [Knobs.artistBox, Knobs.albumBox, Knobs.timesBox] { XCTAssertGreaterThan(box.minY, centre + Knobs.hole, "under the spindle") } for box in [Knobs.titleBox, Knobs.artistBox, Knobs.albumBox, Knobs.timesBox] { for corner in [CGPoint(x: box.minX, y: box.maxY), CGPoint(x: box.maxX, y: box.maxY)] where corner.y > centre { XCTAssertLessThan(hypot(corner.x - centre, corner.y - centre), Knobs.coverRound, "\(box) lies inside the rings") } } XCTAssertEqual(TurntableLook.party.label.painting, "party-label-blank") XCTAssertEqual(TurntableLook.darkRoom.label.painting, "label-blank") } /// RULE: The party's label is printed once, apart from the turntable's: the same words on the other look's label are another picture. func testTheLabelIsPrintedApart() { let cache = TurntableLabelPrintCache() var renders = 0 let words = TurntableDeck.label(SongPartyFaceCatalogue.harbour) let render = { () -> CGImage? in renders += 1 let renderer = ImageRenderer(content: PartyLabelWords(card: SongPartyFaceCatalogue.harbour, side: 128)) renderer.scale = 2 return renderer.cgImage } let party = TurntableLabelPrintCache.Key(words: words, covered: false, side: 128, scale: 2, look: .party) XCTAssertEqual(cache.image(for: party, render: render)?.width, 256) _ = cache.image(for: party, render: render) XCTAssertEqual(renders, 1, "set once") _ = cache.image(for: TurntableLabelPrintCache.Key(words: words, covered: false, side: 128, scale: 2, look: .darkRoom), render: render) XCTAssertEqual(renders, 2, "the dark room's label is another picture") } /// RULE: The party's floor is laid with its painted tile and no lamp; its lights lie over it and drift half a point a second, one whole tile a lap, so they wrap without a seam; they lie still under Reduce Motion. The tolex has no drifting overlay and its lamp lies still. func testTheLightsDrift() throws { XCTAssertEqual(FaceGround.partyFloor.tile, "floor") XCTAssertNil(FaceGround.partyFloor.lamp) let lights = try XCTUnwrap(FaceGround.partyFloor.overlay) XCTAssertEqual(lights.name, "party-lights") XCTAssertEqual(Double(lights.speed), 0.5, accuracy: 1e-9) XCTAssertEqual(Double(hypot(lights.travel.width, lights.travel.height)) / lights.lap, 0.5, accuracy: 1e-9, "half a point a second") XCTAssertEqual(lights.travel.width.truncatingRemainder(dividingBy: FaceGroundOverlay.side.width), 0, accuracy: 1e-9, "a whole tile a lap") XCTAssertEqual(lights.travel.height.truncatingRemainder(dividingBy: FaceGroundOverlay.side.height), 0, accuracy: 1e-9) XCTAssertEqual(lights.offset(after: 0), .zero) XCTAssertEqual(Double(lights.offset(after: 10).width), 5, accuracy: 1e-9) XCTAssertEqual(Double(lights.offset(after: lights.lap + 10).width), 5, accuracy: 1e-6, "it wraps") XCTAssertEqual(lights.offset(after: 10, reduceMotion: true), .zero, "still under Reduce Motion") XCTAssertNil(FaceGround.tolex.overlay) for ground in FaceGround.allCases where ground != .partyFloor { XCTAssertNil(ground.overlay, "\(ground) has nothing drifting") } for name in ["floor", "party-lights"] { XCTAssertNotNil(CottonMedia.image(name, scale: 3), name) } } /// RULE: The party is registered, printed on no stock, laid on the dance floor, and its paintings are in the catalogue — the record, the arm and the jewel the turntable's. func testItStandsOnTheFloor() { XCTAssertNotNil(FaceRegistry.all.first { $0.id == "song.party" }) XCTAssertEqual(FaceRegistry.ground("song.party"), .partyFloor) XCTAssertEqual(FaceRegistry.stock("song.party"), .own) for name in ["party-label-blank", "party-glow", "slipmat", "record", "spindle", "tonearm", "tonearm-shadow", "hifi-jewel-on", "hifi-jewel-off"] { XCTAssertNotNil(CottonMedia.image(name), name) } } /// RULE: The specimens are the turntable's: playing at 2:14 of 4:02 with the cover, paused, playing with no cover, and the longest words, each under the player's word. func testTheSpecimens() { let byName = Dictionary(uniqueKeysWithValues: SongPartyFaceCatalogue.face.specimens.map { ($0.name, $0) }) XCTAssertEqual(Set(byName.keys), Set(SongTurntableFaceCatalogue.face.specimens.map(\.name))) XCTAssertEqual(byName["playing"]?.play.map(TurntableDeck.state), .playing) XCTAssertEqual(byName["playing"]?.play?.elapsed, 134) XCTAssertNotNil(byName["playing"]?.picture) XCTAssertEqual(byName["paused"]?.play.map(TurntableDeck.state), .paused) XCTAssertNil(byName["no cover"]?.picture) XCTAssertNotNil(byName["largest"]) } /// RULE: The party draws playing, paused, with no cover and with the longest words, from its card and the player's word alone, at every text size, on its floor. func testItDrawsInEveryState() throws { let out = ProcessInfo.processInfo.environment["COTTON_FACE_SHOTS"].flatMap { $0.isEmpty ? nil : URL(fileURLWithPath: $0, isDirectory: true) } if let out { try FileManager.default.createDirectory(at: out, withIntermediateDirectories: true) } let states: [(String, DeckCard, Bool, TrackPlay)] = [ ("playing", SongPartyFaceCatalogue.harbour, true, TrackPlay(held: true, playing: true, elapsed: 134, duration: 242)), ("paused", SongPartyFaceCatalogue.harbour, true, TrackPlay(held: true, playing: false, elapsed: 134, duration: 242)), ("no cover", SongPartyFaceCatalogue.train, false, TrackPlay(held: true, playing: true, elapsed: 61, duration: 197)), ("largest", SongPartyFaceCatalogue.largest, false, TrackPlay(held: true, playing: false, elapsed: 312, duration: 669)), ("still", SongPartyFaceCatalogue.harbour, false, .still), ] for size in [DynamicTypeSize.xSmall, .large, .accessibility5] { for (name, card, covered, play) in states { let cover = covered ? FacePicture(key: "poster-ferry", edge: 120) : nil let renderer = ImageRenderer(content: SongTurntableFace(card: card, cover: cover, play: play, toggle: {}, look: .party) .frame(width: 340, height: 451) .background(FaceGroundSurface(.partyFloor)) .environment(\.facePictureDrawer, FixturePictures.drawer) .dynamicTypeSize(size) .themed(.paper)) renderer.scale = 2 let image = try XCTUnwrap(renderer.cgImage, "the party \(name) drew nothing") if let out { try write(image, to: out.appendingPathComponent("song.party-\(name)-\(size).png")) } } } } private func write(_ image: CGImage, to url: URL) throws { #if canImport(UIKit) let data = UIImage(cgImage: image).pngData() #else let data = NSBitmapImageRep(cgImage: image).representation(using: .png, properties: [:]) #endif try XCTUnwrap(data, "\(url.lastPathComponent) was not encoded").write(to: url) } }