From 9280f05a65d39cb2485bbe9d0e1b8ca45d8de40b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Pierzcha=C5=82a?= Date: Mon, 16 Nov 2026 18:50:45 +0100 Subject: [PATCH 01/16] fix(android): dispatch helper taps and keys without the global animation wait The helper's input-tap and key actions injected through the public UiAutomation.injectInputEvent(event, sync) overload, which syncs window animations before DOWN and after UP for up to five seconds each. While a window animation or transition stays pending, every plain press paid up to ten seconds even though dispatch itself was immediate (#4590). Touch plans already avoided this since #1356. Taps and keys now inject through UiAutomationInputDispatcher without the animation wait. A DOWN the framework refuses reached no window, so it is cancelled and injected once more with a fresh down time after the framework's animation sync. Refs #4590 Co-Authored-By: Claude Opus 5.5 --- .../AutomationCommandHandler.java | 44 +++++----- .../UiAutomationInputDispatcher.java | 82 ++++++++++++++++++- .../android-helper-input-dispatch.test.ts | 33 ++++++++ 3 files changed, 136 insertions(+), 23 deletions(-) create mode 100644 test/repo-guards/android-helper-input-dispatch.test.ts diff --git a/android/snapshot-helper/src/main/java/com/callstack/agentdevice/snapshothelper/AutomationCommandHandler.java b/android/snapshot-helper/src/main/java/com/callstack/agentdevice/snapshothelper/AutomationCommandHandler.java index 4b1868f965..8db0de219d 100644 --- a/android/snapshot-helper/src/main/java/com/callstack/agentdevice/snapshothelper/AutomationCommandHandler.java +++ b/android/snapshot-helper/src/main/java/com/callstack/agentdevice/snapshothelper/AutomationCommandHandler.java @@ -78,37 +78,43 @@ private static void tap(UiAutomation automation, double x, double y, InputDispat || x < 0 || y < 0) { throw new IllegalArgumentException("Invalid tap coordinates"); } - long down = SystemClock.uptimeMillis(); - touch(automation, down, MotionEvent.ACTION_DOWN, (float) x, (float) y, dispatch); - touch(automation, down, MotionEvent.ACTION_UP, (float) x, (float) y, dispatch); - } - - private static void touch(UiAutomation automation, long down, int action, float x, float y, InputDispatch dispatch) { - MotionEvent event = MotionEvent.obtain(down, SystemClock.uptimeMillis(), action, x, y, 0); - event.setSource(InputDevice.SOURCE_TOUCHSCREEN); + float pointX = (float) x; + float pointY = (float) y; + dispatch.attempted = true; + long down = UiAutomationInputDispatcher.injectStart( + automation, downTime -> touchEvent(downTime, MotionEvent.ACTION_DOWN, pointX, pointY)); + if (down < 0) throw new IllegalStateException("Touch injection was refused"); + MotionEvent up = touchEvent(down, MotionEvent.ACTION_UP, pointX, pointY); try { - dispatch.attempted = true; - if (!automation.injectInputEvent(event, true)) { + if (!UiAutomationInputDispatcher.inject(automation, up, true)) { throw new IllegalStateException("Touch injection was refused"); } } finally { - event.recycle(); + up.recycle(); } } + private static MotionEvent touchEvent(long down, int action, float x, float y) { + MotionEvent event = MotionEvent.obtain(down, SystemClock.uptimeMillis(), action, x, y, 0); + event.setSource(InputDevice.SOURCE_TOUCHSCREEN); + return event; + } + private static void key(UiAutomation automation, int code, InputDispatch dispatch) { if (code < 0 || code > KeyEvent.getMaxKeyCode()) throw new IllegalArgumentException("Invalid key code"); - long down = SystemClock.uptimeMillis(); - for (int action : new int[] {KeyEvent.ACTION_DOWN, KeyEvent.ACTION_UP}) { - KeyEvent event = new KeyEvent(down, SystemClock.uptimeMillis(), action, code, 0, 0, - KeyCharacterMap.VIRTUAL_KEYBOARD, 0, 0, InputDevice.SOURCE_KEYBOARD); - dispatch.attempted = true; - if (!automation.injectInputEvent(event, true)) { - throw new IllegalStateException("Key injection was refused"); - } + dispatch.attempted = true; + long down = UiAutomationInputDispatcher.injectStart( + automation, downTime -> keyEvent(downTime, KeyEvent.ACTION_DOWN, code)); + if (down < 0 || !UiAutomationInputDispatcher.inject(automation, keyEvent(down, KeyEvent.ACTION_UP, code), true)) { + throw new IllegalStateException("Key injection was refused"); } } + private static KeyEvent keyEvent(long down, int action, int code) { + return new KeyEvent(down, SystemClock.uptimeMillis(), action, code, 0, 0, + KeyCharacterMap.VIRTUAL_KEYBOARD, 0, 0, InputDevice.SOURCE_KEYBOARD); + } + private static void fill(UiAutomation automation, int x, int y, String text, InputDispatch dispatch) { AccessibilityNodeInfo target = null; try { diff --git a/android/snapshot-helper/src/main/java/com/callstack/agentdevice/snapshothelper/UiAutomationInputDispatcher.java b/android/snapshot-helper/src/main/java/com/callstack/agentdevice/snapshothelper/UiAutomationInputDispatcher.java index 791edf083d..4b8c3f83fe 100644 --- a/android/snapshot-helper/src/main/java/com/callstack/agentdevice/snapshothelper/UiAutomationInputDispatcher.java +++ b/android/snapshot-helper/src/main/java/com/callstack/agentdevice/snapshothelper/UiAutomationInputDispatcher.java @@ -1,19 +1,28 @@ package com.callstack.agentdevice.snapshothelper; import android.app.UiAutomation; +import android.os.SystemClock; import android.view.InputEvent; +import android.view.MotionEvent; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; /** - * Dispatches planned input without the framework's global animation synchronization. + * Dispatches input without the framework's global animation synchronization. * - *

The public two-argument overload waits for global animations before every event. Android's - * three-argument test API separates that policy from synchronous input dispatch, but the SDK stub - * hides the method, so the helper resolves it from the runtime framework. + *

The public two-argument overload waits for global animations before a gesture's first event + * and after its last one, up to five seconds each. Android's three-argument test API separates that + * policy from synchronous input dispatch, but the SDK stub hides the method, so the helper resolves + * it from the runtime framework. */ final class UiAutomationInputDispatcher { private static volatile Method injectWithoutAnimationWait = findInjectWithoutAnimationWait(); + private static volatile Method syncInputTransactions = findSyncInputTransactions(); + + /** Builds the first event of a tap or key press for a down time. */ + interface StartEvent { + InputEvent obtain(long downTime); + } private UiAutomationInputDispatcher() {} @@ -32,6 +41,63 @@ static boolean inject(UiAutomation automation, InputEvent event, boolean waitFor } } + /** + * Synchronously injects the first event of a tap or key press and returns its down time, or -1 + * when the framework refused it. A refused start reached no window: a transition can leave nothing + * able to receive it. The start is then injected once more after the framework's animation sync, + * with a fresh down time so the receiver does not measure the wait as a held press. + */ + static long injectStart(UiAutomation automation, StartEvent start) { + long downTime = SystemClock.uptimeMillis(); + if (injectStartRecycled(automation, start.obtain(downTime))) return downTime; + if (!awaitAnimations(automation)) return -1; + downTime = SystemClock.uptimeMillis(); + return injectStartRecycled(automation, start.obtain(downTime)) ? downTime : -1; + } + + private static boolean injectStartRecycled(UiAutomation automation, InputEvent event) { + try { + if (inject(automation, event, true)) return true; + // The dispatcher's input verifier records a dropped DOWN as a pointer that is still down. + // Cancelling it keeps the next DOWN consistent; no window holds the touch to receive it. + if (event instanceof MotionEvent) cancel(automation, (MotionEvent) event); + return false; + } finally { + if (event instanceof MotionEvent) ((MotionEvent) event).recycle(); + } + } + + private static void cancel(UiAutomation automation, MotionEvent refused) { + MotionEvent.PointerProperties[] properties = {new MotionEvent.PointerProperties()}; + MotionEvent.PointerCoords[] coords = {new MotionEvent.PointerCoords()}; + refused.getPointerProperties(0, properties[0]); + refused.getPointerCoords(0, coords[0]); + MotionEvent cancel = MotionEvent.obtain(refused.getDownTime(), refused.getEventTime(), + MotionEvent.ACTION_CANCEL, 1, properties, coords, 0, 0, 1f, 1f, 0, 0, + refused.getSource(), MotionEvent.FLAG_CANCELED); + try { + inject(automation, cancel, false); + } finally { + cancel.recycle(); + } + } + + /** Returns false when injection already waited for animations through the public overload. */ + private static boolean awaitAnimations(UiAutomation automation) { + Method method = syncInputTransactions; + if (injectWithoutAnimationWait == null || method == null) return false; + try { + method.invoke(automation); + return true; + } catch (IllegalAccessException | IllegalArgumentException error) { + syncInputTransactions = null; + return false; + } catch (InvocationTargetException error) { + rethrowCause(error.getCause()); + throw new AssertionError("Unreachable"); + } + } + private static Method findInjectWithoutAnimationWait() { try { return UiAutomation.class.getMethod( @@ -41,6 +107,14 @@ private static Method findInjectWithoutAnimationWait() { } } + private static Method findSyncInputTransactions() { + try { + return UiAutomation.class.getMethod("syncInputTransactions"); + } catch (NoSuchMethodException | SecurityException error) { + return null; + } + } + private static void rethrowCause(Throwable cause) { if (cause instanceof RuntimeException) throw (RuntimeException) cause; if (cause instanceof Error) throw (Error) cause; diff --git a/test/repo-guards/android-helper-input-dispatch.test.ts b/test/repo-guards/android-helper-input-dispatch.test.ts new file mode 100644 index 0000000000..071ce0933a --- /dev/null +++ b/test/repo-guards/android-helper-input-dispatch.test.ts @@ -0,0 +1,33 @@ +import assert from 'node:assert/strict'; +import fs from 'node:fs'; +import path from 'node:path'; +import { fileURLToPath } from 'node:url'; +import { test } from 'vitest'; + +const repoRoot = path.resolve(path.dirname(fileURLToPath(import.meta.url)), '../..'); +const helperSourceDir = path.join( + repoRoot, + 'android/snapshot-helper/src/main/java/com/callstack/agentdevice/snapshothelper', +); +const dispatcherFile = 'UiAutomationInputDispatcher.java'; + +// The public two-argument UiAutomation overload waits up to five seconds for global window +// animations before a gesture's first event and after its last one (#4590, #1356). +test('Android helper input reaches the framework only through the dispatcher without the animation wait', () => { + const sources = fs.readdirSync(helperSourceDir).filter((name) => name.endsWith('.java')); + assert.ok(sources.includes(dispatcherFile), 'the input dispatcher must exist'); + const directInjectors = sources.filter( + (name) => + name !== dispatcherFile && + /\.injectInputEvent\(/.test( + fs + .readFileSync(path.join(helperSourceDir, name), 'utf8') + .replaceAll(/\/\*[\s\S]*?\*\/|\/\/[^\n]*/g, ''), + ), + ); + assert.deepEqual(directInjectors, []); + assert.match( + fs.readFileSync(path.join(helperSourceDir, dispatcherFile), 'utf8'), + /method\.invoke\(automation, event, waitForDispatch, false\)/, + ); +}); From 08b7884fd6f6d9fa997b12e0b4ad38a127bd61a8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Pierzcha=C5=82a?= Date: Mon, 16 Nov 2026 19:08:37 +0100 Subject: [PATCH 02/16] test(android): scan the whole helper source tree for direct input injection Co-Authored-By: Claude Opus 5.5 --- .../android-helper-input-dispatch.test.ts | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/test/repo-guards/android-helper-input-dispatch.test.ts b/test/repo-guards/android-helper-input-dispatch.test.ts index 071ce0933a..7327cacbc2 100644 --- a/test/repo-guards/android-helper-input-dispatch.test.ts +++ b/test/repo-guards/android-helper-input-dispatch.test.ts @@ -5,16 +5,18 @@ import { fileURLToPath } from 'node:url'; import { test } from 'vitest'; const repoRoot = path.resolve(path.dirname(fileURLToPath(import.meta.url)), '../..'); -const helperSourceDir = path.join( - repoRoot, - 'android/snapshot-helper/src/main/java/com/callstack/agentdevice/snapshothelper', +const helperSourceDir = path.join(repoRoot, 'android/snapshot-helper/src/main/java'); +const dispatcherFile = path.join( + 'com/callstack/agentdevice/snapshothelper', + 'UiAutomationInputDispatcher.java', ); -const dispatcherFile = 'UiAutomationInputDispatcher.java'; // The public two-argument UiAutomation overload waits up to five seconds for global window // animations before a gesture's first event and after its last one (#4590, #1356). test('Android helper input reaches the framework only through the dispatcher without the animation wait', () => { - const sources = fs.readdirSync(helperSourceDir).filter((name) => name.endsWith('.java')); + const sources = fs + .readdirSync(helperSourceDir, { recursive: true, encoding: 'utf8' }) + .filter((name) => name.endsWith('.java')); assert.ok(sources.includes(dispatcherFile), 'the input dispatcher must exist'); const directInjectors = sources.filter( (name) => From affdba3af428ba1d5c3f0ec68a08b9038ef6e895 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Pierzcha=C5=82a?= Date: Mon, 16 Nov 2026 19:28:43 +0100 Subject: [PATCH 03/16] fix(android): report an unavailable animation sync apart from a refused press Tap and key presses now share PressSequence, an Android-free policy that the helper's JVM suite tests with a fake injector. A refused start is discarded and retried once after the animation sync. When the runtime skips the animation wait but cannot sync separately, the press fails with its own outcome and error type instead of reporting a refusal. A refused end is not cancelled, because the injection verifier has already recorded it. Co-Authored-By: Claude Opus 5.5 --- .../AutomationCommandHandler.java | 121 ++++++++++++++---- .../snapshothelper/PressSequence.java | 65 ++++++++++ .../UiAutomationInputDispatcher.java | 58 +++------ .../snapshothelper/PressSequenceTest.java | 110 ++++++++++++++++ .../SnapshotHelperTestSuite.java | 1 + 5 files changed, 290 insertions(+), 65 deletions(-) create mode 100644 android/snapshot-helper/src/main/java/com/callstack/agentdevice/snapshothelper/PressSequence.java create mode 100644 android/snapshot-helper/src/test/java/com/callstack/agentdevice/snapshothelper/PressSequenceTest.java diff --git a/android/snapshot-helper/src/main/java/com/callstack/agentdevice/snapshothelper/AutomationCommandHandler.java b/android/snapshot-helper/src/main/java/com/callstack/agentdevice/snapshothelper/AutomationCommandHandler.java index 8db0de219d..f58d43ad41 100644 --- a/android/snapshot-helper/src/main/java/com/callstack/agentdevice/snapshothelper/AutomationCommandHandler.java +++ b/android/snapshot-helper/src/main/java/com/callstack/agentdevice/snapshothelper/AutomationCommandHandler.java @@ -78,41 +78,110 @@ private static void tap(UiAutomation automation, double x, double y, InputDispat || x < 0 || y < 0) { throw new IllegalArgumentException("Invalid tap coordinates"); } - float pointX = (float) x; - float pointY = (float) y; dispatch.attempted = true; - long down = UiAutomationInputDispatcher.injectStart( - automation, downTime -> touchEvent(downTime, MotionEvent.ACTION_DOWN, pointX, pointY)); - if (down < 0) throw new IllegalStateException("Touch injection was refused"); - MotionEvent up = touchEvent(down, MotionEvent.ACTION_UP, pointX, pointY); - try { - if (!UiAutomationInputDispatcher.inject(automation, up, true)) { - throw new IllegalStateException("Touch injection was refused"); - } - } finally { - up.recycle(); - } - } - - private static MotionEvent touchEvent(long down, int action, float x, float y) { - MotionEvent event = MotionEvent.obtain(down, SystemClock.uptimeMillis(), action, x, y, 0); - event.setSource(InputDevice.SOURCE_TOUCHSCREEN); - return event; + requireDispatched(PressSequence.run(new TouchPress(automation, (float) x, (float) y)), "Touch"); } private static void key(UiAutomation automation, int code, InputDispatch dispatch) { if (code < 0 || code > KeyEvent.getMaxKeyCode()) throw new IllegalArgumentException("Invalid key code"); dispatch.attempted = true; - long down = UiAutomationInputDispatcher.injectStart( - automation, downTime -> keyEvent(downTime, KeyEvent.ACTION_DOWN, code)); - if (down < 0 || !UiAutomationInputDispatcher.inject(automation, keyEvent(down, KeyEvent.ACTION_UP, code), true)) { - throw new IllegalStateException("Key injection was refused"); + requireDispatched(PressSequence.run(new KeyPress(automation, code)), "Key"); + } + + private static void requireDispatched(PressSequence.Outcome outcome, String kind) { + switch (outcome) { + case DISPATCHED: + return; + case ANIMATION_SYNC_UNAVAILABLE: + throw new UnsupportedOperationException( + kind + " injection was refused and this runtime cannot wait for animations to retry it"); + case START_REFUSED: + case END_REFUSED: + throw new IllegalStateException(kind + " injection was refused"); } } - private static KeyEvent keyEvent(long down, int action, int code) { - return new KeyEvent(down, SystemClock.uptimeMillis(), action, code, 0, 0, - KeyCharacterMap.VIRTUAL_KEYBOARD, 0, 0, InputDevice.SOURCE_KEYBOARD); + private abstract static class Press implements PressSequence.Injections { + final UiAutomation automation; + + Press(UiAutomation automation) { + this.automation = automation; + } + + @Override + public long now() { + return SystemClock.uptimeMillis(); + } + + @Override + public PressSequence.AnimationSync awaitAnimations() { + return UiAutomationInputDispatcher.awaitAnimations(automation); + } + } + + private static final class TouchPress extends Press { + private final float x; + private final float y; + + TouchPress(UiAutomation automation, float x, float y) { + super(automation); + this.x = x; + this.y = y; + } + + @Override + public boolean start(long downTime) { + return inject(MotionEvent.ACTION_DOWN, downTime); + } + + @Override + public boolean end(long downTime) { + return inject(MotionEvent.ACTION_UP, downTime); + } + + @Override + public void discardRefusedStart(long downTime) { + UiAutomationInputDispatcher.cancelRefusedDown(automation, downTime, x, y); + } + + private boolean inject(int action, long downTime) { + MotionEvent event = MotionEvent.obtain(downTime, SystemClock.uptimeMillis(), action, x, y, 0); + event.setSource(InputDevice.SOURCE_TOUCHSCREEN); + try { + return UiAutomationInputDispatcher.inject(automation, event, true); + } finally { + event.recycle(); + } + } + } + + private static final class KeyPress extends Press { + private final int code; + + KeyPress(UiAutomation automation, int code) { + super(automation); + this.code = code; + } + + @Override + public boolean start(long downTime) { + return inject(KeyEvent.ACTION_DOWN, downTime); + } + + @Override + public boolean end(long downTime) { + return inject(KeyEvent.ACTION_UP, downTime); + } + + /** The injection verifier checks motion events only, so a refused key DOWN leaves no state. */ + @Override + public void discardRefusedStart(long downTime) {} + + private boolean inject(int action, long downTime) { + return UiAutomationInputDispatcher.inject(automation, new KeyEvent(downTime, + SystemClock.uptimeMillis(), action, code, 0, 0, KeyCharacterMap.VIRTUAL_KEYBOARD, 0, 0, + InputDevice.SOURCE_KEYBOARD), true); + } } private static void fill(UiAutomation automation, int x, int y, String text, InputDispatch dispatch) { diff --git a/android/snapshot-helper/src/main/java/com/callstack/agentdevice/snapshothelper/PressSequence.java b/android/snapshot-helper/src/main/java/com/callstack/agentdevice/snapshothelper/PressSequence.java new file mode 100644 index 0000000000..40a2204cc5 --- /dev/null +++ b/android/snapshot-helper/src/main/java/com/callstack/agentdevice/snapshothelper/PressSequence.java @@ -0,0 +1,65 @@ +package com.callstack.agentdevice.snapshothelper; + +/** + * Sequences one tap or key press: a start event, then an end event with the same down time. + * + *

A refused start reached no window, as while a transition leaves nothing able to receive it. + * The injection verifier still records it as landed, so the start is discarded before it is retried + * once after the framework's animation sync, with a fresh down time so the receiver does not measure + * the wait as a held press. A refused end is not cancelled: the verifier has recorded it too, so the + * press is already closed there and a cancel would itself be inconsistent. + */ +final class PressSequence { + /** Builds and injects the platform events of one press. */ + interface Injections { + long now(); + + /** Injects the start event, without the framework's animation wait when the runtime allows. */ + boolean start(long downTime); + + boolean end(long downTime); + + /** Closes the verifier state of a refused start that has this down time. */ + void discardRefusedStart(long downTime); + + AnimationSync awaitAnimations(); + } + + enum AnimationSync { + WAITED, + /** The runtime injects through the public overload, which waited for animations already. */ + ALREADY_WAITED, + /** The runtime skips the animation wait on injection but cannot wait separately. */ + UNAVAILABLE, + } + + enum Outcome { + DISPATCHED, + START_REFUSED, + END_REFUSED, + ANIMATION_SYNC_UNAVAILABLE, + } + + private PressSequence() {} + + static Outcome run(Injections injections) { + long downTime = injections.now(); + if (!injections.start(downTime)) { + injections.discardRefusedStart(downTime); + switch (injections.awaitAnimations()) { + case ALREADY_WAITED: + return Outcome.START_REFUSED; + case UNAVAILABLE: + return Outcome.ANIMATION_SYNC_UNAVAILABLE; + case WAITED: + downTime = injections.now(); + if (!injections.start(downTime)) { + injections.discardRefusedStart(downTime); + return Outcome.START_REFUSED; + } + break; + } + } + return injections.end(downTime) ? Outcome.DISPATCHED : Outcome.END_REFUSED; + } +} diff --git a/android/snapshot-helper/src/main/java/com/callstack/agentdevice/snapshothelper/UiAutomationInputDispatcher.java b/android/snapshot-helper/src/main/java/com/callstack/agentdevice/snapshothelper/UiAutomationInputDispatcher.java index 4b8c3f83fe..bdff4ab2f1 100644 --- a/android/snapshot-helper/src/main/java/com/callstack/agentdevice/snapshothelper/UiAutomationInputDispatcher.java +++ b/android/snapshot-helper/src/main/java/com/callstack/agentdevice/snapshothelper/UiAutomationInputDispatcher.java @@ -2,6 +2,7 @@ import android.app.UiAutomation; import android.os.SystemClock; +import android.view.InputDevice; import android.view.InputEvent; import android.view.MotionEvent; import java.lang.reflect.InvocationTargetException; @@ -19,11 +20,6 @@ final class UiAutomationInputDispatcher { private static volatile Method injectWithoutAnimationWait = findInjectWithoutAnimationWait(); private static volatile Method syncInputTransactions = findSyncInputTransactions(); - /** Builds the first event of a tap or key press for a down time. */ - interface StartEvent { - InputEvent obtain(long downTime); - } - private UiAutomationInputDispatcher() {} static boolean inject(UiAutomation automation, InputEvent event, boolean waitForDispatch) { @@ -42,39 +38,18 @@ static boolean inject(UiAutomation automation, InputEvent event, boolean waitFor } /** - * Synchronously injects the first event of a tap or key press and returns its down time, or -1 - * when the framework refused it. A refused start reached no window: a transition can leave nothing - * able to receive it. The start is then injected once more after the framework's animation sync, - * with a fresh down time so the receiver does not measure the wait as a held press. + * Closes the verifier state of a refused touch DOWN with an ACTION_CANCEL. The verifier records a + * dropped DOWN as a pointer that is still down; no window holds the touch to receive the cancel. */ - static long injectStart(UiAutomation automation, StartEvent start) { - long downTime = SystemClock.uptimeMillis(); - if (injectStartRecycled(automation, start.obtain(downTime))) return downTime; - if (!awaitAnimations(automation)) return -1; - downTime = SystemClock.uptimeMillis(); - return injectStartRecycled(automation, start.obtain(downTime)) ? downTime : -1; - } - - private static boolean injectStartRecycled(UiAutomation automation, InputEvent event) { - try { - if (inject(automation, event, true)) return true; - // The dispatcher's input verifier records a dropped DOWN as a pointer that is still down. - // Cancelling it keeps the next DOWN consistent; no window holds the touch to receive it. - if (event instanceof MotionEvent) cancel(automation, (MotionEvent) event); - return false; - } finally { - if (event instanceof MotionEvent) ((MotionEvent) event).recycle(); - } - } - - private static void cancel(UiAutomation automation, MotionEvent refused) { + static void cancelRefusedDown(UiAutomation automation, long downTime, float x, float y) { MotionEvent.PointerProperties[] properties = {new MotionEvent.PointerProperties()}; + properties[0].id = 0; MotionEvent.PointerCoords[] coords = {new MotionEvent.PointerCoords()}; - refused.getPointerProperties(0, properties[0]); - refused.getPointerCoords(0, coords[0]); - MotionEvent cancel = MotionEvent.obtain(refused.getDownTime(), refused.getEventTime(), + coords[0].x = x; + coords[0].y = y; + MotionEvent cancel = MotionEvent.obtain(downTime, SystemClock.uptimeMillis(), MotionEvent.ACTION_CANCEL, 1, properties, coords, 0, 0, 1f, 1f, 0, 0, - refused.getSource(), MotionEvent.FLAG_CANCELED); + InputDevice.SOURCE_TOUCHSCREEN, MotionEvent.FLAG_CANCELED); try { inject(automation, cancel, false); } finally { @@ -82,16 +57,21 @@ private static void cancel(UiAutomation automation, MotionEvent refused) { } } - /** Returns false when injection already waited for animations through the public overload. */ - private static boolean awaitAnimations(UiAutomation automation) { + /** + * Waits for the framework's animation sync before a refused start is retried. The sync is + * {@code ALREADY_WAITED} when injection runs through the public overload, and {@code UNAVAILABLE} + * when injection skips the wait but the runtime does not expose or permit the separate sync. + */ + static PressSequence.AnimationSync awaitAnimations(UiAutomation automation) { + if (injectWithoutAnimationWait == null) return PressSequence.AnimationSync.ALREADY_WAITED; Method method = syncInputTransactions; - if (injectWithoutAnimationWait == null || method == null) return false; + if (method == null) return PressSequence.AnimationSync.UNAVAILABLE; try { method.invoke(automation); - return true; + return PressSequence.AnimationSync.WAITED; } catch (IllegalAccessException | IllegalArgumentException error) { syncInputTransactions = null; - return false; + return PressSequence.AnimationSync.UNAVAILABLE; } catch (InvocationTargetException error) { rethrowCause(error.getCause()); throw new AssertionError("Unreachable"); diff --git a/android/snapshot-helper/src/test/java/com/callstack/agentdevice/snapshothelper/PressSequenceTest.java b/android/snapshot-helper/src/test/java/com/callstack/agentdevice/snapshothelper/PressSequenceTest.java new file mode 100644 index 0000000000..dccac6b0ca --- /dev/null +++ b/android/snapshot-helper/src/test/java/com/callstack/agentdevice/snapshothelper/PressSequenceTest.java @@ -0,0 +1,110 @@ +package com.callstack.agentdevice.snapshothelper; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; + +public final class PressSequenceTest { + private PressSequenceTest() {} + + static void run() { + endsWithTheStartDownTime(); + retriesARefusedStartAfterTheAnimationSyncWithAFreshDownTime(); + discardsAStartRefusedAgainAfterTheSync(); + doesNotCancelARefusedEnd(); + doesNotRetryWhenInjectionAlreadyWaited(); + reportsAnUnavailableAnimationSyncApartFromARefusal(); + } + + private static void endsWithTheStartDownTime() { + FakeInjections injections = new FakeInjections(PressSequence.AnimationSync.WAITED, true); + expect(PressSequence.run(injections), PressSequence.Outcome.DISPATCHED); + expect(injections.calls, "start 1", "end 1"); + } + + private static void retriesARefusedStartAfterTheAnimationSyncWithAFreshDownTime() { + FakeInjections injections = new FakeInjections(PressSequence.AnimationSync.WAITED, false, true); + expect(PressSequence.run(injections), PressSequence.Outcome.DISPATCHED); + expect(injections.calls, "start 1", "discard 1", "sync", "start 2", "end 2"); + } + + private static void discardsAStartRefusedAgainAfterTheSync() { + FakeInjections injections = + new FakeInjections(PressSequence.AnimationSync.WAITED, false, false); + expect(PressSequence.run(injections), PressSequence.Outcome.START_REFUSED); + expect(injections.calls, "start 1", "discard 1", "sync", "start 2", "discard 2"); + } + + private static void doesNotCancelARefusedEnd() { + FakeInjections injections = new FakeInjections(PressSequence.AnimationSync.WAITED, true); + injections.endAccepted = false; + expect(PressSequence.run(injections), PressSequence.Outcome.END_REFUSED); + expect(injections.calls, "start 1", "end 1"); + } + + private static void doesNotRetryWhenInjectionAlreadyWaited() { + FakeInjections injections = + new FakeInjections(PressSequence.AnimationSync.ALREADY_WAITED, false, true); + expect(PressSequence.run(injections), PressSequence.Outcome.START_REFUSED); + expect(injections.calls, "start 1", "discard 1", "sync"); + } + + private static void reportsAnUnavailableAnimationSyncApartFromARefusal() { + FakeInjections injections = + new FakeInjections(PressSequence.AnimationSync.UNAVAILABLE, false, true); + expect(PressSequence.run(injections), PressSequence.Outcome.ANIMATION_SYNC_UNAVAILABLE); + expect(injections.calls, "start 1", "discard 1", "sync"); + } + + private static void expect(PressSequence.Outcome actual, PressSequence.Outcome expected) { + if (actual != expected) throw new AssertionError("Expected " + expected + ", got " + actual); + } + + private static void expect(List actual, String... expected) { + if (!actual.equals(Arrays.asList(expected))) { + throw new AssertionError("Expected " + Arrays.asList(expected) + ", got " + actual); + } + } + + private static final class FakeInjections implements PressSequence.Injections { + final List calls = new ArrayList<>(); + private final PressSequence.AnimationSync sync; + private final boolean[] startAccepted; + private int starts; + private long clock; + boolean endAccepted = true; + + FakeInjections(PressSequence.AnimationSync sync, boolean... startAccepted) { + this.sync = sync; + this.startAccepted = startAccepted; + } + + @Override + public long now() { + return ++clock; + } + + @Override + public boolean start(long downTime) { + calls.add("start " + downTime); + return startAccepted[starts++]; + } + + @Override + public boolean end(long downTime) { + calls.add("end " + downTime); + return endAccepted; + } + + @Override + public void discardRefusedStart(long downTime) { + calls.add("discard " + downTime); + } + + @Override + public PressSequence.AnimationSync awaitAnimations() { + calls.add("sync"); + return sync; + } + } +} diff --git a/android/snapshot-helper/src/test/java/com/callstack/agentdevice/snapshothelper/SnapshotHelperTestSuite.java b/android/snapshot-helper/src/test/java/com/callstack/agentdevice/snapshothelper/SnapshotHelperTestSuite.java index faca37a485..641215943a 100644 --- a/android/snapshot-helper/src/test/java/com/callstack/agentdevice/snapshothelper/SnapshotHelperTestSuite.java +++ b/android/snapshot-helper/src/test/java/com/callstack/agentdevice/snapshothelper/SnapshotHelperTestSuite.java @@ -5,6 +5,7 @@ private SnapshotHelperTestSuite() {} public static void main(String[] args) throws Exception { PointerEventScheduleTest.run(); + PressSequenceTest.run(); AccessibilityCaptureStabilizerTest.run(); AccessibilityReadBatchTest.run(); AccessibilityTreeXmlTest.run(); From 4bcd7ada3dad08e527a5ef84bcaccd0ee38f3f25 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Pierzcha=C5=82a?= Date: Mon, 16 Nov 2026 19:40:11 +0100 Subject: [PATCH 04/16] docs(android): state why a refused press end gets no cancel Co-Authored-By: Claude Opus 5.5 --- .../agentdevice/snapshothelper/PressSequence.java | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/android/snapshot-helper/src/main/java/com/callstack/agentdevice/snapshothelper/PressSequence.java b/android/snapshot-helper/src/main/java/com/callstack/agentdevice/snapshothelper/PressSequence.java index 40a2204cc5..e3f60c7e16 100644 --- a/android/snapshot-helper/src/main/java/com/callstack/agentdevice/snapshothelper/PressSequence.java +++ b/android/snapshot-helper/src/main/java/com/callstack/agentdevice/snapshothelper/PressSequence.java @@ -6,8 +6,7 @@ *

A refused start reached no window, as while a transition leaves nothing able to receive it. * The injection verifier still records it as landed, so the start is discarded before it is retried * once after the framework's animation sync, with a fresh down time so the receiver does not measure - * the wait as a held press. A refused end is not cancelled: the verifier has recorded it too, so the - * press is already closed there and a cancel would itself be inconsistent. + * the wait as a held press. */ final class PressSequence { /** Builds and injects the platform events of one press. */ @@ -60,6 +59,11 @@ static Outcome run(Injections injections) { break; } } + // A refused end gets no cancel. The verifier records an event before the dispatcher drops it, + // so the press is closed there and a cancel would be inconsistent. The dispatcher drops an end + // only when no pointer or focus is still down, after it cancelled the receiving window itself + // ("touched window was removed", "focus left window"). A timed-out end was already enqueued, + // so the live window still receives it. return injections.end(downTime) ? Outcome.DISPATCHED : Outcome.END_REFUSED; } } From 342b1a1a66060704389ed0f66b628e86e54d9bfd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Pierzcha=C5=82a?= Date: Mon, 16 Nov 2026 19:52:35 +0100 Subject: [PATCH 05/16] fix(android): release a timed-out press start instead of retrying it Synchronous injection reports a dispatch timeout and a drop alike as false, but the dispatcher starts its 30 s deadline when the call enters it, so only a refusal at least that long after the call can be a timeout. A timed-out start was enqueued and reaches the receiver, so it is now released (touch: ACTION_CANCEL, key: ACTION_UP with FLAG_CANCELED) and reported as its own outcome instead of being followed by a second start. Co-Authored-By: Claude Opus 5.5 --- .../AutomationCommandHandler.java | 45 +++++++--- .../snapshothelper/PressSequence.java | 48 +++++++++-- .../UiAutomationInputDispatcher.java | 6 +- .../snapshothelper/PressSequenceTest.java | 86 ++++++++++++++----- 4 files changed, 142 insertions(+), 43 deletions(-) diff --git a/android/snapshot-helper/src/main/java/com/callstack/agentdevice/snapshothelper/AutomationCommandHandler.java b/android/snapshot-helper/src/main/java/com/callstack/agentdevice/snapshothelper/AutomationCommandHandler.java index f58d43ad41..2059171f49 100644 --- a/android/snapshot-helper/src/main/java/com/callstack/agentdevice/snapshothelper/AutomationCommandHandler.java +++ b/android/snapshot-helper/src/main/java/com/callstack/agentdevice/snapshothelper/AutomationCommandHandler.java @@ -18,6 +18,7 @@ import java.io.IOException; import java.io.OutputStream; import java.nio.charset.StandardCharsets; +import java.util.concurrent.TimeoutException; import org.json.JSONObject; /** Executes session input and image capture inside the instrumentation's automation context. */ @@ -73,7 +74,8 @@ private static void input(UiAutomation automation, JSONObject input, InputDispat } } - private static void tap(UiAutomation automation, double x, double y, InputDispatch dispatch) { + private static void tap(UiAutomation automation, double x, double y, InputDispatch dispatch) + throws TimeoutException { if (Double.isNaN(x) || Double.isInfinite(x) || Double.isNaN(y) || Double.isInfinite(y) || x < 0 || y < 0) { throw new IllegalArgumentException("Invalid tap coordinates"); @@ -82,19 +84,24 @@ private static void tap(UiAutomation automation, double x, double y, InputDispat requireDispatched(PressSequence.run(new TouchPress(automation, (float) x, (float) y)), "Touch"); } - private static void key(UiAutomation automation, int code, InputDispatch dispatch) { + private static void key(UiAutomation automation, int code, InputDispatch dispatch) + throws TimeoutException { if (code < 0 || code > KeyEvent.getMaxKeyCode()) throw new IllegalArgumentException("Invalid key code"); dispatch.attempted = true; requireDispatched(PressSequence.run(new KeyPress(automation, code)), "Key"); } - private static void requireDispatched(PressSequence.Outcome outcome, String kind) { + private static void requireDispatched(PressSequence.Outcome outcome, String kind) + throws TimeoutException { switch (outcome) { case DISPATCHED: return; case ANIMATION_SYNC_UNAVAILABLE: throw new UnsupportedOperationException( kind + " injection was refused and this runtime cannot wait for animations to retry it"); + case START_TIMED_OUT: + throw new TimeoutException( + kind + " injection timed out before the app finished handling it; it was cancelled"); case START_REFUSED: case END_REFUSED: throw new IllegalStateException(kind + " injection was refused"); @@ -140,8 +147,13 @@ public boolean end(long downTime) { } @Override - public void discardRefusedStart(long downTime) { - UiAutomationInputDispatcher.cancelRefusedDown(automation, downTime, x, y); + public void discardDroppedStart(long downTime) { + UiAutomationInputDispatcher.cancelDown(automation, downTime, x, y); + } + + @Override + public void cancelStart(long downTime) { + UiAutomationInputDispatcher.cancelDown(automation, downTime, x, y); } private boolean inject(int action, long downTime) { @@ -173,18 +185,29 @@ public boolean end(long downTime) { return inject(KeyEvent.ACTION_UP, downTime); } - /** The injection verifier checks motion events only, so a refused key DOWN leaves no state. */ + /** The injection verifier checks motion events only, so a dropped key DOWN leaves no state. */ + @Override + public void discardDroppedStart(long downTime) {} + + /** Android releases an interrupted key with an ACTION_UP that carries FLAG_CANCELED. */ @Override - public void discardRefusedStart(long downTime) {} + public void cancelStart(long downTime) { + UiAutomationInputDispatcher.inject(automation, event(KeyEvent.ACTION_UP, downTime, + KeyEvent.FLAG_CANCELED), false); + } private boolean inject(int action, long downTime) { - return UiAutomationInputDispatcher.inject(automation, new KeyEvent(downTime, - SystemClock.uptimeMillis(), action, code, 0, 0, KeyCharacterMap.VIRTUAL_KEYBOARD, 0, 0, - InputDevice.SOURCE_KEYBOARD), true); + return UiAutomationInputDispatcher.inject(automation, event(action, downTime, 0), true); + } + + private KeyEvent event(int action, long downTime, int flags) { + return new KeyEvent(downTime, SystemClock.uptimeMillis(), action, code, 0, 0, + KeyCharacterMap.VIRTUAL_KEYBOARD, 0, flags, InputDevice.SOURCE_KEYBOARD); } } - private static void fill(UiAutomation automation, int x, int y, String text, InputDispatch dispatch) { + private static void fill(UiAutomation automation, int x, int y, String text, InputDispatch dispatch) + throws TimeoutException { AccessibilityNodeInfo target = null; try { for (AccessibilityWindowInfo window : automation.getWindows()) { diff --git a/android/snapshot-helper/src/main/java/com/callstack/agentdevice/snapshothelper/PressSequence.java b/android/snapshot-helper/src/main/java/com/callstack/agentdevice/snapshothelper/PressSequence.java index e3f60c7e16..80d8960aa5 100644 --- a/android/snapshot-helper/src/main/java/com/callstack/agentdevice/snapshothelper/PressSequence.java +++ b/android/snapshot-helper/src/main/java/com/callstack/agentdevice/snapshothelper/PressSequence.java @@ -3,12 +3,19 @@ /** * Sequences one tap or key press: a start event, then an end event with the same down time. * - *

A refused start reached no window, as while a transition leaves nothing able to receive it. + *

A dropped start reached no window, as while a transition leaves nothing able to receive it. * The injection verifier still records it as landed, so the start is discarded before it is retried * once after the framework's animation sync, with a fresh down time so the receiver does not measure * the wait as a held press. */ final class PressSequence { + /** + * The framework's synchronous injection deadline, {@code InputManagerService.INJECTION_TIMEOUT_MILLIS}. + * Injection reports a timeout and a drop alike as {@code false}. The dispatcher starts this deadline + * when the call enters it, so only a refusal at least this long after the call can be a timeout. + */ + static final long INJECTION_TIMEOUT_MS = 30_000; + /** Builds and injects the platform events of one press. */ interface Injections { long now(); @@ -18,8 +25,11 @@ interface Injections { boolean end(long downTime); - /** Closes the verifier state of a refused start that has this down time. */ - void discardRefusedStart(long downTime); + /** Closes the verifier state of a dropped start that has this down time. */ + void discardDroppedStart(long downTime); + + /** Releases a start that has this down time and that the receiver may hold. */ + void cancelStart(long downTime); AnimationSync awaitAnimations(); } @@ -35,16 +45,23 @@ enum AnimationSync { enum Outcome { DISPATCHED, START_REFUSED, + START_TIMED_OUT, END_REFUSED, ANIMATION_SYNC_UNAVAILABLE, } + private enum Start { + ACCEPTED, + DROPPED, + TIMED_OUT, + } + private PressSequence() {} static Outcome run(Injections injections) { long downTime = injections.now(); - if (!injections.start(downTime)) { - injections.discardRefusedStart(downTime); + Start start = start(injections, downTime); + if (start == Start.DROPPED) { switch (injections.awaitAnimations()) { case ALREADY_WAITED: return Outcome.START_REFUSED; @@ -52,13 +69,12 @@ static Outcome run(Injections injections) { return Outcome.ANIMATION_SYNC_UNAVAILABLE; case WAITED: downTime = injections.now(); - if (!injections.start(downTime)) { - injections.discardRefusedStart(downTime); - return Outcome.START_REFUSED; - } + start = start(injections, downTime); + if (start == Start.DROPPED) return Outcome.START_REFUSED; break; } } + if (start == Start.TIMED_OUT) return Outcome.START_TIMED_OUT; // A refused end gets no cancel. The verifier records an event before the dispatcher drops it, // so the press is closed there and a cancel would be inconsistent. The dispatcher drops an end // only when no pointer or focus is still down, after it cancelled the receiving window itself @@ -66,4 +82,18 @@ static Outcome run(Injections injections) { // so the live window still receives it. return injections.end(downTime) ? Outcome.DISPATCHED : Outcome.END_REFUSED; } + + /** + * A timed-out start was enqueued before the deadline passed, so the receiver gets it and must not + * see a second start; it is released instead of retried. + */ + private static Start start(Injections injections, long downTime) { + if (injections.start(downTime)) return Start.ACCEPTED; + if (injections.now() - downTime >= INJECTION_TIMEOUT_MS) { + injections.cancelStart(downTime); + return Start.TIMED_OUT; + } + injections.discardDroppedStart(downTime); + return Start.DROPPED; + } } diff --git a/android/snapshot-helper/src/main/java/com/callstack/agentdevice/snapshothelper/UiAutomationInputDispatcher.java b/android/snapshot-helper/src/main/java/com/callstack/agentdevice/snapshothelper/UiAutomationInputDispatcher.java index bdff4ab2f1..518bbb5e58 100644 --- a/android/snapshot-helper/src/main/java/com/callstack/agentdevice/snapshothelper/UiAutomationInputDispatcher.java +++ b/android/snapshot-helper/src/main/java/com/callstack/agentdevice/snapshothelper/UiAutomationInputDispatcher.java @@ -38,10 +38,10 @@ static boolean inject(UiAutomation automation, InputEvent event, boolean waitFor } /** - * Closes the verifier state of a refused touch DOWN with an ACTION_CANCEL. The verifier records a - * dropped DOWN as a pointer that is still down; no window holds the touch to receive the cancel. + * Ends a refused touch DOWN with an ACTION_CANCEL. The verifier records a dropped DOWN as a pointer + * that is still down, and a window that received a timed-out DOWN holds the pointer. */ - static void cancelRefusedDown(UiAutomation automation, long downTime, float x, float y) { + static void cancelDown(UiAutomation automation, long downTime, float x, float y) { MotionEvent.PointerProperties[] properties = {new MotionEvent.PointerProperties()}; properties[0].id = 0; MotionEvent.PointerCoords[] coords = {new MotionEvent.PointerCoords()}; diff --git a/android/snapshot-helper/src/test/java/com/callstack/agentdevice/snapshothelper/PressSequenceTest.java b/android/snapshot-helper/src/test/java/com/callstack/agentdevice/snapshothelper/PressSequenceTest.java index dccac6b0ca..77fa53907e 100644 --- a/android/snapshot-helper/src/test/java/com/callstack/agentdevice/snapshothelper/PressSequenceTest.java +++ b/android/snapshot-helper/src/test/java/com/callstack/agentdevice/snapshothelper/PressSequenceTest.java @@ -5,57 +5,94 @@ import java.util.List; public final class PressSequenceTest { + private static final long TIMEOUT = PressSequence.INJECTION_TIMEOUT_MS; + private PressSequenceTest() {} static void run() { endsWithTheStartDownTime(); - retriesARefusedStartAfterTheAnimationSyncWithAFreshDownTime(); - discardsAStartRefusedAgainAfterTheSync(); + retriesADroppedStartAfterTheAnimationSyncWithAFreshDownTime(); + retriesAStartRefusedJustBeforeTheInjectionDeadline(); + cancelsAndDoesNotRetryAStartRefusedAtTheInjectionDeadline(); + discardsAStartDroppedAgainAfterTheSync(); + cancelsARetriedStartThatTimesOut(); doesNotCancelARefusedEnd(); doesNotRetryWhenInjectionAlreadyWaited(); reportsAnUnavailableAnimationSyncApartFromARefusal(); } private static void endsWithTheStartDownTime() { - FakeInjections injections = new FakeInjections(PressSequence.AnimationSync.WAITED, true); + FakeInjections injections = new FakeInjections(PressSequence.AnimationSync.WAITED, accepted()); expect(PressSequence.run(injections), PressSequence.Outcome.DISPATCHED); expect(injections.calls, "start 1", "end 1"); } - private static void retriesARefusedStartAfterTheAnimationSyncWithAFreshDownTime() { - FakeInjections injections = new FakeInjections(PressSequence.AnimationSync.WAITED, false, true); + private static void retriesADroppedStartAfterTheAnimationSyncWithAFreshDownTime() { + FakeInjections injections = + new FakeInjections(PressSequence.AnimationSync.WAITED, refusedAfter(0), accepted()); + expect(PressSequence.run(injections), PressSequence.Outcome.DISPATCHED); + expect(injections.calls, "start 1", "discard 1", "sync", "start 3", "end 3"); + } + + private static void retriesAStartRefusedJustBeforeTheInjectionDeadline() { + FakeInjections injections = new FakeInjections( + PressSequence.AnimationSync.WAITED, refusedAfter(TIMEOUT - 2), accepted()); expect(PressSequence.run(injections), PressSequence.Outcome.DISPATCHED); - expect(injections.calls, "start 1", "discard 1", "sync", "start 2", "end 2"); + expect(injections.calls, "start 1", "discard 1", "sync", "start 30001", "end 30001"); } - private static void discardsAStartRefusedAgainAfterTheSync() { + private static void cancelsAndDoesNotRetryAStartRefusedAtTheInjectionDeadline() { + FakeInjections injections = new FakeInjections( + PressSequence.AnimationSync.WAITED, refusedAfter(TIMEOUT - 1), accepted()); + expect(PressSequence.run(injections), PressSequence.Outcome.START_TIMED_OUT); + expect(injections.calls, "start 1", "cancel 1"); + } + + private static void discardsAStartDroppedAgainAfterTheSync() { FakeInjections injections = - new FakeInjections(PressSequence.AnimationSync.WAITED, false, false); + new FakeInjections(PressSequence.AnimationSync.WAITED, refusedAfter(0), refusedAfter(0)); expect(PressSequence.run(injections), PressSequence.Outcome.START_REFUSED); - expect(injections.calls, "start 1", "discard 1", "sync", "start 2", "discard 2"); + expect(injections.calls, "start 1", "discard 1", "sync", "start 3", "discard 3"); + } + + private static void cancelsARetriedStartThatTimesOut() { + FakeInjections injections = new FakeInjections( + PressSequence.AnimationSync.WAITED, refusedAfter(0), refusedAfter(TIMEOUT)); + expect(PressSequence.run(injections), PressSequence.Outcome.START_TIMED_OUT); + expect(injections.calls, "start 1", "discard 1", "sync", "start 3", "cancel 3"); } private static void doesNotCancelARefusedEnd() { - FakeInjections injections = new FakeInjections(PressSequence.AnimationSync.WAITED, true); + FakeInjections injections = new FakeInjections(PressSequence.AnimationSync.WAITED, accepted()); injections.endAccepted = false; expect(PressSequence.run(injections), PressSequence.Outcome.END_REFUSED); expect(injections.calls, "start 1", "end 1"); } private static void doesNotRetryWhenInjectionAlreadyWaited() { - FakeInjections injections = - new FakeInjections(PressSequence.AnimationSync.ALREADY_WAITED, false, true); + FakeInjections injections = new FakeInjections( + PressSequence.AnimationSync.ALREADY_WAITED, refusedAfter(0), accepted()); expect(PressSequence.run(injections), PressSequence.Outcome.START_REFUSED); expect(injections.calls, "start 1", "discard 1", "sync"); } private static void reportsAnUnavailableAnimationSyncApartFromARefusal() { - FakeInjections injections = - new FakeInjections(PressSequence.AnimationSync.UNAVAILABLE, false, true); + FakeInjections injections = new FakeInjections( + PressSequence.AnimationSync.UNAVAILABLE, refusedAfter(0), accepted()); expect(PressSequence.run(injections), PressSequence.Outcome.ANIMATION_SYNC_UNAVAILABLE); expect(injections.calls, "start 1", "discard 1", "sync"); } + /** A start the framework accepts. */ + private static long accepted() { + return -1; + } + + /** A start the framework refuses after this many milliseconds. */ + private static long refusedAfter(long elapsedMs) { + return elapsedMs; + } + private static void expect(PressSequence.Outcome actual, PressSequence.Outcome expected) { if (actual != expected) throw new AssertionError("Expected " + expected + ", got " + actual); } @@ -66,17 +103,18 @@ private static void expect(List actual, String... expected) { } } + /** Each {@code now()} advances the clock by 1 ms; a refused start also advances it by its delay. */ private static final class FakeInjections implements PressSequence.Injections { final List calls = new ArrayList<>(); private final PressSequence.AnimationSync sync; - private final boolean[] startAccepted; - private int starts; + private final long[] starts; + private int startIndex; private long clock; boolean endAccepted = true; - FakeInjections(PressSequence.AnimationSync sync, boolean... startAccepted) { + FakeInjections(PressSequence.AnimationSync sync, long... starts) { this.sync = sync; - this.startAccepted = startAccepted; + this.starts = starts; } @Override @@ -87,7 +125,10 @@ public long now() { @Override public boolean start(long downTime) { calls.add("start " + downTime); - return startAccepted[starts++]; + long refusedAfterMs = starts[startIndex++]; + if (refusedAfterMs < 0) return true; + clock += refusedAfterMs; + return false; } @Override @@ -97,10 +138,15 @@ public boolean end(long downTime) { } @Override - public void discardRefusedStart(long downTime) { + public void discardDroppedStart(long downTime) { calls.add("discard " + downTime); } + @Override + public void cancelStart(long downTime) { + calls.add("cancel " + downTime); + } + @Override public PressSequence.AnimationSync awaitAnimations() { calls.add("sync"); From 206e2e8b70ce480fa432a2c909ba863008ecdf2f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Pierzcha=C5=82a?= Date: Mon, 16 Nov 2026 20:03:42 +0100 Subject: [PATCH 06/16] test(android): pin that a timed-out press start fails as a timeout without an end The outcome-to-exception mapping moves from AutomationCommandHandler into PressSequence, next to the outcome it maps, so the JVM suite covers it. Co-Authored-By: Claude Opus 5.5 --- .../AutomationCommandHandler.java | 21 ++----------- .../snapshothelper/PressSequence.java | 20 +++++++++++++ .../snapshothelper/PressSequenceTest.java | 30 +++++++++++++++++++ 3 files changed, 52 insertions(+), 19 deletions(-) diff --git a/android/snapshot-helper/src/main/java/com/callstack/agentdevice/snapshothelper/AutomationCommandHandler.java b/android/snapshot-helper/src/main/java/com/callstack/agentdevice/snapshothelper/AutomationCommandHandler.java index 2059171f49..d28c606d89 100644 --- a/android/snapshot-helper/src/main/java/com/callstack/agentdevice/snapshothelper/AutomationCommandHandler.java +++ b/android/snapshot-helper/src/main/java/com/callstack/agentdevice/snapshothelper/AutomationCommandHandler.java @@ -81,31 +81,14 @@ private static void tap(UiAutomation automation, double x, double y, InputDispat throw new IllegalArgumentException("Invalid tap coordinates"); } dispatch.attempted = true; - requireDispatched(PressSequence.run(new TouchPress(automation, (float) x, (float) y)), "Touch"); + PressSequence.requireDispatched(PressSequence.run(new TouchPress(automation, (float) x, (float) y)), "Touch"); } private static void key(UiAutomation automation, int code, InputDispatch dispatch) throws TimeoutException { if (code < 0 || code > KeyEvent.getMaxKeyCode()) throw new IllegalArgumentException("Invalid key code"); dispatch.attempted = true; - requireDispatched(PressSequence.run(new KeyPress(automation, code)), "Key"); - } - - private static void requireDispatched(PressSequence.Outcome outcome, String kind) - throws TimeoutException { - switch (outcome) { - case DISPATCHED: - return; - case ANIMATION_SYNC_UNAVAILABLE: - throw new UnsupportedOperationException( - kind + " injection was refused and this runtime cannot wait for animations to retry it"); - case START_TIMED_OUT: - throw new TimeoutException( - kind + " injection timed out before the app finished handling it; it was cancelled"); - case START_REFUSED: - case END_REFUSED: - throw new IllegalStateException(kind + " injection was refused"); - } + PressSequence.requireDispatched(PressSequence.run(new KeyPress(automation, code)), "Key"); } private abstract static class Press implements PressSequence.Injections { diff --git a/android/snapshot-helper/src/main/java/com/callstack/agentdevice/snapshothelper/PressSequence.java b/android/snapshot-helper/src/main/java/com/callstack/agentdevice/snapshothelper/PressSequence.java index 80d8960aa5..39908db833 100644 --- a/android/snapshot-helper/src/main/java/com/callstack/agentdevice/snapshothelper/PressSequence.java +++ b/android/snapshot-helper/src/main/java/com/callstack/agentdevice/snapshothelper/PressSequence.java @@ -1,5 +1,7 @@ package com.callstack.agentdevice.snapshothelper; +import java.util.concurrent.TimeoutException; + /** * Sequences one tap or key press: a start event, then an end event with the same down time. * @@ -83,6 +85,24 @@ static Outcome run(Injections injections) { return injections.end(downTime) ? Outcome.DISPATCHED : Outcome.END_REFUSED; } + /** Turns an outcome into the helper's response: a typed exception for each failed press. */ + static void requireDispatched(Outcome outcome, String kind) + throws TimeoutException { + switch (outcome) { + case DISPATCHED: + return; + case ANIMATION_SYNC_UNAVAILABLE: + throw new UnsupportedOperationException( + kind + " injection was refused and this runtime cannot wait for animations to retry it"); + case START_TIMED_OUT: + throw new TimeoutException( + kind + " injection timed out before the app finished handling it; it was cancelled"); + case START_REFUSED: + case END_REFUSED: + throw new IllegalStateException(kind + " injection was refused"); + } + } + /** * A timed-out start was enqueued before the deadline passed, so the receiver gets it and must not * see a second start; it is released instead of retried. diff --git a/android/snapshot-helper/src/test/java/com/callstack/agentdevice/snapshothelper/PressSequenceTest.java b/android/snapshot-helper/src/test/java/com/callstack/agentdevice/snapshothelper/PressSequenceTest.java index 77fa53907e..5af3f32be9 100644 --- a/android/snapshot-helper/src/test/java/com/callstack/agentdevice/snapshothelper/PressSequenceTest.java +++ b/android/snapshot-helper/src/test/java/com/callstack/agentdevice/snapshothelper/PressSequenceTest.java @@ -19,6 +19,8 @@ static void run() { doesNotCancelARefusedEnd(); doesNotRetryWhenInjectionAlreadyWaited(); reportsAnUnavailableAnimationSyncApartFromARefusal(); + failsATimedOutKeyStartWithATimeoutAndInjectsNoEnd(); + failsARefusedEndAsARefusal(); } private static void endsWithTheStartDownTime() { @@ -83,6 +85,34 @@ private static void reportsAnUnavailableAnimationSyncApartFromARefusal() { expect(injections.calls, "start 1", "discard 1", "sync"); } + private static void failsATimedOutKeyStartWithATimeoutAndInjectsNoEnd() { + FakeInjections injections = new FakeInjections( + PressSequence.AnimationSync.WAITED, refusedAfter(TIMEOUT), accepted()); + expectFailure(injections, java.util.concurrent.TimeoutException.class, + "Key injection timed out before the app finished handling it; it was cancelled"); + expect(injections.calls, "start 1", "cancel 1"); + } + + private static void failsARefusedEndAsARefusal() { + FakeInjections injections = new FakeInjections(PressSequence.AnimationSync.WAITED, accepted()); + injections.endAccepted = false; + expectFailure(injections, IllegalStateException.class, "Key injection was refused"); + expect(injections.calls, "start 1", "end 1"); + } + + private static void expectFailure( + FakeInjections injections, Class type, String message) { + try { + PressSequence.requireDispatched(PressSequence.run(injections), "Key"); + } catch (Exception error) { + if (error.getClass() != type || !message.equals(error.getMessage())) { + throw new AssertionError("Expected " + type.getName() + ": " + message + ", got " + error); + } + return; + } + throw new AssertionError("Expected " + type.getName() + ", but the press dispatched"); + } + /** A start the framework accepts. */ private static long accepted() { return -1; From b18555db0f43085e6a90620560169bc407c9d17c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Pierzcha=C5=82a?= Date: Mon, 16 Nov 2026 22:03:42 +0100 Subject: [PATCH 07/16] refactor(android): share one touch event builder and keep typed press outcomes - Taps and touch plans build their finger events through TouchEvents. - A dropped start on a runtime that injects through the public overload fails at once, without a discard, as it did before this change. - A discard or cancel that throws no longer replaces the press outcome. Co-Authored-By: Claude Opus 5.5 --- .../AutomationCommandHandler.java | 26 ++++-- .../snapshothelper/PressSequence.java | 62 +++++++------- .../snapshothelper/TouchEvents.java | 31 +++++++ .../snapshothelper/TouchPlanInjector.java | 32 +------- .../UiAutomationInputDispatcher.java | 38 ++------- .../snapshothelper/PressSequenceTest.java | 82 ++++++++++++------- 6 files changed, 147 insertions(+), 124 deletions(-) create mode 100644 android/snapshot-helper/src/main/java/com/callstack/agentdevice/snapshothelper/TouchEvents.java diff --git a/android/snapshot-helper/src/main/java/com/callstack/agentdevice/snapshothelper/AutomationCommandHandler.java b/android/snapshot-helper/src/main/java/com/callstack/agentdevice/snapshothelper/AutomationCommandHandler.java index d28c606d89..b621705826 100644 --- a/android/snapshot-helper/src/main/java/com/callstack/agentdevice/snapshothelper/AutomationCommandHandler.java +++ b/android/snapshot-helper/src/main/java/com/callstack/agentdevice/snapshothelper/AutomationCommandHandler.java @@ -104,7 +104,12 @@ public long now() { } @Override - public PressSequence.AnimationSync awaitAnimations() { + public boolean skipsAnimationWait() { + return UiAutomationInputDispatcher.skipsAnimationWait(); + } + + @Override + public boolean awaitAnimations() { return UiAutomationInputDispatcher.awaitAnimations(automation); } } @@ -129,21 +134,30 @@ public boolean end(long downTime) { return inject(MotionEvent.ACTION_UP, downTime); } + /** The verifier records a dropped DOWN as a pointer that is still down; no window receives it. */ @Override public void discardDroppedStart(long downTime) { - UiAutomationInputDispatcher.cancelDown(automation, downTime, x, y); + cancel(downTime); } @Override public void cancelStart(long downTime) { - UiAutomationInputDispatcher.cancelDown(automation, downTime, x, y); + cancel(downTime); + } + + private void cancel(long downTime) { + inject(MotionEvent.ACTION_CANCEL, downTime, MotionEvent.FLAG_CANCELED, false); } private boolean inject(int action, long downTime) { - MotionEvent event = MotionEvent.obtain(downTime, SystemClock.uptimeMillis(), action, x, y, 0); - event.setSource(InputDevice.SOURCE_TOUCHSCREEN); + return inject(action, downTime, 0, true); + } + + private boolean inject(int action, long downTime, int flags, boolean waitForDispatch) { + MotionEvent event = TouchEvents.obtain( + downTime, SystemClock.uptimeMillis(), action, new float[] {x}, new float[] {y}, flags); try { - return UiAutomationInputDispatcher.inject(automation, event, true); + return UiAutomationInputDispatcher.inject(automation, event, waitForDispatch); } finally { event.recycle(); } diff --git a/android/snapshot-helper/src/main/java/com/callstack/agentdevice/snapshothelper/PressSequence.java b/android/snapshot-helper/src/main/java/com/callstack/agentdevice/snapshothelper/PressSequence.java index 39908db833..bedd8ff845 100644 --- a/android/snapshot-helper/src/main/java/com/callstack/agentdevice/snapshothelper/PressSequence.java +++ b/android/snapshot-helper/src/main/java/com/callstack/agentdevice/snapshothelper/PressSequence.java @@ -5,10 +5,11 @@ /** * Sequences one tap or key press: a start event, then an end event with the same down time. * - *

A dropped start reached no window, as while a transition leaves nothing able to receive it. - * The injection verifier still records it as landed, so the start is discarded before it is retried - * once after the framework's animation sync, with a fresh down time so the receiver does not measure - * the wait as a held press. + *

Without the framework's animation wait, a start can be dropped because a transition leaves + * nothing able to receive it. The injection verifier still records it as landed, so the start is + * discarded before it is retried once after the framework's animation sync, with a fresh down time + * so the receiver does not measure the wait as a held press. A runtime that injects through the + * public overload already waited, so its dropped start fails at once, as it always did. */ final class PressSequence { /** @@ -22,7 +23,9 @@ final class PressSequence { interface Injections { long now(); - /** Injects the start event, without the framework's animation wait when the runtime allows. */ + /** Whether {@link #start} skips the framework's animation wait on this runtime. */ + boolean skipsAnimationWait(); + boolean start(long downTime); boolean end(long downTime); @@ -33,15 +36,8 @@ interface Injections { /** Releases a start that has this down time and that the receiver may hold. */ void cancelStart(long downTime); - AnimationSync awaitAnimations(); - } - - enum AnimationSync { - WAITED, - /** The runtime injects through the public overload, which waited for animations already. */ - ALREADY_WAITED, - /** The runtime skips the animation wait on injection but cannot wait separately. */ - UNAVAILABLE, + /** Waits for the framework's animation sync; false when the runtime cannot wait separately. */ + boolean awaitAnimations(); } enum Outcome { @@ -64,16 +60,14 @@ static Outcome run(Injections injections) { long downTime = injections.now(); Start start = start(injections, downTime); if (start == Start.DROPPED) { - switch (injections.awaitAnimations()) { - case ALREADY_WAITED: - return Outcome.START_REFUSED; - case UNAVAILABLE: - return Outcome.ANIMATION_SYNC_UNAVAILABLE; - case WAITED: - downTime = injections.now(); - start = start(injections, downTime); - if (start == Start.DROPPED) return Outcome.START_REFUSED; - break; + if (!injections.skipsAnimationWait()) return Outcome.START_REFUSED; + discardDroppedStart(injections, downTime); + if (!injections.awaitAnimations()) return Outcome.ANIMATION_SYNC_UNAVAILABLE; + downTime = injections.now(); + start = start(injections, downTime); + if (start == Start.DROPPED) { + discardDroppedStart(injections, downTime); + return Outcome.START_REFUSED; } } if (start == Start.TIMED_OUT) return Outcome.START_TIMED_OUT; @@ -86,8 +80,7 @@ static Outcome run(Injections injections) { } /** Turns an outcome into the helper's response: a typed exception for each failed press. */ - static void requireDispatched(Outcome outcome, String kind) - throws TimeoutException { + static void requireDispatched(Outcome outcome, String kind) throws TimeoutException { switch (outcome) { case DISPATCHED: return; @@ -109,11 +102,20 @@ static void requireDispatched(Outcome outcome, String kind) */ private static Start start(Injections injections, long downTime) { if (injections.start(downTime)) return Start.ACCEPTED; - if (injections.now() - downTime >= INJECTION_TIMEOUT_MS) { + if (injections.now() - downTime < INJECTION_TIMEOUT_MS) return Start.DROPPED; + try { injections.cancelStart(downTime); - return Start.TIMED_OUT; + } catch (RuntimeException ignored) { + // The timed-out start stays the reported failure. + } + return Start.TIMED_OUT; + } + + private static void discardDroppedStart(Injections injections, long downTime) { + try { + injections.discardDroppedStart(downTime); + } catch (RuntimeException ignored) { + // The verifier keeps the dropped start; the press continues with its own outcome. } - injections.discardDroppedStart(downTime); - return Start.DROPPED; } } diff --git a/android/snapshot-helper/src/main/java/com/callstack/agentdevice/snapshothelper/TouchEvents.java b/android/snapshot-helper/src/main/java/com/callstack/agentdevice/snapshothelper/TouchEvents.java new file mode 100644 index 0000000000..d1630cbe3d --- /dev/null +++ b/android/snapshot-helper/src/main/java/com/callstack/agentdevice/snapshothelper/TouchEvents.java @@ -0,0 +1,31 @@ +package com.callstack.agentdevice.snapshothelper; + +import android.view.InputDevice; +import android.view.MotionEvent; + +/** Builds the touchscreen finger events that taps and touch plans inject. */ +final class TouchEvents { + private TouchEvents() {} + + /** One pointer per coordinate pair; pointer ids follow the array order. */ + static MotionEvent obtain( + long downTime, long eventTime, int action, float[] x, float[] y, int flags) { + int count = x.length; + MotionEvent.PointerProperties[] properties = new MotionEvent.PointerProperties[count]; + MotionEvent.PointerCoords[] coords = new MotionEvent.PointerCoords[count]; + for (int index = 0; index < count; index += 1) { + properties[index] = new MotionEvent.PointerProperties(); + properties[index].id = index; + properties[index].toolType = MotionEvent.TOOL_TYPE_FINGER; + coords[index] = new MotionEvent.PointerCoords(); + coords[index].x = x[index]; + coords[index].y = y[index]; + coords[index].pressure = 1.0f; + coords[index].size = 1.0f; + } + MotionEvent event = MotionEvent.obtain(downTime, eventTime, action, count, properties, coords, + 0, 0, 1.0f, 1.0f, 0, 0, InputDevice.SOURCE_TOUCHSCREEN, flags); + event.setSource(InputDevice.SOURCE_TOUCHSCREEN); + return event; + } +} diff --git a/android/snapshot-helper/src/main/java/com/callstack/agentdevice/snapshothelper/TouchPlanInjector.java b/android/snapshot-helper/src/main/java/com/callstack/agentdevice/snapshothelper/TouchPlanInjector.java index 315205cb7a..9bb0f80f1b 100644 --- a/android/snapshot-helper/src/main/java/com/callstack/agentdevice/snapshothelper/TouchPlanInjector.java +++ b/android/snapshot-helper/src/main/java/com/callstack/agentdevice/snapshothelper/TouchPlanInjector.java @@ -104,37 +104,7 @@ private static void injectCancel( private static MotionEvent motionEvent( long downTime, long eventTime, int action, PointerState pointers) { - MotionEvent.PointerProperties[] properties = - new MotionEvent.PointerProperties[pointers.count]; - MotionEvent.PointerCoords[] coords = new MotionEvent.PointerCoords[pointers.count]; - for (int index = 0; index < pointers.count; index += 1) { - properties[index] = new MotionEvent.PointerProperties(); - properties[index].id = index; - properties[index].toolType = MotionEvent.TOOL_TYPE_FINGER; - coords[index] = new MotionEvent.PointerCoords(); - coords[index].x = pointers.x[index]; - coords[index].y = pointers.y[index]; - coords[index].pressure = 1.0f; - coords[index].size = 1.0f; - } - MotionEvent event = - MotionEvent.obtain( - downTime, - eventTime, - action, - pointers.count, - properties, - coords, - 0, - 0, - 1.0f, - 1.0f, - 0, - 0, - InputDevice.SOURCE_TOUCHSCREEN, - 0); - event.setSource(InputDevice.SOURCE_TOUCHSCREEN); - return event; + return TouchEvents.obtain(downTime, eventTime, action, pointers.x, pointers.y, 0); } private static final class PointerState { diff --git a/android/snapshot-helper/src/main/java/com/callstack/agentdevice/snapshothelper/UiAutomationInputDispatcher.java b/android/snapshot-helper/src/main/java/com/callstack/agentdevice/snapshothelper/UiAutomationInputDispatcher.java index 518bbb5e58..104413cfe2 100644 --- a/android/snapshot-helper/src/main/java/com/callstack/agentdevice/snapshothelper/UiAutomationInputDispatcher.java +++ b/android/snapshot-helper/src/main/java/com/callstack/agentdevice/snapshothelper/UiAutomationInputDispatcher.java @@ -1,10 +1,7 @@ package com.callstack.agentdevice.snapshothelper; import android.app.UiAutomation; -import android.os.SystemClock; -import android.view.InputDevice; import android.view.InputEvent; -import android.view.MotionEvent; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; @@ -37,41 +34,24 @@ static boolean inject(UiAutomation automation, InputEvent event, boolean waitFor } } - /** - * Ends a refused touch DOWN with an ACTION_CANCEL. The verifier records a dropped DOWN as a pointer - * that is still down, and a window that received a timed-out DOWN holds the pointer. - */ - static void cancelDown(UiAutomation automation, long downTime, float x, float y) { - MotionEvent.PointerProperties[] properties = {new MotionEvent.PointerProperties()}; - properties[0].id = 0; - MotionEvent.PointerCoords[] coords = {new MotionEvent.PointerCoords()}; - coords[0].x = x; - coords[0].y = y; - MotionEvent cancel = MotionEvent.obtain(downTime, SystemClock.uptimeMillis(), - MotionEvent.ACTION_CANCEL, 1, properties, coords, 0, 0, 1f, 1f, 0, 0, - InputDevice.SOURCE_TOUCHSCREEN, MotionEvent.FLAG_CANCELED); - try { - inject(automation, cancel, false); - } finally { - cancel.recycle(); - } + /** Whether injection skips the framework's animation wait on this runtime. */ + static boolean skipsAnimationWait() { + return injectWithoutAnimationWait != null; } /** - * Waits for the framework's animation sync before a refused start is retried. The sync is - * {@code ALREADY_WAITED} when injection runs through the public overload, and {@code UNAVAILABLE} - * when injection skips the wait but the runtime does not expose or permit the separate sync. + * Waits for the framework's animation sync. Returns false when the runtime does not expose or + * permit the separate sync. */ - static PressSequence.AnimationSync awaitAnimations(UiAutomation automation) { - if (injectWithoutAnimationWait == null) return PressSequence.AnimationSync.ALREADY_WAITED; + static boolean awaitAnimations(UiAutomation automation) { Method method = syncInputTransactions; - if (method == null) return PressSequence.AnimationSync.UNAVAILABLE; + if (method == null) return false; try { method.invoke(automation); - return PressSequence.AnimationSync.WAITED; + return true; } catch (IllegalAccessException | IllegalArgumentException error) { syncInputTransactions = null; - return PressSequence.AnimationSync.UNAVAILABLE; + return false; } catch (InvocationTargetException error) { rethrowCause(error.getCause()); throw new AssertionError("Unreachable"); diff --git a/android/snapshot-helper/src/test/java/com/callstack/agentdevice/snapshothelper/PressSequenceTest.java b/android/snapshot-helper/src/test/java/com/callstack/agentdevice/snapshothelper/PressSequenceTest.java index 5af3f32be9..f23b1fd976 100644 --- a/android/snapshot-helper/src/test/java/com/callstack/agentdevice/snapshothelper/PressSequenceTest.java +++ b/android/snapshot-helper/src/test/java/com/callstack/agentdevice/snapshothelper/PressSequenceTest.java @@ -3,6 +3,7 @@ import java.util.ArrayList; import java.util.Arrays; import java.util.List; +import java.util.concurrent.TimeoutException; public final class PressSequenceTest { private static final long TIMEOUT = PressSequence.INJECTION_TIMEOUT_MS; @@ -17,84 +18,97 @@ static void run() { discardsAStartDroppedAgainAfterTheSync(); cancelsARetriedStartThatTimesOut(); doesNotCancelARefusedEnd(); - doesNotRetryWhenInjectionAlreadyWaited(); + failsADroppedStartAtOnceWhenInjectionWaitsForAnimations(); reportsAnUnavailableAnimationSyncApartFromARefusal(); + keepsTheTimeoutWhenItsCancelThrows(); + retriesADroppedStartWhenItsDiscardThrows(); failsATimedOutKeyStartWithATimeoutAndInjectsNoEnd(); failsARefusedEndAsARefusal(); } private static void endsWithTheStartDownTime() { - FakeInjections injections = new FakeInjections(PressSequence.AnimationSync.WAITED, accepted()); + FakeInjections injections = FakeInjections.skippingTheWait(accepted()); expect(PressSequence.run(injections), PressSequence.Outcome.DISPATCHED); expect(injections.calls, "start 1", "end 1"); } private static void retriesADroppedStartAfterTheAnimationSyncWithAFreshDownTime() { - FakeInjections injections = - new FakeInjections(PressSequence.AnimationSync.WAITED, refusedAfter(0), accepted()); + FakeInjections injections = FakeInjections.skippingTheWait(refusedAfter(0), accepted()); expect(PressSequence.run(injections), PressSequence.Outcome.DISPATCHED); expect(injections.calls, "start 1", "discard 1", "sync", "start 3", "end 3"); } private static void retriesAStartRefusedJustBeforeTheInjectionDeadline() { - FakeInjections injections = new FakeInjections( - PressSequence.AnimationSync.WAITED, refusedAfter(TIMEOUT - 2), accepted()); + FakeInjections injections = + FakeInjections.skippingTheWait(refusedAfter(TIMEOUT - 2), accepted()); expect(PressSequence.run(injections), PressSequence.Outcome.DISPATCHED); expect(injections.calls, "start 1", "discard 1", "sync", "start 30001", "end 30001"); } private static void cancelsAndDoesNotRetryAStartRefusedAtTheInjectionDeadline() { - FakeInjections injections = new FakeInjections( - PressSequence.AnimationSync.WAITED, refusedAfter(TIMEOUT - 1), accepted()); + FakeInjections injections = + FakeInjections.skippingTheWait(refusedAfter(TIMEOUT - 1), accepted()); expect(PressSequence.run(injections), PressSequence.Outcome.START_TIMED_OUT); expect(injections.calls, "start 1", "cancel 1"); } private static void discardsAStartDroppedAgainAfterTheSync() { - FakeInjections injections = - new FakeInjections(PressSequence.AnimationSync.WAITED, refusedAfter(0), refusedAfter(0)); + FakeInjections injections = FakeInjections.skippingTheWait(refusedAfter(0), refusedAfter(0)); expect(PressSequence.run(injections), PressSequence.Outcome.START_REFUSED); expect(injections.calls, "start 1", "discard 1", "sync", "start 3", "discard 3"); } private static void cancelsARetriedStartThatTimesOut() { - FakeInjections injections = new FakeInjections( - PressSequence.AnimationSync.WAITED, refusedAfter(0), refusedAfter(TIMEOUT)); + FakeInjections injections = + FakeInjections.skippingTheWait(refusedAfter(0), refusedAfter(TIMEOUT)); expect(PressSequence.run(injections), PressSequence.Outcome.START_TIMED_OUT); expect(injections.calls, "start 1", "discard 1", "sync", "start 3", "cancel 3"); } private static void doesNotCancelARefusedEnd() { - FakeInjections injections = new FakeInjections(PressSequence.AnimationSync.WAITED, accepted()); + FakeInjections injections = FakeInjections.skippingTheWait(accepted()); injections.endAccepted = false; expect(PressSequence.run(injections), PressSequence.Outcome.END_REFUSED); expect(injections.calls, "start 1", "end 1"); } - private static void doesNotRetryWhenInjectionAlreadyWaited() { - FakeInjections injections = new FakeInjections( - PressSequence.AnimationSync.ALREADY_WAITED, refusedAfter(0), accepted()); + private static void failsADroppedStartAtOnceWhenInjectionWaitsForAnimations() { + FakeInjections injections = FakeInjections.skippingTheWait(refusedAfter(0), accepted()); + injections.skipsAnimationWait = false; expect(PressSequence.run(injections), PressSequence.Outcome.START_REFUSED); - expect(injections.calls, "start 1", "discard 1", "sync"); + expect(injections.calls, "start 1"); } private static void reportsAnUnavailableAnimationSyncApartFromARefusal() { - FakeInjections injections = new FakeInjections( - PressSequence.AnimationSync.UNAVAILABLE, refusedAfter(0), accepted()); + FakeInjections injections = FakeInjections.skippingTheWait(refusedAfter(0), accepted()); + injections.syncAvailable = false; expect(PressSequence.run(injections), PressSequence.Outcome.ANIMATION_SYNC_UNAVAILABLE); expect(injections.calls, "start 1", "discard 1", "sync"); } + private static void keepsTheTimeoutWhenItsCancelThrows() { + FakeInjections injections = FakeInjections.skippingTheWait(refusedAfter(TIMEOUT), accepted()); + injections.releaseThrows = true; + expect(PressSequence.run(injections), PressSequence.Outcome.START_TIMED_OUT); + expect(injections.calls, "start 1", "cancel 1"); + } + + private static void retriesADroppedStartWhenItsDiscardThrows() { + FakeInjections injections = FakeInjections.skippingTheWait(refusedAfter(0), accepted()); + injections.releaseThrows = true; + expect(PressSequence.run(injections), PressSequence.Outcome.DISPATCHED); + expect(injections.calls, "start 1", "discard 1", "sync", "start 3", "end 3"); + } + private static void failsATimedOutKeyStartWithATimeoutAndInjectsNoEnd() { - FakeInjections injections = new FakeInjections( - PressSequence.AnimationSync.WAITED, refusedAfter(TIMEOUT), accepted()); - expectFailure(injections, java.util.concurrent.TimeoutException.class, + FakeInjections injections = FakeInjections.skippingTheWait(refusedAfter(TIMEOUT), accepted()); + expectFailure(injections, TimeoutException.class, "Key injection timed out before the app finished handling it; it was cancelled"); expect(injections.calls, "start 1", "cancel 1"); } private static void failsARefusedEndAsARefusal() { - FakeInjections injections = new FakeInjections(PressSequence.AnimationSync.WAITED, accepted()); + FakeInjections injections = FakeInjections.skippingTheWait(accepted()); injections.endAccepted = false; expectFailure(injections, IllegalStateException.class, "Key injection was refused"); expect(injections.calls, "start 1", "end 1"); @@ -136,22 +150,32 @@ private static void expect(List actual, String... expected) { /** Each {@code now()} advances the clock by 1 ms; a refused start also advances it by its delay. */ private static final class FakeInjections implements PressSequence.Injections { final List calls = new ArrayList<>(); - private final PressSequence.AnimationSync sync; private final long[] starts; private int startIndex; private long clock; + boolean skipsAnimationWait = true; + boolean syncAvailable = true; boolean endAccepted = true; + boolean releaseThrows; - FakeInjections(PressSequence.AnimationSync sync, long... starts) { - this.sync = sync; + private FakeInjections(long[] starts) { this.starts = starts; } + static FakeInjections skippingTheWait(long... starts) { + return new FakeInjections(starts); + } + @Override public long now() { return ++clock; } + @Override + public boolean skipsAnimationWait() { + return skipsAnimationWait; + } + @Override public boolean start(long downTime) { calls.add("start " + downTime); @@ -170,17 +194,19 @@ public boolean end(long downTime) { @Override public void discardDroppedStart(long downTime) { calls.add("discard " + downTime); + if (releaseThrows) throw new IllegalStateException("discard failed"); } @Override public void cancelStart(long downTime) { calls.add("cancel " + downTime); + if (releaseThrows) throw new IllegalStateException("cancel failed"); } @Override - public PressSequence.AnimationSync awaitAnimations() { + public boolean awaitAnimations() { calls.add("sync"); - return sync; + return syncAvailable; } } } From 3dc041ef817984ea8728bc4de488f81768fd2b35 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Pierzcha=C5=82a?= Date: Mon, 16 Nov 2026 22:23:33 +0100 Subject: [PATCH 08/16] fix(android): bound a press by the host budget and report a refused cancel A synchronous injection can block until the framework's 30 s deadline, so a press that waited it out answered after the host's 30 s input timeout and lost its typed result. The helper now bounds each press by a 40 s budget: the retry of a dropped start runs only when the animation sync and its own deadline fit, and an end that does not fit is injected without waiting for dispatch. The host waits 45 s for helper input; a repo guard keeps that above the press budget. A timed-out start whose cancel is refused now says so in its timeout instead of claiming it was cancelled. Co-Authored-By: Claude Opus 5.5 --- .../AutomationCommandHandler.java | 36 ++--- .../snapshothelper/PressSequence.java | 47 ++++-- .../snapshothelper/PressSequenceTest.java | 141 +++++++++++++----- .../platform-android/src/automation-helper.ts | 4 +- .../android-helper-input-dispatch.test.ts | 25 ++++ 5 files changed, 180 insertions(+), 73 deletions(-) diff --git a/android/snapshot-helper/src/main/java/com/callstack/agentdevice/snapshothelper/AutomationCommandHandler.java b/android/snapshot-helper/src/main/java/com/callstack/agentdevice/snapshothelper/AutomationCommandHandler.java index b621705826..a895cb0536 100644 --- a/android/snapshot-helper/src/main/java/com/callstack/agentdevice/snapshothelper/AutomationCommandHandler.java +++ b/android/snapshot-helper/src/main/java/com/callstack/agentdevice/snapshothelper/AutomationCommandHandler.java @@ -126,31 +126,23 @@ private static final class TouchPress extends Press { @Override public boolean start(long downTime) { - return inject(MotionEvent.ACTION_DOWN, downTime); + return inject(MotionEvent.ACTION_DOWN, downTime, 0, true); } @Override - public boolean end(long downTime) { - return inject(MotionEvent.ACTION_UP, downTime); + public boolean end(long downTime, boolean waitForDispatch) { + return inject(MotionEvent.ACTION_UP, downTime, 0, waitForDispatch); } /** The verifier records a dropped DOWN as a pointer that is still down; no window receives it. */ @Override public void discardDroppedStart(long downTime) { - cancel(downTime); + cancelStart(downTime); } @Override - public void cancelStart(long downTime) { - cancel(downTime); - } - - private void cancel(long downTime) { - inject(MotionEvent.ACTION_CANCEL, downTime, MotionEvent.FLAG_CANCELED, false); - } - - private boolean inject(int action, long downTime) { - return inject(action, downTime, 0, true); + public boolean cancelStart(long downTime) { + return inject(MotionEvent.ACTION_CANCEL, downTime, MotionEvent.FLAG_CANCELED, false); } private boolean inject(int action, long downTime, int flags, boolean waitForDispatch) { @@ -174,12 +166,12 @@ private static final class KeyPress extends Press { @Override public boolean start(long downTime) { - return inject(KeyEvent.ACTION_DOWN, downTime); + return inject(KeyEvent.ACTION_DOWN, downTime, 0, true); } @Override - public boolean end(long downTime) { - return inject(KeyEvent.ACTION_UP, downTime); + public boolean end(long downTime, boolean waitForDispatch) { + return inject(KeyEvent.ACTION_UP, downTime, 0, waitForDispatch); } /** The injection verifier checks motion events only, so a dropped key DOWN leaves no state. */ @@ -188,13 +180,13 @@ public void discardDroppedStart(long downTime) {} /** Android releases an interrupted key with an ACTION_UP that carries FLAG_CANCELED. */ @Override - public void cancelStart(long downTime) { - UiAutomationInputDispatcher.inject(automation, event(KeyEvent.ACTION_UP, downTime, - KeyEvent.FLAG_CANCELED), false); + public boolean cancelStart(long downTime) { + return inject(KeyEvent.ACTION_UP, downTime, KeyEvent.FLAG_CANCELED, false); } - private boolean inject(int action, long downTime) { - return UiAutomationInputDispatcher.inject(automation, event(action, downTime, 0), true); + private boolean inject(int action, long downTime, int flags, boolean waitForDispatch) { + return UiAutomationInputDispatcher.inject( + automation, event(action, downTime, flags), waitForDispatch); } private KeyEvent event(int action, long downTime, int flags) { diff --git a/android/snapshot-helper/src/main/java/com/callstack/agentdevice/snapshothelper/PressSequence.java b/android/snapshot-helper/src/main/java/com/callstack/agentdevice/snapshothelper/PressSequence.java index bedd8ff845..57be5c594f 100644 --- a/android/snapshot-helper/src/main/java/com/callstack/agentdevice/snapshothelper/PressSequence.java +++ b/android/snapshot-helper/src/main/java/com/callstack/agentdevice/snapshothelper/PressSequence.java @@ -19,6 +19,17 @@ final class PressSequence { */ static final long INJECTION_TIMEOUT_MS = 30_000; + /** The framework's cap on one animation sync, {@code WindowManagerService.ANIMATION_COMPLETED_TIMEOUT_MS}. */ + static final long ANIMATION_SYNC_TIMEOUT_MS = 5_000; + + /** + * The longest one press may take, so the helper answers before the host's input timeout. A + * synchronous injection can block until {@link #INJECTION_TIMEOUT_MS}, so each one after the first + * starts only when that deadline still fits: the retry of a dropped start is skipped, and an end + * that does not fit is injected without waiting for dispatch. + */ + static final long PRESS_BUDGET_MS = 40_000; + /** Builds and injects the platform events of one press. */ interface Injections { long now(); @@ -28,13 +39,14 @@ interface Injections { boolean start(long downTime); - boolean end(long downTime); + /** Injects the end event; without {@code waitForDispatch} it cannot report a refusal. */ + boolean end(long downTime, boolean waitForDispatch); /** Closes the verifier state of a dropped start that has this down time. */ void discardDroppedStart(long downTime); - /** Releases a start that has this down time and that the receiver may hold. */ - void cancelStart(long downTime); + /** Releases a start that has this down time and that the receiver may hold; false if refused. */ + boolean cancelStart(long downTime); /** Waits for the framework's animation sync; false when the runtime cannot wait separately. */ boolean awaitAnimations(); @@ -44,6 +56,7 @@ enum Outcome { DISPATCHED, START_REFUSED, START_TIMED_OUT, + START_TIMED_OUT_CANCEL_REFUSED, END_REFUSED, ANIMATION_SYNC_UNAVAILABLE, } @@ -52,17 +65,23 @@ private enum Start { ACCEPTED, DROPPED, TIMED_OUT, + TIMED_OUT_CANCEL_REFUSED, } private PressSequence() {} static Outcome run(Injections injections) { - long downTime = injections.now(); + long pressStart = injections.now(); + long downTime = pressStart; Start start = start(injections, downTime); if (start == Start.DROPPED) { if (!injections.skipsAnimationWait()) return Outcome.START_REFUSED; discardDroppedStart(injections, downTime); + if (!fits(injections, pressStart, ANIMATION_SYNC_TIMEOUT_MS + INJECTION_TIMEOUT_MS)) { + return Outcome.START_REFUSED; + } if (!injections.awaitAnimations()) return Outcome.ANIMATION_SYNC_UNAVAILABLE; + if (!fits(injections, pressStart, INJECTION_TIMEOUT_MS)) return Outcome.START_REFUSED; downTime = injections.now(); start = start(injections, downTime); if (start == Start.DROPPED) { @@ -71,12 +90,18 @@ static Outcome run(Injections injections) { } } if (start == Start.TIMED_OUT) return Outcome.START_TIMED_OUT; + if (start == Start.TIMED_OUT_CANCEL_REFUSED) return Outcome.START_TIMED_OUT_CANCEL_REFUSED; // A refused end gets no cancel. The verifier records an event before the dispatcher drops it, // so the press is closed there and a cancel would be inconsistent. The dispatcher drops an end // only when no pointer or focus is still down, after it cancelled the receiving window itself // ("touched window was removed", "focus left window"). A timed-out end was already enqueued, // so the live window still receives it. - return injections.end(downTime) ? Outcome.DISPATCHED : Outcome.END_REFUSED; + boolean waitForDispatch = fits(injections, pressStart, INJECTION_TIMEOUT_MS); + return injections.end(downTime, waitForDispatch) ? Outcome.DISPATCHED : Outcome.END_REFUSED; + } + + private static boolean fits(Injections injections, long pressStart, long waitMs) { + return injections.now() - pressStart + waitMs <= PRESS_BUDGET_MS; } /** Turns an outcome into the helper's response: a typed exception for each failed press. */ @@ -90,6 +115,9 @@ static void requireDispatched(Outcome outcome, String kind) throws TimeoutExcept case START_TIMED_OUT: throw new TimeoutException( kind + " injection timed out before the app finished handling it; it was cancelled"); + case START_TIMED_OUT_CANCEL_REFUSED: + throw new TimeoutException(kind + " injection timed out before the app finished handling" + + " it, and its cancel was refused, so the app may still hold it"); case START_REFUSED: case END_REFUSED: throw new IllegalStateException(kind + " injection was refused"); @@ -103,12 +131,13 @@ static void requireDispatched(Outcome outcome, String kind) throws TimeoutExcept private static Start start(Injections injections, long downTime) { if (injections.start(downTime)) return Start.ACCEPTED; if (injections.now() - downTime < INJECTION_TIMEOUT_MS) return Start.DROPPED; + boolean cancelled; try { - injections.cancelStart(downTime); - } catch (RuntimeException ignored) { - // The timed-out start stays the reported failure. + cancelled = injections.cancelStart(downTime); + } catch (RuntimeException error) { + cancelled = false; } - return Start.TIMED_OUT; + return cancelled ? Start.TIMED_OUT : Start.TIMED_OUT_CANCEL_REFUSED; } private static void discardDroppedStart(Injections injections, long downTime) { diff --git a/android/snapshot-helper/src/test/java/com/callstack/agentdevice/snapshothelper/PressSequenceTest.java b/android/snapshot-helper/src/test/java/com/callstack/agentdevice/snapshothelper/PressSequenceTest.java index f23b1fd976..b0ced68b94 100644 --- a/android/snapshot-helper/src/test/java/com/callstack/agentdevice/snapshothelper/PressSequenceTest.java +++ b/android/snapshot-helper/src/test/java/com/callstack/agentdevice/snapshothelper/PressSequenceTest.java @@ -13,102 +13,153 @@ private PressSequenceTest() {} static void run() { endsWithTheStartDownTime(); retriesADroppedStartAfterTheAnimationSyncWithAFreshDownTime(); - retriesAStartRefusedJustBeforeTheInjectionDeadline(); + retriesADropThatLeavesRoomForTheSyncAndTheRetryDeadline(); + skipsTheRetryOfADropThatLeavesNoRoomForTheSyncAndTheRetryDeadline(); + skipsTheRetryWhenTheSyncLeavesNoRoomForTheRetryDeadline(); + discardsAStartRefusedJustBeforeTheInjectionDeadlineAsADrop(); cancelsAndDoesNotRetryAStartRefusedAtTheInjectionDeadline(); discardsAStartDroppedAgainAfterTheSync(); cancelsARetriedStartThatTimesOut(); + waitsForTheEndWhenItsDeadlineFits(); + injectsTheEndWithoutWaitingWhenItsDeadlineDoesNotFit(); doesNotCancelARefusedEnd(); failsADroppedStartAtOnceWhenInjectionWaitsForAnimations(); reportsAnUnavailableAnimationSyncApartFromARefusal(); - keepsTheTimeoutWhenItsCancelThrows(); + reportsARefusedCancelOfATimedOutStart(); + treatsAThrowingCancelAsRefused(); retriesADroppedStartWhenItsDiscardThrows(); failsATimedOutKeyStartWithATimeoutAndInjectsNoEnd(); + namesARefusedCancelInTheTimeout(); failsARefusedEndAsARefusal(); } private static void endsWithTheStartDownTime() { - FakeInjections injections = FakeInjections.skippingTheWait(accepted()); + FakeInjections injections = new FakeInjections(accepted()); expect(PressSequence.run(injections), PressSequence.Outcome.DISPATCHED); expect(injections.calls, "start 1", "end 1"); } private static void retriesADroppedStartAfterTheAnimationSyncWithAFreshDownTime() { - FakeInjections injections = FakeInjections.skippingTheWait(refusedAfter(0), accepted()); + FakeInjections injections = new FakeInjections(refusedAfter(0), accepted()); expect(PressSequence.run(injections), PressSequence.Outcome.DISPATCHED); - expect(injections.calls, "start 1", "discard 1", "sync", "start 3", "end 3"); + expect(injections.calls, "start 1", "discard 1", "sync", "start 5", "end 5"); } - private static void retriesAStartRefusedJustBeforeTheInjectionDeadline() { - FakeInjections injections = - FakeInjections.skippingTheWait(refusedAfter(TIMEOUT - 2), accepted()); + private static void retriesADropThatLeavesRoomForTheSyncAndTheRetryDeadline() { + FakeInjections injections = new FakeInjections(refusedAfter(4_998), accepted()); expect(PressSequence.run(injections), PressSequence.Outcome.DISPATCHED); - expect(injections.calls, "start 1", "discard 1", "sync", "start 30001", "end 30001"); + expect(injections.calls, "start 1", "discard 1", "sync", "start 5003", "end 5003"); + } + + private static void skipsTheRetryOfADropThatLeavesNoRoomForTheSyncAndTheRetryDeadline() { + FakeInjections injections = new FakeInjections(refusedAfter(4_999), accepted()); + expect(PressSequence.run(injections), PressSequence.Outcome.START_REFUSED); + expect(injections.calls, "start 1", "discard 1"); + } + + private static void skipsTheRetryWhenTheSyncLeavesNoRoomForTheRetryDeadline() { + FakeInjections injections = new FakeInjections(refusedAfter(0), accepted()); + injections.syncMs = 11_000; + expect(PressSequence.run(injections), PressSequence.Outcome.START_REFUSED); + expect(injections.calls, "start 1", "discard 1", "sync"); + } + + private static void discardsAStartRefusedJustBeforeTheInjectionDeadlineAsADrop() { + FakeInjections injections = new FakeInjections(refusedAfter(TIMEOUT - 2), accepted()); + expect(PressSequence.run(injections), PressSequence.Outcome.START_REFUSED); + expect(injections.calls, "start 1", "discard 1"); } private static void cancelsAndDoesNotRetryAStartRefusedAtTheInjectionDeadline() { - FakeInjections injections = - FakeInjections.skippingTheWait(refusedAfter(TIMEOUT - 1), accepted()); + FakeInjections injections = new FakeInjections(refusedAfter(TIMEOUT - 1), accepted()); expect(PressSequence.run(injections), PressSequence.Outcome.START_TIMED_OUT); expect(injections.calls, "start 1", "cancel 1"); } private static void discardsAStartDroppedAgainAfterTheSync() { - FakeInjections injections = FakeInjections.skippingTheWait(refusedAfter(0), refusedAfter(0)); + FakeInjections injections = new FakeInjections(refusedAfter(0), refusedAfter(0)); expect(PressSequence.run(injections), PressSequence.Outcome.START_REFUSED); - expect(injections.calls, "start 1", "discard 1", "sync", "start 3", "discard 3"); + expect(injections.calls, "start 1", "discard 1", "sync", "start 5", "discard 5"); } private static void cancelsARetriedStartThatTimesOut() { - FakeInjections injections = - FakeInjections.skippingTheWait(refusedAfter(0), refusedAfter(TIMEOUT)); + FakeInjections injections = new FakeInjections(refusedAfter(0), refusedAfter(TIMEOUT)); expect(PressSequence.run(injections), PressSequence.Outcome.START_TIMED_OUT); - expect(injections.calls, "start 1", "discard 1", "sync", "start 3", "cancel 3"); + expect(injections.calls, "start 1", "discard 1", "sync", "start 5", "cancel 5"); + } + + private static void waitsForTheEndWhenItsDeadlineFits() { + FakeInjections injections = new FakeInjections(acceptedAfter(9_999)); + expect(PressSequence.run(injections), PressSequence.Outcome.DISPATCHED); + expect(injections.calls, "start 1", "end 1"); + } + + private static void injectsTheEndWithoutWaitingWhenItsDeadlineDoesNotFit() { + FakeInjections injections = new FakeInjections(acceptedAfter(10_000)); + expect(PressSequence.run(injections), PressSequence.Outcome.DISPATCHED); + expect(injections.calls, "start 1", "end-async 1"); } private static void doesNotCancelARefusedEnd() { - FakeInjections injections = FakeInjections.skippingTheWait(accepted()); + FakeInjections injections = new FakeInjections(accepted()); injections.endAccepted = false; expect(PressSequence.run(injections), PressSequence.Outcome.END_REFUSED); expect(injections.calls, "start 1", "end 1"); } private static void failsADroppedStartAtOnceWhenInjectionWaitsForAnimations() { - FakeInjections injections = FakeInjections.skippingTheWait(refusedAfter(0), accepted()); + FakeInjections injections = new FakeInjections(refusedAfter(0), accepted()); injections.skipsAnimationWait = false; expect(PressSequence.run(injections), PressSequence.Outcome.START_REFUSED); expect(injections.calls, "start 1"); } private static void reportsAnUnavailableAnimationSyncApartFromARefusal() { - FakeInjections injections = FakeInjections.skippingTheWait(refusedAfter(0), accepted()); + FakeInjections injections = new FakeInjections(refusedAfter(0), accepted()); injections.syncAvailable = false; expect(PressSequence.run(injections), PressSequence.Outcome.ANIMATION_SYNC_UNAVAILABLE); expect(injections.calls, "start 1", "discard 1", "sync"); } - private static void keepsTheTimeoutWhenItsCancelThrows() { - FakeInjections injections = FakeInjections.skippingTheWait(refusedAfter(TIMEOUT), accepted()); + private static void reportsARefusedCancelOfATimedOutStart() { + FakeInjections injections = new FakeInjections(refusedAfter(TIMEOUT), accepted()); + injections.cancelAccepted = false; + expect(PressSequence.run(injections), PressSequence.Outcome.START_TIMED_OUT_CANCEL_REFUSED); + expect(injections.calls, "start 1", "cancel 1"); + } + + private static void treatsAThrowingCancelAsRefused() { + FakeInjections injections = new FakeInjections(refusedAfter(TIMEOUT), accepted()); injections.releaseThrows = true; - expect(PressSequence.run(injections), PressSequence.Outcome.START_TIMED_OUT); + expect(PressSequence.run(injections), PressSequence.Outcome.START_TIMED_OUT_CANCEL_REFUSED); expect(injections.calls, "start 1", "cancel 1"); } private static void retriesADroppedStartWhenItsDiscardThrows() { - FakeInjections injections = FakeInjections.skippingTheWait(refusedAfter(0), accepted()); + FakeInjections injections = new FakeInjections(refusedAfter(0), accepted()); injections.releaseThrows = true; expect(PressSequence.run(injections), PressSequence.Outcome.DISPATCHED); - expect(injections.calls, "start 1", "discard 1", "sync", "start 3", "end 3"); + expect(injections.calls, "start 1", "discard 1", "sync", "start 5", "end 5"); } private static void failsATimedOutKeyStartWithATimeoutAndInjectsNoEnd() { - FakeInjections injections = FakeInjections.skippingTheWait(refusedAfter(TIMEOUT), accepted()); + FakeInjections injections = new FakeInjections(refusedAfter(TIMEOUT), accepted()); expectFailure(injections, TimeoutException.class, "Key injection timed out before the app finished handling it; it was cancelled"); expect(injections.calls, "start 1", "cancel 1"); } + private static void namesARefusedCancelInTheTimeout() { + FakeInjections injections = new FakeInjections(refusedAfter(TIMEOUT), accepted()); + injections.cancelAccepted = false; + expectFailure(injections, TimeoutException.class, + "Key injection timed out before the app finished handling it, and its cancel was refused," + + " so the app may still hold it"); + expect(injections.calls, "start 1", "cancel 1"); + } + private static void failsARefusedEndAsARefusal() { - FakeInjections injections = FakeInjections.skippingTheWait(accepted()); + FakeInjections injections = new FakeInjections(accepted()); injections.endAccepted = false; expectFailure(injections, IllegalStateException.class, "Key injection was refused"); expect(injections.calls, "start 1", "end 1"); @@ -127,9 +178,14 @@ private static void expectFailure( throw new AssertionError("Expected " + type.getName() + ", but the press dispatched"); } - /** A start the framework accepts. */ + /** A start the framework accepts at once. */ private static long accepted() { - return -1; + return acceptedAfter(0); + } + + /** A start the framework accepts after this many milliseconds. */ + private static long acceptedAfter(long elapsedMs) { + return -elapsedMs - 1; } /** A start the framework refuses after this many milliseconds. */ @@ -147,7 +203,10 @@ private static void expect(List actual, String... expected) { } } - /** Each {@code now()} advances the clock by 1 ms; a refused start also advances it by its delay. */ + /** + * Each {@code now()} advances the clock by 1 ms. A start advances it by its delay, and the sync by + * {@code syncMs}. + */ private static final class FakeInjections implements PressSequence.Injections { final List calls = new ArrayList<>(); private final long[] starts; @@ -155,17 +214,15 @@ private static final class FakeInjections implements PressSequence.Injections { private long clock; boolean skipsAnimationWait = true; boolean syncAvailable = true; + long syncMs; boolean endAccepted = true; + boolean cancelAccepted = true; boolean releaseThrows; - private FakeInjections(long[] starts) { + FakeInjections(long... starts) { this.starts = starts; } - static FakeInjections skippingTheWait(long... starts) { - return new FakeInjections(starts); - } - @Override public long now() { return ++clock; @@ -179,15 +236,15 @@ public boolean skipsAnimationWait() { @Override public boolean start(long downTime) { calls.add("start " + downTime); - long refusedAfterMs = starts[startIndex++]; - if (refusedAfterMs < 0) return true; - clock += refusedAfterMs; - return false; + long spec = starts[startIndex++]; + boolean accepted = spec < 0; + clock += accepted ? -spec - 1 : spec; + return accepted; } @Override - public boolean end(long downTime) { - calls.add("end " + downTime); + public boolean end(long downTime, boolean waitForDispatch) { + calls.add((waitForDispatch ? "end " : "end-async ") + downTime); return endAccepted; } @@ -198,14 +255,16 @@ public void discardDroppedStart(long downTime) { } @Override - public void cancelStart(long downTime) { + public boolean cancelStart(long downTime) { calls.add("cancel " + downTime); if (releaseThrows) throw new IllegalStateException("cancel failed"); + return cancelAccepted; } @Override public boolean awaitAnimations() { calls.add("sync"); + clock += syncMs; return syncAvailable; } } diff --git a/packages/platform-android/src/automation-helper.ts b/packages/platform-android/src/automation-helper.ts index 10040b1cd1..df96aff5e1 100644 --- a/packages/platform-android/src/automation-helper.ts +++ b/packages/platform-android/src/automation-helper.ts @@ -38,7 +38,9 @@ type AndroidHelperAppResult = | { kind: 'launched'; output: string }; const ACTION_POLICY = { - input: { mutation: true, timeoutMs: 30_000 }, + // Above the helper's press budget (PressSequence.PRESS_BUDGET_MS), so the helper's typed answer + // for a press that waited out the framework's injection deadline arrives before this timeout. + input: { mutation: true, timeoutMs: 45_000 }, app: { mutation: true, timeoutMs: 30_000 }, screenshot: { mutation: false, timeoutMs: 30_000 }, focus: { mutation: false, timeoutMs: 2_000 }, diff --git a/test/repo-guards/android-helper-input-dispatch.test.ts b/test/repo-guards/android-helper-input-dispatch.test.ts index 7327cacbc2..1508d96435 100644 --- a/test/repo-guards/android-helper-input-dispatch.test.ts +++ b/test/repo-guards/android-helper-input-dispatch.test.ts @@ -33,3 +33,28 @@ test('Android helper input reaches the framework only through the dispatcher wit /method\.invoke\(automation, event, waitForDispatch, false\)/, ); }); + +test('the host waits longer for helper input than the helper may spend on one press', () => { + const pressBudgetMs = Number( + fs + .readFileSync( + path.join(helperSourceDir, path.dirname(dispatcherFile), 'PressSequence.java'), + 'utf8', + ) + .match(/static final long PRESS_BUDGET_MS = ([\d_]+);/)?.[1] + ?.replaceAll('_', ''), + ); + const hostInputTimeoutMs = Number( + fs + .readFileSync( + path.join(repoRoot, 'packages/platform-android/src/automation-helper.ts'), + 'utf8', + ) + .match(/input: \{ mutation: true, timeoutMs: ([\d_]+) \}/)?.[1] + ?.replaceAll('_', ''), + ); + assert.ok(pressBudgetMs > 0, 'PressSequence.PRESS_BUDGET_MS must be readable'); + assert.ok(hostInputTimeoutMs > 0, 'the host input timeout must be readable'); + // The margin covers the socket round trip and the async cancel after the budget. + assert.ok(hostInputTimeoutMs >= pressBudgetMs + 5_000); +}); From 53dd31c3da4f95115f44289a3b3d66175b239072 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Pierzcha=C5=82a?= Date: Mon, 16 Nov 2026 22:40:53 +0100 Subject: [PATCH 09/16] refactor(android): release every refused press start and bound the retry by the host budget Replaces the timing-based timeout classification, the cancel-result outcomes, the async end injection and the helper's fixed press budget with one rule. Every refused start is released (touch: ACTION_CANCEL, key: a canceled ACTION_UP), which covers both a dropped start and one that timed out after delivery. The start is retried at most once, only when the host's remaining budget, now sent with each input command, still fits the animation sync and the retry's injection deadline. Fill's tap uses the same command deadline. Co-Authored-By: Claude Opus 5.5 --- .../AutomationCommandHandler.java | 59 +++-- .../snapshothelper/PressSequence.java | 110 +++------- .../snapshothelper/PressSequenceTest.java | 203 ++++++------------ .../automation-helper-input-budget.test.ts | 31 +++ .../platform-android/src/automation-helper.ts | 21 +- .../android-helper-input-dispatch.test.ts | 25 --- 6 files changed, 160 insertions(+), 289 deletions(-) create mode 100644 packages/platform-android/src/automation-helper-input-budget.test.ts diff --git a/android/snapshot-helper/src/main/java/com/callstack/agentdevice/snapshothelper/AutomationCommandHandler.java b/android/snapshot-helper/src/main/java/com/callstack/agentdevice/snapshothelper/AutomationCommandHandler.java index a895cb0536..f3f262d0a4 100644 --- a/android/snapshot-helper/src/main/java/com/callstack/agentdevice/snapshothelper/AutomationCommandHandler.java +++ b/android/snapshot-helper/src/main/java/com/callstack/agentdevice/snapshothelper/AutomationCommandHandler.java @@ -18,7 +18,6 @@ import java.io.IOException; import java.io.OutputStream; import java.nio.charset.StandardCharsets; -import java.util.concurrent.TimeoutException; import org.json.JSONObject; /** Executes session input and image capture inside the instrumentation's automation context. */ @@ -59,36 +58,38 @@ private static final class InputDispatch { } private static void input(UiAutomation automation, JSONObject input, InputDispatch dispatch) throws Exception { + // The host's remaining time for this command; every press inside it answers by this deadline. + long deadline = SystemClock.uptimeMillis() + input.getLong("budgetMs"); switch (input.getString("kind")) { case "tap": - tap(automation, input.getDouble("x"), input.getDouble("y"), dispatch); + tap(automation, input.getDouble("x"), input.getDouble("y"), dispatch, deadline); return; case "key": - key(automation, input.getInt("keyCode"), dispatch); + key(automation, input.getInt("keyCode"), dispatch, deadline); return; case "fill": - fill(automation, input.getInt("x"), input.getInt("y"), input.getString("text"), dispatch); + fill(automation, input.getInt("x"), input.getInt("y"), input.getString("text"), dispatch, + deadline); return; default: throw new IllegalArgumentException("Unknown input kind"); } } - private static void tap(UiAutomation automation, double x, double y, InputDispatch dispatch) - throws TimeoutException { + private static void tap( + UiAutomation automation, double x, double y, InputDispatch dispatch, long deadline) { if (Double.isNaN(x) || Double.isInfinite(x) || Double.isNaN(y) || Double.isInfinite(y) || x < 0 || y < 0) { throw new IllegalArgumentException("Invalid tap coordinates"); } dispatch.attempted = true; - PressSequence.requireDispatched(PressSequence.run(new TouchPress(automation, (float) x, (float) y)), "Touch"); + PressSequence.requireDispatched(PressSequence.run(new TouchPress(automation, (float) x, (float) y), deadline), "Touch"); } - private static void key(UiAutomation automation, int code, InputDispatch dispatch) - throws TimeoutException { + private static void key(UiAutomation automation, int code, InputDispatch dispatch, long deadline) { if (code < 0 || code > KeyEvent.getMaxKeyCode()) throw new IllegalArgumentException("Invalid key code"); dispatch.attempted = true; - PressSequence.requireDispatched(PressSequence.run(new KeyPress(automation, code)), "Key"); + PressSequence.requireDispatched(PressSequence.run(new KeyPress(automation, code), deadline), "Key"); } private abstract static class Press implements PressSequence.Injections { @@ -130,19 +131,14 @@ public boolean start(long downTime) { } @Override - public boolean end(long downTime, boolean waitForDispatch) { - return inject(MotionEvent.ACTION_UP, downTime, 0, waitForDispatch); + public boolean end(long downTime) { + return inject(MotionEvent.ACTION_UP, downTime, 0, true); } - /** The verifier records a dropped DOWN as a pointer that is still down; no window receives it. */ + /** Not awaited: a window that received a timed-out DOWN may not finish handling the cancel. */ @Override - public void discardDroppedStart(long downTime) { - cancelStart(downTime); - } - - @Override - public boolean cancelStart(long downTime) { - return inject(MotionEvent.ACTION_CANCEL, downTime, MotionEvent.FLAG_CANCELED, false); + public void releaseRefusedStart(long downTime) { + inject(MotionEvent.ACTION_CANCEL, downTime, MotionEvent.FLAG_CANCELED, false); } private boolean inject(int action, long downTime, int flags, boolean waitForDispatch) { @@ -170,18 +166,17 @@ public boolean start(long downTime) { } @Override - public boolean end(long downTime, boolean waitForDispatch) { - return inject(KeyEvent.ACTION_UP, downTime, 0, waitForDispatch); + public boolean end(long downTime) { + return inject(KeyEvent.ACTION_UP, downTime, 0, true); } - /** The injection verifier checks motion events only, so a dropped key DOWN leaves no state. */ - @Override - public void discardDroppedStart(long downTime) {} - - /** Android releases an interrupted key with an ACTION_UP that carries FLAG_CANCELED. */ + /** + * Android releases an interrupted key with an ACTION_UP that carries FLAG_CANCELED; receivers + * take no action on a canceled up. Not awaited, like the touch cancel. + */ @Override - public boolean cancelStart(long downTime) { - return inject(KeyEvent.ACTION_UP, downTime, KeyEvent.FLAG_CANCELED, false); + public void releaseRefusedStart(long downTime) { + inject(KeyEvent.ACTION_UP, downTime, KeyEvent.FLAG_CANCELED, false); } private boolean inject(int action, long downTime, int flags, boolean waitForDispatch) { @@ -195,8 +190,8 @@ private KeyEvent event(int action, long downTime, int flags) { } } - private static void fill(UiAutomation automation, int x, int y, String text, InputDispatch dispatch) - throws TimeoutException { + private static void fill( + UiAutomation automation, int x, int y, String text, InputDispatch dispatch, long deadline) { AccessibilityNodeInfo target = null; try { for (AccessibilityWindowInfo window : automation.getWindows()) { @@ -221,7 +216,7 @@ private static void fill(UiAutomation automation, int x, int y, String text, Inp } } if (target == null) throw new IllegalStateException("No editable field at the fill point"); - tap(automation, x, y, dispatch); + tap(automation, x, y, dispatch, deadline); if (!target.refresh()) throw new IllegalStateException("Fill target disappeared after focus"); if (!target.isFocused() && !target.performAction(AccessibilityNodeInfo.ACTION_FOCUS)) { throw new IllegalStateException("Field focus was refused"); diff --git a/android/snapshot-helper/src/main/java/com/callstack/agentdevice/snapshothelper/PressSequence.java b/android/snapshot-helper/src/main/java/com/callstack/agentdevice/snapshothelper/PressSequence.java index 57be5c594f..5d03692e7f 100644 --- a/android/snapshot-helper/src/main/java/com/callstack/agentdevice/snapshothelper/PressSequence.java +++ b/android/snapshot-helper/src/main/java/com/callstack/agentdevice/snapshothelper/PressSequence.java @@ -1,34 +1,21 @@ package com.callstack.agentdevice.snapshothelper; -import java.util.concurrent.TimeoutException; - /** * Sequences one tap or key press: a start event, then an end event with the same down time. * - *

Without the framework's animation wait, a start can be dropped because a transition leaves - * nothing able to receive it. The injection verifier still records it as landed, so the start is - * discarded before it is retried once after the framework's animation sync, with a fresh down time - * so the receiver does not measure the wait as a held press. A runtime that injects through the - * public overload already waited, so its dropped start fails at once, as it always did. + *

Every refused start is released before anything else is decided: the verifier and the policy + * record a start even when the dispatcher drops it, and a start refused by the injection timeout + * was delivered. Without the framework's animation wait, a start is often refused only because a + * transition leaves nothing able to receive it, so it is retried once after the animation sync, + * with a fresh down time, when the host's deadline still has room for that retry. */ final class PressSequence { /** - * The framework's synchronous injection deadline, {@code InputManagerService.INJECTION_TIMEOUT_MILLIS}. - * Injection reports a timeout and a drop alike as {@code false}. The dispatcher starts this deadline - * when the call enters it, so only a refusal at least this long after the call can be a timeout. - */ - static final long INJECTION_TIMEOUT_MS = 30_000; - - /** The framework's cap on one animation sync, {@code WindowManagerService.ANIMATION_COMPLETED_TIMEOUT_MS}. */ - static final long ANIMATION_SYNC_TIMEOUT_MS = 5_000; - - /** - * The longest one press may take, so the helper answers before the host's input timeout. A - * synchronous injection can block until {@link #INJECTION_TIMEOUT_MS}, so each one after the first - * starts only when that deadline still fits: the retry of a dropped start is skipped, and an end - * that does not fit is injected without waiting for dispatch. + * The longest a retry can take before it answers: the framework's animation-sync cap + * ({@code WindowManagerService.ANIMATION_COMPLETED_TIMEOUT_MS}) plus its synchronous injection + * deadline ({@code InputManagerService.INJECTION_TIMEOUT_MILLIS}). */ - static final long PRESS_BUDGET_MS = 40_000; + static final long RETRY_WORST_CASE_MS = 5_000 + 30_000; /** Builds and injects the platform events of one press. */ interface Injections { @@ -39,14 +26,10 @@ interface Injections { boolean start(long downTime); - /** Injects the end event; without {@code waitForDispatch} it cannot report a refusal. */ - boolean end(long downTime, boolean waitForDispatch); - - /** Closes the verifier state of a dropped start that has this down time. */ - void discardDroppedStart(long downTime); + boolean end(long downTime); - /** Releases a start that has this down time and that the receiver may hold; false if refused. */ - boolean cancelStart(long downTime); + /** Releases a refused start that has this down time; the receiver may hold it. */ + void releaseRefusedStart(long downTime); /** Waits for the framework's animation sync; false when the runtime cannot wait separately. */ boolean awaitAnimations(); @@ -55,96 +38,55 @@ interface Injections { enum Outcome { DISPATCHED, START_REFUSED, - START_TIMED_OUT, - START_TIMED_OUT_CANCEL_REFUSED, END_REFUSED, ANIMATION_SYNC_UNAVAILABLE, } - private enum Start { - ACCEPTED, - DROPPED, - TIMED_OUT, - TIMED_OUT_CANCEL_REFUSED, - } - private PressSequence() {} - static Outcome run(Injections injections) { - long pressStart = injections.now(); - long downTime = pressStart; - Start start = start(injections, downTime); - if (start == Start.DROPPED) { - if (!injections.skipsAnimationWait()) return Outcome.START_REFUSED; - discardDroppedStart(injections, downTime); - if (!fits(injections, pressStart, ANIMATION_SYNC_TIMEOUT_MS + INJECTION_TIMEOUT_MS)) { + /** Runs one press that must answer by {@code deadline}, on the {@link Injections#now} clock. */ + static Outcome run(Injections injections, long deadline) { + long downTime = injections.now(); + if (!injections.start(downTime)) { + release(injections, downTime); + if (!injections.skipsAnimationWait() + || deadline - injections.now() < RETRY_WORST_CASE_MS) { return Outcome.START_REFUSED; } if (!injections.awaitAnimations()) return Outcome.ANIMATION_SYNC_UNAVAILABLE; - if (!fits(injections, pressStart, INJECTION_TIMEOUT_MS)) return Outcome.START_REFUSED; downTime = injections.now(); - start = start(injections, downTime); - if (start == Start.DROPPED) { - discardDroppedStart(injections, downTime); + if (!injections.start(downTime)) { + release(injections, downTime); return Outcome.START_REFUSED; } } - if (start == Start.TIMED_OUT) return Outcome.START_TIMED_OUT; - if (start == Start.TIMED_OUT_CANCEL_REFUSED) return Outcome.START_TIMED_OUT_CANCEL_REFUSED; // A refused end gets no cancel. The verifier records an event before the dispatcher drops it, // so the press is closed there and a cancel would be inconsistent. The dispatcher drops an end // only when no pointer or focus is still down, after it cancelled the receiving window itself // ("touched window was removed", "focus left window"). A timed-out end was already enqueued, // so the live window still receives it. - boolean waitForDispatch = fits(injections, pressStart, INJECTION_TIMEOUT_MS); - return injections.end(downTime, waitForDispatch) ? Outcome.DISPATCHED : Outcome.END_REFUSED; - } - - private static boolean fits(Injections injections, long pressStart, long waitMs) { - return injections.now() - pressStart + waitMs <= PRESS_BUDGET_MS; + return injections.end(downTime) ? Outcome.DISPATCHED : Outcome.END_REFUSED; } /** Turns an outcome into the helper's response: a typed exception for each failed press. */ - static void requireDispatched(Outcome outcome, String kind) throws TimeoutException { + static void requireDispatched(Outcome outcome, String kind) { switch (outcome) { case DISPATCHED: return; case ANIMATION_SYNC_UNAVAILABLE: throw new UnsupportedOperationException( kind + " injection was refused and this runtime cannot wait for animations to retry it"); - case START_TIMED_OUT: - throw new TimeoutException( - kind + " injection timed out before the app finished handling it; it was cancelled"); - case START_TIMED_OUT_CANCEL_REFUSED: - throw new TimeoutException(kind + " injection timed out before the app finished handling" - + " it, and its cancel was refused, so the app may still hold it"); case START_REFUSED: case END_REFUSED: throw new IllegalStateException(kind + " injection was refused"); } } - /** - * A timed-out start was enqueued before the deadline passed, so the receiver gets it and must not - * see a second start; it is released instead of retried. - */ - private static Start start(Injections injections, long downTime) { - if (injections.start(downTime)) return Start.ACCEPTED; - if (injections.now() - downTime < INJECTION_TIMEOUT_MS) return Start.DROPPED; - boolean cancelled; - try { - cancelled = injections.cancelStart(downTime); - } catch (RuntimeException error) { - cancelled = false; - } - return cancelled ? Start.TIMED_OUT : Start.TIMED_OUT_CANCEL_REFUSED; - } - - private static void discardDroppedStart(Injections injections, long downTime) { + private static void release(Injections injections, long downTime) { try { - injections.discardDroppedStart(downTime); + injections.releaseRefusedStart(downTime); } catch (RuntimeException ignored) { - // The verifier keeps the dropped start; the press continues with its own outcome. + // The refused start stays the reported outcome. } } } diff --git a/android/snapshot-helper/src/test/java/com/callstack/agentdevice/snapshothelper/PressSequenceTest.java b/android/snapshot-helper/src/test/java/com/callstack/agentdevice/snapshothelper/PressSequenceTest.java index b0ced68b94..56de9feeba 100644 --- a/android/snapshot-helper/src/test/java/com/callstack/agentdevice/snapshothelper/PressSequenceTest.java +++ b/android/snapshot-helper/src/test/java/com/callstack/agentdevice/snapshothelper/PressSequenceTest.java @@ -3,173 +3,110 @@ import java.util.ArrayList; import java.util.Arrays; import java.util.List; -import java.util.concurrent.TimeoutException; public final class PressSequenceTest { - private static final long TIMEOUT = PressSequence.INJECTION_TIMEOUT_MS; + private static final long ROOM = PressSequence.RETRY_WORST_CASE_MS; + /** A deadline far enough away that it never limits the press. */ + private static final long OPEN = Long.MAX_VALUE / 2; private PressSequenceTest() {} static void run() { endsWithTheStartDownTime(); - retriesADroppedStartAfterTheAnimationSyncWithAFreshDownTime(); - retriesADropThatLeavesRoomForTheSyncAndTheRetryDeadline(); - skipsTheRetryOfADropThatLeavesNoRoomForTheSyncAndTheRetryDeadline(); - skipsTheRetryWhenTheSyncLeavesNoRoomForTheRetryDeadline(); - discardsAStartRefusedJustBeforeTheInjectionDeadlineAsADrop(); - cancelsAndDoesNotRetryAStartRefusedAtTheInjectionDeadline(); - discardsAStartDroppedAgainAfterTheSync(); - cancelsARetriedStartThatTimesOut(); - waitsForTheEndWhenItsDeadlineFits(); - injectsTheEndWithoutWaitingWhenItsDeadlineDoesNotFit(); + releasesARefusedStartAndRetriesItAfterTheSyncWithAFreshDownTime(); + retriesWhenTheDeadlineLeavesExactlyRoomForTheRetry(); + releasesWithoutRetryingWhenTheDeadlineLeavesNoRoomForTheRetry(); + releasesWithoutRetryingAStartRefusedLateInTheBudget(); + releasesAStartRefusedAgainAfterTheSync(); doesNotCancelARefusedEnd(); - failsADroppedStartAtOnceWhenInjectionWaitsForAnimations(); + releasesAndFailsADroppedStartAtOnceWhenInjectionWaitsForAnimations(); reportsAnUnavailableAnimationSyncApartFromARefusal(); - reportsARefusedCancelOfATimedOutStart(); - treatsAThrowingCancelAsRefused(); - retriesADroppedStartWhenItsDiscardThrows(); - failsATimedOutKeyStartWithATimeoutAndInjectsNoEnd(); - namesARefusedCancelInTheTimeout(); - failsARefusedEndAsARefusal(); + retriesWhenTheReleaseThrows(); + failsARefusedPressAsARefusal(); + failsAnUnavailableSyncAsUnsupported(); } private static void endsWithTheStartDownTime() { FakeInjections injections = new FakeInjections(accepted()); - expect(PressSequence.run(injections), PressSequence.Outcome.DISPATCHED); + expect(PressSequence.run(injections, OPEN), PressSequence.Outcome.DISPATCHED); expect(injections.calls, "start 1", "end 1"); } - private static void retriesADroppedStartAfterTheAnimationSyncWithAFreshDownTime() { + private static void releasesARefusedStartAndRetriesItAfterTheSyncWithAFreshDownTime() { FakeInjections injections = new FakeInjections(refusedAfter(0), accepted()); - expect(PressSequence.run(injections), PressSequence.Outcome.DISPATCHED); - expect(injections.calls, "start 1", "discard 1", "sync", "start 5", "end 5"); + expect(PressSequence.run(injections, OPEN), PressSequence.Outcome.DISPATCHED); + expect(injections.calls, "start 1", "release 1", "sync", "start 3", "end 3"); } - private static void retriesADropThatLeavesRoomForTheSyncAndTheRetryDeadline() { - FakeInjections injections = new FakeInjections(refusedAfter(4_998), accepted()); - expect(PressSequence.run(injections), PressSequence.Outcome.DISPATCHED); - expect(injections.calls, "start 1", "discard 1", "sync", "start 5003", "end 5003"); - } - - private static void skipsTheRetryOfADropThatLeavesNoRoomForTheSyncAndTheRetryDeadline() { - FakeInjections injections = new FakeInjections(refusedAfter(4_999), accepted()); - expect(PressSequence.run(injections), PressSequence.Outcome.START_REFUSED); - expect(injections.calls, "start 1", "discard 1"); - } - - private static void skipsTheRetryWhenTheSyncLeavesNoRoomForTheRetryDeadline() { + private static void retriesWhenTheDeadlineLeavesExactlyRoomForTheRetry() { FakeInjections injections = new FakeInjections(refusedAfter(0), accepted()); - injections.syncMs = 11_000; - expect(PressSequence.run(injections), PressSequence.Outcome.START_REFUSED); - expect(injections.calls, "start 1", "discard 1", "sync"); + expect(PressSequence.run(injections, 2 + ROOM), PressSequence.Outcome.DISPATCHED); + expect(injections.calls, "start 1", "release 1", "sync", "start 3", "end 3"); } - private static void discardsAStartRefusedJustBeforeTheInjectionDeadlineAsADrop() { - FakeInjections injections = new FakeInjections(refusedAfter(TIMEOUT - 2), accepted()); - expect(PressSequence.run(injections), PressSequence.Outcome.START_REFUSED); - expect(injections.calls, "start 1", "discard 1"); + private static void releasesWithoutRetryingWhenTheDeadlineLeavesNoRoomForTheRetry() { + FakeInjections injections = new FakeInjections(refusedAfter(0), accepted()); + expect(PressSequence.run(injections, 1 + ROOM), PressSequence.Outcome.START_REFUSED); + expect(injections.calls, "start 1", "release 1"); } - private static void cancelsAndDoesNotRetryAStartRefusedAtTheInjectionDeadline() { - FakeInjections injections = new FakeInjections(refusedAfter(TIMEOUT - 1), accepted()); - expect(PressSequence.run(injections), PressSequence.Outcome.START_TIMED_OUT); - expect(injections.calls, "start 1", "cancel 1"); + private static void releasesWithoutRetryingAStartRefusedLateInTheBudget() { + FakeInjections injections = new FakeInjections(refusedAfter(30_000), accepted()); + expect(PressSequence.run(injections, 40_000), PressSequence.Outcome.START_REFUSED); + expect(injections.calls, "start 1", "release 1"); } - private static void discardsAStartDroppedAgainAfterTheSync() { + private static void releasesAStartRefusedAgainAfterTheSync() { FakeInjections injections = new FakeInjections(refusedAfter(0), refusedAfter(0)); - expect(PressSequence.run(injections), PressSequence.Outcome.START_REFUSED); - expect(injections.calls, "start 1", "discard 1", "sync", "start 5", "discard 5"); - } - - private static void cancelsARetriedStartThatTimesOut() { - FakeInjections injections = new FakeInjections(refusedAfter(0), refusedAfter(TIMEOUT)); - expect(PressSequence.run(injections), PressSequence.Outcome.START_TIMED_OUT); - expect(injections.calls, "start 1", "discard 1", "sync", "start 5", "cancel 5"); - } - - private static void waitsForTheEndWhenItsDeadlineFits() { - FakeInjections injections = new FakeInjections(acceptedAfter(9_999)); - expect(PressSequence.run(injections), PressSequence.Outcome.DISPATCHED); - expect(injections.calls, "start 1", "end 1"); - } - - private static void injectsTheEndWithoutWaitingWhenItsDeadlineDoesNotFit() { - FakeInjections injections = new FakeInjections(acceptedAfter(10_000)); - expect(PressSequence.run(injections), PressSequence.Outcome.DISPATCHED); - expect(injections.calls, "start 1", "end-async 1"); + expect(PressSequence.run(injections, OPEN), PressSequence.Outcome.START_REFUSED); + expect(injections.calls, "start 1", "release 1", "sync", "start 3", "release 3"); } private static void doesNotCancelARefusedEnd() { FakeInjections injections = new FakeInjections(accepted()); injections.endAccepted = false; - expect(PressSequence.run(injections), PressSequence.Outcome.END_REFUSED); + expect(PressSequence.run(injections, OPEN), PressSequence.Outcome.END_REFUSED); expect(injections.calls, "start 1", "end 1"); } - private static void failsADroppedStartAtOnceWhenInjectionWaitsForAnimations() { + private static void releasesAndFailsADroppedStartAtOnceWhenInjectionWaitsForAnimations() { FakeInjections injections = new FakeInjections(refusedAfter(0), accepted()); injections.skipsAnimationWait = false; - expect(PressSequence.run(injections), PressSequence.Outcome.START_REFUSED); - expect(injections.calls, "start 1"); + expect(PressSequence.run(injections, OPEN), PressSequence.Outcome.START_REFUSED); + expect(injections.calls, "start 1", "release 1"); } private static void reportsAnUnavailableAnimationSyncApartFromARefusal() { FakeInjections injections = new FakeInjections(refusedAfter(0), accepted()); injections.syncAvailable = false; - expect(PressSequence.run(injections), PressSequence.Outcome.ANIMATION_SYNC_UNAVAILABLE); - expect(injections.calls, "start 1", "discard 1", "sync"); - } - - private static void reportsARefusedCancelOfATimedOutStart() { - FakeInjections injections = new FakeInjections(refusedAfter(TIMEOUT), accepted()); - injections.cancelAccepted = false; - expect(PressSequence.run(injections), PressSequence.Outcome.START_TIMED_OUT_CANCEL_REFUSED); - expect(injections.calls, "start 1", "cancel 1"); - } - - private static void treatsAThrowingCancelAsRefused() { - FakeInjections injections = new FakeInjections(refusedAfter(TIMEOUT), accepted()); - injections.releaseThrows = true; - expect(PressSequence.run(injections), PressSequence.Outcome.START_TIMED_OUT_CANCEL_REFUSED); - expect(injections.calls, "start 1", "cancel 1"); + expect(PressSequence.run(injections, OPEN), PressSequence.Outcome.ANIMATION_SYNC_UNAVAILABLE); + expect(injections.calls, "start 1", "release 1", "sync"); } - private static void retriesADroppedStartWhenItsDiscardThrows() { + private static void retriesWhenTheReleaseThrows() { FakeInjections injections = new FakeInjections(refusedAfter(0), accepted()); injections.releaseThrows = true; - expect(PressSequence.run(injections), PressSequence.Outcome.DISPATCHED); - expect(injections.calls, "start 1", "discard 1", "sync", "start 5", "end 5"); - } - - private static void failsATimedOutKeyStartWithATimeoutAndInjectsNoEnd() { - FakeInjections injections = new FakeInjections(refusedAfter(TIMEOUT), accepted()); - expectFailure(injections, TimeoutException.class, - "Key injection timed out before the app finished handling it; it was cancelled"); - expect(injections.calls, "start 1", "cancel 1"); + expect(PressSequence.run(injections, OPEN), PressSequence.Outcome.DISPATCHED); + expect(injections.calls, "start 1", "release 1", "sync", "start 3", "end 3"); } - private static void namesARefusedCancelInTheTimeout() { - FakeInjections injections = new FakeInjections(refusedAfter(TIMEOUT), accepted()); - injections.cancelAccepted = false; - expectFailure(injections, TimeoutException.class, - "Key injection timed out before the app finished handling it, and its cancel was refused," - + " so the app may still hold it"); - expect(injections.calls, "start 1", "cancel 1"); + private static void failsARefusedPressAsARefusal() { + FakeInjections injections = new FakeInjections(refusedAfter(0), refusedAfter(0)); + expectFailure(injections, IllegalStateException.class, "Key injection was refused"); } - private static void failsARefusedEndAsARefusal() { - FakeInjections injections = new FakeInjections(accepted()); - injections.endAccepted = false; - expectFailure(injections, IllegalStateException.class, "Key injection was refused"); - expect(injections.calls, "start 1", "end 1"); + private static void failsAnUnavailableSyncAsUnsupported() { + FakeInjections injections = new FakeInjections(refusedAfter(0), accepted()); + injections.syncAvailable = false; + expectFailure(injections, UnsupportedOperationException.class, + "Key injection was refused and this runtime cannot wait for animations to retry it"); } private static void expectFailure( - FakeInjections injections, Class type, String message) { + FakeInjections injections, Class type, String message) { try { - PressSequence.requireDispatched(PressSequence.run(injections), "Key"); - } catch (Exception error) { + PressSequence.requireDispatched(PressSequence.run(injections, OPEN), "Key"); + } catch (RuntimeException error) { if (error.getClass() != type || !message.equals(error.getMessage())) { throw new AssertionError("Expected " + type.getName() + ": " + message + ", got " + error); } @@ -180,12 +117,7 @@ private static void expectFailure( /** A start the framework accepts at once. */ private static long accepted() { - return acceptedAfter(0); - } - - /** A start the framework accepts after this many milliseconds. */ - private static long acceptedAfter(long elapsedMs) { - return -elapsedMs - 1; + return -1; } /** A start the framework refuses after this many milliseconds. */ @@ -203,10 +135,7 @@ private static void expect(List actual, String... expected) { } } - /** - * Each {@code now()} advances the clock by 1 ms. A start advances it by its delay, and the sync by - * {@code syncMs}. - */ + /** Each {@code now()} advances the clock by 1 ms; a refused start also advances it by its delay. */ private static final class FakeInjections implements PressSequence.Injections { final List calls = new ArrayList<>(); private final long[] starts; @@ -214,9 +143,7 @@ private static final class FakeInjections implements PressSequence.Injections { private long clock; boolean skipsAnimationWait = true; boolean syncAvailable = true; - long syncMs; boolean endAccepted = true; - boolean cancelAccepted = true; boolean releaseThrows; FakeInjections(long... starts) { @@ -236,35 +163,27 @@ public boolean skipsAnimationWait() { @Override public boolean start(long downTime) { calls.add("start " + downTime); - long spec = starts[startIndex++]; - boolean accepted = spec < 0; - clock += accepted ? -spec - 1 : spec; - return accepted; + long refusedAfterMs = starts[startIndex++]; + if (refusedAfterMs < 0) return true; + clock += refusedAfterMs; + return false; } @Override - public boolean end(long downTime, boolean waitForDispatch) { - calls.add((waitForDispatch ? "end " : "end-async ") + downTime); + public boolean end(long downTime) { + calls.add("end " + downTime); return endAccepted; } @Override - public void discardDroppedStart(long downTime) { - calls.add("discard " + downTime); - if (releaseThrows) throw new IllegalStateException("discard failed"); - } - - @Override - public boolean cancelStart(long downTime) { - calls.add("cancel " + downTime); - if (releaseThrows) throw new IllegalStateException("cancel failed"); - return cancelAccepted; + public void releaseRefusedStart(long downTime) { + calls.add("release " + downTime); + if (releaseThrows) throw new IllegalStateException("release failed"); } @Override public boolean awaitAnimations() { calls.add("sync"); - clock += syncMs; return syncAvailable; } } diff --git a/packages/platform-android/src/automation-helper-input-budget.test.ts b/packages/platform-android/src/automation-helper-input-budget.test.ts new file mode 100644 index 0000000000..f75ece0483 --- /dev/null +++ b/packages/platform-android/src/automation-helper-input-budget.test.ts @@ -0,0 +1,31 @@ +import { expect, test, vi } from 'vitest'; + +vi.mock('./snapshot-helper-session-lifecycle.ts', () => ({ + getLiveAndroidSnapshotHelperSession: () => ({ commands: new Set(['input']) }), + stopAndroidSnapshotHelperSession: vi.fn(), +})); +vi.mock('./automation-helper-artifact.ts', () => ({ + prepareAndroidAutomationHelper: vi.fn(async () => ({ artifact: {} })), + androidAutomationHelperIdentity: vi.fn(() => ({})), +})); +vi.mock('./snapshot-helper-session.ts', () => ({ + runAndroidSnapshotHelperSessionTouchCommand: vi.fn(async () => ({ ok: 'true' })), +})); +import { runAndroidAutomationHelper } from './automation-helper.ts'; +import { runAndroidSnapshotHelperSessionTouchCommand } from './snapshot-helper-session.ts'; +import { ANDROID_EMULATOR } from './__tests__/test-utils/device-fixtures.ts'; + +test('input carries a press budget that ends before the host stops waiting for the helper', async () => { + await expect( + runAndroidAutomationHelper(ANDROID_EMULATOR, { + action: 'input', + input: { kind: 'tap', x: 10, y: 20 }, + }), + ).resolves.toBe(true); + + const [command] = vi.mocked(runAndroidSnapshotHelperSessionTouchCommand).mock.calls[0]!; + const payload = JSON.parse(Buffer.from(command.payloadBase64!, 'base64').toString('utf8')); + expect(payload).toMatchObject({ kind: 'tap', x: 10, y: 20 }); + expect(payload.budgetMs).toBeGreaterThan(0); + expect(payload.budgetMs).toBeLessThan(command.timeoutMs); +}); diff --git a/packages/platform-android/src/automation-helper.ts b/packages/platform-android/src/automation-helper.ts index df96aff5e1..a1a5d2ec60 100644 --- a/packages/platform-android/src/automation-helper.ts +++ b/packages/platform-android/src/automation-helper.ts @@ -38,14 +38,17 @@ type AndroidHelperAppResult = | { kind: 'launched'; output: string }; const ACTION_POLICY = { - // Above the helper's press budget (PressSequence.PRESS_BUDGET_MS), so the helper's typed answer - // for a press that waited out the framework's injection deadline arrives before this timeout. + // A press can block on the framework's 30 s injection deadline, and its one retry needs 35 s of + // room (PressSequence.RETRY_WORST_CASE_MS), so input gets more than the other actions. input: { mutation: true, timeoutMs: 45_000 }, app: { mutation: true, timeoutMs: 30_000 }, screenshot: { mutation: false, timeoutMs: 30_000 }, focus: { mutation: false, timeoutMs: 2_000 }, } as const; +/** The helper must answer input this long before the host timeout so its typed result arrives. */ +const INPUT_ANSWER_MARGIN_MS = 5_000; + /** Reuses the session's automation context without starting instrumentation for sessionless input. */ export function runAndroidAutomationHelper( device: DeviceInfo, @@ -79,10 +82,7 @@ export async function runAndroidAutomationHelper( deviceKey, action, helper: androidAutomationHelperIdentity(prepared.artifact), - payloadBase64: - 'input' in request - ? Buffer.from(JSON.stringify(request.input)).toString('base64') - : undefined, + payloadBase64: encodeHelperPayload(request), timeoutMs: ACTION_POLICY[action].timeoutMs, }); if (!headers) return undefined; @@ -136,6 +136,15 @@ function decodeHelperIndex(value: string | undefined): number | undefined { return value !== undefined && /^\d+$/.test(value) ? Number(value) : undefined; } +function encodeHelperPayload(request: AndroidAutomationRequest): string | undefined { + if (!('input' in request)) return undefined; + const payload = + request.action === 'input' + ? { ...request.input, budgetMs: ACTION_POLICY.input.timeoutMs - INPUT_ANSWER_MARGIN_MS } + : request.input; + return Buffer.from(JSON.stringify(payload)).toString('base64'); +} + function decodeHelperPayload( headers: Record, field: string, diff --git a/test/repo-guards/android-helper-input-dispatch.test.ts b/test/repo-guards/android-helper-input-dispatch.test.ts index 1508d96435..7327cacbc2 100644 --- a/test/repo-guards/android-helper-input-dispatch.test.ts +++ b/test/repo-guards/android-helper-input-dispatch.test.ts @@ -33,28 +33,3 @@ test('Android helper input reaches the framework only through the dispatcher wit /method\.invoke\(automation, event, waitForDispatch, false\)/, ); }); - -test('the host waits longer for helper input than the helper may spend on one press', () => { - const pressBudgetMs = Number( - fs - .readFileSync( - path.join(helperSourceDir, path.dirname(dispatcherFile), 'PressSequence.java'), - 'utf8', - ) - .match(/static final long PRESS_BUDGET_MS = ([\d_]+);/)?.[1] - ?.replaceAll('_', ''), - ); - const hostInputTimeoutMs = Number( - fs - .readFileSync( - path.join(repoRoot, 'packages/platform-android/src/automation-helper.ts'), - 'utf8', - ) - .match(/input: \{ mutation: true, timeoutMs: ([\d_]+) \}/)?.[1] - ?.replaceAll('_', ''), - ); - assert.ok(pressBudgetMs > 0, 'PressSequence.PRESS_BUDGET_MS must be readable'); - assert.ok(hostInputTimeoutMs > 0, 'the host input timeout must be readable'); - // The margin covers the socket round trip and the async cancel after the budget. - assert.ok(hostInputTimeoutMs >= pressBudgetMs + 5_000); -}); From 604873ef4c90356dfc81069eac1ed235e48ffc22 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Pierzcha=C5=82a?= Date: Mon, 16 Nov 2026 22:54:03 +0100 Subject: [PATCH 10/16] fix(android): derive the helper press budget from the request's remaining deadline The host now waits for an input command until the earlier of its input cap and the request's own deadline, and sends the helper that wait less its answer margin, instead of a fixed budget. A request near its deadline sends a small budget, so the helper skips a retry it could not finish in time. Co-Authored-By: Claude Opus 5.5 --- .../__tests__/touch-helper-session.test.ts | 7 ++-- .../automation-helper-input-budget.test.ts | 41 ++++++++++++++++--- .../platform-android/src/automation-helper.ts | 25 ++++++++--- 3 files changed, 58 insertions(+), 15 deletions(-) diff --git a/packages/platform-android/src/__tests__/touch-helper-session.test.ts b/packages/platform-android/src/__tests__/touch-helper-session.test.ts index c9da0919b4..2947e435e0 100644 --- a/packages/platform-android/src/__tests__/touch-helper-session.test.ts +++ b/packages/platform-android/src/__tests__/touch-helper-session.test.ts @@ -727,10 +727,11 @@ test('input actions reuse the live automation socket without adb input calls', a await pressAndroidEnter(device); }, ); + // Outside a request deadline, each command gets the host's input cap less its answer margin. assert.deepEqual(inputs, [ - { kind: 'tap', x: 12, y: 34 }, - { kind: 'key', keyCode: 4 }, - { kind: 'key', keyCode: 66 }, + { kind: 'tap', x: 12, y: 34, budgetMs: 40_000 }, + { kind: 'key', keyCode: 4, budgetMs: 40_000 }, + { kind: 'key', keyCode: 66, budgetMs: 40_000 }, ]); assert.equal( adbCalls.some((args) => args.includes('input')), diff --git a/packages/platform-android/src/automation-helper-input-budget.test.ts b/packages/platform-android/src/automation-helper-input-budget.test.ts index f75ece0483..9547dde68f 100644 --- a/packages/platform-android/src/automation-helper-input-budget.test.ts +++ b/packages/platform-android/src/automation-helper-input-budget.test.ts @@ -11,17 +11,46 @@ vi.mock('./automation-helper-artifact.ts', () => ({ vi.mock('./snapshot-helper-session.ts', () => ({ runAndroidSnapshotHelperSessionTouchCommand: vi.fn(async () => ({ ok: 'true' })), })); +import type { DaemonResponse } from '@agent-device/kernel/contracts'; +import { withDiagnosticsScope } from '@agent-device/host-kit/diagnostics'; +import { withRequestDeadline } from '@agent-device/host-kit/request'; import { runAndroidAutomationHelper } from './automation-helper.ts'; import { runAndroidSnapshotHelperSessionTouchCommand } from './snapshot-helper-session.ts'; import { ANDROID_EMULATOR } from './__tests__/test-utils/device-fixtures.ts'; -test('input carries a press budget that ends before the host stops waiting for the helper', async () => { - await expect( - runAndroidAutomationHelper(ANDROID_EMULATOR, { - action: 'input', - input: { kind: 'tap', x: 10, y: 20 }, +function tap() { + return runAndroidAutomationHelper(ANDROID_EMULATOR, { + action: 'input', + input: { kind: 'tap', x: 10, y: 20 }, + }); +} + +async function tapWithinRequestDeadline(remainingMs: number) { + vi.mocked(runAndroidSnapshotHelperSessionTouchCommand).mockClear(); + await withDiagnosticsScope({ requestId: `budget-${remainingMs}` }, () => + withRequestDeadline(`budget-${remainingMs}`, remainingMs, async () => { + await tap(); + return { ok: true } as unknown as DaemonResponse; }), - ).resolves.toBe(true); + ); + const [command] = vi.mocked(runAndroidSnapshotHelperSessionTouchCommand).mock.calls[0]!; + const payload = JSON.parse(Buffer.from(command.payloadBase64!, 'base64').toString('utf8')); + return { budgetMs: payload.budgetMs as number, timeoutMs: command.timeoutMs }; +} + +test('a request near its deadline sends the helper only what remains of it', async () => { + const near = await tapWithinRequestDeadline(7_000); + expect(near.timeoutMs).toBeLessThanOrEqual(7_000); + expect(near.timeoutMs).toBeGreaterThan(6_000); + expect(near.budgetMs).toBe(near.timeoutMs - 5_000); + + const exhausted = await tapWithinRequestDeadline(3_000); + expect(exhausted.budgetMs).toBe(0); +}); + +test('input carries a press budget that ends before the host stops waiting for the helper', async () => { + vi.mocked(runAndroidSnapshotHelperSessionTouchCommand).mockClear(); + await expect(tap()).resolves.toBe(true); const [command] = vi.mocked(runAndroidSnapshotHelperSessionTouchCommand).mock.calls[0]!; const payload = JSON.parse(Buffer.from(command.payloadBase64!, 'base64').toString('utf8')); diff --git a/packages/platform-android/src/automation-helper.ts b/packages/platform-android/src/automation-helper.ts index a1a5d2ec60..e5ba57261a 100644 --- a/packages/platform-android/src/automation-helper.ts +++ b/packages/platform-android/src/automation-helper.ts @@ -4,7 +4,8 @@ import { getLiveAndroidSnapshotHelperSession, stopAndroidSnapshotHelperSession, } from './snapshot-helper-session-lifecycle.ts'; -import { emitDiagnostic } from '@agent-device/host-kit/diagnostics'; +import { emitDiagnostic, getDiagnosticsMeta } from '@agent-device/host-kit/diagnostics'; +import { getRequestDeadlineMs } from '@agent-device/host-kit/request'; import { getAndroidSnapshotHelperSessionDeviceKey } from './snapshot-helper-retirement.ts'; import { runAndroidSnapshotHelperSessionTouchCommand } from './snapshot-helper-session.ts'; import { @@ -39,7 +40,7 @@ type AndroidHelperAppResult = const ACTION_POLICY = { // A press can block on the framework's 30 s injection deadline, and its one retry needs 35 s of - // room (PressSequence.RETRY_WORST_CASE_MS), so input gets more than the other actions. + // room (PressSequence.RETRY_WORST_CASE_MS), so input may wait longer than the other actions. input: { mutation: true, timeoutMs: 45_000 }, app: { mutation: true, timeoutMs: 30_000 }, screenshot: { mutation: false, timeoutMs: 30_000 }, @@ -77,13 +78,14 @@ export async function runAndroidAutomationHelper( if (!(await getLiveAndroidSnapshotHelperSession(deviceKey))?.commands.has(action)) return undefined; const prepared = await prepareAndroidAutomationHelper(device); + const timeoutMs = action === 'input' ? inputTimeoutMs() : ACTION_POLICY[action].timeoutMs; try { const headers = await runAndroidSnapshotHelperSessionTouchCommand({ deviceKey, action, helper: androidAutomationHelperIdentity(prepared.artifact), - payloadBase64: encodeHelperPayload(request), - timeoutMs: ACTION_POLICY[action].timeoutMs, + payloadBase64: encodeHelperPayload(request, timeoutMs), + timeoutMs, }); if (!headers) return undefined; switch (action) { @@ -136,11 +138,22 @@ function decodeHelperIndex(value: string | undefined): number | undefined { return value !== undefined && /^\d+$/.test(value) ? Number(value) : undefined; } -function encodeHelperPayload(request: AndroidAutomationRequest): string | undefined { +/** The host waits for input up to its cap, or until the request's own deadline when that is sooner. */ +function inputTimeoutMs(): number { + const capMs = ACTION_POLICY.input.timeoutMs; + const requestDeadlineMs = getRequestDeadlineMs(getDiagnosticsMeta().requestId); + if (requestDeadlineMs === undefined) return capMs; + return Math.max(0, Math.min(capMs, requestDeadlineMs - Date.now())); +} + +function encodeHelperPayload( + request: AndroidAutomationRequest, + timeoutMs: number, +): string | undefined { if (!('input' in request)) return undefined; const payload = request.action === 'input' - ? { ...request.input, budgetMs: ACTION_POLICY.input.timeoutMs - INPUT_ANSWER_MARGIN_MS } + ? { ...request.input, budgetMs: Math.max(0, timeoutMs - INPUT_ANSWER_MARGIN_MS) } : request.input; return Buffer.from(JSON.stringify(payload)).toString('base64'); } From f728193a064d6006addb189947b834ab70be1990 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Pierzcha=C5=82a?= Date: Mon, 16 Nov 2026 22:58:21 +0100 Subject: [PATCH 11/16] fix(android): run a press without a budget as a no-retry press A provider-supplied helper can be newer than its host, which then sends no budgetMs; that press is no longer refused with a JSONException but runs without a retry. Also states why the retry reserves the sync and the retried start but not the end, and drops an unused import. Co-Authored-By: Claude Opus 5.5 --- .../snapshothelper/AutomationCommandHandler.java | 5 +++-- .../agentdevice/snapshothelper/PressSequence.java | 8 +++++--- .../agentdevice/snapshothelper/TouchPlanInjector.java | 1 - 3 files changed, 8 insertions(+), 6 deletions(-) diff --git a/android/snapshot-helper/src/main/java/com/callstack/agentdevice/snapshothelper/AutomationCommandHandler.java b/android/snapshot-helper/src/main/java/com/callstack/agentdevice/snapshothelper/AutomationCommandHandler.java index f3f262d0a4..edaa01756e 100644 --- a/android/snapshot-helper/src/main/java/com/callstack/agentdevice/snapshothelper/AutomationCommandHandler.java +++ b/android/snapshot-helper/src/main/java/com/callstack/agentdevice/snapshothelper/AutomationCommandHandler.java @@ -58,8 +58,9 @@ private static final class InputDispatch { } private static void input(UiAutomation automation, JSONObject input, InputDispatch dispatch) throws Exception { - // The host's remaining time for this command; every press inside it answers by this deadline. - long deadline = SystemClock.uptimeMillis() + input.getLong("budgetMs"); + // The host's remaining time for this command bounds every retry decision. A provider-supplied + // helper can be newer than its host, which sends no budget; such a press is never retried. + long deadline = SystemClock.uptimeMillis() + input.optLong("budgetMs", 0); switch (input.getString("kind")) { case "tap": tap(automation, input.getDouble("x"), input.getDouble("y"), dispatch, deadline); diff --git a/android/snapshot-helper/src/main/java/com/callstack/agentdevice/snapshothelper/PressSequence.java b/android/snapshot-helper/src/main/java/com/callstack/agentdevice/snapshothelper/PressSequence.java index 5d03692e7f..0c7fbf23dd 100644 --- a/android/snapshot-helper/src/main/java/com/callstack/agentdevice/snapshothelper/PressSequence.java +++ b/android/snapshot-helper/src/main/java/com/callstack/agentdevice/snapshothelper/PressSequence.java @@ -11,9 +11,11 @@ */ final class PressSequence { /** - * The longest a retry can take before it answers: the framework's animation-sync cap - * ({@code WindowManagerService.ANIMATION_COMPLETED_TIMEOUT_MS}) plus its synchronous injection - * deadline ({@code InputManagerService.INJECTION_TIMEOUT_MILLIS}). + * The room a retry decision reserves: the framework's animation-sync cap + * ({@code WindowManagerService.ANIMATION_COMPLETED_TIMEOUT_MS}) plus the retried start's injection + * deadline ({@code InputManagerService.INJECTION_TIMEOUT_MILLIS}). The end is not reserved: it + * blocks only while the app does not finish handling it, which a press without a retry meets the + * same way, and a 60 s press request could never reserve a second 30 s deadline. */ static final long RETRY_WORST_CASE_MS = 5_000 + 30_000; diff --git a/android/snapshot-helper/src/main/java/com/callstack/agentdevice/snapshothelper/TouchPlanInjector.java b/android/snapshot-helper/src/main/java/com/callstack/agentdevice/snapshothelper/TouchPlanInjector.java index 9bb0f80f1b..76413e0f49 100644 --- a/android/snapshot-helper/src/main/java/com/callstack/agentdevice/snapshothelper/TouchPlanInjector.java +++ b/android/snapshot-helper/src/main/java/com/callstack/agentdevice/snapshothelper/TouchPlanInjector.java @@ -2,7 +2,6 @@ import android.app.UiAutomation; import android.os.SystemClock; -import android.view.InputDevice; import android.view.MotionEvent; /** Injects a validated touch plan through UiAutomation with real-time sample pacing. */ From b23aeaa7003a88b7b310528a72b4fbf390b5e213 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Pierzcha=C5=82a?= Date: Mon, 16 Nov 2026 23:09:05 +0100 Subject: [PATCH 12/16] fix(android): refuse a press whose request has no room for one injection The host now admits helper input at one point: when the request's remaining time, less the answer margin, cannot cover one synchronous injection's 30 s framework deadline, the press is refused with the typed reason android-input-budget-exhausted before anything reaches the helper, so the host can never stop waiting mid-injection and tear the shared session down. Co-Authored-By: Claude Opus 5.5 --- .../automation-helper-input-budget.test.ts | 67 ++++++++++++------- .../platform-android/src/automation-helper.ts | 37 ++++++++-- 2 files changed, 75 insertions(+), 29 deletions(-) diff --git a/packages/platform-android/src/automation-helper-input-budget.test.ts b/packages/platform-android/src/automation-helper-input-budget.test.ts index 9547dde68f..e580875bb3 100644 --- a/packages/platform-android/src/automation-helper-input-budget.test.ts +++ b/packages/platform-android/src/automation-helper-input-budget.test.ts @@ -1,4 +1,4 @@ -import { expect, test, vi } from 'vitest'; +import { beforeEach, expect, test, vi } from 'vitest'; vi.mock('./snapshot-helper-session-lifecycle.ts', () => ({ getLiveAndroidSnapshotHelperSession: () => ({ commands: new Set(['input']) }), @@ -12,12 +12,19 @@ vi.mock('./snapshot-helper-session.ts', () => ({ runAndroidSnapshotHelperSessionTouchCommand: vi.fn(async () => ({ ok: 'true' })), })); import type { DaemonResponse } from '@agent-device/kernel/contracts'; +import { AppError } from '@agent-device/kernel/errors'; import { withDiagnosticsScope } from '@agent-device/host-kit/diagnostics'; import { withRequestDeadline } from '@agent-device/host-kit/request'; import { runAndroidAutomationHelper } from './automation-helper.ts'; +import { prepareAndroidAutomationHelper } from './automation-helper-artifact.ts'; import { runAndroidSnapshotHelperSessionTouchCommand } from './snapshot-helper-session.ts'; +import { stopAndroidSnapshotHelperSession } from './snapshot-helper-session-lifecycle.ts'; import { ANDROID_EMULATOR } from './__tests__/test-utils/device-fixtures.ts'; +beforeEach(() => { + vi.clearAllMocks(); +}); + function tap() { return runAndroidAutomationHelper(ANDROID_EMULATOR, { action: 'input', @@ -25,36 +32,50 @@ function tap() { }); } -async function tapWithinRequestDeadline(remainingMs: number) { - vi.mocked(runAndroidSnapshotHelperSessionTouchCommand).mockClear(); - await withDiagnosticsScope({ requestId: `budget-${remainingMs}` }, () => - withRequestDeadline(`budget-${remainingMs}`, remainingMs, async () => { - await tap(); +async function withinRequestDeadline(remainingMs: number, run: () => Promise): Promise { + let result: T | undefined; + const requestId = `budget-${remainingMs}-${Math.random()}`; + await withDiagnosticsScope({ requestId }, () => + withRequestDeadline(requestId, remainingMs, async () => { + result = await run(); return { ok: true } as unknown as DaemonResponse; }), ); + return result as T; +} + +function sentCommand() { const [command] = vi.mocked(runAndroidSnapshotHelperSessionTouchCommand).mock.calls[0]!; const payload = JSON.parse(Buffer.from(command.payloadBase64!, 'base64').toString('utf8')); - return { budgetMs: payload.budgetMs as number, timeoutMs: command.timeoutMs }; + return { payload, timeoutMs: command.timeoutMs }; } -test('a request near its deadline sends the helper only what remains of it', async () => { - const near = await tapWithinRequestDeadline(7_000); - expect(near.timeoutMs).toBeLessThanOrEqual(7_000); - expect(near.timeoutMs).toBeGreaterThan(6_000); - expect(near.budgetMs).toBe(near.timeoutMs - 5_000); - - const exhausted = await tapWithinRequestDeadline(3_000); - expect(exhausted.budgetMs).toBe(0); +test('without a request deadline, input carries the host cap less the answer margin', async () => { + await expect(tap()).resolves.toBe(true); + const { payload, timeoutMs } = sentCommand(); + expect(payload).toMatchObject({ kind: 'tap', x: 10, y: 20 }); + expect(payload.budgetMs).toBe(timeoutMs - 5_000); }); -test('input carries a press budget that ends before the host stops waiting for the helper', async () => { - vi.mocked(runAndroidSnapshotHelperSessionTouchCommand).mockClear(); - await expect(tap()).resolves.toBe(true); +test('a request with room sends the helper only what remains of its deadline', async () => { + await withinRequestDeadline(38_000, tap); + const { payload, timeoutMs } = sentCommand(); + expect(timeoutMs).toBeLessThanOrEqual(38_000); + expect(timeoutMs).toBeGreaterThan(36_000); + expect(payload.budgetMs).toBe(timeoutMs - 5_000); +}); - const [command] = vi.mocked(runAndroidSnapshotHelperSessionTouchCommand).mock.calls[0]!; - const payload = JSON.parse(Buffer.from(command.payloadBase64!, 'base64').toString('utf8')); - expect(payload).toMatchObject({ kind: 'tap', x: 10, y: 20 }); - expect(payload.budgetMs).toBeGreaterThan(0); - expect(payload.budgetMs).toBeLessThan(command.timeoutMs); +test('a request without room for one injection is refused before anything reaches the helper', async () => { + for (const remainingMs of [34_000, 3_000]) { + const error = await withinRequestDeadline(remainingMs, () => tap().catch((e: unknown) => e)); + expect(error).toBeInstanceOf(AppError); + expect((error as AppError).details).toMatchObject({ + reason: 'android-input-budget-exhausted', + minimumBudgetMs: 30_000, + dispatched: 'no', + }); + } + expect(runAndroidSnapshotHelperSessionTouchCommand).not.toHaveBeenCalled(); + expect(prepareAndroidAutomationHelper).not.toHaveBeenCalled(); + expect(stopAndroidSnapshotHelperSession).not.toHaveBeenCalled(); }); diff --git a/packages/platform-android/src/automation-helper.ts b/packages/platform-android/src/automation-helper.ts index e5ba57261a..bd072e7dbd 100644 --- a/packages/platform-android/src/automation-helper.ts +++ b/packages/platform-android/src/automation-helper.ts @@ -50,6 +50,12 @@ const ACTION_POLICY = { /** The helper must answer input this long before the host timeout so its typed result arrives. */ const INPUT_ANSWER_MARGIN_MS = 5_000; +/** + * The least helper budget a press is sent with: one synchronous injection can block until the + * framework's 30 s deadline, and a host that stopped waiting first would tear the session down. + */ +const MIN_INPUT_BUDGET_MS = 30_000; + /** Reuses the session's automation context without starting instrumentation for sessionless input. */ export function runAndroidAutomationHelper( device: DeviceInfo, @@ -77,8 +83,8 @@ export async function runAndroidAutomationHelper( const deviceKey = getAndroidSnapshotHelperSessionDeviceKey(device); if (!(await getLiveAndroidSnapshotHelperSession(deviceKey))?.commands.has(action)) return undefined; + const timeoutMs = action === 'input' ? admitInputTimeoutMs() : ACTION_POLICY[action].timeoutMs; const prepared = await prepareAndroidAutomationHelper(device); - const timeoutMs = action === 'input' ? inputTimeoutMs() : ACTION_POLICY[action].timeoutMs; try { const headers = await runAndroidSnapshotHelperSessionTouchCommand({ deviceKey, @@ -138,12 +144,31 @@ function decodeHelperIndex(value: string | undefined): number | undefined { return value !== undefined && /^\d+$/.test(value) ? Number(value) : undefined; } -/** The host waits for input up to its cap, or until the request's own deadline when that is sooner. */ -function inputTimeoutMs(): number { +/** + * The host waits for input up to its cap, or until the request's own deadline when that is sooner. + * A request too close to its deadline for one injection is refused before anything is sent. + */ +function admitInputTimeoutMs(): number { const capMs = ACTION_POLICY.input.timeoutMs; const requestDeadlineMs = getRequestDeadlineMs(getDiagnosticsMeta().requestId); - if (requestDeadlineMs === undefined) return capMs; - return Math.max(0, Math.min(capMs, requestDeadlineMs - Date.now())); + const timeoutMs = + requestDeadlineMs === undefined + ? capMs + : Math.max(0, Math.min(capMs, requestDeadlineMs - Date.now())); + const budgetMs = timeoutMs - INPUT_ANSWER_MARGIN_MS; + if (budgetMs < MIN_INPUT_BUDGET_MS) { + throw new AppError( + 'COMMAND_FAILED', + 'Android input was not sent: the request has too little time left for one injection', + { + reason: 'android-input-budget-exhausted', + budgetMs: Math.max(0, budgetMs), + minimumBudgetMs: MIN_INPUT_BUDGET_MS, + dispatched: 'no', + }, + ); + } + return timeoutMs; } function encodeHelperPayload( @@ -153,7 +178,7 @@ function encodeHelperPayload( if (!('input' in request)) return undefined; const payload = request.action === 'input' - ? { ...request.input, budgetMs: Math.max(0, timeoutMs - INPUT_ANSWER_MARGIN_MS) } + ? { ...request.input, budgetMs: timeoutMs - INPUT_ANSWER_MARGIN_MS } : request.input; return Buffer.from(JSON.stringify(payload)).toString('base64'); } From b2765daa6f19c14d3a3e7c620b49f870e49c2009 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Pierzcha=C5=82a?= Date: Mon, 16 Nov 2026 23:13:29 +0100 Subject: [PATCH 13/16] test(android): name the budget test's catch parameter error Co-Authored-By: Claude Opus 5.5 --- .../src/automation-helper-input-budget.test.ts | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/packages/platform-android/src/automation-helper-input-budget.test.ts b/packages/platform-android/src/automation-helper-input-budget.test.ts index e580875bb3..09912ecc9c 100644 --- a/packages/platform-android/src/automation-helper-input-budget.test.ts +++ b/packages/platform-android/src/automation-helper-input-budget.test.ts @@ -67,9 +67,11 @@ test('a request with room sends the helper only what remains of its deadline', a test('a request without room for one injection is refused before anything reaches the helper', async () => { for (const remainingMs of [34_000, 3_000]) { - const error = await withinRequestDeadline(remainingMs, () => tap().catch((e: unknown) => e)); - expect(error).toBeInstanceOf(AppError); - expect((error as AppError).details).toMatchObject({ + const refusal = await withinRequestDeadline(remainingMs, () => + tap().catch((error: unknown) => error), + ); + expect(refusal).toBeInstanceOf(AppError); + expect((refusal as AppError).details).toMatchObject({ reason: 'android-input-budget-exhausted', minimumBudgetMs: 30_000, dispatched: 'no', From 1a51b2e98178c2c8c30e5d082d82fbac67e42e61 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Pierzcha=C5=82a?= Date: Mon, 16 Nov 2026 23:27:00 +0100 Subject: [PATCH 14/16] docs(android): state what the input timeout covers and why a budget refusal skips the fallback Co-Authored-By: Claude Opus 5.5 --- packages/platform-android/src/automation-helper.ts | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/packages/platform-android/src/automation-helper.ts b/packages/platform-android/src/automation-helper.ts index bd072e7dbd..4b5beedee2 100644 --- a/packages/platform-android/src/automation-helper.ts +++ b/packages/platform-android/src/automation-helper.ts @@ -39,8 +39,8 @@ type AndroidHelperAppResult = | { kind: 'launched'; output: string }; const ACTION_POLICY = { - // A press can block on the framework's 30 s injection deadline, and its one retry needs 35 s of - // room (PressSequence.RETRY_WORST_CASE_MS), so input may wait longer than the other actions. + // Less the 5 s answer margin, a 40 s helper budget lets a start refused at once still afford its + // one retry (PressSequence.RETRY_WORST_CASE_MS, 35 s); a later refusal is not retried. input: { mutation: true, timeoutMs: 45_000 }, app: { mutation: true, timeoutMs: 30_000 }, screenshot: { mutation: false, timeoutMs: 30_000 }, @@ -146,7 +146,9 @@ function decodeHelperIndex(value: string | undefined): number | undefined { /** * The host waits for input up to its cap, or until the request's own deadline when that is sooner. - * A request too close to its deadline for one injection is refused before anything is sent. + * A request too close to its deadline for one injection is refused before anything is sent. The + * refusal is thrown before the helper call's fallback on purpose: that path stops the shared session + * and runs `adb shell input`, which can block on the same injection deadline. */ function admitInputTimeoutMs(): number { const capMs = ACTION_POLICY.input.timeoutMs; From 818a200b42120ed78f25a9b3432d4ecd1d666a4d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Pierzcha=C5=82a?= Date: Mon, 16 Nov 2026 23:29:12 +0100 Subject: [PATCH 15/16] fix(android): admit input on one attempt's cost and check fill's time before typing The host refused any press whose request had under 35 s left, because its admission minimum was a full 30 s injection plus the answer margin. It now admits a press when the remaining wait covers one attempt (2 s) and sends the helper the wait less the margin, down to zero, which only rules out the retry. Fill checks once after its focus tap that the accessibility interaction timeout still fits before entering text, and a helper whose host sends no budget assumes the older host's 30 s wait. Co-Authored-By: Claude Opus 5.5 --- .../AutomationCommandHandler.java | 25 +++++++++++++-- .../automation-helper-input-budget.test.ts | 31 ++++++++++--------- .../platform-android/src/automation-helper.ts | 24 +++++++------- 3 files changed, 50 insertions(+), 30 deletions(-) diff --git a/android/snapshot-helper/src/main/java/com/callstack/agentdevice/snapshothelper/AutomationCommandHandler.java b/android/snapshot-helper/src/main/java/com/callstack/agentdevice/snapshothelper/AutomationCommandHandler.java index edaa01756e..150d383271 100644 --- a/android/snapshot-helper/src/main/java/com/callstack/agentdevice/snapshothelper/AutomationCommandHandler.java +++ b/android/snapshot-helper/src/main/java/com/callstack/agentdevice/snapshothelper/AutomationCommandHandler.java @@ -57,10 +57,26 @@ private static final class InputDispatch { boolean attempted; } + /** + * The budget of a host that sends none: a provider-supplied helper can be newer than its host, and + * an older host waits 30 s for input, less the 5 s answer margin. It never affords a retry. + */ + private static final long OLDER_HOST_BUDGET_MS = 25_000; + + /** The framework's accessibility interaction timeout, AccessibilityInteractionClient. */ + private static final long TEXT_ENTRY_RESERVE_MS = 5_000; + + /** The command deadline left too little time for the next step, so it was not started. */ + static final class InputBudgetExhausted extends IllegalStateException { + InputBudgetExhausted(String message) { + super(message); + } + } + private static void input(UiAutomation automation, JSONObject input, InputDispatch dispatch) throws Exception { - // The host's remaining time for this command bounds every retry decision. A provider-supplied - // helper can be newer than its host, which sends no budget; such a press is never retried. - long deadline = SystemClock.uptimeMillis() + input.optLong("budgetMs", 0); + // The host's remaining time for this command bounds every retry decision. + long deadline = + SystemClock.uptimeMillis() + input.optLong("budgetMs", OLDER_HOST_BUDGET_MS); switch (input.getString("kind")) { case "tap": tap(automation, input.getDouble("x"), input.getDouble("y"), dispatch, deadline); @@ -218,6 +234,9 @@ private static void fill( } if (target == null) throw new IllegalStateException("No editable field at the fill point"); tap(automation, x, y, dispatch, deadline); + if (deadline - SystemClock.uptimeMillis() < TEXT_ENTRY_RESERVE_MS) { + throw new InputBudgetExhausted("Fill text was not entered: too little time left after focus"); + } if (!target.refresh()) throw new IllegalStateException("Fill target disappeared after focus"); if (!target.isFocused() && !target.performAction(AccessibilityNodeInfo.ACTION_FOCUS)) { throw new IllegalStateException("Field focus was refused"); diff --git a/packages/platform-android/src/automation-helper-input-budget.test.ts b/packages/platform-android/src/automation-helper-input-budget.test.ts index 09912ecc9c..61eabeb0f1 100644 --- a/packages/platform-android/src/automation-helper-input-budget.test.ts +++ b/packages/platform-android/src/automation-helper-input-budget.test.ts @@ -58,25 +58,26 @@ test('without a request deadline, input carries the host cap less the answer mar }); test('a request with room sends the helper only what remains of its deadline', async () => { - await withinRequestDeadline(38_000, tap); + await withinRequestDeadline(34_000, tap); const { payload, timeoutMs } = sentCommand(); - expect(timeoutMs).toBeLessThanOrEqual(38_000); - expect(timeoutMs).toBeGreaterThan(36_000); + expect(timeoutMs).toBeLessThanOrEqual(34_000); + expect(timeoutMs).toBeGreaterThan(32_000); expect(payload.budgetMs).toBe(timeoutMs - 5_000); }); -test('a request without room for one injection is refused before anything reaches the helper', async () => { - for (const remainingMs of [34_000, 3_000]) { - const refusal = await withinRequestDeadline(remainingMs, () => - tap().catch((error: unknown) => error), - ); - expect(refusal).toBeInstanceOf(AppError); - expect((refusal as AppError).details).toMatchObject({ - reason: 'android-input-budget-exhausted', - minimumBudgetMs: 30_000, - dispatched: 'no', - }); - } +test('a request with room for one attempt but not the answer margin sends a zero budget', async () => { + await withinRequestDeadline(3_000, tap); + expect(sentCommand().payload.budgetMs).toBe(0); +}); + +test('a request without room for one attempt is refused before anything reaches the helper', async () => { + const refusal = await withinRequestDeadline(1_000, () => tap().catch((error: unknown) => error)); + expect(refusal).toBeInstanceOf(AppError); + expect((refusal as AppError).details).toMatchObject({ + reason: 'android-input-budget-exhausted', + minimumMs: 2_000, + dispatched: 'no', + }); expect(runAndroidSnapshotHelperSessionTouchCommand).not.toHaveBeenCalled(); expect(prepareAndroidAutomationHelper).not.toHaveBeenCalled(); expect(stopAndroidSnapshotHelperSession).not.toHaveBeenCalled(); diff --git a/packages/platform-android/src/automation-helper.ts b/packages/platform-android/src/automation-helper.ts index 4b5beedee2..3331fad5de 100644 --- a/packages/platform-android/src/automation-helper.ts +++ b/packages/platform-android/src/automation-helper.ts @@ -51,10 +51,11 @@ const ACTION_POLICY = { const INPUT_ANSWER_MARGIN_MS = 5_000; /** - * The least helper budget a press is sent with: one synchronous injection can block until the - * framework's 30 s deadline, and a host that stopped waiting first would tear the session down. + * The least wait a press is sent with: one injection attempt round trip. On an emulator a whole + * press takes 0.25-0.4 s through the helper or `adb shell input`, so 2 s keeps a wide margin; + * with less, neither path can be expected to answer before the host stops waiting. */ -const MIN_INPUT_BUDGET_MS = 30_000; +const MIN_INPUT_ATTEMPT_MS = 2_000; /** Reuses the session's automation context without starting instrumentation for sessionless input. */ export function runAndroidAutomationHelper( @@ -146,9 +147,9 @@ function decodeHelperIndex(value: string | undefined): number | undefined { /** * The host waits for input up to its cap, or until the request's own deadline when that is sooner. - * A request too close to its deadline for one injection is refused before anything is sent. The - * refusal is thrown before the helper call's fallback on purpose: that path stops the shared session - * and runs `adb shell input`, which can block on the same injection deadline. + * A request with no room for one attempt is refused before anything is sent. The refusal is thrown + * before the helper call's fallback on purpose: that path stops the shared session, and + * `adb shell input` could not answer in that time either. */ function admitInputTimeoutMs(): number { const capMs = ACTION_POLICY.input.timeoutMs; @@ -157,15 +158,14 @@ function admitInputTimeoutMs(): number { requestDeadlineMs === undefined ? capMs : Math.max(0, Math.min(capMs, requestDeadlineMs - Date.now())); - const budgetMs = timeoutMs - INPUT_ANSWER_MARGIN_MS; - if (budgetMs < MIN_INPUT_BUDGET_MS) { + if (timeoutMs < MIN_INPUT_ATTEMPT_MS) { throw new AppError( 'COMMAND_FAILED', - 'Android input was not sent: the request has too little time left for one injection', + 'Android input was not sent: the request has too little time left for one attempt', { reason: 'android-input-budget-exhausted', - budgetMs: Math.max(0, budgetMs), - minimumBudgetMs: MIN_INPUT_BUDGET_MS, + remainingMs: timeoutMs, + minimumMs: MIN_INPUT_ATTEMPT_MS, dispatched: 'no', }, ); @@ -180,7 +180,7 @@ function encodeHelperPayload( if (!('input' in request)) return undefined; const payload = request.action === 'input' - ? { ...request.input, budgetMs: timeoutMs - INPUT_ANSWER_MARGIN_MS } + ? { ...request.input, budgetMs: Math.max(0, timeoutMs - INPUT_ANSWER_MARGIN_MS) } : request.input; return Buffer.from(JSON.stringify(payload)).toString('base64'); } From 218e157c4fd1a5314d7a44a66247ec860cf8f64e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Pierzcha=C5=82a?= Date: Mon, 16 Nov 2026 23:48:21 +0100 Subject: [PATCH 16/16] test(android): pin fill's typed refusal when text entry no longer fits Moves fill's post-tap budget check into requireTextEntryTime, unchanged, so the helper's JVM suite covers both sides of its 5 s boundary. Co-Authored-By: Claude Opus 5.5 --- .../AutomationCommandHandler.java | 11 +++++--- .../snapshothelper/FillTextEntryTimeTest.java | 27 +++++++++++++++++++ .../SnapshotHelperTestSuite.java | 1 + 3 files changed, 36 insertions(+), 3 deletions(-) create mode 100644 android/snapshot-helper/src/test/java/com/callstack/agentdevice/snapshothelper/FillTextEntryTimeTest.java diff --git a/android/snapshot-helper/src/main/java/com/callstack/agentdevice/snapshothelper/AutomationCommandHandler.java b/android/snapshot-helper/src/main/java/com/callstack/agentdevice/snapshothelper/AutomationCommandHandler.java index 150d383271..7e1f63eb75 100644 --- a/android/snapshot-helper/src/main/java/com/callstack/agentdevice/snapshothelper/AutomationCommandHandler.java +++ b/android/snapshot-helper/src/main/java/com/callstack/agentdevice/snapshothelper/AutomationCommandHandler.java @@ -73,6 +73,13 @@ static final class InputBudgetExhausted extends IllegalStateException { } } + /** Refuses text entry the deadline cannot fit, after a focus tap that may have used the budget. */ + static void requireTextEntryTime(long deadline, long now) { + if (deadline - now < TEXT_ENTRY_RESERVE_MS) { + throw new InputBudgetExhausted("Fill text was not entered: too little time left after focus"); + } + } + private static void input(UiAutomation automation, JSONObject input, InputDispatch dispatch) throws Exception { // The host's remaining time for this command bounds every retry decision. long deadline = @@ -234,9 +241,7 @@ private static void fill( } if (target == null) throw new IllegalStateException("No editable field at the fill point"); tap(automation, x, y, dispatch, deadline); - if (deadline - SystemClock.uptimeMillis() < TEXT_ENTRY_RESERVE_MS) { - throw new InputBudgetExhausted("Fill text was not entered: too little time left after focus"); - } + requireTextEntryTime(deadline, SystemClock.uptimeMillis()); if (!target.refresh()) throw new IllegalStateException("Fill target disappeared after focus"); if (!target.isFocused() && !target.performAction(AccessibilityNodeInfo.ACTION_FOCUS)) { throw new IllegalStateException("Field focus was refused"); diff --git a/android/snapshot-helper/src/test/java/com/callstack/agentdevice/snapshothelper/FillTextEntryTimeTest.java b/android/snapshot-helper/src/test/java/com/callstack/agentdevice/snapshothelper/FillTextEntryTimeTest.java new file mode 100644 index 0000000000..1aa9467942 --- /dev/null +++ b/android/snapshot-helper/src/test/java/com/callstack/agentdevice/snapshothelper/FillTextEntryTimeTest.java @@ -0,0 +1,27 @@ +package com.callstack.agentdevice.snapshothelper; + +public final class FillTextEntryTimeTest { + private FillTextEntryTimeTest() {} + + static void run() { + entersTextWhenTheAccessibilityTimeoutStillFits(); + refusesTextEntryWithATypedBudgetRefusalWhenItNoLongerFits(); + } + + private static void entersTextWhenTheAccessibilityTimeoutStillFits() { + AutomationCommandHandler.requireTextEntryTime(15_000, 10_000); + } + + private static void refusesTextEntryWithATypedBudgetRefusalWhenItNoLongerFits() { + try { + AutomationCommandHandler.requireTextEntryTime(14_999, 10_000); + } catch (AutomationCommandHandler.InputBudgetExhausted refusal) { + String expected = "Fill text was not entered: too little time left after focus"; + if (!expected.equals(refusal.getMessage())) { + throw new AssertionError("Expected " + expected + ", got " + refusal.getMessage()); + } + return; + } + throw new AssertionError("Expected the text entry to be refused"); + } +} diff --git a/android/snapshot-helper/src/test/java/com/callstack/agentdevice/snapshothelper/SnapshotHelperTestSuite.java b/android/snapshot-helper/src/test/java/com/callstack/agentdevice/snapshothelper/SnapshotHelperTestSuite.java index 641215943a..43209ba1b0 100644 --- a/android/snapshot-helper/src/test/java/com/callstack/agentdevice/snapshothelper/SnapshotHelperTestSuite.java +++ b/android/snapshot-helper/src/test/java/com/callstack/agentdevice/snapshothelper/SnapshotHelperTestSuite.java @@ -6,6 +6,7 @@ private SnapshotHelperTestSuite() {} public static void main(String[] args) throws Exception { PointerEventScheduleTest.run(); PressSequenceTest.run(); + FillTextEntryTimeTest.run(); AccessibilityCaptureStabilizerTest.run(); AccessibilityReadBatchTest.run(); AccessibilityTreeXmlTest.run();