1886da342Sopenharmony_ci/* 2886da342Sopenharmony_ci * Copyright (c) 2021-2024 Huawei Device Co., Ltd. 3886da342Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License") 4886da342Sopenharmony_ci * you may not use this file except in compliance with the License. 5886da342Sopenharmony_ci * You may obtain a copy of the License at 6886da342Sopenharmony_ci * 7886da342Sopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0 8886da342Sopenharmony_ci * 9886da342Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software 10886da342Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS, 11886da342Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12886da342Sopenharmony_ci * See the License for the specific language governing permissions and 13886da342Sopenharmony_ci * limitations under the License. 14886da342Sopenharmony_ci */ 15886da342Sopenharmony_ci 16886da342Sopenharmony_ciimport Core from './src/core'; 17886da342Sopenharmony_ciimport { DEFAULT, TestType, Size, Level, TAG, PrintTag } from './src/Constant'; 18886da342Sopenharmony_ciimport DataDriver from './src/module/config/DataDriver'; 19886da342Sopenharmony_ciimport ExpectExtend from './src/module/assert/ExpectExtend'; 20886da342Sopenharmony_ciimport OhReport from './src/module/report/OhReport'; 21886da342Sopenharmony_ciimport SysTestKit from './src/module/kit/SysTestKit'; 22886da342Sopenharmony_ciimport { describe, beforeAll, beforeEach, afterEach, afterAll, it, expect, beforeItSpecified, afterItSpecified, xdescribe, xit } from './src/interface'; 23886da342Sopenharmony_ciimport { MockKit, when } from './src/module/mock/MockKit'; 24886da342Sopenharmony_ciimport ArgumentMatchers from './src/module/mock/ArgumentMatchers'; 25886da342Sopenharmony_ciimport worker from '@ohos.worker'; 26886da342Sopenharmony_ci 27886da342Sopenharmony_ciclass Hypium { 28886da342Sopenharmony_ci static context = new Map(); 29886da342Sopenharmony_ci static setData(data) { 30886da342Sopenharmony_ci const core = Core.getInstance(); 31886da342Sopenharmony_ci const dataDriver = new DataDriver({ data }); 32886da342Sopenharmony_ci core.addService('dataDriver', dataDriver); 33886da342Sopenharmony_ci } 34886da342Sopenharmony_ci 35886da342Sopenharmony_ci static setTimeConfig(systemTime) { 36886da342Sopenharmony_ci SysTestKit.systemTime = systemTime; 37886da342Sopenharmony_ci } 38886da342Sopenharmony_ci 39886da342Sopenharmony_ci static set(key, value) { 40886da342Sopenharmony_ci Hypium.context.set(key, value); 41886da342Sopenharmony_ci } 42886da342Sopenharmony_ci 43886da342Sopenharmony_ci static get(key) { 44886da342Sopenharmony_ci return Hypium.context.get(key); 45886da342Sopenharmony_ci } 46886da342Sopenharmony_ci 47886da342Sopenharmony_ci static hypiumTest(abilityDelegator, abilityDelegatorArguments, testsuite) { 48886da342Sopenharmony_ci const core = Core.getInstance(); 49886da342Sopenharmony_ci const expectExtend = new ExpectExtend({ 50886da342Sopenharmony_ci 'id': 'extend' 51886da342Sopenharmony_ci }); 52886da342Sopenharmony_ci core.addService('expect', expectExtend); 53886da342Sopenharmony_ci const ohReport = new OhReport({ 54886da342Sopenharmony_ci 'delegator': abilityDelegator, 55886da342Sopenharmony_ci 'abilityDelegatorArguments': abilityDelegatorArguments 56886da342Sopenharmony_ci }); 57886da342Sopenharmony_ci SysTestKit.delegator = abilityDelegator; 58886da342Sopenharmony_ci core.addService('report', ohReport); 59886da342Sopenharmony_ci core.init(); 60886da342Sopenharmony_ci core.subscribeEvent('spec', ohReport); 61886da342Sopenharmony_ci core.subscribeEvent('suite', ohReport); 62886da342Sopenharmony_ci core.subscribeEvent('task', ohReport); 63886da342Sopenharmony_ci const configService = core.getDefaultService('config'); 64886da342Sopenharmony_ci if (abilityDelegatorArguments !== null) { 65886da342Sopenharmony_ci let testParameters = configService.translateParams(abilityDelegatorArguments.parameters); 66886da342Sopenharmony_ci console.info(`${TAG}parameters:${JSON.stringify(testParameters)}`); 67886da342Sopenharmony_ci configService.setConfig(testParameters); 68886da342Sopenharmony_ci } 69886da342Sopenharmony_ci testsuite(); 70886da342Sopenharmony_ci core.execute(abilityDelegator); 71886da342Sopenharmony_ci } 72886da342Sopenharmony_ci static async hypiumInitWorkers(abilityDelegator, scriptURL, workerNum = 8, params) { 73886da342Sopenharmony_ci console.info(`${TAG}, hypiumInitWorkers call,${scriptURL}`); 74886da342Sopenharmony_ci let workerPromiseArray = []; 75886da342Sopenharmony_ci 76886da342Sopenharmony_ci // 开始统计时间 77886da342Sopenharmony_ci let startTime = await SysTestKit.getRealTime(); 78886da342Sopenharmony_ci for (let i = 0; i < workerNum; i++) { 79886da342Sopenharmony_ci // 创建worker线程 80886da342Sopenharmony_ci const workerPromise = Hypium.createWorkerPromise(scriptURL, i, params); 81886da342Sopenharmony_ci workerPromiseArray.push(workerPromise); 82886da342Sopenharmony_ci } 83886da342Sopenharmony_ci const ret = {total: 0, failure: 0, error: 0, pass: 0, ignore: 0, duration: 0}; 84886da342Sopenharmony_ci Promise.all(workerPromiseArray).then(async (items) => { 85886da342Sopenharmony_ci console.info(`${TAG}, all result from workers, ${JSON.stringify(items)}`); 86886da342Sopenharmony_ci let allItemList = new Array(); 87886da342Sopenharmony_ci // 统计执行结果 88886da342Sopenharmony_ci Hypium.handleWorkerTestResult(ret, allItemList, items); 89886da342Sopenharmony_ci console.info(`${TAG}, all it result, ${JSON.stringify(allItemList)}`); 90886da342Sopenharmony_ci // 统计用例执行结果 91886da342Sopenharmony_ci const retResult = {total: 0, failure: 0, error: 0, pass: 0, ignore: 0, duration: 0}; 92886da342Sopenharmony_ci // 标记用例执行结果 93886da342Sopenharmony_ci Hypium.configWorkerItTestResult(retResult, allItemList); 94886da342Sopenharmony_ci // 打印用例结果 95886da342Sopenharmony_ci Hypium.printWorkerTestResult(abilityDelegator, allItemList); 96886da342Sopenharmony_ci // 用例执行完成统计时间 97886da342Sopenharmony_ci let endTime = await SysTestKit.getRealTime(); 98886da342Sopenharmony_ci const taskConsuming = endTime - startTime; 99886da342Sopenharmony_ci const message = 100886da342Sopenharmony_ci `\n${PrintTag.OHOS_REPORT_ALL_RESULT}: stream=Test run: runTimes: ${ret.total},total: ${retResult.total}, Failure: ${retResult.failure}, Error: ${retResult.error}, Pass: ${retResult.pass}, Ignore: ${retResult.ignore}` + 101886da342Sopenharmony_ci `\n${PrintTag.OHOS_REPORT_ALL_CODE}: ${retResult.failure > 0 || retResult.error > 0 ? -1 : 0}` + 102886da342Sopenharmony_ci `\n${PrintTag.OHOS_REPORT_ALL_STATUS}: taskconsuming=${taskConsuming > 0 ? taskConsuming : ret.duration}`; 103886da342Sopenharmony_ci abilityDelegator.printSync(message); 104886da342Sopenharmony_ci console.info(`${TAG}, [end] you worker test`); 105886da342Sopenharmony_ci abilityDelegator.finishTest('you worker test finished!!!', 0, () => {}); 106886da342Sopenharmony_ci }).catch((e) => { 107886da342Sopenharmony_ci console.info(`${TAG}, [end] error you worker test, ${JSON.stringify(e)}`); 108886da342Sopenharmony_ci abilityDelegator.finishTest('you worker test error finished!!!', 0, () => {}); 109886da342Sopenharmony_ci }).finally(() => { 110886da342Sopenharmony_ci console.info(`${TAG}, all promise finally end`); 111886da342Sopenharmony_ci }); 112886da342Sopenharmony_ci } 113886da342Sopenharmony_ci // 创建worker线程 114886da342Sopenharmony_ci static createWorkerPromise(scriptURL, i, params) { 115886da342Sopenharmony_ci console.info(`${TAG}, createWorkerPromiser, ${scriptURL}, ${i}`); 116886da342Sopenharmony_ci const workerPromise = new Promise((resolve, reject) => { 117886da342Sopenharmony_ci const workerInstance = new worker.ThreadWorker(scriptURL, {name: `worker_${i}`}); 118886da342Sopenharmony_ci console.info(`${TAG}, send data to worker`); 119886da342Sopenharmony_ci // 发送数据到worker线程中 120886da342Sopenharmony_ci workerInstance.postMessage(params); 121886da342Sopenharmony_ci workerInstance.onmessage = function (e) { 122886da342Sopenharmony_ci let currentThreadName = e.data?.currentThreadName; 123886da342Sopenharmony_ci console.info(`${TAG}, receview data from ${currentThreadName}, ${JSON.stringify(e.data)}`); 124886da342Sopenharmony_ci // 125886da342Sopenharmony_ci resolve(e.data?.summary); 126886da342Sopenharmony_ci console.info(`${TAG}, ${currentThreadName} finish`); 127886da342Sopenharmony_ci workerInstance.terminate(); 128886da342Sopenharmony_ci }; 129886da342Sopenharmony_ci workerInstance.onerror = function (e) { 130886da342Sopenharmony_ci console.info(`${TAG}, worker error, ${JSON.stringify(e)}`); 131886da342Sopenharmony_ci reject(e); 132886da342Sopenharmony_ci workerInstance.terminate(); 133886da342Sopenharmony_ci }; 134886da342Sopenharmony_ci workerInstance.onmessageerror = function (e) { 135886da342Sopenharmony_ci console.info(`${TAG}, worker message error, ${JSON.stringify(e)}`); 136886da342Sopenharmony_ci reject(e); 137886da342Sopenharmony_ci workerInstance.terminate(); 138886da342Sopenharmony_ci }; 139886da342Sopenharmony_ci }); 140886da342Sopenharmony_ci return workerPromise; 141886da342Sopenharmony_ci } 142886da342Sopenharmony_ci static handleWorkerTestResult(ret, allItemList, items) { 143886da342Sopenharmony_ci console.info(`${TAG}, handleWorkerTestResult, ${JSON.stringify(items)}`); 144886da342Sopenharmony_ci for (const {total, failure, error, pass, ignore, duration, itItemList} of items) { 145886da342Sopenharmony_ci ret.total += total; 146886da342Sopenharmony_ci ret.failure += failure; 147886da342Sopenharmony_ci ret.error += error; 148886da342Sopenharmony_ci ret.pass += pass; 149886da342Sopenharmony_ci ret.ignore += ignore; 150886da342Sopenharmony_ci ret.duration += duration; 151886da342Sopenharmony_ci Hypium.handleItResult(allItemList, itItemList); 152886da342Sopenharmony_ci } 153886da342Sopenharmony_ci } 154886da342Sopenharmony_ci static handleItResult(allItemList, itItemList) { 155886da342Sopenharmony_ci // 遍历所有的用例结果统计最终结果 156886da342Sopenharmony_ci for (const {currentThreadName, description, result} of itItemList) { 157886da342Sopenharmony_ci let item = allItemList.find((it) => it.description === description); 158886da342Sopenharmony_ci if (item) { 159886da342Sopenharmony_ci let itResult = item.result; 160886da342Sopenharmony_ci // 当在worker中出现一次failure就标记为failure, 出现一次error就标记为error, 所有线程都pass才标记为pass 161886da342Sopenharmony_ci if (itResult === 0) { 162886da342Sopenharmony_ci item.result = result; 163886da342Sopenharmony_ci item.currentThreadName = currentThreadName; 164886da342Sopenharmony_ci } 165886da342Sopenharmony_ci } else { 166886da342Sopenharmony_ci let it = { 167886da342Sopenharmony_ci description: description, 168886da342Sopenharmony_ci currentThreadName: currentThreadName, 169886da342Sopenharmony_ci result: result 170886da342Sopenharmony_ci }; 171886da342Sopenharmony_ci allItemList.push(it); 172886da342Sopenharmony_ci } 173886da342Sopenharmony_ci } 174886da342Sopenharmony_ci } 175886da342Sopenharmony_ci static configWorkerItTestResult(retResult, allItemList) { 176886da342Sopenharmony_ci console.info(`${TAG}, configWorkerItTestResult, ${JSON.stringify(allItemList)}`); 177886da342Sopenharmony_ci for (const {currentThreadName, description, result} of allItemList) { 178886da342Sopenharmony_ci console.info(`${TAG}, description, ${description}, result,${result}`); 179886da342Sopenharmony_ci retResult.total ++; 180886da342Sopenharmony_ci if (result === 0) { 181886da342Sopenharmony_ci retResult.pass ++; 182886da342Sopenharmony_ci } else if (result === -1) { 183886da342Sopenharmony_ci retResult.error ++; 184886da342Sopenharmony_ci } else if (result === -2) { 185886da342Sopenharmony_ci retResult.failure ++; 186886da342Sopenharmony_ci } else { 187886da342Sopenharmony_ci retResult.ignore ++; 188886da342Sopenharmony_ci } 189886da342Sopenharmony_ci } 190886da342Sopenharmony_ci } 191886da342Sopenharmony_ci static printWorkerTestResult(abilityDelegator, allItemList) { 192886da342Sopenharmony_ci console.info(`${TAG}, printWorkerTestResult, ${JSON.stringify(allItemList)}`); 193886da342Sopenharmony_ci let index = 1; 194886da342Sopenharmony_ci for (const {currentThreadName, description, result} of allItemList) { 195886da342Sopenharmony_ci console.info(`${TAG}, description print, ${description}, result,${result}`); 196886da342Sopenharmony_ci let itArray = description.split('#'); 197886da342Sopenharmony_ci let des; 198886da342Sopenharmony_ci let itName; 199886da342Sopenharmony_ci if (itArray.length > 1) { 200886da342Sopenharmony_ci des = itArray[0]; 201886da342Sopenharmony_ci itName = itArray[1]; 202886da342Sopenharmony_ci } else if (itArray.length > 1) { 203886da342Sopenharmony_ci des = itArray[0]; 204886da342Sopenharmony_ci itName = itArray[0]; 205886da342Sopenharmony_ci } else { 206886da342Sopenharmony_ci des = 'undefined'; 207886da342Sopenharmony_ci itName = 'undefined'; 208886da342Sopenharmony_ci } 209886da342Sopenharmony_ci 210886da342Sopenharmony_ci let msg = `\n${PrintTag.OHOS_REPORT_WORKER_STATUS}: class=${des}`; 211886da342Sopenharmony_ci msg += `\n${PrintTag.OHOS_REPORT_WORKER_STATUS}: test=${itName}`; 212886da342Sopenharmony_ci msg += `\n${PrintTag.OHOS_REPORT_WORKER_STATUS}: current=${index}`; 213886da342Sopenharmony_ci msg += `\n${PrintTag.OHOS_REPORT_WORKER_STATUS}: CODE=${result}`; 214886da342Sopenharmony_ci abilityDelegator.printSync(msg); 215886da342Sopenharmony_ci index ++; 216886da342Sopenharmony_ci } 217886da342Sopenharmony_ci } 218886da342Sopenharmony_ci static hypiumWorkerTest(abilityDelegator, abilityDelegatorArguments, testsuite, workerPort) { 219886da342Sopenharmony_ci console.info(`${TAG}, hypiumWorkerTest call`); 220886da342Sopenharmony_ci SysTestKit.workerPort = workerPort; 221886da342Sopenharmony_ci let currentWorkerName = workerPort.name; 222886da342Sopenharmony_ci console.info(`${TAG}, hypiumWorkerTest_currentWorkerName: ${currentWorkerName}`); 223886da342Sopenharmony_ci Hypium.hypiumTest(abilityDelegator, abilityDelegatorArguments, testsuite); 224886da342Sopenharmony_ci 225886da342Sopenharmony_ci } 226886da342Sopenharmony_ci 227886da342Sopenharmony_ci static registerAssert(customAssertion) { 228886da342Sopenharmony_ci const core = Core.getInstance(); 229886da342Sopenharmony_ci const expectService = core.getDefaultService('expect'); 230886da342Sopenharmony_ci let matchers = {}; 231886da342Sopenharmony_ci matchers[customAssertion.name] = customAssertion; 232886da342Sopenharmony_ci expectService.addMatchers(matchers); 233886da342Sopenharmony_ci expectService.customMatchers.push(customAssertion.name); 234886da342Sopenharmony_ci console.info(`${TAG}success to register the ${customAssertion.name}`); 235886da342Sopenharmony_ci } 236886da342Sopenharmony_ci 237886da342Sopenharmony_ci static unregisterAssert(customAssertion) { 238886da342Sopenharmony_ci const core = Core.getInstance(); 239886da342Sopenharmony_ci const expectService = core.getDefaultService('expect'); 240886da342Sopenharmony_ci let customAssertionName = typeof customAssertion === 'function' ? customAssertion.name : customAssertion; 241886da342Sopenharmony_ci expectService.removeMatchers(customAssertionName); 242886da342Sopenharmony_ci console.info(`${TAG}success to unregister the ${customAssertionName}`); 243886da342Sopenharmony_ci } 244886da342Sopenharmony_ci 245886da342Sopenharmony_ci} 246886da342Sopenharmony_ci 247886da342Sopenharmony_ciexport { 248886da342Sopenharmony_ci Hypium, 249886da342Sopenharmony_ci Core, 250886da342Sopenharmony_ci DEFAULT, 251886da342Sopenharmony_ci TestType, 252886da342Sopenharmony_ci Size, 253886da342Sopenharmony_ci Level, 254886da342Sopenharmony_ci DataDriver, 255886da342Sopenharmony_ci ExpectExtend, 256886da342Sopenharmony_ci OhReport, 257886da342Sopenharmony_ci SysTestKit, 258886da342Sopenharmony_ci describe, beforeAll, beforeEach, afterEach, afterAll, it, expect, beforeItSpecified, afterItSpecified, xdescribe, xit, 259886da342Sopenharmony_ci MockKit, when, 260886da342Sopenharmony_ci ArgumentMatchers 261886da342Sopenharmony_ci};