|
| 1 | +import { test, expect, vi } from 'vitest'; |
| 2 | + |
| 3 | +// #1832: blocking-dialog recovery reads the daemon presentation, so it must also consume that |
| 4 | +// presentation's OCCLUSION result. A stale "App isn't responding" surface left under the foreground |
| 5 | +// one must not trigger recovery, and a covered "Close app" must not be tapped ahead of the visible |
| 6 | +// one — the exact disagreement routing through buildSnapshotState exists to remove. |
| 7 | +vi.mock('../../platforms/android/snapshot.ts', () => ({ snapshotAndroid: vi.fn() })); |
| 8 | +vi.mock('../../platforms/android/adb.ts', () => ({ runAndroidAdb: vi.fn() })); |
| 9 | + |
| 10 | +import { snapshotAndroid } from '../../platforms/android/snapshot.ts'; |
| 11 | +import { runAndroidAdb } from '../../platforms/android/adb.ts'; |
| 12 | +import { recoverAndroidBlockingSystemDialog } from '../android-system-dialog.ts'; |
| 13 | +import { makeAndroidSession } from '../../__tests__/test-utils/session-factories.ts'; |
| 14 | +import { makeTestScreenRecordingResource } from '../../__tests__/test-utils/screen-recording-live-handle.ts'; |
| 15 | + |
| 16 | +const SCREEN = { x: 0, y: 0, width: 1080, height: 2400 }; |
| 17 | + |
| 18 | +/** A foreground surface drawn over everything below it — what makes the stale dialog unreachable. */ |
| 19 | +const foregroundSheet = (index: number) => ({ |
| 20 | + index, |
| 21 | + type: 'com.google.android.material.bottomsheet.BottomSheetDialog', |
| 22 | + identifier: 'com.example:id/bottom_sheet', |
| 23 | + hittable: true, |
| 24 | + rect: SCREEN, |
| 25 | +}); |
| 26 | + |
| 27 | +function recordingSession(name: string) { |
| 28 | + const session = makeAndroidSession(name); |
| 29 | + session.screenRecording = makeTestScreenRecordingResource(session, { |
| 30 | + backend: 'adb screenrecord', |
| 31 | + outPath: '/tmp/anr.mp4', |
| 32 | + startedAt: 0, |
| 33 | + }); |
| 34 | + return session; |
| 35 | +} |
| 36 | + |
| 37 | +function tappedPoints(): Array<{ x: number; y: number }> { |
| 38 | + return vi |
| 39 | + .mocked(runAndroidAdb) |
| 40 | + .mock.calls.filter((call) => (call[1] as string[]).includes('tap')) |
| 41 | + .map((call) => { |
| 42 | + const args = call[1] as string[]; |
| 43 | + const at = args.indexOf('tap'); |
| 44 | + return { x: Number(args[at + 1]), y: Number(args[at + 2]) }; |
| 45 | + }); |
| 46 | +} |
| 47 | + |
| 48 | +test('recovery taps the reachable Close app, not a covered one that comes first', async () => { |
| 49 | + vi.mocked(runAndroidAdb).mockReset(); |
| 50 | + // The live ANR dialog is on top; a stale "Close app" from an earlier one is still in the tree |
| 51 | + // underneath the sheet. Document order puts the stale button FIRST, which is what the previous |
| 52 | + // hand-rolled presentation selected: the first text match carrying a rect. |
| 53 | + const staleCloseApp = { |
| 54 | + index: 0, |
| 55 | + type: 'Button', |
| 56 | + label: 'Close app', |
| 57 | + hittable: true, |
| 58 | + rect: { x: 40, y: 300, width: 200, height: 80 }, |
| 59 | + }; |
| 60 | + const sheet = { ...foregroundSheet(1), rect: { x: 0, y: 0, width: 1080, height: 1000 } }; |
| 61 | + const dialog = { |
| 62 | + index: 2, |
| 63 | + type: 'TextView', |
| 64 | + label: "App isn't responding", |
| 65 | + rect: { x: 0, y: 1700, width: 1080, height: 60 }, |
| 66 | + }; |
| 67 | + const visibleCloseApp = { |
| 68 | + index: 3, |
| 69 | + type: 'Button', |
| 70 | + label: 'Close app', |
| 71 | + hittable: true, |
| 72 | + rect: { x: 40, y: 1800, width: 200, height: 80 }, |
| 73 | + }; |
| 74 | + vi.mocked(snapshotAndroid) |
| 75 | + .mockResolvedValueOnce({ nodes: [staleCloseApp, sheet, dialog, visibleCloseApp] } as never) |
| 76 | + .mockResolvedValue({ nodes: [] } as never); |
| 77 | + vi.mocked(runAndroidAdb).mockResolvedValue({ exitCode: 0, stdout: '', stderr: '' } as never); |
| 78 | + |
| 79 | + const result = await recoverAndroidBlockingSystemDialog({ |
| 80 | + session: recordingSession('anr-covered-button'), |
| 81 | + }); |
| 82 | + |
| 83 | + expect(result.status).toBe('recovered'); |
| 84 | + expect(tappedPoints()).toEqual([{ x: 140, y: 1840 }]); |
| 85 | +}); |
| 86 | + |
| 87 | +test('a fully covered dialog does not trigger recovery', async () => { |
| 88 | + vi.mocked(runAndroidAdb).mockReset(); |
| 89 | + const staleDialog = { |
| 90 | + index: 0, |
| 91 | + type: 'TextView', |
| 92 | + label: "App isn't responding", |
| 93 | + hittable: true, |
| 94 | + rect: { x: 0, y: 100, width: 1080, height: 60 }, |
| 95 | + }; |
| 96 | + const staleCloseApp = { |
| 97 | + index: 1, |
| 98 | + type: 'Button', |
| 99 | + label: 'Close app', |
| 100 | + hittable: true, |
| 101 | + rect: { x: 40, y: 300, width: 200, height: 80 }, |
| 102 | + }; |
| 103 | + vi.mocked(snapshotAndroid).mockResolvedValue({ |
| 104 | + nodes: [staleDialog, staleCloseApp, foregroundSheet(2)], |
| 105 | + } as never); |
| 106 | + vi.mocked(runAndroidAdb).mockResolvedValue({ exitCode: 0, stdout: '', stderr: '' } as never); |
| 107 | + |
| 108 | + const result = await recoverAndroidBlockingSystemDialog({ |
| 109 | + session: recordingSession('anr-covered-dialog'), |
| 110 | + }); |
| 111 | + |
| 112 | + expect(result.status).toBe('absent'); |
| 113 | + expect(tappedPoints()).toEqual([]); |
| 114 | +}); |
0 commit comments