• 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 { 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.getParams()["selectPackageName"]!=null && router!=null){
179              //启动app
180              commonStartAbility(new String(router.getParams()["selectPackageName"]).toString(), new String(router.getParams()["selectAbilityName"]).toString())
181
182              //启动悬浮窗
183              globalThis.CreateFloatingWindow()
184              router.back({ uri: "pages/MainPage" })
185			  }
186
187            })
188
189          Divider().height('15%').width("80%").visibility(Visibility.Hidden)
190        }.height("100%")
191      }.width('100%').scrollable(ScrollDirection.Vertical).scrollBar(BarState.Auto)
192
193    }.height('100%').width('100%').backgroundColor('#EEEEEE')
194  }
195
196  onPageShow() {
197    let routerParams = router.getParams()
198    let appName
199    let appVersion
200    let selectPackageName
201    let appIconId
202    if (routerParams != undefined && routerParams !=null) {
203      appName = routerParams["appName"]
204      appVersion = routerParams["appVersion"]
205      selectPackageName = routerParams["selectPackageName"]
206      appIconId = routerParams["appIconId"]
207    }
208    if (appName == null && appName == undefined) {
209      this.selectApp = "请选择一个应用"
210      this.testName = ""
211      this.selectAppIcon = ""
212    } else {
213      this.selectApp = new String(appName).toString()
214      globalThis.appName = new String(appName).toString()
215      globalThis.appVersion = new String(appVersion).toString()
216      globalThis.packageName = new String(selectPackageName).toString()
217      this.selectAppIcon = new String(appIconId).toString()
218      let date = new Date()
219      let M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1);
220      let D = date.getDate() + '-';
221      let h
222      if (date.getHours() < 10) {
223        h = "0" + date.getHours();
224      } else {
225        h = date.getHours();
226      }
227      let m = date.getMinutes();
228      globalThis.testTaskName = "游戏测试" + M + D + h + m
229      this.testName = "游戏测试" + M + D + h + m
230    }
231  }
232
233  resolveTaskConfig() {
234    let collects = this.collectConfigs
235    let collectStr = ""
236    for (var i = 0; i < collects.length; i++) {
237      const collect = collects[i];
238      if (i != collects.length - 1) {
239        collectStr += collect.name + "::" + collect.isSelect + ","
240      } else {
241        collectStr += collect.name + "::" + collect.isSelect
242      }
243    }
244
245    let switchs = this.switchList
246    let switchStr = ""
247    for (var j = 0; j < switchs.length; j++) {
248      const st = switchs[j];
249      if (j != switchs.length - 1) {
250        switchStr += st.id + "::" + st.isOpen + ","
251      } else {
252        switchStr += st.id + "::" + st.isOpen
253      }
254    }
255
256    let taskConfig = {
257      "selectAppName": globalThis.packageName,
258      "allConfigs": collectStr + "," + switchStr
259    }
260
261    let configItems: { [key: string]: boolean } = {}
262    let allConfigsArr: string[] = []
263    let curSelectPkg = ""
264    if (taskConfig !== undefined) {
265      allConfigsArr = taskConfig.allConfigs.split(",")
266      curSelectPkg = taskConfig.selectAppName
267    }
268    for (var index = 0; index < allConfigsArr.length; index++) {
269      const config = allConfigsArr[index];
270      let params = config.split("::")
271      if (params[1] == "true" || params[1] == "1") {
272        configItems[params[0]] = true
273      }else if (params[1] == "false" || params[1] == "0") {
274        configItems[params[0]] = false
275      }else {
276        configItems[params[0]] = false
277      }
278    }
279    globalThis.collectConfigs = configItems
280    globalThis.collectPkg = curSelectPkg
281
282    return taskConfig
283  }
284
285  search(id: string, myArray: Array<SwitchItem>): SwitchItem{
286    for (var i = 0; i < myArray.length; i++) {
287      if (myArray[i].id === id) {
288        return myArray[i];
289      }
290    }
291  }
292}
293
294
295@Component
296struct SwitchComponent {
297  @Link private switchList:Array<SwitchItem>
298
299  build() {
300    Column() {
301      List() {
302        ForEach(this.switchList, (switchItem) => {
303          ListItem() {
304            Row({ space: '15vp' }) {
305              Image(switchItem.switchSrc).width('25vp').height('25vp').margin({ left: '2%' })
306
307              Flex({ justifyContent: FlexAlign.SpaceBetween, alignItems: ItemAlign.Center }) {
308                Text(switchItem.switchName).fontSize('15fp').fontColor($r("app.color.color_333"))
309
310                Toggle({ type: ToggleType.Switch, isOn: switchItem.isOpen })
311                  .width('60vp')
312                  .height('25vp')
313                  .enabled(switchItem.enable)
314                  .onChange((isOn) => {
315                    console.log("isOn" + isOn)
316                    switchItem.isOpen = isOn
317                  })
318                  .margin({ right: '10vp' })
319              }.height('60vp').width('90%')
320            }
321            .height('60vp')
322            .width('100%')
323            .borderRadius('5vp')
324            .backgroundColor($r("app.color.color_fff"))
325            .margin({ top: '10vp' })
326          }
327        }, switchItem => switchItem.switchName)
328      }
329    }.width('95%')
330  }
331}
332
333@CustomDialog
334struct CustomDialogCollect {
335  @Link private collectConfigs: Array<CollectItem>
336  controller: CustomDialogController
337  cancel: () => void
338  confirm: () => void
339
340  build() {
341    Column() {
342      List() {
343        ForEach(this.collectConfigs, (Item) => {
344          ListItem() {
345            if(Item.isSupport) {
346              Row({ space: '15vp' }) {
347                Flex({ justifyContent: FlexAlign.SpaceBetween, alignItems: ItemAlign.Center }) {
348                  Text(Item.name).fontSize('18fp').fontColor($r("app.color.color_333")).margin({ left: 20 })
349                  Toggle({ type: ToggleType.Switch, isOn: Item.isSupport })
350                    .width('60vp')
351                    .height('25vp')
352                    .enabled(true)
353                    .onChange((isOn) => {
354                      Item.isSelect = isOn
355                    })
356                    .margin({ right: '5vp' })
357                }.height('60vp').width('90%')
358              }
359              .height('60vp')
360              .width('100%')
361              .borderRadius('5vp')
362              .backgroundColor($r("app.color.color_fff"))
363              .margin({ top: '10vp' })
364            } else {
365              Row({ space: '15vp' }) {
366                Flex({ justifyContent: FlexAlign.SpaceBetween, alignItems: ItemAlign.Center }) {
367                  Text(Item.name).fontSize('18fp').fontColor($r("app.color.color_333")).margin({ left: 20 })
368                  Toggle({ type: ToggleType.Switch, isOn: Item.isSupport })
369                    .width('60vp')
370                    .height('25vp')
371                    .enabled(false)
372                    .margin({ right: '5vp' })
373                }.height('60vp').width('90%')
374              }
375              .onClick(() => {
376                  prompt.showToast({ duration: 1000, message: "not support" })
377              })
378              .height('60vp')
379              .width('100%')
380              .borderRadius('5vp')
381              .backgroundColor($r("app.color.color_eee"))
382              .margin({ top: '10vp' })
383            }
384          }
385        }, Item => Item.name)
386      }
387
388      Flex({ justifyContent: FlexAlign.SpaceAround }) {
389        Button('cancel')
390          .onClick(() => {
391            this.controller.close()
392            this.cancel()
393          }).backgroundColor(0xffffff).fontColor(Color.Black)
394        Button('confirm')
395          .onClick(() => {
396            this.controller.close()
397            this.confirm()
398          }).backgroundColor(0xffffff).fontColor(Color.Red)
399      }.margin({ bottom: 10 })
400    }
401  }
402}
403
404@CustomDialog
405export struct TextInputDialog {
406  @Link private testName: String
407  controller: CustomDialogController
408  cancel: () => void
409  confirm: () => void
410
411  aboutToAppear() {
412    console.log("TextInputDialog called")
413  }
414
415  build() {
416    Column() {
417      TextArea({ placeholder: '请输入测试名称', text: this.testName.toString() })
418        .placeholderFont({ size: 15 })
419        .fontSize('15fp')
420        .textAlign(TextAlign.Center)
421        .fontSize(30)
422        .onChange((value: string) => {
423          this.testName = value
424        })
425        .padding(20)
426      Flex({ justifyContent: FlexAlign.SpaceAround }) {
427        Button('cancel')
428          .onClick(() => {
429            this.controller.close()
430            this.cancel()
431          }).backgroundColor(0xffffff).fontColor(Color.Black)
432        Button('confirm')
433          .onClick(() => {
434            this.controller.close()
435            this.confirm()
436          }).backgroundColor(0xffffff).fontColor(Color.Red)
437      }.margin({ bottom: 10 })
438    }
439  }
440}