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 {
17  getTimeString,
18  TabPaneCurrentSelection,
19} from '../../../../../src/trace/component/trace/sheet/TabPaneCurrentSelection';
20const processSqlite = require('../../../../../src/trace/database/sql/ProcessThread.sql');
21jest.mock('../../../../../src/trace/database/sql/ProcessThread.sql');
22const sqlite = require('../../../../../src/trace/database/sql/SqlLite.sql');
23jest.mock('../../../../../src/trace/database/sql/SqlLite.sql');
24const gpuSqlite = require('../../../../../src/trace/database/sql/Gpu.sql');
25jest.mock('../../../../../src/trace/database/sql/Gpu.sql');
26
27describe('TabPaneCurrentSelection Test', () => {
28  let tabPaneCurrentSelection = new TabPaneCurrentSelection();
29  tabPaneCurrentSelection.setRealTime = jest.fn();
30  const canvas = document.createElement('canvas');
31  canvas.width = 1;
32  canvas.height = 1;
33  let context = canvas.getContext('2d');
34
35  let cpuData = [
36    {
37      cpu: 1,
38      dur: 1,
39      end_state: 'string',
40      id: 12,
41      name: 'name',
42      priority: 11,
43      processCmdLine: 'processCmdLine',
44      processId: 1111,
45      processName: 'processName',
46      schedId: 221,
47      startTime: 0,
48      tid: 1001,
49      type: 'type',
50    },
51  ];
52  let functionData = [
53    {
54      argsetid: 53161,
55      depth: 0,
56      dur: 570000,
57      funName: 'binder transaction',
58      id: 92749,
59      is_main_thread: 0,
60      parent_id: null,
61      startTs: 9729867000,
62      threadName: 'Thread-15',
63      tid: 2785,
64    },
65  ];
66  let memData = [
67    {
68      trackId: 100,
69      processName: 'processName',
70      pid: 11,
71      upid: 1,
72      trackName: 'trackName',
73      type: 'type',
74      track_id: 'track_id',
75      value: 111,
76      startTime: 0,
77      duration: 1000,
78      maxValue: 4000,
79      delta: 2,
80    },
81  ];
82  let threadData = [
83    {
84      hasSched: 14724852000,
85      pid: 2519,
86      processName: null,
87      threadName: 'ACCS0',
88      tid: 2716,
89      upid: 1,
90      utid: 1,
91      cpu: null,
92      dur: 405001,
93      end_ts: null,
94      id: 11,
95      is_main_thread: 0,
96      name: 'ACCS0',
97      startTime: 58001,
98      start_ts: null,
99      state: 'S',
100      type: 'thread',
101    },
102  ];
103  let wakeupBean = [
104    {
105      wakeupTime: 0,
106      cpu: 1,
107      process: 'process',
108      pid: 11,
109      thread: 'thread',
110      tid: 22,
111      schedulingLatency: 33,
112      schedulingDesc: 'schedulingDesc',
113    },
114  ];
115
116  let queryData = [
117    {
118      id: 1,
119      startTime: 0,
120      hasSched: 14724852000,
121      pid: 2519,
122      processName: null,
123      threadName: 'ACCS0',
124      tid: 2716,
125      upid: 1,
126      utid: 1,
127      cpu: null,
128      dur: 405002,
129      end_ts: null,
130      is_main_thread: 2,
131      name: 'ACCS0',
132      start_ts: null,
133      state: 'S',
134      type: 'thread',
135    },
136  ];
137  let scrollWakeUp = [
138    {
139      startTime: 0,
140      pid: 11,
141      tid: 22,
142    },
143  ];
144  let data = [
145    {
146      cpu: 1,
147      dur: 1,
148      end_state: 'string',
149      id: 12,
150      name: 'name',
151      priority: 11,
152      processCmdLine: 'processCmdLine',
153      processId: 1112,
154      processName: 'processName',
155      schedId: 222,
156      startTime: 0,
157      tid: 1002,
158      type: 'type',
159    },
160  ];
161
162  let jankData = {
163    id: 10,
164    ts: 25415,
165    dur: 1200,
166    name: '1523',
167    depth: 1,
168    jank_tag: true,
169    cmdline: 'com.test',
170    type: '0',
171    pid: 20,
172    frame_type: 'app',
173    app_dur: 110,
174    dst_slice: 488,
175  };
176
177  let jankDataRender = {
178    id: 22,
179    ts: 254152,
180    dur: 1202,
181    name: '1583',
182    depth: 1,
183    jank_tag: true,
184    cmdline: 'render.test',
185    type: '0',
186    pid: 22,
187    frame_type: 'render_service',
188    src_slice: '525',
189    rs_ts: 2562,
190    rs_vsync: '2562',
191    rs_dur: 1528,
192    rs_pid: 1252,
193    rs_name: 'name',
194    gpu_dur: 2568,
195  };
196
197  let irqData = [
198    {
199      id: 25,
200      startNS: 1526,
201      name: 'test',
202      dur: 125,
203      argSetId: 526,
204    },
205  ];
206
207  let clockData = [
208    {
209      filterId: 96,
210      value: 253,
211      startNS: 25852,
212      dur: 125,
213      delta: 2586,
214    },
215  ];
216
217  let functionDataTest = {
218    argsetid: 53161,
219    depth: 0,
220    dur: 570000,
221    funName: 'binder async',
222    id: 92749,
223    is_main_thread: 0,
224    parent_id: null,
225    startTs: 9729867000,
226    threadName: 'Thread-15',
227    tid: 2785,
228  };
229
230  tabPaneCurrentSelection.queryWakeUpData = jest.fn(() => 'WakeUpData');
231  tabPaneCurrentSelection.queryWakeUpData.wb = jest.fn(() => null);
232  tabPaneCurrentSelection.setCpuData(cpuData, undefined, 1);
233  let argsetTest = processSqlite.queryBinderArgsByArgset;
234  let argsetIdTest = sqlite.queryBinderByArgsId;
235  let argsetData = [
236    {
237      argset: 12,
238      keyName: 'test',
239      id: 123,
240      desc: 'desc',
241      strValue: 'value',
242    },
243    {
244      argset: 11,
245      keyName: 'test',
246      id: 113,
247      desc: 'desc',
248      strValue: 'value',
249    },
250  ];
251
252  let argsetIdData = [
253    {
254      type: 'func',
255      startTs: 1258,
256      dur: 25,
257      depth: 1,
258      argsetid: 258,
259    },
260  ];
261  argsetTest.mockResolvedValue(argsetData);
262  argsetIdTest.mockResolvedValue(argsetIdData);
263
264  let gpuDur = gpuSqlite.queryGpuDur;
265  let gpuDurData = [
266    {
267      gpu_dur: 1528,
268    },
269  ];
270  gpuDur.mockResolvedValue(gpuDurData);
271
272  let queryFlows = sqlite.queryFlowsData;
273  let queryFlowsData = [
274    {
275      name: '25962',
276      pid: 1885,
277      cmdline: 'render Test',
278      type: 1,
279    },
280  ];
281  queryFlows.mockResolvedValue(queryFlowsData);
282
283  let queryPreceding = sqlite.queryPrecedingData;
284  let queryPrecedingData = [
285    {
286      name: '2596562',
287      pid: 18854,
288      cmdline: 'app Test',
289      type: 0,
290    },
291  ];
292  queryPreceding.mockResolvedValue(queryPrecedingData);
293  tabPaneCurrentSelection.setMemData(memData)
294  tabPaneCurrentSelection.setCpuData(cpuData, undefined, 1);
295  tabPaneCurrentSelection.setFunctionData(functionData);
296  tabPaneCurrentSelection.setClockData(clockData);
297  tabPaneCurrentSelection.setFunctionData(functionDataTest);
298  it('TabPaneCurrentSelectionTest03', function () {
299    let result = getTimeString(3600_000_000_002);
300    expect(result).toBe('1h 2ns ');
301  });
302
303  it('TabPaneCurrentSelectionTest04', function () {
304    let result = getTimeString(60000000001);
305    expect(result).toBe('1m 1ns ');
306  });
307
308  it('TabPaneCurrentSelectionTest05', function () {
309    let result = getTimeString(1000000001);
310    expect(result).toBe('1s 1ns ');
311  });
312
313  it('TabPaneCurrentSelectionTest06', function () {
314    let result = getTimeString(1000001);
315    expect(result).toBe('1ms 1ns ');
316  });
317
318  it('TabPaneCurrentSelectionTest07', function () {
319    let result = getTimeString(1001);
320    expect(result).toBe('1μs 1ns ');
321  });
322
323  it('TabPaneCurrentSelectionTest08', function () {
324    let result = getTimeString(101);
325    expect(result).toBe('101ns ');
326  });
327
328  it('TabPaneCurrentSelectionTest09', function () {
329    tabPaneCurrentSelection.setCpuData = jest.fn(() => true);
330    tabPaneCurrentSelection.data = jest.fn(() => true);
331    expect(tabPaneCurrentSelection.data).toBeUndefined();
332  });
333
334  it('TabPaneCurrentSelectionTest10', function () {
335    expect(tabPaneCurrentSelection.setCpuData(cpuData, undefined, 1)).toBeTruthy();
336  });
337
338  it('TabPaneCurrentSelectionTest13', function () {
339    expect(tabPaneCurrentSelection.initCanvas()).not.toBeUndefined();
340  });
341
342  it('TabPaneCurrentSelectionTest14', function () {
343    let str = {
344      length: 0,
345    };
346    expect(tabPaneCurrentSelection.transferString(str)).toBe('');
347  });
348
349  it('TabPaneCurrentSelectionTest16', function () {
350    expect(tabPaneCurrentSelection.drawRight(null)).toBeUndefined();
351  });
352
353  it('TabPaneCurrentSelectionTest23', function () {
354    let result = tabPaneCurrentSelection.setJankData(jankData, undefined, 1);
355    expect(result).toBeUndefined();
356  });
357
358  it('TabPaneCurrentSelectionTest25', function () {
359    let result = tabPaneCurrentSelection.setJankData(jankDataRender, undefined, 1);
360    expect(result).toBeUndefined();
361  });
362
363  it('TabPaneCurrentSelectionTest24', function () {
364    let result = tabPaneCurrentSelection.setIrqData(irqData);
365    expect(result).toBeUndefined();
366  });
367
368  it('TabPaneCurrentSelectionTest18', function () {
369    let result = tabPaneCurrentSelection.setStartupData(irqData, 1, []);
370    expect(result).toBeUndefined();
371  });
372  it('TabPaneCurrentSelectionTest19', function () {
373    let result = tabPaneCurrentSelection.setStaticInitData(irqData, 1);
374    expect(result).toBeUndefined();
375  });
376  it('TabPaneCurrentSelectionTest20', function () {
377    let list: never[] = [];
378    let data = [
379      {
380        jank_tag: 1,
381        frame_type: 'render_service',
382      },
383    ];
384    let result = tabPaneCurrentSelection.setJankType(data, list);
385    expect(result).toBeUndefined();
386  });
387  it('TabPaneCurrentSelectionTest21', function () {
388    let data = [{
389      startTime:22,
390    }]
391    let result = tabPaneCurrentSelection.setFrameAnimationData(data)
392    expect(result).toBeTruthy();
393  });
394  it('TabPaneCurrentSelectionTest22', function () {
395    let data = [{}]
396    let result = tabPaneCurrentSelection.queryCPUWakeUpFromData(data)
397    expect(result).toBeTruthy();
398  });
399});
400