"""P3-2a tests: the mResumedActivity foreground parser (RESEARCH_SHIELD R4). Fixture-driven from the on-device captures; the launcher case comes from the R4-documented custom-launcher capture. Window-focus parsing is never used (R4 finding 2: mCurrentFocus=null trap). """ from pathlib import Path from nsh.ha_adb_common import parse_foreground FIXTURES = Path(__file__).parent / "fixtures" # R4: the on-device capture (grep -iE 'resumed') — the wrapped ActivityRecord # plus tolerated Resumed/ResumedActivity duplicates. _TIVIMATE_DUMP = (FIXTURES / "activity_activities_tivimate.txt").read_text() def test_tivimate_fixture_primary_pattern(): assert parse_foreground(_TIVIMATE_DUMP) == "ar.tvplayer.tv" def test_launcher_foreground_from_r4_capture(): # R4 finding 4: the user's custom launcher is a legitimate foreground app. dump = ( "mResumedActivity: ActivityRecord{abcd1234 u0 com.spocky.projengmenu" "/.ui.home.MainActivity\n" " state=RESUMED stopped=false delayedResume=false finishing=false\n" ) assert parse_foreground(dump) == "com.spocky.projengmenu" def test_multiline_wrap_joined_before_matching(): # The wrap splits right after /; the package must survive. dump = ( " mResumedActivity: ActivityRecord{4f0d030 u0 ar.tvplayer.tv/.ui.MainActivity\n" " t8152}\n" ) assert parse_foreground(dump) == "ar.tvplayer.tv" def test_topresumed_only_fallback(): # Newer firmware: no mResumedActivity, only topResumedActivity (checked after). dump = ( " topResumedActivity: ActivityRecord{aa11bb22 u0 com.other.app/.Main t1}\n" ) assert parse_foreground(dump) == "com.other.app" def test_topresumed_never_beats_primary(): dump = ( " mResumedActivity: ActivityRecord{x u0 primary.pkg/.Main t1}\n" " topResumedActivity: ActivityRecord{y u0 newer.pkg/.Main t2}\n" ) assert parse_foreground(dump) == "primary.pkg" def test_empty_and_garbage_return_none(): assert parse_foreground("") is None assert parse_foreground("some random log lines\nwithout any resumed marker") is None def test_resumed_duplicates_tolerated(): # Resumed: / ResumedActivity: / mResumedActivity: all describe the same app. dump = ( " Resumed activities in task display areas (from top to bottom):\n" " Resumed: ActivityRecord{4f0d030 u0 ar.tvplayer.tv/.ui.MainActivity t8152}\n" " ResumedActivity: ActivityRecord{4f0d030 u0 ar.tvplayer.tv/.ui.MainActivity t8152}\n" "mResumedActivity: ActivityRecord{4f0d030 u0 ar.tvplayer.tv/.ui.MainActivity t8152}\n" ) assert parse_foreground(dump) == "ar.tvplayer.tv" def test_null_package_never_returned(): dump = "mResumedActivity: ActivityRecord{4f0d030 u0 null/null t1}\n" assert parse_foreground(dump) is None