1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
//! Registers native elements without accepting a second description of their state.
use gpui::{Element, Hitbox, InteractiveElement};
#[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;
/// Registers an existing element for UI queries. Its native accessibility
/// properties are read automatically; there are no test-only property setters.
///
/// Test-only values cannot override the native control:
/// ```compile_fail
/// use gpui::{div, prelude::*};
/// use gpui_base::TestSupportExt;
/// div().id("input").test_support().test_props(|props| props.value("invented"));
/// ```
pub trait TestSupportExt:
Element<PrepaintState = Option<Hitbox>> + InteractiveElement + Sized
{
/// With `test-support`, observes the element without adding a layout node.
/// Otherwise returns the original element with its exact native type.
/// Call before `track_focus` so the actual focus binding can be observed.
/// Querying focus on a focus-capable element with a missed binding panics.
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 {}
#[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;
}
}