1use 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
11pub trait TestSupportExt:
21 Element<PrepaintState = Option<Hitbox>> + InteractiveElement + Sized
22{
23 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
47pub trait TestTextExt: IntoElement + Sized {
51 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 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}