Skip to content

VectorCraft: An open-source, vector art tool written by scratch in pure Rust

42.0 KB
Raw Blame
use super::*;
use vectorcraft_engine::Session;

fn app() -> VectorcraftApp {
    let mut app = VectorcraftApp::new(Session::new(), Default::default());
    app.run("file.new", json!({"width": 300, "height": 200})).unwrap();
    app
}

fn set(app: &mut VectorcraftApp, fields: Value) {
    let d = app.ui.dialog.as_mut().unwrap();
    for (k, v) in fields.as_object().unwrap() {
        d.fields.insert(k.clone(), v.clone());
    }
}

fn shown(app: &mut VectorcraftApp) -> String {
    crate::tests_labels::painted_text(app, |app, ui| super::super::show(app, ui.ctx()))
}

#[test]
fn calligraphic_options_show_each_value_with_its_variation_and_ok_applies_them() {
    let mut app = app();
    app.run("ui.brushOptions", json!({"name": "6 pt. Flat"})).unwrap();
    let text = shown(&mut app);
    for label in ["Calligraphic Brush Options", "Name:", "Angle:", "Roundness:", "Size:", "Variation:", "Fixed", "OK", "Cancel"] {
        assert!(text.contains(label), "{label} in {text}");
    }
    let d = app.ui.dialog.as_ref().unwrap();
    assert_eq!((d.f64("angle", 0.0), d.f64("roundness", 0.0), d.f64("size", 0.0)), (45.0, 15.0, 6.0));
    // Out-of-range values are kept within their ranges.
    set(&mut app, json!({"angleMode": "random", "angleVariation": 500, "roundness": 140, "sizeMode": "pressure", "sizeVariation": 4}));
    assert!(shown(&mut app).contains("Pressure"));
    super::super::confirm(&mut app).unwrap();
    let def = app.run("brush.get", json!({"name": "6 pt. Flat"})).unwrap();
    assert_eq!(def["modes"], json!(["random", "fixed", "pressure"]));
    assert_eq!((def["variation"].clone(), def["roundness"].clone()), (json!([180.0, 0.0, 4.0]), json!(100.0)));
    // Opened again, it shows what was set.
    app.run("ui.brushOptions", json!({"name": "6 pt. Flat"})).unwrap();
    let d = app.ui.dialog.as_ref().unwrap();
    assert_eq!((d.str("angleMode"), d.f64("sizeVariation", 0.0)), ("random".into(), 4.0));
}

/// #764: Calligraphic and Scatter Brush Options show Tilt, Bearing and Rotation, and OK sets
/// them.
#[test]
fn brush_options_set_tilt_bearing_and_rotation() {
    let mut app = app();
    app.run("ui.brushOptions", json!({"name": "6 pt. Flat"})).unwrap();
    set(&mut app, json!({"angleMode": "bearing", "roundnessMode": "tilt", "sizeMode": "rotation"}));
    let text = shown(&mut app);
    for label in ["Bearing", "Tilt", "Rotation"] {
        assert!(text.contains(label), "{label} in {text}");
    }
    super::super::confirm(&mut app).unwrap();
    let def = app.run("brush.get", json!({"name": "6 pt. Flat"})).unwrap();
    assert_eq!(def["modes"], json!(["bearing", "tilt", "rotation"]));
    app.run("ui.brushOptions", json!({"name": "Confetti"})).unwrap();
    set(&mut app, json!({"sizeMode": "tilt", "spacingMode": "bearing", "scatterMode": "rotation", "rotationMode": "bearing"}));
    let text = shown(&mut app);
    for label in ["Tilt", "Bearing"] {
        assert!(text.contains(label), "{label} in {text}");
    }
    super::super::confirm(&mut app).unwrap();
    let def = app.run("brush.get", json!({"name": "Confetti"})).unwrap();
    assert_eq!(def["modes"], json!(["tilt", "bearing", "rotation", "bearing"]));
}

#[test]
fn a_taken_name_keeps_the_dialog_open() {
    let mut app = app();
    app.run("ui.brushOptions", json!({"name": "6 pt. Flat"})).unwrap();
    set(&mut app, json!({"name": "3 pt. Round"}));
    assert!(super::super::confirm(&mut app).is_err());
    assert!(app.ui.dialog.is_some());
}

#[test]
fn bristle_options_and_an_unknown_brush() {
    let mut app = app();
    app.run("ui.brushOptions", json!({"name": "Bristle Round"})).unwrap();
    let text = shown(&mut app);
    for label in ["Bristle Brush Options", "Shape:", "Bristle Length:", "Stiffness:", "Paint Opacity:"] {
        assert!(text.contains(label), "{label} in {text}");
    }
    set(&mut app, json!({"shape": "flatFan", "density": 0, "length": 120}));
    super::super::confirm(&mut app).unwrap();
    let def = app.run("brush.get", json!({"name": "Bristle Round"})).unwrap();
    assert_eq!((def["shape"].clone(), def["density"].clone(), def["length"].clone()), (json!("flatFan"), json!(1.0), json!(120.0)));
    // No such brush: an error, nothing opens.
    app.ui.dialog = None;
    assert!(app.run("ui.brushOptions", json!({"name": "Nope"})).is_err());
    assert!(app.ui.dialog.is_none());
}

/// Bristle Brush Options' Name field takes at most 31 characters as they are typed; the
/// Calligraphic Brush Options' Name field takes more.
#[test]
fn bristle_brush_options_name_field_takes_at_most_31_characters() {
    let mut app = app();
    let typed = "é".repeat(40);
    for (brush, kept) in [("Bristle Round", 31), ("6 pt. Flat", 40)] {
        let ctx = egui::Context::default();
        crate::theme::install_fonts(&ctx);
        let frame = |app: &mut VectorcraftApp, events: Vec<egui::Event>| {
            let mut out = ctx.run_ui(egui::RawInput { events, ..Default::default() }, |ui| super::super::show(app, ui.ctx()));
            out.textures_delta.clear();
        };
        app.run("ui.brushOptions", json!({ "name": brush })).unwrap();
        // The Name field takes the focus with its text selected; typing replaces it.
        frame(&mut app, vec![]);
        frame(&mut app, vec![]);
        frame(&mut app, vec![egui::Event::Text(typed.clone())]);
        assert_eq!(app.ui.dialog.as_ref().unwrap().str("name"), "é".repeat(kept), "{brush}");
        super::super::confirm(&mut app).unwrap();
        assert!(app.run("brush.get", json!({ "name": "é".repeat(kept) })).is_ok(), "{brush}");
    }
    // A new name of more than 31 characters in the field (a longer saved name with some of its
    // characters deleted) is refused, and the dialog stays open.
    app.run("ui.brushOptions", json!({ "name": "é".repeat(31) })).unwrap();
    set(&mut app, json!({ "name": "é".repeat(35) }));
    assert!(super::super::confirm(&mut app).is_err());
    assert!(app.ui.dialog.is_some());
}

/// A path painted with brush `name`, and the height of the art its stroke draws.
fn brushed(app: &mut VectorcraftApp, name: &str) -> (vectorcraft_doc::NodeId, f64) {
    let id = app.run("shape.line", json!({"x1": 20, "y1": 100, "x2": 280, "y2": 100})).unwrap()["id"].as_u64().unwrap();
    let id = vectorcraft_doc::NodeId(id);
    app.run("brush.apply", json!({ "name": name, "ids": [id.0] })).unwrap();
    (id, drawn_height(app, id))
}

fn drawn_height(app: &VectorcraftApp, id: vectorcraft_doc::NodeId) -> f64 {
    let st = app.session.active().unwrap();
    let pieces = vectorcraft_brush::node_pieces(&st.doc, st.doc.node(id).unwrap()).unwrap();
    vectorcraft_brush::pieces_bounds(&pieces).unwrap().height()
}

#[test]
fn scatter_options_show_each_value_with_its_mode_and_ok_applies_them() {
    let mut app = app();
    let (id, before) = brushed(&mut app, "Confetti");
    app.run("ui.brushOptions", json!({"name": "Confetti"})).unwrap();
    let text = shown(&mut app);
    for label in [
        "Scatter Brush Options",
        "Name:",
        "Size:",
        "Spacing:",
        "Scatter:",
        "Rotation:",
        "Random",
        "Rotation relative to:",
        "Page",
        "Colorization",
        "Method:",
        "Tints",
        "Key Color:",
    ] {
        assert!(text.contains(label), "{label} in {text}");
    }
    let d = app.ui.dialog.as_ref().unwrap();
    assert_eq!((d.f64("sizeMin", 0.0), d.f64("sizeMax", 0.0), d.str("sizeMode")), (60.0, 140.0, "random".into()));
    assert_eq!((d.str("rotationRelativeTo"), d.str("colorization")), ("page".into(), "tints".into()));
    // Values are kept within their ranges (rotation here); Fixed takes the first value.
    set(
        &mut app,
        json!({"sizeMode": "fixed", "sizeMin": 5000, "spacingMode": "pressure", "rotationMin": -400, "rotationRelativeTo": "path", "colorization": "hueShift"}),
    );
    assert!(shown(&mut app).contains("Hue Shift"));
    super::super::confirm(&mut app).unwrap();
    assert!(app.ui.dialog.is_none());
    let def = app.run("brush.get", json!({"name": "Confetti"})).unwrap();
    assert_eq!(
        (&def["size"], &def["rotation"][0], &def["modes"]),
        (&json!([5000.0, 140.0]), &json!(-180.0), &json!(["fixed", "pressure", "random", "random"]))
    );
    assert_eq!((&def["rotation_relative_to_path"], &def["colorization"]["method"]), (&json!(true), &json!("hueShift")));
    assert!(def["colorization"]["key"].is_object(), "the art's colour as the key: {def}");
    assert!(drawn_height(&app, id) > before, "the stroke redraws with the bigger confetti");
    // Opened again, it shows what was set.
    app.run("ui.brushOptions", json!({"name": "Confetti"})).unwrap();
    let d = app.ui.dialog.as_ref().unwrap();
    assert_eq!((d.str("sizeMode"), d.str("spacingMode"), d.str("colorization")), ("fixed".into(), "pressure".into(), "hueShift".into()));
}

#[test]
fn brush_key_color_eyedropper_updates_the_open_dialog() {
    use vectorcraft_tools::{PointerEvent, PointerKind};
    let mut app = app();
    app.run("paint.setFill", json!({"color": "#336699"})).unwrap();
    app.run("shape.rectangle", json!({"x": 40, "y": 40, "width": 80, "height": 60})).unwrap();
    app.run("ui.brushOptions", json!({"name": "Confetti"})).unwrap();
    set(&mut app, json!({"colorization": "hueShift"}));
    let back = app.session.tool_id();
    app.select_tool("eyedropper");
    app.session.set_tool_option("brushKeyColor", &json!(true));
    app.session.set_tool_option("stop", &json!(back));
    let view = app.view_info();
    crate::canvas::dispatch(&mut app, &PointerEvent::new(PointerKind::Down, 80.0, 70.0), view);
    let key = app.ui.dialog.as_ref().unwrap().fields.get("keyColor").cloned().unwrap();
    assert_eq!(vectorcraft_engine::cmd::color_value(&key).unwrap().to_hex(), "#336699");
    assert_eq!(app.session.tool_id(), back);
}

#[test]
fn art_options_set_width_scale_direction_flips_and_colorization() {
    let mut app = app();
    let (id, before) = brushed(&mut app, "Arrow");
    app.run("ui.brushOptions", json!({"name": "Arrow"})).unwrap();
    let text = shown(&mut app);
    for label in [
        "Art Brush Options",
        "Width:",
        "Brush Scale Options",
        "Scale Proportionately",
        "Stretch to Fit Stroke Length",
        "Stretch Between Guides",
        "Start:",
        "End:",
        "Direction:",
        "Flip Along",
        "Flip Across",
        "Colorization",
    ] {
        assert!(text.contains(label), "{label} in {text}");
    }
    let d = app.ui.dialog.as_ref().unwrap();
    assert_eq!(
        (d.f64("width", 0.0), d.str("scaleMode"), d.f64("guideEnd", 0.0), d.str("direction")),
        (100.0, "betweenGuides".into(), 78.0, "leftToRight".into())
    );
    set(&mut app, json!({"width": 250, "guideStart": 10, "guideEnd": 140, "direction": "bottomToTop", "flipAcross": true, "colorization": "none"}));
    super::super::confirm(&mut app).unwrap();
    let def = app.run("brush.get", json!({"name": "Arrow"})).unwrap();
    assert_eq!(
        (&def["width"], &def["scale"], &def["direction"]),
        (&json!(250.0), &json!({"mode": "betweenGuides", "start": 0.1, "end": 1.0}), &json!("bottomToTop"))
    );
    assert_eq!((&def["flip_across"], &def["flip_along"], &def["colorization"]), (&json!(true), &json!(false), &json!({"method": "none"})));
    assert!(drawn_height(&app, id) != before, "the stroke redraws");
    // Stretch to Fit drops the guides.
    app.run("ui.brushOptions", json!({"name": "Arrow"})).unwrap();
    set(&mut app, json!({"scaleMode": "stretch"}));
    super::super::confirm(&mut app).unwrap();
    assert_eq!(app.run("brush.get", json!({"name": "Arrow"})).unwrap()["scale"], json!({"mode": "stretch"}));
}

/// Art Brush Options' Width menu: Pressure takes a second width for the heaviest pen pressure,
/// and a stroke drawn without pressure is half way between the two; Fixed drops the second width.
#[test]
fn art_options_width_follows_the_pen_pressure() {
    let mut app = app();
    let (id, before) = brushed(&mut app, "Tapered Stroke");
    app.run("ui.brushOptions", json!({"name": "Tapered Stroke"})).unwrap();
    let d = app.ui.dialog.as_ref().unwrap();
    assert_eq!((d.str("widthMode"), d.f64("widthMax", 0.0)), ("fixed".into(), 100.0));
    assert!(shown(&mut app).contains("Fixed"));
    set(&mut app, json!({"widthMode": "pressure", "widthMax": 300}));
    assert!(shown(&mut app).contains("Pressure"));
    super::super::confirm(&mut app).unwrap();
    let def = app.run("brush.get", json!({"name": "Tapered Stroke"})).unwrap();
    assert_eq!((&def["width"], &def["width_mode"], &def["width_max"]), (&json!(100.0), &json!("pressure"), &json!(300.0)));
    let after = drawn_height(&app, id);
    assert!((after - 2.0 * before).abs() < 0.01 * before, "200 % half way: {before} then {after}");
    // Opened again, it shows what was set; Fixed drops the second width.
    app.run("ui.brushOptions", json!({"name": "Tapered Stroke"})).unwrap();
    let d = app.ui.dialog.as_ref().unwrap();
    assert_eq!((d.str("widthMode"), d.f64("widthMax", 0.0)), ("pressure".into(), 300.0));
    set(&mut app, json!({"widthMode": "fixed"}));
    super::super::confirm(&mut app).unwrap();
    let def = app.run("brush.get", json!({"name": "Tapered Stroke"})).unwrap();
    assert!(def.get("width_mode").is_none() && def.get("width_max").is_none(), "{def}");
    assert!((drawn_height(&app, id) - before).abs() < 1e-6);
}

/// A context with the app's fonts and theme, for [`frame`].
fn ctx() -> egui::Context {
    let ctx = egui::Context::default();
    crate::theme::install_fonts(&ctx);
    crate::theme::apply(&ctx, Default::default());
    ctx
}

/// Runs one 1200 × 900 frame with `events` through the app's key handling and the dialogs, and
/// returns the center of the text `label` if the frame shows it.
fn frame(ctx: &egui::Context, app: &mut VectorcraftApp, events: Vec<egui::Event>, label: &str) -> Option<egui::Pos2> {
    use egui::{Pos2, Rect};
    let raw = egui::RawInput { screen_rect: Some(Rect::from_min_size(Pos2::ZERO, vec2(1200.0, 900.0))), events, ..Default::default() };
    let mut out = ctx.run_ui(raw, |ui| {
        crate::shortcuts::handle(app, ui.ctx());
        super::super::show(app, ui.ctx());
    });
    out.textures_delta.clear();
    out.shapes.iter().find_map(|s| match &s.shape {
        egui::Shape::Text(t) if t.galley.text() == label => Some(egui::Rect::from_min_size(t.pos, t.galley.size()).center()),
        _ => None,
    })
}

/// Clicks the text `label` the open dialog shows.
fn click_text(ctx: &egui::Context, app: &mut VectorcraftApp, label: &str) {
    use egui::{Event, PointerButton};
    frame(ctx, app, vec![], label);
    let at = frame(ctx, app, vec![], label).unwrap_or_else(|| panic!("no {label:?} shown"));
    let button = |pressed| Event::PointerButton { pos: at, button: PointerButton::Primary, pressed, modifiers: Default::default() };
    frame(ctx, app, vec![Event::PointerMoved(at), button(true)], label);
    frame(ctx, app, vec![button(false)], label);
    frame(ctx, app, vec![], label);
}

/// Presses Escape.
fn escape(ctx: &egui::Context, app: &mut VectorcraftApp) {
    let key = egui::Event::Key { key: egui::Key::Escape, physical_key: None, pressed: true, repeat: false, modifiers: Default::default() };
    frame(ctx, app, vec![key], "");
    frame(ctx, app, vec![], "");
}

/// Scatter, Art and Pattern Brush Options have a Tips button beside Method. A click on it opens
/// Colorization Tips, which names each method and what it does beside a sample of the brush,
/// and a second click closes it. Screen readers name the button, the popover and each sample.
#[test]
fn tips_names_each_colorization_method_beside_a_sample() {
    use crate::widgets::accessibility_tests::{role_and_state, tree};
    use egui::accesskit::Role;
    let mut app = app();
    for name in ["Confetti", "Arrow", "Chain"] {
        app.run("ui.brushOptions", json!({ "name": name })).unwrap();
        let text = shown(&mut app);
        assert!(text.lines().any(|l| l == "Tips") && !text.contains("Colorization Tips"), "{name}: {text}");
    }
    app.run("ui.brushOptions", json!({"name": "Confetti"})).unwrap();
    let ctx = ctx();
    click_text(&ctx, &mut app, "Tips");
    assert!(app.ui.dialog.as_ref().unwrap().bool("tips"), "Tips opens Colorization Tips");
    let text = shown(&mut app);
    for label in [
        "Colorization Tips",
        "The samples show this brush on a blue stroke.",
        "None",
        "The art keeps its own colors.",
        "Tints",
        "Black becomes the stroke color and white stays white. Other colors become tints of the stroke color by their lightness.",
        "Tints and Shades",
        "Hue Shift",
        "The key color becomes the stroke color. The other colors shift their hue by the same amount and keep their saturation and brightness.",
        "Black and white stay as they are. Dark colors become shades of the stroke color and light colors become tints of it.",
    ] {
        assert!(text.contains(label), "{label} in {text}");
    }
    let nodes = tree(|ui| super::super::show(&mut app, ui.ctx()));
    assert_eq!(role_and_state(&nodes, "Tips").0, Role::Button);
    assert_eq!(role_and_state(&nodes, "Colorization Tips").0, Role::Dialog);
    for method in ["None", "Tints", "Tints and Shades", "Hue Shift"] {
        assert!(nodes.iter().any(|n| n.role() == Role::Image && n.label() == Some(method)), "a sample named {method}");
    }
    click_text(&ctx, &mut app, "Tips");
    assert!(!app.ui.dialog.as_ref().unwrap().bool("tips"), "a second click closes it");
    assert!(!shown(&mut app).contains("Colorization Tips"));
    // `ui.dialog.set {field: "tips", value: true}` opens it too.
    set(&mut app, json!({"tips": true}));
    assert!(shown(&mut app).contains("Colorization Tips"));
}

/// Escape with Colorization Tips or the Method list open closes only that: Brush Options stays
/// open with its edits, and the next Escape cancels it.
#[test]
fn escape_closes_tips_or_the_method_list_and_leaves_the_dialog_open() {
    let (mut app, ctx) = (app(), ctx());
    app.run("ui.brushOptions", json!({"name": "Confetti"})).unwrap();
    set(&mut app, json!({"colorization": "tints"}));
    click_text(&ctx, &mut app, "Tips");
    assert!(app.ui.dialog.as_ref().unwrap().bool("tips"));
    escape(&ctx, &mut app);
    let d = app.ui.dialog.as_ref().expect("Escape closes Tips, not Brush Options");
    assert!(!d.bool("tips") && d.str("colorization") == "tints");
    assert!(frame(&ctx, &mut app, vec![], "Colorization Tips").is_none());
    click_text(&ctx, &mut app, "Tints");
    assert!(egui::Popup::is_any_open(&ctx), "the Method list is open");
    escape(&ctx, &mut app);
    assert!(app.ui.dialog.as_ref().is_some_and(|d| d.str("colorization") == "tints"), "Escape closes the list, not Brush Options");
    assert!(!egui::Popup::is_any_open(&ctx));
    escape(&ctx, &mut app);
    assert!(app.ui.dialog.is_none(), "the next Escape cancels Brush Options");
    // Calligraphic Brush Options has no Tips: with `tips` set, Escape still cancels it.
    app.run("ui.brushOptions", json!({"name": "6 pt. Flat"})).unwrap();
    set(&mut app, json!({"tips": true}));
    escape(&ctx, &mut app);
    assert!(app.ui.dialog.is_none(), "Escape cancels Calligraphic Brush Options");
}

/// Each Colorization Tips sample paints the brush with its own method on the blue stroke:
/// Confetti's black and gray art keeps its colors with None, its black becomes the stroke color
/// with Tints and with Hue Shift (black is its key color), and stays black with Tints and Shades.
#[test]
fn each_tips_sample_paints_the_brush_with_its_method() {
    let mut app = app();
    app.run("ui.brushOptions", json!({"name": "Confetti"})).unwrap();
    let d = app.ui.dialog.as_ref().unwrap();
    let key: vectorcraft_color::Color = serde_json::from_value(d.fields["keyColor"].clone()).unwrap();
    let colors = |id: &str| -> Vec<String> {
        let method = vectorcraft_brush::Colorization::parse(id, key).unwrap();
        let doc = brush_art_options::tip_doc(&brush_art_options::edited_def(d), "scatter", method, 120.0, 44.0).unwrap();
        let mut hex: Vec<String> = vectorcraft_brush::art_colors(doc.layers.iter().map(|l| l.as_ref())).iter().map(|c| c.to_hex()).collect();
        hex.sort();
        hex
    };
    let (black, blue) = ("#000000".to_string(), brush_art_options::TIP_STROKE.to_hex());
    let confetti = vectorcraft_brush::defaults().iter().find(|b| b.name == "Confetti").unwrap();
    let art: Vec<String> = vectorcraft_brush::art_colors(confetti.art()).iter().map(|c| c.to_hex()).collect();
    let none = colors("none");
    assert!(none.len() == 2 && art.iter().all(|c| none.contains(c)), "None keeps the art's colors {art:?}: {none:?}");
    let (tints, shades, hue) = (colors("tints"), colors("tintsAndShades"), colors("hueShift"));
    assert!(tints.contains(&blue) && !tints.contains(&black), "{tints:?}");
    assert!(shades.contains(&black) && !shades.contains(&blue), "{shades:?}");
    assert!(hue.contains(&blue) && !hue.contains(&black), "{hue:?}");
    assert!(tints != hue && tints != shades && none != shades, "four different samples");
}

/// `ui.dialog.set` writes any field, `__def` included. With a brush definition that is not
/// an object, Brush Options and Colorization Tips still draw.
#[test]
fn a_brush_definition_that_is_not_an_object_still_draws() {
    let (mut app, ctx) = (app(), ctx());
    app.run("ui.brushOptions", json!({"name": "Confetti"})).unwrap();
    crate::tests_synthetic::control(&mut app, &ctx, "ui.dialog.set", json!({"field": "__def", "value": 5}));
    crate::tests_synthetic::control(&mut app, &ctx, "ui.dialog.set", json!({"field": "tips", "value": true}));
    let text = shown(&mut app);
    assert!(text.contains("Scatter Brush Options") && text.contains("Colorization Tips"), "{text}");
}

#[test]
fn pattern_options_show_the_tiles_and_set_scale_spacing_fit() {
    let mut app = app();
    app.run("ui.brushOptions", json!({"name": "Chain"})).unwrap();
    let text = shown(&mut app);
    for label in [
        "Pattern Brush Options",
        "Scale:",
        "Spacing:",
        "Outer Corner Tile",
        "Side Tile",
        "Inner Corner Tile",
        "Start Tile",
        "End Tile",
        "None",
        "Fit",
        "Stretch to fit",
        "Add space to fit",
        "Approximate path",
        "Flip Along",
    ] {
        assert!(text.contains(label), "{label} in {text}");
    }
    let d = app.ui.dialog.as_ref().unwrap();
    let mut tiles: Vec<&String> = d.fields["__tiles"].as_object().unwrap().keys().collect();
    tiles.sort();
    assert_eq!(tiles, ["innerCorner", "outerCorner", "side"], "Chain has side and corner tiles, no start or end");
    // Diamonds (straight-edged tiles, so sizes compare exactly).
    let (id, before) = brushed(&mut app, "Diamonds");
    app.run("ui.brushOptions", json!({"name": "Diamonds"})).unwrap();
    assert_eq!((app.ui.dialog.as_ref().unwrap().f64("spacing", 0.0), app.ui.dialog.as_ref().unwrap().str("fit")), (25.0, "addSpace".into()));
    set(&mut app, json!({"scale": 200, "spacing": 30, "fit": "approximate", "flipAlong": true}));
    super::super::confirm(&mut app).unwrap();
    let def = app.run("brush.get", json!({"name": "Diamonds"})).unwrap();
    assert_eq!((&def["scale"], &def["spacing"], &def["fit"], &def["flip_along"]), (&json!(200.0), &json!(30.0), &json!("approximate"), &json!(true)));
    assert!((drawn_height(&app, id) / before - 2.0).abs() < 1e-6, "tiles twice the size");
}

/// Each corner tile has a menu below it: Original (only when the brush has corner art), None and
/// the four automatic tiles. OK saves the choice, and an automatic tile or None replaces the art.
#[test]
fn pattern_options_set_each_corner_tile_to_original_none_or_an_automatic_tile() {
    use crate::widgets::accessibility_tests::tree;
    let mut app = app();
    app.run("ui.brushOptions", json!({"name": "Diamonds"})).unwrap();
    let d = app.ui.dialog.as_ref().unwrap();
    assert_eq!((d.str("outerCornerTile"), d.str("innerCornerTile")), ("none".into(), "none".into()), "Diamonds has no corner art");
    for corner in ["outer_corner", "inner_corner"] {
        assert!(!crate::dialogs::brush_art_options::original_available(d, corner), "{corner}: no Original without corner art");
    }
    let shown_tree = tree(|ui| super::super::show(&mut app, ui.ctx()));
    for name in ["Outer Corner Tile", "Inner Corner Tile"] {
        let roles: Vec<_> = shown_tree.iter().filter(|n| n.label() == Some(name)).map(|n| n.role()).collect();
        assert!(roles.contains(&egui::accesskit::Role::ComboBox), "a menu named {name}: {roles:?}");
    }
    for name in ["Side Tile", "Start Tile", "End Tile"] {
        assert!(shown_tree.iter().any(|n| n.label() == Some(name) && n.role() == egui::accesskit::Role::ComboBox), "a menu for {name}");
    }
    set(&mut app, json!({"outerCornerTile": "autoBetween", "innerCornerTile": "autoOverlap"}));
    let text = shown(&mut app);
    assert!(text.contains("Auto-Between") && text.contains("Auto-Overlap"), "{text}");
    super::super::confirm(&mut app).unwrap();
    let def = app.run("brush.get", json!({"name": "Diamonds"})).unwrap();
    assert_eq!((&def["outer_source"], &def["inner_source"]), (&json!("autoBetween"), &json!("autoOverlap")));
    // Chain has corner art: Original keeps it, None and an automatic tile replace it, one corner
    // at a time.
    app.run("ui.brushOptions", json!({"name": "Chain"})).unwrap();
    let d = app.ui.dialog.as_ref().unwrap();
    assert_eq!((d.str("outerCornerTile"), d.str("innerCornerTile")), ("original".into(), "original".into()));
    // Original stays in a corner's menu after another choice, and goes back to the corner art.
    let d = app.ui.dialog.as_mut().unwrap();
    crate::dialogs::brush_art_options::choose_corner(d, "outerCornerTile", "autoBetween");
    assert!(crate::dialogs::brush_art_options::original_available(d, "outer_corner"));
    crate::dialogs::brush_art_options::choose_corner(d, "outerCornerTile", "original");
    assert_eq!(d.str("outerCornerTile"), "original");
    super::super::confirm(&mut app).unwrap();
    let def = app.run("brush.get", json!({"name": "Chain"})).unwrap();
    assert!(def["outer_corner"].is_object() && def["inner_corner"].is_object() && def.get("outer_source").is_none(), "{def}");
    app.run("ui.brushOptions", json!({"name": "Chain"})).unwrap();
    set(&mut app, json!({"innerCornerTile": "none"}));
    super::super::confirm(&mut app).unwrap();
    let def = app.run("brush.get", json!({"name": "Chain"})).unwrap();
    assert!(def["outer_corner"].is_object() && def.get("inner_corner").is_none(), "{def}");
    assert_eq!(def["inner_source"], json!("none"));
    app.run("ui.brushOptions", json!({"name": "Chain"})).unwrap();
    let d = app.ui.dialog.as_ref().unwrap();
    assert_eq!((d.str("outerCornerTile"), d.str("innerCornerTile")), ("original".into(), "none".into()));
    set(&mut app, json!({"outerCornerTile": "autoSliced"}));
    super::super::confirm(&mut app).unwrap();
    let def = app.run("brush.get", json!({"name": "Chain"})).unwrap();
    assert!(def.get("outer_corner").is_none() && def.get("inner_corner").is_none(), "{def}");
    assert_eq!((&def["outer_source"], &def["inner_source"]), (&json!("autoSliced"), &json!("none")));
    app.run("ui.brushOptions", json!({"name": "Chain"})).unwrap();
    assert_eq!(app.ui.dialog.as_ref().unwrap().str("outerCornerTile"), "autoSliced");
}

#[test]
fn pattern_tile_menus_use_document_patterns_and_side_none_keeps_dialog_open() {
    use std::sync::Arc;
    use vectorcraft_doc::{Node, PatternDef};
    use vectorcraft_geom::Rect;

    let mut app = app();
    app.session
        .edit("Pattern", |doc, _| {
            let art = Node::path(
                doc.alloc_id(),
                vectorcraft_geom::shapes::rectangle(Rect::new(0.0, 0.0, 30.0, 4.0)),
                vectorcraft_doc::Appearance::default_art(),
            );
            doc.patterns.push(PatternDef { tile: Rect::new(0.0, 0.0, 10.0, 4.0), ..PatternDef::new("Wide", vec![Arc::new(art)]) });
            Ok(())
        })
        .unwrap();
    let before = app.run("brush.get", json!({"name":"Diamonds"})).unwrap();
    app.run("ui.brushOptions", json!({"name":"Diamonds"})).unwrap();
    let d = app.ui.dialog.as_ref().unwrap();
    assert_eq!(crate::dialogs::brush_art_options::pattern_names(d), vec!["Wide".to_string()]);
    for key in ["sideTile", "outerCornerTile", "innerCornerTile", "startTile", "endTile"] {
        assert!(d.fields.contains_key(key), "{key}");
    }
    for corner in [false, true] {
        let ids: Vec<_> = crate::dialogs::brush_art_options::tile_choices(d, corner).into_iter().map(|(id, _)| id).collect();
        assert!(ids.contains(&"pattern:Wide".to_string()));
        if corner {
            for auto in ["autoCentered", "autoBetween", "autoSliced", "autoOverlap"] {
                assert!(ids.contains(&auto.to_string()));
            }
        } else {
            assert_eq!(ids.first().map(String::as_str), Some("none"));
            assert_eq!(ids.get(1).map(String::as_str), Some("original"));
        }
    }
    // Original stays available after a swatch is chosen, and restores the tile the dialog opened with.
    let d = app.ui.dialog.as_mut().unwrap();
    crate::dialogs::brush_art_options::choose_corner(d, "sideTile", "Wide");
    assert!(crate::dialogs::brush_art_options::original_available(d, "side"));
    crate::dialogs::brush_art_options::choose_corner(d, "sideTile", "original");
    let restored = crate::dialogs::brush_art_options::edited_def(d);
    assert_eq!((&restored["side"], &restored["side_frame"]), (&before["side"], &before["side_frame"]));
    super::super::confirm(&mut app).unwrap();
    let kept = app.run("brush.get", json!({"name":"Diamonds"})).unwrap();
    assert_eq!((&kept["side"], &kept["side_frame"]), (&before["side"], &before["side_frame"]));
    app.run("ui.brushOptions", json!({"name":"Diamonds"})).unwrap();
    let d = app.ui.dialog.as_mut().unwrap();
    crate::dialogs::brush_art_options::choose_corner(d, "sideTile", "Wide");
    let preview = crate::dialogs::brush_art_options::edited_def(d);
    assert_eq!(preview["side_frame"], json!(Rect::new(0.0, 0.0, 10.0, 4.0)));
    assert_ne!(preview["side"], before["side"]);
    super::super::confirm(&mut app).unwrap();
    // A tile that opens on a swatch can also go back to the art it opened with.
    let opened = app.run("brush.get", json!({"name":"Diamonds"})).unwrap();
    app.run("ui.brushOptions", json!({"name":"Diamonds"})).unwrap();
    let d = app.ui.dialog.as_mut().unwrap();
    assert_eq!(d.str("sideTile"), "pattern:Wide");
    assert!(crate::dialogs::brush_art_options::original_available(d, "side"));
    crate::dialogs::brush_art_options::choose_corner(d, "sideTile", "original");
    assert_eq!(crate::dialogs::brush_art_options::edited_def(d)["side"], opened["side"]);
    super::super::cancel(&mut app);
    app.session
        .edit("Delete pattern", |doc, _| {
            doc.patterns.clear();
            Ok(())
        })
        .unwrap();
    app.run("ui.brushOptions", json!({"name":"Diamonds"})).unwrap();
    assert_eq!(app.ui.dialog.as_ref().unwrap().str("sideTile"), "original");
    let saved = app.run("brush.get", json!({"name":"Diamonds"})).unwrap();
    let undo = app.session.doc().unwrap().history.undo.len();
    let d = app.ui.dialog.as_mut().unwrap();
    crate::dialogs::brush_art_options::choose_corner(d, "sideTile", "none");
    let error = super::super::confirm(&mut app).unwrap_err();
    assert!(error.contains("You must choose a side pattern."), "{error}");
    assert!(app.ui.dialog.is_some());
    assert_eq!(app.run("brush.get", json!({"name":"Diamonds"})).unwrap(), saved);
    assert_eq!(app.session.doc().unwrap().history.undo.len(), undo);
}

#[test]
fn new_pattern_brush_keeps_a_tile_swatch_choice() {
    use std::sync::Arc;
    use vectorcraft_doc::{Node, PatternDef};
    use vectorcraft_geom::Rect;

    let mut app = app();
    app.run("shape.rectangle", json!({"x":0,"y":0,"width":20,"height":4})).unwrap();
    app.session
        .edit("Pattern", |doc, _| {
            let art = Node::path(
                doc.alloc_id(),
                vectorcraft_geom::shapes::rectangle(Rect::new(0.0, 0.0, 30.0, 4.0)),
                vectorcraft_doc::Appearance::default_art(),
            );
            doc.patterns.push(PatternDef { tile: Rect::new(0.0, 0.0, 10.0, 4.0), ..PatternDef::new("Wide", vec![Arc::new(art)]) });
            Ok(())
        })
        .unwrap();
    super::open_new(&mut app, "pattern", None).unwrap();
    let name = app.ui.dialog.as_ref().unwrap().str("name");
    let d = app.ui.dialog.as_mut().unwrap();
    crate::dialogs::brush_art_options::choose_corner(d, "sideTile", "Wide");
    super::super::confirm(&mut app).unwrap();
    let def = app.run("brush.get", json!({"name":name})).unwrap();
    assert_eq!(def["side_swatch"], "Wide");
    assert_eq!(def["side_frame"], json!(Rect::new(0.0, 0.0, 10.0, 4.0)));
}

/// A pattern brush with art for one kind of corner draws it at both kinds. Its other corner tile
/// shows that art as Original, and OK keeps it there when the first corner tile changes.
#[test]
fn pattern_options_keep_the_corner_art_a_corner_borrows() {
    let mut app = app();
    let art = app.run("brush.get", json!({"name": "Chain"})).unwrap()["outer_corner"].clone();
    app.run("brush.new", json!({"type": "pattern", "name": "One Corner", "params": {"side": art.clone(), "outer_corner": art}})).unwrap();
    app.run("ui.brushOptions", json!({"name": "One Corner"})).unwrap();
    let d = app.ui.dialog.as_ref().unwrap();
    assert_eq!((d.str("outerCornerTile"), d.str("innerCornerTile")), ("original".into(), "original".into()));
    set(&mut app, json!({"outerCornerTile": "none"}));
    super::super::confirm(&mut app).unwrap();
    let def = app.run("brush.get", json!({"name": "One Corner"})).unwrap();
    assert!(def.get("outer_corner").is_none() && def["inner_corner"].is_object(), "{def}");
    assert_eq!(def["outer_source"], json!("none"));
}

#[test]
fn pattern_options_preview_and_save_borrowed_corner_before_clearing_source() {
    use vectorcraft_geom::Rect;

    let mut app = app();
    let chain = app.run("brush.get", json!({"name": "Chain"})).unwrap();
    let art = chain["outer_corner"].clone();
    let frame = json!(Rect::new(1.0, 2.0, 15.0, 18.0));
    for (name, source, borrowed) in [("Borrow Outer", "inner", "outer"), ("Borrow Inner", "outer", "inner")] {
        let source_art = format!("{source}_corner");
        let source_frame = format!("{source_art}_frame");
        let borrowed_art = format!("{borrowed}_corner");
        let borrowed_frame = format!("{borrowed_art}_frame");
        let source_tile = format!("{source}CornerTile");
        let borrowed_tile = format!("{borrowed}CornerTile");
        app.run("brush.new", json!({"type":"pattern", "name":name, "params":{"side":chain["side"], source_art:art, source_frame:frame}})).unwrap();
        app.run("ui.brushOptions", json!({"name":name})).unwrap();
        let d = app.ui.dialog.as_mut().unwrap();
        crate::dialogs::brush_art_options::choose_corner(d, &source_tile, "none");
        crate::dialogs::brush_art_options::choose_corner(d, &borrowed_tile, "original");
        let preview = crate::dialogs::brush_art_options::edited_def(d);
        assert_eq!(preview[&borrowed_art], chain["outer_corner"], "{borrowed} preview art");
        assert_eq!(preview[&borrowed_frame], frame, "{borrowed} preview frame");
        super::super::confirm(&mut app).unwrap();
        let saved = app.run("brush.get", json!({"name":name})).unwrap();
        assert_eq!(saved[&borrowed_art], chain["outer_corner"], "{borrowed} saved art");
        assert_eq!(saved[&borrowed_frame], frame, "{borrowed} saved frame");
    }
}

#[test]
fn pattern_options_borrowed_swatch_corner_keeps_its_frame() {
    use std::sync::Arc;
    use vectorcraft_doc::{Node, PatternDef};
    use vectorcraft_geom::Rect;

    let mut app = app();
    app.session
        .edit("Pattern", |doc, _| {
            let art = Node::path(
                doc.alloc_id(),
                vectorcraft_geom::shapes::rectangle(Rect::new(0.0, 0.0, 30.0, 4.0)),
                vectorcraft_doc::Appearance::default_art(),
            );
            doc.patterns.push(PatternDef { tile: Rect::new(0.0, 0.0, 10.0, 4.0), ..PatternDef::new("Corner", vec![Arc::new(art)]) });
            Ok(())
        })
        .unwrap();
    let side = app.run("brush.get", json!({"name":"Diamonds"})).unwrap()["side"].clone();
    app.run("brush.new", json!({"type":"pattern", "name":"Borrow", "params":{"side":side}})).unwrap();
    app.run("brush.options", json!({"name":"Borrow", "params":{"outer_corner_tile":{"pattern":"Corner"}}})).unwrap();
    let before = app.run("brush.get", json!({"name":"Borrow"})).unwrap();
    app.run("ui.brushOptions", json!({"name":"Borrow"})).unwrap();
    set(&mut app, json!({"outerCornerTile":"none"}));
    super::super::confirm(&mut app).unwrap();
    let after = app.run("brush.get", json!({"name":"Borrow"})).unwrap();
    assert_eq!(after["inner_corner"], before["outer_corner"]);
    assert_eq!(after["inner_corner_frame"], before["outer_corner_frame"]);
    assert_eq!(after["outer_source"], json!("none"));
}

#[test]
fn pattern_options_selects_swatches_with_reserved_names() {
    use std::sync::Arc;
    use vectorcraft_doc::{Node, PatternDef};
    use vectorcraft_geom::Rect;

    let mut app = app();
    let names = ["none", "original", "autoCentered", "autoBetween", "autoSliced", "autoOverlap"];
    app.session
        .edit("Patterns", |doc, _| {
            for name in names {
                let art = Node::path(
                    doc.alloc_id(),
                    vectorcraft_geom::shapes::rectangle(Rect::new(0.0, 0.0, 30.0, 4.0)),
                    vectorcraft_doc::Appearance::default_art(),
                );
                doc.patterns.push(PatternDef { tile: Rect::new(0.0, 0.0, 10.0, 4.0), ..PatternDef::new(name, vec![Arc::new(art)]) });
            }
            Ok(())
        })
        .unwrap();
    app.run("ui.brushOptions", json!({"name":"Diamonds"})).unwrap();
    let d = app.ui.dialog.as_ref().unwrap();
    for name in names {
        let choice = (format!("pattern:{name}"), name.to_string());
        assert!(crate::dialogs::brush_art_options::tile_choices(d, true).contains(&choice));
    }
    for (field, name) in [
        ("sideTile", "none"),
        ("outerCornerTile", "original"),
        ("innerCornerTile", "autoCentered"),
        ("startTile", "autoBetween"),
        ("endTile", "autoSliced"),
    ] {
        crate::dialogs::brush_art_options::choose_corner(app.ui.dialog.as_mut().unwrap(), field, &format!("pattern:{name}"));
    }
    let preview = crate::dialogs::brush_art_options::edited_def(app.ui.dialog.as_ref().unwrap());
    assert_eq!(preview["side_swatch"], "none");
    super::super::confirm(&mut app).unwrap();
    let saved = app.run("brush.get", json!({"name":"Diamonds"})).unwrap();
    for (art, name) in
        [("side", "none"), ("outer_corner", "original"), ("inner_corner", "autoCentered"), ("start", "autoBetween"), ("end", "autoSliced")]
    {
        assert_eq!(saved[format!("{art}_swatch")], name);
        assert_eq!(saved[format!("{art}_frame")], json!(Rect::new(0.0, 0.0, 10.0, 4.0)));
    }
}

/// OK leaves the corner fields of a corner tile the menus were not set to as they were: a corner
/// without art keeps borrowing the other kind's art when it gets some, and a corner that borrows
/// art still borrows it. A None picked from the menu is saved, after another choice too.
#[test]
fn pattern_options_ok_leaves_corner_tiles_the_menus_were_not_set_to() {
    let mut app = app();
    app.run("ui.brushOptions", json!({"name": "Diamonds"})).unwrap();
    set(&mut app, json!({"scale": 150}));
    super::super::confirm(&mut app).unwrap();
    let def = app.run("brush.get", json!({"name": "Diamonds"})).unwrap();
    assert!(def.get("outer_source").is_none() && def.get("inner_source").is_none(), "{def}");
    let art = app.run("brush.get", json!({"name": "Chain"})).unwrap()["outer_corner"].clone();
    app.run("brush.options", json!({"name": "Diamonds", "params": {"outer_corner": art.clone()}})).unwrap();
    app.run("ui.brushOptions", json!({"name": "Diamonds"})).unwrap();
    assert_eq!(app.ui.dialog.as_ref().unwrap().str("innerCornerTile"), "original", "the inner corners draw the outer art");
    super::super::confirm(&mut app).unwrap();
    let def = app.run("brush.get", json!({"name": "Diamonds"})).unwrap();
    assert!(def["outer_corner"].is_object() && def.get("inner_corner").is_none(), "{def}");

    app.run("brush.new", json!({"type": "pattern", "name": "Plain", "params": {"side": art}})).unwrap();
    app.run("ui.brushOptions", json!({"name": "Plain"})).unwrap();
    let d = app.ui.dialog.as_mut().unwrap();
    crate::dialogs::brush_art_options::choose_corner(d, "outerCornerTile", "autoCentered");
    crate::dialogs::brush_art_options::choose_corner(d, "outerCornerTile", "none");
    super::super::confirm(&mut app).unwrap();
    let def = app.run("brush.get", json!({"name": "Plain"})).unwrap();
    assert_eq!(def["outer_source"], json!("none"));
    assert!(def.get("inner_source").is_none(), "{def}");
    // None set through the control channel is saved too.
    app.run("ui.brushOptions", json!({"name": "Plain"})).unwrap();
    let (set, _) = crate::control::ControlRequest::new("ui.dialog.set", json!({"field": "innerCornerTile", "value": "none"}));
    let crate::control::Outcome::Done(r) = crate::control::handle(&mut app, &egui::Context::default(), &set) else { panic!("not done") };
    assert!(r.get("error").is_none(), "{r}");
    super::super::confirm(&mut app).unwrap();
    let def = app.run("brush.get", json!({"name": "Plain"})).unwrap();
    assert_eq!(def["inner_source"], json!("none"));
}

/// Art Brush Options' Width and Pattern Brush Options' Scale each have a slider beside their
/// field, which screen readers name as the row, with the field's number and text over the
/// value's whole range.
#[test]
fn art_width_and_pattern_scale_have_sliders() {
    use crate::widgets::accessibility_tests::{slider_value, tree};
    let mut app = app();
    app.run("ui.brushOptions", json!({"name": "Arrow"})).unwrap();
    set(&mut app, json!({"width": 250}));
    let shown = tree(|ui| super::super::show(&mut app, ui.ctx()));
    assert_eq!(slider_value(&shown, "Width:"), (Some(250.0), Some(1.0), Some(1000.0), Some("250%".into())));
    app.ui.dialog = None;
    app.run("ui.brushOptions", json!({"name": "Diamonds"})).unwrap();
    let shown = tree(|ui| super::super::show(&mut app, ui.ctx()));
    assert_eq!(slider_value(&shown, "Scale:"), (Some(100.0), Some(1.0), Some(10000.0), Some("100%".into())));
}