• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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('../../../../dist/trace/database/ui-worker/ProcedureWorkerCPU.js', () => {});
17jest.mock('../../../../dist/trace/component/trace/base/TraceSheet.js', () => {});
18jest.mock('../../../../dist/trace/component/SpSystemTrace.js', () => {
19  return {
20    CurrentSlicesTime: () => {},
21  };
22});
23// @ts-ignore
24import {
25  ChartStruct,
26  drawFlagLine,
27  drawLines,
28  getFrameChartColor,
29  getHeatColor,
30  Point,
31  Rect,
32  ns2s,
33  ns2x,
34  drawSelection,
35  drawWakeUp,
36  fillCacheData,
37  findRange,
38  FilterConfig,
39  dataFilterHandler,
40  drawFlagLineSegment,
41  drawSelectionRange,
42  drawLinkLines,
43  // @ts-ignore
44} from '../../../../dist/trace/database/ui-worker/ProcedureWorkerCommon.js';
45// @ts-ignore
46import { Flag } from '../../../../dist/trace/database/ui-worker/ProcedureWorkerTimeline.js';
47// @ts-ignore
48import { ColorUtils } from '../../../../dist/trace/component/trace/base/ColorUtils.js';
49// @ts-ignore
50import { TraceRow } from '../../../../dist/trace/component/trace/base/TraceRow.js';
51// @ts-ignore
52import { TimerShaftElement } from '../../../../dist/trace/component/trace/TimerShaftElement.js';
53// @ts-ignore
54import { EventCenter } from '../../../../dist/trace/component/trace/base/EventCenter.js';
55
56declare global {
57  interface Window {
58    SmartEvent: {
59      UI: {
60        RefreshCanvas: string; //selected menu trace
61        SliceMark: string; //Set the tag scope
62        TraceRowComplete: string; //Triggered after the row component has finished loading data
63        MenuTrace: string; //selected menu trace
64        TimeRange: string; //Set the timeline range
65      };
66    };
67    subscribeOnce(evt: string, fn: (b: any) => void): void;
68    clearTraceRowComplete(): void;
69    unsubscribe(evt: string, fn: (b: any) => void): void;
70    publish(evt: string, data: any): void;
71    subscribe(evt: string, fn: (b: any) => void): void;
72  }
73}
74
75window.SmartEvent = {
76  UI: {
77    MenuTrace: 'SmartEvent-UI-MenuTrace',
78    RefreshCanvas: 'SmartEvent-UI-RefreshCanvas',
79    SliceMark: 'SmartEvent-UI-SliceMark',
80    TimeRange: 'SmartEvent-UI-TimeRange',
81    TraceRowComplete: 'SmartEvent-UI-TraceRowComplete',
82  },
83};
84Window.prototype.unsubscribe = (ev, fn) => EventCenter.unsubscribe(ev, fn);
85Window.prototype.subscribe = (ev, fn) => EventCenter.subscribe(ev, fn);
86Window.prototype.publish = (ev, data) => EventCenter.publish(ev, data);
87Window.prototype.subscribeOnce = (ev, data) => EventCenter.subscribeOnce(ev, data);
88Window.prototype.clearTraceRowComplete = () => EventCenter.clearTraceRowComplete();
89
90describe('ProcedureWorkerCommon Test', () => {
91  let rect = new Rect();
92  let fullData = [
93    {
94      cpu: 2,
95      dur: 140000,
96      end_state: 'S',
97      frame: {
98        y: 5,
99        height: 30,
100      },
101      id: 62,
102      name: '2555',
103      priority: 100,
104      processCmdLine: 'com.test',
105      processId: 2983,
106      processName: 'com.test',
107      schedId: 44,
108      startTime: 3845000,
109      tid: 2996,
110      type: 'thread',
111    },
112  ];
113  let filterData = [
114    {
115      cpu: 0,
116      dur: 69444,
117      end_state: 'sR',
118      frame: { y: 15, height: 10, x: 13, width: 34 },
119      id: 4,
120      name: 'test',
121      priority: 23,
122      processCmdLine: 'test',
123      processId: 3255,
124      processName: 'test',
125      schedId: 3,
126      startTime: 53333,
127      tid: 6,
128      translateY: 0,
129      type: 'thread',
130      v: false,
131    },
132  ];
133  let condition = {
134    startKey: 'startNS',
135    durKey: 'dur',
136    startNS: 20,
137    endNS: 1000,
138    totalNS: 2000,
139    frame: { x: 10, y: 10 },
140    paddingTop: 5,
141    useCache: true,
142  };
143
144  document.body.innerHTML = '<timer-shaft-element id="timerShaftEL"></timer-shaft-element>';
145  let timerShaftElement = document.querySelector('#timerShaftEL') as TimerShaftElement;
146  timerShaftElement.totalNS = 1000;
147  timerShaftElement.startNS = 1000;
148  timerShaftElement.endNS = 2000;
149  timerShaftElement.setRangeNS(1522, 5222);
150  timerShaftElement.getBoundingClientRect = jest.fn(() => {
151    return {
152      width: 648,
153    };
154  });
155
156  it('ProcedureWorkerCommon01', function () {
157    expect(rect.contains(1, 2)).not.toBeUndefined();
158  });
159
160  it('ProcedureWorkerCommon02', function () {
161    expect(rect.containsWithPadding()).not.toBeUndefined();
162  });
163
164  it('ProcedureWorkerCommon03', function () {
165    let point = new Point();
166    expect(point).not.toBeUndefined();
167  });
168
169  it('ProcedureWorkerCommon04', function () {
170    let rect = new Rect();
171    expect(Rect.contains(rect, 1, 2)).toBe(false);
172  });
173
174  it('ProcedureWorkerCommon05', function () {
175    let rect = new Rect();
176    expect(Rect.containsWithPadding(rect, 1, 2, 1, 2)).toBe(false);
177  });
178
179  it('ProcedureWorkerCommon06', function () {
180    let rect = new Rect();
181    expect(Rect.containsWithMargin(rect, 1, 2, 1, 2, 1, 1)).toBe(false);
182  });
183
184  it('ProcedureWorkerCommon07', function () {
185    let rect = new Rect();
186    let rect2 = new Rect();
187    expect(Rect.intersect(rect, rect2)).toBe(false);
188  });
189
190  it('ProcedureWorkerCommon08', function () {
191    let rect = new Rect();
192    expect(rect.containsWithMargin(1, 2, 3, 5, 4, 5)).toBe(false);
193  });
194
195  it('ProcedureWorkerCommon09', function () {
196    let rect = new Rect();
197    expect(rect.containsWithPadding(1, 2, 3, 5)).toBe(false);
198  });
199
200  it('ProcedureWorkerCommon10', function () {
201    let rect = new Rect();
202    let rect2 = new Rect();
203    expect(rect.intersect(rect2)).toBe(false);
204  });
205
206  it('ProcedureWorkerCommon011', function () {
207    expect(ColorUtils.formatNumberComma('11232')).toBe('11,232');
208  });
209
210  it('ProcedureWorkerCommon012', function () {
211    const canvas = document.createElement('canvas');
212    canvas.width = 1;
213    canvas.height = 1;
214    const ctx = canvas.getContext('2d');
215    let aaa = [1, 2, 3];
216    drawLines(ctx, aaa, 1, '#ffff');
217    expect(ColorUtils.formatNumberComma('11232')).toBe('11,232');
218  });
219
220  it('ProcedureWorkerCommon013', function () {
221    const canvas = document.createElement('canvas');
222    canvas.width = 1;
223    canvas.height = 1;
224    const ctx = canvas.getContext('2d');
225    let aaa = [1, 2, 3];
226    let flag = new Flag(1, 2, 3, 4, 5, '#FFF', false);
227    let rect2 = new Rect();
228    drawFlagLine(ctx, aaa, flag, 1, 2, 2, rect2);
229    expect(ColorUtils.formatNumberComma('11232')).toBe('11,232');
230  });
231
232  it('ProcedureWorkerCommon20', function () {
233    expect(ns2s(2_000_000_000)).toBe('2.0 s');
234  });
235
236  it('ProcedureWorkerCommon21', function () {
237    expect(ns2s(2_000_000)).toBe('2.0 ms');
238  });
239
240  it('ProcedureWorkerCommon22', function () {
241    expect(ns2s(2_000)).toBe('2.0 μs');
242  });
243
244  it('ProcedureWorkerCommon23', function () {
245    expect(ns2s(1)).toBe('1.0 ns');
246  });
247
248  it('ProcedureWorkerCommon24', function () {
249    expect(ns2s(-1)).toBe('-1.0 s');
250  });
251
252  it('ProcedureWorkerCommon25', function () {
253    expect(ColorUtils.hashFunc('', 10, 10)).toBe(3);
254  });
255
256  it('ProcedureWorkerCommon26', function () {
257    expect(ns2x(10, 1, 0, 1, { width: 2 })).toBe(2);
258  });
259
260  it('ProcedureWorkerCommon27', function () {
261    expect(ns2x(-10, 1, 0, 1, { width: 2 })).toBe(0);
262  });
263
264  it('ProcedureWorkerCommon28', function () {
265    const canvas = document.createElement('canvas');
266    canvas.width = 1;
267    canvas.height = 1;
268    const ctx = canvas.getContext('2d');
269    let flag = {
270      color: '',
271      x: 1,
272    };
273    let select = {
274      color: '',
275      x: 1,
276      time: '',
277    };
278    let frame = {
279      height: 1,
280    };
281    let slicesTime = {
282      startTime: 1,
283      endTime: 1,
284      color: '#dadada',
285    };
286    expect(drawFlagLine(ctx, flag, select, 1, 2, 1, frame, slicesTime)).toBeUndefined();
287  });
288
289  it('ProcedureWorkerCommon29', function () {
290    const canvas = document.createElement('canvas');
291    canvas.width = 1;
292    canvas.height = 1;
293    const context = canvas.getContext('2d');
294
295    let params = {
296      isRangeSelect: true,
297      rangeSelectObject: {
298        startX: 31,
299        endX: 13,
300        startNS: 132,
301        endNS: 120,
302      },
303      startNS: 21,
304      endNS: 120,
305      totalNS: 49,
306      frame: {
307        y: 1,
308      },
309    };
310    expect(drawSelection(context, params)).toBeUndefined();
311  });
312
313  it('ProcedureWorkerCommon30', function () {
314    const canvas = document.createElement('canvas');
315    canvas.width = 1;
316    canvas.height = 1;
317    const context = canvas.getContext('2d');
318    let wake = {
319      wakeupTime: 12,
320      cpu: 2,
321    };
322    let frame = new Rect(20, 30, 10, 30);
323    let selectCpuStruct = [
324      {
325        cpu: 3,
326        dur: 9031110,
327        end_state: 'R',
328        frame: { y: 0, height: 60, x: 31, width: 3 },
329        id: 9,
330        name: 'test',
331        priority: 120,
332        processCmdLine: 'lin',
333        processId: 3303,
334        processName: 'test',
335        schedId: 55,
336        startTime: 4064044,
337        tid: 3303,
338        translateY: 40,
339        type: 'thread',
340        v: true,
341      },
342    ];
343    expect(drawWakeUp(context, wake, 1, 2, 1, frame, selectCpuStruct, undefined)).toBeUndefined();
344  });
345
346  it('ProcedureWorkerCommon31', function () {
347    let fillCache = fillCacheData(filterData, condition);
348    expect(fillCache).toBe(true);
349  });
350
351  it('ProcedureWorkerCommon32', function () {
352    let slice = findRange(fullData, condition);
353    expect(slice.length).toBe(1);
354  });
355
356  it('ProcedureWorkerCommon33', function () {
357    let condition = {
358      startKey: 'startNS',
359      durKey: 'dur',
360      startNS: 20,
361      endNS: 1000,
362      totalNS: 2000,
363      frame: { x: 10, y: 10 },
364      paddingTop: 5,
365      useCache: false,
366    };
367    let dataFilter = dataFilterHandler(fullData, filterData, condition);
368    expect(dataFilter).toBeUndefined();
369  });
370
371  it('ProcedureWorkerCommon34', function () {
372    const canvas = document.createElement('canvas');
373    canvas.width = 1;
374    canvas.height = 1;
375    const ctx = canvas.getContext('2d');
376    const hoverFlag = {
377      x: 300,
378      y: 300,
379      width: 1300,
380      height: 1030,
381      time: 2550,
382      color: 'red',
383      selected: false,
384      text: 'test',
385      hidden: false,
386      type: 'type',
387    };
388    const selectFlag = {
389      x: 180,
390      y: 180,
391      width: 800,
392      height: 80,
393      time: 258,
394      color: 'green',
395      selected: false,
396      text: 'test',
397      hidden: false,
398      type: 'type',
399    };
400    TraceRow.range = {
401      startNS: 64,
402      endNS: 25453,
403      totalNS: 333,
404    };
405    let data = {
406      sportRuler: {
407        slicesTimeList: [
408          {
409            startTime: 11,
410            endTime: 22,
411            color: '#dadada',
412          },
413          {
414            startTime: 33,
415            endTime: 66,
416            color: '#dadada',
417          },
418        ],
419      },
420    };
421    expect(
422      drawFlagLineSegment(
423        ctx,
424        hoverFlag,
425        selectFlag,
426        {
427          y: 15,
428          height: 10,
429          x: 11,
430          width: 53,
431        },
432        data
433      )
434    ).toBeUndefined();
435  });
436
437  it('ProcedureWorkerCommon35', function () {
438    const canvas = document.createElement('canvas');
439    canvas.width = 1;
440    canvas.height = 1;
441    const context = canvas.getContext('2d');
442    let params = {
443      rangeSelect: true,
444      rangeSelectObject: {
445        startX: 71,
446        endX: 100,
447        startNS: 61,
448        endNS: 100,
449      },
450      startNS: 401,
451      endNS: 190,
452      totalNS: 999,
453      frame: {
454        y: 3,
455      },
456    };
457    TraceRow.rangeSelectObject = {
458      startX: 125,
459      endX: 25226,
460    };
461    expect(drawSelectionRange(context, params)).toBeUndefined();
462  });
463});
464