Skip to content

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

test(actions): Export As is recorded and replayed; a bad recorded step is an error (#7207)

Korg1cha6b950c4fc6d
diff --git a/crates/ui-egui/src/export_dialog.rs b/crates/ui-egui/src/export_dialog.rs
index 2f9bdd2a9..62677292c 100644
--- a/crates/ui-egui/src/export_dialog.rs
+++ b/crates/ui-egui/src/export_dialog.rs
@@ -1085,6 +1085,34 @@ mod tests {
assert_eq!(f["metadata"], "none");
}
+ /// A recorded Export As step replays only with a destination and a format Export As writes
+ /// (#7207); anything else is an error, not a guess.
+ #[test]
+ fn a_replayed_export_needs_a_path_and_a_known_format() {
+ let mut app = PhotocraftApp::new(photocraft_engine::Session::new(), crate::Services::default());
+ assert!(replay(&mut app, &json!({"path": "/x.png"})).unwrap_err().contains("document"));
+ app.run("file.new", json!({"width": 4, "height": 4})).unwrap();
+ for bad in [json!({}), json!({"path": ""}), json!("nope"), json!({"path": 3})] {
+ assert!(replay(&mut app, &bad).unwrap_err().contains("path"), "{bad}");
+ }
+ for format in [json!("bmp"), json!(7)] {
+ assert!(replay(&mut app, &json!({"path": "/x.bmp", "format": format})).unwrap_err().contains("format"), "{format}");
+ }
+ // A setting of the wrong type or out of the dialog's range is an error, not its default.
+ for (key, v) in [
+ ("scale", json!("50")),
+ ("scale", json!(5000)),
+ ("quality", json!(0)),
+ ("transparency", json!("yes")),
+ ("metadata", json!("some")),
+ ("width", json!(-3)),
+ ] {
+ let mut step = json!({"path": "/x.png", "format": "png"});
+ step[key] = v.clone();
+ assert!(replay(&mut app, &step).unwrap_err().contains(key), "{key}: {v}");
+ }
+ }
+
/// Only the dialog's own settings are remembered: not the destination, the document size or
/// the layer the dialog was opened with.
#[test]
diff --git a/crates/ui-egui/src/file_open/tests.rs b/crates/ui-egui/src/file_open/tests.rs
index 00db3ef96..b288c20e2 100644
--- a/crates/ui-egui/src/file_open/tests.rs
+++ b/crates/ui-egui/src/file_open/tests.rs
@@ -898,6 +898,50 @@ fn save_a_copy_is_recorded_and_replayed_as_a_copy() {
assert!(st.is_dirty(), "and its unsaved changes");
}
+/// #7207: File › Export › Export As is recorded in an action with its destination and settings,
+/// as Save a Copy is, and played back without the dialog: on another document it exports that
+/// document, with the recorded format, quality and scale.
+#[test]
+fn export_as_is_recorded_and_replayed() {
+ type Exported = Rc<RefCell<Vec<(String, u32, u32, Option<u8>)>>>;
+ let exported: Exported = Rc::default();
+ let e = exported.clone();
+ let services = Services {
+ export: Some(Box::new(move |doc: &Document, path: &str, s: &crate::ExportSettings| {
+ e.borrow_mut().push((path.to_string(), doc.size.width, doc.size.height, s.jpeg_quality));
+ Ok((b"out".to_vec(), Vec::new()))
+ })),
+ file_dialog: Some(file_dialog::fake(vec![Some(FileDialogAnswer::SaveTo("/pics/out.jpg".into()))]).0),
+ write: Some(Box::new(|_: &str, _: &[u8]| Ok(()))),
+ ..Default::default()
+ };
+ let mut app = PhotocraftApp::new(photocraft_engine::Session::new(), services);
+ let ctx = egui::Context::default();
+ app.run("file.new", json!({"width": 40, "height": 20})).unwrap();
+ app.run("actions.record", json!({})).unwrap();
+ let id = menus::invoke(&mut app, &ctx, "file.export.exportAs", json!({})).unwrap()["dialog"].as_u64().unwrap();
+ let fields = &mut app.ui.dialog_mut(id).unwrap().fields;
+ fields.insert("format".into(), json!("jpg"));
+ fields.insert("quality".into(), json!(70));
+ fields.insert("scale".into(), json!(50));
+ crate::dialogs::confirm(&mut app, id).unwrap();
+ answer(&mut app);
+ app.run("actions.stop", json!({})).unwrap();
+ assert_eq!(*exported.borrow(), [("/pics/out.jpg".to_string(), 20, 10, Some(70))]);
+ let (step_id, step) = app.session.actions.list[0].steps.last().cloned().unwrap();
+ assert_eq!(step_id, "file.export.exportAs");
+ assert_eq!((&step["path"], &step["format"], &step["quality"], &step["scale"]), (&json!("/pics/out.jpg"), &json!("jpg"), &json!(70), &json!(50)));
+ assert!(step.as_object().unwrap().keys().all(|k| !k.starts_with("__")), "only the destination and the dialog's settings: {step}");
+ assert_eq!(crate::actions::label_of("file.export.exportAs"), "Export As");
+
+ app.run("file.new", json!({"width": 100, "height": 60})).unwrap();
+ exported.borrow_mut().clear();
+ let played = app.run("actions.play", json!({"action": 0})).unwrap();
+ assert!(played.get("failed").is_none(), "{played}");
+ assert_eq!(*exported.borrow(), [("/pics/out.jpg".to_string(), 50, 30, Some(70))], "the played document, scaled as recorded");
+ assert!(app.ui.dialogs.is_empty(), "no dialog on playback");
+}
+
/// #4400: a new document saved under a name is listed in Open Recent (and on the Home screen),
/// as opening it is; a Save a Copy isn't, since the document doesn't live there.
#[test]