• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1/*
2 * Copyright (C) 2022 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 TestRunner from '@ohos.application.testRunner'
16import AbilityDelegatorRegistry from '@ohos.application.abilityDelegatorRegistry'
17
18var abilityDelegator = undefined
19var abilityDelegatorArguments = undefined
20
21function translateParamsToString(parameters) {
22    const keySet = new Set([
23        '-s class', '-s notClass', '-s suite', '-s itName',
24        '-s level', '-s testType', '-s size', '-s timeout',
25        '-s package'
26    ])
27    let targetParams = '';
28    for (const key in parameters) {
29        if (keySet.has(key)) {
30            targetParams += ' ' + key + ' ' + parameters[key]
31        }
32    }
33    return targetParams.trim()
34}
35
36async function onAbilityCreateCallback() {
37    console.log('onAbilityCreateCallback');
38}
39
40async function addAbilityMonitorCallback(err: any) {
41    console.info('addAbilityMonitorCallback : ' + JSON.stringify(err))
42}
43
44export default class OpenHarmonyTestRunner implements TestRunner {
45    constructor() {
46    }
47
48    onPrepare() {
49        console.info('OpenHarmonyTestRunner OnPrepare')
50    }
51
52    onRun() {
53        console.log('OpenHarmonyTestRunner onRun run')
54        abilityDelegatorArguments = AbilityDelegatorRegistry.getArguments()
55        abilityDelegator = AbilityDelegatorRegistry.getAbilityDelegator()
56
57        let lMonitor = {
58            abilityName: testAbilityName,
59            onAbilityCreate: onAbilityCreateCallback,
60        };
61        var testAbilityName = abilityDelegatorArguments.parameters['-p'] + '.TestAbility'
62        abilityDelegator.addAbilityMonitor(lMonitor, addAbilityMonitorCallback)
63        var cmd = 'aa start -d 0 -a ' + testAbilityName + ' -b ' + abilityDelegatorArguments.bundleName
64        cmd += ' '+translateParamsToString(abilityDelegatorArguments.parameters)
65        console.info('cmd : '+cmd)
66        abilityDelegator.executeShellCommand(cmd,
67            (err: any, d: any) => {
68                console.info('executeShellCommand : err : ' + JSON.stringify(err));
69                console.info('executeShellCommand : data : ' + d.stdResult);
70                console.info('executeShellCommand : data : ' + d.exitCode);
71            })
72        console.info('OpenHarmonyTestRunner onRun call abilityDelegator.getAppContext')
73        var context = abilityDelegator.getAppContext()
74        console.info('getAppContext : ' + JSON.stringify(context))
75        console.info('OpenHarmonyTestRunner onRun end')
76    }
77};