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 */
15import { TraceRow } from '../../../../src/trace/component/trace/base/TraceRow';
16import { Rect } from '../../../../src/trace/component/trace/timer-shaft/Rect';
17import { soDataFilter, SoRender, SoStruct } from '../../../../src/trace/database/ui-worker/ProcedureWorkerSoInit';
18
19jest.mock('../../../../src/trace/component/SpSystemTrace', () => {
20  return {};
21});
22jest.mock('../../../../src/trace/database/ui-worker/ProcedureWorker', () => {
23  return {};
24});
25describe('ProcedureWorkerSoInit Test', () => {
26  it('soDataFilterTest', () => {
27    const canvas = document.createElement('canvas');
28    canvas.width = 1;
29    canvas.height = 1;
30    const ctx = canvas.getContext('2d');
31    let rect = new Rect(0, 10, 10, 10);
32    let filter = [
33      {
34        startTs: 520,
35        dur: 15400,
36        soName: 'Snapshot0',
37        tid: 0,
38        pid: 21,
39        depth: 5,
40        itid: 42,
41        textMetricsWidth: 52.875,
42        process: ''
43      },
44    ];
45    let list = [
46      {
47        startTs: 32,
48        dur: 1320000,
49        soName: 'Snapshot1',
50        tid: 120,
51        pid: 213,
52        depth: 21,
53        itid: 22,
54        textMetricsWidth: 54.6875,
55        process: ''
56      },
57    ];
58    soDataFilter(list, filter, 100254, 100254, rect, {height: 40, width: 1407, x: 0, y: 0}, true);
59  });
60
61  it('SoStructTest01', () => {
62    const data = {
63      frame: {
64        x: 432,
65        y: 222,
66        width: 340,
67        height: 100,
68      },
69      startTs: 50,
70      dur: 1544000,
71      soName: 'Snapshot0',
72      tid: 0,
73      pid: 4243,
74      depth: 6,
75      itid: 2,
76      textMetricsWidth: 55.75,
77      process: ''
78    };
79    const canvas = document.createElement('canvas');
80    canvas.width = 1;
81    canvas.height = 1;
82    const ctx = canvas.getContext('2d');
83    expect(SoStruct.draw(ctx, data)).toBeUndefined();
84  });
85
86  it('SoStructTest02', () => {
87    const data = {
88      frame: {
89        x: 20,
90        y: 43,
91        width: 120,
92        height: 100,
93      },
94      startTs: 50,
95      dur: 152500,
96      soName: 'Snapshot1',
97      tid: 240,
98      pid: 45,
99      depth: 35,
100      itid: 2,
101      textMetricsWidth: 66.650546875,
102      process: ''
103    };
104    let node = {
105      frame: {
106        x: 20,
107        y: 90,
108        width: 100,
109        height: 500,
110      },
111      startTs: 3200,
112      dur: 42000,
113      soName: 'Snapshot2',
114      tid: 240,
115      pid: 210,
116      depth: 10,
117      itid: 2,
118      textMetricsWidth: 96.2646875,
119      process: ''
120    };
121    expect(SoStruct.setSoFrame(node, 2, 0, 1, 2, data)).toBeUndefined();
122  });
123  it('SoStructTest03', () => {
124    expect(SoStruct).not.toBeUndefined();
125  });
126  it('SoStructTest04 ', function () {
127    let soRender = new SoRender();
128    let req = {
129      lazyRefresh: true,
130      type: '',
131      startNS: 5,
132      endNS: 9,
133      totalNS: 4,
134      frame: {
135        x: 32,
136        y: 20,
137        width: 180,
138        height: 180,
139      },
140      useCache: true,
141      range: {
142        refresh: '',
143      },
144      canvas: 'a',
145      context: {
146        font: '12px sans-serif',
147        fillStyle: '#a1697d',
148        globalAlpha: 0.3,
149        measureText: jest.fn(() => true),
150        clearRect: jest.fn(() => true),
151        stroke: jest.fn(() => true),
152        closePath: jest.fn(() => false),
153        beginPath: jest.fn(() => true),
154        fillRect: jest.fn(() => false),
155        fillText: jest.fn(() => true),
156      },
157      lineColor: '',
158      isHover: 'true',
159      hoverX: 0,
160      params: '',
161      wakeupBean: undefined,
162      flagMoveInfo: '',
163      flagSelectedInfo: '',
164      slicesTime: 4,
165      id: 1,
166      x: 24,
167      y: 24,
168      width: 100,
169      height: 100,
170    };
171    window.postMessage = jest.fn(() => true);
172    TraceRow.range = jest.fn(() => true);
173    TraceRow.range.startNS = jest.fn(() => 1);
174    expect(soRender.renderMainThread(req,new TraceRow<SoStruct>()));
175  });
176});
177