10704ebd2Sopenharmony_ci/*
20704ebd2Sopenharmony_ci * Copyright (c) 2022 Huawei Device Co., Ltd.
30704ebd2Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
40704ebd2Sopenharmony_ci * you may not use this file except in compliance with the License.
50704ebd2Sopenharmony_ci * You may obtain a copy of the License at
60704ebd2Sopenharmony_ci *
70704ebd2Sopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
80704ebd2Sopenharmony_ci *
90704ebd2Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
100704ebd2Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
110704ebd2Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
120704ebd2Sopenharmony_ci * See the License for the specific language governing permissions and
130704ebd2Sopenharmony_ci * limitations under the License.
140704ebd2Sopenharmony_ci */
150704ebd2Sopenharmony_ci
160704ebd2Sopenharmony_ciimport AbilityDelegatorRegistry from '@ohos.application.abilityDelegatorRegistry'
170704ebd2Sopenharmony_ci
180704ebd2Sopenharmony_cifunction translateParamsToString(parameters) {
190704ebd2Sopenharmony_ci    const keySet = new Set([
200704ebd2Sopenharmony_ci        '-s class', '-s notClass', '-s suite', '-s itName',
210704ebd2Sopenharmony_ci        '-s level', '-s testType', '-s size', '-s timeout',
220704ebd2Sopenharmony_ci        '-s package', '-s dryRun'
230704ebd2Sopenharmony_ci    ])
240704ebd2Sopenharmony_ci    let targetParams = '';
250704ebd2Sopenharmony_ci    for (const key in parameters) {
260704ebd2Sopenharmony_ci        if (keySet.has(key)) {
270704ebd2Sopenharmony_ci            targetParams += ' ' + key + ' ' + parameters[key]
280704ebd2Sopenharmony_ci        }
290704ebd2Sopenharmony_ci    }
300704ebd2Sopenharmony_ci    return targetParams.trim()
310704ebd2Sopenharmony_ci}
320704ebd2Sopenharmony_ci
330704ebd2Sopenharmony_ci export default {
340704ebd2Sopenharmony_ci    onPrepare() {
350704ebd2Sopenharmony_ci        console.info('OpenHarmonyTestRunner OnPrepare')
360704ebd2Sopenharmony_ci    },
370704ebd2Sopenharmony_ci    onRun() {
380704ebd2Sopenharmony_ci        console.log('OpenHarmonyTestRunner onRun run')
390704ebd2Sopenharmony_ci        var abilityDelegatorArguments = AbilityDelegatorRegistry.getArguments()
400704ebd2Sopenharmony_ci        var abilityDelegator = AbilityDelegatorRegistry.getAbilityDelegator()
410704ebd2Sopenharmony_ci
420704ebd2Sopenharmony_ci        var testAbilityName = abilityDelegatorArguments.parameters['-p'] + '.TestAbility'
430704ebd2Sopenharmony_ci
440704ebd2Sopenharmony_ci        var cmd = 'aa start -d 0 -a ' + testAbilityName + ' -b ' + abilityDelegatorArguments.bundleName
450704ebd2Sopenharmony_ci        cmd += ' ' + translateParamsToString(abilityDelegatorArguments.parameters)
460704ebd2Sopenharmony_ci        var debug = abilityDelegatorArguments.parameters["-D"]
470704ebd2Sopenharmony_ci        console.info('debug value : '+debug)
480704ebd2Sopenharmony_ci        if (debug == 'true')
490704ebd2Sopenharmony_ci        {
500704ebd2Sopenharmony_ci            cmd += ' -D'
510704ebd2Sopenharmony_ci        }
520704ebd2Sopenharmony_ci        console.info('cmd : '+cmd)
530704ebd2Sopenharmony_ci        abilityDelegator.executeShellCommand(cmd, (err, data) => {
540704ebd2Sopenharmony_ci            console.info('executeShellCommand : err : ' + JSON.stringify(err));
550704ebd2Sopenharmony_ci            console.info('executeShellCommand : data : ' + data.stdResult);
560704ebd2Sopenharmony_ci            console.info('executeShellCommand : data : ' + data.exitCode);
570704ebd2Sopenharmony_ci        })
580704ebd2Sopenharmony_ci    }
590704ebd2Sopenharmony_ci};
60