Skip to content

Commit 6362634

Browse files
committed
refactor(platform-android): derive the viewport once in snapshotAndroid (#3182)
The capture carrier held a pre-derived viewport beside its metadata, so every re-capture hop had to thread the sibling through; a second copy of the display pair also meant two lifetimes for one fact. Carry the raw helper metadata on AndroidUiHierarchyCapture instead and answer the viewport question only in snapshotAndroid, through the shared guard, so both the healthy and the presentation-failed return publish the same box the tree was measured against.
1 parent 2024c0b commit 6362634

2 files changed

Lines changed: 16 additions & 12 deletions

File tree

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

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,21 @@
1-
import type { SnapshotViewportSize } from '@agent-device/kernel/snapshot';
21
import type {
32
AndroidSnapshotCaptureMode,
43
AndroidSnapshotHelperInstallReason,
4+
AndroidSnapshotHelperMetadata,
55
AndroidSnapshotHelperTransport,
66
} from './snapshot-helper-types.ts';
77

88
/**
9-
* One Android capture as the host holds it: the tree, the helper facts behind it, and the screen the
10-
* bounds are measured in (#3182). The viewport is a sibling rather than a field of `metadata` because
11-
* the metadata travels to the response, where the same fact is already published once as `viewport`;
12-
* carrying the raw display pair further than this boundary would leave two copies with different
13-
* lifetimes. Absent when the helper's own display read answered with nothing usable.
9+
* One Android capture as the host holds it: the tree, the backend metadata the response publishes,
10+
* and the raw helper transport metadata behind it. The helper pair (display extent, density) rides
11+
* as raw transport facts rather than as a pre-derived viewport, so the single place that answers the
12+
* viewport question is `snapshotAndroid`, through the shared guard. The backend metadata stays free
13+
* of a second copy of the display read: the response publishes that fact once, as `viewport` (#3182).
1414
*/
1515
export type AndroidUiHierarchyCapture = {
1616
xml: string;
1717
metadata: AndroidSnapshotBackendMetadata;
18-
viewport?: SnapshotViewportSize;
18+
helperMetadata: AndroidSnapshotHelperMetadata;
1919
};
2020

2121
export type AndroidSnapshotBackendMetadata = {

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

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,11 @@ export async function snapshotAndroid(
125125
): Promise<AndroidSnapshotCapture> {
126126
const adb = resolveAndroidAdbProvider(device, options.helperAdb).exec;
127127
const capture = await captureAndroidUiHierarchy(device, options, adb);
128+
// The one place Android answers the viewport question (#3182): the helper's own display read,
129+
// through the shared guard, so both the healthy and the presentation-failed capture below publish
130+
// the same box the tree was measured against, and a display the helper could not address stays
131+
// absent rather than becoming a zero.
132+
const viewport = androidSnapshotViewportFromHelperMetadata(capture.helperMetadata);
128133
const xml = capture.xml;
129134
const tree = parseUiHierarchyTree(xml);
130135
const androidSnapshot = withOcclusionScanDisclosure(capture.metadata, tree);
@@ -155,7 +160,7 @@ export async function snapshotAndroid(
155160
...androidSnapshotTruncationFields(truncated),
156161
androidSnapshot,
157162
quality: { state: 'healthy', backend: 'android-helper' } as const,
158-
...(capture.viewport ? { viewport: capture.viewport } : {}),
163+
...(viewport ? { viewport } : {}),
159164
};
160165
return createAndroidSnapshotCapture(result, {
161166
clickability: buildAndroidSnapshotClickabilityEvidence(built),
@@ -166,7 +171,7 @@ export async function snapshotAndroid(
166171
return attachAndroidPresentationFailureEvidence({
167172
failure: error,
168173
androidSnapshot,
169-
...(capture.viewport ? { viewport: capture.viewport } : {}),
174+
...(viewport ? { viewport } : {}),
170175
});
171176
}
172177
}
@@ -483,9 +488,9 @@ function formatAndroidHelperCaptureResult(
483488
artifact: AndroidSnapshotHelperArtifact,
484489
installReason: AndroidSnapshotHelperInstallResult['reason'],
485490
): AndroidUiHierarchyCapture {
486-
const viewport = androidSnapshotViewportFromHelperMetadata(capture.metadata);
487491
return {
488492
xml: capture.xml,
493+
helperMetadata: capture.metadata,
489494
metadata: {
490495
backend: 'android-helper',
491496
pixelDensity: capture.metadata.pixelDensity,
@@ -506,7 +511,6 @@ function formatAndroidHelperCaptureResult(
506511
helperTruncated: capture.metadata.truncated,
507512
elapsedMs: capture.metadata.elapsedMs,
508513
},
509-
...(viewport ? { viewport } : {}),
510514
};
511515
}
512516

@@ -578,10 +582,10 @@ async function captureAndroidHelperContentAttempt(params: {
578582
outcome: 'captured',
579583
capture: {
580584
xml: helperCapture.xml,
585+
helperMetadata: helperCapture.helperMetadata,
581586
metadata: systemSurfaceOnly
582587
? { ...helperCapture.metadata, systemSurfaceOnly: true }
583588
: helperCapture.metadata,
584-
...(helperCapture.viewport ? { viewport: helperCapture.viewport } : {}),
585589
},
586590
};
587591
}

0 commit comments

Comments
 (0)