Skip to content

Commit d6f6dc3

Browse files
thymikeeclaude
andcommitted
test(android): accept a dropped InputMethod window after dismissal
After `keyboard dismiss` Android can drop the InputMethod window record from the window dump, so the hidden wait timed out on a keyboard that was already gone. The parser now tells a window dump without an InputMethod window (absent) apart from output with no window record at all (unreadable). The hidden wait accepts absent or not drawn, the visible wait still needs a drawn window, and unreadable output never settles either wait. createAdbRead now refuses a non-positive timeout, because zero disables the child-process timeout. The parser doc comment moves back onto the function it describes. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent b7958a9 commit d6f6dc3

4 files changed

Lines changed: 71 additions & 18 deletions

File tree

‎test/integration/android-emulator-e2e/device-evidence.test.ts‎

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
import { expect, test } from 'vitest';
22

3-
import { readAndroidDeviceEvidence, type AndroidAdbRead } from './device-evidence.ts';
3+
import {
4+
createAdbRead,
5+
readAndroidDeviceEvidence,
6+
type AndroidAdbRead,
7+
} from './device-evidence.ts';
48

59
const TARGET = { appId: 'com.callstack.agentdevicelab', serial: 'emulator-5554' };
610

@@ -106,3 +110,7 @@ test('the app id reaches the device shell as one word, never as a script', async
106110
String.raw`'com.lab'\''; reboot'`,
107111
]);
108112
});
113+
114+
test('an adb read refuses a timeout that would disable the child-process timeout', async () => {
115+
await expect(createAdbRead()(['version'], { timeoutMs: 0 })).rejects.toThrow(RangeError);
116+
});

‎test/integration/android-emulator-e2e/device-evidence.ts‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,9 +89,12 @@ export async function readAndroidDeviceEvidence(
8989

9090
export function createAdbRead(): AndroidAdbRead {
9191
return async (args, options) => {
92+
const timeout = options?.timeoutMs ?? DEVICE_PROBE_TIMEOUT_MS;
93+
// A zero timeout disables the child-process timeout instead of expiring at once.
94+
if (!(timeout > 0)) throw new RangeError(`adb read timeout must be positive, got ${timeout}`);
9295
const { stdout } = await execFileAsync('adb', args, {
9396
maxBuffer: 64 * 1024 * 1024,
94-
timeout: options?.timeoutMs ?? DEVICE_PROBE_TIMEOUT_MS,
97+
timeout,
9598
});
9699
return stdout;
97100
};

‎test/integration/android-emulator-e2e/live-ime-window.test.ts‎

Lines changed: 34 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,13 @@ test('an InputMethod window still waiting for its first draw reads as not drawn'
4848
expect(readImeWindowState(`${IME_SHOW_REQUESTED}\n${APP_WINDOW}`)).toBe('not-drawn');
4949
});
5050

51-
test('a dump without an InputMethod window reads as missing, not as not drawn', () => {
52-
expect(readImeWindowState(APP_WINDOW)).toBe('missing');
53-
expect(readImeWindowState("Can't find service: window\n")).toBe('missing');
51+
test('a window dump without an InputMethod window reads as absent', () => {
52+
expect(readImeWindowState(APP_WINDOW)).toBe('absent');
53+
});
54+
55+
test('output without any window record reads as unreadable, not as absent', () => {
56+
expect(readImeWindowState("Can't find service: window\n")).toBe('unreadable');
57+
expect(readImeWindowState('')).toBe('unreadable');
5458
});
5559

5660
beforeEach(() => {
@@ -92,16 +96,41 @@ test('a wait whose adb reads keep failing reports the last read error at the dea
9296
});
9397
});
9498

95-
test('a hidden wait never satisfied by a dump that has no InputMethod window fails with a dump excerpt', async () => {
99+
test('a hidden wait passes once the InputMethod window record is gone from a window dump', async () => {
96100
const result = await settle(
97101
waitForImeWindow(DEVICE, false, 'still drawn', () => Promise.resolve(APP_WINDOW)),
98102
);
99103

104+
expect(result.status).toBe('fulfilled');
105+
});
106+
107+
test('a hidden wait never satisfied by unreadable dumps fails with a dump excerpt', async () => {
108+
const result = await settle(
109+
waitForImeWindow(DEVICE, false, 'still drawn', () =>
110+
Promise.resolve("Can't find service: window\n"),
111+
),
112+
);
113+
114+
expect(result).toMatchObject({
115+
status: 'rejected',
116+
reason: {
117+
message: expect.stringMatching(
118+
/still drawn within 10000ms.*no window records.*Can't find service/s,
119+
),
120+
},
121+
});
122+
});
123+
124+
test('a visible wait never satisfied by a dump without an InputMethod window fails with a dump excerpt', async () => {
125+
const result = await settle(
126+
waitForImeWindow(DEVICE, true, 'never drew', () => Promise.resolve(APP_WINDOW)),
127+
);
128+
100129
expect(result).toMatchObject({
101130
status: 'rejected',
102131
reason: {
103132
message: expect.stringMatching(
104-
/still drawn within 10000ms.*no InputMethod window.*Window #7/s,
133+
/never drew within 10000ms.*no InputMethod window.*Window #7/s,
105134
),
106135
},
107136
});

‎test/integration/android-emulator-e2e/live-ime-window.ts‎

Lines changed: 24 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -12,18 +12,21 @@ const DUMP_EXCERPT_LENGTH = 400;
1212
const IME_WINDOW_HEADER = /^\s*Window #\d+ Window\{[0-9a-f]+ u\d+ InputMethod\}:/;
1313
const ANY_WINDOW_HEADER = /^\s*Window #\d+ Window\{/;
1414

15+
type ImeWindowState = 'drawn' | 'not-drawn' | 'absent' | 'unreadable';
16+
1517
/**
1618
* Whether the InputMethod window has a drawn surface. `mInputShown` flips when the show request is
1719
* accepted, before the keyboard is painted, and `keyboard dismiss` reports hidden before the hide
18-
* animation has removed the surface. A screenshot is only comparable once this settles. `missing`
19-
* means the dump names no InputMethod window, which says nothing about the keyboard.
20+
* animation has removed the surface. A screenshot is only comparable once this settles. `absent`
21+
* means the dump lists windows but no InputMethod window, which Android does after a dismissal.
22+
* `unreadable` means the output holds no window record at all, so it says nothing about the keyboard.
2023
*/
21-
type ImeWindowState = 'drawn' | 'not-drawn' | 'missing';
22-
2324
export function readImeWindowState(windowDump: string): ImeWindowState {
2425
const lines = windowDump.split('\n');
2526
const start = lines.findIndex((line) => IME_WINDOW_HEADER.test(line));
26-
if (start === -1) return 'missing';
27+
if (start === -1) {
28+
return lines.some((line) => ANY_WINDOW_HEADER.test(line)) ? 'absent' : 'unreadable';
29+
}
2730
const next = lines.findIndex((line, index) => index > start && ANY_WINDOW_HEADER.test(line));
2831
const block = lines.slice(start, next === -1 ? undefined : next).join('\n');
2932
return /\bisReadyForDisplay\(\)=true\b/.test(block) && /\bmDrawState=HAS_DRAWN\b/.test(block)
@@ -43,18 +46,15 @@ export async function waitForImeWindow(
4346
['dumpsys', 'window', 'windows'],
4447
['-s', device.serial],
4548
);
46-
const wanted: ImeWindowState = drawn ? 'drawn' : 'not-drawn';
49+
const wanted: readonly ImeWindowState[] = drawn ? ['drawn'] : ['not-drawn', 'absent'];
4750
const deadline = Date.now() + IME_WINDOW_TIMEOUT_MS;
4851
let lastObservation = 'no adb read finished';
4952
for (let remaining = IME_WINDOW_TIMEOUT_MS; remaining > 0; remaining = deadline - Date.now()) {
5053
try {
5154
const dump = await read(argv, { timeoutMs: Math.min(remaining, IME_WINDOW_READ_TIMEOUT_MS) });
5255
const state = readImeWindowState(dump);
53-
if (state === wanted) return;
54-
lastObservation =
55-
state === 'missing'
56-
? `no InputMethod window in the dump: ${dump.trim().slice(0, DUMP_EXCERPT_LENGTH)}`
57-
: `InputMethod window was ${state}`;
56+
if (wanted.includes(state)) return;
57+
lastObservation = describeImeWindowState(state, dump);
5858
} catch (error) {
5959
lastObservation = `last adb read failed: ${error instanceof Error ? error.message : String(error)}`;
6060
}
@@ -63,3 +63,16 @@ export async function waitForImeWindow(
6363
}
6464
assert.fail(`${failure} within ${IME_WINDOW_TIMEOUT_MS}ms (${lastObservation})`);
6565
}
66+
67+
function describeImeWindowState(state: ImeWindowState, dump: string): string {
68+
const excerpt = dump.trim().slice(0, DUMP_EXCERPT_LENGTH);
69+
switch (state) {
70+
case 'drawn':
71+
case 'not-drawn':
72+
return `InputMethod window was ${state}`;
73+
case 'absent':
74+
return `no InputMethod window in the dump: ${excerpt}`;
75+
case 'unreadable':
76+
return `no window records in the dump: ${excerpt}`;
77+
}
78+
}

0 commit comments

Comments
 (0)