1/* 2 * Copyright (C) 2021 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 hiSysEvent from '@ohos.hiSysEvent' 16import {describe, beforeAll, beforeEach, afterEach, afterAll, it, expect} from '@ohos/hypium' 17 18export default function hiSysEventJsTest() { 19describe('hiSysEventJsTest', function () { 20 beforeAll(function() { 21 console.info('hiSysEventJsTest beforeAll called') 22 }) 23 24 afterAll(function() { 25 console.info('hiSysEventJsTest afterAll called') 26 }) 27 28 beforeEach(function() { 29 console.info('hiSysEventJsTest beforeEach called') 30 }) 31 32 afterEach(function() { 33 console.info('hiSysEventJsTest afterEach called') 34 }) 35 36 /** 37 * @tc.number DFX_DFT_HiSysEvent_JS_0100 38 * @tc.name testHiSysEventApi01 39 * @tc.desc 添加Js打点事件-callback方式进行回调处理 40 */ 41 it('testHiSysEventApi01', 1, async function (done) { 42 console.info('testHiSysEventApi01 start') 43 hiSysEvent.write({ 44 domain: "RELIABILITY", 45 name: "STACK", 46 eventType: hiSysEvent.EventType.FAULT, 47 },(err, value) => { 48 console.log('HiSysEvent into json-callback'); 49 if (err) { 50 console.error(`HiSysEvent json-callback-error code=${err.code}`); 51 expect(err.code == 0).assertFail(); 52 done(); 53 } else { 54 console.log(`HiSysEvent json-callback-success value=${value}`); 55 expect(value == 0).assertTrue(); 56 done(); 57 } 58 }); 59 console.info('testHiSysEventApi01 end') 60 }) 61 62 /** 63 * @tc.number DFX_DFT_HiSysEvent_JS_0200 64 * @tc.name testHiSysEventApi02 65 * @tc.desc 添加Js打点事件-Promise方式中then()方法进行打点成功回调处理 66 */ 67 it('testHiSysEventApi02', 1, async function (done) { 68 console.info('testHiSysEventApi02 start') 69 hiSysEvent.write({ 70 domain: "RELIABILITY", 71 name: "STACK", 72 eventType: hiSysEvent.EventType.FAULT, 73 }).then( 74 (value) => { 75 console.log(`HiSysEvent json-callback-success value=${value}`); 76 expect(value == 0).assertTrue(); 77 done(); 78 } 79 ).catch( 80 (err) => { 81 console.error(`HiSysEvent json-callback-error code=${err.code}`); 82 expect(err.code == 0).assertFail(); 83 } 84 ) 85 console.info('testHiSysEventApi02 end') 86 }) 87 88 /** 89 * @tc.number DFX_DFT_HiSysEvent_JS_0300 90 * @tc.name testHiSysEventApi03 91 * @tc.desc 验证调用HiSysEvent write接口,打点fault类型事件,支持基础数据类型自定义传参 92 */ 93 it('testHiSysEventApi03', 1, async function (done) { 94 console.info('testHiSysEventApi03 start') 95 try { 96 hiSysEvent.write({ 97 domain: "RELIABILITY", 98 name: "STACK", 99 eventType: hiSysEvent.EventType.FAULT, 100 params: { 101 MAX: Number.MAX_VALUE, 102 MIN: Number.MIN_VALUE, 103 INT: 100, 104 DOU: 30949.374, 105 PACKAGE_NAME: "com.ohos.hisysevent.test", 106 PROCESS_NAME: "syseventservice_HiSysEventApi03", 107 BOOL: true, 108 UNF: undefined, 109 NUL: null, 110 MSG: [123, 3.14, 0.123] 111 } 112 },(err, value) => { 113 if (err) { 114 console.error(`testHiSysEventApi03 json-callback-error code=${err.code}`); 115 expect().assertFail(); 116 done(); 117 } else { 118 console.log(`testHiSysEventApi03 json-callback-success value=${value}`); 119 expect(value == 0).assertTrue(); 120 console.info('testHiSysEventApi03 end') 121 done(); 122 } 123 }) 124 } catch (error) { 125 console.error(`testHiSysEventApi03 error code: ${error.code}, error msg: ${error.message}`); 126 expect(false).assertTrue() 127 console.info('testHiSysEventApi03 error') 128 done() 129 } 130 }) 131 132 /** 133 * @tc.number DFX_DFT_HiSysEvent_JS_0400 134 * @tc.name testHiSysEventApi04 135 * @tc.desc 验证调用HiSysEvent write接口,打点statistic类型事件,支持基础数据类型自定义传参 136 */ 137 it('testHiSysEventApi04', 3, async function (done) { 138 console.info('testHiSysEventApi04 start') 139 try { 140 hiSysEvent.write({ 141 domain: "HIVIEWDFX", 142 name: "PLUGIN_STATS", 143 eventType: hiSysEvent.EventType.STATISTIC, 144 params: { 145 MAX: Number.MAX_VALUE, 146 MIN: Number.MIN_VALUE, 147 INT: 100, 148 DOU: 30949.374, 149 PACKAGE_NAME: "com.ohos.hisysevent.test", 150 PROCESS_NAME: "syseventservice_HiSysEventApi04", 151 BOOL: true, 152 UNF: undefined, 153 NUL: null, 154 MSG: [123, 3.14, 0.123] 155 } 156 },(err, value) => { 157 if (err) { 158 console.error(`testHiSysEventApi04 json-callback-error code=${err.code}`); 159 expect().assertFail(); 160 done(); 161 } else { 162 console.log(`testHiSysEventApi04 json-callback-success value=${value}`); 163 expect(value == 0).assertTrue(); 164 console.info('testHiSysEventApi04 end') 165 done(); 166 } 167 }) 168 } catch (error) { 169 console.error(`testHiSysEventApi04 error code: ${error.code}, error msg: ${error.message}`); 170 expect(false).assertTrue() 171 console.info('testHiSysEventApi04 error') 172 done() 173 } 174 }) 175 176 /** 177 * @tc.number DFX_DFT_HiSysEvent_JS_0500 178 * @tc.name testHiSysEventApi05 179 * @tc.desc 验证调用HiSysEvent write接口,打点security类型事件,支持基础数据类型自定义传参 180 */ 181 it('testHiSysEventApi05', 3, async function (done) { 182 console.info('testHiSysEventApi05 start') 183 try { 184 hiSysEvent.write({ 185 domain: "ACCOUNT", 186 name: "PERMISSION_EXCEPTION", 187 eventType: hiSysEvent.EventType.SECURITY, 188 params: { 189 MAX: Number.MAX_VALUE, 190 MIN: Number.MIN_VALUE, 191 INT: 100, 192 DOU: 30949.374, 193 PACKAGE_NAME: "com.ohos.hisysevent.test", 194 PROCESS_NAME: "syseventservice_HiSysEventApi05", 195 BOOL: true, 196 UNF: undefined, 197 NUL: null, 198 MSG: [123, 3.14, 0.123] 199 } 200 },(err, value) => { 201 if (err) { 202 console.error(`testHiSysEventApi05 json-callback-error code=${err.code}`); 203 expect().assertFail(); 204 done(); 205 } else { 206 console.log(`testHiSysEventApi05 json-callback-success value=${value}`); 207 expect(value == 0).assertTrue(); 208 console.info('testHiSysEventApi05 end') 209 done(); 210 } 211 }) 212 } catch (error) { 213 console.error(`testHiSysEventApi05 error code: ${error.code}, error msg: ${error.message}`); 214 expect(false).assertTrue() 215 console.info('testHiSysEventApi05 error') 216 done() 217 } 218 }) 219 220 /** 221 * @tc.number DFX_DFT_HiSysEvent_JS_0600 222 * @tc.name testHiSysEventApi06 223 * @tc.desc 验证调用HiSysEvent write接口,打点behavior类型事件,支持基础数据类型自定义传参 224 */ 225 it('testHiSysEventApi06', 3, async function (done) { 226 console.info('testHiSysEventApi06 start') 227 try { 228 hiSysEvent.write({ 229 domain: "HIVIEWDFX", 230 name: "PLUGIN_LOAD", 231 eventType: hiSysEvent.EventType.BEHAVIOR, 232 params: { 233 MAX: Number.MAX_VALUE, 234 MIN: Number.MIN_VALUE, 235 INT: 100, 236 DOU: 30949.374, 237 PACKAGE_NAME: "com.ohos.hisysevent.test", 238 PROCESS_NAME: "syseventservice_HiSysEventApi06", 239 BOOL: true, 240 UNF: undefined, 241 NUL: null, 242 MSG: [123, 3.14, 0.123] 243 } 244 },(err, value) => { 245 if (err) { 246 console.error(`testHiSysEventApi06 json-callback-error code=${err.code}`); 247 expect().assertFail(); 248 done(); 249 } else { 250 console.log(`testHiSysEventApi06 json-callback-success value=${value}`); 251 expect(value == 0).assertTrue(); 252 console.info('testHiSysEventApi06 end') 253 done(); 254 } 255 }) 256 } catch (error) { 257 console.error(`testHiSysEventApi06 error code: ${error.code}, error msg: ${error.message}`); 258 expect(false).assertTrue() 259 console.info('testHiSysEventApi06 error') 260 done() 261 } 262 }) 263 264 /** 265 * @tc.number DFX_DFT_HiSysEvent_JS_0700 266 * @tc.name testHiSysEventApi07 267 * @tc.desc 验证调用HiSysEvent write接口,支持自定义传参最大字符串10k 268 */ 269 it('testHiSysEventApi07', 3, async function (done) { 270 console.info('testHiSysEventApi07 start') 271 try { 272 hiSysEvent.write({ 273 domain: "HIVIEWDFX", 274 name: "PLUGIN_LOAD", 275 eventType: hiSysEvent.EventType.BEHAVIOR, 276 params: { 277 PROCESS_NAME: Array.from({length: 10 * 1024 + 1}).join("a"), 278 } 279 },(err, value) => { 280 if (err) { 281 console.error(`testHiSysEventApi07 json-callback-error code=${err.code}`); 282 expect().assertFail(); 283 done(); 284 } else { 285 console.log(`testHiSysEventApi07 json-callback-success value=${value}`); 286 expect(value == 0).assertTrue(); 287 console.info('testHiSysEventApi07 end') 288 done(); 289 } 290 }) 291 } catch (error) { 292 console.error(`testHiSysEventApi07 error code: ${error.code}, error msg: ${error.message}`); 293 expect(false).assertTrue() 294 console.info('testHiSysEventApi07 error') 295 done() 296 } 297 }) 298 299 /** 300 * @tc.number DFX_DFT_HiSysEvent_JS_0800 301 * @tc.name testHiSysEventApi08 302 * @tc.desc 验证调用HiSysEvent write接口,支持自定义传参最多参数个数128个 303 */ 304 it('testHiSysEventApi08', 3, async function (done) { 305 console.info('testHiSysEventApi08 start') 306 let largeParams = {} 307 for (let i = 0; i < 128; i++) { 308 largeParams["name" + i] = i 309 } 310 try { 311 hiSysEvent.write({ 312 domain: "HIVIEWDFX", 313 name: "PLUGIN_LOAD", 314 eventType: hiSysEvent.EventType.BEHAVIOR, 315 params: largeParams 316 },(err, value) => { 317 if (err) { 318 console.error(`testHiSysEventApi08 json-callback-error code=${err.code}`); 319 expect().assertFail(); 320 done(); 321 } else { 322 console.log(`testHiSysEventApi08 json-callback-success value=${value}`); 323 expect(value == 0).assertTrue(); 324 console.info('testHiSysEventApi08 end') 325 done(); 326 } 327 }) 328 } catch (error) { 329 console.error(`testHiSysEventApi08 error code: ${error.code}, error msg: ${error.message}`); 330 expect(false).assertTrue() 331 console.info('testHiSysEventApi08 error') 332 done() 333 } 334 }) 335 336 /** 337 * @tc.number DFX_DFT_HiSysEvent_JS_0900 338 * @tc.name testHiSysEventApi09 339 * @tc.desc 验证调用HiSysEvent write接口,支持自定义传参最长数组个数100个 340 */ 341 it('testHiSysEventApi09', 3, async function (done) { 342 console.info('testHiSysEventApi09 start') 343 let msgArray = [] 344 for (let i = 0; i < 100; i++) { 345 msgArray[i] = i 346 } 347 try { 348 hiSysEvent.write({ 349 domain: "HIVIEWDFX", 350 name: "PLUGIN_LOAD", 351 eventType: hiSysEvent.EventType.BEHAVIOR, 352 params: { 353 MSG: msgArray 354 } 355 },(err, value) => { 356 if (err) { 357 console.error(`testHiSysEventApi09 json-callback-error code=${err.code}`); 358 expect().assertFail(); 359 done(); 360 } else { 361 console.log(`testHiSysEventApi09 json-callback-success value=${value}`); 362 expect(value == 0).assertTrue(); 363 console.info('testHiSysEventApi09 end') 364 done(); 365 } 366 }) 367 } catch (error) { 368 console.error(`testHiSysEventApi09 error code: ${error.code}, error msg: ${error.message}`); 369 expect(false).assertTrue() 370 console.info('testHiSysEventApi09 error') 371 done() 372 } 373 }) 374 375 /** 376 * @tc.number DFX_DFT_HiSysEvent_JS_1000 377 * @tc.name testHiSysEventApi10 378 * @tc.desc 验证可成功调用addwatcher接口,订阅规则domain+eventName+tag, rule=PREFIX 379 */ 380 it('testHiSysEventApi10', 3, async function (done) { 381 console.info('testHiSysEventApi10 start') 382 let watcher110 383 watcher110 = { 384 rules: [{ 385 domain: "RELIABILI", 386 name: "STA", 387 tag: "STABILI", 388 ruleType: hiSysEvent.RuleType.PREFIX 389 }], 390 onEvent: (info) => { 391 console.info(`testHiSysEventApi10: OnEvent...`) 392 expect(Object.keys(info).length > 0).assertTrue() 393 console.info(`testHiSysEventApi10: domain is ${info.domain}, name is ${info.name}, 394 eventType is ${info.eventType}`) 395 if (info.params instanceof Object) { 396 for (const key in info.params) { 397 console.info(`testHiSysEventApi10: ${key}: ${info.params[key]}`) 398 } 399 } 400 }, 401 onServiceDied: () => { 402 console.info(`testHiSysEventApi10 Onservice died`); 403 } 404 } 405 try { 406 hiSysEvent.addWatcher(watcher110) 407 hiSysEvent.write({ 408 domain: "RELIABILITY", 409 name: "STACK", 410 eventType: hiSysEvent.EventType.FAULT 411 },(err, value) => { 412 if (err) { 413 console.error(`testHiSysEventApi10 json-callback-error code=${err.code}`); 414 done(); 415 } else { 416 console.log(`testHiSysEventApi10 json-callback-success value=${value}`); 417 } 418 }); 419 console.info('testHiSysEventApi10 end') 420 } catch (err) { 421 console.error(`testHiSysEventApi10 > error code: ${err.code}, error msg: ${err.message}`) 422 expect(false).assertTrue() 423 console.info('testHiSysEventApi10 end') 424 done() 425 } 426 setTimeout(() => { 427 hiSysEvent.removeWatcher(watcher110) 428 done() 429 }, 1000) 430 }) 431 432 /** 433 * @tc.number DFX_DFT_HiSysEvent_JS_1100 434 * @tc.name testHiSysEventApi11 435 * @tc.desc 验证可成功调用addwatcher接口,订阅规则domain+eventName+tag, rule=REGULAR 436 */ 437 it('testHiSysEventApi11', 3, async function (done) { 438 console.info('testHiSysEventApi11 start') 439 let watcher111 440 watcher111 = { 441 rules: [{ 442 domain: "RELIABILI\\w+", 443 name: "STA\\w+", 444 tag: "STABILI\\w+", 445 ruleType: hiSysEvent.RuleType.REGULAR 446 }], 447 onEvent: (info) => { 448 console.info(`testHiSysEventApi11: OnEvent...`) 449 expect(Object.keys(info).length > 0).assertTrue() 450 console.info(`testHiSysEventApi11: domain is ${info.domain}, name is ${info.name}, 451 eventType is ${info.eventType}`) 452 if (info.params instanceof Object) { 453 for (const key in info.params) { 454 console.info(`testHiSysEventApi11: ${key}: ${info.params[key]}`) 455 } 456 } 457 }, 458 onServiceDied: () => { 459 console.info(`testHiSysEventApi11 Onservice died`); 460 } 461 } 462 try { 463 hiSysEvent.addWatcher(watcher111) 464 hiSysEvent.write({ 465 domain: "RELIABILITY", 466 name: "STACK", 467 eventType: hiSysEvent.EventType.FAULT 468 },(err, value) => { 469 if (err) { 470 console.error(`testHiSysEventApi11 json-callback-error code=${err.code}`); 471 done() 472 } else { 473 console.log(`testHiSysEventApi11 json-callback-success value=${value}`); 474 } 475 }); 476 console.info('testHiSysEventApi11 end') 477 } catch (err) { 478 console.error(`testHiSysEventApi11 > error code: ${err.code}, error msg: ${err.message}`) 479 expect(false).assertTrue() 480 console.info('testHiSysEventApi11 end') 481 done() 482 } 483 setTimeout(() => { 484 hiSysEvent.removeWatcher(watcher111) 485 done() 486 }, 1000) 487 }) 488 489 /** 490 * @tc.number DFX_DFT_HiSysEvent_JS_1200 491 * @tc.name testHiSysEventApi12 492 * @tc.desc 验证可成功调用addwatcher接口,订阅规则domain+eventName+tag, rule=WHOLE_WORD 493 */ 494 it('testHiSysEventApi12', 3, async function (done) { 495 console.info('testHiSysEventApi12 start') 496 let watcher112 497 watcher112 = { 498 rules: [{ 499 domain: "RELIABILITY", 500 name: "STACK", 501 tag: "STABILITY", 502 ruleType: hiSysEvent.RuleType.WHOLE_WORD 503 }], 504 onEvent: (info) => { 505 console.info(`testHiSysEventApi12: OnEvent...`) 506 expect(Object.keys(info).length > 0).assertTrue() 507 console.info(`testHiSysEventApi12: domain is ${info.domain}, name is ${info.name}, 508 eventType is ${info.eventType}`) 509 if (info.params instanceof Object) { 510 for (const key in info.params) { 511 console.info(`testHiSysEventApi12: ${key}: ${info.params[key]}`) 512 } 513 } 514 }, 515 onServiceDied: () => { 516 console.info(`testHiSysEventApi12 Onservice died`); 517 } 518 } 519 try { 520 hiSysEvent.addWatcher(watcher112) 521 hiSysEvent.write({ 522 domain: "RELIABILITY", 523 name: "STACK", 524 eventType: hiSysEvent.EventType.FAULT 525 },(err, value) => { 526 if (err) { 527 console.error(`testHiSysEventApi12 json-callback-error code=${err.code}`); 528 done(); 529 } else { 530 console.log(`testHiSysEventApi12 json-callback-success value=${value}`); 531 } 532 }); 533 console.info('testHiSysEventApi12 end') 534 } catch (err) { 535 console.error(`testHiSysEventApi12 > error code: ${err.code}, error msg: ${err.message}`) 536 expect(false).assertTrue() 537 console.info('testHiSysEventApi12 end') 538 done() 539 } 540 setTimeout(() => { 541 hiSysEvent.removeWatcher(watcher112) 542 done() 543 }, 1000) 544 }) 545 546 /** 547 * @tc.number DFX_DFT_HiSysEvent_JS_1300 548 * @tc.name testHiSysEventApi13 549 * @tc.desc 验证可成功调用addwatcher接口,订阅规则domain+eventNameg, rule=PREFIX 550 */ 551 it('testHiSysEventApi13', 3, async function (done) { 552 console.info('testHiSysEventApi13 start') 553 let watcher113 554 watcher113 = { 555 rules: [{ 556 domain: "RELIABILI", 557 name: "STA", 558 ruleType: hiSysEvent.RuleType.PREFIX 559 }], 560 onEvent: (info) => { 561 console.info(`testHiSysEventApi13: OnEvent...`) 562 expect(Object.keys(info).length > 0).assertTrue() 563 console.info(`testHiSysEventApi13: domain is ${info.domain}, name is ${info.name}, 564 eventType is ${info.eventType}`) 565 if (info.params instanceof Object) { 566 for (const key in info.params) { 567 console.info(`testHiSysEventApi13: ${key}: ${info.params[key]}`) 568 } 569 } 570 }, 571 onServiceDied: () => { 572 console.info(`testHiSysEventApi13 Onservice died`); 573 } 574 } 575 try { 576 hiSysEvent.addWatcher(watcher113) 577 hiSysEvent.write({ 578 domain: "RELIABILITY", 579 name: "STACK", 580 eventType: hiSysEvent.EventType.FAULT 581 },(err, value) => { 582 if (err) { 583 console.error(`testHiSysEventApi13 json-callback-error code=${err.code}`); 584 done(); 585 } else { 586 console.log(`testHiSysEventApi13 json-callback-success value=${value}`); 587 } 588 }); 589 console.info('testHiSysEventApi13 end') 590 } catch (err) { 591 console.error(`testHiSysEventApi13 > error code: ${err.code}, error msg: ${err.message}`) 592 expect(false).assertTrue() 593 console.info('testHiSysEventApi13 end') 594 done() 595 } 596 setTimeout(() => { 597 hiSysEvent.removeWatcher(watcher113) 598 done() 599 }, 1000) 600 }) 601 602 /** 603 * @tc.number DFX_DFT_HiSysEvent_JS_1400 604 * @tc.name testHiSysEventApi14 605 * @tc.desc 验证可成功调用addwatcher接口,订阅规则domain+eventName, rule=REGULAR 606 */ 607 it('testHiSysEventApi14', 3, async function (done) { 608 console.info('testHiSysEventApi14 start') 609 let watcher114 610 watcher114 = { 611 rules: [{ 612 domain: "RELIABILI\\w+", 613 name: "STA\\w+", 614 ruleType: hiSysEvent.RuleType.REGULAR 615 }], 616 onEvent: (info) => { 617 console.info(`testHiSysEventApi14: OnEvent...`) 618 expect(Object.keys(info).length > 0).assertTrue() 619 console.info(`testHiSysEventApi14: domain is ${info.domain}, name is ${info.name}, 620 eventType is ${info.eventType}`) 621 if (info.params instanceof Object) { 622 for (const key in info.params) { 623 console.info(`testHiSysEventApi14: ${key}: ${info.params[key]}`) 624 } 625 } 626 }, 627 onServiceDied: () => { 628 console.info(`testHiSysEventApi14 Onservice died`); 629 } 630 } 631 try { 632 hiSysEvent.addWatcher(watcher114) 633 hiSysEvent.write({ 634 domain: "RELIABILITY", 635 name: "STACK", 636 eventType: hiSysEvent.EventType.FAULT 637 },(err, value) => { 638 if (err) { 639 console.error(`testHiSysEventApi14 json-callback-error code=${err.code}`); 640 done() 641 } else { 642 console.log(`testHiSysEventApi14 json-callback-success value=${value}`); 643 } 644 }); 645 console.info('testHiSysEventApi14 end') 646 } catch (err) { 647 console.error(`testHiSysEventApi14 > error code: ${err.code}, error msg: ${err.message}`) 648 expect(false).assertTrue() 649 console.info('testHiSysEventApi14 end') 650 done() 651 } 652 setTimeout(() => { 653 hiSysEvent.removeWatcher(watcher114) 654 done() 655 }, 1000) 656 }) 657 658 /** 659 * @tc.number DFX_DFT_HiSysEvent_JS_1500 660 * @tc.name testHiSysEventApi15 661 * @tc.desc 验证可成功调用addwatcher接口,订阅规则domain+eventName, rule=WHOLE_WORD 662 */ 663 it('testHiSysEventApi15', 3, async function (done) { 664 console.info('testHiSysEventApi15 start') 665 let watcher115 666 watcher115 = { 667 rules: [{ 668 domain: "RELIABILITY", 669 name: "STACK", 670 ruleType: hiSysEvent.RuleType.WHOLE_WORD 671 }], 672 onEvent: (info) => { 673 console.info(`testHiSysEventApi15: OnEvent...`) 674 expect(Object.keys(info).length > 0).assertTrue() 675 console.info(`testHiSysEventApi15: domain is ${info.domain}, name is ${info.name}, 676 eventType is ${info.eventType}`) 677 if (info.params instanceof Object) { 678 for (const key in info.params) { 679 console.info(`testHiSysEventApi15: ${key}: ${info.params[key]}`) 680 } 681 } 682 }, 683 onServiceDied: () => { 684 console.info(`testHiSysEventApi15 Onservice died`); 685 } 686 } 687 try { 688 hiSysEvent.addWatcher(watcher115) 689 hiSysEvent.write({ 690 domain: "RELIABILITY", 691 name: "STACK", 692 eventType: hiSysEvent.EventType.FAULT 693 },(err, value) => { 694 if (err) { 695 console.error(`testHiSysEventApi15 json-callback-error code=${err.code}`); 696 done(); 697 } else { 698 console.log(`testHiSysEventApi15 json-callback-success value=${value}`); 699 } 700 }); 701 console.info('testHiSysEventApi15 end') 702 } catch (err) { 703 console.error(`testHiSysEventApi15 > error code: ${err.code}, error msg: ${err.message}`) 704 expect(false).assertTrue() 705 console.info('testHiSysEventApi15 end') 706 done() 707 } 708 setTimeout(() => { 709 hiSysEvent.removeWatcher(watcher115) 710 done() 711 }, 1000) 712 }) 713 714 /** 715 * @tc.number DFX_DFT_HiSysEvent_JS_1600 716 * @tc.name testHiSysEventApi16 717 * @tc.desc 验证可成功调用addwatcher接口,订阅规则tag, rule=PREFIX 718 */ 719 it('testHiSysEventApi16', 3, async function (done) { 720 console.info('testHiSysEventApi16 start') 721 let watcher116 722 watcher116 = { 723 rules: [{ 724 domain: "", 725 name: "", 726 tag: "STABILI", 727 ruleType: hiSysEvent.RuleType.PREFIX 728 }], 729 onEvent: (info) => { 730 console.info(`testHiSysEventApi16: OnEvent...`) 731 expect(Object.keys(info).length > 0).assertTrue() 732 console.info(`testHiSysEventApi16: domain is ${info.domain}, name is ${info.name}, 733 eventType is ${info.eventType}`) 734 if (info.params instanceof Object) { 735 for (const key in info.params) { 736 console.info(`testHiSysEventApi16: ${key}: ${info.params[key]}`) 737 } 738 } 739 }, 740 onServiceDied: () => { 741 console.info(`testHiSysEventApi16 Onservice died`); 742 } 743 } 744 try { 745 hiSysEvent.addWatcher(watcher116) 746 hiSysEvent.write({ 747 domain: "RELIABILITY", 748 name: "STACK", 749 eventType: hiSysEvent.EventType.FAULT 750 },(err, value) => { 751 if (err) { 752 console.error(`testHiSysEventApi16 json-callback-error code=${err.code}`); 753 done(); 754 } else { 755 console.log(`testHiSysEventApi16 json-callback-success value=${value}`); 756 } 757 }); 758 console.info('testHiSysEventApi16 end') 759 } catch (err) { 760 console.error(`testHiSysEventApi16 > error code: ${err.code}, error msg: ${err.message}`) 761 expect(false).assertTrue() 762 console.info('testHiSysEventApi16 end') 763 done() 764 } 765 setTimeout(() => { 766 hiSysEvent.removeWatcher(watcher116) 767 done() 768 }, 1000) 769 }) 770 771 /** 772 * @tc.number DFX_DFT_HiSysEvent_JS_1700 773 * @tc.name testHiSysEventApi17 774 * @tc.desc 验证可成功调用addwatcher接口,订阅规则tag, rule=REGULAR 775 */ 776 it('testHiSysEventApi17', 3, async function (done) { 777 console.info('testHiSysEventApi17 start') 778 let watcher117 779 watcher117 = { 780 rules: [{ 781 domain: "", 782 name: "", 783 tag: "STABILI\\w+", 784 ruleType: hiSysEvent.RuleType.REGULAR 785 }], 786 onEvent: (info) => { 787 console.info(`testHiSysEventApi17: OnEvent...`) 788 expect(Object.keys(info).length > 0).assertTrue() 789 console.info(`testHiSysEventApi17: domain is ${info.domain}, name is ${info.name}, 790 eventType is ${info.eventType}`) 791 if (info.params instanceof Object) { 792 for (const key in info.params) { 793 console.info(`testHiSysEventApi17: ${key}: ${info.params[key]}`) 794 } 795 } 796 }, 797 onServiceDied: () => { 798 console.info(`testHiSysEventApi17 Onservice died`); 799 } 800 } 801 try { 802 hiSysEvent.addWatcher(watcher117) 803 hiSysEvent.write({ 804 domain: "RELIABILITY", 805 name: "STACK", 806 eventType: hiSysEvent.EventType.FAULT 807 },(err, value) => { 808 if (err) { 809 console.error(`testHiSysEventApi17 json-callback-error code=${err.code}`); 810 done() 811 } else { 812 console.log(`testHiSysEventApi17 json-callback-success value=${value}`); 813 } 814 }); 815 console.info('testHiSysEventApi17 end') 816 } catch (err) { 817 console.error(`testHiSysEventApi17 > error code: ${err.code}, error msg: ${err.message}`) 818 expect(false).assertTrue() 819 console.info('testHiSysEventApi17 end') 820 done() 821 } 822 setTimeout(() => { 823 hiSysEvent.removeWatcher(watcher117) 824 done() 825 }, 1000) 826 }) 827 828 /** 829 * @tc.number DFX_DFT_HiSysEvent_JS_1800 830 * @tc.name testHiSysEventApi18 831 * @tc.desc 验证可成功调用addwatcher接口,订阅规则tag, rule=WHOLE_WORD 832 */ 833 it('testHiSysEventApi18', 3, async function (done) { 834 console.info('testHiSysEventApi18 start') 835 let watcher118 836 watcher118 = { 837 rules: [{ 838 domain: "", 839 name: "", 840 tag: "STABILITY", 841 ruleType: hiSysEvent.RuleType.WHOLE_WORD 842 }], 843 onEvent: (info) => { 844 console.info(`testHiSysEventApi18: OnEvent...`) 845 expect(Object.keys(info).length > 0).assertTrue() 846 console.info(`testHiSysEventApi18: domain is ${info.domain}, name is ${info.name}, 847 eventType is ${info.eventType}`) 848 if (info.params instanceof Object) { 849 for (const key in info.params) { 850 console.info(`testHiSysEventApi18: ${key}: ${info.params[key]}`) 851 } 852 } 853 }, 854 onServiceDied: () => { 855 console.info(`testHiSysEventApi18 Onservice died`); 856 } 857 } 858 try { 859 hiSysEvent.addWatcher(watcher118) 860 hiSysEvent.write({ 861 domain: "RELIABILITY", 862 name: "STACK", 863 eventType: hiSysEvent.EventType.FAULT 864 },(err, value) => { 865 if (err) { 866 console.error(`testHiSysEventApi18 json-callback-error code=${err.code}`); 867 done(); 868 } else { 869 console.log(`testHiSysEventApi18 json-callback-success value=${value}`); 870 } 871 }); 872 console.info('testHiSysEventApi18 end') 873 } catch (err) { 874 console.error(`testHiSysEventApi18 > error code: ${err.code}, error msg: ${err.message}`) 875 expect(false).assertTrue() 876 console.info('testHiSysEventApi18 end') 877 done() 878 } 879 setTimeout(() => { 880 hiSysEvent.removeWatcher(watcher118) 881 done() 882 }, 1000) 883 }) 884 885 /** 886 * @tc.number DFX_DFT_HiSysEvent_JS_1900 887 * @tc.name testHiSysEventApi19 888 * @tc.desc 验证可添加订阅者后调用removewatcher接口删除订阅者 889 */ 890 it('testHiSysEventApi19', 3, async function (done) { 891 console.info('testHiSysEventApi19 start') 892 let watcher119 = { 893 rules: [{ 894 domain: "RELIABILITY", 895 name: "STACK", 896 tag: "STABILITY", 897 ruleType: hiSysEvent.RuleType.WHOLE_WORD 898 }], 899 onEvent: (info) => { 900 }, 901 onServiceDied: () => { 902 } 903 } 904 try { 905 hiSysEvent.addWatcher(watcher119) 906 hiSysEvent.removeWatcher(watcher119) 907 expect(true).assertTrue() 908 console.info('testHiSysEventApi19 end') 909 done(); 910 } catch (err) { 911 console.error(`testHiSysEventApi19 > error code: ${err.code}, error msg: ${err.message}`) 912 expect(false).assertTrue() 913 console.info('testHiSysEventApi19 error') 914 done() 915 } 916 }) 917 918 /** 919 * @tc.number DFX_DFT_HiSysEvent_JS_2000 920 * @tc.name testHiSysEventApi20 921 * @tc.desc 验证调用addwatcher接口,可添加20个规则 922 */ 923 it('testHiSysEventApi20', 3, async function (done) { 924 console.info('testHiSysEventApi20 start') 925 let msgArray = [] 926 for (let i = 0; i < 20; i++) { 927 msgArray[i] = { 928 domain: "RELIABILITY" + i, 929 name: "STACK", 930 tag: "STABILITY", 931 ruleType: hiSysEvent.RuleType.WHOLE_WORD, 932 } 933 } 934 let watcher = { 935 rules: msgArray, 936 onEvent: (info) => { 937 }, 938 onServiceDied: () => { 939 } 940 } 941 try { 942 hiSysEvent.addWatcher(watcher) 943 }catch (err) { 944 console.error(`testHiSysEventApi20 > error code: ${err.code}, error msg: ${err.message}`) 945 expect(false).assertTrue() 946 console.info('testHiSysEventApi20 end') 947 done() 948 } 949 hiSysEvent.removeWatcher(watcher) 950 done() 951 }) 952 953 /** 954 * @tc.number DFX_DFT_HiSysEvent_JS_2100 955 * @tc.name testHiSysEventApi21 956 * @tc.desc 验证调用addwatcher接口,可添加20个订阅者 957 */ 958 it('testHiSysEventApi21', 3, async function (done) { 959 console.info('testHiSysEventApi21 start') 960 let maxNumber = 20 961 var watcher = [] 962 try { 963 for (var i = 0; i < maxNumber; i++) { 964 watcher[i] = { 965 rules: [{ 966 domain: "RELIABILITY" + i, 967 name: "STACK", 968 ruleType: hiSysEvent.RuleType.WHOLE_WORD, 969 }], 970 onEvent: (info) => { 971 }, 972 onServiceDied: () => { 973 } 974 } 975 hiSysEvent.addWatcher(watcher[i]) 976 } 977 expect(true).assertTrue() 978 }catch (err) { 979 console.error(`testHiSysEventApi21 > addWatcher error code: ${err.code}, error msg: ${err.message}`) 980 expect(false).assertTrue() 981 done() 982 } 983 try { 984 for (var j = 0; j < maxNumber; j++) { 985 hiSysEvent.removeWatcher(watcher[j]) 986 } 987 console.info('testHiSysEventApi21 remove end') 988 done() 989 }catch (err) { 990 console.error(`testHiSysEventApi21 > remove error code: ${err.code}, error msg: ${err.message}`) 991 expect(false).assertTrue() 992 done() 993 } 994 }) 995 996 /** 997 * @tc.number DFX_DFT_HiSysEvent_JS_2200 998 * @tc.name testHiSysEventApi22 999 * @tc.desc 验证调用query接口,可添加100条查询规则 1000 */ 1001 it('testHiSysEventApi22', 3, async function (done) { 1002 console.info('testHiSysEventApi22 start') 1003 try { 1004 let msgArray = [] 1005 for (let i = 0; i < 100; i++) { 1006 msgArray[i] = { 1007 domain: "RELIABILITY", 1008 names: ["SYS_FREEZE"], 1009 } 1010 } 1011 hiSysEvent.query({ 1012 beginTime: -1, 1013 endTime: -1, 1014 maxEvents: 1, 1015 }, msgArray, { 1016 onQuery: function (infos) { 1017 console.info(`testHiSysEventApi22: onQuery...`) 1018 }, 1019 onComplete: function(reason, total) { 1020 console.info(`testHiSysEventApi22: onComplete...`) 1021 console.info(`testHiSysEventApi22: reason is ${reason}, total is ${total}`) 1022 expect(reason == 0).assertTrue() 1023 console.info(`testHiSysEventApi22 end`) 1024 } 1025 }) 1026 done() 1027 } catch (err) { 1028 console.error(`testHiSysEventApi22 delay > error code: ${err.code}, error msg: ${err.message}`) 1029 expect(false).assertTrue() 1030 console.info('testHiSysEventApi22 error') 1031 done() 1032 } 1033 }) 1034 1035 /** 1036 * @tc.number DFX_DFT_HiSysEvent_JS_2400 1037 * @tc.name testHiSysEventApi24 1038 * @tc.desc HiSysEvent查询接口测试-查询成功回调函数被调用(查询一个事件) 1039 */ 1040 it('testHiSysEventApi24', 1, async function (done) { 1041 console.info('testHiSysEventApi24 start') 1042 try { 1043 hiSysEvent.write({ 1044 domain: "ACCOUNT", 1045 name: "PERMISSION_EXCEPTION", 1046 eventType: hiSysEvent.EventType.SECURITY, 1047 params: { 1048 PID: 487 1049 } 1050 },(err, value) => { 1051 if (err) { 1052 } else { 1053 } 1054 }); 1055 setTimeout(() => { 1056 hiSysEvent.query({ 1057 beginTime: -1, 1058 endTime: -1, 1059 maxEvents: 1, 1060 }, [{ 1061 domain: "ACCOUNT", 1062 names: ["PERMISSION_EXCEPTION"], 1063 }], { 1064 onQuery: function (infos) { 1065 console.info(`testHiSysEventApi24: onQuery...`) 1066 expect(infos.length >= 0).assertTrue() 1067 console.info(`testHiSysEventApi24: infos.size is ${infos.length}`) 1068 if (infos instanceof Array) { 1069 for (let i = 0; i < infos.length; i++) { 1070 let item = infos[i]; 1071 console.info(`testHiSysEventApi24: domain is ${item.domain}, name is ${item.name}, 1072 eventType is ${item.eventType}`) 1073 if (item.params instanceof Object) { 1074 for (const key in item.params) { 1075 console.info(`testHiSysEventApi24: ${key}: ${item.params[key]}`) 1076 } 1077 } 1078 } 1079 } 1080 }, 1081 onComplete: function(reason, total) { 1082 console.info(`testHiSysEventApi24: onComplete...`) 1083 console.info(`testHiSysEventApi24: reason is ${reason}, total is ${total}`) 1084 expect(reason == 0).assertTrue() 1085 done() 1086 console.info(`testHiSysEventApi24 end`) 1087 } 1088 }) 1089 }, 2000); 1090 } catch (error) { 1091 expect(false).assertTrue() 1092 console.error(`error code: ${error.code}, error msg: ${error.message}`); 1093 } 1094 }) 1095 1096 /** 1097 * @tc.number DFX_DFT_HiSysEvent_JS_2500 1098 * @tc.name testHiSysEventApi25 1099 * @tc.desc HiSysEvent查询接口测试-查询成功回调函数被调用(查询多个事件) 1100 */ 1101 it('testHiSysEventApi25', 1, async function (done) { 1102 console.info('testHiSysEventApi25 start') 1103 try { 1104 hiSysEvent.write({ 1105 domain: "HIVIEWDFX", 1106 name: "SYS_USAGE", 1107 eventType: hiSysEvent.EventType.STATISTIC, 1108 },(err, value) => { 1109 if (err) { 1110 } else { 1111 } 1112 }); 1113 console.info('add second..') 1114 setTimeout(() => { 1115 hiSysEvent.write({ 1116 domain: "HIVIEWDFX", 1117 name: "PLUGIN_STATS", 1118 eventType: hiSysEvent.EventType.STATISTIC, 1119 },(err, value) => { 1120 if (err) { 1121 } else { 1122 } 1123 }) 1124 },1000) 1125 setTimeout(() => { 1126 hiSysEvent.query({ 1127 beginTime: -1, 1128 endTime: -1, 1129 maxEvents: 5, 1130 }, [{ 1131 domain: "HIVIEWDFX", 1132 names: ["SYS_USAGE","PLUGIN_STATS"], 1133 }], { 1134 onQuery: function (infos) { 1135 console.info(`testHiSysEventApi25: onQuery...`) 1136 expect(infos.length >= 0).assertTrue() 1137 console.info(`testHiSysEventApi25: infos.size is ${infos.length}`) 1138 if (infos instanceof Array) { 1139 for (let i = 0; i < infos.length; i++) { 1140 let item = infos[i]; 1141 console.info(`testHiSysEventApi25: domain is ${item.domain}, name is ${item.name}, 1142 eventType is ${item.eventType}`) 1143 if (item.params instanceof Object) { 1144 for (const key in item.params) { 1145 console.info(`testHiSysEventApi25: ${key}: ${item.params[key]}`) 1146 } 1147 } 1148 } 1149 } 1150 }, 1151 onComplete: function(reason, total) { 1152 console.info(`testHiSysEventApi25: onComplete...`) 1153 console.info(`testHiSysEventApi25: reason is ${reason}, total is ${total}`) 1154 expect(reason == 0).assertTrue() 1155 done() 1156 console.info(`testHiSysEventApi25 end`) 1157 } 1158 }) 1159 }, 2000); 1160 } catch (error) { 1161 expect(false).assertTrue() 1162 console.error(`error code: ${error.code}, error msg: ${error.message}`); 1163 } 1164 }) 1165}) 1166} 1167