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