forge
Explore
Log in

Don't you dare go Hollow

edith

Clone
★ 0

0 watching0 forksRust18.3 MiB978e685Tahinli3 months ago627 commits

view.rs876 lines, 35.9 KiBHistoryRawDownload
1//! Where a moment is drawn: drops and snaps, the zoom and the scroll, the
2//! paths a file is written to, and the strokes the handler answers.
3
4use super::*;
5
6/// What a drop reads: the frame under the pointer, through the same scale
7/// the boxes are drawn through. Zoomed in, the same pixel is a different
8/// frame -- which is the whole reason `Player::frame_under` goes through
9/// [`Scale`] rather than through the duration alone.
10#[test]
11fn a_drop_reads_the_frame_under_the_pointer_at_every_zoom() {
12 // A 200 px bed inset by the panel's padding, 10 seconds at 30 fps.
13 let bed: Bounds<Pixels> = Bounds {
14 origin: point(px(12.), px(400.)),
15 size: size(px(200.), px(6.)),
16 };
17 let fps = 30.;
18 // The frame a pointer at window `x` names, exactly as `frame_under`
19 // composes it.
20 let under = |scale: Scale, x: f32| frame_at(scale.time_at(px_along(px(x), bed)), fps);
21
22 // The whole 10 s timeline across the 200 px bed: 20 px to the second,
23 // so halfway along is frame 150.
24 let fit = Scale {
25 pps: 20.,
26 start: 0.,
27 };
28 assert_eq!(under(fit, 12.), 0);
29 assert_eq!(under(fit, 112.), 150);
30 assert_eq!(under(fit, 212.), 300);
31
32 // Four times in, starting at second 5: the same middle pixel is now the
33 // frame in the middle of seconds 5..7.5, and the left edge is not 0.
34 let zoomed = Scale {
35 pps: 80.,
36 start: 5.,
37 };
38 assert_eq!(under(zoomed, 12.), 150);
39 assert_eq!(under(zoomed, 112.), 187);
40 assert_eq!(under(zoomed, 212.), 225);
41 // ...and a pointer that slid off either end of the bed names an end of
42 // the bed, never a pixel outside it.
43 assert_eq!(under(zoomed, 0.), 150);
44 assert_eq!(under(fit, 9999.), 300);
45}
46
47/// The snap: a clip let go a few frames off a neighbour's edge lands *on*
48/// it, by whichever of its own ends is nearer, and a clip let go in open bed
49/// stays exactly where the hand left it.
50#[test]
51fn a_dropped_clip_snaps_to_the_edges_worth_landing_on() {
52 // A neighbour covering [100, 160) and the playhead at 300.
53 let marks = [100, 160, 300, 0];
54 // Head a frame short of the neighbour's tail: laid end to end with it.
55 assert_eq!(snapped(158, 40, 4, &marks), 160);
56 // Tail a frame into its head: pulled back so the two meet exactly.
57 assert_eq!(snapped(62, 40, 4, &marks), 60);
58 // The playhead is an edge like any other.
59 assert_eq!(snapped(298, 40, 4, &marks), 300);
60 // Outside the tolerance nothing moves -- a gap the hand meant to leave
61 // is a gap.
62 assert_eq!(snapped(150, 40, 4, &marks), 150);
63 // No tolerance at all (zoomed right in, where a few pixels are worth
64 // less than a frame) is no snap: single frames are placed by hand.
65 assert_eq!(snapped(158, 40, 0, &marks), 158);
66 // The nearer edge wins when two are in reach.
67 assert_eq!(snapped(101, 40, 8, &marks), 100);
68 // ...and a mark closer to the head than `len` cannot pull the clip to a
69 // negative start.
70 assert_eq!(snapped(2, 40, 4, &marks), 0);
71}
72
73/// The shadow a drag draws and the drop that commits are one answer: both
74/// ask [`landing`], so the box seen in flight is the box the release leaves
75/// behind. What this pins down is the composition around the snap -- the
76/// grab offset comes off *before* the magnet, or a clip taken by its tail
77/// would land a boxful late.
78#[test]
79fn a_ghost_and_a_drop_are_one_landing() {
80 // A neighbour covering [100, 160), the playhead at 300, and a 40 frame
81 // clip taken hold of 12 frames in.
82 let marks = [100, 160, 300, 0];
83 let (len, grab, tol) = (40, 12, 4);
84 // Pointer at frame 170: the head is 12 frames behind it, at 158, which
85 // is a frame short of the neighbour's tail -- so both the ghost and the
86 // drop say 160, and the line stands on the mark that pulled it.
87 assert_eq!(landing(170, grab, len, true, tol, &marks), (160, Some(160)));
88 // Without the grab taken off first, the same pointer would land the
89 // box at 170 and no mark would be in reach: the offset is not cosmetic.
90 assert_eq!(landing(170, 0, len, true, tol, &marks), (170, None));
91 // A library row carries no grab and no length the engine has measured,
92 // which is how `Player::place_frame` asks: only its head lands, on the
93 // playhead here.
94 assert_eq!(landing(298, 0, 0, true, tol, &marks), (300, Some(300)));
95 // The magnet off, ghost and drop agree on the raw frame and no line is
96 // drawn -- the frame-by-frame placement the switch is for.
97 assert_eq!(landing(170, grab, len, false, tol, &marks), (158, None));
98 // A pointer nearer the bed's start than the hand is into the box cannot
99 // pull a head below zero.
100 assert_eq!(landing(3, grab, len, true, tol, &marks), (0, Some(0)));
101}
102
103/// Which lanes tint that shadow as refused: the two kinds of file a lane
104/// cannot hold, in the words the release would say them in -- one rule, so
105/// what is shown as impossible is exactly what is refused.
106#[test]
107fn a_lane_refuses_the_files_it_cannot_hold_before_the_release_says_so() {
108 let (video, audio) = (Lane::V1, Lane::A1);
109 let sound = Path::new("/media/take.mp3");
110 let still = Path::new("/media/card.png");
111 let movie = Path::new("/media/take.mp4");
112 assert_eq!(
113 lane_refuses(sound, video).as_deref(),
114 Some("NOT ON V1 — take.mp3 has no picture; drop it on an audio lane")
115 );
116 assert_eq!(
117 lane_refuses(still, audio).as_deref(),
118 Some("NOT ON A1 — card.png is a still image; drop it on a video lane")
119 );
120 // ...and every lane a file *can* go on says nothing at all, which is a
121 // ghost drawn in the file's own colour.
122 assert_eq!(lane_refuses(sound, audio), None);
123 assert_eq!(lane_refuses(still, video), None);
124 assert_eq!(lane_refuses(movie, video), None);
125 // A file with a picture is not refused by an audio lane here: the
126 // engine takes its sound onto one, and the words for a video-only file
127 // are its own.
128 assert_eq!(lane_refuses(movie, audio), None);
129 // ...and a subtitle lane holds none of the three: a caption comes off the
130 // Subtitles list, which is where the refusal points.
131 for file in [sound, still, movie] {
132 let why = lane_refuses(file, Lane::S1).expect("a subtitle lane takes no media");
133 assert!(why.starts_with("NOT ON S1 — "), "{why}");
134 assert!(why.contains("Subtitles list"), "{why}");
135 }
136 // How the two clocks a caption has meet, which is what a palette row's
137 // length is worked out with: a second of words at 30 fps is 30 frames,
138 // and a track shorter than one frame is still a caption to place.
139 assert_eq!(frames_of_us(1_000_000, 30.), 30);
140 assert_eq!(frames_of_us(500_000, 24.), 12);
141 assert_eq!(frames_of_us(1_000, 30.), 1);
142 assert_eq!(frames_of_us(1_000_000, 0.), 1);
143}
144
145/// Where those edges come from: every lane, not the one being dropped on --
146/// and never the clip in the hand or the half of it one track down.
147#[test]
148fn the_marks_are_every_lane_the_playhead_and_the_start() {
149 let clip = |start: u32, frames: u32, link| Clip {
150 fade_in: 0,
151 fade_out: 0,
152 transition_out: 0,
153 start,
154 in_frame: 0,
155 out_frame: frames,
156 source: 0,
157 link,
158 eq: None,
159 color: None,
160 transform: None,
161 fit: Default::default(),
162 speed: Default::default(),
163 };
164 // A grouped take across two lanes at 100..160, and a lone one on the
165 // audio lane at 400..430.
166 let video = [clip(100, 60, Some(7))];
167 let audio = [clip(100, 60, Some(7)), clip(400, 30, None)];
168 let lanes: [&[Clip]; 2] = [&video, &audio];
169
170 // Nothing in the hand: both lanes' edges, the playhead, and 0.
171 let mut all = snap_marks(&lanes, None, None, 300);
172 all.sort_unstable();
173 assert_eq!(all, [0, 100, 100, 160, 160, 300, 400, 430]);
174
175 // The video half in the hand, with its group named as the caller reads it
176 // off the pick: its own edges are gone, and so are its group's on the
177 // other lane -- both boxes travel with the drag. The lone audio clip is
178 // still a target, which is the whole point: a take being carried on V1
179 // lands flush with a sound on A1.
180 let mut carried = snap_marks(&lanes, Some((0, 0)), Some(7), 300);
181 carried.sort_unstable();
182 assert_eq!(carried, [0, 300, 400, 430]);
183
184 // An index that names no clip skips nothing and still answers.
185 assert_eq!(snap_marks(&[], Some((3, 9)), None, 0), [0, 0]);
186}
187
188/// The line the bed draws, and the switch that turns the whole thing off.
189#[test]
190fn the_snap_names_the_mark_it_landed_on_unless_it_is_switched_off() {
191 let marks = [100, 160, 300, 0];
192 // Pulled by the tail: the clip lands at 60 and the line stands on the
193 // edge its *tail* met, 100 -- not on the head it happens to have.
194 assert_eq!(snap_cue(true, 62, 40, 4, &marks), (60, Some(100)));
195 // Pulled by the head: line and landing are the same frame.
196 assert_eq!(snap_cue(true, 158, 40, 4, &marks), (160, Some(160)));
197 // A trim carries no length, so only its own edge lands.
198 assert_eq!(snap_cue(true, 298, 0, 4, &marks), (300, Some(300)));
199 // Open bed: nothing moves and nothing is drawn.
200 assert_eq!(snap_cue(true, 200, 40, 4, &marks), (200, None));
201 // Switched off, a gesture that would have snapped lands raw and draws
202 // no line -- the frame-by-frame placement the toggle is for.
203 assert_eq!(snap_cue(false, 158, 40, 4, &marks), (158, None));
204}
205
206/// The card's rate row: every component the container states and no other,
207/// so a track the header is silent about is absent from the line rather
208/// than sitting in it as a zero.
209#[test]
210fn a_rate_row_says_only_what_the_container_stated() {
211 use crate::MediaBitrate;
212 let all = MediaBitrate {
213 total: Some(8_432_000),
214 video: Some(7_918_000),
215 audio: Some(128_000),
216 };
217 assert_eq!(
218 bitrate_detail(Some(all), 1),
219 "8.4 Mb/s · 7.9 video · 0.13 sound"
220 );
221 // A Matroska states no audio rate of its own: the sound is dropped from
222 // the line, never drawn as "0".
223 assert_eq!(
224 bitrate_detail(
225 Some(MediaBitrate {
226 audio: None,
227 ..all
228 }),
229 1
230 ),
231 "8.4 Mb/s · 7.9 video"
232 );
233 // A still, or anything that would not open: the probe answered, and the
234 // answer is that nobody said.
235 assert_eq!(
236 bitrate_detail(Some(MediaBitrate::default()), 0),
237 "not stated"
238 );
239 // Asked, not answered yet -- a 12 GB film's walk takes seconds.
240 assert_eq!(bitrate_detail(None, 2), "…");
241 // A dual-audio file: the number is the track that plays, and the line
242 // says so rather than letting it stand for the AC-3 beside it.
243 assert_eq!(
244 bitrate_detail(Some(all), 2),
245 "8.4 Mb/s · 7.9 video · 0.13 1 of 2"
246 );
247 // Every component of a tiny file is stated, and small: the line changes
248 // unit, so not one of them reads as the zero it is not. In megabits
249 // this file was "0.00 Mb/s · 0.00 video · 0.00 sound".
250 assert_eq!(
251 bitrate_detail(
252 Some(MediaBitrate {
253 total: Some(4_998),
254 video: Some(113),
255 audio: Some(2_400),
256 }),
257 1
258 ),
259 "5.0 kb/s · 0.11 video · 2.4 sound"
260 );
261}
262
263/// The invariant under the row above, over every rate a container can
264/// state: a component the file *does* state never renders as a zero. The
265/// probe leaves out what is unstated, so a "0.00" on this card would be it
266/// saying a track that plays is silent.
267#[test]
268fn a_stated_rate_never_renders_as_a_zero() {
269 use crate::MediaBitrate;
270 // Every decade from 1 bit a second to a 100 Mb/s master, the rounding
271 // edges of each unit switch, and every pair of them in one line: the
272 // line's unit is picked off its smallest component, so the widest
273 // spread is the one that could round the biggest away.
274 let edges: Vec<u64> = (0..12)
275 .flat_map(|e| {
276 let decade = 10_u64.pow(e);
277 [decade, decade * 4, decade * 5, decade * 9]
278 })
279 .chain([MB_FLOOR - 1, MB_FLOOR, 999_999, 4_998, 113, 2_400])
280 .collect();
281 for &small in &edges {
282 for &big in &edges {
283 let line = bitrate_detail(
284 Some(MediaBitrate {
285 total: Some(small.max(big)),
286 video: Some(big),
287 audio: Some(small),
288 }),
289 2,
290 );
291 for number in line
292 .split(" · ")
293 .filter_map(|part| part.split(' ').next())
294 .filter_map(|n| n.parse::<f64>().ok())
295 {
296 assert!(number > 0., "{small}/{big} bits a second rendered as {line:?}");
297 }
298 }
299 }
300}
301
302#[test]
303fn timecode_counts_frames_inside_the_second() {
304 assert_eq!(timecode(0., 30.), "00:00:00:00");
305 assert_eq!(timecode(-1., 30.), "00:00:00:00"); // clamped, never negative
306 assert_eq!(timecode(4.9667, 30.), "00:00:04:29"); // last frame of a 5 s clip
307 assert_eq!(timecode(5., 30.), "00:00:05:00");
308 assert_eq!(timecode(3661.5, 30.), "01:01:01:15");
309 // Rounding must not spill into a frame the second does not have.
310 assert_eq!(timecode(1. - f64::EPSILON, 30.), "00:00:00:29");
311 assert_eq!(timecode(0.999, 29.97), "00:00:00:29");
312}
313
314#[test]
315fn scrub_seeks_only_on_a_new_frame_and_only_every_100ms() {
316 let (slow, fast) = (Duration::from_millis(100), Duration::from_millis(99));
317 // Both halves must hold: a moved pointer that has not crossed a frame
318 // boundary would reopen the decoder for the same picture.
319 assert!(scrub_due(31, 30, slow));
320 assert!(!scrub_due(30, 30, slow));
321 assert!(!scrub_due(31, 30, fast));
322 assert!(!scrub_due(30, 30, fast));
323 // Exactly at the gap counts, and a long stall never blocks.
324 assert!(scrub_due(1, 0, Duration::from_secs(9)));
325}
326
327#[test]
328fn the_picture_is_restarted_only_when_it_is_really_behind_and_not_twice() {
329 // A quantum's worth of lateness is what every playing frame has; only a
330 // gap an eye reads as out of sync answers with a reopen.
331 assert!(!should_resync(0., None));
332 assert!(!should_resync(LATE_RESYNC, None));
333 assert!(should_resync(LATE_RESYNC + 0.1, None));
334 // ...and a decoder that is still behind right after a restart -- the
335 // usual case, since it was too slow to begin with -- waits out the gap
336 // instead of reopening at every repaint.
337 assert!(!should_resync(9., Some(Instant::now())));
338 assert!(should_resync(
339 9.,
340 Some(Instant::now() - RESYNC_GAP - Duration::from_millis(1))
341 ));
342}
343
344#[test]
345fn export_lands_beside_the_source_and_never_on_it() {
346 // The whole point: the export of an .mp4 is never that .mp4.
347 assert_eq!(
348 export_path("assets/test_baseline.mp4"),
349 std::path::Path::new("assets/test_baseline.export.mp4")
350 );
351 // A second export of an export is still not its own source.
352 assert_eq!(
353 export_path("a.export.mp4"),
354 std::path::Path::new("a.export.export.mp4")
355 );
356 // Extensionless and dotted-directory names keep the directory intact.
357 assert_eq!(export_path("clip"), std::path::Path::new("clip.export.mp4"));
358 assert_eq!(
359 export_path("/v.1/clip.MP4"),
360 std::path::Path::new("/v.1/clip.export.mp4")
361 );
362}
363
364#[test]
365fn the_first_save_lands_beside_the_media() {
366 assert_eq!(
367 project_path("assets/test_av.mp4"),
368 std::path::Path::new("assets/test_av.edith")
369 );
370 // The same rule an export follows: only the last extension moves.
371 assert_eq!(
372 project_path("a.export.mp4"),
373 std::path::Path::new("a.export.edith")
374 );
375 assert_eq!(project_path("clip"), std::path::Path::new("clip.edith"));
376 // Saving a loaded project writes the file it came from, not a second.
377 assert_eq!(project_path("a.edith"), std::path::Path::new("a.edith"));
378}
379
380#[test]
381fn only_an_exact_edith_extension_is_a_project() {
382 let p = std::path::Path::new;
383 assert!(is_project(p("a.edith")));
384 // A dotted directory must not decide it -- the file name does.
385 assert!(is_project(p("/v.mp4/a.edith")));
386 assert!(!is_project(p("a.mp4")));
387 assert!(!is_project(p("/v.edith/a.mp4")));
388 // Exactly what `save_project` writes: a dropped `.EDITH` goes to the
389 // demuxer and is refused there, not parsed as a project.
390 assert!(!is_project(p("a.EDITH")));
391 // An extension, never a bare name.
392 assert!(!is_project(p("edith")));
393 assert!(!is_project(p("a.edith.mp4")));
394}
395
396/// The keys menu is the registry drawn, so a *bindable* stroke cannot go
397/// missing from it by construction. The strokes the modal cards read for
398/// themselves are the ones that could: this reads the key handler's own
399/// source and fails on any key it answers to that the menu never mentions.
400#[test]
401fn no_stroke_is_missing_from_the_keys_menu() {
402 use keymap::{ActionId, Keymap};
403 // Every file the window is written in -- the handler, and the helpers
404 // it asks, wherever they have been moved to. The tests below compare
405 // keys too, and are not shortcuts.
406 let handler = ui_source();
407 let handler = handler.as_str();
408 let keymap = Keymap::defaults();
409 let listed = |key: &str| {
410 let pretty = keymap::Chord {
411 key: key.to_string(),
412 ctrl: false,
413 }
414 .pretty();
415 keymap.lookup(key, false).is_some() || keymap::FIXED.iter().any(|f| f.chord == pretty)
416 };
417 let mut compared = 0;
418 for (at, needle) in handler.match_indices("key == ") {
419 let rest = &handler[at + needle.len()..];
420 let key = match rest.strip_prefix('"') {
421 Some(literal) => {
422 literal[..literal.find('"').expect("unterminated key")].to_string()
423 }
424 // A named constant: the escape the cards get out by.
425 None if rest.starts_with("ESCAPE") => crate::ESCAPE.to_string(),
426 None => panic!("a key compared against something this cannot read: {rest:.20}"),
427 };
428 assert!(
429 listed(&key),
430 "the handler answers to {key:?} and the keys menu never says so"
431 );
432 compared += 1;
433 }
434 // The scan is only a guard while it still finds the comparisons: a
435 // rewrite that spells them differently must come back here.
436 assert!(
437 compared >= 4,
438 "the key comparisons moved; this scan is blind"
439 );
440 // The one branch that is not a comparison -- any digit is a bitrate.
441 assert!(handler.contains("key.parse::<u32>()"));
442 assert!(keymap::FIXED.iter().any(|f| f.chord == "0–9"));
443 // And every entry of both halves lands under a heading the menu draws.
444 for action in ActionId::ALL {
445 assert!(keymap::Category::ALL.contains(&action.category()));
446 }
447 for fixed in keymap::FIXED.iter() {
448 assert!(keymap::Category::ALL.contains(&fixed.category));
449 }
450}
451
452/// The mapping the whole panel is drawn and clicked through: a moment goes
453/// to a pixel on the bed and comes back the same moment, at every zoom.
454#[test]
455fn a_moment_and_the_place_it_is_drawn_are_the_same_at_every_zoom() {
456 let duration = 20.;
457 // The fit, which is the only scale the content's own length picks: 20 s
458 // across a 200 px bed is 10 px to the second, and a 5 s clip is a
459 // quarter of the bed -- the answer the fractional view gave for it.
460 let fit = test_view(Scale::default(), duration).fit();
461 assert_eq!(fit.pps, 10.);
462 assert_eq!(fit.width_px(5.), TEST_BED * 0.25);
463 for t in [0., 5., 12.5, 20.] {
464 assert_eq!(fit.px_at(t), (t / duration) as f32 * TEST_BED, "fit at {t}");
465 }
466 for scale in [
467 fit,
468 Scale {
469 pps: 20.,
470 start: 4.,
471 },
472 Scale {
473 pps: 80.,
474 start: 12.5,
475 },
476 Scale {
477 pps: 375.,
478 start: 19.,
479 },
480 ] {
481 let scale = test_view(scale, duration).settled();
482 for x in [0., 50., 100., TEST_BED] {
483 let at = scale.time_at(x);
484 assert!(
485 (f64::from(scale.px_at(at)) - f64::from(x)).abs() < 1e-3,
486 "{scale:?} round trip at {x}"
487 );
488 }
489 // A stretch as long as what is on the bed is as wide as the bed.
490 assert!(
491 (f64::from(scale.width_px(test_view(scale, duration).span())) - f64::from(TEST_BED))
492 .abs()
493 < 1e-3,
494 "{scale:?} spans the bed"
495 );
496 }
497}
498
499/// The rule that makes a zoom usable: whatever was under the anchor is
500/// still under it afterwards -- the playhead for a key, the pointer for a
501/// ctrl+wheel.
502#[test]
503fn a_zoom_leaves_the_anchor_where_it_was_on_screen() {
504 let duration = 20.;
505 let mut scale = test_view(Scale::default(), duration).settled();
506 // The same three points along the bed the fractional view held: a half,
507 // a quarter and nine tenths of 200 px.
508 for anchor in [100f32, 50., 180.] {
509 // Well past the zoom-in stop: the anchor holds at the clamp too,
510 // which is where it used to slide (the clamp came after the offset).
511 for _ in 0..30 {
512 let at = scale.time_at(anchor);
513 let view = test_view(scale, duration);
514 let zoomed = view.zoomed(ZOOM_STEP, anchor);
515 // Only where the anchor is not pinned by an edge of the
516 // timeline: a view already against an end cannot slide further.
517 let span = test_view(zoomed, duration).span();
518 let pinned = zoomed.start <= 0. || zoomed.start + span >= duration;
519 if !pinned {
520 assert!(
521 (f64::from(zoomed.px_at(at)) - f64::from(anchor)).abs() < 1e-3,
522 "{at} moved: {scale:?} -> {zoomed:?}"
523 );
524 }
525 assert!(zoomed.pps >= scale.pps, "and it did zoom in");
526 scale = zoomed;
527 }
528 }
529}
530
531/// Both stops. In is [`ZOOM_MIN_FRAMES`] across the bed; out, on a timeline
532/// far too short to be worth widening to, is a pixel to the second -- so a
533/// short import can be zoomed out of, a long one zoomed into, and neither
534/// can scroll off an end.
535#[test]
536fn zoom_stops_at_a_bedful_of_time_and_at_a_handful_of_frames() {
537 let (duration, fps) = (20., 30.);
538 let mut scale = Scale::default();
539 for _ in 0..200 {
540 scale = test_view(scale, duration).zoomed(1. / ZOOM_STEP, 100.);
541 }
542 assert!(
543 (test_view(scale, duration).span() - f64::from(TEST_BED) / PPS_MIN).abs() < 1e-6,
544 "widest is a pixel to the second, not the 20 s that happen to be on it"
545 );
546 for _ in 0..200 {
547 scale = test_view(scale, duration).zoomed(ZOOM_STEP, 100.);
548 }
549 assert_eq!(
550 (test_view(scale, duration).span() * fps).round(),
551 ZOOM_MIN_FRAMES,
552 "tightest is a handful of frames"
553 );
554 // Against the far end, the slice still ends at the last frame.
555 let end = test_view(
556 Scale {
557 start: 1e6,
558 ..scale
559 },
560 duration,
561 );
562 let end = (end.settled(), end.span());
563 assert!((end.0.start + end.1 - duration).abs() < 1e-9);
564 assert_eq!(
565 test_view(
566 Scale {
567 start: -5.,
568 ..scale
569 },
570 duration
571 )
572 .settled()
573 .start,
574 0.
575 );
576 // The dead zone the fractional view had: a timeline of a handful of
577 // frames could not be zoomed at all, because its own length was the
578 // floor *and* the ceiling. Both keys work on it now.
579 let tiny = test_view(Scale::default(), 0.1);
580 assert!(tiny.zoomed(ZOOM_STEP, 100.).pps > PPS_DEFAULT);
581 assert!(tiny.zoomed(1. / ZOOM_STEP, 100.).pps < PPS_DEFAULT);
582 // A timeline with no length at all divides by nothing and scrolls
583 // nowhere; the fit of one keeps the scale it had.
584 let empty = test_view(Scale::default(), 0.);
585 assert_eq!(empty.settled(), Scale::default());
586 assert_eq!(empty.fit(), Scale::default());
587 assert_eq!(Scale::default().px_at(0.), 0.);
588 // A bed that was never painted clamps nothing -- there is nothing to
589 // clamp against, and a zoom must survive the frame before the probe.
590 let unpainted = View {
591 bed: 0.,
592 ..test_view(Scale { pps: 4e6, start: 3. }, duration)
593 };
594 assert_eq!(unpainted.settled().pps, 4e6);
595 assert_eq!(unpainted.following(19.), unpainted.scale);
596}
597
598/// The bug a fixed far stop was: two two-and-a-half hour clips are five
599/// hours of timeline, longer than any stop measured in hours, so the end of
600/// the second one could not be brought on screen by any zoom. The stop is
601/// the timeline's own length now, so it can.
602#[test]
603fn zooming_out_reaches_the_end_of_a_timeline_however_long_it_is() {
604 let bed = 900.;
605 let view = |scale: Scale, duration: f64| View {
606 scale,
607 bed,
608 duration,
609 fps: 30.,
610 };
611 // As far out as the keys go, from the scale a fresh project is drawn at.
612 let out = |duration: f64| {
613 let mut scale = Scale::default();
614 for _ in 0..400 {
615 scale = view(scale, duration).zoomed(1. / ZOOM_STEP, 0.);
616 }
617 scale
618 };
619 let five_hours = 2. * 2.5 * 3600.;
620 let wide = out(five_hours);
621 // The whole five hours is on the bed, the last frame drawn inside the
622 // window rather than against its edge.
623 assert_eq!(wide.start, 0.);
624 let end = wide.px_at(five_hours);
625 assert!(end < bed, "the end of the timeline is off the bed at {end} px");
626 assert!(end > bed * 0.9, "and not shrunk into a corner: {end} px");
627 assert!(
628 (view(wide, five_hours).span() - five_hours * ZOOM_OUT_MARGIN).abs() < 1e-6,
629 "the far stop is the timeline plus its margin"
630 );
631 // Zooming back in still reaches the frame stop on a timeline that long:
632 // the far stop moving does not drag the near one with it.
633 let mut scale = wide;
634 for _ in 0..400 {
635 scale = view(scale, five_hours).zoomed(ZOOM_STEP, 0.);
636 }
637 assert_eq!(
638 (view(scale, five_hours).span() * 30.).round(),
639 ZOOM_MIN_FRAMES
640 );
641 // And a ten second project is not zoomed out to four hours of empty
642 // bed: short of a pixel to the second its own length is not worth
643 // widening to, and that is 900 s of bed, not 14400.
644 assert!((view(out(10.), 10.).span() - f64::from(bed) / PPS_MIN).abs() < 1e-6);
645 // Whatever the length, the resting scale is nobody's content: the width
646 // invariant, which a far stop measured off the content would break.
647 assert_eq!(view(Scale::default(), 10.).settled(), Scale::default());
648 assert_eq!(view(Scale::default(), five_hours).settled(), Scale::default());
649 // Shrinking the timeline pulls a fully zoomed out view in with it --
650 // what was showing all of the timeline still shows all of it.
651 let shrunk = view(wide, 1800.).settled();
652 assert!((view(shrunk, 1800.).span() - 1800. * ZOOM_OUT_MARGIN).abs() < 1e-6);
653 assert!(shrunk.px_at(1800.) < bed);
654 // Growing it does not: a scale the user zoomed to is still legal, and
655 // the stop it stopped at is one press further out.
656 assert_eq!(view(wide, 2. * five_hours).settled().pps, wide.pps);
657 assert!(view(wide, 2. * five_hours).zoomed(1. / ZOOM_STEP, 0.).pps < wide.pps);
658}
659
660/// The bare wheel's own move: the view slides along the timeline, what is
661/// drawn where slides with it, the scale never changes, and both ends stop
662/// against the content rather than scrolling it off the bed.
663#[test]
664fn a_scroll_slides_the_view_without_zooming_it() {
665 let duration = 60.;
666 // Zoomed in far enough that there is somewhere to scroll to: 10 s of a
667 // 60 s timeline on the bed.
668 let start = test_view(
669 Scale {
670 pps: f64::from(TEST_BED) / 10.,
671 start: 0.,
672 },
673 duration,
674 )
675 .settled();
676 let notch = TEST_BED * SCROLL_NOTCH_SHARE;
677 let on = test_view(start, duration).scrolled(notch);
678 // A tenth of the bed later, and drawn a tenth of the bed to the left --
679 // the two halves of one move.
680 assert_eq!(on.pps, start.pps, "a scroll is not a zoom");
681 assert!(
682 (on.start - (start.start + 1.)).abs() < 1e-9,
683 "one notch is a tenth of the 10 s on the bed: {on:?}"
684 );
685 assert!(
686 (f64::from(start.px_at(20.) - on.px_at(20.)) - f64::from(notch)).abs() < 1e-3,
687 "and what was drawn moved with it: {on:?}"
688 );
689 // Back the other way, from the same place, is the same distance.
690 let back = test_view(on, duration).scrolled(-notch);
691 assert!((back.start - start.start).abs() < 1e-9, "{back:?}");
692 // Neither end can be scrolled off: the head stops at zero, and the tail
693 // stops with the last frame on the bed -- the clamp is the timeline's
694 // own length, not a number.
695 let mut scale = start;
696 for _ in 0..200 {
697 scale = test_view(scale, duration).scrolled(notch);
698 }
699 assert_eq!(scale.pps, start.pps);
700 assert!(
701 (scale.start - (duration - 10.)).abs() < 1e-6,
702 "the tail stops with the end on the bed: {scale:?}"
703 );
704 for _ in 0..200 {
705 scale = test_view(scale, duration).scrolled(-notch);
706 }
707 assert_eq!(scale.start, 0., "and the head stops at the head: {scale:?}");
708 // A view already showing all of it has nowhere to go.
709 let whole = test_view(Scale::default(), duration).fit();
710 assert_eq!(test_view(whole, duration).scrolled(notch), whole);
711}
712
713/// What makes a zoomed timeline follow the playing head: off the bed at
714/// either end pulls the view onto it, and on the bed it never moves -- a
715/// view that jumped every frame would be unreadable.
716#[test]
717fn the_view_follows_a_playhead_that_runs_off_the_bed() {
718 let duration = 20.;
719 // 5 s on a 200 px bed: 40 px to the second, starting at second 5.
720 let view = test_view(
721 Scale {
722 pps: 40.,
723 start: 5.,
724 },
725 duration,
726 );
727 let scale = view.settled();
728 assert_eq!(view.span(), 5.);
729 // Inside: untouched, whichever part of the slice it is in.
730 for at in [5., 7.5, 10.] {
731 assert_eq!(view.following(at), scale, "{at} is on the bed");
732 }
733 // Past the right edge, as playback does it: the head comes back on the
734 // bed, and the scale is not changed by the scroll.
735 let moved = view.following(12.);
736 assert_eq!(moved.pps, scale.pps);
737 assert!(moved.start > scale.start, "scrolled forward");
738 assert!(
739 moved.px_at(12.) > 0. && moved.px_at(12.) < TEST_BED,
740 "and the playhead is on screen"
741 );
742 // A seek back behind the slice does the same the other way.
743 let back = view.following(1.);
744 assert!(back.start < scale.start);
745 assert!(back.px_at(1.) >= 0.);
746 // With the whole timeline on the bed there is nothing to follow.
747 let whole = test_view(Scale::default(), duration);
748 let fit = whole.fit();
749 for at in [0., 10., 20.] {
750 assert_eq!(
751 test_view(fit, duration).following(at),
752 fit,
753 "the fit never scrolls"
754 );
755 }
756}
757
758/// The wheel during playback. A scroll away from the playing head used to
759/// be undone by the follow on the very next frame -- with a second, longer
760/// media on the timeline making it scrollable at all, one notch in five
761/// reached the screen and the wheel looked dead. The hand keeps the view
762/// now, and the follow takes it back by itself when the head runs into what
763/// the hand is looking at.
764#[test]
765fn a_scroll_during_playback_wins_until_the_head_catches_up() {
766 let duration = 80.;
767 let mut scale = test_view(Scale { pps: 40., start: 0. }, duration).settled();
768 // The render's arbitration, in the two lines it is there: a panned view
769 // is left where the hand put it, and the pan ends where the head is
770 // back on the bed.
771 let frame = |scale: Scale, panned: &mut bool, at: f64| {
772 let scale = match *panned {
773 true => test_view(scale, duration).settled(),
774 false => test_view(scale, duration).following(at),
775 };
776 if *panned && test_view(scale, duration).shows(at) {
777 *panned = false;
778 }
779 scale
780 };
781 // Playing at second one, five notches of the wheel: every one of them
782 // moves the view, and none is taken back by the head being off the bed.
783 let notch = f64::from(TEST_BED * SCROLL_NOTCH_SHARE);
784 let mut panned = false;
785 for n in 1..=5 {
786 scale = test_view(scale, duration).scrolled(TEST_BED * SCROLL_NOTCH_SHARE);
787 panned = true;
788 scale = frame(scale, &mut panned, 1.);
789 // ...and the pan is only *held* while the head is off the bed: the
790 // first notches still show it, so the follow has the view back for
791 // free and would not have moved anything anyway.
792 assert_eq!(panned, scale.start > 1., "held while the head is off");
793 assert!(
794 (scale.start - notch * f64::from(n) / 40.).abs() < 1e-6,
795 "notch {n} reached the screen: {scale:?}"
796 );
797 }
798 // The head runs into it: the pan is given back with nothing pressed...
799 let span = test_view(scale, duration).span();
800 let start = frame(scale, &mut panned, scale.start + span / 2.).start;
801 assert!(!panned, "the follow has the view back");
802 assert_eq!(start, scale.start, "and it did not jump to take it");
803 // ...and the next head that runs off the bed pulls the view again.
804 let ran_off = frame(scale, &mut panned, start + span + 1.);
805 assert!(ran_off.start > start, "the follow follows again: {ran_off:?}");
806 // A paused hand is untouched by any of this: with no follow asking,
807 // `shows` is the only thing the pan is ever released by.
808 assert!(test_view(scale, duration).shows(scale.start));
809 assert!(!test_view(scale, duration).shows(scale.start + span + 0.5));
810}
811
812/// The bug this mapping exists for: the first import used to fill the whole
813/// track whatever it was, because the bed *was* the timeline -- so a 5 s
814/// clip was 100% of the lane, zooming out did nothing, and adding a second
815/// clip silently halved the first one's box.
816#[test]
817fn a_clip_is_drawn_the_same_width_whatever_else_is_on_the_timeline() {
818 let bed = 900.;
819 let of = |duration: f64| View {
820 scale: Scale::default(),
821 bed,
822 duration,
823 fps: 30.,
824 };
825 // One 5 s import, then a second clip after it: 20 s of timeline where
826 // there were 5, and the first box has not moved or narrowed.
827 let (alone, joined) = (of(5.).settled(), of(20.).settled());
828 assert_eq!(alone, joined);
829 assert_eq!(alone.width_px(5.), joined.width_px(5.));
830 assert_eq!(joined.px_at(5.), alone.width_px(5.));
831 // And it does not fill the track: a short clip reads as short.
832 assert!(
833 alone.width_px(5.) < bed / 2.,
834 "5 s at {} px/s is {} px of a {bed} px bed",
835 alone.pps,
836 alone.width_px(5.)
837 );
838 // Zooming out visibly shrinks it, however short the timeline is -- the
839 // press that used to be a no-op.
840 let out = of(5.).zoomed(1. / ZOOM_STEP, 0.);
841 assert!(
842 out.width_px(5.) < alone.width_px(5.),
843 "{} px is not smaller than {} px",
844 out.width_px(5.),
845 alone.width_px(5.)
846 );
847 // The way back to "the whole timeline across the bed" is still one key.
848 assert_eq!(of(5.).fit().width_px(5.), bed);
849}
850
851/// What the zoom button says: how much timeline is on the bed, in a unit
852/// that tells two zooms apart.
853#[test]
854fn the_zoom_button_says_how_much_is_on_the_bed() {
855 assert_eq!(span_label(4.5), "4.5s");
856 assert_eq!(span_label(22.5), "22s");
857 assert_eq!(span_label(90.), "1.5m");
858 assert_eq!(span_label(3600.), "1.0h");
859 // The span a five hour timeline is zoomed all the way out to.
860 assert_eq!(span_label(5. * 3600. * 1.05), "5.2h");
861 // Before the first paint there is no bed and so no answer to give.
862 assert_eq!(span_label(0.), "—");
863 assert_eq!(span_label(f64::NAN), "—");
864 // A span under a second is a span: the tightest zoom is
865 // `ZOOM_MIN_FRAMES` across the bed, which on 240 fps slow-motion is
866 // 0.03s, and "0.0s" would be the pill saying nothing is on the bed.
867 for fps in [60., 120., 240., 1000.] {
868 let label = span_label(ZOOM_MIN_FRAMES / fps);
869 assert_ne!(label, "0.0s", "{fps} fps");
870 assert_ne!(label, "0.00s", "{fps} fps");
871 }
872 assert_eq!(span_label(ZOOM_MIN_FRAMES / 240.), "0.03s");
873 // A frame of quiet at 60 fps, which the silence card says out loud.
874 assert_eq!(secs_label(1. / 60.), "0.02s");
875 assert_eq!(secs_label(4.5), "4.5s");
876}