1/* 2 * Copyright (c) 2025 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 abilityDelegatorRegistry from '@ohos.app.ability.abilityDelegatorRegistry'; 17 18import { Core } from './core'; 19import { TAG, PrintTag } from './Constant'; 20import DataDriver from './module/config/DataDriver'; 21import { ExpectExtend } from './module/assert/ExpectExtend'; 22import { OhReport } from './module/report/OhReport'; 23import { SysTestKit } from './module/kit/SysTestKit'; 24import { TestcaseSummaryIF, ItItemIF, DataDriverData } from './interface'; 25import { AnyType } from './module/types/common'; 26import { ConfigService } from './module/config/configService'; 27 28export class Hypium { 29 public static context = new Map<string, AnyType>(); 30 static setData(data: DataDriverData) { 31 const core = Core.getInstance(); 32 const dataDriver = new DataDriver({ data }); 33 if (core) { 34 core.addService('dataDriver', dataDriver); 35 } else { 36 throw Error('core is not created'); 37 } 38 } 39 static setTimeConfig(systemTime: number) { 40 SysTestKit.systemTime = systemTime; 41 } 42 43 static set(key: string, value: AnyType) { 44 Hypium.context.set(key, value); 45 } 46 47 static get(key: string) { 48 return Hypium.context.get(key); 49 } 50 51 static hypiumTest( 52 abilityDelegator: abilityDelegatorRegistry.AbilityDelegator, 53 abilityDelegatorArguments: abilityDelegatorRegistry.AbilityDelegatorArgs, 54 testsuite: () => void 55 ) { 56 const core = Core.getInstance(); 57 const expectExtend = new ExpectExtend({ 58 id: 'extend', 59 }); 60 const ohReport = new OhReport({ 61 delegator: abilityDelegator, 62 abilityDelegatorArguments: abilityDelegatorArguments, 63 }); 64 SysTestKit.delegator = abilityDelegator; 65 if (core) { 66 core.addService('expect', expectExtend); 67 core.addService('report', ohReport); 68 core.init(); 69 core.subscribeEvent('spec', ohReport); 70 core.subscribeEvent('suite', ohReport); 71 core.subscribeEvent('task', ohReport); 72 const cService = core.getDefaultService('config'); 73 if (cService !== null && abilityDelegatorArguments !== null) { 74 const configService = cService as ConfigService; 75 const testParameters = configService.translateParams(abilityDelegatorArguments.parameters); 76 console.info(`${TAG}parameters:${JSON.stringify(testParameters)}`); 77 configService.setConfig(testParameters); 78 } 79 testsuite(); 80 core.execute(abilityDelegator); 81 } 82 } 83 84} 85