#!/usr/bin/env python3 """COMPUTATION-lane behavioral suite for FlashDim (input -> output). This lane is the sole owner of jump_kind="value". The app's core function is setting a flashlight brightness LEVEL; the observable OUTPUT is the `level_indicator` TextView, formatted " / ". Every assertion here is a real INPUT -> OUTPUT computation: A. tap a level quick-button (Max/Half/Min/Off) -> exact indicator string. maxLevel is DERIVED at runtime (parsed from the Max result "N / N"), never hardcoded; the Half test then asserts the app computes max//2. B. tap the custom vertical seek_bar at a y-fraction -> level = f(position), asserting the exact indicator string observed live at each fraction. C. (depth 2) the Settings "Preferred level" slider dialog -> "New: N", asserting New = f(track fraction). All expected values were OBSERVED on the running original app, never computed from source or a constant. The three VLM records verify the CANVAS-drawn seekbar FILL (which never reaches the view tree) agrees with the numeric level. """ import os import re import sys sys.path.insert(0, os.path.dirname(os.path.abspath(__file__))) from android_testgen_kit import * # noqa: F403,F401 # ── Home helpers ──────────────────────────────────────────────────────────── # restart_app() resumes the app's existing task, and FlashDim's back-stack # (MainActivity -> SettingsActivity, etc.) survives force-stop, so a raw restart # can land on Settings. _go_home() backs out until the home surface (identified # by the level_indicator TextView) is showing. def _go_home(max_back: int = 6) -> bool: restart_app() wait_for_foreground(15) settle("launch") for _ in range(max_back): if node_by_id(ui(), "level_indicator") is not None: return True back() settle("launch") return node_by_id(ui(), "level_indicator") is not None def _read_indicator() -> str: """The 'N / M' text out of a FRESH dump.""" n = node_by_id(ui(), "level_indicator") return n.get("text", "").strip() if n else "" def _norm(s: str) -> str: """Collapse whitespace so '10 / 10' and '10 / 10' compare equal.""" return re.sub(r"\s+", " ", s.strip()) def _parse_pair(s: str): """'N / M' -> (N, M) ints, or None.""" m = re.search(r"(\d+)\s*/\s*(\d+)", s) return (int(m.group(1)), int(m.group(2))) if m else None def _derive_max(): """Run the app to learn maxLevel: tap Max, the indicator reads 'max / max'. Returns (max, note). Never assumes 10.""" if not _go_home(): return None, "setup_failed: home surface (level_indicator) not reachable" if not tap_node(node_by_id(ui(), "max_button")): return None, "setup_failed: max_button not tappable" pair = _parse_pair(_read_indicator()) if not pair: return None, f"setup_failed: could not parse indicator after Max ({_read_indicator()!r})" lvl, mx = pair if lvl != mx: return None, f"behavior_wrong: Max gave '{lvl} / {mx}', expected level==max" return mx, "" def _seek_bounds(): """Fresh seek_bar geometry (cx, y1, height) — re-read before EVERY tap because a state change invalidates coordinates.""" n = node_by_id(ui(), "seek_bar") if not n or "_bounds" not in n: return None x1, y1, x2, y2 = n["_bounds"] return (x1 + x2) // 2, y1, y2 - y1 def _seek_to(frac: float) -> str: """Tap the seek_bar at vertical fraction `frac` (0=top). Returns the resulting indicator string, or '' if the bar was not found.""" b = _seek_bounds() if not b: return "" cx, y1, h = b tap(cx, y1 + round(frac * h)) return _read_indicator() def _shot(name: str) -> None: screencap(os.path.join(RESULTS_DIR, "screenshots", f"{name}.png")) # ══════════════════════════════════════════════════════════════════════════════ # A. Level quick-button -> indicator value (depth 0, jump_kind=value) # Each test derives maxLevel live, then asserts the exact observed string. # Positive/negative pairing: the answer is ABSENT (indicator != target) # before the tap and PRESENT after. # ══════════════════════════════════════════════════════════════════════════════ @and_test(expects=("computation_max_button",)) def test_max_button(): mx, note = _derive_max() # _derive_max already taps Max if mx is None: record("computation_max_button", False, note, depth=0, jump_kind="value", expect="' / '") return expected = f"{mx} / {mx}" got = _read_indicator() ok = _norm(got) == _norm(expected) record("computation_max_button", ok, "" if ok else f"behavior_wrong: Max shows {got!r}, expected {expected!r}", depth=0, jump_kind="value", expect=f"indicator == '{expected}'") @and_test(expects=("computation_half_button",)) def test_half_button(): # Real computation: assert the app computes maxLevel // 2 (integer division). mx, note = _derive_max() if mx is None: record("computation_half_button", False, note, depth=0, jump_kind="value", expect="' / '") return before = _read_indicator() # currently 'max / max' from _derive_max expected = f"{mx // 2} / {mx}" ok_tap = tap_node(node_by_id(ui(), "half_button")) got = _read_indicator() # negative->positive: the half value must have been ABSENT then PRESENT ok = ok_tap and _norm(got) == _norm(expected) and _norm(before) != _norm(expected) if not ok_tap: msg = "setup_failed: half_button not tappable" elif _norm(got) != _norm(expected): msg = f"behavior_wrong: Half shows {got!r}, expected {expected!r} (max//2)" else: msg = "" record("computation_half_button", ok, msg, depth=0, jump_kind="value", expect=f"indicator == '{expected}' (max//2)") @and_test(expects=("computation_min_button",)) def test_min_button(): mx, note = _derive_max() if mx is None: record("computation_min_button", False, note, depth=0, jump_kind="value", expect="'1 / '") return expected = f"1 / {mx}" ok_tap = tap_node(node_by_id(ui(), "min_button")) got = _read_indicator() ok = ok_tap and _norm(got) == _norm(expected) msg = ("" if ok else ("setup_failed: min_button not tappable" if not ok_tap else f"behavior_wrong: Min shows {got!r}, expected {expected!r}")) record("computation_min_button", ok, msg, depth=0, jump_kind="value", expect=f"indicator == '{expected}'") @and_test(expects=("computation_off_button",)) def test_off_button(): # Positive/negative: turn the flash ON (Max) first so the '0 / max' answer # is provably ABSENT, then Off makes it PRESENT. mx, note = _derive_max() # taps Max -> 'max / max' if mx is None: record("computation_off_button", False, note, depth=0, jump_kind="value", expect="'0 / '") return before = _read_indicator() # 'max / max' expected = f"0 / {mx}" ok_tap = tap_node(node_by_id(ui(), "off_button")) got = _read_indicator() ok = (ok_tap and _norm(got) == _norm(expected) and _norm(before) != _norm(expected)) msg = ("" if ok else ("setup_failed: off_button not tappable" if not ok_tap else f"behavior_wrong: Off shows {got!r}, expected {expected!r}")) record("computation_off_button", ok, msg, depth=0, jump_kind="value", expect=f"indicator == '{expected}'") # ══════════════════════════════════════════════════════════════════════════════ # B. Seekbar position -> level value (depth 0, jump_kind=value) # level = f(y-position) on the custom vertical LightLevelSeekBar. # Fractions and expected strings were OBSERVED live and are deterministic # across repeated runs. Bounds are re-read before EACH tap. # ══════════════════════════════════════════════════════════════════════════════ @and_test(expects=("computation_seek_top_max", "computation_seek_top_max_vlm")) def test_seek_top_max(): if not _go_home(): record("computation_seek_top_max", False, "setup_failed: home not reachable", depth=0, jump_kind="value", expect="' / '") return mx = _parse_pair(_read_indicator()) mx = mx[1] if mx else 10 got = _seek_to(0.03) # top of the bar -> saturates at max if not got: record("computation_seek_top_max", False, "setup_failed: seek_bar not found", depth=0, jump_kind="value", expect=f"'{mx} / {mx}'") return expected = f"{mx} / {mx}" ok = _norm(got) == _norm(expected) record("computation_seek_top_max", ok, "" if ok else f"behavior_wrong: top tap shows {got!r}, expected {expected!r}", depth=0, jump_kind="value", expect=f"indicator == '{expected}'") _shot("computation_seek_top_max_vlm") vlm_assert("computation_seek_top_max", "In this flashlight app, the vertical segmented bar on the right " "side is at its maximum level. Are ALL of its segments (roughly ten " "stacked bars, top to bottom) filled/highlighted in the bright " "accent color rather than dark gray?", RESULTS_DIR, expect="YES", depth=0, jump_kind="value", tag="color") @and_test(expects=("computation_seek_mid_half", "computation_seek_mid_half_vlm")) def test_seek_mid_half(): if not _go_home(): record("computation_seek_mid_half", False, "setup_failed: home not reachable", depth=0, jump_kind="value", expect="' / '") return mx = _parse_pair(_read_indicator()) mx = mx[1] if mx else 10 got = _seek_to(0.50) # middle -> half if not got: record("computation_seek_mid_half", False, "setup_failed: seek_bar not found", depth=0, jump_kind="value", expect=f"'{mx // 2} / {mx}'") return expected = f"{mx // 2} / {mx}" ok = _norm(got) == _norm(expected) record("computation_seek_mid_half", ok, "" if ok else f"behavior_wrong: middle tap shows {got!r}, expected {expected!r}", depth=0, jump_kind="value", expect=f"indicator == '{expected}'") _shot("computation_seek_mid_half_vlm") vlm_assert("computation_seek_mid_half", "In this flashlight app, the vertical segmented bar on the right is " "set to roughly half. Is the bar filled/highlighted from the BOTTOM " "up to about the MIDDLE (about half of the stacked segments bright, " "the upper half dark)?", RESULTS_DIR, expect="YES", depth=0, jump_kind="value", tag="position") @and_test(expects=("computation_seek_low_one", "computation_seek_low_one_vlm")) def test_seek_low_one(): if not _go_home(): record("computation_seek_low_one", False, "setup_failed: home not reachable", depth=0, jump_kind="value", expect="'1 / '") return mx = _parse_pair(_read_indicator()) mx = mx[1] if mx else 10 got = _seek_to(0.90) # near bottom -> level 1 if not got: record("computation_seek_low_one", False, "setup_failed: seek_bar not found", depth=0, jump_kind="value", expect=f"'1 / {mx}'") return expected = f"1 / {mx}" ok = _norm(got) == _norm(expected) record("computation_seek_low_one", ok, "" if ok else f"behavior_wrong: low tap shows {got!r}, expected {expected!r}", depth=0, jump_kind="value", expect=f"indicator == '{expected}'") _shot("computation_seek_low_one_vlm") vlm_assert("computation_seek_low_one", "In this flashlight app, the vertical segmented bar on the right is " "set to its lowest non-zero level. Is ONLY the single bottom segment " "highlighted in the bright accent color while all segments above it " "are dark gray?", RESULTS_DIR, expect="YES", depth=0, jump_kind="value", tag="color") @and_test(expects=("computation_seek_bottom_zero",)) def test_seek_bottom_zero(): # Divide/edge case: tapping the very bottom drives the level to 0 (off). # Positive/negative: set Max first so '0 / max' is ABSENT, then bottom tap # makes it PRESENT. if not _go_home(): record("computation_seek_bottom_zero", False, "setup_failed: home not reachable", depth=0, jump_kind="value", expect="'0 / '") return mx = _parse_pair(_read_indicator()) mx = mx[1] if mx else 10 _seek_to(0.03) # drive to max first before = _read_indicator() got = _seek_to(0.999) # very bottom -> 0 if not got: record("computation_seek_bottom_zero", False, "setup_failed: seek_bar not found", depth=0, jump_kind="value", expect=f"'0 / {mx}'") return expected = f"0 / {mx}" ok = _norm(got) == _norm(expected) and _norm(before) != _norm(expected) record("computation_seek_bottom_zero", ok, "" if ok else f"behavior_wrong: bottom tap shows {got!r}, expected {expected!r}", depth=0, jump_kind="value", expect=f"indicator == '{expected}'") @and_test(expects=("computation_seek_frac_seven",)) def test_seek_frac_seven(): # Additional mapping point verified live: f=0.30 -> level 7. if not _go_home(): record("computation_seek_frac_seven", False, "setup_failed: home not reachable", depth=0, jump_kind="value", expect="'7 / '") return mx = _parse_pair(_read_indicator()) mx = mx[1] if mx else 10 got = _seek_to(0.30) if not got: record("computation_seek_frac_seven", False, "setup_failed: seek_bar not found", depth=0, jump_kind="value", expect=f"'7 / {mx}'") return expected = f"7 / {mx}" ok = _norm(got) == _norm(expected) record("computation_seek_frac_seven", ok, "" if ok else f"behavior_wrong: 0.30 tap shows {got!r}, expected {expected!r}", depth=0, jump_kind="value", expect=f"indicator == '{expected}'") @and_test(expects=("computation_seek_frac_three",)) def test_seek_frac_three(): # Additional mapping point verified live: f=0.70 -> level 3. if not _go_home(): record("computation_seek_frac_three", False, "setup_failed: home not reachable", depth=0, jump_kind="value", expect="'3 / '") return mx = _parse_pair(_read_indicator()) mx = mx[1] if mx else 10 got = _seek_to(0.70) if not got: record("computation_seek_frac_three", False, "setup_failed: seek_bar not found", depth=0, jump_kind="value", expect=f"'3 / {mx}'") return expected = f"3 / {mx}" ok = _norm(got) == _norm(expected) record("computation_seek_frac_three", ok, "" if ok else f"behavior_wrong: 0.70 tap shows {got!r}, expected {expected!r}", depth=0, jump_kind="value", expect=f"indicator == '{expected}'") @and_test(expects=("computation_seek_monotonic",)) def test_seek_monotonic(): # Cross-check the mapping is a genuine decreasing function of y (top>bottom), # not a fixed value: level(0.03) > level(0.50) > level(0.90). if not _go_home(): record("computation_seek_monotonic", False, "setup_failed: home not reachable", depth=0, jump_kind="value", expect="level(top) > level(mid) > level(low)") return vals = [] for frac in (0.03, 0.50, 0.90): p = _parse_pair(_seek_to(frac)) vals.append(p[0] if p else None) if any(v is None for v in vals): record("computation_seek_monotonic", False, f"setup_failed: could not read all levels {vals}", depth=0, jump_kind="value", expect="level(top) > level(mid) > level(low)") return top, mid, low = vals ok = top > mid > low record("computation_seek_monotonic", ok, "" if ok else f"behavior_wrong: levels top/mid/low={vals} not strictly decreasing", depth=0, jump_kind="value", expect=f"level decreases top->bottom (got {top}>{mid}>{low})") # ══════════════════════════════════════════════════════════════════════════════ # C. Preferred-level slider dialog (depth 2, jump_kind=value) # home -> Settings (depth 1) -> "Preferred level" dialog (depth 2). # Track fraction f -> "New: N". Two well-separated fractions verified live # (f=0.30 -> New 3, f=0.70 -> New 8). Cancel to avoid persisting state. # ══════════════════════════════════════════════════════════════════════════════ def and_enter_preferred_level(): """Open Settings, then the 'Set preferred level' dialog. Returns (ok, note). Counts two surfaces (Settings, dialog) via and_enter().""" if not _go_home(): return False, "setup_failed: home not reachable" settings = find_node(ui(), content_desc="Settings") if not tap_node(settings): return False, "setup_failed: Settings action not tappable" settle("launch") if node_by_id(ui(), "recycler_view") is None: return False, "setup_failed: Settings screen did not open" and_enter("settings") row = find_node_ci(ui(), "Preferred level") if not tap_node(row): return False, "setup_failed: 'Preferred level' row not tappable" settle("tap") title = find_node_ci(ui(), "Set preferred level") if title is None: return False, "setup_failed: 'Set preferred level' dialog did not open" and_enter("preferred_level_dialog") return True, "" def _read_new_value(): """Parse the 'New: N' number from the dialog's TextView.""" for n in ui(): t = n.get("text", "").replace(" ", "\n") m = re.search(r"New:\s*(\d+)", t) if m: return int(m.group(1)) return None def _slider_set(frac: float): """Tap the slider track in the customPanel at horizontal fraction `frac`; return the resulting New value (int) or None. Re-reads bounds each call.""" cp = node_by_id(ui(), "customPanel") if not cp or "_bounds" not in cp: return None x1, y1, x2, y2 = cp["_bounds"] tx = x1 + round(frac * (x2 - x1)) tx = max(x1 + 1, min(x2 - 1, tx)) # The New/Current text sits at the top of customPanel; the slider track is # in its lower half. Tap ~78% down the panel height (verified live). ty = y1 + round(0.78 * (y2 - y1)) tap(tx, ty) return _read_new_value() @and_test(expects=("computation_pref_slider_low",)) def test_pref_slider_low(): ok_nav, note = and_enter_preferred_level() if not ok_nav: record("computation_pref_slider_low", False, note, depth=2, jump_kind="value", expect="dialog shows 'New: 3'") return before = _read_new_value() got = _slider_set(0.30) # verified live -> New 3 ok = got == 3 and before != 3 if got is None: msg = "setup_failed: could not read 'New:' value after slider tap" elif got != 3: msg = f"behavior_wrong: slider f=0.30 gave New:{got}, expected New:3" else: msg = "" record("computation_pref_slider_low", ok, msg, depth=2, jump_kind="value", expect="dialog shows 'New: 3'") # Cancel so the preferred-level pref is not persisted. tap_text(ui(), "Cancel") @and_test(expects=("computation_pref_slider_high",)) def test_pref_slider_high(): ok_nav, note = and_enter_preferred_level() if not ok_nav: record("computation_pref_slider_high", False, note, depth=2, jump_kind="value", expect="dialog shows 'New: 8'") return got = _slider_set(0.70) # verified live -> New 8 ok = got == 8 if got is None: msg = "setup_failed: could not read 'New:' value after slider tap" elif got != 8: msg = f"behavior_wrong: slider f=0.70 gave New:{got}, expected New:8" else: msg = "" record("computation_pref_slider_high", ok, msg, depth=2, jump_kind="value", expect="dialog shows 'New: 8'") tap_text(ui(), "Cancel") @and_test(expects=("computation_pref_slider_monotonic",)) def test_pref_slider_monotonic(): # The slider is a real function of position: New(0.30) < New(0.70). ok_nav, note = and_enter_preferred_level() if not ok_nav: record("computation_pref_slider_monotonic", False, note, depth=2, jump_kind="value", expect="New(low) < New(high)") return low = _slider_set(0.30) high = _slider_set(0.70) if low is None or high is None: record("computation_pref_slider_monotonic", False, f"setup_failed: could not read New values (low={low}, high={high})", depth=2, jump_kind="value", expect="New(low) < New(high)") tap_text(ui(), "Cancel") return ok = low < high record("computation_pref_slider_monotonic", ok, "" if ok else f"behavior_wrong: New(0.30)={low} not < New(0.70)={high}", depth=2, jump_kind="value", expect=f"New increases with position (got {low} < {high})") tap_text(ui(), "Cancel") TESTS = [ # A. level quick-buttons test_max_button, test_half_button, test_min_button, test_off_button, # B. seekbar position -> level test_seek_top_max, test_seek_mid_half, test_seek_low_one, test_seek_bottom_zero, test_seek_frac_seven, test_seek_frac_three, test_seek_monotonic, # C. preferred-level slider dialog (depth 2) test_pref_slider_low, test_pref_slider_high, test_pref_slider_monotonic, ] if __name__ == "__main__": if not device_online(): print("Device offline — aborting.") sys.exit(1) run_suite(TESTS, "computation")