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 AbilityDelegatorRegistry from '@ohos.app.ability.abilityDelegatorRegistry'; 17import hilog from '@ohos.hilog'; 18import TestRunner from '@ohos.application.testRunner'; 19 20let abilityDelegator; 21let abilityDelegatorArguments; 22 23async function onAbilityCreateCallback(): Promise<void> { 24 hilog.info(0x0000, 'testTag', '%{public}s', 'onAbilityCreateCallback'); 25} 26 27async function addAbilityMonitorCallback(err: Error): Promise<void> { 28 hilog.info(0x0000, 'testTag', 'addAbilityMonitorCallback : %{public}s', JSON.stringify(err) ?? ''); 29} 30 31export default class OpenHarmonyTestRunner implements TestRunner { 32 constructor() { 33 } 34 35 onPrepare(): void { 36 hilog.info(0x0000, 'testTag', '%{public}s', 'OpenHarmonyTestRunner OnPrepare '); 37 } 38 39 async onRun(): Promise<void> { 40 hilog.info(0x0000, 'testTag', '%{public}s', 'OpenHarmonyTestRunner onRun run'); 41 abilityDelegatorArguments = AbilityDelegatorRegistry.getArguments(); 42 abilityDelegator = AbilityDelegatorRegistry.getAbilityDelegator(); 43 let testAbilityName = abilityDelegatorArguments.bundleName + '.TestAbility'; 44 let lMonitor = { 45 abilityName: testAbilityName, 46 onAbilityCreate: onAbilityCreateCallback, 47 }; 48 abilityDelegator.addAbilityMonitor(lMonitor, addAbilityMonitorCallback); 49 let cmd = 'aa start -d 0 -a TestAbility' + ' -b ' + abilityDelegatorArguments.bundleName; 50 let debug = abilityDelegatorArguments.parameters['-D']; 51 if (debug === 'true') { 52 cmd += ' -D'; 53 } 54 hilog.info(0x0000, 'testTag', 'cmd : %{public}s', cmd); 55 abilityDelegator.executeShellCommand(cmd, () => { 56 }); 57 hilog.info(0x0000, 'testTag', '%{public}s', 'OpenHarmonyTestRunner onRun end'); 58 } 59}