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'; 17import { Rect } from '../../../../src/trace/database/ui-worker/ProcedureWorkerCommon'; 18import { 19 XpowerStatisticStruct, 20 XpowerStatisticRender, 21 drawLegend, 22} from '../../../../src/trace/database/ui-worker/ProcedureWorkerXpowerStatistic'; 23jest.mock('../../../../src/js-heap/model/DatabaseStruct', () => { 24 return {}; 25}); 26jest.mock('../../../../src/trace/database/ui-worker/ProcedureWorkerSnapshot', () => { 27 return {}; 28}); 29jest.mock('../../../../src/trace/database/ui-worker/ProcedureWorker', () => { 30 return {}; 31}); 32jest 33 .spyOn(require('../../../../src/trace/database/ui-worker/ProcedureWorkerCommon'), 'isFrameContainPoint') 34 .mockReturnValue(true); 35 36describe('ProcedureWorkerXpowerStatistic Test', () => { 37 let row: TraceRow<XpowerStatisticStruct>; 38 let context: CanvasRenderingContext2D; 39 40 let list = [ 41 'audio', 42 'bluetooth', 43 'camera', 44 'cpu', 45 'display', 46 'flashlight', 47 'gpu', 48 'location', 49 'wifiscan', 50 'wifi', 51 'modem', 52 ]; 53 row = TraceRow.skeleton<XpowerStatisticStruct>(); 54 TraceRow.range = { 55 startNS: 0, 56 endNS: 1000000, 57 totalNS: 1000000, 58 }; 59 60 row.dataListCache = [ 61 { 62 startTime: 100, 63 audio: 0, 64 bluetooth: 10, 65 camera: 100, 66 cpu: 0, 67 display: 0, 68 flashlight: 0, 69 gpu: 0, 70 location: 0, 71 wifiscan: 0, 72 wifi: 0, 73 modem: 0, 74 audioDur: 0, 75 bluetoothDur: 0, 76 cameraDur: 0, 77 cpuDur: 0, 78 displayDur: 0, 79 flashlightDur: 0, 80 gpuDur: 0, 81 locationDur: 0, 82 wifiscanDur: 0, 83 wifiDur: 0, 84 modemDur: 0, 85 type: 557, 86 typeStr: 'bluetooth', 87 dur: 100, 88 energy: 80, 89 }, 90 { 91 startTime: 100, 92 audio: 0, 93 bluetooth: 10, 94 camera: 100, 95 cpu: 0, 96 display: 0, 97 flashlight: 0, 98 gpu: 50, 99 location: 0, 100 wifiscan: 70, 101 wifi: 0, 102 modem: 0, 103 audioDur: 0, 104 bluetoothDur: 0, 105 cameraDur: 0, 106 cpuDur: 0, 107 displayDur: 40, 108 flashlightDur: 10, 109 gpuDur: 0, 110 locationDur: 0, 111 wifiscanDur: 0, 112 wifiDur: 0, 113 modemDur: 0, 114 type: 557, 115 typeStr: 'gpu', 116 dur: 100, 117 energy: 80, 118 }, 119 { 120 startTime: 100, 121 type: 557, 122 typeStr: 'audio', 123 dur: 100, 124 energy: 80, 125 }, 126 { 127 startTime: 100, 128 type: 557, 129 typeStr: 'camera', 130 dur: 100, 131 energy: 80, 132 }, 133 { 134 startTime: 100, 135 type: 557, 136 typeStr: 'cpu', 137 dur: 100, 138 energy: 80, 139 }, 140 { 141 startTime: 100, 142 type: 557, 143 typeStr: 'display', 144 dur: 100, 145 energy: 80, 146 }, 147 { 148 startTime: 100, 149 type: 557, 150 typeStr: 'flashlight', 151 dur: 100, 152 energy: 80, 153 }, 154 { 155 startTime: 100, 156 type: 557, 157 typeStr: 'location', 158 dur: 100, 159 energy: 80, 160 }, 161 { 162 startTime: 100, 163 type: 557, 164 typeStr: 'wifiscan', 165 dur: 100, 166 energy: 80, 167 }, 168 { 169 startTime: 100, 170 type: 557, 171 typeStr: 'wifi', 172 dur: 100, 173 energy: 80, 174 }, 175 { 176 startTime: 100, 177 type: 557, 178 typeStr: 'modem', 179 dur: 100, 180 energy: 80, 181 }, 182 ]; 183 row.rowSettingCheckBoxList = list; 184 row.addRowSettingCheckBox(); 185 186 context = { 187 save: jest.fn(), 188 translate: jest.fn(), 189 rect: jest.fn(), 190 clip: jest.fn(), 191 strokeStyle: '#000000', 192 strokeRect: jest.fn(), 193 fillRect: jest.fn(), 194 fillText: jest.fn(), 195 measureText: jest.fn().mockReturnValue({ width: 10 }), 196 beginPath: jest.fn(), 197 closePath: jest.fn(), 198 stroke: jest.fn(), 199 clearRect: jest.fn(), 200 fill: jest.fn(), 201 fillStyle: '#000000', 202 globalAlpha: 1, 203 font: '10px sans-serif', 204 canvas: { 205 clientWidth: 800, 206 }, 207 } as unknown as CanvasRenderingContext2D; 208 209 it('ProcedureWorkerXpowerStatistic01', () => { 210 const xpowerStatisticRender = new XpowerStatisticRender(); 211 xpowerStatisticRender.renderMainThread({ context, useCache: false }, row); 212 expect(context.beginPath).toHaveBeenCalled(); 213 expect(context.closePath).toHaveBeenCalled(); 214 }); 215 216 it('ProcedureWorkerXpowerStatistic02', () => { 217 const xpowerStatisticRender = new XpowerStatisticRender(); 218 expect(xpowerStatisticRender.renderMainThread({ context, useCache: false }, row)).toBeUndefined(); 219 }); 220 221 it('ProcedureWorkerXpowerStatistic03', () => { 222 row.isHover = true; 223 row.hoverX = 50; 224 row.hoverY = 50; 225 row.dataListCache[0].frame = new Rect(0, 0, 100, 100); 226 const xpowerStatisticRender = new XpowerStatisticRender(); 227 xpowerStatisticRender.renderMainThread({ context, useCache: false }, row); 228 expect(XpowerStatisticStruct.hoverXpowerStruct).toBeDefined(); 229 expect(XpowerStatisticStruct.hoverXpowerStruct!.startTime).toBe(100); 230 }); 231 232 it('ProcedureWorkerXpowerStatistic04', () => { 233 let checked = new Array(11).fill(true); 234 drawLegend({ context, useCache: false }, checked, list, false); 235 expect(context.fillRect).toHaveBeenCalled(); 236 expect(context.fillText).toHaveBeenCalled(); 237 }); 238 239 it('ProcedureWorkerXpowerStatistic05', () => { 240 let checked = new Array(11).fill(false); 241 checked[0] = true; 242 drawLegend({ context, useCache: false }, checked, list, true); 243 expect(context.fillStyle).toBe('#333'); 244 }); 245 246 it('ProcedureWorkerXpowerStatistic06', () => { 247 XpowerStatisticStruct.draw({ context, useCache: false }, row.dataListCache[0], row, true); 248 expect(context.strokeRect).toHaveBeenCalled(); 249 }); 250 251 it('ProcedureWorkerXpowerStatistic07', () => { 252 row.dataListCache[0].frame = new Rect(0, 0, 100, 100); 253 XpowerStatisticStruct.drawHistogram({ context, useCache: false }, row.dataListCache[0], -1, 20, 1, row.frame!); 254 expect(context.fillRect).toHaveBeenCalled(); 255 }); 256 257 it('ProcedureWorkerXpowerStatistic08', () => { 258 row.dataListCache[0].frame = new Rect(0, 0, 100, 100); 259 XpowerStatisticStruct.drawHistogram({ context, useCache: false }, row.dataListCache[0], 50, 5, 1, row.frame!); 260 expect(context.fillRect).toHaveBeenCalled(); 261 }); 262 263 it('ProcedureWorkerXpowerStatistic09', () => { 264 XpowerStatisticStruct.setHoverHtml(row.dataListCache[0], list); 265 expect(row.dataListCache[0].hoverHtml).toBeDefined(); 266 }); 267 268 it('ProcedureWorkerXpowerStatistic10', () => { 269 XpowerStatisticStruct.setHoverHtml(row.dataListCache[1], list); 270 expect(row.dataListCache[1].hoverHtml).toBeDefined(); 271 }); 272 273 it('ProcedureWorkerXpowerStatistic11', () => { 274 XpowerStatisticStruct.setXPowerStatisticFrame(row.dataListCache[1], 5, 0, 1000000, 1000000, row.frame!); 275 expect(row.dataListCache[1].frame).toBeDefined(); 276 expect( 277 XpowerStatisticStruct.setXPowerStatisticFrame(row.dataListCache[1], 5, 0, 1000000, 1000000, row.frame!) 278 ).toBeUndefined(); 279 }); 280 281 it('ProcedureWorkerXpowerStatistic12', () => { 282 expect(XpowerStatisticStruct.computeMaxEnergy(row.dataListCache)).toBeUndefined(); 283 XpowerStatisticStruct.computeMaxEnergy(row.dataListCache); 284 expect(XpowerStatisticStruct.maxEnergy).toEqual(0); 285 }); 286}); 287