mirror of
https://github.com/emilk/egui.git
synced 2026-09-02 23:00:04 -04:00
Add egui testing library (#5166)
- closes #3491 - closes #3926 This adds a testing library to egui based on [kittest](https://github.com/rerun-io/kittest). Kittest is a new [AccessKit](https://github.com/AccessKit/accesskit/)-based testing library. The api is inspired by the js [testing-library](https://testing-library.com/) where the idea is also to query the dom based on accessibility attributes. We made kittest with egui in mind but it should work with any rust gui framework with AccessKit support. It currently has support for: - running the egui app, frame by frame - building the AccessKit tree - ergonomic queries via kittest - via e.g. get_by_name, get_by_role - simulating events based on the accesskit node id - creating arbitrary events based on Harness::input_mut - rendering screenshots via wgpu - snapshot tests with these screenshots A simple test looks like this: ```rust fn main() { let mut checked = false; let app = |ctx: &Context| { CentralPanel::default().show(ctx, |ui| { ui.checkbox(&mut checked, "Check me!"); }); }; let mut harness = Harness::builder().with_size(egui::Vec2::new(200.0, 100.0)).build(app); let checkbox = harness.get_by_name("Check me!"); assert_eq!(checkbox.toggled(), Some(Toggled::False)); checkbox.click(); harness.run(); let checkbox = harness.get_by_name("Check me!"); assert_eq!(checkbox.toggled(), Some(Toggled::True)); // You can even render the ui and do image snapshot tests #[cfg(all(feature = "wgpu", feature = "snapshot"))] egui_kittest::image_snapshot(&egui_kittest::wgpu::TestRenderer::new().render(&harness), "readme_example"); } ``` ~Since getting wgpu to run in ci is a hassle, I'm taking another shot at creating a software renderer for egui (ideally without a huge dependency like skia)~ (this didn't work as well as I hoped and it turns out in CI you can just run tests on a mac runner which comes with a real GPU) Here is a example of a failed snapshot test in ci, it will say which snapshot failed and upload an artifact with the before / after and diff images: https://github.com/emilk/egui/actions/runs/11183049487/job/31090724606?pr=5166
This commit is contained in:
186
crates/egui_kittest/src/lib.rs
Normal file
186
crates/egui_kittest/src/lib.rs
Normal file
@@ -0,0 +1,186 @@
|
||||
#![doc = include_str!("../README.md")]
|
||||
//!
|
||||
//! ## Feature flags
|
||||
#![cfg_attr(feature = "document-features", doc = document_features::document_features!())]
|
||||
|
||||
mod builder;
|
||||
mod event;
|
||||
#[cfg(feature = "snapshot")]
|
||||
mod snapshot;
|
||||
|
||||
#[cfg(feature = "snapshot")]
|
||||
pub use snapshot::*;
|
||||
use std::fmt::{Debug, Formatter};
|
||||
#[cfg(feature = "wgpu")]
|
||||
mod texture_to_image;
|
||||
#[cfg(feature = "wgpu")]
|
||||
pub mod wgpu;
|
||||
|
||||
pub use kittest;
|
||||
use std::mem;
|
||||
|
||||
use crate::event::EventState;
|
||||
pub use builder::*;
|
||||
use egui::{Pos2, Rect, TexturesDelta, Vec2, ViewportId};
|
||||
use kittest::{Node, Queryable};
|
||||
|
||||
/// The test Harness. This contains everything needed to run the test.
|
||||
/// Create a new Harness using [`Harness::new`] or [`Harness::builder`].
|
||||
pub struct Harness<'a> {
|
||||
pub ctx: egui::Context,
|
||||
input: egui::RawInput,
|
||||
kittest: kittest::State,
|
||||
output: egui::FullOutput,
|
||||
texture_deltas: Vec<TexturesDelta>,
|
||||
update_fn: Box<dyn FnMut(&egui::Context) + 'a>,
|
||||
event_state: EventState,
|
||||
}
|
||||
|
||||
impl<'a> Debug for Harness<'a> {
|
||||
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
|
||||
self.kittest.fmt(f)
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a> Harness<'a> {
|
||||
pub(crate) fn from_builder(
|
||||
builder: &HarnessBuilder,
|
||||
mut app: impl FnMut(&egui::Context) + 'a,
|
||||
) -> Self {
|
||||
let ctx = egui::Context::default();
|
||||
ctx.enable_accesskit();
|
||||
let mut input = egui::RawInput {
|
||||
screen_rect: Some(builder.screen_rect),
|
||||
..Default::default()
|
||||
};
|
||||
let viewport = input.viewports.get_mut(&ViewportId::ROOT).unwrap();
|
||||
viewport.native_pixels_per_point = Some(builder.dpi);
|
||||
|
||||
// We need to run egui for a single frame so that the AccessKit state can be initialized
|
||||
// and users can immediately start querying for widgets.
|
||||
let mut output = ctx.run(input.clone(), &mut app);
|
||||
|
||||
let mut harness = Self {
|
||||
update_fn: Box::new(app),
|
||||
ctx,
|
||||
input,
|
||||
kittest: kittest::State::new(
|
||||
output
|
||||
.platform_output
|
||||
.accesskit_update
|
||||
.take()
|
||||
.expect("AccessKit was disabled"),
|
||||
),
|
||||
texture_deltas: vec![mem::take(&mut output.textures_delta)],
|
||||
output,
|
||||
event_state: EventState::default(),
|
||||
};
|
||||
// Run the harness until it is stable, ensuring that all Areas are shown and animations are done
|
||||
harness.run();
|
||||
harness
|
||||
}
|
||||
|
||||
pub fn builder() -> HarnessBuilder {
|
||||
HarnessBuilder::default()
|
||||
}
|
||||
|
||||
/// Create a new Harness with the given app closure.
|
||||
///
|
||||
/// The ui closure will immediately be called once to create the initial ui.
|
||||
///
|
||||
/// If you e.g. want to customize the size of the window, you can use [`Harness::builder`].
|
||||
///
|
||||
/// # Example
|
||||
/// ```rust
|
||||
/// # use egui::CentralPanel;
|
||||
/// # use egui_kittest::Harness;
|
||||
/// let mut harness = Harness::new(|ctx| {
|
||||
/// CentralPanel::default().show(ctx, |ui| {
|
||||
/// ui.label("Hello, world!");
|
||||
/// });
|
||||
/// });
|
||||
/// ```
|
||||
pub fn new(app: impl FnMut(&egui::Context) + 'a) -> Self {
|
||||
Self::builder().build(app)
|
||||
}
|
||||
|
||||
/// Set the size of the window.
|
||||
/// Note: If you only want to set the size once at the beginning,
|
||||
/// prefer using [`HarnessBuilder::with_size`].
|
||||
#[inline]
|
||||
pub fn set_size(&mut self, size: Vec2) -> &mut Self {
|
||||
self.input.screen_rect = Some(Rect::from_min_size(Pos2::ZERO, size));
|
||||
self
|
||||
}
|
||||
|
||||
/// Set the DPI of the window.
|
||||
/// Note: If you only want to set the DPI once at the beginning,
|
||||
/// prefer using [`HarnessBuilder::with_dpi`].
|
||||
#[inline]
|
||||
pub fn set_dpi(&mut self, dpi: f32) -> &mut Self {
|
||||
self.ctx.set_pixels_per_point(dpi);
|
||||
self
|
||||
}
|
||||
|
||||
/// Run a frame.
|
||||
/// This will call the app closure with the current context and update the Harness.
|
||||
pub fn step(&mut self) {
|
||||
for event in self.kittest.take_events() {
|
||||
if let Some(event) = self.event_state.kittest_event_to_egui(event) {
|
||||
self.input.events.push(event);
|
||||
}
|
||||
}
|
||||
|
||||
let mut output = self.ctx.run(self.input.take(), self.update_fn.as_mut());
|
||||
self.kittest.update(
|
||||
output
|
||||
.platform_output
|
||||
.accesskit_update
|
||||
.take()
|
||||
.expect("AccessKit was disabled"),
|
||||
);
|
||||
self.texture_deltas
|
||||
.push(mem::take(&mut output.textures_delta));
|
||||
self.output = output;
|
||||
}
|
||||
|
||||
/// Run a few frames.
|
||||
/// This will soon be changed to run the app until it is "stable", meaning
|
||||
/// - all animations are done
|
||||
/// - no more repaints are requested
|
||||
pub fn run(&mut self) {
|
||||
const STEPS: usize = 2;
|
||||
for _ in 0..STEPS {
|
||||
self.step();
|
||||
}
|
||||
}
|
||||
|
||||
/// Access the [`egui::RawInput`] for the next frame.
|
||||
pub fn input(&self) -> &egui::RawInput {
|
||||
&self.input
|
||||
}
|
||||
|
||||
/// Access the [`egui::RawInput`] for the next frame mutably.
|
||||
pub fn input_mut(&mut self) -> &mut egui::RawInput {
|
||||
&mut self.input
|
||||
}
|
||||
|
||||
/// Access the [`egui::FullOutput`] for the last frame.
|
||||
pub fn output(&self) -> &egui::FullOutput {
|
||||
&self.output
|
||||
}
|
||||
|
||||
/// Access the [`kittest::State`].
|
||||
pub fn kittest_state(&self) -> &kittest::State {
|
||||
&self.kittest
|
||||
}
|
||||
}
|
||||
|
||||
impl<'t, 'n, 'h> Queryable<'t, 'n> for Harness<'h>
|
||||
where
|
||||
'n: 't,
|
||||
{
|
||||
fn node(&'n self) -> Node<'t> {
|
||||
self.kittest_state().node()
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user