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 16// @ts-ignore 17import { TraceRow } from '../../../../dist/trace/component/trace/base/TraceRow.js'; 18// @ts-ignore 19import { SdkSliceRender, SdkSliceStruct } from '../../../../dist/trace/database/ui-worker/ProduceWorkerSdkSlice.js'; 20jest.mock('../../../../dist/trace/database/ui-worker/ProcedureWorker.js', () => { 21 return {}; 22}); 23 24describe('ProduceWorkerSdkSlice Test', () => { 25 it('ProduceWorkerSdkSliceTest01', function () { 26 let sdkSliceRender = new SdkSliceRender(); 27 let list = [ 28 { 29 length: 19, 30 frame: { 31 x: 46, 32 Y: 140, 33 width: 780, 34 height: 80, 35 }, 36 }, 37 ]; 38 let res = [ 39 { 40 length: 81, 41 frame: null, 42 }, 43 ]; 44 expect(sdkSliceRender.sdkSlice(list, res, 1, 5, 4, true)).toBeUndefined(); 45 }); 46 47 it('ProduceWorkerSdkSliceTest02', function () { 48 let sdkSliceRender = new SdkSliceRender(); 49 let list = [ 50 { 51 length: 891, 52 frame: { 53 x: 17, 54 Y: 175, 55 width: 550, 56 height: 870, 57 }, 58 }, 59 ]; 60 let res = [ 61 { 62 length: 430, 63 frame: null, 64 }, 65 ]; 66 expect(sdkSliceRender.sdkSlice(list, res, 1, 5, 4, false)).toBeUndefined(); 67 }); 68 69 it('ProduceWorkerSdkSliceTest03', () => { 70 const data = { 71 startNs: 1, 72 value: 1, 73 frame: { 74 x: 20, 75 y: 20, 76 width: 100, 77 height: 100, 78 }, 79 start_ts: 1, 80 }; 81 const canvas = document.createElement('canvas'); 82 canvas.width = 1; 83 canvas.height = 1; 84 const ctx = canvas.getContext('2d'); 85 expect(SdkSliceStruct.draw(ctx, data)).toBeUndefined(); 86 }); 87 88 it('ProduceWorkerSdkSliceTest04', () => { 89 let node = { 90 startNs: 1, 91 value: 1, 92 frame: { 93 x: 20, 94 y: 20, 95 width: 100, 96 height: 100, 97 }, 98 start_ts: 1, 99 end_ts: 2, 100 }; 101 let frame = { 102 x: 20, 103 y: 20, 104 width: 100, 105 height: 100, 106 }; 107 expect(SdkSliceStruct.setSdkSliceFrame(node, 2, 2, 3, 1, frame)).toBeUndefined(); 108 }); 109 110 it('ProduceWorkerSdkSliceTest05', () => { 111 let node = { 112 startNs: 1, 113 value: 1, 114 frame: { 115 x: 20, 116 y: 20, 117 width: 100, 118 height: 100, 119 }, 120 start_ts: 3, 121 end_ts: 5, 122 }; 123 let frame = { 124 x: 20, 125 y: 20, 126 width: 100, 127 height: 100, 128 }; 129 expect(SdkSliceStruct.setSdkSliceFrame(node, 2, 2, 3, 1, frame)).toBeUndefined(); 130 }); 131 132 it('ProduceWorkerSdkSliceTest06', function () { 133 let sdkSliceRender = new SdkSliceRender(); 134 let sdkSliceReq = { 135 lazyRefresh: true, 136 type: '', 137 startNS: 21, 138 endNS: 31, 139 totalNS: 10, 140 frame: { 141 x: 20, 142 y: 10, 143 width: 100, 144 height: 200, 145 }, 146 useCache: false, 147 range: { 148 refresh: '', 149 }, 150 canvas: 'a', 151 context: { 152 font: '11px sans-serif', 153 fillStyle: '#2c441b', 154 globalAlpha: 0.75, 155 clearRect: jest.fn(() => true), 156 beginPath: jest.fn(() => true), 157 stroke: jest.fn(() => true), 158 closePath: jest.fn(() => true), 159 measureText: jest.fn(() => ''), 160 fillRect: jest.fn(() => true), 161 fillText: jest.fn(() => false), 162 }, 163 lineColor: '#993e00', 164 isHover: '', 165 hoverX: 51, 166 params: '', 167 wakeupBean: undefined, 168 flagMoveInfo: '', 169 flagSelectedInfo: '', 170 slicesTime: 66, 171 id: 1, 172 x: 70, 173 y: 80, 174 width: 15, 175 height: 15, 176 }; 177 window.postMessage = jest.fn(() => true); 178 expect(sdkSliceRender.render(sdkSliceReq, [], [])).toBeUndefined(); 179 }); 180 it('ProduceWorkerSdkSliceTest07', function () { 181 let sdkSliceRender = new SdkSliceRender(); 182 window.postMessage = jest.fn(() => true); 183 let canvas = document.createElement('canvas') as HTMLCanvasElement; 184 let context = canvas.getContext('2d'); 185 const data = { 186 context: context!, 187 useCache: true, 188 type: '', 189 traceRange: [], 190 }; 191 expect(sdkSliceRender.renderMainThread(data, new TraceRow())).toBeUndefined(); 192 }); 193}); 194