Skip to content

Commit 6335af1

Browse files
szdziedzicclaude
andcommitted
refactor(capture): share one Android display read and drop the external-display fallback
- The orientation settle and the screenshot probe read dumpsys display through one function; the settle still fails on a failed read, and the probe still yields no rotation. - An iOS inventory with no built-in panel reports no rotation instead of reading an attached display. - The sparse fallback test asserts the rotation through finalizeDaemonResponse. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent 0976f66 commit 6335af1

5 files changed

Lines changed: 62 additions & 33 deletions

File tree

‎packages/platform-android/src/__tests__/input-actions.test.ts‎

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import {
55
homeAndroid,
66
longPressAndroid,
77
pressAndroidEnter,
8-
parseAndroidDisplayRotationIndices,
98
pressAndroidTvRemote,
109
probeAndroidDisplayRotation,
1110
scrollAndroid,
@@ -484,20 +483,6 @@ test('setAndroidOrientation leaves a display that reports no rotation to the set
484483
});
485484
});
486485

487-
test('parseAndroidDisplayRotationIndices reads one index per logical display', () => {
488-
assert.deepEqual(parseAndroidDisplayRotationIndices(' mCurrentOrientation=1\n mOther=2\n'), [
489-
'1',
490-
]);
491-
assert.deepEqual(
492-
parseAndroidDisplayRotationIndices(' mCurrentOrientation=0\n mCurrentOrientation=3\n'),
493-
['0', '3'],
494-
);
495-
assert.deepEqual(
496-
parseAndroidDisplayRotationIndices(' mOverrideDisplayInfo=DisplayInfo{}\n'),
497-
[],
498-
);
499-
});
500-
501486
test('probeAndroidDisplayRotation reads the rotation of the only display', async () => {
502487
await withFakeAdb(displayReporting(['3']), async ({ device }) => {
503488
const rotation = await probeAndroidDisplayRotation(device, {
@@ -541,3 +526,16 @@ test('probeAndroidDisplayRotation hands its budget and abort signal to the adb s
541526
assert.equal(seen[0]!.timeoutMs, 2_000);
542527
assert.equal(seen[0]!.signal, controller.signal);
543528
});
529+
530+
test('probeAndroidDisplayRotation reports none when the display read fails', async () => {
531+
await withFakeAdb(
532+
(args) => (args[1] === 'dumpsys' ? { stdout: '', stderr: 'denied', exitCode: 1 } : undefined),
533+
async ({ device }) => {
534+
const rotation = await probeAndroidDisplayRotation(device, {
535+
timeoutMs: 2_000,
536+
signal: new AbortController().signal,
537+
});
538+
assert.equal(rotation, undefined);
539+
},
540+
);
541+
});

‎packages/platform-android/src/input-actions.ts‎

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -123,10 +123,9 @@ async function readAndroidDisplayRotation(
123123
deadline: number,
124124
): Promise<string | undefined> {
125125
try {
126-
const result = await runAndroidShell(device, ['dumpsys', 'display'], {
127-
timeoutMs: remainingMs(deadline),
128-
});
129-
return parseAndroidDisplayRotationIndices(result.stdout)[0];
126+
return (
127+
await readAndroidDisplayRotationIndices(device, { timeoutMs: remainingMs(deadline) })
128+
)[0];
130129
} catch (error) {
131130
throw new AppError(
132131
'COMMAND_FAILED',
@@ -140,9 +139,17 @@ function remainingMs(deadline: number): number {
140139
return Math.max(1, deadline - Date.now());
141140
}
142141

143-
/** `dumpsys display` reports one `Surface.ROTATION_*` index per logical display under this key. */
144-
export function parseAndroidDisplayRotationIndices(dumpsysDisplay: string): string[] {
145-
return [...dumpsysDisplay.matchAll(/mCurrentOrientation=(\d)/g)].map((match) => match[1]!);
142+
/**
143+
* The one `dumpsys display` read both the orientation settle and the screenshot probe use: the
144+
* `Surface.ROTATION_*` index of each logical display. A failed or aborted read throws; each caller
145+
* decides what that means.
146+
*/
147+
async function readAndroidDisplayRotationIndices(
148+
device: DeviceInfo,
149+
options: { timeoutMs: number; signal?: AbortSignal },
150+
): Promise<string[]> {
151+
const result = await runAndroidShell(device, ['dumpsys', 'display'], options);
152+
return [...result.stdout.matchAll(/mCurrentOrientation=(\d)/g)].map((match) => match[1]!);
146153
}
147154

148155
/**
@@ -156,13 +163,7 @@ export async function probeAndroidDisplayRotation(
156163
options: { timeoutMs: number; signal: AbortSignal },
157164
): Promise<DeviceRotation | undefined> {
158165
try {
159-
const result = await runAndroidShell(device, ['dumpsys', 'display'], {
160-
allowFailure: true,
161-
timeoutMs: options.timeoutMs,
162-
signal: options.signal,
163-
});
164-
if (result.exitCode !== 0) return undefined;
165-
const indices = parseAndroidDisplayRotationIndices(result.stdout);
166+
const indices = await readAndroidDisplayRotationIndices(device, options);
166167
return indices.length === 1 ? deviceRotationFromSurfaceIndex(Number(indices[0])) : undefined;
167168
} catch {
168169
return undefined;

‎packages/platform-apple/src/core/__tests__/display-inventory.test.ts‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -441,6 +441,23 @@ describe('resolveAppleCaptureTarget', () => {
441441
}
442442
});
443443

444+
test('reports no rotation when CoreDevice lists no built-in panel', async () => {
445+
mockReadHostTextFile.mockResolvedValue(
446+
JSON.stringify({
447+
result: {
448+
displays: [
449+
{
450+
...IPHONE_17_NO_ACTIVE_KEY.result.displays[0],
451+
currentOrientation: 'rot90',
452+
type: { external: {} },
453+
},
454+
],
455+
},
456+
}),
457+
);
458+
assert.deepEqual(await resolveAppleCaptureTarget(IOS_TEST_SIMULATOR), {});
459+
});
460+
444461
test('names the panel but reports no rotation on a multi-panel device', async () => {
445462
// Open, the inner panel is lit and reads rot90 in a portrait hold: its rotation is relative to
446463
// the panel's own geometry, so reporting it would call a portrait capture landscape.

‎packages/platform-apple/src/core/display-inventory.ts‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -338,7 +338,8 @@ export async function resolveAppleCaptureTarget(
338338
if (inventory.multiScreen) {
339339
return inventory.activeDisplay ? { display: inventory.activeDisplay } : {};
340340
}
341-
const panel = inventory.displays.find((entry) => entry.integrated) ?? inventory.displays[0];
341+
// Only a built-in panel is the screen simctl captures; an attached display says nothing about it.
342+
const panel = inventory.displays.find((entry) => entry.integrated);
342343
const displayRotation = panel ? appleDisplayRotation(panel) : undefined;
343344
return displayRotation ? { displayRotation } : {};
344345
}

‎src/daemon/__tests__/sparse-fallback-screenshot.test.ts‎

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ import { makeIosSession } from '../../__tests__/test-utils/session-factories.ts'
55
import { mkdtempForTest } from '../../__tests__/test-utils/tmp-dir.ts';
66
import { SessionStore } from '../session-store.ts';
77
import { dispatchSnapshotViaRuntime } from '../snapshot-runtime.ts';
8+
import type { DownloadableArtifactRegistration } from '../artifact-tracking.ts';
9+
import { finalizeDaemonResponse } from '../request-finalization.ts';
810
import {
911
screenshotRuntimeFixture,
1012
writeSolidPng,
@@ -129,18 +131,28 @@ test('a failed fallback screenshot does not fail the snapshot that was asked for
129131
expect(warnings.some((line) => line.includes('Use screenshot as visual truth'))).toBe(true);
130132
});
131133

132-
test('the fallback screenshot artifact carries the display rotation its capture reported', async () => {
134+
test('the fallback screenshot reaches the artifact inventory with its display rotation', async () => {
133135
const input = await scenario();
134136
const runtime = seed(SPARSE, (captureInput) => {
135137
writeSolidPng(captureInput.outPath);
136138
return { displayRotation: 'landscape-left' };
137139
});
138140

139141
const data = await dispatch(input, runtime);
142+
const tracked: DownloadableArtifactRegistration[] = [];
143+
finalizeDaemonResponse(
144+
{ command: 'snapshot', positionals: [], token: 't', session: input.sessionName },
145+
{ ok: true, data },
146+
(registration) => {
147+
tracked.push(registration);
148+
return 'artifact-id';
149+
},
150+
);
140151

141-
expect(data.artifacts).toEqual([
152+
expect(tracked).toEqual([
142153
expect.objectContaining({
143-
field: 'fallbackScreenshotPath',
154+
artifactType: 'screenshot',
155+
fileName: 'snapshot-fallback.png',
144156
displayRotation: 'landscape-left',
145157
}),
146158
]);

0 commit comments

Comments
 (0)