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