Skip to content

Commit 7f52420

Browse files
committed
fix(snapshot): carry a backend's own viewport through the state seam (#3182)
normalizeBackendSnapshot returned a backend-built state verbatim, so a producer that named its own keyboard band or viewport box beside that state lost it on the one path where the backend was the authority. The two Android rejection helpers advertise a capture they can never yield and now say never, and the helper's one-read claim is scoped to what the code guarantees: density and extent from one DisplayMetrics, not a metrics sample taken with the dump. Proves the display read answers the geometry-free-tree case the issue came from.
1 parent 7026c7b commit 7f52420

4 files changed

Lines changed: 43 additions & 9 deletions

File tree

‎android/snapshot-helper/src/main/java/com/callstack/agentdevice/snapshothelper/DisplaySizeReader.java‎

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,12 @@
1111
* because a rect's coordinates answer to the screen rather than to the app's window.
1212
*
1313
* The caller hands in the extent of the same {@code DisplayMetrics} whose {@code density} this helper
14-
* publishes, so size and density cannot describe two different configurations — it is the one the
15-
* framework laid the captured nodes out with, a {@code wm size} or {@code wm density} override
16-
* included, and it rotates with the screen. {@code getRealSize} is deliberately not consulted: it is
17-
* deprecated and rotates inconsistently across the devices this helper runs on.
14+
* publishes, and it is the configuration the framework lays out with, a {@code wm size} or
15+
* {@code wm density} override included, rotating with the screen. The helper reads that metrics once,
16+
* when it assembles a capture's metadata, so density and extent always describe one configuration —
17+
* exactly the guarantee the density key already makes, and no stronger: a rotation landing between
18+
* the dump and this read changes both keys together. {@code getRealSize} is deliberately not
19+
* consulted: it is deprecated and rotates inconsistently across the devices this helper runs on.
1820
*
1921
* A display that answers with nothing usable publishes no fact at all. Absence is the only way to say
2022
* "unknown", so the host never sees a zero.

‎packages/platform-android/src/__tests__/snapshot-capture.test.ts‎

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,10 @@ afterEach(async () => {
2525
await resetAndroidSnapshotHelperSessions();
2626
});
2727

28-
function helperAdbServing(display: { width?: number; height?: number } = {}): AndroidAdbExecutor {
28+
function helperAdbServing(
29+
display: { width?: number; height?: number } = {},
30+
xml: string = SCREEN_XML,
31+
): AndroidAdbExecutor {
2932
const displayKeys =
3033
display.width !== undefined && display.height !== undefined
3134
? [
@@ -39,7 +42,7 @@ function helperAdbServing(display: { width?: number; height?: number } = {}): An
3942
'INSTRUMENTATION_STATUS: outputFormat=uiautomator-xml',
4043
'INSTRUMENTATION_STATUS: chunkIndex=0',
4144
'INSTRUMENTATION_STATUS: chunkCount=1',
42-
`INSTRUMENTATION_STATUS: payloadBase64=${Buffer.from(SCREEN_XML, 'utf8').toString('base64')}`,
45+
`INSTRUMENTATION_STATUS: payloadBase64=${Buffer.from(xml, 'utf8').toString('base64')}`,
4346
'INSTRUMENTATION_STATUS_CODE: 1',
4447
'INSTRUMENTATION_RESULT: agentDeviceProtocol=android-snapshot-helper-v1',
4548
'INSTRUMENTATION_RESULT: helperApiVersion=1',
@@ -151,3 +154,23 @@ test('androidSnapshotViewportFromHelperMetadata refuses an unusable display read
151154
);
152155
}
153156
});
157+
158+
// The consumer this issue came from takes the extent of all rects as the screen. A display read
159+
// answers that question without the tree, so a capture whose nodes carry no bounds at all still
160+
// names the box those bounds would be measured in.
161+
test('an Android capture with geometry-free nodes still publishes the display it read (#3182)', async () => {
162+
const capture = await snapshotAndroid(device, {
163+
helperAdb: helperAdbServing(
164+
{ width: 1080, height: 2400 },
165+
'<hierarchy><node text="row" /></hierarchy>',
166+
),
167+
helperArtifact,
168+
});
169+
170+
assert.deepEqual(capture.viewport, { width: 1080, height: 2400 });
171+
assert.equal(
172+
capture.nodes.every((node) => node.rect === undefined),
173+
true,
174+
'the tree really carries no geometry',
175+
);
176+
});

‎packages/platform-android/src/snapshot.ts‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -601,7 +601,7 @@ async function rejectAndroidHelperContentUnavailable(params: {
601601
signal?: AbortSignal;
602602
/** A transient capture does not own the helper, so its content verdict leaves the helper alone. */
603603
retireHelper: boolean;
604-
}): Promise<AndroidUiHierarchyCapture> {
604+
}): Promise<never> {
605605
emitDiagnostic({
606606
level: 'error',
607607
phase: 'android_snapshot_helper_content_invalid',
@@ -643,7 +643,7 @@ async function rejectAndroidHelperCaptureFailure(params: {
643643
helperDeviceKey: string;
644644
artifact: AndroidSnapshotHelperArtifact;
645645
adb: AndroidAdbExecutor;
646-
}): Promise<AndroidUiHierarchyCapture> {
646+
}): Promise<never> {
647647
const failureReason = formatAndroidSnapshotHelperFailureReason(params.error);
648648
emitDiagnostic({
649649
level: 'error',

‎src/commands/capture/runtime/snapshot.ts‎

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,16 @@ function normalizeBackendSnapshot(
206206
result: BackendSnapshotResult,
207207
runtime: AgentDeviceRuntime,
208208
): SnapshotState {
209-
if (result.snapshot) return result.snapshot;
209+
// A backend may hand over state it built itself, and the response-level facts it publishes beside
210+
// that state travel with this capture: returning the state alone would drop a producer's own
211+
// keyboard band or viewport box (#3182) on the one path where the backend named them itself.
212+
if (result.snapshot) {
213+
return {
214+
...result.snapshot,
215+
...(result.keyboard ? { keyboard: result.keyboard } : {}),
216+
...(result.viewport ? { viewport: result.viewport } : {}),
217+
};
218+
}
210219
return {
211220
nodes: result.nodes ?? [],
212221
truncated: result.truncated,

0 commit comments

Comments
 (0)