File size: 29,226 Bytes
966ca88
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
#!/usr/bin/env python3
"""STRUCTURE lane — PlusPlusBattery (Jetpack Compose, single Activity).

Owns exactly two things:
  (1) the GATE: the app launches to the Dashboard with its top-level surfaces
      present (main container, title, 4-item bottom-nav host).  jump_kind="none".
  (2) tree-SHAPE deltas: an interaction that changes the view tree's SHAPE — a
      dialog adds a modal window layer (node count / a widget CLASS appears),
      the FAB expand/collapse re-shapes the overlay, a tab renders a distinct
      subtree (a class of checkable View that the Dashboard card list lacks),
      a deep surface exposes a column of CheckBox / a row of SeekBar widgets.
      Each is recorded as jump_kind="transition" and REVERSED to prove it restores.

CEDES to other lanes: content strings (text), checked/enabled/selected (states),
reachability of a named screen (navigation), computed values (computation),
CRUD on records (interaction).  This file asserts SHAPE only.

All observed values below were read off the running original with uiautomator.
"""
import os, sys
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
from android_testgen_kit import *          # noqa: F403,F401

RESULTS_DIR = sys.argv[2] if len(sys.argv) > 2 else os.path.dirname(os.path.abspath(__file__))

# Bottom-nav sibling tabs (content-desc on every surface except the selected one,
# which renders as a plain text label instead).
NAV_SIBLINGS = ("Monitor", "History", "Settings")


# ── depth-family helpers (named and_enter_* so the depth auditor accepts the
# declared depth>=1 as BACKED navigation, per the kit's DEPTH_FWD_RE). Each taps a
# real user-path trigger, confirms the surface actually changed, then increments
# the kit depth counter via the kit's and_enter(). ─────────────────────────────

def and_enter_dialog(nodes, *, text="", content_desc="", info_near="") -> tuple[bool, set]:
    """Open a dialog from a trigger and confirm the surface re-shaped. Returns
    (opened, before_showing). Counts one surface. `info_near` disambiguates a
    duplicate 'Show Info' button by the y-row of the given label."""
    trig = None
    if info_near:
        trig = _info_near(nodes, info_near)
    elif text:
        trig = find_node(nodes, text=text) or find_node_ci(nodes, text)
    elif content_desc:
        trig = find_node(nodes, content_desc=content_desc)
    if trig is None:
        return False, set()
    before = and_showing(nodes)
    if not tap_node(trig):
        return False, before
    after = and_showing()
    if not surface_changed(before, after):
        return False, before
    and_enter("dialog")
    return True, before


def and_enter_tab(nodes, content_desc: str) -> tuple[bool, set]:
    """Tap a bottom-nav tab and confirm its subtree replaced the current one.
    Counts one surface. Returns (ok, before_showing)."""
    trig = find_node(nodes, content_desc=content_desc)
    if trig is None:
        return False, set()
    before = and_showing(nodes)
    if not tap_node(trig):
        return False, before
    after = and_showing()
    if not surface_changed(before, after):
        return False, before
    and_enter(content_desc)
    return True, before


def and_enter_deep(nodes, *, text: str) -> tuple[bool, set]:
    """From a tab, open a nested settings surface (Monitor entry settings /
    Floating window settings / About). Counts one MORE surface (depth 2)."""
    trig = find_node(nodes, text=text) or find_node_ci(nodes, text)
    if trig is None:
        return False, set()
    before = and_showing(nodes)
    if not tap_node(trig):
        return False, before
    after = and_showing()
    if not surface_changed(before, after):
        return False, before
    and_enter(text)
    return True, before


def _info_near(nodes, label: str) -> dict | None:
    """The 'Show Info' button closest in y to `label` (the content-desc repeats
    across cards, so proximity is the only honest disambiguator)."""
    ln = find_node(nodes, text=label) or find_node_ci(nodes, label)
    if not ln or "_center" not in ln:
        return None
    infos = find_nodes(nodes, content_desc="Show Info")
    infos = [n for n in infos if "_center" in n]
    if not infos:
        return None
    return min(infos, key=lambda n: abs(n["_center"][1] - ln["_center"][1]))


def _count_checkable(nodes) -> int:
    return len([n for n in nodes if n.get("checkable") == "true"])


# ══════════════════════════════════════════════════════════════════════════════
# (1) THE GATE — top-level surfaces exist. jump_kind="none", depth=0.
#     Kept to 3 of 18 tests (17%), under the 30% existence ceiling.
# ══════════════════════════════════════════════════════════════════════════════

@and_test(expects=("structure_gate_launch", "structure_gate_launch_vlm"))
def test_gate_launch():
    restart_app()
    fg = wait_for_foreground()
    nodes = ui()
    # Main container present: a Compose host FrameLayout/View tree with the
    # dashboard's card TextViews rendered inside it.
    container = bool(find_by_class(nodes, "FrameLayout")) and bool(find_by_class(nodes, "View"))
    cards = len(find_by_class(nodes, "TextView"))
    ok = bool(fg) and container and cards >= 15
    record("structure_gate_launch", ok,
           "" if ok else f"setup_failed: app did not reach a populated foreground "
                         f"(fg={fg}, container={container}, textviews={cards})",
           depth=0, jump_kind="none")
    vlm_assert("structure_gate_launch",
               "Does this screen show a vertical list of battery-info cards with a "
               "bottom navigation bar containing exactly 4 icons evenly spaced across "
               "the bottom edge?",
               RESULTS_DIR, tag="shape")


@and_test(expects=("structure_gate_navhost",))
def test_gate_navhost():
    restart_app()
    wait_for_foreground()
    nodes = ui()
    # 4-item nav host: 3 sibling tabs by content-desc + the selected "Dashboard"
    # label as text (the selected item renders its label instead of a desc).
    siblings = [cd for cd in NAV_SIBLINGS if find_node(nodes, content_desc=cd)]
    selected = bool(find_node(nodes, text="Dashboard"))
    ok = len(siblings) == 3 and selected
    record("structure_gate_navhost", ok,
           "" if ok else f"feature_missing: bottom-nav host incomplete "
                         f"(siblings={siblings}, dashboard_label={selected})",
           depth=0, jump_kind="none")


@and_test(expects=("structure_gate_title_and_cards",))
def test_gate_title_and_cards():
    restart_app()
    wait_for_foreground()
    nodes = ui()
    # App title present as the top TextView, and the dashboard exposes a stack of
    # value Buttons (info/swap/calibrate/expand affordances) — its shape signature.
    title = bool(find_node(nodes, text="Plus Plus Battery"))
    buttons = len(find_by_class(nodes, "Button"))
    ok = title and buttons == 8
    record("structure_gate_title_and_cards", ok,
           "" if ok else f"feature_missing: dashboard shape wrong "
                         f"(title={title}, buttons={buttons}, expected 8)",
           depth=0, jump_kind="none")


# ══════════════════════════════════════════════════════════════════════════════
# (2) DIALOG SHAPE DELTAS — a dialog adds a modal window layer. depth=1.
# ══════════════════════════════════════════════════════════════════════════════

@and_test(expects=("structure_calibrate_dialog_shape", "structure_calibrate_dialog_shape_vlm"))
def test_calibrate_dialog_shape():
    restart_app()
    wait_for_foreground()
    nodes = ui()
    before_rb = len(find_by_class(nodes, "RadioButton"))          # 0 on dashboard
    opened, before = and_enter_dialog(nodes, content_desc="Calibrate")
    dlg = ui()
    after_rb = len(find_by_class(dlg, "RadioButton"))             # 2 in the dialog
    grew = opened and after_rb == before_rb + 2 and surface_changed(before, and_showing(dlg))
    # Ask the VLM WHILE the dialog is still on screen — a rendered modal-overlay
    # property the tree cannot express. (Fired here, not after Close, so the
    # screenshot actually contains the dialog.)
    settle("tap")
    vlm_assert("structure_calibrate_dialog_shape",
               "Is there a rounded dialog box floating on top of the screen, with the "
               "app content behind it faded or greyed out?",
               RESULTS_DIR, tag="position")
    # reverse: Close removes the RadioButton subtree
    restored = False
    if opened:
        close = find_node(dlg, text="Close")
        if close and tap_node(close):                            # DANGEROUS word -> tap_node, not safe_tap
            restored = len(find_by_class(ui(), "RadioButton")) == before_rb
    ok = grew and restored
    record("structure_calibrate_dialog_shape", ok,
           "" if ok else f"behavior_wrong: calibrate dialog RadioButton delta wrong "
                         f"(before={before_rb}, after={after_rb}, restored={restored})",
           depth=1, jump_kind="transition",
           expect="RadioButton class 0->2 when Calibrate opens, back to 0 on Close")


@and_test(expects=("structure_cyclecount_info_shape",))
def test_cyclecount_info_shape():
    restart_app()
    wait_for_foreground()
    nodes = ui()
    before_n = len(nodes)
    opened, before = and_enter_dialog(nodes, info_near="Charge Cycle Count")
    dlg = ui()
    # The modal replaces the dashboard window: node count collapses sharply and a
    # single "Close" Button appears in place of the card stack.
    shrank = opened and len(dlg) < before_n - 30
    has_close = bool(find_node(dlg, text="Close"))
    changed = opened and surface_changed(before, and_showing(dlg))
    restored = False
    if opened:
        close = find_node(dlg, text="Close")
        if close and tap_node(close):
            restored = len(ui()) >= before_n - 2
    ok = shrank and has_close and changed and restored
    record("structure_cyclecount_info_shape", ok,
           "" if ok else f"behavior_wrong: cycle-count info dialog shape delta wrong "
                         f"(before_n={before_n}, dlg_n={len(dlg)}, close={has_close}, "
                         f"restored={restored})",
           depth=1, jump_kind="transition",
           expect="dashboard subtree (~88 nodes) replaced by a small modal (~13) with a Close button, restored on Close")


@and_test(expects=("structure_voltage_info_shape",))
def test_voltage_info_shape():
    restart_app()
    wait_for_foreground()
    nodes = ui()
    before_n = len(nodes)
    opened, before = and_enter_dialog(nodes, info_near="Battery Voltage")
    dlg = ui()
    shrank = opened and len(dlg) < before_n - 30
    has_close = bool(find_node(dlg, text="Close"))
    changed = opened and surface_changed(before, and_showing(dlg))
    restored = False
    if opened:
        close = find_node(dlg, text="Close")
        if close and tap_node(close):
            restored = len(ui()) >= before_n - 2
    ok = shrank and has_close and changed and restored
    record("structure_voltage_info_shape", ok,
           "" if ok else f"behavior_wrong: voltage info dialog shape delta wrong "
                         f"(before_n={before_n}, dlg_n={len(dlg)}, close={has_close}, "
                         f"restored={restored})",
           depth=1, jump_kind="transition",
           expect="dashboard subtree replaced by a small modal with a Close button, restored on Close")


@and_test(expects=("structure_estfcc_info_shape",))
def test_estfcc_info_shape():
    restart_app()
    wait_for_foreground()
    nodes = ui()
    before_n = len(nodes)
    opened, before = and_enter_dialog(nodes, info_near="Full Charge Capacity")
    dlg = ui()
    shrank = opened and len(dlg) < before_n - 30
    has_close = bool(find_node(dlg, text="Close"))
    changed = opened and surface_changed(before, and_showing(dlg))
    restored = False
    if opened:
        close = find_node(dlg, text="Close")
        if close and tap_node(close):
            restored = len(ui()) >= before_n - 2
    ok = shrank and has_close and changed and restored
    record("structure_estfcc_info_shape", ok,
           "" if ok else f"behavior_wrong: FCC info dialog shape delta wrong "
                         f"(before_n={before_n}, dlg_n={len(dlg)}, close={has_close}, "
                         f"restored={restored})",
           depth=1, jump_kind="transition",
           expect="dashboard subtree replaced by a small modal with a Close button, restored on Close")


@and_test(expects=("structure_history_help_shape",))
def test_history_help_shape():
    restart_app()
    wait_for_foreground()
    ok_tab, _ = and_enter_tab(ui(), "History")
    h = ui()
    before_n = len(h)
    before = and_showing(h)
    help_btn = find_node(h, content_desc="Help")
    opened = bool(help_btn) and tap_node(help_btn)
    dlg = ui()
    shrank = opened and len(dlg) < before_n - 15
    has_close = bool(find_node(dlg, text="Close"))
    changed = opened and surface_changed(before, and_showing(dlg))
    restored = False
    if opened:
        close = find_node(dlg, text="Close")
        if close and tap_node(close):
            restored = len(ui()) >= before_n - 2
    ok = ok_tab and shrank and has_close and changed and restored
    record("structure_history_help_shape", ok,
           "" if ok else f"behavior_wrong: history Help dialog shape delta wrong "
                         f"(tab={ok_tab}, before_n={before_n}, dlg_n={len(dlg)}, "
                         f"close={has_close}, restored={restored})",
           depth=1, jump_kind="transition",
           expect="History list subtree (~43) replaced by a Help modal (~13) with Close, restored on Close")


@and_test(expects=("structure_history_export_shape",))
def test_history_export_shape():
    restart_app()
    wait_for_foreground()
    ok_tab, _ = and_enter_tab(ui(), "History")
    h = ui()
    before_n = len(h)
    before = and_showing(h)
    exp = find_node(h, content_desc="Export")
    opened = bool(exp) and tap_node(exp)
    dlg = ui()
    # Confirm modal adds an Export/Cancel action pair as a new layer.
    changed = opened and surface_changed(before, and_showing(dlg))
    has_actions = bool(find_node(dlg, text="Cancel")) and bool(find_node(dlg, text="Export"))
    restored = False
    if opened:
        cancel = find_node(dlg, text="Cancel")               # safe dismissal, no write
        if cancel and tap_node(cancel):
            restored = len(ui()) >= before_n - 2
    ok = ok_tab and changed and has_actions and restored
    record("structure_history_export_shape", ok,
           "" if ok else f"behavior_wrong: history Export dialog shape delta wrong "
                         f"(tab={ok_tab}, changed={changed}, actions={has_actions}, "
                         f"restored={restored})",
           depth=1, jump_kind="transition",
           expect="Export confirm modal (Export/Cancel buttons) layered over the History list, restored on Cancel")


@and_test(expects=("structure_refresh_dialog_shape",))
def test_refresh_dialog_shape():
    restart_app()
    wait_for_foreground()
    ok_tab, _ = and_enter_tab(ui(), "Settings")
    # The refresh row is below the fold — scroll it into view first.
    ri = scroll_into_view("Set refresh interval (ms)")
    s = ui()
    before_n = len(s)
    before = and_showing(s)
    opened = False
    if ri:
        ri = find_node(s, text="Set refresh interval (ms)") or find_node_ci(s, "Set refresh interval (ms)")
        opened = bool(ri) and tap_node(ri)
        if opened:
            and_enter("refresh_dialog")
    dlg = ui()
    shrank = opened and len(dlg) < before_n - 30
    has_close = bool(find_node(dlg, text="Close"))
    changed = opened and surface_changed(before, and_showing(dlg))
    restored = False
    if opened:
        close = find_node(dlg, text="Close")
        if close and tap_node(close):
            restored = len(ui()) >= before_n - 2
    ok = ok_tab and shrank and has_close and changed and restored
    record("structure_refresh_dialog_shape", ok,
           "" if ok else f"behavior_wrong: refresh dialog shape delta wrong "
                         f"(tab={ok_tab}, before_n={before_n}, dlg_n={len(dlg)}, "
                         f"close={has_close}, restored={restored})",
           depth=1, jump_kind="transition",
           expect="Settings subtree (~83) replaced by the refresh modal (~20) with a Close button, restored on Close")


# ══════════════════════════════════════════════════════════════════════════════
# (3) FAB EXPAND SHAPE DELTA — in-place overlay re-shapes the tree. depth=0.
# ══════════════════════════════════════════════════════════════════════════════

@and_test(expects=("structure_fab_expand_shape", "structure_fab_expand_shape_vlm"))
def test_fab_expand_shape():
    restart_app()
    wait_for_foreground()
    nodes = ui()
    fab = find_node(nodes, content_desc="Open menu")
    before = and_showing(nodes)
    opened = bool(fab) and tap_node(fab)
    exp = ui()
    # The speed-dial overlays the dashboard: the collapsed "Open menu" affordance
    # is gone and a "Close menu" affordance plus menu-item subtree appear.
    flipped = opened and (not find_node(exp, content_desc="Open menu")) and \
        bool(find_node(exp, content_desc="Close menu"))
    reshaped = opened and surface_changed(before, and_showing(exp))
    # Ask the VLM WHILE the speed-dial is expanded (screenshot must contain the stack).
    settle("tap")
    vlm_assert("structure_fab_expand_shape",
               "Above the bottom-right circular button, is there a vertical stack of "
               "labeled action buttons that expanded upward from that button?",
               RESULTS_DIR, tag="shape")
    # reverse
    restored = False
    if opened:
        cm = find_node(exp, content_desc="Close menu")
        if cm and tap_node(cm):
            back = ui()
            restored = bool(find_node(back, content_desc="Open menu")) and \
                not find_node(back, content_desc="Close menu")
    ok = flipped and reshaped and restored
    record("structure_fab_expand_shape", ok,
           "" if ok else f"behavior_wrong: FAB expand shape delta wrong "
                         f"(flipped={flipped}, reshaped={reshaped}, restored={restored})",
           depth=0, jump_kind="transition",
           expect="FAB affordance 'Open menu'->'Close menu' with a menu-item subtree overlay, restored on collapse")


@and_test(expects=("structure_fab_position_vlm",))
def test_fab_position():
    # A pure rendered-position claim the tree cannot express: the collapsed FAB
    # sits at the bottom-right, above the nav bar. VLM-only record.
    restart_app()
    wait_for_foreground()
    ui()  # settle a fresh dashboard before the screenshot
    vlm_assert("structure_fab_position",
               "Is there a single round floating action button positioned in the "
               "bottom-right corner of the screen, just above the bottom navigation bar?",
               RESULTS_DIR, tag="position")


# ══════════════════════════════════════════════════════════════════════════════
# (4) TAB CONTENT SHAPE — each tab renders a DISTINCT top-level subtree. depth=1.
#     Signature = a widget CLASS present here and absent on the Dashboard card list.
# ══════════════════════════════════════════════════════════════════════════════

@and_test(expects=("structure_monitor_tab_shape",))
def test_monitor_tab_shape():
    restart_app()
    wait_for_foreground()
    dash = ui()
    dash_chk = _count_checkable(dash)                     # 0 on dashboard
    ok_tab, before = and_enter_tab(dash, "Monitor")
    mon = ui()
    mon_chk = _count_checkable(mon)                       # 2 toggle Views on Monitor
    # Distinct subtree: Monitor adds a class of checkable View the dashboard lacks,
    # and the surface materially changed.
    ok = ok_tab and dash_chk == 0 and mon_chk == 2 and surface_changed(before, and_showing(mon))
    record("structure_monitor_tab_shape", ok,
           "" if ok else f"behavior_wrong: Monitor subtree shape wrong "
                         f"(tab={ok_tab}, dash_checkable={dash_chk}, monitor_checkable={mon_chk})",
           depth=1, jump_kind="transition",
           expect="checkable-View count 0 (Dashboard) -> 2 (Monitor); distinct subtree")


@and_test(expects=("structure_settings_tab_shape", "structure_settings_tab_shape_vlm"))
def test_settings_tab_shape():
    restart_app()
    wait_for_foreground()
    dash = ui()
    dash_chk = _count_checkable(dash)                     # 0
    ok_tab, before = and_enter_tab(dash, "Settings")
    st = ui()
    set_chk = _count_checkable(st)                        # 6 toggle Views on Settings
    ok = ok_tab and dash_chk == 0 and set_chk == 6 and surface_changed(before, and_showing(st))
    record("structure_settings_tab_shape", ok,
           "" if ok else f"behavior_wrong: Settings subtree shape wrong "
                         f"(tab={ok_tab}, dash_checkable={dash_chk}, settings_checkable={set_chk})",
           depth=1, jump_kind="transition",
           expect="checkable-View count 0 (Dashboard) -> 6 (Settings); distinct subtree")
    vlm_assert("structure_settings_tab_shape",
               "Is the settings content organized into multiple grouped section cards, "
               "each containing rows with a toggle switch on the right side?",
               RESULTS_DIR, tag="layout")


@and_test(expects=("structure_history_tab_shape", "structure_history_tab_shape_vlm"))
def test_history_tab_shape():
    restart_app()
    wait_for_foreground()
    dash = ui()
    ok_tab, before = and_enter_tab(dash, "History")
    h = ui()
    # History's signature: a top toolbar exposing Export + Help icon Buttons that
    # the Dashboard does not have, plus its own list subtree (no checkable Views).
    toolbar = bool(find_node(h, content_desc="Export")) and bool(find_node(h, content_desc="Help"))
    no_toggles = _count_checkable(h) == 0
    dash_toolbar = bool(find_node(dash, content_desc="Export"))   # absent on dashboard
    ok = ok_tab and toolbar and no_toggles and not dash_toolbar and \
        surface_changed(before, and_showing(h))
    record("structure_history_tab_shape", ok,
           "" if ok else f"behavior_wrong: History subtree shape wrong "
                         f"(tab={ok_tab}, toolbar={toolbar}, no_toggles={no_toggles}, "
                         f"dash_had_toolbar={dash_toolbar})",
           depth=1, jump_kind="transition",
           expect="History adds an Export+Help toolbar subtree absent on the Dashboard")
    vlm_assert("structure_history_tab_shape",
               "Are there two circular icon buttons in the top-right corner, with a "
               "list of history record entries below them?",
               RESULTS_DIR, tag="position")


# ══════════════════════════════════════════════════════════════════════════════
# (5) DEEP SURFACE SHAPE — depth=2 (Dashboard -> tab -> nested settings).
#     Backed by TWO and_enter_* calls so the depth auditor accepts depth=2.
# ══════════════════════════════════════════════════════════════════════════════

@and_test(expects=("structure_monitor_entry_checkboxes", "structure_monitor_entry_checkboxes_vlm"))
def test_monitor_entry_checkboxes():
    restart_app()
    wait_for_foreground()
    ok_tab, _ = and_enter_tab(ui(), "Monitor")
    mon = ui()
    ok_deep, before = and_enter_deep(mon, text="Monitor entry settings")
    mes = ui()
    # Deep surface signature: a column of CheckBox widgets (none exist anywhere on
    # the Dashboard/Monitor card surfaces).
    cbs = len(find_by_class(mes, "CheckBox"))
    ok = ok_tab and ok_deep and cbs == 9 and surface_changed(before, and_showing(mes))
    record("structure_monitor_entry_checkboxes", ok,
           "" if ok else f"behavior_wrong: Monitor Entry Settings CheckBox count wrong "
                         f"(tab={ok_tab}, deep={ok_deep}, checkboxes={cbs}, expected 9)",
           depth=2, jump_kind="transition",
           expect="CheckBox class count = 9 on Monitor Entry Settings (0 on the tabs above it)")
    vlm_assert("structure_monitor_entry_checkboxes",
               "Is this a vertical list of rows where each row has a filled checkbox "
               "at its far left followed by a text label?",
               RESULTS_DIR, tag="position")


@and_test(expects=("structure_floating_window_seekbars", "structure_floating_window_seekbars_vlm"))
def test_floating_window_seekbars():
    restart_app()
    wait_for_foreground()
    ok_tab, _ = and_enter_tab(ui(), "Monitor")
    mon = ui()
    ok_deep, before = and_enter_deep(mon, text="Floating window settings")
    fws = ui()
    # Deep surface signature: a set of SeekBar (Compose Slider) widgets that no
    # other surface in this app renders.
    sbs = len(find_by_class(fws, "SeekBar"))
    ok = ok_tab and ok_deep and sbs == 3 and surface_changed(before, and_showing(fws))
    record("structure_floating_window_seekbars", ok,
           "" if ok else f"behavior_wrong: Floating Window Settings SeekBar count wrong "
                         f"(tab={ok_tab}, deep={ok_deep}, seekbars={sbs}, expected 3)",
           depth=2, jump_kind="transition",
           expect="SeekBar class count = 3 on Floating Window Settings")
    vlm_assert("structure_floating_window_seekbars",
               "Does this screen show horizontal slider controls and a horizontal row "
               "of rounded color-option chips grouped inside cards?",
               RESULTS_DIR, tag="shape")


@and_test(expects=("structure_about_shape", "structure_about_shape_vlm"))
def test_about_shape():
    restart_app()
    wait_for_foreground()
    ok_tab, _ = and_enter_tab(ui(), "Settings")
    # About is below the fold on Settings; scroll then open it.
    ab = scroll_into_view("About")
    st = ui()
    ok_deep = False
    before = set()
    if ab:
        ok_deep, before = and_enter_deep(st, text="About")
    ab_nodes = ui()
    # About's signature: a Back affordance + a scrollable long text block, and it
    # carries NONE of Settings' toggle Views.
    back = bool(find_node(ab_nodes, content_desc="Back"))
    has_scroll = bool(find_by_class(ab_nodes, "ScrollView"))
    no_toggles = _count_checkable(ab_nodes) == 0
    ok = ok_tab and ok_deep and back and has_scroll and no_toggles and \
        surface_changed(before, and_showing(ab_nodes))
    record("structure_about_shape", ok,
           "" if ok else f"behavior_wrong: About subtree shape wrong "
                         f"(tab={ok_tab}, deep={ok_deep}, back={back}, "
                         f"scroll={has_scroll}, toggles0={no_toggles})",
           depth=2, jump_kind="transition",
           expect="About = Back + a scrollable text block, none of Settings' toggle Views")
    vlm_assert("structure_about_shape",
               "Is this screen dominated by a single large block of paragraph text "
               "(version, repo, author and battery-field descriptions) rather than "
               "rows of controls?",
               RESULTS_DIR, tag="layout")


TESTS = [
    test_gate_launch,
    test_gate_navhost,
    test_gate_title_and_cards,
    test_calibrate_dialog_shape,
    test_cyclecount_info_shape,
    test_voltage_info_shape,
    test_estfcc_info_shape,
    test_history_help_shape,
    test_history_export_shape,
    test_refresh_dialog_shape,
    test_fab_expand_shape,
    test_fab_position,
    test_monitor_tab_shape,
    test_settings_tab_shape,
    test_history_tab_shape,
    test_monitor_entry_checkboxes,
    test_floating_window_seekbars,
    test_about_shape,
]

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