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 { SpFrameTimeChart } from '../../../../src/trace/component/chart/SpFrameTimeChart';
17jest.mock('../../../../src/trace/component/SpSystemTrace', () => {
18  return {};
19});
20import { TraceRow } from '../../../../src/trace/component/trace/base/TraceRow';
21import { FlagsConfig } from '../../../../src/trace/component/SpFlags';
22
23const intersectionObserverMock = () => ({
24  observe: () => null,
25});
26window.IntersectionObserver = jest.fn().mockImplementation(intersectionObserverMock);
27jest.mock('../../../../src/js-heap/model/DatabaseStruct');
28const sqlite = require('../../../../src/trace/database/sql/SqlLite.sql');
29jest.mock('../../../../src/trace/database/sql/SqlLite.sql');
30jest.mock('../../../../src/trace/database/ui-worker/ProcedureWorker', () => {
31  return {};
32});
33const jankSqlite = require('../../../../src/trace/database/sql/Janks.sql');
34jest.mock('../../../../src/trace/database/sql/Janks.sql');
35const processSqlite = require('../../../../src/trace/database/sql/ProcessThread.sql');
36jest.mock('../../../../src/trace/database/sql/ProcessThread.sql');
37jest.mock('../../../../src/trace/database/ui-worker/ProcedureWorkerSnapshot', () => {
38  return {};
39});
40window.ResizeObserver =
41  window.ResizeObserver ||
42  jest.fn().mockImplementation(() => ({
43    disconnect: jest.fn(),
44    observe: jest.fn(),
45    unobserve: jest.fn(),
46  }));
47
48describe('SpFrameTimeChart Test', () => {
49  let htmlElement: any = document.createElement('sp-system-trace');
50  let spFrameTimeChart = new SpFrameTimeChart(htmlElement);
51  let queryFrameTime = sqlite.queryFrameTimeData;
52  let queryFrameTimeData = [
53    {
54      pid: 256,
55    },
56  ];
57  queryFrameTime.mockResolvedValue(queryFrameTimeData);
58
59  let queryExpectedFrame = jankSqlite.queryExpectedFrameDate;
60  let queryExpectedFrameDate = [
61    {
62      dur: 2585,
63      depth: 1,
64    },
65    {
66      dur: 6688,
67      depth: 1,
68    },
69  ];
70  queryExpectedFrame.mockResolvedValue(queryExpectedFrameDate);
71
72  let queryActualFrame = jankSqlite.queryActualFrameDate;
73  let queryActualFrameDate = [
74    {
75      dur: 6878,
76      depth: 1,
77    },
78    {
79      dur: 6238,
80      depth: 1,
81    },
82  ];
83  queryActualFrame.mockResolvedValue(queryActualFrameDate);
84
85  let frameApp = sqlite.queryFrameApp;
86  let frameAppData = [
87    {
88      appName: 'test0',
89    },
90    {
91      appName: 'test1',
92    },
93    {
94      appName: 'test2',
95    },
96  ];
97  frameApp.mockResolvedValue(frameAppData);
98
99  let frameAnimation = sqlite.queryFrameAnimationData;
100  let frameAnimationData = [
101    {animationId: 1, dynamicEndTs: 4774481414, dynamicStartTs: 4091445476, ts: 4091445476},
102    {
103      animationId: 2,
104      dynamicEndTs: 8325095997,
105      dynamicStartTs: 7652588184,
106      ts: 7652588184,
107    },
108  ];
109  frameAnimation.mockResolvedValue(frameAnimationData);
110
111  let allProcessNames = processSqlite.queryAllProcessNames;
112  let allProcessNameData = [
113    {
114      id: 12,
115      name: 'test name',
116      pid: 255
117    }
118  ];
119  allProcessNames.mockResolvedValue(allProcessNameData);
120
121  let dynamicIdAndName = sqlite.queryDynamicIdAndNameData;
122  let data = [
123    {
124      id: 12,
125      appName: 'name'
126    }
127  ];
128  dynamicIdAndName.mockResolvedValue(data);
129
130  let animationTimeRange = sqlite.queryAnimationTimeRangeData;
131  let rangeData = [
132    {
133      status: 'Response delay',
134      startTs: 225,
135      endTs: 6355
136    }
137  ];
138  animationTimeRange.mockResolvedValue(rangeData);
139
140
141  let animationIdAndName = sqlite.queryAnimationIdAndNameData;
142  let animationIdAndNameData = [
143    {
144      id: 12,
145      name: 'test',
146      info: '{}'
147    }
148  ];
149  animationIdAndName.mockResolvedValue(animationIdAndNameData);
150
151  let frameDynamic = sqlite.queryFrameDynamicData;
152  let frameDynamicData = [
153    {alpha: '1.00', appName: 'test0', height: 2772, id: 74, ts: 28565790, width: 1344, x: 0, y: 0},
154    {
155      alpha: '1.00',
156      appName: 'test0',
157      height: 2772,
158      id: 75,
159      ts: 42341310,
160      width: 1344,
161      x: 0,
162      y: 0,
163    },
164  ];
165  frameDynamic.mockResolvedValue(frameDynamicData);
166
167  let frameSpacing = sqlite.queryFrameSpacing;
168  let frameSpacingData = [
169    {
170      currentFrameHeight: 2768,
171      currentFrameWidth: 1344,
172      currentTs: 17535295995,
173      frameSpacingResult: 0.1,
174      id: 1216,
175      nameId: 'test0',
176      preFrameHeight: 2767,
177      preFrameWidth: 1343,
178      preTs: 17523356412,
179      x: 0,
180      y: 1,
181    },
182    {
183      currentFrameHeight: 2768,
184      currentFrameWidth: 1344,
185      currentTs: 17546478287,
186      frameSpacingResult: 0,
187      id: 1217,
188      nameId: 'test0',
189      preFrameHeight: 2768,
190      preFrameWidth: 1344,
191      preTs: 17535295995,
192      x: 0,
193      y: 1,
194    },
195  ];
196  frameSpacing.mockResolvedValue(frameSpacingData);
197
198  let physical = sqlite.queryPhysicalData;
199  let physicalData = [{physicalFrameRate: 90, physicalHeight: 2772, physicalWidth: 1344}];
200  physical.mockResolvedValue(physicalData);
201
202  it('TabPaneFramesTest01', function () {
203    expect(spFrameTimeChart.init()).toBeTruthy();
204  });
205
206  it('TabPaneFramesTest02', function () {
207    FlagsConfig.updateFlagsConfig('AnimationAnalysis', 'Enabled');
208    spFrameTimeChart.initAnimatedScenesChart(
209      TraceRow.skeleton(),
210      {
211        pid: 1,
212        processName: 'render_service',
213      },
214      TraceRow.skeleton()
215    );
216  });
217  it('TabPaneFramesTest03', function () {
218    expect(spFrameTimeChart.frameNoExpandTimeOut()).toBeTruthy();
219  });
220  it('TabPaneFramesTest04', function () {
221    expect(spFrameTimeChart.frameExpandTimeOut()).toBeTruthy();
222  });
223});
224