Skip to content

Commit

Permalink
Demo app test improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
lucasmerlin committed Dec 29, 2024
1 parent c663e21 commit 7d627a8
Show file tree
Hide file tree
Showing 8 changed files with 74 additions and 20 deletions.
2 changes: 2 additions & 0 deletions crates/eframe/src/epi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@ impl CreationContext<'_> {
storage: None,
gl: None,
get_proc_address: None,
#[cfg(feature = "wgpu")]
wgpu_render_state: None,
raw_window_handle: Err(HandleError::NotSupported),
raw_display_handle: Err(HandleError::NotSupported),
Expand Down Expand Up @@ -682,6 +683,7 @@ impl Frame {
raw_display_handle: Err(HandleError::NotSupported),
raw_window_handle: Err(HandleError::NotSupported),
storage: None,
#[cfg(feature = "wgpu")]
wgpu_render_state: None,
}
}
Expand Down
4 changes: 2 additions & 2 deletions crates/egui_demo_app/src/apps/image_viewer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,8 @@ impl eframe::App for ImageViewer {
fn update(&mut self, ctx: &egui::Context, _: &mut eframe::Frame) {
egui::TopBottomPanel::new(TopBottomSide::Top, "url bar").show(ctx, |ui| {
ui.horizontal_centered(|ui| {
ui.label("URI:");
ui.text_edit_singleline(&mut self.uri_edit_text);
let label = ui.label("URI:");
ui.text_edit_singleline(&mut self.uri_edit_text).labelled_by(label.id);
if ui.small_button("✔").clicked() {
ctx.forget_image(&self.current_uri);
self.uri_edit_text = self.uri_edit_text.trim().to_owned();
Expand Down
3 changes: 3 additions & 0 deletions crates/egui_demo_app/tests/snapshots/clock.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3 changes: 3 additions & 0 deletions crates/egui_demo_app/tests/snapshots/easymarkeditor.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3 changes: 3 additions & 0 deletions crates/egui_demo_app/tests/snapshots/imageviewer.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
51 changes: 39 additions & 12 deletions crates/egui_demo_app/tests/test_demo_app.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use egui::accesskit::Role;
use egui::{Key, Modifiers};
use egui_demo_app::{Anchor, WrapApp};
use egui_kittest::kittest::Queryable;

Expand All @@ -16,24 +17,50 @@ fn test_demo_app() {
.map(|(name, anchor, _)| (name, anchor))
.collect::<Vec<_>>();

#[cfg(feature = "wgpu")]
assert!(
apps.iter()
.find(|(_, anchor)| matches!(anchor, Anchor::Custom3d))
.is_some(),
"Expected to find the Custom3d app.",
);
// #[cfg(feature = "wgpu")]
// assert!(
// apps.iter()
// .find(|(_, anchor)| matches!(anchor, Anchor::Custom3d))
// .is_some(),
// "Expected to find the Custom3d app.",
// );

let mut results = vec![];

for (name, anchor) in apps {
// The widget gallery demo shows the current date, so we can't use it for snapshot testing
if matches!(anchor, Anchor::Demo) {
continue;
}

harness.get_by_role_and_label(Role::Button, name).click();

match anchor {
// The widget gallery demo shows the current date, so we can't use it for snapshot testing
Anchor::Demo => {
continue;
}
// This is already tested extensively elsewhere
Anchor::Rendering => {
continue;
}
// We don't want to rely on a network connection for tests
Anchor::Http => {
continue;
}
// Load a local image where we know it exists and loads quickly
Anchor::ImageViewer => {
harness.run();

harness
.get_by_role_and_label(Role::TextInput, "URI:")
.focus();
harness.press_key_modifiers(Modifiers::COMMAND, Key::A);

harness
.get_by_role_and_label(Role::TextInput, "URI:")
.type_text(format!("file://../eframe/data/icon.png"));

harness.get_by_role_and_label(Role::Button, "✔").click();
}
_ => {}
}

harness.run();

harness
Expand Down
5 changes: 1 addition & 4 deletions crates/egui_kittest/src/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,12 +99,9 @@ impl<State> HarnessBuilder<State> {
}

/// Create a new [Harness] from the given eframe creation closure.
/// If the wgpu feature is enabled and [HarnessBuilder::wgpu] was called, the
/// [eframe::CreationContext] and [eframe::Frame] will have a [egui_wgpu::RenderState]
/// attached.
pub fn build_eframe<'a>(
self,
build: impl FnOnce(&mut eframe::CreationContext) -> State,
build: impl FnOnce(&mut eframe::CreationContext<'a>) -> State,
) -> Harness<'a, State>
where
State: eframe::App,
Expand Down
23 changes: 21 additions & 2 deletions crates/egui_kittest/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,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::{Modifiers, Pos2, Rect, TexturesDelta, Vec2, ViewportId};
use kittest::{Node, Queryable};

/// The test Harness. This contains everything needed to run the test.
Expand Down Expand Up @@ -155,7 +155,7 @@ impl<'a, State> Harness<'a, State> {
}

/// Create a new [Harness] from the given eframe creation closure.
pub fn new_eframe(builder: impl FnOnce(&mut eframe::CreationContext) -> State) -> Self
pub fn new_eframe(builder: impl FnOnce(&mut eframe::CreationContext<'a>) -> State) -> Self
where
State: eframe::App,
{
Expand Down Expand Up @@ -277,6 +277,25 @@ impl<'a, State> Harness<'a, State> {
physical_key: None,
});
}

/// Press a key with modifiers.
/// This will create a key down event and a key up event.
pub fn press_key_modifiers(&mut self, modifiers: Modifiers, key: egui::Key) {
self.input.events.push(egui::Event::Key {
key,
pressed: true,
modifiers,
repeat: false,
physical_key: None,
});
self.input.events.push(egui::Event::Key {
key,
pressed: false,
modifiers,
repeat: false,
physical_key: None,
});
}
}

/// Utilities for stateless harnesses.
Expand Down

0 comments on commit 7d627a8

Please sign in to comment.