1/* 2 * Copyright (c) 2023 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 hilog from '@ohos.hilog'; 17import { BusinessError } from '@ohos.base'; 18import UIAbility from '@ohos.app.ability.UIAbility'; 19import TestRunner from '@ohos.application.testRunner'; 20import AbilityDelegatorRegistry from '@ohos.app.ability.abilityDelegatorRegistry'; 21import Want from '@ohos.app.ability.Want'; 22import resourceManager from '@ohos.resourceManager'; 23import util from '@ohos.util'; 24 25let abilityDelegator: AbilityDelegatorRegistry.AbilityDelegator; 26let abilityDelegatorArguments: AbilityDelegatorRegistry.AbilityDelegatorArgs; 27let jsonPath: string = 'mock/mock-config.json'; 28let tag: string = 'testTag'; //日志标识字符串,作为tag标识当前runner类下的测试行为 29let domain: number = 0x0000; //日志标识,0x0000作为测试框架的业务标识 30 31async function onAbilityCreateCallback(data: UIAbility) { 32 hilog.info(domain, tag, 'onAbilityCreateCallback, data: ${}', JSON.stringify(data)); 33} 34 35async function addAbilityMonitorCallback(err: BusinessError) { 36 hilog.info(domain, tag, 'addAbilityMonitorCallback : %{public}s', JSON.stringify(err) ?? ''); 37} 38 39export default class OpenHarmonyTestRunner implements TestRunner { 40 constructor() { 41 } 42 43 onPrepare() { 44 hilog.info(domain, tag, '%{public}s', 'OpenHarmonyTestRunner OnPrepare'); 45 } 46 47 async onRun() { 48 hilog.info(domain, tag, '%{public}s', 'OpenHarmonyTestRunner onRun run'); 49 abilityDelegatorArguments = AbilityDelegatorRegistry.getArguments() 50 abilityDelegator = AbilityDelegatorRegistry.getAbilityDelegator() 51 let moduleName = abilityDelegatorArguments.parameters['-m']; 52 let context = abilityDelegator.getAppContext().getApplicationContext().createModuleContext(moduleName); 53 let mResourceManager = context.resourceManager; 54 checkMock(abilityDelegator, mResourceManager); 55 const bundleName = abilityDelegatorArguments.bundleName; 56 const testAbilityName: string = 'TestAbility'; 57 let lMonitor: AbilityDelegatorRegistry.AbilityMonitor = { 58 abilityName: testAbilityName, 59 onAbilityCreate: onAbilityCreateCallback, 60 }; 61 abilityDelegator.addAbilityMonitor(lMonitor, addAbilityMonitorCallback) 62 const want: Want = { 63 bundleName: bundleName, 64 abilityName: testAbilityName 65 }; 66 abilityDelegator.startAbility(want, (err: BusinessError, data: void) => { 67 hilog.info(domain, tag, 'startAbility : err : %{public}s', JSON.stringify(err) ?? ''); 68 hilog.info(domain, tag, 'startAbility : data : %{public}s', JSON.stringify(data) ?? ''); 69 }) 70 hilog.info(domain, tag, '%{public}s', 'OpenHarmonyTestRunner onRun end'); 71 } 72} 73 74function checkMock(abilityDelegator: AbilityDelegatorRegistry.AbilityDelegator, resourceManager: resourceManager.ResourceManager) { 75 let rawFile: Uint8Array; 76 try { 77 rawFile = resourceManager.getRawFileContentSync(jsonPath); 78 hilog.info(domain, tag, 'MockList file exists'); 79 let mockStr: string = util.TextDecoder.create("utf-8", { ignoreBOM: true }).decodeWithStream(rawFile); 80 let mockMap: Record<string, string> = getMockList(mockStr); 81 try { 82 abilityDelegator.setMockList(mockMap) 83 } catch (error) { 84 let code = (error as BusinessError).code; 85 let message = (error as BusinessError).message; 86 hilog.error(domain, tag, `abilityDelegator.setMockList failed, error code: ${code}, message: ${message}.`); 87 } 88 } catch (error) { 89 let code = (error as BusinessError).code; 90 let message = (error as BusinessError).message; 91 hilog.error(domain, tag, `ResourceManager:callback getRawFileContent failed, error code: ${code}, message: ${message}.`); 92 } 93} 94 95function getMockList(jsonStr: string) { 96 let jsonObj: Record<string, Object> = JSON.parse(jsonStr); 97 let map: Map<string, object> = new Map<string, object>(Object.entries(jsonObj)); 98 let mockList: Record<string, string> = {}; 99 map.forEach((value: object, key: string) => { 100 let realValue: string = value['source'].toString(); 101 mockList[key] = realValue; 102 }); 103 hilog.info(domain, tag, '%{public}s', 'mock-json value:' + JSON.stringify(mockList) ?? ''); 104 return mockList; 105}