Test the Sharpen tool's Indexed Color and Bitmap refusal

Makes the Dodge refusal tests' session, steps and pixels helpers
pub(super) and reuses them.
This commit is contained in:
rmsff committed 2026-11-10 21:03:11 +03:00
1 parent 3b5a62e75f
commit cbff770f0e
3 files changed
+79 -3

No files matched your search

@@ -696,3 +696,7 @@ impl LiveDab {
#[cfg(test)]
#[path = "dodge_refuses_indexed_tests.rs"]
mod dodge_refuses_indexed_tests;
#[cfg(test)]
#[path = "sharpen_refuses_indexed_tests.rs"]
mod sharpen_refuses_indexed_tests;
@@ -12,7 +12,7 @@ const INDEXED: &str = "Could not use the dodge tool because it does not work wit
const BITMAP: &str = "Could not use the dodge tool because it does not work with Bitmap images (convert image to Grayscale mode to edit).";
/// A session with one 64 × 64 document of `mode` and `depth`, filled mid grey.
fn session(mode: ColorMode, depth: SampleType) -> Session {
pub(super) fn session(mode: ColorMode, depth: SampleType) -> Session {
let mut s = Session::new();
s.add_document(Document::with_background("t", Size::new(64, 64), mode, depth, Color::rgb(0.5, 0.5, 0.5)), None);
s
@@ -22,11 +22,11 @@ fn stroke() -> Value {
json!({"points": [[20, 32], [44, 32]], "size": 20, "hardness": 100, "range": "midtones", "exposure": 100})
}
fn steps(s: &Session) -> usize {
pub(super) fn steps(s: &Session) -> usize {
s.active().unwrap().history.past_len()
}
fn pixels(s: &Session) -> Vec<f32> {
pub(super) fn pixels(s: &Session) -> Vec<f32> {
let st = s.active().unwrap();
let surf = st.doc.layers[0].surface().unwrap();
(0..64).flat_map(|y| (0..64).flat_map(move |x| surf.pixel(x, y))).collect()
@@ -0,0 +1,72 @@
//! #12953: the Sharpen tool refuses Indexed Color and Bitmap documents with the reference
//! application's messages, changing nothing and adding no history; the live stroke is refused the
//! same way. RGB, Grayscale, CMYK and Lab at every depth, Multichannel and Duotone keep sharpening.
use photocraft_color::{ColorMode, SampleType};
use serde_json::json;
use super::dodge_refuses_indexed_tests::{pixels, session, steps};
use super::*;
const INDEXED: &str = "Could not use the sharpen tool because it does not work with Indexed Color images (convert image to RGB Color mode to edit).";
const BITMAP: &str = "Could not use the sharpen tool because it does not work with Bitmap images (convert image to Grayscale mode to edit).";
/// The grey document with its left half black: an edge for the stroke to sharpen.
fn edged(mode: ColorMode, depth: SampleType) -> Session {
let mut s = session(mode, depth);
s.execute("select.rect", json!({"x": 0, "y": 0, "width": 32, "height": 64})).unwrap();
s.execute("edit.fill", json!({"contents": "color", "color": "#000000"})).unwrap();
s.execute("select.deselect", json!({})).unwrap();
s
}
fn stroke() -> Value {
json!({"points": [[20, 32], [44, 32]], "size": 20, "hardness": 100, "strength": 100, "protectDetail": false})
}
/// How far apart the two pixels across the edge are, summed over the native channels.
fn edge_contrast(s: &Session) -> f32 {
let surf = s.active().unwrap().doc.layers[0].surface().unwrap();
surf.pixel(31, 32).iter().zip(surf.pixel(32, 32)).map(|(a, b)| (a - b).abs()).sum()
}
#[test]
fn indexed_and_bitmap_documents_refuse_with_the_reference_words() {
// Per mode: the command's error, the live stroke's error, pixels kept, history kept.
let got = [ColorMode::Indexed, ColorMode::Bitmap].map(|mode| {
let mut s = edged(mode, SampleType::U8);
let (before, n) = (pixels(&s), steps(&s));
let err = s.execute("paint.sharpen", stroke()).err().map(|e| e.to_string());
let live = LiveDab::begin(&s, "paint.sharpen", &stroke()).err().map(|e| e.to_string());
(err, live, pixels(&s) == before, steps(&s) == n)
});
let want = [INDEXED, BITMAP].map(|m| (Some(m.to_string()), Some(m.to_string()), true, true));
assert_eq!(got, want);
}
#[test]
fn other_modes_keep_sharpening_at_every_depth() {
let depths = [SampleType::U8, SampleType::U16, SampleType::F32];
let measured = [ColorMode::Rgb, ColorMode::Grayscale, ColorMode::Cmyk, ColorMode::Lab].into_iter().flat_map(|m| depths.map(|d| (m, d)));
// Not measured in the reference: they keep today's behaviour.
let unmeasured = [(ColorMode::Multichannel, SampleType::U8), (ColorMode::Duotone, SampleType::U8)];
for (mode, depth) in measured.chain(unmeasured) {
let mut s = edged(mode, depth);
let (contrast, n) = (edge_contrast(&s), steps(&s));
let live = LiveDab::begin(&s, "paint.sharpen", &stroke()).map(|_| ());
s.execute("paint.sharpen", stroke()).unwrap_or_else(|e| panic!("{mode:?}/{depth:?}: {e}"));
assert!(edge_contrast(&s) > contrast, "{mode:?}/{depth:?}: the edge got sharper ({contrast} -> {})", edge_contrast(&s));
assert_eq!(steps(&s), n + 1, "{mode:?}/{depth:?}: one history step");
assert!(live.is_ok(), "{mode:?}/{depth:?}: live");
}
}
#[test]
fn dodge_keeps_its_refusal_and_blur_never_gets_the_sharpen_text() {
let mut s = edged(ColorMode::Indexed, SampleType::U8);
let err = s.execute("paint.dodge", json!({"points": [[20, 32], [44, 32]], "size": 20, "exposure": 100})).unwrap_err().to_string();
assert!(err.starts_with("Could not use the dodge tool"), "{err}");
// Blur may get its own refusal (#12656), never Sharpen's.
let blur = s.execute("paint.blur", stroke()).err().map(|e| e.to_string()).unwrap_or_default();
assert!(!blur.contains("sharpen tool"), "{blur}");
}