Files
egui/crates/egui_kittest/tests/context_setup.rs
T
Emil ErnerfeldtandClaude Opus 5.5 8f6d3d6ed9 Add HarnessBuilder::with_context_setup to configure the Context before the first pass (#8686)
* [x] I have followed the instructions in the PR template

A `Harness` runs one pass as soon as it is built, and the root `Ui`
takes its style from the `Context` at the start of the pass.
So a style, fonts or image loaders installed from the ui closure miss
that first pass, and widgets that keep their first layout keep the wrong
one.
In Rerun this made a snapshot test flaky: `egui_table` sized its columns
with the default monospace font whenever the table's data arrived in
time for the first pass, and columns only grow after that.

`HarnessBuilder::with_context_setup(|ctx| …)` runs once on the new
`Context` before that pass, for `build_ui`, `build_ui_state` and
`build_eframe` (before the app is created).
The harness's own test settings (theme, no animations, no cursor
blinking) are still applied after it.

🤖 Written by Claude Code (Claude Opus 5.5); not yet reviewed by a human,
hence the draft.

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-09 17:07:05 +02:00

36 lines
1002 B
Rust

use egui::Vec2;
use egui_kittest::Harness;
#[test]
fn context_setup_applies_to_the_first_pass() {
let spacing = Vec2::splat(42.0);
let mut first_pass_spacing = None;
let harness = Harness::builder()
.with_context_setup(move |ctx| {
ctx.all_styles_mut(|style| style.spacing.item_spacing = spacing);
})
.build_ui(|ui| {
first_pass_spacing.get_or_insert_with(|| ui.spacing().item_spacing);
});
drop(harness);
assert_eq!(first_pass_spacing, Some(spacing));
}
#[test]
fn harness_test_settings_override_context_setup() {
let harness = Harness::builder()
.with_context_setup(|ctx| {
ctx.all_styles_mut(|style| {
style.animation_time = 1.0;
style.visuals.text_cursor.blink = true;
});
})
.build_ui(|_ui| {});
let style = harness.ctx.global_style();
assert_eq!(style.animation_time, 0.0);
assert!(!style.visuals.text_cursor.blink);
}