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  CpuFreqLimitRender,
23  CpuFreqLimitsStruct,
24} from '../../../../src/trace/database/ui-worker/cpu/ProcedureWorkerCpuFreqLimits';
25import { dataFilterHandler } from '../../../../src/trace/database/ui-worker/ProcedureWorkerCommon';
26
27jest.mock('../../../../src/trace/component/SpSystemTrace', () => {
28  return {};
29});
30describe('ProcedureWorkerCpuFreqLimits Test', () => {
31  let cpuFreqLimits = {
32    frame: {
33      x: 20,
34      y: 20,
35      width: 100,
36      height: 100,
37    },
38    startNs: 255,
39    dur: 2545,
40    max: 14111,
41    min: 200,
42    cpu: 10,
43  };
44  it('Test01', () => {
45    const canvas = document.createElement('canvas');
46    canvas.width = 15;
47    canvas.height = 15;
48    const ctx = canvas.getContext('2d');
49
50    const data = {
51      frame: {
52        x: 205,
53        y: 205,
54        width: 100,
55        height: 100,
56      },
57      startNs: 54,
58      dur: 2453,
59      max: 3433,
60      min: 13,
61      cpu: 3,
62    };
63    expect(CpuFreqLimitsStruct.draw(ctx!, data, 2)).toBeUndefined();
64  });
65
66  it('Test02', () => {
67    const canvas = document.createElement('canvas');
68    canvas.width = 1;
69    canvas.height = 1;
70    const ctx = canvas.getContext('2d');
71    expect(CpuFreqLimitsStruct.drawArcLine(ctx, cpuFreqLimits, 100, 500)).toBeUndefined();
72  });
73
74  it('Test03', () => {
75    let node = {
76      frame: {
77        x: 24,
78        y: 20,
79        width: 100,
80        height: 150,
81      },
82      startNS: 200,
83      length: 1,
84      height: 40,
85      startTime: 2,
86      dur: 41,
87    };
88    expect(
89      CpuFreqLimitsStruct.setFreqLimitFrame(node, 1, 1, 1, 1, {
90        width: 10,
91      })
92    ).toBeUndefined();
93  });
94
95  it('Test04', function () {
96    let cpuFreqLimitRender = new CpuFreqLimitRender();
97    let req = [{
98      type: '',
99      startNS: 10,
100      endNS: 101,
101      totalNS: 91,
102      frame: {
103        x: 43,
104        y: 230,
105        width: 340,
106        height: 342,
107      },
108      canvas: 'a',
109      context: {
110        measureText: jest.fn(() => true),
111        clearRect: jest.fn(() => true),
112        stroke: jest.fn(() => false),
113        closePath: jest.fn(() => true),
114        fillText: jest.fn(() => true),
115        beginPath: jest.fn(() => true),
116        fillRect: jest.fn(() => true),
117      },
118      lineColor: '#ffae8a',
119      isHover: '',
120      hoverX: 1,
121      params: '',
122      wakeupBean: undefined,
123      flagMoveInfo: '',
124      flagSelectedInfo: '',
125      slicesTime: 133,
126      id: 36,
127      x: 565,
128      y: 600,
129      width: 100,
130      height: 230,
131    }];
132    window.postMessage = jest.fn(() => true);
133    let frame = {
134      x: 20,
135      y: 20,
136      width: 100,
137      height: 100,
138    };
139    expect(dataFilterHandler(req, [{length: 0}], {
140      startKey: 'startNS',
141      durKey: 'dur',
142      startNS: TraceRow.range?.startNS ?? 0,
143      endNS: TraceRow.range?.endNS ?? 0,
144      totalNS: TraceRow.range?.totalNS ?? 0,
145      frame: frame,
146      paddingTop: 5,
147      useCache: false,
148    })).toBeUndefined();
149  });
150  it('Test05', function () {
151    let cpuFreqLimitRender = new CpuFreqLimitRender();
152    let canvas = document.createElement('canvas') as HTMLCanvasElement;
153    let context = canvas.getContext('2d');
154    const data = {
155      context: context!,
156      useCache: true,
157      type: '',
158      traceRange: [],
159    };
160    window.postMessage = jest.fn(() => true);
161    expect(cpuFreqLimitRender.renderMainThread(data, new TraceRow())).toBeUndefined();
162  });
163});
164