test(plot): verify printer property commands

Use exact platform-specific command expectations.\n\nRefs #773
This commit is contained in:
Hakan Seven 2026-08-17 22:29:47 +03:00
commit 04b9363666

View file

@ -91,21 +91,13 @@ pub fn list_printers() -> Vec<String> {
} }
} }
/// The command that opens the OS printer configuration surface, for the named /// Build the platform printer-properties command.
/// printer when one is selected and the printer list otherwise.
///
/// Split out from [`open_printer_properties`] so the argument construction can
/// be asserted without spawning anything.
#[cfg(not(target_arch = "wasm32"))] #[cfg(not(target_arch = "wasm32"))]
fn printer_properties_command(printer: Option<&str>) -> (&'static str, Vec<String>) { fn printer_properties_command(printer: Option<&str>) -> (&'static str, Vec<String>) {
let named = printer let named = printer
.map(str::trim) .map(str::trim)
.filter(|name| !name.is_empty()); .filter(|name| !name.is_empty());
// printui.dll opens the driver's own Properties dialog for one printer,
// which is where print quality lives on Windows — `dispatch_to_printer_opts`
// prints through ShellExecute's "printto" verb, which carries no quality of
// its own. Without a selection there is nothing to open but the list.
#[cfg(target_os = "windows")] #[cfg(target_os = "windows")]
let command = match named { let command = match named {
Some(name) => ( Some(name) => (
@ -340,27 +332,64 @@ fn dispatch_to_printer_opts(
mod printer_properties_tests { mod printer_properties_tests {
use super::printer_properties_command; use super::printer_properties_command;
#[cfg(target_os = "windows")]
#[test] #[test]
fn a_selected_printer_is_named_in_the_command() { fn windows_opens_selected_printer_or_printer_list() {
let (program, args) = printer_properties_command(Some("Office LaserJet")); assert_eq!(
assert!( printer_properties_command(Some(" Office LaserJet ")),
args.iter().any(|arg| arg == "Office LaserJet"), (
"{program} {args:?} must target the selected printer", "rundll32.exe",
vec![
"printui.dll,PrintUIEntry".to_string(),
"/p".to_string(),
"/n".to_string(),
"Office LaserJet".to_string(),
],
),
);
assert_eq!(
printer_properties_command(None),
("control.exe", vec!["printers".to_string()]),
); );
} }
#[cfg(target_os = "macos")]
#[test] #[test]
fn no_selection_falls_back_to_the_printer_list() { fn macos_opens_print_settings() {
let (_, args) = printer_properties_command(None); let expected = (
assert!( "open",
!args.iter().any(|arg| arg.contains("Office LaserJet")), vec![
"there is no printer to name", "x-apple.systempreferences:com.apple.Print-Scan-Settings.extension".to_string(),
],
);
assert_eq!(
printer_properties_command(Some(" Office LaserJet ")),
expected.clone(),
);
assert_eq!(printer_properties_command(None), expected);
}
#[cfg(all(not(target_os = "windows"), not(target_os = "macos")))]
#[test]
fn unix_opens_selected_cups_printer_or_printer_list() {
assert_eq!(
printer_properties_command(Some(" Office LaserJet ")),
(
"xdg-open",
vec!["http://localhost:631/printers/Office LaserJet".to_string()],
),
);
assert_eq!(
printer_properties_command(None),
(
"xdg-open",
vec!["http://localhost:631/printers".to_string()],
),
); );
} }
#[test] #[test]
fn a_blank_selection_is_treated_as_no_selection() { fn a_blank_selection_is_treated_as_no_selection() {
// The plot dialog stores Some("") before the user picks anything.
assert_eq!( assert_eq!(
printer_properties_command(Some(" ")), printer_properties_command(Some(" ")),
printer_properties_command(None), printer_properties_command(None),