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
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
use std::sync::Arc;
use std::time::{Duration, Instant};
use crate::element::{Element, ElementData, TreeNode};
use crate::error::{Diagnosis, Error, Result};
use crate::event_provider::Subscription;
use crate::locator::Locator;
use crate::provider::Provider;
use crate::role::Role;
/// Polling interval shared by all timeout-bearing lookups (application and
/// shell-surface alike — see `shell.rs`).
pub(crate) const LOOKUP_POLL_INTERVAL: Duration = Duration::from_millis(100);
/// Maximum number of running applications listed in a lookup-failure
/// diagnosis. Bounded per tenet 6 — diagnostics must not grow with an
/// unbounded environment.
const DIAG_APP_LIST_LIMIT: usize = 20;
/// 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 enriched
/// with `diagnose()`'s context. `diagnose` runs only on that terminal
/// failure (tenet 6: enrich at the terminal site, keep the retry signal
/// cheap).
fn poll_lookup<F, D>(timeout: Duration, mut attempt: F, diagnose: D) -> Result<App>
where
F: FnMut() -> Result<App>,
D: FnOnce() -> Diagnosis,
{
let start = Instant::now();
loop {
match attempt() {
Ok(app) => return Ok(app),
Err(e @ Error::SelectorNotMatched { .. }) => {
if start.elapsed() >= timeout {
return Err(merge_diagnosis(e, diagnose()));
}
}
Err(e) => return Err(e),
}
std::thread::sleep(LOOKUP_POLL_INTERVAL);
}
}
/// Merge terminal-site context into an error that may already carry a cheap
/// diagnosis from its construction site (e.g. the enumeration counts that
/// `Provider::app_by_pid` records). Fields already present win — they
/// describe the actual failing attempt.
fn merge_diagnosis(err: Error, extra: Diagnosis) -> Error {
let mut d = err.diagnosis().cloned().unwrap_or_default();
if d.condition.is_none() {
d.condition = extra.condition;
}
if d.last_observed.is_none() {
d.last_observed = extra.last_observed;
}
if d.candidates.is_empty() {
d.candidates = extra.candidates;
}
if d.scope.is_none() {
d.scope = extra.scope;
}
err.diagnose(d)
}
/// Tag the foreground application within an enumerated app list.
///
/// Resolves the focused app's pid *once* via [`Provider::focused_app`] and
/// sets [`StateSet::focused`](crate::element::StateSet::focused) on the
/// entry that is actually in the foreground, so `App::is_foreground` reflects
/// foreground status for apps obtained through `list`/`find` without a per-app
/// focus query.
///
/// Tagging is process-scoped: all native registrations with the foreground
/// PID represent the same public application. To identify the exact window,
/// inspect the `active` flag on [`App::windows`] instead.
///
/// "Nothing is focused" ([`Error::SelectorNotMatched`]) is not an error here:
/// it leaves every entry untagged (`focused = false`). Any other error is a
/// genuine focus-resolution failure and propagates rather than being silently
/// swallowed (tenet 1) — on every backend the focus query needs no more access
/// than the enumeration that produced `apps`.
fn tag_focused(provider: &Arc<dyn Provider>, apps: &mut [ElementData]) -> Result<()> {
let focused = match provider.focused_app() {
Ok(data) => Some(data),
Err(Error::SelectorNotMatched { .. }) => None,
Err(e) => return Err(e),
};
for app in apps.iter_mut() {
app.states.focused = focused.as_ref().is_some_and(|f| {
same_window_identity(app, f) || (app.pid.is_some() && app.pid == f.pid)
});
}
Ok(())
}
/// Collapse native registrations into the process-level applications exposed
/// by the public API. Entries without a PID cannot be proven equivalent and
/// remain distinct.
fn dedup_processes(apps: Vec<ElementData>) -> Vec<ElementData> {
let mut seen = std::collections::HashSet::new();
apps.into_iter()
.filter(|app| app.pid.is_none_or(|pid| seen.insert(pid)))
.collect()
}
/// Bounded "what *is* running" snapshot for application-lookup failures:
/// the candidate list a consumer would otherwise produce by hand-logging
/// `App::list()` around the failure.
fn running_apps_diagnosis(provider: &Arc<dyn Provider>) -> Diagnosis {
let candidates = match provider.list_apps() {
Ok(apps) => {
let total = apps.len();
let mut out: Vec<String> = apps
.iter()
.take(DIAG_APP_LIST_LIMIT)
.map(|a| {
let pid = a.pid.map(|p| format!(" (pid={p})")).unwrap_or_default();
format!("\"{}\"{pid}", a.name.clone().unwrap_or_default())
})
.collect();
if total > DIAG_APP_LIST_LIMIT {
out.push(format!("… (+{} more)", total - DIAG_APP_LIST_LIMIT));
}
out
}
// Surface the collection failure inside the diagnosis instead of
// dropping it (tenet 1) — the original lookup error still wins.
Err(e) => vec![format!("(application enumeration failed: {e})")],
};
Diagnosis {
condition: Some("application discovery".to_string()),
candidates,
..Diagnosis::default()
}
}
/// Whether two element snapshots identify the same window (or the same
/// Application entry).
///
/// Keyed by the platform's stable identity: `stable_id` — on Linux the D-Bus
/// object path, scoped by the access bus name recorded in `raw["bus_name"]`
/// (two bus connections of one process can expose distinct windows under the
/// same path), on Windows the native HWND for top-level windows. Elements
/// without a stable_id (macOS `AXIdentifier` is often absent) key on the
/// handle, which is unique per built node within one enumeration — distinct
/// windows are never merged on presentation data like a shared title and
/// bounds.
pub(crate) fn same_window_identity(a: &ElementData, b: &ElementData) -> bool {
fn key(d: &ElementData) -> String {
d.stable_id
.clone()
.map(|sid| match d.raw.get("bus_name") {
Some(serde_json::Value::String(bus)) => format!("{bus}:{sid}"),
_ => sid,
})
.unwrap_or_else(|| format!("h{}", d.handle))
}
key(a) == key(b)
}
/// 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>,
{
// Predicate finders tag the foreground app so the predicate can match
// on `focused` (e.g. `find(|a| a.focused)`) and matched apps carry
// correct foreground state.
Self::find_matching(
provider,
timeout,
predicate,
|| "application matching predicate".to_string(),
true,
)
}
/// 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.
///
/// `tag_focus` controls whether each poll resolves the foreground app and
/// tags it onto the enumerated candidates. The predicate finders enable it
/// (so `focused` is visible to the predicate); `by_name` disables it — a
/// name lookup neither needs foreground state nor should pay the per-tick
/// focus query (and shouldn't gain a focus-resolution failure mode).
fn find_matching<F, D>(
provider: Arc<dyn Provider>,
timeout: Duration,
predicate: F,
describe: D,
tag_focus: bool,
) -> Result<Self>
where
F: Fn(&ElementData) -> Result<bool>,
D: Fn() -> String,
{
let diag_provider = Arc::clone(&provider);
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 mut apps = dedup_processes(provider.list_apps()?);
if tag_focus {
tag_focused(&provider, &mut apps)?;
}
for data in apps {
if predicate(&data)? {
return Ok(Self::from_data(Arc::clone(&provider), data));
}
}
Err(Error::selector_not_matched(describe()))
},
|| running_apps_diagnosis(&diag_provider),
)
}
/// 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.
///
/// # Errors
///
/// Returns [`Error::InvalidSelector`] if `name` contains a double quote
/// (`"`). The not-found diagnostic for this lookup is the selector string
/// `application[name="<name>"]`, and the selector grammar has no escape
/// sequence for quotes inside attribute values, so such a name cannot be
/// represented — rather than emitting a malformed selector, the lookup is
/// rejected up front. Use [`find_with`](Self::find_with) with a name
/// predicate to locate apps whose names contain double quotes.
pub fn by_name_with(
provider: Arc<dyn Provider>,
name: &str,
timeout: Duration,
) -> Result<Self> {
// The selector grammar's attribute values (`"..."` / `'...'`) have no
// escape support (see `selector.rs`), so a name containing `"` would
// interpolate into a malformed `application[name="..."]` selector
// string below. Surface that clearly instead of producing it.
if name.contains('"') {
return Err(Error::InvalidSelector {
selector: name.to_string(),
message: "app name contains a double quote, which cannot be escaped in the \
selector grammar; use App::find_with with a name predicate instead"
.to_string(),
});
}
// `tag_focus = false`: a name lookup doesn't need foreground state, so
// it skips the per-tick focus query (apps from `by_name` therefore
// report `focused() == false` — use `list`/`find` to query foreground
// status).
Self::find_matching(
provider,
timeout,
|d| Ok(d.name.as_deref() == Some(name)),
|| format!(r#"application[name="{}"]"#, name),
false,
)
}
/// Find an application by process ID, using an explicit provider.
///
/// Prefer `App::by_pid` from the `xa11y` crate which uses the global
/// singleton provider.
///
/// This is the supported way to **wait for a freshly launched process to
/// surface** in the accessibility tree: the lookup polls
/// [`Provider::app_by_pid`] until the application becomes reachable or
/// `timeout` elapses, covering the window between process spawn and the
/// platform bridge registering the app (slow CI runners, toolkits that
/// initialise accessibility lazily). There is no need to hand-roll a
/// poll over [`list_with`](Self::list_with).
///
/// Timeout / polling semantics match [`by_name_with`](Self::by_name_with):
/// `Duration::ZERO` performs exactly one attempt, only
/// [`Error::SelectorNotMatched`] ("not reachable yet") triggers a retry,
/// and permission / platform errors short-circuit immediately.
///
/// Where the platform supports it (macOS AX, Windows UIA), the provider
/// attaches to the process directly instead of filtering app enumeration,
/// so an app whose window is still unnamed mid-startup is found as soon
/// as the accessibility API can reach it.
pub fn by_pid_with(provider: Arc<dyn Provider>, pid: u32, timeout: Duration) -> Result<Self> {
let diag_provider = Arc::clone(&provider);
poll_lookup(
timeout,
|| {
let data = provider.app_by_pid(pid)?;
Ok(Self::from_data(Arc::clone(&provider), data))
},
|| running_apps_diagnosis(&diag_provider),
)
}
/// Resolve the application that currently holds the system foreground,
/// using an explicit provider.
///
/// Prefer `App::foreground` from the `xa11y` crate which uses the global
/// singleton provider.
///
/// Identifies the foreground application via each platform's canonical
/// mechanism: the system-wide `AXUIElement`'s focused-application attribute
/// (macOS), `GetForegroundWindow` + `ElementFromHandle` (Windows), and the
/// focused element's `Application` ancestor in the AT-SPI registry (Linux).
/// Unlike [`find_with`](Self::find_with) with a `|d| d.states.focused`
/// predicate — which enumerates apps and tags foreground state by pid —
/// this calls the platform foreground query directly. The result is the
/// foreground *process*'s `Application` node — one per process on every
/// platform — not the exact window holding the foreground. On Windows the
/// query resolves the foreground HWND to its process's synthesized
/// `Application` node instead of returning the window itself (the modal
/// case; issues #304/#305). To reach the exact foreground window, list the
/// node's [`windows`](Self::windows) and pick the one reporting
/// [`active`](crate::element::StateSet::active).
///
/// Timeout / polling semantics match [`by_name_with`](Self::by_name_with):
/// `Duration::ZERO` performs exactly one attempt, only
/// [`Error::SelectorNotMatched`] ("nothing currently holds focus" — focus
/// rests on the desktop / shell, or the screen is locked) triggers a retry,
/// and any other error short-circuits immediately. The returned `App`
/// always reports [`is_foreground()`](Self::is_foreground) `== true`.
pub fn foreground_with(provider: Arc<dyn Provider>, timeout: Duration) -> Result<Self> {
let diag_provider = Arc::clone(&provider);
poll_lookup(
timeout,
|| {
let mut data = provider.focused_app()?;
// focused_app resolves the foreground app by definition; tag it
// so `App::is_foreground()` agrees with how list/find populate
// the flag.
data.states.focused = true;
Ok(Self::from_data(Arc::clone(&provider), data))
},
|| running_apps_diagnosis(&diag_provider),
)
}
/// 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>> {
// Collapse platform registrations by pid: a public App consistently
// represents a process on every backend.
let mut datas = dedup_processes(provider.list_apps()?);
// Mark the foreground app (one focus query) so `App::focused` is
// populated across the returned list without an extra call per app.
tag_focused(&provider, &mut datas)?;
Ok(datas
.into_iter()
.map(|d| Self::from_data(Arc::clone(&provider), d))
.collect())
}
/// List the top-level windows of the process owning `data`, using an
/// explicit provider.
///
/// Prefer [`App::windows`], which uses this `App`'s provider.
///
/// `data` must be an `Application` element. Passing any other role is an
/// [`Error::ActionNotSupported`] naming the role; the "windows of a
/// window" question has no meaning once app entries stopped being
/// windows. On Windows the children of the synthetic `Application` node
/// *are* the process's top-level windows, so this returns them in
/// enumeration (z-) order; on macOS and Linux it is the same
/// `get_children` + `Window|Dialog` filter applied to the platform's
/// Application node.
///
/// The listing is **process-complete**: macOS and Windows guarantee one
/// Application node per pid, so the single node's children already cover
/// the process, but the Linux AT-SPI registry can register several
/// `Application` entries for one pid (a main application plus a dialog
/// exposed as its own app node), and each entry's `get_children` answer is
/// only its own windows. `App::by_pid` resolves a single entry, so
/// without aggregation `by_pid(pid).windows()` would silently omit the
/// sibling entries' windows. Every same-pid entry is therefore merged and
/// deduplicated by the platform's stable identity — the same key the
/// `xa11y windows` listing applies per pid, with one subtle difference:
/// results here keep the *windows* of the calling entry first, in
/// enumeration order, followed by the other entries' windows (registry
/// order), so a window is never listed twice whatever the entry count.
/// The merge runs only for providers that can split a process across
/// entries ([`Provider::splits_app_across_entries`]): on macOS and
/// Windows one entry covers the process, so `list_apps` is not
/// re-enumerated there at all.
pub fn windows_with(provider: Arc<dyn Provider>, data: &ElementData) -> Result<Vec<Element>> {
if data.role != Role::Application {
return Err(Error::ActionNotSupported {
action: "windows".into(),
role: data.role,
});
}
let roots = provider.app_roots(data)?;
let mut children = Vec::new();
for root in &roots {
for child in provider.get_children(Some(root))? {
if !children.iter().any(|c| same_window_identity(c, &child)) {
children.push(child);
}
}
}
// Only providers that can expose several Application entries per pid
// need the cross-entry merge. On macOS and Windows one entry covers
// the process, so re-enumerating `list_apps` here would be pure waste
// — and a re-enumeration rebuilds macOS data with fresh handles, so
// windows without an AXIdentifier would no longer deduplicate and
// `windows()` would report each window twice. See
// `Provider::splits_app_across_entries`.
if roots.len() == 1 {
if let Some(pid) = data.pid {
if provider.splits_app_across_entries() {
for entry in provider.list_apps()? {
// Skip the calling entry itself (its children are already in
// the list) — identified by the same stable-identity key, not
// by handle: entries are rebuilt per query on Linux, so the
// handle differs across calls while the stable identity
// (D-Bus object path scoped by bus name) does not.
if entry.pid != Some(pid) || same_window_identity(&entry, data) {
continue;
}
for child in provider.get_children(Some(&entry))? {
if !children.iter().any(|c| same_window_identity(c, &child)) {
children.push(child);
}
}
}
}
}
}
Ok(children
.into_iter()
.filter(|d| matches!(d.role, Role::Window | Role::Dialog))
.map(|d| Element::new(d, Arc::clone(&provider)))
.collect())
}
/// List the top-level windows of this application.
///
/// Each call queries the provider — results are not cached. The windows
/// are the application's top-level windows with role `window` or
/// `dialog`, in enumeration order. On Windows the application entry is a
/// synthesized process node whose children are the process's top-level
/// windows (main window plus modal dialogs); macOS's own Application node
/// has the same one-entry-per-process property. On Linux the answer is
/// process-complete: [`App::windows_with`] merges the filtered children of
/// every same-pid AT-SPI Application entry (an app that registers several
/// entries reports its whole process), so the results need not be the
/// direct children of this node and no single z-order spans them. Calling
/// `windows` on a non-Application element (e.g. a window from a previous
/// release, or the `Window` entries this API used to return on Windows)
/// fails with [`Error::ActionNotSupported`] rather than silently
/// answering a question that no longer has a meaning.
pub fn windows(&self) -> Result<Vec<Element>> {
Self::windows_with(Arc::clone(&self.provider), &self.data)
}
/// Reconstruct an application snapshot in a language binding without
/// repeating discovery or losing the process-scoped core operations.
#[doc(hidden)]
pub 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_for_app(Arc::clone(&self.provider), 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 mut children = Vec::new();
for root in self.provider.app_roots(&self.data)? {
for child in self.provider.get_children(Some(&root))? {
if !children.iter().any(|c| same_window_identity(c, &child)) {
children.push(child);
}
}
}
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.
///
/// Includes children from every native registration of the process. See
/// [`Element::tree`] for `max_depth` semantics.
pub fn tree(&self, max_depth: Option<usize>) -> Result<TreeNode> {
let mut tree = self.as_element().tree(Some(0))?;
if max_depth != Some(0) {
let child_depth = max_depth.map(|depth| depth - 1);
for child in self.children()? {
tree.children.push(child.tree(child_depth)?);
}
}
Ok(tree)
}
/// 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. Includes all process
/// registrations, like [`Self::tree`]. See [`Element::dump`] for the output format.
pub fn dump(&self, max_depth: Option<usize>) -> Result<String> {
let mut out = String::new();
crate::element::write_tree_node(&self.tree(max_depth)?, 0, &mut out);
Ok(out)
}
/// Get an [`Element`] handle for the application root.
///
/// This represents one native root. On a platform with several roots per
/// process, use App-level `tree`, `dump`, `children`, and `locator` for the
/// complete process scope.
pub fn as_element(&self) -> Element {
Element::new(self.data.clone(), Arc::clone(&self.provider))
}
/// Whether this application is the foreground application.
///
/// Named `is_foreground` because "focused" is reserved for element-level
/// keyboard focus ([`StateSet::focused`](crate::element::StateSet::focused))
/// elsewhere in the API; this is the foreground-*application* flag.
///
/// Populated when the `App` is obtained via [`list_with`](Self::list_with)
/// or the predicate finders ([`find_with`](Self::find_with) /
/// [`try_find_with`](Self::try_find_with) — where it is also visible to the
/// predicate via `d.states.focused`, so `find(|a| a.states.focused)` selects
/// the foreground app). The value is a point-in-time snapshot taken when the
/// `App` was resolved. Apps obtained directly via
/// [`by_pid_with`](Self::by_pid_with) carry the platform's raw app-element
/// focus state instead (typically `false`).
///
/// This identifies the foreground process. The window-level `active`
/// state identifies the exact foreground window returned by `windows()`.
/// Use [`foreground_with`](Self::foreground_with) (or `App::foreground`
/// from the `xa11y` crate) to resolve the foreground application directly,
/// then pick the exact foreground window from its [`windows`](Self::windows)
/// by the [`active`](crate::element::StateSet::active) state.
pub fn is_foreground(&self) -> bool {
self.data.states.focused
}
/// Deprecated alias for [`is_foreground`](Self::is_foreground).
#[deprecated(
note = "renamed to `is_foreground`; `focused` refers to element keyboard focus elsewhere in the API"
)]
pub fn focused(&self) -> bool {
self.is_foreground()
}
/// 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")
}
/// What [`FocusModeProvider`] reports from `focused_app`.
enum FocusMode {
/// No application currently holds focus.
None,
/// Focus resolution hit a genuine platform failure.
Error,
}
/// Wraps the standard mock provider but overrides `focused_app` so the
/// foreground-tagging error paths can be exercised. Everything else
/// delegates to the inner mock.
struct FocusModeProvider {
inner: Arc<crate::mock::MockProvider>,
mode: FocusMode,
}
impl FocusModeProvider {
fn new(mode: FocusMode) -> Self {
Self {
inner: build_provider(),
mode,
}
}
}
impl Provider for FocusModeProvider {
fn focused_app(&self) -> Result<ElementData> {
match self.mode {
FocusMode::None => Err(Error::selector_not_matched("focused application")),
FocusMode::Error => Err(Error::Platform {
code: 99,
message: "focus query failed".to_string(),
}),
}
}
fn get_children(&self, e: Option<&ElementData>) -> Result<Vec<ElementData>> {
self.inner.get_children(e)
}
fn get_parent(&self, e: &ElementData) -> Result<Option<ElementData>> {
self.inner.get_parent(e)
}
fn list_apps(&self) -> Result<Vec<ElementData>> {
self.inner.list_apps()
}
fn list_shell_surfaces(
&self,
) -> Result<Vec<(crate::shell::ShellSurfaceKind, ElementData)>> {
self.inner.list_shell_surfaces()
}
fn press(&self, e: &ElementData) -> Result<()> {
self.inner.press(e)
}
fn focus(&self, e: &ElementData) -> Result<()> {
self.inner.focus(e)
}
fn blur(&self, e: &ElementData) -> Result<()> {
self.inner.blur(e)
}
fn toggle(&self, e: &ElementData) -> Result<()> {
self.inner.toggle(e)
}
fn select(&self, e: &ElementData) -> Result<()> {
self.inner.select(e)
}
fn expand(&self, e: &ElementData) -> Result<()> {
self.inner.expand(e)
}
fn collapse(&self, e: &ElementData) -> Result<()> {
self.inner.collapse(e)
}
fn show_menu(&self, e: &ElementData) -> Result<()> {
self.inner.show_menu(e)
}
fn increment(&self, e: &ElementData) -> Result<()> {
self.inner.increment(e)
}
fn decrement(&self, e: &ElementData) -> Result<()> {
self.inner.decrement(e)
}
fn scroll_into_view(&self, e: &ElementData) -> Result<()> {
self.inner.scroll_into_view(e)
}
fn set_value(&self, e: &ElementData, v: &str) -> Result<()> {
self.inner.set_value(e, v)
}
fn set_numeric_value(&self, e: &ElementData, v: f64) -> Result<()> {
self.inner.set_numeric_value(e, v)
}
fn type_text(&self, e: &ElementData, t: &str) -> Result<()> {
self.inner.type_text(e, t)
}
fn set_text_selection(&self, e: &ElementData, s: u32, end: u32) -> Result<()> {
self.inner.set_text_selection(e, s, end)
}
fn perform_action(&self, e: &ElementData, a: &str) -> Result<()> {
self.inner.perform_action(e, a)
}
fn activate(&self, e: &ElementData) -> Result<()> {
self.inner.activate(e)
}
fn minimize(&self, e: &ElementData) -> Result<()> {
self.inner.minimize(e)
}
fn maximize(&self, e: &ElementData) -> Result<()> {
self.inner.maximize(e)
}
fn enter_fullscreen(&self, e: &ElementData) -> Result<()> {
self.inner.enter_fullscreen(e)
}
fn restore(&self, e: &ElementData) -> Result<()> {
self.inner.restore(e)
}
fn close(&self, e: &ElementData) -> Result<()> {
self.inner.close(e)
}
fn move_to(&self, e: &ElementData, x: i32, y: i32) -> Result<()> {
self.inner.move_to(e, x, y)
}
fn resize_to(&self, e: &ElementData, w: u32, h: u32) -> Result<()> {
self.inner.resize_to(e, w, h)
}
fn subscribe(&self, e: &ElementData) -> Result<Subscription> {
self.inner.subscribe(e)
}
}
/// Provider modelling the Windows modal case (issue #304): one process
/// owning two top-level windows. `list_apps` returns ONE Application node
/// (pid 42) for the whole process, and its `get_children` answer is both
/// windows — the shape every platform reports after the app-node
/// unification (Windows synthesizes the Application node and answers
/// `get_children(Some(app))` from a process-wide UIA query; macOS reads
/// `AXWindows`, Linux filters the AT-SPI walk). Only the dialog reports
/// `active`, the window-level foreground flag.
struct MultiWindowProvider {
inner: Arc<crate::mock::MockProvider>,
windows: std::sync::Mutex<Vec<ElementData>>,
}
impl MultiWindowProvider {
fn new() -> Self {
Self {
inner: build_provider(),
windows: std::sync::Mutex::new(vec![
Self::window("Main", 100, false),
Self::window("Modal", 101, true),
]),
}
}
fn replace_windows(&self, windows: Vec<ElementData>) {
*self.windows.lock().unwrap_or_else(|e| e.into_inner()) = windows;
}
/// The Application node for the shared process (pid 42).
fn app(handle: u64) -> ElementData {
ElementData {
role: Role::Application,
name: Some("SharedApp".to_string()),
value: None,
description: None,
bounds: None,
actions: vec![],
states: crate::element::StateSet::default(),
numeric_value: None,
min_value: None,
max_value: None,
stable_id: None,
pid: Some(42),
raw: Default::default(),
handle,
}
}
/// A top-level window owned by the shared process (pid 42). `active`
/// models the platform's window-level foreground flag — only the
/// window actually in the foreground reports it.
fn window(name: &str, handle: u64, active: bool) -> ElementData {
ElementData {
role: Role::Window,
name: Some(name.to_string()),
value: None,
description: None,
bounds: None,
actions: vec![],
states: crate::element::StateSet {
active,
..crate::element::StateSet::default()
},
numeric_value: None,
min_value: None,
max_value: None,
stable_id: None,
pid: Some(42),
raw: Default::default(),
handle,
}
}
}
impl Provider for MultiWindowProvider {
fn list_apps(&self) -> Result<Vec<ElementData>> {
// One Application node per process; both top-level windows of the
// shared process are its children (issue #304). The app node is
// not a window, so enumerating it yields the windows, not the
// process twice.
Ok(vec![Self::app(700)])
}
fn focused_app(&self) -> Result<ElementData> {
Ok(Self::app(700))
}
fn list_shell_surfaces(
&self,
) -> Result<Vec<(crate::shell::ShellSurfaceKind, ElementData)>> {
self.inner.list_shell_surfaces()
}
fn get_children(&self, e: Option<&ElementData>) -> Result<Vec<ElementData>> {
match e {
// The Application node's children are the process's top-level
// windows — main + modal, in enumeration (z-) order.
Some(el) if matches!(el.role, Role::Application) && el.pid == Some(42) => Ok(self
.windows
.lock()
.unwrap_or_else(|e| e.into_inner())
.clone()),
_ => self.inner.get_children(e),
}
}
fn get_parent(&self, e: &ElementData) -> Result<Option<ElementData>> {
if matches!(e.role, Role::Window | Role::Dialog) && e.pid == Some(42) {
return Ok(Some(Self::app(700)));
}
self.inner.get_parent(e)
}
fn press(&self, e: &ElementData) -> Result<()> {
self.inner.press(e)
}
fn focus(&self, e: &ElementData) -> Result<()> {
self.inner.focus(e)
}
fn blur(&self, e: &ElementData) -> Result<()> {
self.inner.blur(e)
}
fn toggle(&self, e: &ElementData) -> Result<()> {
self.inner.toggle(e)
}
fn select(&self, e: &ElementData) -> Result<()> {
self.inner.select(e)
}
fn expand(&self, e: &ElementData) -> Result<()> {
self.inner.expand(e)
}
fn collapse(&self, e: &ElementData) -> Result<()> {
self.inner.collapse(e)
}
fn show_menu(&self, e: &ElementData) -> Result<()> {
self.inner.show_menu(e)
}
fn increment(&self, e: &ElementData) -> Result<()> {
self.inner.increment(e)
}
fn decrement(&self, e: &ElementData) -> Result<()> {
self.inner.decrement(e)
}
fn scroll_into_view(&self, e: &ElementData) -> Result<()> {
self.inner.scroll_into_view(e)
}
fn set_value(&self, e: &ElementData, v: &str) -> Result<()> {
self.inner.set_value(e, v)
}
fn set_numeric_value(&self, e: &ElementData, v: f64) -> Result<()> {
self.inner.set_numeric_value(e, v)
}
fn type_text(&self, e: &ElementData, t: &str) -> Result<()> {
self.inner.type_text(e, t)
}
fn set_text_selection(&self, e: &ElementData, s: u32, end: u32) -> Result<()> {
self.inner.set_text_selection(e, s, end)
}
fn perform_action(&self, e: &ElementData, a: &str) -> Result<()> {
self.inner.perform_action(e, a)
}
fn activate(&self, e: &ElementData) -> Result<()> {
self.inner.activate(e)
}
fn minimize(&self, e: &ElementData) -> Result<()> {
self.inner.minimize(e)
}
fn maximize(&self, e: &ElementData) -> Result<()> {
self.inner.maximize(e)
}
fn enter_fullscreen(&self, e: &ElementData) -> Result<()> {
self.inner.enter_fullscreen(e)
}
fn restore(&self, e: &ElementData) -> Result<()> {
self.inner.restore(e)
}
fn close(&self, e: &ElementData) -> Result<()> {
self.inner.close(e)
}
fn move_to(&self, e: &ElementData, x: i32, y: i32) -> Result<()> {
self.inner.move_to(e, x, y)
}
fn resize_to(&self, e: &ElementData, w: u32, h: u32) -> Result<()> {
self.inner.resize_to(e, w, h)
}
fn subscribe(&self, e: &ElementData) -> Result<Subscription> {
self.inner.subscribe(e)
}
}
/// Provider modelling several Application registrations for one process —
/// the shape the Linux AT-SPI registry can legitimately produce (two
/// accessibles sharing a pid, as `desktop-testing.mdx` documents) and what
/// `tag_focused` must disambiguate with the entry-level `active` flag. Two
/// Application entries share pid 42; only the modal one reports `active`.
struct SharedPidAppProvider {
inner: Arc<crate::mock::MockProvider>,
}
impl SharedPidAppProvider {
fn new() -> Self {
Self {
inner: build_provider(),
}
}
/// An Application entry for the shared process (pid 42). `active`
/// models the window-level foreground flag — only the entry whose
/// window is actually in the foreground reports it.
fn app(name: &str, handle: u64, active: bool) -> ElementData {
ElementData {
role: Role::Application,
name: Some(name.to_string()),
value: None,
description: None,
bounds: None,
actions: vec![],
states: crate::element::StateSet {
active,
..crate::element::StateSet::default()
},
numeric_value: None,
min_value: None,
max_value: None,
stable_id: None,
pid: Some(42),
raw: Default::default(),
handle,
}
}
}
impl Provider for SharedPidAppProvider {
fn splits_app_across_entries(&self) -> bool {
true
}
fn list_apps(&self) -> Result<Vec<ElementData>> {
Ok(vec![
Self::app("Main", 200, false),
Self::app("Modal", 201, true),
])
}
fn focused_app(&self) -> Result<ElementData> {
Ok(Self::app("Modal", 201, true))
}
fn list_shell_surfaces(
&self,
) -> Result<Vec<(crate::shell::ShellSurfaceKind, ElementData)>> {
self.inner.list_shell_surfaces()
}
fn get_children(&self, e: Option<&ElementData>) -> Result<Vec<ElementData>> {
self.inner.get_children(e)
}
fn get_parent(&self, e: &ElementData) -> Result<Option<ElementData>> {
self.inner.get_parent(e)
}
fn press(&self, e: &ElementData) -> Result<()> {
self.inner.press(e)
}
fn focus(&self, e: &ElementData) -> Result<()> {
self.inner.focus(e)
}
fn blur(&self, e: &ElementData) -> Result<()> {
self.inner.blur(e)
}
fn toggle(&self, e: &ElementData) -> Result<()> {
self.inner.toggle(e)
}
fn select(&self, e: &ElementData) -> Result<()> {
self.inner.select(e)
}
fn expand(&self, e: &ElementData) -> Result<()> {
self.inner.expand(e)
}
fn collapse(&self, e: &ElementData) -> Result<()> {
self.inner.collapse(e)
}
fn show_menu(&self, e: &ElementData) -> Result<()> {
self.inner.show_menu(e)
}
fn increment(&self, e: &ElementData) -> Result<()> {
self.inner.increment(e)
}
fn decrement(&self, e: &ElementData) -> Result<()> {
self.inner.decrement(e)
}
fn scroll_into_view(&self, e: &ElementData) -> Result<()> {
self.inner.scroll_into_view(e)
}
fn set_value(&self, e: &ElementData, v: &str) -> Result<()> {
self.inner.set_value(e, v)
}
fn set_numeric_value(&self, e: &ElementData, v: f64) -> Result<()> {
self.inner.set_numeric_value(e, v)
}
fn type_text(&self, e: &ElementData, t: &str) -> Result<()> {
self.inner.type_text(e, t)
}
fn set_text_selection(&self, e: &ElementData, s: u32, end: u32) -> Result<()> {
self.inner.set_text_selection(e, s, end)
}
fn perform_action(&self, e: &ElementData, a: &str) -> Result<()> {
self.inner.perform_action(e, a)
}
fn activate(&self, e: &ElementData) -> Result<()> {
self.inner.activate(e)
}
fn minimize(&self, e: &ElementData) -> Result<()> {
self.inner.minimize(e)
}
fn maximize(&self, e: &ElementData) -> Result<()> {
self.inner.maximize(e)
}
fn enter_fullscreen(&self, e: &ElementData) -> Result<()> {
self.inner.enter_fullscreen(e)
}
fn restore(&self, e: &ElementData) -> Result<()> {
self.inner.restore(e)
}
fn close(&self, e: &ElementData) -> Result<()> {
self.inner.close(e)
}
fn move_to(&self, e: &ElementData, x: i32, y: i32) -> Result<()> {
self.inner.move_to(e, x, y)
}
fn resize_to(&self, e: &ElementData, w: u32, h: u32) -> Result<()> {
self.inner.resize_to(e, w, h)
}
fn subscribe(&self, e: &ElementData) -> Result<Subscription> {
self.inner.subscribe(e)
}
}
/// Provider modelling the Linux AT-SPI shape where one process registers
/// twice and each entry exposes **distinct** windows (a main application
/// plus a dialog surfaced as its own app node). `App::by_pid` resolves a
/// single entry, so without the aggregation in [`App::windows_with`] the
/// sibling entry's window would be silently omitted.
struct SplitPidAppProvider {
inner: Arc<crate::mock::MockProvider>,
/// When true the second entry reports the same window as the first
/// (same stable id) — the registry can surface one window under both
/// entries, and the listing must not duplicate it.
overlap: bool,
/// Whether the fixture claims `splits_app_across_entries`. The merge
/// tests build the Linux shape (`true`); the gate test builds a
/// provider that violates the contract by returning two same-pid
/// entries while claiming `false`, so `windows_with` must skip the
/// merge.
claims_split: bool,
/// `list_apps` call counter — the gate test asserts `windows_with`
/// does not re-enumerate when the provider claims a single entry.
list_apps_calls: std::sync::atomic::AtomicUsize,
}
impl SplitPidAppProvider {
fn new(overlap: bool) -> Self {
Self {
inner: build_provider(),
overlap,
claims_split: true,
list_apps_calls: std::sync::atomic::AtomicUsize::new(0),
}
}
fn new_claiming_single_entry(overlap: bool) -> Self {
Self {
inner: build_provider(),
overlap,
claims_split: false,
list_apps_calls: std::sync::atomic::AtomicUsize::new(0),
}
}
fn app(handle: u64) -> ElementData {
ElementData {
role: Role::Application,
name: Some("SharedApp".to_string()),
value: None,
description: None,
bounds: None,
actions: vec![],
states: crate::element::StateSet::default(),
numeric_value: None,
min_value: None,
max_value: None,
stable_id: None,
pid: Some(42),
raw: Default::default(),
handle,
}
}
fn window(name: &str, handle: u64, stable_id: &str) -> ElementData {
ElementData {
role: Role::Window,
name: Some(name.to_string()),
value: None,
description: None,
bounds: None,
actions: vec![],
states: crate::element::StateSet::default(),
numeric_value: None,
min_value: None,
max_value: None,
stable_id: Some(stable_id.to_string()),
pid: Some(42),
raw: Default::default(),
handle,
}
}
}
impl Provider for SplitPidAppProvider {
fn splits_app_across_entries(&self) -> bool {
self.claims_split
}
fn app_roots(&self, app: &ElementData) -> Result<Vec<ElementData>> {
if self.claims_split {
Ok(vec![Self::app(700), Self::app(701)])
} else {
Ok(vec![app.clone()])
}
}
fn list_apps(&self) -> Result<Vec<ElementData>> {
self.list_apps_calls
.fetch_add(1, std::sync::atomic::Ordering::SeqCst);
Ok(vec![Self::app(700), Self::app(701)])
}
fn focused_app(&self) -> Result<ElementData> {
Ok(Self::app(700))
}
fn list_shell_surfaces(
&self,
) -> Result<Vec<(crate::shell::ShellSurfaceKind, ElementData)>> {
self.inner.list_shell_surfaces()
}
fn get_children(&self, e: Option<&ElementData>) -> Result<Vec<ElementData>> {
match e {
Some(el) if matches!(el.role, Role::Application) && el.pid == Some(42) => {
if el.handle == 701 {
let modal = Self::window("Modal", 101, "path/modal");
Ok(if self.overlap {
vec![Self::window("Main", 100, "path/main")]
} else {
vec![modal]
})
} else {
Ok(vec![Self::window("Main", 100, "path/main")])
}
}
_ => self.inner.get_children(e),
}
}
fn get_parent(&self, e: &ElementData) -> Result<Option<ElementData>> {
if matches!(e.role, Role::Window | Role::Dialog) && e.pid == Some(42) {
if e.stable_id.as_deref() == Some("path/main") {
return Ok(Some(Self::app(700)));
}
return Ok(Some(Self::app(701)));
}
self.inner.get_parent(e)
}
fn press(&self, e: &ElementData) -> Result<()> {
self.inner.press(e)
}
fn focus(&self, e: &ElementData) -> Result<()> {
self.inner.focus(e)
}
fn blur(&self, e: &ElementData) -> Result<()> {
self.inner.blur(e)
}
fn toggle(&self, e: &ElementData) -> Result<()> {
self.inner.toggle(e)
}
fn select(&self, e: &ElementData) -> Result<()> {
self.inner.select(e)
}
fn expand(&self, e: &ElementData) -> Result<()> {
self.inner.expand(e)
}
fn collapse(&self, e: &ElementData) -> Result<()> {
self.inner.collapse(e)
}
fn show_menu(&self, e: &ElementData) -> Result<()> {
self.inner.show_menu(e)
}
fn increment(&self, e: &ElementData) -> Result<()> {
self.inner.increment(e)
}
fn decrement(&self, e: &ElementData) -> Result<()> {
self.inner.decrement(e)
}
fn scroll_into_view(&self, e: &ElementData) -> Result<()> {
self.inner.scroll_into_view(e)
}
fn set_value(&self, e: &ElementData, v: &str) -> Result<()> {
self.inner.set_value(e, v)
}
fn set_numeric_value(&self, e: &ElementData, v: f64) -> Result<()> {
self.inner.set_numeric_value(e, v)
}
fn type_text(&self, e: &ElementData, t: &str) -> Result<()> {
self.inner.type_text(e, t)
}
fn set_text_selection(&self, e: &ElementData, s: u32, end: u32) -> Result<()> {
self.inner.set_text_selection(e, s, end)
}
fn perform_action(&self, e: &ElementData, a: &str) -> Result<()> {
self.inner.perform_action(e, a)
}
fn activate(&self, e: &ElementData) -> Result<()> {
self.inner.activate(e)
}
fn minimize(&self, e: &ElementData) -> Result<()> {
self.inner.minimize(e)
}
fn maximize(&self, e: &ElementData) -> Result<()> {
self.inner.maximize(e)
}
fn enter_fullscreen(&self, e: &ElementData) -> Result<()> {
self.inner.enter_fullscreen(e)
}
fn restore(&self, e: &ElementData) -> Result<()> {
self.inner.restore(e)
}
fn close(&self, e: &ElementData) -> Result<()> {
self.inner.close(e)
}
fn move_to(&self, e: &ElementData, x: i32, y: i32) -> Result<()> {
self.inner.move_to(e, x, y)
}
fn resize_to(&self, e: &ElementData, w: u32, h: u32) -> Result<()> {
self.inner.resize_to(e, w, h)
}
fn subscribe(&self, e: &ElementData) -> Result<Subscription> {
self.inner.subscribe(e)
}
}
#[test]
fn windows_with_merges_windows_of_same_pid_application_entries() {
// `App::by_pid` resolves the first same-pid entry only; the Linux
// registry can register a second entry (the dialog's own app node),
// and `windows_with` must merge its windows — the D2 gap the review
// flagged: `by_pid(pid).windows()` used to silently omit them.
let provider: Arc<dyn Provider> = Arc::new(SplitPidAppProvider::new(false));
let app = App::by_pid_with(Arc::clone(&provider), 42, Duration::ZERO)
.expect("pid 42 must resolve in the split fixture");
let windows =
App::windows_with(Arc::clone(&provider), &app.data).expect("windows_with must succeed");
let names: Vec<&str> = windows
.iter()
.map(|w| w.data().name.as_deref().unwrap_or_default())
.collect();
assert_eq!(
names,
vec!["Main", "Modal"],
"both entries' windows must list"
);
assert_eq!(
app.locator("window")
.count()
.expect("locator must search every process registration"),
2
);
assert_eq!(
app.tree(Some(1))
.expect("tree must include every process registration")
.children
.len(),
2
);
assert!(app.dump(Some(1)).unwrap().contains("Modal"));
assert_eq!(app.locator("window:nth(1)").count().unwrap(), 1);
assert_eq!(
app.locator("window:nth(2)")
.element()
.unwrap()
.name
.as_deref(),
Some("Modal")
);
}
#[test]
fn windows_with_deduplicates_a_window_reported_by_two_entries() {
// The registry can surface one window under both Application entries
// (same stable id); the merged listing must not duplicate it.
let provider: Arc<dyn Provider> = Arc::new(SplitPidAppProvider::new(true));
let app = App::by_pid_with(Arc::clone(&provider), 42, Duration::ZERO)
.expect("pid 42 must resolve in the split fixture");
let windows =
App::windows_with(Arc::clone(&provider), &app.data).expect("windows_with must succeed");
let names: Vec<&str> = windows
.iter()
.map(|w| w.data().name.as_deref().unwrap_or_default())
.collect();
assert_eq!(
names,
vec!["Main"],
"a window shared by entries must list once"
);
assert_eq!(app.tree(Some(1)).unwrap().children.len(), 1);
}
#[test]
fn windows_with_skips_the_pid_merge_for_single_entry_providers() {
// macOS and Windows guarantee one Application entry per pid, so the
// same-pid merge must not run there: `windows_with` trusts the
// provider's `splits_app_across_entries` claim. This fixture
// deliberately violates the contract — `list_apps` returns two
// same-pid entries while the provider claims `false` — and the test
// pins the trust: the sibling entry's window must NOT be merged, and
// `list_apps` must not be re-enumerated at all. That re-enumeration is
// also the macOS duplicate-window bug: a rebuilt entry carries fresh
// handles, so an unclaimed merge duplicates windows without an
// AXIdentifier instead of deduplicating them.
let provider = Arc::new(SplitPidAppProvider::new_claiming_single_entry(false));
let any: Arc<dyn Provider> = provider.clone();
let app = App::by_pid_with(Arc::clone(&any), 42, Duration::ZERO)
.expect("pid 42 must resolve in the split fixture");
let calls_after_by_pid = provider
.list_apps_calls
.load(std::sync::atomic::Ordering::SeqCst);
let windows =
App::windows_with(Arc::clone(&any), &app.data).expect("windows_with must succeed");
let names: Vec<&str> = windows
.iter()
.map(|w| w.data().name.as_deref().unwrap_or_default())
.collect();
assert_eq!(
names,
vec!["Main"],
"a provider claiming one entry per pid must not merge a sibling entry's window"
);
assert_eq!(
provider
.list_apps_calls
.load(std::sync::atomic::Ordering::SeqCst),
calls_after_by_pid,
"windows_with must not re-enumerate list_apps on a one-entry-per-pid provider"
);
}
#[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 by_name_with_rejects_double_quote_in_name() {
// The selector grammar has no escape sequence for quotes inside
// attribute values, so a name containing `"` cannot be represented
// in the `application[name="..."]` diagnostic selector. The lookup
// must fail clearly up front instead of emitting a malformed
// selector (tenet 1: no silent fallback to a broken string).
let provider: Arc<dyn Provider> = build_provider();
let err = App::by_name_with(provider, r#"My "Quoted" App"#, Duration::ZERO)
.expect_err("names containing '\"' must be rejected");
match err {
Error::InvalidSelector { selector, message } => {
assert_eq!(selector, r#"My "Quoted" App"#);
assert!(
message.contains("double quote"),
"message must explain the quote limitation: {message}"
);
assert!(
message.contains("find_with"),
"message must point at the predicate-based alternative: {message}"
);
}
other => panic!("expected InvalidSelector, got: {other:?}"),
}
}
#[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 list_with_tags_foreground_app_as_foreground() {
// MockProvider::focused_app reports the application root (pid 1234) as
// foreground. It's the lone pid-matching entry (the macOS/Linux shape),
// so even though the app root itself carries active=false it is tagged
// via the n_matches == 1 rule and comes back `is_foreground()`.
let provider: Arc<dyn Provider> = build_provider();
let apps = App::list_with(provider).expect("list must succeed");
assert_eq!(apps.len(), 1);
assert!(
!apps[0].data.states.active,
"the app root itself is not an active window"
);
assert!(
apps[0].is_foreground(),
"the sole pid-matching entry must be tagged foreground by list_with"
);
}
#[test]
fn find_with_predicate_sees_focused_flag() {
// The predicate runs against the tagged ElementData, so selecting on
// `focused` must match the foreground app.
let provider: Arc<dyn Provider> = build_provider();
let app = App::find_with(provider, Duration::ZERO, |d| d.states.focused)
.expect("the foreground app must be findable via the focused flag");
assert_eq!(app.name, "TestApp");
assert!(app.is_foreground());
}
#[test]
fn list_with_leaves_apps_untagged_when_nothing_focused() {
// A provider whose `focused_app` reports "nothing focused"
// (SelectorNotMatched) must not fail enumeration — every app stays
// unfocused rather than the error propagating.
let provider: Arc<dyn Provider> = Arc::new(FocusModeProvider::new(FocusMode::None));
let apps = App::list_with(provider).expect("list must succeed with no focused app");
assert_eq!(apps.len(), 1);
assert!(
!apps[0].is_foreground(),
"no app should be foreground when focused_app reports none"
);
}
#[test]
fn list_with_propagates_real_focus_errors() {
// A genuine focus-resolution failure (not "nothing focused") must
// surface, not be silently swallowed (tenet 1).
let provider: Arc<dyn Provider> = Arc::new(FocusModeProvider::new(FocusMode::Error));
let err = App::list_with(provider).expect_err("a real focus error must propagate");
assert!(matches!(err, Error::Platform { code: 99, .. }));
}
#[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 { .. }));
}
#[test]
fn windows_with_lists_both_windows_of_shared_pid() {
// The unified shape: ONE Application node per process whose children
// are the process's top-level windows (issue #304: main + modal, in
// enumeration order). Both windows must survive the enumeration, and
// the per-app `windows()` convenience must agree with the explicit
// `windows_with` call.
let provider: Arc<dyn Provider> = Arc::new(MultiWindowProvider::new());
let app = App::by_pid_with(Arc::clone(&provider), 42, Duration::ZERO)
.expect("pid 42 must resolve in the multi-window fixture");
let windows =
App::windows_with(Arc::clone(&provider), &app.data).expect("windows_with must succeed");
let names: Vec<&str> = windows
.iter()
.map(|w| w.data().name.as_deref().unwrap_or_default())
.collect();
assert_eq!(names, vec!["Main", "Modal"], "both windows must list");
let via_app = app.windows().expect("App::windows must succeed");
let names2: Vec<&str> = via_app
.iter()
.map(|w| w.data().name.as_deref().unwrap_or_default())
.collect();
assert_eq!(names2, vec!["Main", "Modal"]);
}
#[test]
fn windows_keep_distinct_windows_with_duplicate_titles() {
// Window titles are presentation, never identity. Two sibling or
// modal windows may legitimately have the same title, and discovery
// must preserve both so a caller can disambiguate with :nth or other
// attributes instead of silently losing one.
let provider = Arc::new(MultiWindowProvider::new());
provider.replace_windows(vec![
MultiWindowProvider::window("Untitled", 100, false),
MultiWindowProvider::window("Untitled", 101, true),
]);
let any: Arc<dyn Provider> = provider;
let app = App::by_pid_with(any, 42, Duration::ZERO)
.expect("the duplicate-title app must resolve");
let windows = app
.windows()
.expect("duplicate-title windows must enumerate");
assert_eq!(windows.len(), 2, "titles must not be used as a dedup key");
assert_ne!(windows[0].data().handle, windows[1].data().handle);
assert_eq!(
app.locator(r#"window[name="Untitled"]"#)
.count()
.expect("both titled windows must be selectable"),
2
);
}
#[test]
fn locator_re_resolves_when_the_matching_window_changes() {
// A Locator is a live query, not a saved Element handle. Model the
// common window-churn case: the first matching window disappears (or
// changes title) and a sibling becomes the match between operations.
let provider = Arc::new(MultiWindowProvider::new());
provider.replace_windows(vec![
MultiWindowProvider::window("Target", 100, true),
MultiWindowProvider::window("Other", 101, false),
]);
let any: Arc<dyn Provider> = provider.clone();
let app = App::by_pid_with(any, 42, Duration::ZERO)
.expect("the changing-window app must resolve");
let locator = app
.locator(r#"window[name="Target"]"#)
.with_timeout(Duration::ZERO);
let first = locator.element().expect("the first target must resolve");
assert_eq!(first.data().handle, 100);
provider.replace_windows(vec![
MultiWindowProvider::window("Other", 100, false),
MultiWindowProvider::window("Target", 101, true),
]);
let second = locator
.element()
.expect("the locator must resolve the replacement target");
assert_eq!(second.data().handle, 101);
assert_ne!(first.data().handle, second.data().handle);
}
#[test]
fn windows_keep_provider_reported_invisible_windows_discoverable() {
// Once a platform provider reports a live top-level window, core
// discovery filters only by role. It must not apply a second
// visibility filter: minimized windows commonly report visible=false,
// while closed windows are removed by the provider (and are covered
// by closed_window_leaves_windows_enumeration).
let provider = Arc::new(MultiWindowProvider::new());
let mut hidden = MultiWindowProvider::window("Provider-reported", 100, false);
hidden.states.visible = false;
hidden.states.minimized = Some(false);
let mut minimized = MultiWindowProvider::window("Minimized", 101, false);
minimized.states.visible = false;
minimized.states.minimized = Some(true);
provider.replace_windows(vec![hidden, minimized]);
let any: Arc<dyn Provider> = provider;
let app = App::by_pid_with(any, 42, Duration::ZERO)
.expect("the invisible-window app must resolve");
let windows = app.windows().expect("reported windows must enumerate");
assert_eq!(windows.len(), 2);
assert!(windows.iter().all(|w| !w.states.visible));
assert_eq!(windows[0].states.minimized, Some(false));
assert_eq!(windows[1].states.minimized, Some(true));
}
#[test]
fn windows_rejects_non_application_elements() {
// "Windows of an app" is defined only for Application elements — the
// app-node-unified contract every platform reports. Asking a window
// for its windows can no longer mean anything, so it must fail with
// the role named in the error (tenet 6), not silently answer
// "no windows".
let app = mock_app();
let window = app
.windows()
.expect("windows must succeed")
.pop()
.expect("the mock app owns a window");
let err = App::windows_with(Arc::clone(app.provider()), window.data())
.expect_err("windows_with on a window must fail");
assert!(
matches!(
&err,
Error::ActionNotSupported { action, role: Role::Window } if action == "windows"
),
"expected ActionNotSupported naming the window role, got {err:?}"
);
}
#[test]
fn windows_on_mock_returns_main_window_child() {
// The macOS/Linux shape: the app element's `role=Window` children.
let app = mock_app();
let windows = app.windows().expect("windows must succeed");
assert_eq!(windows.len(), 1);
assert_eq!(windows[0].data().name.as_deref(), Some("Main Window"));
}
#[test]
fn closed_window_leaves_windows_enumeration() {
// Real providers drop a closed window from the tree; the mock must
// model that too, or a close regression would pass against the mock
// and fail on real platforms. A minimized window (also visible=false)
// must by contrast STAY listed — `closed`, not `visible`, is the
// removal signal.
let app = mock_app();
let windows = app.windows().expect("windows must succeed");
assert_eq!(
windows.len(),
1,
"mock app must expose exactly its main window before any mutation"
);
windows[0].minimize().expect("minimize must succeed");
assert_eq!(
app.windows().expect("windows must succeed").len(),
1,
"a minimized window is visible=false but must stay listed"
);
let windows = app.windows().expect("windows must succeed");
windows[0].close().expect("close must succeed");
assert_eq!(
app.windows().expect("windows must succeed").len(),
0,
"a closed window must drop out of the enumeration"
);
}
#[test]
fn closed_window_stale_handle_is_dead() {
// The `closed` flag must make a window vanish not only from
// `App::windows()` but from every access through a stale handle:
// `get_children` and `get_parent` both treat it as gone, exactly as
// real providers do after destroying the element. A mock that
// resolved a closed window's subtree would pass tests that fail on
// real platforms.
let app = mock_app();
let windows = app.windows().expect("windows must succeed");
let window = windows[0].clone();
// A descendant captured before close — real providers destroy the
// whole subtree with the window, so this handle must die too.
let button = app
.locator("button")
.elements()
.expect("buttons must resolve before the window closes")
.pop()
.expect("the mock app owns a button");
window.close().expect("close must succeed");
assert!(
window
.children()
.expect("children of a closed window must resolve")
.is_empty(),
"a closed window must not resolve children through a stale handle"
);
assert!(
window
.parent()
.expect("parent of a closed window must resolve")
.is_none(),
"a closed window must not resolve a parent through a stale handle"
);
assert!(
button
.parent()
.expect("parent of a stale descendant must resolve")
.is_none(),
"a closed window's descendant must not resolve a parent"
);
assert!(
matches!(button.press(), Err(Error::Platform { .. })),
"a closed window's descendant must not remain actionable"
);
// `windows_with` on a window is a role error after the app-node
// unification (a window is not an Application, and the self-inclusion
// question it used to answer is gone); a closed window's liveness is
// asserted through the app instead — `closed_window_leaves_windows_enumeration`
// covers the empty listing, this asserts the role gate itself.
assert!(
matches!(
App::windows_with(Arc::clone(app.provider()), window.data()),
Err(Error::ActionNotSupported { action, role: Role::Window }) if action == "windows"
),
"windows_with on a window must be ActionNotSupported, not a listing"
);
// The verbs are the same rule with a handle: act on a closed window
// and the mock must fail, not silently mutate a dead node and report
// success (which would let stale-handle regressions pass against the
// mock, tenet 1).
for action in [
"activate",
"minimize",
"maximize",
"enter_fullscreen",
"restore",
"close",
"move_to",
"resize_to",
] {
let err = match action {
"activate" => window
.activate()
.expect_err("activate on a closed window must fail"),
"minimize" => window
.minimize()
.expect_err("minimize on a closed window must fail"),
"maximize" => window
.maximize()
.expect_err("maximize on a closed window must fail"),
"enter_fullscreen" => window
.enter_fullscreen()
.expect_err("enter_fullscreen on a closed window must fail"),
"restore" => window
.restore()
.expect_err("restore on a closed window must fail"),
"close" => window
.close()
.expect_err("close on a closed window must fail"),
"move_to" => window
.move_to(0, 0)
.expect_err("move_to on a closed window must fail"),
"resize_to" => window
.resize_to(100, 100)
.expect_err("resize_to on a closed window must fail"),
_ => unreachable!(),
};
assert!(
matches!(err, Error::Platform { .. }),
"{action} on a closed window must fail with Platform, got {err:?}"
);
}
}
#[test]
fn perform_action_window_verbs_match_the_typed_verbs() {
// `perform_action` is the generic escape hatch; the window names are
// well-known, so the mock must route them to the typed methods
// exactly like the real providers (see WindowsProvider: uia.rs). A
// mock that only recorded the string would let a test that fails on
// every platform pass against it — and the payload verbs take no data
// on this path, so they are rejected surfaceably (tenet 1).
let app = mock_app();
let windows = app.windows().expect("windows must succeed");
let window = windows[0].clone();
window
.perform_action("minimize")
.expect("perform_action(minimize) must succeed");
let re_listed = app.windows().expect("windows must succeed");
assert_eq!(
re_listed[0].states.minimized,
Some(true),
"perform_action(\"minimize\") must really minimize the window"
);
assert!(
matches!(
window.perform_action("move_to"),
Err(Error::InvalidActionData { .. })
),
"perform_action(\"move_to\") must be rejected without coordinates"
);
assert!(
matches!(
window.perform_action("resize_to"),
Err(Error::InvalidActionData { .. })
),
"perform_action(\"resize_to\") must be rejected without dimensions"
);
}
#[test]
fn list_with_collapses_shared_pid_registrations_into_one_process() {
// Regression for issue #304: a process owning several top-level
// windows (main window + modal dialog) must surface as one entry per
// registration sharing the pid in `App::list_with` — the old pid
// dedup silently dropped one. Both Application entries share pid 42,
// which `focused_app` reports as foreground, but tagging is
// window-precise: only the *active* entry (the modal) comes back
// `is_foreground()`, not every entry of the process.
let provider: Arc<dyn Provider> = Arc::new(SharedPidAppProvider::new());
let apps = App::list_with(provider).expect("list must succeed");
assert_eq!(apps.len(), 1, "App is process-scoped");
assert_eq!(apps[0].pid, Some(42));
assert!(apps[0].is_foreground());
}
#[test]
fn list_with_tags_none_when_shared_pid_has_no_active_window() {
// Several Application entries share the foreground pid but none
// reports `active` (the actual foreground window wasn't enumerable).
// Window-precise tagging must then tag *none* of them — honest, not a
// fallback to "tag them all" (tenet 1).
struct NoActiveMultiWindow {
inner: Arc<crate::mock::MockProvider>,
}
impl Provider for NoActiveMultiWindow {
fn list_apps(&self) -> Result<Vec<ElementData>> {
Ok(vec![
SharedPidAppProvider::app("Main", 200, false),
SharedPidAppProvider::app("Modal", 201, false),
])
}
fn focused_app(&self) -> Result<ElementData> {
// Reports pid 42 as foreground, but no enumerated entry is
// active.
Ok(SharedPidAppProvider::app("Ghost", 202, false))
}
fn list_shell_surfaces(
&self,
) -> Result<Vec<(crate::shell::ShellSurfaceKind, ElementData)>> {
self.inner.list_shell_surfaces()
}
fn get_children(&self, e: Option<&ElementData>) -> Result<Vec<ElementData>> {
self.inner.get_children(e)
}
fn get_parent(&self, e: &ElementData) -> Result<Option<ElementData>> {
self.inner.get_parent(e)
}
fn press(&self, e: &ElementData) -> Result<()> {
self.inner.press(e)
}
fn focus(&self, e: &ElementData) -> Result<()> {
self.inner.focus(e)
}
fn blur(&self, e: &ElementData) -> Result<()> {
self.inner.blur(e)
}
fn toggle(&self, e: &ElementData) -> Result<()> {
self.inner.toggle(e)
}
fn select(&self, e: &ElementData) -> Result<()> {
self.inner.select(e)
}
fn expand(&self, e: &ElementData) -> Result<()> {
self.inner.expand(e)
}
fn collapse(&self, e: &ElementData) -> Result<()> {
self.inner.collapse(e)
}
fn show_menu(&self, e: &ElementData) -> Result<()> {
self.inner.show_menu(e)
}
fn increment(&self, e: &ElementData) -> Result<()> {
self.inner.increment(e)
}
fn decrement(&self, e: &ElementData) -> Result<()> {
self.inner.decrement(e)
}
fn scroll_into_view(&self, e: &ElementData) -> Result<()> {
self.inner.scroll_into_view(e)
}
fn set_value(&self, e: &ElementData, v: &str) -> Result<()> {
self.inner.set_value(e, v)
}
fn set_numeric_value(&self, e: &ElementData, v: f64) -> Result<()> {
self.inner.set_numeric_value(e, v)
}
fn type_text(&self, e: &ElementData, t: &str) -> Result<()> {
self.inner.type_text(e, t)
}
fn set_text_selection(&self, e: &ElementData, s: u32, end: u32) -> Result<()> {
self.inner.set_text_selection(e, s, end)
}
fn perform_action(&self, e: &ElementData, a: &str) -> Result<()> {
self.inner.perform_action(e, a)
}
fn activate(&self, e: &ElementData) -> Result<()> {
self.inner.activate(e)
}
fn minimize(&self, e: &ElementData) -> Result<()> {
self.inner.minimize(e)
}
fn maximize(&self, e: &ElementData) -> Result<()> {
self.inner.maximize(e)
}
fn enter_fullscreen(&self, e: &ElementData) -> Result<()> {
self.inner.enter_fullscreen(e)
}
fn restore(&self, e: &ElementData) -> Result<()> {
self.inner.restore(e)
}
fn close(&self, e: &ElementData) -> Result<()> {
self.inner.close(e)
}
fn move_to(&self, e: &ElementData, x: i32, y: i32) -> Result<()> {
self.inner.move_to(e, x, y)
}
fn resize_to(&self, e: &ElementData, w: u32, h: u32) -> Result<()> {
self.inner.resize_to(e, w, h)
}
fn subscribe(&self, e: &ElementData) -> Result<Subscription> {
self.inner.subscribe(e)
}
}
let provider: Arc<dyn Provider> = Arc::new(NoActiveMultiWindow {
inner: build_provider(),
});
let apps = App::list_with(provider).expect("list must succeed");
assert_eq!(apps.len(), 1);
assert!(
apps[0].is_foreground(),
"foreground identity comes from focused_app"
);
}
#[test]
fn foreground_with_resolves_and_tags_the_foreground_app() {
// The mock reports its application root (pid 1234) as the foreground
// app; `foreground_with` must return it and mark it `focused()`.
let provider: Arc<dyn Provider> = build_provider();
let app = App::foreground_with(provider, Duration::ZERO)
.expect("the mock's foreground app must resolve");
assert_eq!(app.name, "TestApp");
assert!(
app.is_foreground(),
"the app returned by foreground_with must always be is_foreground()"
);
}
#[test]
#[allow(deprecated)]
fn focused_is_deprecated_alias_for_is_foreground() {
// `focused()` is retained only as a deprecated alias; it must return
// exactly what `is_foreground()` returns.
let provider: Arc<dyn Provider> = build_provider();
let apps = App::list_with(provider).expect("list must succeed");
assert_eq!(apps.len(), 1);
assert_eq!(apps[0].focused(), apps[0].is_foreground());
assert!(apps[0].focused());
}
#[test]
fn foreground_with_returns_selector_not_matched_when_nothing_focused() {
// "Nothing holds focus" is the retryable not-found signal; with a zero
// timeout that's one attempt and a plain SelectorNotMatched.
let provider: Arc<dyn Provider> = Arc::new(FocusModeProvider::new(FocusMode::None));
let err = App::foreground_with(provider, Duration::ZERO)
.expect_err("nothing focused must surface as not-matched");
assert!(matches!(err, Error::SelectorNotMatched { .. }));
}
#[test]
fn foreground_with_propagates_real_focus_errors_and_fails_fast() {
// A genuine foreground-query failure (not "nothing focused") must
// short-circuit the poll immediately rather than being retried until
// the timeout (mirrors `try_find_with_propagates_predicate_error_...`).
let provider: Arc<dyn Provider> = Arc::new(FocusModeProvider::new(FocusMode::Error));
let start = Instant::now();
let err = App::foreground_with(provider, Duration::from_secs(30))
.expect_err("a real focus error must propagate");
assert!(matches!(err, Error::Platform { code: 99, .. }));
assert!(
start.elapsed() < Duration::from_secs(1),
"a real focus error must fail fast, not wait out the timeout"
);
}
}