1
2/*
3 * Copyright (C) 2024 Huawei Device Co., Ltd.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *     http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17import hilog from '@ohos.hilog';
18import TestRunner from '@ohos.application.testRunner';
19import AbilityDelegatorRegistry from '@ohos.app.ability.abilityDelegatorRegistry';
20
21var abilityDelegator = undefined
22var abilityDelegatorArguments = undefined
23
24async function onAbilityCreateCallback() {
25    hilog.info(0x0000, 'testTag', '%{public}s', 'onAbilityCreateCallback');
26}
27
28async function addAbilityMonitorCallback(err: any) {
29    hilog.info(0x0000, 'testTag', 'addAbilityMonitorCallback : %{public}s', JSON.stringify(err) ?? '');
30}
31
32export default class OpenHarmonyTestRunner implements TestRunner {
33    constructor() {
34    }
35
36    onPrepare() {
37        hilog.info(0x0000, 'testTag', '%{public}s', 'OpenHarmonyTestRunner OnPrepare ');
38    }
39
40    async onRun() {
41        hilog.info(0x0000, 'testTag', '%{public}s', 'OpenHarmonyTestRunner onRun run');
42        abilityDelegatorArguments = AbilityDelegatorRegistry.getArguments()
43        abilityDelegator = AbilityDelegatorRegistry.getAbilityDelegator()
44        var testAbilityName = abilityDelegatorArguments.bundleName + '.TestAbility'
45        let lMonitor = {
46            abilityName: testAbilityName,
47            onAbilityCreate: onAbilityCreateCallback,
48        };
49        abilityDelegator.addAbilityMonitor(lMonitor, addAbilityMonitorCallback)
50        var cmd = 'aa start -d 0 -a TestAbility' + ' -b ' + abilityDelegatorArguments.bundleName
51        var debug = abilityDelegatorArguments.parameters['-D']
52        if (debug == 'true')
53        {
54            cmd += ' -D'
55        }
56        hilog.info(0x0000, 'testTag', 'cmd : %{public}s', cmd);
57        abilityDelegator.executeShellCommand(cmd,
58            (err: any, d: any) => {
59                hilog.info(0x0000, 'testTag', 'executeShellCommand : err : %{public}s', JSON.stringify(err) ?? '');
60                hilog.info(0x0000, 'testTag', 'executeShellCommand : data : %{public}s', d.stdResult ?? '');
61                hilog.info(0x0000, 'testTag', 'executeShellCommand : data : %{public}s', d.exitCode ?? '');
62            })
63        hilog.info(0x0000, 'testTag', '%{public}s', 'OpenHarmonyTestRunner onRun end');
64    }
65}