diff --git a/packages/daemon/src/execution/__tests__/android-system-dialog-occlusion.test.ts b/packages/daemon/src/execution/__tests__/android-system-dialog-occlusion.test.ts index d6c470ce32..81fb1fc9e4 100644 --- a/packages/daemon/src/execution/__tests__/android-system-dialog-occlusion.test.ts +++ b/packages/daemon/src/execution/__tests__/android-system-dialog-occlusion.test.ts @@ -61,6 +61,7 @@ test('recovery taps the reachable Close app, not a covered one that comes first' index: 0, type: 'Button', label: 'Close app', + identifier: 'android:id/aerr_close', hittable: true, rect: { x: 40, y: 300, width: 200, height: 80 }, }; @@ -75,12 +76,27 @@ test('recovery taps the reachable Close app, not a covered one that comes first' index: 3, type: 'Button', label: 'Close app', + identifier: 'android:id/aerr_close', hittable: true, rect: { x: 40, y: 1800, width: 200, height: 80 }, }; + const visibleWait = { + index: 4, + type: 'Button', + label: 'Wait', + identifier: 'android:id/aerr_wait', + hittable: true, + rect: { x: 40, y: 1900, width: 200, height: 80 }, + }; vi.mocked(snapshotAndroid) .mockResolvedValueOnce( - makeAndroidSnapshotCapture([staleCloseApp, sheet, dialog, visibleCloseApp]) as never, + makeAndroidSnapshotCapture([ + staleCloseApp, + sheet, + dialog, + visibleCloseApp, + visibleWait, + ]) as never, ) .mockResolvedValue(makeAndroidSnapshotCapture([]) as never); vi.mocked(runAndroidAdb).mockResolvedValue({ exitCode: 0, stdout: '', stderr: '' } as never); @@ -106,11 +122,25 @@ test('a fully covered dialog does not trigger recovery', async () => { index: 1, type: 'Button', label: 'Close app', + identifier: 'android:id/aerr_close', hittable: true, rect: { x: 40, y: 300, width: 200, height: 80 }, }; + const staleWait = { + index: 2, + type: 'Button', + label: 'Wait', + identifier: 'android:id/aerr_wait', + hittable: true, + rect: { x: 40, y: 400, width: 200, height: 80 }, + }; vi.mocked(snapshotAndroid).mockResolvedValue( - makeAndroidSnapshotCapture([staleDialog, staleCloseApp, foregroundSheet(2)]) as never, + makeAndroidSnapshotCapture([ + staleDialog, + staleCloseApp, + staleWait, + foregroundSheet(3), + ]) as never, ); vi.mocked(runAndroidAdb).mockResolvedValue({ exitCode: 0, stdout: '', stderr: '' } as never); @@ -135,6 +165,7 @@ test('collectively covered Close app is neither selected nor tapped', async () = parentIndex: 0, type: 'android.widget.Button', label: 'Close app', + identifier: 'android:id/aerr_close', hittable: true, rect: { x: 40, y: 300, width: 200, height: 80 }, }, @@ -190,3 +221,63 @@ test('collectively covered Close app is neither selected nor tapped', async () = expect(result.status).toBe('absent'); expect(tappedPoints()).toEqual([]); }); + +test('an ANR dialog in another language is found and closed by its button ids', async () => { + vi.mocked(runAndroidAdb).mockReset(); + vi.mocked(snapshotAndroid) + .mockResolvedValueOnce( + makeAndroidSnapshotCapture([ + { index: 0, type: 'TextView', label: 'Demo reagiert nicht', rect: SCREEN }, + { + index: 1, + type: 'Button', + label: 'App schließen', + identifier: 'android:id/aerr_close', + hittable: true, + rect: { x: 40, y: 1800, width: 200, height: 80 }, + }, + { + index: 2, + type: 'Button', + label: 'Warten', + identifier: 'android:id/aerr_wait', + hittable: true, + rect: { x: 40, y: 1900, width: 200, height: 80 }, + }, + ]) as never, + ) + .mockResolvedValue(makeAndroidSnapshotCapture([]) as never); + vi.mocked(runAndroidAdb).mockResolvedValue({ exitCode: 0, stdout: '', stderr: '' } as never); + + const result = await recoverAndroidBlockingSystemDialog({ + session: recordingSession('anr-localized'), + }); + + expect(result.status).toBe('recovered'); + expect(tappedPoints()).toEqual([{ x: 140, y: 1840 }]); +}); + +test('a crash dialog, which has Close app but no Wait, is not treated as an ANR', async () => { + vi.mocked(runAndroidAdb).mockReset(); + vi.mocked(snapshotAndroid).mockResolvedValue( + makeAndroidSnapshotCapture([ + { index: 0, type: 'TextView', label: 'Demo keeps stopping', rect: SCREEN }, + { + index: 1, + type: 'Button', + label: 'Close app', + identifier: 'android:id/aerr_close', + hittable: true, + rect: { x: 40, y: 1800, width: 200, height: 80 }, + }, + ]) as never, + ); + vi.mocked(runAndroidAdb).mockResolvedValue({ exitCode: 0, stdout: '', stderr: '' } as never); + + const result = await recoverAndroidBlockingSystemDialog({ + session: recordingSession('crash-dialog'), + }); + + expect(result.status).toBe('absent'); + expect(tappedPoints()).toEqual([]); +}); diff --git a/packages/daemon/src/execution/__tests__/android-system-dialog-readiness.test.ts b/packages/daemon/src/execution/__tests__/android-system-dialog-readiness.test.ts index 932b4f426f..1b11f3cfec 100644 --- a/packages/daemon/src/execution/__tests__/android-system-dialog-readiness.test.ts +++ b/packages/daemon/src/execution/__tests__/android-system-dialog-readiness.test.ts @@ -46,6 +46,7 @@ function anrDialogNodes(): RawSnapshotNode[] { index: 1, type: 'android.widget.Button', label: 'Close app', + identifier: 'android:id/aerr_close', rect: CLOSE_APP_RECT, }, ]; @@ -169,6 +170,30 @@ test('an ANR that surfaces between two commands is recovered by the next pre-dis expect(tapCalls(calls)).toEqual([CLOSE_APP_TAP]); }); +test("the session app's ANR is recovered on a device in another language", async () => { + // Recovery finds Close app by its framework id, so a translated label does not hide it. + const session = makeAndroidSession('readiness-anr-localized', { appBundleId: APP }); + const [title, closeApp] = anrDialogNodes(); + screenNodes = [ + { ...title!, label: `${APP} reagiert nicht` }, + { ...closeApp!, label: 'App schließen' }, + ]; + const { observation, calls } = makeObservation(anrFocus); + + const readiness = await ensureAndroidBlockingSystemDialogReady({ + session, + command: 'tap', + phase: 'before-command', + observation, + }); + + expect(readiness).toEqual({ + status: 'recovered', + warning: `Recovered Android app ANR before tap: closed and relaunched ${APP}.`, + }); + expect(tapCalls(calls)).toEqual([CLOSE_APP_TAP]); +}); + test('an ANR whose Close app is covered is not tapped, and recovery reports failure', async () => { // #1832: recovery reads the presentation's occlusion result, so a "Close app" that a foreground // surface covers is unreachable. Tapping it anyway would land the tap on whatever is on top. @@ -209,7 +234,12 @@ test('a system dialog that surfaces between two commands blocks the next command phase: 'before-command', observation, }), - ).rejects.toMatchObject({ code: 'COMMAND_FAILED' }); + ).rejects.toMatchObject({ + code: 'COMMAND_FAILED', + details: { + hint: 'Run alert dismiss to close the app that is not responding, or alert accept to wait for it, then retry.', + }, + }); }); test('an ANR the command itself provoked still fails that command', async () => { diff --git a/packages/daemon/src/execution/__tests__/android-system-dialog-ref-frame.test.ts b/packages/daemon/src/execution/__tests__/android-system-dialog-ref-frame.test.ts index 67dab9618d..add3c80aff 100644 --- a/packages/daemon/src/execution/__tests__/android-system-dialog-ref-frame.test.ts +++ b/packages/daemon/src/execution/__tests__/android-system-dialog-ref-frame.test.ts @@ -30,13 +30,22 @@ test('android blocking-dialog recovery expires the ref frame before its recovery index: 1, type: 'Button', label: 'Close app', + identifier: 'android:id/aerr_close', hittable: true, rect: { x: 20, y: 200, width: 120, height: 44 }, }; + const wait = { + index: 2, + type: 'Button', + label: 'Wait', + identifier: 'android:id/aerr_wait', + hittable: true, + rect: { x: 20, y: 260, width: 120, height: 44 }, + }; // First read: the blocking dialog is present (drives recovery). Post-tap poll: // the dialog is gone, so recovery completes without sleeping through retries. vi.mocked(snapshotAndroid) - .mockResolvedValueOnce(makeAndroidSnapshotCapture([dialog, closeApp]) as never) + .mockResolvedValueOnce(makeAndroidSnapshotCapture([dialog, closeApp, wait]) as never) .mockResolvedValue(makeAndroidSnapshotCapture([]) as never); vi.mocked(runAndroidAdb).mockResolvedValue({ exitCode: 0, stdout: '', stderr: '' } as never); diff --git a/packages/daemon/src/execution/android-system-dialog.ts b/packages/daemon/src/execution/android-system-dialog.ts index d8e335e4ec..303d66daa6 100644 --- a/packages/daemon/src/execution/android-system-dialog.ts +++ b/packages/daemon/src/execution/android-system-dialog.ts @@ -12,12 +12,19 @@ import { expireRefFrame } from '../core/ref-frame.ts'; import type { SessionState } from '../core/session-state.ts'; import { isActiveProviderDevice } from '../core/provider-device-admission.ts'; -const ANDROID_BLOCKING_MODAL_PATTERN = /\bis(?:n(?:'|'|')?t| not)\s+responding\b/i; -const ANDROID_CLOSE_APP_PATTERN = /^close app$/i; +/** + * The framework ids of the ANR dialog's Close app and Wait buttons. They do not depend on the + * device language, and only the ANR dialog has Wait: the crash dialog also has Close app. + */ +const ANDROID_ANR_CLOSE_BUTTON_ID = 'android:id/aerr_close'; +const ANDROID_ANR_WAIT_BUTTON_ID = 'android:id/aerr_wait'; const ANDROID_MODAL_POLL_MS = 500; const ANDROID_MODAL_POLL_ATTEMPTS = 12; const ANDROID_BLOCKING_DIALOG_HINT = 'Wait for Android to recover, close the dialog, restart the app, or reboot the emulator, then retry.'; +/** `alert` is not stopped by this guard, and it presses the ANR dialog's buttons by their ids. */ +const ANDROID_FOREIGN_ANR_HINT = + 'Run alert dismiss to close the app that is not responding, or alert accept to wait for it, then retry.'; const ANDROID_BLOCKING_DIALOG_INSPECTION_WARNING = 'Android blocking-dialog readiness could not be inspected; the command continued.'; const ANDROID_BLOCKING_DIALOG_WARNING_TEXT_LIMIT = 240; @@ -232,7 +239,7 @@ export async function ensureAndroidBlockingSystemDialogReady(params: { command, focus, message: `Android system dialog is blocking ${command}: ${formatAndroidBlockingDialogFocus(focus)}.`, - hint: ANDROID_BLOCKING_DIALOG_HINT, + hint: ANDROID_FOREIGN_ANR_HINT, }); } @@ -379,11 +386,16 @@ function findCloseAppButton( if (options.requireDialogSignal !== false && !containsBlockingDialog(nodes)) { return undefined; } - return nodes.filter(isTouchableDialogNode).find((node) => { - return ( - readNodeTextParts(node).some((text) => ANDROID_CLOSE_APP_PATTERN.test(text)) && node.rect - ); - }); + return findTouchableDialogButton(nodes, ANDROID_ANR_CLOSE_BUTTON_ID); +} + +function findTouchableDialogButton( + nodes: SnapshotNode[], + identifier: string, +): SnapshotNode | undefined { + return nodes.find( + (node) => node.identifier === identifier && node.rect && isTouchableDialogNode(node), + ); } /** A node the recovery tap can actually reach: present in the tree and not covered by a surface above it. */ @@ -439,31 +451,6 @@ function isProviderOwnedSession(session: SessionState): boolean { return Boolean(session.lease?.leaseProvider) || isActiveProviderDevice(session.device); } -function readNodeText(node: { - label?: string; - value?: string | number | boolean | null; - identifier?: string; -}): string { - return readNodeTextParts(node).join(' ').trim(); -} - -function readNodeTextParts(node: { - label?: string; - value?: string | number | boolean | null; - identifier?: string; -}): string[] { - const parts = [node.label, node.identifier]; - if (typeof node.value === 'string' && node.value.trim().length > 0) { - parts.push(node.value); - } - return parts - .filter((part): part is string => typeof part === 'string' && part.trim().length > 0) - .map((part) => part.trim()); -} - function containsBlockingDialog(nodes: SnapshotNode[]): boolean { - return nodes.filter(isTouchableDialogNode).some((node) => { - const text = readNodeText(node); - return text.length > 0 && ANDROID_BLOCKING_MODAL_PATTERN.test(text); - }); + return findTouchableDialogButton(nodes, ANDROID_ANR_WAIT_BUTTON_ID) !== undefined; } diff --git a/packages/daemon/src/execution/interaction/internal/interaction-dispatch-disclosure.test.ts b/packages/daemon/src/execution/interaction/internal/interaction-dispatch-disclosure.test.ts index 5e6df54486..750aed0c69 100644 --- a/packages/daemon/src/execution/interaction/internal/interaction-dispatch-disclosure.test.ts +++ b/packages/daemon/src/execution/interaction/internal/interaction-dispatch-disclosure.test.ts @@ -278,6 +278,7 @@ async function refRefusedAfterAndroidRecovery(command: 'press' | 'gesture'): Pro ref: 'e0', type: 'Button', label: 'Close app', + identifier: 'android:id/aerr_close', rect: { x: 10, y: 20, width: 100, height: 40 }, }, ], diff --git a/packages/daemon/src/execution/interaction/internal/interaction-gesture-tap.test.ts b/packages/daemon/src/execution/interaction/internal/interaction-gesture-tap.test.ts index 8c7037745d..3f3a44ded2 100644 --- a/packages/daemon/src/execution/interaction/internal/interaction-gesture-tap.test.ts +++ b/packages/daemon/src/execution/interaction/internal/interaction-gesture-tap.test.ts @@ -284,6 +284,7 @@ for (const canceled of [false, true]) { ref: 'e0', type: 'Button', label: 'Close app', + identifier: 'android:id/aerr_close', rect: { x: 10, y: 20, width: 100, height: 40 }, }, ], diff --git a/packages/daemon/src/server/__tests__/request-router-android-modal.test.ts b/packages/daemon/src/server/__tests__/request-router-android-modal.test.ts index 77e3fb2200..b88a81544d 100644 --- a/packages/daemon/src/server/__tests__/request-router-android-modal.test.ts +++ b/packages/daemon/src/server/__tests__/request-router-android-modal.test.ts @@ -71,8 +71,16 @@ vi.mock('@agent-device/platform-android/mechanics', async (importOriginal) => { index: 1, type: 'android.widget.Button', label: 'Close app', + identifier: 'android:id/aerr_close', rect: { x: 100, y: 600, width: 220, height: 80 }, }, + { + index: 2, + type: 'android.widget.Button', + label: 'Wait', + identifier: 'android:id/aerr_wait', + rect: { x: 100, y: 700, width: 220, height: 80 }, + }, ]); } return capture([]); diff --git a/packages/platform-android/src/__tests__/alert-detection.test.ts b/packages/platform-android/src/__tests__/alert-detection.test.ts index aa29edf488..18ce68cee0 100644 --- a/packages/platform-android/src/__tests__/alert-detection.test.ts +++ b/packages/platform-android/src/__tests__/alert-detection.test.ts @@ -595,3 +595,23 @@ test('shared Apple label additions also name ordinary Android dialog actions', ( ); } }); + +test('an ANR dialog in another language is found, and its buttons are chosen, by their ids', () => { + const system = { bundleId: 'android' }; + const candidate = findAndroidAlertCandidate([ + node(0, 'android.widget.FrameLayout', system), + node(1, 'android.widget.TextView', { + ...system, + label: 'System UI reagiert nicht', + identifier: 'android:id/alertTitle', + }), + { ...button(2, 'App schließen', 'android:id/aerr_close', { x: 70, y: 1210 }), ...system }, + { ...button(3, 'Warten', 'android:id/aerr_wait', { x: 70, y: 1336 }), ...system }, + ]); + + assert.equal(candidate?.alert.source, 'system-dialog'); + assert.equal(candidate?.alert.title, 'System UI reagiert nicht'); + const buttons = candidate?.buttons ?? []; + assert.equal(chooseAndroidAlertButton(buttons, 'dismiss')?.label, 'App schließen'); + assert.equal(chooseAndroidAlertButton(buttons, 'accept')?.label, 'Warten'); +}); diff --git a/packages/platform-android/src/__tests__/snapshot-content-recovery.test.ts b/packages/platform-android/src/__tests__/snapshot-content-recovery.test.ts index 9a52b3d5f6..5a00b0ddbd 100644 --- a/packages/platform-android/src/__tests__/snapshot-content-recovery.test.ts +++ b/packages/platform-android/src/__tests__/snapshot-content-recovery.test.ts @@ -236,6 +236,43 @@ test('keeps a recognized system dialog without window metadata', () => { expect(decision).toBeUndefined(); }); +test('keeps an ANR dialog whose buttons carry the framework aerr ids', () => { + // The ANR dialog (`AppNotRespondingDialog`) names its buttons aerr_close and aerr_wait, not + // button1..3, in every device language. + const decision = unusableDecisionFor( + helperXml([ + node({ + text: 'System UI reagiert nicht', + resourceId: 'android:id/alertTitle', + packageName: 'com.android.systemui', + className: 'android.widget.TextView', + }), + node({ + text: 'App schließen', + resourceId: 'android:id/aerr_close', + packageName: 'com.android.systemui', + className: 'android.widget.Button', + }), + node({ + text: 'Warten', + resourceId: 'android:id/aerr_wait', + packageName: 'com.android.systemui', + className: 'android.widget.Button', + }), + ]), + { + backend: 'android-helper', + nodeCount: 3, + rootPresent: true, + windowCount: 1, + captureMode: 'interactive-windows', + }, + { foregroundAppPackage: 'com.pagerviewexample' }, + ); + + expect(decision).toBeUndefined(); +}); + test('rejects system chrome inherited under an empty application window', () => { const decision = unusableDecisionFor( helperXml([ diff --git a/packages/platform-android/src/alert-detection.ts b/packages/platform-android/src/alert-detection.ts index 60af1eebce..1bd70cfa53 100644 --- a/packages/platform-android/src/alert-detection.ts +++ b/packages/platform-android/src/alert-detection.ts @@ -52,10 +52,19 @@ export function isAndroidPermissionPackage(packageName: string): boolean { const ANDROID_SYSTEM_DIALOG_PACKAGES = new Set(['android', 'com.android.systemui']); const ANDROID_ALERT_ID_PATTERN = /^android:id\/(?:alertTitle|message|button[123]|parentPanel|buttonPanel|contentPanel)$/i; -const ANDROID_ALERT_BUTTON_ID_PATTERN = /^android:id\/button[123]$/i; +/** AlertDialog's buttons, and the ANR dialog's (`AppNotRespondingDialog`) Close app and Wait. */ +const ANDROID_ALERT_BUTTON_ID_PATTERN = /^android:id\/(?:button[123]|aerr_(?:close|wait))$/i; const ANDROID_PERMISSION_ID_PATTERN = /(?:^|:)id\/permission_/i; const ANDROID_BLOCKING_DIALOG_PATTERN = /\b(?:is(?:n't| not) responding|keeps stopping|has stopped|close app|app info)\b/i; +/** + * The ANR dialog's buttons by framework id, which does not depend on the device language: dismiss + * presses Close app and accept presses Wait. + */ +const ANDROID_ANR_BUTTON_ROLES: ReadonlyMap = new Map([ + ['android:id/aerr_close', 'dismiss'], + ['android:id/aerr_wait', 'accept'], +]); /** The AlertController panel that holds a dialog's title, message, and buttons, under the platform or an AppCompat package. */ export function isAndroidAlertPanelIdentifier(identifier: string | null): boolean { @@ -295,6 +304,8 @@ function classifyAndroidAlertButtonById( if (/(?:^|:)id\/button1$/i.test(identifier)) return 'accept'; if (/(?:^|:)id\/button2$/i.test(identifier)) return 'dismiss'; if (/(?:^|:)id\/button3$/i.test(identifier)) return 'neutral'; + const anrRole = ANDROID_ANR_BUTTON_ROLES.get(identifier); + if (anrRole) return anrRole; if (ANDROID_GRANT_ID_SHAPE.test(identifier)) return UNDECLARED_GRANT; return null; } @@ -376,7 +387,8 @@ function isAndroidSystemDialogNode(node: RawSnapshotNode): boolean { const packageName = node.bundleId ?? ''; return ( ANDROID_SYSTEM_DIALOG_PACKAGES.has(packageName) && - ANDROID_BLOCKING_DIALOG_PATTERN.test(readNodeText(node)) + (ANDROID_BLOCKING_DIALOG_PATTERN.test(readNodeText(node)) || + ANDROID_ANR_BUTTON_ROLES.has(node.identifier ?? '')) ); } diff --git a/test/integration/provider-scenarios/android-alert.test.ts b/test/integration/provider-scenarios/android-alert.test.ts index cde8b4e2f0..5c8f42ff72 100644 --- a/test/integration/provider-scenarios/android-alert.test.ts +++ b/test/integration/provider-scenarios/android-alert.test.ts @@ -185,3 +185,17 @@ test('Provider-backed integration Android alert ignores app-owned sheets', async }, ); }); + +test('Provider-backed integration Android alert accept waits on an ANR dialog', async () => { + await withProviderScenarioResource( + async () => await createAndroidSettingsWorld(dismissibleDialog(androidSystemDialogXml)), + async (world) => { + const client = world.daemon.client(); + await client.apps.open({ app: 'com.example.demo', ...world.selection }); + + const alertAccept = await client.command.alert({ action: 'accept', ...world.selection }); + assert.equal(alertAccept.button, 'Wait'); + assertCommandCall(world.adbCalls, ['shell', 'input', 'tap', '274', '638']); + }, + ); +}); diff --git a/test/integration/provider-scenarios/android-dialog-fixtures.ts b/test/integration/provider-scenarios/android-dialog-fixtures.ts index 42dd71f77c..f7886ef6e1 100644 --- a/test/integration/provider-scenarios/android-dialog-fixtures.ts +++ b/test/integration/provider-scenarios/android-dialog-fixtures.ts @@ -48,8 +48,8 @@ export function androidSystemDialogXml(): string { rootNode(packageName), textNode(1, 'Demo isn't responding', 'android:id/alertTitle', packageName), textNode(2, 'Do you want to close it?', 'android:id/message', packageName), - buttonNode(3, 'Close app', 'android:id/button2', '[52,612][180,664]', packageName), - buttonNode(4, 'Wait', 'android:id/button1', '[210,612][338,664]', packageName), + buttonNode(3, 'Close app', 'android:id/aerr_close', '[52,612][180,664]', packageName), + buttonNode(4, 'Wait', 'android:id/aerr_wait', '[210,612][338,664]', packageName), ' ', ]); } diff --git a/website/docs/docs/commands.md b/website/docs/docs/commands.md index 75313e2803..9794ad5e3e 100644 --- a/website/docs/docs/commands.md +++ b/website/docs/docs/commands.md @@ -955,6 +955,7 @@ agent-device alert dismiss Handle system alerts: - `alert` handles system alerts on iOS, macOS, and Android permission dialogs. On iOS it also covers app-owned modal popups and Safari's page prompts. +- On Android, `alert` also handles the "isn't responding" (ANR) dialog in any device language: `dismiss` presses Close app, and `accept` presses Wait. While another app's ANR dialog has focus, commands that act on the screen are refused and their hint names `alert`. - Use `alert get` for a quick check, and `alert wait ` only when a prompt may appear after async work. Do not use `settings permission` to answer a dialog already on screen; it is for setup before a flow. - `accept` and `dismiss` are sent once and never replayed, even if the response is lost. A closed alert does not prove that a permission was granted, so verify the app outcome. - `accept` picks the lasting grant, such as `Allow While Using App` over `Allow Once`, or "Allow all" on Android's partial-media sheet. On iOS, `dismiss` presses only a recognized decline button, such as `Cancel` or `Don't Allow`. iOS simulators also recognize these labels in the device's preferred languages; on physical iOS devices, tvOS, and visionOS the runner cannot read the OS strings, so it recognizes the system prompts' Simplified Chinese wording from a built-in table, at the rank of the English label each entry translates; wording the table does not cover stays unknown.