| 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 | speed_keys: Vec::new(), |
| 215 | reversed: false, |
| 216 | stabilise: None, deinterlace: Default::default(), mark: Default::default(), |
| 217 | angle: Default::default(), |
| 218 | fade_in: 0, |
| 219 | fade_out: 0, |
| 220 | transition_out: 0, |
| 221 | transition_kind: Default::default(), |
| 222 | visualizer: 0, |
| 223 | viz_paint: 0, |
| 224 | gain_db: 0.0, |
| 225 | gain_keys: Vec::new(), |
| 226 | pan: 0, |
| 227 | pitch: 0.0, |
| 228 | transform_keys: Vec::new(), |
| 229 | opacity_keys: Vec::new(), |
| 230 | color_keys: Vec::new(), |
| 231 | effects: Vec::new(), |
| 232 | start, |
| 233 | in_frame: 0, |
| 234 | out_frame: frames, |
| 235 | source: 0, |
| 236 | link, |
| 237 | eq: None, |
| 238 | color: None, |
| 239 | transform: None, |
| 240 | fit: Default::default(), |
| 241 | speed: Default::default(), |
| 242 | }; |
| 243 | |
| 244 | |
| 245 | let video = [clip(100, 60, Some(7))]; |
| 246 | let audio = [clip(100, 60, Some(7)), clip(400, 30, None)]; |
| 247 | let lanes: [&[Clip]; 2] = [&video, &audio]; |
| 248 | |
| 249 | |
| 250 | let mut all = snap_marks(&lanes, None, None, 300, None, &[]); |
| 251 | all.sort_unstable(); |
| 252 | assert_eq!(all, [0, 100, 100, 160, 160, 300, 400, 430]); |
| 253 | |
| 254 | |
| 255 | |
| 256 | |
| 257 | |
| 258 | |
| 259 | let mut carried = snap_marks(&lanes, Some((0, 0)), Some(7), 300, None, &[]); |
| 260 | carried.sort_unstable(); |
| 261 | assert_eq!(carried, [0, 300, 400, 430]); |
| 262 | |
| 263 | |
| 264 | assert_eq!(snap_marks(&[], Some((3, 9)), None, 0, None, &[]), [0, 0]); |
| 265 | } |
| 266 | |
| 267 | |
| 268 | #[test] |
| 269 | fn the_snap_names_the_mark_it_landed_on_unless_it_is_switched_off() { |
| 270 | let marks = [100, 160, 300, 0]; |
| 271 | |
| 272 | |
| 273 | assert_eq!(snap_cue(true, 62, 40, 4, &marks), (60, Some(100))); |
| 274 | |
| 275 | assert_eq!(snap_cue(true, 158, 40, 4, &marks), (160, Some(160))); |
| 276 | |
| 277 | assert_eq!(snap_cue(true, 298, 0, 4, &marks), (300, Some(300))); |
| 278 | |
| 279 | assert_eq!(snap_cue(true, 200, 40, 4, &marks), (200, None)); |
| 280 | |
| 281 | |
| 282 | assert_eq!(snap_cue(false, 158, 40, 4, &marks), (158, None)); |
| 283 | } |
| 284 | |
| 285 | |
| 286 | |
| 287 | |
| 288 | #[test] |
| 289 | fn a_stated_rate_never_renders_as_a_zero() { |
| 290 | let edges: Vec<u64> = (0..12) |
| 291 | .flat_map(|e| { |
| 292 | let decade = 10_u64.pow(e); |
| 293 | [decade, decade * 4, decade * 5, decade * 9] |
| 294 | }) |
| 295 | .chain([999, 1_000, 999_999, 1_000_000, 4_998, 113, 2_400, 8_432_000]) |
| 296 | .collect(); |
| 297 | for bits in edges { |
| 298 | let line = bits_label(bits); |
| 299 | let number: f64 = line |
| 300 | .split(' ') |
| 301 | .next() |
| 302 | .and_then(|n| n.parse().ok()) |
| 303 | .unwrap_or_else(|| panic!("no number in {line:?}")); |
| 304 | assert!(number > 0., "{bits} bits a second rendered as {line:?}"); |
| 305 | } |
| 306 | } |
| 307 | |
| 308 | #[test] |
| 309 | fn a_rate_is_said_in_the_unit_its_size_asks_for() { |
| 310 | assert_eq!(bits_label(113), "113 b/s"); |
| 311 | assert_eq!(bits_label(4_998), "5.0 kb/s"); |
| 312 | assert_eq!(bits_label(128_000), "128.0 kb/s"); |
| 313 | assert_eq!(bits_label(8_432_000), "8.4 Mb/s"); |
| 314 | } |
| 315 | |
| 316 | #[test] |
| 317 | fn a_size_reads_in_the_largest_unit_that_keeps_a_decimal() { |
| 318 | assert_eq!(size_human(1_200_000_000), "1.2 GB"); |
| 319 | assert_eq!(size_human(840_000_000), "840 MB"); |
| 320 | assert_eq!(size_human(512), "512 B"); |
| 321 | } |
| 322 | |
| 323 | #[test] |
| 324 | fn a_picture_names_its_height_the_way_people_do() { |
| 325 | assert_eq!(resolution_label(1080), "1080p"); |
| 326 | assert_eq!(resolution_label(720), "720p"); |
| 327 | assert_eq!(resolution_label(2160), "4K"); |
| 328 | } |
| 329 | |
| 330 | #[test] |
| 331 | fn a_picture_track_says_only_what_its_header_states() { |
| 332 | let video = engine::VideoFacts { |
| 333 | codec: engine::Codec::H264, |
| 334 | width: 1920, |
| 335 | height: 1080, |
| 336 | frame_rate: Some(29.97), |
| 337 | bit_depth: None, |
| 338 | matrix_assumed: false, |
| 339 | profile: None, |
| 340 | interlaced: None, |
| 341 | }; |
| 342 | assert_eq!( |
| 343 | video_detail(&video, Some(5_200_000)), |
| 344 | "H.264 · 1920×1080 · 29.97 fps · 5.2 Mb/s" |
| 345 | ); |
| 346 | let ten_bit = engine::VideoFacts { |
| 347 | codec: engine::Codec::Hevc, |
| 348 | bit_depth: Some(10), |
| 349 | matrix_assumed: false, |
| 350 | frame_rate: None, |
| 351 | ..video |
| 352 | }; |
| 353 | assert_eq!(video_detail(&ten_bit, None), "HEVC · 1920×1080 · 10-bit"); |
| 354 | let bare = engine::VideoFacts { |
| 355 | width: 0, |
| 356 | height: 0, |
| 357 | ..ten_bit |
| 358 | }; |
| 359 | assert_eq!(video_detail(&bare, None), "HEVC · 10-bit"); |
| 360 | } |
| 361 | |
| 362 | #[test] |
| 363 | fn a_sound_track_says_its_codec_rate_layout_depth_and_bitrate() { |
| 364 | let info = StreamInfo { |
| 365 | index: 0, |
| 366 | codec: "mp4a".to_string(), |
| 367 | channels: 2, |
| 368 | sample_rate: 48_000, |
| 369 | lang: None, |
| 370 | decodable: true, |
| 371 | bit_depth: None, |
| 372 | bitrate: Some(128_000), |
| 373 | }; |
| 374 | assert_eq!(audio_detail(&info), "mp4a · 48 kHz · stereo · 128.0 kb/s"); |
| 375 | let unnamed = StreamInfo { |
| 376 | codec: "unknown".to_string(), |
| 377 | channels: 0, |
| 378 | sample_rate: 0, |
| 379 | bitrate: None, |
| 380 | ..info |
| 381 | }; |
| 382 | assert_eq!(audio_detail(&unnamed), ""); |
| 383 | } |
| 384 | |
| 385 | #[test] |
| 386 | fn a_plate_lists_the_file_then_each_track_and_leaves_out_what_is_unknown() { |
| 387 | use crate::MediaBitrate; |
| 388 | let path = std::path::Path::new("/media/take-3.mp4"); |
| 389 | let facts = engine::MediaFacts { |
| 390 | container: Some("mp4".to_string()), |
| 391 | bytes: Some(1_200_000_000), |
| 392 | video: Some(engine::VideoFacts { |
| 393 | codec: engine::Codec::H264, |
| 394 | width: 1920, |
| 395 | height: 1080, |
| 396 | frame_rate: Some(29.97), |
| 397 | bit_depth: None, |
| 398 | matrix_assumed: false, |
| 399 | profile: None, |
| 400 | interlaced: None, |
| 401 | }), |
| 402 | duration_secs: None, |
| 403 | streams: Vec::new(), |
| 404 | attached_pic: false, |
| 405 | }; |
| 406 | let rate = MediaBitrate { |
| 407 | total: Some(8_432_000), |
| 408 | video: Some(7_918_000), |
| 409 | audio: None, |
| 410 | }; |
| 411 | let track = StreamInfo { |
| 412 | index: 0, |
| 413 | codec: "mp4a".to_string(), |
| 414 | channels: 2, |
| 415 | sample_rate: 48_000, |
| 416 | lang: None, |
| 417 | decodable: true, |
| 418 | bit_depth: None, |
| 419 | bitrate: None, |
| 420 | }; |
| 421 | let rows = file_properties(path, Some(&facts), std::slice::from_ref(&track), Some(rate)); |
| 422 | let labels: Vec<&str> = rows.iter().map(|(label, _)| label.as_str()).collect(); |
| 423 | assert_eq!( |
| 424 | labels, |
| 425 | ["File", "Path", "Container", "Size", "Video", "Audio"] |
| 426 | ); |
| 427 | assert_eq!(rows[0].1, "take-3.mp4"); |
| 428 | assert_eq!(rows[3].1, "1.2 GB · 8.4 Mb/s"); |
| 429 | assert_eq!(rows[4].1, "H.264 · 1920×1080 · 29.97 fps · 7.9 Mb/s"); |
| 430 | assert_eq!(rows[5].1, "mp4a · 48 kHz · stereo"); |
| 431 | |
| 432 | |
| 433 | let second = StreamInfo { |
| 434 | index: 1, |
| 435 | ..track.clone() |
| 436 | }; |
| 437 | let two = file_properties(path, None, &[track, second], None); |
| 438 | let labels: Vec<&str> = two.iter().map(|(label, _)| label.as_str()).collect(); |
| 439 | assert_eq!(labels, ["File", "Path", "Audio 1 of 2", "Audio 2 of 2"]); |
| 440 | |
| 441 | |
| 442 | assert_eq!(file_properties(path, None, &[], None).len(), 2); |
| 443 | } |
| 444 | |
| 445 | #[test] |
| 446 | fn timecode_counts_frames_inside_the_second() { |
| 447 | assert_eq!(timecode(0., 30.), "00:00:00:00"); |
| 448 | assert_eq!(timecode(-1., 30.), "00:00:00:00"); |
| 449 | assert_eq!(timecode(4.9667, 30.), "00:00:04:29"); |
| 450 | assert_eq!(timecode(5., 30.), "00:00:05:00"); |
| 451 | assert_eq!(timecode(3661.5, 30.), "01:01:01:15"); |
| 452 | |
| 453 | assert_eq!(timecode(1. - f64::EPSILON, 30.), "00:00:00:29"); |
| 454 | assert_eq!(timecode(0.999, 29.97), "00:00:00:29"); |
| 455 | } |
| 456 | |
| 457 | #[test] |
| 458 | fn scrub_seeks_only_on_a_new_frame_and_only_every_100ms() { |
| 459 | let (slow, fast) = (Duration::from_millis(100), Duration::from_millis(99)); |
| 460 | |
| 461 | |
| 462 | assert!(scrub_due(31, 30, slow)); |
| 463 | assert!(!scrub_due(30, 30, slow)); |
| 464 | assert!(!scrub_due(31, 30, fast)); |
| 465 | assert!(!scrub_due(30, 30, fast)); |
| 466 | |
| 467 | assert!(scrub_due(1, 0, Duration::from_secs(9))); |
| 468 | } |
| 469 | |
| 470 | #[test] |
| 471 | fn the_picture_is_restarted_only_when_it_is_really_behind_and_not_twice() { |
| 472 | |
| 473 | |
| 474 | assert!(!should_resync(0., None)); |
| 475 | assert!(!should_resync(LATE_RESYNC, None)); |
| 476 | assert!(should_resync(LATE_RESYNC + 0.1, None)); |
| 477 | |
| 478 | |
| 479 | |
| 480 | assert!(!should_resync(9., Some(Instant::now()))); |
| 481 | assert!(should_resync( |
| 482 | 9., |
| 483 | Some(Instant::now() - RESYNC_GAP - Duration::from_millis(1)) |
| 484 | )); |
| 485 | } |
| 486 | |
| 487 | #[test] |
| 488 | fn the_default_export_is_never_a_source_and_never_beside_one() { |
| 489 | use crate::player::library::default_export_path; |
| 490 | use std::path::Path; |
| 491 | let src = Path::new("/no-such-edith-dir/speech.mp4"); |
| 492 | |
| 493 | let project = Path::new("/no-such-edith-dir/speech.edith"); |
| 494 | let out = default_export_path(project, true, Some("speech"), &[src], Format::Mp4); |
| 495 | |
| 496 | |
| 497 | let beside = out.parent() == src.parent(); |
| 498 | let want = if beside { "speech (2).mp4" } else { "speech.mp4" }; |
| 499 | assert!(out.to_string_lossy().ends_with(want), "{out:?}"); |
| 500 | assert_ne!(out, src); |
| 501 | |
| 502 | |
| 503 | let plain = out.with_file_name("speech.mp4"); |
| 504 | let two = default_export_path(project, true, Some("speech"), &[plain.as_path()], Format::Mp4); |
| 505 | assert!(two.to_string_lossy().ends_with("speech (2).mp4"), "{two:?}"); |
| 506 | let three = default_export_path(project, true, Some("speech"), &[plain.as_path(), two.as_path()], Format::Mp4); |
| 507 | assert!(three.to_string_lossy().ends_with("speech (3).mp4"), "{three:?}"); |
| 508 | |
| 509 | let untitled = default_export_path(Path::new(""), false, Some("speech"), &[src], Format::Webm); |
| 510 | assert!(untitled.to_string_lossy().ends_with("speech edit.webm"), "{untitled:?}"); |
| 511 | |
| 512 | let none = default_export_path(Path::new(""), false, None, &[], Format::Mp4); |
| 513 | assert!(none.to_string_lossy().ends_with("Untitled.mp4"), "{none:?}"); |
| 514 | } |
| 515 | |
| 516 | #[test] |
| 517 | fn the_first_save_lands_beside_the_media() { |
| 518 | assert_eq!( |
| 519 | project_path("assets/test_av.mp4"), |
| 520 | std::path::Path::new("assets/test_av.edith") |
| 521 | ); |
| 522 | |
| 523 | assert_eq!( |
| 524 | project_path("a.export.mp4"), |
| 525 | std::path::Path::new("a.export.edith") |
| 526 | ); |
| 527 | assert_eq!(project_path("clip"), std::path::Path::new("clip.edith")); |
| 528 | |
| 529 | assert_eq!(project_path("a.edith"), std::path::Path::new("a.edith")); |
| 530 | } |
| 531 | |
| 532 | #[test] |
| 533 | fn only_an_exact_edith_extension_is_a_project() { |
| 534 | let p = std::path::Path::new; |
| 535 | assert!(is_project(p("a.edith"))); |
| 536 | |
| 537 | assert!(is_project(p("/v.mp4/a.edith"))); |
| 538 | assert!(!is_project(p("a.mp4"))); |
| 539 | assert!(!is_project(p("/v.edith/a.mp4"))); |
| 540 | |
| 541 | |
| 542 | assert!(!is_project(p("a.EDITH"))); |
| 543 | |
| 544 | assert!(!is_project(p("edith"))); |
| 545 | assert!(!is_project(p("a.edith.mp4"))); |
| 546 | } |
| 547 | |
| 548 | |
| 549 | |
| 550 | |
| 551 | |
| 552 | #[test] |
| 553 | fn no_stroke_is_missing_from_the_keys_menu() { |
| 554 | use keymap::{ActionId, Keymap}; |
| 555 | |
| 556 | |
| 557 | |
| 558 | let handler = ui_source(); |
| 559 | let handler = handler.as_str(); |
| 560 | let keymap = Keymap::defaults(); |
| 561 | let listed = |key: &str| { |
| 562 | let pretty = keymap::Chord { |
| 563 | key: key.to_string(), |
| 564 | ctrl: false, |
| 565 | shift: false, |
| 566 | alt: false, |
| 567 | } |
| 568 | .pretty(); |
| 569 | keymap.lookup(key, false, false, false).is_some() |
| 570 | || keymap::FIXED.iter().any(|f| f.chord == pretty) |
| 571 | }; |
| 572 | let mut compared = 0; |
| 573 | for (at, needle) in handler.match_indices("key == ") { |
| 574 | let rest = &handler[at + needle.len()..]; |
| 575 | let key = match rest.strip_prefix('"') { |
| 576 | Some(literal) => literal[..literal.find('"').expect("unterminated key")].to_string(), |
| 577 | |
| 578 | None if rest.starts_with("ESCAPE") => crate::ESCAPE.to_string(), |
| 579 | |
| 580 | None if rest.starts_with("key") => continue, |
| 581 | None => panic!("a key compared against something this cannot read: {rest:.20}"), |
| 582 | }; |
| 583 | assert!( |
| 584 | listed(&key), |
| 585 | "the handler answers to {key:?} and the keys menu never says so" |
| 586 | ); |
| 587 | compared += 1; |
| 588 | } |
| 589 | |
| 590 | |
| 591 | assert!( |
| 592 | compared >= 4, |
| 593 | "the key comparisons moved; this scan is blind" |
| 594 | ); |
| 595 | |
| 596 | assert!(handler.contains("key.parse::<u32>()")); |
| 597 | assert!(keymap::FIXED.iter().any(|f| f.chord == "0–9")); |
| 598 | |
| 599 | for action in ActionId::ALL { |
| 600 | assert!(keymap::Category::ALL.contains(&action.category())); |
| 601 | } |
| 602 | for fixed in keymap::FIXED.iter() { |
| 603 | assert!(keymap::Category::ALL.contains(&fixed.category)); |
| 604 | } |
| 605 | } |
| 606 | |
| 607 | |
| 608 | |
| 609 | #[test] |
| 610 | fn a_moment_and_the_place_it_is_drawn_are_the_same_at_every_zoom() { |
| 611 | let duration = 20.; |
| 612 | |
| 613 | |
| 614 | |
| 615 | let fit = test_view(Scale::default(), duration).fit(); |
| 616 | assert_eq!(fit.pps, 10.); |
| 617 | assert_eq!(fit.width_px(5.), TEST_BED * 0.25); |
| 618 | for t in [0., 5., 12.5, 20.] { |
| 619 | assert_eq!(fit.px_at(t), (t / duration) as f32 * TEST_BED, "fit at {t}"); |
| 620 | } |
| 621 | for scale in [ |
| 622 | fit, |
| 623 | Scale { |
| 624 | pps: 20., |
| 625 | start: 4., |
| 626 | }, |
| 627 | Scale { |
| 628 | pps: 80., |
| 629 | start: 12.5, |
| 630 | }, |
| 631 | Scale { |
| 632 | pps: 375., |
| 633 | start: 19., |
| 634 | }, |
| 635 | ] { |
| 636 | let scale = test_view(scale, duration).settled(); |
| 637 | for x in [0., 50., 100., TEST_BED] { |
| 638 | let at = scale.time_at(x); |
| 639 | assert!( |
| 640 | (f64::from(scale.px_at(at)) - f64::from(x)).abs() < 1e-3, |
| 641 | "{scale:?} round trip at {x}" |
| 642 | ); |
| 643 | } |
| 644 | |
| 645 | assert!( |
| 646 | (f64::from(scale.width_px(test_view(scale, duration).span())) - f64::from(TEST_BED)) |
| 647 | .abs() |
| 648 | < 1e-3, |
| 649 | "{scale:?} spans the bed" |
| 650 | ); |
| 651 | } |
| 652 | } |
| 653 | |
| 654 | |
| 655 | |
| 656 | |
| 657 | #[test] |
| 658 | fn a_zoom_leaves_the_anchor_where_it_was_on_screen() { |
| 659 | let duration = 20.; |
| 660 | let mut scale = test_view(Scale::default(), duration).settled(); |
| 661 | |
| 662 | |
| 663 | for anchor in [100f32, 50., 180.] { |
| 664 | |
| 665 | |
| 666 | for _ in 0..30 { |
| 667 | let at = scale.time_at(anchor); |
| 668 | let view = test_view(scale, duration); |
| 669 | let zoomed = view.zoomed(ZOOM_STEP, anchor); |
| 670 | |
| 671 | |
| 672 | let span = test_view(zoomed, duration).span(); |
| 673 | let pinned = zoomed.start <= 0. || zoomed.start + span >= duration; |
| 674 | if !pinned { |
| 675 | assert!( |
| 676 | (f64::from(zoomed.px_at(at)) - f64::from(anchor)).abs() < 1e-3, |
| 677 | "{at} moved: {scale:?} -> {zoomed:?}" |
| 678 | ); |
| 679 | } |
| 680 | assert!(zoomed.pps >= scale.pps, "and it did zoom in"); |
| 681 | scale = zoomed; |
| 682 | } |
| 683 | } |
| 684 | } |
| 685 | |
| 686 | |
| 687 | |
| 688 | |
| 689 | |
| 690 | #[test] |
| 691 | fn zoom_stops_at_a_bedful_of_time_and_at_a_handful_of_frames() { |
| 692 | let (duration, fps) = (20., 30.); |
| 693 | let mut scale = Scale::default(); |
| 694 | for _ in 0..200 { |
| 695 | scale = test_view(scale, duration).zoomed(1. / ZOOM_STEP, 100.); |
| 696 | } |
| 697 | assert!( |
| 698 | (test_view(scale, duration).span() - f64::from(TEST_BED) / PPS_MIN).abs() < 1e-6, |
| 699 | "widest is a pixel to the second, not the 20 s that happen to be on it" |
| 700 | ); |
| 701 | for _ in 0..200 { |
| 702 | scale = test_view(scale, duration).zoomed(ZOOM_STEP, 100.); |
| 703 | } |
| 704 | assert_eq!( |
| 705 | (test_view(scale, duration).span() * fps).round(), |
| 706 | ZOOM_MIN_FRAMES, |
| 707 | "tightest is a handful of frames" |
| 708 | ); |
| 709 | |
| 710 | let end = test_view( |
| 711 | Scale { |
| 712 | start: 1e6, |
| 713 | ..scale |
| 714 | }, |
| 715 | duration, |
| 716 | ); |
| 717 | let end = (end.settled(), end.span()); |
| 718 | assert!((end.0.start + end.1 - duration).abs() < 1e-9); |
| 719 | assert_eq!( |
| 720 | test_view( |
| 721 | Scale { |
| 722 | start: -5., |
| 723 | ..scale |
| 724 | }, |
| 725 | duration |
| 726 | ) |
| 727 | .settled() |
| 728 | .start, |
| 729 | 0. |
| 730 | ); |
| 731 | |
| 732 | |
| 733 | |
| 734 | let tiny = test_view(Scale::default(), 0.1); |
| 735 | assert!(tiny.zoomed(ZOOM_STEP, 100.).pps > PPS_DEFAULT); |
| 736 | assert!(tiny.zoomed(1. / ZOOM_STEP, 100.).pps < PPS_DEFAULT); |
| 737 | |
| 738 | |
| 739 | let empty = test_view(Scale::default(), 0.); |
| 740 | assert_eq!(empty.settled(), Scale::default()); |
| 741 | assert_eq!(empty.fit(), Scale::default()); |
| 742 | assert_eq!(Scale::default().px_at(0.), 0.); |
| 743 | |
| 744 | |
| 745 | let unpainted = View { |
| 746 | bed: 0., |
| 747 | ..test_view( |
| 748 | Scale { |
| 749 | pps: 4e6, |
| 750 | start: 3., |
| 751 | }, |
| 752 | duration, |
| 753 | ) |
| 754 | }; |
| 755 | assert_eq!(unpainted.settled().pps, 4e6); |
| 756 | assert_eq!(unpainted.following(19.), unpainted.scale); |
| 757 | } |
| 758 | |
| 759 | |
| 760 | |
| 761 | |
| 762 | |
| 763 | #[test] |
| 764 | fn zooming_out_reaches_the_end_of_a_timeline_however_long_it_is() { |
| 765 | let bed = 900.; |
| 766 | let view = |scale: Scale, duration: f64| View { |
| 767 | scale, |
| 768 | bed, |
| 769 | duration, |
| 770 | fps: 30., |
| 771 | }; |
| 772 | |
| 773 | let out = |duration: f64| { |
| 774 | let mut scale = Scale::default(); |
| 775 | for _ in 0..400 { |
| 776 | scale = view(scale, duration).zoomed(1. / ZOOM_STEP, 0.); |
| 777 | } |
| 778 | scale |
| 779 | }; |
| 780 | let five_hours = 2. * 2.5 * 3600.; |
| 781 | let wide = out(five_hours); |
| 782 | |
| 783 | |
| 784 | assert_eq!(wide.start, 0.); |
| 785 | let end = wide.px_at(five_hours); |
| 786 | assert!( |
| 787 | end < bed, |
| 788 | "the end of the timeline is off the bed at {end} px" |
| 789 | ); |
| 790 | assert!(end > bed * 0.9, "and not shrunk into a corner: {end} px"); |
| 791 | assert!( |
| 792 | (view(wide, five_hours).span() - five_hours * ZOOM_OUT_MARGIN).abs() < 1e-6, |
| 793 | "the far stop is the timeline plus its margin" |
| 794 | ); |
| 795 | |
| 796 | |
| 797 | let mut scale = wide; |
| 798 | for _ in 0..400 { |
| 799 | scale = view(scale, five_hours).zoomed(ZOOM_STEP, 0.); |
| 800 | } |
| 801 | assert_eq!( |
| 802 | (view(scale, five_hours).span() * 30.).round(), |
| 803 | ZOOM_MIN_FRAMES |
| 804 | ); |
| 805 | |
| 806 | |
| 807 | |
| 808 | assert!((view(out(10.), 10.).span() - f64::from(bed) / PPS_MIN).abs() < 1e-6); |
| 809 | |
| 810 | |
| 811 | assert_eq!(view(Scale::default(), 10.).settled(), Scale::default()); |
| 812 | assert_eq!( |
| 813 | view(Scale::default(), five_hours).settled(), |
| 814 | Scale::default() |
| 815 | ); |
| 816 | |
| 817 | |
| 818 | let shrunk = view(wide, 1800.).settled(); |
| 819 | assert!((view(shrunk, 1800.).span() - 1800. * ZOOM_OUT_MARGIN).abs() < 1e-6); |
| 820 | assert!(shrunk.px_at(1800.) < bed); |
| 821 | |
| 822 | |
| 823 | assert_eq!(view(wide, 2. * five_hours).settled().pps, wide.pps); |
| 824 | assert!(view(wide, 2. * five_hours).zoomed(1. / ZOOM_STEP, 0.).pps < wide.pps); |
| 825 | } |
| 826 | |
| 827 | |
| 828 | |
| 829 | |
| 830 | #[test] |
| 831 | fn a_scroll_slides_the_view_without_zooming_it() { |
| 832 | let duration = 60.; |
| 833 | |
| 834 | |
| 835 | let start = test_view( |
| 836 | Scale { |
| 837 | pps: f64::from(TEST_BED) / 10., |
| 838 | start: 0., |
| 839 | }, |
| 840 | duration, |
| 841 | ) |
| 842 | .settled(); |
| 843 | let notch = TEST_BED * SCROLL_NOTCH_SHARE; |
| 844 | let on = test_view(start, duration).scrolled(notch); |
| 845 | |
| 846 | |
| 847 | assert_eq!(on.pps, start.pps, "a scroll is not a zoom"); |
| 848 | assert!( |
| 849 | (on.start - (start.start + 1.)).abs() < 1e-9, |
| 850 | "one notch is a tenth of the 10 s on the bed: {on:?}" |
| 851 | ); |
| 852 | assert!( |
| 853 | (f64::from(start.px_at(20.) - on.px_at(20.)) - f64::from(notch)).abs() < 1e-3, |
| 854 | "and what was drawn moved with it: {on:?}" |
| 855 | ); |
| 856 | |
| 857 | let back = test_view(on, duration).scrolled(-notch); |
| 858 | assert!((back.start - start.start).abs() < 1e-9, "{back:?}"); |
| 859 | |
| 860 | |
| 861 | |
| 862 | let mut scale = start; |
| 863 | for _ in 0..200 { |
| 864 | scale = test_view(scale, duration).scrolled(notch); |
| 865 | } |
| 866 | assert_eq!(scale.pps, start.pps); |
| 867 | assert!( |
| 868 | (scale.start - (duration - 10.)).abs() < 1e-6, |
| 869 | "the tail stops with the end on the bed: {scale:?}" |
| 870 | ); |
| 871 | for _ in 0..200 { |
| 872 | scale = test_view(scale, duration).scrolled(-notch); |
| 873 | } |
| 874 | assert_eq!(scale.start, 0., "and the head stops at the head: {scale:?}"); |
| 875 | |
| 876 | let whole = test_view(Scale::default(), duration).fit(); |
| 877 | assert_eq!(test_view(whole, duration).scrolled(notch), whole); |
| 878 | } |
| 879 | |
| 880 | |
| 881 | |
| 882 | |
| 883 | #[test] |
| 884 | fn the_view_follows_a_playhead_that_runs_off_the_bed() { |
| 885 | let duration = 20.; |
| 886 | |
| 887 | let view = test_view( |
| 888 | Scale { |
| 889 | pps: 40., |
| 890 | start: 5., |
| 891 | }, |
| 892 | duration, |
| 893 | ); |
| 894 | let scale = view.settled(); |
| 895 | assert_eq!(view.span(), 5.); |
| 896 | |
| 897 | for at in [5., 7.5, 10.] { |
| 898 | assert_eq!(view.following(at), scale, "{at} is on the bed"); |
| 899 | } |
| 900 | |
| 901 | |
| 902 | let moved = view.following(12.); |
| 903 | assert_eq!(moved.pps, scale.pps); |
| 904 | assert!(moved.start > scale.start, "scrolled forward"); |
| 905 | assert!( |
| 906 | moved.px_at(12.) > 0. && moved.px_at(12.) < TEST_BED, |
| 907 | "and the playhead is on screen" |
| 908 | ); |
| 909 | |
| 910 | let back = view.following(1.); |
| 911 | assert!(back.start < scale.start); |
| 912 | assert!(back.px_at(1.) >= 0.); |
| 913 | |
| 914 | let whole = test_view(Scale::default(), duration); |
| 915 | let fit = whole.fit(); |
| 916 | for at in [0., 10., 20.] { |
| 917 | assert_eq!( |
| 918 | test_view(fit, duration).following(at), |
| 919 | fit, |
| 920 | "the fit never scrolls" |
| 921 | ); |
| 922 | } |
| 923 | } |
| 924 | |
| 925 | |
| 926 | |
| 927 | |
| 928 | |
| 929 | |
| 930 | |
| 931 | #[test] |
| 932 | fn a_scroll_during_playback_wins_until_the_head_catches_up() { |
| 933 | let duration = 80.; |
| 934 | let mut scale = test_view( |
| 935 | Scale { |
| 936 | pps: 40., |
| 937 | start: 0., |
| 938 | }, |
| 939 | duration, |
| 940 | ) |
| 941 | .settled(); |
| 942 | |
| 943 | |
| 944 | |
| 945 | let frame = |scale: Scale, panned: &mut bool, at: f64| { |
| 946 | let scale = match *panned { |
| 947 | true => test_view(scale, duration).settled(), |
| 948 | false => test_view(scale, duration).following(at), |
| 949 | }; |
| 950 | if *panned && test_view(scale, duration).shows(at) { |
| 951 | *panned = false; |
| 952 | } |
| 953 | scale |
| 954 | }; |
| 955 | |
| 956 | |
| 957 | let notch = f64::from(TEST_BED * SCROLL_NOTCH_SHARE); |
| 958 | let mut panned = false; |
| 959 | for n in 1..=5 { |
| 960 | scale = test_view(scale, duration).scrolled(TEST_BED * SCROLL_NOTCH_SHARE); |
| 961 | panned = true; |
| 962 | scale = frame(scale, &mut panned, 1.); |
| 963 | |
| 964 | |
| 965 | |
| 966 | assert_eq!(panned, scale.start > 1., "held while the head is off"); |
| 967 | assert!( |
| 968 | (scale.start - notch * f64::from(n) / 40.).abs() < 1e-6, |
| 969 | "notch {n} reached the screen: {scale:?}" |
| 970 | ); |
| 971 | } |
| 972 | |
| 973 | let span = test_view(scale, duration).span(); |
| 974 | let start = frame(scale, &mut panned, scale.start + span / 2.).start; |
| 975 | assert!(!panned, "the follow has the view back"); |
| 976 | assert_eq!(start, scale.start, "and it did not jump to take it"); |
| 977 | |
| 978 | let ran_off = frame(scale, &mut panned, start + span + 1.); |
| 979 | assert!( |
| 980 | ran_off.start > start, |
| 981 | "the follow follows again: {ran_off:?}" |
| 982 | ); |
| 983 | |
| 984 | |
| 985 | assert!(test_view(scale, duration).shows(scale.start)); |
| 986 | assert!(!test_view(scale, duration).shows(scale.start + span + 0.5)); |
| 987 | } |
| 988 | |
| 989 | |
| 990 | |
| 991 | |
| 992 | |
| 993 | #[test] |
| 994 | fn a_clip_is_drawn_the_same_width_whatever_else_is_on_the_timeline() { |
| 995 | let bed = 900.; |
| 996 | let of = |duration: f64| View { |
| 997 | scale: Scale::default(), |
| 998 | bed, |
| 999 | duration, |
| 1000 | fps: 30., |
| 1001 | }; |
| 1002 | |
| 1003 | |
| 1004 | let (alone, joined) = (of(5.).settled(), of(20.).settled()); |
| 1005 | assert_eq!(alone, joined); |
| 1006 | assert_eq!(alone.width_px(5.), joined.width_px(5.)); |
| 1007 | assert_eq!(joined.px_at(5.), alone.width_px(5.)); |
| 1008 | |
| 1009 | assert!( |
| 1010 | alone.width_px(5.) < bed / 2., |
| 1011 | "5 s at {} px/s is {} px of a {bed} px bed", |
| 1012 | alone.pps, |
| 1013 | alone.width_px(5.) |
| 1014 | ); |
| 1015 | |
| 1016 | |
| 1017 | let out = of(5.).zoomed(1. / ZOOM_STEP, 0.); |
| 1018 | assert!( |
| 1019 | out.width_px(5.) < alone.width_px(5.), |
| 1020 | "{} px is not smaller than {} px", |
| 1021 | out.width_px(5.), |
| 1022 | alone.width_px(5.) |
| 1023 | ); |
| 1024 | |
| 1025 | assert_eq!(of(5.).fit().width_px(5.), bed); |
| 1026 | } |
| 1027 | |
| 1028 | |
| 1029 | |
| 1030 | #[test] |
| 1031 | fn the_zoom_button_says_how_much_is_on_the_bed() { |
| 1032 | |
| 1033 | assert_eq!(secs_label(1. / 60.), "0.02s"); |
| 1034 | assert_eq!(secs_label(4.5), "4.5s"); |
| 1035 | } |
| 1036 | |
| 1037 | |
| 1038 | |
| 1039 | |
| 1040 | |
| 1041 | |
| 1042 | |
| 1043 | #[test] |
| 1044 | fn tab_cycles_dock_bench_inspector_and_wraps() { |
| 1045 | use crate::ui::stance::{Surface, next_surface}; |
| 1046 | |
| 1047 | assert_eq!(next_surface(Surface::Dock, false), Surface::Bench); |
| 1048 | assert_eq!(next_surface(Surface::Bench, false), Surface::Inspector); |
| 1049 | assert_eq!(next_surface(Surface::Inspector, false), Surface::Dock); |
| 1050 | |
| 1051 | |
| 1052 | assert_eq!(next_surface(Surface::Dock, true), Surface::Inspector); |
| 1053 | assert_eq!(next_surface(Surface::Inspector, true), Surface::Bench); |
| 1054 | assert_eq!(next_surface(Surface::Bench, true), Surface::Dock); |
| 1055 | } |
| 1056 | |
| 1057 | |
| 1058 | |
| 1059 | |
| 1060 | |
| 1061 | |
| 1062 | |
| 1063 | |
| 1064 | #[test] |
| 1065 | fn surface_wants_src_active_mounts_the_tab_before_focus_lands_on_it() { |
| 1066 | use crate::ui::stance::{Surface, surface_wants_src_active}; |
| 1067 | |
| 1068 | assert_eq!(surface_wants_src_active(Surface::Dock), Some(true)); |
| 1069 | assert_eq!(surface_wants_src_active(Surface::Inspector), Some(false)); |
| 1070 | |
| 1071 | |
| 1072 | assert_eq!(surface_wants_src_active(Surface::Bench), None); |
| 1073 | } |
| 1074 | |
| 1075 | |
| 1076 | |
| 1077 | |
| 1078 | |
| 1079 | |
| 1080 | |
| 1081 | |
| 1082 | |
| 1083 | |
| 1084 | |
| 1085 | |
| 1086 | |
| 1087 | #[test] |
| 1088 | fn only_tab_is_intercepted_by_a_focused_surface_everything_else_reaches_root() { |
| 1089 | use crate::ui::stance::is_focus_cycle_key; |
| 1090 | |
| 1091 | assert!(is_focus_cycle_key("tab")); |
| 1092 | for key in [ |
| 1093 | "space", "escape", "s", "c", "left", "right", "enter", "delete", "?", |
| 1094 | ] { |
| 1095 | assert!(!is_focus_cycle_key(key), "{key} must bubble to the root"); |
| 1096 | } |
| 1097 | } |
| 1098 | |
| 1099 | |
| 1100 | |
| 1101 | |
| 1102 | |
| 1103 | |
| 1104 | #[test] |
| 1105 | fn escape_is_the_only_key_a_focused_surface_uses_to_leave_the_ring() { |
| 1106 | use crate::ui::stance::is_focus_exit_key; |
| 1107 | |
| 1108 | assert!(is_focus_exit_key("escape")); |
| 1109 | for key in [ |
| 1110 | "tab", "space", "s", "c", "left", "right", "enter", "delete", "?", |
| 1111 | ] { |
| 1112 | assert!(!is_focus_exit_key(key), "{key} must not exit the ring"); |
| 1113 | } |
| 1114 | } |
| 1115 | |
| 1116 | |
| 1117 | |
| 1118 | |
| 1119 | |
| 1120 | #[test] |
| 1121 | fn focus_panels_is_bound_and_lands_on_the_dock() { |
| 1122 | use keymap::{ActionId, Keymap}; |
| 1123 | |
| 1124 | let k = Keymap::defaults(); |
| 1125 | assert_eq!(k.lookup("f6", false, false, false), Some(ActionId::FocusPanels)); |
| 1126 | |
| 1127 | |
| 1128 | assert_eq!(k.lookup("tab", false, false, false), Some(ActionId::Select)); |
| 1129 | } |
| 1130 | |
| 1131 | |
| 1132 | |
| 1133 | |
| 1134 | |
| 1135 | |
| 1136 | |
| 1137 | |
| 1138 | |
| 1139 | |
| 1140 | |
| 1141 | |
| 1142 | #[test] |
| 1143 | fn an_open_card_does_not_swallow_the_document_chords() { |
| 1144 | use crate::ui::stance::card_safe_action; |
| 1145 | use keymap::ActionId; |
| 1146 | |
| 1147 | |
| 1148 | for action in [ |
| 1149 | ActionId::Undo, |
| 1150 | ActionId::Redo, |
| 1151 | ActionId::Save, |
| 1152 | ActionId::Copy, |
| 1153 | ActionId::Paste, |
| 1154 | ] { |
| 1155 | assert!( |
| 1156 | card_safe_action(action), |
| 1157 | "{action:?} is a document chord and a card must not eat it" |
| 1158 | ); |
| 1159 | } |
| 1160 | |
| 1161 | |
| 1162 | |
| 1163 | for action in [ |
| 1164 | ActionId::Deselect, |
| 1165 | ActionId::Cut, |
| 1166 | ActionId::Delete, |
| 1167 | ActionId::ToggleMute, |
| 1168 | ActionId::Equalizer, |
| 1169 | ActionId::Color, |
| 1170 | ActionId::Loop, |
| 1171 | ActionId::TrimIn, |
| 1172 | ] { |
| 1173 | assert!( |
| 1174 | !card_safe_action(action), |
| 1175 | "{action:?} is a card's own stroke and the card must keep it" |
| 1176 | ); |
| 1177 | } |
| 1178 | |
| 1179 | let keys = keymap::Keymap::defaults(); |
| 1180 | for (key, ctrl, action) in [ |
| 1181 | ("z", true, ActionId::Undo), |
| 1182 | ("y", true, ActionId::Redo), |
| 1183 | ("s", true, ActionId::Save), |
| 1184 | ("c", true, ActionId::Copy), |
| 1185 | ("v", true, ActionId::Paste), |
| 1186 | ] { |
| 1187 | assert_eq!( |
| 1188 | keys.lookup(key, ctrl, false, false), |
| 1189 | Some(action), |
| 1190 | "the keymap no longer binds {action:?}" |
| 1191 | ); |
| 1192 | } |
| 1193 | |
| 1194 | |
| 1195 | let stance_rs = src_text("ui/stance.rs"); |
| 1196 | let dispatch = stance_rs.find("card_safe_action(action)").expect( |
| 1197 | "the root key handler no longer asks which chords a card does not take -- an open card \ |
| 1198 | eats every stroke again", |
| 1199 | ); |
| 1200 | assert!( |
| 1201 | stance_rs[..dispatch].contains("this.keymap.lookup(key, ctrl, shift, alt)"), |
| 1202 | "the dispatch no longer goes through the keymap, so a rebind would leave it behind" |
| 1203 | ); |
| 1204 | |
| 1205 | |
| 1206 | |
| 1207 | |
| 1208 | let resolve = stance_rs |
| 1209 | .find("if let Some(action) = this.keymap.lookup(key, ctrl, shift, alt)") |
| 1210 | .expect("the dispatch no longer resolves an action through the keymap"); |
| 1211 | let stmt = &stance_rs[resolve..]; |
| 1212 | let stmt = &stmt[..stmt.find('{').expect("the dispatch statement has no block")]; |
| 1213 | assert!( |
| 1214 | stmt.contains("card_safe_action("), |
| 1215 | "the dispatch resolves an action and never asks whether a card may take \ |
| 1216 | it, so an open card eats the document chords again" |
| 1217 | ); |
| 1218 | for (guard, label) in [ |
| 1219 | ("if this.param_card_key(", "the cards' own key branches"), |
| 1220 | ("if this.card_open()", "the blanket modal guard"), |
| 1221 | ] { |
| 1222 | let at = stance_rs |
| 1223 | .find(guard) |
| 1224 | .unwrap_or_else(|| panic!("{label} moved or was renamed")); |
| 1225 | assert!( |
| 1226 | dispatch < at, |
| 1227 | "{label} reads before the document dispatch -- an open card would \ |
| 1228 | swallow undo, redo, save and the clipboard pair again" |
| 1229 | ); |
| 1230 | } |
| 1231 | } |
| 1232 | |
| 1233 | |
| 1234 | |
| 1235 | #[test] |
| 1236 | fn play_around_is_shift_k_and_runs_two_seconds_either_side() { |
| 1237 | use crate::timeline_math::{PLAY_AROUND_PAD, play_around_span}; |
| 1238 | let km = crate::keymap::Keymap::defaults(); |
| 1239 | assert_eq!(km.lookup("k", false, true, false), Some(ActionId::PlayAround)); |
| 1240 | assert_eq!(km.lookup("k", false, false, false), Some(ActionId::ShuttleStop), "plain K still stops"); |
| 1241 | assert_eq!(PLAY_AROUND_PAD, 2.0); |
| 1242 | assert_eq!(play_around_span(10., 60.), (8., 12.)); |
| 1243 | assert_eq!(play_around_span(1., 60.), (0., 3.), "the lead-in stops at the start"); |
| 1244 | assert_eq!(play_around_span(59., 60.), (57., 60.), "the run-on stops at the end"); |
| 1245 | assert_eq!(play_around_span(0., 0.), (0., 0.), "an empty timeline has no run"); |
| 1246 | } |
| 1247 | |
| 1248 | |
| 1249 | |
| 1250 | |
| 1251 | #[test] |
| 1252 | fn a_drop_snaps_to_a_marker_or_a_chapter_edge() { |
| 1253 | |
| 1254 | let notes = [250, 250, 380, 400, 500]; |
| 1255 | let marks = snap_marks(&[], None, None, 0, None, ¬es); |
| 1256 | for mark in [250, 380, 400, 500] { |
| 1257 | assert!(marks.contains(&mark), "{mark} is a snap target"); |
| 1258 | } |
| 1259 | assert_eq!(snapped(248, 40, 4, &marks), 250, "a head a frame short of a marker meets it"); |
| 1260 | assert_eq!(snapped(497, 0, 4, &marks), 500, "a chapter start pulls the clip onto it"); |
| 1261 | assert_eq!(snapped(150, 40, 4, &marks), 150, "open bed beside them stays where it was"); |
| 1262 | } |