File size: 51,781 Bytes
c1c6a0c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
#!/usr/bin/env python3
"""INTERACTION lane for Doodle (package via argv[1]) — EFFECT-BY-CONSEQUENCE on
persisted user DATA. This app has no documents/CRUD; its "data" is a large set of
persisted SETTINGS that drive a live wallpaper. So every test here follows the
persistence shape:

    restart_app()  ->  navigate into the fragment  ->  put the control in a KNOWN
    source state  ->  CHANGE it to a target with a real tap/drag  ->  restart_app()
    (PROCESS DEATH)  ->  navigate back  ->  assert the TARGET value SURVIVED.

An in-memory-only / dead shell that echoes the flip but does not persist fails the
restart half. We also cover 3+ step SEQUENCES and MULTI-RECORD integrity (two
independent settings both persist).

ORDER-INDEPENDENCE: restart_app() deliberately does NOT wipe settings (that is the
whole point of a persistence probe), and the delegation forbids clear_data() here
(it has uninstalled this package on this emulator). So a test must never assume a
factory default — an earlier test may have changed it. Every test below therefore
ESTABLISHES its own source state first (selects a different sibling / forces the
opposite switch value / drags the slider to the far end), verifies the change
really happened this session, and only then asserts the target survives restart.
That makes the negative half ("it was different before") true by construction,
independent of run order.

Everything below was confirmed on the running original before it was asserted:
  * Appearance night mode On / Off      (RadioButton `checked` survives restart)
  * Appearance dark_text switch           (MaterialSwitch checked survives)
  * Other theme Dark                      (checked survives; also a dark SURFACE, VLM)
  * Other Contrast Medium                 (checked survives; ui_contrast SharedPref)
  * Other GPU switch                      (checked survives)
  * Parallax swipe switch                 (checked survives)
  * Parallax tilt switch                  (checked survives)
  * Size lockscreen-zoom toggle Out       (checked survives; parent switch on by default)
  * Parallax swipe slider / Size scale slider (sibling SeekBar "Value, X" survives)
  * Design/Variant/color-theme thumbnail selection (checkmark VLM-only) survives.

The Other "Contrast" toggle group WAS confirmed drivable on this device
(button_other_contrast_* render enabled="true" clickable="true"); selecting Medium
flips its `checked` and the choice survives process death because contrast is
written to SharedPreferences (ui_contrast) and re-read on fragment load. So it is a
baseline-honest persistence probe, not a faked one.

Deliberately NOT tested: the Appearance "Random wallpaper design -> Daily" flow.
Selecting Daily calls activity.requestThemeRefresh(true), which recreates the
activity; the restart/re-read then races that recreation and the choice does not
re-read reliably. That is a brittle EXECUTION PATH rather than a missing behavior,
so per the ground-truth rule it is omitted, not faked.

CEDES per the lane split: the instantaneous checked FLIP and pure SETTINGS state
belong to the STATES lane, and a slider's numeric "Value, X" belongs to
COMPUTATION. Here every assertion is about the value SURVIVING process death, or a
multi-step SEQUENCE — which is this lane's own.

NOTE on long-press/context menus: this lane is their sole owner, but the app has
none — a long-press on an Overview row acts as a plain tap (navigates in), and a
long-press on the About developer row opens an EXTERNAL browser link. There is no
in-app context menu to assert, so none is fabricated (ground-truth rule).
"""
import os
import sys
import time

sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
from android_testgen_kit import *  # noqa: F403


# ── local helpers ────────────────────────────────────────────────────────────

def _fresh_home():
    """restart_app() and make sure the Overview list is really showing.

    A previous test that tapped the FAB (avoided here) or a link could leave a
    foreign activity on top; force-stopping Doodle does not kill it. Back out of
    any non-Doodle activity first, then relaunch until the Overview list shows.
    """
    for _ in range(4):
        act = get_current_activity().lower()
        if PACKAGE.lower() in act:
            break
        back()
        time.sleep(0.8)
    restart_app()
    wait_for_foreground()
    time.sleep(0.5)
    if node_by_id(ui(), "linear_overview_appearance") is None:
        back()
        time.sleep(0.6)
        restart_app()
        wait_for_foreground()
        time.sleep(0.5)
    return ui()


def _scroll_to_id(rid, max_swipes=10):
    """Bring a row identified by id-suffix on screen, re-dumping between swipes."""
    for _ in range(max_swipes + 1):
        n = node_by_id(ui(), rid)
        if n:
            return n
        scroll_down(0.4)
    return node_by_id(ui(), rid)


def _center_id(rid, max_swipes=10):
    """Scroll a row on screen AND up out of the bottom FAB zone, so a VLM
    screenshot captures the whole control (a control right under fab_main "Set
    wallpaper" is partly occluded, which makes the VLM answer on incomplete
    pixels) and a slider drag has room. Returns the node once its centre is in the
    upper 60% of the screen."""
    n = _scroll_to_id(rid, max_swipes=max_swipes)
    if not n:
        return None
    s = get_screen_info()
    for _ in range(3):
        n = node_by_id(ui(), rid)
        if not n or "_center" not in n:
            return n
        if n["_center"][1] < s["height"] * 0.6:
            return n
        scroll_down(0.25)
    return node_by_id(ui(), rid)


# Depth-family helpers. The and_enter_* / and_reach_* NAME is what backs a
# declared depth>=1 (the audit looks for a call from this family). Each really
# taps the home row and confirms the fragment title before counting the surface.
_FRAGMENTS = {
    "appearance": ("linear_overview_appearance", "Appearance"),
    "parallax": ("linear_overview_parallax", "Parallax"),
    "size": ("linear_overview_size", "Shape size"),
    "other": ("linear_overview_other", "Other options"),
    "about": ("linear_overview_about", "About"),
}


def and_enter_fragment(key):
    """Tap the Overview row for `key` and confirm its fragment toolbar title.
    Counts one surface via and_enter(). Returns True on a confirmed jump."""
    rid, title = _FRAGMENTS[key]
    row = node_by_id(ui(), rid)
    if not row:
        return False
    before = and_showing()
    if not tap_node(row):
        return False
    settle("tap")
    after = and_showing()
    if find_node(ui(), text=title) is None and not surface_changed(before, after):
        return False
    and_enter(key)
    return True


def and_reach_language_sheet():
    """Other -> Language bottom sheet (depth 2). Proven with and_reach_surface:
    'Select language' appears and the Language row's value vanishes."""
    if not and_enter_fragment("other"):
        return False
    row = _scroll_to_id("linear_other_language")
    if not row:
        return False
    return and_reach_surface(row, "Select language", "linear_other_language")


def _cards_in(container_id, min_w=50):
    """Clickable CardViews geometrically inside a named container, left-to-right.
    Thumbnail/chip selection never reaches the tree, so they are picked by
    containment + order, never by any label. min_w drops the clipped edge card."""
    nodes = ui()
    c = node_by_id(nodes, container_id)
    if not c or "_bounds" not in c:
        return []
    cb = c["_bounds"]
    out = [n for n in nodes
           if "CardView" in n.get("class", "") and n.get("clickable") == "true"
           and "_bounds" in n
           and n["_bounds"][1] >= cb[1] - 5 and n["_bounds"][3] <= cb[3] + 5
           and (n["_bounds"][2] - n["_bounds"][0]) >= min_w]
    return sorted(out, key=lambda n: n["_bounds"][0])


def _slider_value(rid):
    """The 'Value, X' content-desc of the sibling SeekBar next to slider `rid`.
    Retries a couple of times: right after a drag the tree can dump mid-layout and
    momentarily omit the value node."""
    for _ in range(3):
        nodes = ui()
        s = node_by_id(nodes, rid)
        if s and "_center" in s:
            for n in nodes:
                cd = n.get("content-desc", "")
                if cd.startswith("Value,") and "_center" in n:
                    if abs(n["_center"][1] - s["_center"][1]) < 220:
                        return cd
        settle("tap")
    return None


def _drag_slider_fraction(rid, frac):
    """Drag slider `rid`'s thumb to a fraction of its track width, STAYING WELL
    INSIDE the ends (a swipe that starts/ends exactly on the extreme pixel makes
    uiautomator dump a broken tree). Returns the resulting 'Value, X'."""
    sl = node_by_id(ui(), rid)
    if not sl or "_bounds" not in sl:
        return None
    b = sl["_bounds"]
    cy = (b[1] + b[3]) // 2
    w = b[2] - b[0]
    frac = max(0.08, min(0.94, frac))
    x_to = b[0] + int(w * frac)
    swipe(b[0] + int(w * 0.5), cy, x_to, cy, 320)
    settle("tap")
    settle("tap")
    return _slider_value(rid)


def _checked(rid):
    n = node_by_id(ui(), rid)
    return None if n is None else n.get("checked")


def _tap_id_after_scroll(rid):
    n = _scroll_to_id(rid)
    return bool(n) and tap_node(n)


def _set_switch(rid, target_true):
    """Force MaterialSwitch `rid` to a known checked state; return the state after.
    Idempotent — taps only if it is not already at the target."""
    want = "true" if target_true else "false"
    sw = _scroll_to_id(rid)
    if not sw:
        return None
    if sw.get("checked") != want:
        tap_node(sw)
        settle("tap")
    return _checked(rid)


def _select_radio_from_other(target_id, other_id):
    """Establish `other_id` selected first, then select `target_id`. Returns
    (other_was_selected, target_now_selected, other_now_cleared) — a genuine move
    is all three true. Order-independent (does not assume a factory default)."""
    o = _scroll_to_id(other_id)
    if o:
        tap_node(o)
        settle("tap")
    other_sel = _checked(other_id) == "true"
    t = _scroll_to_id(target_id)
    if t:
        tap_node(t)
        settle("tap")
    return other_sel, _checked(target_id) == "true", _checked(other_id) == "false"


# ══════════════════════════════════════════════════════════════════════════════
# RULE persistence — a checked bit / slider value that SURVIVES process death.
# Each test ESTABLISHES its source state, so it is order-independent.
# jump_kind="transition"; expect = the persisted target.
# depth=1 (control lives inside a fragment we navigate into via and_enter_fragment).
# ══════════════════════════════════════════════════════════════════════════════

@and_test(expects=("interaction_persist_night_on",))
def test_persist_night_mode_on():
    _fresh_home()
    if not and_enter_fragment("appearance"):
        record("interaction_persist_night_on", False,
               "setup_failed: could not open Appearance fragment",
               depth=1, jump_kind="transition", expect="night_mode On checked after restart")
        return
    if not _scroll_to_id("button_appearance_night_mode_on"):
        record("interaction_persist_night_on", False,
               "feature_missing: no Night mode 'On' control on Appearance",
               depth=1, jump_kind="transition", expect="night_mode On checked after restart")
        return
    other_sel, moved, other_clr = _select_radio_from_other(
        "button_appearance_night_mode_on", "button_appearance_night_mode_off")
    _fresh_home()
    ok_nav = and_enter_fragment("appearance")
    _scroll_to_id("button_appearance_night_mode_on")
    after_on = _checked("button_appearance_night_mode_on")
    ok = other_sel and moved and other_clr and ok_nav and after_on == "true"
    a = _scroll_to_id("button_appearance_night_mode_auto")
    if a:
        tap_node(a)
    record("interaction_persist_night_on", ok,
           "" if ok else "behavior_wrong: Night mode 'On' did not survive restart "
           f"(after_on={after_on})",
           depth=1, jump_kind="transition",
           expect="button_appearance_night_mode_on checked=true after restart")


@and_test(expects=("interaction_persist_night_off",))
def test_persist_night_mode_off():
    _fresh_home()
    if not and_enter_fragment("appearance"):
        record("interaction_persist_night_off", False,
               "setup_failed: could not open Appearance fragment",
               depth=1, jump_kind="transition", expect="night_mode Off checked after restart")
        return
    if not _scroll_to_id("button_appearance_night_mode_off"):
        record("interaction_persist_night_off", False,
               "feature_missing: no Night mode 'Off' control on Appearance",
               depth=1, jump_kind="transition", expect="night_mode Off checked after restart")
        return
    other_sel, moved, other_clr = _select_radio_from_other(
        "button_appearance_night_mode_off", "button_appearance_night_mode_on")
    _fresh_home()
    ok_nav = and_enter_fragment("appearance")
    _scroll_to_id("button_appearance_night_mode_off")
    after_off = _checked("button_appearance_night_mode_off")
    ok = other_sel and moved and other_clr and ok_nav and after_off == "true"
    a = _scroll_to_id("button_appearance_night_mode_auto")
    if a:
        tap_node(a)
    record("interaction_persist_night_off", ok,
           "" if ok else "behavior_wrong: Night mode 'Off' did not survive restart "
           f"(after_off={after_off})",
           depth=1, jump_kind="transition",
           expect="button_appearance_night_mode_off checked=true after restart")


@and_test(expects=("interaction_persist_contrast_medium",))
def test_persist_contrast_medium():
    """The Other 'Contrast' toggle group persists across process death. Contrast is
    written to SharedPreferences (ui_contrast) and re-read on fragment load, so the
    checked RadioButton survives a restart. Source is established as Standard first
    (order-independent), then Medium is selected; after process death Medium must
    still be checked. Restore Standard at the end so later runs are undisturbed."""
    _fresh_home()
    if not and_enter_fragment("other"):
        record("interaction_persist_contrast_medium", False,
               "setup_failed: could not open Other options fragment",
               depth=1, jump_kind="transition", expect="contrast Medium checked after restart")
        return
    if not _scroll_to_id("button_other_contrast_medium"):
        record("interaction_persist_contrast_medium", False,
               "feature_missing: no Contrast 'Medium' control on Other",
               depth=1, jump_kind="transition", expect="contrast Medium checked after restart")
        return
    # source Standard -> target Medium; a genuine move is all three flags true
    other_sel, moved, other_clr = _select_radio_from_other(
        "button_other_contrast_medium", "button_other_contrast_standard")
    _fresh_home()
    ok_nav = and_enter_fragment("other")
    _scroll_to_id("button_other_contrast_medium")
    after = _checked("button_other_contrast_medium")
    ok = other_sel and moved and other_clr and ok_nav and after == "true"
    # restore Standard (safe default) so later runs are not disturbed
    s = _scroll_to_id("button_other_contrast_standard")
    if s:
        tap_node(s)
    record("interaction_persist_contrast_medium", ok,
           "" if ok else "behavior_wrong: contrast Medium did not survive restart "
           f"(after={after})",
           depth=1, jump_kind="transition",
           expect="button_other_contrast_medium checked=true after restart")


@and_test(expects=("interaction_persist_dark_text_off",))
def test_persist_dark_text_switch_off():
    _fresh_home()
    if not and_enter_fragment("appearance"):
        record("interaction_persist_dark_text_off", False,
               "setup_failed: could not open Appearance fragment",
               depth=1, jump_kind="transition", expect="dark_text switch off after restart")
        return
    if not _scroll_to_id("switch_appearance_dark_text"):
        record("interaction_persist_dark_text_off", False,
               "feature_missing: no 'Use dark text' switch on Appearance",
               depth=1, jump_kind="transition", expect="dark_text switch off after restart")
        return
    # source ON -> target OFF
    _set_switch("switch_appearance_dark_text", True)
    source = _checked("switch_appearance_dark_text")
    _set_switch("switch_appearance_dark_text", False)
    target = _checked("switch_appearance_dark_text")
    _fresh_home()
    ok_nav = and_enter_fragment("appearance")
    _scroll_to_id("switch_appearance_dark_text")
    after_restart = _checked("switch_appearance_dark_text")
    ok = (source == "true" and target == "false" and ok_nav
          and after_restart == "false")
    _set_switch("switch_appearance_dark_text", True)   # restore on
    record("interaction_persist_dark_text_off", ok,
           "" if ok else "behavior_wrong: 'Use dark text' off did not survive restart "
           f"(source={source}, after_restart={after_restart})",
           depth=1, jump_kind="transition",
           expect="switch_appearance_dark_text checked=false after restart")


@and_test(expects=("interaction_persist_theme_dark",))
def test_persist_theme_dark():
    _fresh_home()
    if not and_enter_fragment("other"):
        record("interaction_persist_theme_dark", False,
               "setup_failed: could not open Other options fragment",
               depth=1, jump_kind="transition", expect="theme Dark checked after restart")
        return
    if not _scroll_to_id("button_other_theme_dark"):
        record("interaction_persist_theme_dark", False,
               "feature_missing: no App theme 'Dark' control on Other",
               depth=1, jump_kind="transition", expect="theme Dark checked after restart")
        return
    other_sel, moved, other_clr = _select_radio_from_other(
        "button_other_theme_dark", "button_other_theme_light")
    _fresh_home()
    ok_nav = and_enter_fragment("other")
    _scroll_to_id("button_other_theme_dark")
    after_dark = _checked("button_other_theme_dark")
    ok = other_sel and moved and other_clr and ok_nav and after_dark == "true"
    a = _scroll_to_id("button_other_theme_auto")
    if a:
        tap_node(a)
    record("interaction_persist_theme_dark", ok,
           "" if ok else "behavior_wrong: App theme 'Dark' did not survive restart "
           f"(after_dark={after_dark})",
           depth=1, jump_kind="transition",
           expect="button_other_theme_dark checked=true after restart")


@and_test(expects=("interaction_persist_size_scale_slider",))
def test_persist_size_scale_slider():
    """A slider DRAGGED to a new value survives restart. (The numeric value is
    COMPUTATION's; here the claim is SURVIVAL of the dragged value.) We first drag
    the Shape-size scale slider toward the HIGH end (known source), then to a LOW
    fraction (target), so the change is real regardless of the slider's prior
    position, and assert the LOW value persists."""
    _fresh_home()
    if not and_enter_fragment("size"):
        record("interaction_persist_size_scale_slider", False,
               "setup_failed: could not open Shape size fragment",
               depth=1, jump_kind="transition", expect="size scale slider value survives restart")
        return
    if not _center_id("slider_size_scale"):
        record("interaction_persist_size_scale_slider", False,
               "feature_missing: no scale slider on Shape size",
               depth=1, jump_kind="transition", expect="size scale slider value survives restart")
        return
    source_val = _drag_slider_fraction("slider_size_scale", 0.9)     # high source
    target_val = _drag_slider_fraction("slider_size_scale", 0.15)    # low target
    changed = bool(target_val) and target_val != source_val
    _fresh_home()
    ok_nav = and_enter_fragment("size")
    _scroll_to_id("slider_size_scale")
    after_val = _slider_value("slider_size_scale")
    ok = changed and ok_nav and after_val == target_val
    record("interaction_persist_size_scale_slider", ok,
           "" if ok else "behavior_wrong: scale slider value did not survive restart "
           f"(source={source_val}, target={target_val}, after={after_val})",
           depth=1, jump_kind="transition",
           expect=f"slider_size_scale {target_val!r} after restart")


@and_test(expects=("interaction_persist_gpu_off",))
def test_persist_gpu_switch_off():
    _fresh_home()
    if not and_enter_fragment("other"):
        record("interaction_persist_gpu_off", False,
               "setup_failed: could not open Other options fragment",
               depth=1, jump_kind="transition", expect="GPU switch off after restart")
        return
    if not _scroll_to_id("switch_other_gpu"):
        record("interaction_persist_gpu_off", False,
               "feature_missing: no 'Hardware acceleration' switch on Other",
               depth=1, jump_kind="transition", expect="GPU switch off after restart")
        return
    _set_switch("switch_other_gpu", True)
    source = _checked("switch_other_gpu")
    _set_switch("switch_other_gpu", False)
    target = _checked("switch_other_gpu")
    _fresh_home()
    ok_nav = and_enter_fragment("other")
    _scroll_to_id("switch_other_gpu")
    after_restart = _checked("switch_other_gpu")
    ok = (source == "true" and target == "false" and ok_nav
          and after_restart == "false")
    _set_switch("switch_other_gpu", True)   # restore on
    record("interaction_persist_gpu_off", ok,
           "" if ok else "behavior_wrong: Hardware-acceleration off did not survive restart "
           f"(source={source}, after_restart={after_restart})",
           depth=1, jump_kind="transition",
           expect="switch_other_gpu checked=false after restart")


@and_test(expects=("interaction_persist_swipe_switch_off",))
def test_persist_parallax_swipe_switch_off():
    _fresh_home()
    if not and_enter_fragment("parallax"):
        record("interaction_persist_swipe_switch_off", False,
               "setup_failed: could not open Parallax fragment",
               depth=1, jump_kind="transition", expect="swipe switch off after restart")
        return
    if not _scroll_to_id("switch_parallax_swipe"):
        record("interaction_persist_swipe_switch_off", False,
               "feature_missing: no 'Swipe effect' switch on Parallax",
               depth=1, jump_kind="transition", expect="swipe switch off after restart")
        return
    _set_switch("switch_parallax_swipe", True)
    source = _checked("switch_parallax_swipe")
    _set_switch("switch_parallax_swipe", False)
    target = _checked("switch_parallax_swipe")
    _fresh_home()
    ok_nav = and_enter_fragment("parallax")
    _scroll_to_id("switch_parallax_swipe")
    after_restart = _checked("switch_parallax_swipe")
    ok = (source == "true" and target == "false" and ok_nav
          and after_restart == "false")
    _set_switch("switch_parallax_swipe", True)   # restore on
    record("interaction_persist_swipe_switch_off", ok,
           "" if ok else "behavior_wrong: 'Swipe effect' off did not survive restart "
           f"(source={source}, after_restart={after_restart})",
           depth=1, jump_kind="transition",
           expect="switch_parallax_swipe checked=false after restart")


@and_test(expects=("interaction_persist_tilt_switch_on",))
def test_persist_parallax_tilt_switch_on():
    _fresh_home()
    if not and_enter_fragment("parallax"):
        record("interaction_persist_tilt_switch_on", False,
               "setup_failed: could not open Parallax fragment",
               depth=1, jump_kind="transition", expect="tilt switch on after restart")
        return
    if not _scroll_to_id("switch_parallax_tilt"):
        record("interaction_persist_tilt_switch_on", False,
               "feature_missing: no 'Tilt effect' switch on Parallax",
               depth=1, jump_kind="transition", expect="tilt switch on after restart")
        return
    # source OFF -> target ON
    _set_switch("switch_parallax_tilt", False)
    source = _checked("switch_parallax_tilt")
    _set_switch("switch_parallax_tilt", True)
    target = _checked("switch_parallax_tilt")
    _fresh_home()
    ok_nav = and_enter_fragment("parallax")
    _scroll_to_id("switch_parallax_tilt")
    after_restart = _checked("switch_parallax_tilt")
    ok = (source == "false" and target == "true" and ok_nav
          and after_restart == "true")
    _set_switch("switch_parallax_tilt", False)   # restore off (battery warning)
    record("interaction_persist_tilt_switch_on", ok,
           "" if ok else "behavior_wrong: 'Tilt effect' on did not survive restart "
           f"(source={source}, after_restart={after_restart})",
           depth=1, jump_kind="transition",
           expect="switch_parallax_tilt checked=true after restart")


@and_test(expects=("interaction_persist_zoom_unlock_out",))
def test_persist_size_zoom_unlock_out():
    _fresh_home()
    if not and_enter_fragment("size"):
        record("interaction_persist_zoom_unlock_out", False,
               "setup_failed: could not open Shape size fragment",
               depth=1, jump_kind="transition", expect="lockscreen zoom 'Out' checked after restart")
        return
    # the toggle group is only active when its parent switch is on (default on).
    _set_switch("switch_size_zoom_unlock", True)
    if not _scroll_to_id("button_size_zoom_unlock_out"):
        record("interaction_persist_zoom_unlock_out", False,
               "feature_missing: no lockscreen-zoom 'Zoom out' control on Size",
               depth=1, jump_kind="transition", expect="lockscreen zoom 'Out' checked after restart")
        return
    other_sel, moved, other_clr = _select_radio_from_other(
        "button_size_zoom_unlock_out", "button_size_zoom_unlock_in")
    _fresh_home()
    ok_nav = and_enter_fragment("size")
    _scroll_to_id("button_size_zoom_unlock_out")
    after_out = _checked("button_size_zoom_unlock_out")
    ok = other_sel and moved and other_clr and ok_nav and after_out == "true"
    i = _scroll_to_id("button_size_zoom_unlock_in")
    if i:
        tap_node(i)
    record("interaction_persist_zoom_unlock_out", ok,
           "" if ok else "behavior_wrong: lockscreen zoom 'Out' did not survive restart "
           f"(after_out={after_out})",
           depth=1, jump_kind="transition",
           expect="button_size_zoom_unlock_out checked=true after restart")


@and_test(expects=("interaction_persist_slider_swipe",))
def test_persist_parallax_swipe_slider():
    """A SECOND slider persistence, in a different fragment. Drag the Parallax
    swipe slider HIGH (source), then to a LOW fraction (target), and assert the low
    value survives restart."""
    _fresh_home()
    if not and_enter_fragment("parallax"):
        record("interaction_persist_slider_swipe", False,
               "setup_failed: could not open Parallax fragment",
               depth=1, jump_kind="transition", expect="swipe slider value survives restart")
        return
    if not _center_id("slider_parallax_swipe"):
        record("interaction_persist_slider_swipe", False,
               "feature_missing: no swipe slider on Parallax",
               depth=1, jump_kind="transition", expect="swipe slider value survives restart")
        return
    source_val = _drag_slider_fraction("slider_parallax_swipe", 0.9)    # high source
    target_val = _drag_slider_fraction("slider_parallax_swipe", 0.1)    # low target
    changed = bool(target_val) and target_val != source_val
    _fresh_home()
    ok_nav = and_enter_fragment("parallax")
    _scroll_to_id("slider_parallax_swipe")
    after_val = _slider_value("slider_parallax_swipe")
    ok = changed and ok_nav and after_val == target_val
    record("interaction_persist_slider_swipe", ok,
           "" if ok else "behavior_wrong: swipe slider value did not survive restart "
           f"(source={source_val}, target={target_val}, after={after_val})",
           depth=1, jump_kind="transition",
           expect=f"slider_parallax_swipe {target_val!r} after restart")


# ══════════════════════════════════════════════════════════════════════════════
# MULTI-STEP SEQUENCES & MULTI-RECORD integrity — this lane's own.
# ══════════════════════════════════════════════════════════════════════════════

@and_test(expects=("interaction_multi_record_integrity",))
def test_multi_record_integrity():
    """Change TWO independent settings in TWO different fragments, kill the
    process, then assert BOTH survived — a recreation that persists only the last
    write, or keeps one, fails. Setting A: Appearance night mode = On. Setting B:
    Other App theme = Dark. Both are established from a different sibling first."""
    _fresh_home()
    if not and_enter_fragment("appearance") or not _scroll_to_id("button_appearance_night_mode_on"):
        record("interaction_multi_record_integrity", False,
               "setup_failed: could not set Night mode On on Appearance",
               depth=1, jump_kind="transition",
               expect="night On AND theme Dark both checked after restart")
        return
    _, a_moved, _ = _select_radio_from_other(
        "button_appearance_night_mode_on", "button_appearance_night_mode_off")
    _fresh_home()
    if not and_enter_fragment("other") or not _scroll_to_id("button_other_theme_dark"):
        record("interaction_multi_record_integrity", False,
               "setup_failed: could not set App theme Dark on Other",
               depth=1, jump_kind="transition",
               expect="night On AND theme Dark both checked after restart")
        return
    _, b_moved, _ = _select_radio_from_other(
        "button_other_theme_dark", "button_other_theme_light")
    # process death, then verify BOTH
    _fresh_home()
    nav_a = and_enter_fragment("appearance")
    _scroll_to_id("button_appearance_night_mode_on")
    a_after = _checked("button_appearance_night_mode_on")
    _fresh_home()
    nav_b = and_enter_fragment("other")
    _scroll_to_id("button_other_theme_dark")
    b_after = _checked("button_other_theme_dark")
    ok = (a_moved and b_moved and nav_a and nav_b
          and a_after == "true" and b_after == "true")
    # restore theme Auto and night Auto
    au = _scroll_to_id("button_other_theme_auto")
    if au:
        tap_node(au)
    _fresh_home()
    if and_enter_fragment("appearance"):
        na = _scroll_to_id("button_appearance_night_mode_auto")
        if na:
            tap_node(na)
    record("interaction_multi_record_integrity", ok,
           "" if ok else "behavior_wrong: not BOTH settings survived restart "
           f"(night_on={a_after}, theme_dark={b_after})",
           depth=1, jump_kind="transition",
           expect="night_mode_on=true AND theme_dark=true after restart")


@and_test(expects=("interaction_sequence_night_roundtrip",))
def test_sequence_night_roundtrip():
    """4-step SEQUENCE where each step depends on the last: Auto -> On -> Off ->
    Auto, asserting the checked mark moves at every hop and the round-trip lands
    back on Auto (idempotent). A dead shell that echoes only the first input cannot
    reproduce the moving selection. Forces Auto first for a known start."""
    _fresh_home()
    if not and_enter_fragment("appearance"):
        record("interaction_sequence_night_roundtrip", False,
               "setup_failed: could not open Appearance fragment",
               depth=1, jump_kind="value", expect="night mode back to Auto after round-trip")
        return
    if not _scroll_to_id("button_appearance_night_mode_auto"):
        record("interaction_sequence_night_roundtrip", False,
               "feature_missing: no Night mode controls on Appearance",
               depth=1, jump_kind="value", expect="night mode back to Auto after round-trip")
        return
    _tap_id_after_scroll("button_appearance_night_mode_auto")   # known start
    start_auto = _checked("button_appearance_night_mode_auto") == "true"
    _tap_id_after_scroll("button_appearance_night_mode_on")
    s1 = (_checked("button_appearance_night_mode_on") == "true"
          and _checked("button_appearance_night_mode_auto") == "false")
    _tap_id_after_scroll("button_appearance_night_mode_off")
    s2 = (_checked("button_appearance_night_mode_off") == "true"
          and _checked("button_appearance_night_mode_on") == "false")
    _tap_id_after_scroll("button_appearance_night_mode_auto")
    s3 = (_checked("button_appearance_night_mode_auto") == "true"
          and _checked("button_appearance_night_mode_off") == "false")
    ok = start_auto and s1 and s2 and s3
    record("interaction_sequence_night_roundtrip", ok,
           "" if ok else "behavior_wrong: night mode did not step Auto->On->Off->Auto "
           f"(start={start_auto}, s1={s1}, s2={s2}, s3={s3})",
           depth=1, jump_kind="value",
           expect="button_appearance_night_mode_auto checked=true at end of round-trip")


@and_test(expects=("interaction_sequence_night_cycle",))
def test_sequence_night_mode_cycle():
    """3-step SEQUENCE across a RESTART boundary: force Auto -> select On -> kill
    the process -> reopen -> select Off, then assert Off is set and On cleared.
    This proves the app both persisted the mid-sequence write (On) AND accepted a
    dependent follow-up write (Off) after process death — a shell that resets on
    relaunch would show On gone before the Off step and fail."""
    _fresh_home()
    if not and_enter_fragment("appearance"):
        record("interaction_sequence_night_cycle", False,
               "setup_failed: could not open Appearance fragment",
               depth=1, jump_kind="value", expect="night mode ends on Off after cross-restart cycle")
        return
    if not _scroll_to_id("button_appearance_night_mode_auto"):
        record("interaction_sequence_night_cycle", False,
               "feature_missing: no Night mode controls on Appearance",
               depth=1, jump_kind="value", expect="night mode ends on Off after cross-restart cycle")
        return
    _tap_id_after_scroll("button_appearance_night_mode_auto")   # known start
    _tap_id_after_scroll("button_appearance_night_mode_on")
    s1 = _checked("button_appearance_night_mode_on") == "true"
    # process death — the On write must have persisted
    _fresh_home()
    nav = and_enter_fragment("appearance")
    _scroll_to_id("button_appearance_night_mode_on")
    persisted_on = _checked("button_appearance_night_mode_on") == "true"
    _tap_id_after_scroll("button_appearance_night_mode_off")
    s2 = (_checked("button_appearance_night_mode_off") == "true"
          and _checked("button_appearance_night_mode_on") == "false")
    ok = s1 and nav and persisted_on and s2
    a = _scroll_to_id("button_appearance_night_mode_auto")
    if a:
        tap_node(a)
    record("interaction_sequence_night_cycle", ok,
           "" if ok else "behavior_wrong: On->(restart)->Off sequence broke "
           f"(s1={s1}, persisted_on={persisted_on}, s2={s2})",
           depth=1, jump_kind="value",
           expect="button_appearance_night_mode_off checked=true after On survived a restart")


@and_test(expects=("interaction_language_sheet_followsystem",))
def test_language_sheet_followsystem():
    """Depth-2 SHEET sequence: open Other -> Language sheet (a real 2-hop reach),
    confirm the sheet's language list, tap the SAFE no-op 'Follow system', close
    the sheet with BACK, and assert the Language row value is still 'Follow system'
    (the setting neither corrupted nor changed). Follow-system is a no-op — no real
    locale is applied, so the whole app is not re-localized."""
    _fresh_home()
    if not and_reach_language_sheet():
        record("interaction_language_sheet_followsystem", False,
               "setup_failed: could not reach the Language bottom sheet",
               depth=2, jump_kind="value", expect="text_other_language 'Follow system' preserved")
        return
    nodes = ui()
    fs = find_node_ci(nodes, "Follow system")
    listed = fs is not None and find_node_ci(nodes, "Deutsch") is not None
    if not listed:
        record("interaction_language_sheet_followsystem", False,
               "behavior_wrong: Language sheet did not list expected languages",
               depth=2, jump_kind="value", expect="text_other_language 'Follow system' preserved")
        return
    # tap the containing row of 'Follow system' (the name TextView is not clickable)
    tgt = None
    for n in nodes:
        if (n.get("resource-id", "").endswith("linear_language_container")
                and "_bounds" in n and fs.get("_center")
                and n["_bounds"][1] <= fs["_center"][1] <= n["_bounds"][3]):
            tgt = n
            break
    safe_tap(tgt or fs)
    # close the sheet and return to Other
    and_back()
    settle("tap")
    v = node_by_id(ui(), "text_other_language")
    val = v.get("text") if v else None
    back_on_other = find_node(ui(), text="Other options") is not None or val is not None
    ok = (val == "Follow system") and back_on_other
    record("interaction_language_sheet_followsystem", ok,
           "" if ok else "behavior_wrong: Language value not preserved as 'Follow system' "
           f"(got {val!r})",
           depth=2, jump_kind="value",
           expect="text_other_language == 'Follow system' after closing the sheet")


# ══════════════════════════════════════════════════════════════════════════════
# VLM persistence (7) — the CONSEQUENCE is only visual (a checkmark on a
# thumbnail/chip/swatch, or the whole app's theme colour). These cannot be read
# from the view tree (thumbnails are CardViews with selected=false always).
# ══════════════════════════════════════════════════════════════════════════════

@and_test(expects=("interaction_persist_design_select_vlm",))
def test_persist_design_selection_vlm():
    """Select the 2nd Doodle design thumbnail, kill the process, return to
    Appearance, and assert the white checkmark is STILL on the 2nd thumbnail."""
    _fresh_home()
    if not and_enter_fragment("appearance"):
        record("interaction_persist_design_select_vlm", False,
               "setup_failed: could not open Appearance fragment",
               depth=1, jump_kind="value", expect="checkmark on 2nd design thumbnail after restart",
               tag="position")
        return
    _scroll_to_id("linear_appearance_wallpaper_doodle")
    cards = _cards_in("linear_appearance_wallpaper_doodle")
    if len(cards) < 2:
        record("interaction_persist_design_select_vlm", False,
               "feature_missing: fewer than 2 Doodle design thumbnails",
               depth=1, jump_kind="value", expect="checkmark on 2nd design thumbnail after restart",
               tag="position")
        return
    tap_node(cards[1])
    settle("tap")
    _fresh_home()
    and_enter_fragment("appearance")
    _center_id("linear_appearance_wallpaper_doodle")
    vlm_assert(
        "interaction_persist_design_select",
        "In the 'Pixel 4 Doodle Collection' row of 4 wallpaper thumbnails, is the "
        "white checkmark on the SECOND thumbnail from the left (not the first)?",
        expect="YES", depth=1, jump_kind="value", tag="position")
    # restore the 1st
    _scroll_to_id("linear_appearance_wallpaper_doodle")
    c = _cards_in("linear_appearance_wallpaper_doodle")
    if c:
        tap_node(c[0])


@and_test(expects=("interaction_design_checkmark_moved_vlm",))
def test_design_checkmark_moved_vlm():
    """Immediately after tapping the 2nd design thumbnail (no restart), the white
    checkmark has MOVED to it. Establishes the 1st as selected first so the move is
    real, then taps the 2nd. Pairs the tree-invisible selection with a visible
    consequence."""
    _fresh_home()
    if not and_enter_fragment("appearance"):
        record("interaction_design_checkmark_moved_vlm", False,
               "setup_failed: could not open Appearance fragment",
               depth=1, jump_kind="value", expect="checkmark moved to 2nd design thumbnail",
               tag="position")
        return
    _scroll_to_id("linear_appearance_wallpaper_doodle")
    cards = _cards_in("linear_appearance_wallpaper_doodle")
    if len(cards) < 2:
        record("interaction_design_checkmark_moved_vlm", False,
               "feature_missing: fewer than 2 Doodle design thumbnails",
               depth=1, jump_kind="value", expect="checkmark moved to 2nd design thumbnail",
               tag="position")
        return
    tap_node(cards[0])          # establish 1st selected
    settle("tap")
    cards = _cards_in("linear_appearance_wallpaper_doodle")
    tap_node(cards[1])          # move to 2nd
    settle("tap")
    _center_id("linear_appearance_wallpaper_doodle")
    vlm_assert(
        "interaction_design_checkmark_moved",
        "In the 'Pixel 4 Doodle Collection' row of 4 wallpaper thumbnails, is the "
        "white checkmark on the SECOND thumbnail from the left (not the first)?",
        expect="YES", depth=1, jump_kind="value", tag="position")
    _scroll_to_id("linear_appearance_wallpaper_doodle")
    c = _cards_in("linear_appearance_wallpaper_doodle")
    if c:
        tap_node(c[0])


@and_test(expects=("interaction_persist_variant_select_vlm",))
def test_persist_variant_selection_vlm():
    """Select the 2nd Variant chip, kill the process, return to Appearance, and
    assert the checkmark is STILL on the 2nd chip."""
    _fresh_home()
    if not and_enter_fragment("appearance"):
        record("interaction_persist_variant_select_vlm", False,
               "setup_failed: could not open Appearance fragment",
               depth=1, jump_kind="value", expect="checkmark on 2nd variant chip after restart",
               tag="position")
        return
    _scroll_to_id("linear_appearance_variant_container")
    cards = _cards_in("linear_appearance_variant_container")
    if len(cards) < 2:
        record("interaction_persist_variant_select_vlm", False,
               "feature_missing: fewer than 2 Variant chips",
               depth=1, jump_kind="value", expect="checkmark on 2nd variant chip after restart",
               tag="position")
        return
    tap_node(cards[1])
    settle("tap")
    _fresh_home()
    and_enter_fragment("appearance")
    _center_id("linear_appearance_variant_container")
    vlm_assert(
        "interaction_persist_variant_select",
        "Look ONLY at the horizontal 'Variant' row of round colored chips (the row "
        "directly under the word 'Variant'). Exactly one chip has a small checkmark "
        "(tick) drawn on it. Is that checkmark on the SECOND chip from the left "
        "rather than the first?",
        expect="YES", depth=1, jump_kind="value", tag="position")
    _scroll_to_id("linear_appearance_variant_container")
    c = _cards_in("linear_appearance_variant_container")
    if c:
        tap_node(c[0])


@and_test(expects=("interaction_persist_theme_swatch_vlm",))
def test_persist_theme_swatch_vlm():
    """Select the 2nd App-color-theme swatch in Other, kill the process, return,
    and assert the white checkmark is STILL on the 2nd swatch."""
    _fresh_home()
    if not and_enter_fragment("other"):
        record("interaction_persist_theme_swatch_vlm", False,
               "setup_failed: could not open Other options fragment",
               depth=1, jump_kind="value", expect="checkmark on 2nd color-theme swatch after restart",
               tag="position")
        return
    _scroll_to_id("linear_other_theme_container")
    cards = _cards_in("linear_other_theme_container")
    if len(cards) < 2:
        record("interaction_persist_theme_swatch_vlm", False,
               "feature_missing: fewer than 2 color-theme swatches",
               depth=1, jump_kind="value", expect="checkmark on 2nd color-theme swatch after restart",
               tag="position")
        return
    tap_node(cards[1])
    settle("tap")
    _fresh_home()
    and_enter_fragment("other")
    _center_id("linear_other_theme_container")
    vlm_assert(
        "interaction_persist_theme_swatch",
        "In the 'App color theme' row of colored swatches, is the white checkmark "
        "on the SECOND swatch from the left (not the first)?",
        expect="YES", depth=1, jump_kind="value", tag="position")
    _scroll_to_id("linear_other_theme_container")
    c = _cards_in("linear_other_theme_container")
    if c:
        tap_node(c[0])


@and_test(expects=("interaction_persist_theme_dark_surface_vlm",))
def test_persist_theme_dark_surface_vlm():
    """Set the App theme to Dark, kill the process, and assert the app relaunches
    onto a DARK surface (the tree cannot show a colour — this is the visual
    consequence of the persisted theme). Restore Auto after."""
    _fresh_home()
    if not and_enter_fragment("other"):
        record("interaction_persist_theme_dark_surface_vlm", False,
               "setup_failed: could not open Other options fragment",
               depth=1, jump_kind="value", expect="dark app surface after restart",
               tag="color")
        return
    d = _scroll_to_id("button_other_theme_dark")
    if not d:
        record("interaction_persist_theme_dark_surface_vlm", False,
               "feature_missing: no App theme 'Dark' control on Other",
               depth=1, jump_kind="value", expect="dark app surface after restart",
               tag="color")
        return
    tap_node(d)
    settle("tap")
    _fresh_home()   # relaunch onto the Overview; should be dark
    vlm_assert(
        "interaction_persist_theme_dark_surface",
        "Is the app's background predominantly DARK (near-black / very dark grey), "
        "i.e. a dark theme rather than a light/white theme?",
        expect="YES", depth=0, jump_kind="value", tag="color")
    # restore Auto
    _fresh_home()
    if and_enter_fragment("other"):
        a = _scroll_to_id("button_other_theme_auto")
        if a:
            tap_node(a)


@and_test(expects=("interaction_persist_night_on_highlight_vlm",))
def test_persist_night_on_highlight_vlm():
    """Set Night mode = On, kill the process, return to Appearance, and assert the
    'On' segment of the segmented control is the one visually highlighted (filled
    darker) rather than 'Auto' — the rendered selection of the persisted choice."""
    _fresh_home()
    if not and_enter_fragment("appearance"):
        record("interaction_persist_night_on_highlight_vlm", False,
               "setup_failed: could not open Appearance fragment",
               depth=1, jump_kind="value", expect="'On' segment highlighted after restart",
               tag="color")
        return
    if not _scroll_to_id("button_appearance_night_mode_on"):
        record("interaction_persist_night_on_highlight_vlm", False,
               "feature_missing: no Night mode controls on Appearance",
               depth=1, jump_kind="value", expect="'On' segment highlighted after restart",
               tag="color")
        return
    _tap_id_after_scroll("button_appearance_night_mode_on")
    _fresh_home()
    and_enter_fragment("appearance")
    _center_id("button_appearance_night_mode_on")   # lift it out of the FAB zone
    vlm_assert(
        "interaction_persist_night_on_highlight",
        "This screenshot shows a 'Night mode' segmented button group with three "
        "options labelled Auto, On, and Off. Is the 'On' option the selected / "
        "highlighted one (it has a filled/solid pill background while Auto and Off "
        "do not)?",
        expect="YES", depth=1, jump_kind="value", tag="color")
    # restore Auto
    a = _scroll_to_id("button_appearance_night_mode_auto")
    if a:
        tap_node(a)


@and_test(expects=("interaction_multi_record_integrity_vlm",))
def test_multi_record_integrity_vlm():
    """Multi-record integrity, visual half: set BOTH Night mode = On (Appearance)
    AND App theme = Dark (Other), kill the process, and assert the relaunched
    surface is dark (theme B survived)."""
    _fresh_home()
    if not and_enter_fragment("appearance") or not _scroll_to_id("button_appearance_night_mode_on"):
        record("interaction_multi_record_integrity_vlm", False,
               "setup_failed: could not set Night mode On",
               depth=1, jump_kind="value", expect="dark surface after two persisted settings",
               tag="color")
        return
    _tap_id_after_scroll("button_appearance_night_mode_on")
    _fresh_home()
    if not and_enter_fragment("other") or not _scroll_to_id("button_other_theme_dark"):
        record("interaction_multi_record_integrity_vlm", False,
               "setup_failed: could not set App theme Dark",
               depth=1, jump_kind="value", expect="dark surface after two persisted settings",
               tag="color")
        return
    _tap_id_after_scroll("button_other_theme_dark")
    _fresh_home()   # relaunch: should be dark from the persisted theme
    vlm_assert(
        "interaction_multi_record_integrity",
        "Is the whole app currently using a DARK theme, i.e. the background is "
        "near-black / very dark grey rather than white/light?",
        expect="YES", depth=0, jump_kind="value", tag="color")
    # restore Auto theme and Auto night
    _fresh_home()
    if and_enter_fragment("other"):
        a = _scroll_to_id("button_other_theme_auto")
        if a:
            tap_node(a)
    _fresh_home()
    if and_enter_fragment("appearance"):
        na = _scroll_to_id("button_appearance_night_mode_auto")
        if na:
            tap_node(na)


TESTS = [
    # rule persistence
    test_persist_night_mode_on,
    test_persist_night_mode_off,
    test_persist_contrast_medium,
    test_persist_dark_text_switch_off,
    test_persist_theme_dark,
    test_persist_size_scale_slider,
    test_persist_gpu_switch_off,
    test_persist_parallax_swipe_switch_off,
    test_persist_parallax_tilt_switch_on,
    test_persist_size_zoom_unlock_out,
    test_persist_parallax_swipe_slider,
    # sequences / multi-record
    test_multi_record_integrity,
    test_sequence_night_roundtrip,
    test_sequence_night_mode_cycle,
    test_language_sheet_followsystem,
    # VLM persistence (7)
    test_persist_design_selection_vlm,
    test_design_checkmark_moved_vlm,
    test_persist_variant_selection_vlm,
    test_persist_theme_swatch_vlm,
    test_persist_theme_dark_surface_vlm,
    test_persist_night_on_highlight_vlm,
    test_multi_record_integrity_vlm,
]


if __name__ == "__main__":
    run_suite(TESTS, "interaction")