Skip to content

Commit

Permalink
Add Harness::builder and add README.md
Browse files Browse the repository at this point in the history
  • Loading branch information
lucasmerlin committed Oct 5, 2024
1 parent e0d8263 commit e1a8196
Show file tree
Hide file tree
Showing 10 changed files with 244 additions and 164 deletions.
5 changes: 2 additions & 3 deletions crates/egui_demo_lib/src/demo/demo_app_windows.rs
Original file line number Diff line number Diff line change
Expand Up @@ -383,7 +383,7 @@ mod tests {
use crate::demo::demo_app_windows::Demos;
use egui::Vec2;
use egui_kittest::kittest::Queryable;
use egui_kittest::snapshot::try_image_snapshot;
use egui_kittest::try_image_snapshot;
use egui_kittest::wgpu::TestRenderer;
use egui_kittest::Harness;

Expand All @@ -406,14 +406,13 @@ mod tests {
// We need to run the app for multiple frames before all windows have the right size
harness.run();
harness.run();
harness.run();

let window = harness.node().children().next().unwrap();
// TODO(lucasmerlin): Windows should probably have a label?
//let window = harness.get_by_name(name);

let size = window.raw_bounds().expect("window bounds").size();
harness = harness.with_size(Vec2::new(size.width as f32, size.height as f32));
harness.set_size(Vec2::new(size.width as f32, size.height as f32));

// We need to run the app for some more frames...
harness.run();
Expand Down
8 changes: 5 additions & 3 deletions crates/egui_demo_lib/src/demo/widget_gallery.rs
Original file line number Diff line number Diff line change
Expand Up @@ -291,7 +291,7 @@ mod tests {
use super::*;
use crate::View;
use egui::{CentralPanel, Context, Vec2};
use egui_kittest::snapshot::image_snapshot;
use egui_kittest::image_snapshot;
use egui_kittest::wgpu::TestRenderer;
use egui_kittest::Harness;

Expand All @@ -303,10 +303,12 @@ mod tests {
demo.ui(ui);
});
};
let mut harness = Harness::new(app)
let mut harness = Harness::builder()
.with_size(Vec2::new(380.0, 550.0))
.with_dpi(2.0);
.with_dpi(2.0)
.build(app);

// The first and second frames are slightly different, so we take the second frame
harness.run();

let image = TestRenderer::new().render(&harness);
Expand Down
4 changes: 2 additions & 2 deletions crates/egui_demo_lib/tests/snapshots/widget_gallery.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
35 changes: 35 additions & 0 deletions crates/egui_kittest/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# egui_kittest

Ui testing library for egui, based on [kittest](https://github.com/rerun-io/kittest) (a AccessKit based testing library).

```rust
use egui::accesskit::{Role, Toggled};
use egui::{CentralPanel, Context, TextEdit, Vec2};
use egui_kittest::Harness;
use kittest::Queryable;
use std::cell::RefCell;

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");
}
```
50 changes: 0 additions & 50 deletions crates/egui_kittest/examples/kittest.rs

This file was deleted.

Binary file removed crates/egui_kittest/kittest.png
Binary file not shown.
33 changes: 33 additions & 0 deletions crates/egui_kittest/src/builder.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
use crate::Harness;
use egui::{Pos2, Rect, Vec2};

pub struct HarnessBuilder {
pub(crate) screen_rect: Rect,
pub(crate) dpi: f32,
}

impl Default for HarnessBuilder {
fn default() -> Self {
Self {
screen_rect: Rect::from_min_size(Pos2::ZERO, Vec2::new(800.0, 600.0)),
dpi: 1.0,
}
}
}

impl HarnessBuilder {
pub fn with_size(mut self, size: Vec2) -> Self {
self.screen_rect.set_width(size.x);
self.screen_rect.set_height(size.y);
self
}

pub fn with_dpi(mut self, dpi: f32) -> Self {
self.dpi = dpi;
self
}

pub fn build<'a>(self, app: impl FnMut(&egui::Context) + 'a) -> Harness<'a> {
Harness::from_builder(&self, app)
}
}
Loading

0 comments on commit e1a8196

Please sign in to comment.