BlueGit

ict rusty-teams

Files

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

tests.rs

tests.rs 11.3 KiB Download Raw
1use super::*;
2use crate::{
3 Root,
4 group_box::GroupBoxVariant,
5 setting::{SettingGroup, SettingItem},
6};
7use gpui::{
8 Context, InteractiveElement as _, Render, TestAppContext, VisualTestContext, point, size,
9};
10
11struct SettingsHost {
12 pages: Vec<SettingPage>,
13 state: Option<Entity<SettingsState>>,
14}
15
16impl Render for SettingsHost {
17 fn render(&mut self, window: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
18 let settings = Settings::new("search-test")
19 .pages(self.pages.clone())
20 .default_selected_index(SelectIndex {
21 page_ix: 1,
22 group_ix: None,
23 })
24 .render(window, cx)
25 .into_any_element();
26 self.state = Some(window.use_keyed_state("search-test", cx, |_, _| unreachable!()));
27 div().size_full().child(settings)
28 }
29}
30
31fn item(keyword: &'static str) -> SettingItem {
32 item_with_height(keyword, 80.)
33}
34
35fn item_with_height(keyword: &'static str, height: f32) -> SettingItem {
36 SettingItem::render(move |options, _, _| {
37 let selector = format!(
38 "setting-{}-{}-{}",
39 options.page_ix(),
40 options.group_ix(),
41 options.item_ix()
42 );
43 div()
44 .h(px(height))
45 .child("Setting")
46 .debug_selector(move || selector.clone())
47 })
48 .keywords([keyword])
49}
50
51fn setup(cx: &mut TestAppContext) -> (Entity<SettingsHost>, &mut VisualTestContext) {
52 cx.update(|cx| {
53 crate::init(cx);
54 crate::Theme::global_mut(cx).font_size = px(16.);
55 });
56 let pages = vec![
57 SettingPage::new("General").group(SettingGroup::new().item(item("language"))),
58 SettingPage::new("Appearance").default_open(true).groups([
59 SettingGroup::new().item(item("unrelated")),
60 SettingGroup::new()
61 .title("Colors")
62 .item(item("theme colors")),
63 SettingGroup::new()
64 .title("Fonts")
65 .items([item("unrelated"), item("theme font")]),
66 ]),
67 SettingPage::new("Editor").group(SettingGroup::new().item(item("theme editor"))),
68 ];
69 let mut host = None;
70 let (_, cx) = cx.add_window_view(|window, cx| {
71 let view = cx.new(|_| SettingsHost { pages, state: None });
72 host = Some(view.clone());
73 Root::new(view, window, cx)
74 });
75 cx.simulate_resize(size(px(1000.), px(700.)));
76 draw(cx);
77 (host.unwrap(), cx)
78}
79
80fn draw(cx: &mut VisualTestContext) {
81 cx.run_until_parked();
82 cx.update(|window, cx| window.draw(cx).clear(cx));
83}
84
85fn search(host: &Entity<SettingsHost>, query: &str, cx: &mut VisualTestContext) {
86 cx.update(|window, cx| {
87 let input = host
88 .read(cx)
89 .state
90 .as_ref()
91 .unwrap()
92 .read(cx)
93 .search_input
94 .clone();
95 input.update(cx, |input, cx| input.set_value(query, window, cx));
96 });
97 draw(cx);
98}
99
100fn selection(host: &Entity<SettingsHost>, cx: &mut VisualTestContext) -> (usize, Option<usize>) {
101 cx.update(|_, cx| {
102 let selected = host
103 .read(cx)
104 .state
105 .as_ref()
106 .unwrap()
107 .read(cx)
108 .selected_index;
109 (selected.page_ix, selected.group_ix)
110 })
111}
112
113fn click_nav(y: f32, cx: &mut VisualTestContext) {
114 // Fixed-size fixture with a 16px rem; click the label area, away from carets.
115 cx.simulate_click(point(px(50.), px(y)), Default::default());
116 draw(cx);
117}
118
119#[gpui::test]
120fn search_preserves_the_page_and_clicks_use_original_indices(cx: &mut TestAppContext) {
121 let (host, cx) = setup(cx);
122 // The old numeric index is still in range, but would point to Editor.
123 search(&host, "theme", cx);
124 assert_eq!(selection(&host, cx), (1, None));
125 assert!(cx.debug_bounds("setting-1-1-0").is_some());
126 assert!(cx.debug_bounds("setting-2-0-0").is_none());
127
128 // Only Appearance matches; the old implementation renders a blank page.
129 search(&host, "font", cx);
130 assert_eq!(selection(&host, cx), (1, None));
131 assert!(cx.debug_bounds("setting-1-2-1").is_some());
132
133 // Select Editor from the compressed result list, then clear the query.
134 search(&host, "theme", cx);
135 click_nav(156., cx);
136 assert_eq!(selection(&host, cx), (2, None));
137 search(&host, "", cx);
138 assert_eq!(selection(&host, cx), (2, None));
139 assert!(cx.debug_bounds("setting-2-0-0").is_some());
140
141 // The current page disappears: choose the first matching page.
142 search(&host, "font", cx);
143 assert_eq!(selection(&host, cx), (1, None));
144 assert!(cx.debug_bounds("setting-1-2-1").is_some());
145 search(&host, "no matching setting", cx);
146 assert_eq!(selection(&host, cx), (1, None));
147 assert!(cx.debug_bounds("setting-1-2-1").is_none());
148 search(&host, "", cx);
149 assert_eq!(selection(&host, cx), (1, None));
150}
151
152#[gpui::test]
153fn search_preserves_group_and_item_identity(cx: &mut TestAppContext) {
154 let (host, cx) = setup(cx);
155 // Make the target group require scrolling, with an unnamed group before it.
156 cx.update(|_, cx| {
157 host.update(cx, |host, cx| {
158 host.pages[1].groups[1].items = vec![item_with_height("theme colors", 450.)];
159 cx.notify();
160 });
161 });
162 draw(cx);
163 click_nav(156., cx);
164 assert_eq!(selection(&host, cx), (1, Some(2)));
165 search(&host, "theme", cx);
166 assert_eq!(selection(&host, cx), (1, Some(2)));
167 assert!(cx.debug_bounds("setting-1-2-1").is_some());
168 assert!(cx.debug_bounds("setting-1-2-0").is_none());
169 // Click Fonts after the leading page/group/item have been filtered out.
170 click_nav(120., cx);
171 assert_eq!(selection(&host, cx), (1, Some(2)));
172 let target = cx.debug_bounds("setting-1-2-1").unwrap();
173 assert!(target.top() >= px(0.) && target.bottom() <= px(700.));
174 search(&host, "font", cx);
175 assert_eq!(selection(&host, cx), (1, Some(2)));
176 search(&host, "", cx);
177 assert_eq!(selection(&host, cx), (1, Some(2)));
178 search(&host, "colors", cx);
179 assert_eq!(selection(&host, cx), (1, None));
180 assert!(cx.debug_bounds("setting-1-1-0").is_some());
181}
182
183#[gpui::test]
184fn footer_follows_group_search_visibility(cx: &mut TestAppContext) {
185 let (host, cx) = setup(cx);
186 cx.update(|_, cx| {
187 host.update(cx, |host, cx| {
188 host.pages[1].groups[2] = host.pages[1].groups[2].clone().footer(|_, _| {
189 div()
190 .child("Changes apply to this device only.")
191 .debug_selector(|| "font-footer".into())
192 });
193 cx.notify();
194 });
195 });
196 search(&host, "font", cx);
197 assert!(cx.debug_bounds("setting-1-2-1").is_some());
198 assert!(cx.debug_bounds("font-footer").is_some());
199
200 // Footer copy does not independently make a group match the query.
201 search(&host, "colors", cx);
202 assert!(cx.debug_bounds("setting-1-1-0").is_some());
203 assert!(cx.debug_bounds("font-footer").is_none());
204
205 search(&host, "font", cx);
206 assert!(cx.debug_bounds("font-footer").is_some());
207 assert_eq!(selection(&host, cx), (1, None));
208}
209
210#[gpui::test]
211fn resetting_search_results_leaves_hidden_settings_unchanged(cx: &mut TestAppContext) {
212 use std::{cell::Cell, rc::Rc};
213
214 let (_, cx) = setup(cx);
215 let visible = Rc::new(Cell::new(false));
216 let hidden = Rc::new(Cell::new(true));
217 let resettable_item = |keyword, dirty: &Rc<Cell<bool>>| {
218 let read = dirty.clone();
219 let reset = dirty.clone();
220 item(keyword).on_reset(move |_| read.get(), move |_, _| reset.set(false))
221 };
222 let group = SettingGroup::new().items([
223 resettable_item("theme", &visible),
224 resettable_item("hidden", &hidden),
225 ]);
226 cx.update(|window, cx| {
227 assert!(!group.is_resettable("theme", cx));
228 visible.set(true);
229 assert!(group.is_resettable("theme", cx));
230 group.reset("theme", window, cx);
231 });
232 assert!(!visible.get());
233 assert!(hidden.get());
234}
235
236struct VariantHost {
237 pages: Vec<SettingPage>,
238 outline: bool,
239}
240
241impl Render for VariantHost {
242 fn render(&mut self, window: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
243 let mut settings = Settings::new("variant-test").pages(self.pages.clone());
244 if self.outline {
245 settings = settings.with_group_variant(GroupBoxVariant::Outline);
246 }
247 div()
248 .size_full()
249 .child(settings.render(window, cx).into_any_element())
250 }
251}
252
253fn variant_pages(override_first_group: bool) -> Vec<SettingPage> {
254 let first = if override_first_group {
255 SettingGroup::new().variant(GroupBoxVariant::Normal)
256 } else {
257 SettingGroup::new()
258 };
259 vec![SettingPage::new("General").groups([
260 first.item(item("plain")),
261 SettingGroup::new().item(item("outlined")),
262 ])]
263}
264
265#[gpui::test]
266fn group_variant_overrides_the_settings_default(cx: &mut TestAppContext) {
267 cx.update(|cx| {
268 crate::init(cx);
269 crate::Theme::global_mut(cx).font_size = px(16.);
270 });
271
272 let open_window = |pages, outline, cx: &mut TestAppContext| {
273 let (_, cx) = cx.add_window_view(|window, cx| {
274 let view = cx.new(|_| VariantHost { pages, outline });
275 Root::new(view, window, cx)
276 });
277 cx.simulate_resize(size(px(1000.), px(700.)));
278 draw(cx);
279 (
280 cx.debug_bounds("setting-0-0-0").unwrap(),
281 cx.debug_bounds("setting-0-1-0").unwrap(),
282 )
283 };
284
285 // Baseline: no global variant, so both groups present their items directly.
286 let (baseline_first, baseline_second) = open_window(variant_pages(false), false, cx);
287 // A global Outline default with the first group overriding back to Normal.
288 let (overridden_first, inherited_second) = open_window(variant_pages(true), true, cx);
289
290 // The override presents the group exactly like the settings-level default.
291 assert_eq!(
292 f64::from(overridden_first.top() - baseline_first.top()),
293 0.,
294 "the overridden group must lose the global Outline chrome"
295 );
296 // A group without the override keeps the global Outline chrome:
297 // a 1px border and 16px padding above its items.
298 assert_eq!(
299 f64::from(inherited_second.top() - baseline_second.top()),
300 17.,
301 "groups without an override must keep the settings-level variant"
302 );
303}
304
305#[gpui::test]
306fn selecting_a_group_from_another_page_scrolls_to_it(cx: &mut TestAppContext) {
307 let (host, cx) = setup(cx);
308 // Push Fonts below the viewport.
309 cx.update(|_, cx| {
310 host.update(cx, |host, cx| {
311 host.pages[1].groups[1].items = vec![item_with_height("theme colors", 900.)];
312 cx.notify();
313 });
314 });
315 draw(cx);
316 let select = |page_ix, group_ix: Option<usize>, cx: &mut VisualTestContext| {
317 cx.update(|_, cx| {
318 let state = host.read(cx).state.clone().unwrap();
319 state.update(cx, |state, cx| {
320 state.selected_index = SelectIndex { page_ix, group_ix };
321 state.deferred_scroll_group_ix = group_ix;
322 cx.notify();
323 });
324 });
325 draw(cx);
326 };
327
328 // Leaving the page drops its list state; the jump back must not land at the top.
329 select(0, None, cx);
330 select(1, Some(2), cx);
331 let target = cx.debug_bounds("setting-1-2-1").unwrap();
332 assert!(target.top() >= px(0.) && target.bottom() <= px(700.));
333}