import Foundation import XCTest @testable import CottonUI /// A film's own subtitles in Apple's player (FilmSubtitles.swift): which /// track plays (the film's choice over the cinema's setting), and the cues /// drawn at their time plus the film's timing. final class FilmSubtitlesTests: XCTestCase { private let offered: [FilmSubtitlePick.Offered] = [ .init(language: "en", name: "English"), .init(language: "fr", name: "French (made on your Mac)"), .init(language: "en", name: "English (translated from French)"), ] private func tracks(_ choice: PlayerPlaying.Tracks.SubtitleChoice?, on: Bool = true, language: String? = nil) -> PlayerPlaying.Tracks { PlayerPlaying.Tracks(subtitles: on, subtitleLanguage: language, audioLanguage: nil, choice: choice) } /// RULE (17 Nov 2026): the track the owner chose for this film plays, matched by its name and language, else its language; Off is off; with no choice of its own the cinema's language (the phone's when none) plays, and subtitles off is none. func testTheFilmsOwnChoicePlaysOverTheCinemasSetting() { XCTAssertEqual(FilmSubtitlePick.pick(offered, tracks: tracks(.track(language: "en", name: "English (translated from French)")), phone: "hi", allowsOff: true), .option(2)) XCTAssertEqual(FilmSubtitlePick.pick(offered, tracks: tracks(.track(language: "fr-FR", name: "French (an older name)")), phone: "hi", allowsOff: true), .option(1)) XCTAssertEqual(FilmSubtitlePick.pick(offered, tracks: tracks(.off), phone: "en", allowsOff: true), .off) XCTAssertEqual(FilmSubtitlePick.pick(offered, tracks: tracks(.off), phone: "en", allowsOff: false), .leave) XCTAssertEqual(FilmSubtitlePick.pick(offered, tracks: tracks(nil, language: "fr"), phone: "en", allowsOff: true), .option(1)) XCTAssertEqual(FilmSubtitlePick.pick(offered, tracks: tracks(nil), phone: "en-GB", allowsOff: true), .option(0)) XCTAssertEqual(FilmSubtitlePick.pick(offered, tracks: tracks(nil), phone: "ja", allowsOff: true), .leave) XCTAssertEqual(FilmSubtitlePick.pick(offered, tracks: tracks(nil, on: false), phone: "en", allowsOff: true), .off) // A chosen track the film no longer carries falls back to the setting. XCTAssertEqual(FilmSubtitlePick.pick(offered, tracks: tracks(.track(language: "de", name: "German")), phone: "en", allowsOff: true), .option(0)) } /// RULE (17 Nov 2026): shifted, a cue shows at its own time plus the film's timing (earlier when negative) and goes when the next change comes; what is long past is forgotten, and a seek forgets everything heard. func testCuesShowAtTheirTimePlusTheFilmsTiming() { var timeline = FilmCueTimeline() timeline.heard(at: 10, lines: ["Bonjour."]) timeline.heard(at: 12, lines: []) timeline.heard(at: 14, lines: ["Au revoir."]) XCTAssertEqual(timeline.showing(at: 10.2, offset: 0.3), [], "0.3 s later: not yet") XCTAssertEqual(timeline.showing(at: 10.3, offset: 0.3), ["Bonjour."]) XCTAssertEqual(timeline.showing(at: 12.2, offset: 0.3), ["Bonjour."]) XCTAssertEqual(timeline.showing(at: 12.3, offset: 0.3), []) XCTAssertEqual(timeline.showing(at: 13.5, offset: -0.5), ["Au revoir."], "half a second earlier") timeline.heard(at: 14, lines: ["Adieu."]) XCTAssertEqual(timeline.showing(at: 14, offset: 0), ["Adieu."], "one change a moment: the later word wins") timeline.forget(before: 15, offset: 0) XCTAssertEqual(timeline.changes.map(\.at), [14]) timeline.clear() XCTAssertEqual(timeline.showing(at: 15, offset: 0), []) } }