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