mirror of
https://github.com/storytold/photocraft.git
synced 2026-12-02 06:20:48 +01:00
Return setup errors from the Zoom Out regression tests
This commit is contained in:
1 parent
57b6ce3f8a
commit
ca2ccf4f3f
1 file changed
+32
-29
@@ -10,30 +10,30 @@ use crate::shortcut_dispatch::{Outcome, take_log, tests::app_harness};
|
||||
|
||||
type AppHarness = Harness<'static, Box<PhotocraftApp>>;
|
||||
|
||||
fn control(h: &mut AppHarness, method: &str, params: Value) -> Value {
|
||||
fn control(h: &mut AppHarness, method: &str, params: Value) -> Result<Value, String> {
|
||||
let (request, _) = ControlRequest::new(method, params);
|
||||
let ctx = h.ctx.clone();
|
||||
let Reply::Done(reply) = crate::control::handle(h.state_mut(), &ctx, &request) else {
|
||||
panic!("{method} should reply immediately");
|
||||
};
|
||||
reply
|
||||
match crate::control::handle(h.state_mut(), &ctx, &request) {
|
||||
Reply::Done(reply) => Ok(reply),
|
||||
_ => Err(format!("{method} should reply immediately")),
|
||||
}
|
||||
}
|
||||
|
||||
fn at_minimum(width: u32, height: u32, zoom: f32) -> AppHarness {
|
||||
fn at_minimum(width: u32, height: u32, zoom: f32) -> Result<AppHarness, String> {
|
||||
let mut session = photocraft_engine::Session::new();
|
||||
session.execute("file.new", json!({"width": width, "height": height, "background": "transparent"})).unwrap();
|
||||
session.execute("file.new", json!({"width": width, "height": height, "background": "transparent"})).map_err(|error| error.to_string())?;
|
||||
let mut h = app_harness(PhotocraftApp::new(session, crate::Services::default()));
|
||||
assert_eq!(control(&mut h, "ui.set", json!({"zoom": zoom}))["ok"], true);
|
||||
assert_eq!(control(&mut h, "ui.set", json!({"zoom": zoom}))?["ok"], true);
|
||||
h.run_steps(4);
|
||||
assert_eq!(h.state().current_zoom(), zoom);
|
||||
h
|
||||
Ok(h)
|
||||
}
|
||||
|
||||
fn zoom_out_menu_enabled(h: &mut AppHarness) -> bool {
|
||||
let reply = control(h, "ui.menu.list", json!({}));
|
||||
let items = reply["result"].as_array().expect("menu rows");
|
||||
let zoom_out = items.iter().find(|item| item["id"] == "view.zoomOut").expect("View menu Zoom Out row");
|
||||
zoom_out["enabled"].as_bool().expect("Zoom Out menu availability")
|
||||
fn zoom_out_menu_enabled(h: &mut AppHarness) -> Result<bool, String> {
|
||||
let reply = control(h, "ui.menu.list", json!({}))?;
|
||||
let items = reply["result"].as_array().ok_or("missing menu rows")?;
|
||||
let zoom_out = items.iter().find(|item| item["id"] == "view.zoomOut").ok_or("missing View menu Zoom Out row")?;
|
||||
zoom_out["enabled"].as_bool().ok_or_else(|| "missing Zoom Out menu availability".to_string())
|
||||
}
|
||||
|
||||
fn shortcut(h: &mut AppHarness, key: Key) {
|
||||
@@ -42,10 +42,10 @@ fn shortcut(h: &mut AppHarness, key: Key) {
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn zoom_out_is_disabled_at_the_document_minimum_and_reenabled_after_zoom_in() {
|
||||
let mut h = at_minimum(10, 10, 0.2);
|
||||
assert!(!zoom_out_menu_enabled(&mut h));
|
||||
assert_eq!(control(&mut h, "ui.menu.invoke", json!({"id": "view.zoomOut"}))["ok"], false);
|
||||
fn zoom_out_is_disabled_at_the_document_minimum_and_reenabled_after_zoom_in() -> Result<(), String> {
|
||||
let mut h = at_minimum(10, 10, 0.2)?;
|
||||
assert!(!zoom_out_menu_enabled(&mut h)?);
|
||||
assert_eq!(control(&mut h, "ui.menu.invoke", json!({"id": "view.zoomOut"}))?["ok"], false);
|
||||
|
||||
let at = h.get_by_role_and_label(Role::MenuItem, "View").rect().center();
|
||||
h.event(Event::PointerMoved(at));
|
||||
@@ -65,33 +65,36 @@ fn zoom_out_is_disabled_at_the_document_minimum_and_reenabled_after_zoom_in() {
|
||||
|
||||
shortcut(&mut h, Key::Equals);
|
||||
assert!(h.state().current_zoom() > 0.2);
|
||||
assert!(zoom_out_menu_enabled(&mut h));
|
||||
assert!(zoom_out_menu_enabled(&mut h)?);
|
||||
take_log(&h.ctx);
|
||||
shortcut(&mut h, Key::Minus);
|
||||
assert!(take_log(&h.ctx).iter().any(|(id, outcome)| id == "view.zoomOut" && *outcome == Outcome::Ran));
|
||||
assert_eq!(h.state().current_zoom(), 0.2);
|
||||
assert!(!zoom_out_menu_enabled(&mut h));
|
||||
assert!(!zoom_out_menu_enabled(&mut h)?);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn zoom_out_stays_available_at_the_absolute_floor() {
|
||||
let mut h = at_minimum(3000, 2000, 0.001);
|
||||
assert!(zoom_out_menu_enabled(&mut h));
|
||||
assert_eq!(control(&mut h, "ui.menu.invoke", json!({"id": "view.zoomOut"}))["ok"], true);
|
||||
fn zoom_out_stays_available_at_the_absolute_floor() -> Result<(), String> {
|
||||
let mut h = at_minimum(3000, 2000, 0.001)?;
|
||||
assert!(zoom_out_menu_enabled(&mut h)?);
|
||||
assert_eq!(control(&mut h, "ui.menu.invoke", json!({"id": "view.zoomOut"}))?["ok"], true);
|
||||
h.run_steps(4);
|
||||
assert_eq!(h.state().current_zoom(), 0.001);
|
||||
shortcut(&mut h, Key::Minus);
|
||||
assert_eq!(h.state().current_zoom(), 0.001);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn zoom_tool_context_menu_still_offers_zoom_out_at_the_document_minimum() {
|
||||
let mut h = at_minimum(10, 10, 0.2);
|
||||
assert_eq!(control(&mut h, "ui.set", json!({"tool": "zoom"}))["ok"], true);
|
||||
let view = crate::canvas::document_display::active_view(h.state()).expect("canvas view");
|
||||
fn zoom_tool_context_menu_still_offers_zoom_out_at_the_document_minimum() -> Result<(), String> {
|
||||
let mut h = at_minimum(10, 10, 0.2)?;
|
||||
assert_eq!(control(&mut h, "ui.set", json!({"tool": "zoom"}))?["ok"], true);
|
||||
let view = crate::canvas::document_display::active_view(h.state()).ok_or("missing canvas view")?;
|
||||
let at = view.rect.center();
|
||||
assert!(crate::canvas_tool_menu::open_at(h.state_mut(), crate::state::Tool::Zoom, craft::keymap::Mods::default(), at, [5.0, 5.0], None));
|
||||
assert_eq!(control(&mut h, "ui.context.choose", json!({"id": "view.zoomOut"}))["ok"], true);
|
||||
assert_eq!(control(&mut h, "ui.context.choose", json!({"id": "view.zoomOut"}))?["ok"], true);
|
||||
h.run_steps(4);
|
||||
assert_eq!(h.state().current_zoom(), 0.2);
|
||||
Ok(())
|
||||
}
|
||||
Reference in new issue
Block a user