Files
craft-libs/crates/pages/tests/it/views.rs
T

205 lines
7.4 KiB
Rust

//! The page view and the page preview: the scroll anchor under a zoom change, wheel input at the end of the document,
//! the view's page and arrow keys and its zoom commands, and the preview's keys.
use craft_behavior::kurbo;
use craft_pages::layout::{Fit, LayoutOptions};
use craft_pages::ui::fixtures::{DemoPages, PAPER};
use craft_pages::ui::{PagePreview, PageView, PageViewState};
use craft_ui::testing::{Fixture, trial};
use egui::{Key, Modifiers, pos2, vec2};
use std::cell::RefCell;
use std::rc::Rc;
/// The default state with `change` applied.
fn state_with(change: impl FnOnce(&mut PageViewState)) -> PageViewState {
let mut state = PageViewState::default();
change(&mut state);
state
}
type LayoutRig = (Fixture, Rc<RefCell<PageViewState>>, Rc<RefCell<Vec<kurbo::Rect>>>);
/// A page view over `count` demo pages with the state it starts from, and the rects of the pages it laid out.
fn layout_rig(count: usize, start: PageViewState) -> LayoutRig {
let state = Rc::new(RefCell::new(start));
let rects: Rc<RefCell<Vec<kurbo::Rect>>> = Rc::default();
let (kept, seen) = (Rc::clone(&state), Rc::clone(&rects));
let pages = DemoPages::new(count);
let mut fixture = Fixture::new(move |ui| {
let mut now = kept.borrow().clone();
let out = PageView::new("Pages", &pages, &mut now, PAPER).options(LayoutOptions::ACROBAT).show(ui, |_, _| ());
seen.borrow_mut().clone_from(&out.layout.rects);
*kept.borrow_mut() = now;
});
fixture.room(vec2(900.0, 600.0));
fixture.settle();
(fixture, state, rects)
}
#[trial]
fn a_changed_zoom_keeps_the_page_at_the_top_in_place() {
let (mut fixture, state, rects) = layout_rig(
5,
state_with(|s| {
s.fit = Fit::None;
s.zoom = 1.0;
}),
);
let third = rects.borrow()[2];
state.borrow_mut().offset.1 = third.y0 + 0.4 * third.height();
fixture.idle(2);
state.borrow_mut().zoom = 2.0;
fixture.idle(3);
let third = rects.borrow()[2];
let offset = state.borrow().offset.1;
assert!((offset - (third.y0 + 0.4 * third.height())).abs() < 1.0, "offset {offset}, page {third:?}");
}
/// A wheel step of `lines` over the middle of the room.
fn wheel(fixture: &mut Fixture, lines: f32) {
let middle = pos2(450.0, 300.0);
fixture.frame(vec![egui::Event::PointerMoved(middle)]);
fixture.frame(vec![egui::Event::MouseWheel {
unit: egui::MouseWheelUnit::Line,
delta: vec2(0.0, lines),
modifiers: Modifiers::NONE,
phase: egui::TouchPhase::Move,
source: egui::MouseWheelSource::Unknown,
}]);
}
#[trial]
fn wheel_gathered_past_the_bottom_is_forgotten_by_a_pass_without_wheel_input() {
let (mut fixture, state, _) = layout_rig(
4,
state_with(|s| {
s.continuous = false;
s.fit = Fit::None;
s.zoom = 2.0;
}),
);
state.borrow_mut().offset.1 = 1e9;
fixture.idle(2);
wheel(&mut fixture, -0.5);
fixture.idle(1);
wheel(&mut fixture, -0.5);
fixture.idle(1);
assert_eq!(state.borrow().current, 0, "each half notch alone is not a turn");
wheel(&mut fixture, -3.0);
assert_eq!(state.borrow().current, 1, "a whole notch at once is");
}
#[trial]
fn the_preview_steps_with_the_arrow_and_page_keys_from_a_footer_button() {
let at = Rc::new(RefCell::new(1_usize));
let kept = Rc::clone(&at);
let pages = DemoPages::new(5);
let mut fixture = Fixture::new(move |ui| {
let shown = [0, 1, 2, 3, 4];
PagePreview::new(&pages, &shown, &mut kept.borrow_mut(), PAPER).show(ui);
});
fixture.room(vec2(900.0, 600.0));
fixture.settle();
fixture.key(Key::Tab, Modifiers::NONE);
let button = fixture.focused_stop();
assert!(button.is_some(), "Tab focuses a footer button");
fixture.key(Key::ArrowRight, Modifiers::NONE);
assert_eq!(*at.borrow(), 2, "Right steps forward");
fixture.key(Key::PageDown, Modifiers::NONE);
assert_eq!(*at.borrow(), 3, "Page Down steps forward");
fixture.key(Key::ArrowLeft, Modifiers::NONE);
assert_eq!(*at.borrow(), 2, "Left steps back");
assert_eq!(fixture.focused_stop(), button, "the keys leave the focus where it was");
}
/// Tabs to the view's canvas.
fn focus_canvas(fixture: &mut Fixture) {
for _ in 0..4 {
if fixture.focused_role().is_some_and(|role| role.nudge) {
return;
}
fixture.key(Key::Tab, Modifiers::NONE);
}
assert!(fixture.focused_role().is_some_and(|role| role.nudge), "Tab reaches the view's canvas");
}
#[trial]
fn page_keys_turn_and_go_to_the_ends_in_a_view_that_is_not_continuous() {
let (mut fixture, state, _) = layout_rig(
5,
state_with(|s| {
s.continuous = false;
s.fit = Fit::Page;
}),
);
focus_canvas(&mut fixture);
fixture.key(Key::PageDown, Modifiers::NONE);
fixture.idle(1);
assert_eq!(state.borrow().current, 1, "Page Down turns one page");
fixture.key(Key::End, Modifiers::NONE);
fixture.idle(1);
assert_eq!(state.borrow().current, 4, "End goes to the last page");
fixture.key(Key::Home, Modifiers::NONE);
fixture.idle(1);
assert_eq!(state.borrow().current, 0, "Home goes to the first page");
}
#[trial]
fn arrows_go_to_the_next_page_and_scroll_a_line_in_a_continuous_view() {
let (mut fixture, state, _) = layout_rig(5, state_with(|s| s.fit = Fit::Page));
focus_canvas(&mut fixture);
fixture.key(Key::ArrowRight, Modifiers::NONE);
fixture.idle(1);
assert_eq!(state.borrow().current, 1, "Right goes to the next page while the page fits the width");
let before = state.borrow().offset.1;
fixture.key(Key::ArrowDown, Modifiers::NONE);
fixture.idle(1);
assert!(state.borrow().offset.1 > before, "Down scrolls forward");
}
#[trial]
fn zoom_in_zooms_a_focused_view_with_animated_zoom() {
let (mut fixture, state, _) = layout_rig(3, PageViewState::default());
focus_canvas(&mut fixture);
let before = state.borrow().zoom;
fixture.key(Key::Equals, Modifiers::COMMAND);
fixture.settle();
assert!(state.borrow().zoom > before, "Zoom In zooms: {before} -> {}", state.borrow().zoom);
}
#[trial]
fn zoom_keys_under_an_open_preview_leave_the_view_alone() {
let state = Rc::new(RefCell::new(PageViewState::default()));
let open = Rc::new(RefCell::new(false));
let (kept, opened) = (Rc::clone(&state), Rc::clone(&open));
let pages = DemoPages::new(3);
let mut at = 0_usize;
let mut fixture = Fixture::new(move |ui| {
let mut now = kept.borrow().clone();
PageView::new("Pages", &pages, &mut now, PAPER).options(LayoutOptions::ACROBAT).show(ui, |_, _| ());
*kept.borrow_mut() = now;
if *opened.borrow() {
PagePreview::new(&pages, &[0, 1, 2], &mut at, PAPER).show(ui);
}
});
fixture.room(vec2(900.0, 600.0));
fixture.settle();
focus_canvas(&mut fixture);
*open.borrow_mut() = true;
fixture.settle();
let before = state.borrow().zoom;
for (key, modifiers) in [
(Key::Equals, Modifiers::COMMAND),
(Key::Plus, Modifiers::COMMAND),
(Key::Minus, Modifiers::COMMAND),
(Key::Num0, Modifiers::COMMAND),
(Key::Plus, Modifiers::NONE),
(Key::Minus, Modifiers::NONE),
] {
fixture.key(key, modifiers);
fixture.settle();
assert_eq!(state.borrow().zoom, before, "{key:?} with {modifiers:?} under the preview");
}
}