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 XpowerWifiRender, 20 XpowerWifiStruct, 21 drawLegend, 22} from '../../../../src/trace/database/ui-worker/ProcedureWorkerXpowerWifi'; 23import { SpApplication } from '../../../../src/trace/SpApplication'; 24 25jest.mock('../../../../src/trace/database/ui-worker/ProcedureWorker', () => { 26 return {}; 27}); 28 29jest.mock('../../../../src/trace/component/SpSystemTrace', () => { 30 return {}; 31}); 32 33jest.spyOn(require('../../../../src/trace/database/ui-worker/ProcedureWorkerCommon'), 'isFrameContainPoint').mockReturnValue(true); 34 35 36describe('ProcedureWorkerXpowerWifi Test', () => { 37 let row: TraceRow<XpowerWifiStruct>; 38 let context: CanvasRenderingContext2D; 39 document.body.innerHTML = '<sp-application id="sss"></sp-application>'; 40 let spApplication = document.querySelector('#sss') as SpApplication; 41 spApplication.dark = false; 42 beforeEach(() => { 43 row = TraceRow.skeleton<XpowerWifiStruct>(); 44 TraceRow.range = { 45 startNS: 0, 46 endNS: 1000000, 47 totalNS: 1000000, 48 }; 49 row.dataListCache = [ 50 { 51 startTime: 100, 52 rx: 10, 53 tx: 20, 54 }, 55 { 56 startTime: 200, 57 rx: 15, 58 tx: 25, 59 }, 60 ]; 61 let list = ['tx', 'rx']; 62 row.rowSettingCheckBoxList = list; 63 row.addRowSettingCheckBox(); 64 65 context = { 66 save: jest.fn(), 67 translate: jest.fn(), 68 rect: jest.fn(), 69 clip: jest.fn(), 70 strokeStyle: '#000000', 71 strokeRect: jest.fn(), 72 fillRect: jest.fn(), 73 fillText: jest.fn(), 74 measureText: jest.fn().mockReturnValue({ width: 10 }), 75 beginPath: jest.fn(), 76 closePath: jest.fn(), 77 stroke: jest.fn(), 78 clearRect: jest.fn(), 79 fill: jest.fn(), 80 fillStyle: '#000000', 81 globalAlpha: 1, 82 font: '10px sans-serif', 83 canvas: { 84 clientWidth: 800, 85 }, 86 } as unknown as CanvasRenderingContext2D; 87 }); 88 89 it('ProcedureWorkerXpowerWifi01', () => { 90 const xpowerWifiRender = new XpowerWifiRender(); 91 xpowerWifiRender.renderMainThread({ context, useCache: false, name: 'WIFIPackets' }, row); 92 expect(context.beginPath).toHaveBeenCalled(); 93 expect(context.closePath).toHaveBeenCalled(); 94 }); 95 96 it('ProcedureWorkerXpowerWifi02', () => { 97 const xpowerWifiRender = new XpowerWifiRender(); 98 xpowerWifiRender.renderMainThread({ context, useCache: false, name: 'WIFIPackets' }, row); 99 expect(row.dataListCache[0].tx).toBe(20); 100 expect(row.dataListCache[0].rx).toBe(10); 101 expect(row.dataListCache[1].tx).toBe(25); 102 expect(row.dataListCache[1].rx).toBe(15); 103 }); 104 105 it('ProcedureWorkerXpowerWifi03', () => { 106 row.isHover = true; 107 row.hoverX = 50; 108 row.hoverY = 50; 109 row.dataListCache[0].frame = new Rect(0, 0, 100, 100); 110 const xpowerWifiRender = new XpowerWifiRender(); 111 xpowerWifiRender.renderMainThread({ context, useCache: false, name: 'WIFIPackets' }, row); 112 expect(XpowerWifiStruct.hoverPacketsStruct).toBeDefined(); 113 expect(XpowerWifiStruct.hoverPacketsStruct!.startTime).toBe(200); 114 }); 115 116 it('ProcedureWorkerXpowerWifi04', () => { 117 row.isHover = true; 118 row.hoverX = 50; 119 row.hoverY = 50; 120 row.dataListCache[0].frame = new Rect(0, 0, 100, 100); 121 row.dataListCache[1].frame = new Rect(100, 100, 200, 200); 122 const xpowerWifiRender = new XpowerWifiRender(); 123 xpowerWifiRender.renderMainThread({ context, useCache: false, name: 'WIFIBytes' }, row); 124 expect(XpowerWifiStruct.hoverBytesStruct).toBeDefined(); 125 expect(XpowerWifiStruct.hoverBytesStruct!.startTime).toBe(200); 126 }); 127 128 it('ProcedureWorkerXpowerWifi05', () => { 129 row.hoverX = 0; 130 row.hoverY = 0; 131 const xpowerWifiRender = new XpowerWifiRender(); 132 xpowerWifiRender.renderMainThread({ context, useCache: false, name: 'WIFIPackets' }, row); 133 expect(XpowerWifiStruct.hoverPacketsStruct).toBeUndefined(); 134 }); 135 136 it('ProcedureWorkerXpowerWifi06', () => { 137 drawLegend({ context, useCache: false, name: 'WIFIPackets' }, [true, true], false); 138 expect(context.fillRect).toHaveBeenCalled(); 139 expect(context.fillText).toHaveBeenCalled(); 140 }); 141 142 it('ProcedureWorkerXpowerWifi07', () => { 143 drawLegend({ context, useCache: false, name: 'WIFIPackets' }, [true, true], true); 144 expect(context.fillStyle).toBe('#333'); 145 }); 146 147 const data = new XpowerWifiStruct(); 148 data.startTime = 100; 149 data.rx = 10; 150 data.tx = 20; 151 152 it('ProcedureWorkerXpowerWifi08', () => { 153 XpowerWifiStruct.draw({ context, useCache: false, name: 'WIFIPackets' }, data, row, true); 154 expect(context.strokeRect).not.toHaveBeenCalled(); 155 }); 156 157 it('ProcedureWorkerXpowerWifi09', () => { 158 data.frame = new Rect(0, 0, 100, 100); 159 XpowerWifiStruct.drawHistogram({ context, useCache: false }, data, -1, 20, 'tx', row.frame!); 160 expect(context.fillRect).toHaveBeenCalled(); 161 }); 162 163 it('ProcedureWorkerXpowerWifi10', () => { 164 data.frame = new Rect(0, 0, 100, 100); 165 XpowerWifiStruct.drawHistogram({ context, useCache: false }, data, 50, 10, 'rx', row.frame!); 166 expect(context.fillRect).toHaveBeenCalled(); 167 }); 168 169 it('ProcedureWorkerXpowerWifi11', () => { 170 XpowerWifiStruct.setHoverHtml(data, 'WIFIPackets'); 171 expect(data.hoverHtmlPackets).toBeDefined(); 172 }); 173 174 it('ProcedureWorkerXpowerWifi12', () => { 175 XpowerWifiStruct.setHoverHtml(data, 'WIFIBytes'); 176 expect(data.hoverHtmlBytes).toBeDefined(); 177 }); 178 179 it('ProcedureWorkerXpowerWifi13', () => { 180 XpowerWifiStruct.setXPowerStatisticFrame(data, 5, 0, 1000000, 1000000, row.frame!); 181 expect(data.frame).toBeDefined(); 182 }); 183}); 184