| 
									
										
										
										
											2020-04-07 22:04:23 -07:00
										 |  |  | /** | 
					
						
							|  |  |  |  * Copyright 2017 Google Inc. All rights reserved. | 
					
						
							|  |  |  |  * Modifications copyright (c) Microsoft Corporation. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * Licensed under the Apache License, Version 2.0 (the "License"); | 
					
						
							|  |  |  |  * you may not use this file except in compliance with the License. | 
					
						
							|  |  |  |  * You may obtain a copy of the License at | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  *     http://www.apache.org/licenses/LICENSE-2.0
 | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * Unless required by applicable law or agreed to in writing, software | 
					
						
							|  |  |  |  * distributed under the License is distributed on an "AS IS" BASIS, | 
					
						
							|  |  |  |  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | 
					
						
							|  |  |  |  * See the License for the specific language governing permissions and | 
					
						
							|  |  |  |  * limitations under the License. | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /** | 
					
						
							|  |  |  |  * @param {Map<string, boolean>} apiCoverage | 
					
						
							|  |  |  |  * @param {Object} events | 
					
						
							|  |  |  |  * @param {string} className | 
					
						
							|  |  |  |  * @param {!Object} classType | 
					
						
							|  |  |  |  */ | 
					
						
							| 
									
										
										
										
											2020-07-23 10:30:47 -07:00
										 |  |  | function traceAPICoverage(apiCoverage, api, events) { | 
					
						
							| 
									
										
										
										
											2020-07-22 09:08:10 -07:00
										 |  |  |   const uninstalls = []; | 
					
						
							| 
									
										
										
										
											2020-07-23 10:30:47 -07:00
										 |  |  |   for (const [name, classType] of Object.entries(api)) { | 
					
						
							|  |  |  |     const className = name.substring(0, 1).toLowerCase() + name.substring(1); | 
					
						
							|  |  |  |     for (const methodName of Reflect.ownKeys(classType.prototype)) { | 
					
						
							|  |  |  |       const method = Reflect.get(classType.prototype, methodName); | 
					
						
							|  |  |  |       if (methodName === 'constructor' || typeof methodName !== 'string' || methodName.startsWith('_') || typeof method !== 'function') | 
					
						
							|  |  |  |         continue; | 
					
						
							| 
									
										
										
										
											2021-02-11 10:31:57 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-07-23 10:30:47 -07:00
										 |  |  |       apiCoverage.set(`${className}.${methodName}`, false); | 
					
						
							|  |  |  |       const override = function(...args) { | 
					
						
							|  |  |  |         apiCoverage.set(`${className}.${methodName}`, true); | 
					
						
							|  |  |  |         return method.call(this, ...args); | 
					
						
							|  |  |  |       }; | 
					
						
							|  |  |  |       Object.defineProperty(override, 'name', { writable: false, value: methodName }); | 
					
						
							|  |  |  |       Reflect.set(classType.prototype, methodName, override); | 
					
						
							|  |  |  |       uninstalls.push(() => Reflect.set(classType.prototype, methodName, method)); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     if (events[name]) { | 
					
						
							|  |  |  |       const emitClassType = (name === 'BrowserContext' ? api['ChromiumBrowserContext'] : undefined) || classType; | 
					
						
							|  |  |  |       for (const event of Object.values(events[name])) { | 
					
						
							|  |  |  |         if (typeof event !== 'symbol') | 
					
						
							|  |  |  |           apiCoverage.set(`${className}.emit(${JSON.stringify(event)})`, false); | 
					
						
							|  |  |  |       } | 
					
						
							|  |  |  |       const method = Reflect.get(emitClassType.prototype, 'emit'); | 
					
						
							|  |  |  |       Reflect.set(emitClassType.prototype, 'emit', function(event, ...args) { | 
					
						
							|  |  |  |         if (typeof event !== 'symbol' && this.listenerCount(event)) | 
					
						
							|  |  |  |           apiCoverage.set(`${className}.emit(${JSON.stringify(event)})`, true); | 
					
						
							|  |  |  |         return method.call(this, event, ...args); | 
					
						
							|  |  |  |       }); | 
					
						
							|  |  |  |       uninstalls.push(() => Reflect.set(emitClassType.prototype, 'emit', method)); | 
					
						
							| 
									
										
										
										
											2020-04-07 22:04:23 -07:00
										 |  |  |     } | 
					
						
							|  |  |  |   } | 
					
						
							| 
									
										
										
										
											2020-07-22 09:08:10 -07:00
										 |  |  |   return () => uninstalls.forEach(u => u()); | 
					
						
							| 
									
										
										
										
											2020-04-07 22:04:23 -07:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-07-22 09:08:10 -07:00
										 |  |  | /** | 
					
						
							| 
									
										
										
										
											2020-07-23 10:30:47 -07:00
										 |  |  |  * @param {string} browserName | 
					
						
							| 
									
										
										
										
											2020-07-22 09:08:10 -07:00
										 |  |  |  */ | 
					
						
							|  |  |  | function apiForBrowser(browserName) { | 
					
						
							| 
									
										
										
										
											2020-08-22 15:13:51 -07:00
										 |  |  |   const events = require('../lib/client/events').Events; | 
					
						
							|  |  |  |   const api = require('../lib/client/api'); | 
					
						
							| 
									
										
										
										
											2020-08-21 16:26:33 -07:00
										 |  |  |   const otherBrowsers = ['chromium', 'webkit', 'firefox'].filter(name => name.toLowerCase() !== browserName.toLowerCase()); | 
					
						
							| 
									
										
										
										
											2020-07-22 09:08:10 -07:00
										 |  |  |   const filteredKeys = Object.keys(api).filter(apiName => { | 
					
						
							| 
									
										
										
										
											2021-02-01 11:43:26 -08:00
										 |  |  |     if (apiName.toLowerCase().startsWith('electron')) | 
					
						
							|  |  |  |       return browserName === 'chromium'; | 
					
						
							| 
									
										
										
										
											2020-08-21 16:26:33 -07:00
										 |  |  |     return !otherBrowsers.some(otherName => apiName.toLowerCase().startsWith(otherName)); | 
					
						
							| 
									
										
										
										
											2020-04-08 15:19:09 -07:00
										 |  |  |   }); | 
					
						
							| 
									
										
										
										
											2020-07-22 09:08:10 -07:00
										 |  |  |   const filteredAPI = {}; | 
					
						
							|  |  |  |   for (const key of filteredKeys) | 
					
						
							|  |  |  |     filteredAPI[key] = api[key]; | 
					
						
							|  |  |  |   return { | 
					
						
							|  |  |  |     api: filteredAPI, | 
					
						
							|  |  |  |     events | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2020-04-07 22:04:23 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-07-22 09:08:10 -07:00
										 |  |  | /** | 
					
						
							| 
									
										
										
										
											2020-07-23 10:30:47 -07:00
										 |  |  |  * @param {string} browserName | 
					
						
							| 
									
										
										
										
											2020-07-22 09:08:10 -07:00
										 |  |  |  */ | 
					
						
							|  |  |  | function installCoverageHooks(browserName) { | 
					
						
							|  |  |  |   const {api, events} = apiForBrowser(browserName); | 
					
						
							|  |  |  |   const coverage = new Map(); | 
					
						
							| 
									
										
										
										
											2020-07-23 10:30:47 -07:00
										 |  |  |   const uninstall = traceAPICoverage(coverage, api, events); | 
					
						
							| 
									
										
										
										
											2020-07-22 09:08:10 -07:00
										 |  |  |   return {coverage, uninstall}; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | module.exports = { | 
					
						
							|  |  |  |   installCoverageHooks | 
					
						
							|  |  |  | }; |