| 1 | |
| 2 | |
| 3 | |
| 4 | use super::*; |
| 5 | |
| 6 | |
| 7 | |
| 8 | |
| 9 | |
| 10 | |
| 11 | |
| 12 | #[test] |
| 13 | fn a_cached_cue_picture_is_keyed_by_the_cue_not_only_its_moment() { |
| 14 | use crate::ui::preview::sub_image_key; |
| 15 | let (lane, other) = (Lane::S1, Lane::A1); |
| 16 | |
| 17 | |
| 18 | let a = sub_image_key(lane, 1_000_000, 3_000_000, 0x1000); |
| 19 | let b = sub_image_key(lane, 1_000_000, 3_000_000, 0x2000); |
| 20 | assert_ne!( |
| 21 | a, b, |
| 22 | "two different cues at one moment on one lane are one cache key again, so \ |
| 23 | the second paints the first's picture" |
| 24 | ); |
| 25 | |
| 26 | assert_eq!( |
| 27 | a, |
| 28 | sub_image_key(lane, 1_000_000, 3_000_000, 0x1000), |
| 29 | "the cache misses on every repaint of the same cue, so it decodes an 8 MB \ |
| 30 | display set per frame" |
| 31 | ); |
| 32 | |
| 33 | assert_ne!( |
| 34 | a, |
| 35 | sub_image_key(lane, 1_000_000, 2_500_000, 0x1000), |
| 36 | "a cue's own end is no part of the key, so a recut cue serves the old window" |
| 37 | ); |
| 38 | |
| 39 | |
| 40 | assert_ne!( |
| 41 | a, |
| 42 | sub_image_key(other, 1_000_000, 3_000_000, 0x1000), |
| 43 | "the lane is no longer part of the key, so a lane's eye shut leaves the \ |
| 44 | other lane's caption on screen" |
| 45 | ); |
| 46 | |
| 47 | |
| 48 | |
| 49 | |
| 50 | let preview_rs = src_text("ui/preview.rs"); |
| 51 | assert!( |
| 52 | preview_rs.contains( |
| 53 | "let key = sub_image_key(lane, start_us, end_us, Arc::as_ptr(image) as usize);" |
| 54 | ), |
| 55 | "the cue picture is cached under a key that is not the cue's own" |
| 56 | ); |
| 57 | assert!( |
| 58 | preview_rs.contains("*up == key"), |
| 59 | "the cache no longer compares the key it built" |
| 60 | ); |
| 61 | } |
| 62 | |
| 63 | |
| 64 | |
| 65 | |
| 66 | |
| 67 | #[test] |
| 68 | fn a_drop_reads_the_frame_under_the_pointer_at_every_zoom() { |
| 69 | |
| 70 | let bed: Bounds<Pixels> = Bounds { |
| 71 | origin: point(px(12.), px(400.)), |
| 72 | size: size(px(200.), px(6.)), |
| 73 | }; |
| 74 | let fps = 30.; |
| 75 | |
| 76 | |
| 77 | let under = |scale: Scale, x: f32| frame_at(scale.time_at(px_along(px(x), bed)), fps); |
| 78 | |
| 79 | |
| 80 | |
| 81 | let fit = Scale { |
| 82 | pps: 20., |
| 83 | start: 0., |
| 84 | }; |
| 85 | assert_eq!(under(fit, 12.), 0); |
| 86 | assert_eq!(under(fit, 112.), 150); |
| 87 | assert_eq!(under(fit, 212.), 300); |
| 88 | |
| 89 | |
| 90 | |
| 91 | let zoomed = Scale { |
| 92 | pps: 80., |
| 93 | start: 5., |
| 94 | }; |
| 95 | assert_eq!(under(zoomed, 12.), 150); |
| 96 | assert_eq!(under(zoomed, 112.), 187); |
| 97 | assert_eq!(under(zoomed, 212.), 225); |
| 98 | |
| 99 | |
| 100 | assert_eq!(under(zoomed, 0.), 150); |
| 101 | assert_eq!(under(fit, 9999.), 300); |
| 102 | } |
| 103 | |
| 104 | |
| 105 | |
| 106 | |
| 107 | #[test] |
| 108 | fn a_dropped_clip_snaps_to_the_edges_worth_landing_on() { |
| 109 | |
| 110 | let marks = [100, 160, 300, 0]; |
| 111 | |
| 112 | assert_eq!(snapped(158, 40, 4, &marks), 160); |
| 113 | |
| 114 | assert_eq!(snapped(62, 40, 4, &marks), 60); |
| 115 | |
| 116 | assert_eq!(snapped(298, 40, 4, &marks), 300); |
| 117 | |
| 118 | |
| 119 | assert_eq!(snapped(150, 40, 4, &marks), 150); |
| 120 | |
| 121 | |
| 122 | assert_eq!(snapped(158, 40, 0, &marks), 158); |
| 123 | |
| 124 | assert_eq!(snapped(101, 40, 8, &marks), 100); |
| 125 | |
| 126 | |
| 127 | assert_eq!(snapped(2, 40, 4, &marks), 0); |
| 128 | } |
| 129 | |
| 130 | |
| 131 | |
| 132 | |
| 133 | |
| 134 | |
| 135 | #[test] |
| 136 | fn a_ghost_and_a_drop_are_one_landing() { |
| 137 | |
| 138 | |
| 139 | let marks = [100, 160, 300, 0]; |
| 140 | let (len, grab, tol) = (40, 12, 4); |
| 141 | |
| 142 | |
| 143 | |
| 144 | assert_eq!(landing(170, grab, len, true, tol, &marks), (160, Some(160))); |
| 145 | |
| 146 | |
| 147 | assert_eq!(landing(170, 0, len, true, tol, &marks), (170, None)); |
| 148 | |
| 149 | |
| 150 | |
| 151 | assert_eq!(landing(298, 0, 0, true, tol, &marks), (300, Some(300))); |
| 152 | |
| 153 | |
| 154 | assert_eq!(landing(170, grab, len, false, tol, &marks), (158, None)); |
| 155 | |
| 156 | |
| 157 | assert_eq!(landing(3, grab, len, true, tol, &marks), (0, Some(0))); |
| 158 | } |
| 159 | |
| 160 | |
| 161 | |
| 162 | |
| 163 | #[test] |
| 164 | fn a_lane_refuses_the_files_it_cannot_hold_before_the_release_says_so() { |
| 165 | let (video, audio) = (Lane::V1, Lane::A1); |
| 166 | let sound = Path::new("/media/take.mp3"); |
| 167 | let still = Path::new("/media/card.png"); |
| 168 | let movie = Path::new("/media/take.mp4"); |
| 169 | assert_eq!( |
| 170 | lane_refuses(sound, video, false).as_deref(), |
| 171 | Some("NOT ON V1 — take.mp3 has no picture") |
| 172 | ); |
| 173 | assert_eq!( |
| 174 | lane_refuses(still, audio, true).as_deref(), |
| 175 | Some("NOT ON A1 — card.png is a still image") |
| 176 | ); |
| 177 | |
| 178 | |
| 179 | assert_eq!(lane_refuses(sound, audio, false), None); |
| 180 | assert_eq!(lane_refuses(still, video, true), None); |
| 181 | assert_eq!(lane_refuses(movie, video, true), None); |
| 182 | |
| 183 | |
| 184 | |
| 185 | assert_eq!(lane_refuses(movie, audio, true), None); |
| 186 | |
| 187 | |
| 188 | |
| 189 | assert_eq!( |
| 190 | lane_refuses(movie, video, false).as_deref(), |
| 191 | Some("NOT ON V1 — take.mp4 has no picture") |
| 192 | ); |
| 193 | |
| 194 | |
| 195 | for (file, has_video) in [(sound, false), (still, true), (movie, true)] { |
| 196 | let why = lane_refuses(file, Lane::S1, has_video).expect("a subtitle lane takes no media"); |
| 197 | assert!(why.starts_with("NOT ON S1 — "), "{why}"); |
| 198 | assert!(why.contains("Subtitles list"), "{why}"); |
| 199 | } |
| 200 | |
| 201 | |
| 202 | |
| 203 | assert_eq!(frames_of_us(1_000_000, 30.), 30); |
| 204 | assert_eq!(frames_of_us(500_000, 24.), 12); |
| 205 | assert_eq!(frames_of_us(1_000, 30.), 1); |
| 206 | assert_eq!(frames_of_us(1_000_000, 0.), 1); |
| 207 | } |
| 208 | |
| 209 | |
| 210 | |
| 211 | #[test] |
| 212 | fn the_marks_are_every_lane_the_playhead_and_the_start() { |
| 213 | let clip = |start: u32, frames: u32, link| Clip { |
| 214 | fade_in: 0, |
| 215 | fade_out: 0, |
| 216 | transition_out: 0, |
| 217 | visualizer: 0, |
| 218 | viz_paint: 0, |
| 219 | start, |
| 220 | in_frame: 0, |
| 221 | out_frame: frames, |
| 222 | source: 0, |
| 223 | link, |
| 224 | eq: None, |
| 225 | color: None, |
| 226 | transform: None, |
| 227 | fit: Default::default(), |
| 228 | speed: Default::default(), |
| 229 | }; |
| 230 | |
| 231 | |
| 232 | let video = [clip(100, 60, Some(7))]; |
| 233 | let audio = [clip(100, 60, Some(7)), clip(400, 30, None)]; |
| 234 | let lanes: [&[Clip]; 2] = [&video, &audio]; |
| 235 | |
| 236 | |
| 237 | let mut all = snap_marks(&lanes, None, None, 300, None); |
| 238 | all.sort_unstable(); |
| 239 | assert_eq!(all, [0, 100, 100, 160, 160, 300, 400, 430]); |
| 240 | |
| 241 | |
| 242 | |
| 243 | |
| 244 | |
| 245 | |
| 246 | let mut carried = snap_marks(&lanes, Some((0, 0)), Some(7), 300, None); |
| 247 | carried.sort_unstable(); |
| 248 | assert_eq!(carried, [0, 300, 400, 430]); |
| 249 | |
| 250 | |
| 251 | assert_eq!(snap_marks(&[], Some((3, 9)), None, 0, None), [0, 0]); |
| 252 | } |
| 253 | |
| 254 | |
| 255 | #[test] |
| 256 | fn the_snap_names_the_mark_it_landed_on_unless_it_is_switched_off() { |
| 257 | let marks = [100, 160, 300, 0]; |
| 258 | |
| 259 | |
| 260 | assert_eq!(snap_cue(true, 62, 40, 4, &marks), (60, Some(100))); |
| 261 | |
| 262 | assert_eq!(snap_cue(true, 158, 40, 4, &marks), (160, Some(160))); |
| 263 | |
| 264 | assert_eq!(snap_cue(true, 298, 0, 4, &marks), (300, Some(300))); |
| 265 | |
| 266 | assert_eq!(snap_cue(true, 200, 40, 4, &marks), (200, None)); |
| 267 | |
| 268 | |
| 269 | assert_eq!(snap_cue(false, 158, 40, 4, &marks), (158, None)); |
| 270 | } |
| 271 | |
| 272 | |
| 273 | |
| 274 | |
| 275 | #[test] |
| 276 | fn a_rate_row_says_only_what_the_container_stated() { |
| 277 | use crate::MediaBitrate; |
| 278 | let all = MediaBitrate { |
| 279 | total: Some(8_432_000), |
| 280 | video: Some(7_918_000), |
| 281 | audio: Some(128_000), |
| 282 | }; |
| 283 | assert_eq!( |
| 284 | bitrate_detail(Some(all), 1), |
| 285 | "8.4 Mb/s · 7.9 video · 0.13 sound" |
| 286 | ); |
| 287 | |
| 288 | |
| 289 | assert_eq!( |
| 290 | bitrate_detail(Some(MediaBitrate { audio: None, ..all }), 1), |
| 291 | "8.4 Mb/s · 7.9 video" |
| 292 | ); |
| 293 | |
| 294 | |
| 295 | assert_eq!( |
| 296 | bitrate_detail(Some(MediaBitrate::default()), 0), |
| 297 | "not stated" |
| 298 | ); |
| 299 | |
| 300 | assert_eq!(bitrate_detail(None, 2), "…"); |
| 301 | |
| 302 | |
| 303 | assert_eq!( |
| 304 | bitrate_detail(Some(all), 2), |
| 305 | "8.4 Mb/s · 7.9 video · 0.13 1 of 2" |
| 306 | ); |
| 307 | |
| 308 | |
| 309 | |
| 310 | assert_eq!( |
| 311 | bitrate_detail( |
| 312 | Some(MediaBitrate { |
| 313 | total: Some(4_998), |
| 314 | video: Some(113), |
| 315 | audio: Some(2_400), |
| 316 | }), |
| 317 | 1 |
| 318 | ), |
| 319 | "5.0 kb/s · 0.11 video · 2.4 sound" |
| 320 | ); |
| 321 | } |
| 322 | |
| 323 | |
| 324 | |
| 325 | |
| 326 | |
| 327 | #[test] |
| 328 | fn a_stated_rate_never_renders_as_a_zero() { |
| 329 | use crate::MediaBitrate; |
| 330 | |
| 331 | |
| 332 | |
| 333 | |
| 334 | let edges: Vec<u64> = (0..12) |
| 335 | .flat_map(|e| { |
| 336 | let decade = 10_u64.pow(e); |
| 337 | [decade, decade * 4, decade * 5, decade * 9] |
| 338 | }) |
| 339 | .chain([MB_FLOOR - 1, MB_FLOOR, 999_999, 4_998, 113, 2_400]) |
| 340 | .collect(); |
| 341 | for &small in &edges { |
| 342 | for &big in &edges { |
| 343 | let line = bitrate_detail( |
| 344 | Some(MediaBitrate { |
| 345 | total: Some(small.max(big)), |
| 346 | video: Some(big), |
| 347 | audio: Some(small), |
| 348 | }), |
| 349 | 2, |
| 350 | ); |
| 351 | for number in line |
| 352 | .split(" · ") |
| 353 | .filter_map(|part| part.split(' ').next()) |
| 354 | .filter_map(|n| n.parse::<f64>().ok()) |
| 355 | { |
| 356 | assert!( |
| 357 | number > 0., |
| 358 | "{small}/{big} bits a second rendered as {line:?}" |
| 359 | ); |
| 360 | } |
| 361 | } |
| 362 | } |
| 363 | } |
| 364 | |
| 365 | #[test] |
| 366 | fn timecode_counts_frames_inside_the_second() { |
| 367 | assert_eq!(timecode(0., 30.), "00:00:00:00"); |
| 368 | assert_eq!(timecode(-1., 30.), "00:00:00:00"); |
| 369 | assert_eq!(timecode(4.9667, 30.), "00:00:04:29"); |
| 370 | assert_eq!(timecode(5., 30.), "00:00:05:00"); |
| 371 | assert_eq!(timecode(3661.5, 30.), "01:01:01:15"); |
| 372 | |
| 373 | assert_eq!(timecode(1. - f64::EPSILON, 30.), "00:00:00:29"); |
| 374 | assert_eq!(timecode(0.999, 29.97), "00:00:00:29"); |
| 375 | } |
| 376 | |
| 377 | #[test] |
| 378 | fn scrub_seeks_only_on_a_new_frame_and_only_every_100ms() { |
| 379 | let (slow, fast) = (Duration::from_millis(100), Duration::from_millis(99)); |
| 380 | |
| 381 | |
| 382 | assert!(scrub_due(31, 30, slow)); |
| 383 | assert!(!scrub_due(30, 30, slow)); |
| 384 | assert!(!scrub_due(31, 30, fast)); |
| 385 | assert!(!scrub_due(30, 30, fast)); |
| 386 | |
| 387 | assert!(scrub_due(1, 0, Duration::from_secs(9))); |
| 388 | } |
| 389 | |
| 390 | #[test] |
| 391 | fn the_picture_is_restarted_only_when_it_is_really_behind_and_not_twice() { |
| 392 | |
| 393 | |
| 394 | assert!(!should_resync(0., None)); |
| 395 | assert!(!should_resync(LATE_RESYNC, None)); |
| 396 | assert!(should_resync(LATE_RESYNC + 0.1, None)); |
| 397 | |
| 398 | |
| 399 | |
| 400 | assert!(!should_resync(9., Some(Instant::now()))); |
| 401 | assert!(should_resync( |
| 402 | 9., |
| 403 | Some(Instant::now() - RESYNC_GAP - Duration::from_millis(1)) |
| 404 | )); |
| 405 | } |
| 406 | |
| 407 | #[test] |
| 408 | fn export_lands_beside_the_source_and_never_on_it() { |
| 409 | |
| 410 | assert_eq!( |
| 411 | export_path("assets/test_baseline.mp4"), |
| 412 | std::path::Path::new("assets/test_baseline.export.mp4") |
| 413 | ); |
| 414 | |
| 415 | assert_eq!( |
| 416 | export_path("a.export.mp4"), |
| 417 | std::path::Path::new("a.export.export.mp4") |
| 418 | ); |
| 419 | |
| 420 | assert_eq!(export_path("clip"), std::path::Path::new("clip.export.mp4")); |
| 421 | assert_eq!( |
| 422 | export_path("/v.1/clip.MP4"), |
| 423 | std::path::Path::new("/v.1/clip.export.mp4") |
| 424 | ); |
| 425 | } |
| 426 | |
| 427 | #[test] |
| 428 | fn the_first_save_lands_beside_the_media() { |
| 429 | assert_eq!( |
| 430 | project_path("assets/test_av.mp4"), |
| 431 | std::path::Path::new("assets/test_av.edith") |
| 432 | ); |
| 433 | |
| 434 | assert_eq!( |
| 435 | project_path("a.export.mp4"), |
| 436 | std::path::Path::new("a.export.edith") |
| 437 | ); |
| 438 | assert_eq!(project_path("clip"), std::path::Path::new("clip.edith")); |
| 439 | |
| 440 | assert_eq!(project_path("a.edith"), std::path::Path::new("a.edith")); |
| 441 | } |
| 442 | |
| 443 | #[test] |
| 444 | fn only_an_exact_edith_extension_is_a_project() { |
| 445 | let p = std::path::Path::new; |
| 446 | assert!(is_project(p("a.edith"))); |
| 447 | |
| 448 | assert!(is_project(p("/v.mp4/a.edith"))); |
| 449 | assert!(!is_project(p("a.mp4"))); |
| 450 | assert!(!is_project(p("/v.edith/a.mp4"))); |
| 451 | |
| 452 | |
| 453 | assert!(!is_project(p("a.EDITH"))); |
| 454 | |
| 455 | assert!(!is_project(p("edith"))); |
| 456 | assert!(!is_project(p("a.edith.mp4"))); |
| 457 | } |
| 458 | |
| 459 | |
| 460 | |
| 461 | |
| 462 | |
| 463 | #[test] |
| 464 | fn no_stroke_is_missing_from_the_keys_menu() { |
| 465 | use keymap::{ActionId, Keymap}; |
| 466 | |
| 467 | |
| 468 | |
| 469 | let handler = ui_source(); |
| 470 | let handler = handler.as_str(); |
| 471 | let keymap = Keymap::defaults(); |
| 472 | let listed = |key: &str| { |
| 473 | let pretty = keymap::Chord { |
| 474 | key: key.to_string(), |
| 475 | ctrl: false, |
| 476 | } |
| 477 | .pretty(); |
| 478 | keymap.lookup(key, false).is_some() || keymap::FIXED.iter().any(|f| f.chord == pretty) |
| 479 | }; |
| 480 | let mut compared = 0; |
| 481 | for (at, needle) in handler.match_indices("key == ") { |
| 482 | let rest = &handler[at + needle.len()..]; |
| 483 | let key = match rest.strip_prefix('"') { |
| 484 | Some(literal) => literal[..literal.find('"').expect("unterminated key")].to_string(), |
| 485 | |
| 486 | None if rest.starts_with("ESCAPE") => crate::ESCAPE.to_string(), |
| 487 | None => panic!("a key compared against something this cannot read: {rest:.20}"), |
| 488 | }; |
| 489 | assert!( |
| 490 | listed(&key), |
| 491 | "the handler answers to {key:?} and the keys menu never says so" |
| 492 | ); |
| 493 | compared += 1; |
| 494 | } |
| 495 | |
| 496 | |
| 497 | assert!( |
| 498 | compared >= 4, |
| 499 | "the key comparisons moved; this scan is blind" |
| 500 | ); |
| 501 | |
| 502 | assert!(handler.contains("key.parse::<u32>()")); |
| 503 | assert!(keymap::FIXED.iter().any(|f| f.chord == "0–9")); |
| 504 | |
| 505 | for action in ActionId::ALL { |
| 506 | assert!(keymap::Category::ALL.contains(&action.category())); |
| 507 | } |
| 508 | for fixed in keymap::FIXED.iter() { |
| 509 | assert!(keymap::Category::ALL.contains(&fixed.category)); |
| 510 | } |
| 511 | } |
| 512 | |
| 513 | |
| 514 | |
| 515 | #[test] |
| 516 | fn a_moment_and_the_place_it_is_drawn_are_the_same_at_every_zoom() { |
| 517 | let duration = 20.; |
| 518 | |
| 519 | |
| 520 | |
| 521 | let fit = test_view(Scale::default(), duration).fit(); |
| 522 | assert_eq!(fit.pps, 10.); |
| 523 | assert_eq!(fit.width_px(5.), TEST_BED * 0.25); |
| 524 | for t in [0., 5., 12.5, 20.] { |
| 525 | assert_eq!(fit.px_at(t), (t / duration) as f32 * TEST_BED, "fit at {t}"); |
| 526 | } |
| 527 | for scale in [ |
| 528 | fit, |
| 529 | Scale { |
| 530 | pps: 20., |
| 531 | start: 4., |
| 532 | }, |
| 533 | Scale { |
| 534 | pps: 80., |
| 535 | start: 12.5, |
| 536 | }, |
| 537 | Scale { |
| 538 | pps: 375., |
| 539 | start: 19., |
| 540 | }, |
| 541 | ] { |
| 542 | let scale = test_view(scale, duration).settled(); |
| 543 | for x in [0., 50., 100., TEST_BED] { |
| 544 | let at = scale.time_at(x); |
| 545 | assert!( |
| 546 | (f64::from(scale.px_at(at)) - f64::from(x)).abs() < 1e-3, |
| 547 | "{scale:?} round trip at {x}" |
| 548 | ); |
| 549 | } |
| 550 | |
| 551 | assert!( |
| 552 | (f64::from(scale.width_px(test_view(scale, duration).span())) - f64::from(TEST_BED)) |
| 553 | .abs() |
| 554 | < 1e-3, |
| 555 | "{scale:?} spans the bed" |
| 556 | ); |
| 557 | } |
| 558 | } |
| 559 | |
| 560 | |
| 561 | |
| 562 | |
| 563 | #[test] |
| 564 | fn a_zoom_leaves_the_anchor_where_it_was_on_screen() { |
| 565 | let duration = 20.; |
| 566 | let mut scale = test_view(Scale::default(), duration).settled(); |
| 567 | |
| 568 | |
| 569 | for anchor in [100f32, 50., 180.] { |
| 570 | |
| 571 | |
| 572 | for _ in 0..30 { |
| 573 | let at = scale.time_at(anchor); |
| 574 | let view = test_view(scale, duration); |
| 575 | let zoomed = view.zoomed(ZOOM_STEP, anchor); |
| 576 | |
| 577 | |
| 578 | let span = test_view(zoomed, duration).span(); |
| 579 | let pinned = zoomed.start <= 0. || zoomed.start + span >= duration; |
| 580 | if !pinned { |
| 581 | assert!( |
| 582 | (f64::from(zoomed.px_at(at)) - f64::from(anchor)).abs() < 1e-3, |
| 583 | "{at} moved: {scale:?} -> {zoomed:?}" |
| 584 | ); |
| 585 | } |
| 586 | assert!(zoomed.pps >= scale.pps, "and it did zoom in"); |
| 587 | scale = zoomed; |
| 588 | } |
| 589 | } |
| 590 | } |
| 591 | |
| 592 | |
| 593 | |
| 594 | |
| 595 | |
| 596 | #[test] |
| 597 | fn zoom_stops_at_a_bedful_of_time_and_at_a_handful_of_frames() { |
| 598 | let (duration, fps) = (20., 30.); |
| 599 | let mut scale = Scale::default(); |
| 600 | for _ in 0..200 { |
| 601 | scale = test_view(scale, duration).zoomed(1. / ZOOM_STEP, 100.); |
| 602 | } |
| 603 | assert!( |
| 604 | (test_view(scale, duration).span() - f64::from(TEST_BED) / PPS_MIN).abs() < 1e-6, |
| 605 | "widest is a pixel to the second, not the 20 s that happen to be on it" |
| 606 | ); |
| 607 | for _ in 0..200 { |
| 608 | scale = test_view(scale, duration).zoomed(ZOOM_STEP, 100.); |
| 609 | } |
| 610 | assert_eq!( |
| 611 | (test_view(scale, duration).span() * fps).round(), |
| 612 | ZOOM_MIN_FRAMES, |
| 613 | "tightest is a handful of frames" |
| 614 | ); |
| 615 | |
| 616 | let end = test_view( |
| 617 | Scale { |
| 618 | start: 1e6, |
| 619 | ..scale |
| 620 | }, |
| 621 | duration, |
| 622 | ); |
| 623 | let end = (end.settled(), end.span()); |
| 624 | assert!((end.0.start + end.1 - duration).abs() < 1e-9); |
| 625 | assert_eq!( |
| 626 | test_view( |
| 627 | Scale { |
| 628 | start: -5., |
| 629 | ..scale |
| 630 | }, |
| 631 | duration |
| 632 | ) |
| 633 | .settled() |
| 634 | .start, |
| 635 | 0. |
| 636 | ); |
| 637 | |
| 638 | |
| 639 | |
| 640 | let tiny = test_view(Scale::default(), 0.1); |
| 641 | assert!(tiny.zoomed(ZOOM_STEP, 100.).pps > PPS_DEFAULT); |
| 642 | assert!(tiny.zoomed(1. / ZOOM_STEP, 100.).pps < PPS_DEFAULT); |
| 643 | |
| 644 | |
| 645 | let empty = test_view(Scale::default(), 0.); |
| 646 | assert_eq!(empty.settled(), Scale::default()); |
| 647 | assert_eq!(empty.fit(), Scale::default()); |
| 648 | assert_eq!(Scale::default().px_at(0.), 0.); |
| 649 | |
| 650 | |
| 651 | let unpainted = View { |
| 652 | bed: 0., |
| 653 | ..test_view( |
| 654 | Scale { |
| 655 | pps: 4e6, |
| 656 | start: 3., |
| 657 | }, |
| 658 | duration, |
| 659 | ) |
| 660 | }; |
| 661 | assert_eq!(unpainted.settled().pps, 4e6); |
| 662 | assert_eq!(unpainted.following(19.), unpainted.scale); |
| 663 | } |
| 664 | |
| 665 | |
| 666 | |
| 667 | |
| 668 | |
| 669 | #[test] |
| 670 | fn zooming_out_reaches_the_end_of_a_timeline_however_long_it_is() { |
| 671 | let bed = 900.; |
| 672 | let view = |scale: Scale, duration: f64| View { |
| 673 | scale, |
| 674 | bed, |
| 675 | duration, |
| 676 | fps: 30., |
| 677 | }; |
| 678 | |
| 679 | let out = |duration: f64| { |
| 680 | let mut scale = Scale::default(); |
| 681 | for _ in 0..400 { |
| 682 | scale = view(scale, duration).zoomed(1. / ZOOM_STEP, 0.); |
| 683 | } |
| 684 | scale |
| 685 | }; |
| 686 | let five_hours = 2. * 2.5 * 3600.; |
| 687 | let wide = out(five_hours); |
| 688 | |
| 689 | |
| 690 | assert_eq!(wide.start, 0.); |
| 691 | let end = wide.px_at(five_hours); |
| 692 | assert!( |
| 693 | end < bed, |
| 694 | "the end of the timeline is off the bed at {end} px" |
| 695 | ); |
| 696 | assert!(end > bed * 0.9, "and not shrunk into a corner: {end} px"); |
| 697 | assert!( |
| 698 | (view(wide, five_hours).span() - five_hours * ZOOM_OUT_MARGIN).abs() < 1e-6, |
| 699 | "the far stop is the timeline plus its margin" |
| 700 | ); |
| 701 | |
| 702 | |
| 703 | let mut scale = wide; |
| 704 | for _ in 0..400 { |
| 705 | scale = view(scale, five_hours).zoomed(ZOOM_STEP, 0.); |
| 706 | } |
| 707 | assert_eq!( |
| 708 | (view(scale, five_hours).span() * 30.).round(), |
| 709 | ZOOM_MIN_FRAMES |
| 710 | ); |
| 711 | |
| 712 | |
| 713 | |
| 714 | assert!((view(out(10.), 10.).span() - f64::from(bed) / PPS_MIN).abs() < 1e-6); |
| 715 | |
| 716 | |
| 717 | assert_eq!(view(Scale::default(), 10.).settled(), Scale::default()); |
| 718 | assert_eq!( |
| 719 | view(Scale::default(), five_hours).settled(), |
| 720 | Scale::default() |
| 721 | ); |
| 722 | |
| 723 | |
| 724 | let shrunk = view(wide, 1800.).settled(); |
| 725 | assert!((view(shrunk, 1800.).span() - 1800. * ZOOM_OUT_MARGIN).abs() < 1e-6); |
| 726 | assert!(shrunk.px_at(1800.) < bed); |
| 727 | |
| 728 | |
| 729 | assert_eq!(view(wide, 2. * five_hours).settled().pps, wide.pps); |
| 730 | assert!(view(wide, 2. * five_hours).zoomed(1. / ZOOM_STEP, 0.).pps < wide.pps); |
| 731 | } |
| 732 | |
| 733 | |
| 734 | |
| 735 | |
| 736 | #[test] |
| 737 | fn a_scroll_slides_the_view_without_zooming_it() { |
| 738 | let duration = 60.; |
| 739 | |
| 740 | |
| 741 | let start = test_view( |
| 742 | Scale { |
| 743 | pps: f64::from(TEST_BED) / 10., |
| 744 | start: 0., |
| 745 | }, |
| 746 | duration, |
| 747 | ) |
| 748 | .settled(); |
| 749 | let notch = TEST_BED * SCROLL_NOTCH_SHARE; |
| 750 | let on = test_view(start, duration).scrolled(notch); |
| 751 | |
| 752 | |
| 753 | assert_eq!(on.pps, start.pps, "a scroll is not a zoom"); |
| 754 | assert!( |
| 755 | (on.start - (start.start + 1.)).abs() < 1e-9, |
| 756 | "one notch is a tenth of the 10 s on the bed: {on:?}" |
| 757 | ); |
| 758 | assert!( |
| 759 | (f64::from(start.px_at(20.) - on.px_at(20.)) - f64::from(notch)).abs() < 1e-3, |
| 760 | "and what was drawn moved with it: {on:?}" |
| 761 | ); |
| 762 | |
| 763 | let back = test_view(on, duration).scrolled(-notch); |
| 764 | assert!((back.start - start.start).abs() < 1e-9, "{back:?}"); |
| 765 | |
| 766 | |
| 767 | |
| 768 | let mut scale = start; |
| 769 | for _ in 0..200 { |
| 770 | scale = test_view(scale, duration).scrolled(notch); |
| 771 | } |
| 772 | assert_eq!(scale.pps, start.pps); |
| 773 | assert!( |
| 774 | (scale.start - (duration - 10.)).abs() < 1e-6, |
| 775 | "the tail stops with the end on the bed: {scale:?}" |
| 776 | ); |
| 777 | for _ in 0..200 { |
| 778 | scale = test_view(scale, duration).scrolled(-notch); |
| 779 | } |
| 780 | assert_eq!(scale.start, 0., "and the head stops at the head: {scale:?}"); |
| 781 | |
| 782 | let whole = test_view(Scale::default(), duration).fit(); |
| 783 | assert_eq!(test_view(whole, duration).scrolled(notch), whole); |
| 784 | } |
| 785 | |
| 786 | |
| 787 | |
| 788 | |
| 789 | #[test] |
| 790 | fn the_view_follows_a_playhead_that_runs_off_the_bed() { |
| 791 | let duration = 20.; |
| 792 | |
| 793 | let view = test_view( |
| 794 | Scale { |
| 795 | pps: 40., |
| 796 | start: 5., |
| 797 | }, |
| 798 | duration, |
| 799 | ); |
| 800 | let scale = view.settled(); |
| 801 | assert_eq!(view.span(), 5.); |
| 802 | |
| 803 | for at in [5., 7.5, 10.] { |
| 804 | assert_eq!(view.following(at), scale, "{at} is on the bed"); |
| 805 | } |
| 806 | |
| 807 | |
| 808 | let moved = view.following(12.); |
| 809 | assert_eq!(moved.pps, scale.pps); |
| 810 | assert!(moved.start > scale.start, "scrolled forward"); |
| 811 | assert!( |
| 812 | moved.px_at(12.) > 0. && moved.px_at(12.) < TEST_BED, |
| 813 | "and the playhead is on screen" |
| 814 | ); |
| 815 | |
| 816 | let back = view.following(1.); |
| 817 | assert!(back.start < scale.start); |
| 818 | assert!(back.px_at(1.) >= 0.); |
| 819 | |
| 820 | let whole = test_view(Scale::default(), duration); |
| 821 | let fit = whole.fit(); |
| 822 | for at in [0., 10., 20.] { |
| 823 | assert_eq!( |
| 824 | test_view(fit, duration).following(at), |
| 825 | fit, |
| 826 | "the fit never scrolls" |
| 827 | ); |
| 828 | } |
| 829 | } |
| 830 | |
| 831 | |
| 832 | |
| 833 | |
| 834 | |
| 835 | |
| 836 | |
| 837 | #[test] |
| 838 | fn a_scroll_during_playback_wins_until_the_head_catches_up() { |
| 839 | let duration = 80.; |
| 840 | let mut scale = test_view( |
| 841 | Scale { |
| 842 | pps: 40., |
| 843 | start: 0., |
| 844 | }, |
| 845 | duration, |
| 846 | ) |
| 847 | .settled(); |
| 848 | |
| 849 | |
| 850 | |
| 851 | let frame = |scale: Scale, panned: &mut bool, at: f64| { |
| 852 | let scale = match *panned { |
| 853 | true => test_view(scale, duration).settled(), |
| 854 | false => test_view(scale, duration).following(at), |
| 855 | }; |
| 856 | if *panned && test_view(scale, duration).shows(at) { |
| 857 | *panned = false; |
| 858 | } |
| 859 | scale |
| 860 | }; |
| 861 | |
| 862 | |
| 863 | let notch = f64::from(TEST_BED * SCROLL_NOTCH_SHARE); |
| 864 | let mut panned = false; |
| 865 | for n in 1..=5 { |
| 866 | scale = test_view(scale, duration).scrolled(TEST_BED * SCROLL_NOTCH_SHARE); |
| 867 | panned = true; |
| 868 | scale = frame(scale, &mut panned, 1.); |
| 869 | |
| 870 | |
| 871 | |
| 872 | assert_eq!(panned, scale.start > 1., "held while the head is off"); |
| 873 | assert!( |
| 874 | (scale.start - notch * f64::from(n) / 40.).abs() < 1e-6, |
| 875 | "notch {n} reached the screen: {scale:?}" |
| 876 | ); |
| 877 | } |
| 878 | |
| 879 | let span = test_view(scale, duration).span(); |
| 880 | let start = frame(scale, &mut panned, scale.start + span / 2.).start; |
| 881 | assert!(!panned, "the follow has the view back"); |
| 882 | assert_eq!(start, scale.start, "and it did not jump to take it"); |
| 883 | |
| 884 | let ran_off = frame(scale, &mut panned, start + span + 1.); |
| 885 | assert!( |
| 886 | ran_off.start > start, |
| 887 | "the follow follows again: {ran_off:?}" |
| 888 | ); |
| 889 | |
| 890 | |
| 891 | assert!(test_view(scale, duration).shows(scale.start)); |
| 892 | assert!(!test_view(scale, duration).shows(scale.start + span + 0.5)); |
| 893 | } |
| 894 | |
| 895 | |
| 896 | |
| 897 | |
| 898 | |
| 899 | #[test] |
| 900 | fn a_clip_is_drawn_the_same_width_whatever_else_is_on_the_timeline() { |
| 901 | let bed = 900.; |
| 902 | let of = |duration: f64| View { |
| 903 | scale: Scale::default(), |
| 904 | bed, |
| 905 | duration, |
| 906 | fps: 30., |
| 907 | }; |
| 908 | |
| 909 | |
| 910 | let (alone, joined) = (of(5.).settled(), of(20.).settled()); |
| 911 | assert_eq!(alone, joined); |
| 912 | assert_eq!(alone.width_px(5.), joined.width_px(5.)); |
| 913 | assert_eq!(joined.px_at(5.), alone.width_px(5.)); |
| 914 | |
| 915 | assert!( |
| 916 | alone.width_px(5.) < bed / 2., |
| 917 | "5 s at {} px/s is {} px of a {bed} px bed", |
| 918 | alone.pps, |
| 919 | alone.width_px(5.) |
| 920 | ); |
| 921 | |
| 922 | |
| 923 | let out = of(5.).zoomed(1. / ZOOM_STEP, 0.); |
| 924 | assert!( |
| 925 | out.width_px(5.) < alone.width_px(5.), |
| 926 | "{} px is not smaller than {} px", |
| 927 | out.width_px(5.), |
| 928 | alone.width_px(5.) |
| 929 | ); |
| 930 | |
| 931 | assert_eq!(of(5.).fit().width_px(5.), bed); |
| 932 | } |
| 933 | |
| 934 | |
| 935 | |
| 936 | #[test] |
| 937 | fn the_zoom_button_says_how_much_is_on_the_bed() { |
| 938 | |
| 939 | assert_eq!(secs_label(1. / 60.), "0.02s"); |
| 940 | assert_eq!(secs_label(4.5), "4.5s"); |
| 941 | } |
| 942 | |
| 943 | |
| 944 | |
| 945 | |
| 946 | |
| 947 | |
| 948 | |
| 949 | #[test] |
| 950 | fn tab_cycles_dock_bench_inspector_and_wraps() { |
| 951 | use crate::ui::stance::{next_surface, Surface}; |
| 952 | |
| 953 | assert_eq!(next_surface(Surface::Dock, false), Surface::Bench); |
| 954 | assert_eq!(next_surface(Surface::Bench, false), Surface::Inspector); |
| 955 | assert_eq!(next_surface(Surface::Inspector, false), Surface::Dock); |
| 956 | |
| 957 | |
| 958 | assert_eq!(next_surface(Surface::Dock, true), Surface::Inspector); |
| 959 | assert_eq!(next_surface(Surface::Inspector, true), Surface::Bench); |
| 960 | assert_eq!(next_surface(Surface::Bench, true), Surface::Dock); |
| 961 | } |
| 962 | |
| 963 | |
| 964 | |
| 965 | |
| 966 | |
| 967 | |
| 968 | |
| 969 | |
| 970 | #[test] |
| 971 | fn surface_wants_src_active_mounts_the_tab_before_focus_lands_on_it() { |
| 972 | use crate::ui::stance::{surface_wants_src_active, Surface}; |
| 973 | |
| 974 | assert_eq!(surface_wants_src_active(Surface::Dock), Some(true)); |
| 975 | assert_eq!(surface_wants_src_active(Surface::Inspector), Some(false)); |
| 976 | |
| 977 | |
| 978 | assert_eq!(surface_wants_src_active(Surface::Bench), None); |
| 979 | } |
| 980 | |
| 981 | |
| 982 | |
| 983 | |
| 984 | |
| 985 | |
| 986 | |
| 987 | |
| 988 | |
| 989 | |
| 990 | |
| 991 | |
| 992 | |
| 993 | #[test] |
| 994 | fn only_tab_is_intercepted_by_a_focused_surface_everything_else_reaches_root() { |
| 995 | use crate::ui::stance::is_focus_cycle_key; |
| 996 | |
| 997 | assert!(is_focus_cycle_key("tab")); |
| 998 | for key in [ |
| 999 | "space", "escape", "s", "c", "left", "right", "enter", "delete", "?", |
| 1000 | ] { |
| 1001 | assert!(!is_focus_cycle_key(key), "{key} must bubble to the root"); |
| 1002 | } |
| 1003 | } |
| 1004 | |
| 1005 | |
| 1006 | |
| 1007 | |
| 1008 | |
| 1009 | |
| 1010 | #[test] |
| 1011 | fn escape_is_the_only_key_a_focused_surface_uses_to_leave_the_ring() { |
| 1012 | use crate::ui::stance::is_focus_exit_key; |
| 1013 | |
| 1014 | assert!(is_focus_exit_key("escape")); |
| 1015 | for key in [ |
| 1016 | "tab", "space", "s", "c", "left", "right", "enter", "delete", "?", |
| 1017 | ] { |
| 1018 | assert!(!is_focus_exit_key(key), "{key} must not exit the ring"); |
| 1019 | } |
| 1020 | } |
| 1021 | |
| 1022 | |
| 1023 | |
| 1024 | |
| 1025 | |
| 1026 | #[test] |
| 1027 | fn focus_panels_is_bound_and_lands_on_the_dock() { |
| 1028 | use keymap::{ActionId, Keymap}; |
| 1029 | |
| 1030 | let k = Keymap::defaults(); |
| 1031 | assert_eq!(k.lookup("f6", false), Some(ActionId::FocusPanels)); |
| 1032 | |
| 1033 | |
| 1034 | assert_eq!(k.lookup("tab", false), Some(ActionId::Select)); |
| 1035 | } |
| 1036 | |
| 1037 | |
| 1038 | |
| 1039 | |
| 1040 | |
| 1041 | |
| 1042 | |
| 1043 | |
| 1044 | |
| 1045 | |
| 1046 | |
| 1047 | |
| 1048 | #[test] |
| 1049 | fn an_open_card_does_not_swallow_the_document_chords() { |
| 1050 | use crate::ui::stance::card_safe_action; |
| 1051 | use keymap::ActionId; |
| 1052 | |
| 1053 | |
| 1054 | for action in [ |
| 1055 | ActionId::Undo, |
| 1056 | ActionId::Redo, |
| 1057 | ActionId::Save, |
| 1058 | ActionId::Copy, |
| 1059 | ActionId::Paste, |
| 1060 | ] { |
| 1061 | assert!( |
| 1062 | card_safe_action(action), |
| 1063 | "{action:?} is a document chord and a card must not eat it" |
| 1064 | ); |
| 1065 | } |
| 1066 | |
| 1067 | |
| 1068 | |
| 1069 | for action in [ |
| 1070 | ActionId::Deselect, |
| 1071 | ActionId::Cut, |
| 1072 | ActionId::Delete, |
| 1073 | ActionId::ToggleMute, |
| 1074 | ActionId::Equalizer, |
| 1075 | ActionId::Color, |
| 1076 | ActionId::Loop, |
| 1077 | ActionId::TrimIn, |
| 1078 | ] { |
| 1079 | assert!( |
| 1080 | !card_safe_action(action), |
| 1081 | "{action:?} is a card's own stroke and the card must keep it" |
| 1082 | ); |
| 1083 | } |
| 1084 | |
| 1085 | let keys = keymap::Keymap::defaults(); |
| 1086 | for (key, ctrl, action) in [ |
| 1087 | ("z", true, ActionId::Undo), |
| 1088 | ("y", true, ActionId::Redo), |
| 1089 | ("s", true, ActionId::Save), |
| 1090 | ("c", true, ActionId::Copy), |
| 1091 | ("v", true, ActionId::Paste), |
| 1092 | ] { |
| 1093 | assert_eq!( |
| 1094 | keys.lookup(key, ctrl), |
| 1095 | Some(action), |
| 1096 | "the keymap no longer binds {action:?}" |
| 1097 | ); |
| 1098 | } |
| 1099 | |
| 1100 | |
| 1101 | let stance_rs = src_text("ui/stance.rs"); |
| 1102 | let dispatch = stance_rs.find("card_safe_action(action)").expect( |
| 1103 | "the root key handler no longer asks which chords a card does not take -- an open card \ |
| 1104 | eats every stroke again", |
| 1105 | ); |
| 1106 | assert!( |
| 1107 | stance_rs[..dispatch].contains("this.keymap.lookup(key, ctrl)"), |
| 1108 | "the dispatch no longer goes through the keymap, so a rebind would leave it behind" |
| 1109 | ); |
| 1110 | |
| 1111 | |
| 1112 | |
| 1113 | |
| 1114 | let resolve = stance_rs |
| 1115 | .find("if let Some(action) = this.keymap.lookup(key, ctrl)") |
| 1116 | .expect("the dispatch no longer resolves an action through the keymap"); |
| 1117 | let stmt = &stance_rs[resolve..]; |
| 1118 | let stmt = &stmt[..stmt.find('{').expect("the dispatch statement has no block")]; |
| 1119 | assert!( |
| 1120 | stmt.contains("card_safe_action("), |
| 1121 | "the dispatch resolves an action and never asks whether a card may take \ |
| 1122 | it, so an open card eats the document chords again" |
| 1123 | ); |
| 1124 | for (guard, label) in [ |
| 1125 | ("if this.param_card_key(", "the cards' own key branches"), |
| 1126 | ("if this.card_open()", "the blanket modal guard"), |
| 1127 | ] { |
| 1128 | let at = stance_rs |
| 1129 | .find(guard) |
| 1130 | .unwrap_or_else(|| panic!("{label} moved or was renamed")); |
| 1131 | assert!( |
| 1132 | dispatch < at, |
| 1133 | "{label} reads before the document dispatch -- an open card would \ |
| 1134 | swallow undo, redo, save and the clipboard pair again" |
| 1135 | ); |
| 1136 | } |
| 1137 | } |