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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
// Modified for GPUI Deck: update references to the extracted crate.
//! Registers native elements without accepting a second description of their state.
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;
/// 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_deck::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 {}
#[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;
/// Records the text an element displays, for text queries in UI tests.
/// Text components call it with the text they render, so plain text needs
/// no element id.
pub trait TestTextExt: IntoElement + Sized {
/// With `test-support`, records `text` each time the element paints.
/// Otherwise returns the element it builds.
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()
}
}
/// Like [`Self::test_text`] for an image's alternative text, which
/// stands in for what the element draws.
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()));
}
}