//! What the workers hand back: the progress lines, the file named on the //! command line, the scans, the codecs offered, and the subtitles found. use super::*; use crate::player::library::{ auto_proxies_pref_path, load_auto_proxies_pref, save_auto_proxies_pref, }; use crate::subs::{ SUB_SIZE_RANGE, SubOutline, SubPlace, SubStyle, audio_import_tail, load_subtitle_style, save_subtitle_style, sub_line_h_for, sub_size_at, subtitle_style_path, }; use crate::ui::dock_stance::{viz_glow_at, viz_strands_at}; use crate::ui::preview::{bgra_to_rgba, screenshot_path}; /// The notice queue's own half of the sound watch, and the three words it is /// spelled with -- reached by name because the app's own glob does not carry /// them (`notices` is a private module re-exported for the window). use crate::notices::{ audio_change_notice, audio_lost_line, audio_tail, decode_change_notice, decode_failed_line, }; /// The picture twin of the sound watch above, and the same shape for the same /// reason: a decode failure that arrives between two frames used to reach no /// consumer at all, because the worker printed it to stderr and dropped the /// channel -- and a closed channel is what a finished film looks like from the /// app's side, so the transport said ENDED over a frozen frame with nothing in /// the window saying why. /// /// Driven through the queue step, because a test cannot make a decoder fail on /// cue; the engine's writer is the decode worker's, and /// `engine::tests::decode_failure` observes it end to end on the engine side. #[test] fn a_decode_that_fails_under_an_open_file_says_so_and_unsays_it() { // A question stays on screen beside the one ordinary slot, so it is the // line that must survive every change below. let mut notices: std::collections::VecDeque = Default::default(); crate::notices::push_sticky_toast( &mut notices, "Opened take.mp4".into(), crate::notices::Tone::Info, Vec::new(), ); let failed = decode_failed_line("decode error at access unit 130"); assert!( failed.starts_with("Decode failed"), "the line lost the state word the strip reads first: {failed}" ); // The file was fine, and then the picture stopped: the reason arrives // between two frames. decode_change_notice(&mut notices, None, Some("decode error at access unit 130")); assert_eq!( notices.back().map(|notice| notice.text.as_ref()), Some(failed.as_str()), "a decode failure that arrives mid-session is never pushed" ); // The same reason read again on the next frame is not news. decode_change_notice( &mut notices, Some("decode error at access unit 130"), Some("decode error at access unit 130"), ); assert_eq!(notices.len(), 2, "one failure was announced twice"); // Seeked away from, and it decodes again: the line goes with it, or the // strip keeps saying DECODE FAILED over a film that plays. decode_change_notice(&mut notices, Some("decode error at access unit 130"), None); assert_eq!( notices .iter() .map(|notice| notice.text.as_ref()) .collect::>(), vec!["Opened take.mp4"], "the decode failed line is still on the strip over a film that plays" ); // The sound line is a different line, and the one slot means the newest // error is the one on screen: taking that newest line back leaves the // question beside it, and nothing else is touched. audio_change_notice(&mut notices, None, Some("AUDIO_LOST")); decode_change_notice(&mut notices, None, Some("decode error at access unit 7")); decode_change_notice(&mut notices, Some("decode error at access unit 7"), None); assert_eq!( notices .iter() .map(|notice| notice.text.as_ref()) .collect::>(), vec!["Opened take.mp4"], "retracting one reason took more than its own line" ); // The wiring: the pump is the one place every frame passes through, and a // watcher nothing calls is a message nobody reads. assert!( fn_body("pump").contains("self.watch_decode();"), "the pump stopped watching the engine's decode reason, so a corrupt \ file is a silent freeze again" ); // ...and it reads the reason off the session the pump is serving, the same // rule the sound watch follows. assert!( fn_body("decode_reason").contains("self.active_session()"), "the decode watch reads a reason off a session the pump is not \ serving, so a preview flip announces the other session's failure" ); } /// A reason that changes under a session that is already open says so, and stops /// saying so when it clears. This side is the *reader*: the writer of a reason is /// the engine's own `audio_disabled_reason`, and the app's two call sites were /// the *open* paths -- so a reason that arrived mid-session reached no consumer /// at all and the window played on in silence with nothing said. The engine's /// writer is the wave branch's, so the flip is driven through the seam here and /// asserted end to end on the merged tree; nothing below depends on when the /// engine's `tick` sets it. /// /// Driven through the queue step itself -- [`audio_change_notice`] is the whole /// of what a reason change does to the strip -- because a test cannot make a /// sound card fail on cue; the caching around it is the same comparison plus the /// source guard below. #[test] fn a_device_that_dies_under_an_open_file_says_so_and_unsays_it() { // A question stays on screen beside the one ordinary slot, so it is the // line that must survive every change below. let mut notices: std::collections::VecDeque = Default::default(); crate::notices::push_sticky_toast( &mut notices, "Opened take.mp4".into(), crate::notices::Tone::Info, Vec::new(), ); let lost = audio_lost_line("AUDIO_LOST"); // The file was fine, and then the sound is gone: the reason arrives between // two frames. audio_change_notice(&mut notices, None, Some("AUDIO_LOST")); assert_eq!( notices.back().map(|notice| notice.text.as_ref()), Some(lost.as_str()), "a reason that arrives mid-session is never pushed" ); assert!( lost.starts_with("Audio lost") && lost.ends_with(&audio_tail("AUDIO_LOST")), "the standing line lost its state word, or stopped sharing \ `audio_notice`'s own tail" ); // The same reason read again on the next frame is not news. audio_change_notice(&mut notices, Some("AUDIO_LOST"), Some("AUDIO_LOST")); assert_eq!(notices.len(), 2, "one death was announced twice"); // The re-arm takes: the line goes with it, and nothing else does. audio_change_notice(&mut notices, Some("AUDIO_LOST"), None); assert_eq!( notices .iter() .map(|notice| notice.text.as_ref()) .collect::>(), vec!["Opened take.mp4"], "the no audio line is still on the strip over a film that plays" ); // A second death later says so again. audio_change_notice(&mut notices, None, Some("AUDIO_LOST")); assert_eq!( notices.back().map(|notice| notice.text.as_ref()), Some(lost.as_str()) ); // The wiring: the pump is the one place every frame passes through, and a // watcher nothing calls is a message nobody reads. assert!( fn_body("pump").contains("self.watch_audio();"), "the pump stopped watching the engine's sound reason, so a device that \ dies mid-session is silent again" ); // ...and the watch keys its cache to the session it is a statement about: // the two generations are what the *step call* is asked, so a preview flip // cannot be answered as if it were the cached session's own change. let watch = fn_body("watch_audio"); let args = watch .split_once("reason_step(") .expect( "the watch decides for itself instead of through the pure step the flip test drives", ) .1; let args = args.split_once(')').map_or(args, |(args, _)| args); assert!( args.contains("self.audio_watch_gen") && args.contains("self.session_gen"), "the watch asks the step about something other than the session it serves, \ so a preview opening or closing announces the other session's device" ); assert!( fn_body("sound_reason").contains("self.active_session()"), "the watch reads a reason off a session the pump is not serving, so its \ cache and the frame disagree the moment a preview is up" ); // ...and every door that installs or drops a session says so, the preview's // two among them (the one the flip was missed on). for door in ["close_session", "open_preview", "close_preview"] { assert!( fn_body(door).contains("self.session_swapped();"), "{door} installs or drops a session without telling anything keyed to \ one, so a flip reads as news" ); } } /// The flip logic itself, pure: a preview opening or closing is a *different* /// session, so the reason it reports is not news about the cached one -- the /// phantom device loss the verifier caught, and the reason the cache carries a /// generation at all. A change on the session the cache describes is the one that /// speaks. #[test] fn a_preview_flip_is_not_a_device_that_came_back() { use crate::notices::{ReasonStep, reason_step}; // A swap, every way round: seeded, never announced. assert_eq!( reason_step(7, 8, None, Some("AUDIO_LOST")), ReasonStep::Seed ); assert_eq!( reason_step(7, 8, Some("AUDIO_LOST"), None), ReasonStep::Seed ); assert_eq!( reason_step(7, 8, Some("AUDIO_LOST"), Some("AUDIO_LOST")), ReasonStep::Seed, "a flip whose reason reads the same on both sides is still a flip" ); // The same session, the reason arriving, changing, and going. assert_eq!( reason_step(7, 7, None, Some("AUDIO_LOST")), ReasonStep::Push ); assert_eq!( reason_step(7, 7, Some("AUDIO_LOST"), Some("device gone")), ReasonStep::Push ); assert_eq!( reason_step(7, 7, Some("AUDIO_LOST"), None), ReasonStep::Clear ); assert_eq!( reason_step(7, 7, Some("AUDIO_LOST"), Some("AUDIO_LOST")), ReasonStep::Nothing, "the same reason read again on the next frame is not news" ); assert_eq!(reason_step(7, 7, None, None), ReasonStep::Nothing); assert_eq!(reason_step(0, 0, None, None), ReasonStep::Nothing); } /// The toast tail an audio import earns: rate, channel count and rounded /// length, in the words the IMPORTED line appends them in /// ([`Player::take_import`], [`Player::install_media`]). A film gets none of /// it -- the gate is the probed has-video answer, not the extension, so an /// audio-only container wearing .mp4 reads its numbers too. #[test] fn an_audio_import_names_its_rate_channels_and_length() { let tail = audio_import_tail(&asset("test_tone.mp3"), false); assert_eq!(tail, " — 44100 Hz, 2 ch, 3s", "{tail}"); assert_eq!(audio_import_tail(&asset("test_av.mp4"), true), ""); let mp4 = audio_import_tail(&asset("test_audio_only.mp4"), false); assert_eq!(mp4, " — 44100 Hz, 1 ch, 3s", "{mp4}"); } /// The import line's own state machine, driven the way a repaint drives /// it: the worker writes a stage, the poll notices it changed and restarts /// the stall clock, and the line's words change only when a stage has /// actually stood still. The whole point is that a stuck read reads as a /// stuck read and never as a frozen window. #[test] fn an_import_line_says_a_stage_has_stopped_moving_and_only_then() { use std::sync::Arc; use std::sync::atomic::{AtomicU8, Ordering::Relaxed}; let stage = Arc::new(AtomicU8::new(ImportStage::Header as u8)); let started = Instant::now() - Duration::from_secs(9); let mut import = Import { path: PathBuf::from("/films/A Film.mkv"), started, stage: Arc::clone(&stage), seen: ImportStage::Header, // As if the header had been running for those nine seconds. since: started, cancelled: Arc::default(), }; // Nine seconds inside one stage: past the wait a person tolerates, so // the line stops pretending it is a progress line. let since = import.poll(); assert!(since > IMPORT_STALL, "{since}"); let stalled = import_line("A Film.mkv", import.seen, 9., since, 0, false); assert!(stalled.contains("still reading the header"), "{stalled}"); assert!(stalled.contains("not frozen"), "{stalled}"); assert!(stalled.contains("0:09 elapsed"), "{stalled}"); // The worker moves on: the stall clock restarts even though the elapsed // one does not, which is the distinction the two clocks exist for. stage.store(ImportStage::Subtitles as u8, Relaxed); let since = import.poll(); assert_eq!(import.seen, ImportStage::Subtitles); assert!(since < IMPORT_STALL, "{since}"); let moving = import_line("A Film.mkv", import.seen, 9., since, 2, false); assert!( moving.starts_with("Importing A Film.mkv · reading the subtitle tracks"), "{moving}" ); assert!(!moving.contains("still"), "{moving}"); // ...and what is behind it in the queue, which is what a drop of three // files or an argv of three files leaves waiting. assert!(moving.ends_with("· 2 more waiting"), "{moving}"); assert!(!stalled.ends_with("waiting"), "an empty queue says nothing"); // A stage that does not move is not the same fact as a stage that came // back to the same value: the poll only restarts on a change. let held = import.since; import.poll(); assert_eq!(import.since, held, "an unchanged stage must not reset it"); } /// The launch, from argv to the window being up: nothing named on the /// command line is read before there is a window, so all a launch does is /// sort argv into the file that becomes the timeline and the queue that is /// read through -- and a run with no arguments has neither, exactly as it /// had none before. #[test] fn a_launch_queues_the_file_it_names_instead_of_opening_it() { let film = PathBuf::from("/films/Dune.mkv"); let extra = PathBuf::from("/films/Titles.mov"); let (arg, queue) = launch_queue([film.clone(), extra.clone()].into_iter()); assert_eq!(arg.as_deref(), Some(film.as_path()), "argv[1] is the open"); // In the order they were named, the timeline's file first: six header // walks racing over one disk finish no sooner than six in a row, and // the one the person is waiting to watch goes first. assert_eq!(queue, [film, extra], "argv, in arrival order"); // ...and no argument at all is still the empty window: nothing to open // and nothing to read. let (arg, queue) = launch_queue(std::iter::empty()); assert_eq!(arg, None); assert!(queue.is_empty(), "no argv, nothing queued"); } /// Which door a queued file goes through. One queue carries the file argv /// named and every import behind it, so this fork -- made when the worker /// starts and carried to the landing as a [`Landed`] -- is the whole state /// machine: the named file becomes the timeline (a `.edith` restoring a /// whole one), everything else joins the library, including the very same /// film dropped again once the open has landed. #[test] fn the_file_argv_named_lands_as_the_timeline_and_everything_behind_it_as_imports() { let film = PathBuf::from("/films/Dune.mkv"); let extra = PathBuf::from("/films/Titles.mov"); let project = PathBuf::from("/films/Dune.edith"); assert_eq!(arrival(Some(&film), &film), Landing::Open); assert_eq!(arrival(Some(&project), &project), Landing::Project); assert_eq!(arrival(Some(&film), &extra), Landing::Import); // Cleared as it lands: a drop of the film already on the timeline is an // import, which is what a drop has always been. assert_eq!(arrival(None, &film), Landing::Import); // ...except a `.edith`, which is a whole timeline and has nothing to be // imported into: dropped or picked, it lands where argv's does, and its // open is the worker's like every other. assert_eq!(arrival(None, &project), Landing::Project); assert_eq!(arrival(Some(&film), &project), Landing::Project); } /// What the window says while the file argv named is being read: the file's /// name, the read that is running, and a clock proving the window is /// answering -- in the *opening* wording, because the one person who typed /// that name is not importing anything. #[test] fn the_named_file_is_read_under_an_opening_line_not_an_importing_one() { let opening = import_line("Dune.mkv", ImportStage::Header, 0.4, 0.4, 1, true); assert!( opening.starts_with("Opening Dune.mkv · reading the header"), "{opening}" ); assert!(opening.ends_with("· 1 more waiting"), "{opening}"); // Twelve seconds into a cold 25 GB header walk, which is the whole // reason the window is up: it says the read is still moving through the // same stage and that this is not a freeze. let stalled = import_line("Dune.mkv", ImportStage::Header, 12., 12., 0, true); assert!(stalled.starts_with("Opening Dune.mkv · still"), "{stalled}"); assert!(stalled.contains("not frozen"), "{stalled}"); assert!(stalled.contains("0:12 elapsed"), "{stalled}"); // The files behind it are imports and still say so. let import = import_line("Titles.mov", ImportStage::Header, 0.4, 0.4, 0, false); assert!(import.starts_with("Importing Titles.mov"), "{import}"); } /// One gesture over the real gate, with the plumbing around it spelled out: /// a write reseeks (the worker owes a frame again), a landed frame clears /// that and flushes what is held, and the release flushes whatever the /// worker is doing. Forty snapped steps and four frames delivered must cost /// five writes -- the press's and one per frame -- and not forty, which is /// the 22-30 s freeze this exists to remove. #[test] fn a_bar_wide_sweep_writes_once_per_frame_delivered() { let mut stash: Option = None; let mut written = Vec::new(); let mut busy = false; for step in 0..40 { if let Some(value) = stash_or_write(&mut stash, step, step == 0, busy) { // What `write_color` does: the write supersedes the stash and // reseeks, so the worker owes a frame from here. stash = None; written.push(value); busy = true; } // A frame lands every tenth sample: `pump` clears the seek and the // render flushes what the drag held back. if step % 10 == 9 { busy = false; if let Some(value) = stash.take() { written.push(value); busy = true; } } } // The release, whatever the worker is doing. written.extend(stash.take()); assert_eq!( written, vec![0, 9, 19, 29, 39], "one write per frame landed" ); } /// The one value a gesture may never lose: where the hand let go. The /// release samples into a busy worker -- so the sample is held -- and the /// flush behind it is what writes it. #[test] fn a_release_lands_the_value_the_hand_let_go_on() { let mut stash = None; assert_eq!(stash_or_write(&mut stash, 7, false, true), None); assert_eq!(stash_or_write(&mut stash, 11, false, true), None); assert_eq!(stash.take(), Some(11), "the release writes the last sample"); // The press is never held: it is the undo step the gesture rolls back // to, and one taken a frame late is a snapshot of the wrong grade. assert_eq!(stash_or_write(&mut stash, 3, true, true), Some(3)); assert_eq!(stash, None); // Nothing to hold when the worker is idle: the write goes straight out. assert_eq!(stash_or_write(&mut stash, 5, false, false), Some(5)); assert_eq!(stash, None); } /// The wheel's own version of the same gesture, and the one thing a drag on it /// may not lose while the worker is behind: the mark follows the hand. A held /// sample is what the wheel draws -- the clip carries the ink from before it /// until the holding frame lands -- and the pen's own settings, which are not /// part of a hue/saturation sample, survive it untouched. #[test] fn the_wheel_draws_the_sample_a_drag_is_holding() { // Nothing held: the clip's own ink, byte for byte -- the default 0 // included, which materializes to azure where it is read. assert_eq!(viz_held_paint(0, None), 0); assert_eq!(viz_held_paint(0x0208_d2fd, None), 0x0208_d2fd); // Held: that hue and saturation over what the clip already carries. let held = viz_held_paint(0, Some((VizSlot::Main, 253, 210))); assert_eq!(engine::decode::viz_hue_ui(held), 253); assert_eq!(engine::decode::viz_sat_ui(held), 210); // ...in that order, never the other way round: a mark drawn at the wrong // angle is the whole gesture showing the wrong colour. let flipped = viz_held_paint(0, Some((VizSlot::Main, 210, 253))); assert_ne!(held, flipped); assert_eq!(engine::decode::viz_hue_ui(flipped), 210); assert_eq!(engine::decode::viz_sat_ui(flipped), 253); // Strands and glow are the clip's, not the sample's: the wheel carries no // row for either, so a held hue may not move them. assert_eq!(engine::decode::viz_strands_of(held), 8); assert_eq!(engine::decode::viz_glow_of(held), 2); let pen = engine::decode::with_viz_paint_glow(0x0208_d2fd, 9); assert_eq!( engine::decode::viz_glow_of(viz_held_paint(pen, Some((VizSlot::Main, 253, 210)))), 9 ); } /// The strands and glow sliders: a press's share of the track lands on a /// count the engine keeps, at both ends and at the middle, and a share past /// an end pins to that end rather than wrapping. #[test] fn the_strand_and_glow_sliders_land_on_the_engine_range() { assert_eq!(viz_strands_at(0.), 1); assert_eq!(viz_strands_at(1.), 32); // Half a track is 15.5 steps on from one: it rounds up to 17. assert_eq!(viz_strands_at(0.5), 17); assert_eq!(viz_strands_at(-0.2), 1); assert_eq!(viz_strands_at(1.7), 32); assert_eq!(viz_glow_at(0.), 0); assert_eq!(viz_glow_at(1.), 16); assert_eq!(viz_glow_at(0.5), 8); assert_eq!(viz_glow_at(9.), 16); // The default paint's own counts sit inside what the sliders can reach. let azure = engine::decode::VIZ_PAINT_AZURE; assert!((1..=32).contains(&engine::decode::viz_strands_of(azure))); assert!(engine::decode::viz_glow_of(azure) <= 16); } /// The strand's half of the same gesture, and the two things a second ink on /// the one wheel may not get wrong. A strand the clip has not chosen starts at /// the automatic complement the painter draws there -- the clip's hue half a /// turn round, at the clip's own saturation -- so the mark is on a colour the /// picture already has and the first write only says "this one, then". And the /// clip's own ink is not touched by any of it: the whole point of a second ink /// is that one slot's writes leave the other alone. #[test] fn the_strand_slot_starts_at_the_complement_and_the_write_chooses_it() { use engine::decode::{ VIZ_PAINT_AZURE, viz_hue_ui, viz_sat_ui, viz_strand_hue_ui, viz_strand_sat_ui, viz_strand_set, with_viz_paint_hue, with_viz_paint_strands, }; let paint = with_viz_paint_strands(VIZ_PAINT_AZURE, 2); assert!(!viz_strand_set(paint), "no strand colour chosen yet"); assert_eq!( VizSlot::Strand.hue_sat(paint), (viz_hue_ui(paint).wrapping_add(128), viz_sat_ui(paint)), "the unset strand stands where the painter draws its complement" ); // ...half a turn round, wrapping: hue 200's complement is 72, not 328. let late = with_viz_paint_hue(paint, 200); assert_eq!(VizSlot::Strand.hue_sat(late).0, 200u8.wrapping_add(128)); assert_eq!(VizSlot::Strand.hue_sat(late).0, 72); // The write is what chooses it, and reads back as what was written. let picked = VizSlot::Strand.set_hue_sat(paint, 0, 255); assert!(viz_strand_set(picked)); assert_eq!(viz_strand_hue_ui(picked), 0); assert_eq!(viz_strand_sat_ui(picked), 255); assert_eq!(VizSlot::Strand.hue_sat(picked), (0, 255)); // The clip's own colour survives the strand's writes untouched. assert_eq!(viz_hue_ui(picked), viz_hue_ui(paint)); assert_eq!(viz_sat_ui(picked), viz_sat_ui(paint)); // A held strand sample draws the strand, not the clip's colour: the slot // rides with the sample. let held = viz_held_paint(paint, Some((VizSlot::Strand, 0, 255))); assert_eq!(VizSlot::Strand.hue_sat(held), (0, 255)); assert!(viz_strand_set(held)); // And the wheel's own slot writes only its own bytes. let main = VizSlot::Main.set_hue_sat(picked, 33, 44); assert_eq!((viz_hue_ui(main), viz_sat_ui(main)), (33, 44)); assert_eq!((viz_strand_hue_ui(main), viz_strand_sat_ui(main)), (0, 255)); } /// One strand has no dual half, so the strand slot falls back to the clip's own /// colour: a write there lands on the ink the picture actually paints instead /// of choosing a strand colour nothing wears. Two strands put it back -- the /// same reading the Strand chip is enabled by. #[test] fn a_strand_slot_on_a_single_strand_clip_is_the_clip_s_own_colour() { use engine::decode::{ VIZ_PAINT_AZURE, viz_hue_ui, viz_sat_ui, viz_strand_set, viz_strands_of, with_viz_paint_hue, with_viz_paint_sat, with_viz_paint_strands, }; let one = with_viz_paint_strands( with_viz_paint_sat(with_viz_paint_hue(VIZ_PAINT_AZURE, 64), 200), 1, ); assert_eq!(viz_strands_of(one), 1); assert_eq!(VizSlot::Strand.picked(one), VizSlot::Main); let hit = VizSlot::Strand.picked(one).set_hue_sat(one, 0, 255); assert!(!viz_strand_set(hit), "one strand chooses no strand colour"); assert_eq!((viz_hue_ui(hit), viz_sat_ui(hit)), (0, 255)); // The clip's own slot is never redirected -- it is what the fallback // lands on. assert_eq!(VizSlot::Main.picked(one), VizSlot::Main); assert_eq!( VizSlot::Strand.picked(with_viz_paint_strands(one, 2)), VizSlot::Strand ); } /// A seek says nothing until it has stood: an ordinary one is a flicker and /// a cold read of a big file is the case worth words. #[test] fn a_seek_says_so_only_once_it_has_stood() { assert_eq!(seek_line(None), None, "no seek, no line"); assert_eq!(seek_line(Some(Duration::from_millis(300))), None); let line = seek_line(Some(SEEK_STALL + Duration::from_secs(4))).expect("past the stall"); assert_eq!(line, "Opening the picture\u{2026} 6 s"); assert_eq!( seek_line(Some(SEEK_STALL + Duration::from_millis(100))).as_deref(), Some("Opening the picture\u{2026}"), "the seconds only once they say something" ); } /// A read that has stood past the give-up mark says why in plain words instead of a running /// count. A missing file is not in this line at all: the banner over the picture says it. #[test] fn a_picture_that_is_not_coming_says_why() { assert_eq!( seek_line(Some(SEEK_GIVE_UP)).as_deref(), Some("Can't show this frame: nothing could be read at the playhead.") ); assert!(seek_line(Some(SEEK_GIVE_UP - Duration::from_millis(1))).unwrap().starts_with("Opening")); let overlays = src_text("ui/overlays.rs"); assert!(overlays.contains("if self.picture_is_missing() {\n return None;"), "a missing file is the banner's"); } /// The silence card's own state machine, driven the way a repaint drives /// it: a worker moves its mark, the poll notices and restarts the stall /// clock, and the line says a read has stopped only when it actually has. /// The card is up through all of it -- that is the whole change, since the /// same decode used to run on the render thread and hold the frame for /// fifty-one seconds on a 25 GB film. #[test] fn a_silence_card_is_up_while_its_scan_runs_and_says_where_it_has_got_to() { use std::sync::Arc; use std::sync::atomic::Ordering::Relaxed; let progress = Arc::new(engine::silence::Progress::default()); // Two hours and eight minutes, as the header claims it. progress.total.store(7680, Relaxed); let started = Instant::now() - Duration::from_secs(9); let mut scan = SilenceScan { key: (PathBuf::from("/films/A Film.mkv"), 0, 0, 192_000), started, progress: Arc::clone(&progress), seen: 0, since: started, }; // Nine seconds and the mark has not moved: past the wait a person // tolerates, so the line stops pretending it is a progress line. let since = scan.poll(); assert!(since > IMPORT_STALL, "{since}"); let stalled = silence_line(0., 768., 9., since); assert!(stalled.contains("still reading the sound"), "{stalled}"); assert!(stalled.contains("not frozen"), "{stalled}"); assert!(stalled.contains("0:00 of ~12:48 scanned"), "{stalled}"); assert!(stalled.contains("0:09 elapsed"), "{stalled}"); // The worker reports, and the stall clock restarts even though the // elapsed one does not -- the two clocks' whole distinction. let mut last = 0; for deci in [83, 1_140, 4_002] { progress.scanned.store(deci, Relaxed); let since = scan.poll(); assert!(since < IMPORT_STALL, "{since}"); assert!(scan.seen > last, "{} after {last}", scan.seen); last = scan.seen; } let moving = silence_line(scan.seen as f32 / 10., 768., 9., 0.2); assert_eq!(moving, "Scanning · 6:40 of ~12:48 scanned · 0:09 elapsed"); assert!(!moving.contains("still"), "{moving}"); // A header that does not say how long the track is says nothing rather // than guessing at it. let unknown = silence_line(60., 0., 61., 0.2); assert_eq!(unknown, "Scanning · 1:00 scanned · 1:01 elapsed"); // A mark that comes back the same is not the same fact as one that // moved: the poll only restarts on a change. let held = scan.since; scan.poll(); assert_eq!(scan.since, held, "an unchanged mark must not reset it"); } /// The cache is per scanned stretch, which is what stops two films /// thrashing each other's fifty seconds: A, then B, then A again is *one* /// decode of A. And a stretch already being read is waited for rather than /// read twice -- both halves of an A/V take name the same file and the same /// frames. A *trimmed* A is not A: it is a shorter read of its own, which /// is the whole point of scanning the clip instead of the file. #[test] fn a_second_film_does_not_cost_the_first_one_its_levels() { let (a, b) = ( (PathBuf::from("/films/a.mkv"), 0, 0, 3000), (PathBuf::from("/films/b.mkv"), 0, 0, 3000), ); let half = (PathBuf::from("/films/a.mkv"), 0, 0, 1500); let mut cache: std::collections::HashMap = std::collections::HashMap::new(); let mut started = Vec::new(); // What a card open does, three times over, with the worker landing // between each: plan, and start what the plan says to start. for key in [&a, &b, &a] { match scan_plan(cache.contains_key(key), None, key) { ScanPlan::Start => { started.push(key.clone()); cache.insert(key.clone(), ()); } ScanPlan::Marks => {} ScanPlan::Wait => unreachable!("nothing is running"), } } assert_eq!(started, vec![a.clone(), b.clone()], "A was decoded twice"); // The single-slot cache this replaced would have evicted A when B // landed; both are held. assert!(cache.contains_key(&a) && cache.contains_key(&b)); // A scan in flight on the same source is joined, not restarted -- and // one on another source is not waited for. assert_eq!(scan_plan(false, Some(&a), &a), ScanPlan::Wait); assert_eq!(scan_plan(false, Some(&b), &a), ScanPlan::Start); // Levels in hand beat a worker either way: the marks are arithmetic. assert_eq!(scan_plan(true, Some(&a), &a), ScanPlan::Marks); // The same file cut in half is a read of its own: neither A's levels // nor A's worker answers for it. assert_eq!( scan_plan(cache.contains_key(&half), None, &half), ScanPlan::Start ); assert_eq!(scan_plan(false, Some(&a), &half), ScanPlan::Start); // ...and the seconds asked for are the clip's, at the project's rate. assert_eq!(source_secs(&half, 30.), (0., 50.)); assert_eq!( source_secs(&(half.0.clone(), 0, 900, 1500), 30.), (30., 50.) ); // A rate that is not one reads the file rather than nothing. assert_eq!(source_secs(&half, 0.), (0., f64::INFINITY)); } /// The background scan started at import ([`Player::cache_media`]) lands its /// whole-file levels under [`full_scan_key`], and that entry alone is what /// makes the card's own cache check answer "already read" for *every* clip /// cut from that source -- without the per-clip decode `start_silence_scan` /// used to be the only way to get one. The whole point of moving the scan to /// import: the card must find it warm. #[test] fn a_warm_whole_source_scan_answers_the_card_for_every_clip_cut_from_it() { use std::sync::Arc; let mut levels: std::collections::HashMap>> = std::collections::HashMap::new(); let clip_a = (PathBuf::from("/films/a.mkv"), 0, 300, 900); let clip_b = (PathBuf::from("/films/a.mkv"), 0, 1_200, 1_800); // Nothing yet -- neither clip's own read nor a background one. assert!(!silence_cached(&levels, &clip_a)); assert!(!silence_cached(&levels, &clip_b)); // The background scan lands, once, before either half of the take was // ever opened on the card. levels.insert(full_scan_key(&clip_a.0, clip_a.1), Arc::new(vec![0.; 100])); assert!( silence_cached(&levels, &clip_a) && silence_cached(&levels, &clip_b), "one whole-source entry answers for both halves of the take" ); // A clip cut from a *different* file is not answered by it -- the // sentinel is per (path, stream), exactly like every other scan key. let other_file = (PathBuf::from("/films/b.mkv"), 0, 300, 900); assert!(!silence_cached(&levels, &other_file)); // A clip's own exact read still counts too, same as before this existed. levels.insert(clip_a.clone(), Arc::new(vec![-40.; 10])); assert!(silence_cached(&levels, &clip_a)); } /// What the card actually draws from a warm background scan: the same /// dBFS-per-window numbers a direct read of the clip's own stretch would /// have found, because window `k` of the whole file and window `k` of a read /// that started at second 0 name the same slice of the same source /// ([`slice_whole_levels`]'s whole contract). No decode in this test at /// all -- the numbers are synthetic and distinct per window so a wrong /// offset shows up as a wrong value, not a coincidentally-right one. #[test] fn a_clips_slice_of_the_whole_scan_is_the_windows_its_own_read_would_have_found() { // 50 fps: one timeline frame is exactly one silence window // (`engine::silence::WINDOWS_PER_SEC` is 50), so the arithmetic is exact // and the test does not have to fight rounding to prove the offset. let fps = 50.; let whole: Vec = (0..40).map(|i| i as f32).collect(); let slice = slice_whole_levels(&whole, fps, 10, 20); assert_eq!(slice, (10..20).map(|i| i as f32).collect::>()); // The clip's own in point is where its slice starts, not the file's: // a take an hour into the film reads the same as one at its head. assert_eq!( slice_whole_levels(&whole, fps, 0, 5), vec![0., 1., 2., 3., 4.] ); // Past what the background scan has read so far (still running, or // cancelled) is not padded with zeroes -- the caller is told nothing is // there yet, same as [`SilenceScan`]'s own cancel leaves a prefix. assert_eq!( slice_whole_levels(&whole, fps, 35, 60), vec![35., 36., 37., 38., 39.] ); assert_eq!(slice_whole_levels(&whole, fps, 45, 60), Vec::::new()); // A rate that is not a rate reads nothing, the same refusal // `source_secs` makes for a background scan's own range. assert_eq!(slice_whole_levels(&whole, 0., 0, 10), Vec::::new()); } /// The import door's split, which is what keeps the render thread out of a /// header walk: what the worker probed and what the landing registers add up /// to exactly the rows, lengths and refusals the in-place import lands. #[test] fn probing_ahead_lands_exactly_what_importing_in_place_lands() { use std::sync::atomic::AtomicU8; let stage = AtomicU8::new(ImportStage::Header as u8); let plain = { let mut session = PlaybackSession::open(asset("test_av.mp4")).expect("open"); session.set_gain(0.0); session.import(&asset("test_av2.mp4")).expect("av2 matches"); ( session.sources().to_vec(), session.file_frames(&asset("test_av2.mp4")), session.timeline_duration(), ) }; let split = { let mut session = PlaybackSession::open(asset("test_av.mp4")).expect("open"); session.set_gain(0.0); let (subs, probe) = read_parts(&asset("test_av2.mp4"), &stage, Some(session.import_gate())); // This mp4 is walked now (an mp4 can carry `tx3g`) and has no // subtitle track in it, so the worker hands over an empty list -- // the same thing the import lands with. assert!(subs.expect("an mp4 is readable").is_empty()); let probe = probe .expect("a container is probed on the worker") .expect("av2 matches"); session .import_probed(&asset("test_av2.mp4"), probe) .expect("av2 matches"); ( session.sources().to_vec(), session.file_frames(&asset("test_av2.mp4")), session.timeline_duration(), ) }; assert_eq!(plain, split); // ...and it leaves the stage where the line can read it: a worker that // never announced its second read would show one that never ends. assert_eq!( ImportStage::from_u8(stage.load(std::sync::atomic::Ordering::Relaxed)), ImportStage::Subtitles ); // A refusal is the engine's refusal, and now it is *the* refusal: the // walk happens once, on the worker, so what the probe says is what the // notice shows -- worded exactly as importing in place worded it. let mut session = PlaybackSession::open(asset("test_av.mp4")).expect("open"); session.set_gain(0.0); let (_, probe) = read_parts( &asset("test_25fps.mp4"), &stage, Some(session.import_gate()), ); let on_the_worker = probe .expect("a container is probed") .map(|_| ()) .map_err(|e| e.to_string()); assert_eq!( on_the_worker, session .import(&asset("test_25fps.mp4")) .map(|_| ()) .map_err(|e| e.to_string()) ); // A timeline that moved while the worker read is not one the probe was // decided against, so the probe is not trusted -- the import is simply // taken the slow way and lands the same row. let mut moved = PlaybackSession::open(asset("test_av.mp4")).expect("open"); moved.set_gain(0.0); let (_, probe) = read_parts(&asset("test_av2.mp4"), &stage, Some(moved.import_gate())); moved .import(&asset("test_still.png")) .expect("a still joins"); let stale = probe.expect("a container is probed").expect("av2 matches"); assert_eq!( moved.import_probed(&asset("test_av2.mp4"), stale).ok(), Some(2), "a stale probe still lands the file, by the door that re-reads it" ); // A path nothing can read is refused by the worker now, in the engine's // own words: nothing walks it a second time to re-word it, and neither // half is a panic. let (subs, probe) = read_parts( std::path::Path::new("/no/such/film.mkv"), &stage, Some(session.import_gate()), ); assert!(subs.is_err(), "the cue walk comes back as a refusal"); assert!( probe.expect("a container is probed").is_err(), "and so does the container walk" ); } /// The import door's two halves, split across the worker hop: what /// [`read_ahead`] walks is exactly what the render thread would have walked, /// and pushing it says what walking it in place used to say. #[test] fn an_imports_subtitles_are_read_by_the_worker_and_only_pushed_here() { use std::sync::atomic::AtomicU8; let stage = AtomicU8::new(ImportStage::Header as u8); let film = asset("test_subs.mkv"); // The in-place walk (`subtitle_notice`) and the split one land the same // tracks and the same tail on the same timeline. let mut in_place = PlaybackSession::open(asset("test_av.mp4")).expect("open"); in_place.set_gain(0.0); let said = subtitle_notice(&mut in_place, &film); let mut split = PlaybackSession::open(asset("test_av.mp4")).expect("open"); split.set_gain(0.0); let (walked, _) = read_parts(&film, &stage, Some(split.import_gate())); let walked = walked.expect("the mkv is readable"); assert_eq!(subtitle_tail(&mut split, Ok(walked)), said); assert_eq!(split.subtitles().len(), in_place.subtitles().len()); // The same file twice is still one row, and still says so: the dedupe // lives in the push, which is the half that stayed here. let (again, _) = read_parts(&film, &stage, Some(split.import_gate())); let again = again.expect("the mkv is readable"); assert_eq!(subtitle_tail(&mut split, Ok(again)), None); // A standalone `.srt` is walked by the same worker: an import of one is // not a door that reads on the render thread either. let srt = Path::new(env!("CARGO_MANIFEST_DIR")) .join("../engine/tests/data/test_subs.srt") .canonicalize() .expect("the subtitle fixture"); assert_eq!( read_parts(&srt, &stage, Some(split.import_gate())) .0 .expect("the srt is readable") .len(), 1 ); // ...and a refusal is worded as a tail, never as a failed import. let unread: Subs = Err("nothing to read".into()); assert_eq!( subtitle_tail(&mut split, unread), Some(" — subtitles unread: nothing to read".to_string()) ); } #[test] fn every_codec_row_is_offered_or_says_why_not() { // One row per codec, and the boxes it can go in are the container row's // business: ten rows that pick (the picture ones included), two that say why they cannot. A codec // twice over (AV1 · MKV beside AV1 · MP4) was two rows asking the same // question, and five picture rows above the fold is what the card was // called unfriendly for. let offered: Vec<&[Format]> = FORMATS .iter() .map(|&(row, ..)| row) .filter(|row| !row.is_empty()) .collect(); assert_eq!( offered, vec![ &[Format::Mp4][..], &[Format::Av1, Format::Av1Mp4][..], &[Format::Hevc, Format::HevcMp4][..], &[Format::Wav][..], &[Format::Flac][..], &[Format::Mp3][..], &[Format::Ogg][..], &[Format::Opus][..], &[Format::Still, Format::PngSequence][..], &[Format::Gif][..], ] ); // Every format the engine writes is on exactly one row: one the card // cannot reach is one nobody can pick. for format in [ Format::Mp4, Format::Av1, Format::Av1Mp4, Format::Hevc, Format::HevcMp4, Format::Wav, Format::Flac, Format::Mp3, Format::Ogg, Format::Opus, Format::Still, Format::PngSequence, Format::Gif, ] { assert_eq!( FORMATS .iter() .filter(|(row, ..)| row.contains(&format)) .count(), 1, "{format:?}" ); } // The boxes the container row offers, and what the extension of each is // -- the destination follows the format, so these are what a file gets // named with. assert_eq!(containers(Format::Av1), [Format::Av1, Format::Av1Mp4]); assert_eq!(containers(Format::HevcMp4), [Format::Hevc, Format::HevcMp4]); assert_eq!(containers(Format::Mp3), [Format::Mp3]); assert_eq!(Format::Av1.ext(), "mkv"); assert_eq!(Format::Av1Mp4.ext(), "mp4"); assert_eq!(Format::Hevc.ext(), "mkv"); assert_eq!(Format::HevcMp4.ext(), "mp4"); // The container key walks the row and wraps, and does nothing at all // where there is only one box -- a stroke must not invent a choice the // card is not offering. assert_eq!(next_container(Format::Av1), Format::Av1Mp4); assert_eq!(next_container(Format::Av1Mp4), Format::Av1); assert_eq!(next_container(Format::Hevc), Format::HevcMp4); assert_eq!(next_container(Format::Mp4), Format::Mp4); assert_eq!(next_container(Format::Wav), Format::Wav); for (row, stroke, label, detail) in FORMATS { assert!(!label.is_empty(), "a row with no name"); // A refused row is a row with a reason, never a hidden one: an // empty detail column would read as an oversight. assert!(!detail.is_empty(), "{label} says nothing"); // Every row that can be picked has a key of its own, and no row // that cannot has one: the card is drivable without a pointer. assert_eq!(!row.is_empty(), !stroke.is_empty(), "{label}"); if let Some(&first) = row.first() { assert!( stroke.parse::().is_err(), "{label} takes a digit the bitrate needs" ); // Every box on one row is the same codec, so the quality rows // do not change meaning when the container does. assert!(row.iter().all(|f| f.has_video() == first.has_video())); } } // No two rows share a key, and none of them is a stroke the card already // answers to itself -- an ambiguous key is a key that picks the wrong // thing on a card that has no other input. let keys: Vec<&str> = FORMATS .iter() .map(|&(_, stroke, ..)| stroke) .filter(|stroke| !stroke.is_empty()) .collect(); for (i, key) in keys.iter().enumerate() { assert!(!keys[i + 1..].contains(key), "{key} picks two rows"); assert!( !["c", "q", "d", "g", "r", "enter", "backspace", ESCAPE].contains(key), "{key} is already the card's own" ); } // The codecs this program reads and cannot write are rows of their own, // refused by name rather than absent: VP9 and VP8, because AV1 is the // row that replaced them. Their reason travels with them, in the row or // the footer line that collects them -- either way it is on screen // without a click. OGG was the other one until `rusty_vorbis` gave this // project an encoder, and the row that says so is the row above. let (row, _, _, detail) = FORMATS .into_iter() .find(|(_, _, name, _)| *name == "VP9") .expect("VP9 has a row"); assert!(row.is_empty(), "VP9 is not offered"); assert!(detail.contains("replaces it"), "VP9: {detail}"); let (row, _, _, detail) = FORMATS .into_iter() .find(|(_, _, name, _)| *name == "VP8") .expect("VP8 has a row"); assert!(row.is_empty(), "VP8 is not offered"); assert!(detail.contains("replaces it"), "VP8: {detail}"); let (row, _, _, detail) = FORMATS .into_iter() .find(|(_, _, name, _)| *name == "Ogg") .expect("Ogg has a row"); assert_eq!(row, [Format::Ogg], "Ogg is a row that picks now"); assert!( detail.contains("Vorbis"), "the row names its codec in plain words: {detail}" ); assert!( FORMATS .into_iter() .any(|(row, _, _, detail)| row.contains(&Format::Hevc) && detail.contains("intra")) ); // The destination follows the format and keeps the stem, mp4 included. assert_eq!( retarget(std::path::Path::new("/a/take.export.mp4"), Format::Wav), std::path::Path::new("/a/take.export.wav") ); assert_eq!( retarget(std::path::Path::new("/a/take.export.wav"), Format::Mp4), std::path::Path::new("/a/take.export.mp4") ); } /// What the budget row says about the file before it is written: the size a /// rate makes over a span, in the unit the row states it in. #[test] fn the_budget_row_states_the_size_before_it_is_written() { // The rate as a person writes it, and the ratio one spelled out rather // than rounded to a rate nothing is written at. assert_eq!(fps_label(30.), "30"); assert_eq!(fps_label(24000. / 1001.), "23.976"); assert_eq!(fps_label(29.97002997), "29.97"); // The lever's own readout, one decimal, in the unit it is moved in -- // including below a megabit, which is where the floor lives now. assert_eq!(rate_label(6_000_000), "6.0"); assert_eq!(rate_label(6_500_000), "6.5"); assert_eq!(rate_label(600_000), "0.6"); assert_eq!(rate_label(BPS_MIN), "0.1"); } /// Which cue is on screen when: the whole of what the overlay decides, and /// the one piece of it that is arithmetic rather than layout. #[test] fn a_cue_is_on_screen_from_its_start_until_the_moment_it_ends() { use engine::subtitle::Cue; let cue = |start_us, end_us, text: &str| Cue { start_us, end_us, text: text.to_string(), image: None, words: Vec::new(), karaoke: Vec::new(), }; // Two cues that hand over exactly, and one that overlaps the second -- // a sign over a line of dialogue, which is two plates at one moment. let cues = [ cue(500_000, 1_500_000, "first line"), cue(1_500_000, 2_500_000, "second line"), cue(2_000_000, 2_200_000, "a sign"), ]; let at = |t: f64| -> Vec<&str> { cues_at(&cues, t) .into_iter() .map(|c| c.text.as_str()) .collect() }; // Before the first, between none of them: nothing, which is what makes // the overlay disappear rather than sit there empty. assert!(at(0.).is_empty()); assert!(at(0.4).is_empty()); assert!(at(3.).is_empty()); assert_eq!(at(0.5), ["first line"], "on at its own start"); assert_eq!(at(1.4), ["first line"]); // Half-open: the frame the first ends on is the second's, never both. assert_eq!(at(1.5), ["second line"]); assert_eq!(at(2.1), ["second line", "a sign"], "two at once stack"); assert_eq!(at(2.2), ["second line"], "the sign is over"); // ...and the end of the last one is the end of it. assert!(at(2.5).is_empty()); // A negative time is before everything rather than a panic: the // playhead is clamped, but nothing here depends on that. assert!(at(-1.).is_empty()); } /// The cue plate and the transient bars share the bottom edge of the picture, /// and the plate is the one that moves: a notice drawn across the line being /// read costs the reader both of them. #[test] fn a_cue_steps_over_whatever_bar_is_hanging_off_the_picture() { // Nothing hanging there, nothing moved: people know where the line sits, // and the no-notice position is the one they know, to the pixel. assert_eq!(sub_bottom(0.), SUB_BOTTOM); // A bar of any height is a bar the plate is clear of: its bottom edge is // above the bar's top edge, with the plate's own gap still under it. for bars in [1., 26., 48., 96., 300.] { assert!( sub_bottom(bars) >= bars + SUB_BOTTOM, "a {bars} px bar reaches the plate" ); } // A taller bar never lowers the plate, and a box that was never painted // (or measured as nothing) reads as no bar at all rather than as a plate // pulled off the bottom of the window. assert!(sub_bottom(48.) > sub_bottom(26.)); assert_eq!(sub_bottom(-10.), SUB_BOTTOM); } /// The two places a subtitle is drawn -- the plate over the picture and the /// lanes under the tracks -- inside the 640x360 floor the rest of this window /// is sized for, and the plate readable on whatever the film is showing. #[test] fn the_subtitle_plate_and_lanes_fit_the_smallest_window() { // A subtitle lane is a lane: it costs the panel exactly what a third // track costs it, and a timeline with none costs nothing at all -- the // strip that used to hang under the lanes is gone, and with it the one // row nothing could be dropped on. assert_eq!(lanes_h(3) - lanes_h(2), LANE_H + 8.); // What is left for the picture at the floor, with a project's two tracks // and a subtitle lane under them. let picture = 360. - HEADER_H - panel_h(3); assert!( picture > 0., "a subtitle lane pushed the picture off the window" ); // A two-line cue and the gap under it fit inside that, which is the // whole claim: the plate sits *over* the picture and must not need more // of it than there is. assert!( SUB_BOTTOM + 2. * SUB_LINE_H <= picture, "a two-line cue does not fit the smallest picture" ); // The text sits on its line rather than being clipped by it. const { assert!(SUB_LINE_H >= SUB_TEXT); } // White on the plate, not chrome on chrome: a cue is read against the // film, and this is the one pair here that has to survive any picture. for id in crate::ui::theme::PaletteId::ALL { assert!(contrast(id.palette().SUB_FG, 0x000000) >= 7., "{id:?}"); } // A subtitle lane is dragged onto, trimmed on and lifted from, so its row // is a target and binds `HIT_MIN` like every other lane's -- the whole // reason it is a lane and no longer a 16 px strip. const { assert!(LANE_H >= HIT_MIN); } } /// The subtitle style file, round-tripped through a scratch config directory /// -- the same corner-cut persistence as the theme and the keybindings, kept /// to the same test the load/save pair earns: a save followed by a load reads /// back what was written, a file that never existed leaves the defaults in /// force, and a file that exists but says nothing readable does too rather /// than crashing a startup on someone else's edit of the file. #[test] fn the_subtitle_style_file_round_trips_or_falls_back_to_defaults() { let _config_env = config_env_lock(); let dir = std::env::temp_dir().join(format!( "edith-subtitle-style-test-{}-{}", std::process::id(), std::time::SystemTime::now() .duration_since(std::time::UNIX_EPOCH) .unwrap() .as_nanos() )); std::fs::create_dir_all(&dir).unwrap(); // The shared process environment is serialized across every config // round-trip test by `config_env_lock`. crate::keymap::set_test_config_home(Some(dir.clone())); // Nothing written yet: the defaults, not a crash on a missing file. assert_eq!(load_subtitle_style(), SubStyle::default()); // A save, read back whole: family and size both. let iosevka = SubStyle { family: Some("Iosevka".to_string()), size: 22., ..SubStyle::default() }; save_subtitle_style(&iosevka).unwrap(); assert_eq!(load_subtitle_style(), iosevka); // The platform default (`None`) round-trips too, and is not a family // called "". let platform = SubStyle { size: 14., ..SubStyle::default() }; save_subtitle_style(&platform).unwrap(); assert_eq!(load_subtitle_style(), platform); // Every field the card can set reads back as set: outline, box and place // are not in the two-line file they grew out of, so each one has to // survive the trip on its own. let all = SubStyle { family: Some("Iosevka".to_string()), size: 26., outline: SubOutline::Thick, boxed: false, place: SubPlace::Top, }; save_subtitle_style(&all).unwrap(); assert_eq!(load_subtitle_style(), all); // A file present but unreadable as this format -- a stray edit, or bytes // from a future version -- leaves the defaults in force rather than // failing the window that opens over it. std::fs::write(subtitle_style_path(), "not a number\n9999\n").unwrap(); assert_eq!(load_subtitle_style(), SubStyle::default()); // A two-line file from before the outline, box and place existed still // loads its family and size, with the look it always had for the rest. std::fs::write(subtitle_style_path(), "Iosevka\n22\n").unwrap(); assert_eq!(load_subtitle_style(), iosevka); // A line this build does not know keeps its default, and the lines after // it are still read: a value from a future version costs that one field. std::fs::write(subtitle_style_path(), "Iosevka\n22\nextra-bold\noff\ntop\n").unwrap(); assert_eq!( load_subtitle_style(), SubStyle { boxed: false, place: SubPlace::Top, ..iosevka.clone() } ); crate::keymap::set_test_config_home(None); std::fs::remove_dir_all(&dir).ok(); } /// The subtitle card's pure rules: a slider position names a whole size inside /// the range at both ends and between, the outline walks its three steps and /// wraps, and each outline's copies sit where its width says they do. #[test] fn the_subtitle_card_sizes_outlines_and_offsets_are_bounded() { assert_eq!(sub_size_at(0.), SUB_SIZE_RANGE.0); assert_eq!(sub_size_at(1.), SUB_SIZE_RANGE.1); assert_eq!(sub_size_at(-3.), SUB_SIZE_RANGE.0); assert_eq!(sub_size_at(7.), SUB_SIZE_RANGE.1); assert_eq!(sub_size_at(0.5), 19.); assert_eq!(sub_size_at(0.52), 19.); assert_eq!(SubOutline::None.step(1), SubOutline::Thin); assert_eq!(SubOutline::Thick.step(1), SubOutline::None); assert_eq!(SubOutline::None.step(-1), SubOutline::Thick); assert_eq!(SubOutline::Thin.step(-1), SubOutline::None); assert!(SubOutline::None.offsets().is_empty()); assert_eq!(SubOutline::Thin.offsets().len(), 4); assert_eq!(SubOutline::Thick.offsets().len(), 8); for outline in [SubOutline::Thin, SubOutline::Thick] { for &(dx, dy) in outline.offsets() { assert!( dx != 0. || dy != 0., "a copy on the text itself draws nothing" ); assert!(dx.abs() <= 2. && dy.abs() <= 2.); } } } /// The auto-proxies default, round-tripped through a scratch config /// directory the same way the subtitle style is above -- the door D1 wired /// it through, so this is what the settings page header can now honestly /// call the same claim it makes about Proxies: a project's own line always /// beats this one once one is open ([`engine::edith::Document::auto_proxy`]), /// but with nothing open (or nothing saved since the flip) this is what a /// relaunch reads instead of the hardcoded Off. #[test] fn the_auto_proxies_default_round_trips_through_its_own_config_file_or_falls_back_to_off() { let _config_env = config_env_lock(); let dir = std::env::temp_dir().join(format!( "edith-auto-proxies-pref-test-{}-{}", std::process::id(), std::time::SystemTime::now() .duration_since(std::time::UNIX_EPOCH) .unwrap() .as_nanos() )); std::fs::create_dir_all(&dir).unwrap(); // `config_env_lock` serializes every test that changes this process-wide // path. crate::keymap::set_test_config_home(Some(dir.clone())); // Nothing written yet: Off, the default. assert!(!load_auto_proxies_pref(), "a missing file must read as Off"); save_auto_proxies_pref(false).unwrap(); assert!(!load_auto_proxies_pref(), "off must round-trip"); save_auto_proxies_pref(true).unwrap(); assert!( load_auto_proxies_pref(), "on must round-trip too, not just off" ); // Garbled bytes -- a stray edit, or a future dialect -- read as Off rather // than failing a startup on someone else's edit of the file. std::fs::write(auto_proxies_pref_path(), "not on or off\n").unwrap(); assert!( !load_auto_proxies_pref(), "unreadable content must fall back to Off" ); crate::keymap::set_test_config_home(None); std::fs::remove_dir_all(&dir).ok(); } /// The cue's line height scales with its size, on the same ratio the defaults /// draw at -- a bigger plate wants more room per line, not letters growing /// inside an unchanged one. #[test] fn the_cue_line_height_scales_with_its_size() { assert_eq!(sub_line_h_for(SUB_TEXT), SUB_LINE_H); // Twice the size is twice the line height, on the same ratio. assert_eq!(sub_line_h_for(SUB_TEXT * 2.), SUB_LINE_H * 2.); // Always at least the text itself, across the whole size range: a line // shorter than its own text would clip it. for size in [SUB_SIZE_RANGE.0, SUB_TEXT, SUB_SIZE_RANGE.1] { assert!(sub_line_h_for(size) >= size); } } /// The two doors subtitles arrive through, end to end on the fixtures: a /// file beside the media and the tracks inside an mkv, what the overlay then /// says at a given moment, and where the strip draws it. #[test] fn subtitles_arrive_beside_the_media_and_inside_it() { // Which door a path goes through is decided before anything is opened. // ...and a `.mks` is one, the subtitles of a Matroska file alone: it has // no source in it to import, so the drop door has to send it where `+ S` // sends it. Its two siblings are media and must not come here -- a // `.mka` is a song this would stop importing. for name in [ "subs.srt", "SUBS.SRT", "a.vtt", "a.ass", "a.ssa", "s.mks", "S.MKS", ] { assert!(is_subtitle(Path::new(name)), "{name}"); } for name in ["a.mp4", "a.mkv", "song.mka", "film.mk3d", "notes.txt", "a"] { assert!(!is_subtitle(Path::new(name)), "{name}"); } // Every container the engine can walk for tracks inside it, Matroska // and ISO-BMFF alike -- an mp4 carries `tx3g`, so the app has to ask it. // The Matroska half is the engine's own closed set, extension for // extension, so no file is walked here that it would refuse there. for name in [ "film.MKV", "clip.webm", "clip.mp4", "a.m4v", "a.MOV", "song.mka", "s.mks", "f.MK3D", ] { assert!(carries_subtitles(Path::new(name)), "{name}"); assert!( engine::demux::is_matroska(Path::new(name)) || matches!( Path::new(name) .extension() .and_then(|e| e.to_str()) .map(str::to_ascii_lowercase) .as_deref(), Some("mp4" | "m4v" | "mov") ), "{name} is walked by the app but not by the engine" ); } for name in ["song.wav", "still.png", "notes.txt", "a"] { assert!(!carries_subtitles(Path::new(name)), "{name}"); } // The three doors, on the one file that used to split them: what the // drop/argv door does with it ([`Player::take_import`] routes on // `is_subtitle`), what the worker reads for it ([`walk_subtitles`]), and // what `+ S` reads ([`Player::add_subtitles`] -> `parse_subtitles`) are // the same walk of the same bytes. let mks = asset("test_subs.mks"); assert!(is_subtitle(&mks), "the drop door takes it as subtitles"); let dropped = walk_subtitles(&mks).expect("the drop door's worker reads it"); let plus_s = PlaybackSession::parse_subtitles(&mks).expect("`+ S` reads it"); assert!(!dropped.is_empty(), "and there are tracks in it"); assert_eq!( dropped.iter().map(|t| &t.label).collect::>(), plus_s.iter().map(|t| &t.label).collect::>(), ); let mut session = PlaybackSession::open(asset("test_av.mp4")).expect("open the fixture"); session.set_gain(0.0); // Beside the media: the drop door's own call, and the row it makes. let srt = Path::new(env!("CARGO_MANIFEST_DIR")) .join("../engine/tests/data/test_subs.srt") .canonicalize() .expect("the subtitle fixture"); assert_eq!(session.import_subtitles(&srt).expect("the .srt imports"), 1); let track = &session.subtitles()[0]; assert_eq!(track.label, "test_subs.srt"); assert_eq!(subtitle_detail(track), "3 cues"); // Arriving is landing in the *list* and nowhere else: no lane is made for // it, so there is nothing over the picture yet -- the rule every other // medium follows here, and what `Player::subtitle_overlay` reads. assert!( session.subtitle_lanes().is_empty(), "an import places nothing: the palette is where a track lands" ); // What the track itself says at a moment inside the first cue, and between // two of them -- the fixture's own timings (0.5-1.5 s, 2.0-3.25 s). let text = |t: f64| -> Vec { cues_at(&track.cues, t) .into_iter() .map(|c| c.text.clone()) .collect() }; assert_eq!(text(0.7), ["first line"]); assert!(text(1.8).is_empty(), "between two cues the plate goes"); assert_eq!(text(2.5), ["second line\nwith a break"]); // Where the strip puts that first cue: 0.5 s in at 60 px to the second // is 30 px along, and a second of cue is 60 px wide. let scale = Scale::default(); assert_eq!(scale.pps, PPS_DEFAULT); assert_eq!(cue_box(scale, &track.cues[0]), (30., 60.)); // Zoomed right out, a cue worth a fraction of a pixel is still a mark. let far = Scale { pps: 0.01, start: 0., }; assert_eq!(cue_box(far, &track.cues[0]).1, SUB_CUE_MIN_W); // ...and scrolled past, its left edge is negative, exactly as a clip // box's is: the bed clips it. let scrolled = Scale { pps: PPS_DEFAULT, start: 1., }; assert_eq!(cue_box(scrolled, &track.cues[0]).0, -30.); // Inside the media: the tracks of an mkv, taken by the same call every // open and import door makes. Two of them in the fixture, named by what // the file says rather than by number. let notice = subtitle_notice(&mut session, &asset("test_subs.mkv")).expect("the mkv carries two"); assert!(notice.contains('2'), "{notice}"); assert_eq!(session.subtitles().len(), 3); assert_eq!(session.subtitles()[1].label, "eng"); assert_eq!(session.subtitles()[2].label, "fra — Signs"); assert_eq!(subtitle_detail(&session.subtitles()[1]), "3 cues"); // The same file twice adds nothing and says nothing. assert_eq!(subtitle_notice(&mut session, &asset("test_subs.mkv")), None); // An mp4 is walked too (it can carry `tx3g`); this one holds none, so // the notice grows no tail. assert_eq!(subtitle_notice(&mut session, &asset("test_av.mp4")), None); // A track that could not be read says why, where its cue count would // be: what the greyed library row prints, and the whole difference // between "this film has no subtitles" and "these four are pictures". let refused = engine::subtitle::SubtitleTrack { path: PathBuf::from("/a/remux.mkv"), track: Some(1), // Neither field, like `SubtitleTrack::refused` leaves them: what is // refused is never written, and the row keeps the label it was // refused under. language: String::new(), name: String::new(), label: "eng".into(), cues: Vec::new(), bitmap: false, refused: Some("S_HDMV/PGS subtitles are pictures, not text".into()), }; assert!(subtitle_detail(&refused).contains("pictures")); assert!(cues_at(&refused.cues, 1.).is_empty()); // ...and what an embedded track carried through a *cut* maps to: the timeline's // own clock, asked of the engine through the very map an export writes // the file with (`PlaybackSession::timeline_cues`), so the preview and // the file cannot drift apart. The numbers are the export's own // (`export::a_subtitle_file_beside_the_media_keeps_the_timelines_own_ // clock`): 0.5s..2.5s rippled out of a five-second timeline leaves // three seconds, which clips the second cue and takes the third away // altogether -- while the track itself still holds all three, which is // exactly why the drawing may not read them straight. let lanes = session.lanes(); session .cut_regions(&[(15, 60)], &lanes) .expect("cut 0.5s..2.5s out"); assert_eq!(session.timeline_duration(), 3.0); assert_eq!( session.subtitles()[0].cues.len(), 3, "the track is untouched" ); let mapped = session.timeline_cues(0); assert_eq!( mapped .iter() .map(|c| (c.start_us, c.end_us)) .collect::>(), vec![(500_000, 1_500_000), (2_000_000, 3_000_000)] ); // The export's own two lines, at the same moments as above. let drawn = |t: f64| -> Vec { cues_at(&mapped, t) .into_iter() .map(|c| c.text.clone()) .collect() }; assert_eq!(drawn(0.7), ["first line"]); assert_eq!(drawn(2.5), ["second line\nwith a break"]); assert!( drawn(4.2).is_empty(), "a cue past the cut end is drawn where the file writes none" ); // ...and the strip's: the second cue is one second wide now, not 1.25. assert_eq!(cue_box(scale, &mapped[1]), (120., 60.)); // The last mile, what the plate over the picture actually reads // (`Player::subtitle_overlay` -> `PlaybackSession::sub_lane_cues`): a lane // added and still empty draws nothing, and the palette row reaches the // screen the moment it is *placed* on it -- shifted by where it sits, the // way a clip is. let lane = session.add_lane(LaneKind::Subtitle); assert!( session.sub_lane_cues(lane).is_empty(), "an empty subtitle lane draws nothing" ); session .place_sub( lane, 0, SubClip { start: 0, frames: 90, track: 0, in_us: 0, out_us: 5_000_000, link: None, }, ) .expect("the palette row goes down on the lane"); let placed = session.sub_lane_cues(lane); let shown = |t: f64| -> Vec { cues_at(&placed, t) .into_iter() .map(|c| c.text.clone()) .collect() }; // The placement holds five seconds of the track in a three-second box, // so the words cross the plate by the placement's own proportion: the // cue times compress with it (first line [0.5, 1.5) -> [0.3, 0.9), // second [2, 3.25) -> [1.2, 1.95)) -- a unity placement would keep the // file's own times, to the microsecond. assert_eq!(shown(0.7), ["first line"]); assert!(shown(0.95).is_empty(), "between two cues the plate goes"); assert_eq!(shown(1.5), ["second line\nwith a break"]); } /// One lane over the picture and never two ([`Player::active_sub_lane`]): which /// one, at every moment a stack of subtitle lanes goes through -- none, one, /// three, a pick, and the pick's lane going away under it. #[test] fn exactly_one_subtitle_lane_is_shown_whatever_the_pick_and_whatever_is_left() { let sub = |ord| Lane::new(LaneKind::Subtitle, ord); let (s1, s2, s3) = (sub(0), sub(1), sub(2)); // Nowhere for words to be: no lane, nothing shown -- and a pick left over // from a timeline that has been closed does not resurrect one. assert_eq!(active_lane(None, &[]), None); assert_eq!(active_lane(Some(s2), &[]), None); // One lane is shown by being the only one: nobody has to pick anything, // which is what the single-lane timeline always did. assert_eq!(active_lane(None, &[s1]), Some(s1)); // Three, unpicked: the first, so adding lanes never blanks the picture. assert_eq!(active_lane(None, &[s1, s2, s3]), Some(s1)); // ...and picked: that one alone, however many are there. assert_eq!(active_lane(Some(s3), &[s1, s2, s3]), Some(s3)); // The picked lane taken off the timeline -- a removal, or the undo of the // add that made it: the first lane left is shown, never nothing. assert_eq!(active_lane(Some(s3), &[s1, s2]), Some(s1)); // A pick that is not a subtitle lane at all cannot name one either. assert_eq!(active_lane(Some(Lane::V1), &[s1, s2]), Some(s1)); } /// A library preview's whole point ([`Player::open_preview`], /// [`Player::close_preview`]): the timeline's own session is untouched by /// one showing over it. `Player` cannot be built here -- it takes a gpui /// `Context`, which this test binary has no `TestAppContext` to hand it /// (see the crate's other tests for the same limit) -- so this exercises /// the two sessions `open_preview`/`close_preview` juggle directly: seeking /// and playing one, exactly as a preview session's own life does, leaves /// the other's position and clock exactly where they were. #[test] fn a_second_session_playing_never_moves_the_first() { let mut timeline = PlaybackSession::open(asset("test_av.mp4")).expect("open the fixture"); timeline.seek(0.5); let before = timeline.now(); let before_playing = timeline.is_playing(); // Stands in for the preview session `open_preview` opens over it. let mut preview = PlaybackSession::open(asset("test_av.mp4")).expect("open the fixture"); preview.seek(1.0); preview.play(); assert_eq!( timeline.now(), before, "the preview's seek moved the wrong session" ); assert_eq!( timeline.is_playing(), before_playing, "the preview's play moved the wrong session" ); } /// The frozen-preview regression: `render` used to call `tick` on /// `self.session` unconditionally, so a preview session's clock -- the one /// [`Player::pump`] actually reads while previewing -- was never advanced /// and the picture sat on frame 0 forever. The fix routes `tick` through /// [`Player::active_session_mut`], the same preview-first precedence /// `pump`/`transport` already use. This proves `tick` is what a session /// needs to keep its own clock honest, and that ticking one session leaves /// an untouched sibling exactly where it was -- so routing it through /// whichever session is active cannot disturb the other one. #[test] fn ticking_the_preview_session_never_moves_the_timeline() { let mut timeline = PlaybackSession::open(asset("test_av.mp4")).expect("open the fixture"); timeline.seek(0.5); let before = timeline.now(); let mut preview = PlaybackSession::open(asset("test_av.mp4")).expect("open the fixture"); preview.play(); // Stands in for what `render` now does every frame while a preview is // showing: only the active session -- the preview -- gets ticked. preview.tick(); assert_eq!( timeline.now(), before, "ticking the preview moved the timeline's clock" ); } /// The monitoring level is the *window's*, and a preview closing must not hand /// the timeline back the level it had before the preview opened: muting during /// a preview and pressing `esc` used to leave the timeline playing at full /// volume with the control still reading "muted" -- the shown level and the /// heard level disagreed and nothing said so. /// /// What is driven is the real push, [`push_gain`] -- the function /// [`apply_volume`](crate::player::transport::Player::apply_volume) is nothing /// but a call to, over the same two session slots a `Player` holds -- and the /// answer is read back out of the *device* ([`PlaybackSession::gain`]), not /// out of the window. `Player` itself cannot be built here (it takes a gpui /// `Context` this binary has no `TestAppContext` to build; the sibling preview /// tests say the same), so the door that calls the push is pinned by the scan /// below rather than executed. #[test] fn a_preview_close_leaves_the_timeline_at_the_level_the_window_shows() { let timeline = PlaybackSession::open(asset("test_av.mp4")).expect("open the fixture"); // The control: an untouched session is at full volume, so a level read back // as 1.0 is a real answer and not a default this test could pass on. assert_eq!( timeline.gain(), Some(1.0), "a fresh session is not at full volume" ); // Stands in for `open_preview`: a second session arrives, and the window is // muted while it is the one on screen. let preview = PlaybackSession::open(asset("test_av.mp4")).expect("open the fixture"); let muted = 0.0; push_gain(muted, [Some(&timeline), Some(&preview)]); // The preview goes away -- `close_preview` takes it and pushes at what is // left. The session that takes the picture must be the session heard at // the level the control shows. drop(preview); push_gain(muted, [Some(&timeline), None]); assert_eq!( timeline.gain(), Some(muted), "closing a preview handed the timeline back a level the window is not showing" ); // The other half of the regression, and the half a single-session test can // never see: a push that reached only the *active* session (the preview) // left the timeline exactly where it was, which is the reported bug. The // pre-fix shape is therefore the control that makes the assertion above // mean something. let control = PlaybackSession::open(asset("test_av.mp4")).expect("open the fixture"); let other = PlaybackSession::open(asset("test_av.mp4")).expect("open the fixture"); push_gain(muted, [Some(&other), None]); assert_eq!( control.gain(), Some(1.0), "a session outside the push followed it, so the read above proves nothing" ); assert_eq!( other.gain(), Some(muted), "the pushed session ignored the level" ); } /// The wiring the round trip above cannot build a `Player` for: the level /// reaches *both* session slots, and both doors that install or drop a session /// push it. A push at the active session alone is the regression, and it is /// invisible to any test that only ever holds one session. #[test] fn the_window_level_is_pushed_at_every_session_and_on_every_swap() { let apply = fn_body("apply_volume"); assert!( apply.contains("self.session.as_ref()") && apply.contains("self.preview_session.as_ref()"), "the level is pushed at one session only, so a preview closing hands the \ timeline back the level from before the preview opened" ); assert!( !apply.contains("self.active_session()"), "the push still goes through the active session, which is the one-session \ rule this replaced" ); for door in ["open_preview", "close_preview"] { assert!( fn_body(door).contains("self.apply_volume();"), "{door} swaps the session on screen without telling the new one what \ level the window is showing" ); } } /// [`bgra_to_rgba`] and [`save_screenshot`]'s own PNG write, round-tripped: a /// synthetic 2x2 frame -- one pixel per corner, alpha and channel order all /// distinct -- decodes back to the same pixels a viewer would see, in RGBA. #[test] fn a_screenshot_frame_round_trips_through_its_png() { // BGRA, gpui's own layout: red top-left, green top-right, blue // bottom-left, and a half-alpha white bottom-right. #[rustfmt::skip] let bgra: Vec = vec![ 0, 0, 255, 255, 0, 255, 0, 255, 255, 0, 0, 255, 255, 255, 255, 128, ]; let rgba = bgra_to_rgba(&bgra); assert_eq!( rgba[0..4], [255, 0, 0, 255], "red survives the channel swap" ); assert_eq!(rgba[4..8], [0, 255, 0, 255], "green is unmoved by the swap"); assert_eq!(rgba[8..12], [0, 0, 255, 255], "blue swaps into place"); assert_eq!(rgba[12..16], [255, 255, 255, 128], "alpha is untouched"); let dir = std::env::temp_dir().join(format!("edith-screenshot-test-{}", std::process::id())); std::fs::create_dir_all(&dir).expect("scratch dir"); let path = dir.join("roundtrip.png"); // Written by the screenshot's own encoder and read back by `image`, an // independent decoder, so the round trip crosses both crates. let png = ec_image::png_encode::encode_rgba8(2, 2, &rgba).expect("2x2 rgba png"); std::fs::write(&path, png).expect("png write"); let decoded = image::open(&path).expect("png read").to_rgba8(); assert_eq!( decoded.into_raw(), rgba, "the PNG round-trips the pixels exactly" ); let _ = std::fs::remove_dir_all(&dir); } /// [`screenshot_path`]'s no-clobber rule: the same stem and timecode taken /// twice in a row lands on `-2`, and a third time on `-3` -- a screenshot /// never overwrites the one before it, unlike an export's own path. #[test] fn a_repeated_screenshot_name_gets_a_numeric_suffix() { let dir = std::env::temp_dir().join(format!("edith-screenshot-suffix-{}", std::process::id())); std::fs::create_dir_all(&dir).expect("scratch dir"); let first = screenshot_path(&dir, "A Film", "00-01-02"); assert_eq!(first, dir.join("A Film-00-01-02.png")); std::fs::write(&first, b"stand-in for a PNG").expect("write the first"); let second = screenshot_path(&dir, "A Film", "00-01-02"); assert_eq!(second, dir.join("A Film-00-01-02-2.png")); std::fs::write(&second, b"stand-in for a PNG").expect("write the second"); let third = screenshot_path(&dir, "A Film", "00-01-02"); assert_eq!(third, dir.join("A Film-00-01-02-3.png")); // A different timecode never collides with either of the above. let elsewhere = screenshot_path(&dir, "A Film", "00-01-03"); assert_eq!(elsewhere, dir.join("A Film-00-01-03.png")); let _ = std::fs::remove_dir_all(&dir); } /// A first import lands on an empty timeline and fits it; a later one only /// joins the library, and a Media row's double-click adds at the playhead /// rather than previewing. Read from the sources because this binary has no /// `TestAppContext` to drive the window (see `darkroom_source_rows_keep_...`). #[test] fn the_first_import_lands_and_fits_and_a_row_double_click_adds_at_the_playhead() { let library = src_text("player/library.rs"); let take = &library[library.find("fn take_import(").expect("take_import")..]; for needle in [ "self.insert_source(path, 0, None, None, cx);", "self.zoom_fit(cx);", ] { assert!( take.contains(needle), "the first import lost `{needle}`: an empty timeline must take the file" ); } let dock = src_text("ui/dock_stance.rs"); let row = &dock[dock.find("fn source_gestures(").expect("Darkroom source gestures")..]; assert!( row.contains("this.open_preview(&click_path, stream, cx);"), "a Media row's double-click opens the source viewer" ); } /// The time estimate is always "about", in seconds under a minute and a half /// and in minutes after, and a faster machine writes the same film sooner. #[test] fn the_write_time_is_about_and_follows_the_pace() { use crate::export_ui::time_words; assert_eq!(time_words(60., Some(1.)), "about 60 s"); assert_eq!(time_words(600., Some(1.)), "about 10 min"); assert_eq!(time_words(600., Some(4.)), "about 2 min 30 s"); assert_eq!(time_words(100., Some(1.)), "about 1 min 40 s"); // Never a fake "0 s", and no number at all before a pace was measured. assert_eq!(time_words(0., Some(1.)), "about 1 s"); assert_eq!(time_words(60., None), "estimating\u{2026}"); } /// The playback plate draws a non-plain caption style with the engine's own /// burn function, so the word being spoken moves with the playhead: two frames /// inside one karaoke cue are two different pictures, a held pose is the same /// key (no redrawing), and the default style is not routed through it at all. #[test] fn the_plate_draws_the_animated_caption_and_it_changes_inside_one_cue() { use engine::captionanim::{CaptionAnim, CaptionStyle}; use engine::subtitle::Cue; let fps = 25.0; let cue = Cue { start_us: 1_000_000, end_us: 3_000_000, text: "one two three four".to_string(), image: None, karaoke: Vec::new(), // Uneven on purpose: "three" owns most of the cue. words: vec![ (1_000_000, 1_200_000, "one".into()), (1_200_000, 1_400_000, "two".into()), (1_400_000, 2_800_000, "three".into()), (2_800_000, 3_000_000, "four".into()), ], }; let cues = [cue]; let karaoke = engine::text::TextStyle { caption: CaptionStyle { anim: CaptionAnim::Karaoke, ..CaptionStyle::default() }, ..engine::text::TextStyle::default() }; let draw = |style: &engine::text::TextStyle, secs: f64| { let frame = crate::subs::frame_at(secs, fps); let (key, blocks) = crate::subs::animated_caption_key(style, &cues, frame, fps)?; let (w, h, px) = crate::subs::animated_caption_bitmap(style, &blocks, (1280, 720))?; assert_eq!((w, h), (960, 540), "the canvas keeps the project's shape"); assert!(px.as_chunks::<4>().0.iter().any(|p| p[3] > 0), "the caption drew something"); Some((key, px)) }; let early = draw(&karaoke, 1.1).expect("a cue is up at 1.1 s"); let late = draw(&karaoke, 2.0).expect("a cue is up at 2.0 s"); assert_ne!(early.0, late.0, "the word being spoken changed, so did the key"); assert_ne!(early.1, late.1, "and so did the picture"); let held = draw(&karaoke, 2.1).expect("up"); assert_eq!(late.0, held.0, "the same word is the same key: a held pose is not redrawn"); assert_eq!(late.1, held.1); assert!(crate::subs::cues_at(&cues, 3.5).is_empty(), "nothing is handed over between cues"); }