|
| 1 | +use egui_kittest::Harness; |
| 2 | +use egui_kittest::kittest::Queryable; |
| 3 | +use pdfcraft_engine::{DocId, Edit, NewAnnotation, Shape, SignaturePlacementSize, Style}; |
| 4 | + |
| 5 | +use crate::PdfCraftApp; |
| 6 | +use crate::fill_sign::{SavedSig, SignatureSizeRequest}; |
| 7 | + |
| 8 | +fn add_document(app: &mut PdfCraftApp) -> DocId { |
| 9 | + let bytes = app.session.create_blank(300.0, 400.0, 1).unwrap(); |
| 10 | + app.open_bytes("signature-request.pdf", None, bytes.as_ref().clone()).unwrap(); |
| 11 | + let id = app.views[app.active.unwrap()].id; |
| 12 | + for signature in [true, true, false] { |
| 13 | + let shape = if signature { |
| 14 | + pdfcraft_engine::typed_signature_shape([30.0, 200.0], "Synthetic User", 32.0, 0).unwrap() |
| 15 | + } else { |
| 16 | + Shape::Rectangle { rect: [30.0, 180.0, 100.0, 220.0] } |
| 17 | + }; |
| 18 | + app.session |
| 19 | + .apply( |
| 20 | + id, |
| 21 | + Edit::AddAnnotation(NewAnnotation { |
| 22 | + page: 0, |
| 23 | + style: Style::default_for(&shape), |
| 24 | + shape, |
| 25 | + contents: String::new(), |
| 26 | + author: String::new(), |
| 27 | + }), |
| 28 | + ) |
| 29 | + .unwrap(); |
| 30 | + } |
| 31 | + app.views[app.active.unwrap()].comments.selected = Some((0, 0)); |
| 32 | + id |
| 33 | +} |
| 34 | + |
| 35 | +fn app() -> (PdfCraftApp, DocId) { |
| 36 | + let mut app = PdfCraftApp::new(); |
| 37 | + app.set_option("language", "en").unwrap(); |
| 38 | + app.signature = Some(SavedSig::Typed("Synthetic User".into())); |
| 39 | + app.signature_placement_size = Some(SignaturePlacementSize::new(70.0, 16.0).unwrap()); |
| 40 | + let id = add_document(&mut app); |
| 41 | + (app, id) |
| 42 | +} |
| 43 | + |
| 44 | +fn paint_menu(ui: &mut egui::Ui, app: &mut PdfCraftApp) { |
| 45 | + let view = &mut app.views[app.active.unwrap()]; |
| 46 | + let doc = app.session.get(view.id).unwrap(); |
| 47 | + crate::comments::signature_size_items(ui, view, doc, app.signature.is_some(), app.initials.is_some()); |
| 48 | +} |
| 49 | + |
| 50 | +fn cache_menu(ctx: &egui::Context, app: &mut PdfCraftApp) { |
| 51 | + let mut output = ctx.run_ui(egui::RawInput::default(), |ui| paint_menu(ui, app)); |
| 52 | + output.textures_delta.clear(); |
| 53 | +} |
| 54 | + |
| 55 | +#[derive(Debug, PartialEq)] |
| 56 | +struct DocumentState { |
| 57 | + id: DocId, |
| 58 | + bytes: Vec<u8>, |
| 59 | + dirty: bool, |
| 60 | + undo: Option<String>, |
| 61 | + redo: Option<String>, |
| 62 | +} |
| 63 | + |
| 64 | +fn documents(app: &PdfCraftApp) -> Vec<DocumentState> { |
| 65 | + app.session |
| 66 | + .docs() |
| 67 | + .iter() |
| 68 | + .map(|doc| DocumentState { |
| 69 | + id: doc.id, |
| 70 | + bytes: app.session.save_bytes(doc.id).unwrap().as_ref().clone(), |
| 71 | + dirty: doc.dirty, |
| 72 | + undo: doc.can_undo().map(str::to_owned), |
| 73 | + redo: doc.can_redo().map(str::to_owned), |
| 74 | + }) |
| 75 | + .collect() |
| 76 | +} |
| 77 | + |
| 78 | +#[test] |
| 79 | +fn signature_size_request_rejects_stale_identity_and_removed_target_and_always_clears_pending() { |
| 80 | + for change in 0..5 { |
| 81 | + let (mut app, id) = app(); |
| 82 | + let request = SignatureSizeRequest { doc: id, page: 0, annotation: 0, initials: false }; |
| 83 | + let ctx = egui::Context::default(); |
| 84 | + cache_menu(&ctx, &mut app); |
| 85 | + match change { |
| 86 | + 0 => { |
| 87 | + add_document(&mut app); |
| 88 | + } |
| 89 | + 1 => app.views[0].comments.selected = Some((0, 1)), |
| 90 | + 2 => app.signature = None, |
| 91 | + 3 => app.views[0].comments.selected = None, |
| 92 | + _ => app.active = None, |
| 93 | + } |
| 94 | + let before = documents(&app); |
| 95 | + let preferences = (app.signature_placement_size, app.initials_placement_size); |
| 96 | + assert!(!app.dispatch_signature_size_request(request), "stale request case {change} must be refused"); |
| 97 | + assert_eq!((app.signature_placement_size, app.initials_placement_size), preferences); |
| 98 | + assert_eq!(documents(&app), before); |
| 99 | + assert!(app.pending_signature_size_request.is_none(), "early refusal must not leave a request that affects a later command"); |
| 100 | + } |
| 101 | +} |
| 102 | + |
| 103 | +#[test] |
| 104 | +fn signature_size_request_command_freshly_reads_resized_source_and_refuses_deleted_source_after_menu_cache() { |
| 105 | + let (mut app, id) = app(); |
| 106 | + let ctx = egui::Context::default(); |
| 107 | + cache_menu(&ctx, &mut app); |
| 108 | + app.session.apply(id, Edit::ResizeAnnotation { page: 0, index: 0, rect: [30.0, 180.0, 100.0, 197.0] }).unwrap(); |
| 109 | + let size = app.session.get(id).unwrap().signature_placement_size(0, 0).unwrap().unwrap(); |
| 110 | + let before = documents(&app); |
| 111 | + assert!(app.execute("sign.fill.signature.size")); |
| 112 | + assert_eq!(app.signature_placement_size, Some(size), "execution reads the current geometry rather than a cached placement size"); |
| 113 | + assert_eq!(documents(&app), before); |
| 114 | + assert!(app.pending_signature_size_request.is_none()); |
| 115 | + |
| 116 | + cache_menu(&ctx, &mut app); |
| 117 | + // Remove both suitable sources so the ordinary rectangle reuses the cached source index. |
| 118 | + for _ in 0..2 { |
| 119 | + app.session.apply(id, Edit::DeleteAnnotation { page: 0, index: 0 }).unwrap(); |
| 120 | + } |
| 121 | + let before = documents(&app); |
| 122 | + assert!(!app.execute("sign.fill.signature.size")); |
| 123 | + assert_eq!(app.signature_placement_size, Some(size)); |
| 124 | + assert_eq!(documents(&app), before); |
| 125 | + assert!(app.pending_signature_size_request.is_none()); |
| 126 | +} |
| 127 | + |
| 128 | +#[test] |
| 129 | +fn signature_size_menu_cache_actual_items_update_across_frames_source_changes_and_target_changes() { |
| 130 | + let (app, id) = app(); |
| 131 | + let mut h = Harness::builder().with_size(egui::vec2(600.0, 300.0)).build_ui_state(paint_menu, app); |
| 132 | + h.run_steps(4); |
| 133 | + assert!(h.query_by_label("Use this size for saved signature").is_some()); |
| 134 | + assert!(h.query_by_label("Use this size for saved initials").is_none()); |
| 135 | + h.state_mut().views[0].comments.selected = Some((0, 2)); |
| 136 | + h.run_steps(3); |
| 137 | + assert!(h.query_by_label("Use this size for saved signature").is_none(), "changing selection invalidates the previous positive result"); |
| 138 | + h.state_mut().views[0].comments.selected = Some((0, 0)); |
| 139 | + h.run_steps(3); |
| 140 | + assert!(h.query_by_label("Use this size for saved signature").is_some()); |
| 141 | + for _ in 0..2 { |
| 142 | + h.state_mut().session.apply(id, Edit::DeleteAnnotation { page: 0, index: 0 }).unwrap(); |
| 143 | + } |
| 144 | + h.run_steps(3); |
| 145 | + assert!(h.query_by_label("Use this size for saved signature").is_none(), "an ordinary source at the same index is not eligible"); |
| 146 | + h.state_mut().session.undo(id).unwrap(); |
| 147 | + h.run_steps(3); |
| 148 | + assert!(h.query_by_label("Use this size for saved signature").is_some()); |
| 149 | + h.state_mut().signature = None; |
| 150 | + h.run_steps(3); |
| 151 | + assert!(h.query_by_label("Use this size for saved signature").is_none()); |
| 152 | + h.state_mut().initials = Some(SavedSig::Typed("SU".into())); |
| 153 | + h.run_steps(3); |
| 154 | + h.get_by_label("Use this size for saved initials").click(); |
| 155 | + h.run_steps(2); |
| 156 | + let request = h.state_mut().views[0].comments.signature_size_request.take().unwrap(); |
| 157 | + assert_eq!(request, SignatureSizeRequest { doc: id, page: 0, annotation: 0, initials: true }); |
| 158 | + let before = documents(h.state()); |
| 159 | + assert!(h.state_mut().dispatch_signature_size_request(request)); |
| 160 | + assert!(h.state().initials_placement_size.is_some()); |
| 161 | + assert_eq!(documents(h.state()), before); |
| 162 | + assert!(h.state().pending_signature_size_request.is_none()); |
| 163 | +} |
0 commit comments