| 1 | |
| 2 | |
| 3 | |
| 4 | |
| 5 | |
| 6 | |
| 7 | |
| 8 | |
| 9 | |
| 10 | |
| 11 | |
| 12 | |
| 13 | use std::path::PathBuf; |
| 14 | |
| 15 | use engine::project::{Lane, LaneKind, Speed}; |
| 16 | use engine::scale::FitPolicy; |
| 17 | use engine::scratch::Scratch; |
| 18 | use engine::PlaybackSession; |
| 19 | use engine::project::Clip; |
| 20 | |
| 21 | fn asset(name: &str) -> PathBuf { |
| 22 | PathBuf::from(env!("CARGO_MANIFEST_DIR")) |
| 23 | .join("../../assets") |
| 24 | .join(name) |
| 25 | } |
| 26 | |
| 27 | |
| 28 | fn session() -> (PlaybackSession, usize) { |
| 29 | let mut s = PlaybackSession::open(asset("test_av.mp4")).expect("open"); |
| 30 | s.set_gain(0.0); |
| 31 | let idx = s.import(&asset("test_23976fps.mp4")).expect("import"); |
| 32 | assert_eq!(s.file_frames(&asset("test_23976fps.mp4")), 61, "picture is 61 frames"); |
| 33 | (s, idx) |
| 34 | } |
| 35 | |
| 36 | |
| 37 | fn clip(source: usize, in_frame: u32, out_frame: u32) -> Clip { |
| 38 | Clip { |
| 39 | speed_keys: Vec::new(), |
| 40 | reversed: false, |
| 41 | stabilise: None, deinterlace: Default::default(), mark: Default::default(), |
| 42 | angle: Default::default(), |
| 43 | fade_in: 0, |
| 44 | fade_out: 0, |
| 45 | transition_out: 0, |
| 46 | transition_kind: Default::default(), |
| 47 | visualizer: 0, |
| 48 | viz_paint: 0, |
| 49 | gain_db: 0.0, |
| 50 | gain_keys: Vec::new(), |
| 51 | pan: 0, |
| 52 | pitch: 0.0, |
| 53 | transform_keys: Vec::new(), |
| 54 | opacity_keys: Vec::new(), |
| 55 | color_keys: Vec::new(), |
| 56 | effects: Vec::new(), |
| 57 | start: 0, |
| 58 | in_frame, |
| 59 | out_frame, |
| 60 | source, |
| 61 | link: None, |
| 62 | eq: None, |
| 63 | color: None, |
| 64 | transform: None, |
| 65 | fit: FitPolicy::default(), |
| 66 | speed: Speed::NORMAL, |
| 67 | } |
| 68 | } |
| 69 | |
| 70 | fn a(n: usize) -> Lane { |
| 71 | Lane::new(LaneKind::Audio, n) |
| 72 | } |
| 73 | |
| 74 | fn v(n: usize) -> Lane { |
| 75 | Lane::new(LaneKind::Video, n) |
| 76 | } |
| 77 | |
| 78 | |
| 79 | |
| 80 | |
| 81 | fn assert_holds(s: &PlaybackSession, label: &str) { |
| 82 | for lane in s.lanes() { |
| 83 | for (i, c) in s.lane_clips(lane).iter().enumerate() { |
| 84 | let wall = match lane.kind { |
| 85 | LaneKind::Audio => 60, |
| 86 | _ => 61, |
| 87 | }; |
| 88 | if c.source == s.sources().len() - 1 { |
| 89 | assert!( |
| 90 | c.out_frame <= wall, |
| 91 | "{label}: {lane:?} clip {i} ends at {} past its wall {wall}", |
| 92 | c.out_frame |
| 93 | ); |
| 94 | } |
| 95 | } |
| 96 | } |
| 97 | let dir = Scratch::dir("placement_walls"); |
| 98 | let path = dir.join(format!("{}.edith", label.replace(|c: char| !c.is_alphanumeric(), "_"))); |
| 99 | s.save_project(&path) |
| 100 | .unwrap_or_else(|e| panic!("{label}: save refused: {e}")); |
| 101 | let reopened = PlaybackSession::open_project(&path).expect("reopen"); |
| 102 | assert!( |
| 103 | reopened.load_warnings().is_empty(), |
| 104 | "{label}: reopen clamped: {:?}", |
| 105 | reopened.load_warnings() |
| 106 | ); |
| 107 | } |
| 108 | |
| 109 | #[test] |
| 110 | fn place_stream_at_every_lane_holds_to_its_wall() { |
| 111 | let file = asset("test_23976fps.mp4"); |
| 112 | for (label, onto) in [ |
| 113 | ("grouped", None), |
| 114 | ("V1", Some(v(0))), |
| 115 | ("V2", Some(v(1))), |
| 116 | ("A1", Some(a(0))), |
| 117 | ("A2", Some(a(1))), |
| 118 | ] { |
| 119 | let (mut s, _) = session(); |
| 120 | let end = s.timeline_duration(); |
| 121 | s.place_stream_at(end, &file, 0, onto) |
| 122 | .unwrap_or_else(|e| panic!("{label}: {e}")); |
| 123 | assert_holds(&s, &format!("place_stream_at {label}")); |
| 124 | } |
| 125 | } |
| 126 | |
| 127 | #[test] |
| 128 | fn place_at_with_a_range_past_the_sound_holds_to_the_sound() { |
| 129 | for lane in [v(0), v(1), a(0), a(1)] { |
| 130 | let (mut s, src) = session(); |
| 131 | |
| 132 | let lane = match lane.ord { |
| 133 | 0 => lane, |
| 134 | _ => s.add_lane(lane.kind), |
| 135 | }; |
| 136 | let at = s.timeline_duration(); |
| 137 | |
| 138 | |
| 139 | assert!(s.place_at(lane, at, clip(src, 0, 61)), "{lane:?}: placed"); |
| 140 | let landed = s.lane_clips(lane).last().expect("a clip").frames(); |
| 141 | let want = match lane.kind { |
| 142 | LaneKind::Audio => 60, |
| 143 | _ => 61, |
| 144 | }; |
| 145 | assert_eq!(landed, want, "{lane:?} holds to its own wall"); |
| 146 | assert_holds(&s, &format!("place_at {lane:?}")); |
| 147 | } |
| 148 | } |
| 149 | |
| 150 | #[test] |
| 151 | fn place_at_past_the_wall_entirely_is_refused_not_saved_wrong() { |
| 152 | let (mut s, src) = session(); |
| 153 | let at = s.timeline_duration(); |
| 154 | |
| 155 | assert!(!s.place_at(a(0), at, clip(src, 60, 61))); |
| 156 | assert_holds(&s, "refused sound"); |
| 157 | } |
| 158 | |
| 159 | #[test] |
| 160 | fn paste_and_paste_set_hold_each_item_to_its_lane() { |
| 161 | let (mut s, src) = session(); |
| 162 | let at = s.timeline_duration(); |
| 163 | assert!(s.paste_at(at, clip(src, 0, 61)), "paste lands"); |
| 164 | assert_holds(&s, "paste_at"); |
| 165 | |
| 166 | let (mut s, src) = session(); |
| 167 | let at = s.timeline_duration(); |
| 168 | assert!(s.paste_set_at(at, &[(v(0), clip(src, 0, 61)), (a(0), clip(src, 0, 61))])); |
| 169 | assert_eq!(s.lane_clips(a(0)).last().expect("sound").frames(), 60); |
| 170 | assert_eq!(s.lane_clips(v(0)).last().expect("picture").frames(), 61); |
| 171 | assert_holds(&s, "paste_set_at"); |
| 172 | } |
| 173 | |
| 174 | #[test] |
| 175 | fn autosave_writes_a_project_that_runs_past_its_source() { |
| 176 | let (mut s, src) = session(); |
| 177 | let at = s.timeline_duration(); |
| 178 | assert!(s.place_at(a(0), at, clip(src, 0, 61))); |
| 179 | let dir = Scratch::dir("placement_walls_autosave"); |
| 180 | let path = dir.join("autosave.edith"); |
| 181 | s.save_autosave(&path).expect("an autosave is never refused"); |
| 182 | assert!(path.exists(), "the autosave wrote its file"); |
| 183 | |
| 184 | let reopened = PlaybackSession::open_project(&path).expect("the autosave reopens"); |
| 185 | assert!(!reopened.sources().is_empty()); |
| 186 | } |
| 187 | |
| 188 | |
| 189 | |
| 190 | #[test] |
| 191 | fn place_stream_overwrite_at_replaces_and_holds_the_timeline_length() { |
| 192 | let file = asset("test_23976fps.mp4"); |
| 193 | let (mut s, _) = session(); |
| 194 | s.place_stream_at(0.0, &file, 0, None).expect("first placement"); |
| 195 | let before = s.timeline_duration(); |
| 196 | s.place_stream_overwrite_at(0.0, &file, 0, None) |
| 197 | .expect("overwrite over the clip"); |
| 198 | assert_eq!(s.timeline_duration(), before, "an overwrite pushes nothing past the end"); |
| 199 | assert_holds(&s, "overwrite over a take"); |
| 200 | |
| 201 | let (mut t, _) = session(); |
| 202 | t.place_stream_at(0.0, &file, 0, None).expect("first placement"); |
| 203 | let before = t.timeline_duration(); |
| 204 | t.place_stream_at(0.0, &file, 0, None).expect("insert over the clip"); |
| 205 | assert!(t.timeline_duration() > before, "an insert pushes the clip under it along"); |
| 206 | } |