1/* 2 * Copyright (c) 2025 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 systemDateTime from '@ohos.systemDateTime'; 16import abilityDelegatorRegistry from '@ohos.app.ability.abilityDelegatorRegistry'; 17import { BusinessError } from '@ohos.base'; 18import { TAG } from '../../Constant'; 19import { Core } from '../../core'; 20import { SuiteService } from '../service/SuiteService'; 21import { SpecService } from '../service/SpecService'; 22 23class SysTestKit { 24 public id: string; 25 public index: short; 26 public static delegator: abilityDelegatorRegistry.AbilityDelegator | null = null; 27 public static systemTime: number; 28 // public static workerPort: worker.ThreadWorker; 29 constructor() { 30 this.id = 'sysTestKit'; 31 this.index = 0; 32 } 33 34 static getDescribeName(): string { 35 if (Core.getInstance()) { 36 const core = Core.getInstance() as Core; //Core.getInstance()可能返回一个uniontype 37 const suiteService = core.getDefaultService('suite'); 38 if (suiteService !== null) { 39 if (suiteService instanceof SuiteService) { 40 const deaultSuite = suiteService as SuiteService; 41 if (deaultSuite.getCurrentRunningSuite()) { 42 return deaultSuite.getCurrentRunningSuite().description; 43 } 44 } 45 } 46 } 47 return ''; 48 } 49 50 static getItName(): string { 51 if (Core.getInstance()) { 52 const core = Core.getInstance() as Core; //Core.getInstance()可能返回一个uniontype 53 const specService = core.getDefaultService('spec'); 54 if (specService !== null) { 55 if (specService instanceof SpecService) { 56 const deaultSpec = specService as SpecService; 57 const curSpec = deaultSpec.getCurrentRunningSpec(); 58 if (curSpec) { 59 return curSpec.description; 60 } 61 } 62 } 63 } 64 return ''; 65 } 66 67 static getItAttribute(): long { 68 if (Core.getInstance()) { 69 const core = Core.getInstance() as Core; //Core.getInstance()可能返回一个uniontype 70 const specService = core.getDefaultService('spec'); 71 if (specService !== null) { 72 if (specService instanceof SpecService) { 73 const deaultSpec = specService as SpecService; 74 const curSpec = deaultSpec.getCurrentRunningSpec(); 75 if (curSpec) { 76 return curSpec.fi; 77 } 78 } 79 } 80 } 81 return 0; 82 } 83 84 static actionStart(tag: string): void { 85 console.info(`${TAG}${JSON.stringify(tag)}`); 86 const message = '\n' + 'OHOS_REPORT_ACTIONSTART: ' + JSON.stringify(tag) + '\n'; 87 SysTestKit.print(message); 88 console.info(`${TAG}${JSON.stringify(tag)} actionStart print success`); 89 } 90 91 static actionEnd(tag: string): void { 92 console.info(`${TAG}${JSON.stringify(tag)}`); 93 const message = '\n' + 'OHOS_REPORT_ACTIONEND: ' + JSON.stringify(tag) + '\n'; 94 SysTestKit.print(message); 95 console.info(`${TAG}${JSON.stringify(tag)} actionEnd print success`); 96 } 97 static async print(message: string) { 98 const delegator = SysTestKit.delegator; 99 if (delegator === null) { 100 console.info(`delegator is null`); 101 } else { 102 delegator.printSync(message); 103 } 104 } 105 106 static getRealTime(): number { 107 let current = new Date().getTime(); 108 return current; 109 } 110 111 static async existKeyword(keyword: string, timeout?: number): Promise<boolean> { 112 let reg = new RegExp('^[a-zA-Z0-9]{1,}$'); 113 if (!reg.test(keyword)) { 114 throw new Error('keyword must contain more than one string, and only letters and numbers are supported.'); 115 } 116 let time = 4.0; 117 if (timeout !== undefined) { 118 time = timeout; 119 } 120 121 let searchResult = false; 122 let cmd = "hilog -x | grep -i '" + keyword + "' | wc -l"; 123 try { 124 const data = await executePromise(cmd, time); 125 searchResult = data; 126 } catch (err: Error) { 127 throw err; 128 } 129 return searchResult; 130 } 131} 132 133function executePromise(cmd: string, timeout: number) { 134 return new Promise<boolean>((resolve: (value: boolean) => void, reject: (value: Error) => void) => { 135 const delegator = SysTestKit.delegator; 136 if (!delegator) { 137 reject(new Error('delegator is null')); 138 } 139 (delegator as abilityDelegatorRegistry.AbilityDelegator).executeShellCommand( 140 cmd, 141 timeout, 142 (error: BusinessError | null, data: abilityDelegatorRegistry.ShellCmdResult | undefined) => { 143 if (error) { 144 const e = error as BusinessError 145 reject(new Error('executeShellCommand Error: ' + e.message)); 146 console.info(`${TAG}existKeyword CallBack: err : ${e.message}`); 147 } else if(data) { 148 const result = data as abilityDelegatorRegistry.ShellCmdResult; 149 console.info(`${TAG}existKeyword CallBack: data : ${JSON.stringify(result)}`); 150 resolve(parseInt(result.stdResult) > 3 ? true : false); 151 } 152 } 153 ); 154 }); 155} 156 157export { SysTestKit }; 158