Skip to content

Commit

Permalink
WIP: Debug tests with eframe
Browse files Browse the repository at this point in the history
  • Loading branch information
lucasmerlin committed Nov 30, 2024
1 parent 10791cc commit b4a6590
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 1 deletion.
1 change: 1 addition & 0 deletions Cargo.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1363,6 +1363,7 @@ version = "0.29.1"
dependencies = [
"dify",
"document-features",
"eframe",
"egui",
"egui-wgpu",
"egui_kittest",
Expand Down
5 changes: 5 additions & 0 deletions crates/egui_kittest/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,15 @@ wgpu = ["dep:egui-wgpu", "dep:pollster", "dep:image"]
# Adds a dify-based image snapshot utility.
snapshot = ["dep:dify", "dep:image", "image/png"]

# Allows debugging tests via eframe.
run_with_eframe = ["dep:eframe"]

default = ["eframe?/wgpu"]

[dependencies]
kittest.workspace = true
egui = { workspace = true, features = ["accesskit"] }
eframe = { workspace = true, features = ["accesskit"], optional = true }

# wgpu dependencies
egui-wgpu = { workspace = true, optional = true }
Expand Down
17 changes: 16 additions & 1 deletion crates/egui_kittest/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ mod snapshot;
pub use snapshot::*;
use std::fmt::{Debug, Formatter};
mod app_kind;
#[cfg(feature = "run_with_eframe")]
mod run_with_eframe;
#[cfg(feature = "wgpu")]
mod texture_to_image;
#[cfg(feature = "wgpu")]
Expand All @@ -23,7 +25,7 @@ use std::mem;
use crate::app_kind::AppKind;
use crate::event::EventState;
pub use builder::*;
use egui::{Pos2, Rect, TexturesDelta, Vec2, ViewportId};
use egui::{Pos2, Rect, TexturesDelta, Vec2, ViewportBuilder, ViewportId};
use kittest::{Node, Queryable};

/// The test Harness. This contains everything needed to run the test.
Expand Down Expand Up @@ -268,6 +270,19 @@ impl<'a, State> Harness<'a, State> {
physical_key: None,
});
}

#[cfg(feature = "run_with_eframe")]
pub fn run_with_eframe(&mut self) {
let native_options = eframe::NativeOptions {
viewport: ViewportBuilder::default().with_inner_size(self.ctx.screen_rect().size()),
..Default::default()
};
eframe::run_native(
"egui_debug",
native_options,
Box::new(|cc| Ok(Box::new(self))),
);
}
}

/// Utilities for stateless harnesses.
Expand Down
13 changes: 13 additions & 0 deletions crates/egui_kittest/src/run_with_eframe.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
use crate::Harness;
use eframe::Frame;
use egui::Context;

impl<'a, State> eframe::App for &mut Harness<'a, State> {
fn update(&mut self, ctx: &Context, frame: &mut Frame) {
self.app.run(ctx, &mut self.state, false);
}

fn persist_egui_memory(&self) -> bool {
false
}
}
2 changes: 2 additions & 0 deletions crates/egui_kittest/tests/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,7 @@ fn test_shrink() {

harness.fit_contents();

harness.run_with_eframe();

harness.wgpu_snapshot("test_shrink");
}

0 comments on commit b4a6590

Please sign in to comment.