Skip to content

PhotoCraft: An open-source, photo editor written by scratch in pure Rust

test(engine): Content-Aware Fill with an empty sampling area is refused and changes nothing (#6532)

Korg1ch36d6b669d40d
diff --git a/crates/engine/src/edit_menu_cmds/tests.rs b/crates/engine/src/edit_menu_cmds/tests.rs
index bc274795a..dc32b7c8f 100644
--- a/crates/engine/src/edit_menu_cmds/tests.rs
+++ b/crates/engine/src/edit_menu_cmds/tests.rs
@@ -105,10 +105,15 @@ fn blob_session(depth: u32) -> Session {
fn content_aware_fill_huge_area_neither_panics_nor_wraps() {
let mut s = blob_session(8);
// `[1e30, 0, 1e30, 10]` overflowed the i32 additions in the area parser (a debug-build
- // panic, a garbage sampling window in release); the additions saturate now, so this is
- // simply a whole-canvas window.
- let r = s.execute("edit.contentAwareFill", json!({"sampling": "custom", "area": [1e30, 0.0, 1e30, 10.0], "colorAdaptation": "none"})).unwrap();
- assert!(r["filled"].as_u64().unwrap() > 100);
+ // panic, a garbage sampling window in release); the additions saturate now, so the area lies
+ // wholly right of the canvas: nothing to sample, which is an error, not a black fill (#6532).
+ let before = s.active().unwrap().doc.clone();
+ let past = s.active().unwrap().history.past_len();
+ let err = s.execute("edit.contentAwareFill", json!({"sampling": "custom", "area": [1e30, 0.0, 1e30, 10.0], "colorAdaptation": "none"})).unwrap_err();
+ assert!(err.to_string().contains("nothing to sample from"), "{err}");
+ let st = s.active().unwrap();
+ assert!(std::sync::Arc::ptr_eq(&before, &st.doc), "the document changed");
+ assert_eq!(st.history.past_len(), past, "a history step was recorded");
// A malformed area names the problem instead of silently falling back.
for area in [json!([0.0, 0.0, null, 10.0]), json!([0.0, 0.0, 10.0]), json!("nope")] {
let err = s.execute("edit.contentAwareFill", json!({"sampling": "custom", "area": area})).unwrap_err();
@@ -312,6 +317,23 @@ fn content_aware_fill_takes_the_workspace_edits() {
assert!(r["filled"].as_u64().unwrap() > 2800);
}
+/// #6532: a sampling area with no pixels outside the fill area is refused instead of filling it
+/// black, and leaves the document untouched; any margin past the selection fills.
+#[test]
+fn content_aware_fill_with_nothing_to_sample_is_refused() {
+ let mut s = blob_session(8);
+ let before = px(&s, 30, 22);
+ let revision = s.active().unwrap().revision;
+ for p in [json!({"sampling": "rectangular", "margin": 0}), json!({"sampling": "custom", "area": [28, 18, 4, 4]})] {
+ let e = s.execute("edit.contentAwareFill", p.clone()).unwrap_err().to_string();
+ assert!(e.contains("nothing to sample from"), "{p}: {e}");
+ assert_eq!(px(&s, 30, 22), before, "{p}: nothing was filled");
+ assert_eq!(s.active().unwrap().revision, revision, "{p}: no history step");
+ }
+ s.execute("edit.contentAwareFill", json!({"sampling": "rectangular", "margin": 1})).unwrap();
+ assert_ne!(px(&s, 30, 22), before, "the red blob is filled from around the selection");
+}
+
#[test]
fn content_aware_fill_outputs_and_sampling() {
let mut s = blob_session(8);