Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 6 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,16 @@ egui_extras = { version = "0.37", default-features = false, features = ["svg"] }
nohash-hasher = "0.2"
web-time = "1.1"
log = "0.4"
egui_kittest = { version = "0.37", optional = true }

[features]
default = ["clipboard"]
# The system clipboard, read on demand through `Shell::read_clipboard`.
# Without it, or on wasm, a read answers `ClipboardError::Unavailable`.
clipboard = ["dep:arboard"]
# `testing::Driver`: drives a view through kittest's harness in tests. Enable
# it in a dev-dependency.
driver = ["dep:egui_kittest"]
# `testing::screenshot`: renders a harness to PNGs on Mesa lavapipe. Linux
# only for now (#337); elsewhere enabling it is a compile error.
screenshots = ["dep:egui_kittest", "egui_kittest/wgpu", "dep:egui-wgpu", "dep:pollster", "dep:image"]
Expand All @@ -39,7 +43,6 @@ wasm-bindgen = "0.2.126"
web-sys = { version = "0.3.103", features = ["Event", "EventTarget", "HtmlCanvasElement", "KeyboardEvent", "Node"] }

[target.'cfg(target_os = "linux")'.dependencies]
egui_kittest = { version = "0.37", optional = true }
egui-wgpu = { version = "0.37", optional = true, default-features = false }
pollster = { version = "1.0", optional = true }
image = { version = "0.25.10", optional = true, default-features = false, features = ["png"] }
Expand All @@ -49,6 +52,8 @@ egui = { version = "0.37", features = ["default_fonts"] }
eframe = "0.37"
egui_kittest = "0.37"
trybuild = "1"
# eggshell's own tests drive views through `testing::Driver`.
eggshell-ui = { path = ".", features = ["driver"] }
eggshell-ui-shadcn = { path = "crates/eggshell-ui-shadcn", features = ["extras"] }
eggshell-ui-extras = { path = "crates/eggshell-ui-extras", features = ["hover-card", "markdown", "badge", "spinner", "skeleton"] }

Expand Down
6 changes: 6 additions & 0 deletions src/pipeline.rs
Original file line number Diff line number Diff line change
Expand Up @@ -656,6 +656,12 @@ impl LayoutState {
&self.st.kept
}

/// The id every node's path derives from.
#[cfg_attr(not(feature = "driver"), expect(dead_code))]
pub(crate) fn root(&self) -> Id {
self.st.root
}

/// This state, reached from where only `ctx` is at hand.
pub fn shell(&self, ctx: &egui::Context) -> crate::Shell {
crate::Shell::new(ctx, &self.st.kept)
Expand Down
78 changes: 54 additions & 24 deletions src/testing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@
//! - An [`egui_kittest`](https://docs.rs/egui_kittest) `Harness` runs whole
//! frames: build, layout, draw, and the input of each frame. Use it for
//! behaviour: clicks, keys, focus, overlays, motion.
//! - TODO(prose): `Driver`, behind the `driver` feature, wraps that harness:
//! it clicks, drags, types and presses keys by the node they aim at,
//! settles after each action, asks the frame it drew, and checks that the
//! view reported nothing.
//!
//! # Headless
//!
Expand Down Expand Up @@ -170,9 +174,17 @@
//! Each row sits under its item key, and the nodes the row builder makes sit
//! under that: `placed.at(Region).at(Part::List).at(item_key).at(Part::Row(item_key))`.

#[cfg(feature = "driver")]
mod driver;
#[cfg(all(feature = "screenshots", target_os = "linux"))]
pub mod screenshot;

#[cfg(feature = "driver")]
pub use driver::{
After, Animation, Driver, DriverBuilder, Focused, Painted, PaintedText, Path, Press, Reports,
Settled, Stage, Target, TextFinding, Unsettled, WindowFocus,
};

use std::collections::HashMap;

use egui::{Id, Rect, Theme, Vec2};
Expand Down Expand Up @@ -609,20 +621,26 @@ impl<'p> Query<'p> {
#[track_caller]
#[expect(clippy::panic, reason = "a test lookup fails the test")]
pub fn at<K: Key>(&self, key: K) -> Query<'p> {
self.child(&key).unwrap_or_else(|| {
panic!(
"no node {}/{key:?}{}; below {} there are: {}",
self.path,
self.not_built_hint(),
self.display_path(),
list(
self.children()
.into_iter()
.filter_map(|index| self.layout.node(index))
.map(PlacedNode::path)
),
)
})
let readable = format!("{key:?}");
self.child_by_id(child_id_of(self.id, &key), &readable)
.unwrap_or_else(|| panic!("{}", self.missing(&readable)))
}

/// Why there is no node `readable` below this one: the path asked for,
/// then what exists below this one.
fn missing(&self, readable: &str) -> String {
format!(
"no node {}/{readable}{}; below {} there are: {}",
self.path,
self.not_built_hint(),
self.display_path(),
list(
self.children()
.into_iter()
.filter_map(|index| self.layout.node(index))
.map(PlacedNode::path)
),
)
}

/// What to add to the message for a missing node when this one is a
Expand All @@ -638,12 +656,14 @@ impl<'p> Query<'p> {

/// [`at`](Self::at), or `None` when nothing is there.
pub fn try_at<K: Key>(&self, key: K) -> Option<Query<'p>> {
self.child(&key)
self.child_by_id(child_id_of(self.id, &key), &format!("{key:?}"))
}

fn child<K: Key>(&self, key: &K) -> Option<Query<'p>> {
let id = child_id(self.id?, key);
let path = format!("{}/{key:?}", self.path);
/// The node or scope below this one whose id is `id`, which the key
/// that reads as `readable` hashes to.
fn child_by_id(&self, id: Option<Id>, readable: &str) -> Option<Query<'p>> {
let id = id?;
let path = format!("{}/{readable}", self.path);
let index = self.layout.by_id.get(&id).copied();
let is_scope = || {
let prefix = format!("{path}/");
Expand All @@ -669,6 +689,11 @@ impl<'p> Query<'p> {
#[track_caller]
#[expect(clippy::panic, reason = "a test lookup fails the test")]
pub fn text(&self, text: &str) -> Query<'p> {
self.find_text(text).unwrap_or_else(|miss| panic!("{miss}"))
}

/// [`text`](Self::text), or why no one node below this one shows `text`.
fn find_text(&self, text: &str) -> Result<Query<'p>, String> {
let texts: Vec<Shown<'p>> = self
.descendants()
.filter_map(|index| {
Expand All @@ -681,23 +706,23 @@ impl<'p> Query<'p> {
};
let matching: Vec<_> = texts.iter().filter(|(_, _, shown)| shown == text).collect();
match matching[..] {
[&(node, index, _)] => Query {
[&(node, index, _)] => Ok(Query {
layout: self.layout,
path: node.path().to_owned(),
id: node.id(),
index: Some(index),
},
[] => panic!(
}),
[] => Err(format!(
"no text {text:?} below {}; text below it: {}",
self.display_path(),
describe(&mut texts.iter()),
),
_ => panic!(
)),
_ => Err(format!(
"{} nodes show {text:?} below {}: {}",
matching.len(),
self.display_path(),
describe(&mut matching.iter().copied()),
),
)),
}
}

Expand Down Expand Up @@ -769,6 +794,11 @@ impl<'p> Query<'p> {
}
}

/// The id `key` hashes to below `parent`, if the parent has one.
fn child_id_of<K: Key>(parent: Option<Id>, key: &K) -> Option<Id> {
Some(child_id(parent?, key))
}

fn list<S: AsRef<str>>(items: impl Iterator<Item = S>) -> String {
let items: Vec<S> = items.collect();
if items.is_empty() {
Expand Down
Loading
Loading