mirror of
https://github.com/storytold/vectorcraft.git
synced 2026-11-19 16:55:55 +00:00
test: cover Scissors after settling a deferred move
This commit is contained in:
@@ -4,7 +4,7 @@
|
||||
use serde_json::json;
|
||||
use vectorcraft_doc::NodeId;
|
||||
use vectorcraft_engine::{Session, ViewInfo};
|
||||
use vectorcraft_geom::{PathData, Point};
|
||||
use vectorcraft_geom::{Affine, PathData, Point, Vec2};
|
||||
use vectorcraft_tools::{Overlay, PointerEvent, PointerKind};
|
||||
|
||||
fn path(session: &Session, id: NodeId) -> PathData {
|
||||
@@ -74,3 +74,78 @@ fn scissors_snap_follows_pixel_preview_and_restores_the_cut_selection() {
|
||||
assert_eq!(intersection, !pixel_preview, "Pixel Preview {pixel_preview}: intersection feedback");
|
||||
}
|
||||
}
|
||||
|
||||
/// A command that ends an actual drag must settle its deferred move before Scissors reads the
|
||||
/// document. Real-time drawing is the control: both routes cut the same moved geometry.
|
||||
#[test]
|
||||
fn scissors_cuts_after_a_command_settles_a_deferred_move() {
|
||||
for real_time_drawing in [true, false] {
|
||||
let mut session = Session::new();
|
||||
session.execute("file.new", &json!({"width": 800, "height": 600})).unwrap();
|
||||
let rectangle =
|
||||
NodeId(session.execute("shape.rectangle", &json!({"x": 100, "y": 100, "width": 100, "height": 100})).unwrap()["id"].as_u64().unwrap());
|
||||
let crossing = NodeId(session.execute("shape.line", &json!({"x1": 185, "y1": 110, "x2": 205, "y2": 150})).unwrap()["id"].as_u64().unwrap());
|
||||
session.execute("select.set", &json!({"ids": [rectangle.0]})).unwrap();
|
||||
session.execute("prefs.set", &json!({"key": "realTimeDrawing", "value": real_time_drawing})).unwrap();
|
||||
let drag_view = ViewInfo { smart_guides: false, snap_to_point: false, snap_to_grid: false, snap_to_pixel: false, ..ViewInfo::default() };
|
||||
session.select_tool("selection", drag_view).unwrap();
|
||||
let original = path(&session, rectangle);
|
||||
let moved = original.transformed(Affine::translate((40.0, 20.0)));
|
||||
let line = path(&session, crossing);
|
||||
let undo_before = session.doc().unwrap().history.undo.len();
|
||||
|
||||
session.pointer(&PointerEvent::new(PointerKind::Down, 150.0, 150.0), drag_view).unwrap();
|
||||
session.pointer(&PointerEvent::new(PointerKind::Drag, 190.0, 170.0), drag_view).unwrap();
|
||||
assert!(session.in_interaction(), "the pointer drag is still held");
|
||||
if real_time_drawing {
|
||||
assert_eq!(path(&session, rectangle), moved);
|
||||
assert_eq!(session.deferred_move(), None);
|
||||
} else {
|
||||
assert_eq!(path(&session, rectangle), original, "the non-real-time drag has not changed the stored geometry yet");
|
||||
assert_eq!(session.deferred_move(), Some(Vec2::new(40.0, 20.0)), "an actual Selection move is pending");
|
||||
}
|
||||
// command.batch settles the interaction before its steps; selecting the same object
|
||||
// changes no geometry and adds no separate undo step.
|
||||
session
|
||||
.execute(
|
||||
"command.batch",
|
||||
&json!({
|
||||
"label": "Keep selection", "commands": [{"command": "select.set", "params": {"ids": [rectangle.0]}}]
|
||||
}),
|
||||
)
|
||||
.unwrap();
|
||||
assert!(!session.in_interaction());
|
||||
assert_eq!(path(&session, rectangle), moved, "the command keeps the pending move: real-time={real_time_drawing}");
|
||||
assert!(path(&session, rectangle).is_closed());
|
||||
assert_eq!(path(&session, crossing), line);
|
||||
assert_eq!(session.doc().unwrap().history.undo.len(), undo_before + 1);
|
||||
|
||||
// Finish the physical gesture before changing tools. Releasing after the command has
|
||||
// settled it must neither repeat the transform nor discard its committed geometry.
|
||||
session.pointer(&PointerEvent::new(PointerKind::Up, 190.0, 170.0), drag_view).unwrap();
|
||||
assert!(!session.in_interaction());
|
||||
assert_eq!(path(&session, rectangle), moved, "release keeps exactly one move");
|
||||
assert_eq!(path(&session, crossing), line);
|
||||
assert_eq!(session.doc().unwrap().history.undo.len(), undo_before + 1);
|
||||
|
||||
let cut_view = ViewInfo::default();
|
||||
session.select_tool("scissors", cut_view).unwrap();
|
||||
session.pointer(&PointerEvent::new(PointerKind::Move, 192.0, 121.5), cut_view).unwrap();
|
||||
for kind in [PointerKind::Down, PointerKind::Up] {
|
||||
session.pointer(&PointerEvent::new(kind, 192.0, 121.5), cut_view).unwrap();
|
||||
}
|
||||
let opened = path(&session, rectangle);
|
||||
assert!(!opened.is_closed());
|
||||
let anchors = &opened.subpaths[0].anchors;
|
||||
let cut = anchors.first().unwrap().p;
|
||||
assert!(cut.distance(Point::new(190.0, 120.0)) < 1e-6, "the current moved path snaps to its intersection");
|
||||
assert_eq!(anchors.last().unwrap().p, cut);
|
||||
assert_eq!(path(&session, crossing), line);
|
||||
assert_eq!(session.doc().unwrap().history.undo.len(), undo_before + 2);
|
||||
session.execute("edit.undo", &json!({})).unwrap();
|
||||
assert_eq!(path(&session, rectangle), moved, "undoing the cut retains the earlier settled move");
|
||||
assert_eq!(path(&session, crossing), line);
|
||||
assert_eq!(session.doc().unwrap().selection.objects, [rectangle]);
|
||||
assert_eq!(session.doc().unwrap().history.undo.len(), undo_before + 1);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user