From 76810cd654102eb06f41ba128759d22683462d45 Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Wed, 18 Nov 2026 16:15:27 -0500 Subject: [PATCH] pdf-comments-w16: net change of 3 commits - pdf-comments-w16: written from its sheet - pdf-comments-w16: repair 1 - pdf-comments-w16: review fixes (review-1) --- crates/ui-egui/src/tools/comments/tests.rs | 745 ++++++--------------- crates/ui-egui/tests/it/document_links.rs | 155 +++-- crates/ui-egui/tests/perf.rs | 282 ++++---- 3 files changed, 427 insertions(+), 755 deletions(-) diff --git a/crates/ui-egui/src/tools/comments/tests.rs b/crates/ui-egui/src/tools/comments/tests.rs index e43b7fc86f..63415a4851 100644 --- a/crates/ui-egui/src/tools/comments/tests.rs +++ b/crates/ui-egui/src/tools/comments/tests.rs @@ -1,65 +1,37 @@ -use super::geometry::reshaped; +use super::bridge::{self, PageMap}; use super::*; +use craft::behavior::kurbo::{Point, Vec2}; +use craft::pages::ui::PlacedPage; +use craft::review::annotate::{AnnotationEdit, pick_at}; +use craft::review::{AnnotationId, ThreadId}; +use craft::ui::primitives::textures::TextureState; -/// [`reshaped`] without Shift, as `changing_points` takes it. -fn straight(points: &[[f64; 2]], i: usize, to: [f64; 2]) -> Vec<[f64; 2]> { - reshaped(points, i, to, false) +/// The screen size, in points, of a comment marker's icon. +const ICON_POINTS: f32 = 12.0; + +/// The side of a comment marker in page units, as the app feeds `bridge`: the icon's screen size through the page map's +/// own conversion, so a change of the conversion is in the test's path. +fn icon_side(map: &PageMap<'_>) -> f64 { + map.doc_len(ICON_POINTS) } -#[test] -fn freehand_highlight_and_draw_accept_widths_up_to_100() { - assert_eq!(CommentTool::FreehandHighlight.group(), CommentTool::Highlight.group()); - assert_eq!(GROUPS.len(), 3); - let mut prefs = CommentPrefs::default(); - assert_eq!(prefs.style(CommentTool::FreehandHighlight).width, 14.0); - assert_eq!(prefs.style(CommentTool::FreehandHighlight).opacity, 0.35); - for tool in [CommentTool::Ink, CommentTool::FreehandHighlight] { - prefs.set_width(tool, 75.0); - assert_eq!(prefs.style(tool).width, 75.0); - prefs.set_width(tool, 200.0); - assert_eq!(prefs.style(tool).width, 100.0); - prefs.set_width(tool, f64::NAN); - assert_eq!(prefs.style(tool).width, 100.0); - } +/// The screen distance, in points, within which a press picks a comment's body. +const SLOP: f32 = 4.0; + +/// The one placed page of a test: `xf`'s rectangle at `zoom`. +fn placed_page(xf: &PageXform, zoom: f64) -> PlacedPage { + PlacedPage::new(0, xf.rect, zoom, (0.0, xf.rect.height()), TextureState::Missing) } -/// A selected line is drawn where it is going: moved by its body (both ends follow the -/// pointer), with an end dragged (that end alone), and as released until the page is drawn. -#[test] -fn a_moved_or_reshaped_line_is_drawn_at_its_new_points() { - let line = [[10.0, 10.0], [50.0, 10.0]]; - let to_user = |p: Pos2| [f64::from(p.x), f64::from(p.y)]; - let moving = Gesture::Move { page: 0, index: 3, from: pos2(20.0, 10.0) }; - let at = Some(pos2(25.0, 30.0)); - assert_eq!(changing_points(Some(&moving), None, (0, 3), &line, to_user, at, straight), Some(vec![[15.0, 30.0], [55.0, 30.0]])); - assert_eq!(changing_points(Some(&moving), None, (0, 4), &line, to_user, at, straight), None, "another comment's move"); - let released = Edit::MoveAnnotation { page: 0, index: 3, dx: 5.0, dy: 20.0 }; - assert_eq!(changing_points(None, Some(&released), (0, 3), &line, to_user, None, straight), Some(vec![[15.0, 30.0], [55.0, 30.0]])); - let dragging = Gesture::Reshape { page: 0, index: 3, point: 1, points: line.to_vec() }; - let to = Some(pos2(60.0, 10.0)); - assert_eq!(changing_points(Some(&dragging), None, (0, 3), &line, to_user, to, straight), Some(vec![[10.0, 10.0], [60.0, 10.0]])); - assert_eq!(changing_points(None, None, (0, 3), &line, to_user, to, straight), None, "not changing"); - // Released with the rest of the selection: its own move in the batch. - let together = Edit::Batch { - label: "Move comments".into(), - edits: vec![Edit::MoveAnnotation { page: 0, index: 1, dx: 1.0, dy: 1.0 }, Edit::MoveAnnotation { page: 0, index: 3, dx: 5.0, dy: 20.0 }], - }; - assert_eq!(changing_points(None, Some(&together), (0, 3), &line, to_user, None, straight), Some(vec![[15.0, 30.0], [55.0, 30.0]])); - assert_eq!(changing_points(None, Some(&together), (0, 2), &line, to_user, None, straight), None, "not in the batch"); +/// The page map the app feeds `bridge` for `placed` pages shown turned `rotation` degrees. +fn page_map<'a>(placed: &'a [PlacedPage], info: &DocInfo, rotation: u16) -> PageMap<'a> { + // PLAN: assumed `PageMap::new(&[PlacedPage], &DocInfo, view_rotation: u16) -> PageMap` (pdf-comments-w7 A-1). + PageMap::new(placed, info, rotation) } -/// Review on #4094: a callout's text box being resized, and the resize just released, draw its -/// leader line too (to the box where it is going): its points are changing, though they stay. -#[test] -fn a_callout_being_resized_draws_its_leader_line() { - let lead = [[10.0, 10.0], [40.0, 30.0]]; - let to_user = |p: Pos2| [f64::from(p.x), f64::from(p.y)]; - let resizing = Gesture::Resize { page: 0, index: 3, handle: (1, 1), from: pos2(80.0, 60.0), aspect_ratio: None }; - let at = Some(pos2(95.0, 70.0)); - assert_eq!(changing_points(Some(&resizing), None, (0, 3), &lead, to_user, at, straight), Some(lead.to_vec())); - assert_eq!(changing_points(Some(&resizing), None, (0, 4), &lead, to_user, at, straight), None, "another comment's resize"); - let released = Edit::ResizeAnnotation { page: 0, index: 3, rect: [50.0, 20.0, 110.0, 70.0] }; - assert_eq!(changing_points(None, Some(&released), (0, 3), &lead, to_user, None, straight), Some(lead.to_vec())); +/// A kurbo rectangle as an egui one, for comparing a marker with a page's screen rectangle. +fn screen(r: craft::behavior::kurbo::Rect) -> Rect { + Rect::from_min_max(pos2(r.x0 as f32, r.y0 as f32), pos2(r.x1 as f32, r.y1 as f32)) } /// A sticky note on a page turned a quarter is drawn upright beside its `/Rect`: an edit @@ -96,19 +68,8 @@ fn an_upright_note_on_a_turned_page_is_in_view_where_it_is_drawn() { let viewport = Rect::from_min_max(pos2(50.0, 0.0), pos2(140.0, 200.0)); for (no_rotate, in_view) in [(true, true), (false, false)] { let info = DocInfo { pages: vec![page.clone()], annotations: vec![note(no_rotate)], ..Default::default() }; - let cx = PageCx { - page: 0, - xf: &xf, - ptr: &PagePointer::default(), - info: &info, - tool: QuickTool::Select, - prefs: &prefs, - allowed: true, - hidden: false, - redact_mark: ([0.0; 3], 1.0), - keys: Default::default(), - }; - assert_eq!(edited_in_view(&cx, 0, viewport), in_view, "no_rotate: {no_rotate}"); + let cx = PageCx { page: 0, xf: &xf, info: &info, prefs: &prefs, allowed: true, hidden: false }; + assert_eq!(super::composer::edited_in_view(&cx, 0, viewport), in_view, "no_rotate: {no_rotate}"); } } @@ -167,17 +128,20 @@ fn a_kept_comment_is_found_where_a_delete_moved_it() { assert_eq!(after_delete(&before, &changed, gone, (0, 2)), None); let info = DocInfo { annotations: before.clone(), ..Default::default() }; + let moved = DocInfo { annotations: after.clone(), ..Default::default() }; + let thread = |key: (usize, usize)| ThreadId(bridge::id_of(key.0, key.1).0); let mut view = CommentView::from_info(&info); - view.selected = Some((0, 2)); - view.editing = Some((0, 3, "typed".into())); - view.annotation_deleted(&before, &DocInfo { annotations: after.clone(), ..Default::default() }, gone); - view.document_changed(&DocInfo { annotations: after.clone(), ..Default::default() }); - assert_eq!((view.selected, view.editing), (Some((0, 0)), Some((0, 1, "typed".into()))), "both follow"); + view.select_only((0, 2)); + view.panel.editing = Some((thread((0, 3)), 0, "typed".into())); + view.annotation_deleted(&before, &moved, gone); + view.document_changed(&moved); + assert_eq!(view.selection(), vec![(0, 0)], "the selection follows"); + let (editing, _, text) = view.panel.editing.clone().expect("the editor stays open"); + assert_eq!((bridge::key_of(AnnotationId(editing.0)), text.as_str()), ((0, 1), "typed"), "the editor follows"); let mut view = CommentView::from_info(&info); - view.editing = Some((0, 4, "typed".into())); - view.editor_focus = Some(EditorFocus::Take); - view.annotation_deleted(&before, &DocInfo { annotations: after.clone(), ..Default::default() }, gone); - assert_eq!((view.editing, view.editor_focus), (None, None), "the editor closes with the reply it was open on"); + view.panel.editing = Some((thread((0, 4)), 0, "typed".into())); + view.annotation_deleted(&before, &moved, gone); + assert_eq!(view.panel.editing, None, "the editor closes with the reply it was open on"); } use pdfcraft_render::TextGlyph; @@ -302,234 +266,83 @@ fn away_from_upright_horizontal_text_the_caret_stays_at_the_click() { #[test] fn recent_custom_colours_dedupe_cap_and_skip_swatches() { - // #1193: "Other colour…" remembers what it applied, newest first. - let ctx = egui::Context::default(); - assert!(recent_custom_colours(&ctx).is_empty()); - for i in 0..7 { - remember_custom_colour(&ctx, [0.1 * f64::from(i), 0.3, 0.7]); - } - let recent = recent_custom_colours(&ctx); - assert_eq!(recent.len(), RECENT_CUSTOM, "capped"); - assert!(same_colour(&recent[0], &[0.6, 0.3, 0.7]), "newest first: {recent:?}"); - remember_custom_colour(&ctx, [0.2, 0.3, 0.7]); - let again = recent_custom_colours(&ctx); - assert_eq!(again.len(), RECENT_CUSTOM); - assert!( - same_colour(&again[0], &[0.2, 0.3, 0.7]) && again.iter().filter(|c| same_colour(c, &[0.2, 0.3, 0.7])).count() == 1, - "moved, not repeated" - ); - // Swatches and unusable values are not remembered; out-of-range ones are clamped. - remember_custom_colour(&ctx, SWATCHES[0].1); - remember_custom_colour(&ctx, [f64::NAN, 0.0, 0.0]); - assert!(same_colour(&recent_custom_colours(&ctx)[0], &[0.2, 0.3, 0.7])); - remember_custom_colour(&ctx, [1.7, -0.5, 0.25]); - assert!(same_colour(&recent_custom_colours(&ctx)[0], &[1.0, 0.0, 0.25])); -} - -#[test] -fn other_colour_applies_the_picked_colour_once() { - // #1193: the swatch grid has a full picker; Apply returns its colour and remembers it. - use egui_kittest::kittest::Queryable; - let current = [0.2, 0.4, 0.6]; - let mut h = egui_kittest::Harness::new_ui_state( - |ui, picked: &mut Vec| { - if let Some(c) = swatch_grid(ui, Some(current)) { - picked.push(c); - } - }, - Vec::new(), - ); - h.run(); - assert!(h.query_by_label("Apply").is_none(), "the picker starts closed"); - assert!(h.query_by_label("Recent colours").is_none()); - h.get_by_label("Other colour…").click(); - h.run(); - h.get_by_label("Apply").click(); - h.run(); - let picked = h.state().clone(); - assert_eq!(picked.len(), 1, "one choice: {picked:?}"); - assert!(same_colour(&picked[0], ¤t), "the picker opens on the current colour: {picked:?}"); - assert!(h.query_by_label("Apply").is_none(), "applying closes the picker"); - assert!(h.query_by_label("Recent colours").is_some(), "the colour is offered again"); -} - -#[test] -fn two_grids_in_one_parent_open_their_pickers_separately() { - // Redaction properties and Prepare a form draw two grids in one parent ui. - use egui_kittest::kittest::Queryable; - let mut h = egui_kittest::Harness::new_ui(|ui| { - let _ = swatch_grid(ui, Some([0.2, 0.4, 0.6])); - let _ = swatch_grid(ui, Some([0.6, 0.4, 0.2])); - }); - h.run(); - let plus = h.query_all_by_label("Other colour…").collect::>(); - assert_eq!(plus.len(), 2); - plus[0].click(); - h.run(); - assert_eq!(h.query_all_by_label("Apply").count(), 1, "only the first grid's picker opens"); -} - -#[test] -fn other_colour_opens_the_picker_inside_the_quick_bar_popup() { - use egui_kittest::kittest::Queryable; - let mut h = egui_kittest::Harness::new_ui_state( - |ui, prefs: &mut CommentPrefs| { - let _ = quick_bar_controls(ui, CommentTool::Highlight, prefs); - }, - CommentPrefs::default(), - ); - h.run(); - h.get_by_label("Colour").click(); - h.run(); - h.get_by_label("Other colour…").click(); - h.run(); - assert!(h.query_by_label("Apply").is_some(), "the popup stays open with the picker showing"); - h.get_by_label("Apply").click(); - h.run(); - assert!(h.query_by_label("Other colour…").is_none(), "applying a colour closes the popup"); -} - -#[test] -fn other_colour_opens_the_picker_inside_the_quick_bar_fill_popup() { - use egui_kittest::kittest::Queryable; - let prefs = CommentPrefs::default(); - let fill_label = if prefs.style(CommentTool::Rectangle).fill.is_some() { "Fill colour" } else { "Fill colour: none" }; - let mut h = egui_kittest::Harness::new_ui_state( - |ui, prefs: &mut CommentPrefs| { - let _ = quick_bar_controls(ui, CommentTool::Rectangle, prefs); - }, - prefs, - ); - h.ctx.set_fonts(theme::font_definitions()); - h.run(); - h.get_by_label(fill_label).click(); - h.run(); - h.get_by_label("Other colour…").click(); - h.run(); - assert!(h.query_by_label("Apply").is_some(), "the popup stays open with the picker showing"); - h.get_by_label("Apply").click(); - h.run(); - assert!(h.query_by_label("No fill").is_none(), "applying a colour closes the popup"); - assert!(h.state().style(CommentTool::Rectangle).fill.is_some()); -} - -#[test] -fn context_menu_colour_submenu_keeps_its_picker_open_and_a_swatch_closes_the_menu() { - use egui_kittest::kittest::Queryable; - let info = DocInfo { - pages: vec![PageInfo { width: 300.0, height: 400.0, label: String::new(), crop: [0.0, 0.0, 300.0, 400.0], rotation: 0 }], - annotations: vec![Annotation { - page: 0, - subtype: "Square".into(), - author: None, - contents: None, - modified: None, - name: None, - in_reply_to: None, - parent: None, - grouped: false, - rect: [10.0, 10.0, 50.0, 50.0], - color: None, - index: 0, - state: None, - quads: Vec::new(), - locked: false, - intent: None, - fill_sign: false, - points: Vec::new(), - text_box: None, - popup: None, - no_rotate: false, - }], - ..Default::default() + use super::SWATCHES; + use super::colours::{RecentColours, to_srgba}; + let same = |a: &Srgba, b: [f64; 3]| { + let b = to_srgba(b); + (a.r - b.r).abs() < 1e-3 && (a.g - b.g).abs() < 1e-3 && (a.b - b.b).abs() < 1e-3 }; - let mut view = DocView::new(pdfcraft_engine::DocId(1), &info, crate::canvas::ViewDefaults::default()); - view.comments.selected = Some((0, 0)); - let prefs = CommentPrefs::default(); - let mut h = egui_kittest::Harness::new_ui_state( - |ui, (view, actions): &mut (DocView, Vec)| { - ui.button("Page").context_menu(|ui| { - let rights = MenuRights { allowed: true, can_paste: false, can_modify: true }; - if let Some(a) = context_menu(ui, view, &info, &|_, _| true, &prefs, rights) { - actions.push(a); - } - }); - }, - (view, Vec::new()), - ); - h.run(); - h.get_by_label("Page").click_secondary(); - h.run(); - h.get_by_label("Colour").click(); - h.run(); - h.get_by_label("Other colour…").click(); - h.run(); - assert!(h.query_by_label("Apply").is_some(), "the submenu stays open with the picker showing"); - h.get_by_label("Red").click(); - h.run(); - assert!(h.query_by_label("Edit text…").is_none(), "picking a swatch closes the whole menu"); - let actions = &h.state().1; - assert!( - matches!(actions.as_slice(), [CanvasAction::Edit(e)] if matches!(**e, Edit::StyleAnnotation { page: 0, index: 0, color: Some(_), .. })), - "{actions:?}" - ); -} - -#[test] -fn shift_keeps_the_current_proportions_unless_the_box_has_its_own() { - let r = Rect::from_min_max(pos2(100.0, 100.0), pos2(196.0, 132.0)); - assert_eq!(corner_ratio(r, None, false), None); - assert_eq!(corner_ratio(r, None, true), Some(3.0)); - assert_eq!(corner_ratio(r, Some(2.0), false), Some(2.0)); - assert_eq!(corner_ratio(r, Some(2.0), true), Some(2.0)); - // A degenerate box has no proportions to keep (no division by zero). - let flat = Rect::from_min_max(pos2(100.0, 100.0), pos2(196.0, 100.0)); - assert_eq!(corner_ratio(flat, None, true), None); - // Dragging a corner with Shift held scales the box instead of stretching it. - let out = resized(r, (1, 1), vec2(48.0, 0.0), corner_ratio(r, None, true)); - assert_eq!(out.min, r.min); - assert!((out.width() / out.height() - 3.0).abs() < 0.001); -} - -#[test] -fn proportional_corners_keep_the_opposite_anchor_and_edges_stretch_one_axis() { - let r = Rect::from_min_max(pos2(100.0, 100.0), pos2(196.0, 132.0)); - for (hx, hy) in [(-1, -1), (1, -1), (-1, 1), (1, 1)] { - for (dx, dy) in [(24.0, 0.0), (0.0, 16.0), (-48.0, -20.0), (-200.0, -200.0)] { - let out = resized(r, (hx, hy), vec2(f32::from(hx) * dx, f32::from(hy) * dy), Some(3.0)); - assert_eq!(handle_pos(out, (-hx, -hy)), handle_pos(r, (-hx, -hy))); - assert!((out.width() / out.height() - 3.0).abs() < 0.001); - assert!(out.width() >= 4.0 && out.height() >= 4.0); - } + // #1193: "Other colour…" remembers what it applied, newest first. + let recent = RecentColours::default(); + assert!(recent.as_srgba().is_empty()); + for i in 0..7 { + recent.remember([0.1 * f64::from(i), 0.3, 0.7]); } - for handle in [(0, -1), (1, 0), (0, 1), (-1, 0)] { - let out = resized(r, handle, vec2(24.0, 16.0), Some(3.0)); - assert_eq!(out, resized(r, handle, vec2(24.0, 16.0), None)); - assert_eq!(if handle.0 == 0 { out.width() } else { out.height() }, if handle.0 == 0 { r.width() } else { r.height() }); - } - // Ordinary comment corners keep their existing independent width/height behavior. - let free = resized(r, (1, 1), vec2(24.0, 0.0), None); - assert_eq!(free.size(), vec2(120.0, 32.0)); + let list = recent.as_srgba(); + assert_eq!(list.len(), 5, "capped"); + assert!(same(&list[0], [0.6, 0.3, 0.7]), "newest first: {list:?}"); + recent.remember([0.2, 0.3, 0.7]); + let again = recent.as_srgba(); + assert_eq!(again.len(), 5); + assert!(same(&again[0], [0.2, 0.3, 0.7]) && again.iter().filter(|c| same(c, [0.2, 0.3, 0.7])).count() == 1, "moved, not repeated"); + // Swatches and unusable values are not remembered; out-of-range ones are clamped. + recent.remember(SWATCHES[0].1); + recent.remember([f64::NAN, 0.0, 0.0]); + assert!(same(&recent.as_srgba()[0], [0.2, 0.3, 0.7])); + recent.remember([1.7, -0.5, 0.25]); + assert!(same(&recent.as_srgba()[0], [1.0, 0.0, 0.25])); } #[test] fn tools_round_trip_through_their_commands() { + icons::register(); + // PLAN: assumed `super::annotate_icons() -> AnnotateIcons`, the app's CM-C31 supply built from `CommentTool::icon`. + let supplied = super::annotate_icons(); for t in ALL { assert_eq!(CommentTool::from_command(t.command()), Some(t)); assert!(pdfcraft_engine::commands::command(t.command()).is_some(), "{} is registered", t.command()); - assert!(icons::exists(t.icon()), "icon {}", t.icon()); + let icon = supplied.icon(craft::review::ui::annotate::AnnotateCommand::of(t.tool())).expect("the app names every tool's icon"); + assert!(icons::exists(icon), "icon {icon}"); assert!(GROUPS[t.group()].contains(&t)); } } +/// A saved arrow without its head compiles and passes every other check: `Create` maps an Arrow to a line ending in an +/// open arrow, and a plain Line to one with no end. #[test] -fn drawn_shapes_ignore_slips() { - assert_eq!(drawn_shape(CommentTool::Rectangle, &[[0.0, 0.0], [1.0, 1.0]]), None); - assert_eq!(drawn_shape(CommentTool::Rectangle, &[[10.0, 0.0], [0.0, 10.0]]), Some(Shape::Rectangle { rect: [0.0, 0.0, 10.0, 10.0] })); - assert_eq!(drawn_shape(CommentTool::Ink, &[[0.0, 0.0]]), Some(Shape::Ink { strokes: vec![vec![[0.0, 0.0]]] })); +fn an_arrow_is_created_as_a_line_with_an_open_arrow_end() { + let page = PageInfo { width: 300.0, height: 400.0, label: String::new(), crop: [0.0, 0.0, 300.0, 400.0], rotation: 0 }; + let info = DocInfo { pages: vec![page], ..Default::default() }; + let xf = PageXform { rect: Rect::from_min_size(Pos2::ZERO, vec2(300.0, 400.0)), rot: 0, pw: 300.0, ph: 400.0 }; + let placed = [placed_page(&xf, 1.0)]; + let map = page_map(&placed, &info, 0); + let mut view = DocView::new(pdfcraft_engine::DocId(1), &info, crate::canvas::ViewDefaults::default()); + let prefs = CommentPrefs::default(); + let mut created = |kind| { + let drawn = craft::review::Annotation { + id: bridge::id_of(0, 0), + page: 0, + kind, + rect: craft::behavior::kurbo::Rect::new(10.0, 10.0, 60.0, 10.0), + points: vec![Point::new(10.0, 10.0), Point::new(60.0, 10.0)], + strokes: Vec::new(), + color: [255, 0, 0], + opacity: 1.0, + stroke_width: 2.0, + text: String::new(), + locked: false, + aspect: None, + handle_rule: Default::default(), + }; + bridge::edit(AnnotationEdit::Create(drawn), &map, &mut view.comments, &info, &prefs) + }; assert!(matches!( - drawn_shape(CommentTool::Arrow, &[[0.0, 0.0], [5.0, 0.0]]), - Some(Shape::Line { start: LineEnding::None, end: LineEnding::OpenArrow, .. }) + created(craft::review::AnnotationKind::Arrow), + Some(Edit::AddAnnotation(pdfcraft_engine::NewAnnotation { shape: Shape::Line { start: LineEnding::None, end: LineEnding::OpenArrow, .. }, .. })) + )); + assert!(matches!( + created(craft::review::AnnotationKind::Line), + Some(Edit::AddAnnotation(pdfcraft_engine::NewAnnotation { shape: Shape::Line { end: LineEnding::None, .. }, .. })) )); } @@ -548,7 +361,7 @@ fn shift_click_selects_the_range_from_the_comment_with_the_handles() { none.select_range(&order, (0, 0)); assert_eq!(none.selection(), vec![(0, 0)]); cv.select_range(&order, (9, 9)); - assert_eq!(cv.selected, Some((9, 9))); + assert_eq!(cv.primary(), Some((9, 9))); } #[test] @@ -561,41 +374,11 @@ fn text_boxes_fit_their_text() { assert!(long[3] - long[1] > 50.0); } -#[test] -fn markup_comment_markers_skip_rounded_inverted_clamp_bounds() { - let prefs = CommentPrefs::default(); - let ptr = PagePointer::default(); - let info = DocInfo { - pages: vec![PageInfo { width: 12.0, height: 12.0, label: String::new(), crop: [0.0, 0.0, 12.0, 12.0], rotation: 0 }], - ..Default::default() - }; - let a = Annotation { rect: [0.0, 0.0, 12.0, 12.0], contents: Some("Review".into()), ..annotation("Highlight", None, false) }; - for min in [pos2(1.0e-7, 0.0), pos2(0.0, 1.0e-7)] { - let page = Rect::from_min_max(min, pos2(12.0, 12.0)); - assert_eq!(page.size(), vec2(12.0, 12.0)); - let marker_max = page.max - vec2(12.0, 12.0); - assert!(marker_max.x < page.min.x || marker_max.y < page.min.y); - let xf = PageXform { rect: page, rot: 0, pw: 12.0, ph: 12.0 }; - let cx = PageCx { - page: 0, - xf: &xf, - ptr: &ptr, - info: &info, - tool: QuickTool::Select, - prefs: &prefs, - allowed: true, - hidden: false, - redact_mark: ([0.0; 3], 1.0), - keys: crate::page_keys::PageKeys::new(false), - }; - assert!(page.intersects(cx.screen_rects(&a).last().unwrap())); - assert!(cx.comment_marker_rect(&a).is_none()); - } -} - +/// A commented markup's marker lies inside its page for every page and view rotation and zoom, picking the marker takes +/// the topmost comment, and plain or off-page markup has none. #[test] fn markup_comment_markers_stay_inside_turned_and_zoomed_pages() { - let prefs = CommentPrefs::default(); + let tolerance_of = |map: &PageMap<'_>| map.doc_len(SLOP); for (page_rotation, rotation) in [(0, 0), (90, 0), (180, 0), (270, 0), (0, 90), (0, 180), (0, 270), (90, 270)] { for zoom in [0.25, 1.0, 4.0] { let (pw, ph) = if page_rotation % 180 == 0 { (300.0, 200.0) } else { (200.0, 300.0) }; @@ -605,58 +388,36 @@ fn markup_comment_markers_stay_inside_turned_and_zoomed_pages() { pages: vec![PageInfo { width: pw, height: ph, label: String::new(), crop: [0.0, 0.0, 300.0, 200.0], rotation: page_rotation }], ..Default::default() }; - let cx = PageCx { - page: 0, - xf: &xf, - ptr: &PagePointer::default(), - info: &info, - tool: QuickTool::Select, - prefs: &prefs, - allowed: true, - hidden: false, - redact_mark: ([0.0; 3], 1.0), - keys: crate::page_keys::PageKeys::new(false), - }; + let placed = [placed_page(&xf, f64::from(zoom))]; + let map = page_map(&placed, &info, rotation as u16); + let side = icon_side(&map); + let with = |annotations: Vec| DocInfo { annotations, ..info.clone() }; for rect in [[0.0, 180.0, 30.0, 200.0], [270.0, 180.0, 300.0, 200.0], [270.0, 0.0, 300.0, 20.0], [0.0, 0.0, 30.0, 20.0]] { let [x0, y0, x1, y1] = rect; - let mut a = Annotation { + let a = Annotation { page: 0, subtype: "Highlight".into(), author: Some("Ada".into()), contents: Some("Review".into()), - modified: None, - name: None, - in_reply_to: None, - parent: None, - grouped: false, rect, - color: None, - index: 0, - state: None, quads: vec![[x0, y1, x1, y1, x0, y0, x1, y0]], - locked: false, - intent: None, - fill_sign: false, - no_rotate: false, - points: Vec::new(), - text_box: None, - popup: None, + ..annotation("Highlight", None, false) }; - let marker = cx.comment_marker_rect(&a).expect("commented markup has a marker"); - assert!((marker.width() - 12.0).abs() < 0.001 && (marker.height() - 12.0).abs() < 0.001, "{marker:?}"); - assert!(xf.rect.contains_rect(marker), "page/view rotation {page_rotation}/{rotation}, zoom {zoom}, anchor {rect:?}: {marker:?}"); - let mut top = a.clone(); - top.index = 1; - // Inspection order can differ from /Annots order. Picking the marker - // must select the same topmost comment as hovering its marked text. - let hit_info = DocInfo { annotations: vec![top, a.clone()], ..info.clone() }; - let hit_cx = PageCx { info: &hit_info, ..cx }; - assert_eq!(hit_cx.comment_at(marker.center()).map(|a| a.index), Some(1)); - a.contents = None; - assert!(cx.comment_marker_rect(&a).is_none(), "plain markup has no marker"); - a.contents = Some("Review".into()); - a.quads = vec![[1000.0, 1010.0, 1010.0, 1010.0, 1000.0, 1000.0, 1010.0, 1000.0]]; - assert!(cx.comment_marker_rect(&a).is_none(), "off-page markup stays off the page"); + // Inspection order can differ from /Annots order: picking the marker must select the same topmost + // comment as hovering its marked text. + let top = Annotation { index: 1, ..a.clone() }; + let both = with(vec![top, a.clone()]); + let markers = bridge::markers(&both, &map, side); + let (_, marker) = *markers.last().expect("commented markup has a marker"); + let inside = screen(marker); + assert!(xf.rect.contains_rect(inside), "page/view rotation {page_rotation}/{rotation}, zoom {zoom}, anchor {rect:?}: {inside:?}"); + let annotations = bridge::annotations(&both, &map, QuickTool::Select, true); + let picked = pick_at(&annotations, marker.center(), tolerance_of(&map), side, &markers); + assert_eq!(picked.map(|id| bridge::key_of(id).1), Some(1)); + let plain = Annotation { contents: None, ..a.clone() }; + assert!(bridge::markers(&with(vec![plain]), &map, side).is_empty(), "plain markup has no marker"); + let off = Annotation { quads: vec![[1000.0, 1010.0, 1010.0, 1010.0, 1000.0, 1000.0, 1010.0, 1000.0]], ..a }; + assert!(bridge::markers(&with(vec![off]), &map, side).is_empty(), "off-page markup stays off the page"); } } } @@ -669,23 +430,9 @@ fn stale_comment_state_is_cleared_and_delete_never_hits_non_comment_slots() { index, subtype: "Text".into(), rect: [10.0, 10.0, 30.0, 30.0], - color: None, - author: None, contents: Some(name.into()), - modified: None, - in_reply_to: None, - parent: None, - grouped: false, name: Some(name.into()), - state: None, - quads: Vec::new(), - locked: false, - intent: None, - fill_sign: false, - no_rotate: false, - points: Vec::new(), - text_box: None, - popup: None, + ..annotation("Text", None, false) }; // Page 0 has a Widget at index 0 (not in `info.annotations`), and Text comments at 1 and 2. let info = DocInfo { @@ -693,104 +440,66 @@ fn stale_comment_state_is_cleared_and_delete_never_hits_non_comment_slots() { annotations: vec![annot(1, "c1"), annot(2, "c2")], ..Default::default() }; + let thread = |index: usize| ThreadId(bridge::id_of(0, index).0); + let composer = |index: usize, text: &str| Composer { page: 0, at: [10.0, 10.0], kind: ComposerKind::Edit(index), text: text.into(), focus: false }; let mut view = DocView::new(pdfcraft_engine::DocId(1), &info, crate::canvas::ViewDefaults::default()); - view.comments.selected = Some((0, 2)); - view.comments.open_editor(0, 2, "draft".into()); - view.comments.composer = Some(Composer { page: 0, at: [10.0, 10.0], kind: ComposerKind::Edit(2), text: "body".into(), focus: false }); - view.comments.gesture = Some(Gesture::Move { page: 0, index: 2, from: pos2(0.0, 0.0) }); + view.comments.select_only((0, 2)); + view.comments.panel.editing = Some((thread(2), 0, "draft".into())); - // Moving/editing in place or appending a comment at index 3 preserves (0, 2) selection/editing/gesture. + view.comments.composer = Some(composer(2, "body")); + + // Moving/editing in place or appending a comment at index 3 preserves the (0, 2) selection, editor and composer. let mut appended = info.clone(); appended.annotations.push(annot(3, "c3")); view.document_changed(&appended); - assert_eq!(view.comments.selected, Some((0, 2))); - assert!(view.comments.editing.is_some()); + assert_eq!(view.comments.selection(), vec![(0, 2)]); + assert!(view.comments.panel.editing.is_some()); assert!(view.comments.composer.is_some()); - assert!(view.comments.gesture.is_some()); // Deleting comment 1 shifts comment 2 down to index 1: stale state at (0, 2) and shifted (0, 1) must clear. let mut shifted = info.clone(); shifted.annotations = vec![annot(1, "c2")]; - view.comments.selected = Some((0, 1)); - view.comments.open_editor(0, 1, "for c1, not c2".into()); - view.comments.composer = Some(Composer { page: 0, at: [10.0, 10.0], kind: ComposerKind::Edit(1), text: "for c1".into(), focus: false }); - view.comments.gesture = Some(Gesture::Move { page: 0, index: 1, from: pos2(0.0, 0.0) }); + view.comments.select_only((0, 1)); + view.comments.panel.editing = Some((thread(1), 0, "for c1, not c2".into())); + view.comments.composer = Some(composer(1, "for c1")); view.document_changed(&shifted); - assert_eq!(view.comments.selected, None); - assert_eq!(view.comments.editing, None); + assert!(view.comments.selection().is_empty()); + assert_eq!(view.comments.panel.editing, None); assert_eq!(view.comments.composer, None); - assert_eq!(view.comments.gesture, None); - // Pressing Delete when `selected` holds a widget index (0, 0) or stale index (0, 2) emits no edit. - let ctx = egui::Context::default(); + // Deleting a widget slot (0, 0) or a stale slot (0, 2) asks the engine for nothing. + let xf = PageXform { rect: Rect::from_min_size(Pos2::ZERO, vec2(300.0, 400.0)), rot: 0, pw: 300.0, ph: 400.0 }; + let placed = [placed_page(&xf, 1.0)]; + let map = page_map(&placed, &shifted, 0); + let prefs = CommentPrefs::default(); for bad_index in [0, 2] { - view.comments.selected = Some((0, bad_index)); - let mut input = egui::RawInput::default(); - input.events.push(egui::Event::Key { - key: egui::Key::Delete, - physical_key: None, - pressed: true, - repeat: false, - modifiers: egui::Modifiers::NONE, - }); - let mut out = ctx.run_ui(input, |ui| { - let mut tool = QuickTool::Select; - keys(ui.ctx(), crate::page_keys::PageKeys::new(false), &mut view, &shifted, &mut tool, true); - }); - out.textures_delta.clear(); - assert_eq!(view.comments.selected, None); - assert_eq!(view.pending_edit, None, "index {bad_index} must not emit DeleteAnnotation"); + let edit = bridge::edit(AnnotationEdit::Delete(vec![bridge::id_of(0, bad_index)]), &map, &mut view.comments, &shifted, &prefs); + assert_eq!(edit, None, "index {bad_index} must not emit DeleteAnnotation"); } } /// An unnamed comment has no object name to tell it from the one that slid into its index. -/// Deleting the earlier one still drops selection, editing, the composer and a gesture that -/// pointed at that index. +/// Deleting the earlier one still drops selection, editing and the composer that pointed at that index. #[test] fn an_unnamed_shift_clears_state_for_the_old_index() { - let annot = |index: usize| Annotation { - page: 0, - index, - subtype: "Text".into(), - rect: [10.0, 10.0, 30.0, 30.0], - color: None, - author: None, - contents: None, - modified: None, - in_reply_to: None, - parent: None, - grouped: false, - name: None, - state: None, - quads: Vec::new(), - locked: false, - intent: None, - fill_sign: false, - no_rotate: false, - points: Vec::new(), - text_box: None, - popup: None, - }; - let info = DocInfo { - pages: vec![ - PageInfo { width: 300.0, height: 400.0, label: String::new(), crop: [0.0, 0.0, 300.0, 400.0], rotation: 0 }, - PageInfo { width: 300.0, height: 400.0, label: String::new(), crop: [0.0, 0.0, 300.0, 400.0], rotation: 0 }, - ], - annotations: vec![annot(1), annot(2)], - ..Default::default() - }; + let annot = |index: usize| Annotation { page: 0, index, rect: [10.0, 10.0, 30.0, 30.0], ..annotation("Text", None, false) }; + let page = || PageInfo { width: 300.0, height: 400.0, label: String::new(), crop: [0.0, 0.0, 300.0, 400.0], rotation: 0 }; + let info = DocInfo { pages: vec![page(), page()], annotations: vec![annot(1), annot(2)], ..Default::default() }; let mut view = DocView::new(pdfcraft_engine::DocId(1), &info, crate::canvas::ViewDefaults::default()); - view.comments.selected = Some((0, 1)); - view.comments.open_editor(0, 1, "the first note".into()); + view.comments.select_only((0, 1)); + view.comments.panel.editing = Some((ThreadId(bridge::id_of(0, 1).0), 0, "the first note".into())); view.comments.composer = Some(Composer { page: 0, at: [10.0, 10.0], kind: ComposerKind::Edit(1), text: "the first note".into(), focus: false }); - view.comments.gesture = Some(Gesture::Move { page: 0, index: 1, from: pos2(0.0, 0.0) }); + let xf = PageXform { rect: Rect::from_min_size(Pos2::ZERO, vec2(300.0, 400.0)), rot: 0, pw: 300.0, ph: 400.0 }; + let placed = [placed_page(&xf, 1.0)]; + let map = page_map(&placed, &info, 0); + let deleted = bridge::edit(AnnotationEdit::Delete(vec![bridge::id_of(0, 1)]), &map, &mut view.comments, &info, &CommentPrefs::default()); + assert!(deleted.is_some(), "the unnamed comment is deletable"); let mut shifted = info.clone(); shifted.annotations = vec![annot(1)]; view.document_changed(&shifted); - assert_eq!(view.comments.selected, None); - assert_eq!(view.comments.editing, None); + assert!(view.comments.selection().is_empty()); + assert_eq!(view.comments.panel.editing, None); assert_eq!(view.comments.composer, None); - assert_eq!(view.comments.gesture, None); } /// Review on #4358: a Replace Text group is selected by its caret, the group's primary (ISO @@ -827,84 +536,76 @@ fn a_replace_text_group_makes_replace_text_defaults() { assert_eq!(tool_for_default(&all, &all[2], None), Some(CommentTool::Caret)); } -/// The hover outline goes round the comment a press under the pointer would take (the top hit -/// there): not a highlight under a form field, which takes the press. +/// The one part the page registers at the pointer is for the comment a press there would take (the same `pick_at` +/// annotation), and none where nothing would take it. #[test] fn the_hover_outline_goes_round_what_a_press_would_take() { - use super::paint::hover_outline; - use crate::page_cursor::Layer; - use crate::page_router::{CommentPart, Hit, Owner}; let page = PageInfo { width: 200.0, height: 200.0, label: String::new(), crop: [0.0, 0.0, 200.0, 200.0], rotation: 0 }; let xf = PageXform { rect: Rect::from_min_size(Pos2::ZERO, vec2(200.0, 200.0)), rot: 0, pw: 200.0, ph: 200.0 }; let highlight = Annotation { rect: [50.0, 50.0, 150.0, 150.0], ..annotation("Highlight", None, false) }; let info = DocInfo { pages: vec![page], annotations: vec![highlight], ..Default::default() }; let prefs = CommentPrefs::default(); - // The pointer is over the highlight. - let ptr = PagePointer { pos: Some(pos2(100.0, 100.0)), ..PagePointer::default() }; - let cx = PageCx { - page: 0, - xf: &xf, - ptr: &ptr, - info: &info, - tool: QuickTool::Select, - prefs: &prefs, - allowed: true, - hidden: false, - redact_mark: ([0.0; 3], 1.0), - keys: Default::default(), - }; - let on = |owner, layer| Hit::new(owner, layer, egui::CursorIcon::PointingHand); - let comment = on(Owner::Comment { index: 0, part: CommentPart::Body }, Layer::Comment); - let field = on(Owner::Field { field: 0, widget: 0 }, Layer::Field); - let cv = CommentView::default(); - assert!(hover_outline(&cx, &cv, Some(&comment)).is_some(), "the highlight a press there selects"); - assert!(hover_outline(&cx, &cv, Some(&field)).is_none(), "not under a field that takes the press"); - assert!(hover_outline(&cx, &cv, None).is_none(), "nor where nothing would take it"); - let selected = CommentView { selected: Some((0, 0)), ..CommentView::default() }; - assert!(hover_outline(&cx, &selected, Some(&comment)).is_none(), "the selected comment has its own frame"); -} - -#[test] -fn comment_marker_wins_over_an_overlapping_body_for_pick_and_hover() { - let page = PageInfo { width: 200.0, height: 200.0, label: String::new(), crop: [0.0, 0.0, 200.0, 200.0], rotation: 0 }; - let xf = PageXform { rect: Rect::from_min_size(Pos2::ZERO, vec2(200.0, 200.0)), rot: 0, pw: 200.0, ph: 200.0 }; - let markup = Annotation { rect: [50.0, 50.0, 80.0, 65.0], index: 0, ..annotation("Highlight", Some("Anchor"), false) }; - let body = Annotation { rect: [0.0, 0.0, 200.0, 200.0], index: 1, ..annotation("Square", Some("Body"), false) }; - let info = DocInfo { pages: vec![page], annotations: vec![body, markup], ..Default::default() }; - let prefs = CommentPrefs::default(); - let ptr = PagePointer::default(); - let cx = PageCx { - page: 0, - xf: &xf, - ptr: &ptr, - info: &info, - tool: QuickTool::Select, - prefs: &prefs, - allowed: true, - hidden: false, - redact_mark: ([0.0; 3], 1.0), - keys: crate::page_keys::PageKeys::new(false), - }; - let marker = cx.comment_marker_rect(&info.annotations[1]).unwrap(); - assert_eq!(cx.comment_at(marker.center()).map(|a| a.index), Some(0)); - let ptr = PagePointer { pos: Some(marker.center()), ..PagePointer::default() }; - let cx = PageCx { ptr: &ptr, ..cx }; + let placed = [placed_page(&xf, 1.0)]; + let map = page_map(&placed, &info, 0); let mut view = DocView::new(pdfcraft_engine::DocId(1), &info, crate::canvas::ViewDefaults::default()); - super::input::describe(&cx, &mut view); - let ctx = egui::Context::default(); - assert_eq!(view.cursor.resolve(&ctx, QuickTool::Select).as_deref(), Some("Highlight\nAnchor")); - let hidden = PageCx { hidden: true, ..cx }; - assert!(hidden.comment_at(marker.center()).is_none()); - super::input::describe(&hidden, &mut view); - assert!(view.cursor.resolve(&ctx, QuickTool::Select).is_none()); + let tolerance = map.doc_len(SLOP); + let side = icon_side(&map); + let over = Point::new(100.0, 100.0); + let annotations = bridge::annotations(&info, &map, QuickTool::Select, true); + let markers = bridge::markers(&info, &map, side); + assert_eq!(pick_at(&annotations, over, tolerance, side, &markers), Some(bridge::id_of(0, 0)), "the highlight a press there selects"); + // PLAN: the outline shape itself is the overlay's `HoverOutline` (pdf-comments-w8); a request to w8 for an `overlay` helper + // the test can compare against the part's annotation. The part registered at the pointer is asserted here. + let parts = |view: &DocView, pointer| bridge::parts(&info, &map, &prefs, QuickTool::Select, pointer, &view.comments.tool, tolerance, side); + assert_eq!(parts(&view, Some(over)).len(), 1, "one part for the comment under the pointer"); + assert!(parts(&view, None).is_empty(), "no pointer, no part"); + assert!(parts(&view, Some(Point::new(5.0, 5.0))).is_empty(), "nor where nothing would take the press"); + // Over the comment that is selected, no outline part: the handles are what is there. + view.comments.select_only((0, 0)); + assert!(parts(&view, Some(over)).is_empty(), "no outline over a selected comment"); } +/// A silent transform error would carry into every saved comment: the user point survives the trip through the document +/// space on a turned page, a turned view and a zoomed page, and a move by one document unit moves the annotation by the +/// same distance in user space. #[test] -fn insert_text_icon_is_distinct_from_edit_text_and_matches_its_command() { - let insert = CommentTool::Caret.icon(); - let edit = pdfcraft_engine::commands::command("edit.edit_text").unwrap().icon; - let command = pdfcraft_engine::commands::command("comment.caret").unwrap().icon; - assert_ne!(insert, edit, "adjacent tools need distinct icons"); - assert_eq!(insert, command, "the annotation toolbar and command use the same icon"); - assert!(crate::widgets::icons::exists(insert)); +fn an_annotation_round_trips_through_the_page_map_on_turned_and_zoomed_pages() { + for (page_rotation, rotation, zoom) in [(90, 0, 1.0), (0, 90, 1.0), (0, 0, 4.0)] { + let (pw, ph) = if page_rotation % 180 == 0 { (300.0, 200.0) } else { (200.0, 300.0) }; + let (w, h) = if rotation % 180 == 0 { (pw, ph) } else { (ph, pw) }; + let xf = PageXform { rect: Rect::from_min_size(pos2(40.0, 60.0), vec2(w, h) * zoom), rot: rotation, pw, ph }; + let square = Annotation { rect: [40.0, 60.0, 100.0, 100.0], ..annotation("Square", Some("Box"), false) }; + let info = DocInfo { + pages: vec![PageInfo { width: pw, height: ph, label: String::new(), crop: [0.0, 0.0, 300.0, 200.0], rotation: page_rotation }], + annotations: vec![square], + ..Default::default() + }; + let placed = [placed_page(&xf, f64::from(zoom))]; + let map = page_map(&placed, &info, rotation as u16); + let case = format!("page/view rotation {page_rotation}/{rotation}, zoom {zoom}"); + for user in [[10.0, 20.0], [250.0, 150.0]] { + let back = map.to_user(0, map.to_doc(0, user)); + assert!((back[0] - user[0]).abs() < 1e-6 && (back[1] - user[1]).abs() < 1e-6, "{case}: {user:?} came back as {back:?}"); + } + let mut view = DocView::new(pdfcraft_engine::DocId(1), &info, crate::canvas::ViewDefaults::default()); + let delta = Vec2::new(1.0, 0.0); + let moved = bridge::edit(AnnotationEdit::Move { ids: vec![bridge::id_of(0, 0)], delta }, &map, &mut view.comments, &info, &CommentPrefs::default()); + let Some(Edit::MoveAnnotation { dx, dy, .. }) = moved else { panic!("{case}: a move is one MoveAnnotation, got {moved:?}") }; + let origin = map.to_doc(0, [50.0, 80.0]); + let (from, to) = (map.to_user(0, origin), map.to_user(0, origin + delta)); + assert!((dx - (to[0] - from[0])).abs() < 1e-6 && (dy - (to[1] - from[1])).abs() < 1e-6, "{case}: moved ({dx}, {dy})"); + } +} + +/// A hand-written format boundary: a wrong zone shifts every shown age and date. +#[test] +fn a_pdf_date_with_a_zone_reads_as_the_same_instant_in_unix_seconds() { + let zoned = bridge::pdf_date_seconds("D:20240102030405+02'00'"); + assert!(zoned.is_some(), "a zoned date reads"); + assert_eq!(zoned, bridge::pdf_date_seconds("D:20240102010405Z"), "the same instant in UTC"); + let day = bridge::pdf_date_seconds("D:20240102"); + assert!(day.is_some()); + assert_eq!(day, bridge::pdf_date_seconds("D:20240102000000Z"), "no time part reads midnight UTC"); + assert_eq!(bridge::pdf_date_seconds("D:20240103").zip(day).map(|(next, this)| next - this), Some(86_400)); + assert_eq!(bridge::pdf_date_seconds("not a date"), None); } diff --git a/crates/ui-egui/tests/it/document_links.rs b/crates/ui-egui/tests/it/document_links.rs index 3690abcea9..5ff24862fc 100644 --- a/crates/ui-egui/tests/it/document_links.rs +++ b/crates/ui-egui/tests/it/document_links.rs @@ -228,11 +228,11 @@ fn a_web_bookmark_asks_first_like_a_link() { let mut h = bookmark_harness(None); click_bookmark(&mut h, "Website"); assert_eq!(h.state().pending_link, pending(BOOKMARK_URL, LinkOrigin::Link)); - assert_eq!(h.state().last_opened_url, None, "nothing opens before the user answers"); + assert!(support::step_collecting_urls(&mut h, 1).is_empty(), "nothing opens before the user answers"); assert_eq!(h.state().views[0].current, 0); h.get_by_label("Cancel").click(); - h.run_steps(3); - assert_eq!((h.state().pending_link.clone(), h.state().last_opened_url.clone()), (None, None)); + assert!(support::step_collecting_urls(&mut h, 3).is_empty()); + assert_eq!(h.state().pending_link, None); click_bookmark(&mut h, "Appendix"); assert_eq!(h.state().views[0].current, 2); @@ -275,22 +275,25 @@ fn bookmarks_go_through_the_link_policy() { ("Missing", "/Dest (nowhere)".into()), ("Absolute", format!("/A << /S /GoToR /F ({file}) /D [0 /Fit] >>")), ]); - let mut h = bookmark_harness_with(None, pdf); for (title, notice) in [ ("Script", Some("This PDF uses the unsupported JavaScript action.")), ("Program", Some("This PDF uses the unsupported Launch action.")), ("Missing", None), ] { - h.state_mut().toast = None; + // A fresh app per row: each row's notice is its own. + let mut h = bookmark_harness_with(None, pdf.clone()); click_bookmark(&mut h, title); + assert_eq!(support::shown_notice(&h).as_deref(), notice, "{title}"); + let opened = support::step_collecting_urls(&mut h, 2); let s = h.state(); - assert_eq!(s.toast.as_ref().map(|t| t.0.as_str()), notice, "{title}"); - assert_eq!((&s.pending_link, &s.last_opened_url, s.views.len(), s.views[0].current), (&None, &None, 1, 0), "{title}: nothing opened"); + assert_eq!((&s.pending_link, s.views.len(), s.views[0].current), (&None, 1, 0), "{title}: nothing opened"); + assert!(opened.is_empty(), "{title}: no address opened: {opened:?}"); } + let mut h = bookmark_harness_with(None, pdf); click_bookmark(&mut h, "Absolute"); assert_eq!(h.state().pending_remote_link.as_ref().map(|p| p.path.clone()), Some(target)); assert_eq!(h.state().views.len(), 1); - assert!(h.state().toast.is_none(), "the file is not touched before consent"); + assert!(support::shown_notice(&h).is_none(), "the file is not touched before consent"); } #[test] @@ -299,11 +302,10 @@ fn a_link_asks_first_and_opens_when_allowed() { let mut h = harness(); click_link(&mut h); assert_eq!(h.state().pending_link, pending(LINK_URL, LinkOrigin::Link)); - assert_eq!(h.state().last_opened_url, None, "nothing opens before the user answers"); + assert!(support::step_collecting_urls(&mut h, 1).is_empty(), "nothing opens before the user answers"); h.get_by_label("example.org"); h.get_by_label("Open link").click(); - h.run_steps(3); - assert_eq!(h.state().last_opened_url.as_deref(), Some(LINK_URL)); + assert_eq!(support::step_collecting_urls(&mut h, 3), [LINK_URL]); assert_eq!(h.state().pending_link, None); } @@ -320,7 +322,7 @@ fn a_local_gotor_link_opens_in_a_tab_and_applies_its_destination() { assert_eq!(h.state().views.len(), 2); assert_eq!(h.state().active, Some(1)); assert_eq!(h.state().views[1].current, 1, "the remote page destination is applied"); - assert_eq!(h.state().last_opened_url, None, "no URL or external launcher is involved"); + assert!(support::step_collecting_urls(&mut h, 1).is_empty(), "no URL or external launcher is involved"); } /// Three 300×400 pages whose catalog /Dests names `there` (page 3) and `here` (page 2). @@ -359,14 +361,15 @@ fn a_gotor_link_to_a_named_destination_opens_at_its_page() { click_remote_link(&mut h); // The name is looked up off the UI thread, so the tab may open before it moves there. support::wait_until(&mut h, "remote document at its destination", |app| app.views.len() == 2 && app.views[1].current == page); - assert_eq!(h.state().toast, None, "{destination}: applied, so nothing to warn about"); + assert!(support::shown_notice(&h).is_none(), "{destination}: applied, so nothing to warn about"); } // A name the target doesn't define opens it at its first page, with the warning. let mut h = remote_harness(Some(dir.join("source.pdf").to_string_lossy().into_owned()), "target.pdf", "/elsewhere"); click_remote_link(&mut h); - support::wait_until(&mut h, "remote document and the warning", |app| app.views.len() == 2 && app.toast.is_some()); + assert!(support::step_until(&mut h, |h| h.state().views.len() == 2 && support::shown_notice(h).is_some()), "remote document and the warning"); assert_eq!(h.state().views[1].current, 0); - assert!(h.state().toast.as_ref().is_some_and(|(text, _)| text.contains("named destination was not applied")), "{:?}", h.state().toast); + let notice = support::shown_notice(&h); + assert!(notice.as_deref().is_some_and(|text| text.contains("named destination was not applied")), "{notice:?}"); } #[cfg(not(target_arch = "wasm32"))] @@ -420,9 +423,9 @@ fn a_gotor_link_to_a_directory_or_device_is_refused_without_reading_it() { for target in targets { let mut h = remote_harness(Some(dir.join("source.pdf").to_string_lossy().into_owned()), target, "[0 /Fit]"); click_remote_link(&mut h); - support::wait_until(&mut h, "relative invalid target refusal", |app| app.toast.is_some()); + assert!(support::step_until(&mut h, |h| support::shown_notice(h).is_some()), "relative invalid target refusal"); assert_eq!(h.state().views.len(), 1, "{target} opens no tab"); - let toast = &h.state().toast.as_ref().unwrap().0; + let toast = support::shown_notice(&h).unwrap().to_string(); assert!(toast.contains("not a regular file"), "{target}: {toast}"); } let _ = std::fs::remove_dir_all(&dir); @@ -435,7 +438,7 @@ fn an_in_memory_source_does_not_resolve_a_relative_gotor_against_cwd() { let mut h = remote_harness(None, "target.pdf", "[0 /Fit]"); click_remote_link(&mut h); assert_eq!(h.state().views.len(), 1); - let toast = &h.state().toast.as_ref().unwrap().0; + let toast = support::shown_notice(&h).unwrap().to_string(); assert!(toast.contains("relative link needs a source document path"), "{toast}"); } @@ -445,10 +448,10 @@ fn unsupported_launch_action_is_reported_without_claiming_javascript() { let mut h = harness(); click_link_at(&mut h, 230.0); assert_eq!(h.state().pending_link, None); - assert_eq!(h.state().last_opened_url, None); + assert!(support::step_collecting_urls(&mut h, 1).is_empty()); assert_eq!(h.state().views.len(), 1); assert_eq!(h.state().views[0].current, 0); - let toast = h.state().toast.clone().expect("the unsupported action is reported").0; + let toast = support::shown_notice(&h).expect("the unsupported action is reported").to_string(); assert_eq!(toast, "This PDF uses the unsupported Launch action."); } @@ -459,9 +462,8 @@ fn cancel_opens_nothing() { click_link(&mut h); assert!(h.state().pending_link.is_some()); h.get_by_label("Cancel").click(); - h.run_steps(3); + assert!(support::step_collecting_urls(&mut h, 3).is_empty()); assert_eq!(h.state().pending_link, None); - assert_eq!(h.state().last_opened_url, None); } #[test] @@ -471,9 +473,8 @@ fn escape_cancels() { click_link(&mut h); assert!(h.state().pending_link.is_some()); h.key_press(egui::Key::Escape); - h.run_steps(3); + assert!(support::step_collecting_urls(&mut h, 3).is_empty()); assert_eq!(h.state().pending_link, None); - assert_eq!(h.state().last_opened_url, None); } #[test] @@ -482,7 +483,7 @@ fn a_button_uri_action_asks_first() { let mut h = harness(); click_field(&mut h, "web"); assert_eq!(h.state().pending_link, pending(BUTTON_URL, LinkOrigin::Button)); - assert_eq!(h.state().last_opened_url, None); + assert!(support::step_collecting_urls(&mut h, 1).is_empty()); } #[test] @@ -490,10 +491,13 @@ fn a_button_script_cannot_open_a_local_program() { let _gpu = gpu(); let mut h = harness(); click_field(&mut h, "sneaky"); - support::wait_until(&mut h, "script link refusal", |app| app.pending_link.is_some() || app.toast.is_some()); + assert!( + support::step_until(&mut h, |h| h.state().pending_link.is_some() || support::shown_notice(h).is_some()), + "script link refusal" + ); assert_eq!(h.state().pending_link, None, "a file: address is never offered"); - assert_eq!(h.state().last_opened_url, None); - let toast = h.state().toast.clone().expect("the user is told").0; + assert!(support::step_collecting_urls(&mut h, 1).is_empty()); + let toast = support::shown_notice(&h).expect("the user is told").to_string(); assert!(toast.contains("“file:” address"), "{toast}"); } @@ -506,7 +510,7 @@ fn a_script_asks_first_and_only_once() { h.state_mut().run_button_script(id, "web", script); support::wait_until(&mut h, "script link request", |app| app.pending_link.is_some()); assert_eq!(h.state().pending_link, pending("https://example.org/?q=first", LinkOrigin::Script), "one dialog, for the first request"); - assert_eq!(h.state().last_opened_url, None); + assert!(support::step_collecting_urls(&mut h, 1).is_empty()); } #[test] @@ -527,10 +531,14 @@ fn a_script_cannot_open_other_kinds_of_address() { let id = h.state().views[0].id; let script = format!("app.launchURL({});", serde_json::to_string(url).unwrap()); h.state_mut().run_button_script(id, "web", &script); - support::wait_until(&mut h, "script link decision", |app| app.pending_link.is_some() || app.toast.is_some()); + assert!( + support::step_until(&mut h, |h| h.state().pending_link.is_some() || support::shown_notice(h).is_some()), + "script link decision" + ); assert_eq!(h.state().pending_link, None, "{url}"); - assert_eq!(h.state().last_opened_url, None, "{url}"); - assert!(h.state().toast.clone().is_some_and(|t| t.0.contains("won't open")), "{url}: {:?}", h.state().toast); + assert!(support::step_collecting_urls(&mut h, 1).is_empty(), "{url}"); + let notice = support::shown_notice(&h); + assert!(notice.as_deref().is_some_and(|t| t.contains("won't open")), "{url}: {notice:?}"); } } @@ -542,10 +550,13 @@ fn an_email_link_cannot_attach_a_local_file() { let id = h.state().views[0].id; let script = format!("app.launchURL({});", serde_json::to_string(url).unwrap()); h.state_mut().run_button_script(id, "web", &script); - support::wait_until(&mut h, "script link decision", |app| app.pending_link.is_some() || app.toast.is_some()); + assert!( + support::step_until(&mut h, |h| h.state().pending_link.is_some() || support::shown_notice(h).is_some()), + "script link decision" + ); assert_eq!(h.state().pending_link, None, "{url}"); - assert_eq!(h.state().last_opened_url, None, "{url}"); - let toast = h.state().toast.clone().expect("the user is told").0; + assert!(support::step_collecting_urls(&mut h, 1).is_empty(), "{url}"); + let toast = support::shown_notice(&h).expect("the user is told").to_string(); assert!(toast.contains("attach or insert a file"), "{url}: {toast}"); } } @@ -608,7 +619,7 @@ fn an_absolute_gotor_waits_for_consent_and_keeps_its_destination_across_tabs() { h.get_by_label("Allow"); assert_eq!(h.state().views.len(), 1); assert_eq!(h.state().session.docs().len(), 1); - assert!(h.state().toast.is_none(), "the nonexistent target is not accessed before consent"); + assert!(support::shown_notice(&h).is_none(), "the nonexistent target is not accessed before consent"); assert!(h.state().password_prompt.is_none()); // The target only appears AFTER the confirmation is displayed. let target_bytes = linked_pages(2, None); @@ -627,7 +638,7 @@ fn an_absolute_gotor_waits_for_consent_and_keeps_its_destination_across_tabs() { assert_eq!(target_doc.path.as_deref(), Some(file)); assert_eq!(target_doc.bytes.as_slice(), target_bytes.as_slice()); assert!(!h.state().session.get(source).unwrap().dirty); - assert_eq!(h.state().last_opened_url, None); + assert!(support::step_collecting_urls(&mut h, 1).is_empty()); std::fs::remove_dir_all(dir).unwrap(); } @@ -657,7 +668,7 @@ fn cancelling_an_absolute_gotor_by_button_escape_or_dismissal_changes_nothing() assert_eq!(doc.bytes, before); assert!(!doc.dirty); assert!(doc.can_undo().is_none()); - assert!(h.state().toast.is_none()); + assert!(support::shown_notice(&h).is_none()); assert!(h.state().password_prompt.is_none()); } } @@ -692,14 +703,15 @@ fn consenting_to_absolute_gotor_still_validates_the_target() { let before = h.state().session.get(source).unwrap().bytes.clone(); click_remote_link(&mut h); assert!(h.state().pending_remote_link.is_some()); - assert!(h.state().toast.is_none(), "{name} isn't checked before consent"); + assert!(support::shown_notice(&h).is_none(), "{name} isn't checked before consent"); h.get_by_label("Allow").click(); - support::wait_until(&mut h, "approved invalid target refusal", |app| { - app.toast.as_ref().is_some_and(|(message, _)| message.starts_with(expected_error)) - }); + assert!( + support::step_until(&mut h, |h| support::shown_notice(h).is_some_and(|message| message.starts_with(expected_error))), + "approved invalid target refusal" + ); assert!(h.state().pending_remote_link.is_none()); assert_eq!(h.state().views.len(), 1, "{name}"); - assert!(h.state().toast.is_some(), "{name} is refused after consent"); + assert!(support::shown_notice(&h).is_some(), "{name} is refused after consent"); assert_eq!(h.state().session.docs().len(), 1); assert_eq!(h.state().active_ids().map(|(_, id)| id), Some(source)); assert_eq!(h.state().views[0].current, 0); @@ -708,7 +720,7 @@ fn consenting_to_absolute_gotor_still_validates_the_target() { assert!(!doc.dirty); assert!(doc.can_undo().is_none()); assert!(h.state().password_prompt.is_none()); - assert!(h.state().last_opened_url.is_none()); + assert!(support::step_collecting_urls(&mut h, 1).is_empty()); } std::fs::remove_dir_all(dir).unwrap(); } @@ -763,7 +775,7 @@ fn closing_the_source_invalidates_pending_absolute_gotor_consent() { assert!(h.state().views.is_empty()); assert!(h.state().session.docs().is_empty()); assert!(h.state().pending_remote_link.is_none()); - assert!(h.state().toast.is_none()); + assert!(support::shown_notice(&h).is_none()); } fn overlapping_annotation_fixture(subtype: &str, internal: bool) -> Vec { @@ -828,11 +840,10 @@ fn highlighted_link_still_requires_confirmation() { let mut h = overlapping_annotation_harness(overlapping_annotation_fixture("Highlight", false)); click_link(&mut h); assert_eq!(h.state().pending_link, pending(LINK_URL, LinkOrigin::Link)); - assert_eq!(h.state().last_opened_url, None); - assert_eq!(h.state().views[0].comments.selected, None); + assert!(support::step_collecting_urls(&mut h, 1).is_empty()); + assert!(h.state().views[0].comments.selection().is_empty()); h.get_by_label("Open link").click(); - h.run_steps(3); - assert_eq!(h.state().last_opened_url.as_deref(), Some(LINK_URL)); + assert_eq!(support::step_collecting_urls(&mut h, 3), [LINK_URL]); assert_eq!(h.state().pending_link, None); h.run_steps(3); assert_eq!(h.state().pending_link, None, "approval must not create another prompt"); @@ -850,9 +861,8 @@ fn highlighted_link_cancel_and_escape_do_not_open_url() { } else { h.get_by_label("Cancel").click(); } - h.run_steps(3); + assert!(support::step_collecting_urls(&mut h, 3).is_empty()); assert_eq!(h.state().pending_link, None); - assert_eq!(h.state().last_opened_url, None); } } @@ -868,7 +878,7 @@ fn highlighted_internal_link_navigates_after_save_and_reopen() { click_link(&mut h); assert_eq!(h.state().views[0].current, 1); assert_eq!(h.state().pending_link, None); - assert_eq!(h.state().last_opened_url, None); + assert!(support::step_collecting_urls(&mut h, 3).is_empty()); } #[test] @@ -878,9 +888,9 @@ fn highlight_outside_link_remains_selectable() { let rect = h.state().views[0].page_screen_rect(0).unwrap(); let at = egui::pos2(rect.min.x + rect.width() * (60.0 / 300.0), rect.min.y + rect.height() * (1.0 - 325.0 / 400.0)); click_at(&mut h, at); - assert_eq!(h.state().views[0].comments.selected, Some((0, 1))); + assert_eq!(h.state().views[0].comments.selection(), [(0, 1)]); assert_eq!(h.state().pending_link, None); - assert_eq!(h.state().last_opened_url, None); + assert!(support::step_collecting_urls(&mut h, 2).is_empty()); } #[test] @@ -889,9 +899,9 @@ fn other_annotations_over_links_keep_select_click() { for subtype in ["Text", "Square"] { let mut h = overlapping_annotation_harness(overlapping_annotation_fixture(subtype, false)); click_link(&mut h); - assert_eq!(h.state().views[0].comments.selected, Some((0, 1)), "{subtype}"); + assert_eq!(h.state().views[0].comments.selection(), [(0, 1)], "{subtype}"); assert_eq!(h.state().pending_link, None, "{subtype}"); - assert_eq!(h.state().last_opened_url, None, "{subtype}"); + assert!(support::step_collecting_urls(&mut h, 2).is_empty(), "{subtype}"); } } @@ -901,11 +911,10 @@ fn form_widget_keeps_priority_over_highlighted_link() { let mut h = overlapping_annotation_harness(overlapping_annotation_fixture_with_form("Highlight", false, true)); click_link(&mut h); assert_eq!(h.state().pending_link, pending(BUTTON_URL, LinkOrigin::Button)); - assert_eq!(h.state().last_opened_url, None); + assert!(support::step_collecting_urls(&mut h, 1).is_empty()); h.get_by_label("Cancel").click(); - h.run_steps(3); + assert!(support::step_collecting_urls(&mut h, 3).is_empty()); assert_eq!(h.state().pending_link, None); - assert_eq!(h.state().last_opened_url, None); } /// A comment's gesture keeps the page's input until its button is up: a press held through it and @@ -916,16 +925,19 @@ fn active_comment_gesture_keeps_highlighted_link_click() { let _gpu = gpu(); let mut h = overlapping_annotation_harness(overlapping_annotation_fixture("Highlight", false)); let r = h.state().views[0].page_screen_rect(0).unwrap(); - let at = egui::pos2(r.min.x + r.width() * 0.5, r.min.y + r.height() * (1.0 - 325.0 / 400.0)); - h.hover_at(at); + // The link spans x 100..250 of the 300-unit page and the highlight x 30..200: the drag starts and ends inside both, far + // past the press slop (40 units at 150 %). + let along = |x: f32| egui::pos2(r.min.x + r.width() * (x / 300.0), r.min.y + r.height() * (1.0 - 325.0 / 400.0)); + let (from, to) = (along(150.0), along(190.0)); + h.hover_at(from); h.run_steps(2); - h.drag_at(at); + h.drag_at(from); h.run_steps(1); - h.state_mut().views[0].comments.gesture = Some(pdfcraft_ui_egui::comments::Gesture::Move { page: 0, index: 1, from: at }); - h.drop_at(at); - h.run_steps(4); + h.hover_at(to); + h.run_steps(2); + h.drop_at(to); + assert!(support::step_collecting_urls(&mut h, 4).is_empty()); assert_eq!(h.state().pending_link, None); - assert_eq!(h.state().last_opened_url, None); } #[test] @@ -940,9 +952,9 @@ fn highlighted_link_context_click_selects_comment_without_opening_url() { h.event(egui::Event::PointerButton { pos: at, button: egui::PointerButton::Secondary, pressed, modifiers: Default::default() }); } h.run_steps(3); - assert_eq!(h.state().views[0].comments.selected, Some((0, 1))); + assert_eq!(h.state().views[0].comments.selection(), [(0, 1)]); assert_eq!(h.state().pending_link, None); - assert_eq!(h.state().last_opened_url, None); + assert!(support::step_collecting_urls(&mut h, 2).is_empty()); } #[test] @@ -952,11 +964,10 @@ fn highlighted_link_can_be_requested_again_after_cancel() { for _ in 0..2 { click_link(&mut h); assert_eq!(h.state().pending_link, pending(LINK_URL, LinkOrigin::Link)); - assert_eq!(h.state().last_opened_url, None); + assert!(support::step_collecting_urls(&mut h, 1).is_empty()); h.get_by_label("Cancel").click(); - h.run_steps(120); + assert!(support::step_collecting_urls(&mut h, 120).is_empty()); assert_eq!(h.state().pending_link, None); - assert_eq!(h.state().last_opened_url, None); } } @@ -1030,7 +1041,7 @@ fn addresses_written_in_the_page_text_open_like_links() { ] { assert!(click_text_until_pending(&mut h, y), "a click on the line at {y} asked to open nothing"); assert_eq!(h.state().pending_link, pending(url, LinkOrigin::Link), "line at {y}"); - assert_eq!(h.state().last_opened_url, None, "nothing opens before the user answers"); + assert!(support::step_collecting_urls(&mut h, 1).is_empty(), "nothing opens before the user answers"); h.get_by_label("Cancel").click(); h.run_steps(4); assert_eq!(h.state().pending_link, None); diff --git a/crates/ui-egui/tests/perf.rs b/crates/ui-egui/tests/perf.rs index 6200686553..b411229078 100644 --- a/crates/ui-egui/tests/perf.rs +++ b/crates/ui-egui/tests/perf.rs @@ -1,63 +1,12 @@ -//! Frame-time budget on a 500-page document (parity view.large-document-performance): scrolling -//! continuously must keep the UI's own per-frame work far below a 60 fps frame. Rendering runs on -//! worker threads and is not part of the measurement. +//! Work counts on a 500-page document (parity view.large-document-performance): scrolling and wheel frames build only +//! the pages and rows in view, never the whole document. Each assertion is a relation between counts of the same +//! script on a small and a large document, from the component runs the framework counts, so a slow or busy machine +//! cannot fail it and a regression cannot hide behind a fast one. Rendering runs on worker threads and is not counted. +use craft::ui::testing::last_pass_runs; use egui_kittest::Harness; use pdfcraft_ui_egui::PdfCraftApp; -/// The one-minute load average, where the OS reports it (macOS `vm.loadavg`, Linux -/// `/proc/loadavg`). -fn load_average() -> Option { - if let Ok(s) = std::fs::read_to_string("/proc/loadavg") { - return s.split_whitespace().next()?.parse().ok(); - } - let out = std::process::Command::new("sysctl").args(["-n", "vm.loadavg"]).output().ok()?; - String::from_utf8_lossy(&out.stdout).trim_matches(|c: char| c == '{' || c == '}' || c.is_whitespace()).split_whitespace().next()?.parse().ok() -} - -/// Frame budgets only mean something on a machine that isn't overcommitted: with the load -/// average above twice the core count (other builds running), skip and say so. -fn overloaded() -> bool { - let cores = std::thread::available_parallelism().map_or(4, std::num::NonZeroUsize::get) as f64; - match load_average() { - Some(l) if l > 2.0 * cores => { - eprintln!("PERF skipped: load average {l:.0} on {cores} cores; frame budgets need a quiet machine"); - true - } - _ => false, - } -} - -/// The budget tests take turns: each starts render workers, and measuring one while another -/// runs would charge it for the other's work on a small CI machine. -static SERIAL: std::sync::Mutex<()> = std::sync::Mutex::new(()); - -/// Generous for debug builds and shared CI machines; release builds are ~10× faster. -const BUDGET: std::time::Duration = std::time::Duration::from_millis(40); - -/// The average frame over `frames` steps, best of up to three rounds: another process taking the -/// CPU only ever adds time, so the fastest round is the closest to the UI's own cost, and a real -/// regression is over budget in every round. Later rounds run only when a round is over budget. -fn best_average(label: &str, frames: usize, mut step: impl FnMut(usize)) -> std::time::Duration { - let mut best = std::time::Duration::MAX; - for round in 1..=3 { - let start = std::time::Instant::now(); - let mut worst = std::time::Duration::ZERO; - for f in 0..frames { - let t = std::time::Instant::now(); - step(f); - worst = worst.max(t.elapsed()); - } - let avg = start.elapsed() / frames as u32; - eprintln!("PERF {label} round {round}: avg frame {avg:?}, worst {worst:?}"); - best = best.min(avg); - if best < BUDGET { - break; - } - } - best -} - /// `n` text pages with a few comments each, so panels and overlays have work to do. fn big(n: usize) -> Vec { let mut objs: Vec = vec!["<< /Type /Catalog /Pages 2 0 R >>".into()]; @@ -110,122 +59,133 @@ fn named_comments(n: usize) -> Vec { pdf(objs) } -#[test] -fn collapsed_comments_with_ten_thousand_named_roots_stay_within_the_frame_budget() { - if overloaded() { - return; - } - let _turn = SERIAL.lock().unwrap_or_else(std::sync::PoisonError::into_inner); - let bytes = named_comments(10_000); - let mut h = Harness::builder().with_size(egui::vec2(1400.0, 900.0)).build_eframe(move |_cc| { - let mut app = PdfCraftApp::new(); - app.set_option("language", "en").unwrap(); - app.open_bytes("review.pdf", None, bytes).expect("opens"); - app.set_option("left", "closed").unwrap(); - app.set_option("panel", "comments").unwrap(); - app.views[0].comments.collapsed_pages = vec![0]; - app - }); - h.run_steps(4); - { - let state = h.state(); - let info = &state.session.get(state.views[0].id).unwrap().info; - assert_eq!(info.pages.len(), 1); - assert_eq!(info.annotations.len(), 10_000); - for a in &info.annotations { - assert_eq!(a.name.as_deref(), Some(format!("root-{}", a.index).as_str())); - assert!(a.in_reply_to.is_none()); - assert!(a.state.is_none()); - } - assert_eq!(state.views[0].comments.collapsed_pages, [0]); - } - // Folding removes card layout cost, so this measures filtering and thread lookup work. - let avg = best_average("collapsed named comments", 30, |_| h.step()); - assert!(avg < BUDGET, "collapsed comments: average frame {avg:?}"); -} - -#[test] -fn scrolling_a_500_page_document_stays_within_the_frame_budget() { - if overloaded() { - return; - } - let _turn = SERIAL.lock().unwrap_or_else(std::sync::PoisonError::into_inner); - let bytes = big(500); - let t0 = std::time::Instant::now(); - let mut h = Harness::builder().with_size(egui::vec2(1400.0, 900.0)).build_eframe(move |_cc| { - let mut app = PdfCraftApp::new(); - app.set_option("language", "en").unwrap(); - app.open_bytes("big.pdf", None, bytes).expect("opens"); - app - }); - h.run_steps(4); - eprintln!("PERF open {:?}", t0.elapsed()); - // Scroll through the document: 200 frames, jumping ~2 pages per frame. - let avg = best_average("scroll", 200, |f| { - h.state_mut().views[0].goto = Some(((f * 5) / 2 % 500, 0.3)); - h.step(); - }); - assert!(avg < BUDGET, "average frame {avg:?}"); -} - -/// Measures real wheel events and their idle frames, separately from page-jump scheduling. -#[test] -fn regular_wheel_input_on_500_pages_stays_within_the_frame_budget() { - if overloaded() { - return; - } - let _turn = SERIAL.lock().unwrap_or_else(std::sync::PoisonError::into_inner); - let bytes = big(500); +/// The app with `bytes` open and each of `options` set, stepped at 60 frames a second. +fn harness(bytes: Vec, options: &'static [(&'static str, &'static str)]) -> Harness<'static, PdfCraftApp> { let mut h = Harness::builder().with_size(egui::vec2(1400.0, 900.0)).with_step_dt(1.0 / 60.0).build_eframe(move |_cc| { let mut app = PdfCraftApp::new(); app.set_option("language", "en").unwrap(); - app.open_bytes("wheel-budget.pdf", None, bytes).expect("opens"); - for (key, value) in [("left", "closed"), ("panel", "none"), ("layout", "continuous"), ("zoom", "200"), ("animation_effects", "on")] { + app.open_bytes("work.pdf", None, bytes).expect("opens"); + for (key, value) in options { app.set_option(key, value).unwrap(); } app }); h.run_steps(8); - h.hover_at(h.state().views[0].viewport_rect().center()); - h.run_steps(2); - let start = h.state().views[0].current; - let average = best_average("wheel input", 200, |frame| { - if frame % 3 == 0 { - h.event(egui::Event::MouseWheel { - unit: egui::MouseWheelUnit::Line, - delta: egui::vec2(0.0, -1.0), - phase: egui::TouchPhase::Move, - modifiers: egui::Modifiers::NONE, - source: egui::MouseWheelSource::Unknown, - }); - } - h.step(); - }); - assert!(h.state().views[0].current > start, "wheel events must actually move through the document"); - assert!(average < BUDGET, "wheel input: average frame {average:?}"); + h } -#[test] -fn panels_with_hundreds_of_items_stay_within_the_frame_budget() { - if overloaded() { - return; +/// The last path segment of a component's type name, before any generics. +fn short(name: &str) -> &str { + let bare = name.split('<').next().unwrap_or(name); + bare.rsplit("::").next().unwrap_or(bare) +} + +/// What one frame cost: the runs of the component type `row` (every type when `None`) in the frame's last pass, and the +/// passes the frame ran. +fn frame_work(h: &mut Harness<'static, PdfCraftApp>, row: Option<&str>) -> (u32, u64) { + let before = h.ctx.cumulative_pass_nr(); + h.step(); + let passes = h.ctx.cumulative_pass_nr().saturating_sub(before); + let runs = last_pass_runs(&h.ctx).iter().filter(|(name, _)| row.is_none_or(|row| short(name) == row)).map(|(_, count)| *count).sum(); + (runs, passes) +} + +/// The most runs and passes in one frame over `frames` frames, `each` run before every frame. A frame that replays a settled +/// region runs nothing, so the maximum over several frames is the work of the frames that did run. +fn most_work(h: &mut Harness<'static, PdfCraftApp>, row: Option<&str>, frames: usize, mut each: impl FnMut(&mut Harness<'static, PdfCraftApp>, usize)) -> (u32, u64) { + let mut most = (0, 0); + for frame in 0..frames { + each(h, frame); + let (runs, passes) = frame_work(h, row); + most = (most.0.max(runs), most.1.max(passes)); } - let _turn = SERIAL.lock().unwrap_or_else(std::sync::PoisonError::into_inner); - let bytes = big(500); - for panel in ["comments", "pages", "fields"] { - let b = bytes.clone(); - let mut h = Harness::builder().with_size(egui::vec2(1400.0, 900.0)).build_eframe(move |_cc| { - let mut app = PdfCraftApp::new(); - app.set_option("language", "en").unwrap(); - app.open_bytes("big.pdf", None, b).expect("opens"); - app.set_option("panel", panel).unwrap(); - app + most +} + +/// A folded page builds no comment cards, and an open one builds cards for the rows in view only, not for all 10,000 roots. +#[test] +fn a_folded_page_builds_no_comment_cards_and_an_open_one_builds_only_the_rows_in_view() { + const ROOTS: u32 = 10_000; + let mut h = harness(named_comments(ROOTS as usize), &[("left", "closed"), ("panel", "comments")]); + h.state_mut().views[0].comments.panel.collapsed_pages = vec![0]; + { + let state = h.state(); + let info = &state.session.get(state.views[0].id).unwrap().info; + assert_eq!(info.pages.len(), 1); + assert_eq!(info.annotations.len(), ROOTS as usize); + for a in &info.annotations { + assert_eq!(a.name.as_deref(), Some(format!("root-{}", a.index).as_str())); + assert!(a.in_reply_to.is_none()); + assert!(a.state.is_none()); + } + assert_eq!(state.views[0].comments.panel.collapsed_pages, [0]); + } + let (folded, _) = most_work(&mut h, Some("CommentRow"), 6, |_, _| {}); + assert_eq!(folded, 0, "a folded page builds no cards"); + h.state_mut().views[0].comments.panel.collapsed_pages.clear(); + let (open, _) = most_work(&mut h, Some("CommentRow"), 6, |_, _| {}); + assert!(open > 0, "the open page builds the cards in view"); + assert!(open < ROOTS, "{open} cards for {ROOTS} roots: only the rows in view are built"); +} + +/// Jumping through a long document costs work for the pages in view only: the work at 500 pages is no more than at 50. +#[test] +fn jumping_through_pages_builds_only_the_pages_in_view() { + let work = |n: usize| { + let mut h = harness(big(n), &[]); + most_work(&mut h, None, 60, |h, f| h.state_mut().views[0].goto = Some(((f * 5) / 2 % 50, 0.3))) + }; + let (small, large) = (work(50), work(500)); + assert!(small.0 > 0, "the frames build something"); + assert!(large.0 <= small.0, "component runs in one frame: {} at 500 pages, {} at 50", large.0, small.0); + assert!(large.1 <= small.1, "passes in one frame: {} at 500 pages, {} at 50", large.1, small.1); +} + +/// Wheel frames and the idle frames between them do not rebuild the pages of the whole document. +#[test] +fn wheel_frames_build_only_the_pages_in_view() { + let work = |n: usize| { + let mut h = harness( + big(n), + &[("left", "closed"), ("panel", "none"), ("layout", "continuous"), ("zoom", "200"), ("animation_effects", "on")], + ); + h.hover_at(h.state().views[0].viewport_rect().center()); + h.run_steps(2); + let start = h.state().views[0].current; + let most = most_work(&mut h, None, 60, |h, frame| { + if frame % 3 == 0 { + h.event(egui::Event::MouseWheel { + unit: egui::MouseWheelUnit::Line, + delta: egui::vec2(0.0, -1.0), + phase: egui::TouchPhase::Move, + modifiers: egui::Modifiers::NONE, + source: egui::MouseWheelSource::Unknown, + }); + } }); - h.run_steps(4); - let avg = best_average(panel, 100, |f| { - h.state_mut().views[0].goto = Some(((f * 5) % 500, 0.3)); - h.step(); - }); - assert!(avg < BUDGET, "{panel}: average frame {avg:?}"); + assert!(h.state().views[0].current > start, "wheel events must actually move through the document"); + most + }; + let (small, large) = (work(50), work(500)); + assert!(small.0 > 0, "the frames build something"); + assert!(large.0 <= small.0, "component runs in one frame: {} at 500 pages, {} at 50", large.0, small.0); + assert!(large.1 <= small.1, "passes in one frame: {} at 500 pages, {} at 50", large.1, small.1); +} + +/// The Comments, Pages and Fields panels build rows for the rows in view, not for every item: the most row runs in one +/// frame at 500 pages are no more than at 50. +#[test] +fn panels_build_only_the_rows_in_view() { + // The Fields panel has no field in this document, so only the relation holds there; the others must build rows. + for (panel, row, builds) in [("comments", "CommentRow", true), ("pages", "Tile", true), ("fields", "ListRow", false)] { + let work = |n: usize| { + let mut h = harness(big(n), &[("panel", panel)]); + most_work(&mut h, Some(row), 60, |h, f| h.state_mut().views[0].goto = Some(((f * 5) % 50, 0.3))).0 + }; + let (small, large) = (work(50), work(500)); + if builds { + assert!(small > 0, "{panel}: the {row} rows are built"); + } + assert!(large <= small, "{panel}: {large} {row} runs in one frame at 500 pages, {small} at 50"); } }