mirror of
https://github.com/storytold/pdfcraft.git
synced 2026-10-24 10:08:00 -06:00
* Tests: UI tests wait for background work by time, not frame count (#4186) A frame count is not a time: on a busy runner the worker had not finished the open, or the page text had not arrived, within 30 frames or 50 hover rounds. support::step_until (#[must_use]) steps frames until a condition holds or 30 s pass, sleeping briefly between frames, and says whether it held; each test asserts on that with its own message. - control_open_replies_pending_while_the_worker_opens_and_focuses_once_it_has and mode_tabs_keep_the_hand_over_page_text_scrolled_under_them wait with it, and so does control.rs call() for its reply. - support::settle waits for every pending render and fails if the pages are not rendered within 30 s. It replaces all the render-pending settle loops (40 to 3000 frames) and the local settle/wait_for_pages wrappers, except text_select's double-click wait, which is held to 8 frames on purpose to stay inside egui's multi-click window, and view.rs's loops that already wait by a deadline. - wait_until is built on step_until, keeping its diagnostic message. - tabbing_back_to_the_same_field_then_saving_keeps_the_committed_value already waited 30 s for its file. It failed because its "same-frame" case shared its scratch directory with typing_and_saving_in_the_same_frame_saves_the_last_keystrokes, which runs in the same test process (sinceee1ee704, #1045) and deleted the directory mid-save. forms' and pickers' scratch() now give every call a directory of its own. Fixes #4186 * M14: update ctutils to 0.4.5 for RUSTSEC-2026-0385 The dependency-advisory gate rejects ctutils 0.4.2 (non-constant-time mask generation and u128 comparisons, fixed in 0.4.4). It comes in through crypto-bigint for the signing crates. Only its version and checksum change in Cargo.lock. (cherry picked from commit1f8811761a)
159 lines
7.2 KiB
Rust
159 lines
7.2 KiB
Rust
//! Scan & OCR ▸ Recognize text in the real shell (egui_kittest): the dialog, the run and its
|
|
//! result (skipped without the OCR models: `cargo xtask models`).
|
|
|
|
use egui_kittest::Harness;
|
|
use egui_kittest::kittest::Queryable;
|
|
use pdfcraft_engine::{AddedText, Edit, Session, export};
|
|
use pdfcraft_ui_egui::PdfCraftApp;
|
|
|
|
/// A one-page PDF that is only a picture of a sentence.
|
|
fn scan() -> Vec<u8> {
|
|
let mut s = Session::new();
|
|
let text = s.create_from_text("t", "Recognize these scanned words").unwrap();
|
|
let id = s.open("text.pdf", None, text, None).unwrap();
|
|
let png = export::Exporter::new(s.get(id).unwrap()).png(0, 150.0).unwrap();
|
|
s.create_from_images(&[("scan.png".into(), png)]).unwrap().to_vec()
|
|
}
|
|
|
|
#[test]
|
|
fn recognize_text_dialog_adds_searchable_text() {
|
|
let mut h = Harness::builder().with_size(egui::vec2(1400.0, 900.0)).build_eframe(|_cc| {
|
|
let mut app = PdfCraftApp::new();
|
|
app.set_option("language", "en").unwrap();
|
|
app.open_bytes("scan.pdf", None, scan()).unwrap();
|
|
app.run_inline = true;
|
|
app
|
|
});
|
|
h.run_steps(4);
|
|
assert!(h.state_mut().execute("ocr.recognize"));
|
|
h.run_steps(2);
|
|
h.get_by_label("Document language");
|
|
h.get_by_label("Output");
|
|
if !pdfcraft_engine::ocr::available() {
|
|
eprintln!("skipped: OCR models not installed");
|
|
return;
|
|
}
|
|
h.get_by_label("Recognize text").click();
|
|
h.run_steps(3);
|
|
assert!(h.state().ocr_run.is_none(), "finished");
|
|
let toast = h.state().toast.clone().unwrap().0;
|
|
assert!(toast.starts_with("Recognized ") && toast.contains("on 1 page"), "{toast}");
|
|
let app = h.state();
|
|
let id = app.active_ids().unwrap().1;
|
|
assert_eq!(app.session.get(id).unwrap().can_undo(), Some("Recognize text"));
|
|
}
|
|
|
|
#[test]
|
|
fn recognize_text_in_multiple_files_writes_searchable_copies() {
|
|
if !pdfcraft_engine::ocr::available() {
|
|
eprintln!("skipped: OCR models not installed");
|
|
return;
|
|
}
|
|
let dir = std::env::temp_dir().join(format!("pdfcraft-ocr-ui-{}", std::process::id()));
|
|
let _ = std::fs::remove_dir_all(&dir);
|
|
std::fs::create_dir_all(&dir).unwrap();
|
|
let mut app = PdfCraftApp::new();
|
|
app.set_option("language", "en").unwrap();
|
|
app.run_inline = true;
|
|
app.export_dir_override = Some(dir.to_string_lossy().into_owned());
|
|
app.use_files(pdfcraft_ui_egui::FilePurpose::Ocr, vec![("one.pdf".into(), scan()), ("two.pdf".into(), scan())]);
|
|
assert!(app.ocr_batch.is_none(), "finished");
|
|
let toast = app.toast.clone().unwrap().0;
|
|
assert!(toast.starts_with("Recognized ") && toast.ends_with("in 2 files"), "{toast}");
|
|
for name in ["one.pdf", "two.pdf"] {
|
|
let bytes = std::fs::read(dir.join(name)).unwrap();
|
|
assert!(bytes.len() > scan().len(), "an incremental update was appended");
|
|
}
|
|
}
|
|
|
|
/// #1028: "Also recognize pages that already have text" reads a picture on a page that has its own
|
|
/// text, without adding that text a second time.
|
|
#[test]
|
|
fn recognize_text_can_read_pictures_on_pages_with_text() {
|
|
let hybrid = {
|
|
let mut s = Session::new();
|
|
let id = s.open("scan.pdf", None, std::sync::Arc::new(scan()), None).unwrap();
|
|
let (w, h) = {
|
|
let p = &s.get(id).unwrap().info.pages[0];
|
|
(p.width as f64, p.height as f64)
|
|
};
|
|
let note =
|
|
AddedText { rect: [w * 0.1, h * 0.75, w * 0.9, h * 0.85], text: "Invoice reference 4711".into(), size: 18.0, ..Default::default() };
|
|
s.apply(id, Edit::AddText { page: 0, text: note }).unwrap();
|
|
s.save_bytes(id).unwrap().to_vec()
|
|
};
|
|
let mut h = Harness::builder().with_size(egui::vec2(1400.0, 900.0)).build_eframe(|_cc| {
|
|
let mut app = PdfCraftApp::new();
|
|
app.set_option("language", "en").unwrap();
|
|
app.open_bytes("hybrid.pdf", None, hybrid).unwrap();
|
|
app.run_inline = true;
|
|
app
|
|
});
|
|
h.run_steps(4);
|
|
assert!(h.state_mut().execute("ocr.recognize"));
|
|
h.run_steps(2);
|
|
assert!(h.state().ocr_draft.settings().skip_text_pages, "pages with text are skipped by default");
|
|
h.get_by_label("Also recognize pages that already have text").click();
|
|
h.run_steps(2);
|
|
assert!(!h.state().ocr_draft.settings().skip_text_pages);
|
|
if !pdfcraft_engine::ocr::available() {
|
|
eprintln!("skipped: OCR models not installed");
|
|
return;
|
|
}
|
|
h.get_by_label("Recognize text").click();
|
|
h.run_steps(3);
|
|
assert!(h.state().ocr_run.is_none(), "finished");
|
|
let toast = h.state().toast.clone().unwrap().0;
|
|
assert!(toast.starts_with("Recognized ") && toast.ends_with("on 1 page"), "{toast}");
|
|
let app = h.state();
|
|
let id = app.active_ids().unwrap().1;
|
|
let doc = app.session.get(id).unwrap();
|
|
assert_eq!(doc.can_undo(), Some("Recognize text"));
|
|
let mut r = pdfcraft_render::PageRenderer::new(doc.bytes.clone(), Default::default());
|
|
let text = r.render(pdfcraft_render::RenderRequest { page: 0, kind: pdfcraft_render::RequestKind::Text, scale: 1.0, ..Default::default() });
|
|
let text = text.text.unwrap().plain_text().to_lowercase();
|
|
assert_eq!((text.matches("invoice").count(), text.matches("scanned").count()), (1, 1), "{text}");
|
|
}
|
|
|
|
/// Edit text & images on a scanned page: an OCR word (invisible text over the scan) opens for
|
|
/// editing on a click, stays open when the pointer leaves it, and its editor hides the scanned
|
|
/// word instead of showing it through.
|
|
#[test]
|
|
fn an_ocr_word_opens_for_editing_and_stays_open() {
|
|
use pdfcraft_engine::ocr::PlacedWord;
|
|
let mut h = Harness::builder().with_size(egui::vec2(1400.0, 900.0)).build_eframe(|_cc| {
|
|
let mut app = PdfCraftApp::new();
|
|
app.set_option("language", "en").unwrap();
|
|
app.open_bytes("scan.pdf", None, scan()).unwrap();
|
|
let id = app.views[0].id;
|
|
let ph = app.session.get(id).unwrap().info.pages[0].height as f64;
|
|
let words = vec![PlacedWord { text: "Recognize".into(), origin: [72.0, ph - 100.0], across: [80.0, 0.0], up: [0.0, 14.0] }];
|
|
app.session.apply(id, Edit::AddOcrText { page: 0, words }).unwrap();
|
|
app.set_option("zoom", "100").unwrap();
|
|
assert!(app.execute("edit.edit_text"));
|
|
app
|
|
});
|
|
crate::support::settle(&mut h);
|
|
let (r, k) = {
|
|
let app = h.state();
|
|
let d = app.session.get(app.views[0].id).unwrap();
|
|
assert!(d.text_blocks(0).first().is_some_and(|b| b.invisible));
|
|
let r = app.views[0].page_screen_rect(0).unwrap();
|
|
(r, r.width() / d.info.pages[0].width)
|
|
};
|
|
// The word, 100 pt from the top of the page.
|
|
let p = r.min + egui::vec2(100.0, 93.0) * k;
|
|
h.hover_at(p);
|
|
h.run_steps(2);
|
|
for pressed in [true, false] {
|
|
h.event(egui::Event::PointerButton { pos: p, button: egui::PointerButton::Primary, pressed, modifiers: Default::default() });
|
|
}
|
|
h.run_steps(4);
|
|
let opened = |h: &Harness<'_, PdfCraftApp>| h.state().views[0].line_editor.as_ref().map(|e| e.text.clone());
|
|
assert_eq!(opened(&h).as_deref(), Some("Recognize"));
|
|
assert!(h.state().views[0].image_selection.is_none(), "the scan under the word isn't selected");
|
|
h.hover_at(r.min + egui::vec2(300.0, 400.0) * k);
|
|
h.run_steps(4);
|
|
assert_eq!(opened(&h).as_deref(), Some("Recognize"), "moving the pointer away keeps the word open");
|
|
}
|