diff --git a/crates/ui-egui/src/dialogs/tests.rs b/crates/ui-egui/src/dialogs/tests.rs index 91b8aa8d2..cf62dd443 100644 --- a/crates/ui-egui/src/dialogs/tests.rs +++ b/crates/ui-egui/src/dialogs/tests.rs @@ -1,9 +1,16 @@ -use deckcraft_model::text::{Action, Hyperlink, Run, TextBody}; -use deckcraft_model::{Presentation, SlideId}; +use std::sync::Arc; -use super::{ - CurrentLink, HyperlinkSeed, common_click, hyperlink_command, hyperlink_seed_of, hyperlink_should_apply, link_covering, seed_from, seed_of_link, -}; +use craft::host::ScriptedFiles; +use craft::text::ui::hyperlink::{CurrentLink, Hyperlink as UiHyperlink, LinkTarget}; +use deckcraft_model::text::{Action, Hyperlink, Run, TextBody}; +use deckcraft_model::{Presentation, Shape, SlideId}; +use egui::vec2; +use egui_kittest::Harness; +use egui_kittest::kittest::Queryable as _; +use serde_json::{Value, json}; + +use super::{common_click, current_link_of, link_covering}; +use crate::{Services, SlideApp}; fn link(action: Action, tip: &str) -> Hyperlink { Hyperlink { action, tooltip: tip.into(), highlight_click: false, sound: None, stop_sound: false, zoom_return: None } @@ -15,17 +22,31 @@ fn url(address: &str, tip: &str) -> Hyperlink { h } +fn dialog_method(h: &mut Harness<'_, SlideApp>, method: &str, params: Value) -> Result { + let ctx = h.ctx.clone(); + super::control(h.state_mut(), &ctx, method, ¶ms).unwrap() +} + +fn dialog_fields(h: &Harness<'_, SlideApp>, key: &str) -> Value { + crate::control::inspect(h.state(), &h.ctx)["dialogs"] + .as_array() + .unwrap() + .iter() + .find(|d| d["key"] == key) + .unwrap()["fields"] + .clone() +} + +fn export_harness(session: deckcraft_engine::Session, files: Arc) -> Harness<'static, SlideApp> { + crate::test_harness::harness_with(session, Services::default(), Harness::builder().with_size(vec2(1200.0, 900.0)), |p| { + p.host = p.host.with_files(files.clone()); + }) +} + /// Header & Footer ▸ Apply without picking a format leaves a slide's existing date field in /// its own format instead of rewriting it to the default one. #[test] fn header_footer_apply_keeps_an_existing_date_format() { - use std::sync::Arc; - - use egui::vec2; - use egui_kittest::kittest::Queryable as _; - use serde_json::json; - - use crate::SlideApp; let mut session = deckcraft_engine::Session::with_new(); session.execute("design.headerFooter", &json!({"date": true, "dateText": "", "dateFormat": "datetime4", "all": true})).unwrap(); // A file whose document-level format is unset, as an imported deck's is when its date field is not one of the thirteen kinds. @@ -48,13 +69,11 @@ fn header_footer_apply_keeps_an_existing_date_format() { }) .collect() }; - let mut harness = egui_kittest::Harness::builder().with_size(vec2(1200.0, 900.0)).build_ui_state( - |ui, app: &mut SlideApp| { - app.logic(ui.ctx()); - app.ui(ui); - }, - // P3(dialogs-w19): sh.A2 - SlideApp::new_base(session, crate::Services::default()), + let mut harness = crate::test_harness::harness_with( + session, + Services::default(), + Harness::builder().with_size(vec2(1200.0, 900.0)), + |_| {}, ); harness.run_steps(2); assert_eq!(date_fields(harness.state()), vec!["datetime4".to_string()]); @@ -65,163 +84,64 @@ fn header_footer_apply_keeps_an_existing_date_format() { assert_eq!(date_fields(harness.state()), vec!["datetime4".to_string()], "the dialog left the field's format alone"); } -/// The Export… dialog (Animated GIF) shows the GIF options and exports through `file.export` (#171). +/// A reversed slide range refuses export inline; a repaired range writes a once-playing GIF. #[test] fn export_dialog_writes_an_animated_gif() { - use std::cell::RefCell; - - use egui::vec2; - use egui_kittest::kittest::Queryable as _; - use serde_json::json; - - use crate::SlideApp; - let dir = std::env::temp_dir().join(format!("deckcraft-ui-gif-{}", std::process::id())); - std::fs::create_dir_all(&dir).unwrap(); - let path = dir.join("deck.gif"); - let picked = path.to_string_lossy().to_string(); - let suggested = std::rc::Rc::new(RefCell::new(String::new())); - let seen = suggested.clone(); - let services = crate::Services { - pick_save: Some(Box::new(move |s: &str| { - *seen.borrow_mut() = s.to_string(); - Some(picked.clone()) - })), - ..Default::default() - }; + let files = Arc::new(ScriptedFiles::new().saving_suggested()); let mut session = deckcraft_engine::Session::with_new(); session.execute("slide.new", &json!({})).unwrap(); - let mut harness = egui_kittest::Harness::builder().with_size(vec2(1200.0, 900.0)).build_ui_state( - |ui, app: &mut SlideApp| { - app.logic(ui.ctx()); - app.ui(ui); - }, - // P3(dialogs-w19): sh.A2 - SlideApp::new_base(session, services), - ); - harness.run_steps(2); - harness.state_mut().run("app.exportDialog", json!({})).unwrap(); - harness.run_steps(2); - harness.get_by_label("Animated GIF (.gif)").click_accesskit(); - harness.run_steps(2); - assert_eq!(harness.state().dialog.as_ref().and_then(|d| d.fields.get("fmt")).map(String::as_str), Some("gif")); - harness.get_by_label("Seconds per slide:"); - harness.get_by_label("Loop forever").click_accesskit(); - harness.run_steps(2); - // A range outside the deck keeps the dialog open with a message, and nothing is written. - harness.state_mut().dialog.as_mut().unwrap().fields.insert("gifTo".into(), "9".into()); - harness.run_steps(2); - harness.key_press(egui::Key::Enter); - harness.run_steps(2); - assert!(harness.state().dialog.is_some()); - assert!(harness.state().status.as_ref().is_some_and(|(m, _)| m.contains("Slides")), "{:?}", harness.state().status); - assert!(!path.exists()); - harness.state_mut().dialog.as_mut().unwrap().fields.insert("gifTo".into(), "2".into()); - harness.run_steps(2); - harness.key_press(egui::Key::Enter); - harness.run_steps(2); - assert!(harness.state().dialog.is_none(), "the dialog closes after exporting"); - assert!(suggested.borrow().ends_with(".gif"), "{}", suggested.borrow()); - let bytes = std::fs::read(&path).unwrap(); + let mut h = export_harness(session, files.clone()); + h.run_steps(2); + h.state_mut().run("app.exportDialog", json!({})).unwrap(); + h.run_steps(2); + dialog_method(&mut h, "ui.dialog.set", json!({"dialog": "top", "field": "fmt", "value": "gif"})).unwrap(); + let before = dialog_fields(&h, "export")["gifTo"].clone(); + assert!(dialog_method(&mut h, "ui.dialog.set", json!({"dialog": "top", "field": "gifTo", "value": 9})).is_err()); + assert_eq!(dialog_fields(&h, "export")["gifTo"], before, "a refused field leaves its value alone"); + // Endpoints are inclusive and one-based: 2..1 refuses, while 1..2 exports both slides. + for (field, value) in [("gifLoop", json!(false)), ("gifFrom", json!(2)), ("gifTo", json!(1))] { + dialog_method(&mut h, "ui.dialog.set", json!({"dialog": "top", "field": field, "value": value})).unwrap(); + } + dialog_method(&mut h, "ui.dialog.confirm", json!({"dialog": "top"})).unwrap(); + h.run_steps(4); + assert!(super::open_keys(&h.ctx).iter().any(|key| key == "export")); + assert!(h.query_by_label_contains("Slides:").is_some(), "the form shows the range refusal"); + assert!(files.written().is_empty()); + for (field, value) in [("gifFrom", 1), ("gifTo", 2)] { + dialog_method(&mut h, "ui.dialog.set", json!({"dialog": "top", "field": field, "value": value})).unwrap(); + } + h.run_steps(2); + assert!(h.query_by_label_contains("Slides:").is_none(), "editing clears the inline refusal"); + dialog_method(&mut h, "ui.dialog.confirm", json!({"dialog": "top"})).unwrap(); + h.run_steps(6); + assert!(super::open_keys(&h.ctx).is_empty(), "the dialog closes after exporting"); + let written = files.written(); + assert_eq!(written.len(), 1); + let path = &written[0]; + assert_eq!(path.extension().unwrap(), "gif"); + let bytes = files.file(path).unwrap(); assert!(bytes.starts_with(b"GIF89a")); assert_eq!(u16::from_le_bytes([bytes[6], bytes[7]]), 960, "medium by default"); assert!(!bytes.windows(11).any(|w| w == b"NETSCAPE2.0"), "Loop forever unticked: plays once"); - let _ = std::fs::remove_dir_all(&dir); } -/// The Export… dialog offers "Transparent background" for PNG only, and the picture it -/// writes then leaves the slide background out (#784). +/// The transparent PNG option omits the slide background from the exported pixels. #[test] fn export_dialog_writes_a_png_with_a_transparent_background() { - use egui::vec2; - use egui_kittest::kittest::Queryable as _; - use serde_json::json; - - use crate::SlideApp; - let dir = std::env::temp_dir().join(format!("deckcraft-ui-png-clear-{}", std::process::id())); - std::fs::create_dir_all(&dir).unwrap(); - let path = dir.join("slide.png"); - let picked = path.to_string_lossy().to_string(); - let services = crate::Services { pick_save: Some(Box::new(move |_: &str| Some(picked.clone()))), ..Default::default() }; - let mut harness = egui_kittest::Harness::builder().with_size(vec2(1200.0, 900.0)).build_ui_state( - |ui, app: &mut SlideApp| { - app.logic(ui.ctx()); - app.ui(ui); - }, - // P3(dialogs-w19): sh.A2 - SlideApp::new_base(deckcraft_engine::Session::with_new(), services), - ); - harness.run_steps(2); - harness.state_mut().run("app.exportDialog", json!({})).unwrap(); - harness.run_steps(2); - assert!(harness.query_by_label("Transparent background").is_none(), "PDF has no transparent background"); - harness.get_by_label("JPEG image (current slide)").click_accesskit(); - harness.run_steps(2); - assert!(harness.query_by_label("Transparent background").is_none(), "nor has JPEG"); - harness.get_by_label("PNG images (current slide)").click_accesskit(); - harness.run_steps(2); - harness.get_by_label("Transparent background").click_accesskit(); - harness.run_steps(2); - assert_eq!(harness.state().dialog.as_ref().and_then(|d| d.fields.get("pngTransparent")).map(String::as_str), Some("true")); - harness.key_press(egui::Key::Enter); - harness.run_steps(2); - assert!(harness.state().dialog.is_none(), "the dialog closes after exporting"); - let img = deckcraft_render::decode_image(&std::fs::read(&path).unwrap()).unwrap(); + let files = Arc::new(ScriptedFiles::new().saving_suggested()); + let mut h = export_harness(deckcraft_engine::Session::with_new(), files.clone()); + h.run_steps(2); + h.state_mut().run("app.exportDialog", json!({})).unwrap(); + h.run_steps(2); + dialog_method(&mut h, "ui.dialog.set", json!({"dialog": "top", "field": "fmt", "value": "png"})).unwrap(); + dialog_method(&mut h, "ui.dialog.set", json!({"dialog": "top", "field": "pngTransparent", "value": true})).unwrap(); + dialog_method(&mut h, "ui.dialog.confirm", json!({"dialog": "top"})).unwrap(); + h.run_steps(6); + assert!(super::open_keys(&h.ctx).is_empty(), "the dialog closes after exporting"); + let written = files.written(); + assert_eq!(written.len(), 1); + let img = deckcraft_render::decode_image(&files.file(&written[0]).unwrap()).unwrap(); assert_eq!(img.get_pixel(2, 2).0[3], 0, "the background is left out"); - let _ = std::fs::remove_dir_all(&dir); -} - -#[test] -fn no_link_opens_as_a_new_web_address() { - let seed = seed_from(CurrentLink::None, &[]); - assert_eq!(seed, HyperlinkSeed::blank()); - assert!(hyperlink_should_apply(&seed, "url", "https://", "", 0)); -} - -#[test] -fn web_email_and_slide_links_fill_the_dialog() { - let ids = [SlideId(7), SlideId(9)]; - let web = seed_of_link(&url("https://example.org", "See"), &ids); - assert_eq!(web.mode, "url"); - assert_eq!(web.url, "https://example.org"); - assert_eq!(web.tip, "See"); - assert!(!web.preserve); - assert!(hyperlink_should_apply(&web, "url", "https://example.org", "", 0)); - - let mail = seed_of_link(&url("mailto:a@b.org", "Write"), &ids); - assert_eq!(mail.mode, "email"); - assert_eq!(mail.email, "a@b.org"); - assert_eq!(mail.tip, "Write"); - assert_eq!(hyperlink_command("email", "", "a@b.org", 0, "Write", false)["url"], "mailto:a@b.org"); - - let slide = seed_of_link(&link(Action::Slide { slide: SlideId(9) }, "There"), &ids); - assert_eq!(slide.mode, "slide"); - assert_eq!(slide.slide, 1); - assert_eq!(slide.tip, "There"); - assert!(!slide.preserve); - - let glow = url("https://example.org", "glow"); - assert!(glow.highlight_click); - assert!(seed_of_link(&glow, &ids).highlight); - assert_eq!(hyperlink_command("url", "https://example.org", "", 0, "glow", true)["highlightClick"], true); -} - -#[test] -fn actions_this_dialog_does_not_edit_survive_ok() { - let next = seed_of_link(&link(Action::NextSlide, "On"), &[]); - assert!(next.preserve); - assert_eq!(next.tip, "On"); - assert_eq!(next.url, ""); - assert!(!hyperlink_should_apply(&next, "url", "", "", 0)); - assert!(hyperlink_should_apply(&next, "url", "https://example.org", "", 0)); - - let gone = seed_of_link(&link(Action::Slide { slide: SlideId(99) }, ""), &[SlideId(1)]); - assert!(gone.preserve, "a missing slide is not retargeted to the first slide"); - assert!(!hyperlink_should_apply(&gone, "url", "", "", 0)); - - let mixed = seed_from(CurrentLink::Mixed, &[]); - assert!(mixed.preserve); - assert!(!hyperlink_should_apply(&mixed, "url", "", "", 0)); - assert!(hyperlink_should_apply(&mixed, "email", "", "a@b.org", 0)); } #[test] @@ -230,62 +150,103 @@ fn a_text_span_reports_the_link_it_covers() { body.paragraphs[0].runs.clear(); body.paragraphs[0].runs.push(Run::new("Hello ")); let mut world = Run::new("world"); - world.props.link = Some(url("https://example.org", "tip")); + let address = "https://example.org"; + let tip = "tip"; + world.props.link = Some(url(address, tip)); body.paragraphs[0].runs.push(world); + let slides = [SlideId(1)]; - assert_eq!(link_covering(&body, (0, 6), (0, 6)), CurrentLink::None, "a caret inserts; it does not edit the run beside it"); - match link_covering(&body, (0, 6), (0, 11)) { - CurrentLink::One(h) => assert_eq!(h.tooltip, "tip"), - other => panic!("linked word, got {other:?}"), - } - assert_eq!(link_covering(&body, (0, 0), (0, 11)), CurrentLink::Mixed); + assert_eq!(link_covering(&body, (0, 6), (0, 6), &slides), CurrentLink::None, "a caret inserts; it does not edit the run beside it"); + assert_eq!( + link_covering(&body, (0, 6), (0, 11), &slides), + CurrentLink::One(UiHyperlink::new(LinkTarget::Url(address.into())).tooltip(tip)), + ); + assert_eq!(link_covering(&body, (0, 0), (0, 11), &slides), CurrentLink::Mixed); - let mut other = url("https://example.org", "tip"); - other.tooltip = "other".into(); - assert_eq!(common_click([None, None]), CurrentLink::None); - assert!(matches!(common_click([Some(url("https://example.org", "tip"))]), CurrentLink::One(_))); - assert_eq!(common_click([Some(url("https://example.org", "tip")), Some(other)]), CurrentLink::Mixed); - assert_eq!(common_click(std::iter::empty()), CurrentLink::None); + let unlinked = Shape::default(); + let linked = Shape { click: Some(url(address, tip)), ..Default::default() }; + let other = Shape { click: Some(url(address, "other")), ..Default::default() }; + // PLAN: assumed common_click<'a>(impl IntoIterator, &[SlideId]) -> CurrentLink (dialogs-w9) + assert_eq!(common_click([&unlinked, &unlinked], &slides), CurrentLink::None); + assert_eq!(common_click([&linked], &slides), CurrentLink::One(UiHyperlink::new(LinkTarget::Url(address.into())).tooltip(tip))); + assert_eq!(common_click([&linked, &other], &slides), CurrentLink::Mixed); + assert_eq!(common_click(std::iter::empty::<&Shape>(), &slides), CurrentLink::None); } #[test] fn the_open_dialog_reads_the_caret_word_and_the_shape_click() { + let address = "https://example.org"; + let tip = "tip"; let mut doc = Presentation::default(); let shape_id = { - let slide = std::sync::Arc::make_mut(&mut doc.slides[0]); + let slide = Arc::make_mut(&mut doc.slides[0]); let mut body = TextBody::from_text("Hello world"); body.paragraphs[0].runs.clear(); body.paragraphs[0].runs.push(Run::new("Hello ")); let mut world = Run::new("world"); - world.props.link = Some(url("https://example.org", "tip")); + world.props.link = Some(url(address, tip)); body.paragraphs[0].runs.push(world); slide.shapes[0].text = Some(body); slide.shapes[0].id }; let mut st = deckcraft_engine::DocState::new(doc, None, "t".into()); st.selection.text = Some(deckcraft_engine::TextSel { shape: shape_id, anchor: (0, 8), caret: (0, 8), ..Default::default() }); - let seed = hyperlink_seed_of(Some(&st)); - assert_eq!(seed.mode, "url"); - assert_eq!(seed.url, "https://example.org"); - assert_eq!(seed.tip, "tip"); - assert!(!seed.preserve); + assert_eq!(current_link_of(Some(&st)), CurrentLink::One(UiHyperlink::new(LinkTarget::Url(address.into())).tooltip(tip))); st.selection.text = None; st.selection.shapes = vec![shape_id]; + let shape_address = "https://shape.example"; + let shape_tip = "shape tip"; { - let doc = std::sync::Arc::make_mut(&mut st.doc); - let slide = std::sync::Arc::make_mut(&mut doc.slides[0]); + let doc = Arc::make_mut(&mut st.doc); + let slide = Arc::make_mut(&mut doc.slides[0]); let shape = slide.shapes.iter_mut().find(|sh| sh.id == shape_id).unwrap(); - let mut click = url("https://shape.example", "shape tip"); + let mut click = url(shape_address, shape_tip); click.highlight_click = true; shape.click = Some(click); } - let seed = hyperlink_seed_of(Some(&st)); - assert_eq!(seed.url, "https://shape.example"); - assert_eq!(seed.tip, "shape tip"); - assert!(seed.highlight); + let mut expected = UiHyperlink::new(LinkTarget::Url(shape_address.into())).tooltip(shape_tip); + expected.highlight_click = true; + assert_eq!(current_link_of(Some(&st)), CurrentLink::One(expected)); st.selection.text = Some(deckcraft_engine::TextSel { shape: shape_id, anchor: (0, 11), caret: (0, 11), ..Default::default() }); - let seed = hyperlink_seed_of(Some(&st)); - assert_eq!(seed, HyperlinkSeed::blank(), "a caret at the end of the word inserts a new link"); + assert_eq!(current_link_of(Some(&st)), CurrentLink::None, "a caret at the end of the word inserts a new link"); + st.selection.text = None; + for action in [Action::NextSlide, Action::Slide { slide: SlideId(99) }] { + let doc = Arc::make_mut(&mut st.doc); + let slide = Arc::make_mut(&mut doc.slides[0]); + slide.shapes.iter_mut().find(|sh| sh.id == shape_id).unwrap().click = Some(link(action, tip)); + assert_eq!(current_link_of(Some(&st)), CurrentLink::Other, "an unsupported target is not retargeted to the first slide"); + } + + let mut session = deckcraft_engine::Session::new(); + session.add_document(st); + session.execute("slide.new", &json!({})).unwrap(); + session.execute("slide.new", &json!({})).unwrap(); + let st = session.active_mut().unwrap(); + st.selection.slide = 0; + st.selection.slides = vec![st.doc.slides[0].id]; + st.selection.shapes = vec![shape_id]; + let third = st.doc.slides[2].id; + let mut h = crate::test_harness::harness(session, Services::default()); + // The sole Slide Titles group has places 0, 1, 2; Top and its heading consume no place index. + for (action, mode, field, value) in [ + (Action::Url { url: shape_address.into() }, "url", "url", json!(shape_address)), + (Action::Slide { slide: third }, "place", "place", json!(2)), + ] { + { + let st = h.state_mut().session.active_mut().unwrap(); + let doc = Arc::make_mut(&mut st.doc); + let slide = Arc::make_mut(&mut doc.slides[0]); + slide.shapes.iter_mut().find(|sh| sh.id == shape_id).unwrap().click = Some(link(action, shape_tip)); + } + h.state_mut().open_dialog("hyperlink", Value::Null); + h.run_steps(3); + let fields = dialog_fields(&h, "hyperlink"); + assert_eq!(fields["mode"], mode); + assert_eq!(fields[field], value); + dialog_method(&mut h, "ui.dialog.cancel", json!({"dialog": "top"})).unwrap(); + h.run_steps(3); + assert!(super::open_keys(&h.ctx).is_empty()); + } } diff --git a/crates/ui-egui/src/units_tests.rs b/crates/ui-egui/src/units_tests.rs index 96d54b67a..a2e3074ba 100644 --- a/crates/ui-egui/src/units_tests.rs +++ b/crates/ui-egui/src/units_tests.rs @@ -4,36 +4,48 @@ use egui::accesskit::Role; use egui_kittest::Harness; -use egui_kittest::kittest::Queryable; +use egui_kittest::kittest::{NodeT as _, Queryable}; use serde_json::json; use crate::{Services, SlideApp}; use deckcraft_engine::Session; use deckcraft_geom::Unit; -fn harness(app: SlideApp) -> Harness<'static, SlideApp> { - Harness::builder().with_size(egui::vec2(1400.0, 900.0)).build_ui_state( - |ui, app: &mut SlideApp| { - app.logic(ui.ctx()); - app.ui(ui); - }, - app, - ) +fn harness(session: Session) -> Harness<'static, SlideApp> { + crate::test_harness::harness(session, Services::default()) } /// A blank slide with one 2" x 1.5" rectangle at (1", 0.5"), selected. -fn app_with_shape(units: Unit) -> SlideApp { +fn app_with_shape(units: Unit) -> Harness<'static, SlideApp> { let mut s = Session::with_new(); s.execute("slide.new", &json!({"layout": "blank"})).unwrap(); let id = s.execute("shape.insert", &json!({"preset": "rect", "rect": [72.0, 36.0, 144.0, 108.0]})).unwrap()["id"].as_u64().unwrap(); s.execute("edit.select", &json!({"ids": [id]})).unwrap(); s.prefs.units = units; - // P3(dialogs-w19): sh.A2 - SlideApp::new_base(s, Services::default()) + harness(s) } fn text_values(h: &Harness<'_, SlideApp>) -> Vec { - h.query_all_by_role(Role::TextInput).filter_map(|n| n.value()).collect() + h.query_all_by_role(Role::SpinButton).filter_map(|n| n.value()).collect() +} + +fn shown_number(h: &Harness<'_, SlideApp>, name: &str) -> f64 { + h.get_by_role_and_label(Role::SpinButton, name).accesskit_node().numeric_value().unwrap() +} + +fn dialog_method(h: &mut Harness<'_, SlideApp>, method: &str, params: serde_json::Value) -> serde_json::Value { + let ctx = h.ctx.clone(); + crate::dialogs::control(h.state_mut(), &ctx, method, ¶ms).unwrap().unwrap() +} + +fn dialog_fields(h: &Harness<'_, SlideApp>, key: &str) -> serde_json::Value { + crate::control::inspect(h.state(), &h.ctx)["dialogs"] + .as_array() + .unwrap() + .iter() + .find(|d| d["key"] == key) + .unwrap()["fields"] + .clone() } fn selected_xfrm(app: &SlideApp) -> deckcraft_geom::Xfrm { @@ -44,87 +56,75 @@ fn selected_xfrm(app: &SlideApp) -> deckcraft_geom::Xfrm { #[test] fn the_format_pane_shows_and_takes_centimetres() { - let mut app = app_with_shape(Unit::Centimeters); + let mut h = app_with_shape(Unit::Centimeters); + let app = h.state_mut(); app.ui.pane = Some("format".into()); app.ui.format_tab = "size".into(); app.ui.ruler = true; - app.ui.tab = "Shape Format".into(); - let mut h = harness(app); - h.run_steps(3); - h.state_mut().ui.tab = "Home".into(); h.run_steps(3); let shown = text_values(&h); - // 108 pt = 1.5" = 3.81 cm; 144 pt = 5.08 cm; 72 pt = 2.54 cm; 36 pt = 1.27 cm. - for v in ["3.81 cm", "5.08 cm", "2.54 cm", "1.27 cm"] { - assert!(shown.iter().any(|s| s == v), "{v} in {shown:?}"); - } - assert!(shown.iter().all(|s| !s.ends_with('"')), "no inches left: {shown:?}"); - // Typing 2.54 into Width makes the shape one inch wide, not 2.54 inches. - let width = h.query_all_by_role(Role::TextInput).find(|n| n.value().as_deref() == Some("5.08 cm")).unwrap(); - width.focus(); - h.run_steps(1); - h.key_press_modifiers(egui::Modifiers::COMMAND, egui::Key::A); - h.query_all_by_role(Role::TextInput).find(|n| n.is_focused()).unwrap().type_text("2.54"); - h.run_steps(1); - h.key_press(egui::Key::Enter); - h.run_steps(3); + let width = h.get_by_role_and_label(Role::SpinButton, "Width").value().unwrap(); + assert!(width.ends_with("cm"), "{width}"); + assert!((shown_number(&h, "Width") - 5.08).abs() < 1e-6); + assert!(shown.iter().all(|s| !s.ends_with('"') && !s.ends_with("in")), "{shown:?}"); + let before = selected_xfrm(h.state()); + type_into(&mut h, "Width", "2.54"); let x = selected_xfrm(h.state()); assert!((x.w - 72.0).abs() < 1e-6, "{x:?}"); - assert!((x.h - 108.0).abs() < 1e-6, "the height is untouched: {x:?}"); + assert_eq!(x.h, before.h, "the height is untouched"); } #[test] fn the_format_pane_keeps_inches_by_default() { - let mut app = app_with_shape(Unit::default()); + let mut h = app_with_shape(Unit::default()); + let app = h.state_mut(); app.ui.pane = Some("format".into()); app.ui.format_tab = "size".into(); - let mut h = harness(app); h.run_steps(3); let shown = text_values(&h); - assert!(shown.iter().any(|s| s == "2\""), "{shown:?}"); + let width = h.get_by_role_and_label(Role::SpinButton, "Width").value().unwrap(); + assert!(width.ends_with('"') || width.ends_with("in"), "{width}"); + assert_eq!(shown_number(&h, "Width"), 2.0); assert!(shown.iter().all(|s| !s.ends_with("cm")), "{shown:?}"); } /// Shape Format › Size on the ribbon: the same unit as the pane (the pane stays closed here). #[test] fn the_ribbon_size_boxes_show_and_take_centimetres() { - let mut app = app_with_shape(Unit::Centimeters); - app.ui.tab = "Shape Format".into(); - let mut h = harness(app); - h.run_steps(3); - let shown = text_values(&h); - assert!(shown.iter().any(|s| s == "3.81 cm") && shown.iter().any(|s| s == "5.08 cm"), "{shown:?}"); - let height = h.query_all_by_role(Role::TextInput).find(|n| n.value().as_deref() == Some("3.81 cm")).unwrap(); - height.focus(); - h.run_steps(1); - h.key_press_modifiers(egui::Modifiers::COMMAND, egui::Key::A); - h.query_all_by_role(Role::TextInput).find(|n| n.is_focused()).unwrap().type_text("1.27"); - h.run_steps(1); - h.key_press(egui::Key::Enter); + let mut h = app_with_shape(Unit::Centimeters); + h.state_mut().ui.tab = "Shape Format".into(); h.run_steps(3); + for (name, value) in [("Height", 3.81), ("Width", 5.08)] { + let shown = h.get_by_role_and_label(Role::SpinButton, name).value().unwrap(); + assert!(shown.ends_with("cm"), "{name}: {shown}"); + assert!((shown_number(&h, name) - value).abs() < 1e-6); + } + let before = selected_xfrm(h.state()); + type_into(&mut h, "Height", "1.27"); let x = selected_xfrm(h.state()); - assert!((x.h - 36.0).abs() < 1e-6 && (x.w - 144.0).abs() < 1e-6, "{x:?}"); + assert!((x.h - 36.0).abs() < 1e-6, "{x:?}"); + assert_eq!(x.w, before.w); } #[test] fn the_slide_size_dialog_shows_and_takes_centimetres() { - let mut app = app_with_shape(Unit::Centimeters); - app.dialog = Some(crate::dialogs::Dialog::new("slideSize")); - let mut h = harness(app); - h.run_steps(3); - assert!(h.query_by_label("Width (cm):").is_some() && h.query_by_label("Height (cm):").is_some()); - assert!(h.query_by_label("Width (in):").is_none()); - // 960 x 540 pt = 33.87 x 19.05 cm. - let shown = text_values(&h); - assert!(shown.iter().any(|s| s == "33.87") && shown.iter().any(|s| s == "19.05"), "{shown:?}"); - // A 4:3 size typed in centimetres: 25.4 x 19.05 cm = 10" x 7.5" = 720 x 540 pt. - let d = h.state_mut().dialog.as_mut().unwrap(); - d.fields.insert("w".into(), "25.4".into()); - h.run_steps(1); - h.get_by_label("OK").click(); - h.run_steps(3); + let mut h = app_with_shape(Unit::Centimeters); let size = h.state().session.active().unwrap().doc.slide_size; - assert!((size.width - 720.0).abs() < 1e-6 && (size.height - 540.0).abs() < 1e-6, "{size:?}"); + h.state_mut().open_dialog("slideSize", serde_json::Value::Null); + h.run_steps(3); + let fields = dialog_fields(&h, "slideSize"); + assert_eq!(fields["w"], size.width); + let shown = h.get_by_role_and_label(Role::SpinButton, "Width").value().unwrap(); + assert!(shown.ends_with("cm"), "{shown}"); + assert!((shown_number(&h, "Width") - size.width * 2.54 / 72.0).abs() < 1e-6); + assert!(!shown.ends_with('"') && !shown.ends_with("in")); + dialog_method(&mut h, "ui.dialog.set", json!({"dialog": "top", "field": "w", "value": 720.0})); + dialog_method(&mut h, "ui.dialog.confirm", json!({"dialog": "top"})); + h.run_steps(3); + let after = h.state().session.active().unwrap().doc.slide_size; + assert_eq!(after.width, 720.0); + assert_eq!(after.height, size.height); + assert!(crate::dialogs::open_keys(&h.ctx).is_empty()); } /// OK with the shown values untouched keeps the exact size: 13.333" is shown rounded to 33.87 cm, @@ -132,29 +132,26 @@ fn the_slide_size_dialog_shows_and_takes_centimetres() { #[test] fn ok_in_the_slide_size_dialog_without_edits_keeps_the_size() { for units in [Unit::Centimeters, Unit::Inches] { - let mut app = app_with_shape(units); - app.dialog = Some(crate::dialogs::Dialog::new("slideSize")); - let mut h = harness(app); + let mut h = app_with_shape(units); + let before = h.state().session.active().unwrap().doc.slide_size; + h.state_mut().open_dialog("slideSize", serde_json::Value::Null); h.run_steps(3); - h.get_by_label("OK").click(); - h.run_steps(3); - let size = h.state().session.active().unwrap().doc.slide_size; - assert_eq!((size.width, size.height), (960.0, 540.0), "{units:?}"); + assert_eq!(ok_and_size(&mut h), (before.width, before.height), "{units:?}"); } } fn slide_size_dialog(units: Unit, w: f64, h: f64) -> Harness<'static, SlideApp> { - let mut app = app_with_shape(units); - app.session.execute("design.slideSize", &json!({"w": w, "h": h, "scale": "none"})).unwrap(); - app.dialog = Some(crate::dialogs::Dialog::new("slideSize")); - let mut h = harness(app); - h.run_steps(3); - h + let mut trial = app_with_shape(units); + trial.state_mut().session.execute("design.slideSize", &json!({"w": w, "h": h, "scale": "none"})).unwrap(); + trial.state_mut().open_dialog("slideSize", serde_json::Value::Null); + trial.run_steps(3); + trial } fn ok_and_size(h: &mut Harness<'_, SlideApp>) -> (f64, f64) { - h.get_by_label("OK").click(); + dialog_method(h, "ui.dialog.confirm", json!({"dialog": "top"})); h.run_steps(3); + assert!(crate::dialogs::open_keys(&h.ctx).is_empty()); let size = h.state().session.active().unwrap().doc.slide_size; (size.width, size.height) } @@ -165,59 +162,34 @@ fn ok_and_size(h: &mut Harness<'_, SlideApp>) -> (f64, f64) { fn a_slide_size_preset_gives_its_exact_size() { for (units, label, size) in [ (Unit::Centimeters, "Widescreen (16:9)", (960.0, 540.0)), - (Unit::Inches, "Widescreen (16:9)", (960.0, 540.0)), (Unit::Inches, "A4 Paper (210x297 mm)", (780.0, 540.0)), ] { let mut h = slide_size_dialog(units, 720.0, 540.0); - h.get_by_role_and_label(Role::ComboBox, "Slides sized for:").click(); - h.run_steps(2); - h.get_by_label(label).click(); - h.run_steps(2); + // PLAN: assumed preset: SLIDE_SIZES label (dialogs-w8) + dialog_method(&mut h, "ui.dialog.set", json!({"dialog": "top", "field": "preset", "value": label})); assert_eq!(ok_and_size(&mut h), size, "{units:?} {label}"); } } -/// Portrait swaps the exact width and height. -#[test] -fn portrait_swaps_the_exact_slide_size() { - for units in [Unit::Centimeters, Unit::Inches] { - let mut h = slide_size_dialog(units, 960.0, 540.0); - h.get_by_label("Portrait").click(); - h.run_steps(2); - assert_eq!(ok_and_size(&mut h), (540.0, 960.0), "{units:?}"); - } -} - #[test] fn preferences_choose_the_units_and_cancel_puts_them_back() { - let mut app = app_with_shape(Unit::Inches); - app.dialog = Some(crate::dialogs::Dialog::new("preferences")); - let mut h = harness(app); + let mut h = app_with_shape(Unit::Inches); + h.state_mut().open_dialog("preferences", serde_json::Value::Null); h.run_steps(3); - h.get_by_label("Inches").click(); - h.run_steps(2); - h.get_by_label("Centimeters").click(); + dialog_method(&mut h, "ui.dialog.set", json!({"dialog": "top", "field": "units", "value": Unit::Centimeters})); h.run_steps(2); assert_eq!(h.state().session.prefs.units, Unit::Centimeters); - h.query_all_by_role_and_label(Role::Button, "Cancel").next().unwrap().click_accesskit(); + dialog_method(&mut h, "ui.dialog.cancel", json!({"dialog": "top"})); h.run_steps(2); assert_eq!(h.state().session.prefs.units, Unit::Inches); + assert!(crate::dialogs::open_keys(&h.ctx).is_empty()); } -#[test] -fn the_ruler_counts_centimetres_in_halves_and_inches_in_quarters() { - assert_eq!(crate::canvas::ruler_steps(Unit::Inches), (72.0, 4)); - let (cm, sub) = crate::canvas::ruler_steps(Unit::Centimeters); - assert!((cm - 72.0 / 2.54).abs() < 1e-9 && sub == 2, "{cm} {sub}"); - // A hand-edited prefs.json can say points: the ruler stays in inches rather than a tick per point. - assert_eq!(crate::canvas::ruler_steps(Unit::Points), (72.0, 4)); -} - -fn type_into(h: &mut Harness<'_, SlideApp>, shown: &str, text: &str) { - h.query_all_by_role(Role::TextInput).rfind(|n| n.value().as_deref() == Some(shown)).unwrap().focus(); +fn type_into(h: &mut Harness<'_, SlideApp>, name: &str, text: &str) { + h.get_by_role_and_label(Role::SpinButton, name).focus(); h.run_steps(1); h.key_press_modifiers(egui::Modifiers::COMMAND, egui::Key::A); - h.query_all_by_role(Role::TextInput).find(|n| n.is_focused()).unwrap().type_text(text); + h.get_by_role_and_label(Role::SpinButton, name).type_text(text); h.run_steps(1); h.key_press(egui::Key::Enter); h.run_steps(3); @@ -226,27 +198,30 @@ fn type_into(h: &mut Harness<'_, SlideApp>, shown: &str, text: &str) { /// Format Shape › Size & Properties: Scale Width/Height and the From origin of Position. #[test] fn the_format_pane_scales_and_positions_from_the_centre() { - let mut app = app_with_shape(Unit::default()); + let mut h = app_with_shape(Unit::default()); + let app = h.state_mut(); app.ui.pane = Some("format".into()); app.ui.format_tab = "size".into(); - let mut h = harness(app); h.run_steps(3); - let shown = text_values(&h); - assert_eq!(shown.iter().filter(|s| *s == "100%").count(), 2, "{shown:?}"); - // 50% Scale Width (the last "100%" row) halves the 144 pt width. - type_into(&mut h, "100%", "50"); + let base = selected_xfrm(h.state()); + type_into(&mut h, "Scale Width", "50"); let x = selected_xfrm(h.state()); - assert!((x.w - 72.0).abs() < 1e-6 && (x.h - 108.0).abs() < 1e-6, "{x:?}"); - assert!(text_values(&h).iter().any(|s| s == "50%")); - // From Center: the fields read the middle of the box, and typing moves the middle. + assert_eq!(x.w, base.w / 2.0); + assert_eq!(x.h, base.h); + type_into(&mut h, "Scale Width", "75"); + type_into(&mut h, "Scale Height", "50"); + let scaled = selected_xfrm(h.state()); + assert_eq!(scaled.w, base.w * 0.75, "scale uses the captured base"); + assert_eq!(scaled.h, base.h / 2.0); h.get_by_role_and_label(Role::ComboBox, "From").click(); h.run_steps(2); h.get_by_label("Center").click(); h.run_steps(3); - // Box is now (72, 36, 72, 108): its centre is (108, 90) pt = (1.5", 1.25"). - let shown = text_values(&h); - assert!(shown.iter().any(|s| s == "1.5\"") && shown.iter().any(|s| s == "1.25\""), "{shown:?}"); - type_into(&mut h, "1.5\"", "3"); + assert_eq!(selected_xfrm(h.state()), scaled, "choosing an origin does not move the box"); + type_into(&mut h, "Horizontal", "3"); + type_into(&mut h, "Vertical", "2"); let x = selected_xfrm(h.state()); - assert!((x.x - 180.0).abs() < 1e-6 && (x.w - 72.0).abs() < 1e-6 && (x.y - 36.0).abs() < 1e-6, "{x:?}"); + assert_eq!(x.x + x.w / 2.0, 3.0 * 72.0); + assert_eq!(x.y + x.h / 2.0, 2.0 * 72.0); + assert_eq!((x.w, x.h), (scaled.w, scaled.h)); }