Files
vectorcraft/crates/engine/src/tests/widthprofiles.rs
T
dotCooCoo 82dd6df8b6 Width profiles: deleting a profile clears an order that is back to the default
Deleting a saved profile kept only the stored ids, so a profile saved after
the last move was missing from the order passed to set_profile_order and the
default order stayed stored. Delete now builds the whole list as it shows and
stores it only when it differs from the default.
2026-12-05 14:36:00 -08:00

269 lines
14 KiB
Rust

//! The width profile library (`stroke.widthProfile.*`): saved profiles kept with the preferences
//! next to the built-in ones, applied by name with `stroke.set {profile}`.
use serde_json::{Value, json};
use vectorcraft_doc::WidthProfile;
use crate::*;
fn session_with_line() -> (Session, NodeId) {
let mut s = Session::new();
s.execute("file.new", &json!({"width": 200, "height": 200})).unwrap();
let r = s.execute("shape.line", &json!({"x1": 10, "y1": 100, "x2": 150, "y2": 100})).unwrap();
(s, NodeId(r["id"].as_u64().unwrap()))
}
fn profile(s: &Session, id: NodeId) -> Option<WidthProfile> {
s.doc().unwrap().doc.node(id).unwrap().appearance.stroke().unwrap().profile.clone()
}
fn custom() -> WidthProfile {
WidthProfile { points: vec![(0.0, 0.2, 0.2), (0.3, 1.6, 0.8), (1.0, 0.5, 0.5)] }
}
/// Give the line `p` as its variable width (as the Width tool would).
fn set_profile(s: &mut Session, id: NodeId, p: WidthProfile) {
s.edit("Width", |d, _| {
d.node_mut(id).unwrap().appearance.stroke_mut().unwrap().profile = Some(p);
Ok(())
})
.unwrap();
}
fn ids(list: &Value) -> Vec<&str> {
list["profiles"].as_array().unwrap().iter().map(|p| p["id"].as_str().unwrap()).collect()
}
#[test]
fn add_list_apply_delete_reset() {
let (mut s, id) = session_with_line();
let built_in: Vec<&str> = WidthProfile::PRESETS.iter().map(|p| p.id).collect();
let list = s.execute("stroke.widthProfile.list", &json!({})).unwrap();
assert_eq!(ids(&list), built_in);
assert_eq!(list["current"], "uniform");
// Nothing to save from a plain stroke, or from a built-in profile.
assert!(s.execute("stroke.widthProfile.add", &json!({})).is_err());
s.execute("stroke.set", &json!({"profile": "lens"})).unwrap();
assert!(s.execute("stroke.widthProfile.add", &json!({})).is_err());
set_profile(&mut s, id, custom());
assert_eq!(s.execute("stroke.widthProfile.list", &json!({})).unwrap()["current"], "custom");
let undo_depth = s.doc().unwrap().history.undo.len();
assert_eq!(s.execute("stroke.widthProfile.add", &json!({})).unwrap()["name"], "Width Profile 1");
assert_eq!(s.doc().unwrap().history.undo.len(), undo_depth, "the library is not part of the document");
// Listed after the built-ins, and now the stroke's profile.
let list = s.execute("stroke.widthProfile.list", &json!({})).unwrap();
assert_eq!(ids(&list).last(), Some(&"Width Profile 1"));
assert_eq!(list["current"], "Width Profile 1");
let row = list["profiles"].as_array().unwrap().last().unwrap().clone();
assert_eq!((row["builtIn"].clone(), row["points"][1].clone()), (json!(false), json!([0.3, 1.6, 0.8])));
// The same profile can't be saved twice.
assert!(s.execute("stroke.widthProfile.add", &json!({"name": "Again"})).is_err());
// Apply it by name to another stroke.
let other = NodeId(s.execute("shape.line", &json!({"x1": 10, "y1": 150, "x2": 150, "y2": 150})).unwrap()["id"].as_u64().unwrap());
assert!(profile(&s, other).is_none());
s.execute("stroke.set", &json!({"profile": "Width Profile 1"})).unwrap();
assert_eq!(profile(&s, other), Some(custom()));
assert!(s.execute("stroke.set", &json!({"profile": "Nope"})).is_err());
// Delete it (the selected stroke's): strokes keep their widths.
assert_eq!(s.execute("stroke.widthProfile.delete", &json!({})).unwrap()["deleted"], "Width Profile 1");
assert_eq!(ids(&s.execute("stroke.widthProfile.list", &json!({})).unwrap()), built_in);
assert_eq!(profile(&s, other), Some(custom()));
assert!(s.execute("stroke.set", &json!({"profile": "Width Profile 1"})).is_err());
// Reset removes every saved profile.
s.execute("stroke.widthProfile.add", &json!({"name": " Ribbon "})).unwrap();
set_profile(&mut s, other, WidthProfile { points: vec![(0.0, 1.0, 0.0), (1.0, 0.0, 1.0)] });
s.execute("stroke.widthProfile.add", &json!({"name": "Twist"})).unwrap();
assert_eq!(s.prefs.width_profiles.iter().map(|p| p.name.as_str()).collect::<Vec<_>>(), ["Ribbon", "Twist"]);
assert_eq!(s.execute("stroke.widthProfile.reset", &json!({})).unwrap()["removed"], 2);
assert!(s.prefs.width_profiles.is_empty());
assert!(s.execute("stroke.widthProfile.reset", &json!({})).is_err(), "nothing left to reset");
}
#[test]
fn built_ins_cant_be_deleted_and_names_stay_unique() {
let (mut s, id) = session_with_line();
set_profile(&mut s, id, custom());
for taken in ["lens", "Taper Start", "UNIFORM"] {
assert!(s.execute("stroke.widthProfile.add", &json!({"name": taken})).is_err(), "{taken}");
}
assert!(s.execute("stroke.widthProfile.add", &json!({"name": " "})).is_err());
s.execute("stroke.widthProfile.add", &json!({"name": "Mine"})).unwrap();
for b in ["lens", "Lens", "uniform", "wave"] {
let e = s.execute("stroke.widthProfile.delete", &json!({"name": b})).unwrap_err().to_string();
assert!(e.contains("built in"), "{b}: {e}");
}
// With a built-in profile selected, Delete has nothing to delete.
s.execute("stroke.set", &json!({"profile": "pinch"})).unwrap();
assert!(s.execute("stroke.widthProfile.delete", &json!({})).is_err());
assert!(s.execute("stroke.widthProfile.delete", &json!({"name": "Other"})).is_err());
assert_eq!(s.next_profile_name(), "Width Profile 1");
assert_eq!(s.execute("stroke.widthProfile.delete", &json!({"name": "Mine"})).unwrap()["deleted"], "Mine");
}
#[test]
fn saved_profiles_round_trip_with_the_preferences() {
let (mut s, id) = session_with_line();
set_profile(&mut s, id, custom());
s.execute("stroke.widthProfile.add", &json!({"name": "Mine"})).unwrap();
let json = s.prefs.to_json();
assert_eq!(json["widthProfiles"][0]["name"], "Mine");
let back: Prefs = serde_json::from_value(json).unwrap();
assert_eq!(back, s.prefs);
// Without saved profiles nothing is written, and older preference files load with none.
assert!(Prefs::default().to_json().get("widthProfiles").is_none());
let old: Prefs = serde_json::from_value(json!({"keyboardIncrement": 2})).unwrap();
assert!(old.width_profiles.is_empty());
// Setting and resetting other preferences keeps them.
s.execute("prefs.set", &json!({"key": "keyboardIncrement", "value": 3})).unwrap();
s.execute("prefs.reset", &json!({})).unwrap();
assert_eq!(s.prefs.width_profiles.len(), 1);
// A restarted session (preferences applied from the saved file) lists them again.
let mut fresh = Session::new();
fresh.apply_prefs(back);
assert!(fresh.resolve_profile("Mine").is_some_and(|p| p == Some(custom())));
}
fn listed(s: &mut Session) -> Vec<String> {
ids(&s.execute("stroke.widthProfile.list", &json!({})).unwrap()).into_iter().map(str::to_owned).collect()
}
/// A line with two saved profiles, "Alpha" and "Beta", after the built-ins.
fn session_with_saved() -> Session {
let (mut s, id) = session_with_line();
set_profile(&mut s, id, custom());
s.execute("stroke.widthProfile.add", &json!({"name": "Alpha"})).unwrap();
set_profile(&mut s, id, WidthProfile { points: vec![(0.0, 1.0, 0.0), (1.0, 0.0, 1.0)] });
s.execute("stroke.widthProfile.add", &json!({"name": "Beta"})).unwrap();
s
}
#[test]
fn move_reorders_built_in_and_saved_profiles_below_uniform() {
let mut s = session_with_saved();
let undo_depth = s.doc().unwrap().history.undo.len();
// A built-in named by its label, and a saved profile by its name.
assert_eq!(s.execute("stroke.widthProfile.move", &json!({"name": "Wave", "to": 0})).unwrap()["order"][0], "wave");
s.execute("stroke.widthProfile.move", &json!({"name": "Beta", "to": 1})).unwrap();
s.execute("stroke.widthProfile.move", &json!({"name": "taperEnd", "to": 7})).unwrap();
assert_eq!(listed(&mut s), ["uniform", "wave", "Beta", "lens", "taperStart", "pinch", "teardrop", "Alpha", "taperEnd"]);
assert_eq!(s.doc().unwrap().history.undo.len(), undo_depth, "the order is kept with the preferences, not the document");
// The Stroke panel's rows follow it, and a moved profile still applies by its id.
assert_eq!(s.profile_entries().nth(2).map(|e| e.id), Some("Beta"));
s.execute("stroke.set", &json!({"profile": "wave"})).unwrap();
assert_eq!(s.execute("stroke.widthProfile.list", &json!({})).unwrap()["current"], "wave");
// Moving a profile to the place it has leaves the list as it is.
let before = s.prefs.width_profile_order.clone();
assert_eq!(s.execute("stroke.widthProfile.move", &json!({"name": "Beta", "to": 1})).unwrap()["order"][1], "Beta");
assert_eq!(s.prefs.width_profile_order, before);
// Moved back into the first order, the list stores no order.
s.execute("stroke.widthProfile.move", &json!({"name": "wave", "to": 5})).unwrap();
s.execute("stroke.widthProfile.move", &json!({"name": "taperEnd", "to": 3})).unwrap();
s.execute("stroke.widthProfile.move", &json!({"name": "Beta", "to": 7})).unwrap();
assert_eq!(listed(&mut s), ["uniform", "lens", "taperStart", "taperEnd", "pinch", "teardrop", "wave", "Alpha", "Beta"]);
assert!(s.prefs.width_profile_order.is_empty());
}
#[test]
fn move_keeps_uniform_first_and_refuses_bad_params() {
let mut s = session_with_saved();
for p in [
json!({"name": "uniform", "to": 1}),
json!({"name": "Uniform", "to": 0}),
json!({"name": "Nope", "to": 0}),
json!({"name": "Alpha", "to": 8}),
json!({"name": "Alpha", "to": -1}),
json!({"name": "Alpha", "to": 1.5}),
json!({"name": "Alpha", "to": "1"}),
json!({"name": "Alpha"}),
json!({"name": 3, "to": 0}),
json!({"to": 0}),
] {
assert!(s.execute("stroke.widthProfile.move", &p).is_err(), "{p}");
}
let past_the_end = s.execute("stroke.widthProfile.move", &json!({"name": "Alpha", "to": 8})).unwrap_err().to_string();
assert!(past_the_end.contains("must be less than 8"), "{past_the_end}");
assert!(s.prefs.width_profile_order.is_empty(), "a refused move changes nothing");
assert_eq!(listed(&mut s)[0], "uniform");
// The last place is the last row.
s.execute("stroke.widthProfile.move", &json!({"name": "lens", "to": 7})).unwrap();
assert_eq!(listed(&mut s).last().map(String::as_str), Some("lens"));
}
#[test]
fn the_order_survives_the_preferences_and_resets_with_the_profiles() {
let mut s = session_with_saved();
assert_eq!(s.execute("stroke.widthProfile.reset", &json!({})).unwrap()["removed"], 2);
// With only built-ins, a changed order is still something Reset Profiles puts back.
assert!(s.execute("stroke.widthProfile.reset", &json!({})).is_err());
s.execute("stroke.widthProfile.move", &json!({"name": "pinch", "to": 0})).unwrap();
let order = listed(&mut s);
assert_eq!(order[1], "pinch");
// It is written with the preferences, read back by a new session, and kept by Reset Preferences.
let json = s.prefs.to_json();
assert_eq!(json["widthProfileOrder"][0], "pinch");
let mut fresh = Session::new();
fresh.apply_prefs(serde_json::from_value(json).unwrap());
assert_eq!(listed(&mut fresh), order);
s.execute("prefs.reset", &json!({})).unwrap();
assert_eq!(listed(&mut s), order);
assert!(Prefs::default().to_json().get("widthProfileOrder").is_none());
assert_eq!(s.execute("stroke.widthProfile.reset", &json!({})).unwrap()["removed"], 0);
assert_eq!(listed(&mut s), WidthProfile::PRESETS.iter().map(|p| p.id).collect::<Vec<_>>());
}
#[test]
fn a_deleted_profile_leaves_the_order_and_a_new_one_goes_last() {
let (mut s, id) = session_with_line();
set_profile(&mut s, id, custom());
s.execute("stroke.widthProfile.add", &json!({"name": "Mine"})).unwrap();
s.execute("stroke.widthProfile.move", &json!({"name": "Mine", "to": 0})).unwrap();
s.execute("stroke.widthProfile.move", &json!({"name": "wave", "to": 1})).unwrap();
s.execute("stroke.widthProfile.delete", &json!({"name": "Mine"})).unwrap();
assert!(!s.prefs.width_profile_order.iter().any(|id| id == "Mine"));
// Saved again under the same name, it goes to the end instead of back to the top.
s.execute("stroke.widthProfile.add", &json!({"name": "Mine"})).unwrap();
assert_eq!(listed(&mut s), ["uniform", "wave", "lens", "taperStart", "taperEnd", "pinch", "teardrop", "Mine"]);
}
#[test]
fn deleting_the_only_moved_profile_clears_the_stored_order() {
let (mut s, id) = session_with_line();
set_profile(&mut s, id, custom());
s.execute("stroke.widthProfile.add", &json!({"name": "Alpha"})).unwrap();
s.execute("stroke.widthProfile.move", &json!({"name": "Alpha", "to": 0})).unwrap();
// Beta is saved after the move, so the stored order does not name it.
set_profile(&mut s, id, WidthProfile { points: vec![(0.0, 1.0, 0.0), (1.0, 0.0, 1.0)] });
s.execute("stroke.widthProfile.add", &json!({"name": "Beta"})).unwrap();
s.execute("stroke.widthProfile.delete", &json!({"name": "Alpha"})).unwrap();
assert!(s.prefs.width_profile_order.is_empty(), "{:?}", s.prefs.width_profile_order);
assert_eq!(listed(&mut s).last().map(String::as_str), Some("Beta"));
// A real custom order is kept after a delete.
s.execute("stroke.widthProfile.move", &json!({"name": "Beta", "to": 0})).unwrap();
set_profile(&mut s, id, WidthProfile { points: vec![(0.0, 0.5, 0.5), (1.0, 1.0, 1.0)] });
s.execute("stroke.widthProfile.add", &json!({"name": "Gamma"})).unwrap();
s.execute("stroke.widthProfile.delete", &json!({"name": "Gamma"})).unwrap();
assert_eq!(listed(&mut s)[1], "Beta");
assert!(!s.prefs.width_profile_order.is_empty());
}
#[test]
fn every_built_in_applies_and_the_params_doc_names_them() {
let (mut s, id) = session_with_line();
let spec = command_specs().iter().find(|c| c.id == "stroke.set").unwrap();
for p in WidthProfile::PRESETS {
assert!(spec.params.contains(&format!("\"{}\"", p.id)), "{}", p.id);
s.execute("stroke.set", &json!({"profile": p.id})).unwrap();
assert_eq!(s.execute("stroke.widthProfile.list", &json!({})).unwrap()["current"], p.id);
assert_eq!(profile(&s, id).map(|w| w.points), (p.id != "uniform").then(|| p.points.to_vec()));
}
}