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