// A show's radio and the specimen player it is played under in Loom: it
// stands at rest, playing or paused by the player's word alone; its needle
// runs the time played over the length; its pointers sweep their arc; a fine
// TUNE turn moves seconds and a quick one minutes; and a specimen reviewed
// under the player's word starts there. With `COTTON_FACE_SHOTS=
` set,
// the radio in each state is written there beside the face shots.
import CottonFaces
import CottonPaper
import CottonSpecimens
import CottonValues
import SwiftUI
import XCTest
#if canImport(UIKit)
import UIKit
#else
import AppKit
#endif
@MainActor
final class RadioTests: XCTestCase {
private let t0 = Date(timeIntervalSinceReferenceDate: 800_000_000)
private let artwork = FacePicture(key: "podcast-history", edge: 120)
/// RULE: The radio stands at rest while the player holds none of its show, playing while it plays or fetches its episode, and paused while it holds it paused.
func testTheRadioStandsByThePlayersWord() {
XCTAssertEqual(RadioDial.state(.still), .rest)
XCTAssertEqual(RadioDial.state(TrackPlay(held: true, playing: true, elapsed: 10, duration: 100)), .playing)
XCTAssertEqual(RadioDial.state(TrackPlay(held: true, loading: true)), .playing)
XCTAssertEqual(RadioDial.state(TrackPlay(held: true, playing: false, elapsed: 10, duration: 100)), .paused)
}
/// RULE: The radio's episode is its card's `episode` — its title and its length in seconds — and a show with none has none.
func testTheEpisodeIsTheCards() {
let episode = RadioDial.episode(PodcastRadioFaceCatalogue.hour)
XCTAssertEqual(episode?.title, "128. The Distant Coast (Part 3)")
XCTAssertEqual(episode?.seconds, 3756)
XCTAssertNil(RadioDial.episode(PodcastRadioFaceCatalogue.quiet))
}
/// RULE: The scale is the episode's length — the player's where it knows one, else the feed's — and the needle's share is the time played over it, held to the scale; nought for a length unknown.
func testTheNeedleRunsTheTimePlayedOverTheLength() {
let episode = RadioEpisode(title: "One", seconds: 3600)
XCTAssertEqual(RadioDial.length(.still, episode), 3600)
XCTAssertEqual(RadioDial.length(TrackPlay(held: true, duration: 3720), episode), 3720)
XCTAssertEqual(RadioDial.length(TrackPlay(held: true, duration: 0), episode), 3600)
XCTAssertEqual(RadioDial.share(900, of: 3600), 0.25, accuracy: 1e-9)
XCTAssertEqual(RadioDial.share(4000, of: 3600), 1)
XCTAssertEqual(RadioDial.share(-5, of: 3600), 0)
XCTAssertEqual(RadioDial.share(10, of: 0), 0)
}
/// RULE: A pointer sweeps 135 degrees either side of upright, upright at half its share.
func testAPointerSweepsItsArc() {
XCTAssertEqual(RadioDial.pointer(0), -135)
XCTAssertEqual(RadioDial.pointer(0.5), 0)
XCTAssertEqual(RadioDial.pointer(1), 135)
XCTAssertEqual(RadioDial.pointer(2), 135)
}
/// RULE: A slow TUNE turn moves seconds — half a second a degree — and a quick one gears up with its speed, to at most forty times.
func testAFineTurnIsSecondsAndAQuickOneMinutes() {
XCTAssertEqual(RadioDial.tuned(10, speed: 0), 5, accuracy: 1e-9)
XCTAssertEqual(RadioDial.tuned(-10, speed: 0), -5, accuracy: 1e-9)
XCTAssertEqual(RadioDial.tuned(10, speed: 240), 10, accuracy: 1e-9)
XCTAssertGreaterThan(RadioDial.tuned(90, speed: 1200), 60, "a quick quarter turn moves minutes")
XCTAssertEqual(RadioDial.tuned(1, speed: 1_000_000), 20, accuracy: 1e-9)
}
/// RULE: The specimen player stands where a specimen's word says: playing runs on from there at the clock's pace, paused holds; at rest it holds nothing.
func testTheSpecimenPlayerStartsWhereTheSpecimenSays() {
let lengths: [TimeInterval] = [3756]
let playing = SpecimenPlayer(lengths: lengths, standing: TrackPlay(held: true, playing: true, elapsed: 1120), at: t0)
XCTAssertEqual(playing.play(at: t0.addingTimeInterval(20)).elapsed, 1140, accuracy: 1e-6)
XCTAssertTrue(playing.play(at: t0.addingTimeInterval(20)).playing)
let paused = SpecimenPlayer(lengths: lengths, standing: TrackPlay(held: true, playing: false, elapsed: 2470), at: t0)
XCTAssertEqual(paused.play(at: t0.addingTimeInterval(20)).elapsed, 2470, accuracy: 1e-6)
XCTAssertFalse(paused.play(at: t0.addingTimeInterval(20)).playing)
paused.toggle(at: t0.addingTimeInterval(20))
XCTAssertEqual(paused.play(at: t0.addingTimeInterval(30)).elapsed, 2480, accuracy: 1e-6, "played on from where it was paused")
let rest = SpecimenPlayer(lengths: lengths, standing: .still, at: t0)
XCTAssertEqual(rest.play(at: t0), .still)
}
/// RULE: Every radio specimen names the player's word it is reviewed under: rest none, playing playing, paused paused.
func testTheSpecimensStandAtRestPlayingAndPaused() {
let byName = Dictionary(uniqueKeysWithValues: PodcastRadioFaceCatalogue.face.specimens.map { ($0.name, $0) })
XCTAssertNil(byName["rest"]?.play)
XCTAssertEqual(byName["playing"]?.play.map(RadioDial.state), .playing)
XCTAssertEqual(byName["paused"]?.play.map(RadioDial.state), .paused)
}
/// RULE: The radio draws at rest, playing and paused from its card, its artwork and the player's word alone, at every text size, and a show with no artwork draws too.
func testTheRadioDrawsInEveryState() 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, TrackPlay, FacePicture?)] = [
("rest", PodcastRadioFaceCatalogue.hour, .still, artwork),
("playing", PodcastRadioFaceCatalogue.hour, TrackPlay(held: true, playing: true, elapsed: 1120, duration: 3756), artwork),
("paused", PodcastRadioFaceCatalogue.hour, TrackPlay(held: true, playing: false, elapsed: 2470, duration: 3756), artwork),
("near the start", PodcastRadioFaceCatalogue.hour, TrackPlay(held: true, playing: true, elapsed: 40, duration: 3756), artwork),
("no artwork", PodcastRadioFaceCatalogue.quiet, .still, nil),
]
for size in [DynamicTypeSize.xSmall, .large, .accessibility5] {
for (name, card, play, photo) in states {
let renderer = ImageRenderer(content: RadioFace(card: card, photo: photo, play: play, toggle: {}, volume: 0.4, turnVolume: { _ in })
.frame(width: 340, height: 451)
.background(FaceGroundSurface(.formicaCounter))
.environment(\.facePictureDrawer, FixturePictures.drawer)
.dynamicTypeSize(size)
.themed(.paper))
renderer.scale = 2
let image = try XCTUnwrap(renderer.cgImage, "the radio \(name) drew nothing")
if let out, size == .large { try write(image, to: out.appendingPathComponent("podcast.radio-\(name).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)
}
}