• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.application.abilityDelegatorRegistry'
18
19var abilityDelegator = undefined
20var abilityDelegatorArguments = undefined
21
22function translateParamsToString(parameters) {
23    const keySet = new Set([
24        '-s class', '-s notClass', '-s suite', '-s it',
25        '-s level', '-s testType', '-s size', '-s timeout',
26        '-s dryRun'
27    ]);
28    let targetParams = '';
29    for (const key in parameters) {
30        if (keySet.has(key)) {
31            targetParams = `${targetParams} ${key} ${parameters[key]}`;
32        }
33    }
34    return targetParams.trim();
35}
36
37async function onAbilityCreateCallback() {
38    hilog.isLoggable(0x0000, 'testTag', hilog.LogLevel.INFO);
39    hilog.info(0x0000, 'testTag', '%{public}s', 'onAbilityCreateCallback');
40}
41
42async function addAbilityMonitorCallback(err): Promise<void> {
43    hilog.isLoggable(0x0000, 'testTag', hilog.LogLevel.INFO);
44    hilog.info(0x0000, 'testTag', 'addAbilityMonitorCallback : %{public}s', JSON.stringify(err) ?? '');
45}
46
47export default class OpenHarmonyTestRunner implements TestRunner {
48    constructor() {
49    }
50
51    onPrepare() {
52        hilog.isLoggable(0x0000, 'testTag', hilog.LogLevel.INFO);
53        hilog.info(0x0000, 'testTag', '%{public}s', 'OpenHarmonyTestRunner OnPrepare ');
54    }
55
56    async onRun() {
57        hilog.isLoggable(0x0000, 'testTag', hilog.LogLevel.INFO);
58        hilog.info(0x0000, 'testTag', '%{public}s', 'OpenHarmonyTestRunner onRun run');
59        abilityDelegatorArguments = AbilityDelegatorRegistry.getArguments();
60        abilityDelegator = AbilityDelegatorRegistry.getAbilityDelegator();
61        let testAbilityName = abilityDelegatorArguments.bundleName + '.TestAbility';
62        let lMonitor = {
63            abilityName: testAbilityName,
64            onAbilityCreate: onAbilityCreateCallback,
65        };
66        abilityDelegator.addAbilityMonitor(lMonitor, addAbilityMonitorCallback);
67        let cmd = 'aa start -d 0 -a TestAbility' + ' -b ' + abilityDelegatorArguments.bundleName;
68        cmd += ' ' + translateParamsToString(abilityDelegatorArguments.parameters);
69        let debug = abilityDelegatorArguments.parameters['-D'];
70        if (debug == 'true') {
71            cmd += ' -D';
72        }
73        hilog.isLoggable(0x0000, 'testTag', hilog.LogLevel.INFO);
74        hilog.info(0x0000, 'testTag', 'cmd : %{public}s', cmd);
75        abilityDelegator.executeShellCommand(cmd,
76            (err, d) => {
77                hilog.isLoggable(0x0000, 'testTag', hilog.LogLevel.INFO);
78                hilog.info(0x0000, 'testTag', 'executeShellCommand : err : %{public}s', JSON.stringify(err) ?? '');
79                hilog.info(0x0000, 'testTag', 'executeShellCommand : data : %{public}s', d.stdResult ?? '');
80                hilog.info(0x0000, 'testTag', 'executeShellCommand : data : %{public}s', d.exitCode ?? '');
81            });
82        hilog.info(0x0000, 'testTag', '%{public}s', 'OpenHarmonyTestRunner onRun end');
83    }
84}