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