File size: 26,898 Bytes
d292518
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
#!/usr/bin/env python3
"""STRUCTURE lane — Vector Pinball (com.dozingcatsoftware.bouncy).

Owns (a) the launch GATE and (b) tree-SHAPE deltas: a panel/subtree/dialog that
appears or disappears, and child-count shape consequences. SHAPE only — content
strings are text's, checked/selected are states', reachability proofs are
navigation's. Gameplay content (score, ball, field layout) is CANVAS-rendered and
never reaches the view tree, so those facts are the VLM budget for this lane.

All resource-ids matched by LAST segment (node_by_id) so the package prefix, which
differs between original and recreation, is never compared.
"""
import os, sys
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
from android_testgen_kit import *          # noqa: F403,F401


# ── Depth helpers (naming is the requirement that backs a depth>=1 claim) ────────
def and_enter_gameplay(nodes):
    """From the fresh main menu, tap START GAME and confirm gameplay was entered:
    buttonPanel gone + pauseIcon present. Returns (ok, before_keys, after_keys)."""
    btn = node_by_id(nodes, "startGameButton")
    before = and_showing(nodes)
    if not btn or not tap_node(btn):
        return False, before, before
    after_nodes = ui()
    after = and_showing(after_nodes)
    ok = (node_by_id(after_nodes, "buttonPanel") is None
          and node_by_id(after_nodes, "pauseIcon") is not None)
    if ok:
        and_enter("gameplay")
    return ok, before, after


def and_enter_paused_menu(nodes):
    """From gameplay, tap pauseIcon and confirm the paused menu appeared:
    buttonPanel back + resumeGameButton present. Returns (ok, before, after)."""
    icon = node_by_id(nodes, "pauseIcon")
    before = and_showing(nodes)
    if not icon or not tap_node(icon):
        return False, before, before
    after_nodes = ui()
    after = and_showing(after_nodes)
    ok = (node_by_id(after_nodes, "buttonPanel") is not None
          and node_by_id(after_nodes, "resumeGameButton") is not None)
    if ok:
        and_enter("paused_menu")
    return ok, before, after


def and_reach_highscores(nodes):
    """From the main menu, tap HIGH SCORES; PROVE the jump (highScorePanel
    appeared, buttonPanel vanished). Returns (ok, before, after)."""
    btn = node_by_id(nodes, "highScoreButton")
    before = and_showing(nodes)
    if not btn or not tap_node(btn):
        return False, before, before
    after_nodes = ui()
    after = and_showing(after_nodes)
    ok = (node_by_id(after_nodes, "highScorePanel") is not None
          and node_by_id(after_nodes, "buttonPanel") is None)
    if ok:
        and_enter("highscores")
    return ok, before, after


def and_reach_preferences(nodes):
    """From the main menu, tap PREFERENCES; confirm the Preferences activity's
    ListView appeared and the menu buttonPanel is gone. Returns (ok, nodes_after)."""
    btn = node_by_id(nodes, "preferencesButton")
    if not btn or not tap_node(btn):
        return False, nodes
    after_nodes = ui()
    ok = (find_by_class(after_nodes, "ListView")
          and node_by_id(after_nodes, "buttonPanel") is None
          and find_node(after_nodes, text="Sound") is not None)
    if ok:
        and_enter("preferences")
    return ok, after_nodes


def and_reach_about(nodes):
    """From the main menu, tap HELP; confirm the About activity's aboutTextView
    is present and the menu buttonPanel is gone. Returns (ok, nodes_after)."""
    btn = node_by_id(nodes, "aboutButton")
    if not btn or not tap_node(btn):
        return False, nodes
    after_nodes = ui()
    ok = (node_by_id(after_nodes, "aboutTextView") is not None
          and node_by_id(after_nodes, "buttonPanel") is None)
    if ok:
        and_enter("about")
    return ok, after_nodes


def and_enter_pref_dialog(nodes, row_title):
    """Inside Preferences, tap a row title and confirm a dialog subtree opened
    (an alertTitle appeared). Returns (ok, nodes_after)."""
    row = find_node(nodes, text=row_title)
    if not row or not tap_node(row):
        return False, nodes
    after_nodes = ui()
    ok = node_by_id(after_nodes, "alertTitle") is not None
    if ok:
        and_enter(f"dialog:{row_title}")
    return ok, after_nodes


# ══════════════════════════════════════════════════════════════════════════════
# GATE
# ══════════════════════════════════════════════════════════════════════════════

@and_test(expects=("struct_launch_gate",))
def test_launch_gate():
    restart_app(); wait_for_foreground(10)
    nodes = ui()
    ok = (node_by_id(nodes, "buttonPanel") is not None
          and node_by_id(nodes, "startGameButton") is not None)
    record("struct_launch_gate", bool(ok),
           "" if ok else "setup_failed: main menu buttonPanel/startGameButton "
                          "missing at launch",
           depth=0, jump_kind="none")


@and_test(expects=("struct_menu_subtree_shape",))
def test_menu_subtree_shape():
    """The main-menu buttonPanel subtree: a selectTableRow, an unlimitedBalls
    CheckBox, and a TableLayout holding >=5 Button children. SHAPE, not content."""
    restart_app(); wait_for_foreground(10)
    nodes = ui()
    panel = node_by_id(nodes, "buttonPanel")
    row = node_by_id(nodes, "selectTableRow")
    toggle = node_by_id(nodes, "unlimitedBallsToggle")
    n_buttons = len(find_by_class(nodes, "Button"))   # observed: 5 (START..QUIT)
    ok = (panel is not None and row is not None and toggle is not None
          and n_buttons >= 5)
    record("struct_menu_subtree_shape", bool(ok),
           "" if ok else f"behavior_wrong: menu subtree shape wrong "
                         f"(panel={panel is not None}, row={row is not None}, "
                         f"toggle={toggle is not None}, buttons={n_buttons})",
           depth=0, jump_kind="none")


@and_test(expects=("struct_selector_row_shape",))
def test_selector_row_shape():
    """selectTableRow holds exactly two ImageButton children (prev/next arrows)
    flanking the 'Select Table' label — the picker's shape. SHAPE, not content."""
    restart_app(); wait_for_foreground(10)
    nodes = ui()
    prev = node_by_id(nodes, "previousTableButton")
    nxt = node_by_id(nodes, "nextTableButton")
    n_imgbtn = len(find_by_class(nodes, "ImageButton"))   # observed: 2
    ok = prev is not None and nxt is not None and n_imgbtn >= 2
    record("struct_selector_row_shape", bool(ok),
           "" if ok else f"behavior_wrong: table-picker arrows missing "
                         f"(prev={prev is not None}, next={nxt is not None}, "
                         f"imgbtn={n_imgbtn})",
           depth=0, jump_kind="none")


# ══════════════════════════════════════════════════════════════════════════════
# SHAPE DELTAS (state-machine panel swaps within BouncyActivity)
# ══════════════════════════════════════════════════════════════════════════════

@and_test(expects=("struct_start_game_shape",))
def test_start_game_shape():
    """START GAME shape delta: buttonPanel DISAPPEARS + pauseIcon APPEARS.
    Reverse it (pause) and assert the buttonPanel subtree restores."""
    restart_app(); wait_for_foreground(10)
    nodes = ui()
    ok_enter, before, after = and_enter_gameplay(nodes)
    changed = surface_changed(before, after)
    # reverse: pause -> buttonPanel must come back
    restored = False
    if ok_enter:
        g_nodes = ui()
        ok_pause, _b, _a = and_enter_paused_menu(g_nodes)
        restored = ok_pause and node_by_id(ui(), "buttonPanel") is not None
    ok = ok_enter and changed and restored
    record("struct_start_game_shape", bool(ok),
           "" if ok else "behavior_wrong: START GAME did not remove buttonPanel / "
                         "add pauseIcon, or the panel did not restore on pause",
           depth=1, jump_kind="transition",
           expect="buttonPanel->gone & pauseIcon->present on Start; "
                  "buttonPanel restored on pause")


@and_test(expects=("struct_pause_shape",))
def test_pause_shape():
    """Pause shape delta (depth 2): from gameplay, tap pauseIcon -> pauseIcon
    DISAPPEARS + buttonPanel REAPPEARS."""
    restart_app(); wait_for_foreground(10)
    ok_g, _b, _a = and_enter_gameplay(ui())
    ok_p, before, after = (False, set(), set())
    if ok_g:
        ok_p, before, after = and_enter_paused_menu(ui())
    p_nodes = ui()
    pause_gone = node_by_id(p_nodes, "pauseIcon") is None
    panel_back = node_by_id(p_nodes, "buttonPanel") is not None
    ok = ok_g and ok_p and pause_gone and panel_back and surface_changed(before, after)
    record("struct_pause_shape", bool(ok),
           "" if ok else "behavior_wrong: pause did not remove pauseIcon / restore "
                         "buttonPanel",
           depth=2, jump_kind="transition",
           expect="pauseIcon->gone & buttonPanel->present after pause")


@and_test(expects=("struct_paused_menu_shape",))
def test_paused_menu_shape():
    """Paused-menu SHAPE contrast (depth 2): the reappeared buttonPanel now holds
    resumeGameButton + endGameButton, while selectTableRow + unlimitedBallsToggle
    + startGameButton are ABSENT — a different subtree from the fresh menu."""
    restart_app(); wait_for_foreground(10)
    ok_g, _b, _a = and_enter_gameplay(ui())
    ok_p = False
    if ok_g:
        ok_p, _b2, _a2 = and_enter_paused_menu(ui())
    p = ui()
    has_resume = node_by_id(p, "resumeGameButton") is not None
    has_end = node_by_id(p, "endGameButton") is not None
    no_picker = node_by_id(p, "selectTableRow") is None
    no_toggle = node_by_id(p, "unlimitedBallsToggle") is None
    no_start = node_by_id(p, "startGameButton") is None
    ok = ok_g and ok_p and has_resume and has_end and no_picker and no_toggle and no_start
    record("struct_paused_menu_shape", bool(ok),
           "" if ok else f"behavior_wrong: paused-menu shape wrong "
                         f"(resume={has_resume}, end={has_end}, no_picker={no_picker}, "
                         f"no_toggle={no_toggle}, no_start={no_start})",
           depth=2, jump_kind="transition",
           expect="paused panel: +resume/+end, -selectTableRow/-toggle/-startGame")


@and_test(expects=("struct_fresh_menu_shape",))
def test_fresh_menu_shape():
    """Fresh (not-in-progress) menu SHAPE: startGameButton + selectTableRow
    present, resumeGameButton + endGameButton + pauseIcon ABSENT. The exact
    inverse of the paused-menu shape."""
    restart_app(); wait_for_foreground(10)
    nodes = ui()
    has_start = node_by_id(nodes, "startGameButton") is not None
    has_picker = node_by_id(nodes, "selectTableRow") is not None
    no_resume = node_by_id(nodes, "resumeGameButton") is None
    no_end = node_by_id(nodes, "endGameButton") is None
    no_pause = node_by_id(nodes, "pauseIcon") is None
    ok = has_start and has_picker and no_resume and no_end and no_pause
    record("struct_fresh_menu_shape", bool(ok),
           "" if ok else f"behavior_wrong: fresh-menu shape wrong "
                         f"(start={has_start}, picker={has_picker}, "
                         f"no_resume={no_resume}, no_end={no_end}, no_pause={no_pause})",
           depth=0, jump_kind="none")


@and_test(expects=("struct_end_game_restores_fresh_menu",))
def test_end_game_restores_fresh_menu():
    """END GAME shape delta (depth 2): from the paused menu, tap endGameButton ->
    the fresh-menu subtree returns (selectTableRow + startGameButton present,
    resume/end gone). End Game is non-destructive (returns to menu)."""
    restart_app(); wait_for_foreground(10)
    ok_g, _b, _a = and_enter_gameplay(ui())
    ok_p = False
    if ok_g:
        ok_p, _b2, _a2 = and_enter_paused_menu(ui())
    end_btn = node_by_id(ui(), "endGameButton") if ok_p else None
    tapped = tap_node(end_btn) if end_btn else False
    m = ui()
    back_fresh = (node_by_id(m, "selectTableRow") is not None
                  and node_by_id(m, "startGameButton") is not None
                  and node_by_id(m, "resumeGameButton") is None
                  and node_by_id(m, "endGameButton") is None)
    ok = ok_g and ok_p and tapped and back_fresh
    record("struct_end_game_restores_fresh_menu", bool(ok),
           "" if ok else "behavior_wrong: End Game did not restore the fresh menu "
                         "subtree (selectTableRow+startGame back, resume/end gone)",
           depth=2, jump_kind="transition",
           expect="paused -> End Game -> fresh menu subtree (+picker/+start, -resume/-end)")


@and_test(expects=("struct_highscore_panel_shape",))
def test_highscore_panel_shape():
    """HIGH SCORES shape delta (depth 1): highScorePanel APPEARS, buttonPanel
    DISAPPEARS. Reverse via OK and assert the buttonPanel subtree restores."""
    restart_app(); wait_for_foreground(10)
    ok_reach, before, after = and_reach_highscores(ui())
    changed = surface_changed(before, after)
    # panel must hold its title TextView + OK button — SHAPE of the dialog subtree
    h = ui()
    has_ok = node_by_id(h, "hideHighScoreButton") is not None
    # reverse: OK -> buttonPanel back
    restored = False
    if ok_reach and has_ok:
        tap_node(node_by_id(h, "hideHighScoreButton"))
        restored = node_by_id(ui(), "buttonPanel") is not None
    ok = ok_reach and changed and has_ok and restored
    record("struct_highscore_panel_shape", bool(ok),
           "" if ok else "behavior_wrong: HIGH SCORES did not swap buttonPanel for "
                         "highScorePanel(+OK), or OK did not restore the menu",
           depth=1, jump_kind="transition",
           expect="buttonPanel->highScorePanel(+OK) on HIGH SCORES; restored on OK")


@and_test(expects=("struct_back_closes_highscore",))
def test_back_closes_highscore():
    """BACK on the high-scores panel is a shape RESTORE, not an app exit:
    highScorePanel disappears, buttonPanel returns, app still foreground."""
    restart_app(); wait_for_foreground(10)
    ok_reach, _b, _a = and_reach_highscores(ui())
    press_key(4)   # KEYCODE_BACK
    m = ui()
    panel_gone = node_by_id(m, "highScorePanel") is None
    menu_back = node_by_id(m, "buttonPanel") is not None
    still_fg = is_app_foreground()
    ok = ok_reach and panel_gone and menu_back and still_fg
    record("struct_back_closes_highscore", bool(ok),
           "" if ok else "behavior_wrong: BACK did not restore buttonPanel from the "
                         "high-scores panel (or app left foreground)",
           depth=1, jump_kind="transition",
           expect="BACK: highScorePanel->gone, buttonPanel->present, still foreground")


@and_test(expects=("struct_preferences_shape",))
def test_preferences_shape():
    """PREFERENCES shape delta (depth 1, new activity): a ListView with >=8
    preference rows appears; the menu buttonPanel is gone. SHAPE = row count."""
    restart_app(); wait_for_foreground(10)
    ok_reach, prefs = and_reach_preferences(ui())
    # observed 11 rows (title TextViews with android:id/title) + 8 checkboxes
    titles = [n for n in prefs if n.get("resource-id", "").endswith("/title")
              and n.get("class", "").endswith("TextView") and n.get("text")]
    n_rows = len(titles)
    n_checks = len(find_by_class(prefs, "CheckBox"))
    panel_gone = node_by_id(prefs, "buttonPanel") is None
    ok = ok_reach and n_rows >= 8 and n_checks >= 6 and panel_gone
    record("struct_preferences_shape", bool(ok),
           "" if ok else f"behavior_wrong: Preferences shape wrong "
                         f"(rows={n_rows} need>=8, checks={n_checks} need>=6, "
                         f"panel_gone={panel_gone})",
           depth=1, jump_kind="transition",
           expect="Preferences ListView: >=8 rows, >=6 checkboxes, no buttonPanel")


@and_test(expects=("struct_about_shape",))
def test_about_shape():
    """HELP shape delta (depth 1, new activity): the aboutTextView subtree
    dominates and the menu buttonPanel is gone. SHAPE: one large TextView is the
    dominant node (its bounds cover most of the content area)."""
    restart_app(); wait_for_foreground(10)
    ok_reach, about = and_reach_about(ui())
    tv = node_by_id(about, "aboutTextView")
    panel_gone = node_by_id(about, "buttonPanel") is None
    dominant = False
    if tv and "_bounds" in tv:
        x1, y1, x2, y2 = tv["_bounds"]
        s = get_screen_info()
        # the about text is a tall block covering a large share of the height
        dominant = (y2 - y1) >= 0.30 * s["height"] and (x2 - x1) >= 0.6 * s["width"]
    ok = ok_reach and tv is not None and panel_gone and dominant
    record("struct_about_shape", bool(ok),
           "" if ok else "behavior_wrong: About did not show a dominant aboutTextView "
                         "with the menu buttonPanel gone",
           depth=1, jump_kind="transition",
           expect="About: aboutTextView dominant subtree, no buttonPanel")


@and_test(expects=("struct_gamespeed_dialog_shape",))
def test_gamespeed_dialog_shape():
    """Game speed dialog shape delta (depth 2): inside Preferences, tap 'Game
    speed' -> a dialog subtree with exactly 3 CheckedTextView options appears."""
    restart_app(); wait_for_foreground(10)
    ok_pref, prefs = and_reach_preferences(ui())
    ok_dialog, dlg = (False, prefs)
    if ok_pref:
        ok_dialog, dlg = and_enter_pref_dialog(prefs, "Game speed")
    n_options = len(find_by_class(dlg, "CheckedTextView"))   # observed: 3
    has_title = node_by_id(dlg, "alertTitle") is not None
    ok = ok_pref and ok_dialog and n_options == 3 and has_title
    record("struct_gamespeed_dialog_shape", bool(ok),
           "" if ok else f"behavior_wrong: Game speed dialog shape wrong "
                         f"(options={n_options} need 3, title={has_title})",
           depth=2, jump_kind="transition",
           expect="Game speed dialog: alertTitle + exactly 3 CheckedTextView")


@and_test(expects=("struct_linewidth_dialog_shape",))
def test_linewidth_dialog_shape():
    """Line width dialog shape delta (depth 2): inside Preferences, tap 'Line
    width' -> a dialog subtree that adds an EditText (plus a message TextView)."""
    restart_app(); wait_for_foreground(10)
    ok_pref, prefs = and_reach_preferences(ui())
    n_edit_before = len(find_by_class(prefs, "EditText"))   # observed: 0
    ok_dialog, dlg = (False, prefs)
    if ok_pref:
        ok_dialog, dlg = and_enter_pref_dialog(prefs, "Line width")
    n_edit_after = len(find_by_class(dlg, "EditText"))       # observed: 1
    has_title = node_by_id(dlg, "alertTitle") is not None
    ok = (ok_pref and ok_dialog and has_title
          and n_edit_after == n_edit_before + 1)
    record("struct_linewidth_dialog_shape", bool(ok),
           "" if ok else f"behavior_wrong: Line width dialog shape wrong "
                         f"(EditText {n_edit_before}->{n_edit_after}, title={has_title})",
           depth=2, jump_kind="transition",
           expect="Line width dialog: alertTitle + EditText count 0->1")


# ══════════════════════════════════════════════════════════════════════════════
# VLM (8) — canvas/GL/layout facts that never reach the view tree
# ══════════════════════════════════════════════════════════════════════════════

@and_test(expects=("struct_menu_field_rendered_vlm",))
def test_menu_field_rendered_vlm():
    """The pinball FIELD is drawn behind/around the menu panel on launch — a
    complex line-drawing playfield the tree cannot describe."""
    restart_app(); wait_for_foreground(10)
    ui()
    vlm_assert("struct_menu_field_rendered",
               "Behind and around the centered menu panel, is there a complex "
               "line-drawing pinball playfield (thin colored outlines, arcs, and "
               "small round bumpers) filling the dark background?",
               RESULTS_DIR, depth=0, jump_kind="none", tag="shape")


@and_test(expects=("struct_gameplay_field_no_panel_vlm",))
def test_gameplay_field_no_panel_vlm():
    """After START GAME the field fills the screen with the menu panel GONE and a
    circular pause icon at the top-left — a layout fact (panel removed)."""
    restart_app(); wait_for_foreground(10)
    ok_g, _b, _a = and_enter_gameplay(ui())
    if not ok_g:
        record("struct_gameplay_field_no_panel_vlm", False,
               "setup_failed: could not enter gameplay", depth=1,
               jump_kind="transition", tag="layout")
    else:
        vlm_assert("struct_gameplay_field_no_panel",
                   "Is the pinball playfield now shown WITHOUT any centered "
                   "gray-button menu panel over it (no START GAME / HIGH SCORES "
                   "buttons visible)?",
                   RESULTS_DIR, depth=1, jump_kind="transition", tag="layout")


@and_test(expects=("struct_gameplay_pause_icon_topleft_vlm",))
def test_gameplay_pause_icon_topleft_vlm():
    """During gameplay a round pause icon sits at the TOP-LEFT of the top bar —
    an exact rendered position fact."""
    restart_app(); wait_for_foreground(10)
    ok_g, _b, _a = and_enter_gameplay(ui())
    if not ok_g:
        record("struct_gameplay_pause_icon_topleft_vlm", False,
               "setup_failed: could not enter gameplay", depth=1,
               jump_kind="transition", tag="position")
    else:
        vlm_assert("struct_gameplay_pause_icon_topleft",
                   "Is there a small round pause icon (two vertical bars in a "
                   "circle) at the TOP-LEFT corner of the top status bar?",
                   RESULTS_DIR, depth=1, jump_kind="transition", tag="position")


@and_test(expects=("struct_top_score_bar_vlm",))
def test_top_score_bar_vlm():
    """The top status bar spans the full width across the top of the gameplay
    screen and contains a centered score readout — a position/layout fact."""
    restart_app(); wait_for_foreground(10)
    ok_g, _b, _a = and_enter_gameplay(ui())
    if not ok_g:
        record("struct_top_score_bar_vlm", False,
               "setup_failed: could not enter gameplay", depth=1,
               jump_kind="transition", tag="position")
    else:
        vlm_assert("struct_top_score_bar",
                   "Is there a horizontal status bar spanning the full width at the "
                   "very TOP of the screen, containing a centered numeric score "
                   "readout, separate from the playfield below it?",
                   RESULTS_DIR, depth=1, jump_kind="transition", tag="position")


@and_test(expects=("struct_paused_overlay_field_vlm",))
def test_paused_overlay_field_vlm():
    """The paused menu panel is overlaid ON TOP of the still-rendered field (the
    field's colored lines/bumpers remain visible around the panel) — a layering
    fact the tree cannot express."""
    restart_app(); wait_for_foreground(10)
    ok_g, _b, _a = and_enter_gameplay(ui())
    ok_p = False
    if ok_g:
        ok_p, _b2, _a2 = and_enter_paused_menu(ui())
    if not (ok_g and ok_p):
        record("struct_paused_overlay_field_vlm", False,
               "setup_failed: could not reach the paused menu", depth=2,
               jump_kind="transition", tag="layout")
    else:
        vlm_assert("struct_paused_overlay_field",
                   "Is a centered menu panel (with RESUME GAME and END GAME "
                   "buttons) overlaid on top of the pinball playfield, with the "
                   "field's colored lines and bumpers still visible around the "
                   "panel edges?",
                   RESULTS_DIR, depth=2, jump_kind="transition", tag="layout")


@and_test(expects=("struct_table1_layout_vlm",))
def test_table1_layout_vlm():
    """Table 1's rendered field has a distinctive white circle near the top-left
    and cyan/red rollover rings — an exact rendered content fact. (Fresh data =
    table 1 by default.)"""
    clear_data(); restart_app(); wait_for_foreground(10)
    ui()
    vlm_assert("struct_table1_layout",
               "On the pinball field behind the menu, near the TOP-LEFT, is there "
               "a small WHITE circle outline together with red and cyan ring "
               "targets on the field?",
               RESULTS_DIR, depth=0, jump_kind="none", tag="position")


@and_test(expects=("struct_table_switch_changes_field_vlm",))
def test_table_switch_changes_field_vlm():
    """Tapping the next-table arrow changes the RENDERED field layout (a canvas
    consequence; the view tree is unchanged). Table 2 shows a row of yellow
    bumpers along the top — distinct from table 1."""
    clear_data(); restart_app(); wait_for_foreground(10)
    nodes = ui()
    nxt = node_by_id(nodes, "nextTableButton")
    tapped = tap_node(nxt) if nxt else False
    ui()
    if not tapped:
        record("struct_table_switch_changes_field_vlm", False,
               "setup_failed: next-table arrow not found/tappable", depth=0,
               jump_kind="none", tag="color")
    else:
        vlm_assert("struct_table_switch_changes_field",
                   "On the pinball field behind the menu, are there two large "
                   "YELLOW round bumpers near the TOP of the playfield?",
                   RESULTS_DIR, depth=0, jump_kind="none", tag="color")


TESTS = [
    # gate + static shape (depth 0, none) — kept <=30%
    test_launch_gate,
    test_menu_subtree_shape,
    test_selector_row_shape,
    test_fresh_menu_shape,
    # shape deltas (transition)
    test_start_game_shape,
    test_pause_shape,
    test_paused_menu_shape,
    test_end_game_restores_fresh_menu,
    test_highscore_panel_shape,
    test_back_closes_highscore,
    test_preferences_shape,
    test_about_shape,
    test_gamespeed_dialog_shape,
    test_linewidth_dialog_shape,
    # VLM (8) — canvas/layout facts
    test_menu_field_rendered_vlm,
    test_gameplay_field_no_panel_vlm,
    test_gameplay_pause_icon_topleft_vlm,
    test_top_score_bar_vlm,
    test_paused_overlay_field_vlm,
    test_table1_layout_vlm,
    test_table_switch_changes_field_vlm,
]

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