BlueGit

ict rusty-teams

Files

rusty-teams / vendor / gpui-component / src / form / tests.rs

tests.rs

tests.rs 9.1 KiB Download Raw
1use super::*;
2use gpui::{Context, InteractiveElement as _, Render, TestAppContext, div};
3
4#[test]
5fn test_form_builder() {
6 for form in [Form::new(), Form::default(), Form::vertical()] {
7 assert_eq!(form.props.layout, Axis::Vertical);
8 assert_eq!(form.props.columns, 1);
9 assert!(form.footer.is_none());
10 }
11 let form = Form::new()
12 .columns(2)
13 .label_layout(Axis::Horizontal)
14 .child(Field::new())
15 .footer(crate::button::Button::new("save").label("Save"));
16 assert_eq!(form.props.layout, Axis::Horizontal);
17 assert_eq!(form.props.columns, 2);
18 assert_eq!(form.fields.len(), 1);
19 assert!(form.footer.is_some());
20}
21
22struct FormHarness {
23 width: f32,
24 layout: Axis,
25 legacy: bool,
26 footer: Option<bool>,
27}
28
29impl Render for FormHarness {
30 fn render(&mut self, _: &mut Window, _: &mut Context<Self>) -> impl IntoElement {
31 let form = if self.legacy {
32 Form::vertical().layout(self.layout)
33 } else {
34 Form::new().label_layout(self.layout)
35 };
36 div()
37 .debug_selector(|| "form-container".into())
38 .w(px(self.width))
39 .child(
40 form.columns(2)
41 .label_width(px(80.))
42 .children((0..3).map(|ix| {
43 Field::new()
44 .label_fn(move |_, _| {
45 div()
46 .debug_selector(move || format!("label-{ix}"))
47 .w(px(40.))
48 .h(px(20.))
49 })
50 .child(
51 div()
52 .debug_selector(move || format!("control-{ix}"))
53 .w_full()
54 .h(px(24.)),
55 )
56 }))
57 .when_some(self.footer, |this, full_width| {
58 this.footer(
59 div()
60 .debug_selector(|| "footer-content".into())
61 .h(px(30.))
62 .when(full_width, |this| this.w_full())
63 .when(!full_width, |this| this.w(px(80.))),
64 )
65 }),
66 )
67 }
68}
69
70#[gpui::test]
71fn label_layout_is_independent_of_field_columns(cx: &mut TestAppContext) {
72 cx.update(crate::init);
73 for width in [360., 800.] {
74 for layout in [Axis::Horizontal, Axis::Vertical] {
75 let (_, cx) = cx.add_window_view(move |_, _| FormHarness {
76 width,
77 layout,
78 legacy: false,
79 footer: None,
80 });
81 cx.update(|window, cx| window.draw(cx).clear(cx));
82 let first = cx.debug_bounds("control-0").unwrap();
83 let second = cx.debug_bounds("control-1").unwrap();
84 let third = cx.debug_bounds("control-2").unwrap();
85 let label = cx.debug_bounds("label-0").unwrap();
86 assert_eq!(first.top(), second.top());
87 assert!(second.left() > first.right());
88 assert_eq!(first.left(), third.left());
89 assert!(third.top() > first.bottom());
90 match layout {
91 Axis::Horizontal => assert!(label.right() < first.left()),
92 Axis::Vertical => {
93 assert_eq!(label.left(), first.left());
94 assert!(label.bottom() < first.top());
95 }
96 }
97 }
98 }
99}
100
101#[gpui::test]
102fn footer_spans_columns_after_fields_and_aligns_actions_to_trailing_edge(cx: &mut TestAppContext) {
103 cx.update(crate::init);
104 for width in [360., 800.] {
105 for layout in [Axis::Horizontal, Axis::Vertical] {
106 for full_width in [true, false] {
107 let (_, cx) = cx.add_window_view(move |_, _| FormHarness {
108 width,
109 layout,
110 legacy: false,
111 footer: Some(full_width),
112 });
113 cx.update(|window, cx| window.draw(cx).clear(cx));
114 let form = cx.debug_bounds("form-container").unwrap();
115 let footer = cx.debug_bounds("footer-content").unwrap();
116 let last = cx.debug_bounds("control-2").unwrap();
117 assert!(footer.top() > last.bottom());
118 assert_eq!(footer.right(), form.right());
119 assert!(footer.bottom() <= form.bottom());
120 if full_width {
121 assert_eq!(footer.left(), form.left());
122 assert_eq!(footer.size.width, px(width));
123 } else {
124 assert_eq!(footer.size.width, px(80.));
125 }
126 }
127 }
128 }
129}
130
131#[gpui::test]
132fn default_without_footer_keeps_legacy_geometry(cx: &mut TestAppContext) {
133 cx.update(crate::init);
134 for layout in [Axis::Horizontal, Axis::Vertical] {
135 let mut snapshots = Vec::new();
136 for legacy in [true, false] {
137 let (_, cx) = cx.add_window_view(move |_, _| FormHarness {
138 width: 360.,
139 layout,
140 legacy,
141 footer: None,
142 });
143 cx.update(|window, cx| window.draw(cx).clear(cx));
144 snapshots.push([
145 cx.debug_bounds("form-container").unwrap(),
146 cx.debug_bounds("label-0").unwrap(),
147 cx.debug_bounds("control-0").unwrap(),
148 cx.debug_bounds("control-1").unwrap(),
149 cx.debug_bounds("control-2").unwrap(),
150 ]);
151 assert!(cx.debug_bounds("footer-content").is_none());
152 }
153 assert_eq!(snapshots[0], snapshots[1]);
154 }
155}
156
157#[gpui::test]
158fn form_applies_styled_refinements(cx: &mut TestAppContext) {
159 cx.update(crate::init);
160 struct FormCompareHarness {
161 styled: bool,
162 }
163 impl Render for FormCompareHarness {
164 fn render(&mut self, _: &mut Window, _: &mut Context<Self>) -> impl IntoElement {
165 let form = if self.styled {
166 Form::vertical().p(px(20.)).gap_y(px(30.))
167 } else {
168 Form::vertical()
169 };
170 div().w(px(400.)).child(
171 form.child(
172 Field::new().child(
173 div()
174 .debug_selector(|| "control-0".into())
175 .w_full()
176 .h(px(20.)),
177 ),
178 )
179 .child(
180 Field::new().child(
181 div()
182 .debug_selector(|| "control-1".into())
183 .w_full()
184 .h(px(20.)),
185 ),
186 ),
187 )
188 }
189 }
190
191 // Default form without custom styling
192 let (_, cx) = cx.add_window_view(|_, _| FormCompareHarness { styled: false });
193 cx.update(|window, cx| window.draw(cx).clear(cx));
194 let default_first = cx.debug_bounds("control-0").unwrap();
195 let default_second = cx.debug_bounds("control-1").unwrap();
196
197 // Styled form with padding 20px and gap_y 30px
198 let (_, cx) = cx.add_window_view(|_, _| FormCompareHarness { styled: true });
199 cx.update(|window, cx| window.draw(cx).clear(cx));
200 let styled_first = cx.debug_bounds("control-0").unwrap();
201 let styled_second = cx.debug_bounds("control-1").unwrap();
202
203 // Padding applied: top and left shifted by 20px
204 assert_eq!(styled_first.left() - default_first.left(), px(20.));
205 assert_eq!(styled_first.top() - default_first.top(), px(20.));
206
207 // Custom gap applied: spacing between controls increased by (30px - 8px default) = 22px
208 let default_gap = default_second.top() - default_first.bottom();
209 let styled_gap = styled_second.top() - styled_first.bottom();
210 assert_eq!(styled_gap - default_gap, px(22.));
211}
212
213#[gpui::test]
214fn hidden_fields_are_not_rendered(cx: &mut TestAppContext) {
215 cx.update(crate::init);
216 struct HiddenFieldHarness {
217 hide: bool,
218 }
219 impl Render for HiddenFieldHarness {
220 fn render(&mut self, _: &mut Window, _: &mut Context<Self>) -> impl IntoElement {
221 let hide = self.hide;
222 div()
223 .w(px(400.))
224 .child(Form::new().children((0..3).map(|ix| {
225 Field::new().visible(!(hide && ix == 1)).child(
226 div()
227 .debug_selector(move || format!("control-{ix}"))
228 .w_full()
229 .h(px(20.)),
230 )
231 })))
232 }
233 }
234
235 let (_, cx) = cx.add_window_view(|_, _| HiddenFieldHarness { hide: false });
236 cx.update(|window, cx| window.draw(cx).clear(cx));
237 let second_row_top = cx.debug_bounds("control-1").unwrap().top();
238
239 let (_, cx) = cx.add_window_view(|_, _| HiddenFieldHarness { hide: true });
240 cx.update(|window, cx| window.draw(cx).clear(cx));
241 assert!(cx.debug_bounds("control-1").is_none());
242 // The hidden field leaves no grid row behind.
243 assert_eq!(cx.debug_bounds("control-2").unwrap().top(), second_row_top);
244}