From 2de239ee2b6c7455db3ce20ec7be46a48ff7d5df Mon Sep 17 00:00:00 2001 From: bflatastic Date: Sun, 22 Nov 2026 22:58:07 +0700 Subject: [PATCH] Engine tests: mirror axis, Shaper widget and 9-slice guide drags run as the canvas's selection widgets run them, and the precise crosshair is the canvas's Cursor::precise of the tool's pointer --- crates/engine/src/tests/draw2.rs | 6 +- crates/engine/src/tests/pattern.rs | 40 +++++++------ crates/engine/src/tests/prefs.rs | 61 ++++++++++---------- crates/engine/src/tests/shaper.rs | 10 +++- crates/engine/src/tests/symbol_nine_slice.rs | 20 +++++-- 5 files changed, 77 insertions(+), 60 deletions(-) diff --git a/crates/engine/src/tests/draw2.rs b/crates/engine/src/tests/draw2.rs index f8c6182a53..cf84900fe9 100644 --- a/crates/engine/src/tests/draw2.rs +++ b/crates/engine/src/tests/draw2.rs @@ -5107,10 +5107,8 @@ fn the_pen_continues_any_open_path_and_joins_another() { assert_eq!(s.doc().unwrap().selection.objects, vec![a]); assert_eq!(s.cursor(Point::new(300.0, 200.0), none, v), Cursor::PenJoin); assert_eq!(s.cursor(Point::new(350.0, 200.0), none, v), Cursor::PenDrawing, "the middle of the other path"); - // #1009: Use Precise Cursors makes the join pointer a crosshair too. - s.execute("prefs.set", &json!({"key": "usePreciseCursors", "value": true})).unwrap(); - assert_eq!(s.cursor(Point::new(300.0, 200.0), none, v), Cursor::Crosshair); - s.execute("prefs.set", &json!({"key": "usePreciseCursors", "value": false})).unwrap(); + // #1009: Use Precise Cursors makes the join pointer a crosshair too (the canvas applies `Cursor::precise`). + assert_eq!(s.cursor(Point::new(300.0, 200.0), none, v).precise(), Cursor::Crosshair); assert_eq!(gesture(&mut s, &[(300.0, 200.0)], none), 1); let pts = |s: &Session, id| path(s, id).subpaths.iter().flat_map(|sp| sp.anchors.iter().map(|x| (x.p.x, x.p.y))).collect::>(); assert_eq!(pts(&s, a), [(100.0, 100.0), (200.0, 100.0), (300.0, 200.0), (400.0, 200.0)]); diff --git a/crates/engine/src/tests/pattern.rs b/crates/engine/src/tests/pattern.rs index 14dda53e81..bbc71b8dc0 100644 --- a/crates/engine/src/tests/pattern.rs +++ b/crates/engine/src/tests/pattern.rs @@ -1667,21 +1667,31 @@ fn mirror_axis(s: &Session) -> vectorcraft_tools::mirror::MirrorAxis { vectorcraft_tools::mirror::MirrorAxis::of(&st.doc, &st.selection, 1.0).unwrap() } +/// A press on the mirror axis at `at`, as the canvas's selection widgets take it. +fn mirror_press(s: &mut Session, at: Point) -> vectorcraft_tools::mirror::MirrorDrag { + s.with_tool_context(ViewInfo::default(), |cx| vectorcraft_tools::mirror::MirrorDrag::hit(cx, at, Mods::default())).expect("a press on the axis") +} + +/// The mirror axis drag `d` moved to `to`, its actions applied as the canvas's selection widgets apply them. +fn mirror_drag(s: &mut Session, d: &mut vectorcraft_tools::mirror::MirrorDrag, to: Point, mods: Mods) { + let acts = s.with_tool_context(ViewInfo::default(), |cx| d.drag(cx, to, mods)); + s.apply_actions(acts).unwrap(); +} + #[test] fn mirror_axis_drag_previews_once_and_undo_redo_preserve_source() { - use vectorcraft_tools::{PointerEvent, PointerKind}; let mut s = session(); rect(&mut s, 100.0, 100.0, 60.0, 80.0); let id = id_of(&s.execute("object.repeat.mirror", &json!({})).unwrap()); let before = node(&s, id); let axis = mirror_axis(&s); - let v = ViewInfo::default(); - s.select_tool("selection", v).unwrap(); let h = crate::tests::history(&s); - for (kind, dx) in [(PointerKind::Down, 0.0), (PointerKind::Drag, 20.0), (PointerKind::Drag, 30.0), (PointerKind::Up, 40.0)] { - s.pointer(&PointerEvent::new(kind, axis.center.x + dx, axis.center.y), v).unwrap(); + let mut d = mirror_press(&mut s, axis.center); + for dx in [20.0, 30.0, 40.0] { + mirror_drag(&mut s, &mut d, axis.center + vectorcraft_geom::Vec2::new(dx, 0.0), Mods::default()); assert!(close(mirror_axis(&s).center, axis.center + vectorcraft_geom::Vec2::new(dx, 0.0))); } + s.apply_actions(d.finish()).unwrap(); assert_eq!(crate::tests::history(&s).0, h.0 + 1); let after = node(&s, id); let (NodeKind::Repeat(a), NodeKind::Repeat(b)) = (&before.kind, &after.kind) else { panic!() }; @@ -1695,33 +1705,29 @@ fn mirror_axis_drag_previews_once_and_undo_redo_preserve_source() { #[test] fn mirror_axis_rotation_snaps_and_escape_restores_document_and_history() { - use vectorcraft_tools::{Mods, PointerEvent, PointerKind, ToolKey}; let mut s = session(); rect(&mut s, 100.0, 100.0, 60.0, 80.0); let id = id_of(&s.execute("object.repeat.mirror", &json!({})).unwrap()); - let v = ViewInfo::default(); - s.select_tool("selection", v).unwrap(); let before = node(&s, id); let h = crate::tests::history(&s); let axis = mirror_axis(&s); let end = axis.ends[1]; let destination = axis.center + vectorcraft_geom::Vec2::new(70.0, 60.0); - s.pointer(&PointerEvent::new(PointerKind::Down, end.x, end.y), v).unwrap(); let shift = Mods { shift: true, ..Default::default() }; - s.pointer(&PointerEvent::new(PointerKind::Drag, destination.x, destination.y).with_mods(shift), v).unwrap(); + let mut d = mirror_press(&mut s, end); + mirror_drag(&mut s, &mut d, destination, shift); assert!((mirror_axis(&s).angle - 45.0).abs() < 1e-6); assert_eq!(mirror_axis(&s).center, axis.center); - s.tool_key(ToolKey::Escape, Mods::default(), v).unwrap(); - assert!(!s.tool_busy()); - s.pointer(&PointerEvent::new(PointerKind::Up, destination.x, destination.y), v).unwrap(); + s.apply_actions(d.cancel()).unwrap(); assert_eq!(node(&s, id), before); assert_eq!(crate::tests::history(&s), h); // A click is not an edit, and a subsequent real drag still works after cancellation. - s.pointer(&PointerEvent::new(PointerKind::Down, end.x, end.y), v).unwrap(); - s.pointer(&PointerEvent::new(PointerKind::Up, end.x, end.y), v).unwrap(); + let click = mirror_press(&mut s, end).finish(); + s.apply_actions(click).unwrap(); assert_eq!(crate::tests::history(&s), h); - s.pointer(&PointerEvent::new(PointerKind::Down, end.x, end.y), v).unwrap(); - s.pointer(&PointerEvent::new(PointerKind::Up, destination.x, destination.y).with_mods(shift), v).unwrap(); + let mut d = mirror_press(&mut s, end); + mirror_drag(&mut s, &mut d, destination, shift); + s.apply_actions(d.finish()).unwrap(); assert!((mirror_axis(&s).angle - 45.0).abs() < 1e-6); assert_eq!(crate::tests::history(&s).0, h.0 + 1); } diff --git a/crates/engine/src/tests/prefs.rs b/crates/engine/src/tests/prefs.rs index 966e68dc60..cdbce27939 100644 --- a/crates/engine/src/tests/prefs.rs +++ b/crates/engine/src/tests/prefs.rs @@ -699,7 +699,8 @@ fn double_clicking_a_single_object_isolates_it() { } /// General › Use Precise Cursors (#394): the Pen's pointer becomes a crosshair; the Selection -/// tool's arrow stays. +/// tool's arrow stays. The canvas applies the preference to the tool's own pointer +/// (`Cursor::precise`). #[test] fn use_precise_cursors_makes_drawing_cursors_crosshairs() { use vectorcraft_tools::{Cursor, Mods}; @@ -712,41 +713,31 @@ fn use_precise_cursors_makes_drawing_cursors_crosshairs() { assert_eq!(cursor(&mut s, "pen"), Cursor::Pen); assert_eq!(cursor(&mut s, "eyedropper"), Cursor::Eyedropper); s.execute("prefs.set", &json!({"key": "usePreciseCursors", "value": true})).unwrap(); - assert_eq!(cursor(&mut s, "pen"), Cursor::Crosshair); - assert_eq!(cursor(&mut s, "eyedropper"), Cursor::Crosshair); - assert_eq!(cursor(&mut s, "selection"), Cursor::Arrow); + assert_eq!(cursor(&mut s, "pen").precise(), Cursor::Crosshair); + assert_eq!(cursor(&mut s, "eyedropper").precise(), Cursor::Crosshair); + assert_eq!(cursor(&mut s, "selection").precise(), Cursor::Arrow); s.execute("prefs.set", &json!({"key": "usePreciseCursors", "value": false})).unwrap(); assert_eq!(cursor(&mut s, "eyedropper"), Cursor::Eyedropper); } -/// Caps Lock gives the drawing tools the precise crosshair while it is on, and leaves the saved -/// Use Precise Cursors preference as it was. +/// Caps Lock is reported and set through `view.capsLock`, and leaves the saved Use Precise +/// Cursors preference as it was; the canvas gives the drawing tools the crosshair while it is on. #[test] fn caps_lock_gives_drawing_tools_the_crosshair() { - use vectorcraft_tools::{Cursor, Mods}; let mut s = Session::new(); s.execute("file.new", &json!({"width": 400, "height": 400})).unwrap(); - let cursor = |s: &mut Session, tool: &str| { - s.select_tool(tool, ViewInfo::default()).unwrap(); - s.cursor(vectorcraft_geom::Point::new(300.0, 300.0), Mods::default(), ViewInfo::default()) - }; assert_eq!(s.execute("view.capsLock", &json!({})).unwrap(), json!({"on": false})); assert_eq!(s.execute("view.capsLock", &json!({"on": true})).unwrap(), json!({"on": true})); assert!(!s.prefs.use_precise_cursors, "Caps Lock leaves the preference off"); - assert_eq!(cursor(&mut s, "pen"), Cursor::Crosshair); - assert_eq!(cursor(&mut s, "lasso"), Cursor::Crosshair); - assert_eq!(cursor(&mut s, "eyedropper"), Cursor::Crosshair); - assert_eq!(cursor(&mut s, "selection"), Cursor::Arrow); + assert_eq!(s.execute("view.capsLock", &json!({})).unwrap(), json!({"on": true})); assert_eq!(s.execute("view.capsLock", &json!({"on": false})).unwrap(), json!({"on": false})); - assert_eq!(cursor(&mut s, "pen"), Cursor::Pen); - assert_eq!(cursor(&mut s, "eyedropper"), Cursor::Eyedropper); assert!(s.execute("view.capsLock", &json!({"on": 1})).is_err()); assert_eq!(s.execute("view.capsLock", &json!({})).unwrap(), json!({"on": false})); } /// Use Precise Cursors changes the pointer of the tools that have one of their own: off, each shows its own -/// pointer; on, a crosshair. Every other drawing tool shows a crosshair either way, and the Type tool and the -/// Scissors over no path keep theirs. +/// pointer; on, a crosshair (the canvas applies `Cursor::precise` to it). Every other drawing tool shows a +/// crosshair either way, and the Type tool and the Scissors over no path keep theirs. #[test] fn use_precise_cursors_swaps_the_tools_own_pointers_for_a_crosshair() { use vectorcraft_tools::{Cursor, Mods}; @@ -804,13 +795,13 @@ fn use_precise_cursors_swaps_the_tools_own_pointers_for_a_crosshair() { } s.execute("prefs.set", &json!({"key": "usePreciseCursors", "value": true})).unwrap(); for (tool, _) in own { - assert_eq!(cursor(&mut s, tool), Cursor::Crosshair, "{tool} on"); + assert_eq!(cursor(&mut s, tool).precise(), Cursor::Crosshair, "{tool} on"); } for tool in always { - assert_eq!(cursor(&mut s, tool), Cursor::Crosshair, "{tool} on"); + assert_eq!(cursor(&mut s, tool).precise(), Cursor::Crosshair, "{tool} on"); } - assert_eq!(cursor(&mut s, "type"), Cursor::Text); - assert_eq!(cursor(&mut s, "scissors"), Cursor::ScissorsNotAllowed); + assert_eq!(cursor(&mut s, "type").precise(), Cursor::Text); + assert_eq!(cursor(&mut s, "scissors").precise(), Cursor::ScissorsNotAllowed); s.execute("prefs.set", &json!({"key": "usePreciseCursors", "value": false})).unwrap(); for (tool, c) in own { assert_eq!(cursor(&mut s, tool), c, "{tool} off again"); @@ -818,7 +809,8 @@ fn use_precise_cursors_swaps_the_tools_own_pointers_for_a_crosshair() { } /// With no selected path, the Smooth tool shows Not Allowed, and Use Precise Cursors leaves it so; with a -/// selected path, it shows its own pointer, or a crosshair with the preference on. +/// selected path, it shows its own pointer, or a crosshair with the preference on (the canvas applies +/// `Cursor::precise`). #[test] fn use_precise_cursors_keeps_the_smooth_tools_not_allowed_pointer() { use vectorcraft_tools::{Cursor, Mods}; @@ -826,20 +818,21 @@ fn use_precise_cursors_keeps_the_smooth_tools_not_allowed_pointer() { s.execute("file.new", &json!({"width": 400, "height": 400})).unwrap(); let at = vectorcraft_geom::Point::new(300.0, 300.0); s.select_tool("smooth", ViewInfo::default()).unwrap(); + let shown = |c: Cursor, precise: bool| if precise { c.precise() } else { c }; for precise in [false, true] { s.execute("prefs.set", &json!({"key": "usePreciseCursors", "value": precise})).unwrap(); - assert_eq!(s.cursor(at, Mods::default(), ViewInfo::default()), Cursor::NotAllowed, "precise {precise}"); + assert_eq!(shown(s.cursor(at, Mods::default(), ViewInfo::default()), precise), Cursor::NotAllowed, "precise {precise}"); } let id = s.execute("shape.rectangle", &json!({"x": 10, "y": 10, "width": 50, "height": 50})).unwrap()["id"].as_u64().unwrap(); s.execute("select.set", &json!({"ids": [id]})).unwrap(); for (precise, c) in [(false, Cursor::Smooth), (true, Cursor::Crosshair)] { s.execute("prefs.set", &json!({"key": "usePreciseCursors", "value": precise})).unwrap(); - assert_eq!(s.cursor(at, Mods::default(), ViewInfo::default()), c, "precise {precise}"); + assert_eq!(shown(s.cursor(at, Mods::default(), ViewInfo::default()), precise), c, "precise {precise}"); } } /// With Alt held while drawing, the Pencil and the Paintbrush show their closing cursors, and Use Precise Cursors -/// turns those into a crosshair too. +/// turns those into a crosshair too (the canvas applies `Cursor::precise`). #[test] fn alt_shows_the_pencil_and_paintbrush_closing_cursors() { use vectorcraft_tools::{Cursor, Mods, PointerEvent, PointerKind}; @@ -853,7 +846,9 @@ fn alt_shows_the_pencil_and_paintbrush_closing_cursors() { s.select_tool(tool, ViewInfo::default()).unwrap(); s.pointer(&PointerEvent::new(PointerKind::Down, 250.0, 250.0), ViewInfo::default()).unwrap(); s.pointer(&PointerEvent::new(PointerKind::Drag, 300.0, 300.0), ViewInfo::default()).unwrap(); - assert_eq!(s.cursor(at, alt, ViewInfo::default()), if precise { Cursor::Crosshair } else { c }, "{tool}, precise {precise}"); + let shown = s.cursor(at, alt, ViewInfo::default()); + assert_eq!(shown, c, "{tool}, precise {precise}"); + assert_eq!(shown.precise(), Cursor::Crosshair, "{tool}"); s.pointer(&PointerEvent::new(PointerKind::Up, 300.0, 300.0), ViewInfo::default()).unwrap(); } } @@ -2167,10 +2162,14 @@ fn real_time_drawing_off_still_shows_a_dragged_nine_slice_guide() { let left = |s: &Session| s.doc().unwrap().doc.symbol_nine_slice("Box").unwrap().left; assert_eq!(left(&s), 0.25); let steps = s.doc().unwrap().history.undo.len(); - s.pointer(&PointerEvent::new(PointerKind::Down, 125.0, 150.0), v).unwrap(); - s.pointer(&PointerEvent::new(PointerKind::Drag, 110.0, 150.0), v).unwrap(); + let drag = crate::tests::symbol_nine_slice::guide_drag( + &mut s, + v, + vectorcraft_geom::Point::new(125.0, 150.0), + vectorcraft_geom::Point::new(110.0, 150.0), + ); assert!((left(&s) - 0.1).abs() < 1e-9, "the guide moves while dragged: {}", left(&s)); - s.pointer(&PointerEvent::new(PointerKind::Up, 110.0, 150.0), v).unwrap(); + s.apply_actions(drag.finish()).unwrap(); let d = s.doc().unwrap(); assert!((left(&s) - 0.1).abs() < 1e-9); assert_eq!((d.history.undo.len(), d.history.undo.last().map(|e| e.label.as_str())), (steps + 1, Some("Symbol Options"))); diff --git a/crates/engine/src/tests/shaper.rs b/crates/engine/src/tests/shaper.rs index 38e61241f0..d8769037af 100644 --- a/crates/engine/src/tests/shaper.rs +++ b/crates/engine/src/tests/shaper.rs @@ -366,12 +366,18 @@ fn shaper_widget_switches_between_face_selection_and_construction_mode() { s.pointer(&PointerEvent::new(kind, p.x, p.y), v).unwrap(); } }; + // A click on the widget, as the canvas's selection widgets run it before the tool. + let widget_click = |s: &mut Session, p: Point| { + let acts: Vec<_> = s.with_tool_context(v, |cx| vectorcraft_tools::shaperwidget::click(cx, p).into_iter().collect()); + assert!(!acts.is_empty(), "the click is on the widget"); + s.apply_actions(acts).unwrap(); + }; for tool in ["shaper", "selection"] { s.select_tool(tool, v).unwrap(); s.execute("select.set", &json!({"ids": [g.0]})).unwrap(); let w = widget(&s); assert!(!w.construction); - click(&mut s, w.at); + widget_click(&mut s, w.at); assert_eq!(s.doc().unwrap().isolation, Some(sources), "{tool}"); assert!(widget(&s).construction); if tool == "shaper" { @@ -388,7 +394,7 @@ fn shaper_widget_switches_between_face_selection_and_construction_mode() { assert_eq!(fill(&s, g, 15.0, 50.0).as_deref(), Some("#ff0000")); let w = widget(&s); assert!(w.construction); - click(&mut s, w.at); + widget_click(&mut s, w.at); assert_eq!(s.doc().unwrap().isolation, None, "{tool}"); assert_eq!(s.doc().unwrap().selection.objects, vec![g]); s.execute("object.move", &json!({"dx": -10, "dy": 0})).unwrap(); diff --git a/crates/engine/src/tests/symbol_nine_slice.rs b/crates/engine/src/tests/symbol_nine_slice.rs index cba8b8c984..be6bcc5942 100644 --- a/crates/engine/src/tests/symbol_nine_slice.rs +++ b/crates/engine/src/tests/symbol_nine_slice.rs @@ -220,6 +220,16 @@ fn a_dynamic_instance_keeps_its_changes_through_the_guides() { assert_ne!(fills[0].as_deref(), Some("#ff0000"), "only the changed object"); } +/// A press on the edited symbol's 9-slice guide at `from` dragged to `to`, run as the canvas's +/// selection widgets run it; the caller ends it with `finish` or `cancel`. +pub(super) fn guide_drag(s: &mut Session, v: ViewInfo, from: Point, to: Point) -> vectorcraft_tools::nineslice::GuideDrag { + let press = PointerEvent::new(PointerKind::Down, from.x, from.y); + let mut d = s.with_tool_context(v, |cx| vectorcraft_tools::nineslice::GuideDrag::hit(cx, &press)).expect("a guide at the press"); + let acts = s.with_tool_context(v, |cx| d.drag(cx, to)); + s.apply_actions(acts).unwrap(); + d +} + /// The boxes of the paths a symbol instance `id` draws, placed through its symbol's guides. fn placed_boxes(s: &Session, id: NodeId) -> Vec { let d = &s.doc().unwrap().doc; @@ -247,9 +257,8 @@ fn a_guide_dragged_on_the_canvas_changes_how_a_scaled_instance_draws() { // that span to the left of the left guide. let (from, right) = (mid(&e, NineSliceGuide::Left), mid(&e, NineSliceGuide::Right)); let to = from + (from - right) * 0.4; - s.pointer(&PointerEvent::new(PointerKind::Down, from.x + 1.0, from.y), v).unwrap(); - s.pointer(&PointerEvent::new(PointerKind::Drag, to.x, to.y), v).unwrap(); - s.pointer(&PointerEvent::new(PointerKind::Up, to.x, to.y), v).unwrap(); + let d = guide_drag(&mut s, v, Point::new(from.x + 1.0, from.y), to); + s.apply_actions(d.finish()).unwrap(); s.execute("symbol.exitEditing", &json!({})).unwrap(); let boxes = placed_boxes(&s, id); assert_eq!(boxes.len(), 3, "{boxes:?}"); @@ -316,9 +325,8 @@ fn a_guide_dragged_past_the_arts_edge_lies_outside_it() { // The guides lie a quarter in from each edge, so the art's left edge lies half the guides' // distance to the left of the left guide, and 25 % outside it lies as far again. let to = l - (r - l); - s.pointer(&PointerEvent::new(PointerKind::Down, l + 1.0, y), v).unwrap(); - s.pointer(&PointerEvent::new(PointerKind::Drag, to, y), v).unwrap(); - s.pointer(&PointerEvent::new(PointerKind::Up, to, y), v).unwrap(); + let d = guide_drag(&mut s, v, Point::new(l + 1.0, y), Point::new(to, y)); + s.apply_actions(d.finish()).unwrap(); let g = s.doc().unwrap().doc.symbol_nine_slice("Panel").unwrap(); assert!((g.left + 0.25).abs() < 1e-9, "{g:?}"); let e = s.doc().unwrap().doc.edited_nine_slice().unwrap();