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
16import { TraceRow } from '../../../../src/trace/component/trace/base/TraceRow';
17import {
18  HiPerfCpuStruct,
19  HiperfCpuRender2,
20} from '../../../../src/trace/database/ui-worker/hiperf/ProcedureWorkerHiPerfCPU2';
21import { hiPerf } from '../../../../src/trace/database/ui-worker/ProcedureWorkerCommon';
22jest.mock('../../../../src/trace/database/ui-worker/ProcedureWorker', () => {
23  return {};
24});
25jest.mock('../../../../src/trace/component/SpSystemTrace', () => {
26  return {};
27});
28describe('ProcedureWorkerHiPerfCPU Test', () => {
29  let frame = {
30    x: 0,
31    y: 9,
32    width: 10,
33    height: 10,
34  };
35  it('ProcedureWorkerHiPerfCPUTest01', () => {
36    const data = {
37      frame: undefined,
38      cpu: 3,
39      startNs: 56,
40      value: 45,
41    };
42    const canvas = document.createElement('canvas');
43    canvas.width = 1;
44    canvas.height = 4;
45    const ctx = canvas.getContext('2d');
46    expect(HiPerfCpuStruct.draw(ctx, '', data, true)).toBeUndefined();
47  });
48
49  it('ProcedureWorkerHiPerfCPUTest04', () => {
50    const canvas = document.createElement('canvas');
51    canvas.width = 1;
52    canvas.height = 1;
53    const ctx = canvas.getContext('2d');
54    expect(HiPerfCpuStruct.drawRoundRectPath(ctx, 1, 1, 1, 1, 1)).toBeUndefined();
55  });
56
57  it('ProcedureWorkerHiPerfCPUTest05', function () {
58    expect(HiPerfCpuStruct.groupBy10MS([{ id: 1, NS: 3 }, { copy: '1' }], 10, '')).toEqual([
59      { dur: 10000000, height: NaN, eventCount: undefined ,startNS: NaN ,sampleCount: undefined },
60      { dur: 10000000, height: NaN, eventCount: NaN ,startNS: NaN ,sampleCount: 2 }
61    ]);
62  });
63  it('ProcedureWorkerHiPerfCPUTest06', function () {
64    let req = {
65      lazyRefresh: true,
66      type: 'a',
67      startNS: 1,
68      endNS: 1,
69      totalNS: 1,
70      frame: {
71        x: 20,
72        y: 20,
73        width: 100,
74        height: 300,
75      },
76      useCache: false,
77      range: {
78        refresh: '',
79      },
80      canvas: 'a',
81      context: {
82        font: '11px sans-serif',
83        fillStyle: '#ec407a',
84        globalAlpha: 0.7,
85        fill: jest.fn(() => true),
86        clearRect: jest.fn(() => true),
87        beginPath: jest.fn(() => true),
88        stroke: jest.fn(() => true),
89        closePath: jest.fn(() => true),
90        measureText: jest.fn(() => true),
91        fillRect: jest.fn(() => true),
92      },
93      lineColor: '',
94      isHover: '',
95      params: '',
96      wakeupBean: undefined,
97      flagMoveInfo: '',
98      width: 100,
99      flagSelectedInfo: '',
100      slicesTime: 3,
101      id: 1,
102      x: 20,
103      y: 20,
104      height: 100,
105      scale: 100_000_001,
106      hoverX: 1,
107    };
108    let hiperfCpuRender = new HiperfCpuRender2();
109    window.postMessage = jest.fn(() => true);
110    expect(hiperfCpuRender.render(req, [], [], [])).toBeUndefined();
111  });
112  it('ProcedureWorkerHiPerfCPUTest08', function () {
113    let dataList = new Array();
114    dataList.push({
115      startNS: 0,
116      length: 1,
117      frame: { x: 0, y: 9, width: 10, height: 10 },
118      dur: 10,
119    });
120    dataList.push({ startNS: 1, dur: 2, length: 1 });
121    hiPerf(dataList, [{ length: 0 }], dataList, 8, 3, '', true, 1, true);
122  });
123  it('ProcedureWorkerHiPerfCPUTest09 ', function () {
124    let req = {
125      lazyRefresh: true,
126      type: 'a',
127      startNS: 1,
128      endNS: 1,
129      totalNS: 1,
130      frame: {
131        x: 20,
132        y: 20,
133        width: 100,
134        height: 300,
135      },
136      useCache: false,
137      range: {
138        refresh: '',
139      },
140      canvas: 'a',
141      context: {
142        font: '11px sans-serif',
143        fillStyle: '#ec407a',
144        globalAlpha: 0.7,
145        fill: jest.fn(() => true),
146        clearRect: jest.fn(() => true),
147        beginPath: jest.fn(() => true),
148        stroke: jest.fn(() => true),
149        closePath: jest.fn(() => true),
150        measureText: jest.fn(() => true),
151        fillRect: jest.fn(() => true),
152      },
153      lineColor: '',
154      isHover: '',
155      params: '',
156      wakeupBean: undefined,
157      flagMoveInfo: '',
158      width: 100,
159      flagSelectedInfo: '',
160      slicesTime: 3,
161      id: 1,
162      x: 20,
163      y: 20,
164      height: 100,
165      scale: 100_000_001,
166      hoverX: 1,
167    };
168    let hiperfCpuRender = new HiperfCpuRender2();
169    window.postMessage = jest.fn(() => true);
170    expect(hiperfCpuRender.renderMainThread(req,new TraceRow<HiPerfCpuStruct>()))
171  });
172});
173