@@ -36,6 +36,8 @@ export type { HarmonyDeviceOpt } from '@midscene/core/device';
3636const defaultScrollUntilTimes = 10 ;
3737const defaultSwipeSpeed = 600 ;
3838const defaultFastSwipeSpeed = 2000 ;
39+ const maxScrollDistance = 9999999 ;
40+ const scrollQuadrantDivisions = 4 ;
3941
4042const debugDevice = getDebug ( 'harmony:device' ) ;
4143
@@ -128,7 +130,7 @@ export class HarmonyDevice implements AbstractInterface {
128130 await this . scrollUntilLeft ( startingPoint ) ;
129131 } else if ( scrollToEventName === 'singleAction' || ! scrollToEventName ) {
130132 if ( param ?. direction === 'down' || ! param || ! param . direction ) {
131- await this . scrollDown ( param ?. distance || undefined , startingPoint ) ;
133+ await this . scrollDown ( param ?. distance ?? undefined , startingPoint ) ;
132134 } else if ( param . direction === 'up' ) {
133135 await this . scrollUp ( param . distance || undefined , startingPoint ) ;
134136 } else if ( param . direction === 'left' ) {
@@ -257,7 +259,6 @@ export class HarmonyDevice implements AbstractInterface {
257259 }
258260
259261 this . connecting = ( async ( ) => {
260- let error : Error | null = null ;
261262 debugDevice ( `Initializing HDC with device ID: ${ this . deviceId } ` ) ;
262263 try {
263264 this . hdc = new HdcClient ( {
@@ -273,16 +274,10 @@ export class HarmonyDevice implements AbstractInterface {
273274 return this . hdc ;
274275 } catch ( e ) {
275276 debugDevice ( `Failed to initialize HDC: ${ e } ` ) ;
276- error = new Error ( `Unable to connect to device ${ this . deviceId } : ${ e } ` ) ;
277+ throw new Error ( `Unable to connect to device ${ this . deviceId } : ${ e } ` ) ;
277278 } finally {
278279 this . connecting = null ;
279280 }
280-
281- if ( error ) {
282- throw error ;
283- }
284-
285- throw new Error ( 'HDC initialization failed unexpectedly' ) ;
286281 } ) ( ) ;
287282
288283 return this . connecting ;
@@ -320,8 +315,9 @@ export class HarmonyDevice implements AbstractInterface {
320315 const bundleName = this . resolvePackageName ( uri ) ?? uri ;
321316 try {
322317 await hdc . startAbility ( bundleName , 'EntryAbility' ) ;
323- } catch {
324- // EntryAbility failed, auto-discover the main ability
318+ } catch ( e : any ) {
319+ if ( ! e . message ?. includes ( 'resolve ability' ) ) throw e ;
320+ // EntryAbility not found, auto-discover the main ability
325321 const mainAbility = await hdc . queryMainAbility ( bundleName ) ;
326322 if ( ! mainAbility ) {
327323 throw new Error (
@@ -480,7 +476,7 @@ export class HarmonyDevice implements AbstractInterface {
480476 async keyboardPress ( key : string ) : Promise < void > {
481477 // HarmonyOS uitest only accepts Back/Home/Power as string names.
482478 // All other keys must use numeric keycodes.
483- const keyMap : Record < string , string > = {
479+ const harmonyKeyCodeMap : Record < string , string > = {
484480 Enter : '2054' ,
485481 Backspace : '2055' ,
486482 Tab : '2049' ,
@@ -495,14 +491,14 @@ export class HarmonyDevice implements AbstractInterface {
495491 } ;
496492
497493 const normalizedKey = this . normalizeKeyName ( key ) ;
498- const harmonyKey = keyMap [ normalizedKey ] || key ;
494+ const harmonyKey = harmonyKeyCodeMap [ normalizedKey ] ?? key ;
499495
500496 const hdc = await this . getHdc ( ) ;
501497 await hdc . keyEvent ( harmonyKey ) ;
502498 }
503499
504500 private normalizeKeyName ( key : string ) : string {
505- const keyMap : Record < string , string > = {
501+ const keyNameAliasMap : Record < string , string > = {
506502 enter : 'Enter' ,
507503 backspace : 'Backspace' ,
508504 tab : 'Tab' ,
@@ -522,7 +518,7 @@ export class HarmonyDevice implements AbstractInterface {
522518 } ;
523519
524520 const lowerKey = key . toLowerCase ( ) ;
525- return keyMap [ lowerKey ] || key ;
521+ return keyNameAliasMap [ lowerKey ] ?? key ;
526522 }
527523
528524 async scroll ( deltaX : number , deltaY : number , speed ?: number ) : Promise < void > {
@@ -531,7 +527,7 @@ export class HarmonyDevice implements AbstractInterface {
531527 }
532528
533529 const { width, height } = await this . size ( ) ;
534- const n = 4 ;
530+ const n = scrollQuadrantDivisions ;
535531
536532 const startX = Math . round ( deltaX < 0 ? ( n - 1 ) * ( width / n ) : width / n ) ;
537533 const startY = Math . round ( deltaY < 0 ? ( n - 1 ) * ( height / n ) : height / n ) ;
@@ -553,7 +549,7 @@ export class HarmonyDevice implements AbstractInterface {
553549
554550 async scrollDown ( distance ?: number , startPoint ?: Point ) : Promise < void > {
555551 const { height } = await this . size ( ) ;
556- const scrollDistance = Math . round ( distance || height ) ;
552+ const scrollDistance = Math . round ( distance ?? height ) ;
557553
558554 if ( startPoint ) {
559555 const hdc = await this . getHdc ( ) ;
@@ -569,7 +565,7 @@ export class HarmonyDevice implements AbstractInterface {
569565
570566 async scrollUp ( distance ?: number , startPoint ?: Point ) : Promise < void > {
571567 const { height } = await this . size ( ) ;
572- const scrollDistance = Math . round ( distance || height ) ;
568+ const scrollDistance = Math . round ( distance ?? height ) ;
573569
574570 if ( startPoint ) {
575571 const hdc = await this . getHdc ( ) ;
@@ -585,7 +581,7 @@ export class HarmonyDevice implements AbstractInterface {
585581
586582 async scrollLeft ( distance ?: number , startPoint ?: Point ) : Promise < void > {
587583 const { width } = await this . size ( ) ;
588- const scrollDistance = Math . round ( distance || width ) ;
584+ const scrollDistance = Math . round ( distance ?? width ) ;
589585
590586 if ( startPoint ) {
591587 const hdc = await this . getHdc ( ) ;
@@ -601,7 +597,7 @@ export class HarmonyDevice implements AbstractInterface {
601597
602598 async scrollRight ( distance ?: number , startPoint ?: Point ) : Promise < void > {
603599 const { width } = await this . size ( ) ;
604- const scrollDistance = Math . round ( distance || width ) ;
600+ const scrollDistance = Math . round ( distance ?? width ) ;
605601
606602 if ( startPoint ) {
607603 const hdc = await this . getHdc ( ) ;
@@ -636,7 +632,7 @@ export class HarmonyDevice implements AbstractInterface {
636632 }
637633
638634 await repeat ( defaultScrollUntilTimes , ( ) =>
639- this . scroll ( 0 , - 9999999 , defaultFastSwipeSpeed ) ,
635+ this . scroll ( 0 , - maxScrollDistance , defaultFastSwipeSpeed ) ,
640636 ) ;
641637 await sleep ( 1000 ) ;
642638 }
@@ -655,7 +651,7 @@ export class HarmonyDevice implements AbstractInterface {
655651 }
656652
657653 await repeat ( defaultScrollUntilTimes , ( ) =>
658- this . scroll ( 0 , 9999999 , defaultFastSwipeSpeed ) ,
654+ this . scroll ( 0 , maxScrollDistance , defaultFastSwipeSpeed ) ,
659655 ) ;
660656 await sleep ( 1000 ) ;
661657 }
@@ -681,7 +677,7 @@ export class HarmonyDevice implements AbstractInterface {
681677 }
682678
683679 await repeat ( defaultScrollUntilTimes , ( ) =>
684- this . scroll ( - 9999999 , 0 , defaultFastSwipeSpeed ) ,
680+ this . scroll ( - maxScrollDistance , 0 , defaultFastSwipeSpeed ) ,
685681 ) ;
686682 await sleep ( 1000 ) ;
687683 }
@@ -700,7 +696,7 @@ export class HarmonyDevice implements AbstractInterface {
700696 }
701697
702698 await repeat ( defaultScrollUntilTimes , ( ) =>
703- this . scroll ( 9999999 , 0 , defaultFastSwipeSpeed ) ,
699+ this . scroll ( maxScrollDistance , 0 , defaultFastSwipeSpeed ) ,
704700 ) ;
705701 await sleep ( 1000 ) ;
706702 }
0 commit comments