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
16import AbilityDelegatorRegistry from '@ohos.application.abilityDelegatorRegistry';
17
18function translateParamsToString(parameters) {
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', '-s dryRun'
23  ]);
24  let targetParams = '';
25  for (const key in parameters) {
26    if (keySet.has(key)) {
27      targetParams += ' ' + key + ' ' + parameters[key];
28    }
29  }
30  return targetParams.trim();
31}
32
33export default {
34  onPrepare() {
35    console.info('OpenHarmonyTestRunner OnPrepare');
36  },
37  onRun() {
38    console.log('OpenHarmonyTestRunner onRun run');
39    let abilityDelegatorArguments = AbilityDelegatorRegistry.getArguments();
40    let abilityDelegator = AbilityDelegatorRegistry.getAbilityDelegator();
41
42    let testAbilityName = abilityDelegatorArguments.parameters['-p'] + '.TestAbility';
43
44    let cmd = 'aa start -d 0 -a ' + testAbilityName + ' -b ' + abilityDelegatorArguments.bundleName;
45    cmd += ' ' + translateParamsToString(abilityDelegatorArguments.parameters);
46    let debug = abilityDelegatorArguments.parameters['-D'];
47    console.info('debug value : ' + debug);
48    if (debug === 'true')
49    {
50      cmd += ' -D';
51    }
52    console.info('cmd : ' + cmd);
53    abilityDelegator.executeShellCommand(cmd, (err, data) => {
54      console.info('executeShellCommand : err : ' + JSON.stringify(err));
55      console.info('executeShellCommand : data : ' + data.stdResult);
56      console.info('executeShellCommand : data : ' + data.exitCode);
57    });
58  }
59};
60