1use 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
12pub trait TestSupportExt:
22 Element<PrepaintState = Option<Hitbox>> + InteractiveElement + Sized
23{
24 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
48pub trait TestTextExt: IntoElement + Sized {
52 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 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}