21
21
* @param {string } className
22
22
* @param {!Object } classType
23
23
*/
24
- function traceAPICoverage ( apiCoverage , events , className , classType ) {
24
+ function traceAPICoverage ( apiCoverage , api , events ) {
25
25
const uninstalls = [ ] ;
26
- className = className . substring ( 0 , 1 ) . toLowerCase ( ) + className . substring ( 1 ) ;
27
- for ( const methodName of Reflect . ownKeys ( classType . prototype ) ) {
28
- const method = Reflect . get ( classType . prototype , methodName ) ;
29
- if ( methodName === 'constructor' || typeof methodName !== 'string' || methodName . startsWith ( '_' ) || typeof method !== 'function' )
30
- continue ;
31
- apiCoverage . set ( `${ className } .${ methodName } ` , false ) ;
32
- const override = function ( ...args ) {
33
- apiCoverage . set ( `${ className } .${ methodName } ` , true ) ;
34
- return method . call ( this , ...args ) ;
35
- } ;
36
- Object . defineProperty ( override , 'name' , { writable : false , value : methodName } ) ;
37
- Reflect . set ( classType . prototype , methodName , override ) ;
38
- uninstalls . push ( ( ) => Reflect . set ( classType . prototype , methodName , method ) ) ;
39
- }
40
-
41
- if ( events [ classType . name ] ) {
42
- for ( const event of Object . values ( events [ classType . name ] ) ) {
43
- if ( typeof event !== 'symbol' )
44
- apiCoverage . set ( `${ className } .emit(${ JSON . stringify ( event ) } )` , false ) ;
26
+ for ( const [ name , classType ] of Object . entries ( api ) ) {
27
+ const className = name . substring ( 0 , 1 ) . toLowerCase ( ) + name . substring ( 1 ) ;
28
+ for ( const methodName of Reflect . ownKeys ( classType . prototype ) ) {
29
+ const method = Reflect . get ( classType . prototype , methodName ) ;
30
+ if ( methodName === 'constructor' || typeof methodName !== 'string' || methodName . startsWith ( '_' ) || typeof method !== 'function' )
31
+ continue ;
32
+ apiCoverage . set ( `${ className } .${ methodName } ` , false ) ;
33
+ const override = function ( ...args ) {
34
+ apiCoverage . set ( `${ className } .${ methodName } ` , true ) ;
35
+ return method . call ( this , ...args ) ;
36
+ } ;
37
+ Object . defineProperty ( override , 'name' , { writable : false , value : methodName } ) ;
38
+ Reflect . set ( classType . prototype , methodName , override ) ;
39
+ uninstalls . push ( ( ) => Reflect . set ( classType . prototype , methodName , method ) ) ;
40
+ }
41
+ if ( events [ name ] ) {
42
+ const emitClassType = ( name === 'BrowserContext' ? api [ 'ChromiumBrowserContext' ] : undefined ) || classType ;
43
+ for ( const event of Object . values ( events [ name ] ) ) {
44
+ if ( typeof event !== 'symbol' )
45
+ apiCoverage . set ( `${ className } .emit(${ JSON . stringify ( event ) } )` , false ) ;
46
+ }
47
+ const method = Reflect . get ( emitClassType . prototype , 'emit' ) ;
48
+ Reflect . set ( emitClassType . prototype , 'emit' , function ( event , ...args ) {
49
+ if ( typeof event !== 'symbol' && this . listenerCount ( event ) )
50
+ apiCoverage . set ( `${ className } .emit(${ JSON . stringify ( event ) } )` , true ) ;
51
+ return method . call ( this , event , ...args ) ;
52
+ } ) ;
53
+ uninstalls . push ( ( ) => Reflect . set ( emitClassType . prototype , 'emit' , method ) ) ;
45
54
}
46
- const method = Reflect . get ( classType . prototype , 'emit' ) ;
47
- Reflect . set ( classType . prototype , 'emit' , function ( event , ...args ) {
48
- if ( typeof event !== 'symbol' && this . listenerCount ( event ) )
49
- apiCoverage . set ( `${ className } .emit(${ JSON . stringify ( event ) } )` , true ) ;
50
- return method . call ( this , event , ...args ) ;
51
- } ) ;
52
- uninstalls . push ( ( ) => Reflect . set ( classType . prototype , 'emit' , method ) ) ;
53
55
}
54
-
55
56
return ( ) => uninstalls . forEach ( u => u ( ) ) ;
56
57
}
57
58
58
59
/**
59
- * @param {string } browserName
60
+ * @param {string } browserName
60
61
*/
61
62
function apiForBrowser ( browserName ) {
62
63
const BROWSER_CONFIGS = [
@@ -72,7 +73,7 @@ function apiForBrowser(browserName) {
72
73
name : 'Chromium' ,
73
74
events : {
74
75
...require ( '../../lib/events' ) . Events ,
75
- ... require ( '../../lib/chromium/events' ) . Events ,
76
+ ChromiumBrowserContext : require ( '../../lib/chromium/events' ) . Events . CRBrowserContext ,
76
77
}
77
78
} ,
78
79
] ;
@@ -98,15 +99,12 @@ function apiForBrowser(browserName) {
98
99
}
99
100
100
101
/**
101
- * @param {string } browserName
102
+ * @param {string } browserName
102
103
*/
103
104
function installCoverageHooks ( browserName ) {
104
- const uninstalls = [ ] ;
105
105
const { api, events} = apiForBrowser ( browserName ) ;
106
106
const coverage = new Map ( ) ;
107
- for ( const [ name , value ] of Object . entries ( api ) )
108
- uninstalls . push ( traceAPICoverage ( coverage , events , name , value ) ) ;
109
- const uninstall = ( ) => uninstalls . map ( u => u ( ) ) ;
107
+ const uninstall = traceAPICoverage ( coverage , api , events ) ;
110
108
return { coverage, uninstall} ;
111
109
}
112
110
0 commit comments