mirror of
https://github.com/storytold/pdfcraft.git
synced 2026-12-06 14:04:01 -05:00
Test organize thumbnails after moving a page
This commit is contained in:
1 parent
825cdf03ec
commit
0230ac0fc7
1 file changed
+89
@@ -3291,6 +3291,95 @@ fn dragging_thumbnails_reorders_pages() {
|
||||
assert_eq!(sel, vec![2], "the moved page stays selected");
|
||||
}
|
||||
|
||||
/// Retain the actual textures painted by the thumbnail grid, including those replaced after an edit.
|
||||
#[derive(Clone, Default)]
|
||||
struct OrganizeUploads(std::sync::Arc<std::sync::Mutex<std::collections::HashMap<egui::TextureId, std::sync::Arc<egui::ColorImage>>>>);
|
||||
|
||||
impl egui_kittest::TestRenderer for OrganizeUploads {
|
||||
fn handle_delta(&mut self, delta: &mut egui::TexturesDelta) {
|
||||
let mut images = self.0.lock().unwrap();
|
||||
for (id, changes) in delta.set.drain() {
|
||||
for change in changes.into_iter().filter(|c| c.pos.is_none()) {
|
||||
let egui::ImageData::Color(image) = change.image;
|
||||
images.insert(id, image);
|
||||
}
|
||||
}
|
||||
delta.free.clear();
|
||||
}
|
||||
|
||||
fn render(&mut self, _: &egui::Context, _: &egui::FullOutput) -> Result<image::RgbaImage, String> {
|
||||
Err("this renderer records texture uploads".into())
|
||||
}
|
||||
}
|
||||
|
||||
/// The largest uploaded picture actually drawn inside a named Organize tile.
|
||||
fn organize_tile_image(h: &Harness<'static, PdfCraftApp>, uploads: &OrganizeUploads, label: &str) -> Option<std::sync::Arc<egui::ColorImage>> {
|
||||
fn collect<'a>(shape: &'a egui::Shape, out: &mut Vec<&'a egui::Shape>) {
|
||||
match shape {
|
||||
egui::Shape::Vec(children) => children.iter().for_each(|s| collect(s, out)),
|
||||
_ => out.push(shape),
|
||||
}
|
||||
}
|
||||
let tile = h.query_by_label(label)?.rect();
|
||||
let images = uploads.0.lock().unwrap();
|
||||
let mut shapes = Vec::new();
|
||||
h.output().shapes.iter().for_each(|s| collect(&s.shape, &mut shapes));
|
||||
shapes
|
||||
.into_iter()
|
||||
.filter_map(|s| {
|
||||
let egui::Shape::Mesh(mesh) = s else { return None };
|
||||
let bounds = mesh.calc_bounds();
|
||||
let image = images.get(&mesh.texture_id)?;
|
||||
(bounds.width() > 30.0 && bounds.height() > 30.0 && tile.contains(bounds.center()) && image.size[0] > 30 && image.size[1] > 30)
|
||||
.then(|| (bounds.area(), image.clone()))
|
||||
})
|
||||
.max_by(|a, b| a.0.total_cmp(&b.0))
|
||||
.map(|(_, image)| image)
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn moved_organize_page_updates_the_painted_thumbnail_in_the_same_session() {
|
||||
use std::sync::Arc;
|
||||
let jobs = Arc::new(craft::host::ManualJobs::new());
|
||||
let uploads = OrganizeUploads::default();
|
||||
let on = jobs.clone();
|
||||
let mut h = Harness::builder().with_size(egui::vec2(1400.0, 900.0)).renderer(uploads.clone()).build_eframe(move |cc| {
|
||||
support::install_runtime(&cc.egui_ctx);
|
||||
let mut app = support::test_app(support::host_with(on.clone(), Arc::new(craft::host::ScriptedFiles::new())));
|
||||
app.set_option(&egui::Context::default(), "language", "en").unwrap();
|
||||
app.open_bytes(&egui::Context::default(), "doc.pdf", None, fixture(4)).unwrap();
|
||||
app.set_option(&egui::Context::default(), "organize", "on").unwrap();
|
||||
app
|
||||
});
|
||||
let mut before_images = None;
|
||||
for _ in 0..200 {
|
||||
jobs.run_all();
|
||||
h.run_steps(1);
|
||||
let pictures: Option<Vec<_>> = ["Page 1", "Page 2", "Page 3"].iter().map(|label| organize_tile_image(&h, &uploads, label)).collect();
|
||||
if let Some(p) = pictures.filter(|p| p[0].pixels != p[1].pixels && p[1].pixels != p[2].pixels && p[0].pixels != p[2].pixels) {
|
||||
before_images = Some(p);
|
||||
break;
|
||||
}
|
||||
}
|
||||
let before_images = before_images.expect("three visibly distinct page thumbnails are painted");
|
||||
let before = page_texts(h.state());
|
||||
let from = h.get_by_label("Page 1").rect().center();
|
||||
let to = h.get_by_label("Page 3").rect().right_center() - egui::vec2(10.0, 0.0);
|
||||
page_drag(&mut h, from, to, Modifiers::NONE);
|
||||
assert_eq!(page_texts(h.state()), vec![before[1].clone(), before[2].clone(), before[0].clone(), before[3].clone()]);
|
||||
for _ in 0..200 {
|
||||
jobs.run_all();
|
||||
h.run_steps(1);
|
||||
let after: Option<Vec<_>> = ["Page 1", "Page 2", "Page 3"].iter().map(|label| organize_tile_image(&h, &uploads, label)).collect();
|
||||
if after.is_some_and(|p| {
|
||||
p[0].pixels == before_images[1].pixels && p[1].pixels == before_images[2].pixels && p[2].pixels == before_images[0].pixels
|
||||
}) {
|
||||
return;
|
||||
}
|
||||
}
|
||||
panic!("the painted thumbnails did not follow the committed reorder");
|
||||
}
|
||||
|
||||
/// A page drag with the copy modifier held — Ctrl (Option on macOS) — instead of none.
|
||||
/// Modifiers are input state: `ModifiersChanged` once, as the column-select drag does.
|
||||
fn copy_drag(h: &mut Harness<'static, PdfCraftApp>, from: egui::Pos2, to: egui::Pos2) {
|
||||
|
||||
Reference in new issue
Block a user