Skip to content

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

Fix flaky canvas_geom undo test: isolate the text-gamma test in its own binary (#135)

Brandon Thomas27e047f19f6f
diff --git a/crates/compose/src/psblend.rs b/crates/compose/src/psblend.rs
index 3b8448af4..d162e0216 100644
--- a/crates/compose/src/psblend.rs
+++ b/crates/compose/src/psblend.rs
@@ -142,6 +142,10 @@ static TEXT_GAMMA_SETTING: std::sync::atomic::AtomicU32 = std::sync::atomic::Ato
/// Sets Color Settings › "Blend Text Colors Using Gamma" (1 = off). It is an application
/// setting, not stored in documents: files saved with it off (ag-psd float-color) mix type
/// linearly. Values are clamped to Photoshop's 1.00–2.20.
+///
+/// The value is process-wide: a test that changes it races every concurrently running test
+/// that composites a type layer, so such tests belong in their own test binary (see
+/// `crates/engine/tests/text_gamma.rs`).
pub fn set_text_gamma(gamma: f32) {
let g = if gamma.is_finite() { gamma.clamp(1.0, 2.2) } else { TEXT_GAMMA };
TEXT_GAMMA_SETTING.store(g.to_bits(), std::sync::atomic::Ordering::Relaxed);
diff --git a/crates/engine/src/canvas_geom/tests.rs b/crates/engine/src/canvas_geom/tests.rs
index a6f0a441b..2440e66d4 100644
--- a/crates/engine/src/canvas_geom/tests.rs
+++ b/crates/engine/src/canvas_geom/tests.rs
@@ -106,9 +106,19 @@ fn check_turn(depth: u64, mode: &str, t: Turn) {
refresh(&mut d, Refresh::All);
let (mean, big) = diff(&photocraft_compose::flatten(&d).px, &want);
assert!(mean < 2.0 / 255.0 && big < 0.01, "{t:?} {depth} {mode} re-rendered: mean {mean} big {big}");
- // Undo restores the original.
+ // Undo restores the original, bit-exactly.
s.execute("edit.undo", json!({})).unwrap();
- assert_eq!(photocraft_compose::flatten(doc(&s)).px, before.px);
+ let undone = photocraft_compose::flatten(doc(&s));
+ assert_eq!(undone.px.len(), before.px.len(), "{t:?} {depth} {mode}: undo changed the canvas size");
+ let (mut n, mut max, mut first) = (0usize, 0.0f32, None);
+ for (i, (p, q)) in undone.px.iter().zip(&before.px).enumerate() {
+ if p != q {
+ n += 1;
+ max = (0..4).map(|c| (p[c] - q[c]).abs()).fold(max, f32::max);
+ first.get_or_insert((i % W as usize, i / W as usize, *p, *q));
+ }
+ }
+ assert!(n == 0, "{t:?} {depth} {mode}: undo left {n} pixels different (max {max}); first (x, y, undone, before): {first:?}");
}
#[test]
diff --git a/crates/engine/src/color_cmds.rs b/crates/engine/src/color_cmds.rs
index b5520919b..a0a4245de 100644
--- a/crates/engine/src/color_cmds.rs
+++ b/crates/engine/src/color_cmds.rs
@@ -1221,19 +1221,8 @@ mod settings_tests {
assert_eq!(t.color.settings, s.color.settings);
}
- #[test]
- fn blend_text_gamma_setting() {
- let mut s = Session::new();
- assert!((s.color.settings.blend_text_gamma - 1.45).abs() < 1e-6);
- assert!(s.execute("edit.colorSettings", json!({"blendTextGamma": 3.0})).is_err());
- // The compositor's (application-wide) gamma follows; restored to the default at once.
- s.execute("edit.colorSettings", json!({"blendTextGamma": 1.45})).unwrap();
- assert!((photocraft_compose::psblend::text_gamma() - 1.45).abs() < 1e-6);
- let r = s.execute("edit.colorSettings", json!({"blendTextGamma": false})).unwrap();
- assert_eq!(r["settings"]["blendTextGamma"], 1.0);
- s.execute("edit.colorSettings", json!({"blendTextGamma": true})).unwrap();
- assert!((s.color.settings.blend_text_gamma - 1.45).abs() < 1e-6);
- }
+ // The Blend Text Colors Using Gamma test is `tests/text_gamma.rs`: it changes the
+ // process-wide text gamma, which would race every unit test here that composites type.
#[test]
fn open_policies() {
diff --git a/crates/engine/tests/text_gamma.rs b/crates/engine/tests/text_gamma.rs
new file mode 100644
index 000000000..4089dcb21
--- /dev/null
+++ b/crates/engine/tests/text_gamma.rs
@@ -0,0 +1,30 @@
+//! Edit › Color Settings › "Blend Text Colors Using Gamma" drives the compositor's
+//! application-wide text gamma (`photocraft_compose::psblend::text_gamma`).
+//!
+//! This test lives in its own integration-test binary (its own process) on purpose: switching
+//! the gamma away from the default changes how every type layer composites, so running it next
+//! to the engine's unit tests let a concurrent test flatten one document under two gammas
+//! (`canvas_geom::tests::turns_at_every_depth_and_mode` compares the composite before an edit
+//! with the one after its undo, and failed on Windows CI that way). Keep any other test that
+//! changes the gamma in this file.
+
+use photocraft_engine::Session;
+use serde_json::json;
+
+#[test]
+fn blend_text_gamma_setting() {
+ let mut s = Session::new();
+ assert!((s.color.settings.blend_text_gamma - 1.45).abs() < 1e-6);
+ assert!(s.execute("edit.colorSettings", json!({"blendTextGamma": 3.0})).is_err());
+ // The compositor's (application-wide) gamma follows the setting.
+ s.execute("edit.colorSettings", json!({"blendTextGamma": 1.45})).unwrap();
+ assert!((photocraft_compose::psblend::text_gamma() - 1.45).abs() < 1e-6);
+ let r = s.execute("edit.colorSettings", json!({"blendTextGamma": false})).unwrap();
+ assert_eq!(r["settings"]["blendTextGamma"], 1.0);
+ assert_eq!(photocraft_compose::psblend::text_gamma(), 1.0);
+ s.execute("edit.colorSettings", json!({"blendTextGamma": 1.8})).unwrap();
+ assert!((photocraft_compose::psblend::text_gamma() - 1.8).abs() < 1e-6);
+ s.execute("edit.colorSettings", json!({"blendTextGamma": true})).unwrap();
+ assert!((s.color.settings.blend_text_gamma - 1.45).abs() < 1e-6);
+ assert!((photocraft_compose::psblend::text_gamma() - 1.45).abs() < 1e-6);
+}