| 1 | |
| 2 | |
| 3 | |
| 4 | |
| 5 | use crate::titles::{Rect, active_titles, contain, em_px, opacity, outline_offsets, title_area}; |
| 6 | use engine::Title; |
| 7 | use engine::text::{Outline, TextStyle}; |
| 8 | |
| 9 | fn rect(x: f32, y: f32, w: f32, h: f32) -> Rect { |
| 10 | Rect { x, y, w, h } |
| 11 | } |
| 12 | |
| 13 | fn title(start: u32, frames: u32, fade_in: u32, fade_out: u32) -> Title { |
| 14 | Title { |
| 15 | start, |
| 16 | frames, |
| 17 | text: "Chapter one".into(), |
| 18 | style: TextStyle::default(), |
| 19 | fade_in, |
| 20 | fade_out, |
| 21 | in_anim: Default::default(), |
| 22 | out_anim: Default::default(), |
| 23 | anim_frames: 0, |
| 24 | keys: Vec::new(), |
| 25 | } |
| 26 | } |
| 27 | |
| 28 | |
| 29 | |
| 30 | #[test] |
| 31 | fn a_wide_picture_fits_the_width_and_centres_vertically() { |
| 32 | let fitted = contain(rect(0., 0., 1000., 1000.), 1920., 1080.); |
| 33 | assert!((fitted.w - 1000.).abs() < 1e-3, "{fitted:?}"); |
| 34 | assert!((fitted.h - 562.5).abs() < 1e-3, "{fitted:?}"); |
| 35 | assert!((fitted.x - 0.).abs() < 1e-3, "{fitted:?}"); |
| 36 | assert!((fitted.y - 218.75).abs() < 1e-3, "{fitted:?}"); |
| 37 | } |
| 38 | |
| 39 | |
| 40 | |
| 41 | #[test] |
| 42 | fn a_square_picture_in_a_wide_region_is_pillarboxed_from_the_region_origin() { |
| 43 | let fitted = contain(rect(10., 20., 1920., 1080.), 1080., 1080.); |
| 44 | assert!((fitted.h - 1080.).abs() < 1e-3, "{fitted:?}"); |
| 45 | assert!((fitted.w - 1080.).abs() < 1e-3, "{fitted:?}"); |
| 46 | assert!((fitted.x - 430.).abs() < 1e-3, "{fitted:?}"); |
| 47 | assert!((fitted.y - 20.).abs() < 1e-3, "{fitted:?}"); |
| 48 | } |
| 49 | |
| 50 | |
| 51 | |
| 52 | #[test] |
| 53 | fn an_empty_picture_or_region_fits_to_the_region() { |
| 54 | let region = rect(0., 0., 800., 450.); |
| 55 | assert_eq!(contain(region, 0., 1080.), region); |
| 56 | assert_eq!(contain(rect(0., 0., 0., 450.), 1920., 1080.), rect(0., 0., 0., 450.)); |
| 57 | } |
| 58 | |
| 59 | |
| 60 | |
| 61 | |
| 62 | #[test] |
| 63 | fn a_title_size_is_a_permille_of_the_displayed_height() { |
| 64 | let style = TextStyle { |
| 65 | size: 50, |
| 66 | ..TextStyle::default() |
| 67 | }; |
| 68 | assert!((em_px(&style, 1080.) - 54.).abs() < 1e-4); |
| 69 | assert!((em_px(&style, 540.) - 27.).abs() < 1e-4); |
| 70 | let tiny = TextStyle { |
| 71 | size: 10, |
| 72 | ..TextStyle::default() |
| 73 | }; |
| 74 | assert_eq!(em_px(&tiny, 100.), 6.); |
| 75 | } |
| 76 | |
| 77 | |
| 78 | |
| 79 | |
| 80 | #[test] |
| 81 | fn title_words_sit_inside_the_five_percent_safe_margin() { |
| 82 | let area = title_area(rect(100., 50., 1920., 1080.)); |
| 83 | assert!((area.x - 154.).abs() < 1e-3, "{area:?}"); |
| 84 | assert!((area.y - 104.).abs() < 1e-3, "{area:?}"); |
| 85 | assert!((area.w - 1812.).abs() < 1e-3, "{area:?}"); |
| 86 | assert!((area.h - 972.).abs() < 1e-3, "{area:?}"); |
| 87 | } |
| 88 | |
| 89 | |
| 90 | |
| 91 | #[test] |
| 92 | fn the_outline_copies_follow_the_engines_stroke_radius() { |
| 93 | assert!(outline_offsets(Outline::None, 54.).is_empty()); |
| 94 | assert_eq!( |
| 95 | outline_offsets(Outline::Thin, 54.), |
| 96 | vec![(-2., -2.), (2., -2.), (-2., 2.), (2., 2.)] |
| 97 | ); |
| 98 | let thick = outline_offsets(Outline::Thick, 54.); |
| 99 | assert_eq!(thick.len(), 8); |
| 100 | assert!(thick.iter().all(|&(dx, dy)| { |
| 101 | [dx, dy].iter().all(|c| [-5., 0., 5.].contains(c)) |
| 102 | })); |
| 103 | assert_eq!(outline_offsets(Outline::Thin, 6.), vec![(-1., -1.), (1., -1.), (-1., 1.), (1., 1.)]); |
| 104 | } |
| 105 | |
| 106 | |
| 107 | #[test] |
| 108 | fn opacity_is_the_alpha_as_a_fraction() { |
| 109 | assert_eq!(opacity(0), 0.); |
| 110 | assert_eq!(opacity(255), 1.); |
| 111 | assert!((opacity(128) - 128. / 255.).abs() < 1e-6); |
| 112 | } |
| 113 | |
| 114 | |
| 115 | |
| 116 | |
| 117 | #[test] |
| 118 | fn the_titles_up_at_a_frame_are_listed_in_order_with_their_alpha() { |
| 119 | let early = title(0, 15, 5, 0); |
| 120 | let late = title(10, 20, 0, 0); |
| 121 | let titles = [early.clone(), late.clone()]; |
| 122 | |
| 123 | let at_0 = active_titles(&titles, 0); |
| 124 | assert_eq!(at_0.len(), 1); |
| 125 | assert_eq!(at_0[0].0, &early); |
| 126 | assert_eq!(at_0[0].1, 42); |
| 127 | |
| 128 | let at_12 = active_titles(&titles, 12); |
| 129 | let order: Vec<&Title> = at_12.iter().map(|(t, _)| *t).collect(); |
| 130 | assert_eq!(order, vec![&early, &late]); |
| 131 | assert_eq!(at_12[0].1, 255); |
| 132 | |
| 133 | let at_25 = active_titles(&titles, 25); |
| 134 | assert_eq!(at_25.len(), 1); |
| 135 | assert_eq!(at_25[0].0, &late); |
| 136 | assert!(active_titles(&titles, 35).is_empty()); |
| 137 | } |
| 138 | |
| 139 | |
| 140 | |
| 141 | #[test] |
| 142 | fn a_title_is_up_for_exactly_its_frames_and_fades_out_within_them() { |
| 143 | let faded = title(0, 20, 0, 5); |
| 144 | let titles = [faded]; |
| 145 | assert!(active_titles(&titles, 19)[0].1 > 0); |
| 146 | assert!(active_titles(&titles, 20).is_empty()); |
| 147 | let last = active_titles(&titles, 19)[0].1; |
| 148 | let before = active_titles(&titles, 15)[0].1; |
| 149 | assert!(last < before && last > 0, "fade-out ramps: {before} -> {last}"); |
| 150 | } |
| 151 | |
| 152 | |
| 153 | use super::src_text; |
| 154 | |
| 155 | #[test] |
| 156 | fn new_title_is_a_file_new_row_with_alt_t_and_the_t_key_stays_subtitles() { |
| 157 | use crate::ui::menubar::{Entry, Title as MenuTitle}; |
| 158 | fn walk(list: &[Entry], a: crate::keymap::ActionId) -> bool { |
| 159 | list.iter().any(|e| match e { |
| 160 | Entry::Sub(_, inner) => walk(inner, a), |
| 161 | Entry::Act(x) | Entry::Switch(x) | Entry::Choice(x) => *x == a, |
| 162 | Entry::Rule => false, |
| 163 | }) |
| 164 | } |
| 165 | assert!(walk(MenuTitle::Timeline.layout(), crate::keymap::ActionId::NewTitle)); |
| 166 | let k = crate::keymap::Keymap::defaults(); |
| 167 | assert_eq!(k.display(crate::keymap::ActionId::NewTitle), "Alt+T"); |
| 168 | assert_eq!(k.lookup("t", false, false, false), Some(crate::keymap::ActionId::ToggleSubtitles)); |
| 169 | assert!(src_text("player/actions.rs").contains("ActionId::NewTitle => self.new_title(cx)")); |
| 170 | } |
| 171 | |
| 172 | #[test] |
| 173 | fn the_title_section_is_in_the_clip_tab_and_every_control_is_one_write() { |
| 174 | let dock = src_text("ui/dock_stance.rs"); |
| 175 | assert!(dock.contains("title_card(room, cx)")); |
| 176 | let card = src_text("ui/cards/title.rs"); |
| 177 | for needle in ["apply_title_preset", "style.place", "style.outline", "style.boxed", "ScrubField::AnimLength", "title_anim_row", "ScrubField::Size", "remove_title_selected"] { |
| 178 | assert!(card.contains(needle), "{needle}"); |
| 179 | } |
| 180 | assert!(!card.contains("set_title("), "the card writes through Player::write_title only"); |
| 181 | } |
| 182 | |
| 183 | |
| 184 | |
| 185 | #[test] |
| 186 | fn a_title_draft_commits_its_words_and_blank_words_keep_the_old_ones() { |
| 187 | use crate::player::titling::committed_title_words; |
| 188 | assert_eq!(committed_title_words("Intro hit \n").as_deref(), Some("Intro hit")); |
| 189 | assert_eq!(committed_title_words("Line one\nLine two").as_deref(), Some("Line one\nLine two")); |
| 190 | assert_eq!(committed_title_words(" "), None); |
| 191 | assert_eq!(committed_title_words(""), None); |
| 192 | } |
| 193 | |
| 194 | #[test] |
| 195 | fn typing_in_the_title_field_owns_the_keyboard_ahead_of_the_chords_and_escape_leaves_it() { |
| 196 | let stance = src_text("ui/stance.rs"); |
| 197 | let field = stance.find("this.text_field_key(event, window, cx)").expect("text field dispatch"); |
| 198 | let palette = stance.find("if this.palette_open {").expect("palette branch"); |
| 199 | assert!(field < palette, "the field is asked before the palette and the keymap"); |
| 200 | let blur = src_text("player/blur.rs"); |
| 201 | assert!(blur.contains("self.title_key(key, ctrl, shift, alt, key_char, cx)")); |
| 202 | let titling = src_text("player/titling.rs"); |
| 203 | assert!(titling.contains("\"escape\" => self.title_draft = None"), "escape reverts the title"); |
| 204 | assert!(titling.contains("\"enter\" if shift =>"), "shift+enter inserts a newline"); |
| 205 | assert!(titling.contains("\"enter\" => self.commit_title_draft(cx)"), "enter commits"); |
| 206 | } |
| 207 | |
| 208 | #[test] |
| 209 | fn a_title_is_reachable_by_double_click_on_the_timeline_row_and_on_the_picture() { |
| 210 | assert!(src_text("ui/bench_stance/titles_row.rs").contains("click_count() >= 2")); |
| 211 | assert!(src_text("ui/preview.rs").contains("this.open_title(i, event.click_count() >= 2, cx)")); |
| 212 | assert!(src_text("ui/bench_stance/mod.rs").contains("titles_row::titles_row")); |
| 213 | } |
| 214 | |
| 215 | |
| 216 | |
| 217 | fn fn_body(src: &str, sig: &str) -> String { |
| 218 | let at = src.find(sig).unwrap_or_else(|| panic!("no `{sig}` in the source")); |
| 219 | let rest = &src[at..]; |
| 220 | let end = rest.find("\n }\n").map_or(rest.len(), |e| e + 6); |
| 221 | rest[..end].to_string() |
| 222 | } |
| 223 | |
| 224 | |
| 225 | |
| 226 | |
| 227 | #[test] |
| 228 | fn one_pick_at_a_time_and_bare_bed_lets_go_of_the_rest() { |
| 229 | let edit = src_text("player/editkeys.rs"); |
| 230 | let begin = fn_body(&edit, "pub(crate) fn begin_marquee"); |
| 231 | assert!(!begin.contains("title_sel = None"), "the release, not the press, lets the title go"); |
| 232 | let finish = fn_body(&edit, "pub(crate) fn finish_marquee"); |
| 233 | assert!(finish.contains("self.drop_title_selection(cx);") && finish.contains("self.drop_sel_cue();"), "{finish}"); |
| 234 | assert!(finish.contains("self.selected = Selection::new();"), "a plain click on bare bed picks nothing"); |
| 235 | |
| 236 | let view = src_text("player/view.rs"); |
| 237 | let clear = fn_body(&view, "pub(crate) fn clear_picks"); |
| 238 | for needle in ["self.selected = Selection::new();", "self.gap = None;", "self.drop_title_selection(cx);", "self.drop_sel_cue();"] { |
| 239 | assert!(clear.contains(needle), "clear_picks misses {needle}"); |
| 240 | } |
| 241 | let actions = src_text("player/actions.rs"); |
| 242 | assert!(actions.contains("ActionId::Deselect => self.clear_picks(cx),")); |
| 243 | |
| 244 | let timeline = src_text("player/timeline_edit.rs"); |
| 245 | let gap = fn_body(&timeline, "pub(crate) fn select_gap("); |
| 246 | assert!(gap.contains("self.drop_title_selection(cx);") && gap.contains("self.drop_sel_cue();"), "{gap}"); |
| 247 | |
| 248 | let titling = src_text("player/titling.rs"); |
| 249 | let target = fn_body(&titling, "pub(crate) fn title_target"); |
| 250 | assert!(target.contains("self.sel_cue.is_some()") && target.contains("self.gap_pick().is_some()"), "{target}"); |
| 251 | let open = fn_body(&titling, "pub(crate) fn open_title("); |
| 252 | assert!(open.contains("self.drop_sel_cue();"), "a title opened over a caption leaves the caption picked"); |
| 253 | |
| 254 | let cues = src_text("player/caption_cues.rs"); |
| 255 | let menu = fn_body(&cues, "pub(crate) fn open_cue_menu"); |
| 256 | assert!(menu.contains("self.drop_title_selection(cx);") && menu.contains("self.gap = None;"), "{menu}"); |
| 257 | } |
| 258 | |
| 259 | |
| 260 | |
| 261 | #[test] |
| 262 | fn the_panel_scrolls_to_top_on_any_pick_and_the_style_body_needs_its_head() { |
| 263 | let dock = src_text("ui/dock_stance.rs"); |
| 264 | assert!(dock.contains("let picked: PanelPick = (anchor, player.sel_cue, player.title_sel);")); |
| 265 | assert!(dock.contains("Cell<PanelPick> = const { std::cell::Cell::new((None, None, None)) }")); |
| 266 | assert!(dock.contains("on_subtitle.then(|| section_body(player.subtitle_style_card(room, cx))).flatten()")); |
| 267 | } |
| 268 | |
| 269 | |
| 270 | #[test] |
| 271 | fn escape_in_a_caption_says_the_edit_was_cancelled() { |
| 272 | let cues = src_text("player/caption_cues.rs"); |
| 273 | let esc = fn_body(&cues, "pub(crate) fn escape_cue_edit"); |
| 274 | assert!(esc.contains("Caption edit cancelled"), "{esc}"); |
| 275 | let key = fn_body(&cues, "pub(crate) fn cue_edit_key"); |
| 276 | assert!(key.contains("self.escape_cue_edit()"), "{key}"); |
| 277 | } |
| 278 | |
| 279 | |
| 280 | |
| 281 | #[test] |
| 282 | fn a_normalize_result_goes_when_its_scope_or_its_pick_goes() { |
| 283 | let card = src_text("ui/cards/tools.rs"); |
| 284 | assert!(card.contains("t.set_said(Tool::Normalize, Said::default());")); |
| 285 | assert!(card.contains(".when(self.result_shown(t), |d|")); |
| 286 | let shown = fn_body(&card, "fn result_shown("); |
| 287 | assert!(shown.contains("self.selected.anchor(), self.sel_cue, self.title_sel, t.each"), "{shown}"); |
| 288 | } |