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 16jest.mock('../../../../src/trace/component/SpSystemTrace', () => { 17 return {}; 18}); 19import { SpProcessChart } from '../../../../src/trace/component/chart/SpProcessChart'; 20import { TraceRow } from "../../../../src/trace/component/trace/base/TraceRow"; 21import { ProcessStruct } from "../../../../src/trace/database/ui-worker/ProcedureWorkerProcess"; 22 23jest.mock('../../../../src/js-heap/model/DatabaseStruct'); 24const sqlit = require('../../../../src/trace/database/sql/Func.sql'); 25jest.mock('../../../../src/trace/database/sql/Func.sql'); 26const processSqlite = require('../../../../src/trace/database/sql/ProcessThread.sql'); 27jest.mock('../../../../src/trace/database/sql/ProcessThread.sql'); 28const sqlite = require('../../../../src/trace/database/sql/SqlLite.sql'); 29jest.mock('../../../../src/trace/database/sql/SqlLite.sql'); 30const jankSqlite = require('../../../../src/trace/database/sql/Janks.sql'); 31jest.mock('../../../../src/trace/database/sql/Janks.sql'); 32const memSqlite = require('../../../../src/trace/database/sql/Memory.sql'); 33jest.mock('../../../../src/trace/database/sql/Memory.sql'); 34jest.mock('../../../../src/trace/database/ui-worker/ProcedureWorker', () => { 35 return {}; 36}); 37 38const intersectionObserverMock = () => ({ 39 observe: () => null, 40}); 41window.IntersectionObserver = jest.fn().mockImplementation(intersectionObserverMock); 42// @ts-ignore 43window.ResizeObserver = window.ResizeObserver || jest.fn().mockImplementation(() => ({ 44 observe: jest.fn(), 45 unobserve: jest.fn(), 46 disconnect: jest.fn(), 47})); 48 49describe('SpProcessChart Test', () => { 50 let htmlElement: any = document.createElement('sp-system-trace'); 51 let spProcessChart = new SpProcessChart(htmlElement); 52 let MockqueryProcessAsyncFunc = sqlit.queryProcessAsyncFunc; 53 let MockqueryDistributedRelationData = processSqlite.queryDistributedRelationData; 54 MockqueryDistributedRelationData.mockResolvedValue([]); 55 56 MockqueryProcessAsyncFunc.mockResolvedValue([ 57 { 58 tid: 1, 59 pid: 1, 60 threadName: '1', 61 track_id: 3, 62 startTs: 1111, 63 dur: 2000000, 64 funName: 'deliverInputEvent', 65 parent_id: 4, 66 id: 5, 67 cookie: 'ff', 68 depth: 5, 69 argsetid: 6, 70 }, 71 ]); 72 let processContentCount = processSqlite.queryProcessContentCount; 73 processContentCount.mockResolvedValue([ 74 { 75 pid: 1, 76 switch_count: 2, 77 thread_count: 3, 78 slice_count: 4, 79 mem_count: 5, 80 }, 81 ]); 82 let queryProcessThreads = processSqlite.queryProcessThreads; 83 queryProcessThreads.mockResolvedValue([]); 84 let queryProcessThreadsByTable = processSqlite.queryProcessThreadsByTable; 85 queryProcessThreadsByTable.mockResolvedValue([ 86 { 87 pid: 1, 88 tid: 0, 89 processName: 'process', 90 threadName: 'thread', 91 }, 92 ]); 93 let queryProcessMem = processSqlite.queryProcessMem; 94 queryProcessMem.mockResolvedValue([ 95 { 96 trackId: 1, 97 trackName: 'trackName', 98 upid: 2, 99 pid: 1, 100 processName: 'processName', 101 }, 102 ]); 103 let queryEventCountMap = sqlite.queryEventCountMap; 104 queryEventCountMap.mockResolvedValue([ 105 { 106 eventName: 'eventName', 107 count: 1, 108 }, 109 ]); 110 let queryProcess = processSqlite.queryProcess; 111 queryProcess.mockResolvedValue([ 112 { 113 pid: 1, 114 processName: 'processName', 115 }, 116 ]); 117 118 let queryProcessByTable = processSqlite.queryProcessByTable; 119 queryProcessByTable.mockResolvedValue([ 120 { 121 pid: 2, 122 processName: 'processName', 123 }, 124 ]); 125 126 let getMaxDepthByTid = sqlit.getMaxDepthByTid; 127 getMaxDepthByTid.mockResolvedValue([ 128 { 129 tid: 1, 130 maxDepth: 1, 131 }, 132 { 133 tid: 2, 134 maxDepth: 2, 135 }, 136 ]); 137 let queryAllJankProcess = jankSqlite.queryAllJankProcess; 138 queryAllJankProcess.mockResolvedValue([ 139 { 140 pid: 1, 141 }, 142 ]); 143 144 let queryAllExpectedData = sqlite.queryAllExpectedData; 145 queryAllExpectedData.mockResolvedValue([ 146 { 147 id: 41, 148 ts: 749660047, 149 name: 1159, 150 type: 1, 151 dur: 16657682, 152 pid: 1242, 153 cmdline: 'render_service', 154 }, 155 { 156 id: 45, 157 ts: 766321174, 158 name: 1160, 159 type: 1, 160 dur: 16657682, 161 pid: 1, 162 cmdline: 'render_service', 163 }, 164 ]); 165 166 let queryAllActualData = jankSqlite.queryAllActualData; 167 queryAllActualData.mockResolvedValue([ 168 { 169 id: 40, 170 ts: 750328000, 171 name: 1159, 172 type: 0, 173 dur: 22925000, 174 src_slice: '36', 175 jank_tag: 1, 176 dst_slice: null, 177 pid: 1242, 178 cmdline: 'render_service', 179 frame_type: 'render_service', 180 }, 181 { 182 id: 44, 183 ts: 773315000, 184 name: 1160, 185 type: 0, 186 dur: 17740000, 187 src_slice: '38,42', 188 jank_tag: 1, 189 dst_slice: null, 190 pid: 1, 191 cmdline: 'render_service', 192 frame_type: 'render_service', 193 }, 194 ]); 195 196 let queryProcessStartup = processSqlite.queryProcessStartup; 197 queryProcessStartup.mockResolvedValue([ 198 { 199 'pid': 3913, 200 'tid': 3913, 201 'itid': 366, 202 'startTs': 5651745832, 203 'dur': 38654167, 204 'startName': 0, 205 'endItid': 341, 206 'frame': { 207 'y': 5, 208 'height': 20, 209 'x': 1154, 210 'width': 9 211 }, 212 'v': true, 213 'stepName': 'Process Creating (38.65ms)', 214 'textMetricsWidth': 129.072265625 215 }, 216 { 217 'pid': 3913, 218 'tid': 3913, 219 'itid': 341, 220 'startTs': 5690399999, 221 'dur': 43619792, 222 'startName': 1, 223 'endItid': 486, 224 'frame': { 225 'y': 5, 226 'height': 20, 227 'x': 1162, 228 'width': 10 229 }, 230 'v': true, 231 'stepName': 'Application Launching (43.62ms)', 232 'textMetricsWidth': 156.416015625 233 }, 234 { 235 'pid': 3913, 236 'tid': 3913, 237 'itid': 486, 238 'startTs': 5734019791, 239 'dur': 23194270, 240 'startName': 2, 241 'endItid': 486, 242 'frame': { 243 'y': 5, 244 'height': 20, 245 'x': 1171, 246 'width': 6 247 }, 248 'v': true 249 }, 250 { 251 'pid': 3913, 252 'tid': 3913, 253 'itid': 486, 254 'startTs': 5757214061, 255 'dur': 115679167, 256 'startName': 3, 257 'endItid': 486, 258 'frame': { 259 'y': 5, 260 'height': 20, 261 'x': 1176, 262 'width': 24 263 }, 264 'v': true, 265 'stepName': 'UI Ability OnForeground (115.68ms)', 266 'textMetricsWidth': 172.59765625 267 }, 268 { 269 'pid': 3913, 270 'tid': 3913, 271 'itid': 486, 272 'startTs': 5872893228, 273 'dur': 62756250, 274 'startName': 4, 275 'frame': { 276 'y': 5, 277 'height': 20, 278 'x': 1199, 279 'width': 14 280 }, 281 'v': true, 282 'stepName': 'First Frame - APP Phase (62.76ms)', 283 'textMetricsWidth': 162.9638671875 284 }, 285 { 286 'pid': 3913, 287 'tid': 3913, 288 'itid': 17, 289 'startTs': 5968040103, 290 'dur': 29438021, 291 'startName': 5, 292 'frame': { 293 'y': 5, 294 'height': 20, 295 'x': 1219, 296 'width': 7 297 }, 298 'v': true 299 } 300 ]); 301 302 let queryProcessSoInitData = processSqlite.queryProcessSoInitData; 303 queryProcessSoInitData.mockResolvedValue([ 304 { 305 'pid': 3913, 306 'tid': 3913, 307 'itid': 486, 308 'startTs': 5678439061, 309 'dur': 1137500, 310 'soName': ' /system/lib64/seccomp/libapp_filter.z.so', 311 'depth': 0, 312 'frame': { 313 'x': 1160, 314 'y': 0, 315 'width': 1, 316 'height': 20 317 } 318 }, 319 { 320 'pid': 3913, 321 'tid': 3913, 322 'itid': 486, 323 'startTs': 5682777082, 324 'dur': 1130729, 325 'soName': ' /system/lib64/libhidebug.so', 326 'depth': 0, 327 'frame': { 328 'x': 1160, 329 'y': 0, 330 'width': 1, 331 'height': 20 332 } 333 }, 334 { 335 'pid': 3913, 336 'tid': 3913, 337 'itid': 486, 338 'startTs': 5696226041, 339 'dur': 4319791, 340 'soName': ' system/lib64/extensionability/libinputmethod_extension_module.z.so', 341 'depth': 0, 342 'frame': { 343 'x': 1163, 344 'y': 0, 345 'width': 1, 346 'height': 20 347 } 348 }, 349 { 350 'pid': 3913, 351 'tid': 3913, 352 'itid': 486, 353 'startTs': 5700671874, 354 'dur': 4128125, 355 'soName': ' system/lib64/extensionability/libbackup_extension_ability_native.z.so', 356 'depth': 0, 357 'frame': { 358 'x': 1164, 359 'y': 0, 360 'width': 1, 361 'height': 20 362 } 363 }, 364 { 365 'pid': 3913, 366 'tid': 3913, 367 'itid': 486, 368 'startTs': 5704894270, 369 'dur': 2187500, 370 'soName': ' system/lib64/extensionability/libwindow_extension_module.z.so', 371 'depth': 0, 372 'frame': { 373 'x': 1165, 374 'y': 0, 375 'width': 1, 376 'height': 20 377 } 378 }, 379 { 380 'pid': 3913, 381 'tid': 3913, 382 'itid': 486, 383 'startTs': 5707165624, 384 'dur': 1503125, 385 'soName': ' system/lib64/extensionability/libdatashare_ext_ability_module.z.so', 386 'depth': 0, 387 'frame': { 388 'x': 1165, 389 'y': 0, 390 'width': 1, 391 'height': 20 392 } 393 }, 394 { 395 'pid': 3913, 396 'tid': 3913, 397 'itid': 486, 398 'startTs': 5708719791, 399 'dur': 2018229, 400 'soName': ' system/lib64/extensionability/libpush_extension.z.so', 401 'depth': 0, 402 'frame': { 403 'x': 1166, 404 'y': 0, 405 'width': 1, 406 'height': 20 407 } 408 }, 409 { 410 'pid': 3913, 411 'tid': 3913, 412 'itid': 486, 413 'startTs': 5710788020, 414 'dur': 846875, 415 'soName': ' system/lib64/extensionability/libenterprise_admin_extension_module.z.so', 416 'depth': 0, 417 'frame': { 418 'x': 1166, 419 'y': 0, 420 'width': 1, 421 'height': 20 422 } 423 }, 424 { 425 'pid': 3913, 426 'tid': 3913, 427 'itid': 486, 428 'startTs': 5711693749, 429 'dur': 1522917, 430 'soName': ' system/lib64/extensionability/libstatic_subscriber_extension_module.z.so', 431 'depth': 0, 432 'frame': { 433 'x': 1166, 434 'y': 0, 435 'width': 1, 436 'height': 20 437 } 438 }, 439 { 440 'pid': 3913, 441 'tid': 3913, 442 'itid': 486, 443 'startTs': 5713377603, 444 'dur': 1327604, 445 'soName': ' system/lib64/extensionability/libui_extension_module.z.so', 446 'depth': 0, 447 'frame': { 448 'x': 1167, 449 'y': 0, 450 'width': 1, 451 'height': 20 452 } 453 }, 454 { 455 'pid': 3913, 456 'tid': 3913, 457 'itid': 486, 458 'startTs': 5714757291, 459 'dur': 2567187, 460 'soName': ' system/lib64/extensionability/libauthorization_extension_module.z.so', 461 'depth': 0, 462 'frame': { 463 'x': 1167, 464 'y': 0, 465 'width': 1, 466 'height': 20 467 } 468 }, 469 { 470 'pid': 3913, 471 'tid': 3913, 472 'itid': 486, 473 'startTs': 5717385936, 474 'dur': 2341146, 475 'soName': ' system/lib64/extensionability/libaccessibility_extension_module.z.so', 476 'depth': 0, 477 'frame': { 478 'x': 1167, 479 'y': 0, 480 'width': 1, 481 'height': 20 482 } 483 }, 484 { 485 'pid': 3913, 486 'tid': 3913, 487 'itid': 486, 488 'startTs': 5719780728, 489 'dur': 1603646, 490 'soName': ' system/lib64/extensionability/libservice_extension_module.z.so', 491 'depth': 0, 492 'frame': { 493 'x': 1168, 494 'y': 0, 495 'width': 1, 496 'height': 20 497 } 498 }, 499 { 500 'pid': 3913, 501 'tid': 3913, 502 'itid': 486, 503 'startTs': 5721437499, 504 'dur': 1314583, 505 'soName': ' system/lib64/extensionability/libworkschedextension.z.so', 506 'depth': 0, 507 'frame': { 508 'x': 1168, 509 'y': 0, 510 'width': 1, 511 'height': 20 512 } 513 } 514 ]); 515 let processData = processSqlite.queryProcessData; 516 processData.mockResolvedValue([ 517 { 518 cpu: 0, dur: 199000, startTime: 259730000 519 }, 520 { 521 cpu: 2, dur: 147000, startTime: 307742000 522 } 523 ]); 524 let processMemData = processSqlite.queryProcessMemData; 525 processMemData.mockResolvedValue([ 526 { 527 startTime: 593015789, 528 track_id: 153, 529 ts: 30150767408970, 530 type: 'measure', 531 value: 0 532 }, 533 { 534 startTime: 593360060, 535 track_id: 153, 536 ts: 30150767753241, 537 type: 'measure', 538 value: 1 539 } 540 ]); 541 let maxValue = memSqlite.queryMemFilterIdMaxValue; 542 maxValue.mockResolvedValue([ 543 { 544 filterId: 1, 545 maxValue: 522 546 }, 547 { 548 filterId: 2, 549 maxValue: 563 550 } 551 ]); 552 let funcNames = sqlit.queryAllFuncNames; 553 funcNames.mockResolvedValue([ 554 { 555 id: 0, 556 name: 'test' 557 } 558 ]); 559 560 let soInitNames = sqlite.queryAllSoInitNames; 561 soInitNames.mockResolvedValue([ 562 { 563 id: 1, 564 name: 'soInitName' 565 } 566 ]); 567 568 let allProcessNames = processSqlite.queryAllProcessNames; 569 allProcessNames.mockResolvedValue([ 570 { 571 id: 2, 572 name: 'processName', 573 pid: 256 574 } 575 ]); 576 577 let srcSlices = sqlite.queryAllSrcSlices; 578 srcSlices.mockResolvedValue([ 579 { 580 id: 3, 581 src: 'src' 582 } 583 ]); 584 585 let threadNames = processSqlite.queryAllThreadName; 586 threadNames.mockResolvedValue([ 587 { 588 tid: 4, 589 name: 'threadName' 590 } 591 ]); 592 593 spProcessChart.setAttribute = jest.fn(); 594 spProcessChart.addChildTraceRow = jest.fn(); 595 it('SpProcessChart01', function () { 596 spProcessChart.init(); 597 expect(spProcessChart).toBeDefined(); 598 }); 599 600 it('SpProcessChart02', function () { 601 expect(spProcessChart.initAsyncFuncData()).not.toBeUndefined(); 602 }); 603 604 it('SpProcessChart03', function () { 605 spProcessChart.initAsyncFuncData(); 606 spProcessChart.initDeliverInputEvent(); 607 expect(spProcessChart.processAsyncFuncArray.length).toEqual(1); 608 }); 609 610 it('SpProcessChart04', function () { 611 let row = new TraceRow<ProcessStruct>(); 612 let startUpRow = spProcessChart.addStartUpRow(row); 613 expect(startUpRow).not.toBeUndefined(); 614 }); 615 616 it('SpProcessChart05', function () { 617 let row = new TraceRow<ProcessStruct>(); 618 let soInitRow = spProcessChart.addSoInitRow(row, 1); 619 expect(soInitRow).not.toBeUndefined(); 620 }); 621}); 622