// The turntable and the songs it plays: it stands playing or paused by the // player's word alone; its arm turns from the outer groove to the run-out by // the time played over the song's length, its needle on the groove, and // lifts while paused; its record turns 33⅓ times a minute while playing, // comes to rest upright, turns on from there and never under Reduce Motion; // its label's words are the card's, set once into a picture; its other words // are the lines'. With `COTTON_FACE_SHOTS=` set, the turntable 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 SongTurntableTests: XCTestCase { private typealias Knobs = SongTurntableFaceKnobs private let playing = TrackPlay(held: true, playing: true, elapsed: 134, duration: 242) private let paused = TrackPlay(held: true, playing: false, elapsed: 134, duration: 242) /// RULE: The turntable plays while the player plays or fetches the song, and stands paused while it holds it paused or holds none of it. func testItStandsByThePlayersWord() { XCTAssertEqual(TurntableDeck.state(.still), .paused) XCTAssertEqual(TurntableDeck.state(playing), .playing) XCTAssertEqual(TurntableDeck.state(TrackPlay(held: true, loading: true)), .playing) XCTAssertEqual(TurntableDeck.state(paused), .paused) } /// RULE: The arm turns inward from the outer groove at nought to the run-out at the song's end, its needle's distance from the centre running straight by the time played — the layout's own sweep and its reading at 2:14 of 4:02. func testTheArmIsTheClock() { XCTAssertEqual(TurntableDeck.arm(0, of: 242), 0, accuracy: 1e-9) XCTAssertEqual(TurntableDeck.arm(242, of: 242), 20.86014283177707, accuracy: 1e-3, "layout.json's tonearmSweep.endDegrees") XCTAssertEqual(TurntableDeck.sweep, 20.86014283177707, accuracy: 1e-3) XCTAssertEqual(TurntableDeck.arm(134, of: 242), 9.717391291810642, accuracy: 1e-3, "layout.json's currentDegrees at 134 of 242") XCTAssertEqual(TurntableDeck.arm(500, of: 242), TurntableDeck.sweep, accuracy: 1e-9) XCTAssertEqual(TurntableDeck.arm(10, of: 0), 0) var last = -1.0 for second in stride(from: 0.0, through: 242, by: 11) { let angle = TurntableDeck.arm(second, of: 242) XCTAssertGreaterThan(angle, last, "the arm only turns inward as the song plays") last = angle } } /// RULE: The needle sits on the groove: on the painted outer groove at nought, on the run-out at the end, at the groove of the share played between. func testTheNeedleSitsOnTheGroove() { let outer = TurntableDeck.needle(0) XCTAssertEqual(outer.x, Knobs.stylus.x, accuracy: 1e-9) XCTAssertEqual(outer.y, Knobs.stylus.y, accuracy: 1e-9) for share in [0.0, 0.25, 0.5537, 1] { let at = TurntableDeck.needle(TurntableDeck.arm(share * 242, of: 242)) let radius = hypot(at.x - Knobs.centre.x, at.y - Knobs.centre.y) XCTAssertEqual(Double(radius), Knobs.grooves.outer + (Knobs.grooves.inner - Knobs.grooves.outer) * share, accuracy: 1e-6) XCTAssertEqual(TurntableDeck.share(atArm: TurntableDeck.arm(share * 242, of: 242)), share, accuracy: 1e-9) } XCTAssertGreaterThan(Double(Knobs.grooves.inner), Double(Knobs.label.width / 2), "the label lies inside the run-out groove") } /// RULE: A drag of the arm seeks by the groove it brings the needle to, the whole sweep the whole song, held to the song. func testADragOfTheArmSeeks() { XCTAssertEqual(TurntableDeck.turned(134, by: 0, of: 242), 134, accuracy: 1e-6) XCTAssertEqual(TurntableDeck.turned(0, by: TurntableDeck.sweep, of: 242), 242, accuracy: 1e-6) XCTAssertEqual(TurntableDeck.turned(100, by: -90, of: 242), 0) XCTAssertEqual(TurntableDeck.turned(100, by: 90, of: 242), 242, accuracy: 1e-6) XCTAssertGreaterThan(TurntableDeck.turned(100, by: 2, of: 242), 100) XCTAssertEqual(TurntableDeck.turned(100, by: 2, of: 0), 0) } /// RULE: Paused, or holding none of the song, the arm is lifted a few degrees back toward its rest; playing, it lies in the groove. func testPausedTheArmLifts() { XCTAssertEqual(TurntableDeck.arm(134, of: 242, lifted: false), TurntableDeck.arm(134, of: 242)) XCTAssertEqual(TurntableDeck.arm(134, of: 242, lifted: true), TurntableDeck.arm(134, of: 242) - Knobs.lift) XCTAssertGreaterThan(Knobs.lift, 0) XCTAssertLessThan(Knobs.lift, 10, "a few degrees") XCTAssertGreaterThan(Knobs.shadowLifted.height, Knobs.shadowDown.height, "the lifted arm's shadow falls further off") } /// RULE: Playing, the record turns 33⅓ times a minute by the time played; paused it rests at its next whole turn, label upright; started again it turns on from where it rested; a seek moves the arm, not the record; under Reduce Motion it never turns. func testTheRecordTurns() { XCTAssertEqual(Knobs.degreesPerSecond * 60 / 360, 100.0 / 3, accuracy: 1e-9, "33⅓ rpm") XCTAssertEqual(TurntableDeck.turn(1.8, playing: true, phase: 0), 360, accuracy: 1e-9, "one turn in 1.8 seconds") XCTAssertEqual(TurntableDeck.turn(134, playing: true, phase: 0), 26_800, accuracy: 1e-9) let rest = TurntableDeck.turn(134, playing: false, phase: 0) XCTAssertEqual(rest.truncatingRemainder(dividingBy: 360), 0, "at rest the label stands upright") XCTAssertGreaterThanOrEqual(rest, 26_800, "it coasts on, never back") XCTAssertLessThan(rest, 26_800 + 360) let phase = TurntableDeck.resumed(134, phase: 0) XCTAssertEqual(TurntableDeck.turn(134, playing: true, phase: phase), rest, accuracy: 1e-9, "it plays on from where it rested") XCTAssertEqual(TurntableDeck.phase(phase, from: 134, to: 134.25), phase, "playing on keeps the phase") let sought = TurntableDeck.phase(phase, from: 134, to: 60) XCTAssertEqual(TurntableDeck.turn(60, playing: true, phase: sought), TurntableDeck.turn(134, playing: true, phase: phase), accuracy: 1e-9, "a seek back does not spin the record") let leapt = TurntableDeck.phase(phase, from: 134, to: 200) XCTAssertEqual(TurntableDeck.turn(200, playing: true, phase: leapt), TurntableDeck.turn(134, playing: true, phase: phase), accuracy: 1e-9) for play in [true, false] { XCTAssertEqual(TurntableDeck.turn(134, playing: play, phase: 77, reduceMotion: true), 0) } } /// RULE: The label's words are the card's — its name, its artist and its album in capitals — and its three times are played, left and the length; with no length, only the time played. func testTheLabelIsTheCards() { XCTAssertEqual(TurntableDeck.label(SongTurntableFaceCatalogue.harbour), ["Harbour Lights", "THE LOW TIDE", "NIGHT FERRY"]) XCTAssertEqual(TurntableDeck.label(DeckCard(face: "song.turntable", corner: nil, pressing: false, pace: nil, plate: nil, fields: [:])), []) XCTAssertEqual(TurntableDeck.times(134, of: 242), ["2:14", "\u{2212}1:48", "4:02"]) XCTAssertEqual(TurntableDeck.times(134.9, of: 242), ["2:14", "\u{2212}1:48", "4:02"], "the clocks add up to the length") XCTAssertEqual(TurntableDeck.times(5, of: 0), ["0:05"]) XCTAssertEqual(HifiDeck.length(.still, SongTurntableFaceCatalogue.harbour), 242) } /// RULE: The label's words are set into a picture once, the same picture for the same words, size and ink, and a new one when the words change. func testTheLabelIsPrintedOnce() { let cache = TurntableLabelPrintCache() var renders = 0 let key = TurntableLabelPrintCache.Key(words: TurntableDeck.label(SongTurntableFaceCatalogue.harbour), covered: false, side: 128, scale: 2, look: .darkRoom) let render = { () -> CGImage? in renders += 1 let renderer = ImageRenderer(content: TurntableLabelWords(card: SongTurntableFaceCatalogue.harbour, covered: false, side: 128)) renderer.scale = 2 return renderer.cgImage } let first = cache.image(for: key, render: render) XCTAssertNotNil(first) XCTAssertEqual(first?.width, 256) _ = cache.image(for: key, render: render) XCTAssertEqual(renders, 1, "set once") let other = TurntableLabelPrintCache.Key(words: TurntableDeck.label(SongTurntableFaceCatalogue.train), covered: false, side: 128, scale: 2, look: .darkRoom) _ = cache.image(for: other, render: render) XCTAssertEqual(renders, 2) } /// RULE: Every word the turntable says is a line or the card's: its arm, previous and next are the kit's lines, never a literal. func testItsWordsAreLines() { for key in ["position", "next", "previous"] { XCTAssertNotEqual(ShellLines.line("faces", "turntable.\(key)"), "faces.turntable.\(key)", "the kit ships faces.turntable.\(key)") } let source = try? String(contentsOf: URL(fileURLWithPath: #filePath) .deletingLastPathComponent().deletingLastPathComponent().deletingLastPathComponent() .appendingPathComponent("Sources/CottonFaces/Cotton/SongTurntableFace.swift"), encoding: .utf8) let face = try? XCTUnwrap(source).components(separatedBy: "public enum SongTurntableFaceCatalogue")[0] XCTAssertNotNil(face) XCTAssertNil(face?.range(of: #"Text\(""#, options: .regularExpression), "the face types no literal") XCTAssertNil(face?.range(of: #"accessibilityLabel\(""#, options: .regularExpression), "the face says no literal") } /// RULE: The turntable is registered, printed on no stock, laid on the music's black tolex, and its paintings are in the catalogue — the jewel the hi-fi's. func testItLiesOnTheTolex() { XCTAssertNotNil(FaceRegistry.all.first { $0.id == "song.turntable" }) XCTAssertEqual(FaceRegistry.ground("song.turntable"), .tolex) XCTAssertEqual(FaceRegistry.stock("song.turntable"), .own) for name in ["slipmat", "record", "label-blank", "spindle", "tonearm", "tonearm-shadow", "hifi-jewel-on", "hifi-jewel-off", "sleeve-front"] { XCTAssertNotNil(CottonMedia.image(name), name) } } /// RULE: The specimens stand 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: SongTurntableFaceCatalogue.face.specimens.map { ($0.name, $0) }) XCTAssertEqual(byName["playing"]?.play.map(TurntableDeck.state), .playing) XCTAssertEqual(byName["playing"]?.play?.elapsed, 134) XCTAssertEqual(byName["playing"]?.play?.duration, 242) XCTAssertNotNil(byName["playing"]?.picture) XCTAssertEqual(byName["paused"]?.play.map(TurntableDeck.state), .paused) XCTAssertNil(byName["no cover"]?.picture) XCTAssertNotNil(byName["largest"]) } /// RULE: The turntable draws playing, paused, with no cover and with the longest words, from its card and the player's word alone, at every text size. 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", SongTurntableFaceCatalogue.harbour, true, playing), ("paused", SongTurntableFaceCatalogue.harbour, true, paused), ("no cover", SongTurntableFaceCatalogue.train, false, TrackPlay(held: true, playing: true, elapsed: 61, duration: 197)), ("largest", SongTurntableFaceCatalogue.largest, false, TrackPlay(held: true, playing: false, elapsed: 312, duration: 669)), ("still", SongTurntableFaceCatalogue.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: {}) .frame(width: 340, height: 451) .background(FaceGroundSurface(.tolex)) .environment(\.facePictureDrawer, FixturePictures.drawer) .dynamicTypeSize(size) .themed(.paper)) renderer.scale = 2 let image = try XCTUnwrap(renderer.cgImage, "the turntable \(name) drew nothing") if let out { try write(image, to: out.appendingPathComponent("song.turntable-\(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) } }