gpui_material_foundation/test/
query.rs1use gpui::{ElementId, Role, Window};
2use gpui_base::test_support as observation;
3
4use super::{ElementSnapshot, ScopedWindow, TextSnapshot};
5
6pub trait TestQueryExt {
8 fn elements(&self) -> Vec<ElementSnapshot>;
11
12 fn texts(&self) -> Vec<TextSnapshot>;
14
15 fn find_by_label(&self, label: &str) -> ElementSnapshot;
18
19 fn try_find_by_label(&self, label: &str) -> Option<ElementSnapshot>;
22
23 fn find_all_by_label(&self, label: &str) -> Vec<ElementSnapshot>;
25
26 fn find_all_by_role(&self, role: Role) -> Vec<ElementSnapshot>;
28}
29
30fn collect_elements(window: &Window, scope: &[ElementId]) -> Vec<ElementSnapshot> {
31 let mut items: Vec<_> = observation::snapshots(window)
32 .into_iter()
33 .filter(|item| item.path().len() > scope.len() && item.path().starts_with(scope))
34 .collect();
35 items.sort_by(|a, b| {
36 (a.bounds().origin.y, a.bounds().origin.x)
37 .cmp(&(b.bounds().origin.y, b.bounds().origin.x))
38 .then_with(|| format!("{:?}", a.path()).cmp(&format!("{:?}", b.path())))
39 });
40 items
41}
42
43fn collect_texts(window: &Window, scope: &[ElementId]) -> Vec<TextSnapshot> {
44 observation::texts(window)
45 .into_iter()
46 .filter(|item| scope.is_empty() || item.path().starts_with(scope))
47 .collect()
48}
49
50fn find_all_by_label_in(window: &Window, scope: &[ElementId], label: &str) -> Vec<ElementSnapshot> {
51 collect_elements(window, scope)
52 .into_iter()
53 .filter(|item| item.label() == Some(label))
54 .collect()
55}
56
57fn try_find_by_label_in(
58 window: &Window,
59 scope: &[ElementId],
60 label: &str,
61) -> Option<ElementSnapshot> {
62 let mut matches = find_all_by_label_in(window, scope, label);
63 assert!(
64 matches.len() <= 1,
65 "ambiguous accessibility label {label:?} in scope {scope:?}; use within(...) to narrow. \
66 Matches: {:?}",
67 matches.iter().map(|entry| entry.path()).collect::<Vec<_>>()
68 );
69 matches.pop()
70}
71
72fn find_by_label_in(window: &Window, scope: &[ElementId], label: &str) -> ElementSnapshot {
73 try_find_by_label_in(window, scope, label).unwrap_or_else(|| {
74 panic!(
75 "missing accessibility label {label:?} in scope {scope:?}. Registered paths: {}",
76 observation::registered_paths(window)
77 )
78 })
79}
80
81fn find_all_by_role_in(window: &Window, scope: &[ElementId], role: Role) -> Vec<ElementSnapshot> {
82 collect_elements(window, scope)
83 .into_iter()
84 .filter(|item| item.role() == Some(role))
85 .collect()
86}
87
88impl TestQueryExt for Window {
89 fn elements(&self) -> Vec<ElementSnapshot> {
90 collect_elements(self, &[])
91 }
92
93 fn texts(&self) -> Vec<TextSnapshot> {
94 collect_texts(self, &[])
95 }
96
97 fn find_by_label(&self, label: &str) -> ElementSnapshot {
98 find_by_label_in(self, &[], label)
99 }
100
101 fn try_find_by_label(&self, label: &str) -> Option<ElementSnapshot> {
102 try_find_by_label_in(self, &[], label)
103 }
104
105 fn find_all_by_label(&self, label: &str) -> Vec<ElementSnapshot> {
106 find_all_by_label_in(self, &[], label)
107 }
108
109 fn find_all_by_role(&self, role: Role) -> Vec<ElementSnapshot> {
110 find_all_by_role_in(self, &[], role)
111 }
112}
113
114impl TestQueryExt for ScopedWindow<'_> {
115 fn elements(&self) -> Vec<ElementSnapshot> {
116 collect_elements(self.window, &self.scope)
117 }
118
119 fn texts(&self) -> Vec<TextSnapshot> {
120 collect_texts(self.window, &self.scope)
121 }
122
123 fn find_by_label(&self, label: &str) -> ElementSnapshot {
124 find_by_label_in(self.window, &self.scope, label)
125 }
126
127 fn try_find_by_label(&self, label: &str) -> Option<ElementSnapshot> {
128 try_find_by_label_in(self.window, &self.scope, label)
129 }
130
131 fn find_all_by_label(&self, label: &str) -> Vec<ElementSnapshot> {
132 find_all_by_label_in(self.window, &self.scope, label)
133 }
134
135 fn find_all_by_role(&self, role: Role) -> Vec<ElementSnapshot> {
136 find_all_by_role_in(self.window, &self.scope, role)
137 }
138}