diff --git a/crates/ui-contract/src/contract/run.rs b/crates/ui-contract/src/contract/run.rs index fa0d9923d..95a70b207 100644 --- a/crates/ui-contract/src/contract/run.rs +++ b/crates/ui-contract/src/contract/run.rs @@ -939,16 +939,22 @@ fn sense_steps(p: &mut Pass<'_, '_, '_>, ui: &mut Ui, host_at: Option, e if !inert && facts.text() { parts.text.commit_now = focus::commit_now(&ctx) == Some(id); } - // A component that declares no parts skips the step; a debug build runs it anyway to check the declaration. let declares_parts = facts.parts || e.declares_parts(); - if !inert && (declares_parts || cfg!(debug_assertions)) { + #[cfg(feature = "testing")] + let checking_parts = !inert && !declares_parts && crate::testing::contract::checking_parts(); + #[cfg(not(feature = "testing"))] + let checking_parts = false; + if !inert && (declares_parts || checking_parts) { let input = MeasureIn { ui: &*ui, env, proposal: Proposal::Ideal, room: p.room }; e.parts(rect, flow, content_rect, &mut parts, &input, rtl); } + #[cfg(feature = "testing")] + if checking_parts && !declares_parts && !steps::declares_none(&parts) { + crate::testing::contract::bad_parts(facts.name); + } if facts.canvas_control && p.trace.recording() { inspect::canvas_control(&ctx, facts.name); } - debug_assert!(declares_parts || steps::declares_none(&parts), "`{}` sets `PARTS = false` and declares a part", facts.name); steps::check_parts(&ctx, facts.name, &parts); let keys = e.keys(); let (holder_widget, text_keys) = text_holder(&ctx, id, &parts, keys, &mut *e.core().memory); diff --git a/crates/ui-contract/src/contract/steps.rs b/crates/ui-contract/src/contract/steps.rs index 3d4f3551e..6c8b9c22c 100644 --- a/crates/ui-contract/src/contract/steps.rs +++ b/crates/ui-contract/src/contract/steps.rs @@ -31,7 +31,9 @@ pub(crate) use id::{claim_id, draft, id, keeping_ids}; pub(crate) use label::scrub_by_label; pub(crate) use measure::{MeasureIn, guard, measure, measure_content, measure_cx, padding, whole}; pub(crate) use paint::{Fade, UnderVeil, fade, next_shape, paint_focus, paint_part_layers, paint_surface, surface_ink, unfade}; -pub(crate) use parts::{PartsStep, check_parts, declares_none}; +#[cfg(feature = "testing")] +pub(crate) use parts::declares_none; +pub(crate) use parts::{PartsStep, check_parts}; pub(crate) use place::place; pub(crate) use report::{ReportIn, report, report_inert, report_parts, validity_of}; #[cfg(feature = "testing")] diff --git a/crates/ui-contract/src/contract/steps/parts.rs b/crates/ui-contract/src/contract/steps/parts.rs index 455ffc0d7..f09043f22 100644 --- a/crates/ui-contract/src/contract/steps/parts.rs +++ b/crates/ui-contract/src/contract/steps/parts.rs @@ -14,6 +14,7 @@ pub(crate) struct PartsStep { } /// Whether step 7 declared no parts, no roving group, no scroll part and no text, editor or selection part. +#[cfg(feature = "testing")] pub(crate) fn declares_none(parts: &PartsStep) -> bool { parts.decls.is_empty() && parts.rovings.is_empty() diff --git a/crates/ui-contract/src/testing/contract.rs b/crates/ui-contract/src/testing/contract.rs index f7c0ae238..d5631f3df 100644 --- a/crates/ui-contract/src/testing/contract.rs +++ b/crates/ui-contract/src/testing/contract.rs @@ -48,3 +48,33 @@ pub fn raw_widget(ui: &mut egui::Ui, rect: egui::Rect) { pub fn allocate(ui: &mut egui::Ui, size: egui::Vec2) { crate::runtime::egui::fixture::allocate_blocker(ui, size); } + +thread_local! { + static PARTS_CHECK: std::cell::RefCell>> = const { std::cell::RefCell::new(None) }; +} + +/// Run a story with the parts declaration check enabled, and return any offending component types. +pub(crate) fn check_parts_declarations(run: impl FnOnce()) -> Vec<&'static str> { + struct Reset; + impl Drop for Reset { + fn drop(&mut self) { + PARTS_CHECK.with(|check| *check.borrow_mut() = None); + } + } + PARTS_CHECK.with(|check| *check.borrow_mut() = Some(Vec::new())); + let _reset = Reset; + run(); + PARTS_CHECK.with(|check| check.borrow_mut().take().unwrap_or_default()) +} + +pub(crate) fn checking_parts() -> bool { + PARTS_CHECK.with(|check| check.borrow().is_some()) +} + +pub(crate) fn bad_parts(component: &'static str) { + PARTS_CHECK.with(|check| { + if let Some(bad) = check.borrow_mut().as_mut() { + bad.push(component); + } + }); +} diff --git a/crates/ui-contract/src/testing/suite/shape.rs b/crates/ui-contract/src/testing/suite/shape.rs index a2d80820b..0117b2474 100644 --- a/crates/ui-contract/src/testing/suite/shape.rs +++ b/crates/ui-contract/src/testing/suite/shape.rs @@ -4,7 +4,20 @@ use super::{ANY, Case, Subject}; use crate::testing::fixture::Fixture; use egui::{Rect, Vec2}; -pub(super) const CASES: &[Case] = &[Case::new("paints-inside", ANY, small_rooms_paint_inside_the_shown_rect)]; +pub(super) const CASES: &[Case] = + &[Case::new("paints-inside", ANY, small_rooms_paint_inside_the_shown_rect), Case::new("parts-declaration", ANY, parts_declaration)]; + +fn parts_declaration(subject: &Subject) -> Result<(), String> { + for story in &subject.stories { + let bad = crate::testing::contract::check_parts_declarations(|| { + Fixture::story(story).frame(Vec::new()); + }); + if !bad.is_empty() { + return Err(format!("{}: components with PARTS = false declared parts: {bad:?}", story.name)); + } + } + Ok(()) +} /// The slack between a painted bound and the shown rect: the rounding of the layout, as the layout cases allow. A /// shape's own slack adds half its stroke width. diff --git a/crates/ui-contract/tests/it/contract/fixes.rs b/crates/ui-contract/tests/it/contract/fixes.rs index fc1da8d47..1084dba64 100644 --- a/crates/ui-contract/tests/it/contract/fixes.rs +++ b/crates/ui-contract/tests/it/contract/fixes.rs @@ -237,3 +237,42 @@ fn the_step_log_is_kept_in_fixture_frames_only() { let id = seen.borrow().id.expect("the probe ran"); assert!(steps(&app.ctx, id).is_empty(), "an app frame records no steps"); } + +/// A component whose declaration says the pipeline need not run its parts hook. +#[derive(Component)] +struct NoParts<'a> { + #[common] + common: Common, + calls: &'a std::cell::Cell, +} + +impl Component for NoParts<'_> { + const ROLE: Role = Role::None; + const PARTS: bool = false; + type Style = PlainStyle; + type Output = Response; + + fn style(_: &StyleCx<'_>, _: NoPartsStyleKey) -> PlainStyle { + PlainStyle::default() + } + + fn parts(&mut self, _: Rect, _: &mut PartsCx<'_, Self>) { + self.calls.set(self.calls.get() + 1); + } + + fn paint(&mut self, _: &mut PaintUi<'_>, _: &Cx<'_, Self>) {} + + fn finish(self, cx: Cx<'_, Self>) -> Response { + cx.response.clone() + } +} + +#[trial] +fn a_component_declaring_no_parts_skips_the_parts_hook() { + let mut frames = Frames::new(); + let calls = std::cell::Cell::new(0); + frames.frame(vec![], |ui| { + NoParts { common: Common::new(), calls: &calls }.show(ui); + }); + assert_eq!(calls.get(), 0, "the parts hook is skipped on ordinary runs"); +}