strata/runtime/
test_support.rs1use super::*;
13
14#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
16pub enum MatchMode {
17 #[default]
19 Exact,
20 Contains,
22}
23
24#[derive(Debug, Clone, Default)]
30pub struct NodeQuery {
31 text: Option<String>,
32 title: Option<String>,
33 mode: MatchMode,
34}
35
36impl NodeQuery {
37 pub fn text(text: impl Into<String>) -> Self {
39 Self {
40 text: Some(text.into()),
41 ..Self::default()
42 }
43 }
44
45 pub fn title(title: impl Into<String>) -> Self {
52 Self {
53 title: Some(title.into()),
54 ..Self::default()
55 }
56 }
57
58 pub fn with_text(mut self, text: impl Into<String>) -> Self {
60 self.text = Some(text.into());
61 self
62 }
63
64 pub fn with_title(mut self, title: impl Into<String>) -> Self {
66 self.title = Some(title.into());
67 self
68 }
69
70 pub fn contains(mut self) -> Self {
72 self.mode = MatchMode::Contains;
73 self
74 }
75
76 fn matches_str(&self, needle: &str, haystack: &str) -> bool {
77 let needle = normalize_ws(needle);
78 let haystack = normalize_ws(haystack);
79 match self.mode {
80 MatchMode::Exact => haystack == needle,
81 MatchMode::Contains => haystack.contains(&needle),
82 }
83 }
84}
85
86fn normalize_ws(s: &str) -> String {
87 s.split_whitespace().collect::<Vec<_>>().join(" ")
88}
89
90fn collect_text(bp: &NodeBlueprint, out: &mut String) {
91 match bp {
92 NodeBlueprint::Text(t) => {
93 if !out.is_empty() && !out.ends_with(' ') {
94 out.push(' ');
95 }
96 out.push_str(t);
97 }
98 NodeBlueprint::DynamicText(f) => {
99 if !out.is_empty() && !out.ends_with(' ') {
100 out.push(' ');
101 }
102 out.push_str(&f());
103 }
104 NodeBlueprint::Element { children, .. } | NodeBlueprint::Fragment(children) => {
105 for c in children {
106 collect_text(c, out);
107 }
108 }
109 NodeBlueprint::Empty => {}
110 }
111}
112
113fn own_tooltip_attrs(bp: &NodeBlueprint) -> Vec<&str> {
114 let mut found = Vec::new();
115 if let NodeBlueprint::Element {
116 tag, attributes, ..
117 } = bp
118 {
119 let supports_alt = match tag {
120 strata_dom::Tag::Img | strata_dom::Tag::Button => true,
121 strata_dom::Tag::Custom(name) => {
122 name.eq_ignore_ascii_case("img") || name.eq_ignore_ascii_case("button")
123 }
124 _ => false,
125 };
126 for attr in attributes {
127 if let strata_dom::Attribute::Property { key, value } = attr {
128 if key.eq_ignore_ascii_case("title")
129 || (supports_alt && key.eq_ignore_ascii_case("alt"))
130 {
131 found.push(value.as_str());
132 }
133 }
134 }
135 }
136 found
137}
138
139impl AppRuntime {
140 fn query_matches_node(query: &NodeQuery, bp: &NodeBlueprint) -> bool {
141 if !matches!(bp, NodeBlueprint::Element { .. }) {
142 return false;
143 }
144 if query.text.is_none() && query.title.is_none() {
145 return false;
146 }
147 if let Some(ref needle) = query.text {
148 let mut content = String::new();
149 collect_text(bp, &mut content);
150 if !query.matches_str(needle, &content) {
151 return false;
152 }
153 }
154 if let Some(ref needle) = query.title {
155 if !own_tooltip_attrs(bp)
156 .into_iter()
157 .any(|v| query.matches_str(needle, v))
158 {
159 return false;
160 }
161 }
162 true
163 }
164
165 fn collect_query_matches(
169 query: &NodeQuery,
170 bp: &NodeBlueprint,
171 layout: &LayoutNode,
172 path: &mut Vec<usize>,
173 out: &mut Vec<Vec<usize>>,
174 ) -> bool {
175 if path.len() >= 256 {
176 return false;
177 }
178 let mut any_child = false;
179 for (idx, child_bp, child_layout) in Self::paired_blueprint_children(bp, layout) {
180 if matches!(
181 child_layout.style.display,
182 strata_style::types::Display::None
183 ) {
184 continue;
185 }
186 path.push(idx);
187 if Self::collect_query_matches(query, child_bp, child_layout, path, out) {
188 any_child = true;
189 }
190 path.pop();
191 }
192 if !any_child && Self::query_matches_node(query, bp) {
193 out.push(path.clone());
194 return true;
195 }
196 any_child
197 }
198
199 pub fn find_all(&self, query: &NodeQuery) -> Vec<Vec<usize>> {
201 let mut out = Vec::new();
202 let mut path = Vec::new();
203 Self::collect_query_matches(
204 query,
205 &self.root_blueprint,
206 &self.layout_root,
207 &mut path,
208 &mut out,
209 );
210 out
211 }
212
213 pub fn find(&self, query: &NodeQuery) -> Option<Vec<usize>> {
215 self.find_all(query).into_iter().next()
216 }
217
218 pub fn find_last(&self, query: &NodeQuery) -> Option<Vec<usize>> {
221 self.find_all(query).into_iter().next_back()
222 }
223
224 pub fn find_by_text(&self, text: &str) -> Option<Vec<usize>> {
226 self.find(&NodeQuery::text(text))
227 }
228
229 pub fn find_last_by_text(&self, text: &str) -> Option<Vec<usize>> {
231 self.find_last(&NodeQuery::text(text))
232 }
233
234 pub fn find_by_title(&self, title: &str) -> Option<Vec<usize>> {
236 self.find(&NodeQuery::title(title))
237 }
238
239 pub fn find_last_by_title(&self, title: &str) -> Option<Vec<usize>> {
241 self.find_last(&NodeQuery::title(title))
242 }
243
244 pub fn node_text(&self, path: &[usize]) -> Option<String> {
246 let (bp, _) = Self::find_node_by_path(&self.root_blueprint, &self.layout_root, path)?;
247 let mut content = String::new();
248 collect_text(bp, &mut content);
249 Some(normalize_ws(&content))
250 }
251
252 pub fn node_center(&self, path: &[usize]) -> Option<(f32, f32)> {
254 let (_, layout) = Self::find_node_by_path(&self.root_blueprint, &self.layout_root, path)?;
255 let b = layout.geometry.border_box;
256 Some((b.x + b.width * 0.5, b.y + b.height * 0.5))
257 }
258
259 pub fn click_node(&self, path: &[usize]) -> bool {
263 match self.node_center(path) {
264 Some((x, y)) => self.dispatch_click(x, y),
265 None => false,
266 }
267 }
268
269 pub fn click(&self, query: &NodeQuery) -> bool {
271 self.find(query).is_some_and(|p| self.click_node(&p))
272 }
273
274 pub fn click_last(&self, query: &NodeQuery) -> bool {
276 self.find_last(query).is_some_and(|p| self.click_node(&p))
277 }
278
279 pub fn click_text(&self, text: &str) -> bool {
281 self.click(&NodeQuery::text(text))
282 }
283
284 pub fn click_last_text(&self, text: &str) -> bool {
286 self.click_last(&NodeQuery::text(text))
287 }
288
289 pub fn click_title(&self, title: &str) -> bool {
292 self.click(&NodeQuery::title(title))
293 }
294
295 pub fn click_last_title(&self, title: &str) -> bool {
298 self.click_last(&NodeQuery::title(title))
299 }
300}