• 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 */
15
16/*
17 * Copyright (C) 2022 Huawei Device Co., Ltd.
18 * Licensed under the MIT License, Version 2.0 (the 'License');
19 * you may not use this file except in compliance with the License.
20 * You may obtain a copy of the License at
21 *
22 *     https://mit-license.org/
23 *
24 * Unless required by applicable law or agreed to in writing, software
25 * distributed under the License is distributed on an 'AS IS' BASIS,
26 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
27 * See the License for the specific language governing permissions and
28 * limitations under the License.
29 */
30
31import TestRunner from '@ohos.application.testRunner'
32import AbilityDelegatorRegistry from '@ohos.application.abilityDelegatorRegistry'
33
34var abilityDelegator = undefined
35var abilityDelegatorArguments = undefined
36
37function translateParamsToString(parameters) {
38  const keySet = new Set([
39    '-s class', '-s notClass', '-s suite', '-s itName',
40    '-s level', '-s testType', '-s size', '-s timeout',
41    '-s package'
42  ])
43  let targetParams = '';
44  for (const key in parameters) {
45    if (keySet.has(key)) {
46      targetParams += ' ' + key + ' ' + parameters[key]
47    }
48  }
49  return targetParams.trim()
50}
51
52async function onAbilityCreateCallback() {
53  console.log('onAbilityCreateCallback');
54}
55
56async function addAbilityMonitorCallback(err: any) {
57  console.info('addAbilityMonitorCallback : ' + JSON.stringify(err))
58}
59
60export default class OpenHarmonyTestRunner implements TestRunner {
61  constructor() {
62  }
63
64  onPrepare() {
65    console.info('OpenHarmonyTestRunner OnPrepare')
66  }
67
68  onRun() {
69    console.log('OpenHarmonyTestRunner onRun run')
70    abilityDelegatorArguments = AbilityDelegatorRegistry.getArguments()
71    abilityDelegator = AbilityDelegatorRegistry.getAbilityDelegator()
72
73    let lMonitor = {
74      abilityName: testAbilityName,
75      onAbilityCreate: onAbilityCreateCallback,
76    };
77    var testAbilityName = abilityDelegatorArguments.parameters['-p'] + '.TestAbility'
78    abilityDelegator.addAbilityMonitor(lMonitor, addAbilityMonitorCallback)
79    var cmd = 'aa start -d 0 -a ' + testAbilityName + ' -b ' + abilityDelegatorArguments.bundleName
80    cmd += ' ' + translateParamsToString(abilityDelegatorArguments.parameters)
81    console.info('cmd : ' + cmd)
82    var debug = abilityDelegatorArguments.parameters["-D"]
83    if (debug == 'true') {
84      cmd += ' -D'
85    }
86    console.info('cmd : ' + cmd)
87    abilityDelegator.executeShellCommand(cmd,
88      (err: any, d: any) => {
89        console.info('executeShellCommand : err : ' + JSON.stringify(err));
90        console.info('executeShellCommand : data : ' + d.stdResult);
91        console.info('executeShellCommand : data : ' + d.exitCode);
92      })
93    console.info('OpenHarmonyTestRunner onRun call abilityDelegator.getAppContext')
94    var context = abilityDelegator.getAppContext()
95    console.info('getAppContext : ' + JSON.stringify(context))
96    console.info('OpenHarmonyTestRunner onRun end')
97  }
98};
99