Skip to content

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

Test Export As scaling through real PNG exports

harrisonoconnorhoverd1ff8920d54d
diff --git a/crates/ui-egui/src/export_dialog.rs b/crates/ui-egui/src/export_dialog.rs
index 5897132c2..ef3706be0 100644
--- a/crates/ui-egui/src/export_dialog.rs
+++ b/crates/ui-egui/src/export_dialog.rs
@@ -292,6 +292,51 @@ pub fn quick_export_png(app: &mut PhotocraftApp) -> Result<Value, String> {
mod tests {
use super::*;
+ #[test]
+ fn export_document_scales_height_when_rounded_width_is_unchanged() {
+ use photocraft_doc::{Color, ColorMode, SampleType, Size};
+
+ for depth in [SampleType::U8, SampleType::U16, SampleType::F32] {
+ for (width, height, scale, expected) in [
+ (1, 100, 50, (1, 50)),
+ (1, 200, 50, (1, 100)),
+ (10, 100, 99, (10, 99)),
+ (10, 100, 101, (10, 101)),
+ (400, 300, 50, (200, 150)),
+ (400, 300, 100, (400, 300)),
+ ] {
+ let doc = Document::with_background("x", Size::new(width, height), ColorMode::Rgb, depth, Color::WHITE);
+ let before = doc.clone();
+ let mut f = Map::new();
+ f.insert("format".into(), json!("png"));
+ f.insert("scale".into(), json!(scale));
+ let out = export_document(&doc, &f, None).unwrap();
+ assert_eq!((out.size.width, out.size.height), expected, "{width} × {height} at {scale}%, {depth:?}");
+ assert_eq!(out.depth, depth, "export preserves the document's depth");
+ assert_eq!(doc, before, "export leaves the source document unchanged");
+ }
+ }
+ }
+
+ #[test]
+ fn export_document_proxy_limits_both_dimensions_before_rounding() {
+ use photocraft_doc::{Color, ColorMode, SampleType, Size};
+
+ for (width, height, scale, max_side, expected) in
+ [(1, 1000, 100, 360, (1, 360)), (1, 1000, 50, 360, (1, 360)), (1, 1000, 25, 360, (1, 250)), (13, 20, 50, 8, (5, 8)), (20, 13, 50, 8, (8, 5))]
+ {
+ let doc = Document::with_background("x", Size::new(width, height), ColorMode::Rgb, SampleType::U8, Color::WHITE);
+ let before = doc.clone();
+ let mut f = Map::new();
+ f.insert("format".into(), json!("png"));
+ f.insert("scale".into(), json!(scale));
+ let out = export_document(&doc, &f, Some(max_side)).unwrap();
+ assert_eq!((out.size.width, out.size.height), expected, "{width} × {height} at {scale}%, proxy limit {max_side}");
+ assert!(out.size.width.max(out.size.height) <= max_side, "proxy respects its longest-side limit");
+ assert_eq!(doc, before, "preview leaves the source document unchanged");
+ }
+ }
+
#[test]
fn export_document_scales_and_flattens() {
let doc = Document::with_background(
diff --git a/crates/ui-egui/tests/export_as_scaling.rs b/crates/ui-egui/tests/export_as_scaling.rs
new file mode 100644
index 000000000..915b5848d
--- /dev/null
+++ b/crates/ui-egui/tests/export_as_scaling.rs
@@ -0,0 +1,123 @@
+//! #1353: Export As must scale the height even when the rounded width stays unchanged.
+//! Drive the real Scale field and Export button, then decode the PNG the writer received.
+
+use std::cell::RefCell;
+use std::rc::Rc;
+use std::sync::Arc;
+
+use egui_kittest::kittest::Queryable;
+use photocraft_ui_egui::{PhotocraftApp, Services, menus};
+use serde_json::json;
+
+type Harness = egui_kittest::Harness<'static, PhotocraftApp>;
+type Writes = Rc<RefCell<Vec<(String, Vec<u8>)>>>;
+
+fn harness() -> Option<(Harness, Writes)> {
+ let built = std::panic::catch_unwind(|| {
+ let writes = Writes::default();
+ let captured = writes.clone();
+ let h = egui_kittest::Harness::builder().with_size(egui::vec2(1100.0, 760.0)).with_pixels_per_point(1.0).with_max_steps(64).wgpu().build_eframe(
+ move |cc| {
+ PhotocraftApp::setup_context(&cc.egui_ctx, Default::default());
+ let services = Services {
+ pick_save: Some(Box::new(|suggested| Some(suggested.to_string()))),
+ export: Some(Box::new(|doc, path, settings| {
+ let opts = photocraft_io::ExportOptions {
+ xmp: if settings.xmp_all { photocraft_io::XmpEmbed::All } else { photocraft_io::XmpEmbed::None },
+ ..Default::default()
+ };
+ photocraft_io::export(doc, path, &opts).map(|r| (r.bytes, r.warnings)).map_err(|e| e.to_string())
+ })),
+ write: Some(Box::new(move |path, bytes| {
+ captured.borrow_mut().push((path.to_string(), bytes.to_vec()));
+ Ok(())
+ })),
+ ..Default::default()
+ };
+ let mut app = PhotocraftApp::new(photocraft_engine::Session::new(), services);
+ app.session.prefs.edit(|p| p.interface.language = "en".into());
+ if let Some(rs) = cc.wgpu_render_state.as_ref() {
+ app.set_wgpu(rs.clone());
+ }
+ app
+ },
+ );
+ (h, writes)
+ });
+ match built {
+ Ok(h) => Some(h),
+ Err(_) => {
+ eprintln!("skipping: no GPU adapter");
+ None
+ }
+ }
+}
+
+fn evidence(h: &mut Harness, depth: u8, stage: &str) {
+ if let Some(dir) = std::env::var_os("PHOTOCRAFT_EXPORT_EVIDENCE_DIR") {
+ std::fs::create_dir_all(&dir).expect("evidence directory");
+ h.render().expect("render UI").save(std::path::Path::new(&dir).join(format!("{stage}-{depth}bit.png"))).expect("save UI evidence");
+ }
+}
+
+#[test]
+fn export_as_scales_a_narrow_image_without_editing_the_source() {
+ let Some((mut h, writes)) = harness() else { return };
+ h.run_steps(4);
+ for depth in [8, 16, 32] {
+ writes.borrow_mut().clear();
+ {
+ let app = h.state_mut();
+ while app.session.active().is_some() {
+ app.run("file.close", json!({"discard": true})).expect("close");
+ }
+ app.run("file.new", json!({"width": 1, "height": 100, "depth": depth, "background": "white"})).expect("new narrow document");
+ app.sync_views();
+ }
+ let st = h.state().session.active().expect("document");
+ let source = st.doc.clone();
+ let history = st.history.entries();
+ let revision = st.revision;
+
+ let ctx = h.ctx.clone();
+ menus::invoke(h.state_mut(), &ctx, "file.export.exportAs", json!({})).expect("open Export As");
+ h.run_steps(6);
+ let label = h.get_by_label("Scale").rect();
+ let field = h
+ .query_all_by_role(egui::accesskit::Role::SpinButton)
+ .find(|n| {
+ let r = n.rect();
+ r.left() >= label.right() && r.left() < label.right() + 100.0 && (r.center().y - label.center().y).abs() < 3.0
+ })
+ .expect("Scale numeric field");
+ field.click();
+ h.run_steps(2);
+ // Clicking a numeric field selects its current value; type through egui's text input.
+ h.event(egui::Event::Text("50".into()));
+ h.run_steps(3);
+ // Click away to commit the field without Enter confirming the dialog.
+ h.get_by_label("Image Size").click();
+ h.run_steps(3);
+ assert!(h.query_by_label("1 × 50 px").is_some(), "{depth}-bit: dialog shows the requested output size");
+ evidence(&mut h, depth, "dialog");
+
+ h.get_by_label("Export").click();
+ h.run_steps(6);
+ evidence(&mut h, depth, "result");
+ let written = writes.borrow();
+ assert_eq!(written.len(), 1, "{depth}-bit: Export writes one file");
+ let (path, bytes) = &written[0];
+ assert!(path.ends_with(".png"), "PNG export path: {path}");
+ if let Some(dir) = std::env::var_os("PHOTOCRAFT_EXPORT_EVIDENCE_DIR") {
+ std::fs::write(std::path::Path::new(&dir).join(format!("exported-{depth}bit.png")), bytes).expect("save exported PNG evidence");
+ }
+ let png = photocraft_codecs::decode_as(photocraft_codecs::Format::Png, bytes).expect("decode the written PNG");
+ assert_eq!(png.dimensions(), (1, 50), "{depth}-bit: written dimensions match the dialog");
+
+ let st = h.state().session.active().expect("source remains open");
+ assert!(Arc::ptr_eq(&st.doc, &source), "{depth}-bit: source document is unchanged");
+ assert_eq!((st.doc.size.width, st.doc.size.height), (1, 100));
+ assert_eq!(st.history.entries(), history, "export must not add source history");
+ assert_eq!(st.revision, revision, "export must not change source revision");
+ }
+}