diff --git a/Cargo.lock b/Cargo.lock index 659fb5d10..97c2717a6 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -4375,6 +4375,7 @@ dependencies = [ "craft-quit", "craft-settings", "craft-svg", + "craft-testkit", "craft-theme", "craft-tracked", "craft-ui-contract", diff --git a/crates/ui-contract/Cargo.toml b/crates/ui-contract/Cargo.toml index e11954573..904c1251f 100644 --- a/crates/ui-contract/Cargo.toml +++ b/crates/ui-contract/Cargo.toml @@ -19,7 +19,7 @@ default = [] # `craft_ui::testing`: the fixture and helpers used by application tests. # The suite runner, stories, registrations and snapshots are opt-in. testing = ["craft-platform/testing"] -suite = ["testing", "dep:libtest-mimic", "snapshot"] +suite = ["testing", "dep:libtest-mimic", "dep:craft-testkit", "snapshot"] # `craft_ui::testing::snapshot`: stories rendered to images offscreen (desktop only). snapshot = ["dep:egui_kittest", "dep:image"] # Draw colour glyphs (COLR, sbix, CBDT), e.g. colour emoji. @@ -46,6 +46,8 @@ craft-settings = { workspace = true } toml = { version = "1.1", default-features = false, features = ["std", "parse", "serde"] } egui = { workspace = true } libtest-mimic = { version = "0.8", optional = true } +# The gate's sweep fraction (`hostile::divisor`), which the suite's repeat sampling follows. +craft-testkit = { workspace = true, optional = true } craft-host = { workspace = true } # `WindowSystem` and `restore::Store`, which viewport mounts place windows through. # `ui`: the window crate's egui side builds once, the same for the runtime, the shell's `app` and the apps. diff --git a/crates/ui-contract/src/testing/suite/activation.rs b/crates/ui-contract/src/testing/suite/activation.rs index 1922c7d30..719ca9754 100644 --- a/crates/ui-contract/src/testing/suite/activation.rs +++ b/crates/ui-contract/src/testing/suite/activation.rs @@ -1,7 +1,7 @@ //! Activation, declared actions, AccessKit focus and cancelled presses. use super::drive::{Plan, for_input, input_fixture, root_target, run, variant_name, variants}; -use super::{ANY, Case, Subject}; +use super::{ANY, Case, Subject, nodes}; use crate::contract::ShapeKind; use crate::runtime::access::Role; use crate::runtime::egui::access::{AccessTree, Declared, NodeKey}; @@ -9,7 +9,7 @@ use crate::runtime::egui::fixture::AccessActionKind; use crate::testing::fixture::Fixture; use crate::testing::pointer; use crate::testing::story::{EditKind, Required, StoryData, StoryOutput}; -use craft_behavior::Input; +use craft_behavior::{Input, PartRef}; use egui::{Id, Key, Modifiers}; use std::cell::{Cell, RefCell}; use std::collections::HashMap; @@ -53,6 +53,8 @@ fn each_activation_clicks_once(subject: &Subject) -> Result<(), String> { /// Every part that senses `CLICK` of a component that carries no value gives exactly one click on each path it has: /// a primary click, AccessKit `Click` when its node declares it, and Enter or Space after AccessKit `Focus` when it /// is a focus stop; a collection's parts report events, not clicks, and are read by the collection cases. Two clicks mean the part's press also reached the root's activation. +/// A repeated part runs on a fixed sample of its repeats under the gate's fraction ([`nodes::sampled_parts`]). The +/// paths a part has are read from the story's settled base fixture, so a fixture is built only for a path it has. fn each_part_activates_once(subject: &Subject) -> Result<(), String> { if subject.probe.carries_value() || matches!(subject.probe.input, Input::Text | Input::Editor | Input::Display) @@ -62,26 +64,30 @@ fn each_part_activates_once(subject: &Subject) -> Result<(), String> { } let keys = [(Key::Enter, subject.probe.keys.activate.enter()), (Key::Space, subject.probe.keys.activate.space())]; for story in &subject.stories { - let fixture = &subject.base(story).fixture; - let Some(id) = fixture.root_id() else { continue }; - let mut parts = crate::runtime::pointer::sensing_parts(fixture.ctx(), id, craft_behavior::Sense::CLICK); + let base = &subject.base(story).fixture; + let Some(id) = base.root_id() else { continue }; + let mut parts = crate::runtime::pointer::sensing_parts(base.ctx(), id, craft_behavior::Sense::CLICK); parts.dedup(); - for part in parts { + // A part with no node or a disabled one has no path, and the sample is taken of the rest. The base's node keys + // are the input fixture's. + let enabled = |part: PartRef| base.part(id, part).filter(|seen| base.tree().data_of(seen.node).is_none_or(|node| !node.is_disabled())); + parts.retain(|part| enabled(*part).is_some()); + for part in nodes::sampled_parts(parts) { + let Some(seen) = enabled(part) else { continue }; + let declares = |action| base.tree().declares_key(seen.node, action); // Paths: 0 click, 1 AccessKit Click, 2 and 3 the activation keys. - for path in 0..4 { + let paths = [ + (0, "a click", true), + (1, "AccessKit Click", declares(Declared::Click)), + (2, "Enter", keys[0].1 && declares(Declared::Focus)), + (3, "Space", keys[1].1 && declares(Declared::Focus)), + ]; + for (path, name, _) in paths.into_iter().filter(|(_, _, has)| *has) { let mut fixture = input_fixture(story); let Some(target) = fixture.root_id().and_then(|id| fixture.part(id, part)) else { continue }; if fixture.tree().data_of(target.node).is_some_and(|node| node.is_disabled()) { continue; } - let declares = |fixture: &Fixture, action| fixture.tree().declares_key(target.node, action); - let name = match path { - 0 => "a click", - 1 if declares(&fixture, Declared::Click) => "AccessKit Click", - 2 if keys[0].1 && declares(&fixture, Declared::Focus) => "Enter", - 3 if keys[1].1 && declares(&fixture, Declared::Focus) => "Space", - _ => continue, - }; fixture.take_outputs(); match path { 0 => { @@ -121,7 +127,8 @@ fn signature(tree: &AccessTree) -> HashMap { .collect() } -/// Every action a node declares changes something: a click, an edit, a node state or the focus. +/// Every action a node declares changes something: a click, an edit, a node state or the focus. A repeated set of +/// nodes runs on a fixed sample under the gate's fraction ([`nodes::sampled_actions`]). fn declared_actions_are_handled(subject: &Subject) -> Result<(), String> { for story in subject.stories.iter().filter(|story| !story.covers.contains(&Required::Invalid)) { let fixture = &subject.base(story).fixture; @@ -131,12 +138,15 @@ fn declared_actions_are_handled(subject: &Subject) -> Result<(), String> { (Declared::Expand, AccessActionKind::Expand), ]; let tree = fixture.tree(); - let nodes: Vec<_> = tree + let declared: Vec<(NodeKey, Declared)> = tree .keys() .into_iter() .filter(|key| tree.data_of(*key).is_some_and(|node| !node.is_disabled())) - .flat_map(|key| actions.iter().filter(move |(declared, _)| tree.declares_key(key, *declared)).map(move |(_, kind)| (key, *kind))) + .flat_map(|key| actions.iter().filter(move |(declared, _)| tree.declares_key(key, *declared)).map(move |(declared, _)| (key, *declared))) .collect(); + let nodes = nodes::sampled_actions(tree, declared) + .into_iter() + .filter_map(|(key, declared)| actions.iter().find(|(known, _)| *known == declared).map(|(_, kind)| (key, *kind))); for (key, kind) in nodes { let mut fixture = input_fixture(story); let Some(target) = fixture.target(key) else { continue }; @@ -261,14 +271,19 @@ fn every_intent_has_a_declared_action(subject: &Subject) -> Result<(), String> { Ok(()) } -/// AccessKit `Focus` on a node that declares it gives the component key focus. +/// AccessKit `Focus` on a node that declares it gives the component key focus. A story whose settled base fixture has +/// no enabled node that declares it has nothing to check. fn access_focus_focuses(subject: &Subject) -> Result<(), String> { + let focusable = |fixture: &Fixture, key: NodeKey| { + fixture.tree().declares_key(key, Declared::Focus) && fixture.tree().data_of(key).is_some_and(|node| !node.is_disabled()) + }; for story in &subject.stories { + let base = &subject.base(story).fixture; + if !base.tree().keys().into_iter().any(|key| focusable(base, key)) { + continue; + } let mut fixture = input_fixture(story); - let node = - fixture.tree().keys().into_iter().find(|key| { - fixture.tree().declares_key(*key, Declared::Focus) && fixture.tree().data_of(*key).is_some_and(|node| !node.is_disabled()) - }); + let node = fixture.tree().keys().into_iter().find(|key| focusable(&fixture, *key)); let Some(target) = node.and_then(|key| fixture.target(key)) else { continue }; fixture.access(AccessActionKind::Focus, &target); fixture.settle(); diff --git a/crates/ui-contract/src/testing/suite/nodes.rs b/crates/ui-contract/src/testing/suite/nodes.rs index ba0d7fc58..20cf49b1e 100644 --- a/crates/ui-contract/src/testing/suite/nodes.rs +++ b/crates/ui-contract/src/testing/suite/nodes.rs @@ -1,8 +1,9 @@ //! What the container and collection cases read from the AccessKit tree of a fixture. use crate::runtime::access::Role; -use crate::runtime::egui::access::{AccessTree, NodeKey}; +use crate::runtime::egui::access::{AccessTree, Declared, NodeKey}; use crate::testing::fixture::{Fixture, Target}; +use craft_behavior::PartRef; /// `node` and every node below it. pub(super) fn subtree(tree: &AccessTree, node: NodeKey) -> Vec { @@ -77,3 +78,85 @@ pub(super) fn position_in_set(fixture: &Fixture, row: NodeKey) -> Option<(usize, pub(super) fn is_selected(fixture: &Fixture, node: NodeKey) -> bool { fixture.tree().data_of(node).and_then(|data| data.is_selected()).unwrap_or(false) } + +/// Whether the cases that run once per member of a repeated set (each repeat of a part, each row that declares an +/// action) run on a fixed sample of the set: under the gate's sweep fraction, `craft_testkit::hostile::divisor` above +/// 1. The nightly run sets no fraction and checks every member. +fn samples_repeats() -> bool { + #[cfg(not(target_arch = "wasm32"))] + { + craft_testkit::hostile::divisor() > 1 + } + #[cfg(target_arch = "wasm32")] + { + false + } +} + +/// The first, middle and last of `members`; all of them when there are three or fewer. The members of a repeated set +/// come from one code path, so these cover it, its edges included. +fn sample(members: &[T]) -> Vec { + match members.len() { + 0..=3 => members.to_vec(), + n => vec![members[0], members[n / 2], members[n - 1]], + } +} + +/// `parts` with each repeated part ([`PartRef::index`]) cut to the lowest, middle and highest index of its key when +/// [`samples_repeats`]; a part that occurs once always stays. +pub(super) fn sampled_parts(parts: Vec) -> Vec { + if !samples_repeats() { + return parts; + } + let mut kept: Vec = Vec::new(); + for part in &parts { + if part.index.is_none() { + kept.push(*part); + continue; + } + if kept.iter().any(|done| done.key == part.key && done.index.is_some()) { + continue; + } + let mut indices: Vec = parts.iter().filter(|other| other.key == part.key).filter_map(|other| other.index).collect(); + indices.sort_unstable(); + indices.dedup(); + kept.extend(sample(&indices).into_iter().map(|index| part.key.at(index))); + } + kept +} + +/// The keys of `tree` depth first from its root, children in their declared order; nodes the root does not reach +/// follow in the tree's own order. +fn tree_order(tree: &AccessTree) -> Vec { + let mut ordered = Vec::new(); + let mut seen = std::collections::HashSet::new(); + let mut pending: Vec = tree.root_key().into_iter().collect(); + while let Some(key) = pending.pop() { + if seen.insert(key) { + ordered.push(key); + pending.extend(tree.children_of(key).into_iter().rev()); + } + } + ordered.extend(tree.keys().into_iter().filter(|key| !seen.contains(key))); + ordered +} + +/// `pairs` (a node and an action it declares) in tree order; when [`samples_repeats`], set by set, each repeated set +/// cut to its first, middle and last node. Nodes that share a parent and a role and declare the same action are one set. +pub(super) fn sampled_actions(tree: &AccessTree, mut pairs: Vec<(NodeKey, Declared)>) -> Vec<(NodeKey, Declared)> { + let rank: std::collections::HashMap = tree_order(tree).into_iter().enumerate().map(|(at, key)| (key, at)).collect(); + pairs.sort_by_key(|(node, _)| rank.get(node).copied().unwrap_or(usize::MAX)); + let ordered = pairs; + if !samples_repeats() { + return ordered; + } + let set_of = |(node, declared): &(NodeKey, Declared)| (tree.parent_of_key(*node), tree.role_of(*node), *declared); + let mut sets: Vec> = Vec::new(); + for pair in ordered { + match sets.iter_mut().find(|set| set_of(&set[0]) == set_of(&pair)) { + Some(set) => set.push(pair), + None => sets.push(vec![pair]), + } + } + sets.iter().flat_map(|set| sample(set)).collect() +} diff --git a/crates/ui-contract/src/testing/suite/pointer.rs b/crates/ui-contract/src/testing/suite/pointer.rs index 20c396516..15fd8f53f 100644 --- a/crates/ui-contract/src/testing/suite/pointer.rs +++ b/crates/ui-contract/src/testing/suite/pointer.rs @@ -1,14 +1,14 @@ //! The pointer rows of 22.4: slop, hit areas and the arena winner. use super::drive::{Plan, input_fixture, root_target}; -use super::{ANY, Case, Subject}; +use super::{ANY, Case, Subject, nodes}; use crate::contract::ShapeKind; use crate::runtime::egui::access::Declared; use crate::testing::fixture::{Fixture, Target}; use crate::testing::sensing::{ProbeMember, ProbePart, ProbeRoot, RootReport, show}; use crate::testing::story::{EditKind, Grip, Required}; use crate::testing::{Frames, input, pointer}; -use craft_behavior::{HitLevel, PartKey, Sense}; +use craft_behavior::{HitLevel, PartKey, PartRef, Sense}; use egui::{Pos2, Rect, Ui, Vec2}; use std::cell::{Cell, RefCell}; use std::collections::HashSet; @@ -96,8 +96,17 @@ fn at(target: &Target, point: Pos2) -> Target { Target { node: target.node, rect: Rect::from_center_size(point, Vec2::ZERO) } } +/// The visible piece of `part` of the shown component, where a press on it lands, when it is enabled and not empty: one +/// past the window's edge cannot be seen, so not pressed. +fn pressable(fixture: &Fixture, part: PartRef) -> Option { + let target = fixture.root_id().and_then(|id| fixture.part(id, part)).and_then(|target| fixture.visible(&target))?; + let enabled = fixture.tree().data_of(target.node).is_some_and(|node| !node.is_disabled()); + (enabled && target.rect.area() > 0.0).then_some(target) +} + /// A press inside the visible piece of a part's hit area (inside the root's clip), also where it lies outside the root's -/// rect, is taken by the component; on the part a drag moves, a press off its middle moves the value. +/// rect, is taken by the component; on the part a drag moves, a press off its middle moves the value. A repeated part +/// runs on a fixed sample of its repeats under the gate's fraction ([`nodes::sampled_parts`]). fn parts_take_presses_in_their_hit_areas(subject: &Subject) -> Result<(), String> { let probe = subject.probe; for story in &subject.stories { @@ -109,15 +118,11 @@ fn parts_take_presses_in_their_hit_areas(subject: &Subject) -> Result<(), String parts.push(part); } } - for part in parts { + // A part the settled base shows no pressable piece of gets no fixture, and the sample is taken of the rest. + parts.retain(|part| pressable(fixture, *part).is_some()); + for part in nodes::sampled_parts(parts) { let mut fixture = input_fixture(story); - // A press lands on the part's visible piece: one past the window's edge cannot be seen, so not pressed. - let Some(target) = fixture.root_id().and_then(|id| fixture.part(id, part)).and_then(|target| fixture.visible(&target)) else { - continue; - }; - if fixture.tree().data_of(target.node).is_none_or(|node| node.is_disabled()) || target.rect.area() <= 0.0 { - continue; - } + let Some(target) = pressable(&fixture, part) else { continue }; let point = probe_point(target.rect, root_target(&fixture).rect); fixture.press(&at(&target, point)); if !pointer::press_captured(fixture.ctx()) {