Files
vectorcraft/crates/engine/src/tests/cancel.rs
T
dotCooCoo cd6c575ae6 Tests: engine tests assert results instead of only success
Engine tests in crates/engine/src/tests that passed on a broken result
now check the result:

- cancel: Export PDF under a canceled token stops, and outside it the
  whole file is written.
- ids_without_selection: each command refuses with nothing selected,
  then changes the object given in one undo step.
- pathops: every bad input is refused and changes nothing.
- plugins: the folder's plug-in is listed as loaded, nothing is loaded
  once the folder is cleared, and it loads again when set again.
- brushlib: the pasted brush and its pattern equal the copied ones.
- build: Image Trace keeps a preset's settings passed back, for one
  preset with an enhanced version and one without, in place of a loop
  over every preset.
- strokegeom: two width profiles and two arrowheads are checked by
  value, in place of a round trip over every preset and arrowhead.
- script_xmp: the ACL test fails instead of returning when the default
  ACL cannot be set.
- Tests that checked every item of a list now also check its length, so
  an empty list fails (bboxrotate, build, clippaint, colormgmt,
  constrain_new, dynamic_symbols, freeform, gradient).

Removed: a manual timing probe in areafit, covered by
auto_height_in_columns_lays_out_the_columns_twice_a_keystroke, and two
plug-in file generators in plugins that ran only with an environment
variable set, covered by live_effects_are_given_their_object_s_paints
and filters_set_effects_and_stroke_styles.

object.path.simplify now refuses a negative tolerance and a level
outside 0 to 100, which it clamped before.
2026-12-01 18:29:53 -08:00

97 lines
3.9 KiB
Rust

//! Cancelling a long export (#2165): it stops at the next checkpoint with `EngineError::Cancelled`,
//! writes nothing, leaves the document as it was, and the session goes on working.
use std::cell::Cell;
use std::rc::Rc;
use serde_json::json;
use crate::cancel::{self, CancelToken, ProgressSink};
use crate::{EngineError, Session};
fn session(artboards: usize) -> Session {
let mut s = Session::new();
s.execute("file.new", &json!({"width": 100, "height": 80, "artboards": artboards})).unwrap();
s.execute("shape.rectangle", &json!({"x": 10, "y": 10, "width": 40, "height": 30})).unwrap();
s
}
fn revision(s: &Session) -> u64 {
s.doc().unwrap().revision
}
#[test]
fn a_pdf_export_cancelled_between_pages_stops_writes_nothing_and_changes_nothing() {
let dir = vectorcraft_testkit::TempDir::new("cancel-pdf");
let path = dir.join("out.pdf");
std::fs::remove_file(&path).ok();
let mut s = session(5);
let before = (revision(&s), s.doc().unwrap().history.undo.len());
let token = CancelToken::new();
let pages = Rc::new(Cell::new(0));
// The sink runs inside the export, as the page loop reports each page: cancel on the third.
let sink: ProgressSink = {
let (token, pages) = (token.clone(), pages.clone());
Rc::new(move |done, total, _| {
// Stages before the pages report without a total.
if total != 5 {
return;
}
pages.set(done);
if done == 3 {
token.cancel();
}
})
};
let params = json!({"path": path.to_str().unwrap()});
let r = cancel::scope(&token, Some(sink), || s.execute("document.exportPdf", &params));
assert!(matches!(r, Err(EngineError::Cancelled)), "{r:?}");
assert_eq!(pages.get(), 3, "the export stopped at the checkpoint after the cancel, not at the end");
assert!(!path.exists(), "a cancelled export writes no file");
assert_eq!((revision(&s), s.doc().unwrap().history.undo.len()), before, "the document is as it was");
// The session is fine: the same export, not cancelled, completes.
s.execute("document.exportPdf", &params).unwrap();
assert!(path.exists());
}
#[test]
fn a_cancelled_flattening_export_stops_before_the_pages() {
let mut s = session(2);
let token = CancelToken::new();
token.cancel();
// PDF/X-1a flattens transparency first; a token raised beforehand stops at the first check.
let r = cancel::scope(&token, None, || s.execute("document.exportPdf", &json!({"standard": "pdfX1a"})));
assert!(matches!(r, Err(EngineError::Cancelled)), "{r:?}");
assert!(s.execute("document.exportPdf", &json!({"standard": "pdfX1a"})).is_ok());
}
#[test]
fn raster_exports_and_svg_stop_when_cancelled_too() {
let dir = vectorcraft_testkit::TempDir::new("cancel-formats");
let mut s = session(3);
let token = CancelToken::new();
token.cancel();
for ext in ["png", "svg", "pdf"] {
let path = dir.join(format!("out.{ext}"));
std::fs::remove_file(&path).ok();
let params = json!({"path": path.to_str().unwrap()});
let r = cancel::scope(&token, None, || s.execute("document.export", &params));
assert!(matches!(r, Err(EngineError::Cancelled)), "{ext}: {r:?}");
assert!(!path.exists(), "{ext}: nothing is written");
}
}
#[test]
fn nothing_is_cancelled_outside_a_scope() {
let token = CancelToken::new();
token.cancel();
// The token only counts for work run under it, on its thread.
let mut s = session(1);
let r = cancel::scope(&token, None, || s.execute("document.exportPdf", &json!({})));
assert!(matches!(r, Err(EngineError::Cancelled)), "under the token: {r:?}");
let r = s.execute("document.exportPdf", &json!({})).unwrap();
let bytes = vectorcraft_format::base64_decode(r["dataBase64"].as_str().unwrap()).unwrap();
assert!(bytes.starts_with(b"%PDF-"), "outside it the whole file is written");
assert!(bytes.ends_with(b"%%EOF"), "to its end");
}