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 TestRunner from '@ohos.application.testRunner';
18import AbilityDelegatorRegistry from '@ohos.app.ability.abilityDelegatorRegistry';
19
20var abilityDelegator = undefined
21var abilityDelegatorArguments = undefined
22
23async function onAbilityCreateCallback() {
24    hilog.info(0x0000, 'testTag', '%{public}s', 'onAbilityCreateCallback');
25}
26
27async function addAbilityMonitorCallback(err: any) {
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() {
36        hilog.info(0x0000, 'testTag', '%{public}s', 'OpenHarmonyTestRunner OnPrepare ');
37    }
38
39    async onRun() {
40        hilog.info(0x0000, 'testTag', '%{public}s', 'OpenHarmonyTestRunner onRun run');
41        abilityDelegatorArguments = AbilityDelegatorRegistry.getArguments()
42        abilityDelegator = AbilityDelegatorRegistry.getAbilityDelegator()
43        var testAbilityName = abilityDelegatorArguments.bundleName + '.TestAbility'
44        let lMonitor = {
45            abilityName: testAbilityName,
46            onAbilityCreate: onAbilityCreateCallback,
47        };
48        abilityDelegator.addAbilityMonitor(lMonitor, addAbilityMonitorCallback)
49        var cmd = 'aa start -d 0 -a TestAbility' + ' -b ' + abilityDelegatorArguments.bundleName
50        var debug = abilityDelegatorArguments.parameters['-D']
51        if (debug == 'true')
52        {
53            cmd += ' -D'
54        }
55        hilog.info(0x0000, 'testTag', 'cmd : %{public}s', cmd);
56        abilityDelegator.executeShellCommand(cmd,
57            (err: any, d: any) => {
58                hilog.info(0x0000, 'testTag', 'executeShellCommand : err : %{public}s', JSON.stringify(err) ?? '');
59                hilog.info(0x0000, 'testTag', 'executeShellCommand : data : %{public}s', d.stdResult ?? '');
60                hilog.info(0x0000, 'testTag', 'executeShellCommand : data : %{public}s', d.exitCode ?? '');
61            })
62        hilog.info(0x0000, 'testTag', '%{public}s', 'OpenHarmonyTestRunner onRun end');
63    }
64}