Files
lightgui_rust/crates/lightgui-support/tests/font_tests.rs
T
cnmcdee 3d53281388 Add lightgui-debug and lightgui-support: GDB MI debugger + theme/preferences/codeview/cmake ports
lightgui-debug: GdbSession over gdb -q -i=mi2 pipes with hand-written MI
parser (mi.rs), settings/breakpoint/variable types, and the 44-check
C++ harness ported as Rust tests (37 console + 7 GTK, gdb 17.1).
lightgui-support: CodeTheme + C tokenizer/highlighter, FontSettings,
CodeView fold/gutter/inspection widget, CMake project builder.
Tests: debug 59/59, support 23/23 (incl. theme/font/cmake suites).
cargo test clean, zero rustc warnings, clippy clean, cargo fmt clean.
2026-11-29 06:10:11 +00:00

181 lines
5.6 KiB
Rust

// Font settings tests — Rust port of tests/font_tests.cpp.
// Run under Xvfb:
// xvfb-run -a cargo test -p lightgui-support --test font_tests
// The whole suite runs as one #[test] because preferences_current()
// and the screen-wide providers are process-wide globals, exactly
// like the C++ harness's single main().
use gtk::prelude::*;
use lightgui_support::preferences::{
preferences_applied_font, preferences_apply, preferences_apply_to_text_view, FontSettings,
};
struct Checker {
pass: usize,
fail: usize,
failed: Vec<String>,
}
impl Checker {
fn check(&mut self, cond: bool, name: &str) {
if cond {
self.pass += 1;
println!("PASS {}", name);
} else {
self.fail += 1;
self.failed.push(name.to_string());
println!("FAIL {}", name);
}
}
fn finish(&self) {
println!("FONT TESTS: {} pass, {} fail", self.pass, self.fail);
if !self.failed.is_empty() {
panic!("FAILED: {}", self.failed.join(" "));
}
}
}
#[test]
fn font_suite() {
gtk::init().expect("gtk::init");
let mut ck = Checker {
pass: 0,
fail: 0,
failed: Vec::new(),
};
// 1. Defaults.
let def = FontSettings::default();
ck.check(def.font == "Sans 10", "defaults-ui-font");
ck.check(def.code_font == "Monospace 11", "defaults-code-font");
// 2. Save / load round-trip at an explicit path.
let fs = FontSettings {
font: "DejaVu Sans 14".to_string(),
code_font: "DejaVu Sans Mono 13".to_string(),
};
let path = "/tmp/lightgui_font_harness_rust.conf";
ck.check(fs.save(path), "save-roundtrip");
let mut fs2 = FontSettings::default();
ck.check(fs2.load(path), "load-roundtrip");
ck.check(
fs2.font == "DejaVu Sans 14" && fs2.code_font == "DejaVu Sans Mono 13",
"roundtrip-values",
);
// 3. Missing file: load fails, defaults kept.
let mut fs3 = FontSettings::default();
ck.check(
!fs3.load("/tmp/lightgui_font_no_such_file_rust.conf"),
"load-missing-fails",
);
ck.check(fs3.font == "Sans 10", "load-missing-keeps-defaults");
// 4. Garbage in the file does not break loading.
std::fs::write(
"/tmp/lightgui_font_garbage_rust.conf",
"# comment\nnot-a-kv-line\nfont=\ncode_font=\n",
)
.unwrap();
let mut fs4 = FontSettings::default();
ck.check(
fs4.load("/tmp/lightgui_font_garbage_rust.conf"),
"load-garbage-file",
);
ck.check(
fs4.font == "Sans 10" && fs4.code_font == "Monospace 11",
"garbage-falls-back-to-defaults",
);
// 5. Sanitising: empty -> fallback, sizes clamped to 6-48 pt.
ck.check(
FontSettings::sanitized("", "Sans 10") == "Sans 10",
"sanitize-empty",
);
ck.check(
FontSettings::size_of(&FontSettings::sanitized("Sans 99", "Sans 10")) == 48,
"sanitize-clamp-high",
);
ck.check(
FontSettings::size_of(&FontSettings::sanitized("Sans 2", "Sans 10")) == 6,
"sanitize-clamp-low",
);
ck.check(
FontSettings::family_of(&FontSettings::sanitized("DejaVu Sans Mono 13", "Sans 10"))
== "DejaVu Sans Mono",
"sanitize-keeps-family",
);
// 6. Env override for the default path (mirrors debugger.conf).
std::env::set_var("LIGHTGUI_SETTINGS_CONF", "/tmp/lightgui_font_env_rust.conf");
ck.check(
FontSettings::default_path() == "/tmp/lightgui_font_env_rust.conf",
"env-override-path",
);
std::env::remove_var("LIGHTGUI_SETTINGS_CONF");
// 7. CSS generation.
ck.check(
fs.ui_css().contains("DejaVu Sans") && fs.ui_css().contains("14pt"),
"ui-css-content",
);
ck.check(
fs.code_css().contains("textview")
&& fs.code_css().contains("DejaVu Sans Mono")
&& fs.code_css().contains("13pt"),
"code-css-content",
);
// 8. Live apply to a text view: the widget really shows the font.
let tv = gtk::TextView::new();
tv.show();
preferences_apply_to_text_view(tv.upcast_ref(), &fs);
while gtk::events_pending() {
gtk::main_iteration();
}
let applied = preferences_applied_font(tv.upcast_ref());
ck.check(
FontSettings::family_of(&applied) == "DejaVu Sans Mono"
&& FontSettings::size_of(&applied) == 13,
"apply-to-text-view",
);
// 9. Screen-wide apply: a fresh text view and a label pick it up.
preferences_apply(&fs);
let tv2 = gtk::TextView::new();
let lbl = gtk::Label::new(Some("probe"));
tv2.show();
lbl.show();
while gtk::events_pending() {
gtk::main_iteration();
}
let applied2 = preferences_applied_font(tv2.upcast_ref());
ck.check(
FontSettings::family_of(&applied2) == "DejaVu Sans Mono"
&& FontSettings::size_of(&applied2) == 13,
"screen-apply-text-view",
);
let applied_lbl = preferences_applied_font(lbl.upcast_ref());
ck.check(
FontSettings::family_of(&applied_lbl) == "DejaVu Sans"
&& FontSettings::size_of(&applied_lbl) == 14,
"screen-apply-label",
);
// 10. Re-applying defaults restores the defaults (no crash, no leak
// of the old providers).
preferences_apply(&FontSettings::default());
let tv3 = gtk::TextView::new();
tv3.show();
while gtk::events_pending() {
gtk::main_iteration();
}
let applied3 = preferences_applied_font(tv3.upcast_ref());
ck.check(
FontSettings::family_of(&applied3) == "Monospace" && FontSettings::size_of(&applied3) == 11,
"reapply-defaults",
);
ck.finish();
}