1/* 2 * Copyright (c) 2021-2022 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16import {SuiteService, SpecService, ExpectService, ReportService} from './service'; 17import {ConfigService} from './module/config/configService'; 18import {SpecEvent, TaskEvent, SuiteEvent} from './event'; 19 20/** 21 * core service for execute testcase. 22 */ 23class Core { 24 static getInstance() { 25 if (!this.instance) { 26 this.instance = new Core(); 27 } 28 return this.instance; 29 } 30 31 constructor() { 32 this.instance = null; 33 this.services = { 34 suite: {}, 35 spec: {}, 36 config: {}, 37 expect: {}, 38 log: {}, 39 report: {} 40 41 }; 42 this.events = { 43 suite: {}, 44 spec: {}, 45 task: {} 46 }; 47 } 48 49 addService(name, service) { 50 let serviceObj = {}; 51 if (!this.services[name]) { 52 this.services[name] = serviceObj; 53 } else { 54 serviceObj = this.services[name]; 55 } 56 serviceObj[service.id] = service; 57 } 58 59 getDefaultService(name) { 60 return this.services[name].default; 61 } 62 63 getServices(name) { 64 return this.services[name]; 65 } 66 67 registerEvent(serviceName, event) { 68 let eventObj = {}; 69 if (!this.events[serviceName]) { 70 this.events[serviceName] = eventObj; 71 } else { 72 eventObj = this.events[serviceName]; 73 } 74 eventObj[event.id] = event; 75 } 76 77 unRegisterEvent(serviceName, eventID) { 78 const eventObj = this.events[serviceName]; 79 if (eventObj) { 80 delete eventObj[eventID]; 81 } 82 } 83 84 subscribeEvent(serviceName, serviceObj) { 85 const eventObj = this.events[serviceName]; 86 if (eventObj) { 87 for (const attr in eventObj) { 88 eventObj[attr]['subscribeEvent'](serviceObj); 89 } 90 } 91 } 92 93 async fireEvents(serviceName, eventName) { 94 const eventObj = this.events[serviceName]; 95 if (!eventObj) { 96 return; 97 } 98 for (const attr in eventObj) { 99 await eventObj[attr][eventName](); 100 } 101 } 102 103 addToGlobal(apis) { 104 if (typeof globalThis !== 'undefined') { 105 for (let api in apis) { 106 globalThis[api] = apis[api]; 107 } 108 } 109 for (const api in apis) { 110 this[api] = apis[api]; 111 } 112 } 113 114 init() { 115 this.addService('suite', new SuiteService({id: 'default'})); 116 this.addService('spec', new SpecService({id: 'default'})); 117 this.addService('expect', new ExpectService({id: 'default'})); 118 this.addService('report', new ReportService({id: 'default'})); 119 this.addService('config', new ConfigService({id: 'default'})); 120 this.registerEvent('task', new TaskEvent({id: 'default', coreContext: this})); 121 this.registerEvent('suite', new SuiteEvent({id: 'default', coreContext: this})); 122 this.registerEvent('spec', new SpecEvent({id: 'default', coreContext: this})); 123 this.subscribeEvent('spec', this.getDefaultService('report')); 124 this.subscribeEvent('suite', this.getDefaultService('report')); 125 this.subscribeEvent('task', this.getDefaultService('report')); 126 const context = this; 127 for (const key in this.services) { 128 const serviceObj = this.services[key]; 129 for (const serviceID in serviceObj) { 130 const service = serviceObj[serviceID]; 131 service.init(context); 132 133 if (typeof service.apis !== 'function') { 134 continue; 135 } 136 const apis = service.apis(); 137 if (apis) { 138 this.addToGlobal(apis); 139 } 140 } 141 } 142 } 143 144 execute(abilityDelegator) { 145 const suiteService = this.getDefaultService('suite'); 146 const configService = this.getDefaultService('config'); 147 if (configService['dryRun'] === 'true') { 148 (async function () { 149 await suiteService.dryRun(abilityDelegator); 150 })(); 151 return; 152 } 153 setTimeout(() => { 154 suiteService.execute(); 155 }, 10); 156 } 157} 158 159export default Core; 160