91 lines
3.9 KiB
Rust
91 lines
3.9 KiB
Rust
//! Every shipped icon animation parses and evaluates at every frame.
|
|
|
|
use notescraft_lottie::Animation;
|
|
|
|
#[test]
|
|
fn every_shipped_icon_parses_and_plays() {
|
|
let dir = std::path::Path::new(env!("CARGO_MANIFEST_DIR")).join("../../assets/icons-anim");
|
|
let mut seen = 0;
|
|
let mut failures = Vec::new();
|
|
for entry in std::fs::read_dir(&dir).map(|entries| entries.flatten().collect::<Vec<_>>()).unwrap_or_default() {
|
|
let path = entry.path();
|
|
if path.extension().is_none_or(|ext| ext != "json") || path.file_name().is_some_and(|name| name == "index.json") {
|
|
continue;
|
|
}
|
|
let text = std::fs::read_to_string(&path).unwrap_or_default();
|
|
let animation = match Animation::parse(&text) {
|
|
Ok(animation) => animation,
|
|
Err(error) => {
|
|
failures.push(format!("{}: {error}", path.file_name().map(|n| n.to_string_lossy().into_owned()).unwrap_or_default()));
|
|
continue;
|
|
}
|
|
};
|
|
let (first, last) = (animation.first_frame(), animation.last_frame());
|
|
assert!(last > first, "{}: no duration", path.display());
|
|
let mut frame = first;
|
|
while frame <= last {
|
|
assert!(!animation.frame_at(frame).items.is_empty(), "{}: empty at frame {frame}", path.display());
|
|
frame += 1.0;
|
|
}
|
|
seen += 1;
|
|
}
|
|
assert!(failures.is_empty(), "icons that do not parse:\n{}", failures.join("\n"));
|
|
assert!(seen >= 60, "icons found: {seen}");
|
|
}
|
|
|
|
fn shipped(name: &str) -> Animation {
|
|
let path = std::path::Path::new(env!("CARGO_MANIFEST_DIR")).join(format!("../../assets/icons-anim/{name}.json"));
|
|
Animation::parse(&std::fs::read_to_string(path).expect("asset exists")).expect("asset parses")
|
|
}
|
|
|
|
fn points(frame: ¬escraft_lottie::Frame) -> Vec<[f32; 2]> {
|
|
frame
|
|
.items
|
|
.iter()
|
|
.flat_map(|item| match item {
|
|
notescraft_lottie::Item::Stroke(stroke) => &stroke.polylines,
|
|
notescraft_lottie::Item::Fill(fill) => &fill.polylines,
|
|
})
|
|
.flat_map(|line| line.points.iter().copied())
|
|
.collect()
|
|
}
|
|
|
|
#[test]
|
|
fn lock_and_panels_include_their_closed_body_contours() {
|
|
for name in ["lock", "panel-left", "panel-right"] {
|
|
let frame = shipped(name).frame_at(0.0);
|
|
assert!(
|
|
frame.items.iter().any(|item| matches!(item,
|
|
notescraft_lottie::Item::Stroke(stroke) if stroke.polylines.iter().any(|line| line.closed)
|
|
)),
|
|
"{name} lacks its rounded rectangle"
|
|
);
|
|
}
|
|
}
|
|
|
|
#[test]
|
|
fn every_icon_has_visible_motion_and_oneshots_return_to_rest() {
|
|
let path = std::path::Path::new(env!("CARGO_MANIFEST_DIR")).join("../../assets/icons-anim/index.json");
|
|
let index: serde_json::Value = serde_json::from_str(&std::fs::read_to_string(path).expect("index")).expect("JSON");
|
|
for kind in ["toggle", "oneshot"] {
|
|
for name in index[kind].as_array().expect("names").iter().filter_map(serde_json::Value::as_str) {
|
|
let animation = shipped(name);
|
|
let first = animation.frame_at(0.0);
|
|
let initial = points(&first);
|
|
let moved = (1_u16..=8).any(|step| {
|
|
let frame = animation.frame_at(animation.last_frame() * f32::from(step) / 8.0);
|
|
let sample = points(&frame);
|
|
sample.len() != initial.len()
|
|
|| frame.items.len() != first.items.len()
|
|
|| sample.iter().zip(&initial).any(|([x, y], [ix, iy])| (x - ix).hypot(y - iy) >= 0.5)
|
|
});
|
|
assert!(moved, "{name} has no visible motion");
|
|
if kind == "oneshot" {
|
|
let final_points = points(&animation.frame_at(animation.last_frame()));
|
|
assert_eq!(initial.len(), final_points.len(), "{name} endpoint topology");
|
|
assert!(initial.iter().zip(final_points).all(|([x, y], [fx, fy])| (x - fx).hypot(y - fy) < 0.001), "{name} fails to return to rest");
|
|
}
|
|
}
|
|
}
|
|
}
|