test-android-native-screenshot.mjs 1.3 KB

123456789101112131415161718192021
  1. import { test } from 'node:test';
  2. import assert from 'node:assert/strict';
  3. import { captureNativeScreenshot } from './android-webview-cdp.mjs';
  4. const png = Buffer.from([137, 80, 78, 71, 13, 10, 26, 10, 0, 255, 13, 10]);
  5. test('native screenshot keeps exact PNG bytes and targets one explicit device', () => {
  6. let written = 0;
  7. captureNativeScreenshot('fixture.png', 'fixture-serial', {
  8. run(command, args, options) { assert.equal(command, 'adb'); assert.deepEqual(args, ['-s', 'fixture-serial', 'exec-out', 'screencap', '-p']); assert.equal(options.encoding, undefined); return { status: 0, stdout: png }; },
  9. write(file, data, options) { written++; assert.equal(file, 'fixture.png'); assert.deepEqual(data, png); assert.equal(options.flag, 'wx'); },
  10. });
  11. assert.equal(written, 1);
  12. });
  13. test('failed or text-corrupted capture must never write successful evidence', () => {
  14. for (const result of [{ status: 1, stdout: png }, { status: 0, stdout: png.toString() }]) {
  15. assert.throws(() => captureNativeScreenshot('fixture.png', 'fixture-serial', { run: () => result, write() { assert.fail('must not write failed screenshot'); } }));
  16. }
  17. });
  18. test('missing explicit device fails before invoking ADB', () => {
  19. assert.throws(() => captureNativeScreenshot('fixture.png', '', { run() { assert.fail('must not run'); } }));
  20. });