Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 48 additions & 1 deletion packages/platform-android/src/__tests__/input-actions.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -499,7 +499,12 @@ test('pressAndroid dispatches no failure suppression even when one is smuggled p
});

// The orientation settle polls at its own interval; the clock is the assertion, not the wait.
vi.mock('@agent-device/host-kit/retry', () => ({ sleep: async () => {} }));
// A fake clock makes the wait consume that clock, as a real wait does.
vi.mock('@agent-device/host-kit/retry', () => ({
sleep: async (ms: number) => {
if (vi.isFakeTimers()) vi.setSystemTime(Date.now() + ms);
},
}));

const ORIENTATION_CALLS = [
['shell', 'settings', 'put', 'system', 'accelerometer_rotation', '0'],
Expand Down Expand Up @@ -559,6 +564,48 @@ test('setAndroidOrientation fails when the display never reports the requested r
}
});

test('positive-cost display reads end the settle on the last rotation, never on a starved read', async () => {
// #4562: main slept to the deadline and admitted one final read on the milliseconds left, so a
// real 100 ms `dumpsys display` overran it and its adb timeout replaced the "did not take
// effect" fact a pinned foreground app must see. The loop now keeps one read's reserve (300 ms)
// unspent by the wait: every admitted read can answer, and the last one gets exactly the reserve.
vi.useFakeTimers({ now: 0, toFake: ['Date'] });
const probeBudgets: number[] = [];
let reads = 0;
try {
await withFakeAdb(
(args, options) => {
if (args[1] !== 'dumpsys') return undefined;
const budgetMs = options?.timeoutMs ?? 0;
probeBudgets.push(budgetMs);
const costMs = reads === 0 ? 50 : 100;
reads += 1;
vi.setSystemTime(Date.now() + Math.min(costMs, budgetMs));
if (budgetMs < costMs)
return new Error(`adb shell dumpsys display timed out after ${budgetMs}ms`);
return ' mCurrentOrientation=0\n';
},
async ({ device }) => {
await assert.rejects(setAndroidOrientation(device, 'landscape-left'), (error: unknown) => {
assert.ok(error instanceof AppError);
assert.doesNotMatch(error.message, /could not confirm the display rotation/);
assert.match(
error.message,
/did not take effect: the display still reports rotation 0 after 15000ms/,
);
assert.equal(error.details?.observedRotation, 0);
return true;
});
},
);
assert.equal(reads, 26);
assert.equal(Math.min(...probeBudgets), 300);
assert.equal(probeBudgets.at(-1), 300);
} finally {
vi.useRealTimers();
}
});

test('a display probe that hangs for the whole budget ends the settle as a failure', async () => {
vi.useFakeTimers({ now: 0, toFake: ['Date'] });
try {
Expand Down
12 changes: 9 additions & 3 deletions packages/platform-android/src/input-actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,9 @@

const ORIENTATION_SETTLE_TIMEOUT_MS = 15_000;
const ORIENTATION_SETTLE_POLL_MS = 500;
// A `dumpsys display` read takes 20–280 ms on a loaded device; a read admitted with less left
// than this can never answer, and its adb timeout would mask the "did not take effect" fact (#4562).
const MIN_ORIENTATION_READ_BUDGET_MS = 300;

/**
* The display rotates some time after the setting lands; on a loaded emulator that takes
Expand All @@ -164,10 +167,13 @@
): Promise<void> {
const deadline = Date.now() + ORIENTATION_SETTLE_TIMEOUT_MS;
let observed = await readAndroidDisplayRotation(device, orientation, deadline);
while (observed !== undefined && observed !== userRotation && Date.now() < deadline) {
await sleep(Math.min(ORIENTATION_SETTLE_POLL_MS, remainingMs(deadline)));
while (observed !== undefined && observed !== userRotation) {
// The wait spends only what is above one read's reserve, so every admitted read can answer.
const leftMs = deadline - Date.now();
if (leftMs < MIN_ORIENTATION_READ_BUDGET_MS) break;
await sleep(Math.min(ORIENTATION_SETTLE_POLL_MS, leftMs - MIN_ORIENTATION_READ_BUDGET_MS));
observed = await readAndroidDisplayRotation(device, orientation, deadline);
}

Check warning on line 176 in packages/platform-android/src/input-actions.ts

View check run for this annotation

apex-sentinel-staging / Apex Sentinel

minor: Reserve is checked before the sleep, so the final read can still be admitted starved

On a loaded host `sleep` returns late, so `deadline - Date.now()` after the wait can fall below `MIN_ORIENTATION_READ_BUDGET_MS`; the read then overruns and throws an adb timeout instead of the real "did not take effect" failure. The loop checks the reserve before sleeping (line 173) but never re-checks after the sleep before admitting the read (line 175).
Comment thread
thymikee marked this conversation as resolved.
Resolved
if (observed === undefined || observed === userRotation) return;
throw new AppError(
'COMMAND_FAILED',
Expand Down Expand Up @@ -238,7 +244,7 @@
*/
export async function probeAndroidDisplayRotation(
device: DeviceInfo,
options: { timeoutMs: number; signal: AbortSignal },
options: { timeoutMs: number; signal?: AbortSignal },
): Promise<DeviceRotation | undefined> {
try {
const indices = await readAndroidDisplayRotationIndices(device, options);
Expand Down
Loading