1/* 2 * Copyright (c) Huawei Technologies Co., Ltd. 2021. All rights reserved. 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 hidebug from '@ohos.hidebug' 16import fs from '@ohos.file.fs' 17import process from '@ohos.process' 18import featureAbility from '@ohos.ability.featureAbility' 19 20import {describe, beforeAll, beforeEach, afterEach, afterAll, it, expect} from 'deccjsunit/index' 21 22describe("HidebugJsTest", function () { 23 beforeAll(function() { 24 /* 25 * @tc.setup: setup invoked before all testcases 26 */ 27 console.info('HidebugJsTest beforeAll called') 28 }) 29 30 afterAll(function() { 31 /* 32 * @tc.teardown: teardown invoked after all testcases 33 */ 34 console.info('HidebugJsTest afterAll called') 35 }) 36 37 beforeEach(function() { 38 /* 39 * @tc.setup: setup invoked before each testcases 40 */ 41 console.info('HidebugJsTest beforeEach called') 42 }) 43 44 afterEach(function() { 45 /* 46 * @tc.teardown: teardown invoked after each testcases 47 */ 48 console.info('HidebugJsTest afterEach called') 49 }) 50 51 async function msleep(time) { 52 let promise = new Promise((resolve, reject) => { 53 setTimeout(() => resolve("done!"), time) 54 }); 55 let result = await promise; 56 } 57 58 /** 59 * test 60 * 61 * @tc.name: HidebugJsTest_001 62 * @tc.desc: 检测cpuProfiler采集的cpuprofiler数据是否含有js napi callframe信息 63 * @tc.type: FUNC 64 * @tc.require: issueI5NXHX 65 */ 66 it('HidebugJsTest_001', 0, function () { 67 console.info("---------------------------HidebugJsTest_001----------------------------------"); 68 try { 69 let timestamp = Date.now(); 70 let filename = "cpuprofiler_" + timestamp.toString(); 71 hidebug.startProfiling(filename); 72 for (let i = 0; i < 3; i++) { 73 hidebug.getSharedDirty(); 74 } 75 hidebug.stopProfiling(); 76 let path = "/proc/self/root/data/storage/el2/base/files/" + filename + ".json"; 77 let data = fs.readTextSync(path); 78 if (data.includes("napi")) { 79 expect(true).assertTrue(); 80 } else { 81 expect(false).assertTrue(); 82 } 83 } catch (err) { 84 console.error('HidebugJsTest_001 has failed for ' + err); 85 expect(false).assertTrue(); 86 } 87 }) 88 89 /** 90 * @tc.name: HidebugJsTest_002 91 * @tc.desc: startJsCpuProfiling/stopJsCpuProfiling的正常测试, startProfiling/stopProfiling的更新版本 92 * @tc.type: FUNC 93 * @tc.require: issueI5VY8L 94 */ 95 it('HidebugJsTest_002', 0, function () { 96 console.info("---------------------------HidebugJsTest_002----------------------------------"); 97 try { 98 let timestamp = Date.now(); 99 let filename = "cpuprofiler_" + timestamp.toString(); 100 hidebug.startJsCpuProfiling(filename); 101 for (let i = 0; i < 3; i++) { 102 hidebug.getSharedDirty(); 103 } 104 hidebug.stopJsCpuProfiling(); 105 let path = "/proc/self/root/data/storage/el2/base/files/" + filename + ".json"; 106 let data = fs.readTextSync(path); 107 if (data.includes("napi")) { 108 expect(true).assertTrue(); 109 } else { 110 expect(false).assertTrue(); 111 } 112 } catch (err) { 113 console.error('HidebugJsTest_002 has failed for ' + err); 114 expect(false).assertTrue(); 115 } 116 }) 117 118 /** 119 * @tc.name: HidebugJsTest_003 120 * @tc.desc: startJsCpuProfiling/stopJsCpuProfiling的异常测试, startProfiling/stopProfiling的更新版本 121 * @tc.type: FUNC 122 * @tc.require: issueI5VY8L 123 */ 124 it('HidebugJsTest_003', 0, function () { 125 console.info("---------------------------HidebugJsTest_003----------------------------------"); 126 try { 127 hidebug.startJsCpuProfiling(); 128 for (let i = 0; i < 3; i++) { 129 hidebug.getSharedDirty(); 130 } 131 hidebug.stopJsCpuProfiling(); 132 } catch (error) { 133 console.info(error.code); 134 console.info(error.message); 135 expect(error.code === "401").assertTrue(); 136 } 137 }) 138 139 /** 140 * @tc.name: HidebugJsTest_004 141 * @tc.desc: dumpJsHeapData的正常测试, dumpHeapData的更新版本 142 * @tc.type: FUNC 143 * @tc.require: issueI5VY8L 144 */ 145 it('HidebugJsTest_004', 0, function () { 146 console.info("---------------------------HidebugJsTest_004----------------------------------"); 147 try { 148 hidebug.dumpJsHeapData("heapData"); 149 expect(true).assertTrue(); 150 } catch (error) { 151 console.info(error.code); 152 console.info(error.message); 153 } 154 }) 155 156 /** 157 * @tc.name: HidebugJsTest_005 158 * @tc.desc: dumpJsHeapData的异常测试, dumpHeapData的更新版本 159 * @tc.type: FUNC 160 * @tc.require: issueI5VY8L 161 */ 162 it('HidebugJsTest_005', 0, function () { 163 console.info("---------------------------HidebugJsTest_005----------------------------------"); 164 try { 165 hidebug.dumpJsHeapData(); 166 } catch (error) { 167 console.info(error.code); 168 console.info(error.message); 169 expect(error.code === "401").assertTrue(); 170 } 171 }) 172 173 /** 174 * @tc.name: HidebugJsTest_006 175 * @tc.desc: getServiceDump的正常测试 176 * @tc.type: FUNC 177 * @tc.require: issueI5VY8L 178 */ 179 it('HidebugJsTest_006', 0, function () { 180 console.info("---------------------------HidebugJsTest_006----------------------------------"); 181 let context = featureAbility.getContext(); 182 context.getFilesDir().then((data) => { 183 const path = data + "/serviceInfo1.txt"; 184 console.info("output path: " + path); 185 let file = fs.openSync(path, fs.OpenMode.READ_WRITE | fs.OpenMode.CREATE); 186 const serviceId = 10; 187 const args = new Array("allInfo"); 188 try { 189 hidebug.getServiceDump(serviceId, file.fd, args); 190 expect(true).assertTrue(); 191 } catch (error) { 192 console.info(error.code); 193 console.info(error.message); 194 } 195 fs.closeSync(file); 196 }) 197 }) 198 199 /** 200 * @tc.name: HidebugJsTest_007 201 * @tc.desc: getServiceDump的异常测试,参数错误 202 * @tc.type: FUNC 203 * @tc.require: issueI5VY8L 204 */ 205 it('HidebugJsTest_007', 0, function () { 206 console.info("---------------------------HidebugJsTest_007----------------------------------"); 207 let context = featureAbility.getContext(); 208 context.getFilesDir().then((data) => { 209 const path = data + "/serviceInfo2.txt"; 210 console.info("output path: " + path); 211 let file = fs.openSync(path, fs.OpenMode.READ_WRITE | fs.OpenMode.CREATE); 212 const serviceId = 10; 213 const args = new Array("allInfo"); 214 try { 215 hidebug.getServiceDump(serviceId); 216 } catch (error) { 217 console.info(error.code); 218 console.info(error.message); 219 expect(error.code === "401").assertTrue(); 220 } 221 fs.closeSync(file); 222 }) 223 }) 224 225 /** 226 * @tc.name: HidebugJsTest_008 227 * @tc.desc: getServiceDump的异常测试,查询system ability失败 228 * @tc.type: FUNC 229 * @tc.require: issueI5VY8L 230 */ 231 it('HidebugJsTest_008', 0, function () { 232 console.info("---------------------------HidebugJsTest_008----------------------------------"); 233 let context = featureAbility.getContext(); 234 context.getFilesDir().then((data) => { 235 const path = data + "/serviceInfo3.txt"; 236 console.info("output path: " + path); 237 let file = fs.openSync(path, fs.OpenMode.READ_WRITE | fs.OpenMode.CREATE); 238 const serviceId = -10; 239 const args = new Array("allInfo"); 240 try { 241 hidebug.getServiceDump(serviceId, file.fd, args); 242 } catch (error) { 243 console.info(error.code); 244 console.info(error.message); 245 expect(error.code === "11400101").assertTrue(); 246 } 247 fs.closeSync(file); 248 }) 249 }) 250 251 /** 252 * @tc.name: HidebugJsTest_009 253 * @tc.desc: getAppNativeMemInfo的正常测试, getVss()/getPss()/getSharedDirty()/getPrivateDirty()的更新版本 254 * @tc.type: FUNC 255 * @tc.require: issueI5VY8L 256 */ 257 it('HidebugJsTest_009', 0, function () { 258 console.info("---------------------------HidebugJsTest_009----------------------------------"); 259 try { 260 let nativeMemInfo = hidebug.getAppNativeMemInfo(); 261 expect(nativeMemInfo.pss >= 0).assertTrue(); 262 expect(nativeMemInfo.vss >= 0).assertTrue(); 263 expect(nativeMemInfo.rss >= 0).assertTrue(); 264 expect(nativeMemInfo.sharedDirty >= 0).assertTrue(); 265 expect(nativeMemInfo.privateDirty >= 0).assertTrue(); 266 expect(nativeMemInfo.sharedClean >= 0).assertTrue(); 267 expect(nativeMemInfo.privateClean >= 0).assertTrue(); 268 } catch (error) { 269 console.info(error.code); 270 console.info(error.message); 271 expect(false).assertTrue(); 272 } 273 }) 274 275 /** 276 * @tc.name: HidebugJsTest_010 277 * @tc.desc: getSystemMemInfo()的正常测试 278 * @tc.type: FUNC 279 * @tc.require: issueI5VY8L 280 */ 281 it('HidebugJsTest_010', 0, function () { 282 console.info("---------------------------HidebugJsTest_010----------------------------------"); 283 try { 284 let systemMemInfo = hidebug.getSystemMemInfo(); 285 expect(systemMemInfo.totalMem >= 0).assertTrue(); 286 expect(systemMemInfo.freeMem >= 0).assertTrue(); 287 expect(systemMemInfo.availableMem >= 0).assertTrue(); 288 } catch (error) { 289 console.info(error.code); 290 console.info(error.message); 291 expect(false).assertTrue(); 292 } 293 }) 294 295 /** 296 * @tc.name: HidebugJsTest_011 297 * @tc.desc: getSystemCpuUsage的正常测试,查询system cpu usage 298 * @tc.type: FUNC 299 * @tc.require: issueI90Z36 300 */ 301 it('HidebugJsTest_011', 0, function () { 302 console.info("---------------------------HidebugJsTest_011----------------------------------"); 303 try { 304 let sysCpuUsage = hidebug.getSystemCpuUsage(); 305 expect(sysCpuUsage >= 0 && sysCpuUsage <= 1).assertTrue(); 306 } catch (error) { 307 console.info(error.code); 308 console.info(error.message); 309 expect(false).assertTrue(); 310 } 311 }) 312 313 /** 314 * @tc.name: HidebugJsTest_012 315 * @tc.desc: getAppMemoryLimit正常测试 316 * @tc.type: FUNC 317 * @tc.require: issueI8ZX7S 318 */ 319 it('HidebugJsTest_012', 0, function () { 320 console.info("---------------------------HidebugJsTest_012----------------------------------"); 321 try { 322 let temp = hidebug.getAppMemoryLimit(); 323 expect(temp.rssLimit >= BigInt(0)).assertTrue(); 324 expect(temp.vssLimit >= BigInt(0)).assertTrue(); 325 expect(temp.vmHeapLimit >= BigInt(0)).assertTrue(); 326 } catch (error) { 327 expect().assertFail(); 328 } 329 }) 330 331 /** 332 * @tc.name: HidebugJsTest_013 333 * @tc.desc: getAppVMMemoryInfo正常测试 334 * @tc.type: FUNC 335 * @tc.require: issueI5VY8L 336 */ 337 it('HidebugJsTest_013', 0, function () { 338 console.info("---------------------------HidebugJsTest_013----------------------------------"); 339 try { 340 let result = hidebug.getAppVMMemoryInfo(); 341 expect(result.allArraySize >= 0 && result.totalHeap >= 0 && result.heapUsed >= 0).assertTrue(); 342 } catch (error) { 343 console.info(error.code); 344 console.info(error.message); 345 expect(false).assertTrue(); 346 } 347 }) 348 349 /** 350 * @tc.name: HidebugJsTest_014 351 * @tc.desc: getAppThreadCpuUsage正常测试 352 * @tc.type: FUNC 353 * @tc.require: issueI5VY8L 354 */ 355 it('HidebugJsTest_014', 0, function () { 356 console.info("---------------------------HidebugJsTest_014----------------------------------"); 357 try { 358 let appThreadCpuUsage = hidebug.getAppThreadCpuUsage(); 359 expect(appThreadCpuUsage.length >= 0).assertTrue(); 360 } catch (error) { 361 console.info(error.code); 362 console.info(error.message); 363 expect(false).assertTrue(); 364 } 365 }) 366 367 /** 368 * @tc.name: HidebugJsTest_015 369 * @tc.desc: StartAppTraceCapture正常测试 370 * @tc.type: FUNC 371 * @tc.require: issueI5VY8L 372 */ 373 it('HidebugJsTest_015', 0, function () { 374 console.info("---------------------------HidebugJsTest_015----------------------------------"); 375 try { 376 let tags = [hidebug.tags.ABILITY_MANAGER]; 377 let flag = hidebug.TraceFlag.MAIN_THREAD; 378 let limitSize = 1024 * 1024; 379 let fileName = hidebug.startAppTraceCapture(tags, flag, limitSize); 380 for (let i = 0; i < 3; i++) { 381 hidebug.getSharedDirty(); 382 } 383 hidebug.stopAppTraceCapture(); 384 expect(fileName.length > 0).assertTrue(); 385 } catch (error) { 386 console.info(error.code); 387 console.info(error.message); 388 expect(false).assertTrue(); 389 } 390 }) 391 392}) 393