systemless 0.30.0

High-Level Emulation for classic Macintosh applications
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
//! Integration test exercising Toolbox Showcase for issues #1078, #1081,
//! #1264, #1265, #1266, #1267, #1268, and #1269.
#![allow(dead_code)]

use std::path::{Path, PathBuf};

use systemless::display::render_screen_with_gamma;
use systemless::game::{init_game, load_game, new_runner_with_screen_depth};
use systemless::menu_model::GuestMenuSnapshot;
use systemless::runner::{FixtureRunner, ResourceManagerSnapshot};

const SHOWCASE_SIT: &[u8] = include_bytes!("toolbox-showcase/toolbox-showcase.sit");

const MENU_APPLE: i16 = 128;
const MENU_PAGES: i16 = 129;
const MENU_STATE: i16 = 130;
const MENU_FILE: i16 = 131;
const MENU_OPTIONS: i16 = 132;

const MENU_DIFFICULTY: i16 = 140;
const MENU_SOUND: i16 = 141;
const MENU_RENDERER: i16 = 142;

/* Pages menu items */
const ITEM_PAGE_GRAPHICS: i16 = 1;
const ITEM_PAGE_CONTROLS: i16 = 2;
const ITEM_PAGE_WINDOWS: i16 = 3;
const ITEM_PAGE_DRAWING: i16 = 4;
const ITEM_PAGE_PREFERENCES: i16 = 5;
const ITEM_PAGE_DIALOGS: i16 = 6;
const ITEM_PAGE_TEXTEDIT: i16 = 7;
const ITEM_PAGE_PALETTES: i16 = 8;
const ITEM_PAGE_LISTS: i16 = 9;
const ITEM_PAGE_SOUND: i16 = 10;
const ITEM_PAGE_STYLED_TEXT: i16 = 11;
const ITEM_PAGE_STANDARD_FILE: i16 = 12;
const ITEM_PAGE_RESOURCES: i16 = 13;
const ITEM_PAGE_SPRITES: i16 = 14;

/* State menu items */
const ITEM_STATE_BUTTON: i16 = 1;
const ITEM_STATE_CHECKBOX: i16 = 2;
const ITEM_STATE_SCROLLBAR: i16 = 3;
const ITEM_STATE_AUX_WINDOW: i16 = 4;
const ITEM_STATE_SOUND_COMPLETE: i16 = 5;

/* Options menu items */
const ITEM_OPT_DIFFICULTY: i16 = 1;
const ITEM_OPT_SOUND: i16 = 2;
const ITEM_OPT_RENDERER: i16 = 3;
const ITEM_OPT_RESET_PREFS: i16 = 5;
const ITEM_OPT_LAUNCH_DIALOG: i16 = 6;

/* Difficulty submenu items */
const ITEM_DIFF_EASY: i16 = 1;
const ITEM_DIFF_NORMAL: i16 = 2;
const ITEM_DIFF_HARD: i16 = 3;

/* Sound submenu items */
const ITEM_SOUND_MUTE: i16 = 1;
const ITEM_SOUND_FX_ONLY: i16 = 2;
const ITEM_SOUND_MUSIC_ONLY: i16 = 3;
const ITEM_SOUND_FULL: i16 = 4;

/* Renderer submenu items */
const ITEM_RENDERER_FLAT: i16 = 1;
const ITEM_RENDERER_BEVEL: i16 = 2;
const ITEM_RENDERER_CONTRAST: i16 = 3;

/* File menu items */
const ITEM_FILE_PREFERENCES: i16 = 1;
const ITEM_FILE_OPTIONS: i16 = 2;
const ITEM_FILE_QUIT: i16 = 4;

/* Apple menu items */
const ITEM_APPLE_ABOUT: i16 = 1;

const REFERENCE_UPDATE_ENV: &str = "SYSTEMLESS_UPDATE_TOOLBOX_REFERENCES";

fn prefer_powerpc() -> bool {
    matches!(
        std::env::var("SYSTEMLESS_PREFER_POWERPC").ok().as_deref(),
        Some("1" | "true" | "True" | "TRUE" | "yes" | "Yes" | "YES")
    )
}

fn menu_item_checked(snapshot: &GuestMenuSnapshot, menu_id: i16, item_number: i16) -> bool {
    snapshot
        .menus
        .iter()
        .find(|menu| menu.id == menu_id)
        .and_then(|menu| menu.items.iter().find(|item| item.number == item_number))
        .map(|item| item.checked)
        .unwrap_or(false)
}

fn step_until<F>(runner: &mut FixtureRunner, label: &str, mut condition: F)
where
    F: FnMut(&mut FixtureRunner) -> bool,
{
    const BATCH_STEPS: usize = 50_000;
    const MAX_ITERATIONS: usize = 200;

    for iteration in 0..MAX_ITERATIONS {
        if condition(runner) {
            return;
        }
        let (_steps, still_running) = runner.run_steps(BATCH_STEPS, None);
        if !still_running || runner.is_halted() {
            panic!(
                "Emulation halted unexpectedly while waiting for '{label}' at iteration {iteration}:\n\
                 PC: {:08X?}\n\
                 Trap: {:04X?}\n\
                 SP: {:08X?}\n\
                 D0: {:08X?}\n\
                 ExitToShell: {}\n\
                 Total instructions: {}",
                runner.halted_pc(),
                runner.halted_trap(),
                runner.halted_sp(),
                runner.halted_d0(),
                runner.halted_by_exit_to_shell(),
                runner.total_instructions(),
            );
        }
    }
    panic!(
        "Timed out waiting for '{label}' after {} instructions",
        runner.total_instructions()
    );
}

fn step_until_gui<F>(runner: &mut FixtureRunner, label: &str, mut condition: F)
where
    F: FnMut(&mut FixtureRunner) -> bool,
{
    const BATCH_STEPS: usize = 50_000;
    const MAX_ITERATIONS: usize = 200;

    for iteration in 0..MAX_ITERATIONS {
        if condition(runner) {
            return;
        }
        let target_tick = runner.guest_tick().saturating_add(1);
        let (_steps, still_running) =
            runner.run_steps_with_audio(BATCH_STEPS, Some(target_tick), 0);
        if !still_running || runner.is_halted() {
            panic!(
                "Emulation halted unexpectedly while waiting for '{label}' at iteration {iteration}:\n\
                 PC: {:08X?}\n\
                 Trap: {:04X?}\n\
                 SP: {:08X?}\n\
                 D0: {:08X?}\n\
                 ExitToShell: {}\n\
                 Total instructions: {}",
                runner.halted_pc(),
                runner.halted_trap(),
                runner.halted_sp(),
                runner.halted_d0(),
                runner.halted_by_exit_to_shell(),
                runner.total_instructions(),
            );
        }
    }
    panic!(
        "Timed out waiting for '{label}' after {} instructions",
        runner.total_instructions()
    );
}

fn click_point(runner: &mut FixtureRunner, v: i16, h: i16) {
    runner.set_mouse_position(v, h);
    runner.push_mouse_down(v, h);
    runner.push_mouse_up(v, h);
}

fn drag_mouse(runner: &mut FixtureRunner, from_v: i16, from_h: i16, to_v: i16, to_h: i16) {
    runner.set_mouse_position(from_v, from_h);
    runner.push_mouse_down(from_v, from_h);
    run_ticks(runner, "mouse down registered", 1);
    let steps = 4;
    for step in 1..=steps {
        let v = from_v + (to_v - from_v) * step / steps;
        let h = from_h + (to_h - from_h) * step / steps;
        runner.set_mouse_position(v, h);
        run_ticks(runner, "mouse drag step", 1);
    }
    runner.push_mouse_up(to_v, to_h);
    run_ticks(runner, "mouse up registered", 1);
}

fn sound_page_rendered(runner: &mut FixtureRunner, win_top: i16, win_left: i16) -> bool {
    // SetPage updates the menu state before DrawMainWindow paints the page.
    // The right-hand lifecycle panel is only present after DrawSoundPage has
    // completed, so its border is a semantic rendering gate for page actions.
    // Include the filled panel, its heading, and the final Dispose button:
    // outer panel borders are drawn before their contents and DrawControls,
    // so border-only checks can still catch a partial frame.
    [
        (82, 420),
        (160, 420),
        (94, 318),
        (237, 420),
        (160, 305),
        (160, 534),
        (290, 153),
        (313, 153),
    ]
    .iter()
    .all(|(v, h)| {
        let [red, green, blue] = screen_rgb(runner, (win_top + v) as u16, (win_left + h) as u16);
        red < 250 || green < 250 || blue < 250
    })
}

fn screen_rgb(runner: &mut FixtureRunner, v: u16, h: u16) -> [u8; 3] {
    runner.composite_frame();
    let screen_mode = runner.dispatcher().screen_mode;
    let (_, _, width, height, _) = screen_mode;
    assert!(h < width && v < height, "sample point must be on screen");
    let rgba = render_screen_with_gamma(
        runner.bus(),
        screen_mode,
        &runner.dispatcher().device_clut,
        &runner.dispatcher().device_gamma,
    );
    let offset = (usize::from(v) * usize::from(width) + usize::from(h)) * 4;
    [rgba[offset], rgba[offset + 1], rgba[offset + 2]]
}

fn reference_path(powerpc: bool, filename: &str) -> PathBuf {
    Path::new(env!("CARGO_MANIFEST_DIR"))
        .join("tests/toolbox-showcase/reference")
        .join(if powerpc {
            "systemless-ppc"
        } else {
            "systemless-68k"
        })
        .join(filename)
}

fn update_references() -> bool {
    matches!(
        std::env::var(REFERENCE_UPDATE_ENV).ok().as_deref(),
        Some("1" | "true" | "True" | "TRUE" | "yes" | "Yes" | "YES")
    )
}

fn resource_count(snapshot: &ResourceManagerSnapshot, res_type: [u8; 4]) -> usize {
    snapshot
        .counts
        .iter()
        .find(|(candidate, _)| *candidate == res_type)
        .map(|(_, count)| *count)
        .unwrap_or(0)
}

fn assert_resource_browser_snapshot(runner: &mut FixtureRunner, loaded_id: Option<i16>) {
    let snapshot = runner.resource_manager_snapshot();
    assert_eq!(
        snapshot.current_file, 0,
        "application resource file must be current"
    );
    assert_eq!(resource_count(&snapshot, *b"DATA"), 3);
    assert_eq!(resource_count(&snapshot, *b"MENU"), 8);
    assert_eq!(resource_count(&snapshot, *b"WIND"), 1);

    let expected = [
        (201i16, "Browser Seed", 15usize),
        (202i16, "Deferred Payload", 19usize),
        (203i16, "Mutable Record", 17usize),
    ];
    assert_eq!(
        snapshot.data_entries.len(),
        expected.len(),
        "Resource Browser must enumerate exactly the three DATA records"
    );
    for (entry, (id, name, size)) in snapshot.data_entries.iter().zip(expected) {
        assert_eq!(entry.res_type, *b"DATA");
        assert_eq!(entry.id, id);
        assert_eq!(entry.name.as_deref(), Some(name));
        assert_eq!(entry.attrs, 0, "fixture DATA resources must remain clean");
        assert_eq!(entry.size, size);
        assert_eq!(entry.loaded, loaded_id == Some(id));
    }
}

fn rendered_rgb(runner: &mut FixtureRunner) -> (u32, u32, Vec<u8>) {
    runner.composite_frame();
    let screen_mode = runner.dispatcher().screen_mode;
    let (_, _, width, height, _) = screen_mode;
    let rgba = render_screen_with_gamma(
        runner.bus(),
        screen_mode,
        &runner.dispatcher().device_clut,
        &runner.dispatcher().device_gamma,
    );
    let rgb = rgba
        .chunks_exact(4)
        .flat_map(|pixel| [pixel[0], pixel[1], pixel[2]])
        .collect();
    (u32::from(width), u32::from(height), rgb)
}

fn write_rgb(path: &Path, width: u32, height: u32, rgb: Vec<u8>) {
    let image = image::RgbImage::from_raw(width, height, rgb)
        .expect("rendered RGB buffer must match its dimensions");
    image
        .save(path)
        .unwrap_or_else(|error| panic!("failed to write {}: {error}", path.display()));
}

fn assert_reference_frame(runner: &mut FixtureRunner, filename: &str) {
    let (width, height, actual) = rendered_rgb(runner);
    let reference = reference_path(runner.is_powerpc_app(), filename);

    if update_references() {
        write_rgb(&reference, width, height, actual);
        eprintln!("updated {}", reference.display());
        return;
    }

    let expected = image::open(&reference)
        .unwrap_or_else(|error| panic!("failed to read {}: {error}", reference.display()))
        .to_rgb8();
    assert_eq!(
        expected.dimensions(),
        (width, height),
        "reference dimensions differ for {}",
        reference.display()
    );
    if expected.as_raw() == &actual {
        return;
    }

    let differing_pixels = expected
        .as_raw()
        .chunks_exact(3)
        .zip(actual.chunks_exact(3))
        .filter(|(expected, actual)| expected != actual)
        .count();
    let actual_path = std::env::temp_dir().join(format!(
        "systemless-toolbox-showcase-{}-{filename}",
        if runner.is_powerpc_app() {
            "ppc"
        } else {
            "68k"
        }
    ));
    write_rgb(&actual_path, width, height, actual);
    panic!(
        "{} differs at {differing_pixels} of {} pixels; actual frame written to {}. Set {REFERENCE_UPDATE_ENV}=1 to accept new references",
        reference.display(),
        width * height,
        actual_path.display()
    );
}

fn run_ticks(runner: &mut FixtureRunner, label: &str, ticks: u32) {
    let target = runner.guest_tick().saturating_add(ticks);
    while runner.guest_tick() < target {
        let (_steps, still_running) = runner.run_steps(50_000, None);
        assert!(
            still_running && !runner.is_halted(),
            "emulation halted while waiting for {label}"
        );
    }
}

fn run_audio(runner: &mut FixtureRunner, label: &str, samples: usize) {
    let (_steps, still_running) = runner.run_steps_with_audio(50_000, None, samples);
    assert!(
        still_running && !runner.is_halted(),
        "emulation halted while waiting for {label}"
    );
}

fn assert_non_silent_audio(audio: &[u8], label: &str) {
    let minimum = *audio
        .iter()
        .min()
        .unwrap_or_else(|| panic!("{label} must contain mixed PCM samples"));
    let maximum = *audio
        .iter()
        .max()
        .unwrap_or_else(|| panic!("{label} must contain mixed PCM samples"));
    assert!(
        maximum.saturating_sub(minimum) >= 32,
        "{label} must contain a non-silent waveform, min={minimum} max={maximum}"
    );
}

fn step_until_with_audio<F>(
    runner: &mut FixtureRunner,
    label: &str,
    audio_samples: usize,
    mut condition: F,
) where
    F: FnMut(&mut FixtureRunner) -> bool,
{
    const MAX_ITERATIONS: usize = 200;

    for iteration in 0..MAX_ITERATIONS {
        if condition(runner) {
            return;
        }
        let (_steps, still_running) = runner.run_steps_with_audio(50_000, None, audio_samples);
        assert!(
            still_running && !runner.is_halted(),
            "emulation halted while waiting for {label} at iteration {iteration}"
        );
    }
    panic!(
        "Timed out waiting for '{label}' after {} instructions",
        runner.total_instructions()
    );
}

fn run_gui_ticks(runner: &mut FixtureRunner, label: &str, ticks: u32) {
    let target = runner.guest_tick().saturating_add(ticks);
    while runner.guest_tick() < target {
        let frame_target = runner.guest_tick().saturating_add(1).min(target);
        let (_steps, still_running) = runner.run_steps_with_audio(50_000, Some(frame_target), 0);
        assert!(
            still_running && !runner.is_halted(),
            "emulation halted while waiting for {label}"
        );
    }
}

fn assert_graphics_page_rendered(runner: &mut FixtureRunner) {
    // WIND 128 begins at (50, 40); this samples the center of the red oval
    // drawn at local Rect(205, 55, 325, 135).
    let [red, green, blue] = screen_rgb(runner, 145, 305);
    assert!(
        red > green.saturating_add(80) && red > blue.saturating_add(80),
        "Graphics page red oval was not rendered: rgb=({red}, {green}, {blue})"
    );
}

fn assert_drawing_page_rendered(runner: &mut FixtureRunner, win_top: i16, win_left: i16) {
    // Star polygon center at local (110, 345) -> global (win_top + 110, win_left + 345)
    // Gold fill: red/green prominent
    let [star_r, star_g, star_b] =
        screen_rgb(runner, (win_top + 110) as u16, (win_left + 345) as u16);
    assert!(
        star_r > 150 && star_g > 100 && star_b < 100,
        "Drawing page star polygon was not rendered: rgb=({star_r}, {star_g}, {star_b})"
    );

    // Pie chart arc red slice at local (105, 470)
    let [pie_r, pie_g, pie_b] = screen_rgb(runner, (win_top + 105) as u16, (win_left + 470) as u16);
    assert!(
        pie_r > pie_g.saturating_add(40) && pie_r > pie_b.saturating_add(40),
        "Drawing page pie chart red arc was not rendered: rgb=({pie_r}, {pie_g}, {pie_b})"
    );

    // Raised 3D bevel white highlight at local (50, 22)
    let [bev_r, bev_g, bev_b] = screen_rgb(runner, (win_top + 50) as u16, (win_left + 22) as u16);
    assert!(
        bev_r > 200 && bev_g > 200 && bev_b > 200,
        "Drawing page 3D bevel highlight was not rendered: rgb=({bev_r}, {bev_g}, {bev_b})"
    );

    // Sunken gauge well green bar at local (110, 175)
    let [gauge_r, gauge_g, gauge_b] =
        screen_rgb(runner, (win_top + 110) as u16, (win_left + 175) as u16);
    assert!(
        gauge_g > gauge_r.saturating_add(30) && gauge_g > gauge_b.saturating_add(30),
        "Drawing page sunken gauge bar was not rendered: rgb=({gauge_r}, {gauge_g}, {gauge_b})"
    );
}

fn region_contains_color<F>(
    runner: &mut FixtureRunner,
    top: i16,
    left: i16,
    bottom: i16,
    right: i16,
    matches: F,
) -> bool
where
    F: Fn([u8; 3]) -> bool,
{
    let (width, height, rgb) = rendered_rgb(runner);
    let top = u32::from(top.max(0) as u16).min(height);
    let left = u32::from(left.max(0) as u16).min(width);
    let bottom = u32::from(bottom.max(0) as u16).min(height);
    let right = u32::from(right.max(0) as u16).min(width);
    (top..bottom).any(|v| {
        (left..right).any(|h| {
            let offset = ((v * width + h) * 3) as usize;
            matches([rgb[offset], rgb[offset + 1], rgb[offset + 2]])
        })
    })
}

fn assert_styled_text_page_rendered(runner: &mut FixtureRunner, win_top: i16, win_left: i16) {
    // The upper well is the actual TEStyleNew output. Restrict the probes to
    // its text rectangle so a decorative legend cannot satisfy the check.
    let text_top = win_top + 76;
    let text_left = win_left + 34;
    let text_bottom = win_top + 114;
    let text_right = win_left + 521;
    assert!(
        region_contains_color(
            runner,
            text_top,
            text_left,
            text_bottom,
            text_right,
            |rgb| { rgb[2] > rgb[0].saturating_add(20) && rgb[2] > rgb[1].saturating_add(10) }
        ),
        "styled TextEdit blue run was not rendered"
    );
    assert!(
        region_contains_color(
            runner,
            text_top,
            text_left,
            text_bottom,
            text_right,
            |rgb| { rgb[0] > rgb[1].saturating_add(25) && rgb[0] > rgb[2].saturating_add(25) }
        ),
        "styled TextEdit red run was not rendered"
    );
    assert!(
        region_contains_color(
            runner,
            text_top,
            text_left,
            text_bottom,
            text_right,
            |rgb| { rgb[1] > rgb[0].saturating_add(15) && rgb[1] > rgb[2].saturating_add(15) }
        ),
        "styled TextEdit green run was not rendered"
    );
    assert!(
        region_contains_color(
            runner,
            text_top,
            text_left,
            text_bottom,
            text_right,
            |rgb| { rgb[0] > 45 && rgb[2] > 45 && rgb[1].saturating_add(20) < rgb[0].min(rgb[2]) }
        ),
        "styled TextEdit purple run was not rendered"
    );

    // These three rows are lengths returned by CharWidth, TextWidth, and
    // MeasureText, respectively. Their colors and non-zero extents are
    // generated from those API results by the fixture.
    assert!(
        region_contains_color(
            runner,
            win_top + 263,
            win_left + 399,
            win_top + 270,
            win_left + 450,
            |rgb| { rgb[2] > rgb[0].saturating_add(20) }
        ),
        "CharWidth result ruler was not rendered"
    );
    assert!(
        region_contains_color(
            runner,
            win_top + 278,
            win_left + 399,
            win_top + 285,
            win_left + 500,
            |rgb| { rgb[1] > rgb[0].saturating_add(15) }
        ),
        "TextWidth result ruler was not rendered"
    );
    assert!(
        region_contains_color(
            runner,
            win_top + 293,
            win_left + 399,
            win_top + 300,
            win_left + 500,
            |rgb| { rgb[0] > 45 && rgb[2] > 45 }
        ),
        "MeasureText result ruler was not rendered"
    );
}

fn assert_sprites_page_rendered(
    runner: &mut FixtureRunner,
    win_top: i16,
    win_left: i16,
) -> ([u8; 3], [u8; 3]) {
    // The page copies the 320×128 GWorld into local Rect(24, 80, 344, 208).
    // The first sprite's body center is world (62, 70), and its matte corner
    // at (38, 38) must leave the scene background untouched.
    let first_body = screen_rgb(
        runner,
        (win_top + 80 + 70) as u16,
        (win_left + 24 + 62) as u16,
    );
    let matte_outside = screen_rgb(
        runner,
        (win_top + 80 + 38) as u16,
        (win_left + 24 + 38) as u16,
    );
    let scene_background = screen_rgb(
        runner,
        (win_top + 80 + 8) as u16,
        (win_left + 24 + 8) as u16,
    );
    assert_ne!(
        first_body, scene_background,
        "CopyMask sprite body must differ from the offscreen scene background"
    );
    assert_eq!(
        matte_outside, scene_background,
        "CopyMask's transparent matte pixels must preserve the scene"
    );

    // The second sprite is centered at world (238, 70). Its center is both
    // inside the deep mask and inside the BitMapToRegion-derived clip.
    let second_body = screen_rgb(
        runner,
        (win_top + 80 + 70) as u16,
        (win_left + 24 + 238) as u16,
    );
    assert_ne!(
        second_body, scene_background,
        "CopyDeepMask's deep-mask center must render inside the region clip"
    );

    // SetCPixel/GetCPixel writes a red probe at world (15, 12), outside both
    // sprites, and the page copies that pixel to the visible scene.
    let probe = screen_rgb(
        runner,
        (win_top + 80 + 12) as u16,
        (win_left + 24 + 15) as u16,
    );
    assert!(
        probe[0] > probe[1].saturating_add(30),
        "SetCPixel/GetCPixel probe must retain its red component: rgb={probe:?}"
    );

    (first_body, second_body)
}

#[test]
fn test_toolbox_showcase() {
    let mut runner = new_runner_with_screen_depth(8);
    let powerpc = prefer_powerpc();
    runner
        .set_powerpc_screen_depth(if powerpc { 16 } else { 8 })
        .expect("selected PowerPC fixture screen depth must be supported");
    runner.set_app_start_time(3_786_912_000);
    runner.set_menu_bar_visible(true);

    let app = load_game(&mut runner, SHOWCASE_SIT).expect("failed to load toolbox showcase");
    assert_eq!(
        app.is_powerpc(),
        powerpc,
        "expected PowerPC executable match with SYSTEMLESS_PREFER_POWERPC"
    );

    init_game(&mut runner, &app);
    assert_eq!(
        runner.is_powerpc_app(),
        powerpc,
        "initialized runner must use the selected executable slice"
    );

    // 1. Run until menus and initial window are ready.
    step_until(&mut runner, "initial menu and window readiness", |r| {
        let snapshot = r.guest_menu_snapshot();
        let has_pages_menu = snapshot.menus.iter().any(|m| m.id == MENU_PAGES);
        let has_options_menu = snapshot.menus.iter().any(|m| m.id == MENU_OPTIONS);
        let has_window = r.window_count() >= 1;
        let has_bounds = r.window_bounds() != (0, 0, 0, 0);
        let graphics_checked = menu_item_checked(&snapshot, MENU_PAGES, ITEM_PAGE_GRAPHICS);
        has_pages_menu && has_options_menu && has_window && has_bounds && graphics_checked
    });

    let snapshot = runner.guest_menu_snapshot();
    assert!(
        menu_item_checked(&snapshot, MENU_PAGES, ITEM_PAGE_GRAPHICS),
        "initial page must be Graphics"
    );
    assert!(
        !menu_item_checked(&snapshot, MENU_PAGES, ITEM_PAGE_CONTROLS),
        "Controls page must not be checked initially"
    );
    assert!(
        !menu_item_checked(&snapshot, MENU_PAGES, ITEM_PAGE_WINDOWS),
        "Windows page must not be checked initially"
    );
    assert!(
        !menu_item_checked(&snapshot, MENU_PAGES, ITEM_PAGE_DRAWING),
        "Drawing page must not be checked initially"
    );
    assert!(
        !menu_item_checked(&snapshot, MENU_PAGES, ITEM_PAGE_PREFERENCES),
        "Preferences page must not be checked initially"
    );
    assert!(
        !menu_item_checked(&snapshot, MENU_PAGES, ITEM_PAGE_DIALOGS),
        "Dialogs page must not be checked initially"
    );
    assert!(
        !menu_item_checked(&snapshot, MENU_PAGES, ITEM_PAGE_PALETTES),
        "Palettes page must not be checked initially"
    );
    assert!(
        !menu_item_checked(&snapshot, MENU_PAGES, ITEM_PAGE_TEXTEDIT),
        "TextEdit page must not be checked initially"
    );
    assert!(
        !menu_item_checked(&snapshot, MENU_PAGES, ITEM_PAGE_LISTS),
        "Lists & Inventory page must not be checked initially"
    );
    assert!(
        !menu_item_checked(&snapshot, MENU_PAGES, ITEM_PAGE_SOUND),
        "Sound & Channels page must not be checked initially"
    );
    assert!(
        !menu_item_checked(&snapshot, MENU_STATE, ITEM_STATE_SOUND_COMPLETE),
        "Sound completion must not be checked initially"
    );
    assert!(
        !menu_item_checked(&snapshot, MENU_PAGES, ITEM_PAGE_STYLED_TEXT),
        "Styled Text & Fonts page must not be checked initially"
    );
    assert!(
        !menu_item_checked(&snapshot, MENU_PAGES, ITEM_PAGE_STANDARD_FILE),
        "Standard File page must not be checked initially"
    );
    assert!(
        !menu_item_checked(&snapshot, MENU_PAGES, ITEM_PAGE_RESOURCES),
        "Resource Browser page must not be checked initially"
    );
    assert!(
        !menu_item_checked(&snapshot, MENU_PAGES, ITEM_PAGE_SPRITES),
        "Sprites page must not be checked initially"
    );

    // Validate hierarchical menu structure in snapshot
    let file_menu = snapshot
        .menus
        .iter()
        .find(|m| m.id == MENU_FILE)
        .expect("File menu must be present in menu snapshot");
    assert!(
        file_menu.visible_in_menu_bar,
        "File menu must be visible in menu bar"
    );
    assert!(
        !file_menu.hierarchical,
        "File menu must be a top-level menu"
    );
    assert_eq!(
        file_menu.items[(ITEM_FILE_OPTIONS - 1) as usize].submenu_id,
        Some(MENU_OPTIONS),
        "Game Options must attach submenu 132"
    );

    let options_menu = snapshot
        .menus
        .iter()
        .find(|m| m.id == MENU_OPTIONS)
        .expect("Options menu must be present in menu snapshot");
    assert!(
        !options_menu.visible_in_menu_bar,
        "Options submenu must not be visible in the menu bar"
    );
    assert!(
        options_menu.hierarchical,
        "Options menu must be registered as a hierarchical submenu"
    );
    assert_eq!(
        options_menu.items[0].submenu_id,
        Some(MENU_DIFFICULTY),
        "Difficulty item must attach submenu 140"
    );
    assert_eq!(
        options_menu.items[1].submenu_id,
        Some(MENU_SOUND),
        "Sound item must attach submenu 141"
    );
    assert_eq!(
        options_menu.items[2].submenu_id,
        Some(MENU_RENDERER),
        "Renderer item must attach submenu 142"
    );

    let diff_menu = snapshot
        .menus
        .iter()
        .find(|m| m.id == MENU_DIFFICULTY)
        .expect("Difficulty submenu must be registered in snapshot");
    assert!(
        diff_menu.hierarchical,
        "Difficulty submenu must be flagged hierarchical"
    );
    assert!(
        !diff_menu.visible_in_menu_bar,
        "Difficulty submenu must not be visible on the top menu bar"
    );
    assert!(
        menu_item_checked(&snapshot, MENU_DIFFICULTY, ITEM_DIFF_NORMAL),
        "Veteran (Normal) difficulty must be checked initially"
    );
    assert!(
        menu_item_checked(&snapshot, MENU_SOUND, ITEM_SOUND_FULL),
        "Full Audio must be checked initially"
    );
    assert!(
        menu_item_checked(&snapshot, MENU_RENDERER, ITEM_RENDERER_BEVEL),
        "QD3D Bevels renderer must be checked initially"
    );

    assert_eq!(runner.window_count(), 1, "initial window count must be 1");
    assert!(
        !menu_item_checked(&snapshot, MENU_STATE, ITEM_STATE_AUX_WINDOW),
        "Auxiliary window state must be false initially"
    );
    assert_graphics_page_rendered(&mut runner);
    runner.set_mouse_position(550, 760);
    assert_reference_frame(&mut runner, "01-graphics.png");

    // 2. Switch to Controls and exercise every control.
    assert!(
        runner.select_guest_menu_item(MENU_PAGES, ITEM_PAGE_CONTROLS),
        "failed to queue selection of Controls page"
    );
    step_until(&mut runner, "switch to Controls page", |r| {
        let snapshot = r.guest_menu_snapshot();
        menu_item_checked(&snapshot, MENU_PAGES, ITEM_PAGE_CONTROLS)
    });
    let snapshot = runner.guest_menu_snapshot();
    assert!(!menu_item_checked(
        &snapshot,
        MENU_PAGES,
        ITEM_PAGE_GRAPHICS
    ));
    assert!(menu_item_checked(&snapshot, MENU_PAGES, ITEM_PAGE_CONTROLS));
    assert!(!menu_item_checked(&snapshot, MENU_PAGES, ITEM_PAGE_WINDOWS));
    assert_eq!(runner.window_count(), 1);
    assert!(!menu_item_checked(
        &snapshot,
        MENU_STATE,
        ITEM_STATE_AUX_WINDOW
    ));

    let (win_top, win_left, _win_bottom, _win_right) = runner.window_bounds();

    // Button: Rect (top=255, left=40, bottom=279, right=150)
    let button_v = win_top + (255 + 279) / 2;
    let button_h = win_left + (40 + 150) / 2;
    click_point(&mut runner, button_v, button_h);
    step_until(&mut runner, "activate button control", |r| {
        let snapshot = r.guest_menu_snapshot();
        menu_item_checked(&snapshot, MENU_STATE, ITEM_STATE_BUTTON)
    });
    let snapshot = runner.guest_menu_snapshot();
    assert!(
        menu_item_checked(&snapshot, MENU_STATE, ITEM_STATE_BUTTON),
        "Button state checkmark must be set"
    );

    // Checkbox: Rect (top=255, left=185, bottom=279, right=315)
    let checkbox_v = win_top + (255 + 279) / 2;
    let checkbox_h = win_left + (185 + 315) / 2;
    click_point(&mut runner, checkbox_v, checkbox_h);
    step_until(&mut runner, "activate checkbox control", |r| {
        let snapshot = r.guest_menu_snapshot();
        menu_item_checked(&snapshot, MENU_STATE, ITEM_STATE_CHECKBOX)
    });
    let snapshot = runner.guest_menu_snapshot();
    assert!(
        menu_item_checked(&snapshot, MENU_STATE, ITEM_STATE_CHECKBOX),
        "Checkbox state checkmark must be set"
    );

    // Scrollbar: Rect (top=310, left=40, bottom=326, right=500)
    let scrollbar_v = win_top + (310 + 326) / 2;
    let scrollbar_h = win_left + 492;
    click_point(&mut runner, scrollbar_v, scrollbar_h);
    step_until(&mut runner, "activate scrollbar control", |r| {
        let snapshot = r.guest_menu_snapshot();
        menu_item_checked(&snapshot, MENU_STATE, ITEM_STATE_SCROLLBAR)
    });
    let snapshot = runner.guest_menu_snapshot();
    assert!(
        menu_item_checked(&snapshot, MENU_STATE, ITEM_STATE_SCROLLBAR),
        "Scrollbar state checkmark must be set"
    );

    // Hold the State menu open so the visual baseline includes all three
    // completed control actions as checkmarks as well as the control values.
    runner.set_mouse_position(10, 108);
    runner.push_mouse_down(10, 108);
    run_ticks(&mut runner, "State menu to open", 4);
    assert_reference_frame(&mut runner, "02-controls.png");
    runner.push_mouse_up(10, 108);
    run_ticks(&mut runner, "State menu to close", 1);

    // 3. Switch to Windows and create the auxiliary document window.
    assert!(
        runner.select_guest_menu_item(MENU_PAGES, ITEM_PAGE_WINDOWS),
        "failed to queue selection of Windows page"
    );
    step_until(
        &mut runner,
        "switch to Windows page and open auxiliary window",
        |r| {
            let snapshot = r.guest_menu_snapshot();
            let page_checked = menu_item_checked(&snapshot, MENU_PAGES, ITEM_PAGE_WINDOWS);
            let aux_state = menu_item_checked(&snapshot, MENU_STATE, ITEM_STATE_AUX_WINDOW);
            let two_windows = r.window_count() == 2;
            page_checked && aux_state && two_windows
        },
    );
    let snapshot = runner.guest_menu_snapshot();
    assert!(!menu_item_checked(
        &snapshot,
        MENU_PAGES,
        ITEM_PAGE_GRAPHICS
    ));
    assert!(!menu_item_checked(
        &snapshot,
        MENU_PAGES,
        ITEM_PAGE_CONTROLS
    ));
    assert!(menu_item_checked(&snapshot, MENU_PAGES, ITEM_PAGE_WINDOWS));
    assert_eq!(
        runner.window_count(),
        2,
        "auxiliary window must increase window count to 2"
    );
    assert!(
        menu_item_checked(&snapshot, MENU_STATE, ITEM_STATE_AUX_WINDOW),
        "Auxiliary window state checkmark must be set"
    );
    run_ticks(&mut runner, "Windows page to settle", 1);
    runner.set_mouse_position(550, 760);
    assert_reference_frame(&mut runner, "03-windows.png");

    // 3b. Drag auxiliary window by title bar and return it
    drag_mouse(&mut runner, 145, 250, 175, 280);
    run_ticks(&mut runner, "aux window dragged", 2);
    drag_mouse(&mut runner, 175, 280, 145, 250);
    run_ticks(&mut runner, "aux window returned", 2);

    // 3c. Resize auxiliary window using its grow box and restore it
    drag_mouse(&mut runner, 292, 562, 322, 602);
    run_ticks(&mut runner, "aux window resized", 2);
    drag_mouse(&mut runner, 322, 602, 292, 562);
    run_ticks(&mut runner, "aux window resize restored", 2);

    // 4. Switch to Drawing & 3D Bevels page (disposing auxiliary window).
    let completed_qd3d_frames = runner.completed_qd3d_frame_count();
    assert!(
        runner.select_guest_menu_item(MENU_PAGES, ITEM_PAGE_DRAWING),
        "failed to queue selection of Drawing page"
    );
    step_until(&mut runner, "switch to Drawing page", |r| {
        let snapshot = r.guest_menu_snapshot();
        let page_checked = menu_item_checked(&snapshot, MENU_PAGES, ITEM_PAGE_DRAWING);
        let aux_cleared = !menu_item_checked(&snapshot, MENU_STATE, ITEM_STATE_AUX_WINDOW);
        let one_window = r.window_count() == 1;
        page_checked && aux_cleared && one_window
    });
    step_until(
        &mut runner,
        "Drawing page and native QuickDraw 3D to finish",
        |r| {
            let [red, green, blue] = screen_rgb(r, (win_top + 110) as u16, (win_left + 345) as u16);
            let drawing_finished = red > 150 && green > 100 && blue < 100;
            drawing_finished && (!powerpc || r.completed_qd3d_frame_count() > completed_qd3d_frames)
        },
    );
    let snapshot = runner.guest_menu_snapshot();
    assert!(menu_item_checked(&snapshot, MENU_PAGES, ITEM_PAGE_DRAWING));
    runner.set_mouse_position(550, 760);
    assert_reference_frame(&mut runner, "04-drawing.png");
    assert_drawing_page_rendered(&mut runner, win_top, win_left);

    // 5. Switch to Game Preferences and test bidirectional control & submenu synchronization.
    assert!(
        runner.select_guest_menu_item(MENU_PAGES, ITEM_PAGE_PREFERENCES),
        "failed to queue selection of Game Preferences page"
    );
    step_until(&mut runner, "switch to Game Preferences page", |r| {
        let snapshot = r.guest_menu_snapshot();
        menu_item_checked(&snapshot, MENU_PAGES, ITEM_PAGE_PREFERENCES)
    });

    // 5a. Click "Recruit (Easy)" radio button: local (80, 320)
    click_point(&mut runner, win_top + 80, win_left + 320);
    step_until(&mut runner, "select Easy difficulty radio", |r| {
        let snapshot = r.guest_menu_snapshot();
        menu_item_checked(&snapshot, MENU_DIFFICULTY, ITEM_DIFF_EASY)
            && !menu_item_checked(&snapshot, MENU_DIFFICULTY, ITEM_DIFF_NORMAL)
    });
    let snapshot = runner.guest_menu_snapshot();
    assert!(menu_item_checked(
        &snapshot,
        MENU_DIFFICULTY,
        ITEM_DIFF_EASY
    ));

    // 5b. Click "Nightmare (Hard)" radio button: local (130, 320)
    click_point(&mut runner, win_top + 130, win_left + 320);
    step_until(&mut runner, "select Hard difficulty radio", |r| {
        let snapshot = r.guest_menu_snapshot();
        menu_item_checked(&snapshot, MENU_DIFFICULTY, ITEM_DIFF_HARD)
            && !menu_item_checked(&snapshot, MENU_DIFFICULTY, ITEM_DIFF_EASY)
    });

    // 5c. Toggle SFX checkbox off: local (80, 122)
    click_point(&mut runner, win_top + 80, win_left + 122);
    step_until(&mut runner, "toggle SFX checkbox off", |r| {
        let snapshot = r.guest_menu_snapshot();
        menu_item_checked(&snapshot, MENU_SOUND, ITEM_SOUND_MUSIC_ONLY)
    });

    // 5d. Toggle Music checkbox off: local (105, 122)
    click_point(&mut runner, win_top + 105, win_left + 122);
    step_until(&mut runner, "toggle Music checkbox off", |r| {
        let snapshot = r.guest_menu_snapshot();
        menu_item_checked(&snapshot, MENU_SOUND, ITEM_SOUND_MUTE)
    });

    // 5e. Click "Classic 2D Flat" renderer radio: local (205, 335)
    click_point(&mut runner, win_top + 205, win_left + 335);
    step_until(&mut runner, "select Flat renderer radio", |r| {
        let snapshot = r.guest_menu_snapshot();
        menu_item_checked(&snapshot, MENU_RENDERER, ITEM_RENDERER_FLAT)
    });

    // 5f. Test hierarchical submenu command directly updates preferences state
    assert!(
        runner.select_guest_menu_item(MENU_DIFFICULTY, ITEM_DIFF_NORMAL),
        "failed to select Veteran (Normal) from Difficulty submenu"
    );
    step_until(&mut runner, "submenu select Normal difficulty", |r| {
        let snapshot = r.guest_menu_snapshot();
        menu_item_checked(&snapshot, MENU_DIFFICULTY, ITEM_DIFF_NORMAL)
    });

    assert!(
        runner.select_guest_menu_item(MENU_SOUND, ITEM_SOUND_FULL),
        "failed to select Full Audio from Sound submenu"
    );
    step_until(&mut runner, "submenu select Full Audio", |r| {
        let snapshot = r.guest_menu_snapshot();
        menu_item_checked(&snapshot, MENU_SOUND, ITEM_SOUND_FULL)
    });

    assert!(
        runner.select_guest_menu_item(MENU_RENDERER, ITEM_RENDERER_BEVEL),
        "failed to select QD3D Bevels from Renderer submenu"
    );
    step_until(&mut runner, "submenu select QD3D Bevels", |r| {
        let snapshot = r.guest_menu_snapshot();
        menu_item_checked(&snapshot, MENU_RENDERER, ITEM_RENDERER_BEVEL)
    });

    // Exercise volume scrollbar: step left arrow, drag thumb, and step right arrow
    click_point(&mut runner, win_top + 203, win_left + 43);
    run_ticks(&mut runner, "step volume left", 1);
    drag_mouse(
        &mut runner,
        win_top + 203,
        win_left + 158,
        win_top + 203,
        win_left + 110,
    );
    run_ticks(&mut runner, "drag volume thumb left", 1);
    drag_mouse(
        &mut runner,
        win_top + 203,
        win_left + 110,
        win_top + 203,
        win_left + 158,
    );
    run_ticks(&mut runner, "drag volume thumb right", 1);
    click_point(&mut runner, win_top + 203, win_left + 207);
    run_ticks(&mut runner, "Preferences page to settle", 1);
    runner.set_mouse_position(550, 760);
    assert_reference_frame(&mut runner, "05-preferences.png");

    // 6. Hold open the nested File > Game Options parent so the hierarchical
    // indicator is part of the visual baseline, then exercise a leaf command.
    runner.set_mouse_position(10, 146);
    runner.push_mouse_down(10, 146);
    run_ticks(&mut runner, "File menu to open", 2);
    runner.set_mouse_position(49, 170);
    run_ticks(&mut runner, "Game Options parent to highlight", 4);
    assert_reference_frame(&mut runner, "06-nested-menus.png");
    runner.push_mouse_up(49, 170);
    run_ticks(&mut runner, "File menu to close", 1);
    assert!(runner.select_guest_menu_item(MENU_RENDERER, ITEM_RENDERER_CONTRAST));
    step_until(&mut runner, "nested menu select High Contrast", |r| {
        menu_item_checked(
            &r.guest_menu_snapshot(),
            MENU_RENDERER,
            ITEM_RENDERER_CONTRAST,
        )
    });

    // Restore QD3D Bevels through the deterministic semantic selector.
    assert!(runner.select_guest_menu_item(MENU_RENDERER, ITEM_RENDERER_BEVEL));
    step_until(&mut runner, "restore QD3D Bevels renderer", |r| {
        menu_item_checked(&r.guest_menu_snapshot(), MENU_RENDERER, ITEM_RENDERER_BEVEL)
    });

    // 7. Switch to Dialogs & Alerts page and exercise modal dialog and alert sessions.
    assert!(
        runner.select_guest_menu_item(MENU_PAGES, ITEM_PAGE_DIALOGS),
        "failed to queue selection of Dialogs page"
    );
    step_until(&mut runner, "switch to Dialogs page", |r| {
        let snapshot = r.guest_menu_snapshot();
        menu_item_checked(&snapshot, MENU_PAGES, ITEM_PAGE_DIALOGS)
    });

    // 6a. Open Modal Preferences Dialog via button: local (317, 130)
    click_point(&mut runner, win_top + 317, win_left + 130);
    run_ticks(&mut runner, "Modal preferences dialog to open", 2);
    assert_reference_frame(&mut runner, "07-modal-dialog.png");
    // Dialog bounds: {100, 130, 290, 470}
    // Click Checkbox item 4 (Enable 3D): global (155, 300)
    click_point(&mut runner, 155, 300);
    // Click OK button item 1: global (260, 405)
    click_point(&mut runner, 260, 405);
    run_ticks(&mut runner, "Modal dialog to close", 2);

    // 6b. Display About Alert via button: local (317, 325)
    click_point(&mut runner, win_top + 317, win_left + 325);
    run_ticks(&mut runner, "About alert to open", 2);
    assert_reference_frame(&mut runner, "08-alert.png");
    // Alert bounds: {130, 150, 260, 450}
    // Click OK button item 1: global (230, 395)
    click_point(&mut runner, 230, 395);
    run_ticks(&mut runner, "Alert to close", 2);

    runner.set_mouse_position(550, 760);
    assert_reference_frame(&mut runner, "09-dialogs.png");

    // 10. Switch to TextEdit page and exercise live buffer, justification & layout.
    assert!(
        runner.select_guest_menu_item(MENU_PAGES, ITEM_PAGE_TEXTEDIT),
        "failed to queue selection of TextEdit page"
    );
    step_until(&mut runner, "switch to TextEdit page", |r| {
        menu_item_checked(&r.guest_menu_snapshot(), MENU_PAGES, ITEM_PAGE_TEXTEDIT)
    });
    // Click Center alignment radio: local (178, 445)
    click_point(&mut runner, win_top + 178, win_left + 445);
    run_ticks(&mut runner, "center alignment to settle", 2);

    runner.set_mouse_position(550, 760);
    assert_reference_frame(&mut runner, "10-textedit.png");

    // 11. Activate a mixed-usage palette, draw with palette entries, then
    // animate three explicit device indexes without redrawing the swatches.
    assert!(
        runner.select_guest_menu_item(MENU_PAGES, ITEM_PAGE_PALETTES),
        "failed to queue selection of Palettes page"
    );
    step_until(&mut runner, "switch to Palettes page", |r| {
        menu_item_checked(&r.guest_menu_snapshot(), MENU_PAGES, ITEM_PAGE_PALETTES)
    });
    step_until(&mut runner, "indexed PICT transfer to render", |r| {
        screen_rgb(r, (win_top + 280) as u16, (win_left + 340) as u16) != [255, 255, 255]
    });
    let indexed_picture_rgb = [340, 365, 382, 400, 421, 450, 480, 500, 520]
        .map(|x| screen_rgb(&mut runner, (win_top + 280) as u16, (win_left + x) as u16));
    assert_eq!(
        indexed_picture_rgb,
        if powerpc {
            [
                [49, 255, 49],
                [49, 206, 49],
                [99, 206, 49],
                [99, 206, 99],
                [99, 156, 99],
                [156, 156, 99],
                [156, 99, 99],
                [206, 99, 156],
                [206, 49, 49],
            ]
        } else {
            [
                [84, 255, 84],
                [84, 218, 84],
                [135, 218, 84],
                [37, 27, 138],
                [135, 179, 135],
                [179, 179, 135],
                [179, 135, 135],
                [218, 135, 179],
                [218, 84, 84],
            ]
        },
        "DrawPicture and CopyBits must preserve the exact architecture-specific indexed PICT color sequence across CTables"
    );
    let initial_device_rgb =
        screen_rgb(&mut runner, (win_top + 130) as u16, (win_left + 100) as u16);
    assert_ne!(
        initial_device_rgb,
        [0, 0, 0],
        "mixed-usage palette must populate the indexed device CLUT with non-zero RGB"
    );
    let sample_x = win_left + 350;
    let sample_y = win_top + 130;
    let pict_band_sample = screen_rgb(&mut runner, sample_y as u16, sample_x as u16);
    assert!(
        pict_band_sample != [0, 0, 0],
        "indexed PICT band must resolve to non-black indexed pixels via offscreen GWorld and destination palette"
    );
    let same_device_left = screen_rgb(&mut runner, (win_top + 310) as u16, (win_left + 345) as u16);
    let same_device_middle =
        screen_rgb(&mut runner, (win_top + 310) as u16, (win_left + 410) as u16);
    let same_device_right =
        screen_rgb(&mut runner, (win_top + 310) as u16, (win_left + 505) as u16);
    assert!(
        same_device_left != [0, 0, 0]
            && same_device_middle != [0, 0, 0]
            && same_device_right != [0, 0, 0],
        "same-device indexed CopyBits must not remap a transient black device CTable to black"
    );
    assert!(
        same_device_left != same_device_middle
            && same_device_middle != same_device_right
            && same_device_left != same_device_right,
        "same-device indexed CopyBits must preserve three distinct positional device indexes"
    );
    let inverse_table_band =
        screen_rgb(&mut runner, (win_top + 331) as u16, (win_left + 450) as u16);
    if powerpc {
        assert!(
            inverse_table_band[0] < 16
                && inverse_table_band[1] < 16
                && inverse_table_band[2] > 80,
            "direct-color RGBForeColor must render the requested dark blue; got {inverse_table_band:?}"
        );
    } else {
        assert_eq!(
            inverse_table_band,
            [154, 147, 161],
            "8-bit RGBForeColor must use the screen GDevice inverse-table index and default gamma transfer when logical and hardware CLUTs differ"
        );
    }
    let initial_palette_rgb = [
        screen_rgb(&mut runner, (win_top + 130) as u16, (win_left + 100) as u16),
        screen_rgb(&mut runner, (win_top + 130) as u16, (win_left + 280) as u16),
        screen_rgb(&mut runner, (win_top + 130) as u16, (win_left + 440) as u16),
    ];
    assert_eq!(
        initial_palette_rgb,
        if powerpc {
            [[255, 255, 0], [255, 99, 0], [0, 206, 173]]
        } else {
            [[255, 255, 0], [255, 135, 0], [0, 218, 192]]
        },
        "initial palette swatches must retain their exact architecture-specific RGB values"
    );
    runner.set_mouse_position(550, 760);
    assert_reference_frame(&mut runner, "11-palette.png");

    // 12. Animate Palette button: local Rect (342, 40, 366, 230).
    click_point(&mut runner, win_top + 354, win_left + 135);
    run_ticks(&mut runner, "palette animation to settle", 1);
    let animated_palette_rgb = [
        screen_rgb(&mut runner, (win_top + 130) as u16, (win_left + 100) as u16),
        screen_rgb(&mut runner, (win_top + 130) as u16, (win_left + 280) as u16),
        screen_rgb(&mut runner, (win_top + 130) as u16, (win_left + 440) as u16),
    ];
    if powerpc {
        assert_eq!(
            animated_palette_rgb, initial_palette_rgb,
            "AnimateEntry must leave already-drawn pixels unchanged on a direct-color device"
        );
    } else {
        assert_eq!(
            animated_palette_rgb,
            [[255, 39, 179], [39, 231, 119], [63, 119, 255]],
            "AnimateEntry must apply the exact replacement CLUT colors without a redraw"
        );
    }
    runner.set_mouse_position(550, 760);
    assert_reference_frame(&mut runner, "12-palette-animated.png");

    // 13. Menu-bar hover selection: press mouse down in File, hover/drag
    // to Pages menu, and release over the Graphics item to select it.
    runner.set_mouse_position(10, 146); // Options menu bar cell
    runner.push_mouse_down(10, 146);
    run_ticks(&mut runner, "Menu tracking start", 2);

    runner.set_mouse_position(10, 56); // Drag across menu bar to Pages menu
    run_ticks(&mut runner, "Menu tracking hover to Pages", 2);

    runner.set_mouse_position(28, 56); // Hover down to Graphics item
    run_ticks(&mut runner, "Menu tracking hover to Graphics item", 2);
    assert_reference_frame(&mut runner, "13-menu-hover.png");
    runner.push_mouse_up(28, 56); // Release mouse to trigger selection

    step_until(&mut runner, "hover-select switch to Graphics page", |r| {
        let snapshot = r.guest_menu_snapshot();
        let graphics_checked = menu_item_checked(&snapshot, MENU_PAGES, ITEM_PAGE_GRAPHICS);
        let aux_cleared = !menu_item_checked(&snapshot, MENU_STATE, ITEM_STATE_AUX_WINDOW);
        let one_window = r.window_count() == 1;
        graphics_checked && aux_cleared && one_window
    });

    let snapshot = runner.guest_menu_snapshot();
    assert!(menu_item_checked(&snapshot, MENU_PAGES, ITEM_PAGE_GRAPHICS));
    assert!(!menu_item_checked(
        &snapshot,
        MENU_PAGES,
        ITEM_PAGE_CONTROLS
    ));
    assert!(!menu_item_checked(&snapshot, MENU_PAGES, ITEM_PAGE_WINDOWS));
    assert!(!menu_item_checked(&snapshot, MENU_PAGES, ITEM_PAGE_DRAWING));
    assert!(!menu_item_checked(
        &snapshot,
        MENU_PAGES,
        ITEM_PAGE_PREFERENCES
    ));
    assert!(!menu_item_checked(&snapshot, MENU_PAGES, ITEM_PAGE_DIALOGS));
    assert!(!menu_item_checked(
        &snapshot,
        MENU_PAGES,
        ITEM_PAGE_PALETTES
    ));
    assert!(!menu_item_checked(
        &snapshot,
        MENU_PAGES,
        ITEM_PAGE_TEXTEDIT
    ));
    assert!(!menu_item_checked(&snapshot, MENU_PAGES, ITEM_PAGE_LISTS));
    assert_eq!(
        runner.window_count(),
        1,
        "window count must remain 1 after returning to Graphics"
    );
    assert!(
        !menu_item_checked(&snapshot, MENU_STATE, ITEM_STATE_AUX_WINDOW),
        "Auxiliary window state checkmark must remain cleared"
    );
    run_ticks(&mut runner, "returned Graphics page to settle", 1);
    assert_graphics_page_rendered(&mut runner);
    runner.set_mouse_position(550, 760);
    assert_reference_frame(&mut runner, "14-graphics-return.png");

    // 15. Switch to Lists & Inventory and exercise selection, cell access,
    // mutation, scrolling, resizing, and activation through the List Manager.
    assert!(
        runner.select_guest_menu_item(MENU_PAGES, ITEM_PAGE_LISTS),
        "failed to queue selection of Lists & Inventory page"
    );
    step_until(&mut runner, "switch to Lists & Inventory page", |r| {
        menu_item_checked(&r.guest_menu_snapshot(), MENU_PAGES, ITEM_PAGE_LISTS)
    });
    let snapshot = runner.guest_menu_snapshot();
    assert!(menu_item_checked(&snapshot, MENU_PAGES, ITEM_PAGE_LISTS));
    assert!(!menu_item_checked(
        &snapshot,
        MENU_PAGES,
        ITEM_PAGE_GRAPHICS
    ));
    assert!(!menu_item_checked(&snapshot, MENU_PAGES, ITEM_PAGE_SOUND));

    runner.set_mouse_position(550, 760);
    assert_reference_frame(&mut runner, "15-lists.png");

    // Select row 8 (zero-based row 7) in the initial 18-pixel cell layout.
    // The sample is in the row's empty right-hand background, so it observes
    // the List Manager's selection highlight rather than text rasterization.
    let list_row_v = win_top + 78 + (7 * 18) + 9;
    let list_row_h = win_left + 24 + 480;
    let unselected_row_rgb = screen_rgb(&mut runner, list_row_v as u16, list_row_h as u16);
    click_point(&mut runner, list_row_v, list_row_h);
    step_until(&mut runner, "select inventory row", |r| {
        screen_rgb(r, list_row_v as u16, list_row_h as u16) != unselected_row_rgb
    });

    // Inspect Selection calls LGetSelect/LGetCell again and keeps the same
    // selected-cell result visible in the inspector readout.
    click_point(&mut runner, win_top + 254, win_left + 80);
    run_ticks(&mut runner, "inspect selected inventory row", 1);
    runner.set_mouse_position(550, 760);
    let selected_frame = rendered_rgb(&mut runner).2;

    // Update Selected Row appends data obtained through LGetCell and writes
    // it back with LSetCell.
    click_point(&mut runner, win_top + 254, win_left + 205);
    run_ticks(&mut runner, "mutate selected inventory row", 1);
    runner.set_mouse_position(550, 760);
    let mutated_frame = rendered_rgb(&mut runner).2;
    assert_ne!(
        selected_frame, mutated_frame,
        "LSetCell mutation must change the rendered list/status state"
    );

    // The four-row request is intentionally bounded by List Manager data
    // bounds when the visible page cannot expose a full four-row displacement.
    click_point(&mut runner, win_top + 254, win_left + 334);
    run_ticks(&mut runner, "scroll inventory list", 1);
    runner.set_mouse_position(550, 760);
    let scrolled_frame = rendered_rgb(&mut runner).2;
    assert_ne!(
        mutated_frame, scrolled_frame,
        "LScroll must change the rendered list/status state"
    );

    click_point(&mut runner, win_top + 254, win_left + 442);
    run_ticks(&mut runner, "resize inventory list", 1);
    runner.set_mouse_position(550, 760);
    let resized_frame = rendered_rgb(&mut runner).2;
    assert_ne!(
        scrolled_frame, resized_frame,
        "LSize must change the rendered list/status state"
    );

    // Toggle activation off and on so both LActivate paths are exercised.
    click_point(&mut runner, win_top + 286, win_left + 97);
    run_ticks(&mut runner, "deactivate inventory list", 1);
    runner.set_mouse_position(550, 760);
    let inactive_frame = rendered_rgb(&mut runner).2;
    assert_ne!(
        resized_frame, inactive_frame,
        "LActivate(FALSE) must change the rendered list/status state"
    );

    click_point(&mut runner, win_top + 286, win_left + 97);
    run_ticks(&mut runner, "reactivate inventory list", 1);
    runner.set_mouse_position(550, 760);
    let active_frame = rendered_rgb(&mut runner).2;
    assert_ne!(
        inactive_frame, active_frame,
        "LActivate(TRUE) must change the rendered list/status state"
    );
    assert_reference_frame(&mut runner, "16-lists-interacted.png");

    // 17. Activate Sound & Channels and exercise the high- and low-level
    // Sound Manager paths, including deterministic PCM output.
    assert!(
        runner.select_guest_menu_item(MENU_PAGES, ITEM_PAGE_SOUND),
        "failed to queue selection of Sound & Channels page"
    );
    let mut sound_page_rendered_streak = 0;
    step_until(&mut runner, "switch to Sound & Channels page", |r| {
        let ready = menu_item_checked(&r.guest_menu_snapshot(), MENU_PAGES, ITEM_PAGE_SOUND)
            && sound_page_rendered(r, win_top, win_left);
        if ready {
            sound_page_rendered_streak += 1;
        } else {
            sound_page_rendered_streak = 0;
        }
        // One frame can observe the panel/control drawing between guest
        // toolbox calls. Require the same semantic state after another slice
        // so the page's idle redraw has completed before input is sent.
        sound_page_rendered_streak >= 2
    });
    // Let the guest finish the redraw after the last semantic sample; the
    // condition itself is evaluated before the next emulation slice.
    run_ticks(&mut runner, "finish Sound & Channels page redraw", 1);
    step_until(&mut runner, "render Sound & Channels controls", |r| {
        sound_page_rendered(r, win_top, win_left)
    });
    assert!(
        sound_page_rendered(&mut runner, win_top, win_left),
        "Sound & Channels controls must be rendered before interaction"
    );
    assert_eq!(
        runner.dispatcher().sound_manager().channels.len(),
        1,
        "entering Sound & Channels must allocate one channel"
    );
    let sound_snapshot = runner.guest_menu_snapshot();
    assert!(
        !menu_item_checked(&sound_snapshot, MENU_STATE, ITEM_STATE_SOUND_COMPLETE),
        "sound completion must start unchecked"
    );
    runner.set_mouse_position(550, 760);
    assert_reference_frame(&mut runner, "17-sound-controls.png");

    // SysBeep uses the Sound Manager's internal alert channel and must produce
    // captured PCM without changing the page's explicitly allocated channel.
    click_point(&mut runner, win_top + 267, win_left + 62);
    run_audio(&mut runner, "SysBeep output", 4096);
    let beep_audio = runner.drain_audio();
    assert_non_silent_audio(&beep_audio, "SysBeep output");
    assert_eq!(
        runner.dispatcher().sound_manager().channels.len(),
        1,
        "completed SysBeep must release its temporary channel"
    );

    // SndPlay decodes the resource-backed format-1 sample into bufferCmd
    // playback. Queue a volume command and callback behind the sample, then
    // exercise immediate flush and quiet. The immediate pair intentionally
    // cancels the queued callback; the completion pass below queues a fresh
    // callback and verifies its guest-visible effect.
    click_point(&mut runner, win_top + 267, win_left + 157);
    run_audio(&mut runner, "SndPlay output", 16);
    let play_audio = runner.drain_audio();
    assert_non_silent_audio(&play_audio, "SndPlay output");
    assert!(
        runner.dispatcher().sound_manager().debug_buffer_cmd_count >= 1,
        "SndPlay must submit the resource's bufferCmd"
    );

    click_point(&mut runner, win_top + 267, win_left + 262);
    run_audio(&mut runner, "queued Sound Manager commands", 16);
    click_point(&mut runner, win_top + 267, win_left + 352);
    run_audio(&mut runner, "immediate flush", 4);
    click_point(&mut runner, win_top + 267, win_left + 422);
    run_audio(&mut runner, "immediate quiet", 4);
    let sound_manager = runner.dispatcher().sound_manager();
    assert!(
        sound_manager.debug_cmd_codes_seen.contains(&46),
        "queued volumeCmd must reach the Sound Manager"
    );
    assert!(
        sound_manager.debug_cmd_codes_seen.contains(&4),
        "immediate flushCmd must reach the Sound Manager"
    );
    assert!(
        sound_manager.debug_cmd_codes_seen.contains(&3),
        "immediate quietCmd must reach the Sound Manager"
    );
    // Play again and wait for a fresh callback flag to be reflected in the
    // State menu. The audio assertion covers the host PCM boundary; the menu
    // assertion covers guest-visible completion semantics.
    click_point(&mut runner, win_top + 302, win_left + 82);
    step_until_with_audio(&mut runner, "Sound Manager callback completion", 512, |r| {
        menu_item_checked(
            &r.guest_menu_snapshot(),
            MENU_STATE,
            ITEM_STATE_SOUND_COMPLETE,
        )
    });
    let completion_audio = runner.drain_audio();
    assert_non_silent_audio(&completion_audio, "completed SndPlay output");
    let sound_manager = runner.dispatcher().sound_manager();
    assert!(
        sound_manager.debug_buffer_cmd_count >= 2,
        "completion pass must submit a second resource bufferCmd"
    );
    assert!(
        sound_manager.debug_samples_mixed > 0,
        "Sound Manager playback must contribute mixed samples"
    );
    assert!(
        sound_manager.pending_sound_callbacks.is_empty(),
        "completion callback must be consumed"
    );
    assert!(
        sound_manager
            .channels
            .iter()
            .all(|channel| !channel.has_active_playback()),
        "completion pass must leave the channel idle"
    );
    let sound_snapshot = runner.guest_menu_snapshot();
    assert!(menu_item_checked(
        &sound_snapshot,
        MENU_STATE,
        ITEM_STATE_SOUND_COMPLETE
    ));

    // Hold State open so the callback checkmark is part of the exact frame,
    // then dispose the explicitly allocated channel through its button.
    runner.set_mouse_position(10, 108);
    runner.push_mouse_down(10, 108);
    run_ticks(&mut runner, "State menu to show sound completion", 4);
    assert_reference_frame(&mut runner, "18-sound-complete.png");
    runner.push_mouse_up(10, 108);
    run_ticks(&mut runner, "State menu to close after sound completion", 1);
    click_point(&mut runner, win_top + 302, win_left + 200);
    step_until(&mut runner, "dispose Sound Manager channel", |r| {
        r.dispatcher().sound_manager().channels.is_empty()
    });
    assert!(
        menu_item_checked(
            &runner.guest_menu_snapshot(),
            MENU_STATE,
            ITEM_STATE_SOUND_COMPLETE
        ),
        "sound completion checkmark must survive channel disposal"
    );

    // 19. Switch to Styled Text & Fonts and verify the live styled TextEdit,
    // Font Manager lookups, and measurement rulers.
    assert!(
        runner.select_guest_menu_item(MENU_PAGES, ITEM_PAGE_STYLED_TEXT),
        "failed to queue selection of Styled Text & Fonts page"
    );
    step_until(&mut runner, "switch to Styled Text & Fonts page", |r| {
        menu_item_checked(&r.guest_menu_snapshot(), MENU_PAGES, ITEM_PAGE_STYLED_TEXT)
    });
    let snapshot = runner.guest_menu_snapshot();
    assert!(menu_item_checked(
        &snapshot,
        MENU_PAGES,
        ITEM_PAGE_STYLED_TEXT
    ));
    assert!(!menu_item_checked(&snapshot, MENU_PAGES, ITEM_PAGE_LISTS));
    run_ticks(&mut runner, "Styled Text & Fonts page to settle", 1);
    assert_styled_text_page_rendered(&mut runner, win_top, win_left);
    runner.set_mouse_position(550, 760);
    assert_reference_frame(&mut runner, "19-styled-text.png");

    // 20. Exercise Standard File's modern and legacy Open/Save entry points.
    // The archive contains a folder with one TEXT and one DATA file. The
    // modern Open filter must enter the folder and return the TEXT FSSpec;
    // the other paths prove cancellation and editable Save names.
    assert!(
        runner.select_guest_menu_item(MENU_PAGES, ITEM_PAGE_STANDARD_FILE),
        "failed to queue selection of Standard File page"
    );
    step_until(&mut runner, "switch to Standard File page", |r| {
        menu_item_checked(
            &r.guest_menu_snapshot(),
            MENU_PAGES,
            ITEM_PAGE_STANDARD_FILE,
        )
    });
    runner.set_mouse_position(550, 760);
    let page_dialog_sample = screen_rgb(&mut runner, 212, 223);
    let page_save_dialog_sample = screen_rgb(&mut runner, 227, 221);
    let legacy_get_sample_point = if powerpc { (100, 100) } else { (50, 0) };
    let legacy_save_sample_point = if powerpc { (100, 100) } else { (227, 221) };
    let page_legacy_get_sample = screen_rgb(
        &mut runner,
        legacy_get_sample_point.0,
        legacy_get_sample_point.1,
    );
    let page_legacy_save_sample = screen_rgb(
        &mut runner,
        legacy_save_sample_point.0,
        legacy_save_sample_point.1,
    );
    let legacy_get_sample = legacy_get_sample_point;
    assert_reference_frame(&mut runner, "20-standard-file-page.png");

    // Modern StandardGetFile: the fixture folder is the selected first row.
    click_point(&mut runner, win_top + 216, win_left + 86);
    runner.set_mouse_position(550, 760);
    step_until_gui(&mut runner, "modern StandardGetFile dialog", |r| {
        screen_rgb(r, 212, 223) != page_dialog_sample
    });
    runner.set_mouse_position(550, 760);
    assert_reference_frame(&mut runner, "21-standard-file-open.png");

    // Open the selected folder, then accept its filtered TEXT row with Return.
    click_point(&mut runner, 211 + 148, 222 + 298);
    runner.set_mouse_position(550, 760);
    run_gui_ticks(&mut runner, "StandardGetFile folder navigation", 1);
    runner.push_key_down(0x24, b'\r');
    runner.push_key_up(0x24, b'\r');
    step_until_gui(&mut runner, "modern StandardGetFile acceptance", |r| {
        screen_rgb(r, 212, 223) == page_dialog_sample
    });

    // Legacy SFGetFile: cancel from a filtered dialog and verify the page
    // returns to its normal framebuffer before the next operation.
    click_point(&mut runner, win_top + 216, win_left + 226);
    runner.set_mouse_position(550, 760);
    step_until_gui(&mut runner, "legacy SFGetFile dialog", |r| {
        screen_rgb(r, legacy_get_sample.0, legacy_get_sample.1) != page_legacy_get_sample
    });
    runner.push_key_down(0x35, 0);
    runner.push_key_up(0x35, 0);
    step_until_gui(&mut runner, "legacy SFGetFile cancellation", |r| {
        screen_rgb(r, legacy_get_sample.0, legacy_get_sample.1) == page_legacy_get_sample
    });

    // Modern StandardPutFile: the default name is selected, so typing one
    // character replaces it; Return accepts and the app consumes the FSSpec
    // through FSpCreate/FSpDelete.
    click_point(&mut runner, win_top + 216, win_left + 360);
    runner.set_mouse_position(550, 760);
    step_until_gui(&mut runner, "modern StandardPutFile dialog", |r| {
        screen_rgb(r, 227, 221) != page_save_dialog_sample
    });
    runner.push_key_down(0x00, b'S');
    runner.push_key_up(0x00, b'S');
    runner.push_key_down(0x24, b'\r');
    runner.push_key_up(0x24, b'\r');
    step_until_gui(&mut runner, "modern StandardPutFile acceptance", |r| {
        screen_rgb(r, 227, 221) == page_save_dialog_sample
    });

    // Legacy SFPutFile: cancellation must leave its SFReply good bit false.
    click_point(&mut runner, win_top + 216, win_left + 480);
    runner.set_mouse_position(550, 760);
    step_until_gui(&mut runner, "legacy SFPutFile dialog", |r| {
        screen_rgb(r, legacy_save_sample_point.0, legacy_save_sample_point.1)
            != page_legacy_save_sample
    });
    runner.push_key_down(0x35, 0);
    runner.push_key_up(0x35, 0);
    step_until_gui(&mut runner, "legacy SFPutFile cancellation", |r| {
        screen_rgb(r, legacy_save_sample_point.0, legacy_save_sample_point.1)
            == page_legacy_save_sample
    });

    runner.set_mouse_position(550, 760);
    assert_reference_frame(&mut runner, "22-standard-file-complete.png");

    // 23. Switch to Resource Browser and exercise map enumeration, named
    // lookup/deferred loading, handle release, and reload. The controls are
    // local Rects (252..276) from left to right: Refresh Map, Load Named, and
    // Release Handle.
    assert!(
        runner.select_guest_menu_item(MENU_PAGES, ITEM_PAGE_RESOURCES),
        "failed to queue selection of Resource Browser page"
    );
    step_until(&mut runner, "switch to Resource Browser page", |r| {
        menu_item_checked(&r.guest_menu_snapshot(), MENU_PAGES, ITEM_PAGE_RESOURCES)
    });
    let snapshot = runner.guest_menu_snapshot();
    assert!(menu_item_checked(
        &snapshot,
        MENU_PAGES,
        ITEM_PAGE_RESOURCES
    ));
    assert!(!menu_item_checked(
        &snapshot,
        MENU_PAGES,
        ITEM_PAGE_STANDARD_FILE
    ));

    runner.set_mouse_position(550, 760);
    let enumerated_frame = rendered_rgb(&mut runner).2;
    assert_resource_browser_snapshot(&mut runner, None);
    assert_reference_frame(&mut runner, "23-resource-browser.png");

    // Refresh exercises a second Count1Resources/Get1IndResource pass and
    // must keep map-only enumeration deterministic.
    click_point(&mut runner, win_top + 264, win_left + 78);
    run_ticks(&mut runner, "refresh resource map", 1);
    runner.set_mouse_position(550, 760);
    assert_eq!(
        enumerated_frame,
        rendered_rgb(&mut runner).2,
        "refreshing the deferred map must preserve the deterministic frame"
    );
    assert_resource_browser_snapshot(&mut runner, None);

    click_point(&mut runner, win_top + 264, win_left + 200);
    run_ticks(&mut runner, "load named resource", 1);
    runner.set_mouse_position(550, 760);
    let loaded_frame = rendered_rgb(&mut runner).2;
    assert_resource_browser_snapshot(&mut runner, Some(203));
    assert_ne!(
        enumerated_frame, loaded_frame,
        "LoadResource must change the visible resource lifecycle state"
    );
    assert_reference_frame(&mut runner, "24-resource-browser-loaded.png");

    click_point(&mut runner, win_top + 264, win_left + 328);
    run_ticks(&mut runner, "release named resource", 1);
    runner.set_mouse_position(550, 760);
    let released_frame = rendered_rgb(&mut runner).2;
    assert_resource_browser_snapshot(&mut runner, None);
    assert_ne!(
        loaded_frame, released_frame,
        "ReleaseResource must change the visible resource lifecycle state"
    );
    assert_reference_frame(&mut runner, "25-resource-browser-released.png");

    // Reloading the same named record after ReleaseResource should restore
    // the exact loaded frame, proving that the map reference can be reused.
    click_point(&mut runner, win_top + 264, win_left + 200);
    run_ticks(&mut runner, "reload named resource", 1);
    runner.set_mouse_position(550, 760);
    assert_resource_browser_snapshot(&mut runner, Some(203));
    assert_eq!(
        loaded_frame,
        rendered_rgb(&mut runner).2,
        "released resource must reload to the same deterministic loaded frame"
    );

    // 22. Switch to Sprites, Masks & Scrolling and build the scene in
    // offscreen GWorlds. The page uses 1-bit CopyMask, 8-bit CopyDeepMask,
    // BitMapToRegion, and SetCPixel/GetCPixel before presenting through one
    // indexed-to-screen CopyBits transfer.
    assert!(
        runner.select_guest_menu_item(MENU_PAGES, ITEM_PAGE_SPRITES),
        "failed to queue selection of Sprites, Masks & Scrolling page"
    );
    step_until(
        &mut runner,
        "switch to Sprites, Masks & Scrolling page",
        |r| menu_item_checked(&r.guest_menu_snapshot(), MENU_PAGES, ITEM_PAGE_SPRITES),
    );
    let snapshot = runner.guest_menu_snapshot();
    assert!(menu_item_checked(&snapshot, MENU_PAGES, ITEM_PAGE_SPRITES));
    assert!(!menu_item_checked(
        &snapshot,
        MENU_PAGES,
        ITEM_PAGE_RESOURCES
    ));
    run_ticks(&mut runner, "sprite scene to settle", 1);
    runner.set_mouse_position(550, 760);
    let (initial_first_body, initial_second_body) =
        assert_sprites_page_rendered(&mut runner, win_top, win_left);
    let initial_frame = rendered_rgb(&mut runner).2;
    assert_reference_frame(&mut runner, "26-sprites.png");

    // 23. Animate the source sprite and rebuild the offscreen scene. This
    // must change the sprite pixels while leaving the matte/region pipeline
    // intact on both CPU slices.
    click_point(&mut runner, win_top + 316, win_left + 90);
    run_ticks(&mut runner, "animated sprite to settle", 1);
    let (animated_first_body, animated_second_body) =
        assert_sprites_page_rendered(&mut runner, win_top, win_left);
    assert_ne!(
        initial_first_body, animated_first_body,
        "Animate Sprite must change the CopyMask sprite frame"
    );
    assert_ne!(
        initial_second_body, animated_second_body,
        "Animate Sprite must change the CopyDeepMask sprite frame"
    );
    let animated_frame = rendered_rgb(&mut runner).2;
    assert_ne!(
        initial_frame, animated_frame,
        "animated sprite scene must change the framebuffer"
    );
    runner.set_mouse_position(550, 760);
    assert_reference_frame(&mut runner, "27-sprites-animated.png");

    // 24. Scroll the existing offscreen raster left. The first sprite's
    // center should move by 24 pixels, and ScrollRect must report nonzero
    // changed bytes plus the right-hand vacated strip.
    click_point(&mut runner, win_top + 316, win_left + 208);
    if powerpc {
        // Native PowerPC owns its QuickDraw state separately from the 68K
        // TrapDispatcher, so use the visible offscreen result as the
        // architecture-neutral ScrollRect checkpoint.
        step_until(&mut runner, "ScrollRect to move sprite scene", |r| {
            let shifted = screen_rgb(r, (win_top + 80 + 70) as u16, (win_left + 24 + 38) as u16);
            let old = screen_rgb(r, (win_top + 80 + 70) as u16, (win_left + 24 + 62) as u16);
            shifted == animated_first_body && old != animated_first_body
        });
    } else {
        let prior_scrolls = runner.dispatcher().debug_scroll_rect_nonzero_delta_count;
        step_until(&mut runner, "ScrollRect to move sprite scene", |r| {
            r.dispatcher().debug_scroll_rect_nonzero_delta_count > prior_scrolls
        });
        assert_eq!(
            runner.dispatcher().debug_scroll_rect_last_delta,
            (-24, 0),
            "first scene scroll must shift pixels left by 24"
        );
        assert!(
            runner.dispatcher().debug_scroll_rect_last_changed_bytes > 0,
            "ScrollRect must change bytes in the offscreen GWorld raster"
        );
    }
    let shifted_body = screen_rgb(
        &mut runner,
        (win_top + 80 + 70) as u16,
        (win_left + 24 + 38) as u16,
    );
    let old_center = screen_rgb(
        &mut runner,
        (win_top + 80 + 70) as u16,
        (win_left + 24 + 62) as u16,
    );
    assert_eq!(
        shifted_body, animated_first_body,
        "ScrollRect must move the sprite center 24 pixels left"
    );
    assert_ne!(
        old_center, animated_first_body,
        "ScrollRect must vacate the sprite's former center"
    );
    runner.set_mouse_position(550, 760);
    assert_reference_frame(&mut runner, "28-sprites-scrolled.png");
}