mirror of
https://github.com/storytold/pdfcraft.git
synced 2026-11-26 04:58:07 +00:00
Merge red5/g5 (startup test through its shown/launch helpers)
This commit is contained in:
@@ -96,6 +96,22 @@ pub(crate) fn test_app() -> PdfCraftApp {
|
||||
PdfCraftApp::with_host(std::sync::Arc::new(host), services)
|
||||
}
|
||||
|
||||
/// `app` in a window: an open it starts finishes on the window's next frame, as at launch.
|
||||
#[cfg(test)]
|
||||
pub(crate) fn shown(app: PdfCraftApp) -> egui_kittest::Harness<'static, PdfCraftApp> {
|
||||
egui_kittest::Harness::builder().build_eframe(move |cc| {
|
||||
crate::tests::install_runtime(&cc.egui_ctx);
|
||||
app
|
||||
})
|
||||
}
|
||||
|
||||
/// Opens `files` as a launch does, then runs the frames that take the opened documents.
|
||||
#[cfg(test)]
|
||||
pub(crate) fn launch(h: &mut egui_kittest::Harness<'static, PdfCraftApp>, files: &[String], two_paged: bool) {
|
||||
open_files(h.state_mut(), files, two_paged);
|
||||
h.run_steps(2);
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
@@ -193,7 +209,9 @@ mod tests {
|
||||
app.open_bytes(&egui::Context::default(), "restored.pdf", None, include_bytes!("../../../crates/ui-egui/tests/data/form.pdf").to_vec())
|
||||
.unwrap();
|
||||
let restored = app.views[0].id;
|
||||
open_files(&mut app, &[text.into(), second.into()], enabled);
|
||||
let mut h = shown(app);
|
||||
launch(&mut h, &[text.into(), second.into()], enabled);
|
||||
let app = h.state_mut();
|
||||
assert_eq!(app.views.len(), 3);
|
||||
assert_eq!(app.views[0].id, restored);
|
||||
assert_eq!(app.views[0].view.display, PageDisplay::Single);
|
||||
@@ -202,7 +220,7 @@ mod tests {
|
||||
assert!(!view.view.continuous);
|
||||
}
|
||||
// The launch override never reaches the saved settings.
|
||||
assert_eq!(saved_display(&app), PageDisplay::Single);
|
||||
assert_eq!(saved_display(app), PageDisplay::Single);
|
||||
// Existing --layout applies after the flag to the active document only.
|
||||
if enabled {
|
||||
app.set_launch_layout("continuous").unwrap();
|
||||
@@ -214,9 +232,9 @@ mod tests {
|
||||
app.set_option(&egui::Context::default(), "default-layout", "two-up").unwrap();
|
||||
assert_eq!(app.view_defaults.display, PageDisplay::TwoUp);
|
||||
assert!(app.view_defaults.continuous);
|
||||
assert_eq!(saved_display(&app), PageDisplay::TwoUp);
|
||||
open_files(&mut app, &[], enabled);
|
||||
assert_eq!(app.views.len(), 3);
|
||||
assert_eq!(saved_display(app), PageDisplay::TwoUp);
|
||||
launch(&mut h, &[], enabled);
|
||||
assert_eq!(h.state().views.len(), 3);
|
||||
}
|
||||
std::fs::remove_file(path).unwrap();
|
||||
std::fs::remove_file(second_path).unwrap();
|
||||
@@ -241,7 +259,9 @@ mod tests {
|
||||
include_bytes!("../../../crates/ui-egui/tests/data/form.pdf").to_vec(),
|
||||
)
|
||||
.unwrap();
|
||||
open_files(&mut app, &args.files, args.two_paged);
|
||||
let mut h = shown(app);
|
||||
launch(&mut h, &args.files, args.two_paged);
|
||||
let app = h.state_mut();
|
||||
for (key, value) in args.options {
|
||||
assert_eq!(key, "layout");
|
||||
app.set_launch_layout(&value).unwrap();
|
||||
@@ -272,21 +292,11 @@ mod repeated_file_tests {
|
||||
let name = path.to_str().unwrap().to_string();
|
||||
let mut app = test_app();
|
||||
app.set_option(&egui::Context::default(), "default-layout", "continuous").unwrap();
|
||||
app.open_path(&name);
|
||||
// An open lands on a frame, as the files named at launch do in the window.
|
||||
let mut h = egui_kittest::Harness::builder().build_eframe(move |cc| {
|
||||
crate::tests::install_runtime(&cc.egui_ctx);
|
||||
app
|
||||
});
|
||||
for _ in 0..20 {
|
||||
if !h.state().views.is_empty() {
|
||||
break;
|
||||
}
|
||||
h.run_steps(1);
|
||||
}
|
||||
let mut h = shown(app);
|
||||
launch(&mut h, std::slice::from_ref(&name), false);
|
||||
let original = h.state().views[0].id;
|
||||
launch(&mut h, &[name.clone(), name], true);
|
||||
let app = h.state_mut();
|
||||
let original = app.views[0].id;
|
||||
open_files(app, &[name.clone(), name], true);
|
||||
assert_eq!(app.views.len(), 1);
|
||||
assert_eq!(app.views[0].id, original);
|
||||
assert_eq!(app.views[0].view.display, PageDisplay::TwoUp);
|
||||
|
||||
@@ -67,11 +67,13 @@ fn title_bar(app: &mut PdfCraftApp, ui: &mut egui::Ui) {
|
||||
Some(doc) => Title::new(doc.name.clone()),
|
||||
None => Title::new("PdfCraft"),
|
||||
};
|
||||
let theme_icon = match app.theme_preference {
|
||||
ModeChoice::System => "monitor",
|
||||
ModeChoice::Light => "sun",
|
||||
ModeChoice::Dark => "moon",
|
||||
let (theme_icon, theme_label) = match app.theme_preference {
|
||||
ModeChoice::System => ("monitor", tl!("Use system setting")),
|
||||
ModeChoice::Light => ("sun", tl!("Light gray")),
|
||||
ModeChoice::Dark => ("moon", tl!("Dark gray")),
|
||||
};
|
||||
// The button names the choice it shows; a click steps to the next one.
|
||||
let theme_label = format!("{}: {theme_label}", tl!("Display theme"));
|
||||
let out = TitleBar::new().show_titled(
|
||||
ui,
|
||||
title,
|
||||
@@ -87,7 +89,7 @@ fn title_bar(app: &mut PdfCraftApp, ui: &mut egui::Ui) {
|
||||
|ui, _| {
|
||||
IconButton::command(IconId::new("messages-square"), "help.discord").show(ui);
|
||||
IconButton::command(IconId::new("circle-help"), "help.shortcuts").show(ui);
|
||||
let theme_button = IconButton::command(IconId::new(theme_icon), ThemeChoice::NextMode.command()).show(ui);
|
||||
let theme_button = IconButton::command(IconId::new(theme_icon), ThemeChoice::NextMode.command()).label(theme_label.clone()).show(ui);
|
||||
let opener = theme_button.response().clone();
|
||||
Menu::context(tl!("Appearance"), &opener).show(ui, |ui, _| {
|
||||
let appearance = CommandRef::with_params("app.preferences", serde_json::json!({ "tab": "appearance" }));
|
||||
|
||||
@@ -26,7 +26,7 @@ fn at_the_minimum_width_the_menus_the_modes_and_search_stay_reachable() {
|
||||
let search = h.get_by_label("Find tools and commands").rect();
|
||||
assert!(window.contains_rect(search), "command search is inside the window: {search:?}");
|
||||
// The strip shows the current mode whatever the room; the rest fit or fold into its overflow.
|
||||
let mode = h.get_by_label("All tools").rect();
|
||||
let mode = h.get_by_role_and_label(Role::Tab, "All tools").rect();
|
||||
assert!(window.contains_rect(mode), "the current mode is inside the window: {mode:?}");
|
||||
assert!(!mode.intersects(search), "the mode strip and the search do not overlap: {mode:?} {search:?}");
|
||||
// The menu bar folds the titles that don't fit into "⋯", or all of them into one "Menu" when not
|
||||
|
||||
@@ -219,7 +219,7 @@ fn a_watched_label_replacement_keeps_the_display_mode_and_focused_page() {
|
||||
assert_eq!(view.zoom(), 1.0);
|
||||
assert_eq!(view.page_input, "iv");
|
||||
assert_eq!(h.ctx.memory(|m| m.focused()), Some(editor));
|
||||
h.get(By::new().role(egui::accesskit::Role::TextInput).value("iv"));
|
||||
h.get(By::new().role(egui::accesskit::Role::TextInput).label("Current page").value("iv"));
|
||||
|
||||
// Keep the replacement editor's selection and then its caret, without re-focusing it.
|
||||
h.event(egui::Event::Text("2".into()));
|
||||
|
||||
@@ -85,7 +85,12 @@ pub fn fire_menu(h: &mut Harness<'static, PdfCraftApp>, command: impl Into<Comma
|
||||
|
||||
/// The text of the newest notice card the main window shows, if any: what the app said last.
|
||||
pub fn shown_notice(h: &Harness<'_, PdfCraftApp>) -> Option<String> {
|
||||
toast::cards(&h.ctx, &WindowKey::main()).last().map(|card| card.toast.message.as_str().to_owned())
|
||||
// A posted notice's text is the card's title; a message and bullet lines follow it.
|
||||
toast::cards(&h.ctx, &WindowKey::main()).last().map(|card| {
|
||||
let toast = &card.toast;
|
||||
let parts = toast.title.iter().chain(std::iter::once(&toast.message)).chain(&toast.lines);
|
||||
parts.map(|part| part.as_str()).filter(|text| !text.is_empty()).collect::<Vec<_>>().join("\n")
|
||||
})
|
||||
}
|
||||
|
||||
/// The services the tests give the app: no wake, no autosave folder, the GitHub update source, a fresh
|
||||
|
||||
@@ -27,13 +27,23 @@ fn harness_with(settings: Option<&'static str>, system: Option<egui::Theme>, run
|
||||
app
|
||||
}
|
||||
});
|
||||
h.input_mut().system_theme = system;
|
||||
os_theme(&mut h, system);
|
||||
if run {
|
||||
h.run_steps(4);
|
||||
}
|
||||
(h, tx)
|
||||
}
|
||||
|
||||
/// The desktop reports `theme` as the windowing integration does: the raw input's field, and an
|
||||
/// `Event::SystemTheme` for a scheme it knows. An integration never reports a known scheme as gone.
|
||||
fn os_theme(h: &mut Harness<'static, PdfCraftApp>, theme: Option<egui::Theme>) {
|
||||
let input = h.input_mut();
|
||||
input.system_theme = theme;
|
||||
if let Some(theme) = theme {
|
||||
input.events.push(egui::Event::SystemTheme(theme));
|
||||
}
|
||||
}
|
||||
|
||||
fn harness() -> (Harness<'static, PdfCraftApp>, ControlClient) {
|
||||
harness_with(None, Some(egui::Theme::Dark), true)
|
||||
}
|
||||
@@ -109,15 +119,15 @@ fn toolbar_and_view_menu_share_three_state_preferences() {
|
||||
h.get_by_label("Use system setting").click();
|
||||
h.run_steps(3);
|
||||
assert_theme(&mut h, &c, "System", "Dark");
|
||||
h.input_mut().system_theme = Some(egui::Theme::Light);
|
||||
os_theme(&mut h, Some(egui::Theme::Light));
|
||||
h.run_steps(3);
|
||||
assert_theme(&mut h, &c, "System", "Light");
|
||||
click_toolbar(&mut h, "Use system setting");
|
||||
click_toolbar(&mut h, "Light gray");
|
||||
// A manual choice exits system mode and survives subsequent OS changes.
|
||||
h.input_mut().system_theme = Some(egui::Theme::Dark);
|
||||
os_theme(&mut h, Some(egui::Theme::Dark));
|
||||
h.run_steps(2);
|
||||
h.input_mut().system_theme = Some(egui::Theme::Light);
|
||||
os_theme(&mut h, Some(egui::Theme::Light));
|
||||
h.run_steps(2);
|
||||
assert_theme(&mut h, &c, "Dark", "Dark");
|
||||
}
|
||||
@@ -186,14 +196,14 @@ fn restored_system_tracks_native_input_without_rewriting_saved_preference() {
|
||||
for (input, expected) in
|
||||
[(Some(egui::Theme::Dark), "Dark"), (Some(egui::Theme::Light), "Light"), (None, "Light"), (Some(egui::Theme::Dark), "Dark")]
|
||||
{
|
||||
h.input_mut().system_theme = input;
|
||||
os_theme(&mut h, input);
|
||||
h.run_steps(3);
|
||||
assert_theme(&mut h, &c, "System", expected);
|
||||
}
|
||||
for (manual, expected) in [("light", "Light"), ("dark", "Dark")] {
|
||||
ok(&mut h, &c, "ui.set", json!({"key": "theme", "value": manual}));
|
||||
for input in [Some(egui::Theme::Dark), Some(egui::Theme::Light), None] {
|
||||
h.input_mut().system_theme = input;
|
||||
os_theme(&mut h, input);
|
||||
h.run_steps(3);
|
||||
assert_theme(&mut h, &c, expected, expected);
|
||||
}
|
||||
@@ -231,9 +241,9 @@ fn preferences_theme_cards_and_use_system_setting_stay_in_sync() {
|
||||
// The Light card itself, not its radio row, picks Light, and a dark OS doesn't undo it.
|
||||
h.get_by_label("Light theme").click();
|
||||
h.run_steps(3);
|
||||
h.input_mut().system_theme = Some(egui::Theme::Light);
|
||||
os_theme(&mut h, Some(egui::Theme::Light));
|
||||
h.run_steps(2);
|
||||
h.input_mut().system_theme = Some(egui::Theme::Dark);
|
||||
os_theme(&mut h, Some(egui::Theme::Dark));
|
||||
h.run_steps(2);
|
||||
assert_theme(&mut h, &c, "Light", "Light");
|
||||
assert_eq!(mode_shown(&h), "Light");
|
||||
@@ -248,7 +258,7 @@ fn preferences_theme_cards_and_use_system_setting_stay_in_sync() {
|
||||
h.run_steps(3);
|
||||
assert_theme(&mut h, &c, "System", "Dark");
|
||||
assert_eq!(mode_shown(&h), "Use system setting");
|
||||
h.input_mut().system_theme = Some(egui::Theme::Light);
|
||||
os_theme(&mut h, Some(egui::Theme::Light));
|
||||
h.run_steps(3);
|
||||
assert_theme(&mut h, &c, "System", "Light");
|
||||
// Only the Light card is marked Active while the OS is light.
|
||||
|
||||
@@ -592,10 +592,9 @@ fn layouts_fit_height_labels_and_system_theme() {
|
||||
}
|
||||
|
||||
fn page_input_focused(h: &Harness<'static, PdfCraftApp>) -> bool {
|
||||
let focused = h.ctx.memory(|m| m.focused());
|
||||
let view = &h.state().views[h.state().active.unwrap()];
|
||||
// Also identifies the original editor when running this test-only patch on the baseline.
|
||||
focused == Some(egui::Id::unique(("page-input", view.id.0))) || focused == Some(egui::Id::unique("page-input"))
|
||||
use egui_kittest::kittest::NodeT as _;
|
||||
// The view rail shows the active document's editor alone.
|
||||
h.query_all(By::new().role(egui::accesskit::Role::TextInput).label("Current page")).any(|n| n.accesskit_node().is_focused())
|
||||
}
|
||||
|
||||
/// Exercise the real navigation editor, including its focus and Enter handling.
|
||||
@@ -1071,7 +1070,8 @@ fn initial_view_is_edited_and_honoured_on_open() {
|
||||
use pdfcraft_engine::{InitialLayout, Magnification, Navigation};
|
||||
let mut h = harness();
|
||||
open_properties(&mut h, PropsTab::InitialView);
|
||||
h.get_by_label("Open to page");
|
||||
// The form row names its field group; the field inside carries the same name.
|
||||
h.get_by_role_and_label(egui::accesskit::Role::Group, "Open to page");
|
||||
set_properties_field(&mut h, "navigation", json!("bookmarks"));
|
||||
set_properties_field(&mut h, "layout", json!("two-up-cover"));
|
||||
set_properties_field(&mut h, "magnification", json!("fit-width"));
|
||||
|
||||
Reference in New Issue
Block a user