//! The script object model: `app`, its documents, their selection and page items. use serde_json::{Value, json}; use crate::*; fn run(s: &mut Session, source: &str) -> Result { s.execute("script.run", &json!({"source": source, "name": "t.js"})) } /// What the run logged. fn log(s: &mut Session, source: &str) -> Vec { let r = run(s, source).unwrap_or_else(|e| panic!("{source}: {e}")); r["log"].as_array().cloned().unwrap_or_default() } /// The message of the run's failure. fn failure(s: &mut Session, source: &str) -> String { run(s, source).map(|r| panic!("{source} ran: {r}")).unwrap_err().to_string() } fn rect(s: &mut Session, x: f64, y: f64, w: f64, h: f64) -> NodeId { NodeId(s.execute("shape.rectangle", &json!({"x": x, "y": y, "width": w, "height": h})).unwrap()["id"].as_u64().unwrap()) } /// A 400 × 300 pt document with two rectangles. fn doc() -> (Session, NodeId, NodeId) { let mut s = Session::new(); s.execute("file.new", &json!({"width": 400, "height": 300})).unwrap(); let a = rect(&mut s, 10.0, 20.0, 30.0, 40.0); let b = rect(&mut s, 100.0, 100.0, 50.0, 50.0); (s, a, b) } fn undo_steps(s: &Session) -> usize { s.active().unwrap().history.undo.len() } #[test] fn app_documents_track_active_tab_and_new_document() { let mut s = Session::new(); assert_eq!(log(&mut s, "vc.log(app.documents.length); vc.log(app.typename);"), vec![json!(0), json!("Application")]); assert!(failure(&mut s, "app.activeDocument;").contains("No active document")); s.execute("file.new", &json!({"width": 400, "height": 300})).unwrap(); s.execute("file.new", &json!({"width": 200, "height": 100})).unwrap(); let out = log( &mut s, "var d = app.documents; vc.log(d.length); vc.log(app.activeDocument.width);\ app.activeDocument = d[1]; vc.log(app.activeDocument.width); vc.log(d[0].parent.typename);\ vc.log(d[5] === undefined); vc.log(app.activeDocument instanceof Document);\ var n = d.add(DocumentColorSpace.CMYK, 50, 60, 2);\ vc.log(d.length); vc.log(app.activeDocument.name === n.name); vc.log(n.documentColorSpace); vc.log(n.height);\ vc.log(d.getByName(n.name).width);", ); let expected = [json!(2), json!(200), json!(400), json!("Application"), json!(true), json!(true), json!(3), json!(true)]; assert_eq!(out[..8], expected); assert_eq!(out[8..], [json!("DocumentColorSpace.CMYK"), json!(60), json!(50)]); assert_eq!(s.documents().len(), 3); assert_eq!(s.active_index(), Some(2)); assert_eq!(s.documents()[2].doc.artboards.len(), 2); // A bad argument changes nothing. assert!(failure(&mut s, "app.documents.add(DocumentColorSpace.RGB, -5);").contains("width")); assert!(failure(&mut s, "app.documents.add('RGB');").contains("colorSpace")); assert!(failure(&mut s, "app.documents.add(undefined, 10, 10, 101);").contains("numArtBoards")); // The spacing is refused at file.new's limit, with the script's own name for it. let spacing = "app.documents.add(undefined, 10, 10, 2, DocumentArtboardLayout.Row, 100001);"; assert!(failure(&mut s, spacing).contains("artboardSpacing must be from 0 to 100000 points")); assert!(failure(&mut s, "app.documents.index('none');").contains("Not found")); assert_eq!(s.documents().len(), 3); } #[test] fn page_items_are_live_and_typed_for_all_node_kinds() { let (mut s, a, b) = doc(); s.execute("select.set", &json!({"ids": [a.0, b.0]})).unwrap(); s.execute("object.group", &json!({})).unwrap(); rect(&mut s, 200.0, 200.0, 10.0, 10.0); s.execute("guide.add", &json!({"vertical": true, "pos": 120})).unwrap(); s.execute("text.create", &json!({"x": 10, "y": 250, "text": "hi"})).unwrap(); let out = log( &mut s, "var p = app.activeDocument.pageItems, t = [];\ for (var i = 0; i < p.length; i++) t.push(p[i].typename);\ vc.log(t.join(','));\ vc.log(p[3].parent.typename); vc.log(p[0].parent.typename); vc.log(p.parent.typename);\ p[1].remove(); vc.log(p.length); vc.log(p[-1] === undefined && p[1.5] === undefined && p[99] === undefined);\ vc.log(p[2] instanceof PathItem && p[2] instanceof PageItem);", ); // Topmost first, each group before the objects in it. assert_eq!( out, vec![ json!("TextFrame,PathItem,GroupItem,PathItem,PathItem"), json!("GroupItem"), json!("Layer"), json!("Document"), json!(4), json!(true), json!(true) ] ); } #[test] fn page_items_list_the_art_inside_a_blend() { let (mut s, a, b) = doc(); log(&mut s, "app.activeDocument.pageItems[0].name = 'first';"); s.execute("object.blend.make", &json!({"ids": [a.0, b.0], "steps": 2})).unwrap(); let out = log( &mut s, "var p = app.activeDocument.pageItems, t = [];\ for (var i = 0; i < p.length; i++) t.push(p[i].typename);\ vc.log(t.join(','));\ vc.log(p.getByName('first').parent.typename); vc.log(p.index('first').typename);", ); assert_eq!(out, vec![json!("BlendItem,PathItem,PathItem"), json!("BlendItem"), json!("PathItem")]); } #[test] fn page_item_geometry_uses_document_coordinates_with_y_up() { let (mut s, _, _) = doc(); let out = log( &mut s, "var i = app.activeDocument.pageItems[1];\ vc.log(i.geometricBounds); vc.log(i.position); vc.log([i.left, i.top, i.width, i.height]);\ app.coordinateSystem = CoordinateSystem.ARTBOARDCOORDINATESYSTEM; vc.log(i.position);\ app.coordinateSystem = CoordinateSystem.DOCUMENTCOORDINATESYSTEM;\ i.position = [20, 250]; i.width = 60; vc.log(i.geometricBounds);", ); assert_eq!(out[0], json!([10, 280, 40, 240])); assert_eq!(out[1], json!([10, 280])); assert_eq!(out[2], json!([10, 280, 30, 40])); assert_eq!(out[3], json!([10, 20])); assert_eq!(out[4], json!([20, 250, 80, 210])); } /// `controlBounds` takes in the direction handles of a curve, which reach past its visible bounds. #[test] fn control_bounds_take_in_the_direction_handles() { let (mut s, _, _) = doc(); let id = s.execute("path.create", &json!({"d": "M100 200 C100 100 200 100 200 200"})).unwrap()["id"].as_u64().unwrap(); s.execute("select.set", &json!({"ids": [id]})).unwrap(); let out = log(&mut s, "var i = app.activeDocument.selection[0]; vc.log(i.visibleBounds); vc.log(i.controlBounds);"); let top = |v: &Value| v[1].as_f64().unwrap(); assert!(top(&out[0]) < 180.0, "{out:?}"); assert_eq!(top(&out[1]), 200.0, "{out:?}"); assert_eq!(out[1][2].as_f64().unwrap(), out[0][2].as_f64().unwrap(), "{out:?}"); } /// The `controlBounds` of a group leave out the handles of a hidden curve in it. #[test] fn control_bounds_leave_out_hidden_paths() { let (mut s, a, _) = doc(); let id = s.execute("path.create", &json!({"d": "M100 200 C100 -400 200 -400 200 200"})).unwrap()["id"].as_u64().unwrap(); s.execute("select.set", &json!({"ids": [a.0, id]})).unwrap(); s.execute("object.group", &json!({})).unwrap(); s.execute("object.setProps", &json!({"id": id, "visible": false})).unwrap(); s.execute("select.set", &json!({"ids": [a.0]})).unwrap(); let out = log(&mut s, "var g = app.activeDocument.selection[0].parent; vc.log(g.visibleBounds); vc.log(g.controlBounds);"); assert_eq!(out[1], out[0], "{out:?}"); } /// Each anchor `controlBounds` visits counts toward the data a run passes, so reading the bounds of /// a large path again and again ends the run where reading its geometric bounds does not. #[test] fn control_bounds_count_the_anchors_they_visit() { let (mut s, _, _) = doc(); let d: String = (0..2000u32).map(|i| format!(" L{} {}", i % 300, (i * 7) % 200)).collect(); let id = s.execute("path.create", &json!({"d": format!("M0 0{d}")})).unwrap()["id"].as_u64().unwrap(); s.execute("select.set", &json!({"ids": [id]})).unwrap(); // 100 reads of 2001 anchors are about 200 KB of steps, over a 64 KiB limit. crate::script::TEST_BRIDGE_BYTES.with(|b| b.set(Some(1 << 16))); let read = |s: &mut Session, what: &str| run(s, &format!("var i = app.activeDocument.selection[0]; for (var n = 0; n < 100; n++) i.{what};")); let geometric = read(&mut s, "geometricBounds"); let control = read(&mut s, "controlBounds"); crate::script::TEST_BRIDGE_BYTES.with(|b| b.set(None)); geometric.unwrap(); let e = control.unwrap_err().to_string(); assert!(e.contains("passed more data"), "{e}"); } /// The `controlBounds` of a clip group leave out the handles of the clipped art past the clipping /// path. #[test] fn control_bounds_of_a_clip_group_come_from_its_clipping_path() { let (mut s, _, _) = doc(); let id = s.execute("path.create", &json!({"d": "M100 200 C100 -400 200 -400 200 200"})).unwrap()["id"].as_u64().unwrap(); let mask = rect(&mut s, 100.0, 150.0, 100.0, 100.0); s.execute("select.set", &json!({"ids": [id, mask.0]})).unwrap(); s.execute("object.clippingMask.make", &json!({})).unwrap(); let out = log(&mut s, "var g = app.activeDocument.groupItems[0]; vc.log(g.clipped); vc.log(g.visibleBounds); vc.log(g.controlBounds);"); assert_eq!(out[0], json!(true), "{out:?}"); assert_eq!(out[2], out[1], "{out:?}"); } /// `controlBounds` takes in the Bézier handles of a path, which can reach past what it draws. #[test] fn page_item_control_bounds_take_in_the_handles() { let (mut s, _, _) = doc(); let anchors = json!([{"x": 0, "y": 100, "out": [0, 0]}, {"x": 100, "y": 100, "in": [100, 0]}]); s.execute("path.create", &json!({"anchors": anchors})).unwrap(); let out = log(&mut s, "var i = app.activeDocument.pageItems[0]; vc.log(i.visibleBounds); vc.log(i.controlBounds);"); let b = |v: &Value| -> Vec { v.as_array().unwrap().iter().map(|n| n.as_f64().unwrap()).collect() }; let (visible, control) = (b(&out[0]), b(&out[1])); assert!(visible[1] < 290.0, "the curve stays well under its handles: {visible:?}"); assert!((control[1] - 300.0).abs() < 1e-9, "the handles reach the top of the page: {control:?}"); assert!(control[0] <= visible[0] && control[2] >= visible[2] && control[3] <= visible[3], "{visible:?} {control:?}"); } #[test] fn control_bounds_include_handles_inside_live_containers() { for command in ["object.blend.make", "object.envelope.makeWithMesh", "object.repeat.grid", "object.intertwine.make"] { let (mut s, _, _) = doc(); let path = s .execute("path.create", &json!({"anchors": [{"x": 200, "y": 0, "out": [200, -100]}, {"x": 300, "y": 0, "in": [300, -100]}]})) .unwrap()["id"] .as_u64() .unwrap(); let other = rect(&mut s, 210.0, 10.0, 10.0, 10.0); s.execute("select.set", &json!({"ids": [path, other.0]})).unwrap(); let params = match command { "object.blend.make" => json!({"steps": 2}), "object.envelope.makeWithMesh" | "object.repeat.grid" => json!({"rows": 1, "cols": 1}), _ => json!({}), }; s.execute(command, ¶ms).unwrap(); let out = log( &mut s, "app.coordinateSystem = CoordinateSystem.ARTBOARDCOORDINATESYSTEM; var p = app.activeDocument.selection[0];\ vc.log(p.controlBounds[1] < p.visibleBounds[1]);", ); assert_eq!(out, [json!(true)], "{command}"); } } #[test] fn control_bounds_take_in_the_direction_points() { let (mut s, _, _) = doc(); // An arch whose top is at y 40 and whose direction points are at y 20. let id = s.execute("path.create", &json!({"d": "M100 100C100 20 200 20 200 100"})).unwrap()["id"].clone(); s.execute("select.set", &json!({"ids": [id]})).unwrap(); let out = log(&mut s, "var i = app.activeDocument.selection[0]; vc.log(i.visibleBounds); vc.log(i.controlBounds);"); let top = |v: &Value| v[1].as_f64().unwrap_or(f64::NAN); assert!(top(&out[0]) < 265.0, "{out:?}"); assert_eq!(top(&out[1]), 280.0, "{out:?}"); } #[test] fn page_item_size_setters_on_a_rotated_object_give_the_page_aligned_size() { let (mut s, a, b) = doc(); s.execute("select.set", &json!({"ids": [a.0]})).unwrap(); s.execute("object.rotate", &json!({"angle": 30})).unwrap(); let other = format!("{:?}", s.active().unwrap().doc.node(b)); let out = log( &mut s, "var i = app.activeDocument.selection[0], h = i.height;\ i.width = 80; vc.log([i.width, i.height - h]);\ i.height = 90; vc.log([i.width, i.height]);\ var g = i.geometricBounds; vc.log([g[2] - g[0], g[1] - g[3]]);", ); let pairs: Vec<[f64; 2]> = out.iter().map(|v| [v[0].as_f64().unwrap(), v[1].as_f64().unwrap()]).collect(); for (got, want) in pairs.iter().zip([[80.0, 0.0], [80.0, 90.0], [80.0, 90.0]]) { assert!((got[0] - want[0]).abs() < 1e-6 && (got[1] - want[1]).abs() < 1e-6, "{out:?}"); } assert_eq!(pairs.len(), 3, "{out:?}"); assert_eq!(format!("{:?}", s.active().unwrap().doc.node(b)), other, "the other object stays as it was"); } /// Type in a document in the back that the switch to that document measures again (here: not /// measured yet) is sized and moved by the bounds measured after the switch. #[test] fn page_item_size_and_position_setters_measure_type_in_a_back_document_after_the_switch() { let mut s = Session::new(); s.execute("file.new", &json!({"width": 400, "height": 300})).unwrap(); let id = NodeId(s.execute("text.create", &json!({"x": 10, "y": 100, "text": "Wide type here", "size": 40})).unwrap()["id"].as_u64().unwrap()); let uid = s.active().unwrap().uid; s.execute("file.new", &json!({"width": 200, "height": 100})).unwrap(); let front = s.active().unwrap().uid; let unmeasure = |s: &mut Session| { let st = s.document_mut(uid).unwrap(); if let Some(NodeKind::Text(t)) = std::sync::Arc::make_mut(&mut st.doc).node_mut(id).map(|n| &mut n.kind) { t.cached_bounds = None; t.cached_baselines.clear(); } }; let mut got = Vec::new(); for (set, want) in [("t.width = 150; vc.log(t.width);", 150.0), ("t.height = 90; vc.log(t.height);", 90.0), ("t.top = 200; vc.log(t.top);", 200.0)] { unmeasure(&mut s); let out = log(&mut s, &format!("var t = app.documents[1].pageItems[0]; {set}")); got.push((set, out[0].as_f64().unwrap(), want)); } for (set, got, want) in got { assert!((got - want).abs() < 1e-6, "{set} read {got}"); } assert_eq!(s.active().unwrap().uid, front, "the front document stays in front"); } /// The getters measure type in a document in the back as the setters do, so assigning an item's /// own width, height, left, top or position back to it changes nothing and makes no undo step, /// and for an object without type the document is not switched to. #[test] fn page_item_geometry_read_from_a_back_document_assigns_back_without_a_change() { let mut s = Session::new(); s.execute("file.new", &json!({"width": 400, "height": 300})).unwrap(); rect(&mut s, 200.0, 20.0, 30.0, 40.0); let id = NodeId(s.execute("text.create", &json!({"x": 10, "y": 100, "text": "Wide type here", "size": 40})).unwrap()["id"].as_u64().unwrap()); let uid = s.active().unwrap().uid; s.execute("file.new", &json!({"width": 200, "height": 100})).unwrap(); let front = s.active().unwrap().uid; let back = |s: &Session| s.documents().iter().find(|d| d.uid == uid).map_or(0, |d| d.history.undo.len()); let steps = back(&s); let sets = ["t.width = t.width;", "t.height = t.height;", "t.left = t.left;", "t.top = t.top;", "t.position = t.position;"]; for (kind, set) in sets.iter().flat_map(|set| [("TextFrame", set), ("PathItem", set)]) { let st = s.document_mut(uid).unwrap(); if let Some(NodeKind::Text(t)) = std::sync::Arc::make_mut(&mut st.doc).node_mut(id).map(|n| &mut n.kind) { t.cached_bounds = None; t.cached_baselines.clear(); } // A switch to the back document and back again would stamp the front document anew. let stamp = s.active().unwrap().activated; let out = log( &mut s, &format!( "var p = app.documents[1].pageItems, t = p[0].typename == '{kind}' ? p[0] : p[1];\ var w = t.width, x = t.left; {set} vc.log(t.width - w); vc.log(t.left - x);" ), ); assert_eq!(out.len(), 2, "{kind} {set}"); for d in &out { assert!(d.as_f64().unwrap().abs() < 1e-6, "{kind} {set} moved or resized the item by {d}"); } assert_eq!(back(&s), steps, "{kind} {set} made an undo step"); if kind == "PathItem" { assert_eq!(s.active().unwrap().activated, stamp, "{set} switched documents"); } } assert_eq!(s.active().unwrap().uid, front, "the front document stays in front"); let st = s.document_mut(uid).unwrap(); if let Some(NodeKind::Text(t)) = std::sync::Arc::make_mut(&mut st.doc).node_mut(id).map(|n| &mut n.kind) { t.cached_bounds = None; t.cached_baselines.clear(); } let in_back = log(&mut s, "vc.log(app.documents[1].pageItems[0].width);"); let index = s.documents().iter().position(|d| d.uid == uid).unwrap(); s.execute("document.activate", &json!({"index": index})).unwrap(); let in_front = log(&mut s, "vc.log(app.activeDocument.pageItems[0].width);"); assert_eq!(in_back, in_front, "the width read in the back is the width read in front"); } /// `controlBounds` holds a curve's handles, which reach past the curve; for a rectangle it is the /// visible bounds. #[test] fn control_bounds_hold_the_handles_of_a_curve() { let (mut s, _, _) = doc(); s.execute("path.create", &json!({"d": "M100 200 C100 100 200 100 200 200"})).unwrap(); let out = log( &mut s, "var p = app.activeDocument.pageItems, c = p[0], r = p[1];\ function h(b) { return b[1] - b[3]; }\ vc.log([h(c.visibleBounds), h(c.controlBounds), c.controlBounds[0] <= c.visibleBounds[0]]);\ vc.log(String(r.controlBounds) === String(r.visibleBounds));", ); let heights = out[0].as_array().unwrap(); let (visible, control) = (heights[0].as_f64().unwrap(), heights[1].as_f64().unwrap()); // The curve rises 75 pt and its handles 100 pt; half the stroke's weight reaches above the curve. assert!(control - visible > 20.0 && control - visible <= 25.0 + 1e-6, "{out:?}"); assert_eq!(heights[2], json!(true), "{out:?}"); assert_eq!(out[1], json!(true), "{out:?}"); } #[test] fn convert_coordinate_pair_is_inverse_and_y_up() { let mut s = Session::new(); s.execute("file.new", &json!({"width": 400, "height": 300})).unwrap(); let out = log( &mut s, "var d = app.activeDocument, D = CoordinateSystem.DOCUMENTCOORDINATESYSTEM, A = CoordinateSystem.ARTBOARDCOORDINATESYSTEM;\ var p = d.convertCoordinate([10, 100], A, D); vc.log(p); vc.log(d.convertCoordinate(p, D, A));\ vc.log(d.convertCoordinate([3, 4], D, D));", ); assert_eq!(out, vec![json!([10, 200]), json!([10, 100]), json!([3, 4])]); assert!( failure( &mut s, "app.activeDocument.convertCoordinate([1, NaN], CoordinateSystem.DOCUMENTCOORDINATESYSTEM, CoordinateSystem.DOCUMENTCOORDINATESYSTEM);" ) .contains("finite") ); assert!( failure(&mut s, "app.activeDocument.convertCoordinate([1, 2], 'x', CoordinateSystem.DOCUMENTCOORDINATESYSTEM);").contains("CoordinateSystem") ); } #[test] fn ruler_origin_round_trips_without_moving_art() { let (mut s, a, _) = doc(); let art = format!("{:?}", s.active().unwrap().doc.layers); let out = log(&mut s, "var d = app.activeDocument; vc.log(d.rulerOrigin); d.rulerOrigin = [50, 60]; vc.log(d.rulerOrigin);"); assert_eq!(out, vec![json!([0, 300]), json!([50, 60])]); assert_eq!(s.active().unwrap().doc.ruler_origin_for(0), vectorcraft_geom::Point::new(50.0, 240.0)); assert_eq!(format!("{:?}", s.active().unwrap().doc.layers), art); assert!(s.active().unwrap().doc.node(a).is_some()); let steps = undo_steps(&s); log(&mut s, "var d = app.activeDocument; d.rulerOrigin = d.rulerOrigin;"); assert_eq!(undo_steps(&s), steps, "setting the same origin adds no step"); assert!(failure(&mut s, "app.activeDocument.rulerOrigin = [1e12, 0];").contains("outside the canvas")); } /// A window on the document of [`doc`] that shows 300 × 200 pt around (200, 150) at 200%. fn window() -> WindowView { WindowView { center: vectorcraft_geom::Point::new(200.0, 150.0), zoom: 2.0, zoom_baseline: 1.0, rotation: 0.0, size: [600.0, 400.0], screen_mode: 0, } } /// Shows the active document in the window `v`. fn show(s: &mut Session, v: WindowView) { let uid = s.active().unwrap().uid; s.window_views = vec![((uid, 1), v)]; } /// The window of the active document. fn shown(s: &Session) -> Option { let uid = s.active().unwrap().uid; s.window_views.iter().find(|((u, _), _)| *u == uid).map(|(_, v)| *v) } #[test] fn the_view_shows_the_part_of_the_document_its_window_shows() { let (mut s, ..) = doc(); let none = "var v = app.activeDocument.views; vc.log(v.length); vc.log(v[0] === undefined); vc.log(0 in v); vc.log('a' in v);"; assert_eq!(log(&mut s, none), vec![json!(0), json!(true), json!(false), json!(false)], "a document without a window has no view"); show(&mut s, window()); // A rectangle drawn over the visible part of the document has the view's bounds. let r = window().visible(); rect(&mut s, r.x0, r.y0, r.width(), r.height()); let out = log( &mut s, "var d = app.activeDocument; var v = d.views[0]; vc.log(d.views.length); vc.log(v.typename);\ vc.log(v.parent === d); vc.log(v.bounds); var r = d.pageItems[0];\ vc.log(r.geometricBounds); vc.log(v.centerPoint); vc.log(r.position[0] + r.width / 2);\ vc.log(r.position[1] - r.height / 2);", ); assert_eq!(out[..3], [json!(1), json!("View"), json!(true)]); assert_eq!(out[3], out[4], "the view's bounds are the visible area"); assert_eq!(out[5], json!([out[6], out[7]]), "the centre is the middle of the visible area"); // Turning the view shows more of the document around the same centre. show(&mut s, WindowView { rotation: 30.0, ..window() }); let turned = log(&mut s, "vc.log(app.activeDocument.views[0].bounds);"); let width = |b: &Value| b[2].as_f64().unwrap() - b[0].as_f64().unwrap(); assert!(width(&turned[0]) > width(&out[3])); let names = log(&mut s, "var d = app.activeDocument; vc.log(String(d.views) + String(d.views[0]));"); assert_eq!(names, vec![json!("[Views][View]")]); assert_eq!(log(&mut s, "var v = app.activeDocument.views; vc.log(0 in v); vc.log(1 in v);"), vec![json!(true), json!(false)]); // A named engine keeps the views from an earlier run, but they are stale. run(&mut s, "#targetengine keep\nvar vs = app.activeDocument.views, v = vs[0];").unwrap(); for member in ["String(vs)", "String(v)"] { let e = failure(&mut s, &format!("#targetengine keep\n{member};")); assert!(e.contains("Stale document"), "{member}: {e}"); } } #[test] fn setting_the_zoom_changes_the_view_without_an_undo_step() { let (mut s, ..) = doc(); show(&mut s, window()); // Another document's window. s.window_views.push(((u64::MAX, 1), window())); let steps = undo_steps(&s); let out = log( &mut s, "var v = app.activeDocument.views[0]; var b = v.bounds; v.zoom = 4; vc.log(v.zoom);\ vc.log(v.bounds[2] - v.bounds[0] < b[2] - b[0]); v.centerPoint = [100, 100]; vc.log(v.centerPoint);\ v.screenMode = ScreenMode.FULLSCREEN; vc.log(v.screenMode);", ); assert_eq!(out, vec![json!(4), json!(true), json!([100, 100]), json!("ScreenMode.FULLSCREEN")]); let now = shown(&s).unwrap(); assert!(now.zoom > window().zoom); assert!(now.center != window().center && now.screen_mode != window().screen_mode); assert_eq!(s.window_views[1].1.screen_mode, now.screen_mode, "the screen mode is the same in every window"); assert_eq!(s.window_views[1].1.zoom, window().zoom, "the zoom is each window's own"); assert_eq!(undo_steps(&s), steps, "a view change is not undoable"); // A bad value throws and leaves the view as it is. let bad = [ ("v.zoom = 0;", "RangeError"), ("v.zoom = 1000;", "RangeError"), ("v.zoom = '2';", "TypeError"), ("v.centerPoint = [1];", "TypeError"), ("v.centerPoint = [1e12, 0];", "RangeError"), ("v.screenMode = 'FULLSCREEN';", "TypeError"), ]; for (bad, name) in bad { let out = log(&mut s, &format!("var v = app.activeDocument.views[0]; try {{ {bad} vc.log('set'); }} catch (e) {{ vc.log(e.name); }}")); assert_eq!(out, vec![json!(name)], "{bad}"); assert_eq!(shown(&s), Some(now), "{bad}"); } // A run that fails after changing the view keeps the change. failure(&mut s, "app.activeDocument.views[0].zoom = 1; throw new Error('stop');"); assert_eq!(shown(&s).map(|v| v.zoom), Some(1.0)); } #[test] fn view_centers_stay_inside_the_canvas_after_coordinate_conversion() { let (mut s, ..) = doc(); let origin = vectorcraft_geom::Point::new(3.0e6, 3.0e6); s.edit("Move Artboard", |d, _| { d.artboards[0].rect = vectorcraft_geom::Rect::from_origin_size(origin, (400.0, 300.0)); Ok(()) }) .unwrap(); show(&mut s, window()); let steps = undo_steps(&s); for coordinates in ["ARTBOARDCOORDINATESYSTEM", "DOCUMENTCOORDINATESYSTEM"] { let script_point = |p: vectorcraft_geom::Point| { let y = if coordinates == "ARTBOARDCOORDINATESYSTEM" { p.y - origin.y } else { origin.y + 300.0 - p.y }; [p.x - origin.x, y] }; for bound in [-MAX_COORD, MAX_COORD] { for horizontal in [false, true] { let point = |n| if horizontal { vectorcraft_geom::Point::new(n, 0.0) } else { vectorcraft_geom::Point::new(0.0, n) }; let before = shown(&s); let [x, y] = script_point(point(bound + bound.signum())); let source = format!( "app.coordinateSystem = CoordinateSystem.{coordinates}; var v = app.activeDocument.views[0]; \ try {{ v.centerPoint = [{x}, {y}]; vc.log('set'); }} catch (e) {{ vc.log(e.name); }}" ); assert_eq!(log(&mut s, &source), vec![json!("RangeError")], "{source}"); assert_eq!(shown(&s), before, "a rejected center leaves the view unchanged"); let [x, y] = script_point(point(bound)); log(&mut s, &format!("app.coordinateSystem = CoordinateSystem.{coordinates}; app.activeDocument.views[0].centerPoint = [{x}, {y}];")); assert_eq!(shown(&s).unwrap().center, point(bound), "the canvas boundary is allowed"); } } } assert_eq!(undo_steps(&s), steps); } #[test] fn page_item_properties_join_one_undo_step() { let (mut s, a, _) = doc(); let steps = undo_steps(&s); let out = log( &mut s, "var i = app.activeDocument.pageItems[1];\ i.name = 'box'; i.opacity = 40; i.locked = true; i.locked = false; i.hidden = true; i.hidden = false;\ vc.log([i.name, i.opacity, i.locked, i.hidden, i.editable, i.zOrderPosition]);\ vc.log(app.activeDocument.pageItems.getByName('box').name);", ); assert_eq!(out, vec![json!(["box", 40, false, false, true, 1]), json!("box")]); let st = s.active().unwrap(); let n = st.doc.node(a).unwrap(); assert_eq!((n.name.as_deref(), (n.opacity * 100.0).round()), (Some("box"), 40.0)); assert_eq!(undo_steps(&s), steps + 1, "one undo step for the run"); let steps = undo_steps(&s); log(&mut s, "var i = app.activeDocument.pageItems[1]; i.name = 'box'; i.opacity = 40; i.hidden = false;"); assert_eq!(undo_steps(&s), steps, "values that change nothing add no step"); for (source, part) in [ ("app.activeDocument.pageItems[0].opacity = 101;", "opacity"), ("app.activeDocument.pageItems[0].hidden = 'yes';", "hidden"), ("app.activeDocument.pageItems[0].width = 0;", "width"), ("app.activeDocument.pageItems[0].position = [1];", "position"), ] { assert!(failure(&mut s, source).contains(part), "{source}"); } } /// `app.undo()` and `app.redo()` step through the run's own edits, and the run still ends as one /// undo step. They change nothing past the run's first edit or its last undone one. #[test] fn app_undo_and_redo_step_through_the_runs_edits() { let (mut s, a, b) = doc(); let steps = undo_steps(&s); let out = log( &mut s, "var d = app.activeDocument; var i = d.pageItems[0]; var unnamed = i.name;\ app.undo(); app.redo();\ i.name = 'first'; i.opacity = 40;\ app.undo(); vc.log([i.name, i.opacity]);\ app.redo(); vc.log(i.opacity);\ app.redo(); vc.log(i.opacity);\ app.undo(); app.undo(); app.undo(); vc.log([i.name === unnamed, i.opacity]);\ vc.log(typeof app.undo());\ d.pageItems[1].name = 'kept';", ); assert_eq!(out, vec![json!(["first", 100]), json!(40), json!(40), json!([true, 100]), json!("undefined")]); assert_eq!(undo_steps(&s), steps + 1, "one undo step for the run"); let st = s.active().unwrap(); let names: Vec<_> = [a, b].iter().map(|id| st.doc.node(*id).unwrap().name.clone()).collect(); assert!(names.contains(&Some("kept".to_string())) && !names.contains(&Some("first".to_string())), "{names:?}"); s.execute("edit.undo", &json!({})).unwrap(); let st = s.active().unwrap(); assert!(st.doc.node(a).is_some_and(|n| n.name.is_none()) && st.doc.node(b).is_some_and(|n| n.name.is_none()), "undoing the run"); let mut s = Session::new(); assert!(failure(&mut s, "app.undo();").contains("No active document")); assert!(failure(&mut s, "app.redo();").contains("No active document")); } /// A layer, page item or artboard read before `app.undo()` took it away is stale, even when an /// object added afterwards takes its id; one that `app.redo()` brings back, and one that was there /// before the undone step, still work. #[test] fn a_handle_from_before_an_undo_does_not_name_a_later_object() { let (mut s, _, _) = doc(); let out = log( &mut s, "var d = app.activeDocument, kept = d.layers[0], m;\ var a = d.layers.add(); app.undo(); var b = d.layers.add(); b.name = 'b';\ m = ''; try { a.name = 'x'; } catch (e) { m = e.message; } vc.log([m, b.name]);\ m = ''; try { a.groupItems.length; } catch (e) { m = e.message; } vc.log(m);\ var p = d.groupItems.add(); app.undo(); var q = d.groupItems.add(); q.name = 'q';\ m = ''; try { p.name = 'y'; } catch (e) { m = e.message; } vc.log([m, q.name]);\ var r = d.artboards.add([500, 300, 700, 100]); app.undo(); var t = d.artboards.add([800, 300, 1000, 100]); t.name = 't';\ m = ''; try { r.name = 'z'; } catch (e) { m = e.message; } vc.log([m, t.name]);\ var c = d.layers.add(); app.undo(); app.redo(); c.name = 'c'; kept.name = 'kept'; vc.log([c.name, kept.name]);", ); assert_eq!( out, vec![ json!(["Stale layer", "b"]), json!("Stale layer"), json!(["Stale page item", "q"]), json!(["Stale artboard", "t"]), json!(["c", "kept"]), ] ); } /// An opacity a graph mark already has still replaces the opacity another mark of its series left on /// the graph, as `object.setProps` does. #[test] fn an_opacity_a_graph_mark_already_has_replaces_the_one_its_series_keeps() { let mut s = Session::new(); s.execute("file.new", &json!({"width": 800, "height": 800})).unwrap(); let csv = ",2024,2025\nQ1,3,2\nQ2,5,4"; let r = s.execute("graph.create", &json!({"type": "column", "x": 100, "y": 100, "width": 300, "height": 200, "csv": csv})).unwrap(); let id = NodeId(r["id"].as_u64().unwrap()); let kept = |s: &Session| s.active().unwrap().doc.node(id).unwrap().extras().graph.as_deref().unwrap().series_paints.first().and_then(|p| p.opacity); let series = |s: &Session| { let n = s.active().unwrap().doc.node(id).unwrap().clone(); let group = n.children().unwrap().iter().find(|c| c.extras().series_index == Some(0)).unwrap().clone(); group.children().unwrap().iter().map(|m| m.id).collect::>() }; let marks = series(&s); s.execute("object.setProps", &json!({"id": marks[0].0, "opacity": 50})).unwrap(); assert_eq!(kept(&s), Some(0.5)); s.execute("object.setProps", &json!({"id": series(&s)[1].0, "name": "mark"})).unwrap(); // In a hidden or locked layer the same assignment would still change the graph, so it is refused. let layer = s.active().unwrap().doc.layers[0].id; for (on, off) in [ (json!({"ids": [layer.0], "visible": false}), json!({"ids": [layer.0], "visible": true})), (json!({"ids": [layer.0], "locked": true}), json!({"ids": [layer.0], "locked": false})), ] { s.execute("object.setProps", &on).unwrap(); let steps = undo_steps(&s); let e = failure(&mut s, "var m = app.activeDocument.pageItems.getByName('mark'); m.opacity = m.opacity;"); assert!(e.contains("Cannot edit"), "{on}: {e}"); assert_eq!(kept(&s), Some(0.5), "{on}"); assert_eq!(undo_steps(&s), steps, "{on}"); s.execute("object.setProps", &off).unwrap(); } log(&mut s, "app.activeDocument.pageItems.getByName('mark').opacity = 100;"); assert_eq!(kept(&s), None); } /// A blend mode a graph mark or axis label already has still replaces the blend mode another object /// of its series or kind left on the graph, and is refused in a hidden or locked layer. #[test] fn a_blend_mode_a_graph_mark_or_label_already_has_replaces_the_one_the_graph_keeps() { let mut s = Session::new(); s.execute("file.new", &json!({"width": 800, "height": 800})).unwrap(); let csv = ",2024,2025\nQ1,3,2\nQ2,5,4"; let r = s.execute("graph.create", &json!({"type": "column", "x": 100, "y": 100, "width": 300, "height": 200, "csv": csv})).unwrap(); let id = NodeId(r["id"].as_u64().unwrap()); let spec = |s: &Session| s.active().unwrap().doc.node(id).unwrap().extras().graph.as_deref().unwrap().clone(); let kept_mark = |s: &Session| spec(s).series_paints.first().and_then(|p| p.blend); let kept_label = |s: &Session| spec(s).label_styles.values_rows.as_ref().and_then(|r| r.paint.blend); let graph = s.active().unwrap().doc.node(id).unwrap().clone(); let group = graph.children().unwrap().iter().find(|c| c.extras().series_index == Some(0)).unwrap().clone(); let marks: Vec = group.children().unwrap().iter().map(|m| m.id).collect(); let axes = graph.children().unwrap().iter().find(|c| c.name.as_deref() == Some("Axes")).unwrap().clone(); // The value axis numbers come first among the Axes group's labels. let labels: Vec = axes.children().unwrap().iter().filter(|c| matches!(c.kind, NodeKind::Text(_))).map(|c| c.id).collect(); s.execute("object.setProps", &json!({"id": marks[0].0, "blend": "Multiply"})).unwrap(); s.execute("object.setProps", &json!({"id": labels[0].0, "blend": "Screen"})).unwrap(); let both = (Some(vectorcraft_color::BlendMode::Multiply), Some(vectorcraft_color::BlendMode::Screen)); assert_eq!((kept_mark(&s), kept_label(&s)), both); s.execute("object.setProps", &json!({"id": marks[1].0, "name": "mark"})).unwrap(); s.execute("object.setProps", &json!({"id": labels[1].0, "name": "label"})).unwrap(); let layer = s.active().unwrap().doc.layers[0].id; for (on, off) in [ (json!({"ids": [layer.0], "visible": false}), json!({"ids": [layer.0], "visible": true})), (json!({"ids": [layer.0], "locked": true}), json!({"ids": [layer.0], "locked": false})), ] { s.execute("object.setProps", &on).unwrap(); for name in ["mark", "label"] { let steps = undo_steps(&s); let e = failure(&mut s, &format!("var m = app.activeDocument.pageItems.getByName('{name}'); m.blendingMode = m.blendingMode;")); assert!(e.contains("Cannot edit"), "{on} {name}: {e}"); assert_eq!((kept_mark(&s), kept_label(&s)), both, "{on} {name}"); assert_eq!(undo_steps(&s), steps, "{on} {name}"); } s.execute("object.setProps", &off).unwrap(); } log( &mut s, "var p = app.activeDocument.pageItems; p.getByName('mark').blendingMode = BlendModes.NORMAL; p.getByName('label').blendingMode = BlendModes.NORMAL;", ); assert_eq!((kept_mark(&s), kept_label(&s)), (None, None)); } /// `app.redraw()` returns `undefined` and leaves the art and the undo history as they were. #[test] fn redraw_returns_and_changes_nothing() { let (mut s, _, top) = doc(); let (art, steps) = (format!("{:?}", s.active().unwrap().doc.layers), undo_steps(&s)); assert_eq!(log(&mut s, "vc.log(typeof app.redraw()); vc.log(app.redraw(1, 'x') === undefined);"), vec![json!("undefined"), json!(true)]); assert_eq!(format!("{:?}", s.active().unwrap().doc.layers), art); assert_eq!(undo_steps(&s), steps); // In a run in the app, edits before and after the calls still end as one undo step. s.script_frames = true; let source = "var i = app.activeDocument.pageItems[0]; i.opacity = 40; app.redraw(); i.opacity = 30; app.redraw();"; assert_eq!(s.execute("script.run", &json!({"source": source, "name": "t.js"})).unwrap()["running"], true); let ended = (0..10_000).find_map(|_| s.script_pump()).unwrap_or_else(|| panic!("the run did not end")); ended.unwrap(); // The edit after the first call landed too: the opacity is below the 40% set before it. assert!(s.active().unwrap().doc.node(top).unwrap().opacity < 0.4); assert_eq!(undo_steps(&s), steps + 1); } /// Assigning an item the opacity it reports, rounded to four decimals, changes nothing. #[test] fn assigning_the_reported_opacity_changes_nothing() { let (mut s, a, _) = doc(); s.execute("object.setProps", &json!({"id": a.0, "opacity": 100.0 / 3.0})).unwrap(); let (before, steps) = (s.active().unwrap().doc.node(a).unwrap().opacity, undo_steps(&s)); let out = log(&mut s, "var i = app.activeDocument.pageItems[1]; vc.log(i.opacity); i.opacity = i.opacity;"); assert_eq!(out, vec![json!(33.3333)]); assert_eq!(s.active().unwrap().doc.node(a).unwrap().opacity, before); assert_eq!(undo_steps(&s), steps); assert_eq!(s.script_report().unwrap()["slowestCommands"], json!([]), "the assignment runs no command"); // A value close to the reported one but not equal to it is still applied. s.execute("object.setProps", &json!({"id": a.0, "opacity": 50})).unwrap(); let (before, steps) = (s.active().unwrap().doc.node(a).unwrap().opacity, undo_steps(&s)); log(&mut s, "app.activeDocument.pageItems[1].opacity = 50.00005;"); assert!(s.active().unwrap().doc.node(a).unwrap().opacity > before, "the assigned opacity was applied"); assert_eq!(undo_steps(&s), steps + 1); } /// An item's or a layer's opacity that differs from the reported one, however little, is set as /// given. #[test] fn an_opacity_near_the_reported_one_is_still_set() { let (mut s, a, _) = doc(); s.execute("object.setProps", &json!({"id": a.0, "opacity": 0})).unwrap(); let out = log( &mut s, "var q = app.activeDocument.pageItems, i = q[0].opacity === 0 ? q[0] : q[1]; i.opacity = 0.00009; vc.log(i.opacity);\ var l = app.activeDocument.layers[0]; l.opacity = 0; l.opacity = 0.00009; vc.log(l.opacity);", ); assert_eq!(out, vec![json!(0.0001), json!(0.0001)]); let d = &s.active().unwrap().doc; assert!(d.node(a).unwrap().opacity > 0.0); assert!(d.node(d.layer_containing(a).unwrap()).unwrap().opacity > 0.0); } /// `blendingMode` reads and sets the object's own blend mode as a `BlendModes` value; the mode it /// already has adds no undo step, and any other value throws and changes nothing. #[test] fn blending_mode_reads_and_sets_the_objects_blend_mode() { let (mut s, a, b) = doc(); let blend = |s: &Session, id: NodeId| s.active().unwrap().doc.node(id).unwrap().blend; let steps = undo_steps(&s); let out = log( &mut s, "var i = app.activeDocument.pageItems[1]; vc.log(i.blendingMode);\ i.blendingMode = BlendModes.MULTIPLY; vc.log(i.blendingMode);\ vc.log([i.hasOwnProperty('blendingMode'), BlendModes.SATURATIONBLEND, BlendModes.COLORBLEND]);", ); assert_eq!(out[..2], [json!("BlendModes.NORMAL"), json!("BlendModes.MULTIPLY")]); assert_eq!(out[2], json!([true, "BlendModes.SATURATIONBLEND", "BlendModes.COLORBLEND"])); assert_ne!(blend(&s, a), blend(&s, b), "only the assigned object changed"); assert_eq!(undo_steps(&s), steps + 1, "one undo step for the run"); let (before, steps) = (blend(&s, a), undo_steps(&s)); log(&mut s, "var i = app.activeDocument.pageItems[1]; i.blendingMode = BlendModes.MULTIPLY; i.blendingMode = i.blendingMode;"); assert_eq!((blend(&s, a), undo_steps(&s)), (before, steps), "the mode the object has adds no step"); // Two modes the help page names with a suffix set the modes the Transparency panel calls Color // and Saturation. log(&mut s, "var p = app.activeDocument.pageItems; p[1].blendingMode = BlendModes.COLORBLEND; p[0].blendingMode = BlendModes.SATURATIONBLEND;"); assert_eq!((blend(&s, a).label(), blend(&s, b).label()), ("Color", "Saturation")); let (before, steps) = (blend(&s, a), undo_steps(&s)); for value in ["'BlendModes.SCREEN '", "'MULTIPLY'", "'Multiply'", "'BlendModes.multiply'", "'BlendModes.NOPE'", "5", "null", "ZOrderMethod.BRINGTOFRONT"] { // The error is caught in the script, so the run does not roll back and the mode read right // after it is the one the setter left. let out = log( &mut s, &format!( "var i = app.activeDocument.pageItems[1], e = '';\ try {{ i.blendingMode = {value}; }} catch (x) {{ e = String(x); }}\ vc.log(e); vc.log(i.blendingMode);" ), ); let e = out[0].as_str().unwrap_or_default(); assert!(e.contains("blendingMode needs a BlendModes value"), "{value}: {e}"); assert_eq!(out[1], json!("BlendModes.COLORBLEND"), "{value}"); assert_eq!((blend(&s, a), undo_steps(&s)), (before, steps), "{value}"); } } /// An opacity near the one an item has, but not the one it reports, is set. #[test] fn an_opacity_close_to_the_current_one_is_set() { let (mut s, a, _) = doc(); s.execute("object.setProps", &json!({"id": a.0, "opacity": 50})).unwrap(); let out = log(&mut s, "var i = app.activeDocument.pageItems[1]; i.opacity = 50.00009; vc.log(i.opacity);"); assert_eq!(out, vec![json!(50.0001)]); } /// A layer opacity near the one the layer has, but not the one it reports, is set. #[test] fn a_layer_opacity_close_to_the_current_one_is_set() { let (mut s, _, _) = doc(); let out = log(&mut s, "var l = app.activeDocument.layers[0]; l.opacity = 50; l.opacity = 50.00009; vc.log(l.opacity);"); assert_eq!(out, vec![json!(50.0001)]); // The same number again stores as the opacity the layer has, and runs no command. let sets = |s: &Session| s.journal.iter().filter(|(id, _)| id == "object.setProps").count(); let before = sets(&s); log(&mut s, "app.activeDocument.layers[0].opacity = 50.00009;"); assert_eq!(sets(&s), before); } #[test] fn selection_assignment_is_atomic_across_invalid_items() { let (mut s, a, b) = doc(); s.execute("object.setProps", &json!({"ids": [b.0], "locked": true})).unwrap(); let e = failure(&mut s, "var p = app.activeDocument.pageItems; app.activeDocument.selection = [p[0], p[1]];"); assert!(e.contains("Cannot select PathItem"), "{e}"); assert!(s.active().unwrap().selection.objects.is_empty()); // In either order, the refused assignment leaves nothing selected while the script runs. let out = log( &mut s, "var d = app.activeDocument, p = d.pageItems;\ function pick(list) { var m = ''; try { d.selection = list; } catch (e) { m = e.message; } vc.log([m, d.selection.length === 0]); }\ pick([p[0], p[1]]); pick([p[1], p[0]]);", ); assert_eq!(out.len(), 2); for o in &out { assert!(o[0].as_str().is_some_and(|m| m.contains("Cannot select PathItem")), "{o}"); assert_eq!(o[1], json!(true), "{o}"); } // With the valid item first and the error caught, the run sees the selection unchanged. let out = log( &mut s, "var d = app.activeDocument, p = d.pageItems; try { d.selection = [p[1], p[0]]; vc.log('assigned'); }\ catch (e) { vc.log(String(e).indexOf('Cannot select PathItem') >= 0); } vc.log(d.selection.length);", ); assert_eq!(out, vec![json!(true), json!(0)]); assert!(s.active().unwrap().selection.objects.is_empty()); s.execute("object.setProps", &json!({"ids": [b.0], "locked": false})).unwrap(); let out = log( &mut s, "var d = app.activeDocument, p = d.pageItems; vc.log(d.selection instanceof Array && d.selection.length === 0);\ d.selection = [p[1], p[0], p[1]]; vc.log(d.selection.length); vc.log(app.selection[0].selected);\ p[0].selected = false; vc.log(d.selection.length);\ d.selection = p[1]; vc.log(d.selection.length);", ); assert_eq!(out, vec![json!(true), json!(2), json!(true), json!(1), json!(1)]); assert_eq!(s.active().unwrap().selection.objects, vec![a]); // An item of another document cannot be selected here. s.execute("file.new", &json!({"width": 100, "height": 100})).unwrap(); let e = failure(&mut s, "app.activeDocument.selection = app.documents[1].pageItems[0];"); assert!(e.contains("Cannot select"), "{e}"); } /// An index past the end of `pageItems` gives `undefined`, which does not clear the selection; /// `null` and `[]` do. #[test] fn assigning_an_index_past_the_end_to_the_selection_throws_and_keeps_it() { let (mut s, a, _) = doc(); s.execute("select.set", &json!({"ids": [a.0]})).unwrap(); let caught = "var d = app.activeDocument, m = '';\ try { d.selection = d.pageItems[99]; } catch (e) { m = e.name; }\ vc.log(m); vc.log(d.selection.length);"; assert_eq!(log(&mut s, caught), vec![json!("TypeError"), json!(1)]); assert_eq!(s.active().unwrap().selection.objects, vec![a]); for clear in ["null", "[]"] { s.execute("select.set", &json!({"ids": [a.0]})).unwrap(); log(&mut s, &format!("app.activeDocument.selection = {clear};")); assert!(s.active().unwrap().selection.objects.is_empty(), "{clear}"); } } /// `controlBounds` takes in a direction point that lies past the curve; `visibleBounds` does not. #[test] fn control_bounds_take_in_direction_points_past_the_curve() { let (mut s, _, _) = doc(); s.execute("path.create", &json!({"anchors": [{"x": 200, "y": 0, "out": [200, -100]}, {"x": 300, "y": 0, "in": [300, -100]}]})).unwrap(); let out = log( &mut s, "app.coordinateSystem = CoordinateSystem.ARTBOARDCOORDINATESYSTEM; var p = app.activeDocument.pageItems;\ vc.log(p[0].controlBounds); vc.log(p[0].visibleBounds); vc.log(p[2].controlBounds); vc.log(p[2].visibleBounds);", ); let top = |v: &Value| v[1].as_f64().unwrap(); assert!((top(&out[0]) + 100.0).abs() < 1e-9, "{out:?}"); assert!(top(&out[1]) > -90.0, "{out:?}"); // A rectangle's direction points lie on its edges. assert_eq!(out[2], out[3]); } #[test] fn a_failed_selection_assignment_leaves_the_selection_within_the_run() { let (mut s, _, b) = doc(); s.execute("object.setProps", &json!({"ids": [b.0], "locked": true})).unwrap(); // The run goes on after the failed assignment, so nothing it ends with puts the selection back. let c = rect(&mut s, 200.0, 20.0, 10.0, 10.0); s.execute("select.set", &json!({"ids": [c.0]})).unwrap(); let out = log( &mut s, "var d = app.activeDocument, p = d.pageItems, open, shut;\ for (var i = 0; i < p.length; i++) { if (p[i].locked) shut = p[i]; else if (!p[i].selected) open = p[i]; }\ try { d.selection = [open, shut]; vc.log('assigned'); } catch (e) { vc.log(String(e.message).indexOf('Cannot select') >= 0); }\ vc.log([d.selection.length, open.selected, d.selection[0].locked]);", ); assert_eq!(out, vec![json!(true), json!([1, false, false])]); assert_eq!(s.active().unwrap().selection.objects, vec![c]); } #[test] fn a_guide_path_is_not_selected_while_guides_are_locked() { let (mut s, a, b) = doc(); s.execute("select.set", &json!({"ids": [a.0]})).unwrap(); s.execute("view.guides.make", &json!({})).unwrap(); s.execute("select.set", &json!({"ids": [b.0]})).unwrap(); s.execute("view.guides.lock", &json!({"locked": true})).unwrap(); // The assignment and `selected` refuse the guide path, and the selection stays as it was. for source in ["app.activeDocument.selection = app.activeDocument.pageItems[1];", "app.activeDocument.pageItems[1].selected = true;"] { let e = failure(&mut s, source); assert!(e.contains(&format!("Cannot select PathItem {}", a.0)), "{source}: {e}"); assert_eq!(s.active().unwrap().selection.objects, vec![b], "{source}"); } // With the guides unlocked, the guide path is selected. s.execute("view.guides.lock", &json!({"locked": false})).unwrap(); log(&mut s, "app.activeDocument.selection = app.activeDocument.pageItems[1];"); assert_eq!(s.active().unwrap().selection.objects, vec![a]); } #[test] fn an_inactive_document_changes_without_switching_tabs() { let (mut s, a, _) = doc(); s.execute("file.new", &json!({"width": 100, "height": 100})).unwrap(); log(&mut s, "var d = app.documents[1]; d.pageItems[1].name = 'far'; d.selection = d.pageItems[0];"); assert_eq!(s.active_index(), Some(1)); let first = &s.documents()[0]; assert_eq!(first.doc.node(a).unwrap().name.as_deref(), Some("far")); assert_eq!(first.selection.objects.len(), 1); } #[test] fn documents_list_the_current_document_first_then_the_most_recently_current() { let mut s = Session::new(); for _ in 0..3 { s.execute("file.new", &json!({"width": 100, "height": 100})).unwrap(); rect(&mut s, 0.0, 0.0, 5.0, 5.0); } let out = log( &mut s, "var t = {}, n = ['A', 'B', 'C'];\ for (var i = 0; i < 3; i++) t[app.documents[2 - i].name] = n[i];\ function order() { var r = []; for (var i = 0; i < app.documents.length; i++) r.push(t[app.documents[i].name]); return r.join(''); }\ var A = app.documents[2], B = app.documents[1], C = app.documents[0];\ vc.log(order());\ app.activeDocument = A; vc.log(order());\ app.activeDocument = B; vc.log(order());\ app.activeDocument = app.documents[2]; vc.log(order());\ A.pageItems[0].name = 'far'; vc.log(order());\ vc.log([app.documents[0] === app.activeDocument, app.documents.getByName(B.name) === B]);", ); // A change to a document in the back leaves the order as it was. assert_eq!(out, vec![json!("CBA"), json!("ACB"), json!("BAC"), json!("CBA"), json!("CBA"), json!([true, true])]); // Closing a document in the back keeps the others in their order as the tabs shift. s.execute("document.activate", &json!({"index": 0})).unwrap(); s.execute("document.activate", &json!({"index": 1})).unwrap(); s.execute("document.activate", &json!({"index": 2})).unwrap(); assert_eq!(s.front_order(), vec![2, 1, 0]); s.close_document(0); assert_eq!(s.front_order(), vec![1, 0]); } #[test] fn move_duplicate_and_zorder_obey_parent_order() { let (mut s, a, b) = doc(); let out = log( &mut s, "var p = app.activeDocument.pageItems, first = p[1];\ first.zOrder(ZOrderMethod.BRINGTOFRONT); vc.log(first.zOrderPosition);\ var c = first.duplicate(); vc.log(c.zOrderPosition); vc.log(p.length); vc.log(app.activeDocument.selection.length);\ c.zOrder(ZOrderMethod.SENDTOBACK); vc.log(c.zOrderPosition);", ); assert_eq!(out, vec![json!(2), json!(3), json!(3), json!(1), json!(1)]); assert_eq!(s.active().unwrap().selection.objects, vec![b], "the selection stays as it was"); let layer = &s.active().unwrap().doc.layers[0]; let order: Vec = layer.children().unwrap().iter().map(|n| n.id).collect(); assert_eq!(order[1..], [b, a]); assert!(failure(&mut s, "app.activeDocument.pageItems[0].zOrder('front');").contains("ZOrderMethod")); } /// The ids of the children of the first layer, bottom first. fn layer_order(s: &Session) -> Vec { s.active().unwrap().doc.layers[0].children().unwrap().iter().map(|n| n.id).collect() } #[test] fn move_and_duplicate_place_by_element_placement() { let (mut s, a, b) = doc(); let c = rect(&mut s, 300.0, 10.0, 5.0, 5.0); s.execute("select.none", &json!({})).unwrap(); let steps = undo_steps(&s); // Before puts it directly above the relative object, after directly below. let r = log(&mut s, "var p = app.activeDocument.pageItems; var a = p[2], c = p[0]; vc.log(a.move(c, ElementPlacement.PLACEBEFORE) === undefined);"); assert_eq!(r, vec![json!(false)]); assert_eq!(layer_order(&s), [b, c, a]); log(&mut s, "var p = app.activeDocument.pageItems; p[0].move(p[2], ElementPlacement.PLACEAFTER);"); assert_eq!(layer_order(&s), [a, b, c]); // At the end of a container is under its contents, at the beginning on top. log(&mut s, "var p = app.activeDocument.pageItems; p[0].move(p[0].parent, ElementPlacement.PLACEATEND);"); assert_eq!(layer_order(&s), [c, a, b]); log(&mut s, "var p = app.activeDocument.pageItems; p[2].move(p[2].parent, ElementPlacement.PLACEATBEGINNING);"); assert_eq!(layer_order(&s), [a, b, c]); assert_eq!(undo_steps(&s), steps + 4, "one step for each run that changed the order"); // Moving to where it is runs nothing. let steps = undo_steps(&s); log(&mut s, "var p = app.activeDocument.pageItems; p[0].move(p[1], ElementPlacement.PLACEBEFORE);"); assert_eq!(undo_steps(&s), steps); // Into a group, and a copy beside an object. s.execute("select.set", &json!({"ids": [a.0, b.0]})).unwrap(); let g = NodeId(s.execute("object.group", &json!({})).unwrap()["id"].as_u64().unwrap()); s.execute("select.none", &json!({})).unwrap(); log(&mut s, "var p = app.activeDocument.pageItems; p[0].move(p[1], ElementPlacement.INSIDE);"); let inside: Vec = s.active().unwrap().doc.node(g).unwrap().children().unwrap().iter().map(|n| n.id).collect(); assert_eq!(inside, [a, b, c]); let out = log( &mut s, "var g = app.activeDocument.pageItems[0], a = app.activeDocument.pageItems[3];\ var copy = a.duplicate(g, ElementPlacement.PLACEATEND); vc.log(copy.parent.typename); vc.log(copy.zOrderPosition);", ); assert_eq!(out, vec![json!("GroupItem"), json!(1)]); assert_eq!(s.active().unwrap().doc.node(g).unwrap().children().unwrap().len(), 4); // Refused moves change nothing. let before = format!("{:?}", s.active().unwrap().doc.layers); for (source, part) in [ ("var g = app.activeDocument.pageItems[0]; g.move(app.activeDocument.pageItems[1], ElementPlacement.INSIDE);", "into itself"), ("var p = app.activeDocument.pageItems; p[1].move(p[2], ElementPlacement.INSIDE);", "cannot hold objects"), ("var p = app.activeDocument.pageItems; p[1].move(p[2], 'inside');", "ElementPlacement"), ("var p = app.activeDocument.pageItems; p[1].move();", "relative object"), ("var p = app.activeDocument.pageItems; p[1].move(app.activeDocument);", "not available yet"), ("var p = app.activeDocument.pageItems; p[1].duplicate(undefined, ElementPlacement.INSIDE);", "relative object"), ] { let e = failure(&mut s, source); assert!(e.contains(part), "{source}: {e}"); } assert_eq!(format!("{:?}", s.active().unwrap().doc.layers), before); // A locked group takes nothing. s.execute("object.setProps", &json!({"ids": [g.0], "locked": true})).unwrap(); rect(&mut s, 0.0, 0.0, 5.0, 5.0); let e = failure(&mut s, "var p = app.activeDocument.pageItems; p[0].move(p[1], ElementPlacement.INSIDE);"); assert!(e.contains("hidden or locked"), "{e}"); } /// An object cannot go behind a clip group's clipping path; one put at the end of the group goes /// directly above the clipping path. #[test] fn an_object_cannot_go_behind_a_clipping_path() { let (mut s, a, b) = doc(); s.execute("select.set", &json!({"ids": [a.0, b.0]})).unwrap(); let g = NodeId(s.execute("object.clippingMask.make", &json!({})).unwrap()["id"].as_u64().unwrap()); let kids = |s: &Session| s.active().unwrap().doc.node(g).unwrap().children().unwrap().iter().map(|n| n.id).collect::>(); let before = kids(&s); s.execute("object.setProps", &json!({"id": before[0].0, "name": "mask"})).unwrap(); s.execute("object.setProps", &json!({"id": before[1].0, "name": "art"})).unwrap(); s.execute("select.none", &json!({})).unwrap(); let e = failure(&mut s, "var p = app.activeDocument.pageItems; p.getByName('art').move(p.getByName('mask'), ElementPlacement.PLACEAFTER);"); assert!(e.contains("an object cannot go below a clipping mask"), "{e}"); assert_eq!(kids(&s), before); // The clipping path moved directly below the object above it stays where it is. let steps = undo_steps(&s); log(&mut s, "var p = app.activeDocument.pageItems; p.getByName('mask').move(p.getByName('art'), ElementPlacement.PLACEAFTER);"); assert_eq!(kids(&s), before); assert_eq!(undo_steps(&s), steps); // The clipping path put at the end of its own clip group stays where it is; put on top of the // group's contents, it is refused. log(&mut s, "var p = app.activeDocument.pageItems; p.getByName('mask').move(p.getByName('art').parent, ElementPlacement.PLACEATEND);"); assert_eq!(kids(&s), before); assert_eq!(undo_steps(&s), steps); for place in ["INSIDE", "PLACEATBEGINNING"] { let e = failure( &mut s, &format!("var p = app.activeDocument.pageItems; p.getByName('mask').move(p.getByName('art').parent, ElementPlacement.{place});"), ); assert!(e.contains("the clipping path stays at the bottom of its clip group"), "{place}: {e}"); assert_eq!(kids(&s), before, "{place}"); } let c = rect(&mut s, 300.0, 10.0, 5.0, 5.0); s.execute("object.setProps", &json!({"id": c.0, "name": "c"})).unwrap(); log(&mut s, "var p = app.activeDocument.pageItems; p.getByName('c').move(p.getByName('art').parent, ElementPlacement.PLACEATEND);"); assert_eq!(kids(&s), [before[0], c, before[1]]); } #[test] fn a_copy_goes_beside_its_source() { let (mut s, a, b) = doc(); let out = log( &mut s, "var a = app.activeDocument.pageItems[1], c = a.duplicate(a, ElementPlacement.PLACEAFTER);\ vc.log([c.zOrderPosition, a.zOrderPosition]);", ); assert_eq!(out, vec![json!([1, 2])]); let order = layer_order(&s); assert_eq!(order[1..], [a, b]); // An object moved directly above or below itself stays where it is with no undo step, and it // cannot go into itself; a copy cannot go into its source. let (order, steps) = (layer_order(&s), undo_steps(&s)); log(&mut s, "var p = app.activeDocument.pageItems; p[1].move(p[1], ElementPlacement.PLACEAFTER); p[0].move(p[0], ElementPlacement.PLACEBEFORE);"); assert_eq!((layer_order(&s), undo_steps(&s)), (order, steps)); assert!(failure(&mut s, "var p = app.activeDocument.pageItems; p[1].move(p[1], ElementPlacement.PLACEATBEGINNING);").contains("into itself")); s.execute("select.set", &json!({"ids": [a.0, b.0]})).unwrap(); s.execute("object.group", &json!({})).unwrap(); let e = failure(&mut s, "var p = app.activeDocument.pageItems, g = p[0]; g.duplicate(g, ElementPlacement.INSIDE);"); assert!(e.contains("into itself"), "{e}"); } /// A clipping group's clipping path stays at the bottom of the group, so an object cannot go /// below it; above it is the bottom of the clipped art. #[test] fn nothing_goes_below_a_clipping_path() { let (mut s, a, b) = doc(); let c = rect(&mut s, 300.0, 10.0, 5.0, 5.0); s.execute("object.setProps", &json!({"ids": [b.0], "name": "mask"})).unwrap(); s.execute("object.setProps", &json!({"ids": [c.0], "name": "loose"})).unwrap(); s.execute("select.set", &json!({"ids": [a.0, b.0]})).unwrap(); let g = NodeId(s.execute("object.clippingMask.make", &json!({})).unwrap()["id"].as_u64().unwrap()); s.execute("select.none", &json!({})).unwrap(); let children = |s: &Session| -> Vec { s.active().unwrap().doc.node(g).unwrap().children().unwrap().iter().map(|n| n.id).collect() }; assert_eq!(children(&s)[0], b, "the clipping path is the group's first child"); let before = format!("{:?}", s.active().unwrap().doc.layers); let steps = undo_steps(&s); for source in [ "var p = app.activeDocument.pageItems; p.getByName('loose').move(p.getByName('mask'), ElementPlacement.PLACEAFTER);", "var p = app.activeDocument.pageItems; p.getByName('loose').duplicate(p.getByName('mask'), ElementPlacement.PLACEAFTER);", ] { let e = failure(&mut s, source); assert!(e.contains("an object cannot go below a clipping mask"), "{source}: {e}"); } assert_eq!(format!("{:?}", s.active().unwrap().doc.layers), before, "a refused move changes nothing"); assert_eq!(undo_steps(&s), steps); log(&mut s, "var p = app.activeDocument.pageItems; p.getByName('loose').move(p.getByName('mask'), ElementPlacement.PLACEBEFORE);"); assert_eq!(children(&s), [b, c, a]); } #[test] fn a_copy_that_cannot_be_placed_is_deleted() { let mut s = Session::new(); s.execute("file.new", &json!({"width": 400, "height": 300})).unwrap(); let a = rect(&mut s, 10.0, 10.0, 50.0, 50.0); let b = rect(&mut s, 30.0, 30.0, 50.0, 50.0); s.execute("select.set", &json!({"ids": [a.0, b.0]})).unwrap(); s.execute("livePaint.make", &json!({})).unwrap(); s.execute("text.create", &json!({"x": 200, "y": 200, "text": "Hi"})).unwrap(); s.execute("select.none", &json!({})).unwrap(); let before = format!("{:?}", s.active().unwrap().doc.layers); let (steps, step) = (undo_steps(&s), s.active().unwrap().history.step()); // A Live Paint group takes no type, so the copy is refused after it was made. let out = log( &mut s, "var p = app.activeDocument.pageItems, n = p.length, g = p[1], t = p[0], m = '';\ try { t.duplicate(g, ElementPlacement.INSIDE); } catch (e) { m = e.message; }\ vc.log(m); vc.log(p.length === n);", ); assert!(out[0].as_str().is_some_and(|m| m.contains("Live Paint takes paths")), "{out:?}"); assert_eq!(out[1], json!(true)); assert_eq!(format!("{:?}", s.active().unwrap().doc.layers), before); // The refused copy leaves no undo step and does not change the document's step. assert_eq!((undo_steps(&s), s.active().unwrap().history.step()), (steps, step)); // Nor does it count toward the run's growth allowance: many refused copies stay within one // that a few kept copies would pass. crate::script::TEST_GROWTH.with(|g| g.set(Some(5))); let r = run( &mut s, "var p = app.activeDocument.pageItems, n = p.length, g = p[0], t = p[n - 1], refused = 0;\ for (var k = 0; k < 20; k++) { try { t.duplicate(g, ElementPlacement.INSIDE); } catch (e) { refused++; } }\ vc.log(refused);", ); crate::script::TEST_GROWTH.with(|g| g.set(None)); assert_eq!(r.unwrap()["log"], json!([20])); } #[test] fn a_path_inside_a_compound_path_is_not_duplicated() { let (mut s, a, b) = doc(); s.execute("select.set", &json!({"ids": [a.0, b.0]})).unwrap(); s.execute("object.compoundPath.make", &json!({})).unwrap(); let before = format!("{:?}", s.active().unwrap().doc.layers); let steps = undo_steps(&s); let e = failure(&mut s, "var p = app.activeDocument.pageItems; vc.log(p[0].typename); p[1].duplicate();"); assert!(e.contains("Duplicating a path inside a compound path is not available yet"), "{e}"); assert_eq!(format!("{:?}", s.active().unwrap().doc.layers), before); assert_eq!(undo_steps(&s), steps); // The compound path itself is copied whole. let out = log(&mut s, "var p = app.activeDocument.pageItems, c = p[0].duplicate(); vc.log([c.typename, p.length]);"); assert_eq!(out, vec![json!(["CompoundPathItem", 6])]); } #[test] fn a_path_inside_a_compound_path_is_not_reordered() { let (mut s, a, b) = doc(); s.execute("select.set", &json!({"ids": [a.0, b.0]})).unwrap(); s.execute("object.compoundPath.make", &json!({})).unwrap(); rect(&mut s, 200.0, 200.0, 20.0, 20.0); let before = format!("{:?}", s.active().unwrap().doc.layers); let steps = undo_steps(&s); let out = log( &mut s, "var p = app.activeDocument.pageItems, m = [], out = [];\ for (var i = 0; i < p.length; i++) if (p[i].parent.typename == 'CompoundPathItem') m.push(p[i]);\ var ways = [ZOrderMethod.BRINGTOFRONT, ZOrderMethod.BRINGFORWARD, ZOrderMethod.SENDBACKWARD, ZOrderMethod.SENDTOBACK];\ for (var j = 0; j < m.length; j++) for (var k = 0; k < ways.length; k++) {\ try { m[j].zOrder(ways[k]); out.push('ran'); } catch (e) { out.push(e.message); }\ }\ vc.log(m.length); vc.log(out);", ); assert_eq!(out[0], json!(2)); let messages = out[1].as_array().cloned().unwrap_or_default(); assert_eq!(messages.len(), 8, "{out:?}"); for m in &messages { assert!(m.as_str().is_some_and(|m| m.contains("Reordering a path inside a compound path is not available yet")), "{out:?}"); } assert_eq!(format!("{:?}", s.active().unwrap().doc.layers), before); assert_eq!(undo_steps(&s), steps); // The compound path itself still moves. let out = log( &mut s, "var p = app.activeDocument.pageItems, c;\ for (var i = 0; i < p.length; i++) if (p[i].typename == 'CompoundPathItem') c = p[i];\ var at = c.zOrderPosition; c.zOrder(ZOrderMethod.BRINGTOFRONT); vc.log([at, c.zOrderPosition]);", ); assert_eq!(out, vec![json!([1, 2])]); } /// [`doc`] with `a`, `b` and a rectangle `c` on top made into a clip group `g`, clipped by `c`. The /// page items are `g`, `b`, `a` and `c`, topmost first. fn clip_group() -> (Session, NodeId, NodeId, NodeId, NodeId) { let (mut s, a, b) = doc(); let c = rect(&mut s, 0.0, 0.0, 200.0, 200.0); s.execute("select.set", &json!({"ids": [a.0, b.0, c.0]})).unwrap(); let g = NodeId(s.execute("object.clippingMask.make", &json!({})).unwrap()["id"].as_u64().unwrap()); s.execute("select.none", &json!({})).unwrap(); assert_eq!(children_of(&s, g), [c, a, b], "the clipping path is at the bottom"); (s, g, a, b, c) } /// The ids of the children of `id`, bottom first. fn children_of(s: &Session, id: NodeId) -> Vec { s.active().unwrap().doc.node(id).unwrap().children().unwrap().iter().map(|n| n.id).collect() } #[test] fn nothing_is_placed_below_a_clipping_path() { let (mut s, g, a, b, c) = clip_group(); let d = rect(&mut s, 300.0, 10.0, 5.0, 5.0); s.execute("select.none", &json!({})).unwrap(); let (before, steps) = (format!("{:?}", s.active().unwrap().doc.layers), undo_steps(&s)); // From outside the group, from above the clipping path inside it, a copy, and a copy of the // clipping path below itself. let out = log( &mut s, "var p = app.activeDocument.pageItems, mask = p[4], out = [];\ var tries = [function () { p[0].move(mask, ElementPlacement.PLACEAFTER); },\ function () { p[3].move(mask, ElementPlacement.PLACEAFTER); },\ function () { p[0].duplicate(mask, ElementPlacement.PLACEAFTER); },\ function () { mask.duplicate(mask, ElementPlacement.PLACEAFTER); }];\ for (var i = 0; i < tries.length; i++) { try { tries[i](); out.push('ran'); } catch (e) { out.push(e.message); } }\ vc.log(out);", ); let messages = out[0].as_array().cloned().unwrap_or_default(); assert_eq!(messages.len(), 4, "{out:?}"); for m in &messages { assert!(m.as_str().is_some_and(|m| m.contains("an object cannot go below a clipping mask")), "{out:?}"); } assert_eq!(format!("{:?}", s.active().unwrap().doc.layers), before); assert_eq!(undo_steps(&s), steps); // Directly above the clipping path is allowed, and so is moving the clipping path beside itself. log(&mut s, "var p = app.activeDocument.pageItems; p[0].move(p[4], ElementPlacement.PLACEBEFORE);"); assert_eq!(children_of(&s, g), [c, d, a, b]); let steps = undo_steps(&s); log(&mut s, "var p = app.activeDocument.pageItems; p[4].move(p[4], ElementPlacement.PLACEAFTER);"); assert_eq!((children_of(&s, g), undo_steps(&s)), (vec![c, d, a, b], steps)); } #[test] fn zorder_that_leaves_an_object_in_place_runs_no_command() { // The topmost art of a layer with a ruler guide above it. let (mut s, a, b) = doc(); let layer = s.active().unwrap().doc.layers[0].id; s.execute("guide.add", &json!({"vertical": true, "pos": 50, "layer": layer.0})).unwrap(); let kids = s.active().unwrap().doc.layers[0].children().unwrap().clone(); assert!(kids.len() == 3 && kids[2].is_ruler_guide(), "the guide is above the art"); let steps = undo_steps(&s); let r = run(&mut s, "var p = app.activeDocument.pageItems; p[0].zOrder(ZOrderMethod.BRINGTOFRONT); p[0].zOrder(ZOrderMethod.BRINGFORWARD);").unwrap(); assert_eq!((&r["slowestCommands"], undo_steps(&s)), (&json!([]), steps)); assert_eq!(layer_order(&s)[..2], [a, b]); // The lowest object above a clipping path, and the clipping path itself. let (mut s, g, a, b, c) = clip_group(); let steps = undo_steps(&s); let r = run( &mut s, "var p = app.activeDocument.pageItems, ways = [ZOrderMethod.BRINGTOFRONT, ZOrderMethod.BRINGFORWARD,\ ZOrderMethod.SENDBACKWARD, ZOrderMethod.SENDTOBACK];\ p[2].zOrder(ZOrderMethod.SENDBACKWARD); p[2].zOrder(ZOrderMethod.SENDTOBACK);\ for (var k = 0; k < ways.length; k++) p[3].zOrder(ways[k]);", ) .unwrap(); assert_eq!((&r["slowestCommands"], undo_steps(&s)), (&json!([]), steps)); assert_eq!(children_of(&s, g), [c, a, b]); // A change still runs the command. log(&mut s, "app.activeDocument.pageItems[1].zOrder(ZOrderMethod.SENDTOBACK);"); assert_eq!((children_of(&s, g), undo_steps(&s)), (vec![c, b, a], steps + 1)); } #[test] fn moving_the_last_path_out_of_a_compound_path_keeps_the_compound_path() { let (mut s, a, b) = doc(); s.execute("select.set", &json!({"ids": [a.0, b.0]})).unwrap(); s.execute("object.compoundPath.make", &json!({})).unwrap(); let compound = layer_order(&s)[0]; let layer = s.active().unwrap().doc.layers[0].id; let steps = undo_steps(&s); let out = log( &mut s, "var d = app.activeDocument, c = d.pageItems[0], m = [d.pageItems[2], d.pageItems[1]];\ m[0].move(c, ElementPlacement.PLACEBEFORE); vc.log(d.pageItems.length);\ m[1].move(c, ElementPlacement.PLACEBEFORE); vc.log(d.pageItems.length);\ vc.log([c.typename, c === d.pageItems[2]]);\ var bad = []; for (var k in c) { try { c[k]; } catch (e) { bad.push(k + ': ' + e.message); } } vc.log(bad);\ vc.log([c.geometricBounds, c.visibleBounds, c.controlBounds, c.position, c.width, c.height]);\ c.left = 5; c.top = 5; c.position = [1, 2]; c.width = 10; c.height = 10; vc.log(c.geometricBounds);", ); let zeros = json!([0, 0, 0, 0]); assert_eq!(out[..3], [json!(3), json!(3), json!(["CompoundPathItem", true])]); // Every property `for`-`in` lists reads, and the emptied compound path has zero bounds that // moving or resizing leaves as they are. assert_eq!(out[3..], [json!([]), json!([zeros, zeros, zeros, [0, 0], 0, 0]), zeros]); let doc = &s.active().unwrap().doc; assert!(doc.node(compound).is_some_and(|n| n.children().is_some_and(|c| c.is_empty())), "the compound path stays, empty"); // Each path went directly above the compound path, the second above the first. assert_eq!(layer_order(&s), [compound, b, a]); assert!([a, b].iter().all(|id| doc.parent_of(*id) == Some(layer))); assert_eq!(undo_steps(&s), steps + 1, "the run is one undo step"); } /// `remove()` on the last path of a compound path keeps the compound path, empty, as `move` does, /// and a document holding it saves, reopens, exports and releases it. #[test] fn removing_the_last_path_of_a_compound_path_keeps_the_compound_path() { let (mut s, a, b) = doc(); s.execute("select.set", &json!({"ids": [a.0, b.0]})).unwrap(); s.execute("object.compoundPath.make", &json!({})).unwrap(); let compound = layer_order(&s)[0]; let steps = undo_steps(&s); let out = log( &mut s, "var d = app.activeDocument, c = d.pageItems[0]; d.pageItems[2].remove(); d.pageItems[1].remove();\ vc.log([c.typename, d.pageItems.length, c === d.pageItems[0]]);", ); assert_eq!(out, vec![json!(["CompoundPathItem", 1, true])]); let doc = s.active().unwrap().doc.clone(); assert!(doc.node(compound).is_some_and(|n| n.children().is_some_and(|c| c.is_empty())), "the compound path stays, empty"); assert_eq!(undo_steps(&s), steps + 1, "the run is one undo step"); assert!(s.execute("edit.clear", &json!({"ids": [compound.0], "keepEmpty": 1})).is_err(), "keepEmpty must be a boolean"); // The native format keeps it, and the other writers and Release Compound Path accept it. let back = vectorcraft_format::load(&vectorcraft_format::save(&doc, false)).unwrap(); assert!(back.node(compound).is_some_and(|n| matches!(n.kind, NodeKind::Compound { .. }) && n.children().is_some_and(|c| c.is_empty()))); for format in ["svg", "pdf", "vectorcraft"] { s.execute("document.export", &json!({"format": format})).unwrap_or_else(|e| panic!("{format}: {e}")); } s.execute("select.set", &json!({"ids": [compound.0]})).unwrap(); s.execute("object.compoundPath.release", &json!({})).unwrap(); } /// Clear with `keepEmpty` keeps the emptied compound path also when it deletes direct-selected /// anchors, and without it deletes the compound path as before. #[test] fn clearing_anchors_with_keep_empty_keeps_the_compound_path() { for keep in [true, false] { let (mut s, a, b) = doc(); s.execute("select.set", &json!({"ids": [a.0, b.0]})).unwrap(); s.execute("object.compoundPath.make", &json!({})).unwrap(); let compound = layer_order(&s)[0]; for (id, mode) in [(a, "set"), (b, "add")] { s.execute("select.anchors", &json!({"id": id.0, "anchors": [[0, 0], [0, 1], [0, 2]], "mode": mode})).unwrap(); } s.execute("edit.clear", &json!({"keepEmpty": keep})).unwrap(); let d = &s.active().unwrap().doc; assert!(d.node(a).is_none() && d.node(b).is_none(), "keepEmpty {keep}: both paths are deleted"); if keep { assert!(d.node(compound).is_some_and(|n| n.children().is_some_and(|c| c.is_empty())), "keepEmpty true keeps it, empty"); } else { assert!(d.node(compound).is_none(), "keepEmpty false deletes the compound path"); } } } #[test] fn a_copy_of_an_object_with_a_large_id_is_the_copy() { let mut s = Session::new(); s.execute("file.new", &json!({"width": 400, "height": 300})).unwrap(); // The copy's id, 2^53 + 1, is not exact as a number. let big = 1u64 << 53; s.active_mut().unwrap().change_unsaved(|d| std::sync::Arc::make_mut(d).raise_next_id(big)); let a = rect(&mut s, 10.0, 20.0, 30.0, 40.0); assert_eq!(a.0, big); let out = log(&mut s, "var i = app.activeDocument.pageItems[0], c = i.duplicate(); c.name = 'copy'; vc.log(c === i); vc.log(i.name);"); assert_eq!(out, vec![json!(false), json!("")]); let d = &s.active().unwrap().doc; assert_eq!(d.node(NodeId(big + 1)).and_then(|n| n.name.clone()).as_deref(), Some("copy")); assert_eq!(d.node(a).unwrap().name, None); } #[test] fn a_grid_by_column_takes_the_number_of_rows() { let mut s = Session::new(); // With five artboards in four rows, the columns hold four and one. for (layout, count, given, rows, cols) in [("GridByCol", 6, 2, 2, 3), ("RLGridByCol", 6, 2, 2, 3), ("GridByRow", 6, 2, 3, 2), ("GridByCol", 5, 4, 4, 2), ("RLGridByCol", 5, 4, 4, 2)] { run(&mut s, &format!("app.documents.add(DocumentColorSpace.RGB, 100, 100, {count}, DocumentArtboardLayout.{layout}, 10, {given});")).unwrap(); let boards = &s.active().unwrap().doc.artboards; let distinct = |mut v: Vec| { v.sort_unstable(); v.dedup(); v }; let tops = distinct(boards.iter().map(|b| b.rect.y0.round() as i64).collect()); let lefts = distinct(boards.iter().map(|b| b.rect.x0.round() as i64).collect()); assert_eq!((tops.len(), lefts.len()), (rows, cols), "{layout} {count}"); // Each artboard's row and column, counted from the top and from the left: a grid by column // fills each column top to bottom, and the right-to-left grid starts at the rightmost column. let index = |v: &[i64], x: f64| v.iter().position(|&t| t == x.round() as i64); let placed: Vec<_> = boards.iter().map(|b| (index(&tops, b.rect.y0), index(&lefts, b.rect.x0))).collect(); let expected: Vec<_> = (0..count) .map(|i| match layout { "GridByRow" => (Some(i / cols), Some(i % cols)), "GridByCol" => (Some(i % rows), Some(i / rows)), _ => (Some(i % rows), Some(cols - 1 - i / rows)), }) .collect(); assert_eq!(placed, expected, "{layout} {count}"); } } #[test] fn file_new_takes_the_rows_of_a_grid_by_column() { let mut s = Session::new(); let first_column = |s: &mut Session, layout: Value| { s.execute("file.new", &json!({"width": 100, "height": 100, "artboards": 5, "artboardLayout": layout})).unwrap(); let boards = &s.active().unwrap().doc.artboards; boards.iter().filter(|b| b.rect.x0.round() as i64 == boards[0].rect.x0.round() as i64).count() }; assert_eq!(first_column(&mut s, json!({"layout": "gridByColumn", "rows": 4})), 4); // Rows take the place of columns given beside them. assert_eq!(first_column(&mut s, json!({"layout": "gridByColumn", "rows": 4, "columns": 1})), 4); // A preset keeps and lists the rows, with the columns they give. use crate::cmd::newdoc::{ArtboardLayout, DocSettings}; let settings = DocSettings { artboards: 5, layout: ArtboardLayout::GridByColumn, ..DocSettings::default() }; let settings = settings.with_params(&json!({"artboardLayout": {"rows": 4}}), "file.new").unwrap(); let listed = settings.to_json(); assert_eq!((&listed["artboardLayout"]["columns"], &listed["artboardLayout"]["rows"]), (&json!(2), &json!(4)), "{listed}"); let stored: DocSettings = serde_json::from_value(serde_json::to_value(&settings).unwrap()).unwrap(); assert_eq!(stored.rows, 4); // More artboards keep the rows; columns alone, or another layout, replace them. let more = stored.clone().with_params(&json!({"artboards": 9, "artboardLayout": listed["artboardLayout"]}), "file.new").unwrap(); assert_eq!(more.boards().iter().filter(|b| b.x0 == more.boards()[0].x0).count(), 4); let by_columns = stored.clone().with_params(&json!({"artboardLayout": {"columns": 1}}), "file.new").unwrap(); assert_eq!((by_columns.rows, by_columns.boards().iter().filter(|b| b.x0 == by_columns.boards()[0].x0).count()), (0, 5)); for layout in [json!({"layout": "gridByRow"}), json!("gridByRow")] { let by_row = stored.clone().with_params(&json!({"artboardLayout": layout}), "file.new").unwrap(); assert_eq!((by_row.rows, by_row.to_json()["artboardLayout"].get("rows")), (0, None), "{layout}"); } // A change of spacing alone leaves the stored columns as they are, so leaving the rows later // gives the same grid with or without it. let to_row = json!({"artboardLayout": {"layout": "gridByRow"}}); let plain = stored.clone().with_params(&json!({"artboards": 9}), "file.new").unwrap(); let spaced = stored.clone().with_params(&json!({"artboards": 9, "artboardLayout": {"spacing": 20}}), "file.new").unwrap(); assert_eq!((plain.grid_columns(), spaced.grid_columns()), (3, 3)); let (plain, spaced) = (plain.with_params(&to_row, "file.new").unwrap(), spaced.with_params(&to_row, "file.new").unwrap()); assert_eq!(plain.grid_columns(), spaced.grid_columns()); let e = s.execute("file.new", &json!({"artboardLayout": {"layout": "gridByColumn", "rows": 0}})).unwrap_err().to_string(); assert!(e.contains("rows must be a whole number from 1 to 1000"), "{e}"); for layout in ["gridByRow", "row", "column"] { let e = s.execute("file.new", &json!({"artboardLayout": {"layout": layout, "rows": 2}})).unwrap_err().to_string(); assert!(e.contains("rows needs the gridByColumn layout"), "{layout}: {e}"); } } #[test] fn file_new_replays_legacy_fractional_and_large_rows() { for (rows, rounded) in [(3.6, 4), (1000.4, 1000), (1000.5, 1000), (5000.0, 1000)] { let params = json!({"width": 100, "height": 100, "artboards": 5, "artboardLayout": {"layout": "gridByColumn", "rows": rows}}); let mut s = Session::new(); // A direct call refuses a fraction or a count over 1000; a replay rounds it and keeps it to at most 1000. let direct = Session::new().execute("file.new", ¶ms); assert!(direct.is_err(), "{rows}: {direct:?}"); s.replay("file.new", ¶ms).unwrap(); let mut expected = Session::new(); let mut params = params; params["artboardLayout"]["rows"] = json!(rounded); expected.execute("file.new", ¶ms).unwrap(); let boards = |s: &Session| s.active().unwrap().doc.artboards.iter().map(|b| b.rect).collect::>(); assert_eq!(boards(&s), boards(&expected)); } } #[test] fn a_listed_preset_keeps_more_rows_than_artboards() { use crate::cmd::newdoc::{ArtboardLayout, DocSettings}; let settings = DocSettings { artboards: 2, layout: ArtboardLayout::GridByColumn, ..DocSettings::default() }; let settings = settings.with_params(&json!({"artboardLayout": {"rows": 4}}), "file.new").unwrap(); let listed = settings.to_json(); assert_eq!((&listed["artboardLayout"]["columns"], &listed["artboardLayout"]["rows"]), (&json!(1), &json!(4)), "{listed}"); // Five artboards from the listing fill four rows, not two. let more = settings.with_params(&json!({"artboards": 5, "artboardLayout": listed["artboardLayout"]}), "file.new").unwrap(); assert_eq!(more.boards().iter().filter(|b| b.x0 == more.boards()[0].x0).count(), 4); } /// A layout that replaces the rows keeps the columns the rows gave the artboards there are now, /// whether or not the change of artboards came with a layout of its own. #[test] fn replacing_rows_keeps_the_columns_of_the_artboards_there_are_now() { use crate::cmd::newdoc::{ArtboardLayout, DocSettings}; let settings = DocSettings { artboards: 2, layout: ArtboardLayout::GridByColumn, ..DocSettings::default() }; let settings = settings.with_params(&json!({"artboardLayout": {"rows": 4}}), "file.new").unwrap(); for change in [json!({"artboards": 9}), json!({"artboards": 9, "artboardLayout": {"spacing": 20}})] { let more = settings.clone().with_params(&change, "file.new").unwrap(); for layout in [json!("gridByRow"), json!({"layout": "gridByRow"})] { let by_row = more.clone().with_params(&json!({"artboardLayout": layout}), "file.new").unwrap(); assert_eq!(by_row.grid_columns(), 3, "{change} then {layout}"); } } } #[test] fn stale_documents_throw_on_every_member() { let (mut s, _, _) = doc(); run(&mut s, "#targetengine keep\nvar d = app.activeDocument, p = d.pageItems, l = p[0].parent;").unwrap(); for (member, message) in [ ("d.typename", "Stale document"), ("d.parent", "Stale document"), ("p.typename", "Stale document"), ("p.toString()", "Stale document"), ("l.typename", "Stale"), ] { let e = failure(&mut s, &format!("#targetengine keep\n{member};")); assert!(e.contains(message), "{member}: {e}"); } } #[test] fn hiding_or_locking_a_selected_item_deselects_it() { let (mut s, a, b) = doc(); s.execute("select.set", &json!({"ids": [a.0, b.0]})).unwrap(); log(&mut s, "app.activeDocument.pageItems[1].hidden = true;"); assert_eq!(s.active().unwrap().selection.objects, vec![b]); log(&mut s, "app.activeDocument.pageItems[0].locked = true;"); assert!(s.active().unwrap().selection.objects.is_empty()); } #[test] fn an_edit_keeps_direct_selected_anchors() { let (mut s, a, _) = doc(); s.execute("select.anchors", &json!({"id": a.0, "anchors": [[0, 1]], "mode": "set"})).unwrap(); let before = s.active().unwrap().selection.clone(); assert!(!before.anchors.is_empty()); log(&mut s, "var p = app.activeDocument.pageItems; p[1].duplicate(); p[1].zOrder(ZOrderMethod.SENDTOBACK);"); assert_eq!(s.active().unwrap().selection.anchors, before.anchors); assert_eq!(s.active().unwrap().selection.objects, before.objects); assert!(s.journal.iter().any(|(id, _)| id == "select.anchorsMany"), "the journal holds the restore"); } /// A path saved with selected segments and no anchor entry gets its segments back, and only the /// anchors at their ends, which `select.segments` selects with them. #[test] fn an_edit_gives_a_path_with_only_segments_no_other_anchors() { let (mut s, a, b) = doc(); s.execute("select.segments", &json!({"id": a.0, "segments": [[0, 1]], "mode": "set"})).unwrap(); let ends = s.active().unwrap().selection.anchors.get(&a).cloned(); assert_eq!(ends.as_ref().map(|e| e.len()), Some(2)); s.execute("select.add", &json!({"ids": [b.0]})).unwrap(); s.active_mut().unwrap().selection.anchors.remove(&a); let before = s.active().unwrap().selection.clone(); assert!(before.segments.contains_key(&a) && !before.anchors.contains_key(&a)); log(&mut s, "var p = app.activeDocument.pageItems; p[1].duplicate(); p[1].zOrder(ZOrderMethod.SENDTOBACK);"); assert!(s.journal.iter().any(|(id, _)| id == "select.anchorsMany"), "the journal holds the restore"); let sel = &s.active().unwrap().selection; assert_eq!(sel.anchors.get(&a), ends.as_ref()); assert_eq!(sel.segments.get(&a), before.segments.get(&a)); let mut objects = sel.objects.clone(); objects.sort(); let mut was = before.objects.clone(); was.sort(); assert_eq!(objects, was, "the same objects are selected"); } #[test] fn selecting_an_item_keeps_the_direct_selected_anchors_of_others() { let (mut s, a, b) = doc(); s.execute("select.anchors", &json!({"id": a.0, "anchors": [[0, 1]], "mode": "set"})).unwrap(); s.execute("select.add", &json!({"ids": [b.0]})).unwrap(); let anchors = s.active().unwrap().selection.anchors.clone(); assert!(anchors.contains_key(&a)); // Lists start with the topmost object, so `b` is `pageItems[0]`. log(&mut s, "app.activeDocument.pageItems[0].selected = false;"); let sel = &s.active().unwrap().selection; assert_eq!((sel.objects.clone(), sel.anchors.clone()), (vec![a], anchors.clone())); log(&mut s, "app.activeDocument.pageItems[0].selected = true;"); let sel = &s.active().unwrap().selection; assert_eq!((sel.objects.clone(), sel.anchors.get(&a)), (vec![a, b], anchors.get(&a))); } #[test] fn an_edit_keeps_the_order_of_a_mixed_selection() { let (mut s, a, b) = doc(); rect(&mut s, 200.0, 200.0, 20.0, 20.0); s.execute("select.anchors", &json!({"id": a.0, "anchors": [[0, 1]], "mode": "set"})).unwrap(); s.execute("select.add", &json!({"ids": [b.0]})).unwrap(); let before = s.active().unwrap().selection.clone(); assert_eq!(before.objects, vec![a, b]); log(&mut s, "app.activeDocument.pageItems[2].duplicate();"); let sel = &s.active().unwrap().selection; assert_eq!((sel.objects.clone(), sel.anchors.clone()), (before.objects, before.anchors)); } /// A path with segments selected and no anchors gets the segments back, not every anchor. #[test] fn an_edit_keeps_a_path_with_only_a_segment_selected() { let (mut s, a, b) = doc(); rect(&mut s, 200.0, 200.0, 20.0, 20.0); s.execute("select.anchors", &json!({"id": a.0, "anchors": [[0, 1]], "mode": "set"})).unwrap(); s.execute("select.segments", &json!({"id": b.0, "segments": [[0, 1]], "mode": "add"})).unwrap(); s.doc_mut().unwrap().selection.anchors.remove(&b); let before = s.active().unwrap().selection.clone(); log(&mut s, "app.activeDocument.pageItems[2].duplicate();"); let sel = &s.active().unwrap().selection; assert_eq!((&sel.objects, &sel.segments, sel.anchors.get(&a)), (&before.objects, &before.segments, before.anchors.get(&a))); // The segment brings back its own two end points, as `select.segments` selects them, and none // of the path's other anchors: the journaled restore selects none of them first. assert_eq!(sel.anchors.get(&b).map(|k| k.len()), Some(2), "{sel:?}"); let restore = s.journal.iter().rev().find(|(id, _)| id == "select.anchorsMany").map(|(_, p)| p.clone()).unwrap(); let path = restore["items"].as_array().unwrap().iter().find(|i| i["id"] == json!(b.0)).cloned().unwrap(); assert_eq!(path["anchors"], json!([]), "{restore}"); } #[test] fn assigning_the_selection_clears_a_layers_panel_target() { let (mut s, a, _) = doc(); s.execute("layer.target", &json!({"id": a.0})).unwrap(); assert_eq!(s.active().unwrap().selection.target, Some(a)); log(&mut s, "var d = app.activeDocument; d.selection = d.pageItems[1];"); let sel = &s.active().unwrap().selection; assert_eq!((sel.objects.clone(), sel.target), (vec![a], None)); } #[test] fn an_edit_keeps_a_layers_panel_target() { let (mut s, a, b) = doc(); s.execute("layer.target", &json!({"id": a.0})).unwrap(); log(&mut s, "app.activeDocument.pageItems[1].duplicate();"); let sel = &s.active().unwrap().selection; assert_eq!((sel.objects.clone(), sel.target), (vec![a], Some(a))); // A targeted layer stays targeted, with its art selected. let layer = s.active().unwrap().doc.layers[0].id; s.execute("layer.target", &json!({"id": layer.0})).unwrap(); log(&mut s, "app.activeDocument.pageItems[1].duplicate();"); let sel = &s.active().unwrap().selection; assert_eq!((sel.target, sel.objects.len(), sel.objects.contains(&b)), (Some(layer), 4, true)); } #[test] fn an_edit_keeps_the_current_layer() { let (mut s, a, b) = doc(); let first = s.active().unwrap().doc.layers[0].id; let back = NodeId(s.execute("layer.new", &json!({})).unwrap()["id"].as_u64().unwrap()); let layer = |s: &Session| (s.active().unwrap().active_layer, s.active().unwrap().highlighted_rows()); assert_eq!(layer(&s), (Some(back), vec![back])); // Moving an object of the first layer selects it for the command; the new layer stays current. log(&mut s, "app.activeDocument.pageItems[0].left = 50;"); assert_eq!(layer(&s), (Some(back), vec![back])); assert_eq!(s.active().unwrap().selection.objects, vec![b]); // With nothing selected and no row highlighted, the layer stays current and no row is highlighted. s.execute("select.none", &json!({})).unwrap(); s.execute("layer.setCurrent", &json!({"id": back.0})).unwrap(); s.execute("layer.highlight", &json!({"ids": [], "mode": "set"})).unwrap(); assert_eq!(layer(&s), (Some(back), vec![])); log(&mut s, "app.activeDocument.pageItems[1].left = 120;"); assert_eq!(layer(&s), (Some(back), vec![])); assert!(s.active().unwrap().selection.objects.is_empty()); // Targeting a layer makes it current and leaves another layer's row highlighted; both stay. s.execute("layer.highlight", &json!({"ids": [first.0], "mode": "set"})).unwrap(); s.execute("layer.target", &json!({"id": back.0})).unwrap(); assert_eq!(layer(&s), (Some(back), vec![first])); log(&mut s, "app.activeDocument.pageItems[0].left = 70;"); assert_eq!(layer(&s), (Some(back), vec![first])); // Assigning the selection makes the layer of the selected object current, as a click does. log(&mut s, "app.activeDocument.selection = app.activeDocument.pageItems[1];"); assert_eq!(s.active().unwrap().active_layer, Some(first)); assert_eq!(s.active().unwrap().selection.objects, vec![a]); } /// A document with a rectangle at the bottom of the stack, then a dynamic symbol instance of two /// rectangles and an Objects on Path group of one rectangle: (session, instance, group, the /// group's rectangle). fn chosen_parts() -> (Session, NodeId, NodeId, NodeId) { let mut s = Session::new(); s.execute("file.new", &json!({"width": 400, "height": 300})).unwrap(); rect(&mut s, 300.0, 200.0, 20.0, 20.0); let (x, y) = (rect(&mut s, 10.0, 10.0, 20.0, 20.0), rect(&mut s, 50.0, 10.0, 20.0, 20.0)); let id = |v: Value| NodeId(v["id"].as_u64().unwrap()); let inst = id(s.execute("symbol.new", &json!({"name": "Pair", "ids": [x.0, y.0], "type": "dynamic"})).unwrap()); let member = rect(&mut s, 10.0, 100.0, 20.0, 20.0); let line = id(s.execute("shape.line", &json!({"x1": 50, "y1": 250, "x2": 350, "y2": 250})).unwrap()); s.execute("select.set", &json!({"ids": [member.0]})).unwrap(); let group = id(s.execute("object.objectsOnPath.attach", &json!({"path": line.0})).unwrap()); (s, inst, group, member) } #[test] fn an_edit_keeps_an_object_chosen_inside_a_symbol_or_on_a_path() { let (mut s, inst, group, member) = chosen_parts(); s.execute("symbol.instance.selectPart", &json!({"id": inst.0, "path": [1]})).unwrap(); let before = s.active().unwrap().selection.clone(); log(&mut s, "app.activeDocument.pageItems[0].duplicate();"); let sel = &s.active().unwrap().selection; assert_eq!((sel.objects.clone(), sel.symbol_part.clone()), (before.objects, before.symbol_part)); s.execute("object.objectsOnPath.selectMember", &json!({"member": member.0})).unwrap(); log(&mut s, "app.activeDocument.pageItems[0].duplicate();"); let sel = &s.active().unwrap().selection; assert_eq!((sel.objects.clone(), sel.on_path_member), (vec![group], Some(member))); } #[test] fn assigning_the_selection_selects_a_symbol_or_on_path_group_as_a_whole() { let (mut s, inst, group, member) = chosen_parts(); s.execute("symbol.instance.selectPart", &json!({"id": inst.0, "path": [1]})).unwrap(); log(&mut s, "var d = app.activeDocument; d.selection = d.selection[0];"); let sel = &s.active().unwrap().selection; assert_eq!((sel.objects.clone(), sel.symbol_part.clone()), (vec![inst], None)); s.execute("object.objectsOnPath.selectMember", &json!({"member": member.0})).unwrap(); log(&mut s, "var d = app.activeDocument; d.selection = d.selection[0];"); let sel = &s.active().unwrap().selection; assert_eq!((sel.objects.clone(), sel.on_path_member), (vec![group], None)); } #[test] fn a_held_image_is_stale_when_it_is_embedded() { let dir = super::links::Folder::new("dom-embed"); let photo = dir.file("photo.png"); super::links::write(&photo, &super::links::png(20, 10, [255, 0, 0])); let mut s = super::links::session(); let id = super::links::place(&mut s, &photo); let out = log( &mut s, &format!( "var i = app.activeDocument.pageItems[0]; vc.log(i.typename); vc.run('links.embed', {{ids: [{}]}});\ var j = app.activeDocument.pageItems[0], m = ''; try {{ i.typename; }} catch (e) {{ m = e.message; }}\ vc.log([i === j, m, j.typename, j instanceof RasterItem, i instanceof PlacedItem]);", id.0 ), ); assert_eq!(out, vec![json!("PlacedItem"), json!([false, "Stale page item", "RasterItem", true, true])]); } #[test] fn an_edit_keeps_the_key_object() { let (mut s, a, b) = doc(); s.execute("select.set", &json!({"ids": [a.0, b.0]})).unwrap(); s.execute("select.key", &json!({"id": b.0})).unwrap(); log(&mut s, "var p = app.activeDocument.pageItems; p[0].duplicate();"); let sel = &s.active().unwrap().selection; assert_eq!((sel.objects.clone(), sel.key), (vec![a, b], Some(b))); } #[test] fn an_edit_keeps_selected_slices() { let (mut s, a, _) = doc(); s.execute("object.slice.make", &json!({"ids": [a.0]})).unwrap(); s.execute("object.slice.select", &json!({"slices": [a.0]})).unwrap(); log(&mut s, "app.activeDocument.pageItems[1].duplicate();"); let sel = &s.active().unwrap().selection; assert_eq!((sel.slices.clone(), sel.objects.len()), (vec![a], 0)); } #[test] fn a_startup_preset_alone_gives_its_first_size() { let mut s = Session::new(); for profile in ["Web", "Print", "Mobile"] { let first = crate::cmd::newdoc::category(&s, profile).unwrap().remove(0); let out = log(&mut s, &format!("var d = app.documents.addDocument('{profile}'); vc.log([d.width, d.height]);")); let got = (out[0][0].as_f64(), out[0][1].as_f64()); assert_eq!(got, (Some(first.width), Some(first.height)), "{profile}"); } } #[test] fn page_item_members_not_built_yet_throw_on_read_and_assignment() { let (mut s, _, _) = doc(); for source in [ "app.activeDocument.pageItems[0].isIsolated;", "app.activeDocument.pageItems[0].uuid = 'x';", "app.activeDocument.pageItems[0].bringInPerspective(0, 0);", ] { assert!(failure(&mut s, source).contains("is not available yet"), "{source}"); } assert!(failure(&mut s, "app.activeDocument.swatches = 1;").contains("Document.swatches is not available yet")); // A deleted item or an earlier run's document is stale first. let e = failure(&mut s, "var i = app.activeDocument.pageItems[0]; i.remove(); i.isIsolated;"); assert!(e.contains("Stale page item"), "{e}"); run(&mut s, "#targetengine keep\nvar d = app.activeDocument;").unwrap(); let e = failure(&mut s, "#targetengine keep\nd.layers;"); assert!(e.contains("Stale document"), "{e}"); } #[test] fn assigning_the_same_objects_clears_the_key_and_guides() { let (mut s, a, b) = doc(); s.execute("guide.add", &json!({"vertical": true, "pos": 120})).unwrap(); s.execute("select.set", &json!({"ids": [a.0, b.0]})).unwrap(); s.execute("select.key", &json!({"id": b.0})).unwrap(); s.execute("guide.select", &json!({"indexes": [0], "toggle": true})).unwrap(); assert!(!s.active().unwrap().selection.guides.is_empty()); log(&mut s, "var d = app.activeDocument; d.selection = [d.pageItems[1], d.pageItems[0]];"); let sel = &s.active().unwrap().selection; assert_eq!((sel.objects.clone(), sel.key, sel.guides.len()), (vec![a, b], None, 0)); s.execute("guide.select", &json!({"indexes": [0]})).unwrap(); log(&mut s, "app.activeDocument.selection = null;"); assert!(s.active().unwrap().selection.guides.is_empty()); } /// Assigning `undefined` or a value that is not a page item to the selection throws and keeps it. #[test] fn assigning_undefined_to_the_selection_throws_and_keeps_it() { let (mut s, a, b) = doc(); s.execute("select.set", &json!({"ids": [a.0, b.0]})).unwrap(); for source in ["var u; app.activeDocument.selection = u;", "app.activeDocument.selection = undefined;", "app.activeDocument.selection = 1;"] { let e = failure(&mut s, source); assert!(e.contains("TypeError"), "{source}: {e}"); assert_eq!(s.active().unwrap().selection.objects, vec![a, b], "{source}"); } } #[test] fn a_page_item_with_a_large_id_keeps_its_id() { let mut s = Session::new(); s.execute("file.new", &json!({"width": 400, "height": 300})).unwrap(); let big = (1u64 << 53) + 1; s.active_mut().unwrap().change_unsaved(|d| std::sync::Arc::make_mut(d).raise_next_id(big)); let a = rect(&mut s, 10.0, 20.0, 30.0, 40.0); assert_eq!(a.0, big); let out = log(&mut s, "var i = app.activeDocument.pageItems[0]; i.name = 'big'; vc.log(i.name); i.remove();"); assert_eq!(out, vec![json!("big")]); assert!(s.active().unwrap().doc.node(a).is_none()); } /// A path the script makes keeps its id when the id is past 2^53, where the script's numbers round. #[test] fn a_path_made_with_a_large_id_keeps_its_id() { let mut s = Session::new(); s.execute("file.new", &json!({"width": 400, "height": 300})).unwrap(); let big = (1u64 << 53) + 1; s.active_mut().unwrap().change_unsaved(|d| std::sync::Arc::make_mut(d).raise_next_id(big)); let out = log(&mut s, "var p = app.activeDocument.pathItems.rectangle(10, 10, 20, 20); p.name = 'big'; vc.log(p.name);"); assert_eq!(out, vec![json!("big")]); let d = &s.active().unwrap().doc; assert_eq!(d.node(NodeId(big)).and_then(|n| n.name.clone()).as_deref(), Some("big")); } /// Assigning `undefined` to the selection, as a lookup past the end of a list gives, throws and /// leaves the selection as it was; `null` still clears it. #[test] fn assigning_undefined_to_the_selection_throws() { let (mut s, a, _) = doc(); s.execute("select.set", &json!({"ids": [a.0]})).unwrap(); let e = failure(&mut s, "var d = app.activeDocument; d.selection = d.pageItems[99];"); assert!(e.contains("TypeError") || e.contains("not undefined"), "{e}"); assert_eq!(s.active().unwrap().selection.objects, vec![a]); log(&mut s, "app.activeDocument.selection = null;"); assert!(s.active().unwrap().selection.objects.is_empty()); } #[test] fn only_class_constructors_construct() { let (mut s, _, _) = doc(); let out = log( &mut s, "var m = [];\ try { new app.documents.add(); } catch (e) { m.push(e.name); }\ try { new app.activeDocument.pageItems[0].remove(); } catch (e) { m.push(e.name); }\ vc.log(m); vc.log(app.documents.length); vc.log(app.activeDocument.pageItems.length);", ); assert_eq!(out, vec![json!(["TypeError", "TypeError"]), json!(1), json!(2)]); } #[test] fn a_document_preset_is_an_instance_of_its_class() { let mut s = Session::new(); let out = log(&mut s, "var p = new DocumentPreset(); vc.log(p instanceof DocumentPreset); vc.log(p.typename); vc.log(p.width);"); assert_eq!(out, vec![json!(true), json!("DocumentPreset"), json!(612)]); // A named engine keeps the preset, but in a later run its type name throws, as its settings do. run(&mut s, "#targetengine keep\nvar held = new DocumentPreset();").unwrap(); let e = failure(&mut s, "#targetengine keep\nheld.typename;"); assert!(e.contains("this is not a DocumentPreset"), "{e}"); } /// A preset kept from an earlier run throws from `typename` and `toString()` as it does from its /// settings. #[test] fn a_document_preset_from_an_earlier_run_throws_from_typename_and_to_string() { let mut s = Session::new(); run(&mut s, "#targetengine keep\nvar p = new DocumentPreset();").unwrap(); for member in ["p.width", "p.typename", "p.toString()", "String(p)"] { let e = failure(&mut s, &format!("#targetengine keep\n{member};")); assert!(e.contains("TypeError") && e.contains("not a DocumentPreset"), "{member}: {e}"); } } /// The values the presets hold count against the run's heap, a value assigned again in place of /// the one before it. #[test] fn document_preset_values_are_bounded_by_the_runs_heap() { use crate::script::TEST_ENGINE_HEAP; TEST_ENGINE_HEAP.with(|h| h.set(Some(2 << 20))); let mut s = Session::new(); // A title of 4,092 bytes, which with the loop counter stays within the 4,096 a title may have. let big = "var s = 'a'; while (s.length < 4092) s += 'a';"; let again = format!("#targetengine presets\n{big} var p = new DocumentPreset(); for (var k = 0; k < 1000; k++) p.title = s + k; vc.log(p.title.length);"); assert_eq!(log(&mut s, &again), vec![json!(4095)]); let many = format!( "#targetengine presets\n{big} var m = '';\ try {{ for (var k = 0; k < 2000; k++) {{ var p = new DocumentPreset(); p.title = s; }} }} catch (e) {{ m = e.name + ': ' + e.message; }}\ vc.log(k < 2000); vc.log(m);" ); let out = log(&mut s, &many); TEST_ENGINE_HEAP.with(|h| h.set(None)); assert_eq!(out[0], json!(true), "{out:?}"); assert!(out[1].as_str().unwrap().starts_with("RangeError: the DocumentPreset settings"), "{out:?}"); } /// An array a preset holds counts a whole value for each element: presets that each hold a /// `documentBleedOffsetRect` of four numbers reach the run's heap after fewer presets than ones /// that each hold an eight-byte `title`, which counts one value and its text. #[test] fn document_preset_arrays_count_each_element_they_hold() { use crate::script::TEST_ENGINE_HEAP; let made = |assign: &str| { TEST_ENGINE_HEAP.with(|h| h.set(Some(1 << 20))); let mut s = Session::new(); let out = log( &mut s, &format!( "#targetengine presets\nvar m = '';\ try {{ for (var k = 0; k < 10000; k++) {{ var p = new DocumentPreset(); {assign} }} }} catch (e) {{ m = e.name + ': ' + e.message; }}\ vc.log(k); vc.log(m);" ), ); TEST_ENGINE_HEAP.with(|h| h.set(None)); assert!(out[1].as_str().unwrap().starts_with("RangeError: the DocumentPreset settings"), "{assign}: {out:?}"); out[0].as_f64().unwrap() }; let with_title = made("p.title = 'abcdefgh';"); let with_rect = made("p.documentBleedOffsetRect = [1, 2, 3, 4];"); assert!(with_rect < with_title, "{with_rect} presets with a rectangle, {with_title} with a title"); } #[test] fn the_same_document_or_item_is_the_same_object() { let (mut s, a, _) = doc(); s.execute("select.set", &json!({"ids": [a.0]})).unwrap(); let out = log( &mut s, "var d = app.activeDocument, p = d.pageItems;\ vc.log([d === app.documents[0], p[1] === p[1], p[1] === d.selection[0], p[0] !== p[1], p === d.pageItems]);", ); assert_eq!(out, vec![json!([true, true, true, true, true])]); } #[test] fn collection_entries_are_found_by_in_and_array_methods() { let (mut s, _, _) = doc(); let out = log( &mut s, "var p = app.activeDocument.pageItems, d = app.documents;\ vc.log([0 in p, '1' in p, 2 in p, 0 in d, 1 in d]);\ vc.log(Array.prototype.map.call(p, function (i) { return i.typename; }).join(','));", ); assert_eq!(out, vec![json!([true, true, false, true, false]), json!("PathItem,PathItem")]); } #[test] fn placement_in_another_document_is_refused() { let (mut s, _, _) = doc(); s.execute("file.new", &json!({"width": 100, "height": 100})).unwrap(); rect(&mut s, 0.0, 0.0, 5.0, 5.0); let before: Vec = s.documents().iter().map(|d| format!("{:?}", d.doc.layers)).collect(); for source in [ "app.documents[0].pageItems[0].move(app.documents[1].pageItems[0], ElementPlacement.PLACEBEFORE);", "app.documents[0].pageItems[0].duplicate(app.documents[1].pageItems[0].parent, ElementPlacement.INSIDE);", ] { let e = failure(&mut s, source); assert!(e.contains("Cross-document placement is not available"), "{source}: {e}"); } let after: Vec = s.documents().iter().map(|d| format!("{:?}", d.doc.layers)).collect(); assert_eq!(after, before); } #[test] fn note_is_the_attributes_note() { let (mut s, a, _) = doc(); let steps = undo_steps(&s); let out = log(&mut s, "var i = app.activeDocument.pageItems[1]; vc.log(i.note); i.note = 'check bleed'; vc.log(i.note);"); assert_eq!(out, vec![json!(""), json!("check bleed")]); assert_eq!(s.active().unwrap().doc.node(a).unwrap().extras().attrs.as_ref().unwrap().note, "check bleed"); assert_eq!(undo_steps(&s), steps + 1); log(&mut s, "app.activeDocument.pageItems[1].note = 'check bleed';"); assert_eq!(undo_steps(&s), steps + 1, "the same note adds no step"); // A number or a Boolean becomes its text, as it does for `name`. let out = log(&mut s, "var i = app.activeDocument.pageItems[1]; i.note = 5; vc.log([typeof i.note, i.note]); i.note = true; vc.log(i.note);"); assert_eq!(out, vec![json!(["string", "5"]), json!("true")]); for value in ["null", "undefined", "{}"] { assert!(failure(&mut s, &format!("app.activeDocument.pageItems[0].note = {value};")).contains("note must be a string"), "{value}"); } assert!(failure(&mut s, "app.activeDocument.pageItems[0].note = new Array(5000).join('n');").contains("too long")); } #[test] fn add_document_starts_from_the_built_in_preset_when_a_saved_one_has_its_name() { let mut s = Session::new(); s.execute("file.newPresets.save", &json!({"name": "Letter", "width": 100, "height": 50, "colorMode": "rgb"})).unwrap(); let out = log(&mut s, "var d = app.documents.addDocument('Print'); vc.log([d.width, d.height, d.documentColorSpace]);"); assert_eq!(out[0], json!([612, 792, "DocumentColorSpace.CMYK"])); // Recent keeps the built-in preset's name, as it does when no saved preset has it. assert_eq!(s.prefs.recent_new_docs[0].name, "Letter"); let out = log(&mut s, "var p = new DocumentPreset(); p.width = 300; var e = app.documents.addDocument('Print', p); vc.log([e.width, e.height]);"); assert_eq!(out[0], json!([300, 792])); assert_eq!(s.prefs.recent_new_docs[0].name, "Custom"); } #[test] fn a_profile_starts_from_its_built_in_preset_and_not_a_saved_one_of_the_same_name() { let mut s = Session::new(); s.execute( "file.newPresets.save", &json!({"name": "Letter", "width": 100, "height": 50, "units": "Millimeters", "colorMode": "rgb", "artboardLayout": "row"}), ) .unwrap(); let out = log(&mut s, "var d = app.documents.addDocument('Print'); vc.log([d.width, d.height, d.documentColorSpace]);"); assert_eq!(out, vec![json!([612, 792, "DocumentColorSpace.CMYK"])]); let out = log(&mut s, "var p = new DocumentPreset(); p.width = 300; var d = app.documents.addDocument('Print', p); vc.log([d.width, d.height]);"); assert_eq!(out, vec![json!([300, 792])], "presetSettings goes over the built-in preset"); // Several artboards are laid out as the built-in preset lays them out when no saved preset // has its name, and the title is kept. let three = "var p = new DocumentPreset(); p.numArtboards = 3; p.title = 'Three';\ p.artboardLayout = undefined; p.artboardRowsOrCols = undefined; p.artboardSpacing = undefined; var d = app.documents.addDocument('Print', p);\ var r = []; for (var i = 0; i < d.artboards.length; i++) r.push(d.artboards[i].artboardRect); vc.log([d.name, r]);"; let out = log(&mut s, three); let unshadowed = log(&mut Session::new(), three); assert_eq!(out[0][0], json!("Three")); assert_eq!(out[0][1].as_array().map(Vec::len), Some(3), "{out:?}"); assert_eq!(out, unshadowed, "the saved preset changes nothing"); } #[test] fn document_preset_applies_only_its_given_fields() { let mut s = Session::new(); let out = log( &mut s, "var p = new DocumentPreset(); vc.log([p.typename, p.width, p.height, p.numArtboards, p.artboardSpacing, p.artboardLayout]);\ vc.log(p.colorMode === undefined && p.units === undefined);\ p.width = 300; p.height = 200; p.numArtboards = 2; p.colorMode = DocumentColorSpace.CMYK; p.title = 'Flyer';\ p.units = RulerUnits.Millimeters; p.rasterResolution = DocumentRasterResolution.HighResolution;\ var d = app.documents.addDocument('Print', p); vc.log([d.name, d.width, d.height, d.documentColorSpace, d.rulerUnits]);\ var e = app.documents.addDocument(DocumentPresetType.BasicRGB); vc.log(e.documentColorSpace);", ); assert_eq!(out[0], json!(["DocumentPreset", 612, 792, 1, 20, "DocumentArtboardLayout.GridByRow"])); assert_eq!(out[1], json!(true)); assert_eq!(out[2], json!(["Flyer", 300, 200, "DocumentColorSpace.CMYK", "RulerUnits.Millimeters"])); assert_eq!(out[3], json!("DocumentColorSpace.RGB")); assert_eq!(s.documents().len(), 2); assert_eq!(s.documents()[0].doc.artboards.len(), 2); assert!((s.documents()[0].doc.raster_effects_ppi - 300.0).abs() < 1e-9); for (source, part) in [ ("app.documents.addDocument('Nope');", "no startup preset"), ("var p = new DocumentPreset(); p.numArtboards = 101; app.documents.addDocument('Print', p);", "numArtBoards"), ("var p = new DocumentPreset(); p.units = RulerUnits.Qs; app.documents.addDocument('Print', p);", "units"), ( "var p = new DocumentPreset(); p.transparencyGrid = DocumentTransparencyGrid.TransparencyGridRed; app.documents.addDocument('Print', p);", "not available yet", ), ("app.documents.addDocument('Print', 5);", "DocumentPreset"), ("app.documents.addDocument(7);", "startup preset"), ] { let e = failure(&mut s, source); assert!(e.contains(part), "{source}: {e}"); } assert_eq!(s.documents().len(), 2, "a refused preset makes no document"); // A saved preset with the built-in preset's name does not make the refused settings fit. s.execute("file.newPresets.save", &json!({"name": "Letter", "artboardLayout": "column"})).unwrap(); // With the layout unset, the built-in preset puts the five 1,000,000 pt artboards in one row, // which does not fit, while the saved preset would put them in one column. let e = failure( &mut s, "var p = new DocumentPreset(); p.width = 1000000; p.numArtboards = 5;\ p.artboardLayout = undefined; p.artboardRowsOrCols = undefined; app.documents.addDocument('Print', p);", ); assert!(e.contains("don't fit"), "{e}"); assert_eq!(s.documents().len(), 2, "settings the built-in preset refuses make no document"); } #[test] fn a_startup_preset_uses_its_own_size_beside_a_saved_preset_of_the_same_name() { let mut s = Session::new(); s.execute("file.newPresets.save", &json!({"name": "Letter", "width": 300, "height": 200})).unwrap(); let first = crate::cmd::newdoc::category(&s, "Print").unwrap().remove(0); assert_eq!(first.name, "Letter"); let out = log(&mut s, "var d = app.documents.addDocument('Print'); vc.log([d.width, d.height]);"); assert_eq!((out[0][0].as_f64(), out[0][1].as_f64()), (Some(first.width), Some(first.height)), "{out:?}"); } #[test] fn document_presets_hold_a_limited_amount_of_data() { let mut s = Session::new(); // A 4,096-byte title fits and replacing it does not add up; one byte more is refused. let out = log( &mut s, "var t = 'x'; for (var k = 0; k < 12; k++) t += t; var p = new DocumentPreset();\ for (var k = 0; k < 20; k++) p.title = t; vc.log(p.title.length);", ); assert_eq!(out, vec![json!(4096)]); let e = failure(&mut s, "var t = 'x'; for (var k = 0; k < 12; k++) t += t; var p = new DocumentPreset(); p.title = t + 'x';"); assert!(e.contains("the title is too long"), "{e}"); } #[test] fn moving_art_on_another_layer_keeps_the_current_layer() { let mut s = Session::new(); s.execute("file.new", &json!({"width": 400, "height": 300})).unwrap(); let a = s.active().unwrap().current_layer().unwrap(); s.execute("layer.new", &json!({})).unwrap(); let b = s.active().unwrap().current_layer().unwrap(); assert_ne!(a, b); rect(&mut s, 10.0, 20.0, 30.0, 40.0); s.execute("select.none", &json!({})).unwrap(); s.active_mut().unwrap().active_layer = Some(a); log(&mut s, "var i = app.activeDocument.pageItems[0]; i.left = i.left + 5; i.translate(5, 0);"); assert_eq!(s.active().unwrap().current_layer(), Some(a)); assert!(s.active().unwrap().selection.objects.is_empty()); } #[test] fn headless_add_document_dialog_fails_without_creating() { let mut s = Session::new(); let e = failure(&mut s, "app.documents.addDocument('Print', new DocumentPreset(), true);"); assert!(e.contains("Dialog unavailable"), "{e}"); assert!(s.documents().is_empty()); let out = log(&mut s, "vc.log(app.documents.addDocument('Web', undefined, false).typename);"); assert_eq!(out, vec![json!("Document")]); assert_eq!(s.documents().len(), 1); } /// A run's `addDocument` with Overprint Preview turns the view on when the run ends; a run that /// fails leaves it off. #[test] fn add_document_with_overprint_preview_turns_it_on_when_the_run_ends() { if !super::in_own_process("tests::script_dom::add_document_with_overprint_preview_turns_it_on_when_the_run_ends") { return; } let mut s = Session::new(); let overprint = |s: &mut Session| s.execute("view.proofSetup", &json!({})).unwrap()["overprintPreview"].clone(); s.execute("view.overprintPreview", &json!({"on": false})).unwrap(); let add = "var p = new DocumentPreset(); p.previewMode = DocumentPreviewMode.OverprintPreview; app.documents.addDocument('Print', p);"; let e = failure(&mut s, &format!("{add} throw new Error('stopped here');")); assert!(e.contains("stopped here"), "{e}"); assert_eq!(overprint(&mut s), false); assert!(s.documents().is_empty()); assert_eq!(log(&mut s, &format!("{add} vc.log(app.documents.length);")), vec![json!(1)]); assert_eq!(overprint(&mut s), true); assert_eq!(s.documents().len(), 1); s.execute("view.overprintPreview", &json!({"on": false})).unwrap(); } /// A run's `addDocument` reads the images the profile links to beside its file, as the person's /// File › New does. #[test] fn add_document_reads_the_images_the_profile_links_to() { let dir = std::env::temp_dir().join(format!("vc-script-newdoc-links-{}", std::process::id())); let _ = std::fs::remove_dir_all(&dir); let mut s = Session::new(); s.attach_new_doc_profiles(dir.clone()); s.execute("file.newProfiles.edit", &json!({"name": "Print"})).unwrap(); let img = craft_image_codecs::Image::from_raw( 600, 300, craft_image_codecs::ChannelLayout::Rgba, craft_image_codecs::SampleType::U8, [200u8, 30, 30, 255].repeat(600 * 300), ) .unwrap(); let mut bytes = vec![]; craft_image_codecs::encode(&img, craft_image_codecs::Format::Png, &craft_image_codecs::EncodeOptions::default()) .map(|b| bytes.extend_from_slice(&b)) .unwrap(); let pic = dir.join("logo.png"); std::fs::write(&pic, &bytes).unwrap(); s.execute("file.place", &json!({"path": pic.to_string_lossy(), "link": true})).unwrap(); s.execute("symbol.new", &json!({"name": "Logo"})).unwrap(); s.execute("document.save", &json!({})).unwrap(); let read = |s: &Session| { let images = &s.doc().unwrap().doc.images; !images.is_empty() && images.values().all(|b| !b.is_proxy()) }; s.execute("file.new", &json!({"preset": "Letter"})).unwrap(); assert!(read(&s), "the person's File › New reads the linked image"); // No document open when the run begins links to the image, so the run's gate alone does not // let it be read. while !s.documents().is_empty() { s.execute("file.close", &json!({})).unwrap(); } log(&mut s, "app.documents.addDocument('Print');"); assert!(read(&s), "a script's addDocument reads it too"); let _ = std::fs::remove_dir_all(&dir); } /// `addDocumentNoUI(startupPreset)` makes the document `addDocument(startupPreset)` makes, and /// ignores the arguments after the preset's name. #[test] fn add_document_no_ui_makes_the_startup_presets_document() { let mut s = Session::new(); let out = log( &mut s, "var a = app.documents.addDocument('Web'); var p = new DocumentPreset(); p.width = 333;\ var b = app.documents.addDocumentNoUI('Web', p, true);\ vc.log([b.typename, b.width === a.width, b.height === a.height, b.width === 333, app.documents.length]);", ); assert_eq!(out, vec![json!(["Document", true, true, false, 2])]); let e = failure(&mut s, "app.documents.addDocumentNoUI('No Such Preset');"); assert!(e.contains("no startup preset called No Such Preset"), "{e}"); assert_eq!(s.documents().len(), 2); } #[test] fn setting_selected_keeps_how_the_other_objects_are_selected() { let (mut s, a, b) = doc(); let c = rect(&mut s, 200.0, 100.0, 50.0, 50.0); s.execute("select.set", &json!({"ids": [b.0]})).unwrap(); s.execute("select.anchors", &json!({"id": a.0, "anchors": [[0, 0]], "mode": "add"})).unwrap(); let anchors = s.active().unwrap().selection.anchors.get(&a).cloned(); assert!(anchors.is_some()); log( &mut s, "var p = app.activeDocument.pageItems;\ for (var i = 0; i < p.length; i++) { if (p[i].left == 200) p[i].selected = true; if (p[i].left == 100) p[i].selected = false; }", ); let sel = &s.active().unwrap().selection; assert_eq!(sel.objects, vec![a, c]); assert_eq!(sel.anchors.get(&a).cloned(), anchors, "the direct-selected anchors stay"); } #[test] fn add_document_takes_only_a_document_preset() { let mut s = Session::new(); for source in [ "app.documents.addDocument('Print', {width: 500});", "app.documents.addDocument('Print', {});", "app.documents.addDocument('Print', {typename: 'DocumentPreset', width: 500});", "var p = new DocumentPreset(); var q = {}; for (var k in p) q[k] = p[k]; app.documents.addDocument('Print', q);", "app.documents.addDocument('Print', [612, 792]);", "app.documents.addDocument('Print', 'DocumentPreset');", ] { let e = failure(&mut s, source); assert!(e.contains("TypeError") && e.contains("presetSettings must be a DocumentPreset"), "{source}: {e}"); } assert!(s.documents().is_empty(), "a refused preset makes no document"); let out = log(&mut s, "var p = new DocumentPreset(); p.width = 500; vc.log(app.documents.addDocument('Print', p).width);"); assert_eq!(out, vec![json!(500)]); } #[test] fn document_preset_settings_are_limited_in_bytes() { use crate::script::TEST_ENGINE_HEAP; TEST_ENGINE_HEAP.with(|h| h.set(Some(2 << 20))); let mut s = Session::new(); let out = log( &mut s, "#targetengine presets\nvar title = 'a'; while (title.length < 4092) title += 'a';\ var presets = [];\ for (var k = 0; k < 1000; k++) { var p = new DocumentPreset(); p.title = 'old'; presets.push(p); }\ var message = '';\ try { for (var k = 0; k < presets.length; k++) presets[k].title = title; }\ catch (e) { message = e.name + ': ' + e.message; }\ vc.log(message); vc.log(k > 0 && k < presets.length); vc.log(presets[k].title);\ presets[0].title = 'x'; presets[k].title = title;\ vc.log(presets[0].title); vc.log(presets[k].title === title);", ); TEST_ENGINE_HEAP.with(|h| h.set(None)); assert_eq!( out, vec![ json!(format!("RangeError: the DocumentPreset settings would hold more than {} bytes", 2 << 20)), json!(true), json!("old"), json!("x"), json!(true), ] ); } #[test] fn a_new_document_preset_is_refused_past_the_byte_limit() { use crate::script::TEST_ENGINE_HEAP; let preset_bytes = 6 * std::mem::size_of::() + "DocumentArtboardLayout.GridByRow".len(); TEST_ENGINE_HEAP.with(|h| h.set(Some(2 << 20))); let mut s = Session::new(); let out = log( &mut s, &format!( "#targetengine presets\nvar title = 'a'; while (title.length < 4092) title += 'a';\ var first = new DocumentPreset(); first.title = title;\ try {{ for (var k = 0; k < 2000; k++) {{ var p = new DocumentPreset(); p.title = title; }} }} catch (e) {{}}\ var message = '';\ try {{ for (var k = 0; k < 10000; k++) new DocumentPreset(); }}\ catch (e) {{ message = e.name + ': ' + e.message; }}\ vc.log(message); vc.log(k < 10000);\ first.title = title.substring({preset_bytes});\ vc.log((new DocumentPreset()).width);\ var again = ''; try {{ new DocumentPreset(); }} catch (e) {{ again = e.name + ': ' + e.message; }}\ vc.log(again);" ), ); TEST_ENGINE_HEAP.with(|h| h.set(None)); let message = json!(format!("RangeError: the DocumentPreset settings would hold more than {} bytes", 2 << 20)); assert_eq!(out, vec![message.clone(), json!(true), json!(612), message]); } #[test] fn add_document_applies_the_profile_file_and_throws_when_it_cannot_be_read() { let root = vectorcraft_testkit::TempDir::new("script-add-document-profile"); let dir = root.join("profiles"); let mut s = Session::new(); s.attach_new_doc_profiles(dir.clone()); // A profile with no file gives the defaults. let out = log(&mut s, "vc.log(app.documents.addDocument('Web').typename);"); assert_eq!(out, vec![json!("Document")]); assert_eq!(s.doc().unwrap().links_from, None); // A script reads the profile's file in the app's own folder, with no document open from it. s.execute("file.newProfiles.edit", &json!({"name": "Print"})).unwrap(); while !s.documents().is_empty() { s.execute("file.close", &json!({})).unwrap(); } let file = dir.join("Print.vectorcraft").to_string_lossy().to_string(); log(&mut s, "app.documents.addDocument('Print');"); assert_eq!(s.doc().unwrap().links_from.as_deref(), Some(file.as_str())); while !s.documents().is_empty() { s.execute("file.close", &json!({})).unwrap(); } std::fs::write(dir.join("Print.vectorcraft"), b"not a document").unwrap(); // file.new itself makes the document with the defaults and reports the profile it could not read. let r = s.execute("file.new", &json!({"preset": "Letter"})).unwrap(); assert_eq!(r["profileError"]["profile"], "Print", "{r}"); s.execute("file.close", &json!({})).unwrap(); let recent = s.execute("file.newPresets", &json!({"category": "Recent"})).unwrap(); let e = failure(&mut s, "app.documents.addDocument('Print');"); assert!(e.contains("Error: the New Document profile Print could not be read"), "{e}"); assert!(s.documents().is_empty(), "the document made with the defaults is put back"); // A script that catches the error finds nothing changed. let out = log( &mut s, "var p = new DocumentPreset(); p.width = 333;\ try { app.documents.addDocument('Print', p); } catch (e) { vc.log(e.name); } vc.log(app.documents.length);", ); assert_eq!(out, vec![json!("Error"), json!(0)]); assert_eq!(s.execute("file.newPresets", &json!({"category": "Recent"})).unwrap(), recent, "the Recent sizes are as they were"); // A startup preset with no profile reads no file. let out = log(&mut s, "vc.log(app.documents.addDocument('Basic RGB').documentColorSpace);"); assert_eq!(out, vec![json!("DocumentColorSpace.RGB")]); } #[test] fn add_document_names_a_color_profile_of_the_profile_file_that_cannot_be_used() { let root = vectorcraft_testkit::TempDir::new("script-add-document-warning"); let dir = root.join("profiles"); let mut s = Session::new(); s.attach_new_doc_profiles(dir.clone()); s.execute("file.newProfiles.edit", &json!({"name": "Print"})).unwrap(); while !s.documents().is_empty() { s.execute("file.close", &json!({})).unwrap(); } let file = dir.join("Print.vectorcraft"); let mut v: Value = serde_json::from_slice(&std::fs::read(&file).unwrap()).unwrap(); let broken = format!("Script profile broken {}", std::process::id()); v["profiles"] = json!({ &broken: {"mime": "application/vnd.iccprofile", "data": "AAAA"} }); std::fs::write(&file, serde_json::to_vec(&v).unwrap()).unwrap(); let r = run(&mut s, "vc.log(app.documents.addDocument('Print').typename);").unwrap(); assert_eq!(r["log"], json!(["Document"])); let lines = r["console"]["lines"].as_array().cloned().unwrap_or_default(); let named = lines.iter().filter_map(Value::as_str).any(|l| l.starts_with("addDocument: ") && l.contains(&broken)); assert!(named, "{lines:?}"); assert_eq!(s.documents().len(), 1); } #[test] fn add_document_refuses_pixel_preview() { let mut s = Session::new(); let e = failure(&mut s, "var p = new DocumentPreset(); p.previewMode = DocumentPreviewMode.PixelPreview; app.documents.addDocument('Web', p);"); assert!(e.contains("DocumentPreviewMode.PixelPreview is not available yet"), "{e}"); assert!(s.documents().is_empty()); let out = log( &mut s, "var p = new DocumentPreset(); p.previewMode = DocumentPreviewMode.DefaultPreview; vc.log(app.documents.addDocument('Web', p).typename);", ); assert_eq!(out, vec![json!("Document")]); } #[test] fn document_preset_values_count_toward_the_data_a_run_passes() { // A 4 KiB title, set once, then read or handed to addDocument 512 times: 2 MiB, over a 1 MiB limit. crate::script::TEST_BRIDGE_BYTES.with(|b| b.set(Some(1 << 20))); let mut s = Session::new(); let set = "var p = new DocumentPreset(); p.title = new Array(4097).join('t');"; let read = failure(&mut s, &format!("{set} for (var i = 0; i < 512; i++) p.title;")); let added = failure(&mut s, &format!("{set} for (var i = 0; i < 512; i++) try {{ app.documents.addDocument('Print', p, 5); }} catch (e) {{}}")); let within = run(&mut s, &format!("{set} for (var i = 0; i < 8; i++) p.title;")); crate::script::TEST_BRIDGE_BYTES.with(|b| b.set(None)); for e in [read, added] { assert!(e.contains("passed more data"), "{e}"); } within.unwrap(); assert!(s.documents().is_empty()); } #[test] fn document_preset_setter_refuses_a_value_of_the_wrong_kind_or_size_and_keeps_the_old_one() { let mut s = Session::new(); let out = log( &mut s, "var p = new DocumentPreset(); p.title = 'Flyer';\ var bad = [['title', new Array(4098).join('t')], ['title', 5], ['width', 'wide'], ['width', Infinity], ['height', null],\ ['colorMode', 'RGB'], ['units', {}], ['artboardLayout', 'DocumentColorSpace.RGB'], ['documentBleedLink', 1],\ ['documentBleedOffsetRect', [1, 2, 3]], ['documentBleedOffsetRect', [1, 2, 3, 'x']], ['transparencyGrid', 'TransparencyGridRed']];\ for (var i = 0; i < bad.length; i++) { try { p[bad[i][0]] = bad[i][1]; vc.log('kept ' + bad[i][0]); } catch (e) { vc.log(e.name); } }\ vc.log([p.title, p.width, p.height, p.colorMode === undefined, p.units === undefined, p.artboardLayout,\ p.documentBleedLink === undefined, p.documentBleedOffsetRect === undefined, p.transparencyGrid === undefined]);", ); let mut names = vec![json!("RangeError")]; names.extend(std::iter::repeat_n(json!("TypeError"), 11)); assert_eq!(out[..12], names[..], "{out:?}"); assert_eq!(out[12], json!(["Flyer", 612, 792, true, true, "DocumentArtboardLayout.GridByRow", true, true, true])); let out = log( &mut s, "var p = new DocumentPreset(); p.title = new Array(4097).join('t'); p.documentBleedLink = null; p.transparencyGrid = null;\ p.documentBleedOffsetRect = [0, 0, 0, 0]; p.width = undefined; vc.log([p.title.length, p.documentBleedLink, p.width === undefined]);\ p.documentBleedOffsetRect = undefined; vc.log(app.documents.addDocument('Print', p).typename);", ); assert_eq!(out, vec![json!([4096, null, true]), json!("Document")]); } #[test] fn wrappers_go_stale_after_delete_and_undo() { let (mut s, _, _) = doc(); let e = failure(&mut s, "var i = app.activeDocument.pageItems[0]; i.remove(); i.name;"); assert!(e.contains("Stale page item"), "{e}"); let e = failure(&mut s, "var c = app.activeDocument.pageItems[0].duplicate(); vc.run('edit.undo'); c.position;"); assert!(e.contains("Stale page item"), "{e}"); let e = failure(&mut s, "var p = app.activeDocument.pageItems; p.removeAll(); vc.log(p.length); p[0].name;"); assert!(e.contains("undefined"), "{e}"); // A named engine keeps its globals, but an object from an earlier run is stale. run(&mut s, "#targetengine keep\nvar held = app.activeDocument.pageItems[0];").unwrap(); let e = failure(&mut s, "#targetengine keep\nheld.name;"); assert!(e.contains("Stale page item"), "{e}"); } /// A setter on a removed item reports the stale item before it checks the value it was given. #[test] fn setters_on_a_stale_item_report_it_before_a_bad_value() { let (mut s, _, _) = doc(); for set in [ "name = {}", "note = {}", "hidden = 'x'", "locked = 'x'", "opacity = 500", "blendingMode = 'x'", "selected = 'x'", "position = 'x'", "left = 'x'", "top = 'x'", "width = -1", "height = -1", ] { let e = failure(&mut s, &format!("var i = app.activeDocument.pageItems[0].duplicate(); i.remove(); i.{set};")); assert!(e.contains("Stale page item"), "{set}: {e}"); } } /// File › Revert loads objects with the ids they were saved with: an object or a layer read before /// the revert does not name the loaded one. #[test] fn objects_read_before_a_revert_are_stale_and_ones_read_after_are_the_loaded_ones() { // `pageItems[0]` is the topmost object, the one made last. let (mut s, _, b) = doc(); let dir = vectorcraft_testkit::TempDir::new("dom-revert"); let path = dir.join("saved.vectorcraft"); s.execute("document.save", &json!({"path": path.to_string_lossy()})).unwrap(); for read in ["i.name", "i.name = 'after'", "app.activeDocument.selection = i", "l.name"] { let source = format!("var i = app.activeDocument.pageItems[0], l = i.parent; i.name = 'before'; vc.run('file.revert'); {read};"); let e = failure(&mut s, &source); assert!(e.contains("Stale page item"), "{read}: {e}"); } // Objects read after the revert are the loaded ones. let out = log( &mut s, "var p = app.activeDocument.pageItems; p[0].name = 'before'; vc.run('file.revert');\ app.activeDocument.pageItems[0].name = 'loaded'; vc.log(app.activeDocument.pageItems.length);", ); assert_eq!(out, vec![json!(2)]); assert_eq!(s.active().unwrap().doc.node(b).unwrap().name.as_deref(), Some("loaded")); } #[test] fn wrappers_go_stale_after_revert() { let (mut s, ..) = doc(); let folder = vectorcraft_testkit::TempDir::new("script-dom-revert"); let path = folder.join("saved's art.vectorcraft"); s.execute("document.save", &json!({"path": path.to_string_lossy()})).unwrap(); let held = "var d = app.activeDocument, i = d.pageItems[0], l = i.parent; i.name = 'changed'; vc.run('file.revert');"; let out = log(&mut s, &format!("{held} vc.log(d.pageItems.length); vc.log(d.pageItems[0] === i); vc.log(d.pageItems[0].parent === l);")); assert_eq!(out, vec![json!(2), json!(false), json!(false)]); for operation in [ "i.name", "i.name = 'wrong'", "l.name", "i.move(d.pageItems[1], ElementPlacement.PLACEBEFORE)", "d.pageItems[1].duplicate(i, ElementPlacement.PLACEBEFORE)", "d.pageItems[1].move(l, ElementPlacement.PLACEATEND)", ] { let source = format!("{held} try {{ {operation}; vc.log('ran'); }} catch (e) {{ vc.log(e.message); }} vc.log(d.pageItems.length);"); assert_eq!(log(&mut s, &source), vec![json!("Stale page item"), json!(2)], "{operation}"); } } /// A layer's collections read before File › Revert do not list the loaded document's art, even /// where the loaded layer has the same id. #[test] fn a_layers_collections_read_before_a_revert_are_stale() { let (mut s, ..) = doc(); let folder = vectorcraft_testkit::TempDir::new("script-dom-revert-lists"); let path = folder.join("saved.vectorcraft"); s.execute("document.save", &json!({"path": path.to_string_lossy()})).unwrap(); for list in ["pathItems", "pageItems", "groupItems", "compoundPathItems", "layers"] { let source = format!( "var d = app.activeDocument, c = d.layers[0].{list}; vc.log(c.length >= 0); d.pageItems[0].name = 'changed'; vc.run('file.revert');\ try {{ c.length; vc.log('ran'); }} catch (e) {{ vc.log('stale'); }} vc.log(d.layers[0].{list}.length >= 0);" ); assert_eq!(log(&mut s, &source), vec![json!(true), json!("stale"), json!(true)], "{list}"); } } /// An artboard read before File › Revert is stale, even though the loaded artboard has the same /// id; the document's `artboards` read before it lists the loaded ones. #[test] fn an_artboard_read_before_a_revert_is_stale() { let (mut s, ..) = doc(); let folder = vectorcraft_testkit::TempDir::new("script-dom-revert-artboard"); let path = folder.join("saved.vectorcraft"); s.execute("document.save", &json!({"path": path.to_string_lossy()})).unwrap(); let out = log( &mut s, "var d = app.activeDocument, bs = d.artboards, b = bs[0], saved = b.name; b.name = 'changed'; vc.run('file.revert');\ try { b.name = 'wrong'; vc.log('ran'); } catch (e) { vc.log(e.message); } vc.log(bs[0].name == saved);", ); assert_eq!(out, vec![json!("Stale artboard"), json!(true)]); } /// A key that is the text of a number but not an array index reaches an indexer as a number, and /// a layer's collections and `artboards` look it up as a name, as the document's `pageItems` does. /// `index()` still takes a number as a position. #[test] fn scoped_collections_find_names_that_are_numbers() { let (mut s, ..) = doc(); let out = log( &mut s, "var d = app.activeDocument, l = d.layers[0], i = l.pageItems[0]; i.name = '-1'; l.layers.add().name = '1.5';\ d.artboards[0].name = '-1';\ vc.log(l.pageItems['-1'] === i); vc.log(l.pathItems['-1'] === i); vc.log(l.layers['1.5'].name);\ vc.log(d.artboards['-1'].name); vc.log(l.pageItems['-2'] === undefined); vc.log(l.pageItems.index(0) === i);\ try { l.pageItems.index(-1); vc.log('ran'); } catch (e) { vc.log(e.message); }\ try { d.artboards.index(-1); vc.log('ran'); } catch (e) { vc.log(e.message); }", ); assert_eq!(out, vec![json!(true), json!(true), json!("1.5"), json!("-1"), json!(true), json!(true), json!("Not found"), json!("Not found")]); } /// A layer's or group's `pageItems` reads a name, `index` and `toString` as the document's does. #[test] fn a_layers_page_items_read_names_as_the_documents_do() { let (mut s, _, b) = doc(); s.execute("object.setProps", &json!({"ids": [b.0], "name": "Logo"})).unwrap(); let out = log( &mut s, "var p = app.activeDocument.layers[0].pageItems;\ vc.log([p['Logo'].name, 'Logo' in p, 'none' in p, p[-1] === undefined, p.index(0).name, String(p)]);\ var m = ''; try { p['none']; } catch (e) { m = e.message; } vc.log(m);\ var n = ''; try { p.index(1.5); } catch (e) { n = e.message; } vc.log(n);", ); assert_eq!(out, vec![json!(["Logo", true, false, true, "Logo", "[PageItems]"]), json!("Not found"), json!("Not found")]); } /// A layer's `textFrames.areaText` refuses a path in a locked layer and leaves it as it was. #[test] fn area_text_into_a_layer_refuses_a_locked_path() { let (mut s, a, _) = doc(); s.execute("object.setProps", &json!({"ids": [a.0], "locked": true})).unwrap(); let steps = undo_steps(&s); let e = failure(&mut s, "var d = app.activeDocument, p = d.pathItems[d.pathItems.length - 1], l = d.layers.add(); l.textFrames.areaText(p);"); assert!(e.contains("hidden or locked"), "{e}"); assert_eq!(undo_steps(&s), steps); assert!(s.active().unwrap().doc.node(a).is_some_and(|n| matches!(n.kind, NodeKind::Path { .. }))); } /// `clipped` on a group on a locked artboard throws `Cannot edit` and keeps the group as it was. #[test] fn clipping_a_group_on_a_locked_artboard_is_refused() { let (mut s, a, b) = doc(); s.execute("select.set", &json!({"ids": [a.0, b.0]})).unwrap(); s.execute("object.group", &json!({})).unwrap(); s.execute("artboard.setProps", &json!({"index": 0, "locked": true})).unwrap(); let steps = undo_steps(&s); let e = failure(&mut s, "app.activeDocument.groupItems[0].clipped = true;"); assert!(e.contains("Cannot edit"), "{e}"); assert_eq!(undo_steps(&s), steps); assert!(!s.active().unwrap().doc.layers[0].children().unwrap().iter().any(|n| n.clips())); } /// A layer's or group's collection and an artboard read before File › Revert are stale after it, /// while the document's own collections follow the loaded document. #[test] fn scoped_collections_and_artboards_go_stale_after_revert() { let (mut s, ..) = doc(); let folder = vectorcraft_testkit::TempDir::new("script-dom-revert-scoped"); let path = folder.join("saved.vectorcraft"); s.execute("document.save", &json!({"path": path.to_string_lossy()})).unwrap(); let held = "var d = app.activeDocument, p = d.layers[0].pageItems, g = d.layers[0].groupItems, ab = d.artboards[0], all = d.groupItems;\ d.pageItems[0].name = 'changed'; vc.run('file.revert');"; for (read, stale) in [("p.length", "Stale page item"), ("g.length", "Stale page item"), ("ab.name", "Stale artboard"), ("ab.name = 'x'", "Stale artboard")] { let source = format!("{held} try {{ {read}; vc.log('ran'); }} catch (e) {{ vc.log(e.message); }}"); assert_eq!(log(&mut s, &source), vec![json!(stale)], "{read}"); } assert_eq!(log(&mut s, &format!("{held} vc.log(all.length); vc.log(d.artboards[0].name);")), vec![json!(0), json!("Artboard 1")]); } #[test] fn remove_all_refuses_locked_art_before_deleting_anything() { let (mut s, a, b) = doc(); s.execute("object.setProps", &json!({"ids": [b.0], "locked": true})).unwrap(); // The script catches the error and finds both objects still there before the run ends, so the // run's own rollback cannot hide a deletion. let caught = "var p = app.activeDocument.pageItems, m = '';\ try { p.removeAll(); } catch (e) { m = e.message; }\ vc.log(m); vc.log(p.length);"; let out = log(&mut s, caught); assert!(out[0].as_str().unwrap().contains(&b.0.to_string()), "{out:?}"); assert_eq!(out[1], json!(2)); assert!(s.active().unwrap().doc.node(a).is_some() && s.active().unwrap().doc.node(b).is_some()); s.execute("object.setProps", &json!({"ids": [b.0], "locked": false})).unwrap(); // A locked object inside an unlocked group stops it too. s.execute("select.set", &json!({"ids": [a.0, b.0]})).unwrap(); let g = s.execute("object.group", &json!({})).unwrap()["id"].as_u64().unwrap(); s.execute("object.setProps", &json!({"ids": [b.0], "locked": true})).unwrap(); let out = log(&mut s, caught); assert!(out[0].as_str().unwrap().contains(&g.to_string()), "{out:?}"); assert_eq!(out[1], json!(3)); assert!(s.active().unwrap().doc.node(b).is_some()); s.execute("object.setProps", &json!({"ids": [b.0], "locked": false})).unwrap(); let out = log(&mut s, "var p = app.activeDocument.pageItems; p.removeAll(); vc.log(p.length);"); assert_eq!(out, vec![json!(0)]); } #[test] fn remove_refuses_hidden_or_locked_art_as_remove_all_does() { let (mut s, a, b) = doc(); for (on, off) in [ (json!({"ids": [b.0], "locked": true}), json!({"ids": [b.0], "locked": false})), (json!({"ids": [b.0], "visible": false}), json!({"ids": [b.0], "visible": true})), ] { s.execute("object.setProps", &on).unwrap(); let e = failure(&mut s, "app.activeDocument.pageItems[0].remove();"); assert!(e.contains(&format!("cannot delete hidden or locked objects: {}", b.0)), "{on}: {e}"); assert!(s.active().unwrap().doc.node(b).is_some(), "{on}"); s.execute("object.setProps", &off).unwrap(); } // A group that holds a locked object is refused too. s.execute("select.set", &json!({"ids": [a.0, b.0]})).unwrap(); let g = s.execute("object.group", &json!({})).unwrap()["id"].as_u64().unwrap(); s.execute("object.setProps", &json!({"ids": [b.0], "locked": true})).unwrap(); let e = failure(&mut s, "app.activeDocument.pageItems[0].remove();"); assert!(e.contains(&g.to_string()), "{e}"); assert!(s.active().unwrap().doc.node(b).is_some()); s.execute("object.setProps", &json!({"ids": [b.0], "locked": false})).unwrap(); log(&mut s, "app.activeDocument.pageItems[0].remove();"); assert!(s.active().unwrap().doc.node(b).is_none()); } /// Art on a locked artboard stays: `remove()` and `removeAll()` throw and delete nothing. #[test] fn remove_and_remove_all_refuse_art_on_a_locked_artboard() { let (mut s, a, b) = doc(); let lock = |s: &mut Session, locked: bool| { s.edit("Lock Artboard", |d, _| { d.artboards[0].locked = locked; Ok(()) }) .unwrap(); }; lock(&mut s, true); // An object on a second, unlocked artboard is not deleted either. s.execute("artboard.new", &json!({"x": 1000, "y": 0, "width": 400, "height": 300})).unwrap(); let c = rect(&mut s, 1100.0, 100.0, 50.0, 50.0); let steps = undo_steps(&s); let e = failure(&mut s, "app.activeDocument.pageItems[1].remove();"); assert!(e.contains(&format!("cannot delete objects on a locked artboard: {}", b.0)), "{e}"); let e = failure(&mut s, "app.activeDocument.pageItems.removeAll();"); assert!(e.contains("cannot delete objects on a locked artboard") && e.contains(&a.0.to_string()), "{e}"); let out = log(&mut s, "try { app.activeDocument.pageItems.removeAll(); } catch (e) { vc.log(e.name); } vc.log(app.activeDocument.pageItems.length);"); assert_eq!(out, vec![json!("Error"), json!(3)]); let doc = &s.active().unwrap().doc; assert!(doc.node(a).is_some() && doc.node(b).is_some() && doc.node(c).is_some()); assert_eq!(undo_steps(&s), steps); // `editable` is false for art on the locked artboard and true for art on the unlocked one. let editable = "var p = app.activeDocument.pageItems; vc.log([p[0].editable, p[1].editable, p[2].editable]);"; assert_eq!(log(&mut s, editable), vec![json!([true, false, false])]); lock(&mut s, false); assert_eq!(log(&mut s, editable), vec![json!([true, true, true])]); let out = log(&mut s, "var p = app.activeDocument.pageItems; p[1].remove(); p.removeAll(); vc.log(p.length);"); assert_eq!(out, vec![json!(0)]); } /// The `removeAll()` of a layer's or group's collections, and of `groupItems` and /// `compoundPathItems`, refuses art on a locked artboard as the document's `pageItems.removeAll()` /// does, and deletes nothing. #[test] fn scoped_remove_all_refuses_art_on_a_locked_artboard() { let (mut s, a, b) = doc(); s.execute("select.set", &json!({"ids": [a.0, b.0]})).unwrap(); let g = s.execute("object.group", &json!({})).unwrap()["id"].as_u64().unwrap(); s.execute("object.setProps", &json!({"id": g, "name": "locked"})).unwrap(); let c = rect(&mut s, 200.0, 100.0, 20.0, 20.0); let e = rect(&mut s, 250.0, 100.0, 20.0, 20.0); s.execute("select.set", &json!({"ids": [c.0, e.0]})).unwrap(); s.execute("object.compoundPath.make", &json!({})).unwrap(); let lock = |s: &mut Session, locked: bool| { s.edit("Lock Artboard", |d, _| { d.artboards[0].locked = locked; Ok(()) }) .unwrap(); }; lock(&mut s, true); // A path and a group on a second, unlocked artboard, made last so each lists first in its // scope, are not deleted either. s.execute("artboard.new", &json!({"x": 1000, "y": 0, "width": 400, "height": 300})).unwrap(); let u = rect(&mut s, 1300.0, 100.0, 20.0, 20.0); let v = rect(&mut s, 1100.0, 100.0, 20.0, 20.0); let w = rect(&mut s, 1150.0, 100.0, 20.0, 20.0); s.execute("select.set", &json!({"ids": [v.0, w.0]})).unwrap(); s.execute("object.group", &json!({})).unwrap(); s.execute("select.none", &json!({})).unwrap(); let before = format!("{:?}", s.active().unwrap().doc.layers); let steps = undo_steps(&s); // The lengths are read inside the run, after the refusal is caught, so a delete that ran before // the refusal shows here even though a failed run would put the tree back. let lengths = "vc.log([d.pageItems.length, d.layers[0].pageItems.length, d.groupItems.length, d.groupItems.length && d.groupItems[0].pageItems.length, \ d.compoundPathItems.length]);"; let counts = log(&mut s, &format!("var d = app.activeDocument; {lengths}")); for scope in [ "app.activeDocument.layers[0].pageItems", "app.activeDocument.layers[0].groupItems", "app.activeDocument.groupItems", "app.activeDocument.groupItems.getByName('locked').pageItems", "app.activeDocument.layers[0].compoundPathItems", "app.activeDocument.compoundPathItems", "app.activeDocument.pathItems", "app.activeDocument.groupItems.getByName('locked').pathItems", "app.activeDocument.compoundPathItems[0].pathItems", ] { let out = log( &mut s, &format!("var d = app.activeDocument; try {{ {scope}.removeAll(); vc.log('ok'); }} catch (e) {{ vc.log(e.message); }} {lengths}"), ); let err = out[0].as_str().unwrap_or_default(); assert!(err.contains("cannot delete objects on a locked artboard"), "{scope}: {err}"); assert_eq!(out[1..], counts[..], "{scope}: nothing is deleted before the refusal"); assert_eq!(format!("{:?}", s.active().unwrap().doc.layers), before, "{scope}"); assert_eq!(undo_steps(&s), steps, "{scope}"); let doc = &s.active().unwrap().doc; assert!([u, v, w].iter().all(|i| doc.node(*i).is_some()), "{scope}"); } lock(&mut s, false); let out = log(&mut s, "var d = app.activeDocument; d.layers[0].pageItems.removeAll(); vc.log(d.pageItems.length);"); assert_eq!(out, vec![json!(0)]); } /// `removeAll()` on a compound path's `pathItems` deletes its paths and keeps the compound path, /// empty, as `remove()` on its last path does. #[test] fn removing_all_the_paths_of_a_compound_path_keeps_the_compound_path() { let (mut s, a, b) = doc(); s.execute("select.set", &json!({"ids": [a.0, b.0]})).unwrap(); s.execute("object.compoundPath.make", &json!({})).unwrap(); let compound = layer_order(&s)[0]; let steps = undo_steps(&s); let out = log( &mut s, "var c = app.activeDocument.compoundPathItems[0]; c.pathItems.removeAll(); vc.log([c.typename, c.pathItems.length, app.activeDocument.compoundPathItems.length]);", ); assert_eq!(out, vec![json!(["CompoundPathItem", 0, 1])]); assert!(s.active().unwrap().doc.node(compound).is_some_and(|n| n.children().is_some_and(|c| c.is_empty())), "the compound path stays, empty"); assert_eq!(undo_steps(&s), steps + 1); } /// `removeAll()` on the `pathItems` of a compound path that clips a group keeping other art /// would leave an empty clipping path, so it throws and deletes nothing, as deleting the clipping /// path itself does. Removing some of its paths still works. #[test] fn removing_all_the_paths_of_a_clipping_compound_path_is_refused() { let (mut s, a, b) = doc(); s.execute("select.set", &json!({"ids": [a.0, b.0]})).unwrap(); s.execute("object.compoundPath.make", &json!({})).unwrap(); let compound = s.active().unwrap().doc.parent_of(a).unwrap(); let other = rect(&mut s, 200.0, 20.0, 30.0, 40.0); s.execute("select.set", &json!({"ids": [compound.0, other.0]})).unwrap(); s.execute("object.group", &json!({})).unwrap(); run(&mut s, "var g = app.activeDocument.groupItems[0]; g.compoundPathItems[0].zOrder(ZOrderMethod.BRINGTOFRONT); g.clipped = true;").unwrap(); let d = &s.active().unwrap().doc; assert_eq!(d.parent_of(compound).and_then(|g| d.node(g)).and_then(|g| g.children()).map(|k| k[0].id), Some(compound)); let before = s.active().unwrap().doc.clone(); let steps = undo_steps(&s); let e = failure(&mut s, "app.activeDocument.compoundPathItems[0].pathItems.removeAll();"); assert!(e.contains("cannot delete a clipping path"), "{e}"); assert!(e.contains(&compound.0.to_string()), "{e}"); assert_eq!(s.active().unwrap().doc, before); assert_eq!(undo_steps(&s), steps); run(&mut s, "app.activeDocument.compoundPathItems[0].pathItems[0].remove();").unwrap(); assert_eq!(s.active().unwrap().doc.node(compound).and_then(|n| n.children()).map(Vec::len), Some(1)); } /// `move` leaves art on a locked artboard where it is, and a move that leaves an object where it /// is runs no command, also in a layer whose ruler guides are above its art. #[test] fn move_refuses_art_on_a_locked_artboard_and_skips_a_move_to_the_same_place() { let (mut s, a, b) = doc(); s.execute("guide.add", &json!({"vertical": true, "pos": 120})).unwrap(); let steps = undo_steps(&s); // The topmost art, moved to the top of its layer, stays where it is. log( &mut s, "var d = app.activeDocument, p = d.pageItems, top = p[0];\ for (var i = 1; i < p.length; i++) { if (p[i].zOrderPosition > top.zOrderPosition) top = p[i]; }\ top.move(top.parent);", ); assert_eq!(undo_steps(&s), steps, "a move to the same place adds no step"); s.edit("Lock Artboard", |d, _| { d.artboards[0].locked = true; Ok(()) }) .unwrap(); let steps = undo_steps(&s); let order = |s: &Session| format!("{:?}", (s.active().unwrap().doc.position(a), s.active().unwrap().doc.position(b))); let before = order(&s); let e = failure(&mut s, "var p = app.activeDocument.pageItems; p[0].move(p[0].parent, ElementPlacement.PLACEATBEGINNING);"); assert!(e.contains("Cannot move PathItem"), "{e}"); let e = failure(&mut s, "var p = app.activeDocument.pageItems; p[1].move(p[1].parent, ElementPlacement.PLACEATEND);"); assert!(e.contains("Cannot move PathItem"), "{e}"); assert_eq!(order(&s), before); assert_eq!(undo_steps(&s), steps); } #[test] fn text_frames_remove_all_refuses_type_on_a_locked_artboard() { let mut s = Session::new(); s.execute("file.new", &json!({"width": 400, "height": 300})).unwrap(); let t = NodeId(s.execute("text.create", &json!({"x": 10, "y": 250, "text": "hi"})).unwrap()["id"].as_u64().unwrap()); s.edit("Lock Artboard", |d, _| { d.artboards[0].locked = true; Ok(()) }) .unwrap(); let steps = undo_steps(&s); let e = failure(&mut s, "app.activeDocument.textFrames.removeAll();"); assert!(e.contains(&format!("cannot delete objects on a locked artboard: {}", t.0)), "{e}"); assert!(s.active().unwrap().doc.node(t).is_some()); assert_eq!(undo_steps(&s), steps); } #[test] fn move_and_duplicate_refuse_art_on_a_locked_artboard() { let (mut s, a, b) = doc(); let lock = |s: &mut Session, locked: bool| { s.edit("Lock Artboard", |d, _| { d.artboards[0].locked = locked; Ok(()) }) .unwrap(); }; lock(&mut s, true); let (order, steps) = (layer_order(&s), undo_steps(&s)); let e = failure(&mut s, "var p = app.activeDocument.pageItems; p[1].move(p[0], ElementPlacement.PLACEBEFORE);"); assert!(e.contains(&format!("Cannot move PathItem {}", a.0)), "{e}"); assert_eq!((layer_order(&s), undo_steps(&s)), (order.clone(), steps)); // `duplicate` refuses it too, with or without a relative object. for source in ["app.activeDocument.pageItems[1].duplicate();", "var p = app.activeDocument.pageItems; p[1].duplicate(p[0]);"] { let e = failure(&mut s, source); assert!(e.contains(&format!("Cannot duplicate PathItem {}", a.0)), "{source}: {e}"); } assert_eq!((layer_order(&s), undo_steps(&s)), (order, steps)); lock(&mut s, false); log(&mut s, "var p = app.activeDocument.pageItems; p[1].move(p[0], ElementPlacement.PLACEBEFORE);"); assert_eq!(layer_order(&s), [b, a]); } #[test] fn duplicate_refuses_hidden_or_locked_art() { let (mut s, a, b) = doc(); for (on, off) in [ (json!({"ids": [b.0], "locked": true}), json!({"ids": [b.0], "locked": false})), (json!({"ids": [b.0], "visible": false}), json!({"ids": [b.0], "visible": true})), ] { s.execute("object.setProps", &on).unwrap(); let (order, steps) = (layer_order(&s), undo_steps(&s)); // With no argument and with a relative object. for source in ["app.activeDocument.pageItems[0].duplicate();", "var p = app.activeDocument.pageItems; p[0].duplicate(p[1]);"] { let e = failure(&mut s, source); assert!(e.contains(&format!("Cannot duplicate PathItem {}", b.0)), "{on} {source}: {e}"); } assert_eq!((layer_order(&s), undo_steps(&s)), (order, steps), "{on}"); s.execute("object.setProps", &off).unwrap(); } log(&mut s, "app.activeDocument.pageItems[0].duplicate();"); let order = layer_order(&s); assert_eq!(order.len(), 3); assert!(order.contains(&a) && order.contains(&b)); } #[test] fn setters_refuse_art_on_a_locked_artboard() { let (mut s, _, _) = doc(); s.edit("Lock Artboard", |d, _| { d.artboards[0].locked = true; Ok(()) }) .unwrap(); let (before, steps) = (format!("{:?}", s.active().unwrap().doc.layers), undo_steps(&s)); let out = log( &mut s, "var p = app.activeDocument.pageItems[0], refused = [];\ var setters = [function(){p.position=[5,5]},function(){p.left=5},function(){p.top=5},\ function(){p.width=10},function(){p.height=10},function(){p.opacity=50},\ function(){p.hidden=true},function(){p.locked=true},function(){p.zOrder(ZOrderMethod.SENDTOBACK)}];\ for(var i=0;i/a\b.svg` is not the open `/a/b.svg`. #[test] fn get_is_file_open_keeps_a_backslash_in_a_unix_file_name() { let (mut s, _, _) = doc(); let fixture = vectorcraft_testkit::TempDir::new("script-file-open-backslash"); let dir = fixture.join("a"); std::fs::create_dir_all(&dir).unwrap(); let path = dir.join("b.svg"); s.execute("document.save", &json!({"path": path.to_string_lossy()})).unwrap(); let same = serde_json::to_string(&path.to_string_lossy()).unwrap(); let backslash = serde_json::to_string(&format!("{}\\b.svg", dir.to_string_lossy())).unwrap(); let out = log(&mut s, &format!("vc.log(app.getIsFileOpen({same})); vc.log(app.getIsFileOpen({backslash}));")); assert_eq!(out, vec![json!(true), json!(cfg!(windows))]); } #[test] fn a_menu_command_runs_its_command_on_the_selection() { let (mut s, a, b) = doc(); s.execute("select.set", &json!({"ids": [a.0, b.0]})).unwrap(); let g = NodeId(s.execute("object.group", &json!({})).unwrap()["id"].as_u64().unwrap()); let steps = undo_steps(&s); let out = log(&mut s, "app.executeMenuCommand('ungroup'); vc.log(app.activeDocument.selection.length);"); assert_eq!(out, vec![json!(2)]); let st = s.active().unwrap(); assert!(st.doc.node(g).is_none(), "the group is gone"); assert!(st.doc.node(a).is_some() && st.doc.node(b).is_some()); assert_eq!(undo_steps(&s), steps + 1, "one undo step for the run"); // With nothing selected the menu item does nothing and adds no undo step. s.execute("select.none", &json!({})).unwrap(); let steps = undo_steps(&s); let before = s.active().unwrap().doc.clone(); log(&mut s, "app.executeMenuCommand('ungroup'); app.executeMenuCommand('releaseMask');"); assert_eq!(undo_steps(&s), steps); assert!(std::sync::Arc::ptr_eq(&before, &s.active().unwrap().doc), "nothing changed"); // selectall then group makes one group of both rectangles. let out = log(&mut s, "app.executeMenuCommand('selectall'); app.executeMenuCommand('group'); vc.log(app.activeDocument.selection[0].typename);"); assert_eq!(out, vec![json!("GroupItem")]); } #[test] fn the_fit_to_artwork_menu_command_fits_the_artboard_to_the_art() { let (mut s, _, _) = doc(); let before = s.active().unwrap().doc.artboards[0].rect; log(&mut s, "app.executeMenuCommand('Fit Artboard to artwork bounds');"); let after = s.active().unwrap().doc.artboards[0].rect; assert!(after.width() < before.width() && after.height() < before.height(), "{before:?} -> {after:?}"); // An artboard that is locked, or has no art on it, leaves the menu item dimmed: it does nothing. let (mut s, _, _) = doc(); s.execute("artboard.setProps", &json!({"index": 0, "locked": true})).unwrap(); let steps = undo_steps(&s); assert_eq!(log(&mut s, "app.executeMenuCommand('Fit Artboard to artwork bounds'); vc.log(1);"), vec![json!(1)]); assert_eq!(s.active().unwrap().doc.artboards[0].rect, before); assert_eq!(undo_steps(&s), steps, "locked"); let mut s = Session::new(); s.execute("file.new", &json!({"width": 400, "height": 300})).unwrap(); let empty = s.active().unwrap().doc.artboards[0].rect; assert_eq!(log(&mut s, "app.executeMenuCommand('Fit Artboard to artwork bounds'); vc.log(1);"), vec![json!(1)]); assert_eq!(s.active().unwrap().doc.artboards[0].rect, empty); assert_eq!(undo_steps(&s), 0, "no art"); } #[test] fn an_unknown_menu_command_throws_and_names_it() { let (mut s, a, _) = doc(); s.execute("select.set", &json!({"ids": [a.0]})).unwrap(); let steps = undo_steps(&s); let e = failure(&mut s, "app.executeMenuCommand('acmeNoSuchMenu');"); assert!(e.contains("acmeNoSuchMenu"), "{e}"); assert!(failure(&mut s, "app.executeMenuCommand('Ungroup');").contains("Ungroup"), "names are matched exactly"); assert!(failure(&mut s, "app.executeMenuCommand(5);").contains("string")); assert!(failure(&mut s, "app.executeMenuCommand();").contains("string")); assert!(failure(&mut s, "app.executeMenuCommand(new Array(5000).join('a'));").contains("too long")); assert_eq!(undo_steps(&s), steps, "nothing changed"); // With no document open a known menu item does nothing, and an unknown one still throws. let mut s = Session::new(); assert_eq!(log(&mut s, "app.executeMenuCommand('selectall'); vc.log(app.documents.length);"), vec![json!(0)]); assert!(failure(&mut s, "app.executeMenuCommand('acmeNoSuchMenu');").contains("acmeNoSuchMenu")); } /// `getIsFileOpen` folds `\` into `/` and letter case only by Windows' rules: on macOS and Linux a /// `\` is part of a file name, so `a\b.svg` is not the file `b.svg` in the folder `a`. #[test] fn get_is_file_open_compares_paths_by_the_platform_s_rules() { use crate::script::dom::path_key_on; assert_ne!(path_key_on("/tmp/work/a\\b.svg", false), path_key_on("/tmp/work/a/b.svg", false)); assert_ne!(path_key_on("/tmp/work/A.svg", false), path_key_on("/tmp/work/a.svg", false)); assert_eq!(path_key_on("/tmp/work/", false), path_key_on("/tmp/work", false)); assert_eq!(path_key_on("C:\\Work\\A.svg", true), path_key_on("c:/work/a.svg", true)); assert_eq!(path_key_on("C:\\Work\\", true), path_key_on("c:/work", true)); } #[test] fn hostile_dom_values_do_not_reenter_or_exceed_limits() { let (mut s, _, _) = doc(); let before = s.active().unwrap().selection.objects.clone(); // A plain object shaped like an object of the model is not one. let e = failure(&mut s, "var f = {}; f['\\u0000hostRef'] = ['PathItem', 0, 1, 1]; app.activeDocument.selection = f;"); assert!(e.contains("not plain data"), "{e}"); // A getter in a value is refused before it runs. let out = log( &mut s, "var hit = 0, v = {}; Object.defineProperty(v, 0, {get: function () { hit++; return 1; }, enumerable: true});\ try { app.activeDocument.rulerOrigin = v; } catch (e) { vc.log(e.message); } vc.log(hit);", ); assert!(out[0].as_str().is_some_and(|m| m.contains("rulerOrigin")), "{out:?}"); assert_eq!(out[1], json!(0)); let e = failure(&mut s, "var a = []; for (var i = 0; i < 10001; i++) a.push(app.activeDocument.pageItems[0]); app.activeDocument.selection = a;"); assert!(e.contains("at most 10000"), "{e}"); assert_eq!(s.active().unwrap().selection.objects, before); } #[test] fn prototype_changes_cannot_forge_a_page_item() { let (mut s, a, _) = doc(); let out = log( &mut s, "var f = Object.create(PathItem.prototype);\ try { f.name = 'x'; vc.log('wrote'); } catch (e) { vc.log(e.message); }\ try { PathItem.prototype.name = 'y'; vc.log('wrote'); } catch (e) { vc.log(e.message); } PageItem = null; Document.prototype.pageItems = null;\ vc.log(Object.isFrozen(PathItem.prototype) || !Object.isExtensible(PathItem.prototype));\ vc.log(app.activeDocument.pageItems.length);\ try { new PathItem(); vc.log('made'); } catch (e) { vc.log(e.name); }", ); assert_eq!(out, vec![json!("this is not a page item"), json!("this is not a page item"), json!(true), json!(2), json!("TypeError")]); assert_eq!(s.active().unwrap().doc.node(a).unwrap().name, None); } #[test] fn select_tool_makes_the_named_tool_current_when_the_run_ends() { let (mut s, _, _) = doc(); assert_eq!(s.tool_id(), "selection"); // A tool's id, its label and a name in the form scripts pass all choose it; the last one counts. assert_eq!(log(&mut s, "vc.log(app.selectTool('pen'));"), vec![json!(true)]); assert_eq!(s.tool_id(), "pen"); log(&mut s, "app.selectTool('Rectangle Tool');"); assert_eq!(s.tool_id(), "rectangle"); log(&mut s, "app.selectTool('Adobe Ellipse Tool'); app.selectTool('Adobe Direct Select Tool');"); assert_eq!(s.tool_id(), "directSelection"); log(&mut s, "app.selectTool('Adobe Select Tool');"); assert_eq!(s.tool_id(), "selection"); // An unknown name throws, a caught error chooses nothing, and a run that fails leaves the tool. assert!(failure(&mut s, "app.selectTool('No Such Tool');").contains("No tool is named \"No Such Tool\"")); assert!(failure(&mut s, "app.selectTool('Adobe ');").contains("No tool is named")); let out = log(&mut s, "try { app.selectTool('pencil tool'); } catch (e) { vc.log(e.message); }"); assert_eq!(out, vec![json!("No tool is named \"pencil tool\"")]); assert!(failure(&mut s, "app.selectTool('pen'); throw new Error('stop');").contains("stop")); let out = log(&mut s, "try { app.selectTool(5); } catch (e) { vc.log(e.name); }"); assert_eq!(out, vec![json!("TypeError")]); assert!(failure(&mut s, "app.selectTool(new Array(5000).join('a'));").contains("too long")); assert_eq!(s.tool_id(), "selection"); // With no document open the tool is chosen too. let mut empty = Session::new(); log(&mut empty, "app.selectTool('Adobe Freehand Tool');"); assert_eq!(empty.tool_id(), "pencil"); } #[test] fn unsupported_members_say_so() { let (mut s, _, _) = doc(); assert!(failure(&mut s, "app.activeDocument.swatches;").contains("Document.swatches is not available yet")); } fn layer_names(s: &Session) -> Vec { s.active().unwrap().doc.layers.iter().rev().map(|n| n.name.clone().unwrap_or_default()).collect() } #[test] fn layers_list_top_first_and_add_makes_the_new_layer_current() { let (mut s, a, _) = doc(); let steps = undo_steps(&s); let out = log( &mut s, "var d = app.activeDocument, L = d.layers; vc.log(L.length); vc.log(L.typename); vc.log(L.parent.typename);\ var n = L.add(); n.name = 'Top'; vc.log(L.length); vc.log(L[0].name); vc.log(d.activeLayer.name);\ vc.log(L.getByName('Top').zOrderPosition); vc.log(L[1].zOrderPosition); vc.log(n.parent.typename);\ var sub = L[1].layers.add(); sub.name = 'Sub'; vc.log(L[1].layers.length); vc.log(sub.parent.name);\ vc.log(L.length); vc.log(L[5] === undefined); vc.log(L[1].pageItems.length); vc.log(L[1].pageItems[0].typename);\ vc.log(sub.zOrderPosition);", ); let expected = [json!(1), json!("Layers"), json!("Document"), json!(2), json!("Top"), json!("Top"), json!(2), json!(1), json!("Document")]; assert_eq!(out[..9], expected); // A layer's pageItems hold its art, not its sublayers, and a sublayer's zOrderPosition counts // only the layers beside it. assert_eq!(out[9..], [json!(1), json!("Layer 1"), json!(2), json!(true), json!(2), json!("PathItem"), json!(1)]); assert_eq!(layer_names(&s), ["Top", "Layer 1"]); // One run is one undo step. assert_eq!(undo_steps(&s), steps + 1); let lid = s.active().unwrap().doc.layer_containing(a).unwrap(); assert!(failure(&mut s, "app.activeDocument.layers.getByName('none');").contains("Not found")); assert!(failure(&mut s, "app.activeDocument.layers.getByName(5);").contains("give a name")); assert_eq!(s.active().unwrap().doc.layer_containing(a), Some(lid)); } #[test] fn a_new_or_assigned_layer_stays_the_current_layer() { let (mut s, a, _) = doc(); s.execute("select.set", &json!({"ids": [a.0]})).unwrap(); let out = log( &mut s, "var d = app.activeDocument, base = d.layers[0];\ var n = d.layers.add(); n.name = 'New'; vc.log(d.activeLayer.name);\ d.activeLayer = base; vc.log(d.activeLayer.name);\ d.activeLayer = n; vc.log(d.activeLayer.name);\ var sub = base.layers.add(); sub.name = 'Sub'; vc.log(d.activeLayer.name);\ vc.log(d.selection.length); vc.log(d.selection[0].layer.name);", ); assert_eq!(out, [json!("New"), json!("Layer 1"), json!("New"), json!("Sub"), json!(1), json!("Layer 1")]); // The layer the script made current is still current after the run. let st = s.active().unwrap(); let current = st.current_layer().and_then(|l| st.doc.node(l)).and_then(|n| n.name.clone()); assert_eq!(current.as_deref(), Some("Sub")); } #[test] fn a_new_layer_or_group_with_a_large_id_is_the_new_one() { let mut s = Session::new(); s.execute("file.new", &json!({"width": 400, "height": 300})).unwrap(); // The new ids, 2^53 + 1 and 2^53 + 3, are not exact as numbers. for (id, source) in [(1, "var l = app.activeDocument.layers.add(); l.name = 'made';"), (3, "var g = app.activeDocument.groupItems.add(); g.name = 'made';")] { let big = (1u64 << 53) + id; s.active_mut().unwrap().change_unsaved(|d| std::sync::Arc::make_mut(d).raise_next_id(big)); run(&mut s, source).unwrap_or_else(|e| panic!("{source}: {e}")); let d = &s.active().unwrap().doc; assert_eq!(d.node(NodeId(big)).and_then(|n| n.name.clone()).as_deref(), Some("made"), "{source}"); } } #[test] fn layer_properties_round_trip_and_reject_bad_values() { let (mut s, _, _) = doc(); let out = log( &mut s, "var l = app.activeDocument.layers[0];\ l.name = 'Art'; l.printable = false; l.preview = false; l.opacity = 40; l.dimPlacedImages = true;\ l.color = {red: 10, green: 20, blue: 30};\ vc.log(l.name); vc.log(l.printable); vc.log(l.preview); vc.log(l.opacity); vc.log(l.dimPlacedImages);\ var c = l.color; vc.log([c.red, c.green, c.blue]); vc.log(l.visible); vc.log(l.locked); vc.log(l.typename);\ l.visible = false; vc.log(l.visible); l.visible = true; l.locked = true; vc.log(l.locked); l.locked = false;", ); let expected = [json!("Art"), json!(false), json!(false), json!(40), json!(true), json!([10, 20, 30]), json!(true), json!(false)]; assert_eq!(out[..8], expected); assert_eq!(out[8..], [json!("Layer"), json!(false), json!(true)]); // Setting the values a layer has adds no undo step. let steps = undo_steps(&s); let same = "var l = app.activeDocument.layers[0]; l.name = 'Art'; l.opacity = 40; l.printable = false; l.color = {red: 10, green: 20, blue: 30};"; run(&mut s, same).unwrap(); assert_eq!(undo_steps(&s), steps); let before = format!("{:?}", s.active().unwrap().doc.layers); for (src, msg) in [ ("app.activeDocument.layers[0].name = null;", "name must be a string"), ("app.activeDocument.layers[0].name = {};", "name must be a string"), ("app.activeDocument.layers[0].name = ' ';", "cannot be empty"), ("app.activeDocument.layers[0].name = new Array(600).join('x');", "at most 512"), ("app.activeDocument.layers[0].opacity = 200;", "from 0 to 100"), ("app.activeDocument.layers[0].opacity = NaN;", "finite number"), ("app.activeDocument.layers[0].visible = 1;", "true or false"), ("app.activeDocument.layers[0].color = {red: 300, green: 0, blue: 0};", "color.red"), ("app.activeDocument.layers[0].color = {red: 1.5, green: 0, blue: 0};", "color.red"), ("app.activeDocument.layers[0].color = 'red';", "red, green and blue"), ("app.activeDocument.layers[0].zOrder('up');", "ZOrderMethod"), ("app.activeDocument.activeLayer = 3;", "not a Layer"), ] { assert!(failure(&mut s, src).contains(msg), "{src}"); } assert_eq!(format!("{:?}", s.active().unwrap().doc.layers), before); assert_eq!(undo_steps(&s), steps); } #[test] fn layer_move_and_z_order_keep_ids() { let (mut s, _, _) = doc(); run(&mut s, "var L = app.activeDocument.layers; L.add().name = 'B'; L.add().name = 'C';").unwrap(); assert_eq!(layer_names(&s), ["C", "B", "Layer 1"]); let out = log( &mut s, "var L = app.activeDocument.layers, c = L[0]; c.zOrder(ZOrderMethod.SENDTOBACK); vc.log(L[2].name); vc.log(c.zOrderPosition);\ c.zOrder(ZOrderMethod.BRINGFORWARD); vc.log(L[1].name);\ c.move(L.getByName('B'), ElementPlacement.PLACEBEFORE); vc.log(L[0].name);\ c.move(L.getByName('B')); vc.log(L.length); vc.log(c.parent.name); vc.log(c.name);\ c.move(app.activeDocument); vc.log(L.length); vc.log(L[0].name);", ); assert_eq!(out, [json!("C"), json!(1), json!("C"), json!("C"), json!(2), json!("B"), json!("C"), json!(3), json!("C")]); assert!(failure(&mut s, "var L = app.activeDocument.layers; L[0].move(L[0]);").contains("into itself")); assert!(failure(&mut s, "var L = app.activeDocument.layers; L[0].move(L[1].pageItems);").contains("a layer or a document")); assert!(failure(&mut s, "var L = app.activeDocument.layers; L[0].move();").contains("relative object")); } #[test] fn sublayer_z_order_steps_past_the_next_layer_and_the_art_between() { let (mut s, _, _) = doc(); let out = log( &mut s, "var P = app.activeDocument.layers[0], x = P.layers.add(); x.name = 'X'; x.zOrder(ZOrderMethod.SENDTOBACK);\ var y = P.layers.add(); y.name = 'Y'; vc.log([x.zOrderPosition, y.zOrderPosition]);\ y.zOrder(ZOrderMethod.SENDBACKWARD); vc.log([x.zOrderPosition, y.zOrderPosition, P.layers[0].name]);\ y.zOrder(ZOrderMethod.BRINGFORWARD); vc.log([x.zOrderPosition, y.zOrderPosition, P.layers[0].name]);\ y.zOrder(ZOrderMethod.BRINGFORWARD); vc.log(P.pageItems.length);", ); assert_eq!(out, [json!([1, 2]), json!([2, 1, "X"]), json!([1, 2, "Y"]), json!(2)]); // The last step had no layer to pass, so Y stayed under the art. let d = &s.active().unwrap().doc; let p = d.layers.last().unwrap(); let kids = d.children(Some(p.id)).unwrap(); assert!(kids[1].is_layer() && !kids[2].is_layer() && !kids[3].is_layer()); } #[test] fn removed_layers_throw_and_the_last_layer_stays() { let (mut s, _, _) = doc(); let out = log( &mut s, "var L = app.activeDocument.layers, n = L.add(); n.layers.add(); n.layers.add(); vc.log(n.layers.length);\ n.layers.removeAll(); vc.log(n.layers.length); n.remove(); vc.log(L.length);\ try { n.name; } catch (e) { vc.log(String(e)); }", ); assert_eq!(out[..3], [json!(2), json!(0), json!(1)]); assert!(out[3].as_str().unwrap().contains("Stale layer"), "{out:?}"); assert_eq!(s.active().unwrap().doc.layers.len(), 1); assert!(failure(&mut s, "app.activeDocument.layers[0].remove();").contains("at least one layer")); assert!(failure(&mut s, "app.activeDocument.layers.removeAll();").contains("at least one layer")); assert_eq!(s.active().unwrap().doc.layers.len(), 1); assert!(failure(&mut s, "Object.getOwnPropertyDescriptor(Layer.prototype, 'name').get.call({});").contains("not a Layer")); } #[test] fn group_items_list_every_depth_and_clipped_toggles_the_mask() { let (mut s, a, b) = doc(); s.execute("select.set", &json!({"ids": [a.0, b.0]})).unwrap(); s.execute("object.group", &json!({})).unwrap(); let out = log( &mut s, "var d = app.activeDocument, g = d.groupItems[0]; vc.log(d.groupItems.length); vc.log(g.pageItems.length);\ var inner = g.groupItems.add(); vc.log(inner.typename); vc.log(inner.pageItems.length);\ vc.log(d.groupItems.length); vc.log(g.groupItems.length); vc.log(d.layers[0].groupItems.length);\ vc.log(g.layer.name); vc.log(g.clipped); vc.log(d.compoundPathItems.length);\ d.layers[0].groupItems.add(); vc.log(d.layers[0].groupItems.length); vc.log(g.pageItems.parent.typename);", ); let expected = [json!(1), json!(2), json!("GroupItem"), json!(0), json!(2), json!(1), json!(1), json!("Layer 1"), json!(false), json!(0)]; assert_eq!(out[..10], expected); assert_eq!(out[10..], [json!(2), json!("GroupItem")]); // `clipped` on a group with a path on top makes the topmost path its clipping path. let mut s = Session::new(); s.execute("file.new", &json!({"width": 400, "height": 300})).unwrap(); let (a, b) = (rect(&mut s, 0.0, 0.0, 10.0, 10.0), rect(&mut s, 0.0, 0.0, 5.0, 5.0)); s.execute("select.set", &json!({"ids": [a.0, b.0]})).unwrap(); let gid = NodeId(s.execute("object.group", &json!({})).unwrap()["id"].as_u64().unwrap()); let steps = undo_steps(&s); run(&mut s, "var g = app.activeDocument.layers[0].groupItems[0]; g.clipped = false;").unwrap(); assert_eq!(undo_steps(&s), steps); assert_eq!(log(&mut s, "var g = app.activeDocument.layers[0].groupItems[0]; g.clipped = true; vc.log(g.clipped);"), [json!(true)]); let st = s.active().unwrap(); let g = st.doc.node(gid).unwrap(); assert!(g.clips()); assert_eq!(g.children().unwrap()[0].id, b); assert_eq!(undo_steps(&s), steps + 1); assert!(failure(&mut s, "app.activeDocument.groupItems[0].clipped = 'yes';").contains("true or false")); assert!(failure(&mut s, "app.activeDocument.groupItems.getByName('none');").contains("Not found")); } #[test] fn a_layers_and_a_groups_lists_give_the_topmost_object_first() { let (mut s, a, b) = doc(); let c = rect(&mut s, 200.0, 100.0, 70.0, 20.0); s.execute("select.set", &json!({"ids": [b.0, c.0]})).unwrap(); s.execute("object.group", &json!({})).unwrap(); s.execute("select.set", &json!({"ids": [a.0]})).unwrap(); s.execute("object.compoundPath.make", &json!({})).unwrap(); s.execute("select.none", &json!({})).unwrap(); // The layer holds, bottom to top, the compound path of `a` and the group of `b` (50 pt wide) // and `c` (70 pt wide). let out = log( &mut s, "var L = app.activeDocument.layers[0], P = L.pageItems, g = L.groupItems[0];\ vc.log(P[0].typename); vc.log(P[1].typename); vc.log(g.pageItems[0].width); vc.log(g.pageItems[1].width);\ var e = L.groupItems.add(); e.name = 'E'; vc.log(L.groupItems[0].name); vc.log(L.pageItems[0].name);\ vc.log(L.compoundPathItems[0].pathItems[0].width);", ); assert_eq!(out, [json!("GroupItem"), json!("CompoundPathItem"), json!(70), json!(50), json!("E"), json!("E"), json!(30)]); } #[test] fn a_layers_and_a_groups_path_items_list_only_the_paths_directly_in_them_topmost_first() { let (mut s, _, b) = doc(); let c = rect(&mut s, 200.0, 100.0, 70.0, 20.0); s.execute("select.set", &json!({"ids": [b.0, c.0]})).unwrap(); s.execute("object.group", &json!({})).unwrap(); s.execute("select.none", &json!({})).unwrap(); // The layer holds `a` (30 pt wide) and a group of `b` (50 pt wide) and `c` (70 pt wide); the // document's list holds all three, the layer's only `a`, and a sublayer's path is its own. let out = log( &mut s, "var d = app.activeDocument, L = d.layers[0], g = L.groupItems[0], P = L.pathItems, G = g.pathItems;\ vc.log(P.length); vc.log(P[0].width); vc.log(P.typename); vc.log(P.parent.typename);\ vc.log(G.length); vc.log(G[0].width); vc.log(G[1].width); vc.log(G.parent.typename); vc.log(d.pathItems.length);\ var sub = L.layers.add(); sub.pathItems.rectangle(10, 10, 5, 5); vc.log(L.pathItems.length); vc.log(sub.pathItems.length);\ sub.pathItems[0].name = 'B'; P[0].name = 'B'; G[1].name = 'B'; G[0].name = 'B';\ vc.log(G.getByName('B') === G[0]); vc.log(G.index('B') === G[0]); vc.log(P.getByName('B') === P[0]); vc.log(P[1] === undefined);", ); let expected = [json!(1), json!(30), json!("PathItems"), json!("Layer"), json!(2), json!(70), json!(50), json!("GroupItem"), json!(3), json!(1), json!(1)]; assert_eq!(out[..11], expected); // With the sublayer's path above the group, the layer's own path and both of the group's paths // named `B`, each collection's named lookups give its own topmost path. assert_eq!(out[11..], [json!(true), json!(true), json!(true), json!(true)]); } #[test] fn a_loop_over_a_groups_path_items_builds_the_list_once() { let mut s = Session::new(); s.execute("file.new", &json!({"width": 400, "height": 300})).unwrap(); let ids: Vec = (0..1000).map(|i| rect(&mut s, f64::from(i % 40) * 10.0, 10.0, 5.0, 5.0).0).collect(); s.execute("select.set", &json!({"ids": ids})).unwrap(); s.execute("object.group", &json!({})).unwrap(); // Reading `length` and `[i]` 1,000 times each stays within 512 KiB of data only when the list // of 1,000 paths is not built again for each read. crate::script::TEST_BRIDGE_BYTES.with(|b| b.set(Some(1 << 19))); let r = run(&mut s, "var P = app.activeDocument.groupItems[0].pathItems, n = 0; for (var i = 0; i < P.length; i++) if (P[i]) n++; vc.log(n);"); crate::script::TEST_BRIDGE_BYTES.with(|b| b.set(None)); assert_eq!(r.unwrap()["log"], json!([1000])); } #[test] fn a_layers_path_items_make_their_paths_on_top_of_that_layer() { let (mut s, ..) = doc(); let bottom = s.active().unwrap().doc.layers[0].id; let selected = s.active().unwrap().selection.clone(); let steps = undo_steps(&s); // A new layer becomes the current one; each creator of the first layer's `pathItems` still // puts its path on top of that layer. let out = log( &mut s, "var d = app.activeDocument, top = d.layers.add(), L = d.layers[1], P = L.pathItems;\ var make = [function () { return P.rectangle(100, 100, 20, 10); }, function () { return P.roundedRectangle(100, 100, 20, 10); },\ function () { return P.ellipse(); }, function () { return P.polygon(); }, function () { return P.star(); }, function () { return P.add(); }];\ for (var i = 0; i < make.length; i++) { var p = make[i](); p.name = 'm' + i; vc.log(p.layer.name === L.name); vc.log(L.pageItems[0].name); }\ vc.log(d.activeLayer.name === top.name); vc.log(top.pageItems.length); vc.log(P.length);", ); let mut expected = Vec::new(); for i in 0..6 { expected.extend([json!(true), json!(format!("m{i}"))]); } expected.extend([json!(true), json!(0), json!(8)]); assert_eq!(out, expected); assert_eq!(undo_steps(&s), steps + 1); let st = s.active().unwrap(); let kids = st.doc.node(bottom).unwrap().children().unwrap(); assert_eq!(kids.last().unwrap().name.as_deref(), Some("m5")); assert!(st.selection == selected); } #[test] fn a_groups_path_items_add_puts_the_path_in_the_group() { let (mut s, a, b) = doc(); s.execute("select.set", &json!({"ids": [a.0, b.0]})).unwrap(); let gid = NodeId(s.execute("object.group", &json!({})).unwrap()["id"].as_u64().unwrap()); s.execute("select.none", &json!({})).unwrap(); let steps = undo_steps(&s); let before = format!("{:?}", s.active().unwrap().doc.layers); let out = log( &mut s, "var g = app.activeDocument.groupItems[0], n = g.pathItems.add(); n.name = 'N';\ vc.log(n.parent.typename); vc.log(g.pageItems[0].name); vc.log(n.pathPoints.length);\ var r = g.pathItems.rectangle(50, 50, 10, 10); r.name = 'R'; vc.log(g.pathItems[0].name); vc.log(g.pathItems.length);", ); assert_eq!(out, [json!("GroupItem"), json!("N"), json!(0), json!("R"), json!(4)]); assert_eq!(undo_steps(&s), steps + 1); let d = &s.active().unwrap().doc; let top: Vec> = d.node(gid).unwrap().children().unwrap().iter().rev().take(2).map(|n| n.name.as_deref()).collect(); assert_eq!(top, [Some("R"), Some("N")]); s.execute("edit.undo", &json!({})).unwrap(); assert_eq!(format!("{:?}", s.active().unwrap().doc.layers), before); } #[test] fn path_items_of_a_hidden_or_locked_layer_or_group_make_no_path() { const REFUSED: &str = "Cannot add a path to a hidden or locked layer or group"; let (mut s, a, b) = doc(); s.execute("select.set", &json!({"ids": [a.0, b.0]})).unwrap(); let gid = NodeId(s.execute("object.group", &json!({})).unwrap()["id"].as_u64().unwrap()); s.execute("select.none", &json!({})).unwrap(); let lid = s.active().unwrap().doc.layers[0].id; let before = format!("{:?}", s.active().unwrap().doc.layers); s.execute("layer.setProps", &json!({"id": lid.0, "locked": true})).unwrap(); let locked = format!("{:?}", s.active().unwrap().doc.layers); for src in ["app.activeDocument.layers[0].pathItems.rectangle(10, 10, 5, 5);", "app.activeDocument.layers[0].pathItems.add();"] { assert!(failure(&mut s, src).contains(REFUSED), "{src}"); assert_eq!(format!("{:?}", s.active().unwrap().doc.layers), locked); } // Caught inside the script, each refusal leaves both collections as they were, and the group, // shown and unlocked itself, is refused for its locked layer. let out = log( &mut s, "var L = app.activeDocument.layers[0], g = app.activeDocument.groupItems[0], m = [];\ var n = [L.pathItems.length, g.pathItems.length];\ try { L.pathItems.rectangle(10, 10, 5, 5); } catch (e) { m.push(e.message); }\ try { g.pathItems.add(); } catch (e) { m.push(e.message); }\ try { g.pathItems.ellipse(); } catch (e) { m.push(e.message); }\ vc.log(m); vc.log([L.pathItems.length, g.pathItems.length]); vc.log(n);", ); let refused: Vec<&str> = out[0].as_array().unwrap().iter().filter_map(Value::as_str).collect(); assert_eq!(refused.len(), 3, "{out:?}"); assert!(refused.iter().all(|m| m.contains(REFUSED)), "{out:?}"); assert_eq!(out[1], out[2], "the collections are unchanged inside the script"); assert_eq!(format!("{:?}", s.active().unwrap().doc.layers), locked); s.execute("layer.setProps", &json!({"id": lid.0, "locked": false})).unwrap(); s.execute("layer.setProps", &json!({"id": gid.0, "visible": false})).unwrap(); let hidden = format!("{:?}", s.active().unwrap().doc.layers); assert!(failure(&mut s, "app.activeDocument.groupItems[0].pathItems.ellipse();").contains(REFUSED)); assert_eq!(format!("{:?}", s.active().unwrap().doc.layers), hidden); s.execute("layer.setProps", &json!({"id": gid.0, "visible": true})).unwrap(); assert_eq!(format!("{:?}", s.active().unwrap().doc.layers), before); // A group's `pathItems` read after the group is gone throw. let msg = failure(&mut s, "var g = app.activeDocument.groupItems[0], P = g.pathItems; g.remove(); P.length;"); assert!(msg.contains("Stale page item"), "{msg}"); } /// A collection read with a name gives what its `getByName` gives, in a layer's or a group's /// collections as in the document's, and a name nothing has is not `in` it. #[test] fn collections_read_with_a_name_give_the_named_object() { let (mut s, a, b) = doc(); s.execute("select.set", &json!({"ids": [a.0, b.0]})).unwrap(); s.execute("object.group", &json!({})).unwrap(); s.execute("select.none", &json!({})).unwrap(); let out = log( &mut s, "var d = app.activeDocument, g = d.groupItems[0], r = g.pageItems[0]; r.name = 'Logo'; g.name = 'G';\ vc.log([g.pageItems['Logo'] === r, g.pathItems['Logo'] === r, d.pathItems['Logo'] === r, d.groupItems['G'] === g,\ d.layers[0].groupItems['G'] === g, d.layers['Layer 1'] === d.layers[0], d.artboards[d.artboards[0].name] === d.artboards[0]]);\ vc.log(['Logo' in g.pageItems, 'missing' in g.pageItems, 'missing' in g.pathItems, 'missing' in d.layers, 'missing' in d.artboards]);\ var m = ''; try { g.pageItems['missing']; } catch (e) { m = e.message; } vc.log(m);", ); assert_eq!(out[0], json!([true, true, true, true, true, true, true]), "{out:?}"); assert_eq!(out[1], json!([true, false, false, false, false]), "{out:?}"); assert_eq!(out[2], json!("Not found"), "{out:?}"); } /// A compound path's `pathItems.removeAll()` leaves it, empty, as `remove()` of its last path /// does. #[test] fn clearing_the_paths_of_a_compound_path_keeps_the_compound_path() { let (mut s, a, b) = doc(); s.execute("select.set", &json!({"ids": [a.0, b.0]})).unwrap(); s.execute("object.compoundPath.make", &json!({})).unwrap(); let compound = layer_order(&s)[0]; let out = log(&mut s, "var c = app.activeDocument.compoundPathItems[0]; c.pathItems.removeAll(); vc.log([c.typename, c.pathItems.length]);"); assert_eq!(out, vec![json!(["CompoundPathItem", 0])]); let d = &s.active().unwrap().doc; assert!(d.node(compound).is_some_and(|n| n.children().is_some_and(|c| c.is_empty())), "the compound path stays, empty"); } /// A Live Paint group's collections make nothing in it: what they made would join the group's /// paths instead of being one of its objects. #[test] fn collections_of_a_live_paint_group_make_nothing() { const REFUSED: &str = "Cannot add an object to a Live Paint group"; let (mut s, a, b) = doc(); s.execute("select.set", &json!({"ids": [a.0, b.0]})).unwrap(); s.execute("livePaint.make", &json!({})).unwrap(); s.execute("select.none", &json!({})).unwrap(); let before = format!("{:?}", s.active().unwrap().doc.layers); let out = log( &mut s, "var g = app.activeDocument.pageItems[0], m = [];\ try { g.pathItems.rectangle(10, 10, 5, 5); } catch (e) { m.push(e.message); }\ try { g.pathItems.add(); } catch (e) { m.push(e.message); }\ try { g.groupItems.add(); } catch (e) { m.push(e.message); }\ try { g.compoundPathItems.add(); } catch (e) { m.push(e.message); }\ vc.log(m);", ); let refused: Vec<&str> = out[0].as_array().unwrap().iter().filter_map(Value::as_str).collect(); assert_eq!(refused.len(), 4, "{out:?}"); assert!(refused.iter().all(|m| m.contains(REFUSED)), "{out:?}"); assert_eq!(format!("{:?}", s.active().unwrap().doc.layers), before); } #[test] fn compound_path_items_add_makes_an_empty_compound_path_that_takes_paths() { let (mut s, a, b) = doc(); s.execute("select.set", &json!({"ids": [a.0]})).unwrap(); s.execute("paint.setFill", &json!({"color": "#ff0000"})).unwrap(); s.execute("select.none", &json!({})).unwrap(); let red = s.active().unwrap().doc.node(a).unwrap().appearance.clone(); let steps = undo_steps(&s); let out = log( &mut s, "var d = app.activeDocument, c = d.compoundPathItems.add(); c.name = 'C'; vc.log(c.typename); vc.log(c.pathItems.length);\ vc.log(d.compoundPathItems.length); vc.log(c.layer.name);", ); assert_eq!(out, [json!("CompoundPathItem"), json!(0), json!(1), json!("Layer 1")]); assert_eq!(undo_steps(&s), steps + 1); let cid = { let d = &s.active().unwrap().doc; let lid = d.layer_containing(a).unwrap(); d.node(lid).unwrap().children().unwrap().iter().find(|n| matches!(n.kind, NodeKind::Compound { .. })).unwrap().id }; // Paths moved into it become its members, and it takes the paint of the first one. The layer // lists the new compound path first, then `b`, then `a`. let source = "var d = app.activeDocument, c = d.compoundPathItems.getByName('C'), L = d.layers[0].pageItems, a = L[2], b = L[1];\ vc.log(L[0].name); a.move(c, ElementPlacement.PLACEATBEGINNING); b.move(c, ElementPlacement.PLACEATEND);\ vc.log(c.pathItems.length); vc.log(a.parent.typename); vc.log(b.parent.typename);"; assert_eq!(log(&mut s, source), [json!("C"), json!(2), json!("CompoundPathItem"), json!("CompoundPathItem")]); let c = s.active().unwrap().doc.node(cid).unwrap(); let members: Vec = c.children().unwrap().iter().map(|n| n.id).collect(); assert_eq!(members.len(), 2); assert!(members.contains(&a) && members.contains(&b)); assert_eq!(c.appearance, red); // A group goes neither into a compound path nor beside a path inside one. let before = format!("{:?}", s.active().unwrap().doc.layers); for src in [ "var d = app.activeDocument, g = d.groupItems.add(); g.move(d.compoundPathItems.getByName('C'));", "var d = app.activeDocument, g = d.groupItems.add(); g.move(d.compoundPathItems.getByName('C').pathItems[0], ElementPlacement.PLACEBEFORE);", "app.activeDocument.compoundPathItems.add().move(app.activeDocument.compoundPathItems.getByName('C'));", ] { assert!(failure(&mut s, src).contains("only paths"), "{src}"); } assert_eq!(format!("{:?}", s.active().unwrap().doc.layers), before); // Caught inside the run, the refused move leaves the object where it was and the compound // path's members as they were. for (make, to) in [("d.groupItems.add()", "C"), ("d.groupItems.add()", "C.pathItems[0], ElementPlacement.PLACEBEFORE"), ("d.compoundPathItems.add()", "C")] { let src = format!( "var d = app.activeDocument, C = d.compoundPathItems.getByName('C'), o = {make};\ try {{ o.move({to}); vc.log('moved'); }} catch (e) {{ vc.log(String(e).indexOf('only paths') >= 0); }}\ vc.log(o.parent.typename); vc.log(C.pathItems.length); vc.log(C.pathItems[0].parent.typename);" ); assert_eq!(log(&mut s, &src), [json!(true), json!("Layer"), json!(2), json!("CompoundPathItem")], "{src}"); } assert!(s.execute("object.newGroup", &json!({"compound": 1})).unwrap_err().to_string().contains("true or false")); } /// The paint of a compound path's only path is the compound path's: moved into an empty compound /// path, the path gives it that paint, not its own default one. #[test] fn a_path_moved_out_of_a_compound_path_gives_an_empty_one_the_paint_it_was_drawn_with() { let (mut s, a, _) = doc(); s.execute("select.set", &json!({"ids": [a.0]})).unwrap(); s.execute("paint.setFill", &json!({"color": "#ff0000"})).unwrap(); let red = s.active().unwrap().doc.node(a).unwrap().appearance.clone(); s.execute("object.compoundPath.make", &json!({})).unwrap(); s.execute("select.none", &json!({})).unwrap(); let out = log( &mut s, "var d = app.activeDocument, old = d.compoundPathItems[0], c = d.compoundPathItems.add(); c.name = 'C';\ old.pathItems[0].move(c); vc.log(d.compoundPathItems.getByName('C').pathItems.length);", ); assert_eq!(out, [json!(1)]); let d = &s.active().unwrap().doc; let c = d.parent_of(a).and_then(|p| d.node(p)).unwrap(); assert_eq!(c.name.as_deref(), Some("C")); assert_eq!(c.appearance, red); assert_ne!(d.node(a).unwrap().appearance, red); } /// `node.move` into a compound path leaves the path without paint or pixel alignment of its own, /// as Make Compound Path leaves its paths, and the compound path takes the alignment. #[test] fn node_move_into_a_compound_path_clears_the_paths_own_paint() { let (mut s, a, b) = doc(); s.execute("select.set", &json!({"ids": [a.0]})).unwrap(); s.execute("object.compoundPath.make", &json!({})).unwrap(); let cid = s.active().unwrap().doc.parent_of(a).unwrap(); s.execute("select.set", &json!({"ids": [b.0]})).unwrap(); s.execute("paint.setFill", &json!({"color": "#00ff00"})).unwrap(); s.execute("object.setPixelAligned", &json!({"on": true})).unwrap(); s.execute("select.none", &json!({})).unwrap(); let painted = s.active().unwrap().doc.node(b).unwrap().appearance.clone(); let member = s.active().unwrap().doc.node(a).unwrap().appearance.clone(); let steps = undo_steps(&s); s.execute("node.move", &json!({"id": b.0, "parent": cid.0, "index": 1})).unwrap(); assert_eq!(undo_steps(&s), steps + 1); let d = &s.active().unwrap().doc; assert_eq!(d.parent_of(b), Some(cid)); let n = d.node(b).unwrap(); assert_ne!(n.appearance, painted); assert_eq!(n.appearance, member); assert!(!n.extras().pixel_aligned); assert!(d.node(cid).unwrap().extras().pixel_aligned); } #[test] fn node_move_refuses_a_guide_and_a_locked_or_hidden_compound_path() { let (mut s, a, b) = doc(); let cid = NodeId(s.execute("object.newGroup", &json!({"compound": true})).unwrap()["id"].as_u64().unwrap()); assert!(matches!(s.active().unwrap().doc.node(cid).unwrap().kind, NodeKind::Compound { .. })); s.execute("select.set", &json!({"ids": [a.0]})).unwrap(); s.execute("view.guides.make", &json!({})).unwrap(); let before = format!("{:?}", s.active().unwrap().doc.layers); let err = s.execute("node.move", &json!({"id": a.0, "parent": cid.0, "index": 0})).unwrap_err().to_string(); assert!(err.contains("only paths"), "{err}"); for hide in ["object.lock", "object.hide"] { s.execute("select.set", &json!({"ids": [cid.0]})).unwrap(); s.execute(hide, &json!({})).unwrap(); let before = format!("{:?}", s.active().unwrap().doc.layers); let err = s.execute("node.move", &json!({"id": b.0, "parent": cid.0, "index": 0})).unwrap_err().to_string(); assert!(err.contains("hidden or locked"), "{hide}: {err}"); assert_eq!(format!("{:?}", s.active().unwrap().doc.layers), before); s.execute("edit.undo", &json!({})).unwrap(); } assert_eq!(format!("{:?}", s.active().unwrap().doc.layers), before); } /// `node.move` refuses a clip group's clipping path for a compound path and leaves the group /// clipped as it was; the art the group clips still goes in. #[test] fn node_move_refuses_a_clipping_path_for_a_compound_path() { let (mut s, a, b) = doc(); s.execute("select.set", &json!({"ids": [a.0, b.0]})).unwrap(); let g = NodeId(s.execute("object.clippingMask.make", &json!({})).unwrap()["id"].as_u64().unwrap()); s.execute("select.none", &json!({})).unwrap(); let cid = NodeId(s.execute("object.newGroup", &json!({"compound": true})).unwrap()["id"].as_u64().unwrap()); let d = &s.active().unwrap().doc; assert_eq!(d.position(b).map(|(p, i, _)| (p, i)), Some((Some(g), 0))); let (before, steps) = (format!("{:?}", d.layers), undo_steps(&s)); let err = s.execute("node.move", &json!({"id": b.0, "parent": cid.0, "index": 0})).unwrap_err().to_string(); assert!(err.contains("clipping path"), "{err}"); assert_eq!(format!("{:?}", s.active().unwrap().doc.layers), before); assert_eq!(undo_steps(&s), steps); s.execute("node.move", &json!({"id": a.0, "parent": cid.0, "index": 0})).unwrap(); let d = &s.active().unwrap().doc; assert_eq!(d.parent_of(a), Some(cid)); assert!(d.node(g).unwrap().clips()); assert_eq!(d.position(b).map(|(p, i, _)| (p, i)), Some((Some(g), 0))); } /// `duplicate(compoundPath)` puts the copy into the compound path; an empty one takes its paint. #[test] fn a_path_duplicated_into_an_empty_compound_path_becomes_its_path() { let (mut s, a, _) = doc(); s.execute("select.set", &json!({"ids": [a.0]})).unwrap(); s.execute("paint.setFill", &json!({"color": "#0000ff"})).unwrap(); s.execute("select.none", &json!({})).unwrap(); let blue = s.active().unwrap().doc.node(a).unwrap().appearance.clone(); let out = log( &mut s, "var d = app.activeDocument, c = d.compoundPathItems.add(), p = d.layers[0].pageItems[2]; var q = p.duplicate(c);\ vc.log(c.pathItems.length); vc.log(q.parent.typename); vc.log(p.parent.typename);", ); assert_eq!(out, [json!(1), json!("CompoundPathItem"), json!("Layer")]); let d = &s.active().unwrap().doc; let lid = d.layer_containing(a).unwrap(); let c = d.node(lid).unwrap().children().unwrap().iter().find(|n| matches!(n.kind, NodeKind::Compound { .. })).unwrap(); assert_eq!(c.appearance, blue); assert_eq!(d.node(a).unwrap().appearance, blue); // The copy keeps no paint, live shape or pixel alignment of its own. let q = &c.children().unwrap()[0]; assert_ne!(q.appearance, blue); assert!(matches!(q.kind, NodeKind::Path { live: None, .. })); assert!(!q.extras().pixel_aligned); } /// A compound path's `pathItems` list its topmost path first. #[test] fn a_compound_paths_path_items_list_the_topmost_path_first() { let (mut s, a, b) = doc(); s.execute("select.set", &json!({"ids": [a.0, b.0]})).unwrap(); s.execute("object.compoundPath.make", &json!({})).unwrap(); s.execute("select.none", &json!({})).unwrap(); let cid = s.active().unwrap().doc.parent_of(a).unwrap(); let source = "var P = app.activeDocument.compoundPathItems[0].pathItems; vc.log(P[0].width); vc.log(P[1].width);"; // `b` (50 pt wide) on top, then `a` (30 pt wide) on top. s.execute("node.move", &json!({"id": b.0, "parent": cid.0, "index": 1})).unwrap(); assert_eq!(log(&mut s, source), [json!(50), json!(30)]); s.execute("node.move", &json!({"id": a.0, "parent": cid.0, "index": 1})).unwrap(); assert_eq!(log(&mut s, source), [json!(30), json!(50)]); } /// `index(i)` on a layer's `pageItems` and a compound path's `pathItems` gives the object `[i]` /// gives, and `Not found` past the end. #[test] fn scoped_collections_index_by_number() { let (mut s, a, b) = doc(); s.execute("select.set", &json!({"ids": [a.0, b.0]})).unwrap(); s.execute("object.compoundPath.make", &json!({})).unwrap(); let out = log( &mut s, "var d = app.activeDocument, L = d.layers[0].pageItems, P = d.compoundPathItems[0].pathItems;\ vc.log(L.index(0).typename); vc.log(P.index(1).width === P[1].width);", ); assert_eq!(out, vec![json!("CompoundPathItem"), json!(true)]); let e = failure(&mut s, "app.activeDocument.compoundPathItems[0].pathItems.index(5);"); assert!(e.contains("Not found"), "{e}"); } #[test] fn scoped_collection_methods_reject_the_other_collection_class() { let (mut s, a, b) = doc(); s.execute("select.set", &json!({"ids": [a.0, b.0]})).unwrap(); s.execute("object.compoundPath.make", &json!({})).unwrap(); rect(&mut s, 200.0, 20.0, 30.0, 40.0); let before = s.active().unwrap().doc.clone(); let steps = undo_steps(&s); for (class, receiver) in [("PathItems", "app.activeDocument.layers[0].pageItems"), ("PageItems", "app.activeDocument.compoundPathItems[0].pathItems")] { for call in ["removeAll.call(r)", "getByName.call(r, '')", "index.call(r, 0)"] { let source = format!("var r = {receiver}; {class}.prototype.{call};"); let error = failure(&mut s, &source); assert!(error.contains(&format!("not a {class}")), "{source}: {error}"); assert_eq!(s.active().unwrap().doc, before); assert_eq!(undo_steps(&s), steps); } for property in ["typename", "length", "parent"] { let source = format!("Object.getOwnPropertyDescriptor({class}.prototype, '{property}').get.call({receiver});"); let error = failure(&mut s, &source); assert!(error.contains(&format!("not a {class}")), "{source}: {error}"); } } assert_eq!(s.active().unwrap().doc, before); assert_eq!(undo_steps(&s), steps); } #[test] fn clear_releases_a_group_after_removing_its_last_compound_mask_member() { for mode in ["whole", "anchors", "mixed"] { let (mut s, a, b) = doc(); s.execute("select.set", &json!({"ids": [a.0, b.0]})).unwrap(); s.execute("object.compoundPath.make", &json!({})).unwrap(); let compound = s.active().unwrap().doc.parent_of(a).unwrap(); s.execute("edit.clear", &json!({"ids": [b.0]})).unwrap(); s.execute("select.set", &json!({"ids": [compound.0]})).unwrap(); let group = NodeId(s.execute("object.group", &json!({})).unwrap()["id"].as_u64().unwrap()); run(&mut s, "app.activeDocument.groupItems[0].clipped = true;").unwrap(); let other = rect(&mut s, 200.0, 20.0, 30.0, 40.0); s.execute("select.set", &json!({"ids": [a.0]})).unwrap(); if mode == "anchors" { s.execute("select.anchors", &json!({"id": a.0, "anchors": [[0, 0], [0, 1], [0, 2]]})).unwrap(); } else if mode == "mixed" { s.execute("select.anchors", &json!({"id": other.0, "anchors": [[0, 0]], "mode": "add"})).unwrap(); } let before = s.active().unwrap().doc.clone(); let selection = s.active().unwrap().selection.clone(); let steps = undo_steps(&s); s.execute("edit.clear", &json!({})).unwrap(); let d = &s.active().unwrap().doc; assert!(d.node(a).is_none() && d.node(compound).is_none(), "{mode}"); assert!(d.node(other).is_some(), "{mode}"); assert!(d.node(group).unwrap().children().unwrap().is_empty(), "{mode}"); assert!(!d.node(group).unwrap().clips(), "{mode}"); assert_eq!(undo_steps(&s), steps + 1, "{mode}"); let after = d.clone(); s.execute("edit.undo", &json!({})).unwrap(); assert_eq!(s.active().unwrap().doc, before, "{mode}"); assert_eq!(s.active().unwrap().selection, selection, "{mode}"); s.execute("edit.redo", &json!({})).unwrap(); assert_eq!(s.active().unwrap().doc, after, "{mode}"); } } /// Ruler guides left in a clipped layer do not count as other objects: `removeAll()` deletes the /// layer's clipping compound path. #[test] fn scoped_remove_all_deletes_a_clipping_path_beside_ruler_guides() { let (mut s, a, b) = doc(); s.execute("select.set", &json!({"ids": [a.0, b.0]})).unwrap(); s.execute("object.compoundPath.make", &json!({})).unwrap(); let cid = s.active().unwrap().doc.parent_of(a).unwrap(); let layer = s.active().unwrap().doc.parent_of(cid).unwrap(); s.execute("guide.add", &json!({"vertical": true, "pos": 120})).unwrap(); s.execute("layer.clippingMask.toggle", &json!({"id": layer.0})).unwrap(); run(&mut s, "app.activeDocument.layers[0].compoundPathItems.removeAll();").unwrap(); assert!(s.active().unwrap().doc.node(cid).is_none()); assert!(!s.active().unwrap().doc.node(layer).unwrap().clips()); let added = rect(&mut s, 200.0, 20.0, 30.0, 40.0); assert_eq!(s.active().unwrap().doc.parent_of(added), Some(layer)); assert!(!s.active().unwrap().doc.node(layer).unwrap().clips()); s.execute("edit.undo", &json!({})).unwrap(); s.execute("edit.undo", &json!({})).unwrap(); assert!(s.active().unwrap().doc.node(cid).is_some()); assert!(s.active().unwrap().doc.node(layer).unwrap().clips()); s.execute("edit.redo", &json!({})).unwrap(); rect(&mut s, 200.0, 20.0, 30.0, 40.0); assert!(!s.active().unwrap().doc.node(layer).unwrap().clips()); } #[test] fn scoped_remove_all_releases_an_empty_clipping_group_in_one_step() { let (mut s, a, b) = doc(); s.execute("select.set", &json!({"ids": [a.0, b.0]})).unwrap(); s.execute("object.group", &json!({})).unwrap(); run(&mut s, "app.activeDocument.groupItems[0].clipped = true;").unwrap(); let steps = undo_steps(&s); let out = log(&mut s, "var g = app.activeDocument.groupItems[0]; g.pageItems.removeAll(); vc.log(g.clipped); vc.log(g.pageItems.length);"); assert_eq!(out, [json!(false), json!(0)]); assert_eq!(undo_steps(&s), steps + 1); s.execute("edit.undo", &json!({})).unwrap(); let out = log(&mut s, "var g = app.activeDocument.groupItems[0]; vc.log(g.clipped); vc.log(g.pageItems.length);"); assert_eq!(out, [json!(true), json!(2)]); } #[test] fn clipped_setter_refuses_locked_artboards_layers_and_groups() { for target in ["artboard", "layer", "group"] { for clipped in [false, true] { let (mut s, a, b) = doc(); s.execute("select.set", &json!({"ids": [a.0, b.0]})).unwrap(); let g = s.execute("object.group", &json!({})).unwrap()["id"].as_u64().unwrap(); if clipped { run(&mut s, "app.activeDocument.groupItems[0].clipped = true;").unwrap(); } let layer = s.active().unwrap().doc.layer_containing(NodeId(g)).unwrap(); s.edit("Lock", |d, _| { match target { "artboard" => d.artboards[0].locked = true, "layer" => d.node_mut(layer).unwrap().locked = true, _ => d.node_mut(NodeId(g)).unwrap().locked = true, } Ok(()) }) .unwrap(); let steps = undo_steps(&s); let src = format!( "var g = app.activeDocument.groupItems[0]; try {{ g.clipped = {}; vc.log('changed'); }} catch (e) {{ vc.log(e.message); }} vc.log(g.clipped);", !clipped ); let out = log(&mut s, &src); assert!(out[0].as_str().unwrap().contains("locked"), "{target}: {out:?}"); assert_eq!(out[1], json!(clipped)); assert_eq!(undo_steps(&s), steps); } } } #[test] fn clipped_setter_refuses_a_hidden_or_locked_topmost_object() { for hidden in [false, true] { let (mut s, a, b) = doc(); s.execute("select.set", &json!({"ids": [a.0, b.0]})).unwrap(); s.execute("object.group", &json!({})).unwrap(); s.edit("Protect", |d, _| { let n = d.node_mut(b).unwrap(); if hidden { n.visible = false; } else { n.locked = true; } Ok(()) }) .unwrap(); let before = format!("{:?}", s.active().unwrap().doc.node(b).unwrap().appearance); let steps = undo_steps(&s); let out = log( &mut s, "var g = app.activeDocument.groupItems[0]; try { g.clipped = true; vc.log('changed'); } catch (e) { vc.log(e.message); } vc.log(g.clipped);", ); assert!(out[0].as_str().unwrap().contains("hidden or locked"), "hidden {hidden}: {out:?}"); assert_eq!(out[1], json!(false)); assert_eq!(format!("{:?}", s.active().unwrap().doc.node(b).unwrap().appearance), before); assert_eq!(undo_steps(&s), steps); } } #[test] fn scoped_collections_convert_to_strings() { let (mut s, ..) = doc(); let out = log( &mut s, "var l = app.activeDocument.layers[0], g = l.groupItems.add(), c = g.compoundPathItems.add();\ var lists = [l.pageItems, g.pageItems, l.layers, l.groupItems, g.groupItems, l.compoundPathItems, g.compoundPathItems, c.pathItems];\ for (var i = 0; i < lists.length; i++) { vc.log(String(lists[i])); vc.log(lists[i].toString()); }", ); let expected: Vec<_> = ["PageItems", "PageItems", "Layers", "GroupItems", "GroupItems", "CompoundPathItems", "CompoundPathItems", "PathItems"] .into_iter() .flat_map(|name| [json!(format!("[{name}]")), json!(format!("[{name}]"))]) .collect(); assert_eq!(out, expected); } #[test] fn layer_color_can_be_assigned_to_path_paint() { let (mut s, ..) = doc(); let out = log( &mut s, "var l = app.activeDocument.layers[0]; l.color = {red: 10, green: 20, blue: 30};\ var c = l.color; vc.log(c.typename); var p = l.pageItems[0]; p.fillColor = c;\ vc.log([p.fillColor.red, p.fillColor.green, p.fillColor.blue]);", ); assert_eq!(out, [json!("RGBColor"), json!([10, 20, 30])]); } /// `removeAll()` refuses to delete the compound path that clips a group while the group keeps /// other art, and deletes nothing. #[test] fn scoped_remove_all_refuses_a_clipping_compound_path() { let (mut s, a, b) = doc(); let e = rect(&mut s, 200.0, 100.0, 20.0, 20.0); s.execute("select.set", &json!({"ids": [a.0, b.0]})).unwrap(); s.execute("object.compoundPath.make", &json!({})).unwrap(); s.execute("object.arrange.bringToFront", &json!({})).unwrap(); let cid = s.active().unwrap().doc.parent_of(a).unwrap(); s.execute("select.set", &json!({"ids": [cid.0, e.0]})).unwrap(); s.execute("object.group", &json!({})).unwrap(); run(&mut s, "app.activeDocument.groupItems[0].clipped = true;").unwrap(); let before = format!("{:?}", s.active().unwrap().doc.layers); let steps = undo_steps(&s); let err = failure(&mut s, "app.activeDocument.groupItems[0].compoundPathItems.removeAll();"); assert!(err.contains("clipping"), "{err}"); assert_eq!(format!("{:?}", s.active().unwrap().doc.layers), before); assert_eq!(undo_steps(&s), steps); } /// Runs the document's `pathItems.removeAll()`, then logs its error message and the number of paths. const CAUGHT_PATHS_REMOVE_ALL: &str = "var p = app.activeDocument.pathItems, m = '';\ try { p.removeAll(); } catch (e) { m = e.message; }\ vc.log(m); vc.log(p.length);"; /// The document's `pathItems.removeAll()` refuses to delete the path that clips a group while the /// group keeps other art, and deletes nothing, as the scoped collections do. #[test] fn document_path_items_remove_all_refuses_a_clipping_path() { let (mut s, a, b) = doc(); let t = NodeId(s.execute("text.create", &json!({"x": 10, "y": 250, "text": "hi"})).unwrap()["id"].as_u64().unwrap()); s.execute("select.set", &json!({"ids": [a.0, t.0]})).unwrap(); s.execute("object.group", &json!({})).unwrap(); s.execute("select.set", &json!({"ids": [a.0]})).unwrap(); s.execute("object.arrange.bringToFront", &json!({})).unwrap(); run(&mut s, "app.activeDocument.groupItems[0].clipped = true;").unwrap(); let d = &s.active().unwrap().doc; assert_eq!(d.parent_of(a).and_then(|g| d.node(g)).and_then(|g| g.children()).map(|k| k[0].id), Some(a)); let before = s.active().unwrap().doc.clone(); let steps = undo_steps(&s); // The script catches the error and counts the paths before the run ends, so the run's own // rollback cannot hide a deletion. let out = log(&mut s, CAUGHT_PATHS_REMOVE_ALL); let e = out[0].as_str().unwrap_or_default(); assert!(e.contains("cannot delete a clipping path"), "{out:?}"); assert!(e.contains(&a.0.to_string()), "{out:?}"); assert!(!e.contains(&b.0.to_string()), "{out:?}"); assert_eq!(out[1], json!(2)); assert_eq!(s.active().unwrap().doc, before); assert_eq!(undo_steps(&s), steps); } /// The document's `pathItems.removeAll()` would empty a compound path that clips a group keeping /// other art, so it throws and deletes nothing. #[test] fn document_path_items_remove_all_refuses_to_empty_a_clipping_compound_path() { let (mut s, a, b) = doc(); let t = NodeId(s.execute("text.create", &json!({"x": 10, "y": 250, "text": "hi"})).unwrap()["id"].as_u64().unwrap()); s.execute("select.set", &json!({"ids": [a.0, b.0]})).unwrap(); s.execute("object.compoundPath.make", &json!({})).unwrap(); let compound = s.active().unwrap().doc.parent_of(a).unwrap(); s.execute("select.set", &json!({"ids": [compound.0, t.0]})).unwrap(); s.execute("object.group", &json!({})).unwrap(); s.execute("select.set", &json!({"ids": [compound.0]})).unwrap(); s.execute("object.arrange.bringToFront", &json!({})).unwrap(); run(&mut s, "app.activeDocument.groupItems[0].clipped = true;").unwrap(); let d = &s.active().unwrap().doc; assert_eq!(d.parent_of(compound).and_then(|g| d.node(g)).and_then(|g| g.children()).map(|k| k[0].id), Some(compound)); let before = s.active().unwrap().doc.clone(); let steps = undo_steps(&s); let out = log(&mut s, CAUGHT_PATHS_REMOVE_ALL); let e = out[0].as_str().unwrap_or_default(); assert!(e.contains("cannot delete a clipping path"), "{out:?}"); assert!(e.contains(&compound.0.to_string()), "{out:?}"); assert_eq!(out[1], json!(2)); assert_eq!(s.active().unwrap().doc, before); assert_eq!(undo_steps(&s), steps); } /// A clipping mask made of a group of paths, as an imported SVG mask with several shapes is, would /// be left empty by the document's `pathItems.removeAll()`, so it throws and deletes nothing. #[test] fn document_path_items_remove_all_refuses_to_empty_a_clipping_group_of_paths() { let (mut s, a, b) = doc(); let t = NodeId(s.execute("text.create", &json!({"x": 10, "y": 250, "text": "hi"})).unwrap()["id"].as_u64().unwrap()); s.execute("select.set", &json!({"ids": [a.0, b.0]})).unwrap(); let mask = NodeId(s.execute("object.group", &json!({})).unwrap()["id"].as_u64().unwrap()); s.execute("select.set", &json!({"ids": [mask.0, t.0]})).unwrap(); let outer = NodeId(s.execute("object.group", &json!({})).unwrap()["id"].as_u64().unwrap()); s.edit("Clip", |d, _| { if let Some(NodeKind::Group { children, clip }) = d.node_mut(outer).map(|n| &mut n.kind) { children.sort_by_key(|c| c.id != mask); *clip = true; } Ok(()) }) .unwrap(); let d = &s.active().unwrap().doc; assert!(d.node(outer).unwrap().clips()); assert_eq!(d.node(outer).and_then(|g| g.children()).map(|k| k[0].id), Some(mask)); let before = s.active().unwrap().doc.clone(); let steps = undo_steps(&s); let out = log(&mut s, CAUGHT_PATHS_REMOVE_ALL); let e = out[0].as_str().unwrap_or_default(); assert!(e.contains("cannot delete a clipping path"), "{out:?}"); assert!(e.contains(&mask.0.to_string()), "{out:?}"); assert_eq!(out[1], json!(2)); assert_eq!(s.active().unwrap().doc, before); assert_eq!(undo_steps(&s), steps); } /// Type clips by its glyph outlines, so a clipping group that keeps type after its paths go still /// clips, and the document's `pathItems.removeAll()` deletes every path. #[test] fn document_path_items_remove_all_keeps_a_clipping_group_that_holds_type() { let (mut s, a, b) = doc(); let t = NodeId(s.execute("text.create", &json!({"x": 10, "y": 250, "text": "hi"})).unwrap()["id"].as_u64().unwrap()); let u = NodeId(s.execute("text.create", &json!({"x": 10, "y": 300, "text": "yo"})).unwrap()["id"].as_u64().unwrap()); s.execute("select.set", &json!({"ids": [a.0, t.0]})).unwrap(); let mask = NodeId(s.execute("object.group", &json!({})).unwrap()["id"].as_u64().unwrap()); s.execute("select.set", &json!({"ids": [mask.0, u.0]})).unwrap(); let outer = NodeId(s.execute("object.group", &json!({})).unwrap()["id"].as_u64().unwrap()); s.edit("Clip", |d, _| { if let Some(NodeKind::Group { children, clip }) = d.node_mut(outer).map(|n| &mut n.kind) { children.sort_by_key(|c| c.id != mask); *clip = true; } Ok(()) }) .unwrap(); let d = &s.active().unwrap().doc; assert!(d.node(outer).unwrap().clips()); assert_eq!(d.node(outer).and_then(|g| g.children()).map(|k| k[0].id), Some(mask)); let steps = undo_steps(&s); let out = log(&mut s, CAUGHT_PATHS_REMOVE_ALL); assert_eq!(out[0], json!(""), "{out:?}"); assert_eq!(out[1], json!(0)); let d = &s.active().unwrap().doc; assert!(d.node(a).is_none() && d.node(b).is_none()); assert!(d.node(t).is_some() && d.node(u).is_some()); assert_eq!(undo_steps(&s), steps + 1); } /// Type with no characters to draw gives no clip outline, so a clipping group whose paths would /// leave only such type makes the document's `pathItems.removeAll()` throw and delete nothing. #[test] fn document_path_items_remove_all_refuses_when_only_blank_type_would_clip() { let (mut s, a, _) = doc(); let t = NodeId(s.execute("text.create", &json!({"x": 10, "y": 250, "text": "hi"})).unwrap()["id"].as_u64().unwrap()); let u = NodeId(s.execute("text.create", &json!({"x": 10, "y": 300, "text": "yo"})).unwrap()["id"].as_u64().unwrap()); s.execute("select.set", &json!({"ids": [a.0, t.0]})).unwrap(); let mask = NodeId(s.execute("object.group", &json!({})).unwrap()["id"].as_u64().unwrap()); s.execute("select.set", &json!({"ids": [mask.0, u.0]})).unwrap(); let outer = NodeId(s.execute("object.group", &json!({})).unwrap()["id"].as_u64().unwrap()); s.edit("Clip", |d, _| { if let Some(NodeKind::Text(tx)) = d.node_mut(t).map(|n| &mut n.kind) { tx.runs.iter_mut().for_each(|r| r.text = " ".into()); } if let Some(NodeKind::Group { children, clip }) = d.node_mut(outer).map(|n| &mut n.kind) { children.sort_by_key(|c| c.id != mask); *clip = true; } Ok(()) }) .unwrap(); let before = s.active().unwrap().doc.clone(); let steps = undo_steps(&s); let out = log(&mut s, CAUGHT_PATHS_REMOVE_ALL); let e = out[0].as_str().unwrap_or_default(); assert!(e.contains("cannot delete a clipping path"), "{out:?}"); assert!(e.contains(&mask.0.to_string()), "{out:?}"); assert_eq!(out[1], json!(2)); assert_eq!(s.active().unwrap().doc, before); assert_eq!(undo_steps(&s), steps); } /// A clipping group nested in a clipping mask clips by its first member only, so type it clips does /// not keep the outer mask's outline, and the document's `pathItems.removeAll()` names the outer /// mask too. #[test] fn document_path_items_remove_all_ignores_type_clipped_inside_a_nested_mask() { let (mut s, a, b) = doc(); let t = NodeId(s.execute("text.create", &json!({"x": 10, "y": 250, "text": "hi"})).unwrap()["id"].as_u64().unwrap()); let u = NodeId(s.execute("text.create", &json!({"x": 10, "y": 300, "text": "yo"})).unwrap()["id"].as_u64().unwrap()); s.execute("select.set", &json!({"ids": [b.0, t.0]})).unwrap(); let nested = NodeId(s.execute("object.group", &json!({})).unwrap()["id"].as_u64().unwrap()); s.execute("select.set", &json!({"ids": [a.0, nested.0]})).unwrap(); let mask = NodeId(s.execute("object.group", &json!({})).unwrap()["id"].as_u64().unwrap()); s.execute("select.set", &json!({"ids": [mask.0, u.0]})).unwrap(); let outer = NodeId(s.execute("object.group", &json!({})).unwrap()["id"].as_u64().unwrap()); s.edit("Clip", |d, _| { for (g, first) in [(nested, b), (outer, mask)] { if let Some(NodeKind::Group { children, clip }) = d.node_mut(g).map(|n| &mut n.kind) { children.sort_by_key(|c| c.id != first); *clip = true; } } Ok(()) }) .unwrap(); let before = s.active().unwrap().doc.clone(); let out = log(&mut s, CAUGHT_PATHS_REMOVE_ALL); let e = out[0].as_str().unwrap_or_default(); assert!(e.contains("cannot delete a clipping path"), "{out:?}"); assert!(e.split(": ").last().unwrap_or_default().split(", ").any(|id| id == mask.0.to_string()), "{out:?}"); assert_eq!(out[1], json!(2)); assert_eq!(s.active().unwrap().doc, before); } /// A numbered paragraph whose text is a single space still draws its number, which clips, so the /// document's `pathItems.removeAll()` deletes the paths of a clipping group that keeps it. #[test] fn document_path_items_remove_all_keeps_a_clipping_group_whose_type_draws_only_a_list_number() { use vectorcraft_doc::text::{NumberStyle, ParaList}; let (mut s, a, b) = doc(); let t = NodeId(s.execute("text.create", &json!({"x": 10, "y": 250, "text": "hi"})).unwrap()["id"].as_u64().unwrap()); let u = NodeId(s.execute("text.create", &json!({"x": 10, "y": 300, "text": "yo"})).unwrap()["id"].as_u64().unwrap()); s.execute("select.set", &json!({"ids": [a.0, t.0]})).unwrap(); let mask = NodeId(s.execute("object.group", &json!({})).unwrap()["id"].as_u64().unwrap()); s.execute("select.set", &json!({"ids": [mask.0, u.0]})).unwrap(); let outer = NodeId(s.execute("object.group", &json!({})).unwrap()["id"].as_u64().unwrap()); s.edit("Clip", |d, _| { if let Some(NodeKind::Text(tx)) = d.node_mut(t).map(|n| &mut n.kind) { tx.runs.iter_mut().for_each(|r| r.text = " ".into()); let mut styles = tx.paragraph_styles(); styles.iter_mut().for_each(|p| p.list = Some(ParaList::numbered(NumberStyle::Arabic))); tx.set_paragraph_styles(styles); } if let Some(NodeKind::Group { children, clip }) = d.node_mut(outer).map(|n| &mut n.kind) { children.sort_by_key(|c| c.id != mask); *clip = true; } Ok(()) }) .unwrap(); if let Some(NodeKind::Text(tx)) = s.active().unwrap().doc.node(t).map(|n| &n.kind) { assert!(!vectorcraft_text::layout(vectorcraft_text::FontDb::global(), tx).to_bezpath().elements().is_empty()); } let steps = undo_steps(&s); let out = log(&mut s, CAUGHT_PATHS_REMOVE_ALL); assert_eq!(out[0], json!(""), "{out:?}"); assert_eq!(out[1], json!(0)); let d = &s.active().unwrap().doc; assert!(d.node(a).is_none() && d.node(b).is_none()); assert!(d.node(t).is_some() && d.node(u).is_some()); assert_eq!(undo_steps(&s), steps + 1); } /// Area type too short for its first line lays out no glyphs and gives no clip outline, so a /// clipping group whose paths would leave only such type makes the document's /// `pathItems.removeAll()` throw and delete nothing. #[test] fn document_path_items_remove_all_refuses_when_only_overset_type_would_clip() { let (mut s, a, _) = doc(); let t = NodeId( s.execute("text.create", &json!({"x": 10, "y": 250, "size": 12, "area": {"width": 100, "height": 1}, "text": "hi"})).unwrap()["id"] .as_u64() .unwrap(), ); let u = NodeId(s.execute("text.create", &json!({"x": 10, "y": 300, "text": "yo"})).unwrap()["id"].as_u64().unwrap()); if let Some(NodeKind::Text(tx)) = s.active().unwrap().doc.node(t).map(|n| &n.kind) { assert!(vectorcraft_text::layout(vectorcraft_text::FontDb::global(), tx).to_bezpath().elements().is_empty()); } s.execute("select.set", &json!({"ids": [a.0, t.0]})).unwrap(); let mask = NodeId(s.execute("object.group", &json!({})).unwrap()["id"].as_u64().unwrap()); s.execute("select.set", &json!({"ids": [mask.0, u.0]})).unwrap(); let outer = NodeId(s.execute("object.group", &json!({})).unwrap()["id"].as_u64().unwrap()); s.edit("Clip", |d, _| { if let Some(NodeKind::Group { children, clip }) = d.node_mut(outer).map(|n| &mut n.kind) { children.sort_by_key(|c| c.id != mask); *clip = true; } Ok(()) }) .unwrap(); let before = s.active().unwrap().doc.clone(); let out = log(&mut s, CAUGHT_PATHS_REMOVE_ALL); let e = out[0].as_str().unwrap_or_default(); assert!(e.contains("cannot delete a clipping path"), "{out:?}"); assert!(e.contains(&mask.0.to_string()), "{out:?}"); assert_eq!(out[1], json!(2)); assert_eq!(s.active().unwrap().doc, before); } /// Hidden type does not clip, so a clipping group whose paths would leave only hidden type makes /// the document's `pathItems.removeAll()` throw and delete nothing. #[test] fn document_path_items_remove_all_refuses_when_only_hidden_type_would_clip() { let (mut s, a, _) = doc(); let t = NodeId(s.execute("text.create", &json!({"x": 10, "y": 250, "text": "hi"})).unwrap()["id"].as_u64().unwrap()); let u = NodeId(s.execute("text.create", &json!({"x": 10, "y": 300, "text": "yo"})).unwrap()["id"].as_u64().unwrap()); s.execute("select.set", &json!({"ids": [a.0, t.0]})).unwrap(); let mask = NodeId(s.execute("object.group", &json!({})).unwrap()["id"].as_u64().unwrap()); s.execute("select.set", &json!({"ids": [mask.0, u.0]})).unwrap(); let outer = NodeId(s.execute("object.group", &json!({})).unwrap()["id"].as_u64().unwrap()); s.edit("Clip", |d, _| { if let Some(n) = d.node_mut(t) { n.visible = false; } if let Some(NodeKind::Group { children, clip }) = d.node_mut(outer).map(|n| &mut n.kind) { children.sort_by_key(|c| c.id != mask); *clip = true; } Ok(()) }) .unwrap(); let before = s.active().unwrap().doc.clone(); let steps = undo_steps(&s); let out = log(&mut s, CAUGHT_PATHS_REMOVE_ALL); let e = out[0].as_str().unwrap_or_default(); assert!(e.contains("cannot delete a clipping path"), "{out:?}"); assert!(e.contains(&mask.0.to_string()), "{out:?}"); assert_eq!(out[1], json!(2)); assert_eq!(s.active().unwrap().doc, before); assert_eq!(undo_steps(&s), steps); } #[test] fn document_path_items_remove_all_refuses_to_empty_a_clipping_group_with_an_empty_subgroup() { let (mut s, a, b) = doc(); let t = NodeId(s.execute("text.create", &json!({"x": 10, "y": 250, "text": "hi"})).unwrap()["id"].as_u64().unwrap()); s.execute("select.set", &json!({"ids": [a.0, b.0]})).unwrap(); let mask = NodeId(s.execute("object.group", &json!({})).unwrap()["id"].as_u64().unwrap()); let empty = NodeId(s.execute("object.newGroup", &json!({"parent": mask.0})).unwrap()["id"].as_u64().unwrap()); s.execute("select.set", &json!({"ids": [mask.0, t.0]})).unwrap(); let outer = NodeId(s.execute("object.group", &json!({})).unwrap()["id"].as_u64().unwrap()); s.edit("Clip", |d, _| { if let Some(NodeKind::Group { children, clip }) = d.node_mut(outer).map(|n| &mut n.kind) { children.sort_by_key(|c| c.id != mask); *clip = true; } Ok(()) }) .unwrap(); let d = &s.active().unwrap().doc; assert!(d.node(outer).unwrap().clips()); assert_eq!(d.node(outer).and_then(|g| g.children()).map(|k| k[0].id), Some(mask)); assert!(d.node(empty).unwrap().children().unwrap().is_empty()); let before = d.clone(); let steps = undo_steps(&s); let out = log(&mut s, CAUGHT_PATHS_REMOVE_ALL); assert_eq!( out[0], json!(format!("cannot delete a clipping path while its group or layer keeps other objects; release the clipping mask first: {}", mask.0)) ); assert_eq!(out[1], json!(2)); assert_eq!(s.active().unwrap().doc, before); assert_eq!(undo_steps(&s), steps); } #[test] fn document_path_items_remove_all_keeps_empty_groups_but_removes_empty_compounds() { for compound in [false, true] { let (mut s, a, b) = doc(); let c = rect(&mut s, 200.0, 100.0, 20.0, 20.0); s.execute("select.set", &json!({"ids": [b.0, c.0]})).unwrap(); s.execute(if compound { "object.compoundPath.make" } else { "object.group" }, &json!({})).unwrap(); let sibling = s.active().unwrap().doc.parent_of(b).unwrap(); let layer = s.active().unwrap().doc.parent_of(a).unwrap(); s.execute("select.set", &json!({"ids": [a.0]})).unwrap(); s.execute("object.arrange.bringToFront", &json!({})).unwrap(); s.execute("layer.clippingMask.toggle", &json!({"id": layer.0})).unwrap(); let d = &s.active().unwrap().doc; assert!(d.node(layer).unwrap().clips()); assert_eq!(d.node(layer).and_then(|n| n.children()).map(|k| k[0].id), Some(a)); let before = d.clone(); let steps = undo_steps(&s); let out = log(&mut s, CAUGHT_PATHS_REMOVE_ALL); if compound { assert_eq!(out, [json!(""), json!(0)]); let d = &s.active().unwrap().doc; assert!(d.node(sibling).is_none()); assert!(!d.node(layer).unwrap().clips()); assert_eq!(undo_steps(&s), steps + 1); s.execute("edit.undo", &json!({})).unwrap(); assert_eq!(s.active().unwrap().doc, before); } else { assert_eq!( out[0], json!(format!( "cannot delete a clipping path while its group or layer keeps other objects; release the clipping mask first: {}", a.0 )) ); assert_eq!(out[1], json!(3)); assert_eq!(s.active().unwrap().doc, before); assert_eq!(undo_steps(&s), steps); } } } /// A path that leaves a compound path for a layer is drawn as it was: it takes the compound /// path's paint and pixel alignment, as Release Compound Path gives them. #[test] fn a_path_moved_out_of_a_compound_path_keeps_the_paint_it_was_drawn_with() { let (mut s, a, b) = doc(); s.execute("select.set", &json!({"ids": [a.0, b.0]})).unwrap(); s.execute("object.compoundPath.make", &json!({})).unwrap(); s.execute("paint.setFill", &json!({"color": "#ff0000"})).unwrap(); s.execute("object.setPixelAligned", &json!({"on": true})).unwrap(); s.execute("select.none", &json!({})).unwrap(); let (cid, red, lid) = { let d = &s.active().unwrap().doc; let cid = d.parent_of(a).unwrap(); (cid, d.node(cid).unwrap().appearance.clone(), d.layer_containing(a).unwrap()) }; assert_ne!(s.active().unwrap().doc.node(a).unwrap().appearance, red); s.execute("node.move", &json!({"id": a.0, "parent": lid.0})).unwrap(); let d = &s.active().unwrap().doc; assert_eq!(d.parent_of(a), Some(lid)); let n = d.node(a).unwrap(); assert_eq!(n.appearance, red); assert!(n.extras().pixel_aligned); // The path left behind keeps none of its own. assert_eq!(d.parent_of(b), Some(cid)); assert_ne!(d.node(b).unwrap().appearance, red); } /// A path that leaves a pixel-aligned compound path for another compound path makes that one /// pixel-aligned, as it gives it the paint it was drawn with. #[test] fn a_path_moved_between_compound_paths_keeps_its_pixel_alignment() { let (mut s, a, _) = doc(); s.execute("select.set", &json!({"ids": [a.0]})).unwrap(); s.execute("object.compoundPath.make", &json!({})).unwrap(); s.execute("object.setPixelAligned", &json!({"on": true})).unwrap(); s.execute("select.none", &json!({})).unwrap(); assert!(!s.active().unwrap().doc.node(a).unwrap().extras().pixel_aligned); let to = NodeId(s.execute("object.newGroup", &json!({"compound": true})).unwrap()["id"].as_u64().unwrap()); assert!(!s.active().unwrap().doc.node(to).unwrap().extras().pixel_aligned); s.execute("node.move", &json!({"id": a.0, "parent": to.0, "index": 0})).unwrap(); let d = &s.active().unwrap().doc; assert_eq!(d.parent_of(a), Some(to)); assert!(d.node(to).unwrap().extras().pixel_aligned); assert!(!d.node(a).unwrap().extras().pixel_aligned); } /// A pixel-aligned compound path snaps a path that was not aligned when it takes it in. #[test] fn a_pixel_aligned_compound_path_snaps_a_path_moved_into_it() { let (mut s, a, _) = doc(); s.execute("select.set", &json!({"ids": [a.0]})).unwrap(); s.execute("object.compoundPath.make", &json!({})).unwrap(); s.execute("object.setPixelAligned", &json!({"on": true})).unwrap(); s.execute("select.none", &json!({})).unwrap(); let cid = s.active().unwrap().doc.parent_of(a).unwrap(); let loose = rect(&mut s, 200.25, 20.5, 30.0, 40.0); s.execute("select.none", &json!({})).unwrap(); assert!(!s.active().unwrap().doc.node(loose).unwrap().extras().pixel_aligned); s.execute("node.move", &json!({"id": loose.0, "parent": cid.0, "index": 0})).unwrap(); let d = &s.active().unwrap().doc; assert_eq!(d.parent_of(loose), Some(cid)); // Both paths have a 1 pt stroke, so the grid puts their edges where it put the aligned path's. let (b, at) = (d.node(loose).unwrap().geometric_bounds().unwrap(), d.node(a).unwrap().geometric_bounds().unwrap()); assert_ne!(b.x0, 200.25); for (v, w) in [(b.x0, at.x0), (b.y0, at.y0), (b.x1, at.x1), (b.y1, at.y1)] { assert_eq!(v.fract(), w.fract(), "{b:?} is on the pixel grid as {at:?} is"); } } /// A path off the pixel grid that moves into a pixel-aligned compound path is snapped to the grid /// with the compound path's other paths. #[test] fn a_path_moved_into_a_pixel_aligned_compound_path_is_snapped() { let (mut s, a, _) = doc(); s.execute("select.set", &json!({"ids": [a.0]})).unwrap(); s.execute("object.compoundPath.make", &json!({})).unwrap(); s.execute("object.setPixelAligned", &json!({"on": true})).unwrap(); s.execute("select.none", &json!({})).unwrap(); let to = s.active().unwrap().doc.parent_of(a).unwrap(); let r = rect(&mut s, 200.3, 100.3, 20.4, 20.4); s.execute("object.setPixelAligned", &json!({"ids": [r.0], "on": false})).unwrap(); s.execute("select.none", &json!({})).unwrap(); assert!(!s.active().unwrap().doc.node(r).unwrap().extras().pixel_aligned); let before = s.active().unwrap().doc.bounds_of(&[r], false).unwrap(); assert!(before.x0.fract().abs() > 0.01, "{before:?}"); s.execute("node.move", &json!({"id": r.0, "parent": to.0, "index": 0})).unwrap(); let d = &s.active().unwrap().doc; assert!(d.node(to).unwrap().extras().pixel_aligned); let after = d.bounds_of(&[r], false).unwrap(); assert_ne!(after, before); assert!(!d.node(r).unwrap().extras().pixel_aligned); assert!([after.x0, after.y0, after.x1, after.y1].iter().all(|v| (v * 2.0).fract().abs() < 1e-9), "not snapped: {after:?}"); s.execute("edit.undo", &json!({})).unwrap(); assert_eq!(s.active().unwrap().doc.bounds_of(&[r], false).unwrap(), before); } #[test] fn a_compound_paths_path_items_are_page_items() { let (mut s, a, b) = doc(); s.execute("select.set", &json!({"ids": [a.0, b.0]})).unwrap(); s.execute("object.compoundPath.make", &json!({})).unwrap(); let out = log( &mut s, "var d = app.activeDocument, p = d.compoundPathItems[0].pathItems; vc.log(p.typename); vc.log(p.length);\ vc.log(p[0].typename); vc.log(d.layers[0].pageItems.typename); vc.log(p instanceof PathItems);\ vc.log(p instanceof PageItems); vc.log(p.parent === d.compoundPathItems[0]);\ p[0].name = 'member'; vc.log(p.getByName('member') === p[0]); vc.log(p.index(0) === p[0]);", ); assert_eq!( out, [json!("PathItems"), json!(2), json!("PathItem"), json!("PageItems"), json!(true), json!(false), json!(true), json!(true), json!(true)] ); // The path creation methods make the new path inside the compound path. for (made, call) in ["add()", "rectangle(100, 100, 10, 10)", "roundedRectangle(100, 100, 10, 10)", "ellipse(100, 100, 10, 10)", "polygon()", "star()"] .into_iter() .enumerate() { let source = format!( "var p = app.activeDocument.compoundPathItems[0].pathItems; try {{ p.{call}; vc.log('made'); }} catch (e) {{ vc.log(e.message); }} vc.log(p.length);" ); let out = log(&mut s, &source); assert_eq!(out, [json!("made"), json!(made + 3)], "{call}"); } run(&mut s, "var c = app.activeDocument.compoundPathItems[0]; c.pathItems.removeAll(); vc.log(c.pathItems.length);").unwrap(); assert!(s.active().unwrap().doc.node(a).is_none() && s.active().unwrap().doc.node(b).is_none()); } #[test] fn a_compound_paths_path_items_add_makes_the_path_inside_it() { let (mut s, _, _) = doc(); let steps = undo_steps(&s); let out = log( &mut s, "var c = app.activeDocument.layers[0].compoundPathItems.add(); var p = c.pathItems.add(); p.setEntirePath([[0, 0], [10, 0], [10, 10]]);\ var r = c.pathItems.rectangle(50, 20, 10, 10); vc.log(c.pathItems.length); vc.log(p.parent.typename); vc.log(r.parent.typename);\ vc.log(c.pathItems[0].width);", ); assert_eq!(out, [json!(2), json!("CompoundPathItem"), json!("CompoundPathItem"), json!(10)]); let d = &s.active().unwrap().doc; let compound = d.layers[0].children().unwrap().iter().find(|n| matches!(n.kind, NodeKind::Compound { .. })).unwrap(); assert_eq!(compound.children().unwrap().len(), 2); assert_eq!(undo_steps(&s), steps + 1, "the run is one undo step"); } #[test] fn art_collections_follow_edits_between_reads() { let (mut s, _, _) = doc(); let out = log( &mut s, "var d = app.activeDocument, p = d.layers[0].pageItems, g = d.groupItems; vc.log(p.length); vc.log(g.length);\ d.layers[0].groupItems.add(); vc.log(p.length); vc.log(p[0].typename); vc.log(g.length);\ p[0].remove(); vc.log(p.length); vc.log(p[0].typename);", ); assert_eq!(out, [json!(2), json!(0), json!(3), json!("GroupItem"), json!(1), json!(2), json!("PathItem")]); } #[test] fn a_loop_over_groups_and_their_items_lists_each_collection_once() { let mut s = Session::new(); s.execute("file.new", &json!({"width": 400, "height": 300})).unwrap(); for i in 0..5 { let x = f64::from(i) * 20.0; let (a, b) = (rect(&mut s, x, 0.0, 10.0, 10.0), rect(&mut s, x, 20.0, 10.0, 10.0)); s.execute("select.set", &json!({"ids": [a.0, b.0]})).unwrap(); s.execute("object.group", &json!({})).unwrap(); } // Both scripts make the same calls. The first reads the groups and each group's items in // turn; the second reads every group before any group's items, so it builds each list once. let nested = "var gs = app.activeDocument.groupItems, n = 0;\ for (var i = 0; i < gs.length; i++) { var p = gs[i].pageItems; for (var j = 0; j < p.length; j++) { p[j]; n++; } }\ vc.log(n);"; let in_order = "var gs = app.activeDocument.groupItems, all = [], n = 0;\ for (var i = 0; i < gs.length; i++) { all.push(gs[i]); }\ for (var k = 0; k < all.length; k++) { var p = all[k].pageItems; for (var j = 0; j < p.length; j++) { p[j]; n++; } }\ vc.log(n);"; crate::script::dom::ART_SCANS.with(|c| c.set(0)); let a = run(&mut s, nested).unwrap(); // The groups' list and each group's list are built once: 1 + 5 lists. let scans = crate::script::dom::ART_SCANS.with(std::cell::Cell::get); assert!(scans <= 6, "{scans} lists built for 5 groups"); let b = run(&mut s, in_order).unwrap(); assert_eq!((a["log"].clone(), b["log"].clone()), (json!([10]), json!([10]))); assert_eq!(a["hostCalls"], b["hostCalls"], "{a}\n{b}"); // A list built again for each lookup would add a host call to both. Building the document's // page items for `groupItems` counts one call. assert_eq!(a["hostCalls"], json!(51), "{a}"); } /// A loop over `layers.length` and `layers[i]` lists the layers once while the document is /// unchanged, so it looks at each layer a bounded number of times. #[test] fn a_loop_over_layers_lists_them_once() { let mut s = Session::new(); s.execute("file.new", &json!({"width": 400, "height": 300})).unwrap(); for _ in 0..39 { s.execute("layer.new", &json!({})).unwrap(); } let n = s.active().unwrap().doc.layers.len() as u64; crate::script::dom::LAYER_SCANS.with(|c| c.set(0)); let out = log(&mut s, "var L = app.activeDocument.layers, n = 0; for (var i = 0; i < L.length; i++) { L[i]; n++; } vc.log(n);"); assert_eq!(out, vec![json!(n)]); let scans = crate::script::dom::LAYER_SCANS.with(std::cell::Cell::get); assert!(scans <= 2 * n, "{scans} nodes looked at for {n} layers"); } /// Reading `zOrderPosition` for every layer lists the layers once while the document is unchanged. #[test] fn z_order_position_of_each_layer_lists_them_once() { let mut s = Session::new(); s.execute("file.new", &json!({"width": 400, "height": 300})).unwrap(); for _ in 0..39 { s.execute("layer.new", &json!({})).unwrap(); } let n = s.active().unwrap().doc.layers.len() as u64; crate::script::dom::LAYER_SCANS.with(|c| c.set(0)); let out = log( &mut s, "var L = app.activeDocument.layers, k = L.length, z = []; for (var i = 0; i < k; i++) { z.push(L[i].zOrderPosition); } vc.log(z[0]); vc.log(z[k - 1]);", ); assert_eq!(out, vec![json!(n), json!(1)]); let scans = crate::script::dom::LAYER_SCANS.with(std::cell::Cell::get); assert!(scans <= 2 * n, "{scans} nodes looked at for {n} layers"); } /// `groupItems.createFromFile` on a collection whose group is gone reports the stale object before /// the member that is not available yet. #[test] fn create_from_file_on_a_stale_group_collection_reports_it_stale() { let (mut s, a, b) = doc(); s.execute("select.set", &json!({"ids": [a.0, b.0]})).unwrap(); s.execute("object.group", &json!({})).unwrap(); s.execute("select.none", &json!({})).unwrap(); let e = failure(&mut s, "var g = app.activeDocument.groupItems[0], c = g.groupItems; g.remove(); c.createFromFile(new File('x.svg'));"); assert!(e.contains("Stale page item"), "{e}"); let e = failure(&mut s, "app.activeDocument.groupItems[0].groupItems.createFromFile(new File('x.svg'));"); assert!(e.contains("GroupItems.createFromFile is not available yet"), "{e}"); } /// Each `getByName` on a layer's `pageItems` is charged to the host budget like one on the /// document's `pageItems`, also when the list was already built. #[test] fn scoped_name_searches_are_charged_each_time() { let (mut s, ..) = doc(); let calls = |s: &mut Session, list: &str, k: usize| { let source = format!("var p = {list}; p.length; for (var i = 0; i < {k}; i++) {{ try {{ p.getByName('none'); }} catch (e) {{}} }}"); run(s, &source).unwrap()["hostCalls"].as_u64().unwrap() }; let doc_cost = calls(&mut s, "app.activeDocument.pageItems", 6) - calls(&mut s, "app.activeDocument.pageItems", 1); let layer_cost = calls(&mut s, "app.activeDocument.layers[0].pageItems", 6) - calls(&mut s, "app.activeDocument.layers[0].pageItems", 1); assert_eq!(layer_cost, doc_cost); } #[test] fn layer_z_order_scans_are_charged_each_time() { let (mut s, ..) = doc(); let repeated = "var l = app.activeDocument.layers[0]; for (var i = 0; i < 512; i++) l.zOrderPosition;"; crate::script::TEST_BRIDGE_BYTES.with(|b| b.set(Some(1 << 16))); let small = run(&mut s, repeated); crate::script::TEST_BRIDGE_BYTES.with(|b| b.set(None)); small.unwrap(); for _ in 0..100 { s.execute("layer.new", &json!({})).unwrap(); } crate::script::TEST_BRIDGE_BYTES.with(|b| b.set(Some(1 << 16))); let once = run(&mut s, "var l = app.activeDocument.layers[0]; l.zOrderPosition;"); let large = run(&mut s, repeated); crate::script::TEST_BRIDGE_BYTES.with(|b| b.set(None)); once.unwrap(); let error = large.unwrap_err().to_string(); assert!(error.contains("passed more data"), "{error}"); } #[test] fn has_selected_artwork_keeps_selected_objects_in_other_layers() { let (mut s, a, b) = doc(); s.execute("layer.new", &json!({})).unwrap(); let other = rect(&mut s, 220.0, 30.0, 10.0, 10.0); s.execute("select.set", &json!({"ids": [a.0, other.0]})).unwrap(); for (operation, a_selected, b_selected) in [("vc.log(l.hasSelectedArtwork)", true, false), ("l.hasSelectedArtwork = false", false, false), ("l.hasSelectedArtwork = true", true, true)] { let source = format!("var l = app.activeDocument.layers.getByName('Layer 1'); {operation};"); run(&mut s, &source).unwrap(); assert!(s.active().unwrap().selection.objects.contains(&other), "{operation}"); let selected = &s.active().unwrap().selection.objects; assert_eq!((selected.contains(&a), selected.contains(&b)), (a_selected, b_selected), "{operation}"); } } #[test] fn layer_lists_follow_edits_between_reads() { let (mut s, _, _) = doc(); let out = log( &mut s, "var L = app.activeDocument.layers, n = L.length; for (var i = 0; i < L.length; i++) L[i];\ var made = L.add(); made.name = 'Top'; vc.log(L.length - n); vc.log(L[0].name); vc.log(L.getByName('Top').name);\ made.remove(); vc.log(L.length - n);", ); assert_eq!(out, [json!(1), json!("Top"), json!("Top"), json!(0)]); } #[test] fn group_items_remove_all_refuses_locked_groups() { let (mut s, a, b) = doc(); s.execute("select.set", &json!({"ids": [a.0]})).unwrap(); let g = s.execute("object.group", &json!({})).unwrap()["id"].as_u64().unwrap(); s.execute("object.setProps", &json!({"ids": [g], "locked": true})).unwrap(); let before = format!("{:?}", s.active().unwrap().doc.layers); assert!(failure(&mut s, "app.activeDocument.groupItems.removeAll();").contains("hidden or locked")); assert_eq!(format!("{:?}", s.active().unwrap().doc.layers), before); s.execute("object.setProps", &json!({"ids": [g], "locked": false})).unwrap(); run(&mut s, "app.activeDocument.groupItems.removeAll();").unwrap(); let d = &s.active().unwrap().doc; assert!(d.node(NodeId(g)).is_none() && d.node(a).is_none() && d.node(b).is_some()); } #[test] fn new_group_command_checks_its_parent() { let (mut s, a, _) = doc(); let layer = s.active().unwrap().doc.layer_containing(a).unwrap(); let g = s.execute("object.newGroup", &json!({"parent": layer.0, "index": 0})).unwrap()["id"].as_u64().unwrap(); let d = &s.active().unwrap().doc; assert_eq!(d.position(NodeId(g)).map(|(p, i, _)| (p, i)), Some((Some(layer), 0))); assert!(s.execute("object.newGroup", &json!({"parent": a.0})).unwrap_err().to_string().contains("not a layer or group")); assert!(s.execute("object.newGroup", &json!({"parent": 999_999})).unwrap_err().to_string().contains("no layer or group")); assert!(s.execute("object.newGroup", &json!({"parent": "x"})).unwrap_err().to_string().contains("parent must be")); assert!(s.execute("object.newGroup", &json!({"index": -1})).unwrap_err().to_string().contains("index must be")); let before = format!("{:?}", s.active().unwrap().doc.layers); assert!(s.execute("object.newGroup", &json!({"parent": null})).unwrap_err().to_string().contains("parent must be")); assert!(s.execute("object.newGroup", &json!({"index": null})).unwrap_err().to_string().contains("index must be")); assert!(s.execute("object.newGroup", &json!({"compound": null})).unwrap_err().to_string().contains("compound must be")); assert_eq!(format!("{:?}", s.active().unwrap().doc.layers), before); s.execute("layer.setProps", &json!({"id": layer.0, "locked": true})).unwrap(); assert!(s.execute("object.newGroup", &json!({"parent": layer.0})).unwrap_err().to_string().contains("hidden or locked")); } #[test] fn artboards_add_insert_rename_and_remove() { let (mut s, _, _) = doc(); let out = log( &mut s, "var A = app.activeDocument.artboards; vc.log(A.length); vc.log(A[0].artboardRect); vc.log(A.typename);\ var b = A.add([500, 300, 700, 100]); b.name = 'Two'; vc.log(A.length); vc.log(A[1].name); vc.log(b.artboardRect);\ A.insert([0, -100, 100, -200], 1); vc.log(A[1].artboardRect); vc.log(b.name); vc.log(A.getByName('Two').artboardRect[0]);\ b.artboardRect = [500, 300, 650, 150]; vc.log(b.artboardRect); b.rulerPAR = 2; vc.log(b.rulerPAR);\ b.showCenter = true; b.showCrossHairs = true; b.showSafeAreas = true; vc.log([b.showCenter, b.showCrossHairs, b.showSafeAreas]);\ b.rulerOrigin = [10, 20]; vc.log(b.rulerOrigin); vc.log(b.parent.typename);\ A.remove(0); vc.log(A.length); b.remove(); vc.log(A.length); try { b.name; } catch (e) { vc.log(String(e)); }", ); assert_eq!(out[..4], [json!(1), json!([0, 300, 400, 0]), json!("Artboards"), json!(2)]); assert_eq!(out[4..7], [json!("Two"), json!([500, 300, 700, 100]), json!([0, -100, 100, -200])]); assert_eq!(out[7..11], [json!("Two"), json!(500), json!([500, 300, 650, 150]), json!(2)]); assert_eq!(out[11..16], [json!([true, true, true]), json!([10, 20]), json!("Document"), json!(2), json!(1)]); assert!(out[16].as_str().unwrap().contains("Stale artboard"), "{out:?}"); assert_eq!(s.active().unwrap().doc.artboards.len(), 1); } #[test] fn new_artboards_get_a_name_no_other_artboard_has() { let (mut s, _, _) = doc(); let out = log( &mut s, "var A = app.activeDocument.artboards; A[0].name = 'Artboard 2';\ vc.log(A.add([500, 300, 700, 100]).name); A.insert([0, -100, 100, -200], 0); vc.log(A[0].name);\ vc.log(A.add([800, 300, 900, 100]).name);", ); assert_eq!(out, [json!("Artboard 3"), json!("Artboard 4"), json!("Artboard 5")]); } #[test] fn names_take_numbers_and_booleans() { let (mut s, _, _) = doc(); let out = log( &mut s, "var d = app.activeDocument, L = d.layers[0];\ L.name = 5; vc.log(L.name); vc.log(typeof L.name); L.name = 1.5; vc.log(L.name); L.name = true; vc.log(L.name);\ d.artboards[0].name = 7; vc.log(d.artboards[0].name); vc.log(typeof d.artboards[0].name);", ); assert_eq!(out, [json!("5"), json!("string"), json!("1.5"), json!("true"), json!("7"), json!("string")]); } #[test] fn an_empty_artboard_name_gives_the_default_name() { let (mut s, _, _) = doc(); let out = log( &mut s, "var A = app.activeDocument.artboards; A.add([500, 300, 700, 100]);\ A[0].name = 'Front'; A[1].name = 'Back'; A[0].name = ''; A[1].name = ' ';\ vc.log([A[0].name, A[1].name]);", ); assert_eq!(out, [json!(["Artboard 1", "Artboard 2"])]); } #[test] fn two_artboards_may_share_a_name() { let (mut s, _, _) = doc(); let out = log( &mut s, "var A = app.activeDocument.artboards; A.add([500, 300, 700, 100]);\ A[1].name = 'Same'; A[0].name = 'Same'; vc.log([A[0].name, A[1].name]); vc.log(A.getByName('Same').artboardRect[0]);", ); assert_eq!(out, [json!(["Same", "Same"]), json!(0)]); let names: Vec = s.active().unwrap().doc.artboards.iter().map(|a| a.name.clone()).collect(); assert_eq!(names, ["Same", "Same"]); } #[test] fn artboards_reject_bad_arguments_before_any_change() { let (mut s, _, _) = doc(); run(&mut s, "app.activeDocument.artboards.add([500, 300, 700, 100]).name = 'Two';").unwrap(); let before = format!("{:?}", s.active().unwrap().doc.artboards); let steps = undo_steps(&s); for (src, msg) in [ ("app.activeDocument.artboards.add([0, 0, 10]);", "four numbers"), ("app.activeDocument.artboards.add('x');", "four numbers"), ("app.activeDocument.artboards.add([10, 0, 0, 10]);", "right side"), ("app.activeDocument.artboards.add([0, 0, NaN, -10]);", "finite number"), ("app.activeDocument.artboards.add([0, 0, 1e300, -10]);", "outside the canvas"), ("app.activeDocument.artboards.add([0, 0, 0.5, -10]);", "at least 1 point"), ("app.activeDocument.artboards[0].artboardRect = [0, 0, 10, -0.25];", "at least 1 point"), ("app.activeDocument.artboards.insert([0, 0, 10, -10], 5);", "index must be"), ("app.activeDocument.artboards.insert([0, 0, 10, -10]);", "needs an index"), ("app.activeDocument.artboards.remove(2);", "index must be"), ("app.activeDocument.artboards.remove(-1);", "index must be"), ("app.activeDocument.artboards[0].name = null;", "must be a string"), ("app.activeDocument.artboards[0].name = new Array(5000).join('x');", "too long"), ("app.activeDocument.artboards[0].rulerPAR = 20;", "from 0.1 to 10"), ("app.activeDocument.artboards[0].showCenter = 'on';", "true or false"), ("app.activeDocument.artboards.setActiveArtboardIndex(9);", "index must be"), ("app.activeDocument.artboards.getByName('none');", "Not found"), ] { assert!(failure(&mut s, src).contains(msg), "{src}"); } assert_eq!(format!("{:?}", s.active().unwrap().doc.artboards), before); assert_eq!(undo_steps(&s), steps); run(&mut s, "app.activeDocument.artboards.remove(1);").unwrap(); assert!(failure(&mut s, "app.activeDocument.artboards[0].remove();").to_lowercase().contains("artboard")); assert_eq!(s.active().unwrap().doc.artboards.len(), 1); } #[test] fn set_active_artboard_index_is_reported_without_an_undo_step() { let (mut s, _, _) = doc(); run(&mut s, "app.activeDocument.artboards.add([500, 300, 700, 100]);").unwrap(); let steps = undo_steps(&s); let r = run(&mut s, "var A = app.activeDocument.artboards; A.setActiveArtboardIndex(1); vc.log(A.getActiveArtboardIndex());").unwrap(); assert_eq!(r["log"], json!([1])); assert_eq!(r["activeArtboards"], json!([{"document": 0, "artboard": 1}])); assert_eq!(undo_steps(&s), steps); let st = s.active().unwrap(); assert_eq!(st.active_artboard, Some(st.doc.artboards[1].id)); // Artboard coordinates follow the active artboard. let out = log(&mut s, "app.coordinateSystem = CoordinateSystem.ARTBOARDCOORDINATESYSTEM; vc.log(app.activeDocument.artboards[1].artboardRect);"); assert_eq!(out, [json!([0, 0, 200, 200])]); let r = run(&mut s, "app.activeDocument.artboards.setActiveArtboardIndex(1);").unwrap(); assert!(r.get("activeArtboards").is_none()); } /// A stale container's `pageItems.index(n)` and `groupItems.index(n)` throw the stale error for /// every number, a negative one included. #[test] fn a_stale_containers_list_throws_its_stale_error_for_any_index() { let (mut s, ..) = doc(); let folder = vectorcraft_testkit::TempDir::new("script-dom-stale-index"); let path = folder.join("saved.vectorcraft"); s.execute("document.save", &json!({"path": path.to_string_lossy()})).unwrap(); for list in ["pageItems", "groupItems"] { let source = format!("var d = app.activeDocument, p = d.layers[0].{list}; d.pageItems[0].name = 'changed'; vc.run('file.revert'); p.index(-1);"); let e = failure(&mut s, &source); assert!(e.contains("Stale page item"), "{list}: {e}"); } } /// `removeAll()` on a clip group releases its mask, so an object added to the group afterwards is /// not its clipping path; the run's one undo step puts everything back. #[test] fn remove_all_on_a_clip_group_releases_its_mask() { let (mut s, a, b) = doc(); s.execute("select.set", &json!({"ids": [a.0, b.0]})).unwrap(); let g = NodeId(s.execute("object.clippingMask.make", &json!({})).unwrap()["id"].as_u64().unwrap()); s.execute("select.none", &json!({})).unwrap(); let steps = undo_steps(&s); let out = log(&mut s, "var g = app.activeDocument.groupItems[0]; g.pageItems.removeAll(); vc.log(g.clipped); g.groupItems.add(); vc.log(g.clipped);"); assert_eq!(out, [json!(false), json!(false)]); assert!(!s.active().unwrap().doc.node(g).unwrap().clips()); assert_eq!(undo_steps(&s), steps + 1); s.execute("edit.undo", &json!({})).unwrap(); assert_eq!(undo_steps(&s), steps); let st = s.active().unwrap(); assert!(st.doc.node(g).unwrap().clips()); assert_eq!(st.doc.node(g).unwrap().children().unwrap().len(), 2); // `groupItems.removeAll()` leaves the clipping path, and the group keeps its mask. let out = log( &mut s, "var g = app.activeDocument.groupItems[0]; g.groupItems.add(); g.groupItems.removeAll(); vc.log(g.clipped); vc.log(g.pageItems.length);", ); assert_eq!(out, [json!(true), json!(2)]); } /// `remove()` on a clip group's clipping path while other art stays in the group releases the /// mask, so the art above it is not made the clipping path, and so does emptying a clipping layer /// with the document's `pageItems.removeAll()`. #[test] fn deleting_a_clipping_path_releases_its_mask() { let (mut s, a, b) = doc(); let c = rect(&mut s, 200.0, 100.0, 20.0, 20.0); let e = rect(&mut s, 250.0, 100.0, 20.0, 20.0); s.execute("select.set", &json!({"ids": [c.0, e.0]})).unwrap(); s.execute("object.compoundPath.make", &json!({})).unwrap(); s.execute("select.add", &json!({"ids": [a.0, b.0]})).unwrap(); let g = NodeId(s.execute("object.clippingMask.make", &json!({})).unwrap()["id"].as_u64().unwrap()); s.execute("select.none", &json!({})).unwrap(); let kids = |s: &Session| s.active().unwrap().doc.node(g).unwrap().children().unwrap().iter().map(|n| n.id).collect::>(); let mask = kids(&s)[0]; assert!(matches!(s.active().unwrap().doc.node(mask).unwrap().kind, NodeKind::Compound { .. })); s.execute("object.setProps", &json!({"id": mask.0, "name": "mask"})).unwrap(); let clips = |s: &Session| s.active().unwrap().doc.node(g).unwrap().clips(); assert!(clips(&s)); let out = log(&mut s, "var g = app.activeDocument.groupItems[0]; g.pageItems.getByName('mask').remove(); vc.log([g.clipped, g.pageItems.length]);"); assert_eq!(out, [json!([false, 2])]); assert!(!clips(&s)); assert_eq!(kids(&s), [a, b]); // A clipping layer emptied by `pageItems.removeAll()` releases its mask too. let (mut s, _, _) = doc(); let layer = s.active().unwrap().doc.layers[0].id; s.execute("layer.clippingMask.toggle", &json!({"id": layer.0})).unwrap(); assert!(s.active().unwrap().doc.layers[0].clips()); log(&mut s, "app.activeDocument.pageItems.removeAll();"); assert!(!s.active().unwrap().doc.layers[0].clips()); } /// Choosing the artboard that is active only by default keeps that artboard: one inserted before it /// moves it to the next index, and the report gives that index. #[test] fn choosing_the_default_active_artboard_keeps_it_when_one_is_inserted_before() { let (mut s, _, _) = doc(); assert_eq!(s.active().unwrap().active_artboard, None); let first = s.active().unwrap().doc.artboards[0].id; let r = run( &mut s, "var A = app.activeDocument.artboards; A.setActiveArtboardIndex(0); A.insert([800, 300, 900, 100], 0);\ vc.log(A.getActiveArtboardIndex());", ) .unwrap(); assert_eq!(r["log"], json!([1])); assert_eq!(r["activeArtboards"], json!([{"document": 0, "artboard": 1}])); assert_eq!(s.active().unwrap().active_artboard, Some(first)); } /// In a document the run makes, choosing artboard 0 and then inserting one before it still puts /// the document in the report. #[test] fn a_new_documents_chosen_artboard_is_reported_after_an_insert_before_it() { let (mut s, _, _) = doc(); let r = run( &mut s, "var d = app.documents.add(); var A = d.artboards; A.setActiveArtboardIndex(0); A.insert([800, 300, 900, 100], 0);\ vc.log(A.getActiveArtboardIndex());", ) .unwrap(); assert_eq!(r["log"], json!([1])); let list = r["activeArtboards"].as_array().cloned().unwrap_or_default(); assert!(list.iter().any(|e| e["artboard"] == json!(1)), "{r}"); } /// Without a chosen artboard, an artboard inserted before the default active one moves it to the /// next index, in an open document and in one the run makes, and the report gives that index. #[test] fn inserting_before_the_default_active_artboard_keeps_it() { let (mut s, _, _) = doc(); let first = s.active().unwrap().doc.artboards[0].id; let r = run(&mut s, "var A = app.activeDocument.artboards; A.insert([800, 300, 900, 100], 0); vc.log(A.getActiveArtboardIndex());").unwrap(); assert_eq!(r["log"], json!([1])); assert_eq!(r["activeArtboards"], json!([{"document": 0, "artboard": 1}])); assert_eq!(s.active().unwrap().active_artboard, Some(first)); let r = run(&mut s, "var d = app.documents.add(); var A = d.artboards; A.insert([800, 300, 900, 100], 0); vc.log(A.getActiveArtboardIndex());") .unwrap(); assert_eq!(r["log"], json!([1])); let list = r["activeArtboards"].as_array().cloned().unwrap_or_default(); assert!(list.iter().any(|e| e["artboard"] == json!(1)), "{r}"); } /// After the chosen artboard is removed, the artboard active in its place stays active when one is /// inserted before it. #[test] fn inserting_before_the_fallback_after_the_chosen_artboard_is_removed_keeps_it() { let (mut s, _, _) = doc(); let r = run( &mut s, "var A = app.activeDocument.artboards; A.add([800, 300, 900, 100]); A.setActiveArtboardIndex(1); A.remove(1);\ var was = A[A.getActiveArtboardIndex()].name; A.insert([1000, 300, 1100, 100], 0);\ vc.log(A[A.getActiveArtboardIndex()].name == was);", ) .unwrap(); assert_eq!(r["log"], json!([true]), "{r}"); } /// `artboards.insert` gives nothing back; `artboards.add` gives the new artboard. #[test] fn artboards_insert_returns_nothing() { let (mut s, _, _) = doc(); let out = log( &mut s, "var A = app.activeDocument.artboards; vc.log(typeof A.insert([0, -100, 100, -200], 0)); vc.log(A.add([500, 300, 700, 100]).typename);", ); assert_eq!(out, [json!("undefined"), json!("Artboard")]); } /// The collections' `index` is described as giving an element, and `toString` of a collection /// whose container is gone fails as `typename` does. #[test] fn collection_index_reflection_and_stale_to_string() { let (mut s, a, _) = doc(); let out = log( &mut s, "var d = app.activeDocument; vc.log([d.layers.reflect.find('index').dataType, d.groupItems.reflect.find('index').dataType,\ d.compoundPathItems.reflect.find('index').dataType, d.artboards.reflect.find('index').dataType]);", ); assert_eq!(out, [json!(["Layer", "GroupItem", "CompoundPathItem", "Artboard"])]); s.execute("select.set", &json!({"ids": [a.0]})).unwrap(); s.execute("object.group", &json!({})).unwrap(); let live = log(&mut s, "vc.log(app.activeDocument.groupItems[0].groupItems.toString()); vc.log(app.activeDocument.layers[0].layers.toString());"); assert_eq!(live, [json!("[GroupItems]"), json!("[Layers]")]); let source = "var g = app.activeDocument.groupItems[0]; var c = g.groupItems; g.remove(); c.toString();"; let error = failure(&mut s, source); assert!(error.contains("Stale page item"), "{error}"); let source = "var l = app.activeDocument.layers.add(); var c = l.layers; l.remove(); c.toString();"; let error = failure(&mut s, source); assert!(error.contains("Stale layer"), "{error}"); } /// Setting `hasSelectedArtwork = true` on a layer whose art is all selected runs no command but /// is still charged for the art it looks at. #[test] fn has_selected_artwork_charges_the_walk_when_nothing_is_added() { // `locked`: the art is locked, so nothing is selectable; otherwise it is all selected. let repeated = |n: usize, locked: bool| { let mut s = Session::new(); s.execute("file.new", &json!({"width": 400, "height": 300})).unwrap(); let ids: Vec = (0..n).map(|i| rect(&mut s, i as f64, 0.0, 1.0, 1.0).0).collect(); if locked { s.execute("object.setProps", &json!({"ids": ids, "locked": true})).unwrap(); s.execute("select.none", &json!({})).unwrap(); } else { s.execute("select.set", &json!({"ids": ids})).unwrap(); } let steps = undo_steps(&s); crate::script::TEST_BRIDGE_BYTES.with(|b| b.set(Some(1 << 13))); let r = run(&mut s, "var l = app.activeDocument.layers[0]; for (var i = 0; i < 50; i++) l.hasSelectedArtwork = true;"); crate::script::TEST_BRIDGE_BYTES.with(|b| b.set(None)); assert_eq!(undo_steps(&s), steps); r }; for locked in [false, true] { repeated(1, locked).unwrap(); let error = repeated(200, locked).unwrap_err().to_string(); assert!(error.contains("passed more data"), "{locked}: {error}"); } // `false` on an empty layer is charged for the selection held in another layer, which stays. let deselect = |n: usize| { let mut s = Session::new(); s.execute("file.new", &json!({"width": 400, "height": 300})).unwrap(); let ids: Vec = (0..n).map(|i| rect(&mut s, i as f64, 0.0, 1.0, 1.0).0).collect(); s.execute("layer.new", &json!({})).unwrap(); s.execute("select.set", &json!({"ids": ids})).unwrap(); crate::script::TEST_BRIDGE_BYTES.with(|b| b.set(Some(1 << 13))); let r = run(&mut s, "var l = app.activeDocument.layers[0]; for (var i = 0; i < 50; i++) l.hasSelectedArtwork = false;"); crate::script::TEST_BRIDGE_BYTES.with(|b| b.set(None)); assert_eq!(s.active().unwrap().selection.objects.len(), n); r }; deselect(1).unwrap(); let error = deselect(200).unwrap_err().to_string(); assert!(error.contains("passed more data"), "{error}"); } /// Reading `hasSelectedArtwork` is charged for every selected object it looks at. #[test] fn has_selected_artwork_charges_the_selection_it_reads() { let repeated = |n: usize| { let mut s = Session::new(); s.execute("file.new", &json!({"width": 400, "height": 300})).unwrap(); let ids: Vec = (0..n).map(|i| rect(&mut s, i as f64, 0.0, 1.0, 1.0).0).collect(); s.execute("layer.new", &json!({})).unwrap(); s.execute("select.set", &json!({"ids": ids})).unwrap(); crate::script::TEST_BRIDGE_BYTES.with(|b| b.set(Some(1 << 13))); let r = run(&mut s, "var l = app.activeDocument.layers[0]; for (var i = 0; i < 50; i++) l.hasSelectedArtwork;"); crate::script::TEST_BRIDGE_BYTES.with(|b| b.set(None)); r }; repeated(1).unwrap(); let error = repeated(200).unwrap_err().to_string(); assert!(error.contains("passed more data"), "{error}"); } /// While a group is isolated, `hasSelectedArtwork = true` selects the art inside the group, as /// Select All does. #[test] fn has_selected_artwork_selects_inside_an_isolated_group() { let (mut s, a, b) = doc(); s.execute("select.set", &json!({"ids": [a.0, b.0]})).unwrap(); let g = s.execute("object.group", &json!({})).unwrap()["id"].as_u64().unwrap(); s.execute("object.isolate", &json!({"id": g})).unwrap(); s.execute("select.none", &json!({})).unwrap(); let out = log(&mut s, "var l = app.activeDocument.layers[0]; l.hasSelectedArtwork = true; vc.log(l.hasSelectedArtwork);"); assert_eq!(out, [json!(true)]); let sel = &s.active().unwrap().selection.objects; assert!(sel.contains(&a) && sel.contains(&b), "{sel:?}"); } /// While Lock Guides is on, a path made into a guide is not counted as art to add, so a selection /// at the limit does not refuse a layer that holds only such a path. #[test] fn has_selected_artwork_skips_locked_guide_paths() { let mut s = Session::new(); s.execute("file.new", &json!({"width": 400, "height": 300})).unwrap(); let a = rect(&mut s, 10.0, 20.0, 30.0, 40.0); s.execute("select.set", &json!({"ids": [a.0]})).unwrap(); s.execute("view.guides.make", &json!({})).unwrap(); s.execute("view.guides.lock", &json!({"locked": true})).unwrap(); s.execute("layer.new", &json!({})).unwrap(); let other = rect(&mut s, 220.0, 30.0, 10.0, 10.0); s.active_mut().unwrap().selection.objects = vec![other; 10_000]; run(&mut s, "app.activeDocument.layers.getByName('Layer 1').hasSelectedArtwork = true;").unwrap_or_else(|e| panic!("{e}")); assert_eq!(s.active().unwrap().selection.objects.len(), 10_000); s.execute("select.none", &json!({})).unwrap(); let steps = undo_steps(&s); run(&mut s, "app.activeDocument.layers.getByName('Layer 1').hasSelectedArtwork = true;").unwrap(); assert_eq!(undo_steps(&s), steps); assert!(s.active().unwrap().selection.objects.is_empty()); } #[test] fn the_report_gives_the_active_artboard_the_run_leaves() { let (mut s, _, _) = doc(); run(&mut s, "var A = app.activeDocument.artboards; A.add([500, 300, 700, 100]); A.add([800, 300, 900, 100]);").unwrap(); // The chosen artboard is deleted later in the run: the report gives the one active at the end. let r = run(&mut s, "var A = app.activeDocument.artboards; A.setActiveArtboardIndex(2); A.remove(2); vc.log(A.getActiveArtboardIndex());").unwrap(); let active = r["log"][0].clone(); assert!(active.as_u64().is_some_and(|i| i < 2), "{r}"); assert_eq!(r["activeArtboards"], json!([{"document": 0, "artboard": active}])); } #[test] fn the_report_gives_the_active_artboards_new_index_after_an_insert_or_remove() { let (mut s, _, _) = doc(); run(&mut s, "var A = app.activeDocument.artboards; A.add([500, 300, 700, 100]); A.setActiveArtboardIndex(1);").unwrap(); // An artboard inserted or removed before the active one moves it to another index. let r = run(&mut s, "var A = app.activeDocument.artboards; A.insert([800, 300, 900, 100], 0); vc.log(A.getActiveArtboardIndex());").unwrap(); assert_eq!(r["log"], json!([2])); assert_eq!(r["activeArtboards"], json!([{"document": 0, "artboard": 2}])); let r = run(&mut s, "var A = app.activeDocument.artboards; A.remove(0); vc.log(A.getActiveArtboardIndex());").unwrap(); assert_eq!(r["log"], json!([1])); assert_eq!(r["activeArtboards"], json!([{"document": 0, "artboard": 1}])); // One added after it leaves its index as it was. let r = run(&mut s, "app.activeDocument.artboards.add([1000, 300, 1100, 100]);").unwrap(); assert!(r.get("activeArtboards").is_none(), "{r}"); } #[test] fn an_artboard_rectangle_stays_inside_the_canvas() { let (mut s, _, _) = doc(); let before = format!("{:?}", s.active().unwrap().doc.artboards); let steps = undo_steps(&s); // Each number is within the coordinate range, but the right edge is past the canvas. for src in [ "app.activeDocument.artboards[0].artboardRect = [3999950, 0, 4000100, -100];", "app.activeDocument.artboards.add([-4000100, 0, -3999950, -100]);", "app.activeDocument.artboards.insert([0, 4000600, 100, 4000500], 0);", ] { assert!(failure(&mut s, src).contains("outside the canvas"), "{src}"); } assert_eq!(format!("{:?}", s.active().unwrap().doc.artboards), before); assert_eq!(undo_steps(&s), steps); } #[test] fn has_selected_artwork_selects_and_deselects_a_layers_art() { let (mut s, a, b) = doc(); s.execute("select.none", &json!({})).unwrap(); let out = log( &mut s, "var l = app.activeDocument.layers[0]; vc.log(l.hasSelectedArtwork); l.hasSelectedArtwork = true;\ vc.log(app.activeDocument.selection.length); vc.log(l.hasSelectedArtwork); vc.log(l.isIsolated);", ); assert_eq!(out, [json!(false), json!(2), json!(true), json!(false)]); let sel = &s.active().unwrap().selection.objects; assert!(sel.contains(&a) && sel.contains(&b)); run(&mut s, "app.activeDocument.layers[0].hasSelectedArtwork = false;").unwrap(); assert!(s.active().unwrap().selection.objects.is_empty()); assert!(failure(&mut s, "app.activeDocument.layers[0].hasSelectedArtwork = 0;").contains("true or false")); } #[test] fn has_selected_artwork_keeps_the_rest_of_the_selection() { let (mut s, a, b) = doc(); s.execute("guide.add", &json!({"vertical": true, "pos": 120})).unwrap(); s.execute("guide.select", &json!({"indexes": [0]})).unwrap(); // An empty layer has nothing to deselect or add: the selected guide stays. let steps = undo_steps(&s); run(&mut s, "var l = app.activeDocument.layers.add(); l.hasSelectedArtwork = false; l.hasSelectedArtwork = true;").unwrap(); assert_eq!(s.active().unwrap().selection.guides.len(), 1); assert_eq!(undo_steps(&s), steps + 1); // Deselecting a layer's art leaves the guide selected, and so does adding it. s.execute("select.set", &json!({"ids": [a.0]})).unwrap(); s.execute("guide.select", &json!({"indexes": [0], "toggle": true})).unwrap(); run(&mut s, "app.activeDocument.layers.getByName('Layer 1').hasSelectedArtwork = false;").unwrap(); let sel = &s.active().unwrap().selection; assert_eq!((sel.objects.len(), sel.guides.len()), (0, 1)); run(&mut s, "app.activeDocument.layers.getByName('Layer 1').hasSelectedArtwork = true;").unwrap(); let sel = &s.active().unwrap().selection; assert!(sel.objects.contains(&a) && sel.objects.contains(&b)); assert_eq!(sel.guides.len(), 1); } /// The selection limit counts only the art an assignment would add: a layer with nothing to add /// changes nothing beside a selection already at the limit, and one with art to add throws. #[test] fn has_selected_artwork_checks_the_limit_only_for_art_it_adds() { let (mut s, a, _) = doc(); run(&mut s, "app.activeDocument.layers.add();").unwrap(); // A selection of 10,001 entries, as the limit counts them. s.active_mut().unwrap().selection.objects = vec![a; 10_001]; run(&mut s, "var l = app.activeDocument.layers[0]; l.hasSelectedArtwork = true;").unwrap_or_else(|e| panic!("{e}")); assert_eq!(s.active().unwrap().selection.objects.len(), 10_001); s.active_mut().unwrap().selection.objects = vec![a; 10_000]; let e = failure(&mut s, "app.activeDocument.layers.getByName('Layer 1').hasSelectedArtwork = true;"); assert!(e.contains("at most 10000"), "{e}"); } #[test] fn has_selected_artwork_on_an_empty_layer_passes_a_full_selection() { let (mut s, a, _) = doc(); let n = crate::script::dom::MAX_LISTED; // Each Duplicate of everything doubles the objects. s.execute("select.all", &json!({})).unwrap(); for _ in 0..n.ilog2() { s.execute("edit.duplicate", &json!({})).unwrap(); s.execute("select.all", &json!({})).unwrap(); } assert!(s.active().unwrap().selection.objects.len() > n); // The empty layer adds nothing, so the selection's size does not matter. run(&mut s, "app.activeDocument.layers.add().hasSelectedArtwork = true;").unwrap(); // A layer with an unselected object would take the selection past the limit. s.execute("select.toggle", &json!({"ids": [a.0]})).unwrap(); assert!(s.active().unwrap().selection.objects.len() > n); let steps = undo_steps(&s); assert!(failure(&mut s, "app.activeDocument.layers.getByName('Layer 1').hasSelectedArtwork = true;").contains("at most")); assert!(!s.active().unwrap().selection.objects.contains(&a)); assert_eq!(undo_steps(&s), steps); } #[test] fn artboards_insert_gives_undefined_and_index_reflects_an_artboard() { let (mut s, _, _) = doc(); let out = log( &mut s, "var A = app.activeDocument.artboards; vc.log(typeof A.insert([500, 300, 700, 100], 0));\ function info(o, n) { var m = o.reflect.methods; for (var i = 0; i < m.length; i++) if (m[i].name == n) return m[i].dataType + ':' + m[i].arguments[0].dataType; }\ function prop(o, n) { var m = o.reflect.properties; for (var i = 0; i < m.length; i++) if (m[i].name == n) return m[i].dataType; }\ vc.log(info(A, 'index')); vc.log(prop(app.activeDocument.pageItems, 'parent'));", ); assert_eq!(out, [json!("undefined"), json!("Artboard:*"), json!("*")]); } #[test] fn artboards_stop_at_one_hundred() { let (mut s, _, _) = doc(); run(&mut s, "var A = app.activeDocument.artboards; for (var i = 1; i < 100; i++) A.add([i * 10, -400, i * 10 + 5, -405]);").unwrap(); assert_eq!(s.active().unwrap().doc.artboards.len(), 100); let steps = undo_steps(&s); assert!(failure(&mut s, "app.activeDocument.artboards.add([0, -500, 10, -510]);").contains("at most 100 artboards")); assert!(failure(&mut s, "app.activeDocument.artboards.insert([0, -500, 10, -510], 0);").contains("at most 100 artboards")); assert_eq!(s.active().unwrap().doc.artboards.len(), 100); assert_eq!(undo_steps(&s), steps); } #[test] fn every_page_item_has_its_layer_and_members_check_this() { let (mut s, _, _) = doc(); let out = log( &mut s, "var d = app.activeDocument, p = d.pageItems[0], n = d.layers.add(); n.name = 'Up'; p.move(n);\ vc.log(p.layer.name); vc.log(d.pageItems[d.pageItems.length - 1].layer.name); vc.log(p.layer.layers.typename);\ vc.log(n.pageItems.typename); vc.log(n.groupItems.typename);", ); assert_eq!(out, [json!("Up"), json!("Layer 1"), json!("Layers"), json!("PageItems"), json!("GroupItems")]); for src in [ "Object.getOwnPropertyDescriptor(Layers.prototype, 'typename').get.call({});", "Object.getOwnPropertyDescriptor(GroupItems.prototype, 'typename').get.call({});", "Object.getOwnPropertyDescriptor(Artboard.prototype, 'typename').get.call({});", "Object.getOwnPropertyDescriptor(Artboards.prototype, 'typename').get.call({});", "Object.getOwnPropertyDescriptor(Layer.prototype, 'pathItems').get.call({});", ] { assert!(failure(&mut s, src).contains("this is not a"), "{src}"); } let stale = "var L = app.activeDocument.layers, n = L.add(), c = n.layers, g = n.groupItems; n.remove();"; for read in ["c.typename", "g.typename", "n.pathItems"] { assert!(failure(&mut s, &format!("{stale} {read};")).contains("Stale"), "{read}"); } } /// A script that names each visible selected object after its place in `app.selection`, testing /// the members it uses with `hasOwnProperty` and `propertyIsEnumerable` first. A member that is not /// available yet, such as `isIsolated`, is read only when `hasOwnProperty` reports it, so it is /// never read. const NAME_PICKED: &str = "\ var picked = app.selection, k = picked.length, named = 0;\ if (!k) alert('Select something first.');\ while (k--) {\ var obj = picked[k];\ var usable = obj.propertyIsEnumerable('name') && obj.hasOwnProperty('hidden') && !obj.hidden;\ if (obj.hasOwnProperty('isIsolated') && obj.isIsolated) usable = false;\ if (usable) { obj.name = 'picked ' + k; named += 1; }\ }\ if (picked.length && named < picked.length) alert('Some objects were skipped.');"; #[test] fn a_script_that_checks_has_own_property_changes_every_kind_of_selected_object() { let mut s = Session::new(); s.execute("file.new", &json!({"width": 600, "height": 400})).unwrap(); let path = rect(&mut s, 10.0, 10.0, 20.0, 20.0); let (g1, g2) = (rect(&mut s, 50.0, 10.0, 20.0, 20.0), rect(&mut s, 80.0, 10.0, 20.0, 20.0)); s.execute("select.set", &json!({"ids": [g1.0, g2.0]})).unwrap(); let group = NodeId(s.execute("object.group", &json!({})).unwrap()["id"].as_u64().unwrap()); let text = NodeId(s.execute("text.create", &json!({"x": 10, "y": 100, "text": "hi"})).unwrap()["id"].as_u64().unwrap()); let (c1, c2) = (rect(&mut s, 150.0, 10.0, 20.0, 20.0), rect(&mut s, 155.0, 15.0, 20.0, 20.0)); s.execute("select.set", &json!({"ids": [c1.0, c2.0]})).unwrap(); s.execute("object.compoundPath.make", &json!({})).unwrap(); let compound = s.active().unwrap().selection.objects[0]; let objects = [path, group, text, compound]; s.execute("select.set", &json!({"ids": objects.iter().map(|n| n.0).collect::>()})).unwrap(); let names = |s: &Session| -> Vec> { let d = &s.active().unwrap().doc; objects.iter().map(|n| d.node(*n).unwrap().name.clone()).collect() }; let before = names(&s); let steps = undo_steps(&s); // The run shows no alert: without an answer for one, it would stop there. let r = run(&mut s, NAME_PICKED).unwrap_or_else(|e| panic!("{e}")); assert_eq!(r["dialogs"], json!([]), "{r}"); let after = names(&s); assert!(after.iter().all(|n| n.as_deref().is_some_and(|n| n.starts_with("picked "))), "every kind was named: {after:?}"); assert_eq!(after.iter().collect::>().len(), objects.len(), "each got its own name: {after:?}"); assert_eq!(undo_steps(&s), steps + 1, "the run is one undo step"); s.execute("edit.undo", &json!({})).unwrap(); assert_eq!(names(&s), before); // Opacity reads and writes on every kind too: each object goes to its own value, read back from the script. s.execute("select.set", &json!({"ids": objects.iter().map(|n| n.0).collect::>()})).unwrap(); let opacities = |s: &Session| -> Vec { let d = &s.active().unwrap().doc; objects.iter().map(|n| d.node(*n).unwrap().opacity).collect() }; let before = opacities(&s); let steps = undo_steps(&s); let out = log( &mut s, "var sel = app.activeDocument.selection, read = [];\ for (var i = 0; i < sel.length; i++) { sel[i].opacity = 20 + 10 * i; read.push(sel[i].opacity); }\ vc.log(read);", ); assert_eq!(out, vec![json!([20, 30, 40, 50])]); let after = opacities(&s); assert!(after.iter().zip(&before).all(|(a, b)| a < b), "every kind got a lower opacity: {before:?} -> {after:?}"); assert_eq!(undo_steps(&s), steps + 1, "the run is one undo step"); s.execute("edit.undo", &json!({})).unwrap(); assert_eq!(opacities(&s), before); } #[test] fn the_selection_is_an_empty_list_when_nothing_is_selected() { let (mut s, _, _) = doc(); s.execute("select.set", &json!({"ids": []})).unwrap(); let out = log(&mut s, "var d = app.activeDocument; vc.log([d.selection instanceof Array, d.selection.length, app.selection.length]);"); assert_eq!(out, vec![json!([true, 0, 0])]); let steps = undo_steps(&s); let r = s.execute("script.run", &json!({"source": NAME_PICKED, "name": "t.js", "answers": [{"kind": "alert", "value": "ok"}]})).unwrap(); assert_eq!(r["dialogs"][0]["message"], json!("Select something first."), "{r}"); assert_eq!(undo_steps(&s), steps); } #[test] fn page_items_report_their_available_members_as_their_own() { let (mut s, _, _) = doc(); let out = log( &mut s, "var p = app.activeDocument.pageItems[0], d = app.activeDocument, keys = [];\ for (var k in p) keys.push(k);\ vc.log([p.hasOwnProperty('opacity'), p.hasOwnProperty('typename'), p.hasOwnProperty('duplicate'),\ Object.prototype.hasOwnProperty.call(p, 'name'), p.propertyIsEnumerable('opacity'), d.hasOwnProperty('selection'),\ app.hasOwnProperty('activeDocument')]);\ vc.log([p.hasOwnProperty('isIsolated'), p.hasOwnProperty('bringInPerspective'), d.hasOwnProperty('swatches'),\ p.hasOwnProperty('nosuch'), p.hasOwnProperty('hasOwnProperty'), p.hasOwnProperty('constructor')]);\ vc.log([keys.indexOf('opacity') >= 0, keys.indexOf('typename') >= 0, keys.indexOf('duplicate'), keys.indexOf('isIsolated')]);", ); assert_eq!(out[0], json!([true, true, true, true, true, true, true])); // Members that are not available yet, unknown names and inherited ones are not the object's own. assert_eq!(out[1], json!([false, false, false, false, false, false])); // `for`-`in` lists the properties, not the methods or the members that are not available yet. assert_eq!(out[2], json!([true, true, -1, -1])); } /// A name a page item class's prototype has, such as `constructor`, is not an object's own, and a /// script cannot put a name on the prototype. #[test] fn a_property_added_to_a_class_prototype_is_not_an_objects_own() { let (mut s, _, _) = doc(); let out = log( &mut s, "var p = app.activeDocument.pageItems[0]; PathItem.prototype.foo = 1; PageItem.prototype.bar = 2;\ vc.log([PathItem.prototype.foo === undefined, PageItem.prototype.bar === undefined, 'foo' in p]);\ vc.log([p.hasOwnProperty('foo'), p.hasOwnProperty('bar'), p.propertyIsEnumerable('foo'), p.hasOwnProperty('constructor')]);", ); assert_eq!(out, vec![json!([true, true, false]), json!([false, false, false, false])]); } /// Reading every property `for`-`in` lists calls each getter, and each call counts against the run: /// a loop of such reads ends at `maxFuel` and at `script.stop`. #[test] fn reading_every_listed_property_is_charged_and_can_be_stopped() { let (mut s, _, _) = doc(); let mut calls = |body: &str| { let source = format!("var p = app.activeDocument.pageItems[0], n = 0; for (var k in p) {{ {body} n++; }} vc.log(n);"); let r = run(&mut s, &source).unwrap_or_else(|e| panic!("{e}")); (r["hostCalls"].as_u64().unwrap(), r["log"][0].as_u64().unwrap()) }; let (listed, n) = calls(""); let (read, m) = calls("p[k];"); assert_eq!(n, m); assert!(n > 10, "{n} properties"); assert!(read >= listed + n, "{listed} host calls to list, {read} to read {n} properties"); let spin = "var p = app.activeDocument.pageItems[0]; while (true) for (var k in p) p[k];"; let e = s.execute("script.run", &json!({"source": spin, "name": "t.js", "maxFuel": 200_000})).unwrap_err(); assert!(e.to_string().ends_with("(limit)"), "{e}"); s.script_frames = true; assert_eq!(run(&mut s, spin).unwrap()["running"], true); assert!(s.script_pump().is_none()); assert_eq!(s.execute("script.stop", &json!({})).unwrap()["stopped"], true); assert!(s.script_pump().unwrap().is_err()); assert!(!s.script_running()); } /// `pageItems` lists art topmost first, and the selection lists the selected objects in that /// order whatever order they were selected or made in. #[test] fn page_items_and_the_selection_list_the_topmost_object_first() { let (mut s, a, b) = doc(); s.execute("object.setProps", &json!({"ids": [a.0], "name": "bottom"})).unwrap(); s.execute("object.setProps", &json!({"ids": [b.0], "name": "top"})).unwrap(); s.execute("select.set", &json!({"ids": [a.0]})).unwrap(); s.execute("select.add", &json!({"ids": [b.0]})).unwrap(); assert_eq!(s.active().unwrap().selection.objects, vec![a, b], "picked bottom first"); let out = log( &mut s, "var d = app.activeDocument; vc.log([d.pageItems[0].name, d.pageItems[1].name, d.selection[0].name, d.selection[1].name]);\ d.selection = [d.pageItems[1], d.pageItems[0]]; vc.log(d.selection[0].name);", ); assert_eq!(out, vec![json!(["top", "bottom", "top", "bottom"]), json!("top")]); // An object made last and sent to the back comes after the others it is selected with. let c = rect(&mut s, 200.0, 200.0, 20.0, 20.0); s.execute("object.setProps", &json!({"ids": [c.0], "name": "last"})).unwrap(); let out = log( &mut s, "var d = app.activeDocument, c = d.pageItems.getByName('last'); c.zOrder(ZOrderMethod.SENDTOBACK);\ d.selection = null; c.selected = true; d.pageItems.getByName('top').selected = true;\ vc.log([d.selection[0].name, d.selection[1].name, d.pageItems[2].name]);", ); assert_eq!(out, vec![json!(["top", "last", "last"])]); } /// The selection lists a group before the objects in it, in `pageItems`' order, and reading it /// costs as much in a large document as in a small one. #[test] fn the_selection_follows_page_items_and_costs_the_same_in_a_large_document() { let (mut s, a, b) = doc(); let c = rect(&mut s, 200.0, 200.0, 20.0, 20.0); s.execute("select.set", &json!({"ids": [a.0, b.0]})).unwrap(); let group = s.execute("object.group", &json!({})).unwrap()["id"].as_u64().unwrap(); s.execute("select.set", &json!({"ids": [a.0, c.0, group]})).unwrap(); let source = "var d = app.activeDocument, want = [], got = [];\ for (var i = 0; i < d.pageItems.length; i++) if (d.pageItems[i].selected) want.push(d.pageItems[i].typename + i);\ for (var i = 0; i < d.pageItems.length; i++) for (var j = 0; j < d.selection.length; j++)\ if (d.selection[j] === d.pageItems[i]) got[j] = d.pageItems[i].typename + i;\ vc.log([want, got]);"; let out = log(&mut s, source); assert_eq!(out[0][0], out[0][1], "the selection is in pageItems' order"); assert_eq!(out[0][0].as_array().map(Vec::len), Some(3)); // Each pass edits the document, so nothing kept from the last read can be reused. let edits = "var d = app.activeDocument; for (var i = 0; i < 50; i++) d.selection[0].opacity = 50 + i % 2;"; // The script's fuel, and the host's calls and the bytes they count, which hold the work the // host does for them: 64 KiB is less than one list of the large document's objects. let cost = |s: &mut Session| { crate::script::TEST_BRIDGE_BYTES.with(|b| b.set(Some(1 << 16))); let r = run(s, edits); crate::script::TEST_BRIDGE_BYTES.with(|b| b.set(None)); let r = r.unwrap_or_else(|e| panic!("the reads went over: {e}")); (r["fuelUsed"].as_u64().unwrap(), r["hostCalls"].as_u64().unwrap()) }; s.execute("select.set", &json!({"ids": [c.0]})).unwrap(); let small = cost(&mut s); // 3 objects doubled 11 times: 6,144. s.execute("select.all", &json!({})).unwrap(); for _ in 0..11 { s.execute("edit.duplicate", &json!({})).unwrap(); s.execute("select.all", &json!({})).unwrap(); } s.execute("select.set", &json!({"ids": [c.0]})).unwrap(); assert_eq!(small, cost(&mut s)); } /// Building a collection's list again after an edit counts the objects it lists against the /// run's allowance, so a loop that edits and reads `pageItems`, `pathItems` or `textFrames` in a /// large document ends with the limit instead of walking the document each time for nothing. #[test] fn rebuilding_a_collection_after_an_edit_counts_against_the_run() { let (mut s, a, _) = doc(); let source = |list: &str| { format!("var d = app.activeDocument, it = d.pageItems[0]; for (var i = 0; i < 40; i++) {{ it.opacity = 50 + i % 2; d.{list}.length; }}") }; let lists = ["pageItems", "pathItems", "textFrames"]; crate::script::TEST_BRIDGE_BYTES.with(|b| b.set(Some(1 << 20))); let small: Vec = lists.iter().map(|l| run(&mut s, &source(l)).is_ok()).collect(); crate::script::TEST_BRIDGE_BYTES.with(|b| b.set(None)); assert_eq!(small, vec![true; 3], "a small document stays under 1 MiB"); // 2 objects doubled 11 times: 4,096, a list of 64 KiB each time it is built again. s.execute("select.set", &json!({"ids": [a.0]})).unwrap(); s.execute("select.all", &json!({})).unwrap(); for _ in 0..11 { s.execute("edit.duplicate", &json!({})).unwrap(); s.execute("select.all", &json!({})).unwrap(); } // Nothing selected, so putting the selection back after each edit passes no list of ids. s.execute("select.none", &json!({})).unwrap(); for list in lists { crate::script::TEST_BRIDGE_BYTES.with(|b| b.set(Some(1 << 20))); let r = run(&mut s, &source(list)); crate::script::TEST_BRIDGE_BYTES.with(|b| b.set(None)); let e = r.map(|r| panic!("{list}: 40 lists of 4,096 objects stayed under 1 MiB: {r}")).unwrap_err().to_string(); assert!(e.contains("passed more data to and from commands"), "{list}: {e}"); } } /// A collection read with a name gives what `getByName` gives, and throws as it does for no match. #[test] fn a_collection_read_with_a_name_gives_the_named_object() { let (mut s, a, _) = doc(); s.execute("object.setProps", &json!({"ids": [a.0], "name": "n"})).unwrap(); let out = log( &mut s, "var d = app.activeDocument, p = d.pageItems, m = [];\ vc.log([p['n'] === p.getByName('n'), p['n'].name, 'n' in p, 'nosuch' in p, p['length'] === p.length]);\ vc.log([app.documents[d.name] === d, d.name in app.documents, 'nosuch' in app.documents]);\ try { p['nosuch']; } catch (e) { m.push(e.message); }\ try { app.documents.nosuch; } catch (e) { m.push(e.message); }\ vc.log(m);", ); assert_eq!(out, vec![json!([true, "n", true, false, true]), json!([true, true, false]), json!(["Not found", "Not found"])]); // A name the collection inherits is not looked up as an object's name when it is assigned. let out = log(&mut s, "Object.prototype.tag = 1; var p = app.activeDocument.pageItems; p.tag = 2; vc.log(p.tag);"); assert_eq!(out, vec![json!(1)]); } /// A collection read with a name that is the text of a number but not an array index, such as `-1` /// or `1.5`, gives the object of that name, and `undefined` when nothing has it; array indices stay /// positions, and `index` still takes a number as a position. #[test] fn a_collection_read_with_a_number_name_that_is_not_an_index_gives_the_named_object() { let (mut s, a, b) = doc(); s.execute("object.setProps", &json!({"ids": [a.0], "name": "-1"})).unwrap(); s.execute("object.setProps", &json!({"ids": [b.0], "name": "NaN"})).unwrap(); let out = log( &mut s, "var p = app.activeDocument.pageItems, m = [];\ vc.log([p['-1'] === p.getByName('-1'), '-1' in p, p[-1] === p.getByName('-1'), p.NaN === p.getByName('NaN'), 'NaN' in p]);\ vc.log([p['1.5'] === undefined, '1.5' in p, p[0].name, p['1'].name]);\ try { p.index(-1); } catch (e) { m.push(e.message); }\ var t = new DocumentPreset(); t.title = '1.5'; var d = app.documents.addDocument('Print', t);\ var ds = app.documents; vc.log([ds['1.5'] === d, '1.5' in ds, ds[1.5] === d, ds['-1'] === undefined, '-1' in ds]);\ try { ds.index(1.5); } catch (e) { m.push(e.message); }\ vc.log(m);", ); assert_eq!( out, vec![ json!([true, true, true, true, true]), json!([true, false, "NaN", "-1"]), json!([true, true, true, true, false]), json!(["Not found", "Not found"]), ] ); } /// `opacity`, `blendingMode`, `hidden`, `locked` and the position and size setters on an object in a hidden or /// locked layer throw and the run changes nothing, while its name and note still change; an object /// that is hidden or locked itself takes them. #[test] fn setters_refuse_art_in_a_hidden_or_locked_layer() { let (mut s, a, b) = doc(); let layer = NodeId(s.execute("layer.new", &json!({})).unwrap()["id"].as_u64().unwrap()); let c = rect(&mut s, 200.0, 200.0, 20.0, 20.0); rect(&mut s, 300.0, 200.0, 20.0, 20.0); for (id, name) in [(a, "locked"), (b, "free"), (c, "closed")] { s.execute("object.setProps", &json!({"ids": [id.0], "name": name})).unwrap(); } for (on, off) in [ (json!({"ids": [layer.0], "visible": false}), json!({"ids": [layer.0], "visible": true})), (json!({"ids": [layer.0], "locked": true}), json!({"ids": [layer.0], "locked": false})), ] { s.execute("object.setProps", &on).unwrap(); // `hidden` and `locked` are refused even when the value is the one the object has. for set in [ "opacity = 50", "blendingMode = BlendModes.MULTIPLY", "hidden = true", "hidden = false", "locked = true", "locked = false", "left = 5", "top = 5", "position = [5, 5]", "width = 10", "height = 10", "zOrder(ZOrderMethod.BRINGTOFRONT)", ] { let (before, steps) = (format!("{:?}", s.active().unwrap().doc.layers), undo_steps(&s)); // The change made before the refusal is put back with the rest of the run. let e = failure(&mut s, &format!("var p = app.activeDocument.pageItems; p.getByName('free').opacity = 20; p.getByName('closed').{set};")); assert!(e.contains(&format!("Cannot edit PathItem {}", c.0)), "{on} {set}: {e}"); assert_eq!(format!("{:?}", s.active().unwrap().doc.layers), before, "{on} {set}"); assert_eq!(undo_steps(&s), steps, "{on} {set}"); } let out = log( &mut s, "var c = app.activeDocument.pageItems.getByName('closed'); c.note = 'n'; c.name = 'renamed'; c.opacity = c.opacity;\ c.blendingMode = c.blendingMode; vc.log([c.name, c.note, c.opacity, c.blendingMode]); c.name = 'closed'; c.note = '';", ); assert_eq!(out, vec![json!(["renamed", "n", 100, "BlendModes.NORMAL"])], "{on}"); s.execute("object.setProps", &off).unwrap(); } s.execute("object.setProps", &json!({"ids": [a.0], "locked": true})).unwrap(); s.execute("object.setProps", &json!({"ids": [b.0], "visible": false})).unwrap(); let out = log( &mut s, "var p = app.activeDocument.pageItems; p.getByName('locked').opacity = 50; p.getByName('free').opacity = 40;\ p.getByName('locked').blendingMode = BlendModes.SCREEN; p.getByName('free').blendingMode = BlendModes.DARKEN;\ vc.log([p.getByName('locked').opacity, p.getByName('free').opacity, p.getByName('locked').blendingMode,\ p.getByName('free').blendingMode]);", ); assert_eq!(out, vec![json!([50, 40, "BlendModes.SCREEN", "BlendModes.DARKEN"])]); } /// `name` takes a number or a Boolean as the text `String` gives it, and nothing else. #[test] fn a_name_given_as_a_number_or_a_boolean_becomes_its_text() { let (mut s, a, _) = doc(); let out = log( &mut s, "var i = app.activeDocument.pageItems[1]; i.name = 5; vc.log([typeof i.name, i.name]); i.name = 2.5e-7; vc.log(i.name);\ i.name = true; vc.log(i.name);", ); assert_eq!(out, vec![json!(["string", "5"]), json!("2.5e-7"), json!("true")]); assert_eq!(s.active().unwrap().doc.node(a).unwrap().name.as_deref(), Some("true")); for value in ["null", "undefined", "{}"] { assert!(failure(&mut s, &format!("app.activeDocument.pageItems[1].name = {value};")).contains("name must be a string"), "{value}"); } } /// `String` of an object of the object model names its class, and its name when it has one. #[test] fn objects_of_the_object_model_print_their_class_and_name() { let (mut s, a, _) = doc(); s.execute("object.setProps", &json!({"ids": [a.0], "name": "Logo"})).unwrap(); let out = log( &mut s, "var d = app.activeDocument, p = d.pageItems;\ vc.log([String(p[0]), String(p[1]), '' + p[1], p[1].toString(), String(p[1].parent) === '[Layer ' + p[1].parent.name + ']']);\ vc.log([String(d) === '[Document ' + d.name + ']', String(app), String(p), String(app.documents)]);", ); assert_eq!(out[0], json!(["[PathItem ]", "[PathItem Logo]", "[PathItem Logo]", "[PathItem Logo]", true])); assert_eq!(out[1], json!([true, "[Application]", "[PageItems]", "[Documents]"])); } /// `activeDocument`, `documents` and `selection` without `app.` read the application's members, and /// assigning one assigns the member: `selection` selects, and `documents` keeps its value outside /// strict mode and throws a `TypeError` in strict mode code or with `#strict on`. #[test] fn the_application_members_can_be_named_without_app() { let (mut s, a, b) = doc(); s.execute("select.set", &json!({"ids": [a.0]})).unwrap(); let out = log( &mut s, "vc.log([activeDocument === app.activeDocument, documents === app.documents, documents.length,\ selection.length, selection[0] === app.activeDocument.pageItems[1], typeof activeDocument]);", ); assert_eq!(out, vec![json!([true, true, 1, 1, true, "object"])]); let out = log( &mut s, "var d = app.activeDocument; selection = null; vc.log(d.selection.length);\ selection = [d.pageItems[0], d.pageItems[1]]; vc.log(d.selection.length);\ app.selection = d.pageItems[0]; vc.log([d.selection.length, selection[0] === d.pageItems[0]]);\ var selection = d.selection; documents = 5; activeDocument = d; vc.log([selection.length, typeof documents]);", ); assert_eq!(out, vec![json!(0), json!(2), json!([1, true]), json!([1, "object"])]); assert_eq!(s.active().unwrap().selection.objects, vec![b]); // A top-level `var selection = []` clears the selection; a push goes to a list nobody keeps. let out = log(&mut s, "var selection = []; selection.push(app.activeDocument.pageItems[0]); vc.log(selection.length);"); assert_eq!(out, vec![json!(0)]); assert!(s.active().unwrap().selection.objects.is_empty()); assert!(failure(&mut s, "activeDocument = 5;").contains("not a Document")); let e = failure(&mut s, "'use strict'; documents = 5;"); assert!(e.contains("TypeError") && e.contains("cannot assign to property 'documents': it has only a getter"), "{e}"); let e = failure(&mut s, "#strict on\ndocuments = 5;"); assert!(e.contains("TypeError") && e.contains("cannot assign to property 'documents': it has only a getter"), "{e}"); // With no document open, reading `activeDocument` throws as `app.activeDocument` does. let mut s = Session::new(); assert!(failure(&mut s, "activeDocument;").contains("No active document")); } /// Assigning to a name a collection does not have throws as reading it does, and assigning an /// index does not replace the object there. #[test] fn assigning_a_name_a_collection_lacks_throws() { let (mut s, _, _) = doc(); let steps = undo_steps(&s); for source in ["app.activeDocument.pageItems.qux = 4;", "app.documents.qux = 4;"] { assert!(failure(&mut s, source).contains("Not found"), "{source}"); } let out = log(&mut s, "var p = app.activeDocument.pageItems; p[0] = 3; vc.log(typeof p[0]);"); assert_eq!(out, vec![json!("object")]); // On a page item the value is not kept; strict-mode code throws instead. let out = log(&mut s, "var i = app.activeDocument.pageItems[0]; i.qux = 4; vc.log(typeof i.qux);"); assert_eq!(out, vec![json!("undefined")]); let e = failure(&mut s, "'use strict'; app.activeDocument.pageItems[0].qux = 4;"); assert!(e.contains("TypeError") && e.contains("qux"), "{e}"); assert_eq!(undo_steps(&s), steps); } /// `JSON.stringify` writes a collection, or data that holds one, without reading a `toJSON` the /// collection lacks, while a script that reads `toJSON` itself gets `Not found`. #[test] fn json_stringify_writes_a_collection() { let (mut s, _, _) = doc(); let out = log( &mut s, "var p = app.activeDocument.pageItems, m = ''; vc.log([JSON.stringify(p), JSON.stringify({list: [p, app.documents]})]);\ try { p.toJSON; } catch (e) { m = e.message; } vc.log(m);", ); assert_eq!(out, vec![json!(["{}", "{\"list\":[{},{}]}"]), json!("Not found")]); }