Skip to content

Commit f3b86f1

Browse files
committed
test(android): the fill sampler paces itself by an injectable clock, so its tests advance time instead of waiting it out
1 parent 9d29a59 commit f3b86f1

2 files changed

Lines changed: 48 additions & 15 deletions

File tree

‎packages/platform-android/src/__tests__/fill-verification.test.ts‎

Lines changed: 36 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import { ANDROID_SNAPSHOT_HELPER_FIXTURE_ARTIFACT } from './test-utils/android-s
1515
import {
1616
readAndroidFillTargetBeforeMutation,
1717
verifyAndroidFilledText,
18+
type FillVerificationClock,
1819
} from '../fill-verification.ts';
1920
import { resetAndroidSnapshotHelperSessions } from '../snapshot-helper-session-lifecycle.ts';
2021
import {
@@ -93,43 +94,66 @@ test('verification samples re-read the hierarchy instead of sharing one capture'
9394
});
9495

9596
test('a value the app renders late still verifies, as long as it lands before the deadline', async () => {
96-
// Six samples of the hint before the value shows: about 900 ms in, past the retired schedule's
97-
// last sample at 500 ms and inside the deadline.
97+
// Six samples of the hint before the value shows: 900 ms in, past the retired schedule's last
98+
// sample at 500 ms and inside the deadline.
9899
const session = createFillHelperSession({
99100
textForCapture: (captureIndex) => (captureIndex <= 6 ? 'Key echo' : 'chips'),
100101
});
102+
const clock = createFakeClock();
101103

102104
const verification = await withFillHelperProvider(
103105
session.provider,
104106
async () =>
105-
await verifyAndroidFilledText(device, 10, 10, 'chips', {
106-
helperSessionScope: 'daemon-session',
107-
}),
107+
await verifyAndroidFilledText(
108+
device,
109+
10,
110+
10,
111+
'chips',
112+
{ helperSessionScope: 'daemon-session' },
113+
clock,
114+
),
108115
);
109116

110117
assert.equal(verification.ok, true);
111118
assert.equal(session.captureCount(), 8, 'six hint samples, then the value held for two');
119+
assert.equal(clock.now(), 1050, 'the value held from the 900 ms sample to the 1050 ms one');
112120
});
113121

114122
test('a value that never lands is a mismatch at the deadline, not before it', async () => {
115123
const session = createFillHelperSession({ textForCapture: () => 'Key echo' });
116-
const startedAt = Date.now();
124+
const clock = createFakeClock();
117125

118126
const verification = await withFillHelperProvider(
119127
session.provider,
120128
async () =>
121-
await verifyAndroidFilledText(device, 10, 10, 'chips', {
122-
helperSessionScope: 'daemon-session',
123-
}),
129+
await verifyAndroidFilledText(
130+
device,
131+
10,
132+
10,
133+
'chips',
134+
{ helperSessionScope: 'daemon-session' },
135+
clock,
136+
),
124137
);
125138

126139
assert.equal(verification.ok, false);
127140
assert.equal(verification.reason, 'text_mismatch');
128-
assert.equal(verification.actual, 'Key echo', 'the last sample is the answer');
129-
assert.ok(Date.now() - startedAt >= 1500, 'sampling ran to the deadline');
130-
assert.ok(session.captureCount() >= 4, 'the deadline admits more samples than the old schedule');
141+
assert.equal(verification.actual, 'Key echo', 'the sample at the deadline is the answer');
142+
assert.equal(clock.now(), 1500, 'sampling ran to the deadline and not past it');
143+
assert.equal(session.captureCount(), 11, 'a sample every 150 ms from 0 to 1500');
131144
});
132145

146+
/** A clock the sampler advances by sleeping, so the deadline is reached without waiting it out. */
147+
function createFakeClock(): FillVerificationClock & { now(): number } {
148+
let elapsed = 0;
149+
return {
150+
now: () => elapsed,
151+
sleep: async (milliseconds) => {
152+
elapsed += milliseconds;
153+
},
154+
};
155+
}
156+
133157
test('the pre-action target read shares the daemon-session helper with the samples', async () => {
134158
const session = createFillHelperSession();
135159

‎packages/platform-android/src/fill-verification.ts‎

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -55,29 +55,38 @@ type AndroidFillVerificationContext = {
5555
const FILL_VERIFICATION_SAMPLE_INTERVAL_MS = 150;
5656
const FILL_VERIFICATION_DEADLINE_MS = 1500;
5757

58+
/** The clock the sampler paces itself by; a test hands in one it advances instead of waiting. */
59+
export type FillVerificationClock = Readonly<{
60+
now(): number;
61+
sleep(milliseconds: number): Promise<void>;
62+
}>;
63+
64+
const wallClock: FillVerificationClock = Object.freeze({ now: () => Date.now(), sleep });
65+
5866
export async function verifyAndroidFilledText(
5967
device: DeviceInfo,
6068
x: number,
6169
y: number,
6270
expected: string,
6371
helper: AndroidHelperSessionOptions = {},
72+
clock: FillVerificationClock = wallClock,
6473
): Promise<AndroidFillVerification> {
6574
const context = await readAndroidFillVerificationContext(device);
66-
const deadline = Date.now() + FILL_VERIFICATION_DEADLINE_MS;
75+
const deadline = clock.now() + FILL_VERIFICATION_DEADLINE_MS;
6776
let previous: AndroidFillVerification | null = null;
6877

6978
for (;;) {
7079
const verification = await inspectAndroidFilledText(device, x, y, expected, context, helper);
7180
if (verification.reason === 'ime_capture') {
7281
return verification;
7382
}
74-
const remainingMs = deadline - Date.now();
83+
const remainingMs = deadline - clock.now();
7584
if ((verification.ok && previous?.ok) || remainingMs <= 0) {
7685
return verification;
7786
}
7887
previous = verification;
7988
// The last sleep ends at the deadline, so the final sample starts there and not an interval later.
80-
await sleep(Math.min(FILL_VERIFICATION_SAMPLE_INTERVAL_MS, remainingMs));
89+
await clock.sleep(Math.min(FILL_VERIFICATION_SAMPLE_INTERVAL_MS, remainingMs));
8190
}
8291
}
8392

0 commit comments

Comments
 (0)