|
| 1 | +import assert from 'node:assert/strict'; |
| 2 | +import { afterEach, beforeEach, test } from 'vitest'; |
| 3 | +import './test-utils/android-host-test-setup.ts'; |
| 4 | +import { |
| 5 | + androidSnapshotPublicationInput, |
| 6 | + androidSnapshotViewportFromHelperMetadata, |
| 7 | +} from '../snapshot-capture.ts'; |
| 8 | +import { snapshotAndroid } from '../snapshot.ts'; |
| 9 | +import { resetAndroidSnapshotHelperInstallCache } from '../snapshot-helper-install.ts'; |
| 10 | +import { resetAndroidSnapshotHelperSessions } from '../snapshot-helper-session-lifecycle.ts'; |
| 11 | +import type { AndroidAdbExecutor } from '../snapshot-helper.ts'; |
| 12 | +import { |
| 13 | + androidSnapshotQualityDevice as device, |
| 14 | + androidSnapshotQualityHelperArtifact as helperArtifact, |
| 15 | +} from './snapshot-quality-fixtures.ts'; |
| 16 | + |
| 17 | +const SCREEN_XML = '<hierarchy><node text="row" bounds="[0,0][1080,200]" /></hierarchy>'; |
| 18 | + |
| 19 | +beforeEach(async () => { |
| 20 | + await resetAndroidSnapshotHelperSessions(); |
| 21 | + resetAndroidSnapshotHelperInstallCache(); |
| 22 | +}); |
| 23 | + |
| 24 | +afterEach(async () => { |
| 25 | + await resetAndroidSnapshotHelperSessions(); |
| 26 | +}); |
| 27 | + |
| 28 | +function helperAdbServing(display: { width?: number; height?: number } = {}): AndroidAdbExecutor { |
| 29 | + const displayKeys = |
| 30 | + display.width !== undefined && display.height !== undefined |
| 31 | + ? [ |
| 32 | + `INSTRUMENTATION_RESULT: displayWidth=${display.width}`, |
| 33 | + `INSTRUMENTATION_RESULT: displayHeight=${display.height}`, |
| 34 | + ] |
| 35 | + : []; |
| 36 | + const stdout = [ |
| 37 | + 'INSTRUMENTATION_STATUS: agentDeviceProtocol=android-snapshot-helper-v1', |
| 38 | + 'INSTRUMENTATION_STATUS: helperApiVersion=1', |
| 39 | + 'INSTRUMENTATION_STATUS: outputFormat=uiautomator-xml', |
| 40 | + 'INSTRUMENTATION_STATUS: chunkIndex=0', |
| 41 | + 'INSTRUMENTATION_STATUS: chunkCount=1', |
| 42 | + `INSTRUMENTATION_STATUS: payloadBase64=${Buffer.from(SCREEN_XML, 'utf8').toString('base64')}`, |
| 43 | + 'INSTRUMENTATION_STATUS_CODE: 1', |
| 44 | + 'INSTRUMENTATION_RESULT: agentDeviceProtocol=android-snapshot-helper-v1', |
| 45 | + 'INSTRUMENTATION_RESULT: helperApiVersion=1', |
| 46 | + 'INSTRUMENTATION_RESULT: ok=true', |
| 47 | + 'INSTRUMENTATION_RESULT: outputFormat=uiautomator-xml', |
| 48 | + 'INSTRUMENTATION_RESULT: waitForIdleTimeoutMs=0', |
| 49 | + 'INSTRUMENTATION_RESULT: timeoutMs=8000', |
| 50 | + 'INSTRUMENTATION_RESULT: maxDepth=128', |
| 51 | + 'INSTRUMENTATION_RESULT: maxNodes=5000', |
| 52 | + 'INSTRUMENTATION_RESULT: rootPresent=true', |
| 53 | + 'INSTRUMENTATION_RESULT: captureMode=interactive-windows', |
| 54 | + 'INSTRUMENTATION_RESULT: windowCount=1', |
| 55 | + 'INSTRUMENTATION_RESULT: nodeCount=1', |
| 56 | + 'INSTRUMENTATION_RESULT: truncated=false', |
| 57 | + 'INSTRUMENTATION_RESULT: elapsedMs=12', |
| 58 | + 'INSTRUMENTATION_RESULT: pixelDensity=2.625', |
| 59 | + ...displayKeys, |
| 60 | + 'INSTRUMENTATION_CODE: 0', |
| 61 | + ].join('\n'); |
| 62 | + return async (args) => { |
| 63 | + if (args.includes('--show-versioncode')) { |
| 64 | + return { |
| 65 | + exitCode: 0, |
| 66 | + stdout: 'package:com.callstack.agentdevice.snapshothelper versionCode:13004', |
| 67 | + stderr: '', |
| 68 | + }; |
| 69 | + } |
| 70 | + if (args.includes('instrument')) { |
| 71 | + return { exitCode: 0, stdout, stderr: '' }; |
| 72 | + } |
| 73 | + throw new Error(`unexpected helper adb args: ${args.join(' ')}`); |
| 74 | + }; |
| 75 | +} |
| 76 | + |
| 77 | +// #3182: the published viewport comes from the display the helper measured, not from the tree, so a |
| 78 | +// capture with content and a capture of an empty screen answer the same question. |
| 79 | +test('snapshotAndroid publishes the display the helper measured (#3182)', async () => { |
| 80 | + const capture = await snapshotAndroid(device, { |
| 81 | + helperAdb: helperAdbServing({ width: 1080, height: 2400 }), |
| 82 | + helperArtifact, |
| 83 | + }); |
| 84 | + |
| 85 | + assert.deepEqual(capture.viewport, { width: 1080, height: 2400 }); |
| 86 | + // The response publishes the fact once. The raw display pair stays on the helper transport and the |
| 87 | + // backend metadata the response carries, so a consumer cannot read two copies of it. |
| 88 | + assert.equal( |
| 89 | + 'displayWidth' in (capture.androidSnapshot as Record<string, unknown>), |
| 90 | + false, |
| 91 | + 'the backend metadata carries no second copy of the display read', |
| 92 | + ); |
| 93 | +}); |
| 94 | + |
| 95 | +test('the Android viewport survives the publication adapter into the daemon capture (#3182)', async () => { |
| 96 | + const capture = await snapshotAndroid(device, { |
| 97 | + helperAdb: helperAdbServing({ width: 1080, height: 2400 }), |
| 98 | + helperArtifact, |
| 99 | + }); |
| 100 | + |
| 101 | + assert.deepEqual(androidSnapshotPublicationInput(capture).viewport, { |
| 102 | + width: 1080, |
| 103 | + height: 2400, |
| 104 | + }); |
| 105 | +}); |
| 106 | + |
| 107 | +test('a helper with no display read leaves the Android viewport absent, never zero (#3182)', async () => { |
| 108 | + const capture = await snapshotAndroid(device, { |
| 109 | + helperAdb: helperAdbServing(), |
| 110 | + helperArtifact, |
| 111 | + }); |
| 112 | + |
| 113 | + assert.equal(capture.viewport, undefined); |
| 114 | + assert.equal('viewport' in capture, false, 'absent is not the same as an empty box'); |
| 115 | +}); |
| 116 | + |
| 117 | +// The consequence the display read buys: the box is a property of the device, so the capture that |
| 118 | +// reports nothing about the tree can still report the screen its (absent) bounds belonged to. |
| 119 | +test('a presentation-failed Android capture still publishes the display it read (#3182)', async () => { |
| 120 | + const capture = await snapshotAndroid(device, { |
| 121 | + helperAdb: helperAdbServing({ width: 1080, height: 2400 }), |
| 122 | + helperArtifact, |
| 123 | + androidPresentation: { deadlineAtMs: 100, now: () => 100 }, |
| 124 | + }); |
| 125 | + |
| 126 | + assert.deepEqual(capture.nodes, [], 'the projection was discarded'); |
| 127 | + assert.deepEqual(capture.viewport, { width: 1080, height: 2400 }); |
| 128 | +}); |
| 129 | + |
| 130 | +test('androidSnapshotViewportFromHelperMetadata refuses an unusable display read (#3182)', () => { |
| 131 | + assert.deepEqual( |
| 132 | + androidSnapshotViewportFromHelperMetadata({ |
| 133 | + outputFormat: 'uiautomator-xml', |
| 134 | + displayWidth: 1080, |
| 135 | + displayHeight: 2400, |
| 136 | + }), |
| 137 | + { width: 1080, height: 2400 }, |
| 138 | + ); |
| 139 | + for (const unusable of [ |
| 140 | + {}, |
| 141 | + { displayWidth: 0, displayHeight: 2400 }, |
| 142 | + { displayWidth: 1080, displayHeight: 0 }, |
| 143 | + { displayWidth: -1, displayHeight: 2400 }, |
| 144 | + { displayWidth: Number.NaN, displayHeight: 2400 }, |
| 145 | + { displayWidth: Number.POSITIVE_INFINITY, displayHeight: 2400 }, |
| 146 | + ]) { |
| 147 | + assert.equal( |
| 148 | + androidSnapshotViewportFromHelperMetadata({ outputFormat: 'uiautomator-xml', ...unusable }), |
| 149 | + undefined, |
| 150 | + JSON.stringify(unusable), |
| 151 | + ); |
| 152 | + } |
| 153 | +}); |
0 commit comments