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
18jest.mock('../../../../src/trace/database/ui-worker/ProcedureWorker', () => {
19  return {};
20});
21import {
22  CpuAbilityMonitorStruct,
23  CpuAbilityRender,
24} from '../../../../src/trace/database/ui-worker/ProcedureWorkerCpuAbility';
25import { dataFilterHandler } from '../../../../src/trace/database/ui-worker/ProcedureWorkerCommon';
26
27jest.mock('../../../../src/trace/component/SpSystemTrace', () => {
28  return {};
29});
30describe('CpuAbilityMonitorStruct Test', () => {
31  const canvas = document.createElement('canvas');
32  canvas.width = 14;
33  canvas.height = 11;
34  const ctx = canvas.getContext('2d');
35
36  const data = {
37    frame: {
38      x: 201,
39      y: 204,
40      width: 100,
41      height: 100,
42    },
43    startNS: 200,
44    value: 50,
45  };
46  let frame = {
47    x: 20,
48    y: 20,
49    width: 100,
50    height: 100,
51  };
52
53  const Sourcedata = {
54    frame: {
55      x: 20,
56      y: 20,
57      width: 100,
58      height: 100,
59    },
60    maxCpuUtilization: 200,
61    value: 50,
62  };
63  it('CpuAbilityMonitorStructTest01', function () {
64    expect(CpuAbilityMonitorStruct.draw(ctx, data)).toBeUndefined();
65  });
66  it('CpuAbilityMonitorStructTest03', function () {
67    expect(CpuAbilityMonitorStruct.draw(ctx, Sourcedata)).toBeUndefined();
68  });
69
70  it('CpuAbilityMonitorStructTest05', function () {
71    let dataList = new Array();
72    dataList.push({
73      startNs: 0,
74      dur: 10,
75      frame: {x: 0, y: 9, width: 10, height: 10},
76    });
77    dataList.push({startNs: 1, dur: 111});
78    dataFilterHandler(dataList, [{length: 0}], {
79      startKey: 'startNS',
80      durKey: 'dur',
81      startNS: TraceRow.range?.startNS ?? 0,
82      endNS: TraceRow.range?.endNS ?? 0,
83      totalNS: TraceRow.range?.totalNS ?? 0,
84      frame: frame,
85      paddingTop: 5,
86      useCache: false,
87    });
88  });
89
90  it('CpuAbilityMonitorStructTest06', function () {
91    let cpuAbilityRender = new CpuAbilityRender();
92    let cpuAbilityReq = {
93      lazyRefresh: true,
94      type: '',
95      startNS: 2,
96      endNS: 3,
97      totalNS: 1,
98      frame: {
99        x: 11,
100        y: 11,
101        width: 90,
102        height: 90,
103      },
104      useCache: false,
105      range: {
106        refresh: 'refresh',
107      },
108      canvas: '',
109      context: {
110        font: '11px sans-serif',
111        fillStyle: '#ec407a',
112        globalAlpha: 0.7,
113      },
114      lineColor: '#ec407a',
115      isHover: '',
116      hoverX: 0,
117      params: '',
118      wakeupBean: undefined,
119      flagMoveInfo: '',
120      flagSelectedInfo: 'k',
121      slicesTime: 1,
122      id: 1,
123      x: 12,
124      y: 12,
125      width: 102,
126      height: 102,
127    };
128    window.postMessage = jest.fn(() => true);
129    expect(cpuAbilityRender.render(cpuAbilityReq, [], [])).toBeUndefined();
130  });
131  it('CpuAbilityMonitorStructTest07', function () {
132    let cpuAbilityRender = new CpuAbilityRender();
133    let canvas = document.createElement('canvas') as HTMLCanvasElement;
134    let context = canvas.getContext('2d');
135    const data = {
136      context: context!,
137      useCache: true,
138      type: '',
139      traceRange: [],
140    };
141    window.postMessage = jest.fn(() => true);
142    expect(cpuAbilityRender.renderMainThread(data, new TraceRow())).toBeUndefined();
143  });
144  it('CpuAbilityMonitorStructTest08 ', function () {
145    let cpuAbilityNode = {
146      frame: {
147        x: 9,
148        y: 87,
149        width: 878,
150        height: 80,
151      },
152      startNS: 700,
153      length: 135,
154      height: 40,
155      startTime: 450,
156      dur: 9,
157    };
158    let frame = {
159      x: 2,
160      y: 4,
161      width: 87,
162      height: 80,
163    };
164    expect(CpuAbilityMonitorStruct.setCpuAbilityFrame(cpuAbilityNode, 1, 1, 1, 1, frame)).toBeUndefined();
165  });
166});
167