1/*
2 * Copyright (C) 2024 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 */
15import hilog from '@ohos.hilog';
16import TestRunner from '@ohos.application.testRunner';
17import AbilityDelegatorRegistry from '@ohos.app.ability.abilityDelegatorRegistry';
18
19var abilityDelegator = undefined
20var abilityDelegatorArguments = undefined
21
22async function onAbilityCreateCallback() {
23    hilog.info(0x0000, 'testTag', '%{public}s', 'onAbilityCreateCallback');
24}
25
26async function addAbilityMonitorCallback(err: any) {
27    hilog.info(0x0000, 'testTag', 'addAbilityMonitorCallback : %{public}s',
28        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    onRun() {
40        hilog.info(0x0000, 'testTag', '%{public}s', 'OpenHarmonyTestRunner onRun run');
41        abilityDelegatorArguments = AbilityDelegatorRegistry.getArguments()
42        abilityDelegator = AbilityDelegatorRegistry.getAbilityDelegator()
43        var testAbilityName = abilityDelegatorArguments.parameters['-p'] + '.TestAbility'
44        let lMonitor = {
45            abilityName: testAbilityName,
46            onAbilityCreate: onAbilityCreateCallback,
47        };
48        abilityDelegator.addAbilityMonitor(lMonitor, addAbilityMonitorCallback)
49        var cmd = 'aa start -d 0 -a ' + testAbilityName + ' -b ' + abilityDelegatorArguments.bundleName
50        var 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            (err: any, d: any) => {
57                hilog.info(0x0000, 'testTag', 'executeShellCommand : err : %{public}s',
58                    JSON.stringify(err) ?? '');
59                hilog.info(0x0000, 'testTag', 'executeShellCommand : data : %{public}s',
60                    d.stdResult ?? '');
61                hilog.info(0x0000, 'testTag', 'executeShellCommand : data : %{public}s',
62                    d.exitCode ?? '');
63            })
64        hilog.info(0x0000, 'testTag', '%{public}s', 'OpenHarmonyTestRunner onRun end');
65    }
66};