use gpui::{Element, Hitbox, InteractiveElement, IntoElement, SharedString};
#[doc(hidden)]
#[cfg(feature = "test-support")]
pub type ObservedElement<E> = crate::test_support::Observed<E>;
#[doc(hidden)]
#[cfg(not(feature = "test-support"))]
pub type ObservedElement<E> = E;
pub trait TestSupportExt:
Element<PrepaintState = Option<Hitbox>> + InteractiveElement + Sized
{
fn test_support(self) -> ObservedElement<Self> {
#[cfg(feature = "test-support")]
{
crate::test_support::Observed::new(self)
}
#[cfg(not(feature = "test-support"))]
{
self
}
}
}
impl<E: Element<PrepaintState = Option<Hitbox>> + InteractiveElement> TestSupportExt for E {}
#[doc(hidden)]
#[cfg(feature = "test-support")]
pub type ObservedTextElement<E> = crate::test_support::ObservedText<E>;
#[doc(hidden)]
#[cfg(not(feature = "test-support"))]
pub type ObservedTextElement<E> = E;
pub trait TestTextExt: IntoElement + Sized {
fn test_text(self, text: impl Into<SharedString>) -> ObservedTextElement<Self::Element> {
#[cfg(feature = "test-support")]
{
crate::test_support::ObservedText::new(self.into_element(), text.into(), false)
}
#[cfg(not(feature = "test-support"))]
{
let _ = text;
self.into_element()
}
}
fn test_alt_text(self, text: impl Into<SharedString>) -> ObservedTextElement<Self::Element> {
#[cfg(feature = "test-support")]
{
crate::test_support::ObservedText::new(self.into_element(), text.into(), true)
}
#[cfg(not(feature = "test-support"))]
{
let _ = text;
self.into_element()
}
}
}
impl<E: IntoElement> TestTextExt for E {}
#[cfg(test)]
mod tests {
use super::*;
use gpui::div;
#[test]
fn observation_preserves_identity_and_native_type_in_normal_builds() {
let element = div().id("target").test_support().test_support();
assert_eq!(Element::id(&element), Some("target".into()));
#[cfg(not(feature = "test-support"))]
let _: gpui::Stateful<gpui::Div> = element;
}
#[test]
fn text_observation_keeps_builder_methods() {
use gpui::{ParentElement as _, Styled as _};
let element = div()
.test_text("example")
.id("target")
.text_sm()
.child("example");
assert_eq!(Element::id(&element), Some("target".into()));
}
}