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 { SpHiSysEnergyChart } from '../../../../src/trace/component/chart/SpHiSysEnergyChart';
17import { LitPopover } from '../../../../src/base-ui/popover/LitPopoverV';
18jest.mock('../../../../src/trace/component/SpSystemTrace', () => {
19  return {};
20});
21jest.mock('../../../../src/js-heap/model/DatabaseStruct', () => {
22  return {};
23});
24jest.mock('../../../../src/trace/database/ui-worker/ProcedureWorkerSnapshot', () => {
25  return {};
26});
27jest.mock('../../../../src/trace/database/ui-worker/ProcedureWorker', () => {
28  return {};
29});
30const intersectionObserverMock = () => ({
31  observe: () => null,
32});
33window.IntersectionObserver = jest.fn().mockImplementation(intersectionObserverMock);
34window.ResizeObserver =
35  window.ResizeObserver ||
36  jest.fn().mockImplementation(() => ({
37    disconnect: jest.fn(),
38    observe: jest.fn(),
39    unobserve: jest.fn(),
40  }));
41
42const sqlite = require('../../../../src/trace/database/sql/SqlLite.sql');
43jest.mock('../../../../src/trace/database/sql/SqlLite.sql');
44const processSqlite = require('../../../../src/trace/database/sql/ProcessThread.sql');
45jest.mock('../../../../src/trace/database/sql/ProcessThread.sql');
46describe('SpHiSysEnergyChart Test', () => {
47  let htmlElement: any = document.createElement('sp-system-trace');
48  let spHiSysEnergyChart = new SpHiSysEnergyChart(htmlElement);
49
50  let htmlDivElement = document.createElement<LitPopover>('div');
51  htmlDivElement.setAttribute('id', 'appNameList');
52
53  let anomalyData = sqlite.queryAnomalyData;
54  anomalyData.mockResolvedValue([]);
55
56  let maxStateValue = sqlite.queryMaxStateValue;
57  let max = [
58    {
59      maxValue: 200,
60      type: 'state',
61    },
62    {
63      maxValue: 300,
64      type: 'sensor',
65    },
66  ];
67  maxStateValue.mockResolvedValue(max);
68
69  let stateInitData = sqlite.queryStateInitValue;
70  let stateInitInit = [{
71    eventName: '',
72    keyName: '',
73  }];
74  stateInitData.mockResolvedValue(stateInitInit);
75
76  let MockExits = sqlite.queryEnergyEventExits;
77  MockExits.mockResolvedValue(['trace_hisys_event']);
78  let powerData = processSqlite.queryPowerData;
79  let power = [
80    {
81      startNS: 5999127351,
82      eventName: 'POWER_IDE_AUDIO',
83      appKey: 'APPNAME',
84      eventValue: 'com.example.himusicdemo,com.example.himusicdemo_js,com.example.himusicdemo_app',
85    },
86    {
87      startNS: 1291120516,
88      eventName: 'POWER_IDE_AUDIO',
89      appKey: 'BACKGROUND_ENERGY',
90      eventValue: '854,258,141',
91    },
92  ];
93  powerData.mockResolvedValue(power);
94
95  let stateData = sqlite.queryStateData;
96  stateData.mockResolvedValue([]);
97
98  let sysEventAppName = sqlite.queryEnergyAppName;
99  let appName = [
100    {
101      string_value: 'app_name',
102    },
103  ];
104  sysEventAppName.mockResolvedValue(appName);
105
106
107  let querySystemLocationData = sqlite.querySystemLocationData;
108  let querySystemLockData = sqlite.querySystemLockData;
109  let querySystemSchedulerData = sqlite.querySystemSchedulerData;
110  let queryConfigSysEventAppName = sqlite.queryConfigEnergyAppName;
111  let location = [
112    {
113      ts: 100652222,
114      eventName: 'GNSS_STATE',
115      appKey: 'TYPE',
116      Value: '1',
117    },
118    {
119      ts: 3333332224,
120      eventName: 'GNSS_STATE',
121      appKey: 'TAG',
122      Value: '2',
123    },
124  ];
125
126  let lock = [
127    {
128      ts: 96555551,
129      eventName: 'POWER_RUNNINGLOCK',
130      appKey: 'TYPE',
131      Value: '1',
132    },
133    {
134      ts: 333234222,
135      eventName: 'POWER_RUNNINGLOCK',
136      appKey: 'TAG',
137      Value: '2',
138    },
139  ];
140
141  let work = [
142    {
143      ts: 100593835619,
144      eventName: 'WORK_ADD',
145      appKey: 'TYPE',
146      Value: '1',
147    },
148    {
149      ts: 2315652241,
150      eventName: 'WORK_STOP',
151      appKey: 'TAG',
152      Value: '2',
153    },
154  ];
155
156  let process = [
157    {
158      process_name: 'process1',
159    },
160  ];
161  querySystemLocationData.mockResolvedValue(location);
162  querySystemLockData.mockResolvedValue(lock);
163  querySystemSchedulerData.mockResolvedValue(work);
164  queryConfigSysEventAppName.mockResolvedValue(process);
165
166  it('SpHiSysEnergyChartTest01', function () {
167    spHiSysEnergyChart.init();
168    expect(SpHiSysEnergyChart.app_name).toBeUndefined();
169  });
170
171  it('SpHiSysEnergyChartTest04', function () {
172    let result = [
173      {
174        startNS: 33255112,
175        eventName: 'POWER_IDE_AUDIO',
176        appKey: 'APPNAME',
177        eventValue: 'com.example.himusicdemo,com.example.himusicdemo_js,com.example.himusicdemo_app',
178      },
179      {
180        startNS: 5999127352,
181        eventName: 'POWER_IDE_AUDIO',
182        appKey: 'BACKGROUND_ENERGY',
183        eventValue: '854,258,141',
184      },
185      {
186        startNS: 223224352,
187        eventName: 'POWER_IDE_BLUETOOTH',
188        appKey: 'APPNAME',
189        eventValue: 'com.ohos.settings,bt_switch,bt_switch_js,bt_switch_app',
190      },
191      {
192        startNS: 86222222,
193        eventName: 'POWER_IDE_BLUETOOTH',
194        appKey: 'BACKGROUND_ENERGY',
195        eventValue: '76,12,43,431',
196      },
197      {
198        startNS: 5999127382,
199        eventName: 'POWER_IDE_CAMERA',
200        appKey: 'APPNAME',
201        eventValue: 'com.ohos.camera,com.ohos.camera_app,com.ohos.camera_js,com.ohos.camera_ts',
202      },
203      {
204        startNS: 264166822,
205        eventName: 'POWER_IDE_CAMERA',
206        appKey: 'BACKGROUND_ENERGY',
207        eventValue: '375,475,255,963',
208      },
209    ];
210    expect(spHiSysEnergyChart.getPowerData(result)).toBeTruthy();
211  });
212
213  it('SpHiSysEnergyChartTest05', function () {
214    expect(spHiSysEnergyChart.getPowerData([])).toBeTruthy();
215  });
216
217  it('SpHiSysEnergyChartTest06', function () {
218    expect(htmlDivElement.onclick).toBe(null);
219  });
220});
221