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 { commonStartAbility } from '../common/utils/AbilityUtils'; 17import router from '@system.router'; 18import { SwitchItem, CollectItem } from '../common/entity/LocalConfigEntity'; 19import { StartTestTitleComponent } from '../common/ui/StartTestTitleComponent'; 20import { ProfilerTask } from '../common/profiler/ProfilerTask' 21import { CollectorType } from '../common/profiler/base/ProfilerConstant' 22import { getPidOfAbility } from '../common/utils/SystemUtils'; 23import prompt from '@system.prompt'; 24import SPLogger from '../../ets/common/utils/SPLogger' 25 26 27const TAG = "StartTestPage" 28/* 29 * 测试配置页 30 */ 31@Entry 32@Component 33struct StartTestPage { 34 @State selectApp: string = "请选择一个应用" 35 @State selectAppIcon: string = "" 36 @State private collectConfigs: Array<CollectItem> = new Array() 37 @State private switchList:Array<SwitchItem> = new Array( 38 new SwitchItem("trace", '是否抓取trace', $r("app.media.icon_average_frame_b"), false, true), 39 new SwitchItem("screen_capture", '是否开启截图', $r("app.media.icon_screencap"), false, true) 40 ) 41 @State private testName: string = "" // 测试名称 42 43 dialogController: CustomDialogController = new CustomDialogController({ 44 builder: CustomDialogCollect({ cancel: () => { 45 }, confirm: () => { 46 }, collectConfigs: $collectConfigs }), 47 cancel: () => { 48 }, 49 autoCancel: true 50 }) 51 textController: CustomDialogController = new CustomDialogController({ 52 builder: TextInputDialog({ cancel: () => { 53 }, confirm: () => { 54 }, testName: $testName }), 55 cancel: () => { 56 }, 57 autoCancel: true 58 }) 59 60 aboutToAppear() { 61 let supportMap = ProfilerTask 62 .getInstance() 63 .getSupports([ 64 CollectorType.TYPE_CPU.toString(), 65 CollectorType.TYPE_GPU.toString(), 66 CollectorType.TYPE_DDR.toString(), 67 CollectorType.TYPE_FPS.toString(), 68 CollectorType.TYPE_POWER.toString(), 69 CollectorType.TYPE_TEMPERATURE.toString(), 70 CollectorType.TYPE_RAM.toString() 71 ]) 72 73 var iterator = supportMap.keys() 74 for (var i = 0; i < supportMap.size; i++) { 75 let key = iterator.next().value 76 let val = supportMap.get(key).valueOf() 77 this.collectConfigs.push( 78 new CollectItem(key, val, val) 79 ) 80 } 81 82 } 83 84 build() { 85 86 Column() { 87 //开始测试title 88 StartTestTitleComponent({ title: "开始测试" }) 89 90 Scroll() { 91 Column() { 92 //请选择一个应用 93 Row({ space: '15vp' }) { 94 if (this.selectAppIcon == "") { 95 Image($r('app.media.logo')).width('70vp').height('70vp').margin({ left: '2%' }) 96 } else { 97 Image(this.selectAppIcon).width('70vp').height('70vp').margin({ left: '2%' }) 98 } 99 Flex({ justifyContent: FlexAlign.SpaceBetween, alignItems: ItemAlign.Center }) { 100 Text(this.selectApp).fontSize('15fp').fontColor($r("app.color.color_333")) 101 102 Image($r('app.media.icon_enter')).width('15vp').height('15vp').margin({ right: '35vp' }) 103 }.height('70vp').width('85%') 104 } 105 .height('100vp') 106 .width('95%') 107 .borderRadius('5vp') 108 .backgroundColor($r("app.color.color_fff")) 109 .margin({ top: '20vp' }) 110 .onClick(() => { 111 router.push({ uri: 'pages/AppSelectPage', params: { 112 StartPage: "StartTest" 113 } }) 114 }) 115 116 //测试指标 117 Row({ space: '15vp' }) { 118 Image($r('app.media.icon_test_index')).width('25vp').height('25vp').margin({ left: '2%' }) 119 120 Flex({ justifyContent: FlexAlign.SpaceBetween, alignItems: ItemAlign.Center }) { 121 Text('测试指标').fontSize('15fp').fontColor($r("app.color.color_333")) 122 123 Image($r('app.media.icon_enter')).width('15vp').height('15vp').margin({ right: '15vp' }) 124 }.height('60vp').width('90%').onClick(() => { 125 console.log("TestIndicators---------onClick") 126 if (this.selectApp == "请选择一个应用" || this.selectApp == "SmartPerf") { 127 console.log("TestIndicators---------please choose app") 128 prompt.showToast({ message: "please choose app!", duration: 1000 }) 129 return 130 } 131 console.log("TestIndicators---------dialogController.open()") 132 this.dialogController.open() 133 console.log("TestIndicators---------dialogController.open End()") 134 }) 135 } 136 .height('60vp') 137 .width('95%') 138 .borderRadius('5vp') 139 .backgroundColor($r("app.color.color_fff")) 140 .margin({ top: '10vp' }) 141 142 //测试名称 143 Row({ space: '15vp' }) { 144 Image($r('app.media.icon_test_name')).width('25vp').height('25vp').margin({ left: '2%' }) 145 146 Flex({ justifyContent: FlexAlign.SpaceBetween, alignItems: ItemAlign.Center }) { 147 Text('测试名称').fontSize('15fp').fontColor($r("app.color.color_333")) 148 149 Row() { 150 Text(this.testName).fontSize('15fp').fontColor($r("app.color.color_333")) 151 152 Image($r('app.media.icon_enter')).width('15vp').height('15vp').margin({ right: '15vp' }) 153 } 154 }.height('60vp').width('90%').onClick(() => { 155 this.textController.open() 156 }) 157 158 }.height('60vp').width('95%').borderRadius('5vp').backgroundColor($r("app.color.color_fff")) 159 160 SwitchComponent({ switchList: $switchList }) 161 Blank() 162 Button('开始测试') 163 .fontSize('15fp') 164 .fontColor($r('app.color.color_fff')) 165 .border({ radius: '20vp' }) 166 .width('80%') 167 .height('60vp') 168 .backgroundColor($r("app.color.colorPrimary")) 169 .onClick(() => { 170 171 let taskConfig = this.resolveTaskConfig() 172 console.log("console.log:" + JSON.stringify(taskConfig)); 173 174 if (this.selectApp == "请选择一个应用" || this.selectApp == "SmartPerf") { 175 prompt.showToast({ message: "please choose app!", duration: 1000 }) 176 return 177 } 178 if(router!=null){ 179 if(router.getParams()!=null){ 180 if(router.getParams()["selectPackageName"]!=null){ 181 //启动app 182 commonStartAbility(new String(router.getParams()["selectPackageName"]).toString(), new String(router.getParams()["selectAbilityName"]).toString()) 183 //启动悬浮窗 184 globalThis.CreateFloatingWindow() 185 router.back({ uri: "pages/MainPage" }) 186 } 187 } 188 } 189 }) 190 191 Divider().height('15%').width("80%").visibility(Visibility.Hidden) 192 }.height("100%") 193 }.width('100%').scrollable(ScrollDirection.Vertical).scrollBar(BarState.Auto) 194 195 }.height('100%').width('100%').backgroundColor('#EEEEEE') 196 } 197 198 onPageShow() { 199 let routerParams = router.getParams() 200 let appName 201 let appVersion 202 let selectPackageName 203 let appIconId 204 if (routerParams != undefined && routerParams !=null) { 205 appName = routerParams["appName"] 206 appVersion = routerParams["appVersion"] 207 selectPackageName = routerParams["selectPackageName"] 208 appIconId = routerParams["appIconId"] 209 } 210 if (appName == null && appName == undefined) { 211 this.selectApp = "请选择一个应用" 212 this.testName = "" 213 this.selectAppIcon = "" 214 } else { 215 this.selectApp = new String(appName).toString() 216 globalThis.appName = new String(appName).toString() 217 globalThis.appVersion = new String(appVersion).toString() 218 globalThis.packageName = new String(selectPackageName).toString() 219 this.selectAppIcon = new String(appIconId).toString() 220 let date = new Date() 221 let M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1); 222 let D = date.getDate() + '-'; 223 let h 224 if (date.getHours() < 10) { 225 h = "0" + date.getHours(); 226 } else { 227 h = date.getHours(); 228 } 229 let m = date.getMinutes(); 230 globalThis.testTaskName = "游戏测试" + M + D + h + m 231 this.testName = "游戏测试" + M + D + h + m 232 } 233 } 234 235 resolveTaskConfig() { 236 let collects = this.collectConfigs 237 let collectStr = "" 238 for (var i = 0; i < collects.length; i++) { 239 const collect = collects[i]; 240 if (i != collects.length - 1) { 241 collectStr += collect.name + "::" + collect.isSelect + "," 242 } else { 243 collectStr += collect.name + "::" + collect.isSelect 244 } 245 } 246 247 let switchs = this.switchList 248 let switchStr = "" 249 for (var j = 0; j < switchs.length; j++) { 250 const st = switchs[j]; 251 if (j != switchs.length - 1) { 252 switchStr += st.id + "::" + st.isOpen + "," 253 } else { 254 switchStr += st.id + "::" + st.isOpen 255 } 256 } 257 258 let taskConfig = { 259 "selectAppName": globalThis.packageName, 260 "allConfigs": collectStr + "," + switchStr 261 } 262 263 let configItems: { [key: string]: boolean } = {} 264 let allConfigsArr: string[] = [] 265 let curSelectPkg = "" 266 if (taskConfig !== undefined) { 267 allConfigsArr = taskConfig.allConfigs.split(",") 268 curSelectPkg = taskConfig.selectAppName 269 } 270 for (var index = 0; index < allConfigsArr.length; index++) { 271 const config = allConfigsArr[index]; 272 let params = config.split("::") 273 if (params[1] == "true" || params[1] == "1") { 274 configItems[params[0]] = true 275 }else if (params[1] == "false" || params[1] == "0") { 276 configItems[params[0]] = false 277 }else { 278 configItems[params[0]] = false 279 } 280 } 281 globalThis.collectConfigs = configItems 282 globalThis.collectPkg = curSelectPkg 283 284 return taskConfig 285 } 286 287 search(id: string, myArray: Array<SwitchItem>): SwitchItem{ 288 for (var i = 0; i < myArray.length; i++) { 289 if (myArray[i].id === id) { 290 return myArray[i]; 291 } 292 } 293 } 294} 295 296 297@Component 298struct SwitchComponent { 299 @Link private switchList:Array<SwitchItem> 300 301 build() { 302 Column() { 303 List() { 304 ForEach(this.switchList, (switchItem) => { 305 ListItem() { 306 Row({ space: '15vp' }) { 307 Image(switchItem.switchSrc).width('25vp').height('25vp').margin({ left: '2%' }) 308 309 Flex({ justifyContent: FlexAlign.SpaceBetween, alignItems: ItemAlign.Center }) { 310 Text(switchItem.switchName).fontSize('15fp').fontColor($r("app.color.color_333")) 311 312 Toggle({ type: ToggleType.Switch, isOn: switchItem.isOpen }) 313 .width('60vp') 314 .height('25vp') 315 .enabled(switchItem.enable) 316 .onChange((isOn) => { 317 console.log("isOn" + isOn) 318 switchItem.isOpen = isOn 319 }) 320 .margin({ right: '10vp' }) 321 }.height('60vp').width('90%') 322 } 323 .height('60vp') 324 .width('100%') 325 .borderRadius('5vp') 326 .backgroundColor($r("app.color.color_fff")) 327 .margin({ top: '10vp' }) 328 } 329 }, switchItem => switchItem.switchName) 330 } 331 }.width('95%') 332 } 333} 334 335@CustomDialog 336struct CustomDialogCollect { 337 @Link private collectConfigs: Array<CollectItem> 338 controller: CustomDialogController 339 cancel: () => void 340 confirm: () => void 341 342 build() { 343 Column() { 344 List() { 345 ForEach(this.collectConfigs, (Item) => { 346 ListItem() { 347 if(Item.isSupport) { 348 Row({ space: '15vp' }) { 349 Flex({ justifyContent: FlexAlign.SpaceBetween, alignItems: ItemAlign.Center }) { 350 Text(Item.name).fontSize('18fp').fontColor($r("app.color.color_333")).margin({ left: 20 }) 351 Toggle({ type: ToggleType.Switch, isOn: Item.isSupport }) 352 .width('60vp') 353 .height('25vp') 354 .enabled(true) 355 .onChange((isOn) => { 356 Item.isSelect = isOn 357 }) 358 .margin({ right: '5vp' }) 359 }.height('60vp').width('90%') 360 } 361 .height('60vp') 362 .width('100%') 363 .borderRadius('5vp') 364 .backgroundColor($r("app.color.color_fff")) 365 .margin({ top: '10vp' }) 366 } else { 367 Row({ space: '15vp' }) { 368 Flex({ justifyContent: FlexAlign.SpaceBetween, alignItems: ItemAlign.Center }) { 369 Text(Item.name).fontSize('18fp').fontColor($r("app.color.color_333")).margin({ left: 20 }) 370 Toggle({ type: ToggleType.Switch, isOn: Item.isSupport }) 371 .width('60vp') 372 .height('25vp') 373 .enabled(false) 374 .margin({ right: '5vp' }) 375 }.height('60vp').width('90%') 376 } 377 .onClick(() => { 378 prompt.showToast({ duration: 1000, message: "not support" }) 379 }) 380 .height('60vp') 381 .width('100%') 382 .borderRadius('5vp') 383 .backgroundColor($r("app.color.color_eee")) 384 .margin({ top: '10vp' }) 385 } 386 } 387 }, Item => Item.name) 388 } 389 390 Flex({ justifyContent: FlexAlign.SpaceAround }) { 391 Button('cancel') 392 .onClick(() => { 393 this.controller.close() 394 this.cancel() 395 }).backgroundColor(0xffffff).fontColor(Color.Black) 396 Button('confirm') 397 .onClick(() => { 398 this.controller.close() 399 this.confirm() 400 }).backgroundColor(0xffffff).fontColor(Color.Red) 401 }.margin({ bottom: 10 }) 402 } 403 } 404} 405 406@CustomDialog 407export struct TextInputDialog { 408 @Link private testName: String 409 controller: CustomDialogController 410 cancel: () => void 411 confirm: () => void 412 413 aboutToAppear() { 414 console.log("TextInputDialog called") 415 } 416 417 build() { 418 Column() { 419 TextArea({ placeholder: '请输入测试名称', text: this.testName.toString() }) 420 .placeholderFont({ size: 15 }) 421 .fontSize('15fp') 422 .textAlign(TextAlign.Center) 423 .fontSize(30) 424 .onChange((value: string) => { 425 this.testName = value 426 }) 427 .padding(20) 428 Flex({ justifyContent: FlexAlign.SpaceAround }) { 429 Button('cancel') 430 .onClick(() => { 431 this.controller.close() 432 this.cancel() 433 }).backgroundColor(0xffffff).fontColor(Color.Black) 434 Button('confirm') 435 .onClick(() => { 436 this.controller.close() 437 this.confirm() 438 }).backgroundColor(0xffffff).fontColor(Color.Red) 439 }.margin({ bottom: 10 }) 440 } 441 } 442}