ferroui-android 0.2.0

The Android platform of FerroUI: a view in an activity, rendered through EGL or to the native window
Documentation
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
2119
2120
2121
2122
2123
2124
//! The smoke application of the Android platform: a shared library that
//! `scripts/android/apk.sh` packages and `scripts/android/emu-smoke.sh` runs
//! on the emulator (`docs/porting/android-platform.md`, sections 9 and 10).
//!
//! ```text
//! scripts/android/apk.sh smoke
//! scripts/android/emu-smoke.sh
//! ```
//!
//! The application shows a view with a stripe, a line of text and an
//! ellipse, drawn by the compositor with Skia on the render thread, and
//! checks itself: the activity, the view and its surface; the size and the
//! scaling against the screen; the insets; the dispatcher (a timer, and a
//! job posted from another thread); frames, by reading pixels back from the
//! surface that is being drawn, inside the frame; touch input the script
//! injects; and the end of the activity. Every check is a line `[ ok ]` or
//! `[FAIL]` in the log of the system under the tag `ferroui-smoke` and in
//! the file `smoke-report.txt` of the files directory of the application;
//! the last line is `RESULT: PASS` or `RESULT: FAIL`.
//!
//! The file `smoke.properties` of the files directory, when the script has
//! written it, selects what is run: `mode=software` renders through the
//! native window instead of EGL, `mode=vulkan` renders through Vulkan alone
//! (the library built with the feature `vulkan` of ferroui-android:
//! `scripts/android/apk.sh smoke --features ferroui-android/vulkan`),
//! `input=0` leaves out the checks that need the script to act (touch,
//! keys, the night mode).
//!
//! A line `SCRIPT <command> <arguments>` asks the script that runs the
//! application to do something on the device (`scripts/android/emu-smoke.sh`
//! lists the commands).
//!
//! On another system than Android the library is empty.

#[cfg(target_os = "android")]
ferroui_android::android_application!(app::build);

#[cfg(target_os = "android")]
mod app {
    use ferroui_android::interop::java::{
        call_boolean, call_float, call_int, call_object, call_void, new_object, string_of, JavaClass, JavaLocal,
        JavaRef, JavaValue,
    };
    use ferroui_android::log::{self, LogPriority};
    use ferroui_android::{
        AndroidApplicationExtensions, AndroidPlatform, AndroidPlatformOptions, AndroidRenderingMode,
        AndroidViewControlHandle, FerroActivity, FerroAndroidApplication,
    };
    use ferroui_base::input::platform::ClipboardExtensions;
    use ferroui_base::platform::storage::WellKnownFolder;
    use ferroui_base::reactive::IDisposable;
    use ferroui_base::utilities::Uri;
    use ferroui_controls::platform::{
        FeedbackAction, FeedbackType, INativeControlHostControlTopLevelAttachment,
        INativeControlHostDestroyableControlHandle, INativeControlHostImpl, IPlatformFeedback, IPlatformHandle,
    };
    use ferroui_base::input::text_input::{
        TextInputMethodClient, TextInputMethodClientEvents, TextInputMethodClientRequestedEventArgs, TextSelection,
    };
    use ferroui_base::input::{
        InputElement, InputElementImpl, Key, KeyEventArgs, PointerEventArgs, PointerPressedEventArgs,
        PointerReleasedEventArgs, PointerType, TextInputEventArgs,
    };
    use ferroui_base::interactivity::{Interactive, InteractiveImpl};
    use ferroui_base::layout::{HorizontalAlignment, LayoutableImpl};
    use ferroui_base::media::immutable::ImmutableSolidColorBrush;
    use ferroui_base::media::{Color, DrawingContext, IBrush, ImmediateDrawingContext};
    use ferroui_base::platform::{IPlatformSettings, PlatformColorValues, PlatformThemeVariant};
    use ferroui_base::rendering::scene_graph::ICustomDrawOperation;
    use ferroui_base::threading::{Dispatcher, DispatcherPriority, DispatcherTimer};
    use ferroui_base::{
        ferro_class, ferro_impl_classes, instantiate, FerroLocator, FerroObjectImpl, LocatorExtensions, Point, Rect,
        Ref, StyledElementImpl, Thickness, Visual, VisualImpl,
    };
    use ferroui_base::interactivity::RoutedEventArgs;
    use ferroui_controls::application_lifetimes::{ActivatedEventArgs, ActivationKind, IActivatableLifetime};
    use ferroui_controls::platform::{InputPaneState, InputPaneStateEventArgs};
    use ferroui_controls::automation::AutomationProperties;
    use ferroui_controls::primitives::RangeBase;
    use ferroui_controls::shapes::Ellipse;
    use ferroui_controls::{
        AppBuilder, Application, ApplicationImpl, ApplicationImplExt, Border, Button, CheckBox, Control, ControlImpl,
        NewApplication, Panel, Slider, StackPanel, TextBlock, TextBox, TopLevel,
    };
    use ferroui_skia::ISkiaApiLeaseFeature;
    use std::any::TypeId;
    use std::cell::{Cell, RefCell};
    use std::rc::Rc;
    use std::sync::atomic::{AtomicBool, Ordering};
    use std::sync::{Arc, Mutex, PoisonError};
    use std::time::{Duration, Instant};

    /// The tag of the lines of the smoke run.
    const TAG: &str = "ferroui-smoke";

    /// The colours drawn.
    const FILL: [u8; 3] = [0x33, 0x66, 0x99];
    const STRIPE: [u8; 3] = [0xcc, 0x33, 0x33];
    const ELLIPSE: [u8; 3] = [0xff, 0xcc, 0x00];
    const STRIPE_HEIGHT: f64 = 96.0;
    const ELLIPSE_SIZE: f64 = 120.0;

    fn brush(color: [u8; 3]) -> Option<Rc<dyn IBrush>> {
        Some(Rc::new(ImmutableSolidColorBrush::new(Color::from_rgb(color[0], color[1], color[2]))))
    }

    // ---- the options of the run -----------------------------------------------------------

    #[derive(Clone, Copy)]
    struct SmokeOptions {
        software: bool,
        vulkan: bool,
        input: bool,
    }

    fn files_dir() -> Option<String> {
        let context = FerroAndroidApplication::context();
        let directory = call_object(&context, "getFilesDir", "()Ljava/io/File;", &[])?;
        let path = call_object(&directory, "getAbsolutePath", "()Ljava/lang/String;", &[])?;
        Some(string_of(&path))
    }

    fn read_options() -> SmokeOptions {
        let mut options = SmokeOptions { software: false, vulkan: false, input: true };
        let text =
            files_dir().and_then(|directory| std::fs::read_to_string(format!("{directory}/smoke.properties")).ok());
        for line in text.as_deref().unwrap_or_default().lines() {
            match line.trim().split_once('=') {
                Some(("mode", value)) => {
                    options.software = value.trim() == "software";
                    options.vulkan = value.trim() == "vulkan";
                }
                Some(("input", value)) => options.input = value.trim() != "0",
                _ => {}
            }
        }
        options
    }

    ferroui_base::ferro_thread_local! {
        static OPTIONS: Cell<SmokeOptions> =
            const { Cell::new(SmokeOptions { software: false, vulkan: false, input: true }) };
        static REPORT: Report = Report::default();
    }

    // ---- the report -----------------------------------------------------------------------

    #[derive(Default)]
    struct Report {
        lines: RefCell<Vec<String>>,
        failed: Cell<bool>,
    }

    fn check(name: &str, ok: bool, detail: impl AsRef<str>) {
        let line = format!("{} {name}: {}", if ok { "[ ok ]" } else { "[FAIL]" }, detail.as_ref());
        log::write(if ok { LogPriority::Info } else { LogPriority::Error }, TAG, &line);
        REPORT.with(|report| {
            if !ok {
                report.failed.set(true);
            }
            report.lines.borrow_mut().push(line);
        });
    }

    fn note(text: impl AsRef<str>) {
        log::write(LogPriority::Info, TAG, text.as_ref());
        REPORT.with(|report| report.lines.borrow_mut().push(text.as_ref().to_string()));
    }

    /// Writes the result line and the report file.
    fn finish_report() {
        let passed = REPORT.with(|report| !report.failed.get());
        note(if passed { "RESULT: PASS" } else { "RESULT: FAIL" });
        let lines = REPORT.with(|report| report.lines.borrow().join("\n"));
        match files_dir() {
            Some(directory) => {
                if let Err(error) = std::fs::write(format!("{directory}/smoke-report.txt"), lines + "\n") {
                    log::write(LogPriority::Error, TAG, &format!("the report file could not be written: {error}"));
                }
            }
            None => log::write(LogPriority::Error, TAG, "the application has no files directory"),
        }
        log::write(LogPriority::Info, TAG, "REPORT WRITTEN");
    }

    // ---- the application ------------------------------------------------------------------

    #[repr(C)]
    pub struct App {
        base: Application,
    }

    ferro_class!(App: Application);
    ferro_impl_classes!(App: FerroObjectImpl);

    impl NewApplication for App {
        fn new_application() -> Ref<Self> {
            instantiate(Self { base: Application::construct() })
        }
    }

    impl ApplicationImpl for App {
        fn initialize(this: &Self) {
            Self::parent_initialize(this);
            this.set_name(Some("FerroUI Android smoke".to_string()));
        }

        fn on_framework_initialization_completed(this: &Self) {
            let lifetime = this.application_lifetime();
            match lifetime.as_ref().and_then(|lifetime| lifetime.as_activity_application_lifetime()) {
                Some(lifetime) => {
                    lifetime.set_main_view_factory(Some(Rc::new(create_main_view)));
                    check("lifetime", true, "the application has the activity lifetime of the platform");
                }
                None => check("lifetime", false, "the application has no activity lifetime"),
            }

            Self::parent_on_framework_initialization_completed(this);
        }
    }

    /// The application builder: what `android_application!` is given.
    pub fn build() -> AppBuilder {
        let smoke = read_options();
        OPTIONS.with(|options| options.set(smoke));

        let options = AndroidPlatformOptions {
            rendering_mode: if smoke.software {
                vec![AndroidRenderingMode::Software]
            } else if smoke.vulkan {
                // Vulkan alone, without a fallback: a mode that does not initialise fails the
                // run instead of being passed over.
                vec![AndroidRenderingMode::Vulkan]
            } else {
                AndroidPlatformOptions::default().rendering_mode
            },
        };
        note(format!(
            "The library is loaded. Rendering modes asked for: {:?}; touch checks: {}",
            options.rendering_mode, smoke.input
        ));

        AppBuilder::configure::<App>().with(Rc::new(options)).use_android().after_setup(move |_| {
            check(
                "setup",
                Dispatcher::ui_thread().check_access() && AndroidPlatform::options().is_some(),
                "the platform is initialized and the dispatcher belongs to the main thread",
            );
            if smoke.vulkan {
                vulkan_checks();
            }
        })
    }

    /// Rendering with Vulkan: the platform registered the platform graphics
    /// of Vulkan, and a context of them has a device with a queue under its
    /// lock, the surface factory of the platform and an instance with the
    /// surface extension of Android. The frames themselves are checked by
    /// the readback, on the render thread.
    fn vulkan_checks() {
        use ferroui_base::platform::{IOptionalFeatureProvider, IPlatformGraphics};
        use ferroui_base::LocatorExtensions;
        use ferroui_vulkan::{IVulkanKhrSurfacePlatformSurfaceFactory, IVulkanPlatformGraphicsContext};

        let Some(graphics) = ferroui_base::FerroLocator::current().get_service::<Arc<dyn IPlatformGraphics>>() else {
            check("platform graphics", false, "the platform registered no platform graphics");
            return;
        };
        let context = graphics.create_context();
        let features: &dyn IOptionalFeatureProvider = &*context;
        let Some(vulkan) = features.try_get::<dyn IVulkanPlatformGraphicsContext>() else {
            check("platform graphics", false, "the platform graphics are not the graphics of Vulkan");
            context.dispose();
            return;
        };
        check("platform graphics", true, "the platform registered the graphics of Vulkan");
        let device = vulkan.device();
        let lock = context.ensure_current();
        let queue = device.main_queue_handle();
        lock.dispose();
        check(
            "Vulkan context",
            device.handle() != 0 && queue != 0,
            format!(
                "a context of the graphics has the device {:#x} with the queue family {} and the extensions {}",
                device.handle(),
                device.graphics_queue_family_index(),
                device.enabled_extensions().join(" ")
            ),
        );
        let has_factory = features.try_get_shared::<dyn IVulkanKhrSurfacePlatformSurfaceFactory>().is_some();
        check(
            "Vulkan surface factory",
            has_factory,
            format!("the context {} the surface factory of the platform", if has_factory { "has" } else { "has not" }),
        );
        let has_android = vulkan.instance().enabled_extensions().iter().any(|name| name == "VK_KHR_android_surface");
        check(
            "Vulkan instance",
            has_android,
            format!("the instance has the extensions {}", vulkan.instance().enabled_extensions().join(" ")),
        );
        context.dispose();
        device.dispose();
    }

    // ---- the view -------------------------------------------------------------------------

    /// What the render thread reads of a frame, and what the UI thread asks
    /// it to read.
    #[derive(Default)]
    struct Readback {
        /// The points to read, in pixels of the surface, with their names.
        points: Vec<(&'static str, i32, i32)>,
        /// The rectangle of the line of text, in pixels of the surface.
        text: Option<(i32, i32, i32, i32)>,
        /// The frames read so far.
        frames: u32,
        thread: String,
        surface: (i32, i32),
        gpu: bool,
        colors: Vec<(&'static str, [u8; 4])>,
        /// The pixels of the rectangle of the text that are nearly white.
        text_pixels: usize,
        error: Option<String>,
    }

    /// A control over the whole view that draws nothing and reads the frame
    /// back: its custom drawing operation runs on the thread that renders,
    /// after everything under it was drawn, with the Skia surface of the
    /// frame.
    #[repr(C)]
    struct Probe {
        base: Control,
        readback: Arc<Mutex<Readback>>,
    }

    ferro_class!(Probe: Control);
    ferro_impl_classes!(Probe: FerroObjectImpl, StyledElementImpl, InteractiveImpl, ControlImpl);

    impl InputElementImpl for Probe {}
    impl LayoutableImpl for Probe {}

    impl VisualImpl for Probe {
        fn render(this: &Self, context: &mut DrawingContext) {
            let bounds = this.bounds();
            let op: Arc<dyn ICustomDrawOperation> = Arc::new(ProbeOp {
                bounds: Rect::new(0.0, 0.0, bounds.width, bounds.height),
                readback: this.readback.clone(),
            });
            context.custom(&op);
        }
    }

    impl Probe {
        fn new(readback: Arc<Mutex<Readback>>) -> Ref<Self> {
            let this = instantiate(Self { base: Control::construct(), readback });
            this.set_is_hit_test_visible(false);
            this
        }
    }

    struct ProbeOp {
        bounds: Rect,
        readback: Arc<Mutex<Readback>>,
    }

    impl ICustomDrawOperation for ProbeOp {
        fn bounds(&self) -> Rect {
            self.bounds
        }

        fn hit_test(&self, _p: Point) -> bool {
            false
        }

        fn equals(&self, _other: &dyn ICustomDrawOperation) -> bool {
            false
        }

        fn dispose(&self) {}

        fn render(&self, context: &mut ImmediateDrawingContext<'_>) {
            let mut readback = self.readback.lock().unwrap_or_else(PoisonError::into_inner);
            if readback.frames >= 12 || readback.points.is_empty() {
                return;
            }

            let lease = context
                .try_get_feature(TypeId::of::<dyn ISkiaApiLeaseFeature>())
                .and_then(|feature| feature.downcast_ref::<Rc<dyn ISkiaApiLeaseFeature>>().cloned());
            let Some(lease) = lease else {
                readback.error =
                    Some("the drawing context of the frame gives no access to its Skia surface".to_string());
                return;
            };
            let lease = lease.lease();
            readback.gpu = lease.gr_context().is_some();
            let Some(mut surface) = lease.sk_surface() else {
                readback.error = Some("the frame has no Skia surface".to_string());
                lease.dispose();
                return;
            };

            readback.surface = (surface.width(), surface.height());
            readback.thread = std::thread::current().name().unwrap_or("(unnamed)").to_string();

            let read = |surface: &mut skia_safe::Surface, x: i32, y: i32, width: i32, height: i32| -> Option<Vec<u8>> {
                let info = skia_safe::ImageInfo::new(
                    (width, height),
                    skia_safe::ColorType::RGBA8888,
                    skia_safe::AlphaType::Premul,
                    None,
                );
                let mut pixels = vec![0u8; width as usize * height as usize * 4];
                surface.read_pixels(&info, &mut pixels, width as usize * 4, (x, y)).then_some(pixels)
            };

            let points = readback.points.clone();
            let mut colors = Vec::new();
            for (name, x, y) in points {
                match read(&mut surface, x, y, 1, 1) {
                    Some(pixel) => colors.push((name, [pixel[0], pixel[1], pixel[2], pixel[3]])),
                    None => readback.error = Some(format!("the pixel at {x},{y} ({name}) could not be read back")),
                }
            }
            readback.colors = colors;

            if let Some((x, y, width, height)) = readback.text {
                let (width, height) = (width.min(surface.width() - x), height.min(surface.height() - y));
                if width > 0 && height > 0 {
                    match read(&mut surface, x, y, width, height) {
                        Some(pixels) => {
                            readback.text_pixels =
                                pixels.chunks_exact(4).filter(|p| p[0] > 200 && p[1] > 200 && p[2] > 200).count();
                        }
                        None => readback.error = Some("the rectangle of the text could not be read back".to_string()),
                    }
                }
            }

            readback.frames += 1;
            lease.dispose();
        }
    }

    /// What the pointer handlers of the view saw.
    #[derive(Default)]
    struct Pointers {
        pressed: RefCell<Vec<Point>>,
        moved: Cell<u32>,
        released: RefCell<Vec<Point>>,
    }

    /// The client of the input method of the smoke run: a text and a
    /// selection in UTF-16 code units, edited as a text box edits its own
    /// by the text input and the delete keys the editor of the view gets.
    struct EditorClient {
        events: TextInputMethodClientEvents,
        visual: Ref<Visual>,
        text: RefCell<Vec<u16>>,
        selection: Cell<TextSelection>,
        /// The text inputs the editor got while it was the client.
        inputs: Cell<u32>,
    }

    impl EditorClient {
        fn text(&self) -> String {
            String::from_utf16_lossy(&self.text.borrow())
        }

        fn ordered(&self) -> (usize, usize) {
            let selection = self.selection.get();
            let length = self.text.borrow().len() as i32;
            let start = selection.start.min(selection.end).clamp(0, length);
            let end = selection.start.max(selection.end).clamp(0, length);
            (start as usize, end as usize)
        }

        fn replace(&self, start: usize, end: usize, with: &str) {
            let units: Vec<u16> = with.encode_utf16().collect();
            let caret = (start + units.len()) as i32;
            self.text.borrow_mut().splice(start..end, units);
            self.selection.set(TextSelection::new(caret, caret));
            self.raise_surrounding_text_changed();
            self.raise_selection_changed();
        }

        fn input(&self, text: &str) {
            self.inputs.set(self.inputs.get() + 1);
            let (start, end) = self.ordered();
            self.replace(start, end, text);
        }

        fn delete(&self, forward: bool) {
            let (start, end) = self.ordered();
            let length = self.text.borrow().len();
            if end > start {
                self.replace(start, end, "");
            } else if forward && start < length {
                self.replace(start, start + 1, "");
            } else if !forward && start > 0 {
                self.replace(start - 1, start, "");
            }
        }
    }

    impl TextInputMethodClient for EditorClient {
        fn events(&self) -> &TextInputMethodClientEvents {
            &self.events
        }

        fn text_view_visual(&self) -> Option<Ref<Visual>> {
            Some(self.visual.clone())
        }

        fn supports_preedit(&self) -> bool {
            false
        }

        fn supports_surrounding_text(&self) -> bool {
            true
        }

        fn surrounding_text(&self) -> String {
            self.text()
        }

        fn cursor_rectangle(&self) -> Rect {
            Rect::default()
        }

        fn selection(&self) -> TextSelection {
            self.selection.get()
        }

        fn set_selection(&self, value: TextSelection) {
            self.selection.set(value);
        }
    }

    struct MainView {
        editor: Ref<Border>,
        /// Something else that takes the focus, and has no text to edit.
        other: Ref<Border>,
        client: Rc<EditorClient>,
        root: Ref<Panel>,
        probe: Ref<Probe>,
        text: Ref<TextBlock>,
        ellipse: Ref<Ellipse>,
        readback: Arc<Mutex<Readback>>,
        pointers: Rc<Pointers>,
        /// The controls of the accessibility checks, and the clicks of the button.
        button: Ref<Button>,
        check_box: Ref<CheckBox>,
        slider: Ref<Slider>,
        text_box: Ref<TextBox>,
        clicks: Rc<Cell<u32>>,
    }

    fn create_main_view() -> Ref<Control> {
        let stripe = Border::new();
        stripe.set_height(STRIPE_HEIGHT);
        stripe.set_background(brush(STRIPE));

        let text = TextBlock::new();
        text.set_text(Some("FerroUI on Android"));
        text.set_font_size(28.0);
        text.set_foreground(brush([0xff, 0xff, 0xff]));
        text.set_margin(Thickness::uniform(16.0));
        text.set_horizontal_alignment(HorizontalAlignment::Center);

        let ellipse = Ellipse::new();
        ellipse.set_width(ELLIPSE_SIZE);
        ellipse.set_height(ELLIPSE_SIZE);
        ellipse.set_fill(brush(ELLIPSE));
        ellipse.set_horizontal_alignment(HorizontalAlignment::Center);

        // The controls of the accessibility checks. The application has no theme, so a
        // control has no template and draws nothing: each is given its size, and what the
        // accessibility tree says of it comes from its automation peer.
        let sized = |control: &Control, name: &str| {
            control.set_width(200.0);
            control.set_height(40.0);
            control.set_horizontal_alignment(HorizontalAlignment::Center);
            AutomationProperties::set_name(control, Some(name));
        };
        let button = Button::new();
        sized(&button, "Press");
        let clicks = Rc::new(Cell::new(0u32));
        button.click({
            let clicks = clicks.clone();
            move |_, _| clicks.set(clicks.get() + 1)
        });
        let check_box = CheckBox::new();
        sized(&check_box, "Agree");
        let slider = Slider::new();
        sized(&slider, "Volume");
        {
            let range: &RangeBase = &slider;
            range.set_minimum(0.0);
            range.set_maximum(10.0);
            range.set_range_value(4.0);
        }
        let text_box = TextBox::new();
        sized(&text_box, "Name");
        text_box.set_text(Some("NameText"));
        AutomationProperties::set_help_text(&text_box, Some("Your name"));

        let stack = StackPanel::new();
        stack.children().add(stripe);
        stack.children().add(text.clone());
        stack.children().add(ellipse.clone());
        stack.children().add(button.clone());
        stack.children().add(check_box.clone());
        stack.children().add(slider.clone());
        stack.children().add(text_box.clone());

        let background = Border::new();
        background.set_background(brush(FILL));
        background.set_child(stack);

        let readback = Arc::new(Mutex::new(Readback::default()));
        let probe = Probe::new(readback.clone());

        // The editor of the input method checks, and something else to move the focus to:
        // two elements of one pixel that draw nothing.
        let focusable = || {
            let border = Border::new();
            border.set_width(1.0);
            border.set_height(1.0);
            border.set_focusable(true);
            border
        };
        let (editor, other) = (focusable(), focusable());
        let client = Rc::new(EditorClient {
            events: TextInputMethodClientEvents::new(),
            visual: editor.clone().upcast(),
            text: RefCell::new(Vec::new()),
            selection: Cell::new(TextSelection::default()),
            inputs: Cell::new(0),
        });
        editor.add_handler(InputElement::text_input_method_client_requested_event(), {
            let client = client.clone();
            move |_: &Interactive, e: &TextInputMethodClientRequestedEventArgs| {
                let client: Rc<dyn TextInputMethodClient> = client.clone();
                e.set_client(Some(client));
            }
        });
        editor.add_handler(InputElement::text_input_event(), {
            let client = client.clone();
            move |_: &Interactive, e: &TextInputEventArgs| client.input(e.text.as_deref().unwrap_or_default())
        });
        editor.add_handler(InputElement::key_down_event(), {
            let client = client.clone();
            move |_: &Interactive, e: &KeyEventArgs| match e.key {
                Key::Delete => client.delete(true),
                Key::Back => client.delete(false),
                _ => {}
            }
        });

        let root = Panel::new();
        root.children().add(background);
        root.children().add(editor.clone());
        root.children().add(other.clone());
        root.children().add(probe.clone());

        let pointers = Rc::new(Pointers::default());
        root.add_handler(InputElement::pointer_pressed_event(), {
            let (pointers, root) = (pointers.clone(), root.clone());
            move |_: &Interactive, e: &PointerPressedEventArgs| {
                pointers.pressed.borrow_mut().push(e.get_position(Some(&root)));
            }
        });
        root.add_handler(InputElement::pointer_moved_event(), {
            let pointers = pointers.clone();
            move |_: &Interactive, _: &PointerEventArgs| pointers.moved.set(pointers.moved.get() + 1)
        });
        root.add_handler(InputElement::pointer_released_event(), {
            let (pointers, root) = (pointers.clone(), root.clone());
            move |_: &Interactive, e: &PointerReleasedEventArgs| {
                pointers.released.borrow_mut().push(e.get_position(Some(&root)));
            }
        });

        smoke::start(Rc::new(MainView {
            editor,
            other,
            client,
            root: root.clone(),
            probe,
            text,
            ellipse,
            readback,
            pointers,
            button,
            check_box,
            slider,
            text_box,
            clicks,
        }));

        root.upcast()
    }

    // ---- the checks -----------------------------------------------------------------------

    mod smoke {
        use super::*;

        #[derive(Clone, Copy, PartialEq, Eq, Debug)]
        enum Stage {
            WaitForSurface,
            Dispatcher,
            Frames,
            Input,
            Keys,
            InputPane,
            InputMethod,
            InputPaneClosed,
            NativeControl,
            Accessibility,
            Settings,
            Background,
            Foreground,
            Rotated,
            RotatedBack,
            Protocol,
            BackHandled,
            BackNotHandled,
            BackResumed,
            Finish,
            WaitForDestroy,
            Done,
        }

        struct State {
            view: Rc<MainView>,
            stage: Cell<Stage>,
            /// When the stage began.
            since: Cell<Instant>,
            timer_fired: Rc<Cell<bool>>,
            posted: Arc<AtomicBool>,
            posted_on_main: Arc<AtomicBool>,
            /// The pixel the script is asked to tap, and the logical point
            /// it is.
            tap: Cell<(i32, i32, Point)>,
            scaling: Cell<f64>,
            /// A step of a stage that is taken once.
            aimed: Cell<bool>,
            /// How far the accessibility stage is: 0 reads the nodes and performs the
            /// actions, 1 looks at what the actions did.
            accessibility_step: Cell<u32>,
            keys: Rc<Keys>,
            /// The colour values the platform settings raised as changed.
            color_changes: Rc<RefCell<Vec<PlatformColorValues>>>,
            /// The theme of the system when the settings stage began, and how
            /// far the stage is: 0 asked for the other theme, 1 asked for the
            /// first one again.
            first_theme: Cell<PlatformThemeVariant>,
            settings_step: Cell<u32>,
            /// The changes of the state of the input pane.
            pane_changes: Rc<RefCell<Vec<(InputPaneState, Rect, Duration, bool)>>>,
            lifecycle: Rc<Lifecycle>,
            /// The native control of the native control host check, and its attachment.
            native_control: RefCell<Option<NativeControl>>,
            /// Whether a back request is handled, and how many arrived.
            handle_back: Rc<Cell<bool>>,
            back_requests: Rc<Cell<u32>>,
        }

        type NativeControl =
            (Rc<dyn INativeControlHostDestroyableControlHandle>, Rc<dyn INativeControlHostControlTopLevelAttachment>);

        /// The value of a future that is complete when it is first asked.
        fn now<T>(future: impl std::future::Future<Output = T>) -> Option<T> {
            let future = std::pin::pin!(future);
            match future.poll(&mut std::task::Context::from_waker(std::task::Waker::noop())) {
                std::task::Poll::Ready(value) => Some(value),
                std::task::Poll::Pending => None,
            }
        }

        /// A feature of the platform implementation of the top-level.
        fn feature<T: ?Sized + 'static>(state: &State) -> Option<Rc<T>> {
            let platform_impl = top_level(state)?.platform_impl()?;
            let feature = platform_impl.try_get_feature(TypeId::of::<T>())?;
            feature.downcast_ref::<Rc<T>>().cloned()
        }

        /// What the activatable lifetime of the application raised.
        #[derive(Default)]
        struct Lifecycle {
            activated: RefCell<Vec<ActivationKind>>,
            deactivated: RefCell<Vec<ActivationKind>>,
            protocols: RefCell<Vec<String>>,
        }

        /// The key and text events the top-level raised.
        #[derive(Default)]
        struct Keys {
            down: RefCell<Vec<(Key, Option<String>)>>,
            up: RefCell<Vec<Key>>,
            text: RefCell<String>,
        }

        ferroui_base::ferro_thread_local! {
            /// How many times the main view was made: once per start of the activity.
            static STARTS: Cell<u32> = const { Cell::new(0) };
        }

        fn second_start() -> bool {
            STARTS.with(Cell::get) >= 2
        }

        pub fn start(view: Rc<MainView>) {
            STARTS.with(|starts| starts.set(starts.get() + 1));
            if second_start() {
                note("The activity was started again in the process: the checks of the surface and of the frames follow.");
            }
            let state = Rc::new(State {
                view,
                stage: Cell::new(Stage::WaitForSurface),
                since: Cell::new(Instant::now()),
                timer_fired: Rc::new(Cell::new(false)),
                posted: Arc::new(AtomicBool::new(false)),
                posted_on_main: Arc::new(AtomicBool::new(false)),
                tap: Cell::new((0, 0, Point::default())),
                scaling: Cell::new(1.0),
                aimed: Cell::new(false),
                accessibility_step: Cell::new(0),
                keys: Rc::new(Keys::default()),
                color_changes: Rc::new(RefCell::new(Vec::new())),
                first_theme: Cell::new(PlatformThemeVariant::Light),
                settings_step: Cell::new(0),
                pane_changes: Rc::new(RefCell::new(Vec::new())),
                lifecycle: Rc::new(Lifecycle::default()),
                native_control: RefCell::new(None),
                handle_back: Rc::new(Cell::new(true)),
                back_requests: Rc::new(Cell::new(0)),
            });

            let timer = DispatcherTimer::new();
            timer.set_interval(Duration::from_millis(100));
            let _ = timer.tick({
                let state = state.clone();
                move |timer: &DispatcherTimer| {
                    step(&state);
                    if state.stage.get() == Stage::Done {
                        timer.stop();
                    }
                }
            });
            timer.start();
            // The timer lives as long as the application.
            std::mem::forget(timer);
        }

        fn enter(state: &State, stage: Stage) {
            state.stage.set(stage);
            state.since.set(Instant::now());
        }

        fn elapsed(state: &State) -> Duration {
            state.since.get().elapsed()
        }

        fn top_level(state: &State) -> Option<Ref<TopLevel>> {
            TopLevel::get_top_level(Some(&state.view.root))
        }

        fn near(pixel: [u8; 4], color: [u8; 3]) -> bool {
            (0..3).all(|i| (i32::from(pixel[i]) - i32::from(color[i])).abs() <= 3)
        }

        fn step(state: &Rc<State>) {
            match state.stage.get() {
                Stage::WaitForSurface => wait_for_surface(state),
                Stage::Dispatcher => dispatcher(state),
                Stage::Frames => frames(state),
                Stage::Input => input(state),
                Stage::Keys => keys(state),
                Stage::InputPane => input_pane(state),
                Stage::InputMethod => input_method(state),
                Stage::InputPaneClosed => input_pane_closed(state),
                Stage::NativeControl => native_control(state),
                Stage::Accessibility => accessibility(state),
                Stage::Settings => settings(state),
                Stage::Background => background(state),
                Stage::Foreground => foreground(state),
                Stage::Rotated => rotated(state),
                Stage::RotatedBack => rotated_back(state),
                Stage::Protocol => protocol(state),
                Stage::BackHandled => back_handled(state),
                Stage::BackNotHandled => back_not_handled(state),
                Stage::BackResumed => back_resumed(state),
                Stage::Finish => finish(state),
                Stage::WaitForDestroy => wait_for_destroy(state),
                Stage::Done => {}
            }
        }

        fn wait_for_surface(state: &Rc<State>) {
            let activity = FerroActivity::current_main_activity();
            let top_level = top_level(state);
            let size = top_level.as_ref().map(|top_level| top_level.client_size());
            let ready = activity.is_some() && size.is_some_and(|size| size.width > 1.0 && size.height > 1.0);
            if !ready {
                if elapsed(state) > Duration::from_secs(20) {
                    check(
                        "surface",
                        false,
                        format!(
                            "no surface after 20 s (main activity resumed: {}, top-level: {}, client size: {size:?})",
                            activity.is_some(),
                            top_level.is_some()
                        ),
                    );
                    enter(state, Stage::Finish);
                }
                return;
            }
            let (Some(activity), Some(top_level), Some(size)) = (activity, top_level, size) else {
                return;
            };

            check(
                "activity",
                activity.is_main_activity() && activity.view().is_some(),
                "the main activity was created and resumed, and has its view",
            );

            let scaling = top_level.render_scaling();
            state.scaling.set(scaling);
            let pixels = (size.width * scaling, size.height * scaling);
            check(
                "surface",
                scaling >= 1.0
                    && (pixels.0 - pixels.0.round()).abs() < 1e-6
                    && (pixels.1 - pixels.1.round()).abs() < 1e-6,
                format!(
                    "the client size is {}x{} at scaling {scaling}: a surface of {}x{} px",
                    size.width,
                    size.height,
                    pixels.0.round(),
                    pixels.1.round()
                ),
            );

            // The screen.
            match top_level.screens() {
                Some(screens) => {
                    let all = screens.all();
                    let primary = screens.primary();
                    let own = screens.screen_from_top_level(&top_level);
                    let described = own.as_ref().map(|screen| {
                        format!(
                            "'{}', bounds {:?}, working area {:?}, scaling {}, orientation {:?}",
                            screen.display_name().unwrap_or_default(),
                            screen.bounds(),
                            screen.working_area(),
                            screen.scaling(),
                            screen.current_orientation()
                        )
                    });
                    let ok = !all.is_empty()
                        && primary.is_some()
                        && own.as_ref().is_some_and(|screen| {
                            f64::from(screen.bounds().width) >= pixels.0
                                && f64::from(screen.bounds().height) >= pixels.1
                                && (screen.scaling() - scaling).abs() < 1e-6
                        });
                    check(
                        "screen",
                        ok,
                        format!(
                            "{} screen(s), a primary one: {}; the screen of the view: {}",
                            all.len(),
                            primary.is_some(),
                            described.unwrap_or_else(|| "none".to_string())
                        ),
                    );
                }
                None => check("screen", false, "the top-level has no screens"),
            }

            // The insets.
            match top_level.insets_manager() {
                Some(insets) => {
                    let padding = insets.safe_area_padding();
                    let edge_to_edge = insets.displays_edge_to_edge();
                    // A window that is displayed edge to edge has the status bar over it.
                    let ok = if edge_to_edge { padding.top > 0.0 } else { padding == Thickness::default() };
                    check(
                        "insets",
                        ok,
                        format!(
                            "displayed edge to edge: {edge_to_edge}; safe area padding {padding:?}; system bars visible: {:?}",
                            insets.is_system_bar_visible()
                        ),
                    );
                }
                None => check("insets", false, "the top-level has no insets manager"),
            }

            // The key and text events of the top-level, for the keys the script injects.
            top_level.add_handler(InputElement::key_down_event(), {
                let keys = state.keys.clone();
                move |_: &Interactive, e: &KeyEventArgs| keys.down.borrow_mut().push((e.key, e.key_symbol.clone()))
            });
            top_level.add_handler(InputElement::key_up_event(), {
                let keys = state.keys.clone();
                move |_: &Interactive, e: &KeyEventArgs| keys.up.borrow_mut().push(e.key)
            });
            top_level.add_handler(InputElement::text_input_event(), {
                let keys = state.keys.clone();
                move |_: &Interactive, e: &TextInputEventArgs| {
                    keys.text.borrow_mut().push_str(e.text.as_deref().unwrap_or_default());
                }
            });

            // The back requests of the top-level, and the activations of the application.
            top_level.add_handler(TopLevel::back_requested_event(), {
                let (handle_back, back_requests) = (state.handle_back.clone(), state.back_requests.clone());
                move |_: &Interactive, e: &RoutedEventArgs| {
                    back_requests.set(back_requests.get() + 1);
                    e.set_handled(handle_back.get());
                }
            });
            if let Some(lifetime) = FerroLocator::current().get_service::<dyn IActivatableLifetime>() {
                // The subscriptions live as long as the application.
                std::mem::forget(lifetime.activated(Rc::new({
                    let lifecycle = state.lifecycle.clone();
                    move |e: &ActivatedEventArgs| {
                        lifecycle.activated.borrow_mut().push(e.kind());
                        if let Some(protocol) = e.as_protocol_activated() {
                            lifecycle.protocols.borrow_mut().push(protocol.uri().original_string().to_string());
                        }
                    }
                })));
                std::mem::forget(lifetime.deactivated(Rc::new({
                    let lifecycle = state.lifecycle.clone();
                    move |e: &ActivatedEventArgs| lifecycle.deactivated.borrow_mut().push(e.kind())
                })));
            }

            // The dispatcher: a timer, and a job from another thread.
            let fired = state.timer_fired.clone();
            let _ = DispatcherTimer::run_once(
                move || fired.set(true),
                Duration::from_millis(50),
                DispatcherPriority::NORMAL,
            );
            let (posted, posted_on_main) = (state.posted.clone(), state.posted_on_main.clone());
            std::thread::Builder::new()
                .name("smoke-poster".to_string())
                .spawn(move || {
                    Dispatcher::ui_thread().post(
                        move || {
                            posted_on_main.store(Dispatcher::ui_thread().check_access(), Ordering::SeqCst);
                            posted.store(true, Ordering::SeqCst);
                        },
                        DispatcherPriority::NORMAL,
                    );
                })
                .expect("a thread can be started");
            enter(state, Stage::Dispatcher);
        }

        fn dispatcher(state: &Rc<State>) {
            let (fired, posted) = (state.timer_fired.get(), state.posted.load(Ordering::SeqCst));
            if !(fired && posted) && elapsed(state) < Duration::from_secs(5) {
                return;
            }
            check("dispatcher timer", fired, "a timer of 50 ms fired on the main looper");
            check(
                "dispatcher signal",
                posted && state.posted_on_main.load(Ordering::SeqCst),
                "a job posted from a thread the virtual machine did not start ran on the main thread",
            );

            aim_readback(state);
            enter(state, Stage::Frames);
        }

        /// Tells the render thread what to read back, for the layout as it is now, and
        /// forgets the frames read so far.
        fn aim_readback(state: &Rc<State>) {
            // What the render thread is asked to read back: the middle of the stripe, the
            // centre of the ellipse, a point of the background below both, and the rectangle
            // of the text.
            let view = &state.view;
            let scaling = state.scaling.get();
            let size = view.root.bounds();
            let to_pixel = |point: Point| ((point.x * scaling) as i32, (point.y * scaling) as i32);
            let ellipse_center = view
                .ellipse
                .translate_point(Point::new(ELLIPSE_SIZE / 2.0, ELLIPSE_SIZE / 2.0), &view.root)
                .unwrap_or_default();
            let text_origin = view.text.translate_point(Point::default(), &view.root).unwrap_or_default();
            let text_bounds = view.text.bounds();
            let stripe = to_pixel(Point::new(size.width / 2.0, STRIPE_HEIGHT / 2.0));
            let ellipse = to_pixel(ellipse_center);
            let background = to_pixel(Point::new(size.width / 2.0, size.height - 40.0));
            let text_at = to_pixel(text_origin);
            {
                let mut readback = view.readback.lock().unwrap_or_else(PoisonError::into_inner);
                readback.frames = 0;
                readback.colors.clear();
                readback.points = vec![
                    ("stripe", stripe.0, stripe.1),
                    ("ellipse", ellipse.0, ellipse.1),
                    ("background", background.0, background.1),
                ];
                readback.text = Some((
                    text_at.0,
                    text_at.1,
                    (text_bounds.width * scaling) as i32,
                    (text_bounds.height * scaling) as i32,
                ));
            }
            state.tap.set((ellipse.0, ellipse.1, ellipse_center));
        }

        /// The frames read back since the readback was aimed, the size of the surface
        /// they were read from, and whether the three points have the colours drawn.
        fn frames_read(state: &Rc<State>) -> (u32, (i32, i32), bool) {
            // Something changes every tick, so that frames keep coming.
            state.view.probe.invalidate_visual();

            let readback = state.view.readback.lock().unwrap_or_else(PoisonError::into_inner);
            let colors = [("stripe", STRIPE), ("ellipse", ELLIPSE), ("background", FILL)].iter().all(|(name, expected)| {
                readback.colors.iter().any(|(read_name, read)| read_name == name && near(*read, *expected))
            });
            (readback.frames, readback.surface, colors)
        }

        fn frames(state: &Rc<State>) {
            // Something changes every tick, so that frames keep coming.
            state.view.probe.invalidate_visual();

            let readback = state.view.readback.lock().unwrap_or_else(PoisonError::into_inner);
            if readback.frames < 3 && readback.error.is_none() && elapsed(state) < Duration::from_secs(15) {
                return;
            }

            check(
                "frames",
                readback.frames >= 3,
                format!(
                    "{} frame(s) drawn and read back on the thread '{}', from a Skia surface of {}x{} px ({})",
                    readback.frames,
                    readback.thread,
                    readback.surface.0,
                    readback.surface.1,
                    match (readback.gpu, OPTIONS.with(Cell::get).vulkan) {
                        (true, true) => "GPU: Vulkan",
                        (true, false) => "GPU: OpenGL ES through EGL",
                        (false, _) => "raster: the buffer of the native window",
                    }
                ),
            );
            check(
                "render thread",
                readback.thread == "Render Thread",
                format!("the frames were drawn on the thread '{}'", readback.thread),
            );
            let software = OPTIONS.with(Cell::get).software;
            check(
                "rendering mode",
                readback.frames == 0 || readback.gpu != software,
                if software {
                    "software was asked for and the frames are raster"
                } else if OPTIONS.with(Cell::get).vulkan {
                    "Vulkan was asked for and the frames are drawn on a GPU surface"
                } else {
                    "the default order gave a GPU surface"
                },
            );
            if let Some(error) = &readback.error {
                check("readback", false, error);
            }
            let color = |name: &str| readback.colors.iter().find(|(n, _)| *n == name).map(|(_, color)| *color);
            for (name, expected) in [("stripe", STRIPE), ("ellipse", ELLIPSE), ("background", FILL)] {
                let read = color(name);
                check(
                    &format!("pixel of the {name}"),
                    read.is_some_and(|read| near(read, expected)),
                    format!("read back {read:?}, drawn {expected:?}"),
                );
            }
            check(
                "text",
                readback.text_pixels > 0,
                format!(
                    "{} nearly white pixel(s) in the rectangle of the line of text {:?}",
                    readback.text_pixels, readback.text
                ),
            );
            drop(readback);

            if second_start() {
                enter(state, Stage::Finish);
                return;
            }

            // The script reads the lines that begin with SCRIPT and does what each says, in
            // order: here a picture of the screen, a tap at the pixel and a swipe from it.
            note("SCRIPT picture smoke");
            if OPTIONS.with(Cell::get).input {
                let (x, y, _) = state.tap.get();
                note(format!("SCRIPT tap {x} {y}"));
                note(format!("SCRIPT swipe {x} {y} {x} {} 400", y + 300));
                enter(state, Stage::Input);
            } else {
                enter(state, Stage::Finish);
            }
        }

        fn input(state: &Rc<State>) {
            let pointers = &state.view.pointers;
            let (pressed, released, moved) =
                (pointers.pressed.borrow().len(), pointers.released.borrow().len(), pointers.moved.get());
            // A tap, then a swipe: two presses, two releases, and moves between the second pair.
            let complete = pressed >= 2 && released >= 2 && moved >= 1;
            if !complete && elapsed(state) < Duration::from_secs(25) {
                return;
            }

            let (_, _, expected) = state.tap.get();
            let first = pointers.pressed.borrow().first().copied();
            check(
                "touch pressed",
                first.is_some_and(|point| (point.x - expected.x).abs() <= 1.5 && (point.y - expected.y).abs() <= 1.5),
                format!(
                    "the tap arrived as a pointer pressed at {first:?}; injected at the logical point {expected:?}"
                ),
            );
            check(
                "touch moved and released",
                complete,
                format!("{pressed} pressed, {moved} moved and {released} released event(s) for a tap and a swipe"),
            );

            // The key A, the text "ferro" (which the shell injects as the keys of a virtual
            // keyboard) and the enter key.
            note("SCRIPT keyevent 29");
            note("SCRIPT text ferro");
            note("SCRIPT keyevent 66");
            enter(state, Stage::Keys);
        }

        fn keys(state: &Rc<State>) {
            let keys = &state.keys;
            let complete = keys.up.borrow().contains(&Key::Enter);
            if !complete && elapsed(state) < Duration::from_secs(40) {
                return;
            }

            let down = keys.down.borrow();
            let up = keys.up.borrow();
            let text = keys.text.borrow();
            check(
                "key down and up",
                down.first() == Some(&(Key::A, Some("a".to_string()))) && up.first() == Some(&Key::A),
                format!(
                    "the key A arrived as key down {:?} and key up {:?}; {} key down and {} key up event(s) in all",
                    down.first(),
                    up.first(),
                    down.len(),
                    up.len()
                ),
            );
            let down_keys: Vec<Key> = down.iter().map(|(key, _)| *key).collect();
            check(
                "key text",
                *text == "aferro" && down_keys == [Key::A, Key::F, Key::E, Key::R, Key::R, Key::O, Key::Enter],
                format!("the text input of the keys is {:?}; the keys that went down: {down_keys:?}", *text),
            );
            check(
                "key without text",
                // A key the shell injects has no scan code, so no physical key, and the line feed
                // it produces is not a key symbol: the enter key of a keyboard has "\r".
                down.last() == Some(&(Key::Enter, None)) && !text.contains(['\r', '\n']),
                format!("the enter key went down as {:?} and raised no text", down.last()),
            );
            drop((down, up, text));

            // The input method: the editor takes the focus, which makes it the client of the
            // input method and asks for the soft keyboard.
            match top_level(state).and_then(|top_level| top_level.input_pane()) {
                Some(input_pane) => {
                    let changes = state.pane_changes.clone();
                    // The subscription lives as long as the application.
                    std::mem::forget(input_pane.state_changed(Rc::new(move |e: &InputPaneStateEventArgs| {
                        changes.borrow_mut().push((
                            e.new_state(),
                            e.end_rect(),
                            e.animation_duration(),
                            e.easing().is_some(),
                        ));
                    })));
                    let focused = state.view.editor.focus();
                    note(format!("The editor took the focus: {focused}"));
                    enter(state, Stage::InputPane);
                }
                None => {
                    check("input pane", false, "the top-level has no input pane");
                    begin_settings(state);
                }
            }
        }

        fn input_pane(state: &Rc<State>) {
            let Some(input_pane) = top_level(state).and_then(|top_level| top_level.input_pane()) else {
                return;
            };
            let rect = input_pane.occluded_rect();
            let open = input_pane.state() == InputPaneState::Open && rect.height > 0.0;
            // The animation of the insets reports the change; it starts after the state.
            let animated = state.pane_changes.borrow().iter().any(|(new_state, ..)| *new_state == InputPaneState::Open);
            if !(open && animated) && elapsed(state) < Duration::from_secs(40) {
                return;
            }
            check(
                "input pane",
                open && animated,
                format!(
                    "with the editor focused the input pane is {:?} and covers {rect:?}; changes reported (state, \
                     end, duration, easing): {:?}",
                    input_pane.state(),
                    state.pane_changes.borrow()
                ),
            );
            if !open {
                state.view.other.focus();
                begin_settings(state);
                return;
            }

            // Two taps on letters of the soft keyboard: in its second and third row of keys
            // (the pane has a strip of suggestions, three rows of letters and a bottom row).
            let scaling = state.scaling.get();
            let at = |x: f64, y: f64| {
                format!(
                    "SCRIPT tap {} {}",
                    ((rect.x + rect.width * x) * scaling).round(),
                    ((rect.y + rect.height * y) * scaling).round()
                )
            };
            note("SCRIPT picture ime");
            note(at(0.45, 0.42));
            note(at(0.65, 0.58));
            enter(state, Stage::InputMethod);
        }

        fn input_method(state: &Rc<State>) {
            let client = &state.view.client;
            let text = client.text();
            let complete = text.encode_utf16().count() >= 2;
            if !complete && elapsed(state) < Duration::from_secs(30) {
                return;
            }
            check(
                "input method",
                complete,
                format!(
                    "two keys of the soft keyboard made the text of the editor {text:?}, selection {:?}, through {} \
                     text input(s) of the input connection",
                    client.selection(),
                    client.inputs.get()
                ),
            );

            // Another element takes the focus: no client, and the soft keyboard goes.
            state.pane_changes.borrow_mut().clear();
            state.view.other.focus();
            enter(state, Stage::InputPaneClosed);
        }

        fn input_pane_closed(state: &Rc<State>) {
            let Some(input_pane) = top_level(state).and_then(|top_level| top_level.input_pane()) else {
                return;
            };
            let closed = input_pane.state() == InputPaneState::Closed && input_pane.occluded_rect().height == 0.0;
            if !closed && elapsed(state) < Duration::from_secs(30) {
                return;
            }
            check(
                "input pane closed",
                closed,
                format!(
                    "without a client the input pane is {:?} and covers {:?}; changes reported: {:?}",
                    input_pane.state(),
                    input_pane.occluded_rect(),
                    state.pane_changes.borrow()
                ),
            );
            services(state);
        }

        /// The services of the top-level that answer at once: the clipboard, the
        /// feedback, the launcher, the storage provider; then the native control host.
        fn services(state: &Rc<State>) {
            match top_level(state).and_then(|top_level| top_level.clipboard()) {
                Some(clipboard) => {
                    let text = "FerroUI \u{17c}\u{f3}\u{142}w \u{1f600}";
                    let set = now(clipboard.set_text_async(Some(text)));
                    let got = now(clipboard.try_get_text_async());
                    check(
                        "clipboard",
                        matches!(set, Some(Ok(()))) && matches!(&got, Some(Ok(Some(got))) if got == text),
                        format!("text set on the clipboard of the system ({set:?}) reads back as {got:?}"),
                    );
                    let cleared = now(clipboard.clear_async());
                    let after = now(clipboard.try_get_text_async());
                    check(
                        "clipboard cleared",
                        matches!(cleared, Some(Ok(()))) && matches!(&after, Some(Ok(None))),
                        format!("cleared ({cleared:?}) the clipboard has the text {after:?}"),
                    );
                }
                None => check("clipboard", false, "the top-level has no clipboard"),
            }

            match feature::<dyn IPlatformFeedback>(state) {
                Some(feedback) => {
                    let hold = feedback.perform(FeedbackAction::hold(), FeedbackType::Haptic);
                    let hold_sound = feedback.perform(FeedbackAction::hold(), FeedbackType::Sound);
                    let click = feedback.perform(FeedbackAction::click(), FeedbackType::Sound);
                    check(
                        "platform feedback",
                        hold && !hold_sound && click,
                        format!(
                            "a haptic hold was performed: {hold}; a hold as a sound: {hold_sound}; a click sound: {click}"
                        ),
                    );
                }
                None => check("platform feedback", false, "the top-level has no platform feedback"),
            }

            match top_level(state) {
                Some(top_level) => {
                    let uri = Uri::absolute("ferroui-smoke-nobody://nothing").expect("a URI");
                    let launched = now(top_level.launcher().launch_uri_async(&uri));
                    check(
                        "launcher",
                        launched == Some(false),
                        format!("a URI no application of the device handles was launched: {launched:?}"),
                    );

                    let provider = top_level.storage_provider();
                    let documents = now(provider.try_get_well_known_folder_async(WellKnownFolder::Documents));
                    let folder = documents.as_ref().and_then(|folder| folder.as_ref().map(|folder| folder.path().original_string().to_string()));
                    check(
                        "storage provider",
                        provider.can_open() && provider.can_save() && provider.can_pick_folder() && folder.is_some(),
                        format!(
                            "the provider can open: {}, save: {}, pick a folder: {}; the documents folder is {folder:?}",
                            provider.can_open(),
                            provider.can_save(),
                            provider.can_pick_folder()
                        ),
                    );
                }
                None => check("launcher", false, "there is no top-level"),
            }

            // The native control host: a default child, attached and shown in a rectangle.
            let host = feature::<dyn INativeControlHostImpl>(state);
            let parent = top_level(state).and_then(|top_level| top_level.platform_impl()).and_then(|i| i.handle());
            match (host, parent) {
                (Some(host), Some(parent)) => {
                    let child = host.create_default_child(parent);
                    let handle: Rc<dyn IPlatformHandle> = child.clone();
                    let compatible = host.is_compatible_with(&*handle);
                    let attachment = host.create_new_attachment(handle);
                    attachment.show_in_bounds(Rect::new(20.0, 30.0, 100.0, 50.0));
                    note(format!("A native control was attached (its handle is compatible with the host: {compatible})"));
                    *state.native_control.borrow_mut() = Some((child, attachment));
                    enter(state, Stage::NativeControl);
                }
                _ => {
                    check("native control host", false, "the top-level has no native control host");
                    begin_settings(state);
                }
            }
        }

        fn native_control(state: &Rc<State>) {
            // A layout pass of the system places the view.
            if elapsed(state) < Duration::from_millis(1500) {
                return;
            }
            let Some((child, attachment)) = state.native_control.borrow_mut().take() else {
                enter(state, Stage::Accessibility);
                return;
            };
            let view = child.as_any().downcast_ref::<AndroidViewControlHandle>().and_then(AndroidViewControlHandle::view);
            match view {
                Some(view) => {
                    let int = |name: &str| call_int(&view, name, "()I", &[]);
                    let has_parent = || call_object(&view, "getParent", "()Landroid/view/ViewParent;", &[]).is_some();
                    let scaling = state.scaling.get();
                    let expected =
                        ((20.0 * scaling) as i32, (30.0 * scaling) as i32, (100.0 * scaling) as i32, (50.0 * scaling) as i32);
                    let placed = (int("getLeft"), int("getTop"), int("getWidth"), int("getHeight"));
                    let shown = has_parent() && int("getVisibility") == 0;
                    attachment.hide_with_size(ferroui_base::Size::new(100.0, 50.0));
                    let hidden = int("getVisibility") == 8;
                    attachment.dispose();
                    let removed = !has_parent();
                    check(
                        "native control host",
                        shown && placed == expected && hidden && removed,
                        format!(
                            "a view of the system shown in (20, 30, 100, 50) is a visible child of the view: {shown}, \
                             placed at (left, top, width, height) {placed:?} px, expected {expected:?}; hidden it is \
                             gone: {hidden}; disposed it left the view: {removed}"
                        ),
                    );
                }
                None => check("native control host", false, "the default child is not a view"),
            }
            child.destroy();
            enter(state, Stage::Accessibility);
        }

        // ---- accessibility ----------------------------------------------------------------

        const NODE_INFO: &str = "Landroid/view/accessibility/AccessibilityNodeInfo;";
        const ACTION_CLICK: i32 = 16;
        const ACTION_SCROLL_FORWARD: i32 = 4096;
        const ACTION_SET_TEXT: i32 = 2097152;

        /// A node of the accessibility tree as the node provider of the view gives it.
        struct Node {
            class_name: String,
            text: String,
            content_description: String,
            /// The bounds on the screen, written as the window dump of the system writes them.
            bounds: String,
            actions: i32,
            clickable: bool,
            checkable: bool,
            checked: bool,
            editable: bool,
            visible: bool,
            /// Minimum, maximum and current value of the range, if the node has one.
            range: Option<(f32, f32, f32)>,
        }

        fn text_of(node: &JavaLocal, getter: &str) -> String {
            call_object(node, getter, "()Ljava/lang/CharSequence;", &[])
                .and_then(|text| call_object(&text, "toString", "()Ljava/lang/String;", &[]))
                .map(|text| string_of(&text))
                .unwrap_or_default()
        }

        fn node_of(provider: &JavaLocal, virtual_view_id: i32) -> Option<Node> {
            let node = call_object(
                provider,
                "createAccessibilityNodeInfo",
                &format!("(I){NODE_INFO}"),
                &[JavaValue::Int(virtual_view_id)],
            )?;
            let rect = new_object(&JavaClass::find("android/graphics/Rect"), "()V", &[]);
            let rect_ref: &dyn JavaRef = &rect;
            call_void(&node, "getBoundsInScreen", "(Landroid/graphics/Rect;)V", &[JavaValue::Object(Some(rect_ref))]);
            let bounds = call_object(&rect, "toShortString", "()Ljava/lang/String;", &[])
                .map(|text| string_of(&text))
                .unwrap_or_default();
            let range = call_object(
                &node,
                "getRangeInfo",
                "()Landroid/view/accessibility/AccessibilityNodeInfo$RangeInfo;",
                &[],
            )
            .map(|range| {
                (
                    call_float(&range, "getMin", "()F", &[]),
                    call_float(&range, "getMax", "()F", &[]),
                    call_float(&range, "getCurrent", "()F", &[]),
                )
            });
            let flag = |name: &str| call_boolean(&node, name, "()Z", &[]);
            Some(Node {
                class_name: text_of(&node, "getClassName"),
                text: text_of(&node, "getText"),
                content_description: text_of(&node, "getContentDescription"),
                bounds,
                actions: call_int(&node, "getActions", "()I", &[]),
                clickable: flag("isClickable"),
                checkable: flag("isCheckable"),
                checked: flag("isChecked"),
                editable: flag("isEditable"),
                visible: flag("isVisibleToUser"),
                range,
            })
        }

        /// The bounds the access helper gives the node of a control: the corners of its
        /// bounds in the top-level, in pixels.
        fn expected_bounds(state: &State, control: &Visual) -> String {
            let scaling = state.scaling.get();
            let size = control.bounds().size();
            let root: &Visual = &state.view.root;
            let top_left = control.translate_point(Point::new(0.0, 0.0), root).unwrap_or_default();
            let (left, top) = ((top_left.x * scaling) as i32, (top_left.y * scaling) as i32);
            let (right, bottom) =
                (((top_left.x + size.width) * scaling) as i32, ((top_left.y + size.height) * scaling) as i32);
            format!("[{left},{top}][{right},{bottom}]")
        }

        fn perform(provider: &JavaLocal, virtual_view_id: i32, action: i32, arguments: Option<&JavaLocal>) -> bool {
            call_boolean(
                provider,
                "performAction",
                "(IILandroid/os/Bundle;)Z",
                &[
                    JavaValue::Int(virtual_view_id),
                    JavaValue::Int(action),
                    JavaValue::Object(arguments.map(|arguments| arguments as &dyn JavaRef)),
                ],
            )
        }

        /// What an application can read of its own accessibility tree: the node provider of
        /// the view, the nodes of four controls and their actions; and what the script reads
        /// from outside, the window dump of the system.
        fn accessibility(state: &Rc<State>) {
            let view = FerroActivity::current_main_activity().and_then(|activity| activity.view());
            let provider = view.as_ref().and_then(|view| {
                call_object(
                    view.java_object(),
                    "getAccessibilityNodeProvider",
                    "()Landroid/view/accessibility/AccessibilityNodeProvider;",
                    &[],
                )
            });
            let (Some(view), Some(provider)) = (view, provider) else {
                check("accessibility provider", false, "the view has no accessibility node provider");
                begin_settings(state);
                return;
            };
            let main = &state.view;
            let ids = [
                view.accessibility_virtual_view_id(&main.button),
                view.accessibility_virtual_view_id(&main.check_box),
                view.accessibility_virtual_view_id(&main.slider),
                view.accessibility_virtual_view_id(&main.text_box),
            ];

            if state.accessibility_step.get() == 0 {
                // The node of the host view: the view itself, with virtual views as children.
                let host =
                    call_object(&provider, "createAccessibilityNodeInfo", &format!("(I){NODE_INFO}"), &[JavaValue::Int(-1)]);
                let children = host.as_ref().map_or(0, |host| call_int(host, "getChildCount", "()I", &[]));
                check(
                    "accessibility provider",
                    children > 0,
                    format!(
                        "the view has a node provider; the node of the view has {children} virtual child(ren); the \
                         virtual views of the button, the check box, the slider and the text box are {ids:?}"
                    ),
                );

                let controls: [(&str, &Visual, &str); 4] = [
                    ("accessibility button", &main.button, "Button"),
                    ("accessibility check box", &main.check_box, "CheckBox"),
                    ("accessibility slider", &main.slider, "Slider"),
                    ("accessibility text box", &main.text_box, "TextBox"),
                ];
                let mut dump = Vec::new();
                for (index, (name, control, class_name)) in controls.iter().enumerate() {
                    let expected = expected_bounds(state, control);
                    let Some(node) = node_of(&provider, ids[index]) else {
                        check(name, false, "the provider has no node of the virtual view");
                        continue;
                    };
                    let (specific, said) = match index {
                        0 => (
                            node.text == "Press" && node.clickable && node.actions & ACTION_CLICK != 0 && !node.checkable,
                            format!("clickable: {}", node.clickable),
                        ),
                        1 => (
                            node.text == "Agree"
                                && node.checkable
                                && !node.checked
                                && node.clickable
                                && node.actions & ACTION_CLICK != 0,
                            format!("checkable: {}, checked: {}", node.checkable, node.checked),
                        ),
                        2 => (
                            node.text == "Volume" && node.range == Some((0.0, 10.0, 4.0)),
                            format!("range (minimum, maximum, current): {:?}", node.range),
                        ),
                        _ => (
                            node.text == "NameText"
                                && node.content_description == "Your name"
                                && node.editable
                                && node.actions & ACTION_SET_TEXT != 0,
                            format!("editable: {}", node.editable),
                        ),
                    };
                    check(
                        name,
                        node.class_name == *class_name && node.bounds == expected && node.visible && specific,
                        format!(
                            "class \"{}\", text \"{}\", content description \"{}\", bounds in screen {} (the control: \
                             {expected}), visible to the user: {}, actions {:#x}, {said}",
                            node.class_name, node.text, node.content_description, node.bounds, node.visible, node.actions
                        ),
                    );
                    dump.push(expected);
                }

                // The actions. The reference gives a range no action of its own: the slider
                // refuses, and its node follows the value the application sets.
                let clicked = perform(&provider, ids[0], ACTION_CLICK, None);
                let toggled = perform(&provider, ids[1], ACTION_CLICK, None);
                let ranged = perform(&provider, ids[2], ACTION_SCROLL_FORWARD, None);
                let arguments = new_object(&JavaClass::find("android/os/Bundle"), "()V", &[]);
                call_void(
                    &arguments,
                    "putCharSequence",
                    "(Ljava/lang/String;Ljava/lang/CharSequence;)V",
                    &[JavaValue::String("ACTION_ARGUMENT_SET_TEXT_CHARSEQUENCE"), JavaValue::String("!")],
                );
                let text_set = perform(&provider, ids[3], ACTION_SET_TEXT, Some(&arguments));
                let range: &RangeBase = &main.slider;
                range.set_range_value(7.0);
                check(
                    "accessibility actions",
                    clicked && toggled && !ranged && text_set,
                    format!(
                        "performAction: a click on the button {clicked}, a click on the check box {toggled}, a scroll \
                         forward on the slider (the reference has no range action) {ranged}, set text on the text box \
                         {text_set}"
                    ),
                );

                // The script asks the system for the window dump and looks for the four
                // nodes in it, each by its text with its bounds.
                if dump.len() == 4 {
                    note(format!(
                        "SCRIPT a11y-dump Press:{} Agree:{} Volume:{} NameText!:{}",
                        dump[0], dump[1], dump[2], dump[3]
                    ));
                }
                state.accessibility_step.set(1);
                enter(state, Stage::Accessibility);
                return;
            }

            // The dump takes the script a few seconds.
            if elapsed(state) < Duration::from_secs(12) {
                return;
            }
            let check_box = node_of(&provider, ids[1]);
            let slider = node_of(&provider, ids[2]);
            let text_box = node_of(&provider, ids[3]);
            let checked = check_box.as_ref().is_some_and(|node| node.checked);
            let current = slider.as_ref().and_then(|node| node.range).map(|range| range.2);
            let text = text_box.map(|node| node.text).unwrap_or_default();
            check(
                "accessibility effects",
                main.clicks.get() == 1
                    && main.check_box.is_checked() == Some(true)
                    && checked
                    && main.text_box.text().as_deref() == Some("NameText!")
                    && text == "NameText!"
                    && current == Some(7.0),
                format!(
                    "the click raised {} click(s) of the button; the check box is checked: {:?}, and its node says \
                     {checked}; the text box has {:?} and its node \"{text}\"; the slider was set to 7 and its node \
                     has {current:?}",
                    main.clicks.get(),
                    main.check_box.is_checked(),
                    main.text_box.text()
                ),
            );
            begin_settings(state);
        }


        /// The settings of the platform, and the night mode the script switches.
        fn begin_settings(state: &Rc<State>) {
            match FerroLocator::current().get_service::<dyn IPlatformSettings>() {
                Some(settings) => {
                    let colors = settings.get_color_values();
                    let (tap, double_tap) =
                        (settings.get_tap_size(PointerType::Touch), settings.get_double_tap_size(PointerType::Touch));
                    check(
                        "platform settings",
                        colors.accent_color1().a == 0xff
                            && tap.width > 0.0
                            && double_tap.width > tap.width
                            && !settings.preferred_application_language().is_empty(),
                        format!(
                            "theme {:?}, contrast {:?}, accents {:?} {:?} {:?}; tap size {tap:?}, double tap size \
                             {double_tap:?} within {:?}, hold after {:?}; language {:?}",
                            colors.theme_variant(),
                            colors.contrast_preference(),
                            colors.accent_color1(),
                            colors.accent_color2(),
                            colors.accent_color3(),
                            settings.get_double_tap_time(PointerType::Touch),
                            settings.hold_wait_duration(),
                            settings.preferred_application_language()
                        ),
                    );
                    let changes = state.color_changes.clone();
                    // The subscription lives as long as the application.
                    std::mem::forget(settings.color_values_changed(Rc::new(move |values: &PlatformColorValues| {
                        changes.borrow_mut().push(*values);
                    })));
                    state.first_theme.set(colors.theme_variant());
                    note(format!("SCRIPT night {}", if colors.theme_variant() == PlatformThemeVariant::Dark { "no" } else { "yes" }));
                    enter(state, Stage::Settings);
                }
                None => {
                    check("platform settings", false, "no platform settings are registered");
                    enter(state, Stage::Finish);
                }
            }
        }

        fn settings(state: &Rc<State>) {
            let first = state.first_theme.get();
            let other =
                if first == PlatformThemeVariant::Dark { PlatformThemeVariant::Light } else { PlatformThemeVariant::Dark };
            let wanted = if state.settings_step.get() == 0 { other } else { first };
            let last = state.color_changes.borrow().last().map(PlatformColorValues::theme_variant);
            let arrived = last == Some(wanted);
            // The broadcast of a configuration change is slow on a device that has just booted.
            if !arrived && elapsed(state) < Duration::from_secs(60) {
                return;
            }

            let current = FerroLocator::current()
                .get_service::<dyn IPlatformSettings>()
                .map(|settings| settings.get_color_values().theme_variant());
            if state.settings_step.get() == 0 {
                check(
                    "night mode",
                    arrived && current == Some(wanted),
                    format!(
                        "the system went from {first:?} to {wanted:?}: the settings raised {} change(s), the last to \
                         {last:?}, and now answer {current:?}",
                        state.color_changes.borrow().len()
                    ),
                );
                note(format!("SCRIPT night {}", if first == PlatformThemeVariant::Dark { "yes" } else { "no" }));
                state.settings_step.set(1);
                enter(state, Stage::Settings);
            } else {
                check(
                    "night mode back",
                    arrived && current == Some(wanted),
                    format!("the system went back to {wanted:?}: the last change was to {last:?}, the settings answer {current:?}"),
                );

                // The lifecycle: the home button sends the application to the background.
                state.lifecycle.deactivated.borrow_mut().clear();
                note("SCRIPT home");
                enter(state, Stage::Background);
            }
        }

        fn background(state: &Rc<State>) {
            let deactivated = state.lifecycle.deactivated.borrow().contains(&ActivationKind::Background);
            if !deactivated && elapsed(state) < Duration::from_secs(30) {
                return;
            }
            check(
                "background",
                deactivated,
                format!(
                    "after the home button the application was deactivated: {:?}",
                    state.lifecycle.deactivated.borrow()
                ),
            );
            // The surface of a stopped activity is destroyed; the frames that are read back
            // after the activity is back are drawn to a new one.
            state.lifecycle.activated.borrow_mut().clear();
            aim_readback(state);
            note("SCRIPT resume");
            enter(state, Stage::Foreground);
        }

        fn foreground(state: &Rc<State>) {
            let activated = state.lifecycle.activated.borrow().contains(&ActivationKind::Background);
            let (frames, surface, colors) = frames_read(state);
            if !(activated && frames >= 3 && colors) && elapsed(state) < Duration::from_secs(40) {
                return;
            }
            check(
                "foreground",
                activated && frames >= 3 && colors,
                format!(
                    "back in the foreground the application was activated ({:?}) and {frames} frame(s) were drawn \
                     to the new surface of {}x{} px, with the colours drawn: {colors}",
                    state.lifecycle.activated.borrow(),
                    surface.0,
                    surface.1
                ),
            );

            // Rotation: the surface changes its size and the layout follows.
            note("SCRIPT rotate 1");
            enter(state, Stage::Rotated);
        }

        /// The client size of the top-level and the orientation of its screen.
        fn orientation(state: &Rc<State>) -> (ferroui_base::Size, String) {
            let top_level = top_level(state);
            let size = top_level.as_ref().map(|top_level| top_level.client_size()).unwrap_or_default();
            let orientation = top_level
                .as_ref()
                .and_then(|top_level| top_level.screens())
                .and_then(|screens| top_level.as_ref().and_then(|top_level| screens.screen_from_top_level(top_level)))
                .map(|screen| format!("{:?}", screen.current_orientation()))
                .unwrap_or_default();
            (size, orientation)
        }

        fn rotated(state: &Rc<State>) {
            let (size, orientation) = orientation(state);
            let landscape = size.width > size.height;
            // The readback is aimed once the layout has the new size.
            let root = state.view.root.bounds();
            if landscape && root.width > root.height && !state.aimed.replace(true) {
                aim_readback(state);
            }
            let (frames, surface, colors) = frames_read(state);
            let drawn = state.aimed.get() && frames >= 3 && colors && surface.0 > surface.1;
            if !(landscape && drawn) && elapsed(state) < Duration::from_secs(40) {
                return;
            }
            check(
                "rotation",
                landscape && drawn && orientation.contains("Landscape"),
                format!(
                    "rotated by a quarter the client size is {}x{}, the screen is {orientation}, and {frames} \
                     frame(s) were drawn to a surface of {}x{} px with the colours drawn: {colors}",
                    size.width, size.height, surface.0, surface.1
                ),
            );
            state.aimed.set(false);
            note("SCRIPT rotate 0");
            enter(state, Stage::RotatedBack);
        }

        fn rotated_back(state: &Rc<State>) {
            let (size, orientation) = orientation(state);
            let portrait = size.height > size.width;
            let root = state.view.root.bounds();
            if portrait && root.height > root.width && !state.aimed.replace(true) {
                aim_readback(state);
            }
            let (frames, surface, colors) = frames_read(state);
            let drawn = state.aimed.get() && frames >= 3 && colors && surface.1 > surface.0;
            if !(portrait && drawn) && elapsed(state) < Duration::from_secs(40) {
                return;
            }
            check(
                "rotation back",
                portrait && drawn && orientation.contains("Portrait"),
                format!(
                    "rotated back the client size is {}x{}, the screen is {orientation}, and {frames} frame(s) \
                     were drawn to a surface of {}x{} px with the colours drawn: {colors}",
                    size.width, size.height, surface.0, surface.1
                ),
            );

            // An intent with a URI for the activity that runs: a protocol activation.
            note("SCRIPT view ferroui-smoke://hello/world?answer=42");
            enter(state, Stage::Protocol);
        }

        fn protocol(state: &Rc<State>) {
            let arrived = !state.lifecycle.protocols.borrow().is_empty();
            if !arrived && elapsed(state) < Duration::from_secs(30) {
                return;
            }
            check(
                "protocol activation",
                state.lifecycle.protocols.borrow().first().map(String::as_str)
                    == Some("ferroui-smoke://hello/world?answer=42"),
                format!(
                    "a new intent with a URI activated the application with {:?} (activations: {:?})",
                    state.lifecycle.protocols.borrow(),
                    state.lifecycle.activated.borrow()
                ),
            );

            // The back button, handled by the application.
            state.handle_back.set(true);
            state.back_requests.set(0);
            state.lifecycle.deactivated.borrow_mut().clear();
            note("SCRIPT back");
            enter(state, Stage::BackHandled);
        }

        fn back_handled(state: &Rc<State>) {
            let requested = state.back_requests.get() > 0;
            // After the request, a moment in which a default action of the system would show.
            if !requested && elapsed(state) < Duration::from_secs(30) {
                return;
            }
            if requested && !state.aimed.replace(true) {
                state.since.set(Instant::now());
                return;
            }
            if requested && elapsed(state) < Duration::from_secs(3) {
                return;
            }
            state.aimed.set(false);
            let stayed =
                state.lifecycle.deactivated.borrow().is_empty() && FerroActivity::current_main_activity().is_some();
            check(
                "back handled",
                requested && stayed,
                format!(
                    "the back button raised {} back request(s) of the top-level, which were handled; the activity \
                     stayed in the foreground: {stayed}",
                    state.back_requests.get()
                ),
            );

            // The back button, not handled: the system does its default (the task of a main
            // activity goes to the background).
            state.handle_back.set(false);
            state.back_requests.set(0);
            note("SCRIPT back");
            enter(state, Stage::BackNotHandled);
        }

        fn back_not_handled(state: &Rc<State>) {
            let requested = state.back_requests.get() > 0;
            let left = !state.lifecycle.deactivated.borrow().is_empty()
                || FerroActivity::current_main_activity().is_none();
            if !(requested && left) && elapsed(state) < Duration::from_secs(30) {
                return;
            }
            check(
                "back not handled",
                requested && left,
                format!(
                    "the back button raised {} back request(s), which nobody handled; the default action of the \
                     system followed (deactivated: {:?}, main activity alive: {})",
                    state.back_requests.get(),
                    state.lifecycle.deactivated.borrow(),
                    FerroActivity::current_main_activity().is_some()
                ),
            );
            if FerroActivity::current_main_activity().is_none() {
                // The default action finished the activity: the script starts it again, and
                // the main view that is made then runs the checks of a second start.
                note("SCRIPT resume");
                enter(state, Stage::Done);
                return;
            }
            state.lifecycle.activated.borrow_mut().clear();
            aim_readback(state);
            note("SCRIPT resume");
            enter(state, Stage::BackResumed);
        }

        fn back_resumed(state: &Rc<State>) {
            let activity = FerroActivity::current_main_activity().is_some();
            let (frames, _, colors) = frames_read(state);
            if !(activity && frames >= 3 && colors) && elapsed(state) < Duration::from_secs(40) {
                return;
            }
            note(format!(
                "Back in the foreground after the back button: main activity {activity}, {frames} frame(s), colours {colors}"
            ));
            enter(state, Stage::Finish);
        }

        fn finish(state: &Rc<State>) {
            match FerroActivity::current_main_activity() {
                Some(activity) => {
                    note("Finishing the activity");
                    activity.finish();
                    enter(state, Stage::WaitForDestroy);
                }
                None => {
                    check("finish", false, "there is no main activity to finish");
                    finish_report();
                    enter(state, Stage::Done);
                }
            }
        }

        fn wait_for_destroy(state: &Rc<State>) {
            let destroyed = FerroActivity::current_main_activity().is_none();
            // The close transition of the system comes first; on an emulator it takes seconds.
            if !destroyed && elapsed(state) < Duration::from_secs(40) {
                return;
            }
            check(
                "finish",
                destroyed && top_level(state).is_none(),
                format!(
                    "the activity was destroyed: {destroyed}; the view left its top-level: {}",
                    top_level(state).is_none()
                ),
            );
            if !second_start() && OPTIONS.with(Cell::get).input {
                // The process lives on: the script starts the activity again, and the main
                // view that is made then runs the checks of a second start and the report.
                note("SCRIPT resume");
                enter(state, Stage::Done);
                return;
            }
            finish_report();
            enter(state, Stage::Done);
        }
    }
}