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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
#!/usr/bin/env python3
"""NAVIGATION-lane behavioral suite for FlashDim.

Owns surface REACHABILITY and BACK-STACK integrity: tapping a nav trigger
(toolbar action, quick-action button, preference row) lands on the intended
surface, and BACK unwinds it hop by hop. This lane is the SOLE owner of
jump_kind="reach" and of the depth>=2 chains — every jump is PROVEN with
and_reach(before, after, target, source): the TARGET surface's discriminating
element APPEARED *and* the SOURCE surface's element DISAPPEARED. A screen that was
in the dump all along was never reached, and a dead shell that never opens the
dialog FAILS.

CEDES: dialog CONTENT strings -> text; state flips (radio/switch) -> states;
displayed values (level, counter) -> computation; tree SHAPE counts -> structure.
Here we assert only that we crossed a surface boundary and that BACK returns us.

All target/source strings below were observed live on the original app:
  home  --morse_button-->      "Flash morse code"      (Off disappears)
  home  --interval_button-->   "Interval Flash"        (Off disappears)
  home  --icon_credits_action->"Show Icon Credits"     (Off disappears)   [About]
  home  --links_action-->      "Repository on GitHub"  (Off disappears)   [Links]
  home  --settings_action-->   "Flashlight controls"   (Off disappears)   [Settings]
  About --Show Icon Credits--> "Open Pictogrammers"    (Show Icon Credits gone) [d2]
  Settings --Preferred level-->"Set preferred level"   (Context awareness gone) [d2]
  Settings --about row-->      "Repository on GitHub"  (Flashlight controls gone)[d2]
"""
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 / jump_kind="reach" must be backed by
# a real and_enter_*/and_reach_* call so the depth audit sees genuine navigation
# (never a relaunch). The and_reach_* helpers return the reach VERDICT (bool) and
# count one surface via and_reach_surface(); the and_enter_* helpers open a
# surface and return its fresh node list for chaining.
# ──────────────────────────────────────────────────────────────────────────────

def _home():
    """Restart to a settled MainActivity and return its node list."""
    restart_app()
    wait_for_foreground()
    return ui()


def and_reach_morse_dialog(home):
    """home --morse_button--> Morse dialog. Verdict: title appears, Off gone."""
    n = node_by_id(home, "morse_button")
    return bool(n) and and_reach_surface(n, "Flash morse code", "Off")


def and_reach_interval_dialog(home):
    """home --interval_button--> Interval dialog."""
    n = node_by_id(home, "interval_button")
    return bool(n) and and_reach_surface(n, "Interval Flash", "Off")


def and_reach_about_dialog(home):
    """home --icon_credits_action--> About dialog (button 'Show Icon Credits')."""
    n = node_by_id(home, "icon_credits_action")
    return bool(n) and and_reach_surface(n, "Show Icon Credits", "Off")


def and_reach_links_dialog(home):
    """home --links_action--> Links dialog (row 'Repository on GitHub')."""
    n = node_by_id(home, "links_action")
    return bool(n) and and_reach_surface(n, "Repository on GitHub", "Off")


def and_enter_about_dialog(home):
    """Open the About dialog and return its fresh node list (for chaining)."""
    if not and_reach_about_dialog(home):
        return None
    return ui()


def and_reach_icon_credits(about):
    """About --Show Icon Credits--> Icon Credits dialog (button 'Open
    Pictogrammers'); the 'Show Icon Credits' button disappears."""
    n = node_by_id(about, "button1")  # "Show Icon Credits"
    return bool(n) and and_reach_surface(n, "Open Pictogrammers", "Show Icon Credits")


def and_reach_settings(home):
    """home --settings_action--> SettingsActivity. Verdict: 'Flashlight controls'
    category appears, home 'Quick Actions' label disappears (home 'Off' cannot be
    the source: the Settings summaries contain the substring 'off')."""
    n = node_by_id(home, "settings_action")
    return bool(n) and and_reach_surface(n, "Flashlight controls", "Quick Actions")


def and_enter_settings(home):
    """Open Settings and return its fresh node list (for chaining)."""
    if not and_reach_settings(home):
        return None
    return ui()


def and_reach_preferred_level(settings):
    """Settings --Preferred level row--> 'Set preferred level' dialog; the
    'Context awareness' row disappears (its own title, not the row we tapped,
    because 'Set preferred level' contains 'preferred level' as a substring)."""
    row = find_node_ci(settings, "Preferred level")
    return bool(row) and and_reach_surface(row, "Set preferred level", "Context awareness")


def and_reach_settings_links(settings):
    """Settings --about row ('by Cyb3rKo')--> Links dialog. 'Repository on GitHub'
    appears; the 'Flashlight controls' category disappears."""
    row = find_node_ci(settings, "by Cyb3rKo") or find_node_ci(settings, "FlashDim Dev")
    return bool(row) and and_reach_surface(row, "Repository on GitHub", "Flashlight controls")


def _home_restored():
    """True iff we are back on the MainActivity home surface (Off button visible
    and NOT on a blank/dead screen)."""
    nodes = ui()
    return (node_by_id(nodes, "off_button") is not None
            and find_node_ci(nodes, "Quick Actions") is not None)


# ══════════════════════════════════════════════════════════════════════════════
# DEPTH-1 reach: each top-level destination reached from home.
# ══════════════════════════════════════════════════════════════════════════════

@and_test(expects=("nav_reach_morse", "nav_reach_morse_vlm"))
def test_reach_morse():
    home = _home()
    ok = and_reach_morse_dialog(home)
    record("nav_reach_morse", ok,
           "" if ok else "feature_missing: Morse dialog ('Flash morse code') did "
           "not appear / home 'Off' did not disappear after tapping morse_button",
           depth=1, jump_kind="reach",
           expect="'Flash morse code' dialog title visible; home 'Off' button gone")
    vlm_assert("nav_reach_morse",
               "Is a dialog titled 'Flash morse code' shown with a single-line "
               "text input field above a 'Flash' button?",
               RESULTS_DIR, tag="layout")


@and_test(expects=("nav_reach_interval",))
def test_reach_interval():
    home = _home()
    ok = and_reach_interval_dialog(home)
    record("nav_reach_interval", ok,
           "" if ok else "feature_missing: Interval dialog ('Interval Flash') did "
           "not appear / home 'Off' did not disappear after tapping interval_button",
           depth=1, jump_kind="reach",
           expect="'Interval Flash' dialog title visible; home 'Off' button gone")


@and_test(expects=("nav_reach_interval_vlm",))
def test_reach_interval_vlm():
    home = _home()
    ok = and_reach_interval_dialog(home)
    record("nav_reach_interval_probe", ok,
           "" if ok else "setup_failed: could not open Interval dialog for VLM",
           depth=1, jump_kind="reach",
           expect="'Interval Flash' dialog reached for screenshot")
    vlm_assert("nav_reach_interval",
               "Is a dialog shown with two side-by-side radio toggles labelled "
               "'TIME' and 'BPM' and a numeric input field?",
               RESULTS_DIR, tag="position")


@and_test(expects=("nav_reach_links", "nav_reach_links_vlm"))
def test_reach_links():
    home = _home()
    ok = and_reach_links_dialog(home)
    record("nav_reach_links", ok,
           "" if ok else "feature_missing: Links dialog ('Repository on GitHub') "
           "did not appear / home 'Off' did not disappear after tapping links_action",
           depth=1, jump_kind="reach",
           expect="'Repository on GitHub' row visible; home 'Off' button gone")
    vlm_assert("nav_reach_links",
               "Is a dialog titled 'Links' shown listing several rows, each row "
               "having a button labelled 'Visit' on its right side?",
               RESULTS_DIR, tag="position")


@and_test(expects=("nav_reach_about",))
def test_reach_about():
    home = _home()
    ok = and_reach_about_dialog(home)
    record("nav_reach_about", ok,
           "" if ok else "feature_missing: About dialog ('Show Icon Credits' "
           "button) did not appear / home 'Off' did not disappear after tapping "
           "icon_credits_action",
           depth=1, jump_kind="reach",
           expect="'Show Icon Credits' button visible; home 'Off' button gone")


@and_test(expects=("nav_reach_settings", "nav_reach_settings_vlm"))
def test_reach_settings():
    home = _home()
    ok = and_reach_settings(home)
    # A single-Activity app cannot discriminate by activity, but FlashDim uses a
    # real SettingsActivity, so corroborate the surface jump with it.
    on_settings = "SettingsActivity" in get_current_activity()
    record("nav_reach_settings", ok and on_settings,
           "" if ok and on_settings else "feature_missing: Settings surface "
           "('Flashlight controls') did not appear / SettingsActivity not resumed",
           depth=1, jump_kind="reach",
           expect="'Flashlight controls' category visible in SettingsActivity; "
                  "home 'Quick Actions' label gone")
    vlm_assert("nav_reach_settings",
               "Is a settings screen shown as a vertical scrollable list of "
               "labelled rows, several rows having an on/off toggle switch on the "
               "right, with a back arrow at the top-left?",
               RESULTS_DIR, tag="position")


# ══════════════════════════════════════════════════════════════════════════════
# DEPTH-2 chains (owned by this lane). Each chains two and_enter/and_reach hops.
# ══════════════════════════════════════════════════════════════════════════════

@and_test(expects=("nav_reach_icon_credits_d2", "nav_reach_icon_credits_d2_vlm"))
def test_reach_icon_credits_depth2():
    home = _home()
    about = and_enter_about_dialog(home)          # hop 1: home -> About
    deep = about is not None and and_reach_icon_credits(about)  # hop 2 -> Icon Credits
    record("nav_reach_icon_credits_d2", deep,
           "" if deep else "feature_missing: Icon Credits dialog ('Open "
           "Pictogrammers') not reached from About via 'Show Icon Credits'",
           depth=2, jump_kind="reach",
           expect="'Open Pictogrammers' button visible; 'Show Icon Credits' gone "
                  "(home -> About -> Icon Credits)")
    vlm_assert("nav_reach_icon_credits_d2",
               "Is a dialog titled 'Icon Credits' shown, listing icon credit lines "
               "and a button reading 'Open Pictogrammers'?",
               RESULTS_DIR, tag="content")


@and_test(expects=("nav_reach_preferred_level_d2", "nav_reach_preferred_level_d2_vlm"))
def test_reach_preferred_level_depth2():
    home = _home()
    settings = and_enter_settings(home)           # hop 1: home -> Settings
    deep = settings is not None and and_reach_preferred_level(settings)  # hop 2
    record("nav_reach_preferred_level_d2", deep,
           "" if deep else "feature_missing: 'Set preferred level' dialog not "
           "reached from Settings via the 'Preferred level' row",
           depth=2, jump_kind="reach",
           expect="'Set preferred level' dialog visible; 'Context awareness' row "
                  "gone (home -> Settings -> preferred-level dialog)")
    vlm_assert("nav_reach_preferred_level_d2",
               "In the centered pop-up dialog, is there a wide horizontal "
               "light-purple / lavender bar with a row of small evenly-spaced dots "
               "along it?",
               RESULTS_DIR, tag="shape")


@and_test(expects=("nav_reach_settings_links_d2",))
def test_reach_settings_links_depth2():
    home = _home()
    settings = and_enter_settings(home)           # hop 1: home -> Settings
    deep = settings is not None and and_reach_settings_links(settings)  # hop 2
    record("nav_reach_settings_links_d2", deep,
           "" if deep else "feature_missing: Links dialog not reached from the "
           "Settings 'about' row (home -> Settings -> Links)",
           depth=2, jump_kind="reach",
           expect="'Repository on GitHub' visible; 'Flashlight controls' category "
                  "gone (home -> Settings -> Links)")


# ══════════════════════════════════════════════════════════════════════════════
# BACK-STACK integrity: BACK returns to where we came from, hop by hop.
# ══════════════════════════════════════════════════════════════════════════════

@and_test(expects=("nav_back_morse_to_home",))
def test_back_from_morse_returns_home():
    home = _home()
    deep = and_reach_morse_dialog(home)
    # one BACK pops the dialog; home 'Off'/'Quick Actions' must be restored and it
    # must NOT be a blank/dead screen.
    back_ok = deep and and_back() == 0 and _home_restored()
    record("nav_back_morse_to_home", back_ok,
           "" if back_ok else "behavior_wrong: BACK from the Morse dialog did not "
           "restore the home surface (Off / Quick Actions not visible)",
           depth=1, jump_kind="reach",
           expect="home 'Off' button + 'Quick Actions' restored after one BACK")


@and_test(expects=("nav_back_settings_to_home",))
def test_back_from_settings_returns_home():
    home = _home()
    deep = and_reach_settings(home)
    back_ok = deep and and_back() == 0 and _home_restored()
    on_home = "MainActivity" in get_current_activity()
    record("nav_back_settings_to_home", back_ok and on_home,
           "" if back_ok and on_home else "behavior_wrong: BACK from Settings did "
           "not return to the home MainActivity surface",
           depth=1, jump_kind="reach",
           expect="MainActivity home restored (Off visible) after one BACK from "
                  "Settings")


@and_test(expects=("nav_up_arrow_settings_to_home",))
def test_up_arrow_from_settings_returns_home():
    home = _home()
    deep = and_reach_settings(home)
    # The toolbar Up affordance (content-desc 'Navigate up') is the intended way
    # back from Settings; assert it lands on home.
    up = find_node(ui(), content_desc="Navigate up") if deep else None
    tapped = up is not None and tap_node(up)
    if tapped:
        settle("launch")
        and_reset_depth()  # the Up affordance popped the surface (no BACK keypress)
    up_ok = tapped and _home_restored() and "MainActivity" in get_current_activity()
    record("nav_up_arrow_settings_to_home", up_ok,
           "" if up_ok else "behavior_wrong: the Settings toolbar Up arrow did not "
           "return to the home MainActivity surface",
           depth=1, jump_kind="reach",
           expect="MainActivity home restored after tapping the 'Navigate up' arrow")


@and_test(expects=("nav_backstack_icon_credits_unwinds",))
def test_backstack_icon_credits_unwinds():
    """Back-stack from the DEEPEST surface to home. We descend two hops
    (home -> About -> Icon Credits, depth 2). OBSERVED on the original: About and
    Icon Credits share one AlertDialog instance (its content is swapped in place),
    so a single BACK from the deepest surface dismisses the whole dialog straight
    to home rather than re-showing About. Assert that observed unwind: BACK from
    Icon Credits restores home (Off + Quick Actions) and it is NOT a blank screen,
    and the deepest surface's 'Open Pictogrammers' is gone."""
    home = _home()
    about = and_enter_about_dialog(home)
    deep = about is not None and and_reach_icon_credits(about)  # depth 2
    unwound = False
    if deep:
        and_back()                       # pop the shared dialog
        after = ui()
        unwound = (_home_restored()
                   and find_node_ci(after, "Open Pictogrammers") is None
                   and "MainActivity" in get_current_activity())
    ok = deep and unwound
    record("nav_backstack_icon_credits_unwinds", ok,
           "" if ok else "behavior_wrong: BACK from the depth-2 Icon Credits "
           "surface did not return to the home MainActivity (Off / Quick Actions)",
           depth=2, jump_kind="reach",
           expect="one BACK from Icon Credits (depth 2) restores home ('Off' + "
                  "'Quick Actions'); 'Open Pictogrammers' gone")


@and_test(expects=("nav_backstack_preferred_level_unwinds",))
def test_backstack_preferred_level_unwinds():
    """home -> Settings -> Set-preferred-level dialog, then BACK -> Settings,
    BACK -> home."""
    home = _home()
    settings = and_enter_settings(home)
    deep = settings is not None and and_reach_preferred_level(settings)  # depth 2
    # pop 1: dialog -> Settings (the 'Context awareness' row reappears; the dialog
    # title 'Set preferred level' is gone).
    pop1 = False
    if deep:
        and_back()
        mid = ui()
        pop1 = (find_node_ci(mid, "Context awareness") is not None
                and find_node_ci(mid, "Set preferred level") is None)
    # pop 2: Settings -> home.
    pop2 = False
    if pop1:
        and_back()
        pop2 = _home_restored() and "MainActivity" in get_current_activity()
    ok = deep and pop1 and pop2
    record("nav_backstack_preferred_level_unwinds", ok,
           "" if ok else "behavior_wrong: BACK did not unwind the preferred-level "
           "dialog -> Settings -> home one hop at a time",
           depth=2, jump_kind="reach",
           expect="BACK #1 restores Settings ('Context awareness'); BACK #2 "
                  "restores home MainActivity ('Off')")


@and_test(expects=("nav_back_links_to_home",))
def test_back_from_links_returns_home():
    home = _home()
    deep = and_reach_links_dialog(home)
    back_ok = deep and and_back() == 0 and _home_restored()
    record("nav_back_links_to_home", back_ok,
           "" if back_ok else "behavior_wrong: BACK from the Links dialog did not "
           "restore the home surface",
           depth=1, jump_kind="reach",
           expect="home 'Off' + 'Quick Actions' restored after one BACK from Links")


@and_test(expects=("nav_back_about_to_home",))
def test_back_from_about_returns_home():
    home = _home()
    deep = and_reach_about_dialog(home)
    back_ok = deep and and_back() == 0 and _home_restored()
    record("nav_back_about_to_home", back_ok,
           "" if back_ok else "behavior_wrong: BACK from the About dialog did not "
           "restore the home surface",
           depth=1, jump_kind="reach",
           expect="home 'Off' + 'Quick Actions' restored after one BACK from About")


TESTS = [
    # depth-1 reach
    test_reach_morse,
    test_reach_interval,
    test_reach_interval_vlm,
    test_reach_links,
    test_reach_about,
    test_reach_settings,
    # depth-2 chains
    test_reach_icon_credits_depth2,
    test_reach_preferred_level_depth2,
    test_reach_settings_links_depth2,
    # back-stack integrity
    test_back_from_morse_returns_home,
    test_back_from_settings_returns_home,
    test_up_arrow_from_settings_returns_home,
    test_backstack_icon_credits_unwinds,
    test_backstack_preferred_level_unwinds,
    test_back_from_links_returns_home,
    test_back_from_about_returns_home,
]

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