forge
Explore
Log in

Don't you dare go Hollow

edith

Clone
★ 0

0 watching0 forksRust18.3 MiB978e685Tahinli3 months ago627 commits

editing.rs2124 lines, 88.0 KiBHistoryRawDownload
1//! Importing, the library column, the menus, and every edit a clip or a
2//! track can be handed.
3
4use super::*;
5
6/// What the file manager is handed: the parts a path keeps as they are, and
7/// the ones the bus would otherwise read as something else.
8#[test]
9fn file_uri_encodes_what_a_path_carries() {
10 assert_eq!(
11 file_uri(std::path::Path::new("/a/b.mp4")),
12 "file:///a/b.mp4"
13 );
14 assert_eq!(
15 file_uri(std::path::Path::new("/home/x/out dir/my export.mp4")),
16 "file:///home/x/out%20dir/my%20export.mp4"
17 );
18 assert_eq!(
19 file_uri(std::path::Path::new("/tmp/ünlü#1?.mp4")),
20 "file:///tmp/%C3%BCnl%C3%BC%231%3F.mp4"
21 );
22}
23
24/// An import fills the *library* and nothing else -- the whole point of
25/// this door: the row is there at the file's own length before any clip
26/// plays it, wearing the tint the lanes will tint that clip with, and the
27/// timeline is exactly as long as it was. Placing it is the drag, and the
28/// drag is a separate act.
29#[test]
30fn an_import_adds_a_row_at_its_own_length_and_leaves_the_lanes_alone() {
31 let mut session = PlaybackSession::open(asset("test_av.mp4")).expect("open the fixture");
32 session.set_gain(0.0);
33 let before = session.timeline_duration();
34 let clips: Vec<usize> = session
35 .lanes()
36 .into_iter()
37 .map(|lane| session.lane_clips(lane).len())
38 .collect();
39 session.import(&asset("test_av2.mp4")).expect("av2 matches");
40 assert_eq!(session.sources().len(), 2, "the library grew");
41 assert_eq!(
42 session.timeline_duration(),
43 before,
44 "an import must not place a clip"
45 );
46 assert_eq!(
47 session
48 .lanes()
49 .into_iter()
50 .map(|lane| session.lane_clips(lane).len())
51 .collect::<Vec<_>>(),
52 clips,
53 "no lane moved"
54 );
55 // The rows the panel draws: one per source, each its file's own length
56 // -- source 1 has no clip anywhere and is still 4 s at 30 fps.
57 let rows = library_rows(
58 session.sources(),
59 &HashMap::new(),
60 &HashMap::new(),
61 None,
62 |path| session.file_frames(path),
63 );
64 assert_eq!(rows.len(), 2);
65 assert_eq!(rows[0].frames, 150, "5 s at 30 fps");
66 assert_eq!(rows[1].frames, 120, "4 s at 30 fps, never placed");
67 // The swatch is the clip colour, by the same index and the same
68 // function -- what makes the panel and the lanes one association.
69 for row in 0..rows.len() {
70 assert_eq!(rows[row].tint, row);
71 assert_eq!(source_tint(row), SOURCE_TINTS()[row % SOURCE_TINTS().len()]);
72 }
73}
74
75/// The window opened on a song and nothing else -- the launch argument, the
76/// drop on an empty window and the Import button all end in the same
77/// `PlaybackSession::open`. The library lists it placeable, the lane door
78/// the Add button and a drag share puts it on `A1`, and the one format that
79/// needs a picture says so on its own row instead of failing at the end of
80/// an export.
81#[test]
82fn a_song_opens_the_window_by_itself() {
83 let mut session =
84 PlaybackSession::open(asset("test_tone.mp3")).expect("a song is a timeline");
85 session.set_gain(0.0);
86 // The source's own path, which is the canonical one a row carries.
87 let path = session.sources()[0].path.clone();
88 assert!(session.lane_clips(Lane::V1).is_empty(), "no picture");
89 assert_eq!(session.lane_clips(Lane::A1).len(), 1);
90
91 // The library row: probed like any other source, and not greyed --
92 // `unusable` is what the panel dims a row with.
93 let streams = HashMap::from([(
94 path.clone(),
95 engine::AudioSession::probe_streams(&path).expect("probe the song"),
96 )]);
97 let rate = streams[&path]
98 .iter()
99 .find(|s| s.index == session.sources()[0].audio_stream)
100 .map(|s| (s.sample_rate, s.channels));
101 let frames = session.file_frames(&path);
102 let rows = library_rows(session.sources(), &streams, &HashMap::new(), rate, |_| {
103 frames
104 });
105 assert_eq!(rows.len(), 1);
106 assert_eq!(rows[0].name, "test_tone.mp3");
107 assert_eq!(rows[0].unusable, None, "the row is placeable");
108 assert_eq!(rows[0].frames, 90, "3 s at the audio-only 30 fps");
109
110 // ...and it places, on the audio lane, through the door `insert_source`
111 // uses: a second copy of the song at the playhead.
112 session.seek(1.0);
113 assert!(
114 session
115 .place_stream_at(1.0, &path, 0, Some(Lane::A1))
116 .expect("its own file is on this timeline")
117 );
118 assert_eq!(session.lane_clips(Lane::A1).len(), 2);
119 assert!(session.lane_clips(Lane::V1).is_empty(), "still no picture");
120
121 // Both picture rows carry the reason rather than the format's detail
122 // line, each naming itself; the audio formats are what this timeline is
123 // and are never refused.
124 assert_eq!(
125 format_refusal(&session, Format::Mp4).as_deref(),
126 Some("no picture — an mp4 would be black; export WAV, FLAC, MP3 or OGG")
127 );
128 assert_eq!(
129 format_refusal(&session, Format::Av1).as_deref(),
130 Some("no picture — an AV1 Matroska would be black; export WAV, FLAC, MP3 or OGG")
131 );
132 assert_eq!(
133 format_refusal(&session, Format::Av1Mp4).as_deref(),
134 Some("no picture — an AV1 mp4 would be black; export WAV, FLAC, MP3 or OGG")
135 );
136 assert_eq!(format_refusal(&session, Format::Wav), None);
137 assert_eq!(format_refusal(&session, Format::Flac), None);
138 assert_eq!(format_refusal(&session, Format::Mp3), None);
139}
140
141/// A file's every audio track gets a row: the ones the timeline can take
142/// are placeable, the ones it cannot are listed greyed with the reason.
143/// Which rows exist at all is the branchy part of the panel, so it is
144/// planned as data and checked here rather than through the pointer.
145#[test]
146fn every_audio_stream_of_a_file_is_a_row_usable_or_not() {
147 let multi = PathBuf::from("/m/movie.mp4");
148 let sources = [source("/m/movie.mp4", 0)];
149 let mut streams = HashMap::new();
150 streams.insert(
151 multi.clone(),
152 vec![
153 info(0, 44_100, 2, None, true),
154 info(1, 44_100, 2, Some("fra"), true),
155 info(2, 22_050, 1, Some("deu"), true),
156 info(3, 0, 0, None, false),
157 ],
158 );
159 let rows = library_rows(
160 &sources,
161 &streams,
162 &HashMap::new(),
163 Some((44_100, 2)),
164 |_| 90,
165 );
166 assert_eq!(
167 rows.iter().map(|r| r.stream).collect::<Vec<_>>(),
168 [0, 1, 2, 3],
169 "one row per audio stream, in file order"
170 );
171 assert!(
172 rows.iter().all(|r| r.path == multi && r.tint == 0),
173 "every stream of one file wears the file's own tint"
174 );
175 assert_eq!(rows[0].name, "movie.mp4 [audio 1]");
176 assert_eq!(rows[1].name, "movie.mp4 [audio 2]");
177 assert_eq!(rows[1].detail, "fra 44.1 kHz stereo");
178 // Placeable: the one already on the timeline, and the one that matches
179 // it. The mono track cannot join a stereo timeline -- one device and one
180 // copied AAC track for the whole of it -- and the codec we cannot read
181 // cannot join anything. Both say which. Its 22 kHz is *not* part of that
182 // any more: a rate of its own is resampled at the decoder's door, and a
183 // row greyed for one the engine accepts is this picker telling a lie.
184 assert_eq!((&rows[0].unusable, &rows[1].unusable), (&None, &None));
185 assert_eq!(rows[2].unusable.as_deref(), Some("the timeline is stereo"));
186 assert_eq!(
187 unusable(&info(9, 48_000, 2, None, true), Some((44_100, 2))),
188 None,
189 "48 kHz stereo joins a 44.1 kHz stereo timeline"
190 );
191 assert_eq!(rows[3].unusable.as_deref(), Some("unsupported codec"));
192 assert_eq!(
193 rows[3].detail, "",
194 "a stream we cannot parse claims nothing"
195 );
196 // Every row is the same file, so every row is that file's length.
197 assert!(rows.iter().all(|r| r.frames == 90));
198
199 // The single-stream case is exactly one row and no stream tag: no
200 // regression for the media everything else in the world is.
201 let plain = [source("/m/plain.mp4", 0)];
202 let mut one = HashMap::new();
203 one.insert(
204 PathBuf::from("/m/plain.mp4"),
205 vec![info(0, 44_100, 2, None, true)],
206 );
207 let rows = library_rows(&plain, &one, &HashMap::new(), Some((44_100, 2)), |_| 90);
208 assert_eq!(rows.len(), 1);
209 assert_eq!(rows[0].name, "plain.mp4");
210 assert_eq!(
211 rows[0].detail, "",
212 "one audio track is the row it has always been: name and length"
213 );
214 // ...as is a silent file, and a file not probed yet.
215 let mut silent = HashMap::new();
216 silent.insert(PathBuf::from("/m/plain.mp4"), Vec::new());
217 for probe in [silent, HashMap::new()] {
218 let rows = library_rows(&plain, &probe, &HashMap::new(), None, |_| 90);
219 assert_eq!(rows.len(), 1);
220 assert_eq!(rows[0].name, "plain.mp4");
221 assert_eq!(rows[0].detail, "");
222 }
223
224 // A second stream *placed* on the timeline is a source entry of its
225 // own: it keeps its row, no duplicate appears for it, and the rows of
226 // one file stay together whatever order the entries came in.
227 let placed = [
228 source("/m/movie.mp4", 0),
229 source("/m/other.mp4", 0),
230 source("/m/movie.mp4", 2),
231 ];
232 streams.insert(
233 PathBuf::from("/m/other.mp4"),
234 vec![info(0, 44_100, 2, None, true)],
235 );
236 let rows = library_rows(
237 &placed,
238 &streams,
239 &HashMap::new(),
240 Some((44_100, 2)),
241 |_| 90,
242 );
243 assert_eq!(
244 rows.iter()
245 .map(|r| (file_name(&r.path), r.stream))
246 .collect::<Vec<_>>(),
247 [
248 ("movie.mp4".to_string(), 0),
249 ("other.mp4".to_string(), 0),
250 ("movie.mp4".to_string(), 2),
251 ("movie.mp4".to_string(), 1),
252 ("movie.mp4".to_string(), 3),
253 ]
254 );
255 assert!(
256 rows[2].unusable.is_none(),
257 "a stream already on the timeline is playing, whatever a probe says"
258 );
259 assert_eq!(rows[1].tint, 1, "the other file is the other tint");
260}
261
262/// What the clip menu offers, and where it hangs. The two items that act on
263/// the playhead rather than on the clicked clip are the ones that can be
264/// inapplicable, and a menu at the edge of the window has to come back inside
265/// it or its last item cannot be clicked at all.
266///
267/// A caption's own menu, opened on a box that is not a clip: the rows that act
268/// on it -- Delete, which routes to `Player::lift_sub`, and the group pair now
269/// that a caption may be grouped by hand -- the timeline's global items beside
270/// them, and nothing else drawn at all. What a placed subtitle used to have no
271/// menu, and no stroke, to do.
272#[test]
273fn a_caption_is_deleted_by_the_same_row_and_stroke_every_box_is() {
274 use keymap::ActionId;
275 // A subtitle lane holds no `Clip`, so this is what the player reads off a
276 // right-clicked caption: nothing in the clip index space, and `caption`.
277 let cap = Ctx {
278 caption: true,
279 timeline: true,
280 ..Ctx::default()
281 };
282 assert!(cap.clip.is_none(), "a caption is never a clip");
283 assert_eq!(
284 enable(ActionId::Delete, cap),
285 Enable::Yes,
286 "the one thing a placed caption could always be asked to do",
287 );
288 // The clip rows are refusals of *kind* on a caption, so the menu leaves
289 // them out rather than drawing "click a clip first" over a clicked box.
290 for clip_only in [ActionId::Copy, ActionId::Lift, ActionId::Equalizer] {
291 assert_eq!(
292 enable(clip_only, cap),
293 Enable::Hidden("this is a caption"),
294 "{clip_only:?} is not a row on a caption",
295 );
296 }
297 // The group rows are the caption's now: a caption out of any group says so
298 // in the detach's own words, and Group over one pick is the partner hunt.
299 assert_eq!(
300 enable(ActionId::Detach, cap),
301 Enable::No("this caption is not grouped")
302 );
303 assert_eq!(enable(ActionId::Group, cap), Enable::Yes);
304 // ...and a caption in a group detaches like a clip does.
305 let grouped = Ctx {
306 caption: true,
307 caption_link: Some(3),
308 timeline: true,
309 ..Ctx::default()
310 };
311 assert_eq!(enable(ActionId::Detach, grouped), Enable::Yes);
312 // ...and the playhead's actions are *not* refusals: the split and the
313 // regroup act at the line and the cards fall back to the clip under it, so
314 // a caption in hand is no reason for them to go dead -- which, hidden, is
315 // exactly what they went: `Player::act` returns without a word on `Hidden`,
316 // and no click on the bed clears the mark that put them there.
317 for playhead in [
318 ActionId::Cut,
319 ActionId::Regroup,
320 ActionId::Color,
321 ActionId::Speed,
322 ActionId::Fit,
323 ActionId::Silence,
324 ] {
325 assert_eq!(
326 enable(playhead, cap),
327 Enable::Yes,
328 "{playhead:?} is about the playhead and not about the mark",
329 );
330 }
331 // ...but live is not *listed*: every one of those rows reaches the clip
332 // under the playhead and not the caption, and a menu opened on a caption
333 // that offered a grade would read as the caption's. The caption's menu is
334 // its own rows and the global pair the card already names as global.
335 assert_eq!(
336 menu_items(cap),
337 vec![
338 ActionId::Paste,
339 ActionId::Delete,
340 ActionId::Detach,
341 ActionId::Group,
342 ActionId::ToggleMute,
343 ],
344 "the caption's menu is its removal, its group rows, and the global ones",
345 );
346 // ...and the same reading with a clip under it is the clip menu, untouched:
347 // the flag is off wherever a `Clip` was clicked.
348 assert!(menu_items(Ctx {
349 timeline: true,
350 ..Ctx::default()
351 })
352 .contains(&ActionId::Cut));
353}
354
355/// A timeline with a subtitle palette and an empty subtitle lane: what a
356/// caption is placed on, at the fixture's own rate.
357fn with_subtitle_lane() -> (PlaybackSession, Lane) {
358 let mut session = PlaybackSession::open(asset("test_av.mp4")).expect("open the fixture");
359 session.set_gain(0.0);
360 subtitle_notice(&mut session, &asset("test_subs.mkv")).expect("the mkv carries subtitles");
361 let lane = session.add_lane(LaneKind::Subtitle);
362 (session, lane)
363}
364
365/// A second of palette track 0, to be placed wherever the test drops it.
366fn one_second(start: u32) -> SubClip {
367 SubClip {
368 start,
369 frames: 30,
370 track: 0,
371 in_us: 0,
372 out_us: 1_000_000,
373 link: None,
374 }
375}
376
377/// A drop the engine *refuses* moves nothing -- so it must move no mark either.
378/// A drag towards the left edge saturates to frame 0 (`landing`), and reading
379/// the mark back off the frame the drop asked for handed it to whatever caption
380/// already covered frame 0: the next Delete then lifted a caption the hand never
381/// touched. The mark stays on the caption that did not move.
382#[test]
383fn a_refused_caption_drop_leaves_the_mark_where_it_was() {
384 use crate::sub_mark;
385 let (mut session, lane) = with_subtitle_lane();
386 session.place_sub(lane, 0, one_second(0)).expect("A goes down");
387 session
388 .place_sub(lane, 60, one_second(0))
389 .expect("B goes down after it");
390 let before = session.sub_lane(lane).to_vec();
391 // The mark is on B, the caption in hand: index 1 of the lane.
392 assert_eq!(sub_mark(session.sub_lane(lane), 60), Some(1));
393
394 let err = session
395 .move_sub(lane, 1, lane, 0)
396 .expect_err("A already covers frame 0");
397 assert!(err.to_string().contains("already covers"), "{err}");
398 assert_eq!(session.sub_lane(lane), before, "a refusal moved nothing");
399 // The trap this pins: the frame the drop *asked* for names A, so the old
400 // re-read marked A -- the caption nobody dragged -- and `x` lifted it.
401 assert_eq!(
402 sub_mark(session.sub_lane(lane), 0),
403 Some(0),
404 "frame 0 is A's, which is why a refused drop must not re-read the mark",
405 );
406 // Nothing was renumbered, so B is still index 1 and still the marked one.
407 assert_eq!(sub_mark(session.sub_lane(lane), 60), Some(1));
408}
409
410/// A lane inserts its captions in start order (`Project::place_sub`), so one
411/// placed *before* the marked caption slides it one along: the mark left on its
412/// old index would name the caption that just arrived, and `x` would lift that
413/// one instead. The mark is kept by the caption's start frame, which is its name
414/// on its lane, so it follows the caption it was on.
415#[test]
416fn a_caption_placed_before_the_marked_one_leaves_the_mark_on_the_original() {
417 use crate::sub_mark;
418 let (mut session, lane) = with_subtitle_lane();
419 session
420 .place_sub(lane, 100, one_second(0))
421 .expect("A goes down");
422 let marked = session.sub_lane(lane)[0].start;
423 assert_eq!(sub_mark(session.sub_lane(lane), marked), Some(0));
424
425 session
426 .place_sub(lane, 0, one_second(0))
427 .expect("the new one goes down before it");
428 assert_eq!(
429 session
430 .sub_lane(lane)
431 .iter()
432 .map(|s| s.start)
433 .collect::<Vec<_>>(),
434 [0, 100],
435 "the lane holds them in start order",
436 );
437 // What the player does with the mark across that placement.
438 let mark = sub_mark(session.sub_lane(lane), marked).expect("A is still there");
439 assert_eq!(mark, 1, "A slid one along and the mark went with it");
440
441 // ...and the Delete that stroke reaches takes A, not the caption that
442 // arrived: `Player::delete_selected` routes a caption's mark to `lift_sub`.
443 assert!(session.lift_sub(lane, mark));
444 assert_eq!(
445 session
446 .sub_lane(lane)
447 .iter()
448 .map(|s| s.start)
449 .collect::<Vec<_>>(),
450 [0],
451 "the caption the user marked is the one that died",
452 );
453}
454
455/// A caption a hand grouped with a clip pairs it for the whole-take question:
456/// deleting the picture takes the group, because the group -- not the lane's
457/// kind -- is what pairs. The engine door is the one the menu's Group row
458/// reaches (`group_all`), tested here from the selection's own side of it.
459#[test]
460fn a_caption_grouped_with_a_clip_pairs_it_for_the_whole_take_question() {
461 let (mut session, lane) = with_subtitle_lane();
462 session
463 .place_sub(lane, 0, one_second(0))
464 .expect("the caption goes down");
465 session
466 .group_all(&[(Lane::V1, 0), (lane, 0)])
467 .expect("a clip and a caption are a group");
468 // Paired through the caption alone: the sound half is gone (the fixture's
469 // take was split above), and the question is the picture's to answer.
470 assert!(
471 whole_take(&session, Lane::V1, 0),
472 "the caption the clip was grouped with pairs it"
473 );
474 // ...and the detach takes the pairing off again.
475 assert!(session.ungroup(lane, 0));
476 assert!(
477 !whole_take(&session, Lane::V1, 0),
478 "ungrouped, the clip is a half like any other"
479 );
480}
481
482/// An ungrouped clip on a further lane (ord >= 1) is a layer, not a take: it
483/// sits over the timeline rather than in it, so deleting it lifts a hole
484/// rather than rippling one closed. Pins the `(_, 1..) => false` arm
485/// `whole_take` answers before it ever reaches the video/audio arms below.
486#[test]
487fn an_ungrouped_clip_on_a_further_lane_is_a_layer_not_a_take() {
488 use engine::project::LaneKind;
489
490 let mut session = PlaybackSession::open(asset("test_av.mp4")).expect("open the fixture");
491 session.set_gain(0.0);
492 let v2 = session.add_lane(LaneKind::Video);
493 assert!(session.move_clip_to(Lane::V1, 0, v2, 0), "V1 -> V2");
494 // Its old pairing with A1 came along with the move -- detach it, an
495 // ungrouped clip is exactly the case this arm is for.
496 assert!(session.ungroup(v2, 0));
497 assert!(
498 !whole_take(&session, v2, 0),
499 "an ungrouped clip on ord >= 1 is a layer, not a take"
500 );
501 // ...and its delete lifts, leaving the hole under it open: the take
502 // beneath (there is none here, but the lane) is untouched.
503 assert!(session.lift_clip(v2, 0));
504 assert!(session.lane_clips(v2).is_empty());
505}
506
507/// A caption anchor names the media half of its group: the video member for
508/// the cards that are about the picture, the audio member for the ones about
509/// the sound. A caption in no group names nothing -- which is what the cards'
510/// new refusals say out loud.
511#[test]
512fn a_caption_anchor_names_the_media_half_of_its_group() {
513 use crate::caption_media_half;
514
515 let (mut session, lane) = with_subtitle_lane();
516 session
517 .place_sub(lane, 0, one_second(0))
518 .expect("the caption goes down");
519 // Ungrouped: no pictures and no sound behind the words.
520 assert_eq!(caption_media_half(&session, (lane, 0), LaneKind::Video), None);
521 assert_eq!(caption_media_half(&session, (lane, 0), LaneKind::Audio), None);
522 // Grouped with the take -- whose sound rides along unasked -- both halves
523 // answer by lane and index.
524 session
525 .group_all(&[(Lane::V1, 0), (lane, 0)])
526 .expect("a clip and a caption are a group");
527 assert_eq!(
528 caption_media_half(&session, (lane, 0), LaneKind::Video),
529 Some((Lane::V1, 0))
530 );
531 assert_eq!(
532 caption_media_half(&session, (lane, 0), LaneKind::Audio),
533 Some((Lane::A1, 0))
534 );
535}
536
537/// The manual group's oracle question: a selection of placements, one per lane.
538/// Two picks on two lanes is a group waiting to happen; a lane picked twice is
539/// the one thing a group may never hold; a single pick is the partner hunt.
540#[test]
541fn group_is_live_over_a_multi_selection_and_words_a_repeated_lane() {
542 use keymap::ActionId;
543 let distinct = Ctx {
544 timeline: true,
545 picks: 2,
546 pick_lanes: 2,
547 ..Ctx::default()
548 };
549 assert_eq!(enable(ActionId::Group, distinct), Enable::Yes);
550 let twice = Ctx {
551 timeline: true,
552 picks: 2,
553 pick_lanes: 1,
554 ..Ctx::default()
555 };
556 assert_eq!(
557 enable(ActionId::Group, twice),
558 Enable::No("a group is one clip per lane: keep one pick per track")
559 );
560 // One pick and none at all: the partner hunt and its own refusal, which
561 // the player words rather than the oracle.
562 let one = Ctx {
563 timeline: true,
564 picks: 1,
565 pick_lanes: 1,
566 ..Ctx::default()
567 };
568 assert_eq!(enable(ActionId::Group, one), Enable::Yes);
569 // Nothing picked at all, on an open timeline: the partner hunt's own
570 // refusal, worded by the player rather than the oracle.
571 let none = Ctx {
572 timeline: true,
573 ..Ctx::default()
574 };
575 assert_eq!(enable(ActionId::Group, none), Enable::Yes);
576 // Stale picks (an edit moved the indices) count as none.
577 let stale = Ctx {
578 timeline: true,
579 picks: 0,
580 pick_lanes: 0,
581 ..Ctx::default()
582 };
583 assert_eq!(enable(ActionId::Group, stale), Enable::Yes);
584}
585
586/// The playhead's actions with a caption marked: live, and doing what they say.
587/// Hidden, they were dead *and* silent -- `Player::act` says nothing on a class
588/// refusal -- from the first click on a caption until something else was
589/// clicked, and no click on the bed clears that mark.
590#[test]
591fn the_playhead_actions_still_work_with_a_caption_marked() {
592 use keymap::ActionId;
593 let cap = Ctx {
594 caption: true,
595 timeline: true,
596 ..Ctx::default()
597 };
598 assert_eq!(enable(ActionId::Cut, cap), Enable::Yes);
599 assert_eq!(enable(ActionId::Regroup, cap), Enable::Yes);
600
601 // And the edits themselves, on a timeline holding a placed caption: the
602 // split at the playhead and the rejoin at the seam it leaves.
603 let (mut session, lane) = with_subtitle_lane();
604 session.place_sub(lane, 0, one_second(0)).expect("it goes down");
605 let clips = session.lane_clips(Lane::V1).len();
606 assert!(session.cut_at(2.0), "the playhead splits the clip under it");
607 assert_eq!(session.lane_clips(Lane::V1).len(), clips + 1);
608 assert!(session.regroup_at(2.0), "and the seam rejoins");
609 assert_eq!(session.lane_clips(Lane::V1).len(), clips);
610 assert_eq!(session.sub_lane(lane).len(), 1, "the caption is untouched");
611}
612
613#[test]
614fn the_clip_menu_dims_what_the_playhead_is_not_on_and_stays_in_the_window() {
615 use keymap::{ActionId, Keymap};
616 // Frames 30..90 of the timeline, taken from the head of its source.
617 let clip = Clip {
618 fade_in: 0,
619 fade_out: 0,
620 transition_out: 0,
621 start: 30,
622 in_frame: 0,
623 out_frame: 60,
624 source: 0,
625 link: None,
626 eq: None,
627 color: None,
628 transform: None,
629 fit: FitPolicy::default(),
630 speed: Speed::NORMAL,
631 };
632 assert_eq!(clip.end(), 90);
633 let a1 = Lane::A1;
634 let v1 = Lane::V1;
635 // The question the clip menu asks: a timeline is open and the menu was
636 // opened on this clip, at this playhead.
637 let on = |clip: &Clip, lane, action, playhead| {
638 enable(
639 action,
640 Ctx {
641 clip: Some((*clip, lane)),
642 playhead,
643 timeline: true,
644 ..Ctx::default()
645 },
646 )
647 };
648 let offered = |clip: &Clip, lane, action, playhead| on(clip, lane, action, playhead).yes();
649 // Cut splits from inside only: neither edge has anything to split off.
650 assert!(offered(&clip, v1, ActionId::Cut, 31));
651 assert!(offered(&clip, v1, ActionId::Cut, 89));
652 assert!(!offered(&clip, v1, ActionId::Cut, 30));
653 assert!(!offered(&clip, v1, ActionId::Cut, 90));
654 assert!(!offered(&clip, v1, ActionId::Cut, 200));
655 // A slowed clip refuses more than the edges do, and it may not say the
656 // edges' words while doing it: at a quarter speed each of its sixty
657 // source frames is on screen four timeline frames, and only the first of
658 // the four is a frame the file has. The three between are inside the
659 // clip by any reading of the playhead, so "only from inside a clip"
660 // there is a refusal claiming something the screen contradicts.
661 let slow = Clip {
662 fade_in: 0,
663 fade_out: 0,
664 speed: Speed::MIN,
665 ..clip
666 };
667 assert_eq!(slow.frames(), 240, "a quarter speed is four times as long");
668 assert!(offered(&slow, v1, ActionId::Cut, 34), "34 is a source frame");
669 assert!(!offered(&slow, v1, ActionId::Cut, 35));
670 assert_eq!(
671 on(&slow, v1, ActionId::Cut, 35).why(),
672 Some("this speed holds one frame here — step to the next"),
673 );
674 assert_eq!(
675 on(&slow, v1, ActionId::Cut, 30).why(),
676 Some("only from inside a clip"),
677 "the head is still the edge's refusal, speed or no speed",
678 );
679 // Regroup is the other way round: only where this clip meets another.
680 assert!(offered(&clip, v1, ActionId::Regroup, 30));
681 assert!(offered(&clip, v1, ActionId::Regroup, 90));
682 assert!(!offered(&clip, v1, ActionId::Regroup, 60));
683 // Detach is the clip's own business: nothing to take apart in one that
684 // names no group, and whether the group still has another half is the
685 // engine's question. Group is offered on every clip, for that reason.
686 assert!(!offered(&clip, v1, ActionId::Detach, 0));
687 let grouped = Clip {
688 fade_in: 0,
689 fade_out: 0,
690 link: Some(3),
691 ..clip
692 };
693 assert!(offered(&grouped, v1, ActionId::Detach, 60));
694 assert!(offered(&clip, a1, ActionId::Group, 0));
695 // The equalizer is the one item the *lane* decides: it filters samples,
696 // and a video clip has none of its own. Never the playhead's business.
697 assert!(offered(&clip, a1, ActionId::Equalizer, 0));
698 assert!(offered(&clip, a1, ActionId::Equalizer, 60));
699 assert!(!offered(&clip, v1, ActionId::Equalizer, 60));
700 // The rest act on the clip that was clicked, so they always mean
701 // something -- the engine words its own refusals.
702 for action in [ActionId::Delete, ActionId::Lift, ActionId::ToggleMute] {
703 assert!(offered(&clip, v1, action, 0));
704 assert!(offered(&clip, a1, action, 60));
705 }
706 // Except the grade, which is a picture setting: offered on a video
707 // clip wherever the playhead is, dimmed on a waveform.
708 assert!(offered(&clip, v1, ActionId::Color, 0));
709 assert!(!offered(&clip, a1, ActionId::Color, 60));
710 // The two kinds of no, which is what the row is told apart by: a grade
711 // on a waveform is a *class* answer -- an audio clip has no picture and
712 // never will, so the menu leaves the item out entirely -- where a cut at
713 // the clip's edge is this moment's answer and the next click of the
714 // playhead changes it, so that one is drawn, dimmed, and says why.
715 //
716 // What the menu draws for a clip of each kind, in order: the render
717 // loop's `continue`, as a list.
718 let menu = |lane, image, playhead| {
719 MENU_ITEMS
720 .into_iter()
721 .filter(|&action| {
722 enable(
723 action,
724 Ctx {
725 clip: Some((clip, lane)),
726 image,
727 playhead,
728 timeline: true,
729 ..Ctx::default()
730 },
731 )
732 .listed()
733 })
734 .collect::<Vec<_>>()
735 };
736 // Sound: no grade and no fit policy, and the equalizer that is the
737 // whole reason an audio clip has a menu of its own.
738 let sound = menu(a1, false, 60);
739 assert!(!sound.contains(&ActionId::Color), "{sound:?}");
740 assert!(!sound.contains(&ActionId::Fit), "{sound:?}");
741 assert!(sound.contains(&ActionId::Equalizer));
742 assert!(sound.contains(&ActionId::Silence));
743 // Picture: the mirror of it. The sound of a take is the audio lane's,
744 // clip for clip, so the equalizer is not this clip's business -- but the
745 // silence scan is, because it opens on the half it is grouped with.
746 let picture = menu(v1, false, 60);
747 assert!(!picture.contains(&ActionId::Equalizer), "{picture:?}");
748 assert!(picture.contains(&ActionId::Color));
749 assert!(picture.contains(&ActionId::Fit));
750 assert!(picture.contains(&ActionId::Silence));
751 // A still: picture with no sound anywhere, ever. Graded, fitted and
752 // re-timed like any other clip (a speed reaches a still through the same
753 // rewrite), scanned like none.
754 let still = menu(v1, true, 60);
755 assert!(!still.contains(&ActionId::Silence), "{still:?}");
756 assert!(!still.contains(&ActionId::Equalizer), "{still:?}");
757 assert!(still.contains(&ActionId::Color));
758 assert!(still.contains(&ActionId::Fit));
759 assert!(still.contains(&ActionId::Speed));
760 // ...and the state refusals are on all three, dimmed rather than gone:
761 // at 30 the playhead is on this clip's head, where a cut has nothing to
762 // split off -- a row the next click of the playhead lights.
763 for rows in [menu(a1, false, 30), menu(v1, false, 30), menu(v1, true, 30)] {
764 assert!(rows.contains(&ActionId::Cut), "{rows:?}");
765 assert!(rows.contains(&ActionId::Detach), "{rows:?}");
766 }
767 // The actions card is the other half of the rule: it lists the whole
768 // registry, so a class refusal is dimmed there with its reason and never
769 // dropped -- an action missing from the one surface that lists
770 // everything would read as an action that does not exist.
771 let listed: Vec<ActionId> = keys_rows()
772 .into_iter()
773 .filter_map(|r| match r {
774 KeyRow::Act(action) => Some(action),
775 _ => None,
776 })
777 .collect();
778 for (lane, action) in [
779 (a1, ActionId::Color),
780 (a1, ActionId::Fit),
781 (v1, ActionId::Equalizer),
782 ] {
783 assert!(matches!(on(&clip, lane, action, 60), Enable::Hidden(_)));
784 assert!(listed.contains(&action), "{action:?} left the actions card");
785 assert!(on(&clip, lane, action, 60).why().is_some());
786 }
787 assert!(matches!(on(&clip, v1, ActionId::Cut, 30), Enable::No(_)));
788 assert!(matches!(on(&clip, v1, ActionId::Regroup, 60), Enable::No(_)));
789 assert!(matches!(on(&clip, v1, ActionId::Detach, 0), Enable::No(_)));
790 // Every refusal says something, and says it short enough to sit in the
791 // menu's right-hand column beside a label -- the still's included, which
792 // the card dims with while the menu leaves the row out.
793 for action in MENU_ITEMS {
794 for (lane, image) in [(v1, false), (a1, false), (v1, true)] {
795 for playhead in [0, 30, 60, 90] {
796 let refusal = enable(
797 action,
798 Ctx {
799 clip: Some((clip, lane)),
800 image,
801 playhead,
802 timeline: true,
803 ..Ctx::default()
804 },
805 );
806 if let Some(why) = refusal.why() {
807 assert!(!why.is_empty() && why.len() <= 30, "{action:?}: {why:?}");
808 }
809 }
810 }
811 }
812 assert!(matches!(
813 enable(
814 ActionId::Silence,
815 Ctx {
816 clip: Some((clip, v1)),
817 image: true,
818 playhead: 60,
819 timeline: true,
820 ..Ctx::default()
821 }
822 ),
823 Enable::Hidden(_)
824 ));
825 // The editor as a whole, which is how the actions card asks: with no
826 // timeline nothing is offered, an export leaves only its own cancel,
827 // and the three that act on the marked clip say so when none is.
828 let whole = |action, ctx| enable(action, ctx);
829 assert_eq!(
830 whole(ActionId::Play, Ctx::default()),
831 Enable::No("no timeline open")
832 );
833 let live = Ctx {
834 timeline: true,
835 playable: true,
836 ..Ctx::default()
837 };
838 assert!(whole(ActionId::Play, live).yes());
839 // ...and an open timeline with nothing on its lanes refuses the
840 // transport in the oracle rather than in the button, which is what
841 // makes the key and the button say the same thing.
842 assert_eq!(
843 whole(
844 ActionId::Play,
845 Ctx {
846 playable: false,
847 ..live
848 }
849 ),
850 Enable::No("put a clip on a lane first")
851 );
852 // The magnet and the monitoring level are the editor's own and answer
853 // with no timeline at all -- the keyboard always fired them there, and
854 // now so does the toolbar.
855 //
856 // ...and so do the two stand-in switches, which is not a nicety: they are
857 // *import* options -- what an arriving film gets made for it, and what its
858 // picture is then decoded from -- so a person answers them before the first
859 // file, and a switch that waited for one was a switch that came too late to
860 // stop the encode it was about.
861 // ...and so do the project settings: they decide what the next file
862 // scaffolds as (`pending_settings`), so a gate that waited for a timeline
863 // refused them exactly when they mean the most.
864 for editor_wide in [
865 ActionId::ToggleSnap,
866 ActionId::ToggleMute,
867 ActionId::VolumeUp,
868 ActionId::VolumeDown,
869 ActionId::ToggleProxies,
870 ActionId::ToggleAutoProxies,
871 ActionId::Resolution,
872 ] {
873 assert!(
874 whole(editor_wide, Ctx::default()).yes(),
875 "{editor_wide:?} is dead with no file open while its key still fires"
876 );
877 }
878 assert!(!whole(ActionId::Delete, live).yes());
879 assert!(!whole(ActionId::Paste, live).yes());
880 assert!(
881 whole(
882 ActionId::Paste,
883 Ctx {
884 clipboard: true,
885 ..live
886 }
887 )
888 .yes()
889 );
890 assert!(!whole(ActionId::CancelExport, live).yes());
891 let busy = Ctx {
892 exporting: true,
893 ..live
894 };
895 assert!(whole(ActionId::CancelExport, busy).yes());
896 for action in ActionId::ALL {
897 // Nothing may touch the edit list while an export is reading it --
898 // and the palette does not: it repaints this window and writes one
899 // word to the user's own config file, which is why it is one of the
900 // actions besides the cancel that stay live here. Fullscreen is the
901 // same kind of thing: the platform's own window toggle, touching no
902 // edit list either.
903 let allowed = action == ActionId::CancelExport
904 || action == ActionId::Theme
905 || action == ActionId::Fullscreen
906 || action == ActionId::SubtitleStyle;
907 assert_eq!(
908 whole(action, busy).yes(),
909 allowed,
910 "{action:?} while an export reads the edit list"
911 );
912 }
913 // The playhead frame is the engine's own rule, boundary included.
914 assert_eq!(frame_at(1.0, 30.), 30);
915 assert_eq!(frame_at(0.0, 30.), 0);
916 assert_eq!(frame_at(-1.0, 30.), 0);
917 // Where it hangs: at the pointer when it fits, back inside when not.
918 let viewport = size(px(800.), px(400.));
919 assert_eq!(menu_at(point(px(10.), px(10.)), viewport, 150.), (10., 10.));
920 assert_eq!(
921 menu_at(point(px(700.), px(380.)), viewport, 150.),
922 (800. - MENU_W, 250.)
923 );
924 // A window smaller than the menu loses its bottom, never its top.
925 assert_eq!(
926 menu_at(point(px(90.), px(40.)), size(px(100.), px(50.)), 150.),
927 (0., 0.)
928 );
929 // Every item is an action the registry knows, so the menu and the keys
930 // menu say the same thing about it -- and none of them is unreachable
931 // by keyboard, which is what makes the hint column worth drawing.
932 let keymap = Keymap::defaults();
933 for action in MENU_ITEMS {
934 assert!(ActionId::ALL.contains(&action), "{action:?} is not listed");
935 assert_ne!(keymap.display(action), "unbound", "{action:?}");
936 }
937 // ...and the whole card still fits the 640x360 floor, however many items
938 // it grows to: the list is what scrolls where the window is too short
939 // for it, never the card that grows.
940 let items = MENU_ITEMS.len() + 1; // Properties
941 let floor = size(px(640.), px(360.));
942 assert!(MENU_PAD * 2. + menu_rows_h(items, floor) <= 360., "too tall");
943 assert!(
944 menu_rows_h(items, floor) / MENU_ROW_H >= 12.,
945 "too few items visible to scan on the smallest window"
946 );
947 assert_eq!(
948 menu_at(point(px(0.), px(0.)), floor, {
949 MENU_PAD * 2. + menu_rows_h(items, floor)
950 }),
951 (0., 0.)
952 );
953 // The picker is the same card and the longest list in it is the palette
954 // family list, which grows every time somebody asks for more colours.
955 // Twelve of them still stand on screen *whole* at the floor -- so the
956 // list's `overflow_y_scroll` is a safety net rather than something a
957 // person has to find. The day a family makes this fail is the day the
958 // card owes the affordance line the inspector and the cards carry
959 // ("more below — scroll the …"): a row nobody knows is under the fold
960 // is a row that is not there, and no scroll bar says it.
961 let families = crate::ui::theme::PaletteId::ALL.len();
962 assert_eq!(
963 menu_rows_h(families, floor),
964 families as f32 * MENU_ROW_H,
965 "{families} palettes no longer fit 640x360 -- the picker now needs \
966 the 'more below' line and a keyboard cursor that scrolls into view"
967 );
968 // ...and the detail beside each one is short enough to survive the
969 // right-hand column at that width instead of being truncated mid-word.
970 for id in crate::ui::theme::PaletteId::ALL {
971 assert!(id.detail().len() <= 30, "{id:?}: {:?}", id.detail());
972 }
973}
974
975/// Both menus, at the two things they can be opened on, and the box each
976/// one is drawn in. Two rules, and the render obeys them by *calling* what
977/// this calls -- [`menu_items`] and [`row_items`] are the only lists either
978/// menu is built from, and [`menu_rows_h`] is the height each is both placed
979/// by and drawn to:
980///
981/// 1. a row exists only where the oracle lists the action for the very thing
982/// that was right-clicked, so an item can never offer a video action on a
983/// waveform (the complaint this comes from) and a new action added to
984/// `MENU_ITEMS` cannot appear where it does not apply;
985/// 2. the whole card is inside the window, wherever it was opened and
986/// however long the list -- a menu drawn past the bottom edge is a menu
987/// whose last items nobody can click.
988#[test]
989fn a_menu_offers_only_what_applies_and_is_drawn_inside_the_window() {
990 use keymap::ActionId;
991 let clip = Clip {
992 fade_in: 0,
993 fade_out: 0,
994 transition_out: 0,
995 start: 30,
996 in_frame: 0,
997 out_frame: 60,
998 source: 0,
999 link: Some(1),
1000 eq: None,
1001 color: None,
1002 transform: None,
1003 fit: FitPolicy::default(),
1004 speed: Speed::NORMAL,
1005 };
1006 let ctx = |lane, image| Ctx {
1007 clip: Some((clip, lane)),
1008 image,
1009 playhead: 60,
1010 timeline: true,
1011 ..Ctx::default()
1012 };
1013 // The oracle is the whole of what the menu draws: every item it lists is
1014 // one the oracle would list, and every item it leaves out is one the
1015 // oracle hides -- there is no third answer, and no hand-written list.
1016 for (lane, image) in [(Lane::V1, false), (Lane::A1, false), (Lane::V1, true)] {
1017 let ctx = ctx(lane, image);
1018 let rows = menu_items(ctx);
1019 for action in MENU_ITEMS {
1020 assert_eq!(
1021 rows.contains(&action),
1022 enable(action, ctx).listed(),
1023 "{action:?} on {lane:?}, image={image}"
1024 );
1025 }
1026 }
1027 // Sound has no picture settings; picture has no equalizer of its own; a
1028 // still has no sound to scan. The user's own words: an audio clip must
1029 // not be offered what only a picture can be given.
1030 let sound = menu_items(ctx(Lane::A1, false));
1031 assert!(!sound.contains(&ActionId::Color), "{sound:?}");
1032 assert!(!sound.contains(&ActionId::Fit), "{sound:?}");
1033 assert!(sound.contains(&ActionId::Equalizer));
1034 let picture = menu_items(ctx(Lane::V1, false));
1035 assert!(!picture.contains(&ActionId::Equalizer), "{picture:?}");
1036 assert!(picture.contains(&ActionId::Color));
1037 assert!(!menu_items(ctx(Lane::V1, true)).contains(&ActionId::Silence));
1038 // The library menu is the same rule on the other panel: its items come
1039 // off `row_items` and nothing else, and the two that change the timeline
1040 // say why rather than being clicked and refused afterwards.
1041 let live = RowCtx {
1042 timeline: true,
1043 usable: true,
1044 ..RowCtx::default()
1045 };
1046 for ctx in [
1047 live,
1048 RowCtx {
1049 usable: false,
1050 ..live
1051 },
1052 RowCtx { placed: 2, ..live },
1053 RowCtx {
1054 exporting: true,
1055 ..live
1056 },
1057 RowCtx::default(),
1058 ] {
1059 let rows = row_items(ctx);
1060 for item in ROW_ITEMS {
1061 assert_eq!(rows.contains(&item), row_enable(item, ctx).listed());
1062 }
1063 // Whatever the state, the two that need neither timeline nor edit
1064 // list are offered: a file is always describable and findable.
1065 assert!(row_enable(RowItem::Reveal, ctx).yes());
1066 assert!(row_enable(RowItem::Properties, ctx).yes());
1067 }
1068 assert!(row_enable(RowItem::Add, live).yes());
1069 assert!(
1070 !row_enable(
1071 RowItem::Add,
1072 RowCtx {
1073 usable: false,
1074 ..live
1075 }
1076 )
1077 .yes(),
1078 "a file that cannot join this timeline is not an Add anybody can ask for"
1079 );
1080 assert!(!row_enable(RowItem::Remove, RowCtx { placed: 1, ..live }).yes());
1081 assert!(!row_enable(RowItem::Add, RowCtx::default()).yes());
1082 // Every refusal is short enough to sit in the hint column beside its
1083 // label, the clip menu's rule.
1084 for item in ROW_ITEMS {
1085 for ctx in [live, RowCtx::default()] {
1086 if let Some(why) = row_enable(item, ctx).why() {
1087 assert!(!why.is_empty() && why.len() <= 30, "{why:?}");
1088 }
1089 }
1090 }
1091 // ...and the box. Every window from the floor up, every corner of it,
1092 // and every list length either menu can have: the card is placed by
1093 // `MENU_PAD * 2 + menu_rows_h` and drawn to it, so this is the card.
1094 for viewport in [
1095 size(px(640.), px(360.)),
1096 size(px(800.), px(600.)),
1097 size(px(1280.), px(690.)),
1098 // Smaller than the floor the layout is sized for: it still may not
1099 // draw outside the window it has.
1100 size(px(320.), px(200.)),
1101 ] {
1102 for rows in 1..=MENU_ITEMS.len() + 1 {
1103 let h = MENU_PAD * 2. + menu_rows_h(rows, viewport);
1104 for at in [
1105 point(px(0.), px(0.)),
1106 point(px(10.), px(10.)),
1107 // The click that started all this: low in the window, where
1108 // the menu used to hang off the bottom edge.
1109 point(px(0.), viewport.height - px(4.)),
1110 point(viewport.width - px(4.), viewport.height - px(4.)),
1111 point(viewport.width * 2., viewport.height * 2.),
1112 ] {
1113 let (x, y) = menu_at(at, viewport, h);
1114 assert!(x >= 0. && y >= 0., "{x},{y} outside {viewport:?}");
1115 assert!(
1116 x + MENU_W <= f32::from(viewport.width) + 0.01,
1117 "{rows} rows at {at:?} hang off the right of {viewport:?}"
1118 );
1119 assert!(
1120 y + h <= f32::from(viewport.height) + 0.01,
1121 "{rows} rows at {at:?} hang off the bottom of {viewport:?}"
1122 );
1123 }
1124 }
1125 }
1126 // On any window with the room, the whole list is drawn rather than
1127 // twelve rows of it and a scroll nobody is told about.
1128 let items = MENU_ITEMS.len() + 1;
1129 let real = size(px(1280.), px(690.));
1130 assert_eq!(menu_rows_h(items, real), items as f32 * MENU_ROW_H);
1131 assert!(menu_rows_h(items, size(px(640.), px(360.))) < items as f32 * MENU_ROW_H);
1132}
1133
1134/// The other half of the keys menu's guarantee, and the audit this batch was
1135/// asked for kept as a test: no action may be a stroke and nothing else.
1136/// The actions card answers it for all of them at once -- its rows come off
1137/// [`ActionId::ALL`], so a fortieth action is on it the moment it exists and
1138/// there is no hand-written list here to fall behind.
1139#[test]
1140fn every_action_is_reachable_without_the_keyboard() {
1141 use keymap::ActionId;
1142 let source = ui_source();
1143 let source = source.as_str();
1144 let element = |id: &str| source.contains(&format!("\"{id}\""));
1145 let listed: Vec<ActionId> = keys_rows()
1146 .iter()
1147 .filter_map(|r| match r {
1148 KeyRow::Act(a) => Some(*a),
1149 _ => None,
1150 })
1151 .collect();
1152 for action in ActionId::ALL {
1153 assert_eq!(
1154 listed.iter().filter(|a| **a == action).count(),
1155 1,
1156 "{action:?} is reachable by keyboard only"
1157 );
1158 }
1159 // The snap has a door of its own as well as its row on the card: the
1160 // button beside the zoom, which says which way it is set as well as
1161 // setting it.
1162 assert!(element("snap"), "no snap button beside the zoom");
1163 // And the card is a door the pointer can open: the panel's own button.
1164 assert!(element("keys"), "no way to open the actions card");
1165 // The card-local strokes have the same rule, and each of them is a thing
1166 // on its card: the graph and its two buttons, the colour bars and their
1167 // reset, the speed bar and its presets, and the silence card's rows --
1168 // whose steppers are the pointer's only way to a threshold.
1169 for id in [
1170 "eq-graph",
1171 "eq-reset",
1172 "eq-spectrum",
1173 "color-bar",
1174 "color-reset",
1175 "speed-bar",
1176 "speed-preset",
1177 "silence-row",
1178 "silence-step",
1179 "mix-row",
1180 "mix-step",
1181 "silence-apply",
1182 "export-confirm",
1183 ] {
1184 assert!(element(id), "{id} is not on any card");
1185 }
1186 // ...and each one is named by the row that carries its stroke, so a
1187 // card-local key added to `FIXED` with nothing to click fails here
1188 // instead of being noticed by whoever tries to use the card without a
1189 // keyboard. `KeyRow::Fixed` is "shown and never offered", which used to
1190 // mean the twenty-eight rows below were the one part of this editor no
1191 // reachability test read.
1192 for fixed in keymap::FIXED.iter() {
1193 match fixed.reach {
1194 keymap::Reach::Click(id) => assert!(
1195 element(id),
1196 "{:?} ({}) points at {id}, which is on no card",
1197 fixed.chord,
1198 fixed.label
1199 ),
1200 // Nothing to click by decision, not by omission: getting out of
1201 // a card, and the hold that repeats what a drag already does.
1202 keymap::Reach::Gesture => {}
1203 }
1204 }
1205}
1206
1207/// The other half of "getting out of a card is a `Reach::Gesture`": the
1208/// gesture has to exist. Every card's scrim closes it on a press, so a hand
1209/// that never touches the keyboard can shut every one of them -- for seven
1210/// cards `esc` was the only exit, which is the same complaint as an action
1211/// reachable by stroke alone, said about the way out instead of the way in.
1212///
1213/// Read off [`Player::modal`] rather than off a list written here: a card
1214/// counted there and not closed by [`Player::close_card`] fails this test
1215/// the day it is added, which is the only way the two stay in step.
1216#[test]
1217fn every_card_closes_without_the_keyboard() {
1218 for card in [
1219 "keys_overlay",
1220 "export_card",
1221 "eq_card",
1222 "color_card",
1223 "speed_card",
1224 "mix_card",
1225 "silence_card",
1226 "subtitle_style_card",
1227 ] {
1228 let src = fn_body(card);
1229 assert!(
1230 src.contains("this.close_card()"),
1231 "{card}'s scrim swallows the press without closing the card: \
1232 escape is the only way out of it"
1233 );
1234 assert!(
1235 src.contains("MouseButton::Left, swallow"),
1236 "{card}'s body does not swallow its own presses, so a press on \
1237 one of its controls would close the card under itself"
1238 );
1239 }
1240 // ...and every state that makes the window modal is a state that press
1241 // clears. `exporting()` is not one of them: a running export is a job
1242 // with a cancel button, not a card with a scrim.
1243 let close = fn_body("close_card");
1244 for field in fn_body("modal").split("self.").skip(1).filter_map(|rest| {
1245 let name: String = rest
1246 .chars()
1247 .take_while(|c| c.is_alphanumeric() || *c == '_')
1248 .collect();
1249 (!rest[name.len()..].starts_with('(')).then_some(name)
1250 }) {
1251 assert!(
1252 close.contains(&format!("self.{field}")),
1253 "{field} makes the window modal and `close_card` leaves it set: \
1254 that card cannot be closed by pointer"
1255 );
1256 }
1257}
1258
1259/// A clip too short to hit is still a clip: at a far zoom-out its box is
1260/// drawn [`HIT_MIN`] wide rather than the fraction of a pixel it is worth,
1261/// because a box nobody can put a pointer on cannot be selected, dragged,
1262/// trimmed or given a menu -- and an invisible unselectable take is strictly
1263/// worse than one drawn a few pixels too wide.
1264#[test]
1265fn a_clip_box_is_never_narrower_than_its_target() {
1266 // Two seconds on a bed showing an hour: a fifth of a pixel.
1267 let far = Scale {
1268 pps: 0.1,
1269 start: 0.,
1270 };
1271 assert!(far.width_px(2.) < 1., "the fixture is not zoomed out enough");
1272 assert_eq!(clip_width(far.width_px(2.)), HIT_MIN);
1273 // Zero is the floor's own case: a clip trimmed to nothing, and the
1274 // width a lane draws before its first frame is measured.
1275 assert_eq!(clip_width(0.), HIT_MIN);
1276 // What is already wide enough keeps its own width, to the pixel: the
1277 // floor is a floor and never a resize.
1278 let near = Scale::default();
1279 assert_eq!(clip_width(near.width_px(5.)), near.width_px(5.));
1280 // ...and the padding is not trimmable: the strips are asked of the
1281 // clip's own width, so a box drawn wider than its length keeps all of
1282 // that box as a body to select and drag by ([`trims`]).
1283 assert!(!trims(far.width_px(2.)));
1284 assert!(clip_width(far.width_px(2.)) >= HIT_MIN);
1285}
1286
1287/// What the Detach and Group items do to a real timeline: a music video's
1288/// sound comes off its picture, Delete on the loose half takes that half
1289/// only, undo puts both back, and Group makes the two one take again --
1290/// whole-take delete and all.
1291#[test]
1292fn a_detached_half_is_removed_alone_and_groups_again() {
1293 let mut session = PlaybackSession::open(asset("test_av.mp4")).expect("open the fixture");
1294 session.set_gain(0.0);
1295 assert!(whole_take(&session, Lane::V1, 0), "one take to start with");
1296 assert!(whole_take(&session, Lane::A1, 0));
1297 let frames = session.lane_clips(Lane::V1)[0].len();
1298
1299 // Detach audio: neither half is a whole take any more, so Delete on
1300 // either leaves the other exactly where it is.
1301 assert!(session.ungroup(Lane::V1, 0));
1302 assert!(
1303 !whole_take(&session, Lane::A1, 0),
1304 "the sound is a half now"
1305 );
1306 assert!(!whole_take(&session, Lane::V1, 0), "and so is the picture");
1307 assert!(session.lift_clip(Lane::A1, 0));
1308 assert!(session.lane_clips(Lane::A1).is_empty(), "the sound went");
1309 assert_eq!(session.lane_clips(Lane::V1).len(), 1, "the picture stayed");
1310 assert_eq!(session.lane_clips(Lane::V1)[0].len(), frames, "untrimmed");
1311
1312 // One undo per edit, the removal then the detach.
1313 assert!(session.undo());
1314 assert_eq!(session.lane_clips(Lane::A1).len(), 1, "the sound is back");
1315 assert!(session.undo());
1316 assert!(whole_take(&session, Lane::A1, 0), "one take again");
1317
1318 // Group: the partner is the clip covering these very frames on the
1319 // other track, which is what the item hands the engine.
1320 assert!(session.ungroup(Lane::V1, 0));
1321 assert_eq!(span_partner(&session, Lane::V1, 0), Some((Lane::A1, 0)));
1322 session
1323 .group(Lane::V1, 0, Lane::A1, 0)
1324 .expect("both halves still cover the same frames");
1325 assert!(
1326 whole_take(&session, Lane::A1, 0),
1327 "a take that ripples again"
1328 );
1329 assert_eq!(
1330 span_partner(&session, Lane::V1, 0),
1331 None,
1332 "and nothing left on another track to group with"
1333 );
1334}
1335
1336/// Which clip Group reaches when more than one covers the span: the sound,
1337/// whatever order the lanes are stored in. A project file may hold them in
1338/// any order -- a video layer *before* the audio lane among them -- and
1339/// "group this" means the other half of the take, never the layer above it.
1340#[test]
1341fn group_reaches_the_sound_before_a_video_layer_over_it() {
1342 use engine::project::LaneKind;
1343
1344 let mut session = PlaybackSession::open(asset("test_av.mp4")).expect("open the fixture");
1345 session.set_gain(0.0);
1346 let v2 = session.add_lane(LaneKind::Video);
1347 // A bare layer, through the one-lane door: a *drop* on V2 brings the
1348 // file's sound down with it and would put a third clip over this span.
1349 let whole = session.lane_clips(Lane::V1)[0];
1350 assert!(
1351 session.place_at(v2, 0.0, whole),
1352 "a layer covering the same frames as the take"
1353 );
1354
1355 // Saved and loaded back with the lanes in the order a hand-written
1356 // project may hold them: the sound last, behind the layer.
1357 let dir = engine::scratch::Scratch::dir("ve_group");
1358 let file = dir.join("lanes.edith");
1359 session.save_project(&file).expect("save the project");
1360 let text = std::fs::read_to_string(&file).expect("read it back");
1361 let (sound, rest): (Vec<&str>, Vec<&str>) =
1362 text.lines().partition(|l| l.starts_with("audio "));
1363 std::fs::write(
1364 &file,
1365 format!("{}\n{}\n", rest.join("\n"), sound.join("\n")),
1366 )
1367 .expect("write the reordered project");
1368 let mut session = PlaybackSession::open_project(&file).expect("it loads as it stands");
1369 std::fs::remove_dir_all(&dir).ok();
1370 assert_eq!(
1371 session.lanes(),
1372 vec![Lane::V1, v2, Lane::A1],
1373 "the sound is the last lane there"
1374 );
1375
1376 // Detached, so Group has a choice to get wrong: the layer covers these
1377 // frames too, and it is the lane the walk meets first.
1378 assert!(session.ungroup(Lane::V1, 0));
1379 // Group on the picture reaches the sound, not that layer.
1380 assert_eq!(span_partner(&session, Lane::V1, 0), Some((Lane::A1, 0)));
1381 session
1382 .group(Lane::V1, 0, Lane::A1, 0)
1383 .expect("the two halves cover the same frames");
1384 // ...and a lane of its own kind is still groupable, once the sound is
1385 // spoken for: two video lanes may be one take.
1386 assert_eq!(
1387 span_partner(&session, Lane::V1, 0),
1388 Some((v2, 0)),
1389 "the layer is what is left to group with"
1390 );
1391}
1392
1393/// The refusal path, end to end against the real files: an incompatible
1394/// import changes nothing, and the library mirrors `sources()` 1:1, so a
1395/// refused file cannot leave a row behind.
1396#[test]
1397fn a_refused_import_leaves_no_row_and_an_accepted_one_is_whole() {
1398 let mut session = PlaybackSession::open(asset("test_av.mp4")).expect("open the fixture");
1399 // Silent like the engine suite: this opens the real device.
1400 session.set_gain(0.0);
1401 assert_eq!(session.sources().len(), 1);
1402 // 640x360 joins now (the project canvas places it), and so does a file
1403 // with no sound (it plays silence over its span). What is left is one
1404 // output device: a mono track cannot join a stereo timeline.
1405 let refusal = session
1406 .import(&asset("test_ac3.mp4"))
1407 .expect_err("a mono track must not join a stereo timeline")
1408 .to_string();
1409 assert!(refusal.contains("audio"), "refusal must name it: {refusal}");
1410 assert_eq!(session.sources().len(), 1, "a refusal added a row");
1411 // An accepted one does add a row, and it reads as the whole file: 4 s
1412 // at 30 fps, its own length and not one taken off the lanes.
1413 session.import(&asset("test_av2.mp4")).expect("av2 matches");
1414 assert_eq!(session.sources().len(), 2);
1415 let second = session.sources()[1].path.clone();
1416 assert_eq!(session.file_frames(&second), 120);
1417 assert_eq!(timecode(120. / 30., 30.), "00:00:04:00");
1418}
1419
1420/// What the Add button and a dropped row both do, minus the pointer: the
1421/// clip [`Player::insert_source`] builds, put in where the playhead is. One
1422/// call, so what a drop does cannot drift from what the button does.
1423#[test]
1424fn adding_a_row_drops_the_whole_source_in_at_the_playhead() {
1425 let mut session = PlaybackSession::open(asset("test_av.mp4")).expect("open the fixture");
1426 session.set_gain(0.0);
1427 session.import(&asset("test_av2.mp4")).expect("av2 matches");
1428 // In the library only: the 5 s the fixture is, still.
1429 assert_eq!(session.timeline_duration(), 5.0);
1430 // Two seconds in, which is inside the first take: the insert splits it.
1431 session.seek(2.0);
1432 let second = session.sources()[1].path.clone();
1433 let frames = session.file_frames(&second);
1434 // Through the engine door `insert_source` uses, with the row's own
1435 // stream: the button, the drop and this are one call.
1436 assert!(
1437 session
1438 .place_stream_at(2.0, &second, 0, None)
1439 .expect("av2 is already on the timeline")
1440 );
1441 // The whole of source 1 went in and nothing was painted over: the
1442 // timeline is longer by exactly that file.
1443 assert_eq!(session.timeline_duration(), 9.0);
1444 let (video, audio) = (session.lane_clips(Lane::V1), session.lane_clips(Lane::A1));
1445 // One take, not a video clip with no sound under it: both lanes hold
1446 // the same clip at the same place, in the same group.
1447 let at = |lane: &[Clip]| {
1448 *lane
1449 .iter()
1450 .find(|c| c.start == 60)
1451 .expect("inserted at 2 s")
1452 };
1453 assert_eq!(at(video), at(audio));
1454 assert_eq!(at(video).source, 1);
1455 assert_eq!(at(video).len(), frames);
1456 assert!(at(video).link.is_some());
1457 assert_eq!(video.len(), audio.len());
1458
1459 // The same door with a *second audio stream* of a file already there:
1460 // a new source entry, the same picture, and the row that was dragged
1461 // is what plays. This is the whole user-facing point of the slice.
1462 let mut session =
1463 PlaybackSession::open(asset("test_multilang.mp4")).expect("open the fixture");
1464 session.set_gain(0.0);
1465 let path = session.sources()[0].path.clone();
1466 let frames = session.file_frames(&path);
1467 let end = session.timeline_duration();
1468 assert!(
1469 session
1470 .place_stream_at(end, &path, 1, None)
1471 .expect("the French track shares the timeline's parameters")
1472 );
1473 assert_eq!(session.sources()[1].audio_stream, 1);
1474 assert_eq!(session.timeline_duration(), end * 2.0);
1475 // Both rows are the same file, so both rows are that file's length --
1476 // the second one before any clip of its own existed.
1477 assert_eq!(session.file_frames(&path), frames);
1478}
1479
1480/// The drop a hand actually makes: a library row let go on the *empty* bed
1481/// past the last clip, which is most of the bed on any timeline shorter
1482/// than the window. The whole chain the release runs, minus gpui's own
1483/// pointer read -- [`Player::place_frame`]'s [`landing`], the frame back
1484/// through the rate as [`Player::insert_source`] hands it over, and the one
1485/// engine door a row goes through -- and the head lands on the frame the
1486/// ghost was drawn on, black in front of it. It used to be swallowed by the
1487/// clipboard's clamp inside `Project::paste` and appended after the last
1488/// clip wherever it was let go, which is the bug this pins.
1489#[test]
1490fn a_row_dropped_on_the_open_bed_lands_under_the_pointer() {
1491 let mut session = PlaybackSession::open(asset("test_av.mp4")).expect("open the fixture");
1492 session.set_gain(0.0);
1493 session.import(&asset("test_av2.mp4")).expect("av2 matches");
1494 let second = session.sources()[1].path.clone();
1495 let fps = session.meta().frame_rate;
1496 assert_eq!(session.timeline_duration(), 5.0);
1497 // 5 s of timeline on a bed 30 s wide: the open bed past the last clip
1498 // is five sixths of it.
1499 let bed: Bounds<Pixels> = Bounds {
1500 origin: point(px(12.), px(400.)),
1501 size: size(px(600.), px(LANE_H)),
1502 };
1503 let scale = Scale {
1504 pps: 20.,
1505 start: 0.,
1506 };
1507
1508 // What `Player::place_frame` answers for a pointer 200 px along the
1509 // bed: ten seconds in, five seconds past the end of the timeline, and
1510 // no mark anywhere near enough to pull it back.
1511 let clips = [session.lane_clips(Lane::V1), session.lane_clips(Lane::A1)];
1512 let marks = snap_marks(&clips, None, None, frame_at(session.now(), fps));
1513 let under = frame_at(scale.time_at(px_along(px(212.), bed)), fps);
1514 let (at, cue) = landing(under, 0, 0, true, scale.snap_frames(fps), &marks);
1515 assert_eq!((at, cue), (300, None), "the pointer is on frame 300");
1516
1517 // ...and what the release does with it: the frame back through the same
1518 // rate every box is drawn at, into the door the Add button uses too.
1519 assert!(
1520 session
1521 .place_stream_at(f64::from(at) / fps, &second, 0, None)
1522 .expect("av2 is already on this timeline")
1523 );
1524 let head = |lane| {
1525 session
1526 .lane_clips(lane)
1527 .last()
1528 .copied()
1529 .expect("the dropped clip")
1530 };
1531 assert_eq!(
1532 head(Lane::V1).start,
1533 at,
1534 "the drop landed somewhere other than under the pointer"
1535 );
1536 assert_eq!(head(Lane::A1).start, at, "...and its sound with it");
1537 assert!(head(Lane::V1).link.is_some(), "one grouped take");
1538 assert_eq!(head(Lane::V1).link, head(Lane::A1).link);
1539 // The bed in front of it stays black: nothing was stretched to reach
1540 // it, and the 4 s file is the whole of what was added.
1541 assert_eq!(session.timeline_duration(), 14.0);
1542}
1543
1544/// Remove from library, through the door the menu item uses
1545/// ([`Player::remove_source`] calls exactly this): refused by name while
1546/// clips play from the file, and once they do not the row leaves the list.
1547#[test]
1548fn removing_a_row_is_refused_while_it_plays_and_takes_the_row_away() {
1549 let mut session = PlaybackSession::open(asset("test_av.mp4")).expect("open the fixture");
1550 session.set_gain(0.0);
1551 // Imported, then dragged onto the end: an import alone fills the
1552 // library, and a row with no clip is removable without any refusal to
1553 // test. This one has to have a take on the timeline.
1554 session.import(&asset("test_av2.mp4")).expect("av2 matches");
1555 let second = session.sources()[1].path.clone();
1556 let end = session.timeline_duration();
1557 assert!(
1558 session
1559 .place_stream_at(end, &second, 0, None)
1560 .expect("a file just imported is on this timeline")
1561 );
1562 let streams = HashMap::new();
1563 let rows = |session: &PlaybackSession| {
1564 library_rows(session.sources(), &streams, &HashMap::new(), None, |_| 0).len()
1565 };
1566 assert_eq!(rows(&session), 2, "a row per source");
1567
1568 let refusal = session
1569 .remove_source(&second, 0)
1570 .expect_err("its take is still on the timeline")
1571 .to_string();
1572 assert!(refusal.contains("still plays"), "{refusal}");
1573 assert!(
1574 refusal.contains("V1 (1 clip)") && refusal.contains("A1 (1 clip)"),
1575 "the refusal names the lanes to clear: {refusal}"
1576 );
1577 assert_eq!(rows(&session), 2, "and the row is still there");
1578
1579 // Delete that take -- the whole group, from either half -- and the file
1580 // can go. The list is what says so.
1581 let last = session.lane_clips(Lane::V1).len() - 1;
1582 assert!(session.delete_clip(Lane::V1, last));
1583 session
1584 .remove_source(&second, 0)
1585 .expect("nothing plays av2 any more");
1586 assert_eq!(rows(&session), 1, "the row went with it");
1587 assert_eq!(session.sources().len(), 1);
1588 // The one file left is held to the same rule and no other: its own take
1589 // is still on the lanes, so it stays...
1590 let first = session.sources()[0].path.clone();
1591 let refusal = session
1592 .remove_source(&first, 0)
1593 .expect_err("its take is still on the timeline")
1594 .to_string();
1595 assert!(refusal.contains("still plays"), "{refusal}");
1596 // ...and once nothing plays it, the *last* row goes too. What is left
1597 // is the empty library `Player::close_session` turns back into the
1598 // window the editor launches as -- the user-reported bug was that this
1599 // very removal was refused, leaving a row that could never be taken
1600 // out.
1601 assert!(session.delete_clip(Lane::V1, 0));
1602 session
1603 .remove_source(&first, 0)
1604 .expect("the only row goes like any other");
1605 assert_eq!(rows(&session), 0, "an empty library");
1606 assert!(session.sources().is_empty());
1607 // And it is still a session: silent, empty, and asked for its length
1608 // rather than panicking on a source list that is not there.
1609 assert_eq!(session.timeline_duration(), 0.0);
1610 assert!(
1611 session.save_project(&asset("nothing.edith")).is_err(),
1612 "a project naming no file could never be opened again, so it is not written"
1613 );
1614 // A row this timeline never had is refused, not panicked on.
1615 assert!(session.remove_source(&second, 0).is_err());
1616}
1617
1618/// The clipboard after a library removal. A copied clip names its file by
1619/// index and a removal renumbers the list, so this is the difference
1620/// between pasting the take that was copied and pasting **another file**
1621/// over the same range ([`clipboard_after_remove`], called by
1622/// [`Player::remove_source`]).
1623#[test]
1624fn a_copied_clip_is_renumbered_or_dropped_when_a_row_leaves_the_library() {
1625 let clip = |source: usize| Clip {
1626 fade_in: 0,
1627 fade_out: 0,
1628 transition_out: 0,
1629 start: 0,
1630 in_frame: 0,
1631 out_frame: 30,
1632 source,
1633 link: None,
1634 eq: None,
1635 color: None,
1636 transform: None,
1637 fit: Default::default(),
1638 speed: Default::default(),
1639 };
1640 // Copied from source 2, source 0 removed: the same file is source 1 now.
1641 assert_eq!(
1642 clipboard_after_remove(Some(clip(2)), 0).map(|c| c.source),
1643 Some(1),
1644 "the clipboard follows its file down the list"
1645 );
1646 // Copied from a source *before* the one that went: untouched.
1647 assert_eq!(
1648 clipboard_after_remove(Some(clip(0)), 2).map(|c| c.source),
1649 Some(0)
1650 );
1651 // Copied from the row that was just removed: there is nothing left to
1652 // paste, and pasting the next file along would be a lie.
1653 assert!(clipboard_after_remove(Some(clip(1)), 1).is_none());
1654 assert!(clipboard_after_remove(None, 0).is_none());
1655}
1656
1657/// The trim-a-clip path through the doors the edge drag uses:
1658/// [`Player::trim_to`] clamps the pointer with `trim_room` and
1659/// [`Player::commit_trim`] writes it with `trim_clip`. The clip plays less
1660/// of its file, the sound linked to it follows, the head trim moves the
1661/// in-point, and one undo takes a whole gesture back.
1662///
1663/// The routing *to* these doors -- the 6 px edge strip claiming the press
1664/// the clip's own body-drag would otherwise take -- is gpui hitbox
1665/// behaviour (`occlude`) and is not reachable without a window.
1666#[test]
1667fn a_clip_trimmed_by_its_edge_plays_less_of_its_file() {
1668 use engine::project::Edge;
1669
1670 let mut session = PlaybackSession::open(asset("test_av.mp4")).expect("open the fixture");
1671 session.set_gain(0.0);
1672 let whole = session.lane_clips(Lane::V1)[0];
1673 let full = session.timeline_duration();
1674 assert_eq!(
1675 session.trim_room(Lane::V1, 0, Edge::End),
1676 Some((whole.start + 1, whole.end())),
1677 "the file's own last frame is how far the tail goes"
1678 );
1679 assert!(
1680 !session.trim_clip(Lane::V1, 0, Edge::End, 9_999),
1681 "it already plays all of it"
1682 );
1683
1684 // Pulled in by a third: the timeline ends earlier and the sound with it.
1685 let shorter = whole.end() - whole.len() / 3;
1686 assert!(session.trim_clip(Lane::V1, 0, Edge::End, shorter));
1687 assert_eq!(session.lane_clips(Lane::V1)[0].end(), shorter);
1688 assert_eq!(
1689 session.lane_clips(Lane::A1)[0].end(),
1690 shorter,
1691 "the linked sound was trimmed with the picture"
1692 );
1693 assert!(session.timeline_duration() < full, "and plays out earlier");
1694
1695 // ...and dragged back out, as far as the file goes and no further.
1696 assert!(session.trim_clip(Lane::V1, 0, Edge::End, 9_999));
1697 assert_eq!(
1698 session.lane_clips(Lane::V1)[0],
1699 whole,
1700 "the whole take back"
1701 );
1702
1703 // The head takes the in-point with it, so what plays at the new start
1704 // is source frame 10 rather than source frame 0.
1705 assert!(session.trim_clip(Lane::V1, 0, Edge::Start, 10));
1706 let head = session.lane_clips(Lane::V1)[0];
1707 assert_eq!((head.start, head.in_frame), (10, 10));
1708 assert_eq!(session.lane_clips(Lane::A1)[0].in_frame, 10, "sound too");
1709 assert_eq!(
1710 session.trim_room(Lane::V1, 0, Edge::Start).map(|r| r.0),
1711 Some(0),
1712 "and it may be pulled back out to the file's first frame"
1713 );
1714
1715 assert!(session.undo(), "one step for the whole drag");
1716 assert_eq!(session.lane_clips(Lane::V1)[0], whole);
1717}
1718
1719/// The move-a-clip-between-tracks path through the door the drop uses
1720/// ([`Player::move_clip`] calls exactly this): the clip changes row, the
1721/// *picture* comes from the new row afterwards -- which is what "it plays
1722/// from there" means -- and one undo puts it back.
1723#[test]
1724fn a_clip_dragged_onto_another_track_plays_from_it() {
1725 use engine::project::LaneKind;
1726
1727 let mut session = PlaybackSession::open(asset("test_av.mp4")).expect("open the fixture");
1728 session.set_gain(0.0);
1729 let v2 = session.add_lane(LaneKind::Video);
1730 assert_eq!(session.video_clip_at(0.0), Some((Lane::V1, 0)));
1731
1732 assert!(session.move_clip_to(Lane::V1, 0, v2, 0), "V1 -> V2");
1733 assert!(session.lane_clips(Lane::V1).is_empty(), "it left V1");
1734 assert_eq!(session.lane_clips(v2).len(), 1, "and landed on V2");
1735 assert_eq!(
1736 session.video_clip_at(0.0),
1737 Some((v2, 0)),
1738 "the picture now comes from V2"
1739 );
1740 assert_eq!(session.lane_clips(Lane::A1).len(), 1, "its sound stayed");
1741
1742 // Dropped on a lane of the other kind it is refused and nothing moves --
1743 // the notice the front-end shows for it says which kind of lane to use.
1744 // (The other refusal, landing on another clip, is the engine's own test
1745 // `move_clip_keeps_the_frames_and_refuses_the_rest`.)
1746 assert!(!session.move_clip_to(v2, 0, Lane::A1, 0), "picture on A1");
1747 assert_eq!(session.lane_clips(v2).len(), 1, "and it stayed on V2");
1748 assert!(
1749 session.move_clip_to(v2, 0, Lane::V1, 0),
1750 "dragged back down"
1751 );
1752
1753 // One undo per move, and each is a single step.
1754 assert!(session.undo(), "the drag back");
1755 assert_eq!(session.video_clip_at(0.0), Some((v2, 0)));
1756 assert!(session.undo(), "the drag up");
1757 assert_eq!(session.video_clip_at(0.0), Some((Lane::V1, 0)));
1758 assert!(session.lane_clips(v2).is_empty());
1759}
1760
1761/// The add-a-track path end to end through the doors the buttons and the
1762/// drop use: `+ V` adds a row, a library row let go over it lands there and
1763/// nowhere else, Delete on it leaves the lanes under it where they are, and
1764/// undo takes the whole thing back one step at a time.
1765#[test]
1766fn a_track_can_be_added_dropped_on_edited_and_taken_back() {
1767 use engine::project::LaneKind;
1768
1769 let mut session = PlaybackSession::open(asset("test_av.mp4")).expect("open the fixture");
1770 session.set_gain(0.0);
1771 assert_eq!(session.lanes(), vec![Lane::V1, Lane::A1]);
1772 // What the `+ V` button asks for, and what the row it draws is called.
1773 let v2 = session.add_lane(LaneKind::Video);
1774 assert_eq!(v2.label(), "V2");
1775 assert_eq!(session.lanes(), vec![Lane::V1, Lane::A1, v2]);
1776 assert!(session.lane_clips(v2).is_empty());
1777 // A library row let go over that row: the same door the Add button
1778 // uses, told which lane it was let go over.
1779 let path = session.sources()[0].path.clone();
1780 assert!(
1781 session
1782 .place_stream_at(1.0, &path, 0, Some(v2))
1783 .expect("its own file is on this timeline")
1784 );
1785 assert_eq!(session.lane_clips(v2).len(), 1, "the drop landed on V2");
1786 assert_eq!(session.lane_clips(v2)[0].start, 30, "at the playhead");
1787 // ...with the sound it came with, on the row of its own the drop added:
1788 // a file with audio dropped on a layer used to land silent.
1789 let a2 = Lane::new(LaneKind::Audio, 1);
1790 assert_eq!(session.lanes(), vec![Lane::V1, Lane::A1, v2, a2]);
1791 assert_eq!(session.lane_clips(a2), session.lane_clips(v2), "same take");
1792 assert_eq!(
1793 session.lane_clips(v2)[0].link,
1794 session.lane_clips(a2)[0].link,
1795 "grouped, so a drag moves both"
1796 );
1797 // And nowhere else: the first pair is exactly as it was, one take each.
1798 assert_eq!(session.lane_clips(Lane::V1).len(), 1);
1799 assert_eq!(session.lane_clips(Lane::A1).len(), 1);
1800 // ...and it is a project that saves and opens again as it stands: a
1801 // grouped pair on a further row is what the file has to carry.
1802 let dir = engine::scratch::Scratch::dir("ve_layer");
1803 let file = dir.join("layer.edith");
1804 session.save_project(&file).expect("save the project");
1805 let back = PlaybackSession::open_project(&file).expect("it loads as it stands");
1806 std::fs::remove_dir_all(&dir).ok();
1807 assert_eq!(back.lanes(), session.lanes());
1808 for lane in back.lanes() {
1809 assert_eq!(back.lane_clips(lane), session.lane_clips(lane), "{lane:?}");
1810 }
1811 // The drop grouped `V2` with the `A2` it added -- a take, same as the
1812 // first pair's, just on an added row -- so Delete would close both their
1813 // own lanes too (`Project::delete_members`), not drag the first pair out
1814 // of step: `delete_members` ripples only a member's own lane. Lift is
1815 // tested directly here instead, to check its own hole-left-open law
1816 // without going through the Delete key's routing.
1817 assert!(whole_take(&session, v2, 0));
1818 assert!(whole_take(&session, Lane::V1, 0));
1819 assert!(whole_take(&session, Lane::A1, 0));
1820 assert_eq!(a2.label(), "A2");
1821 assert!(session.lift_clip(v2, 0));
1822 assert!(session.lane_clips(v2).is_empty());
1823 assert_eq!(session.lane_clips(Lane::V1).len(), 1, "V1 stayed put");
1824 assert_eq!(
1825 session.lane_clips(a2).len(),
1826 1,
1827 "a lift takes the half it names: the sound is still there to lift"
1828 );
1829 assert!(session.lift_clip(a2, 0));
1830
1831 // A second audio lane used to be what an mp4 export could not write --
1832 // it copied one AAC track and a mix is not a copy. It mixes now
1833 // (`export::copy_audio`), so no row is greyed by a count of lanes.
1834 for format in [Format::Mp4, Format::Av1, Format::Av1Mp4] {
1835 assert_eq!(
1836 format_refusal(&session, format),
1837 None,
1838 "two audio lanes are a mix, not a refusal"
1839 );
1840 }
1841
1842 // Undo, one edit at a time and backwards: the lift of the sound, the
1843 // lift of the picture, the drop on V2 with the lane A2 it added, and
1844 // last the lane V2 -- an added track is one step like every other edit.
1845 for lanes in [4, 4, 3, 2] {
1846 assert!(session.undo());
1847 assert_eq!(session.lanes().len(), lanes);
1848 }
1849 assert_eq!(session.lanes(), vec![Lane::V1, Lane::A1]);
1850 assert_eq!(session.lane_clips(Lane::V1).len(), 1);
1851 assert_eq!(format_refusal(&session, Format::Mp4), None);
1852}
1853
1854/// A caption grouped with a clip on an *added* lane closes its lane's hole
1855/// too, not just the first pair's: the same law commit `cdc53a6` gave `V1`
1856/// (`whole_take` -> `Project::delete_members`) has to hold from every anchor
1857/// a Delete can be clicked from, or a grouped caption on `V2`/subtitle
1858/// survives its clip as an orphan link. `whole_take` used to answer `false`
1859/// for any lane with `ord >= 1` no matter what it was grouped with, which
1860/// routed `Player::delete_selected` to `lift_clip` -- the clip's own lane
1861/// left a hole, and the caption it was grouped with was never touched at
1862/// all.
1863#[test]
1864fn a_grouped_clip_on_an_added_lane_closes_its_hole_with_its_caption() {
1865 use engine::project::LaneKind;
1866
1867 let (mut session, sub_lane) = with_subtitle_lane();
1868 let v2 = session.add_lane(LaneKind::Video);
1869 let path = session.sources()[0].path.clone();
1870 assert!(
1871 session
1872 .place_stream_at(0.0, &path, 0, Some(v2))
1873 .expect("its own file is on this timeline")
1874 );
1875 session
1876 .place_sub(sub_lane, 0, one_second(0))
1877 .expect("the caption goes down");
1878 session
1879 .group_all(&[(v2, 0), (sub_lane, 0)])
1880 .expect("a clip on an added lane and a caption are a group");
1881
1882 // The group pairs the clip for the whole-take question from its own
1883 // lane, `ord >= 1` or not.
1884 assert!(whole_take(&session, v2, 0));
1885
1886 // Two clips after the group's own -- something to close the hole
1887 // against on the caption's lane too.
1888 session
1889 .place_sub(sub_lane, 60, one_second(60))
1890 .expect("a second caption after it");
1891 assert_eq!(session.sub_lane(sub_lane).len(), 2);
1892
1893 // `Player::delete_selected` calls exactly this door once `whole_take`
1894 // says yes -- the fix is that it now reaches it from `v2` too.
1895 assert!(session.delete_clip(v2, 0));
1896 assert!(session.lane_clips(v2).is_empty(), "the clip is gone");
1897 assert_eq!(
1898 session.sub_lane(sub_lane).len(),
1899 1,
1900 "its grouped caption went with it, not left as an orphan"
1901 );
1902 assert_eq!(
1903 session.sub_lane(sub_lane)[0].start, 30,
1904 "and the hole under it closed: the caption after it slid up"
1905 );
1906}
1907
1908#[test]
1909fn a_source_is_only_ever_asked_for_its_peaks_once() {
1910 let (a, b) = (asset("test_av.mp4"), asset("test_av2.mp4"));
1911 // Two files, and two streams of the first: three envelopes, because
1912 // one file's two audio tracks are two different waveforms.
1913 let sources = [
1914 Source {
1915 path: a.clone(),
1916 audio_stream: 0,
1917 },
1918 Source {
1919 path: a.clone(),
1920 audio_stream: 1,
1921 },
1922 Source {
1923 path: b.clone(),
1924 audio_stream: 0,
1925 },
1926 ];
1927 let keys = |s: &[Source]| {
1928 s.iter()
1929 .map(|s| (s.path.clone(), s.audio_stream))
1930 .collect::<Vec<_>>()
1931 };
1932 let mut waves: HashMap<(PathBuf, usize), Wave> = HashMap::new();
1933 assert_eq!(unseen_sources(&sources, &waves), keys(&sources));
1934 // The entry goes in when the decode *starts*, so the sixty repaints a
1935 // second that happen while it runs must not start it again -- which is
1936 // what this asserts about a key whose value is not an answer yet.
1937 waves.insert((a.clone(), 0), Wave::Loading);
1938 assert_eq!(
1939 unseen_sources(&sources, &waves),
1940 keys(&sources[1..]),
1941 "the file's other stream is a key of its own"
1942 );
1943 // A file with no audio is an answer like any other: never re-asked.
1944 waves.insert((a, 1), Wave::Silent);
1945 waves.insert((b.clone(), 0), Wave::Silent);
1946 assert!(unseen_sources(&sources, &waves).is_empty());
1947 // The stream probe is per *file*: the two entries of `a` ask once.
1948 let mut streams: HashMap<PathBuf, Vec<StreamInfo>> = HashMap::new();
1949 assert_eq!(
1950 unseen_paths(&sources, &streams),
1951 vec![asset("test_av.mp4"), b]
1952 );
1953 streams.insert(asset("test_av.mp4"), Vec::new());
1954 assert_eq!(unseen_paths(&sources, &streams).len(), 1);
1955}
1956
1957/// The auto switch, which is the whole of what an import asks: with it off no
1958/// film is handed to [`engine::proxy`] at all, and turning Proxies on is what
1959/// asks for the ones the project is missing -- there is no other door, so a
1960/// project that never wants a stand-in never spends a minute encoding one.
1961#[test]
1962fn a_project_that_makes_no_proxies_by_itself_starts_none_until_it_is_cut_on_them() {
1963 let sources = [Source {
1964 path: asset("test_av.mp4"),
1965 audio_stream: 0,
1966 }];
1967 let none: HashMap<PathBuf, ()> = HashMap::new();
1968 let one = vec![asset("test_av.mp4")];
1969 // Today's behaviour, both ways round: auto on starts them whether or not
1970 // the picture is cut on them -- the cache is warmed for later.
1971 assert_eq!(proxies_to_start(true, false, &sources, &none), one);
1972 assert_eq!(proxies_to_start(true, true, &sources, &none), one);
1973 // Auto off and not cut on them: nothing is started at all.
1974 assert!(
1975 proxies_to_start(false, false, &sources, &none).is_empty(),
1976 "an import started a stand-in the project asked it not to"
1977 );
1978 // ...and the switch is the ask: the film comes through the moment Proxies
1979 // goes on, because it was left unseen rather than marked done.
1980 assert_eq!(proxies_to_start(false, true, &sources, &none), one);
1981 // One start per film either way: a film already in the map is one already
1982 // asked about, sixty repaints a second notwithstanding.
1983 let seen: HashMap<PathBuf, ()> = HashMap::from([(asset("test_av.mp4"), ())]);
1984 assert!(proxies_to_start(true, true, &sources, &seen).is_empty());
1985}
1986
1987#[test]
1988fn the_add_button_is_dead_unless_it_would_do_something() {
1989 let picked = (PathBuf::from("/m/0.mp4"), 0);
1990 assert!(can_add(Some(&picked), true, false));
1991 // Nothing picked, nothing to put it on, or an export reading the very
1992 // edit list this would change.
1993 assert!(!can_add(None, true, false));
1994 assert!(!can_add(Some(&picked), false, false));
1995 assert!(!can_add(Some(&picked), true, true));
1996}
1997
1998#[test]
1999fn the_media_column_never_takes_the_picture_over() {
2000 for window in [360., 640., 1280., 1920., 3840.] {
2001 let w = crate::library_w(window);
2002 // The whole point of the budget: the picture keeps the majority of
2003 // the row at every size a window can be.
2004 assert!(w <= window / 3., "{window}px window gave the list {w}px");
2005 assert!(w >= LIBRARY_MIN_W.min(window / 3.), "{window}px: {w}px");
2006 assert!(w <= LIBRARY_MAX_W);
2007 }
2008 // It yields: a narrower window gives the list less, never the same.
2009 assert!(crate::library_w(640.) < crate::library_w(1280.));
2010 // Rows are clickable, so WCAG 2.5.8 binds them like every other target,
2011 // and a name over a timecode has to fit inside one.
2012 assert!(ROW_H >= HIT_MIN);
2013 assert!(SWATCH_W < LIBRARY_MIN_W);
2014}
2015
2016#[test]
2017fn the_window_is_named_after_the_program_and_what_is_open() {
2018 assert_eq!(window_title("test_av.mp4"), "test_av.mp4 — edith");
2019 // An empty window is the program, not "no file open — edith".
2020 assert_eq!(window_title(NO_FILE), "edith");
2021}
2022
2023#[test]
2024fn frac_along_measures_from_the_elements_own_left_edge() {
2025 // A ruler inset by the panel's 12 px padding: window x is not bar x.
2026 let bar: Bounds<Pixels> = Bounds {
2027 origin: point(px(12.), px(400.)),
2028 size: size(px(200.), px(6.)),
2029 };
2030 assert_eq!(frac_along(px(12.), bar), 0.);
2031 assert_eq!(frac_along(px(112.), bar), 0.5);
2032 assert_eq!(frac_along(px(212.), bar), 1.);
2033 // Outside the bar (a click that slid off) clamps, never extrapolates.
2034 assert_eq!(frac_along(px(0.), bar), 0.);
2035 assert_eq!(frac_along(px(9999.), bar), 1.);
2036 // Never painted: no division by zero, no NaN reaching seek().
2037 assert_eq!(frac_along(px(50.), Bounds::default()), 0.);
2038
2039 // The equalizer's axis is the other one, and reads the same way.
2040 let graph: Bounds<Pixels> = Bounds {
2041 origin: point(px(12.), px(60.)),
2042 size: size(px(296.), px(EQ_GRAPH_H)),
2043 };
2044 assert_eq!(frac_down(px(60.), graph), 0.);
2045 assert_eq!(frac_down(px(60. + EQ_GRAPH_H / 2.), graph), 0.5);
2046 assert_eq!(frac_down(px(9999.), graph), 1.);
2047 // Never painted reads as flat: an unpainted graph must not slam a band
2048 // to +12 dB on the first press.
2049 assert_eq!(frac_down(px(50.), Bounds::default()), 0.5);
2050}
2051
2052/// A fade handle's pixels turn into frames at the bed's own scale, and a
2053/// degenerate scale (nothing painted) turns into no movement at all.
2054#[test]
2055fn a_fade_drag_converts_pixels_to_frames_at_the_beds_scale() {
2056 use crate::timeline_math::fade_delta_frames;
2057 // 100 px/s at 25 fps: one second of pixels is one second of frames.
2058 assert_eq!(fade_delta_frames(100., 100., 25.), 25);
2059 // Signed: dragging back shrinks the fade by the same conversion.
2060 assert_eq!(fade_delta_frames(-50., 100., 25.), -13);
2061 // Nothing drawn to drag against moves nothing.
2062 assert_eq!(fade_delta_frames(40., 0., 25.), 0);
2063 assert_eq!(fade_delta_frames(40., -1., 25.), 0);
2064}
2065
2066/// The cut odometer (DESIGN.md §6): `,` `.` walk it one cut at a time, shift
2067/// strides ten, and either end holds rather than wrapping.
2068#[test]
2069fn walk_cut_strides_and_clamps_at_either_end() {
2070 use crate::timeline_math::walk_cut;
2071 assert_eq!(walk_cut(3, 10, true, 1), 4);
2072 assert_eq!(walk_cut(3, 10, false, 1), 2);
2073 assert_eq!(walk_cut(3, 10, true, 10), 9, "clamps at the last cut");
2074 assert_eq!(walk_cut(3, 10, false, 10), 0, "clamps at the first cut");
2075 assert_eq!(walk_cut(9, 10, true, 1), 9, "already at the end holds");
2076 assert_eq!(walk_cut(0, 10, false, 1), 0, "already at the start holds");
2077 // No cuts at all: nothing to land on.
2078 assert_eq!(walk_cut(0, 0, true, 1), 0);
2079}
2080
2081/// The no-aim trim (DESIGN.md §6): a frame detent clamped to the room the
2082/// engine already answered, in either direction, and never past it.
2083#[test]
2084fn nudge_edge_moves_one_frame_and_clamps_to_the_room() {
2085 use crate::timeline_math::nudge_edge;
2086 assert_eq!(nudge_edge(50, 1, 0, 100), 51);
2087 assert_eq!(nudge_edge(50, -1, 0, 100), 49);
2088 assert_eq!(nudge_edge(0, -1, 0, 100), 0, "the room's own floor holds");
2089 assert_eq!(nudge_edge(100, 1, 0, 100), 100, "the room's own ceiling holds");
2090 // A 4px-wide clip's room can be one frame wide: still clamps, never
2091 // panics on the narrow end.
2092 assert_eq!(nudge_edge(5, 1, 5, 5), 5);
2093}
2094
2095/// Loop-trim's restart (DESIGN.md §6): only once the window's far edge is
2096/// reached, and never with the mode off.
2097#[test]
2098fn should_loop_restart_only_at_the_windows_far_edge() {
2099 use crate::timeline_math::should_loop_restart;
2100 assert!(!should_loop_restart(40, Some((30, 60))));
2101 assert!(should_loop_restart(60, Some((30, 60))));
2102 assert!(should_loop_restart(61, Some((30, 60))), "past it restarts too");
2103 assert!(!should_loop_restart(60, None), "loop-trim off restarts nothing");
2104}
2105
2106/// The paused-then-started repro this predicate was blamed for and cleared
2107/// of: `should_loop_restart` takes no play/pause state at all, so resting
2108/// exactly at the window's far edge -- as a transport paused after playing
2109/// through the cut once, or paused right on arrival by a scrub -- reads
2110/// "restart" from the predicate alone regardless. The bug that froze a
2111/// paused-then-`space`d transport at the loop's start frame was not this: it
2112/// was `Player::toggle_loop_trim` forcing `session.play()` on arm, so the
2113/// user's own next `space` toggled straight back to paused. It is
2114/// `Player::pump`'s `session.is_playing()` guard around this call that must
2115/// hold a paused transport off the seek until playback actually starts.
2116#[test]
2117fn should_loop_restart_is_play_state_blind_the_pump_gates_it() {
2118 use crate::timeline_math::should_loop_restart;
2119 assert!(
2120 should_loop_restart(60, Some((30, 60))),
2121 "the predicate alone can't tell paused-at-the-edge from playing-\
2122 into-it -- that distinction is pump's is_playing() guard, not this"
2123 );
2124}