Skip to content

Commit dce8c32

Browse files
thymikeeclaude
andcommitted
test(android): wait on the InputMethod window before comparing keyboard pixels
The keyboard dismissal check compared screenshots taken as soon as mInputShown flipped. That flag changes when the show or hide request is accepted, not when the keyboard is drawn or removed, so both captures could show the same pixels. Wait for the InputMethod window to be drawn before the first capture and gone before the second, and fail with a message naming which one did not happen. Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
1 parent 27d87b1 commit dce8c32

5 files changed

Lines changed: 109 additions & 1 deletion

File tree

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ export async function readAndroidDeviceEvidence(
8484
return `${sections.join('\n\n')}\n`;
8585
}
8686

87-
function createAdbRead(): AndroidAdbRead {
87+
export function createAdbRead(): AndroidAdbRead {
8888
return async (args) => {
8989
const { stdout } = await execFileAsync('adb', args, {
9090
maxBuffer: 64 * 1024 * 1024,

‎test/integration/android-emulator-e2e/live-form-scenario.ts‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import {
1212
requireAndroidResourceId,
1313
} from './live-assertions.ts';
1414
import { type LiveContext, runStep, verifyBehavior, verifyCommand } from './live-harness.ts';
15+
import { waitForImeWindow } from './live-ime-window.ts';
1516

1617
const C = PUBLIC_COMMANDS;
1718
export const ANDROID_TEST_IME_PACKAGE = 'com.callstack.agentdevice.imehelper';
@@ -119,6 +120,11 @@ export async function assertKeyboardIme(context: LiveContext): Promise<void> {
119120
JSON.stringify(keyboardStatus.json),
120121
);
121122

123+
await waitForImeWindow(
124+
context,
125+
true,
126+
'Android keyboard was reported visible but its InputMethod window never drew',
127+
);
122128
const keyboardVisiblePath = path.join(context.artifactDir, 'keyboard-visible.png');
123129
await capturePng(context, 'capture visible Android keyboard', keyboardVisiblePath);
124130
const dismiss = await runStep(context, 'dismiss Android keyboard safely', [
@@ -133,6 +139,11 @@ export async function assertKeyboardIme(context: LiveContext): Promise<void> {
133139
JSON.stringify(dismiss.json),
134140
);
135141

142+
await waitForImeWindow(
143+
context,
144+
false,
145+
'Android keyboard was reported hidden but its InputMethod window is still drawn',
146+
);
136147
const keyboardHiddenPath = path.join(context.artifactDir, 'keyboard-hidden.png');
137148
await capturePng(context, 'capture dismissed Android keyboard', keyboardHiddenPath);
138149
assertFilesDiffer(
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
import { expect, test } from 'vitest';
2+
3+
import { isImeWindowDrawn } from './live-ime-window.ts';
4+
5+
const APP_WINDOW = [
6+
' Window #7 Window{9a1c2b3 u0 com.callstack.agentdevicelab/com.callstack.agentdevicelab.MainActivity}:',
7+
' mViewVisibility=0x0 mHaveFrame=true mObscured=false',
8+
' mHasSurface=true isReadyForDisplay()=true mWindowRemovalAllowed=false',
9+
' Surface: shown=true mDrawState=HAS_DRAWN mLastHidden=false',
10+
].join('\n');
11+
12+
const IME_DRAWN = [
13+
' Window #6 Window{c137705 u0 InputMethod}:',
14+
' mViewVisibility=0x0 mHaveFrame=true mObscured=false',
15+
' mHasSurface=true isReadyForDisplay()=true mWindowRemovalAllowed=false',
16+
' Surface: shown=true mDrawState=HAS_DRAWN mLastHidden=false',
17+
].join('\n');
18+
19+
const IME_REMOVED = [
20+
' Window #6 Window{c137705 u0 InputMethod}:',
21+
' mViewVisibility=0x8 mHaveFrame=true mObscured=false',
22+
' mHasSurface=false isReadyForDisplay()=false mWindowRemovalAllowed=false',
23+
' mDrawState=NO_SURFACE mLastHidden=false',
24+
].join('\n');
25+
26+
const IME_SHOW_REQUESTED = [
27+
' Window #6 Window{c137705 u0 InputMethod}:',
28+
' mViewVisibility=0x0 mHaveFrame=true mObscured=false',
29+
' mHasSurface=true isReadyForDisplay()=true mWindowRemovalAllowed=false',
30+
' Surface: shown=true mDrawState=COMMIT_DRAW_PENDING mLastHidden=false',
31+
].join('\n');
32+
33+
test('a drawn InputMethod window reads as drawn', () => {
34+
expect(isImeWindowDrawn(`${IME_DRAWN}\n${APP_WINDOW}`)).toBe(true);
35+
});
36+
37+
test('an InputMethod window that is last in the dump reads as drawn', () => {
38+
expect(isImeWindowDrawn(`${APP_WINDOW}\n${IME_DRAWN}`)).toBe(true);
39+
});
40+
41+
test('a removed InputMethod window reads as not drawn even when the app window below it is drawn', () => {
42+
expect(isImeWindowDrawn(`${IME_REMOVED}\n${APP_WINDOW}`)).toBe(false);
43+
});
44+
45+
test('an InputMethod window still waiting for its first draw reads as not drawn', () => {
46+
expect(isImeWindowDrawn(`${IME_SHOW_REQUESTED}\n${APP_WINDOW}`)).toBe(false);
47+
});
48+
49+
test('a dump without an InputMethod window reads as not drawn', () => {
50+
expect(isImeWindowDrawn(APP_WINDOW)).toBe(false);
51+
});
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
import assert from 'node:assert/strict';
2+
3+
import { deviceShellArgv } from '@agent-device/kernel/device-shell';
4+
import { type AndroidAdbRead, createAdbRead } from './device-evidence.ts';
5+
import type { LiveContext } from './live-harness.ts';
6+
7+
const IME_WINDOW_TIMEOUT_MS = 10_000;
8+
const IME_WINDOW_POLL_MS = 250;
9+
10+
const IME_WINDOW_HEADER = /^\s*Window #\d+ Window\{[0-9a-f]+ u\d+ InputMethod\}:/;
11+
const ANY_WINDOW_HEADER = /^\s*Window #\d+ Window\{/;
12+
13+
/**
14+
* Whether the InputMethod window has a drawn surface. `mInputShown` flips when the show request is
15+
* accepted, before the keyboard is painted, and `keyboard dismiss` reports hidden before the hide
16+
* animation has removed the surface. A screenshot is only comparable once this settles.
17+
*/
18+
export function isImeWindowDrawn(windowDump: string): boolean {
19+
const lines = windowDump.split('\n');
20+
const start = lines.findIndex((line) => IME_WINDOW_HEADER.test(line));
21+
if (start === -1) return false;
22+
const next = lines.findIndex((line, index) => index > start && ANY_WINDOW_HEADER.test(line));
23+
const block = lines.slice(start, next === -1 ? undefined : next).join('\n');
24+
return /\bisReadyForDisplay\(\)=true\b/.test(block) && /\bmDrawState=HAS_DRAWN\b/.test(block);
25+
}
26+
27+
export async function waitForImeWindow(
28+
context: LiveContext,
29+
drawn: boolean,
30+
failure: string,
31+
read: AndroidAdbRead = createAdbRead(),
32+
): Promise<void> {
33+
const argv = deviceShellArgv(
34+
'adb',
35+
'shell',
36+
['dumpsys', 'window', 'windows'],
37+
['-s', context.serial],
38+
);
39+
const deadline = Date.now() + IME_WINDOW_TIMEOUT_MS;
40+
do {
41+
if (isImeWindowDrawn(await read(argv)) === drawn) return;
42+
await new Promise((resolve) => setTimeout(resolve, IME_WINDOW_POLL_MS));
43+
} while (Date.now() < deadline);
44+
assert.fail(`${failure} within ${IME_WINDOW_TIMEOUT_MS}ms`);
45+
}

‎vitest.config.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,7 @@ export default defineConfig({
235235
// The Android failed-step evidence reader: it replays adb output through the probe
236236
// seam, so the crash/process/activity selectors need no emulator to be pinned.
237237
'test/integration/android-emulator-e2e/device-evidence.test.ts',
238+
'test/integration/android-emulator-e2e/live-ime-window.test.ts',
238239
// The frozen replay-compat corpus (#1417): parse-only, no device or
239240
// subprocess work, so it belongs in the fast lane next to the
240241
// grammar it guards.

0 commit comments

Comments
 (0)