Skip to content

Commit e6aed84

Browse files
committed
refactor: update imports to use explicit file paths, enhance error handling and logging in scrcpy functions
1 parent 9f191f6 commit e6aed84

File tree

1 file changed

+93
-36
lines changed

1 file changed

+93
-36
lines changed

tasks/realTest.ts

Lines changed: 93 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,19 @@
11
/* eslint-disable @typescript-eslint/no-unused-vars */
22
import fs from 'node:fs';
33
import path from 'node:path';
4+
import { fileURLToPath } from 'node:url';
5+
import { dirname } from 'node:path';
6+
import { assert } from 'node:console';
47

5-
import adb, { DeviceClient, KeyCodesMap, Utils, MotionEventMap, Client, Minicap, Scrcpy, VideoStreamFramePacket } from '../src';
6-
import { IpRouteEntry, IpRuleEntry } from '../src/adb/command/host-transport';
7-
import Parser from '../src/adb/parser';
8-
import { KeyEventMap } from '../src/adb/thirdparty/STFService/STFServiceModel';
9-
import ThirdUtils from '../src/adb/thirdparty/ThirdUtils';
108
import pc from 'picocolors';
119

12-
import { fileURLToPath } from 'node:url';
13-
import { dirname } from 'node:path';
10+
import adb, { DeviceClient, KeyCodesMap, Utils, MotionEventMap, Client, Minicap, Scrcpy, VideoStreamFramePacket } from '../src/index.js';
11+
import { IpRouteEntry, IpRuleEntry } from '../src/adb/command/host-transport/index.js';
12+
import Parser from '../src/adb/parser.js';
13+
import { KeyEventMap } from '../src/adb/thirdparty/STFService/STFServiceModel.js';
14+
import ThirdUtils from '../src/adb/thirdparty/ThirdUtils.js';
15+
16+
// import './patchEventEmitter.js';
1417

1518
const __filename = fileURLToPath(import.meta.url);
1619
const __dirname = dirname(__filename);
@@ -82,7 +85,10 @@ const testScrcpy = async (deviceClient: DeviceClient) => {
8285
}
8386
}
8487

85-
88+
/**
89+
* Works with V2.7
90+
* @param deviceClient
91+
*/
8692
const testScrcpyTextInput = async (deviceClient: DeviceClient) => {
8793
const scrcpy = deviceClient.scrcpy({});
8894
try {
@@ -113,6 +119,15 @@ const testScrcpyswap = async (deviceClient: DeviceClient) => {
113119
const pointerId = BigInt('0xFFFFFFFFFFFFFFFF');
114120
await scrcpy.start();
115121
console.log(`Started`);
122+
scrcpy.on("error", (e) => {
123+
console.error('scrcpy error', e);
124+
});
125+
scrcpy.on("config", (meta) => {
126+
console.log('scrcpy config', meta);
127+
});
128+
scrcpy.on("frame", (data) => {
129+
console.log(`scrcpy emit hit first frame isKeyframe: ${data.keyframe}`);
130+
});
116131
await Utils.delay(100);
117132
const width = await scrcpy.width;
118133
const height = await scrcpy.height;
@@ -138,11 +153,12 @@ const testScrcpyswap = async (deviceClient: DeviceClient) => {
138153
}
139154

140155
/** test the 2 ways to capture Error in atrcpy */
156+
// encoderName had been replaced by list_encoders in V 2.0
141157
const testScrcpyEncoder = async (deviceClient: DeviceClient) => {
142158
const scrcpy = deviceClient.scrcpy({ encoderName: '_' });
143159
try {
144160
let nbError = 0;
145-
scrcpy.on('error', (e) => { nbError++; console.log(e) });
161+
scrcpy.on('error', (e) => { nbError++; console.error("scrcpy Error:", e) });
146162
// scrcpy.on('error', (e) => { nbError++; /* get Error message line per line */ });
147163
await scrcpy.start();
148164
const error = await scrcpy.onTermination;
@@ -199,7 +215,7 @@ const stressMinicap = async (deviceClient: DeviceClient) => {
199215
console.log("closeing", closeing)
200216
}
201217

202-
const stressScrCpy = async (deviceClient: DeviceClient) => {
218+
const stressTestScrCpy = async (deviceClient: DeviceClient) => {
203219
// const scrcpy = deviceClient.scrcpy({port: 8099, maxFps: 1, maxSize: 320});
204220
const scrcpys: Scrcpy[] = [];
205221
for (let i = 0; i < 15; i++) {
@@ -211,16 +227,21 @@ const stressScrCpy = async (deviceClient: DeviceClient) => {
211227
scrcpy.once('frame', (data) => {
212228
console.log(`${pc.magenta('scrcpy')} emit hit first frame isKeyframe: ${data.keyframe} from #${pass}`);
213229
})
230+
scrcpy.once('error', (data) => {
231+
console.log(`${pc.red('scrcpy')} emit hit error: ${data} from #${pass}`);
232+
})
233+
214234
await scrcpy.start();
215235
await scrcpy.firstFrame;
216236
console.log(`${pc.magenta('scrcpy')} Should had emit hit first frame isKeyframe from #${pass}`);
237+
await Utils.delay(500);
217238
} catch (e) {
218-
console.error(`start minicap ${pass} failed ${(e as Error).message}`);
239+
console.error(`Start Scrcpy PASS:${pass} failed ${(e as Error).message}`);
219240
}
220241
}
221242
// await Util.delay(1000);
222243
const closeing = scrcpys.map(m => m.stop());
223-
console.log("closeing", closeing)
244+
console.log("closing", closeing)
224245
}
225246

226247

@@ -431,7 +452,10 @@ const testRouting = async (deviceClient: DeviceClient) => {
431452
//for (const rule of rules2)
432453
// console.log(rule.toStirng());
433454
}
434-
455+
/**
456+
*
457+
* @param deviceClient dump an XML file with the current UI
458+
*/
435459
const testUiautomator = async (deviceClient: DeviceClient) => {
436460
const duplex = await deviceClient.shell('uiautomator dump /dev/tty');
437461
const result = await new Parser(duplex).readAll();
@@ -448,21 +472,13 @@ const testTracker = async (adbClient: Client) => {
448472
tracker.end();
449473
}
450474

451-
const main = async () => {
452-
// process.env.DEBUG = '*';
453-
const adbClient = adb.createClient();
454-
const devices = await adbClient.listDevices();
455-
456-
if (!devices.length) {
457-
console.error('Need at least one connected android device');
458-
return;
459-
}
460-
461-
const deviceClient = devices[0].getClient();
462-
//const ips = await deviceClient.getIpAddress();
463-
//console.log(ips);
464-
// let pkgs = await deviceClient.listPackages();
465-
// pkgs = pkgs.filter(p => p.name.endsWith('.chrome'))
475+
async function testBasic(deviceClient: DeviceClient) {
476+
const ips = await deviceClient.getIpAddress();
477+
assert(ips.length > 0, "No IP address found");
478+
// console.log(ips);
479+
let pkgs = await deviceClient.listPackages();
480+
pkgs = pkgs.filter(p => p.name.endsWith('.chrome'))
481+
assert(pkgs.length > 0, "No chrome package found");
466482
// if (pkgs.length) {
467483
// console.log(`Pkg: ${pkgs[0].name}`);
468484
// const info = await pkgs[0].getInfo();
@@ -473,26 +489,55 @@ const main = async () => {
473489
// }
474490
// await deviceClient.clear('com.android.chrome');
475491

476-
// const ret1 = await deviceClient.stat('/');
492+
const ret1 = await deviceClient.stat('/');
493+
assert((0o40000 & ret1.mode) === 0o40000, 'isDirectory');
494+
assert(ret1.isDirectory(), 'isDirectory');
495+
assert(ret1.size > 0, 'size');
477496
// console.log(ret1);
478-
// const ret2 = await deviceClient.stat64('/system');
497+
const ret2 = await deviceClient.stat64('/system');
498+
assert((0o40000n & ret2.mode) === 0o40000n, 'isDirectory');
499+
assert(ret2.isDirectory(), 'isDirectory');
500+
assert(ret2.size > 0, 'size');
501+
479502
// console.log(ret2.ctime);
480503
// console.log(ret2.isDirectory());
481504

482-
// const list = await deviceClient.readdir64('/');
505+
const list = await deviceClient.readdir64('/');
506+
assert(list.length, 'can list /');
483507
// console.log(list.map(a=>a.toString()).join('\n'));
508+
}
509+
510+
const main = async () => {
511+
// process.env.DEBUG = '*';
512+
const adbClient = adb.createClient();
513+
const devices = await adbClient.listDevices();
514+
515+
if (!devices.length) {
516+
console.error('Need at least one connected android device');
517+
return;
518+
}
519+
520+
const deviceClient = devices[0].getClient();
521+
522+
// await testBasic(deviceClient);
523+
484524
// await deviceClient.extra.usbTethering(true);
485525
// await deviceClient.extra.airPlainMode(false, 300);
486526
// await deviceClient.extra.airPlainMode(true);
487-
// await testScrcpyEncoder(deviceClient);
488-
await testScrcpy(deviceClient);
527+
528+
// await testScrcpyEncoder(deviceClient); // removed in V2.0
529+
// await testScrcpy(deviceClient);
489530
// await testUiautomator(deviceClient);
490531
// await testScrcpyTextInput(deviceClient);
491532
// await testScrcpyswap(deviceClient);
533+
//
492534
// await testMinicap(deviceClient);
493535
// await stressMinicap(deviceClient);
494-
// await stressScrCpy(deviceClient);
495-
// await mtestSTFService(deviceClient);
536+
537+
// process.stdin.resume();
538+
await stressTestScrCpy(deviceClient);
539+
540+
// await testSTFService(deviceClient);
496541
// await testService(deviceClient);
497542
// await extractFramesStream(deviceClient, 'OMX.qcom.video.encoder.avc');
498543
// await extractFramesStream(deviceClient, 'c2.android.avc.encoder');
@@ -504,4 +549,16 @@ const main = async () => {
504549
console.log('all Done');
505550
}
506551

507-
main().catch(e => console.error(e));
552+
process.on('unhandledRejection', (reason, promise) => {
553+
debugger;
554+
console.error('Unhandled Rejection at:', promise, 'reason:', reason);
555+
});
556+
557+
process.on('exit', () => console.log('Processus en cours de fermeture'));
558+
process.on('SIGINT', () => console.log('SIGINT reçu'));
559+
process.on('SIGTERM', () => console.log('SIGTERM reçu'));
560+
561+
562+
main().catch(e => console.error("ERROR", e)).finally(() => {
563+
console.log('Processus terminé');
564+
});

0 commit comments

Comments
 (0)