RecreationBench / android /cyb3rko-flashdim /tests /test_android_structure.py
VoyagerXHF's picture
Upload folder using huggingface_hub
966ca88 verified
Raw History Blame Contribute Delete
22.9 kB
#!/usr/bin/env python3
"""STRUCTURE-lane behavioral suite for FlashDim.
Owns: (1) the launch GATE (MainActivity + its top-level surfaces exist) and
(2) tree-SHAPE deltas — a trigger changes the child-count / subtree presence of
the tree. Content (strings), state (checked/enabled), reachability, and computed
values are CEDED to their own lanes; here we only assert SHAPE (counts, subtree
appears/disappears), and always reverse a delta to show it restores.
"""
import os
import sys
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
from android_testgen_kit import * # noqa: F403,E402
RESULTS_DIR = sys.argv[2] if len(sys.argv) > 2 else "/workspace/testgen/eval/results"
# ──────────────────────────────────────────────────────────────────────────────
# Depth-family helpers. A declared depth>=1 must be backed by a real
# and_enter_*/and_reach_* call so the depth audit sees genuine navigation.
# Each returns the "before" (home) node list so the test can compare shapes.
# ──────────────────────────────────────────────────────────────────────────────
def _home_ready():
"""Restart to a settled MainActivity and return its node list."""
restart_app()
wait_for_foreground()
return ui()
def and_enter_morse_dialog(home):
"""Tap morse_button, wait for the AlertDialog subtree, cross one surface."""
n = node_by_id(home, "morse_button")
if not tap_node(n):
return None
settle("tap")
and_enter("morse_dialog")
return ui()
def and_enter_interval_dialog(home):
"""Tap interval_button → Interval AlertDialog subtree; cross one surface."""
n = node_by_id(home, "interval_button")
if not tap_node(n):
return None
settle("tap")
and_enter("interval_dialog")
return ui()
def and_enter_links_dialog(home):
"""Toolbar links_action → Links AlertDialog subtree; cross one surface."""
n = node_by_id(home, "links_action")
if not tap_node(n):
return None
settle("tap")
and_enter("links_dialog")
return ui()
def and_enter_about_dialog(home):
"""Toolbar icon_credits_action → About AlertDialog subtree; cross one surface."""
n = node_by_id(home, "icon_credits_action")
if not tap_node(n):
return None
settle("tap")
and_enter("about_dialog")
return ui()
def and_enter_settings(home):
"""Toolbar settings_action → SettingsActivity; cross one surface."""
n = node_by_id(home, "settings_action")
if not tap_node(n):
return None
settle("launch")
and_enter("settings")
return ui()
def _visible_button_keys(nodes):
"""Set of on-screen Button/CompoundButton identity keys — the SHAPE of the
button set. Uses node_key so we compare identities, not raw strings order."""
s = get_screen_info()
out = set()
for n in nodes:
cls = n.get("class", "")
if "Button" not in cls:
continue
b = n.get("_bounds")
if not b:
continue
x1, y1, x2, y2 = b
if (x2 - x1) < 5 or (y2 - y1) < 5:
continue
if x2 <= 0 or y2 <= 0 or x1 >= s["width"] or y1 >= s["height"]:
continue
k = node_key(n)
if k:
out.add(k)
return out
# ──────────────────────────────────────────────────────────────────────────────
# GATE — pure existence of the top-level surfaces (jump_kind="none", depth 0).
# Kept to <=2 tests; the rest are shape deltas.
# ──────────────────────────────────────────────────────────────────────────────
@and_test(expects=("structure_gate_launch",))
def test_gate_launch():
"""App launches to MainActivity with its two anchor surfaces: the button
container (>=7 Button descendants) AND the custom seek_bar view."""
home = _home_ready()
fg = wait_for_foreground()
container = node_by_id(home, "button_container")
n_buttons = len(_visible_button_keys(home))
seek = node_by_id(home, "seek_bar")
ok = bool(fg and container and n_buttons >= 7 and seek)
msg = ""
if not fg:
msg = "setup_failed: app did not reach the foreground"
elif not container:
msg = "feature_missing: button_container not present on home"
elif n_buttons < 7:
msg = f"behavior_wrong: expected >=7 buttons on home, saw {n_buttons}"
elif not seek:
msg = "feature_missing: seek_bar not present on home"
record("structure_gate_launch", ok, msg, depth=0, jump_kind="none")
@and_test(expects=("structure_gate_toolbar",))
def test_gate_toolbar():
"""The top app bar hosts its three action controls (link / settings / info),
matched by content-desc, not by internal id or string casing."""
home = _home_ready()
bar = node_by_id(home, "top_app_bar")
links = node_by_id(home, "links_action")
settings = node_by_id(home, "settings_action")
credits = node_by_id(home, "icon_credits_action")
present = sum(1 for x in (links, settings, credits) if x)
ok = bool(bar and present == 3)
msg = ("" if ok else
f"feature_missing: top app bar host+3 actions incomplete "
f"(bar={bool(bar)}, actions={present}/3)")
record("structure_gate_toolbar", ok, msg, depth=0, jump_kind="none")
# ──────────────────────────────────────────────────────────────────────────────
# SHAPE DELTAS — in-place (depth 0) and dialog/activity (depth>=1, backed).
# ──────────────────────────────────────────────────────────────────────────────
@and_test(expects=("structure_sos_shrinks_buttons", "structure_sos_shrinks_buttons_vlm"))
def test_sos_shrinks_button_set():
"""SOS mode is a MODE SWITCH that removes a whole class of buttons: tapping
sos_button hides morse/interval/max/half/min, so the visible-Button set
SHRINKS; tapping off_button restores it. Pure in-place shape delta, depth 0."""
home = _home_ready()
before = _visible_button_keys(home)
n0 = len(before)
ok_tap = tap_node(node_by_id(home, "sos_button"))
settle("tap")
mid_nodes = ui()
during = _visible_button_keys(mid_nodes)
n1 = len(during)
shrank = ok_tap and n1 < n0
# Reverse: Off exits SOS and the button set must grow back to the original.
off = node_by_id(mid_nodes, "off_button")
ok_off = tap_node(off)
settle("tap")
after = _visible_button_keys(ui())
n2 = len(after)
restored = ok_off and n2 == n0
ok = bool(shrank and restored)
if not ok_tap:
msg = "setup_failed: sos_button not tappable"
elif not shrank:
msg = f"behavior_wrong: button set did not shrink on SOS ({n0}->{n1})"
elif not restored:
msg = f"behavior_wrong: button set not restored on Off ({n0}->{n1}->{n2})"
else:
msg = ""
record("structure_sos_shrinks_buttons", ok, msg, depth=0, jump_kind="transition",
expect=f"visible Button count {n0}->{n1} on SOS, restored to {n0} on Off")
# VLM: in SOS the left button column collapses to exactly two rounded
# buttons ("SOS" greyed, "Off" highlighted) with an empty gap below — a
# self-contained count/position fact visible in this single frame.
home2 = _home_ready()
tap_node(node_by_id(home2, "sos_button"))
settle("tap")
vlm_assert("structure_sos_shrinks_buttons",
"On the LEFT side of this screen, are there exactly two rounded "
"buttons stacked at the top (one labelled 'SOS' and one labelled "
"'Off'), with empty space below them?", RESULTS_DIR, tag="position")
@and_test(expects=("structure_morse_dialog_editext",))
def test_morse_dialog_adds_edittext():
"""Morse dialog adds a NEW window layer: home has 0 EditText; the dialog
subtree contributes an alert title + exactly one EditText. Reverse with BACK
and assert the EditText count returns to 0. depth 1, backed by helper."""
home = _home_ready()
et_before = len(find_by_class(home, "EditText"))
dlg = and_enter_morse_dialog(home)
if dlg is None:
record("structure_morse_dialog_editext", False,
"setup_failed: morse_button not tappable", depth=1,
jump_kind="transition", expect="EditText 0->1 in Morse dialog")
return
et_in = len(find_by_class(dlg, "EditText"))
title = node_by_id(dlg, "alertTitle")
added = et_in >= et_before + 1 and title is not None
# Reverse: dismiss the dialog; the EditText subtree must be gone.
and_back()
settle("tap")
et_after = len(find_by_class(ui(), "EditText"))
restored = et_after == et_before
ok = bool(added and restored)
if not added:
msg = (f"behavior_wrong: Morse dialog did not add an EditText "
f"(home={et_before}, dialog={et_in}, title={bool(title)})")
elif not restored:
msg = f"behavior_wrong: EditText not removed on BACK ({et_before}->{et_in}->{et_after})"
else:
msg = ""
record("structure_morse_dialog_editext", ok, msg, depth=1, jump_kind="transition",
expect=f"EditText {et_before}->{et_in} + alertTitle in Morse dialog, restored to {et_before}")
@and_test(expects=("structure_interval_radiobuttons",))
def test_interval_dialog_adds_radiobuttons():
"""Interval dialog introduces a class of widget absent from home: home has 0
RadioButtons; the dialog subtree has exactly 2 (TIME / BPM). Assert the
RadioButton count went 0->2, then BACK restores it to 0. depth 1, backed."""
home = _home_ready()
rb_before = len(find_by_class(home, "RadioButton"))
dlg = and_enter_interval_dialog(home)
if dlg is None:
record("structure_interval_radiobuttons", False,
"setup_failed: interval_button not tappable", depth=1,
jump_kind="transition", expect="RadioButton 0->2 in Interval dialog")
return
rb_in = len(find_by_class(dlg, "RadioButton"))
added = rb_in == rb_before + 2
and_back()
settle("tap")
rb_after = len(find_by_class(ui(), "RadioButton"))
restored = rb_after == rb_before
ok = bool(added and restored)
if not added:
msg = (f"behavior_wrong: Interval dialog RadioButton count "
f"{rb_before}->{rb_in}, expected +2")
elif not restored:
msg = f"behavior_wrong: RadioButtons not removed on BACK ({rb_before}->{rb_in}->{rb_after})"
else:
msg = ""
record("structure_interval_radiobuttons", ok, msg, depth=1, jump_kind="transition",
expect=f"RadioButton {rb_before}->{rb_in} in Interval dialog, restored to {rb_before}")
@and_test(expects=("structure_interval_mode_suffix",))
def test_interval_mode_toggles_suffix_subtree():
"""Within the Interval dialog, switching TIME->BPM removes the 'ms' suffix
node — a subtree-presence delta inside an already-open surface. Assert the
suffix node is present in TIME mode and gone in BPM mode, then restored.
depth 1 (backed by and_enter_interval_dialog)."""
home = _home_ready()
dlg = and_enter_interval_dialog(home)
if dlg is None:
record("structure_interval_mode_suffix", False,
"setup_failed: interval_button not tappable", depth=1,
jump_kind="transition", expect="suffix node present in TIME, gone in BPM")
return
suffix_time = node_by_id(dlg, "textinput_suffix_text")
present_time = suffix_time is not None
ok_bpm = tap_node(node_by_id(dlg, "bpm_button"))
settle("tap")
bpm_nodes = ui()
suffix_bpm = node_by_id(bpm_nodes, "textinput_suffix_text")
gone_bpm = suffix_bpm is None
# Reverse back to TIME: the suffix node must reappear.
ok_time = tap_node(node_by_id(bpm_nodes, "time_button"))
settle("tap")
suffix_back = node_by_id(ui(), "textinput_suffix_text")
restored = suffix_back is not None
ok = bool(present_time and ok_bpm and gone_bpm and ok_time and restored)
if not present_time:
msg = "behavior_wrong: 'ms' suffix node missing in default TIME mode"
elif not gone_bpm:
msg = "behavior_wrong: 'ms' suffix node still present after switching to BPM"
elif not restored:
msg = "behavior_wrong: 'ms' suffix node did not return on switching back to TIME"
else:
msg = ""
record("structure_interval_mode_suffix", ok, msg, depth=1, jump_kind="transition",
expect="suffix subtree present in TIME -> absent in BPM -> restored in TIME")
@and_test(expects=("structure_links_dialog_visit_buttons",))
def test_links_dialog_adds_visit_buttons():
"""Links dialog is a new window layer with 3 'Visit' row buttons that are not
on home. Assert the count of 'Visit'-labelled buttons went 0->3, BACK->0.
depth 1, backed by and_enter_links_dialog."""
home = _home_ready()
def _visit_count(nodes):
return len(find_nodes(nodes, text="Visit"))
v_before = _visit_count(home)
dlg = and_enter_links_dialog(home)
if dlg is None:
record("structure_links_dialog_visit_buttons", False,
"setup_failed: links_action not tappable", depth=1,
jump_kind="transition", expect="'Visit' buttons 0->3 in Links dialog")
return
v_in = _visit_count(dlg)
added = v_in >= 3 and v_in > v_before
and_back()
settle("tap")
v_after = _visit_count(ui())
restored = v_after == v_before
ok = bool(added and restored)
if not added:
msg = f"behavior_wrong: Links dialog 'Visit' buttons {v_before}->{v_in}, expected >=3"
elif not restored:
msg = f"behavior_wrong: 'Visit' buttons not removed on BACK ({v_before}->{v_in}->{v_after})"
else:
msg = ""
record("structure_links_dialog_visit_buttons", ok, msg, depth=1, jump_kind="transition",
expect=f"'Visit' button count {v_before}->{v_in} in Links dialog, restored to {v_before}")
@and_test(expects=("structure_about_dialog_appears",))
def test_about_dialog_appears():
"""About dialog adds a window layer over home: it contributes an alert title
plus its own action buttons (button1/button3). Assert the alert panel + its
id-addressable buttons appear, then BACK removes it (home button set returns).
depth 1, backed."""
home = _home_ready()
home_btns = len(_visible_button_keys(home))
dlg = and_enter_about_dialog(home)
if dlg is None:
record("structure_about_dialog_appears", False,
"setup_failed: icon_credits_action not tappable", depth=1,
jump_kind="transition", expect="alertTitle + >=2 dialog buttons appear")
return
title = node_by_id(dlg, "alertTitle")
# Count the dialog's OWN action buttons by id — home's underlying buttons
# stay in the tree behind the dialog and would inflate a raw class count.
dlg_btns = sum(1 for rid in ("button1", "button2", "button3")
if node_by_id(dlg, rid))
appeared = title is not None and dlg_btns >= 2
and_back()
settle("tap")
after_btns = len(_visible_button_keys(ui()))
restored = after_btns == home_btns
ok = bool(appeared and restored)
if not appeared:
msg = (f"behavior_wrong: About dialog panel incomplete "
f"(title={bool(title)}, dialog buttons={dlg_btns})")
elif not restored:
msg = f"behavior_wrong: dialog not dismissed on BACK (home buttons {home_btns}->{after_btns})"
else:
msg = ""
record("structure_about_dialog_appears", ok, msg, depth=1, jump_kind="transition",
expect=f"alertTitle + {dlg_btns} dialog buttons appear over home, removed on BACK")
@and_test(expects=("structure_settings_shape_flip",))
def test_settings_activity_shape_flip():
"""Opening Settings flips the whole tree shape: home's button_container and
seek_bar are gone, replaced by a scrollable preference list (RecyclerView)
with many TextView title/summary rows. Assert the container disappears and a
scroll container with many rows appears; BACK restores home. depth 1, backed."""
home = _home_ready()
home_container = node_by_id(home, "button_container")
st = and_enter_settings(home)
if st is None:
record("structure_settings_shape_flip", False,
"setup_failed: settings_action not tappable", depth=1,
jump_kind="transition",
expect="button_container gone, scrollable preference list present")
return
st_container = node_by_id(st, "button_container")
scroller = (find_by_class(st, "RecyclerView")
or find_by_class(st, "ScrollView")
or find_by_class(st, "ListView"))
n_text = len(find_by_class(st, "TextView"))
flipped = st_container is None and bool(scroller) and n_text >= 8
and_back()
settle("launch")
home_again = node_by_id(ui(), "button_container")
restored = home_again is not None
ok = bool(home_container is not None and flipped and restored)
if home_container is None:
msg = "setup_failed: button_container not on home before opening Settings"
elif not flipped:
msg = (f"behavior_wrong: Settings shape flip incomplete "
f"(container_gone={st_container is None}, scroller={bool(scroller)}, "
f"textviews={n_text})")
elif not restored:
msg = "behavior_wrong: home button_container did not return after leaving Settings"
else:
msg = ""
record("structure_settings_shape_flip", ok, msg, depth=1, jump_kind="transition",
expect="home button_container gone -> scrollable preference list "
f"({n_text} TextViews) in Settings, container restored on BACK")
@and_test(expects=("structure_settings_switch_rows",))
def test_settings_has_many_switch_rows():
"""The Settings preference list has a class of widget the home tree never
shows: multiple Switch controls. Home has 0 Switches; Settings has several.
Assert Switch count 0 (home) -> many (Settings). depth 1, backed."""
home = _home_ready()
sw_home = len(find_by_class(home, "Switch"))
st = and_enter_settings(home)
if st is None:
record("structure_settings_switch_rows", False,
"setup_failed: settings_action not tappable", depth=1,
jump_kind="transition", expect="Switch count 0 (home) -> >=3 (Settings)")
return
sw_settings = len(find_by_class(st, "Switch"))
ok = sw_home == 0 and sw_settings >= 3
msg = ("" if ok else
f"behavior_wrong: Switch count home={sw_home} settings={sw_settings}, "
f"expected 0 -> >=3")
record("structure_settings_switch_rows", ok, msg, depth=1, jump_kind="transition",
expect=f"Switch count {sw_home} (home) -> {sw_settings} (Settings)")
# ──────────────────────────────────────────────────────────────────────────────
# Remaining VLM records (spatial layout, unreadable from the dump). depth 0,
# jump_kind="none" — these describe the static rendered home surface.
# Total VLM records in this file = 5 (SOS layout above + the four below).
# ──────────────────────────────────────────────────────────────────────────────
@and_test(expects=("structure_home_layout_vlm",))
def test_home_layout_vlm():
"""Home layout is a two-column split: rounded action/brightness buttons fill
the LEFT half, and the segmented level indicator fills the RIGHT half."""
_home_ready()
vlm_assert("structure_home_layout",
"Is the screen split into two columns, with a stack of rounded "
"buttons (SOS, Morse, Maximum, Off) filling the LEFT half and a "
"separate column of stacked rounded blocks on the RIGHT half?",
RESULTS_DIR, tag="position")
@and_test(expects=("structure_toolbar_icons_vlm",))
def test_toolbar_icons_vlm():
"""Toolbar: three icon buttons in the top-right (chain/link, sliders, info)."""
_home_ready()
vlm_assert("structure_toolbar_icons",
"Are there three separate icon buttons grouped in the top-right "
"corner of the top app bar (a link/chain icon, a sliders icon, and "
"an info circle icon)?", RESULTS_DIR, tag="position")
@and_test(expects=("structure_seekbar_segments_vlm",))
def test_seekbar_segments_vlm():
"""Seekbar shape: a tall vertical bar divided into ~10 stacked rounded
segments. Set Max first so the segments are lit and clearly delineated."""
home = _home_ready()
tap_node(node_by_id(home, "max_button"))
settle("tap")
vlm_assert("structure_seekbar_segments",
"On the right side of the screen, is there a tall vertical bar that "
"is divided into roughly ten stacked rounded segments?",
RESULTS_DIR, tag="shape")
@and_test(expects=("structure_button_pills_vlm",))
def test_button_pill_shapes_vlm():
"""Button shapes: Maximum/Half/Minimum/Off are large rounded pill shapes
stacked vertically."""
_home_ready()
vlm_assert("structure_button_pills",
"Are the brightness buttons (Maximum, Half, Minimum, Off) drawn as "
"large rounded pill/capsule shapes stacked vertically in a column?",
RESULTS_DIR, tag="shape")
TESTS = [
test_gate_launch,
test_gate_toolbar,
test_sos_shrinks_button_set,
test_morse_dialog_adds_edittext,
test_interval_dialog_adds_radiobuttons,
test_interval_mode_toggles_suffix_subtree,
test_links_dialog_adds_visit_buttons,
test_about_dialog_appears,
test_settings_activity_shape_flip,
test_settings_has_many_switch_rows,
test_home_layout_vlm,
test_toolbar_icons_vlm,
test_seekbar_segments_vlm,
test_button_pill_shapes_vlm,
]
if __name__ == "__main__":
run_suite(TESTS, "structure")