@@ -8,15 +8,14 @@ import {
88 ConnectionStatus ,
99 DeviceConnectionEventMap ,
1010 FlashDataSource ,
11- FlashEvent ,
12- SerialDataEvent ,
13- ConnectionStatusEvent ,
11+ FlashOptions ,
12+ ProgressStage ,
1413 DeviceError ,
1514 DeviceErrorCode ,
1615 TypedEventTarget ,
17- MicrobitWebUSBConnection ,
18- SerialConnectionEventMap ,
1916} from "@microbit/microbit-connection" ;
17+ import { SerialConnectionEventMap } from "@microbit/microbit-connection/usb" ;
18+ import { MicrobitUSBConnection } from "@microbit/microbit-connection/usb" ;
2019
2120/**
2221 * A mock device used during end-to-end testing.
@@ -27,11 +26,9 @@ import {
2726 */
2827export class MockDeviceConnection
2928 extends TypedEventTarget < DeviceConnectionEventMap & SerialConnectionEventMap >
30- implements MicrobitWebUSBConnection
29+ implements MicrobitUSBConnection
3130{
32- status : ConnectionStatus = ( navigator as any ) . usb
33- ? ConnectionStatus . NO_AUTHORIZED_DEVICE
34- : ConnectionStatus . NOT_SUPPORTED ;
31+ status : ConnectionStatus = ConnectionStatus . NoAuthorizedDevice ;
3532
3633 private connectResults : DeviceErrorCode [ ] = [ ] ;
3734
@@ -42,34 +39,38 @@ export class MockDeviceConnection
4239 }
4340
4441 mockSerialWrite ( data : string ) {
45- this . dispatchTypedEvent ( "serialdata" , new SerialDataEvent ( data ) ) ;
42+ this . dispatchEvent ( "serialdata" , { data } ) ;
4643 }
4744
4845 mockConnect ( code : DeviceErrorCode ) {
4946 this . connectResults . push ( code ) ;
5047 }
5148
5249 async initialize ( ) : Promise < void > { }
50+ async checkAvailability ( ) {
51+ return "available" as const ;
52+ }
5353
5454 dispose ( ) { }
55- getDeviceId ( ) : number | undefined {
56- return undefined ;
55+ getDeviceId ( ) : number {
56+ return 0 ;
5757 }
5858 setRequestDeviceExclusionFilters ( ) : void { }
59- getDevice ( ) { }
59+ getDevice ( ) {
60+ return undefined ;
61+ }
6062 async softwareReset ( ) : Promise < void > { }
6163
62- async connect ( ) : Promise < ConnectionStatus > {
64+ async connect ( ) : Promise < void > {
6365 const next = this . connectResults . shift ( ) ;
6466 if ( next ) {
6567 throw new DeviceError ( { code : next , message : "Mocked failure" } ) ;
6668 }
6769
68- this . setStatus ( ConnectionStatus . CONNECTED ) ;
69- return this . status ;
70+ this . setStatus ( ConnectionStatus . Connected ) ;
7071 }
7172
72- getBoardVersion ( ) : BoardVersion | undefined {
73+ getBoardVersion ( ) : BoardVersion {
7374 return "V2" ;
7475 }
7576
@@ -81,42 +82,32 @@ export class MockDeviceConnection
8182 */
8283 async flash (
8384 _dataSource : FlashDataSource ,
84- options : {
85- /**
86- * True to use a partial flash where possible, false to force a full flash.
87- */
88- partial : boolean ;
89- /**
90- * A progress callback. Called with undefined when the process is complete or has failed.
91- */
92- progress : ( percentage : number | undefined ) => void ;
93- }
85+ options : FlashOptions
9486 ) : Promise < void > {
9587 await new Promise ( ( resolve ) => setTimeout ( resolve , 100 ) ) ;
96- options . progress ( 0.5 ) ;
88+ options . progress ?. ( ProgressStage . PartialFlashing , 0.5 ) ;
9789 await new Promise ( ( resolve ) => setTimeout ( resolve , 100 ) ) ;
98- options . progress ( undefined ) ;
99- this . dispatchTypedEvent ( "flash" , new FlashEvent ( ) ) ;
90+ this . dispatchEvent ( "flash" ) ;
10091 }
10192
10293 async disconnect ( ) : Promise < void > {
103- this . setStatus ( ConnectionStatus . DISCONNECTED ) ;
94+ this . setStatus ( ConnectionStatus . Disconnected ) ;
10495 }
10596
10697 async serialWrite ( data : string ) : Promise < void > {
10798 console . log ( "[Serial] " , data ) ;
10899 }
109100
110101 private setStatus ( newStatus : ConnectionStatus ) {
102+ const previousStatus = this . status ;
111103 this . status = newStatus ;
112- this . dispatchTypedEvent ( "status" , new ConnectionStatusEvent ( this . status ) ) ;
104+ this . dispatchEvent ( "status" , {
105+ status : newStatus ,
106+ previousStatus,
107+ } ) ;
113108 }
114109
115110 clearDevice ( ) : void {
116- this . setStatus ( ConnectionStatus . NO_AUTHORIZED_DEVICE ) ;
117- }
118-
119- mockWebUsbNotSupported ( ) : void {
120- this . setStatus ( ConnectionStatus . NOT_SUPPORTED ) ;
111+ this . setStatus ( ConnectionStatus . NoAuthorizedDevice ) ;
121112 }
122113}
0 commit comments