|
| 1 | +//! A painter must never panic — nor hang — on input the schema accepts. |
| 2 | +//! |
| 3 | +//! `rustmotion validate` is the documented gate before delivery, and it answers |
| 4 | +//! "Valid scenario" for every component below. Each one was observed aborting or |
| 5 | +//! wedging the renderer mid-frame, which kills the whole encode: the scenarios |
| 6 | +//! here are the reproductions, verbatim, kept as a regression floor. |
| 7 | +//! |
| 8 | +//! Every case routes through the real pipeline — serde, box_builder, run_layout, |
| 9 | +//! paint_tree — so a fix that only guards the painter while leaving the component |
| 10 | +//! undeserialisable would still fail. |
| 11 | +
|
| 12 | +use std::sync::mpsc; |
| 13 | +use std::time::Duration; |
| 14 | + |
| 15 | +use rustmotion_components::box_builder::{build_scene_with_anim, BuildAnimationCtx}; |
| 16 | +use rustmotion_components::legacy_dispatch::LegacyPaintDispatcher; |
| 17 | +use rustmotion_components::{ChildComponent, Component, PositionMode}; |
| 18 | +use rustmotion_core::css::taffy_bridge::ConversionContext; |
| 19 | +use rustmotion_core::engine::layout_pass::run_layout; |
| 20 | +use rustmotion_core::engine::paint_pass::{paint_tree, PaintFrame}; |
| 21 | + |
| 22 | +const W: u32 = 400; |
| 23 | +const H: u32 = 300; |
| 24 | + |
| 25 | +/// Deserialize one component and paint it at `time`. Panics propagate — that is |
| 26 | +/// the point of the test. |
| 27 | +fn paint(json: serde_json::Value, time: f64) { |
| 28 | + let component: Component = serde_json::from_value(json).expect("component is schema-valid"); |
| 29 | + let children = vec![ChildComponent { |
| 30 | + component, |
| 31 | + position: Some(PositionMode::Absolute { x: 0.0, y: 0.0 }), |
| 32 | + x: None, |
| 33 | + y: None, |
| 34 | + z_index: None, |
| 35 | + bleed: false, |
| 36 | + }]; |
| 37 | + |
| 38 | + let mut surface = |
| 39 | + skia_safe::surfaces::raster_n32_premul((W as i32, H as i32)).expect("raster surface"); |
| 40 | + let canvas = surface.canvas(); |
| 41 | + canvas.clear(skia_safe::Color4f::new(0.0, 0.0, 0.0, 0.0)); |
| 42 | + |
| 43 | + let built = build_scene_with_anim( |
| 44 | + &children, |
| 45 | + (W as f32, H as f32), |
| 46 | + BuildAnimationCtx { |
| 47 | + time, |
| 48 | + scene_duration: 2.0, |
| 49 | + fps: 30, |
| 50 | + }, |
| 51 | + ); |
| 52 | + let layout = run_layout( |
| 53 | + &built.root, |
| 54 | + (W as f32, H as f32), |
| 55 | + &ConversionContext::default(), |
| 56 | + ); |
| 57 | + let dispatcher = LegacyPaintDispatcher::for_scene(&built); |
| 58 | + let frame = PaintFrame { |
| 59 | + time, |
| 60 | + frame_index: (time * 30.0) as u32, |
| 61 | + fps: 30, |
| 62 | + video_width: W, |
| 63 | + video_height: H, |
| 64 | + scene_duration: 2.0, |
| 65 | + camera: None, |
| 66 | + }; |
| 67 | + paint_tree(canvas, &built.root, &layout, &frame, &dispatcher); |
| 68 | +} |
| 69 | + |
| 70 | +/// Paint on a worker so a runaway loop fails the test instead of wedging the |
| 71 | +/// suite. `dot_spacing: 0` used to spin for billions of iterations; a plain |
| 72 | +/// `paint()` call would hang CI rather than report. |
| 73 | +fn paint_within(json: serde_json::Value, time: f64, budget: Duration, what: &str) { |
| 74 | + let (tx, rx) = mpsc::channel(); |
| 75 | + let worker = std::thread::spawn(move || { |
| 76 | + paint(json, time); |
| 77 | + let _ = tx.send(()); |
| 78 | + }); |
| 79 | + match rx.recv_timeout(budget) { |
| 80 | + Ok(()) => worker.join().expect(what), |
| 81 | + Err(_) => panic!("{what}: still painting after {budget:?} — runaway loop"), |
| 82 | + } |
| 83 | +} |
| 84 | + |
| 85 | +#[test] |
| 86 | +fn shape_survives_a_gradient_whose_stops_do_not_match_its_colors() { |
| 87 | + // skia asserts pos.len() == colors.len() inside the gradient shader, so a |
| 88 | + // mismatched `stops` aborted the process rather than returning an error. |
| 89 | + for gradient_type in ["linear", "radial"] { |
| 90 | + paint( |
| 91 | + serde_json::json!({ |
| 92 | + "type": "shape", |
| 93 | + "shape": "rect", |
| 94 | + "style": { "width": 200, "height": 100 }, |
| 95 | + "fill": { |
| 96 | + "type": gradient_type, |
| 97 | + "colors": ["#FF0000", "#0000FF"], |
| 98 | + "stops": [0.0, 0.5, 1.0] |
| 99 | + } |
| 100 | + }), |
| 101 | + 0.5, |
| 102 | + ); |
| 103 | + } |
| 104 | +} |
| 105 | + |
| 106 | +#[test] |
| 107 | +fn table_survives_an_empty_row_colors_list() { |
| 108 | + // `row_colors: []` deserializes to Some(vec![]), so the default palette was |
| 109 | + // never substituted and the modulo guard still indexed an empty slice. |
| 110 | + paint( |
| 111 | + serde_json::json!({ |
| 112 | + "type": "table", |
| 113 | + "headers": ["A", "B"], |
| 114 | + "rows": [["1", "2"], ["3", "4"]], |
| 115 | + "row_colors": [], |
| 116 | + "style": { "width": 300, "height": 200 } |
| 117 | + }), |
| 118 | + 0.5, |
| 119 | + ); |
| 120 | +} |
| 121 | + |
| 122 | +#[test] |
| 123 | +fn tag_cloud_survives_an_empty_colors_list() { |
| 124 | + // palette() returned the caller's empty vec, and the painter took |
| 125 | + // `index % palette.len()` on it. |
| 126 | + paint( |
| 127 | + serde_json::json!({ |
| 128 | + "type": "tag_cloud", |
| 129 | + "tags": [{ "text": "rust", "weight": 3 }, { "text": "skia", "weight": 1 }], |
| 130 | + "colors": [], |
| 131 | + "style": { "width": 300, "height": 200 } |
| 132 | + }), |
| 133 | + 0.5, |
| 134 | + ); |
| 135 | +} |
| 136 | + |
| 137 | +#[test] |
| 138 | +fn dot_map_terminates_on_a_zero_dot_spacing() { |
| 139 | + // (w - 0) / 0 is +inf, and `inf as u32` saturates to u32::MAX in Rust, so the |
| 140 | + // nested loop was scheduled for ~1.8e19 iterations. The geometry pass catches |
| 141 | + // dot_spacing: 0.01 but not 0. |
| 142 | + paint_within( |
| 143 | + serde_json::json!({ |
| 144 | + "type": "dot_map", |
| 145 | + "points": [{ "lat": 48.8, "lng": 2.3 }], |
| 146 | + "dot_spacing": 0, |
| 147 | + "style": { "width": 300, "height": 150 } |
| 148 | + }), |
| 149 | + 0.5, |
| 150 | + Duration::from_secs(10), |
| 151 | + "dot_map with dot_spacing: 0", |
| 152 | + ); |
| 153 | +} |
| 154 | + |
| 155 | +#[test] |
| 156 | +fn codeblock_diff_survives_multibyte_text() { |
| 157 | + // The edit script counts bytes while the reveal interpolates a fraction of |
| 158 | + // that count, so mid-animation offsets landed inside a multi-byte character |
| 159 | + // and `replace_range` aborted with "not a char boundary". |
| 160 | + for (from, to) in [ |
| 161 | + ("let a = 1;", "let café = «héllo→»;"), |
| 162 | + ("let x = 1;", "let y = \"éàü\";"), |
| 163 | + ("a", "日本語のテキスト"), |
| 164 | + ("ok", "🎬 clap"), |
| 165 | + ] { |
| 166 | + // Sweep the reveal: the panic only fires on the frames where progress |
| 167 | + // lands part-way through a glyph. |
| 168 | + for step in 0..=20 { |
| 169 | + paint( |
| 170 | + serde_json::json!({ |
| 171 | + "type": "codeblock", |
| 172 | + "code": from, |
| 173 | + "language": "rust", |
| 174 | + "diff": true, |
| 175 | + "states": [ |
| 176 | + { "code": from, "at": 0.0 }, |
| 177 | + { "code": to, "at": 0.4, "cursor": { "enabled": true } } |
| 178 | + ], |
| 179 | + "style": { "width": 380, "height": 120 } |
| 180 | + }), |
| 181 | + f64::from(step) * 0.1, |
| 182 | + ); |
| 183 | + } |
| 184 | + } |
| 185 | +} |
0 commit comments