Skip to main content

gpui_base/
observe.rs

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