File size: 19,548 Bytes
41dfd8d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
#!/usr/bin/env python3
"""STRUCTURE lane — Urik Keyboard.

Owns the GATE (app launches with its top-level surfaces present) and tree-SHAPE
deltas (an interaction changes the view tree's shape: a RecyclerView appears, a
dialog/sheet subtree is added, a rendered keyboard grid appears). SHAPE ONLY —
never content strings, checked/enabled/selected state, focus, or reachability.

Every observed count below was confirmed on the running original app:
  S0 launcher : FAB + 3 named Buttons (Enable/Open Settings/About) + CardView
                preview card holding ~14 rendered key Buttons; container is
                LinearLayout/FrameLayout (this app has NO ViewGroup / no Compose).
  Settings    : RecyclerView of 8 preference rows; the 3 launcher buttons gone.
  AutoCorrect : 5 Switch rows + a bottom test_field EditText.
  Sugg. dialog: adds 3 CheckedTextView (a new AlertDialog window layer).
  Features    : bottom-sheet subtree of 14 TextViews (7 tip title/desc pairs).
  ThemePicker : RecyclerView of theme cards (6 FrameLayout content-desc "* theme"
                visible; recycles, so we assert >3).
  LayoutMapper: 40 rendered Buttons (number row + QWERTY rows + Reset All).
  Typing      : 7 Switch rows and, after scrolling, a SeekBar (id seekbar).
  Languages   : 12 CheckBox children (assert >=5).
"""
import os, sys
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
from android_testgen_kit import *          # noqa: F403


# ── local navigation helpers (depth-family: name backs a declared depth>=1) ──
def and_enter_settings():
    """S0 -> SettingsActivity. Returns (ok, nodes_after)."""
    tap_text(ui(), "Open Settings")
    nodes = ui()
    ok = find_by_class(nodes, "RecyclerView") and find_node(nodes, text="Auto-Correct Settings")
    if ok:
        and_enter("settings")
    return bool(ok), nodes


def and_enter_autocorrect():
    """S0 -> Settings -> AutoCorrectionFragment. Returns (ok, nodes_after)."""
    ok, _ = and_enter_settings()
    if not ok:
        return False, []
    tap_text(ui(), "Auto-Correct Settings")
    nodes = ui()
    ok2 = node_by_id(nodes, "test_field") or find_node(nodes, text="Spell Check")
    if ok2:
        and_enter("autocorrect")
    return bool(ok2), nodes


def and_enter_fragment(row_text):
    """S0 -> Settings -> a settings sub-fragment reached by row_text.
    Returns (ok, nodes_after). row_text is matched literally (heed '&')."""
    ok, _ = and_enter_settings()
    if not ok:
        return False, []
    row = find_node(ui(), text=row_text)
    if not row:
        return False, []
    tap_node(row)
    and_enter(row_text)
    return True, ui()


# ══════════════════════════════════════════════════════════════════════════════
# GATE — the app launches and its top-level surfaces exist (depth 0, none)
# ══════════════════════════════════════════════════════════════════════════════

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


@and_test(expects=("structure_gate_s0_surfaces", "structure_gate_s0_surfaces_vlm"))
def test_gate_s0_surfaces():
    restart_app()
    wait_for_foreground()
    nodes = ui()
    fab = len(find_by_class(nodes, "FloatingActionButton")) >= 1
    named = sum(1 for t in ("Enable", "Open Settings", "About")
                if find_node(nodes, text=t) is not None)
    card = len(find_by_class(nodes, "CardView")) >= 1
    ok = fab and named >= 3 and card
    record("structure_gate_s0_surfaces", ok,
           "" if ok else f"feature_missing: S0 top-level surfaces absent "
                         f"(fab={fab} named_buttons={named}/3 card={card})",
           depth=0, jump_kind="none")
    vlm_assert("structure_gate_s0_surfaces",
               "Is the app logo/icon near the top-center of the screen with a "
               "vertical stack of full-width action buttons positioned at the "
               "bottom of the screen?",
               RESULTS_DIR, tag="position")


@and_test(expects=("structure_gate_preview_card", "structure_gate_preview_card_vlm"))
def test_gate_preview_card():
    restart_app()
    wait_for_foreground()
    nodes = ui()
    # The preview card renders a mini keyboard: many Buttons that are neither the
    # info FAB nor the three named action buttons.
    action = {"Enable", "Open Settings", "About"}
    preview = [n for n in find_by_class(nodes, "Button")
               if "FloatingActionButton" not in n.get("class", "")
               and n.get("text", "") not in action]
    ok = len(preview) >= 8
    record("structure_gate_preview_card", ok,
           "" if ok else f"feature_missing: keyboard-preview card has too few rendered "
                         f"key buttons ({len(preview)})",
           depth=0, jump_kind="none")
    vlm_assert("structure_gate_preview_card",
               "Is there a keyboard preview showing letter keys (like Z X C V B N M) "
               "arranged in horizontal rows like a keyboard?",
               RESULTS_DIR, tag="shape")


# ══════════════════════════════════════════════════════════════════════════════
# SHAPE DELTAS — a trigger changes the tree's shape (transition; reversed)
# ══════════════════════════════════════════════════════════════════════════════

@and_test(expects=("structure_settings_shape", "structure_settings_shape_vlm"))
def test_settings_shape():
    restart_app()
    wait_for_foreground()
    before_nodes = ui()
    before_rv = len(find_by_class(before_nodes, "RecyclerView"))
    before_btn = len(find_by_class(before_nodes, "Button"))
    ok_nav, after_nodes = and_enter_settings()
    after_rv = len(find_by_class(after_nodes, "RecyclerView"))
    after_rows = sum(1 for n in after_nodes
                     if n.get("resource-id", "").endswith("title") and n.get("text", "").strip())
    after_btn = len(find_by_class(after_nodes, "Button"))
    # SHAPE delta: a preference RecyclerView with many rows appears; the launcher's
    # 3 named action buttons + preview grid collapse away.
    appeared = before_rv == 0 and after_rv >= 1 and after_rows >= 6
    collapsed = after_btn < before_btn
    # Capture the VLM screenshot NOW, while the Settings list is on screen — before
    # reversing the transition, or the shot would be of S0 again.
    vlm_assert("structure_settings_shape",
               "Is this a settings screen showing a vertical list of text rows "
               "stacked top to bottom, each row a bold title with a smaller grey "
               "subtitle beneath it?",
               RESULTS_DIR, tag="layout")
    # reverse: back to S0 -> RecyclerView gone, launcher buttons return.
    restart_app()
    wait_for_foreground()
    restored = len(find_by_class(ui(), "RecyclerView")) == 0
    ok = ok_nav and appeared and collapsed and restored
    record("structure_settings_shape", ok,
           "" if ok else f"behavior_wrong: Settings shape delta wrong "
                         f"(rv {before_rv}->{after_rv}, rows={after_rows}, "
                         f"btn {before_btn}->{after_btn}, restored={restored})",
           depth=1, jump_kind="transition",
           expect="RecyclerView 0->1 with >=6 preference rows on Settings; "
                  "launcher buttons collapse; restored to 0 RecyclerView on relaunch")


@and_test(expects=("structure_autocorrect_shape", "structure_autocorrect_shape_vlm"))
def test_autocorrect_shape():
    restart_app()
    wait_for_foreground()
    ok_nav, nodes = and_enter_autocorrect()
    n_switch = len(find_by_class(nodes, "Switch"))
    has_field = node_by_id(nodes, "test_field") is not None
    # SHAPE: a preference list composed of several Switch rows plus a bottom
    # EditText (the "Test changes" field) — a composition absent from S0/Settings.
    ok = ok_nav and n_switch >= 4 and has_field
    record("structure_autocorrect_shape", ok,
           "" if ok else f"behavior_wrong: AutoCorrect shape wrong "
                         f"(switches={n_switch} expected>=4, test_field={has_field})",
           depth=2, jump_kind="transition",
           expect="AutoCorrect list has >=4 Switch rows and a bottom test_field EditText")
    # Not "every" row has a switch (Suggestion Count does not), so ask about the
    # switches that ARE present being aligned on the right edge — a position fact.
    vlm_assert("structure_autocorrect_shape",
               "Are there several toggle switches on this screen, each aligned "
               "against the right edge of its row?",
               RESULTS_DIR, tag="position")


@and_test(expects=("structure_suggestion_dialog_shape",))
def test_suggestion_dialog_shape():
    restart_app()
    wait_for_foreground()
    ok_nav, nodes = and_enter_autocorrect()
    if not ok_nav:
        record("structure_suggestion_dialog_shape", False,
               "setup_failed: could not reach AutoCorrect fragment",
               depth=2, jump_kind="transition",
               expect="Suggestion Count dialog adds >=3 CheckedTextView; 0 when dismissed")
        return
    before = len(find_by_class(nodes, "CheckedTextView"))
    tap_text(nodes, "Suggestion Count")
    after_nodes = ui()
    after = len(find_by_class(after_nodes, "CheckedTextView"))
    has_cancel = find_node_ci(after_nodes, "Cancel") is not None
    # SHAPE: a dialog subtree (list of CheckedTextView options + Cancel) is added.
    appeared = before == 0 and after >= 3 and has_cancel
    # reverse: dismiss the dialog -> CheckedTextView subtree gone.
    back()
    restored = len(find_by_class(ui(), "CheckedTextView")) == 0
    ok = appeared and restored
    record("structure_suggestion_dialog_shape", ok,
           "" if ok else f"behavior_wrong: dialog shape delta wrong "
                         f"(CheckedTextView {before}->{after}, cancel={has_cancel}, "
                         f"restored={restored})",
           depth=2, jump_kind="transition",
           expect="Suggestion Count dialog adds CheckedTextView 0->3 (+Cancel), "
                  "restored to 0 on dismiss")


@and_test(expects=("structure_features_sheet_shape", "structure_features_sheet_shape_vlm"))
def test_features_sheet_shape():
    restart_app()
    wait_for_foreground()
    nodes = ui()
    before_tv = sum(1 for n in find_by_class(nodes, "TextView") if n.get("text", "").strip())
    fabs = find_by_class(nodes, "FloatingActionButton")
    if not fabs:
        record("structure_features_sheet_shape", False,
               "feature_missing: info FAB not found on S0",
               depth=1, jump_kind="transition",
               expect="Features bottom sheet adds many TextViews (tip pairs)")
    else:
        before = and_showing(nodes)
        tap_node(fabs[0])
        and_enter("features_sheet")
        after_nodes = ui()
        after = and_showing(after_nodes)
        after_tv = sum(1 for n in find_by_class(after_nodes, "TextView") if n.get("text", "").strip())
        # SHAPE: a bottom-sheet subtree with many stacked TextViews is added.
        ok = surface_changed(before, after) and after_tv >= before_tv + 8
        record("structure_features_sheet_shape", ok,
               "" if ok else f"behavior_wrong: Features sheet shape delta too small "
                             f"(TextViews {before_tv}->{after_tv})",
               depth=1, jump_kind="transition",
               expect="Features bottom sheet adds >=8 TextViews over S0's 2")
    vlm_assert("structure_features_sheet_shape",
               "Is a panel showing a vertical stacked list of feature tips, each "
               "with a bold title and a description line beneath it?",
               RESULTS_DIR, tag="layout")


@and_test(expects=("structure_themepicker_shape", "structure_themepicker_shape_vlm"))
def test_themepicker_shape():
    restart_app()
    wait_for_foreground()
    nodes = ui()
    before_rv = len(find_by_class(nodes, "RecyclerView"))
    # The theme-name label under the preview card opens ThemePicker; its text is
    # the current theme (state-dependent), so locate it as the tappable TextView
    # sitting just below the preview CardView rather than by a fixed string.
    card = find_by_class(nodes, "CardView")
    theme_label = None
    if card:
        cy2 = card[0]["_bounds"][3]
        cands = [n for n in find_by_class(nodes, "TextView")
                 if n.get("text", "").strip() and n.get("text", "") != "Urik Keyboard"
                 and n.get("_bounds", (0, 0, 0, 0))[1] >= cy2 - 120]
        theme_label = cands[0] if cands else None
    if not theme_label:
        record("structure_themepicker_shape", False,
               "setup_failed: theme-name label under the preview card not found",
               depth=1, jump_kind="transition",
               expect="ThemePicker RecyclerView shows >3 theme-card children")
    else:
        tap_node(theme_label)
        and_enter("themepicker")
        after_nodes = ui()
        after_rv = len(find_by_class(after_nodes, "RecyclerView"))
        cards = [n for n in after_nodes
                 if "theme" in n.get("content-desc", "").lower()
                 and "FrameLayout" in n.get("class", "")]
        # SHAPE: a RecyclerView whose children are theme cards (child count > 3).
        ok = before_rv == 0 and after_rv >= 1 and len(cards) > 3
        record("structure_themepicker_shape", ok,
               "" if ok else f"behavior_wrong: ThemePicker shape wrong "
                             f"(rv {before_rv}->{after_rv}, theme cards={len(cards)})",
               depth=1, jump_kind="transition",
               expect="ThemePicker adds a RecyclerView with >3 theme-card FrameLayouts")
    vlm_assert("structure_themepicker_shape",
               "Is this a scrollable vertical list of keyboard-theme preview cards, "
               "each card showing a small keyboard rendering?",
               RESULTS_DIR, tag="layout")


@and_test(expects=("structure_layoutmapper_shape", "structure_layoutmapper_shape_vlm"))
def test_layoutmapper_shape():
    restart_app()
    wait_for_foreground()
    ok_nav, nodes = and_enter_fragment("Layout &amp; Input")
    if not ok_nav:
        record("structure_layoutmapper_shape", False,
               "setup_failed: could not reach Layout & Input fragment",
               depth=3, jump_kind="transition",
               expect="LayoutMapper renders a full QWERTY grid of >=30 Button keys")
    else:
        ck = find_node(nodes, text="Customize Keys")
        before_btn = len(find_by_class(nodes, "Button"))
        if not ck:
            record("structure_layoutmapper_shape", False,
                   "feature_missing: Customize Keys row not found",
                   depth=3, jump_kind="transition",
                   expect="LayoutMapper renders a full QWERTY grid of >=30 Button keys")
        else:
            tap_node(ck)
            and_enter("layoutmapper")
            after_nodes = ui()
            after_btn = len(find_by_class(after_nodes, "Button"))
            # SHAPE: a rendered full keyboard — many Button children in rows.
            ok = after_btn >= 30 and after_btn > before_btn
            record("structure_layoutmapper_shape", ok,
                   "" if ok else f"behavior_wrong: LayoutMapper key grid too small "
                                 f"(buttons {before_btn}->{after_btn}, expected >=30)",
                   depth=3, jump_kind="transition",
                   expect="LayoutMapper renders >=30 Button keys (number row + QWERTY)")
    vlm_assert("structure_layoutmapper_shape",
               "Is a full QWERTY keyboard rendered as multiple horizontal rows of "
               "square keys (a number row on top, then letter rows)?",
               RESULTS_DIR, tag="shape")


@and_test(expects=("structure_typing_seekbar_shape",))
def test_typing_seekbar_shape():
    restart_app()
    wait_for_foreground()
    ok_nav, nodes = and_enter_fragment("Typing Behavior Settings")
    if not ok_nav:
        record("structure_typing_seekbar_shape", False,
               "setup_failed: could not reach Typing Behavior fragment",
               depth=2, jump_kind="transition",
               expect="Typing Behavior list contains a SeekBar among many Switch rows")
        return
    n_switch = len(find_by_class(nodes, "Switch"))
    # The Vibration Strength SeekBar sits near the bottom; scroll it into view.
    scroll_into_view("Vibration Strength", max_swipes=5)
    after_nodes = ui()
    has_seekbar = (len(find_by_class(after_nodes, "SeekBar")) >= 1
                   or node_by_id(after_nodes, "seekbar") is not None)
    # SHAPE: this fragment's composition uniquely includes a SeekBar widget class
    # alongside its several Switch rows (other fragments have none).
    ok = n_switch >= 4 and has_seekbar
    record("structure_typing_seekbar_shape", ok,
           "" if ok else f"behavior_wrong: Typing Behavior shape wrong "
                         f"(switches={n_switch}, seekbar_present={has_seekbar})",
           depth=2, jump_kind="transition",
           expect="Typing Behavior has >=4 Switch rows and a SeekBar widget")


@and_test(expects=("structure_languages_shape", "structure_languages_shape_vlm"))
def test_languages_shape():
    restart_app()
    wait_for_foreground()
    ok_nav, nodes = and_enter_fragment("Language Settings")
    if not ok_nav:
        record("structure_languages_shape", False,
               "setup_failed: could not reach Language Settings fragment",
               depth=2, jump_kind="transition",
               expect="Languages fragment shows a list of >=5 CheckBox children")
    else:
        n_checkbox = len(find_by_class(nodes, "CheckBox"))
        # SHAPE: a list whose children are language CheckBoxes (>=5).
        ok = n_checkbox >= 5
        record("structure_languages_shape", ok,
               "" if ok else f"behavior_wrong: too few language CheckBoxes "
                             f"({n_checkbox}, expected >=5)",
               depth=2, jump_kind="transition",
               expect="Languages fragment shows >=5 CheckBox children")
    vlm_assert("structure_languages_shape",
               "Does this screen show a vertical scrollable list of language rows, "
               "each row having a checkbox?",
               RESULTS_DIR, tag="layout")


TESTS = [
    test_gate_launch,
    test_gate_s0_surfaces,
    test_gate_preview_card,
    test_settings_shape,
    test_autocorrect_shape,
    test_suggestion_dialog_shape,
    test_features_sheet_shape,
    test_themepicker_shape,
    test_layoutmapper_shape,
    test_typing_seekbar_shape,
    test_languages_shape,
]

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