1/* 2 * Copyright (c) 2022-2023 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 {TAG} from '../../Constant'; 17import Core from '../../core.js'; 18 19export default class SysTestKit { 20 21 static delegator = null; 22 static systemTime = null; 23 24 constructor() { 25 this.id = 'sysTestKit'; 26 this.index = 0; 27 } 28 29 static getDescribeName() { 30 return Core.getInstance().getDefaultService('suite').getCurrentRunningSuite().description; 31 } 32 33 static getItName() { 34 return Core.getInstance().getDefaultService('spec').getCurrentRunningSpec().description; 35 } 36 37 static getItAttribute() { 38 return Core.getInstance().getDefaultService('spec').getCurrentRunningSpec().fi; 39 } 40 41 static actionStart(tag) { 42 console.info(`${TAG}${JSON.stringify(tag)}`); 43 var message = '\n' + 'OHOS_REPORT_ACTIONSTART: ' + JSON.stringify(tag) + '\n'; 44 SysTestKit.print(message); 45 console.info(`${TAG}${JSON.stringify(tag)} actionStart print success`); 46 } 47 48 static actionEnd(tag) { 49 console.info(`${TAG}${JSON.stringify(tag)}`); 50 var message = '\n' + 'OHOS_REPORT_ACTIONEND: ' + JSON.stringify(tag) + '\n'; 51 SysTestKit.print(message); 52 console.info(`${TAG}${JSON.stringify(tag)} actionEnd print success`); 53 } 54 55 static async existKeyword(keyword, timeout) { 56 let reg = new RegExp(/^[a-zA-Z0-9]{1,}$/); 57 if (!reg.test(keyword)) { 58 throw new Error('keyword must contain more than one string, and only letters and numbers are supported.'); 59 } 60 timeout = timeout || 4; 61 62 let searchResult = false; 63 let cmd = 'hilog -x | grep -i \'' + keyword + '\' | wc -l'; 64 await executePromise(cmd, timeout).then((data) => { 65 searchResult = data; 66 }); 67 return searchResult; 68 } 69 static async print(message) { 70 if ('printSync' in SysTestKit.delegator) { 71 console.debug(`${TAG}printSync called ...`); 72 SysTestKit.delegator.printSync(message); 73 } else { 74 await SysTestKit.delegator.print(message); 75 } 76 } 77 78 static async getRealTime() { 79 let currentTime = new Date().getTime(); 80 if (SysTestKit.systemTime !== null && SysTestKit.systemTime !== undefined) { 81 await SysTestKit.systemTime.getRealTime().then((time) => { 82 console.info(`${TAG}systemTime.getRealTime success data: ${JSON.stringify(time)}`); 83 currentTime = time; 84 }).catch((error) => { 85 console.error(`${TAG}failed to systemTime.getRealTime because ${JSON.stringify(error)}`); 86 }); 87 } 88 return currentTime; 89 } 90} 91 92function executePromise(cmd, timeout) { 93 return new Promise((resolve, reject) => { 94 SysTestKit.delegator.executeShellCommand(cmd, timeout, 95 (error, data) => { 96 console.info(`${TAG}existKeyword CallBack: err : ${JSON.stringify(error)}`); 97 console.info(`${TAG}existKeyword CallBack: data : ${JSON.stringify(data)}`); 98 resolve(parseInt(data.stdResult) > 3 ? true : false); 99 }); 100 }); 101}