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'; 17 18import { 19 anomaly, 20 EnergyAnomalyStruct, 21 EnergyAnomalyRender, 22} from '../../../../src/trace/database/ui-worker/ProcedureWorkerEnergyAnomaly'; 23 24jest.mock('../../../../src/js-heap/model/DatabaseStruct', () => { 25 return {}; 26}); 27jest.mock('../../../../src/trace/database/ui-worker/ProcedureWorkerSnapshot', () => { 28 return {}; 29}); 30jest.mock('../../../../src/trace/database/ui-worker/ProcedureWorker', () => { 31 return {}; 32}); 33describe('ProcedureWorkerEnergyAnomaly Test', () => { 34 it('ProcedureWorkerEnergyAnomalyTest01', function () { 35 let frame = { 36 x: 30, 37 y: 20, 38 width: 550, 39 height: 500, 40 }; 41 let energyAnomalyDataList = new Array(); 42 energyAnomalyDataList.push({ 43 startNS: 111, 44 dur: 40, 45 length: 23, 46 frame: {x: 0, y: 29, width: 22, height: 101}, 47 }); 48 energyAnomalyDataList.push({startNS: 11, dur: 21, length: 10}); 49 anomaly(energyAnomalyDataList, [{length: 1}], 1, 3, 2, frame, '', true); 50 }); 51 52 it('ProcedureWorkerEnergyAnomalyTest02', function () { 53 let frame = { 54 x: 50, 55 y: 32, 56 width: 600, 57 height: 200, 58 }; 59 let energyAnomalyDataList = new Array(); 60 energyAnomalyDataList.push({ 61 startNS: 22, 62 dur: 30, 63 length: 25, 64 frame: {x: 0, y: 19, width: 32, height: 102}, 65 }); 66 energyAnomalyDataList.push({startNS: 12, dur: 22, length: 12}); 67 anomaly(energyAnomalyDataList, [{length: 0}], 1, 3, 2, frame, '', false); 68 }); 69 70 it('ProcedureWorkerEnergyAnomalyTest03', function () { 71 const canvas = document.createElement('canvas'); 72 canvas.width = 7; 73 canvas.height = 7; 74 const ctx = canvas.getContext('2d'); 75 76 const data = { 77 frame: { 78 x: 207, 79 y: 207, 80 width: 100, 81 height: 100, 82 }, 83 }; 84 let path = new Path2D(); 85 expect(EnergyAnomalyStruct.draw(ctx, path, data)).toBeUndefined(); 86 }); 87 88 it('ProcedureWorkerEnergyAnomalyTest04', function () { 89 let node = { 90 frame: { 91 x: 20, 92 y: 50, 93 width: 100, 94 height: 500, 95 }, 96 startNS: 56, 97 value: 60, 98 startTs: 3, 99 dur: 1, 100 height: 2, 101 }; 102 let frame = { 103 x: 22, 104 y: 22, 105 width: 130, 106 height: 130, 107 }; 108 expect(EnergyAnomalyStruct.setAnomalyFrame(node, 1, 2, 5, frame)).toBeUndefined(); 109 }); 110 111 it('ProcedureWorkerEnergyAnomalyTest05', function () { 112 let node = { 113 frame: { 114 x: 20, 115 y: 20, 116 width: 100, 117 height: 100, 118 }, 119 startNS: 6, 120 value: 50, 121 startTs: 3, 122 dur: 3, 123 height: 2, 124 }; 125 let frame = { 126 x: 20, 127 y: 20, 128 width: 100, 129 height: 100, 130 }; 131 expect(EnergyAnomalyStruct.setAnomalyFrame(node, 1, 2, 5, frame)).toBeUndefined(); 132 }); 133 134 it('ProcedureWorkerEnergyAnomalyTest06', function () { 135 let energyAnomalyRender = new EnergyAnomalyRender(); 136 let energyAnomalyReq = { 137 lazyRefresh: true, 138 type: '', 139 startNS: 0, 140 endNS: 9, 141 totalNS: 9, 142 frame: { 143 x: 30, 144 y: 30, 145 width: 140, 146 height: 140, 147 }, 148 useCache: false, 149 range: { 150 refresh: '12', 151 }, 152 canvas: 'b', 153 context: { 154 font: '13px sans-serif', 155 fillStyle: '#e00f55', 156 globalAlpha: 0.4, 157 canvas: { 158 clientWidth: 12, 159 }, 160 clearRect: jest.fn(() => true), 161 closePath: jest.fn(() => true), 162 measureText: jest.fn(() => false), 163 fillRect: jest.fn(() => true), 164 beginPath: jest.fn(() => true), 165 stroke: jest.fn(() => true), 166 fillText: jest.fn(() => true), 167 }, 168 lineColor: '#000000', 169 isHover: '', 170 hoverX: 1, 171 params: '', 172 wakeupBean: true, 173 flagMoveInfo: '', 174 flagSelectedInfo: '', 175 slicesTime: 6, 176 id: 1, 177 x: 20, 178 y: 20, 179 width: 150, 180 height: 150, 181 }; 182 window.postMessage = jest.fn(() => true); 183 expect(energyAnomalyRender.render(energyAnomalyReq, [], [])).toBeUndefined(); 184 }); 185 it('ProcedureWorkerEnergyAnomalyTest07 ', function () { 186 let req; 187 let row; 188 let dataList; 189 let dataListCache; 190 let context; 191 let spApplication; 192 req = { 193 useCache: true, 194 context: { 195 font: '13px sans-serif', 196 fillStyle: '#e00f55', 197 globalAlpha: 0.4, 198 canvas: { 199 clientWidth: 12, 200 }, 201 clearRect: jest.fn(() => true), 202 closePath: jest.fn(() => true), 203 measureText: jest.fn(() => false), 204 fillRect: jest.fn(() => true), 205 beginPath: jest.fn(() => true), 206 stroke: jest.fn(() => true), 207 fillText: jest.fn(() => true), 208 }, 209 type: 'testType', 210 appName: 'testAppName', 211 canvasWidth: 800, 212 }; 213 dataList = []; 214 dataListCache = []; 215 row = { 216 dataList: dataList, 217 dataListCache: dataListCache, 218 frame: 'testFrame', 219 isHover: true, 220 hoverX: 100, 221 hoverY: 100, 222 }; 223 spApplication = { 224 hasAttribute: jest.fn().mockReturnValue(true), 225 }; 226 let energyAnomalyRender = new EnergyAnomalyRender(); 227 global.document.getElementsByTagName = jest.fn().mockReturnValue([spApplication]); 228 TraceRow.range = jest.fn(() => true); 229 TraceRow.range.startNS = jest.fn(() => 1); 230 expect(energyAnomalyRender.renderMainThread(req, row)).toBeUndefined(); 231 }); 232}); 233