1/*
2 * Copyright (c) 2023 Hunan OpenValley Digital Industry Development 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', JSON.stringify(err) ?? '');
28}
29
30export default class OpenHarmonyTestRunner implements TestRunner {
31  constructor() {
32  }
33
34  onPrepare() {
35    hilog.info(0x0000, 'testTag', '%{public}s', 'OpenHarmonyTestRunner OnPrepare ');
36  }
37
38  async onRun() {
39    hilog.info(0x0000, 'testTag', '%{public}s', 'OpenHarmonyTestRunner onRun run');
40    abilityDelegatorArguments = AbilityDelegatorRegistry.getArguments()
41    abilityDelegator = AbilityDelegatorRegistry.getAbilityDelegator()
42    var testAbilityName = abilityDelegatorArguments.bundleName + '.TestAbility'
43    let lMonitor = {
44      abilityName: testAbilityName,
45      onAbilityCreate: onAbilityCreateCallback,
46    };
47    abilityDelegator.addAbilityMonitor(lMonitor, addAbilityMonitorCallback)
48    var cmd = 'aa start -d 0 -a TestAbility' + ' -b ' + abilityDelegatorArguments.bundleName
49    var debug = abilityDelegatorArguments.parameters['-D']
50    if (debug == 'true') {
51      cmd += ' -D'
52    }
53    hilog.info(0x0000, 'testTag', 'cmd : %{public}s', cmd);
54    abilityDelegator.executeShellCommand(cmd,
55      (err: any, d: any) => {
56        hilog.info(0x0000, 'testTag', 'executeShellCommand : err : %{public}s', JSON.stringify(err) ?? '');
57        hilog.info(0x0000, 'testTag', 'executeShellCommand : data : %{public}s', d.stdResult ?? '');
58        hilog.info(0x0000, 'testTag', 'executeShellCommand : data : %{public}s', d.exitCode ?? '');
59      })
60    hilog.info(0x0000, 'testTag', '%{public}s', 'OpenHarmonyTestRunner onRun end');
61  }
62}
63
64