Skip to main content

gpui_deck/
observe.rs

1// Modified for GPUI Deck: update references to the extracted crate.
2//! Registers native elements without accepting a second description of their state.
3use gpui::{Element, Hitbox, InteractiveElement, IntoElement, SharedString};
4
5#[doc(hidden)]
6#[cfg(feature = "test-support")]
7pub type ObservedElement<E> = crate::test_support::Observed<E>;
8#[doc(hidden)]
9#[cfg(not(feature = "test-support"))]
10pub type ObservedElement<E> = E;
11
12/// Registers an existing element for UI queries. Its native accessibility
13/// properties are read automatically; there are no test-only property setters.
14///
15/// Test-only values cannot override the native control:
16/// ```compile_fail
17/// use gpui::{div, prelude::*};
18/// use gpui_deck::TestSupportExt;
19/// div().id("input").test_support().test_props(|props| props.value("invented"));
20/// ```
21pub trait TestSupportExt:
22    Element<PrepaintState = Option<Hitbox>> + InteractiveElement + Sized
23{
24    /// With `test-support`, observes the element without adding a layout node.
25    /// Otherwise returns the original element with its exact native type.
26    /// Call before `track_focus` so the actual focus binding can be observed.
27    /// Querying focus on a focus-capable element with a missed binding panics.
28    fn test_support(self) -> ObservedElement<Self> {
29        #[cfg(feature = "test-support")]
30        {
31            crate::test_support::Observed::new(self)
32        }
33        #[cfg(not(feature = "test-support"))]
34        {
35            self
36        }
37    }
38}
39impl<E: Element<PrepaintState = Option<Hitbox>> + InteractiveElement> TestSupportExt for E {}
40
41#[doc(hidden)]
42#[cfg(feature = "test-support")]
43pub type ObservedTextElement<E> = crate::test_support::ObservedText<E>;
44#[doc(hidden)]
45#[cfg(not(feature = "test-support"))]
46pub type ObservedTextElement<E> = E;
47
48/// Records the text an element displays, for text queries in UI tests.
49/// Text components call it with the text they render, so plain text needs
50/// no element id.
51pub trait TestTextExt: IntoElement + Sized {
52    /// With `test-support`, records `text` each time the element paints.
53    /// Otherwise returns the element it builds.
54    fn test_text(self, text: impl Into<SharedString>) -> ObservedTextElement<Self::Element> {
55        #[cfg(feature = "test-support")]
56        {
57            crate::test_support::ObservedText::new(self.into_element(), text.into(), false)
58        }
59        #[cfg(not(feature = "test-support"))]
60        {
61            let _ = text;
62            self.into_element()
63        }
64    }
65
66    /// Like [`Self::test_text`] for an image's alternative text, which
67    /// stands in for what the element draws.
68    fn test_alt_text(self, text: impl Into<SharedString>) -> ObservedTextElement<Self::Element> {
69        #[cfg(feature = "test-support")]
70        {
71            crate::test_support::ObservedText::new(self.into_element(), text.into(), true)
72        }
73        #[cfg(not(feature = "test-support"))]
74        {
75            let _ = text;
76            self.into_element()
77        }
78    }
79}
80impl<E: IntoElement> TestTextExt for E {}
81
82#[cfg(test)]
83mod tests {
84    use super::*;
85    use gpui::div;
86
87    #[test]
88    fn observation_preserves_identity_and_native_type_in_normal_builds() {
89        let element = div().id("target").test_support().test_support();
90        assert_eq!(Element::id(&element), Some("target".into()));
91        #[cfg(not(feature = "test-support"))]
92        let _: gpui::Stateful<gpui::Div> = element;
93    }
94
95    #[test]
96    fn text_observation_keeps_builder_methods() {
97        use gpui::{ParentElement as _, Styled as _};
98
99        let element = div()
100            .test_text("example")
101            .id("target")
102            .text_sm()
103            .child("example");
104        assert_eq!(Element::id(&element), Some("target".into()));
105    }
106}