• 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
16// @ts-ignore
17import { TraceRow } from '../../../../dist/trace/component/trace/base/TraceRow.js';
18
19jest.mock('../../../../dist/trace/database/ui-worker/ProcedureWorker.js', () => {
20  return {};
21});
22// @ts-ignore
23import {
24  CpuStateRender,
25  CpuStateStruct,
26  cpuState,
27} from '../../../../dist/trace/database/ui-worker/ProcedureWorkerCpuState.js';
28
29describe('ProcedureWorkerCpuState Test', () => {
30  it('ProcedureWorkerCpuStateTest01', function () {
31    let node = {
32      frame: {
33        x: 43,
34        y: 40,
35        width: 135,
36        height: 190,
37      },
38      startNS: 220,
39      value: 20,
40      startTs: 15,
41      dur: 12,
42      height: 62,
43    };
44    let frame = {
45      x: 80,
46      y: 28,
47      width: 180,
48      height: 80,
49    };
50    expect(CpuStateStruct.setFrame(node, 2, 2, 6, 4, frame)).toBeUndefined();
51  });
52
53  it('ProcedureWorkerCpuStateTest01', function () {
54    let node = {
55      frame: {
56        x: 10,
57        y: 22,
58        width: 540,
59        height: 700,
60      },
61      startNS: 560,
62      value: 10,
63      startTs: 7,
64      dur: 74,
65      height: 12,
66    };
67    let frame = {
68      x: 30,
69      y: 50,
70      width: 760,
71      height: 660,
72    };
73    expect(CpuStateStruct.setFrame(node, 2, 2, 6, 4, frame)).toBeUndefined();
74  });
75
76  it('ProcedureWorkerCpuStateTest02', function () {
77    let cpuStateRender = new CpuStateRender();
78    let node = [
79      {
80        frame: {
81          x: 20,
82          y: 20,
83          width: 100,
84          height: 100,
85        },
86        startNS: 200,
87        length: 0,
88        height: 2,
89      },
90    ];
91    let frame = {
92      x: 20,
93      y: 20,
94      width: 100,
95      height: 100,
96    };
97    expect(cpuStateRender.cpuState(node, [{ length: 1 }], '', [], 4, 1, 1, frame, true)).toBeUndefined();
98  });
99
100  it('ProcedureWorkerCpuStateTest03', function () {
101    let cpuStateRender = new CpuStateRender();
102    let dataList = new Array();
103    dataList.push({
104      startNS: 55,
105      dur: 120,
106      length: 53,
107      frame: { x: 0, y: 54, width: 30, height: 350 },
108    });
109    dataList.push({ startNS: 1, dur: 2, length: 1 });
110    let res = [
111      {
112        frame: {
113          x: 77,
114          y: 67,
115          width: 170,
116          height: 700,
117        },
118        startNS: 17,
119        length: 61,
120        height: 25,
121        dur: 12,
122      },
123    ];
124    let frame = {
125      x: 30,
126      y: 23,
127      width: 173,
128      height: 350,
129    };
130    expect(cpuStateRender.cpuState([], dataList, '', res, 1, 6, 5, frame, true)).toBeUndefined();
131  });
132
133  it('ProcedureWorkerCpuStateTest04', function () {
134    let cpuStateRender = new CpuStateRender();
135    let dataList = new Array();
136    dataList.push({
137      startNS: 23,
138      dur: 120,
139      length: 21,
140      frame: { x: 20, y: 45, width: 50, height: 150 },
141    });
142    dataList.push({ startNS: 1, dur: 2, length: 1 });
143    let res = [
144      {
145        frame: {
146          x: 57,
147          y: 30,
148          width: 770,
149          height: 503,
150        },
151        startNS: 13,
152        length: 21,
153        height: 22,
154        dur: 156,
155      },
156    ];
157    let frame = {
158      x: 60,
159      y: 23,
160      width: 170,
161      height: 900,
162    };
163    expect(cpuStateRender.cpuState([], dataList, '', res, 1, 6, 5, frame, true)).toBeUndefined();
164  });
165  it('ProcedureWorkerCpuStateTest05', function () {
166    let res = [
167      {
168        frame: {
169          x: 20,
170          y: 20,
171          width: 100,
172          height: 100,
173        },
174        startNS: 10,
175        length: 1,
176        height: 2,
177        dur: 1,
178      },
179    ];
180    const canvas = document.createElement('canvas');
181    canvas.width = 1;
182    canvas.height = 1;
183    const ctx = canvas.getContext('2d');
184    let path = new Path2D();
185    expect(CpuStateStruct.draw(ctx, path, res)).toBeUndefined();
186  });
187  it('ProcedureWorkerCpuStateTest06', function () {
188    let node = {
189      frame: {
190        x: 20,
191        y: 20,
192        width: 100,
193        height: 100,
194      },
195      startNS: 200,
196      value: 50,
197      startTs: 3,
198      dur: 1,
199      height: 2,
200    };
201    let frame = {
202      x: 20,
203      y: 20,
204      width: 100,
205      height: 100,
206    };
207    expect(CpuStateStruct.setCpuFrame(node, 2, 2, 6, frame)).toBeUndefined();
208  });
209  it('ProcedureWorkerCpuStateTest07', function () {
210    let req = {
211      lazyRefresh: true,
212      type: '',
213      startNS: 1,
214      endNS: 11,
215      totalNS: 1,
216      frame: {
217        x: 201,
218        y: 201,
219        width: 100,
220        height: 100,
221      },
222      useCache: false,
223      range: {
224        refresh: '',
225      },
226      canvas: 'b',
227      context: {
228        font: '11px sans-serif',
229        fillStyle: '#ec407a',
230        globalAlpha: 0.6,
231        beginPath: jest.fn(() => true),
232        stroke: jest.fn(() => true),
233        clearRect: jest.fn(() => true),
234        closePath: jest.fn(() => true),
235        measureText: jest.fn(() => true),
236        fillRect: jest.fn(() => true),
237        fill: jest.fn(() => true),
238      },
239      lineColor: '',
240      isHover: '',
241      hoverX: 1,
242      params: '',
243      wakeupBean: undefined,
244      flagMoveInfo: '',
245      flagSelectedInfo: '',
246      slicesTime: 3,
247      id: 1,
248      x: 20,
249      y: 20,
250      width: 100,
251      height: 100,
252    };
253    let cpuStateRender = new CpuStateRender();
254    window.postMessage = jest.fn(() => true);
255    expect(cpuStateRender.render(req, [], [], [])).toBeUndefined();
256  });
257  it('ProcedureWorkerCpuStateTest08', function () {
258    let cpuStateRender = new CpuStateRender();
259    let canvas = document.createElement('canvas') as HTMLCanvasElement;
260    let context = canvas.getContext('2d');
261    const data = {
262      cpuStateContext: context!,
263      useCache: true,
264      type: '',
265      traceRange: [],
266    };
267    window.postMessage = jest.fn(() => true);
268    expect(cpuStateRender.renderMainThread(data, new TraceRow())).toBeUndefined();
269  });
270});
271