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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
use std::sync::Arc;
use std::time::{Duration, Instant};
use crate::element::{Element, ElementData, TreeNode};
use crate::error::{Error, Result};
use crate::event_provider::Subscription;
use crate::locator::Locator;
use crate::provider::Provider;
/// Polling interval shared by all timeout-bearing lookups.
const LOOKUP_POLL_INTERVAL: Duration = Duration::from_millis(100);
/// Run `attempt` repeatedly until it succeeds or `timeout` elapses, treating
/// `SelectorNotMatched` as a "not yet" signal. All other errors short-circuit.
///
/// `Duration::ZERO` performs exactly one attempt — identical to a non-polling
/// call. On timeout, returns the last `SelectorNotMatched` error.
fn poll_lookup<F>(timeout: Duration, mut attempt: F) -> Result<App>
where
F: FnMut() -> Result<App>,
{
let start = Instant::now();
loop {
match attempt() {
Ok(app) => return Ok(app),
Err(e @ Error::SelectorNotMatched { .. }) => {
if start.elapsed() >= timeout {
return Err(e);
}
}
Err(e) => return Err(e),
}
std::thread::sleep(LOOKUP_POLL_INTERVAL);
}
}
/// A running application, the entry point for accessibility queries.
///
/// `App` is **not** an [`Element`] — it represents the application as a whole
/// and provides a [`locator`](App::locator) to search its accessibility tree.
pub struct App {
/// Application name.
pub name: String,
/// Process ID.
pub pid: Option<u32>,
/// The underlying element data for this application.
pub data: ElementData,
provider: Arc<dyn Provider>,
}
impl App {
/// Find an application matching `predicate`, using an explicit provider.
///
/// Prefer `App::find` from the `xa11y` crate which uses the global
/// singleton provider. `predicate` runs against each running app's
/// [`ElementData`] on every poll; the first match in enumeration order
/// wins. Timeout / polling semantics match
/// [`by_name_with`](Self::by_name_with): `Duration::ZERO` performs a
/// single attempt, only [`Error::SelectorNotMatched`] triggers a retry,
/// and a failing `list_apps()` short-circuits.
///
/// For a predicate that can itself fail, see
/// [`try_find_with`](Self::try_find_with).
pub fn find_with<F>(
provider: Arc<dyn Provider>,
timeout: Duration,
predicate: F,
) -> Result<Self>
where
F: Fn(&ElementData) -> bool,
{
Self::try_find_with(provider, timeout, move |d| Ok(predicate(d)))
}
/// Like [`find_with`](Self::find_with), but with a fallible predicate.
///
/// The predicate's result drives the same retry contract the lookup uses
/// for the apps it enumerates: `Ok(false)` means "not this one, keep
/// polling", while `Err(_)` aborts the search immediately and propagates
/// — it is *not* treated as "no match". Language bindings use this so a
/// predicate exception fails fast instead of being silently swallowed and
/// surfacing later as a spurious timeout.
pub fn try_find_with<F>(
provider: Arc<dyn Provider>,
timeout: Duration,
predicate: F,
) -> Result<Self>
where
F: Fn(&ElementData) -> Result<bool>,
{
Self::find_matching(provider, timeout, predicate, || {
"application matching predicate".to_string()
})
}
/// Shared predicate-based discovery loop. `describe` supplies the
/// [`Error::SelectorNotMatched`] selector string so name/pid lookups keep
/// their specific, actionable error messages while sharing one match loop.
fn find_matching<F, D>(
provider: Arc<dyn Provider>,
timeout: Duration,
predicate: F,
describe: D,
) -> Result<Self>
where
F: Fn(&ElementData) -> Result<bool>,
D: Fn() -> String,
{
poll_lookup(timeout, || {
// Discovery is platform-specific (CGWindowList on macOS, AT-SPI
// registry on Linux, UIA desktop root on Windows). `list_apps()`
// is the canonical enumeration primitive and we filter in Rust,
// so app names containing `"`, `]`, or other characters
// significant in the selector grammar don't need escaping.
//
// Errors from `list_apps()` propagate so callers can distinguish
// "app not found" from "accessibility is broken". A predicate
// error propagates for the same reason — `poll_lookup` only
// retries `SelectorNotMatched`, so anything else fails fast.
let apps = provider.list_apps()?;
for data in apps {
if predicate(&data)? {
return Ok(Self::from_data(Arc::clone(&provider), data));
}
}
Err(Error::SelectorNotMatched {
selector: describe(),
})
})
}
/// Find an application by exact name, using an explicit provider.
///
/// Prefer `App::by_name` from the `xa11y` crate which uses the global
/// singleton provider. Use this variant when you need to supply a specific
/// provider (e.g. a mock in unit tests).
///
/// Polls the accessibility API until the app appears or `timeout` elapses.
/// `Duration::ZERO` performs exactly one attempt (no waiting). Only
/// [`Error::SelectorNotMatched`] triggers a retry; other errors
/// (permission, parse, platform) short-circuit immediately.
pub fn by_name_with(
provider: Arc<dyn Provider>,
name: &str,
timeout: Duration,
) -> Result<Self> {
Self::find_matching(
provider,
timeout,
|d| Ok(d.name.as_deref() == Some(name)),
|| format!(r#"application[name="{}"]"#, name),
)
}
/// Find an application by process ID, using an explicit provider.
///
/// Prefer `App::by_pid` from the `xa11y` crate which uses the global
/// singleton provider. See [`by_name_with`](Self::by_name_with) for the
/// timeout / polling semantics.
pub fn by_pid_with(provider: Arc<dyn Provider>, pid: u32, timeout: Duration) -> Result<Self> {
Self::find_matching(
provider,
timeout,
|d| Ok(d.pid == Some(pid)),
|| format!("application with pid={}", pid),
)
}
/// List all running applications, using an explicit provider.
///
/// Prefer `App::list` from the `xa11y` crate which uses the global
/// singleton provider.
pub fn list_with(provider: Arc<dyn Provider>) -> Result<Vec<Self>> {
// `list_apps()` is the platform-specific discovery primitive — it
// already handles the per-OS app/window split (Linux/macOS return
// `Application` elements; Windows returns top-level `Window`
// elements), so we just wrap each entry.
let datas = provider.list_apps()?;
Ok(datas
.into_iter()
.map(|d| Self::from_data(Arc::clone(&provider), d))
.collect())
}
fn from_data(provider: Arc<dyn Provider>, data: ElementData) -> Self {
let name = data.name.clone().unwrap_or_default();
let pid = data.pid;
Self {
name,
pid,
data,
provider,
}
}
/// Create a [`Locator`] to search this application's accessibility tree.
pub fn locator(&self, selector: &str) -> Locator {
Locator::new(
Arc::clone(&self.provider),
Some(self.data.clone()),
selector,
)
}
/// Subscribe to accessibility events from this application.
pub fn subscribe(&self) -> Result<Subscription> {
self.provider.subscribe(&self.data)
}
/// Get direct children (typically windows) of this application.
pub fn children(&self) -> Result<Vec<Element>> {
let children = self.provider.get_children(Some(&self.data))?;
Ok(children
.into_iter()
.map(|d| Element::new(d, Arc::clone(&self.provider)))
.collect())
}
/// Capture the application's accessibility tree as a recursive snapshot,
/// rooted at the application element.
///
/// Equivalent to `self.as_element().tree(max_depth)`. See
/// [`Element::tree`] for `max_depth` semantics.
pub fn tree(&self, max_depth: Option<usize>) -> Result<TreeNode> {
self.as_element().tree(max_depth)
}
/// Render the application's accessibility tree as an indented string,
/// rooted at the application element.
///
/// The primary inspection helper for figuring out the role/name of every
/// element in an app before writing selectors. Equivalent to
/// `self.as_element().dump(max_depth)`. See [`Element::dump`] for the
/// output format.
pub fn dump(&self, max_depth: Option<usize>) -> Result<String> {
self.as_element().dump(max_depth)
}
/// Get an [`Element`] handle for the application root.
///
/// Useful when you want to use Element-level methods (e.g. `tree`,
/// `dump`, `children`) without going through a locator.
pub fn as_element(&self) -> Element {
Element::new(self.data.clone(), Arc::clone(&self.provider))
}
/// Get the provider reference.
pub fn provider(&self) -> &Arc<dyn Provider> {
&self.provider
}
}
impl std::fmt::Display for App {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "application \"{}\"", self.name)
}
}
impl std::fmt::Debug for App {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("App")
.field("name", &self.name)
.field("pid", &self.pid)
.finish()
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::mock::build_provider;
use crate::role::Role;
fn mock_app() -> App {
let provider: Arc<dyn Provider> = build_provider();
App::by_name_with(provider, "TestApp", Duration::ZERO)
.expect("TestApp must exist in mock tree")
}
#[test]
fn app_tree_returns_application_root() {
let node = mock_app().tree(None).expect("tree must succeed");
assert_eq!(node.role, "application");
assert_eq!(node.name.as_deref(), Some("TestApp"));
assert!(
!node.children.is_empty(),
"TestApp must have at least one window child"
);
}
#[test]
fn app_tree_max_depth_zero_has_no_children() {
let node = mock_app().tree(Some(0)).expect("tree must succeed");
assert_eq!(node.role, "application");
assert!(node.children.is_empty());
}
#[test]
fn app_tree_max_depth_one_stops_at_direct_children() {
let node = mock_app().tree(Some(1)).expect("tree must succeed");
assert!(!node.children.is_empty());
for child in &node.children {
assert!(
child.children.is_empty(),
"max_depth=1 must stop after direct children"
);
}
}
#[test]
fn app_dump_contains_application_root() {
let s = mock_app().dump(None).expect("dump must succeed");
assert!(
s.contains(r#"application "TestApp""#),
"dump output should include the application root: {s}"
);
}
#[test]
fn app_dump_max_depth_zero_is_one_line() {
let s = mock_app().dump(Some(0)).expect("dump must succeed");
let non_empty: Vec<&str> = s.lines().filter(|l| !l.trim().is_empty()).collect();
assert_eq!(non_empty.len(), 1, "max_depth=0 should be a single line");
assert!(non_empty[0].contains("application"));
}
#[test]
fn app_as_element_is_root() {
let app = mock_app();
let el = app.as_element();
assert_eq!(el.data().role, Role::Application);
assert_eq!(el.data().name.as_deref(), Some("TestApp"));
}
#[test]
fn find_with_matches_by_predicate() {
let provider: Arc<dyn Provider> = build_provider();
let app = App::find_with(provider, Duration::ZERO, |d| {
d.name.as_deref() == Some("TestApp")
})
.expect("predicate must match TestApp in mock tree");
assert_eq!(app.name, "TestApp");
}
#[test]
fn find_with_no_match_returns_selector_not_matched() {
let provider: Arc<dyn Provider> = build_provider();
let err = App::find_with(provider, Duration::ZERO, |_| false)
.expect_err("a never-true predicate must not match any app");
assert!(matches!(err, Error::SelectorNotMatched { .. }));
}
#[test]
fn try_find_with_propagates_predicate_error_and_fails_fast() {
let provider: Arc<dyn Provider> = build_provider();
// A generous timeout: if the predicate error were treated as "no
// match" the call would block for 30s. Returning immediately proves
// the error short-circuits the poll loop.
let start = Instant::now();
let err = App::try_find_with(provider, Duration::from_secs(30), |_| {
Err(Error::Platform {
code: 7,
message: "boom".to_string(),
})
})
.expect_err("a predicate error must propagate, not retry");
assert!(matches!(err, Error::Platform { code: 7, .. }));
assert!(
start.elapsed() < Duration::from_secs(1),
"predicate error must fail fast, not wait out the timeout"
);
}
#[test]
fn try_find_with_ok_false_keeps_polling_then_times_out() {
let provider: Arc<dyn Provider> = build_provider();
// `Ok(false)` is "not yet" — with a zero timeout that's one attempt
// and then a normal not-found result (no error propagation).
let err = App::try_find_with(provider, Duration::ZERO, |_| Ok(false))
.expect_err("an always-Ok(false) predicate must not match");
assert!(matches!(err, Error::SelectorNotMatched { .. }));
}
}