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';
17
18import {
19  CpuStruct,
20  CpuRender,
21  EmptyRender,
22} from '../../../../src/trace/database/ui-worker/cpu/ProcedureWorkerCPU';
23
24jest.mock('../../../../src/trace/component/trace/timer-shaft/RangeRuler', () => {
25  return {};
26});
27jest.mock('../../../../src/trace/database/ui-worker/ProcedureWorker', () => {
28  return {};
29});
30jest.mock('../../../../src/trace/component/SpSystemTrace', () => {
31  return {};
32});
33describe(' Test', () => {
34  const dataSource = {
35    frame: {
36      x: 310,
37      y: 130,
38      width: 1430,
39      height: 1430,
40    },
41    startNS: 430,
42    processId: '',
43  };
44
45  it('CPUTest03', () => {
46    const canvas = document.createElement('canvas');
47    canvas.width = 13;
48    canvas.height = 13;
49    const ctx = canvas.getContext('2d');
50
51    const data = {
52      frame: {
53        x: 205,
54        y: 205,
55        width: 101,
56        height: 101,
57      },
58      startNS: 201,
59      value: 51,
60    };
61    expect(CpuStruct.draw(ctx, data)).toBeUndefined();
62  });
63
64  it('CPUTest04', () => {
65    expect(CpuStruct.equals(new CpuStruct(), new CpuStruct())).toBeTruthy();
66  });
67
68  it('CPUTest06', () => {
69    expect(CpuStruct.equals([], dataSource)).toBeFalsy();
70  });
71
72  it('CPUTest05', () => {
73    const canvas = document.createElement('canvas');
74    canvas.width = 1;
75    canvas.height = 1;
76    const ctx = canvas.getContext('2d');
77
78    const data = {
79      frame: {
80        x: 230,
81        y: 202,
82        width: 100,
83        height: 100,
84      },
85      startNS: 200,
86      value: 50,
87    };
88    expect(CpuStruct.draw(ctx, data)).toBeUndefined();
89  });
90
91  it('CPUTest07', function () {
92    let cpuRender = new CpuRender();
93    let node = [
94      {
95        frame: {
96          x: 20,
97          y: 20,
98          width: 100,
99          height: 100,
100        },
101        startNS: 200,
102        length: 1,
103        height: 2,
104      },
105    ];
106    let frame = {
107      x: 20,
108      y: 20,
109      width: 100,
110      height: 100,
111    };
112    let list = [
113      {
114        frame: {
115          x: 10,
116          y: 20,
117          width: 100,
118          height: 100,
119        },
120        startNS: 200,
121        length: 2,
122        height: 2,
123      },
124    ];
125    expect(cpuRender.cpu(list, node, 1, 1, 1, frame, true)).toBeUndefined();
126  });
127
128  it('CPUTest07', function () {
129    let cpuRender = new CpuRender();
130    let node = [
131      {
132        frame: {
133          x: 207,
134          y: 201,
135          width: 110,
136          height: 200,
137        },
138        startNS: 200,
139        length: 31,
140        height: 30,
141      },
142    ];
143    let frame = {
144      x: 29,
145      y: 69,
146      width: 330,
147      height: 430,
148    };
149    let list = [
150      {
151        frame: {
152          x: 42,
153          y: 42,
154          width: 143,
155          height: 430,
156        },
157        startNS: 200,
158        length: 32,
159        height: 2,
160      },
161    ];
162    expect(cpuRender.cpu(list, node, 1, 1, 1, frame, false)).toBeUndefined();
163  });
164
165  it('CPUTest08', () => {
166    let node = {
167      frame: {
168        x: 9,
169        y: 87,
170        width: 878,
171        height: 80,
172      },
173      startNS: 700,
174      length: 135,
175      height: 40,
176      startTime: 450,
177      dur: 9,
178    };
179    expect(CpuStruct.setCpuFrame(node, 1, 1, 1, { width: 10 })).toBeUndefined();
180  });
181
182  it('CPUTest09', () => {
183    let node = {
184      frame: {
185        x: 90,
186        y: 20,
187        width: 25,
188        height: 98,
189      },
190      startNS: 690,
191      length: 28,
192      height: 60,
193      startTime: 2,
194      dur: 221,
195    };
196    expect(CpuStruct.setCpuFrame(node, 1, 1, 1, { width: 10 })).toBeUndefined();
197  });
198
199  it('CPUTest10', function () {
200    let emptyRender = new EmptyRender();
201    let req = {
202      type: '',
203      startNS: 0,
204      endNS: 100,
205      totalNS: 100,
206      frame: {
207        x: 20,
208        y: 31,
209        width: 75,
210        height: 90,
211      },
212      canvas: 'abc',
213      context: {
214        measureText: jest.fn(() => true),
215        clearRect: jest.fn(() => true),
216        closePath: jest.fn(() => true),
217        fillRect: jest.fn(() => []),
218        fillText: jest.fn(() => true),
219        beginPath: jest.fn(() => true),
220        stroke: jest.fn(() => true),
221      },
222      lineColor: '#0084b3',
223      isHover: '',
224      hoverX: 1,
225      params: '',
226      wakeupBean: undefined,
227      flagMoveInfo: '',
228      flagSelectedInfo: '',
229      slicesTime: 121,
230      id: 231,
231      x: 40,
232      y: 40,
233      width: 140,
234      height: 104,
235    };
236    window.postMessage = jest.fn(() => true);
237    expect(emptyRender.render(req, [], [])).toBeUndefined();
238  });
239  it('CPUTest11', function () {
240    let emptyRender = new EmptyRender();
241    let canvas = document.createElement('canvas') as HTMLCanvasElement;
242    let context = canvas.getContext('2d');
243    const data = {
244      context: context!,
245      useCache: true,
246      type: '',
247      traceRange: [],
248    };
249    window.postMessage = jest.fn(() => true);
250    expect(emptyRender.renderMainThread(data, new TraceRow())).toBeUndefined();
251  });
252});
253