File size: 27,036 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
#!/usr/bin/env python3
"""STRUCTURE lane — QuickTile Settings (com.rbn.qtsettings).

This lane owns exactly two things:
  1. The launch GATE: the app reaches the foreground and its top-level surfaces
     exist (both tabs, the shortcut icon, the help icon). jump_kind="none",
     kept to a small minority.
  2. Tree-SHAPE deltas: an interaction that changes the SHAPE of the view tree
     (a dialog window layer appears, a conditional reveal adds a class of widget,
     a tab page-swap changes which card subtree is present, an AnimatedVisibility
     region adds a subtree). Each is jump_kind="transition" with the observed
     delta in `expect`, and is reversed to assert it restores where stable.

Everything CEDED to other lanes: exact content strings (text), checked/enabled/
selected state (states), pure reachability of a named screen (navigation),
computed values (computation), CRUD on records (interaction). Here we assert only
that the tree's SHAPE changed by the delta we observed on the running original.
"""
import os
import sys

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


# ── depth-family helpers (back a declared depth>=1 with a real navigation) ──────
def and_enter_shortcuts(nodes):
    """Tap the top-bar 'Open shortcut settings' icon and confirm the Shortcut
    Actions surface replaced the DNS tab. Counts one surface crossed."""
    n = find_node(nodes, content_desc="Open shortcut settings")
    if n is None:
        return False
    # 'Add Custom Hostname' is DNS-tab-only (the shortcut screen reuses the
    # 'Private DNS Tile' string as a group header, so it is NOT a valid source).
    ok = and_reach_surface(n, target="Shortcut Actions", source="Add Custom Hostname")
    return ok


def and_enter_dialog(nodes, *, content_desc="", text="", target=""):
    """Open an overlay dialog by tapping a named trigger, and confirm a dialog-only
    key APPEARED (proving a new window layer). Counts one surface crossed."""
    n = None
    if content_desc:
        n = find_node(nodes, content_desc=content_desc)
    if n is None and text:
        n = find_node(nodes, text=text) or find_node_ci(nodes, text)
    if n is None or not is_safe(n):
        return False, and_showing(nodes)
    before = and_showing(nodes)
    if not tap_node(n):
        return False, before
    after = and_showing()
    appeared = and_reach(before, after, target) if target else surface_changed(before, after)
    if appeared:
        and_enter(target or content_desc or text)
    return appeared, after


# ══════════════════════════════════════════════════════════════════════════════
# GATE — the app launches and its top-level surfaces exist (jump_kind="none")
# ══════════════════════════════════════════════════════════════════════════════

@and_test(expects=("structure_gate_launch",))
def test_gate_launch():
    """The app reaches the foreground and renders a top-level container."""
    restart_app()
    fg = wait_for_foreground()
    nodes = ui()
    container = bool(find_by_class(nodes, "ViewGroup")
                     or find_by_class(nodes, "FrameLayout"))
    ok = bool(fg and container)
    record("structure_gate_launch", ok,
           "" if ok else "setup_failed: app did not reach the foreground with a container",
           depth=0, jump_kind="none")


@and_test(expects=("structure_gate_top_surfaces",))
def test_gate_top_surfaces():
    """Both tab labels and both top-bar icon controls exist on the first surface —
    matched by class + content-desc, never by internal id."""
    restart_app()
    wait_for_foreground()
    nodes = ui()
    dns_tab = find_node(nodes, text="Private DNS", **{"class": "android.widget.TextView"})
    usb_tab = find_node(nodes, text="USB Debugging", **{"class": "android.widget.TextView"})
    shortcut_icon = find_node(nodes, content_desc="Open shortcut settings")
    help_icon = find_node(nodes, content_desc="Help / Grant Permission")
    ok = all([dns_tab, usb_tab, shortcut_icon, help_icon])
    missing = [n for n, v in (("Private DNS tab", dns_tab), ("USB tab", usb_tab),
                              ("shortcut icon", shortcut_icon), ("help icon", help_icon))
               if not v]
    record("structure_gate_top_surfaces", bool(ok),
           "" if ok else f"feature_missing: top surface(s) absent: {', '.join(missing)}",
           depth=0, jump_kind="none")


@and_test(expects=("structure_dns_card_layout_vlm",))
def test_dns_card_layout_vlm():
    """The DNS tile card renders as a vertical list of labelled checkbox rows."""
    restart_app()
    wait_for_foreground()
    ui()
    vlm_assert("structure_dns_card_layout",
               "In the card titled 'Private DNS Tile', is there a vertically stacked "
               "list where each row has a square checkbox on the left and a text label "
               "to its right?",
               RESULTS_DIR, tag="layout", depth=0)


# ══════════════════════════════════════════════════════════════════════════════
# SHAPE DELTA — tab page-swap changes which card subtree is present
# ══════════════════════════════════════════════════════════════════════════════

@and_test(expects=("structure_tab_swap_shape", "structure_tab_swap_shape_vlm"))
def test_tab_swap_shape():
    """Tapping the USB Debugging tab swaps the card subtree: the 'USB Debugging Tile'
    header + 'USB Debug On/Off' rows APPEAR and the 'Private DNS Tile' subtree
    DISAPPEARS. Tapping 'Private DNS' reverses it (DNS subtree restored)."""
    restart_app()
    wait_for_foreground()
    before = and_showing(ui())
    dns_present0 = any("Private DNS Tile" in k for k in before)

    tapped_usb = tap_text(ui(), "USB Debugging")
    after = and_showing()
    swapped = and_reach(before, after, target="USB Debugging Tile",
                        source="Private DNS Tile")

    # reverse: back to the DNS tab, the DNS subtree must be restored
    tapped_dns = tap_text(ui(), "Private DNS")
    restored = and_showing()
    reversed_ok = and_reach(after, restored, target="Private DNS Tile",
                            source="USB Debugging Tile")

    ok = tapped_usb and dns_present0 and swapped and tapped_dns and reversed_ok
    if not swapped:
        msg = "behavior_wrong: USB card subtree did not replace the DNS card subtree"
    elif not reversed_ok:
        msg = "behavior_wrong: DNS card subtree not restored after re-selecting the tab"
    else:
        msg = "" if ok else "setup_failed: tab trigger not tappable"
    record("structure_tab_swap_shape", bool(ok), msg,
           depth=0, jump_kind="transition",
           expect="Card subtree swaps DNS<->USB on tab change; 'Private DNS Tile' "
                  "leaves and 'USB Debugging Tile' enters, restored on reverse")

    # VLM: the selected-tab underline indicator (position of the active tab marker)
    restart_app()
    wait_for_foreground()
    tap_text(ui(), "USB Debugging")
    ui()
    vlm_assert("structure_tab_swap_shape",
               "There are two tabs, 'Private DNS' and 'USB Debugging'. Is the coloured "
               "selection underline positioned under the 'USB Debugging' tab (the right "
               "tab), not under 'Private DNS'?",
               RESULTS_DIR, tag="position", depth=0)


@and_test(expects=("structure_tab_indicator_default_vlm",))
def test_tab_indicator_default_vlm():
    """On launch the selection underline sits under the left tab (Private DNS)."""
    restart_app()
    wait_for_foreground()
    ui()
    vlm_assert("structure_tab_indicator_default",
               "There are two tabs, 'Private DNS' and 'USB Debugging'. Is the coloured "
               "selection underline positioned under the 'Private DNS' tab (the left "
               "tab)?",
               RESULTS_DIR, tag="position", depth=0)


# ══════════════════════════════════════════════════════════════════════════════
# SHAPE DELTA — conditional reveal (VPN detection radios appear/hide)
# ══════════════════════════════════════════════════════════════════════════════

@and_test(expects=("structure_vpn_reveal_shape", "structure_vpn_reveal_shape_vlm"))
def test_vpn_reveal_shape():
    """Toggling 'Automatically disable DNS on VPN connections' ON reveals a
    'VPN Detection Mode:' label plus RadioButton widgets that are absent by
    default; toggling OFF hides them again. Assert the RadioButton-class count
    delta (0 -> >=1) and its restoration, plus the label appearing."""
    clear_data()  # guarantee the VPN pref starts OFF (its default)
    launch_app()
    wait_for_foreground()
    nodes0 = ui()
    radios_before = len(find_by_class(nodes0, "RadioButton"))
    label_before = find_node(nodes0, text="VPN Detection Mode:") is not None

    tapped_on = tap_text(ui(), "Automatically disable DNS on VPN connections")
    on_nodes = ui()
    radios_on = len(find_by_class(on_nodes, "RadioButton"))
    label_on = find_node(on_nodes, text="VPN Detection Mode:") is not None
    revealed = tapped_on and radios_before == 0 and radios_on >= 1 and label_on

    tapped_off = tap_text(ui(), "Automatically disable DNS on VPN connections")
    off_nodes = ui()
    radios_off = len(find_by_class(off_nodes, "RadioButton"))
    label_off = find_node(off_nodes, text="VPN Detection Mode:") is not None
    hidden = tapped_off and radios_off == radios_before and not label_off

    ok = revealed and hidden and not label_before
    if not revealed:
        msg = "behavior_wrong: VPN detection radios/label did not appear when toggled on"
    elif not hidden:
        msg = "behavior_wrong: VPN detection radios/label did not hide when toggled off"
    else:
        msg = ""
    record("structure_vpn_reveal_shape", bool(ok), msg,
           depth=0, jump_kind="transition",
           expect="RadioButton count 0->>=1 and 'VPN Detection Mode:' label appears on "
                  "VPN toggle ON, both removed on toggle OFF")

    # VLM: the revealed radio-row layout (a radio button beside its label)
    clear_data()
    launch_app()
    wait_for_foreground()
    tap_text(ui(), "Automatically disable DNS on VPN connections")
    ui()
    vlm_assert("structure_vpn_reveal_shape",
               "Below the checked 'Automatically disable DNS on VPN connections' row, is "
               "there a 'VPN Detection Mode:' heading followed by at least one circular "
               "radio-button control next to a text label?",
               RESULTS_DIR, tag="shape", depth=0)


@and_test(expects=("structure_network_reveal_shape",))
def test_network_reveal_shape():
    """Toggling 'Auto-switch DNS based on network type' ON reveals a
    'Network Detection Mode:' subtree that is absent by default. Per the prior
    run, the toggle-back-to-restore path was flaky, so we assert only the stable
    ON-delta (new keys appear)."""
    clear_data()  # guarantee the auto-switch pref starts OFF (its default)
    launch_app()
    wait_for_foreground()
    before = and_showing(ui())
    mode_before = any("DNS on WiFi:" in k for k in before)

    tapped = tap_text(ui(), "Auto-switch DNS based on network type")
    after = and_showing()
    # 'DNS on WiFi:' is the first revealed control and stays in view (the
    # 'Network Detection Mode:' radios sit below the fold).
    appeared = and_reach(after=after, before=before, target="DNS on WiFi:")
    grew = surface_changed(before, after, min_delta=2)

    ok = tapped and not mode_before and appeared and grew
    record("structure_network_reveal_shape", bool(ok),
           "" if ok else "behavior_wrong: network-type detection subtree did not appear "
                          "when the auto-switch toggle was turned on",
           depth=0, jump_kind="transition",
           expect="Network-type DNS subtree appears (surface grows, 'DNS on WiFi:' "
                  "control enters) on auto-switch toggle ON")


# ══════════════════════════════════════════════════════════════════════════════
# SHAPE DELTA — dialogs add a new window layer (depth 1, backed by and_enter_dialog)
# ══════════════════════════════════════════════════════════════════════════════

@and_test(expects=("structure_about_dialog_shape", "structure_about_dialog_shape_vlm"))
def test_about_dialog_shape():
    """The Help '?' icon opens the About dialog (WRITE_SECURE_SETTINGS is granted,
    so no permission dialog). Its window adds dialog-only keys ('Permission Setup
    Help', 'Close') that do not exist on the DNS tab. BACK removes the layer and
    the DNS card subtree returns."""
    restart_app()
    wait_for_foreground()
    nodes = ui()
    opened, after = and_enter_dialog(nodes, content_desc="Help / Grant Permission",
                                     target="Permission Setup Help")
    close_present = any("Close" in k for k in after)
    dialog_shape = opened and close_present

    and_back()
    restored = and_showing()
    back_ok = and_reach(after, restored, target="Private DNS Tile")

    ok = dialog_shape and back_ok
    if not dialog_shape:
        msg = "behavior_wrong: About dialog layer did not appear on the Help icon"
    elif not back_ok:
        msg = "behavior_wrong: dialog layer did not dismiss back to the DNS surface"
    else:
        msg = ""
    record("structure_about_dialog_shape", bool(ok), msg,
           depth=1, jump_kind="transition",
           expect="About dialog window adds 'Permission Setup Help'+'Close' keys over "
                  "the DNS tab, removed on BACK (DNS card restored)")

    # VLM: the dialog is a card layered ON TOP of the (dimmed) underlying list
    restart_app()
    wait_for_foreground()
    and_enter_dialog(ui(), content_desc="Help / Grant Permission",
                     target="Permission Setup Help")
    vlm_assert("structure_about_dialog_shape",
               "Is a rounded dialog card layered on top of the screen, with the "
               "underlying app content still faintly visible and dimmed around the "
               "card's edges?",
               RESULTS_DIR, tag="layout", depth=1)


@and_test(expects=("structure_addhost_dialog_shape",))
def test_addhost_dialog_shape():
    """Tapping 'Add Custom Hostname' opens a dialog whose subtree adds two EditText
    fields (Display Name + Hostname) that are absent on the DNS tab (0 EditText).
    Cancel removes the layer and the DNS card subtree returns."""
    restart_app()
    wait_for_foreground()
    nodes0 = ui()
    edits_before = len(find_by_class(nodes0, "EditText"))
    before = and_showing(nodes0)

    tapped = tap_text(nodes0, "Add Custom Hostname")
    on_nodes = ui()
    edits_on = len(find_by_class(on_nodes, "EditText"))
    after = and_showing(on_nodes)
    field_delta = tapped and edits_before == 0 and edits_on == 2
    if field_delta:
        and_enter("Add Hostname")

    # reverse via Cancel (never Save/Delete). Cancel alone dismisses the dialog
    # window, so its EditText subtree must be gone — do NOT press BACK on top of
    # Cancel (that would navigate the underlying app).
    cancel = find_node(ui(), text="Cancel")
    tapped_cancel = bool(cancel and safe_tap(cancel))
    back_nodes = ui()
    edits_after_cancel = len(find_by_class(back_nodes, "EditText"))
    reversed_ok = tapped_cancel and edits_after_cancel == 0

    ok = field_delta and reversed_ok
    if not field_delta:
        msg = "behavior_wrong: Add Hostname dialog did not add two text-entry fields"
    elif not reversed_ok:
        msg = "behavior_wrong: dialog EditText subtree not removed after Cancel"
    else:
        msg = ""
    record("structure_addhost_dialog_shape", bool(ok), msg,
           depth=1, jump_kind="transition",
           expect="Add Hostname dialog adds 2 EditText fields (0->2) over the DNS tab; "
                  "the field subtree is removed (2->0) on Cancel")


@and_test(expects=("structure_dnsinfo_dialog_shape",))
def test_dnsinfo_dialog_shape():
    """Tapping the 'Info about Cloudflare (1.1.1.1)' icon opens a DNS-info dialog
    whose window adds an 'About Cloudflare (1.1.1.1)' + 'Close' subtree and hides
    the DNS card subtree. BACK removes the layer and the DNS card returns."""
    restart_app()
    wait_for_foreground()
    nodes = ui()
    opened, after = and_enter_dialog(nodes,
                                     content_desc="Info about Cloudflare (1.1.1.1)",
                                     target="About Cloudflare (1.1.1.1)")
    close_present = any("Close" in k for k in after)
    dns_gone = not any("Private DNS Tile" in k for k in after)
    dialog_shape = opened and close_present and dns_gone

    and_back()
    restored = and_showing()
    back_ok = and_reach(after, restored, target="Private DNS Tile")

    ok = dialog_shape and back_ok
    if not dialog_shape:
        msg = "behavior_wrong: DNS-info dialog layer did not appear on the info icon"
    elif not back_ok:
        msg = "behavior_wrong: DNS-info dialog did not dismiss back to the DNS surface"
    else:
        msg = ""
    record("structure_dnsinfo_dialog_shape", bool(ok), msg,
           depth=1, jump_kind="transition",
           expect="DNS-info dialog window adds 'About Cloudflare (1.1.1.1)'+'Close' and "
                  "removes the DNS card subtree, restored on BACK")


# ══════════════════════════════════════════════════════════════════════════════
# SHAPE DELTA — Shortcut Actions screen + the info-pill AnimatedVisibility expand
# ══════════════════════════════════════════════════════════════════════════════

@and_test(expects=("structure_shortcut_screen_shape", "structure_shortcut_screen_shape_vlm"))
def test_shortcut_screen_shape():
    """The top-bar 'Open shortcut settings' icon reaches the Shortcut Actions
    surface: its 'Shortcut Actions'/'Active shortcuts: 0 / 15' subtree APPEARS
    (with per-row Star buttons + Switch views) and the DNS card subtree
    DISAPPEARS. BACK returns to the DNS tab."""
    restart_app()
    wait_for_foreground()
    before = and_showing(ui())

    reached = and_enter_shortcuts(ui())
    after = and_showing()
    pill_present = any("Active shortcuts" in k for k in after)
    switches = len(find_nodes(ui(), **{"class": "android.view.View", "checkable": True}))
    rows_shape = reached and pill_present and switches >= 5

    and_back()
    restored = and_showing()
    back_ok = and_reach(after, restored, target="Add Custom Hostname",
                        source="Shortcut Actions")

    ok = rows_shape and back_ok
    if not reached:
        msg = "feature_missing: Shortcut Actions surface not reached from the shortcut icon"
    elif not pill_present or switches < 5:
        msg = "behavior_wrong: shortcut screen missing the info pill / per-row switches"
    elif not back_ok:
        msg = "behavior_wrong: BACK did not return to the DNS tab surface"
    else:
        msg = ""
    record("structure_shortcut_screen_shape", bool(ok), msg,
           depth=1, jump_kind="transition",
           expect="Shortcut Actions surface replaces the DNS card; adds the "
                  "'Active shortcuts: N / 15' pill and >=5 per-row toggle Views; on "
                  "BACK 'Shortcut Actions' leaves and the DNS tab (Add Custom Hostname) "
                  "returns")

    # VLM: each shortcut row carries a star icon and a toggle switch
    restart_app()
    wait_for_foreground()
    and_enter_shortcuts(ui())
    vlm_assert("structure_shortcut_screen_shape",
               "In the shortcut list rows (e.g. 'DNS Off', 'DNS Auto'), does each row "
               "have both a star icon and a pill-shaped toggle switch on its right side?",
               RESULTS_DIR, tag="shape", depth=1)


@and_test(expects=("structure_shortcut_pill_expand_shape", "structure_shortcut_pill_expand_shape_vlm"))
def test_shortcut_pill_expand_shape():
    """On the Shortcut Actions screen, tapping the 'Active shortcuts: N / 15' info
    pill expands an AnimatedVisibility description subtree (a long paragraph
    mentioning 'Samsung Routines') that is absent when collapsed. Tapping again
    collapses it and the paragraph is removed again."""
    restart_app()
    wait_for_foreground()
    reached = and_enter_shortcuts(ui())

    collapsed = and_showing()
    para_before = any("Samsung Routines" in k for k in collapsed)

    pill = find_node(ui(), text="Active shortcuts: 0 / 15")
    tapped_open = safe_tap(pill) if pill else False
    expanded = and_showing()
    para_after = any("Samsung Routines" in k for k in expanded)
    grew = surface_changed(collapsed, expanded, min_delta=1)
    revealed = reached and tapped_open and not para_before and para_after and grew

    # reverse: tap the pill again, paragraph must be removed
    pill2 = find_node(ui(), text="Active shortcuts: 0 / 15")
    tapped_close = safe_tap(pill2) if pill2 else False
    recollapsed = and_showing()
    para_final = any("Samsung Routines" in k for k in recollapsed)
    hidden = tapped_close and not para_final

    ok = revealed and hidden
    if not revealed:
        msg = "behavior_wrong: info-pill did not expand an added description subtree"
    elif not hidden:
        msg = "behavior_wrong: info-pill description not removed when collapsed again"
    else:
        msg = ""
    record("structure_shortcut_pill_expand_shape", bool(ok), msg,
           depth=1, jump_kind="transition",
           expect="Tapping the info pill adds a long description subtree (surface grows), "
                  "removed when tapped again")

    # VLM: the info pill styling (a filled/coloured rounded pill, not a plain row)
    restart_app()
    wait_for_foreground()
    and_enter_shortcuts(ui())
    vlm_assert("structure_shortcut_pill_expand_shape",
               "Near the top of the Shortcut Actions screen, is the 'Active shortcuts: "
               "0 / 15' text inside a filled rounded pill/banner with a distinct "
               "background colour, spanning the width of the screen?",
               RESULTS_DIR, tag="shape", depth=1)


# ══════════════════════════════════════════════════════════════════════════════
# SHAPE DELTA — USB tab exposes a disabled/greyed checkbox class not on the DNS tab
# ══════════════════════════════════════════════════════════════════════════════

@and_test(expects=("structure_usb_disabled_shape", "structure_usb_disabled_shape_vlm"))
def test_usb_disabled_shape():
    """The USB tab's shape differs from the DNS tab: because Developer Options are
    disabled, it renders enabled=false CheckBox widgets (the greyed USB Debug
    On/Off + auto-revert). The DNS tab has zero disabled CheckBoxes. Switching
    back to the DNS tab restores that (no disabled CheckBoxes)."""
    restart_app()
    wait_for_foreground()
    dns_nodes = ui()
    dns_disabled = len(find_nodes(dns_nodes, **{"class": "android.widget.CheckBox",
                                                "enabled": False}))

    tapped_usb = tap_text(ui(), "USB Debugging")
    usb_nodes = ui()
    usb_disabled = len(find_nodes(usb_nodes, **{"class": "android.widget.CheckBox",
                                                "enabled": False}))
    delta_ok = tapped_usb and dns_disabled == 0 and usb_disabled >= 2

    tapped_dns = tap_text(ui(), "Private DNS")
    back_nodes = ui()
    back_disabled = len(find_nodes(back_nodes, **{"class": "android.widget.CheckBox",
                                                  "enabled": False}))
    restored = tapped_dns and back_disabled == 0

    ok = delta_ok and restored
    if not delta_ok:
        msg = "behavior_wrong: USB tab did not expose >=2 disabled CheckBox widgets"
    elif not restored:
        msg = "behavior_wrong: DNS tab still shows disabled CheckBoxes after switching back"
    else:
        msg = ""
    record("structure_usb_disabled_shape", bool(ok), msg,
           depth=0, jump_kind="transition",
           expect="Disabled-CheckBox count 0 (DNS) -> >=2 (USB, Dev Options off), "
                  "restored to 0 on switching back")

    # VLM: the greyed USB checkboxes render at reduced opacity vs the enabled ones
    restart_app()
    wait_for_foreground()
    tap_text(ui(), "USB Debugging")
    ui()
    vlm_assert("structure_usb_disabled_shape",
               "On the USB Debugging card, are the 'USB Debug On' and 'USB Debug Off' "
               "rows visibly greyed out / faded (lower opacity) compared with the darker, "
               "fully-visible 'Also toggle Developer Options' row below them?",
               RESULTS_DIR, tag="color", depth=0)


# ══════════════════════════════════════════════════════════════════════════════

TESTS = [
    test_gate_launch,
    test_gate_top_surfaces,
    test_dns_card_layout_vlm,
    test_tab_swap_shape,
    test_tab_indicator_default_vlm,
    test_vpn_reveal_shape,
    test_network_reveal_shape,
    test_about_dialog_shape,
    test_addhost_dialog_shape,
    test_dnsinfo_dialog_shape,
    test_shortcut_screen_shape,
    test_shortcut_pill_expand_shape,
    test_usb_disabled_shape,
]

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