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
3 changes: 2 additions & 1 deletion packages/core/src/serve-emu.ts
Original file line number Diff line number Diff line change
Expand Up @@ -400,7 +400,8 @@ export class ServeEmu {
const backend = await this.start(id);
const adb = await adbPath();
const [tree, health, display] = await Promise.all([
this.json(backend.url, "/api/accessibility"),
// The tree must line up with the current screen even immediately after an input.
this.json(backend.url, "/api/accessibility", { headers: { "Cache-Control": "no-cache" } }),
this.health(backend.url),
execute(adb, ["-s", id, "shell", "wm", "size"], { timeout: 3000, maxBuffer: 4096 }),
]);
Expand Down
5 changes: 3 additions & 2 deletions packages/runtimes/serve-emu/src/api/routes/inspection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,11 +88,12 @@ export function inspectionRoutes(): ApiRoute<ApiDependencies>[] {
{
method: "GET",
path: "/api/accessibility",
handler: async ({ deps }) => {
handler: async ({ deps, request }) => {
const { readAccessibilitySnapshot, requestContext, errorResponse } =
deps;
try {
return Response.json(await readAccessibilitySnapshot(requestContext));
const cacheMs = request.headers.get("cache-control") === "no-cache" ? 0 : undefined;
return Response.json(await readAccessibilitySnapshot(requestContext, cacheMs));
} catch (err) {
return errorResponse(err);
}
Expand Down
2 changes: 1 addition & 1 deletion packages/runtimes/serve-emu/src/device-session-context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ export class ActiveDeviceSession<
this.assertUsable();
const now = this.#now();
if (
this.#accessibilitySnapshotCache &&
cacheMs > 0 && this.#accessibilitySnapshotCache &&
this.#accessibilitySnapshotCache.expiresMs > now
) {
return Promise.resolve(this.#accessibilitySnapshotCache.snapshot);
Expand Down
9 changes: 9 additions & 0 deletions packages/runtimes/test/serve-emu-node.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,9 +82,11 @@ function fakeScrcpy() {

test("Node Android server preserves HTTP, WebSocket video, input acknowledgements, and cleanup", async t => {
const scrcpy = fakeScrcpy();
let captures = 0;
const backend = await runtime.startServer({ serial: "emulator-5554", port: 0 }, {
openScrcpy: async () => scrcpy.session,
createInputQueue: () => scrcpy.inputQueue,
loadAccessibility: async () => ({ ok: true, capturedAt: String(++captures), nodes: [] }),
});
t.after(() => backend.stop());
const base = `http://127.0.0.1:${backend.server.port}`;
Expand All @@ -96,6 +98,12 @@ test("Node Android server preserves HTTP, WebSocket video, input acknowledgement
const page = await fetch(base);
assert.match(page.headers.get("content-type") ?? "", /text\/html/);
assert.equal(await page.text(), "<html>Android</html>");
const tree = async (fresh = false) => {
const response = await fetch(`${base}/api/accessibility`, { headers: fresh ? { "Cache-Control": "no-cache" } : {} });
return response.json();
};
assert.equal((await tree()).capturedAt, "1");
assert.equal((await tree()).capturedAt, "1");

const socket = new WebSocket(`ws://127.0.0.1:${backend.server.port}/ws?frame-meta=1`);
t.after(() => socket.terminate());
Expand All @@ -108,6 +116,7 @@ test("Node Android server preserves HTTP, WebSocket video, input acknowledgement
assert.equal(parsedReply.ok, true);
assert.equal(parsedReply.requestId, "input-1");
assert.ok(scrcpy.packets.length > 0);
assert.equal((await tree(true)).capturedAt, "2", "element selection reads the screen after input");
const video = once(socket, "message");
const data = Buffer.from([0, 0, 0, 1, 0x65, 0x80]);
scrcpy.frame({ type: "frame", data, pts: 1000n, isKey: true, isConfig: false });
Expand Down
Loading