Experience_index.ets 13.6 KB
Newer Older
T
tianwenzhe 已提交
1
/*
T
tianwenzhe 已提交
2
 * Copyright (c) 2022 Huawei Device Co., Ltd.
T
tianwenzhe 已提交
3 4 5 6 7 8 9 10 11 12 13 14
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
T
tianwenzhe 已提交
15

T
tianwenzhe 已提交
16 17 18 19 20 21 22 23 24 25
import prompt from '@ohos.prompt';
import router from '@ohos.router';
import fileio from '@ohos.fileio';
import xml from '@ohos.xml';
import fileIO from '@ohos.fileio';
import util from '@ohos.util';
import data_preferences from '@ohos.data.preferences';

const TAG = 'ParseXML';
let path = globalThis.dir;
T
tianwenzhe 已提交
26
let path1 = path + '/Experience';
27
let ExperienceDataPath = path + '/ExperienceData';
T
tianwenzhe 已提交
28 29

let VarColor = [];
T
tianwenzhe 已提交
30
let ColorBackPath = path + '/ExperienceColorBack.txt';
T
tianwenzhe 已提交
31
let txtPath = path1 + '/ExperienceReport.txt';
T
tianwenzhe 已提交
32
let xmlPath = path1 + '/ExperienceTest.xml';
T
tianwenzhe 已提交
33 34 35 36 37 38 39 40 41 42

@Entry
@Component
struct IndexPage {
  @State ClearAll : boolean = false;
  private current : number = undefined;
  @State count : number = 0;
  @State result : string = '';
  @State TEST : number = 0;
  private TestCaseList = [
T
tianwenzhe 已提交
43 44
    {title:'DeskFps',uri:'pages/Experience/DeskFps'},
    {title:'PhotoFps',uri:'pages/Experience/PhotoFps'},
45 46 47 48 49 50 51 52 53
    {title:'CameraColdStart',uri:'pages/Experience/CameraColdStartTest'},
    {title:'SettingsColdStart',uri:'pages/Experience/SettingsColdStartTest'},
    {title:'PhotosColdStart',uri:'pages/Experience/PhotosColdStartTest'},
    {title:'MmsColdStart',uri:'pages/Experience/MmsColdStartTest'},
    {title:'ContactsColdStart',uri:'pages/Experience/ContactsColdStartTest'},
    {title:'SettingsHotStart',uri:'pages/Experience/SettingsHotStartTest'},
    {title:'PhotosHotStart',uri:'pages/Experience/PhotosHotStartTest'},
    {title:'MmsHotStart',uri:'pages/Experience/MmsHotStartTest'},
    {title:'ContactsHotStart',uri:'pages/Experience/ContactsHotStartTest'},
T
tianwenzhe 已提交
54 55 56 57 58 59 60
    {title:'ViewPhoto',uri:'pages/Experience/ViewPhotoTest'},
    {title:'KeyboardDisplay',uri:'pages/Experience/KeyboardDisplayTest'},
    {title:'StartTaskManager',uri:'pages/Experience/StartTaskManager'},
    {title:'StartBackgroundTask',uri:'pages/Experience/StartBackgroundTask'},
    {title:'StartNotification',uri:'pages/Experience/StartNotification'},
    {title:'ScrollPhotosList',uri:'pages/Experience/ScrollPhotosList'},
    {title:'KeyboardInput',uri:'pages/Experience/KeyboardInputTest'},
T
tianwenzhe 已提交
61 62 63 64 65 66 67 68 69 70 71 72
  ]
  @State ColorObject : string[] = VarColor;
  async onPageShow(){
    let Test = null;
    let context =null;
    context = globalThis.getContext();
    let preferences;

    let promise = data_preferences.getPreferences(context,'mystore');
    await promise.then((object) => {
      preferences = object;
    });
T
tianwenzhe 已提交
73
    promise = preferences.get('Experience', 0);
T
tianwenzhe 已提交
74 75
    await promise.then((data) => {
      Test = data;
T
tianwenzhe 已提交
76
      console.info("Succeeded in getting value of 'Experience'. Data: " + data);
T
tianwenzhe 已提交
77 78 79 80 81 82 83 84 85 86
    });

    if(Test != 1) {
      let fd = fileio.openSync(ColorBackPath, 0o102, 0o666);
      for(let i = 0; i < this.TestCaseList.length; i++) {
        let log = (this.TestCaseList[i].title+';'+'#ff808080'+';').toString();
        fileio.writeSync(fd,log);
      }
      fileio.closeSync(fd);
    }
T
tianwenzhe 已提交
87
    promise = preferences.put('Experience', 1);
T
tianwenzhe 已提交
88
    promise.then(() => {
T
tianwenzhe 已提交
89
      console.info("Succeeded in putting value of 'Experience'.");
T
tianwenzhe 已提交
90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115
    });
    promise = preferences.flush();
    promise.then(() => {
      console.info("Succeeded in flushing.");
    });
    this.TEST = Test;

    let opt = fileio.openSync(ColorBackPath, 0o100 | 0o2002, 0o664);
    let buff = new ArrayBuffer(40960);
    fileio.readSync(opt,buff);
    let ColorBack = String.fromCharCode.apply(null, new Uint8Array(buff));
    let TestList = ColorBack.split(';');
    for (let i = 0; i < Math.floor(TestList.length / 2); i++) {
      VarColor[i] = TestList[i*2+1];
    }
    fileio.closeSync(opt);

    if (this.count === 1){
      this.result = router.getParams()['result'];
      let titles = router.getParams()['title'];
      let name1 = '刚刚点进了哪个用例:'+ titles;
      let results = this.result;
      let WriteTitle = (titles).toString();
      let number = WriteTitle.length + 11;
      let Index = ColorBack.indexOf(WriteTitle);

T
tianwenzhe 已提交
116
      if (this.result === 'true '){
T
tianwenzhe 已提交
117 118 119 120 121 122 123 124 125 126 127 128 129
        this.ColorObject[this.current] = '#ff008000';
        let Log = (titles+';'+'#ff008000'+';');
        let key = ColorBack.substring(Index,Index+number);
        let FD = fileio.openSync(ColorBackPath, 0o102, 0o666);
        ColorBack = ColorBack.replace(key,Log);
        let buffer = new ArrayBuffer(4096);
        let rd = fileio.readSync(FD,buffer);
        let Report = ColorBack.substring(0,rd);
        fileio.closeSync(FD);
        let Fd = fileio.openSync(ColorBackPath, 0o102, 0o666);
        fileio.writeSync(Fd,Report);
        filewrite(name1,results,titles)
      }
T
tianwenzhe 已提交
130
      else if (this.result === 'false'){
T
tianwenzhe 已提交
131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153
        this.ColorObject[this.current] = '#ffff0000';
        let Log = (titles+';'+'#ffff0000'+';');
        let key = ColorBack.substring(Index,Index+number);
        let FD = fileio.openSync(ColorBackPath, 0o102, 0o666);
        ColorBack = ColorBack.replace(key,Log);
        let buffer = new ArrayBuffer(4096);
        let rd = fileio.readSync(FD,buffer);
        let Report = ColorBack.substring(0,rd);
        fileio.closeSync(FD);
        let Fd = fileio.openSync(ColorBackPath, 0o102, 0o666);
        fileio.writeSync(Fd,Report);
        filewrite(name1,results,titles);
      }
      else if (this.result === 'None'){
        this.ColorObject[this.current] = this.ColorObject[this.current];
      }
    }
  }
  build(){
    Column(){
      Row() {
        Button() {
          Image($r('app.media.ic_public_back')).width('20vp').height('18vp')
154
        }.backgroundColor(Color.Black).size({ width: '40vp', height: '30vp' })
T
tianwenzhe 已提交
155 156 157 158
        .onClick(() => {
          router.back();
        })
        Row(){
T
tianwenzhe 已提交
159
          Text('Experience SubSystem')
T
tianwenzhe 已提交
160
            .fontColor(Color.White)
T
tianwenzhe 已提交
161
            .fontSize('22fp')
T
tianwenzhe 已提交
162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206
        }.justifyContent(FlexAlign.SpaceAround).backgroundColor(Color.Black)
        Row() {
          Button(){
            Image($r('app.media.ic_public_delete'))
              .width('30vp')
              .height('30vp')
          }.backgroundColor(Color.Black)
          .onClick(() => {
            AlertDialog.show(
              {
                message: "是否删除所有记录",
                primaryButton: {
                  value: 'Yes',
                  action: () => {
                    this.ClearAll = true;
                    this.ColorObject.forEach((value, index) => {
                      this.ColorObject[index] =  '#ff808080';
                    });
                    this.ClearText();
                    prompt.showToast({
                      message: '结果已删除', duration: 1000
                    });
                  }
                },
                secondaryButton: {
                  value: 'No',
                  action: () => {

                  }
                },
                cancel: () => {

                }
              }
            )
          })
          Button(){
            Image($r('app.media.ic_public_save'))
              .width('30vp')
              .height('30vp')
              .margin({ left: 30 })
          }.backgroundColor(Color.Black)
          .onClick(() => {
            this.ReadTextParseXml();
            AlertDialog.show({
T
tianwenzhe 已提交
207
              message: "报告已生成,如需查看通过命令行输入'hdc_std file recv /data/app/el2/100/base/com.example.actsvalidator/haps/entry/files/Experience/ExperienceTest.xml -本地路径'",
T
tianwenzhe 已提交
208 209 210 211 212 213 214 215 216 217 218
              confirm:{
                value:'OK',
                action:()=>{
                  prompt.showToast({
                    message: '报告已生成', duration: 1000
                  })
                }
              },
              cancel: () => {
                prompt.showToast({
                  message: '报告已生成', duration: 1000
T
tianwenzhe 已提交
219
                });
T
tianwenzhe 已提交
220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246
              }
            })
          })
        }
      }.width('100%').justifyContent(FlexAlign.SpaceAround).margin({top:'15vp'}).height('6%').backgroundColor(Color.Black)
      List({space:5}){
        ForEach(this.TestCaseList,(item,index) => {
          ListItem(){
            Row(){
              Text(item.title).fontSize(16).fontColor(Color.Black)
            }.width('100%').height(50).alignItems(VerticalAlign.Center).backgroundColor(this.count===0&&this.TEST===0?'#ff808080':this.ColorObject[index])
            .onClick(( )=>{
              this.count = 1;
              this.ClearAll=false;
              this.current = index;
              router.push({
                url: item.uri,
              })
            })
          }
        },item  => item.title)
      }.width('100%').height('92%')
    }.width('100%').height('100%').backgroundColor(Color.Black)
  }
  ClearText(){
    fileio.rmdirSync(path1);
    fileio.mkdirSync(path1);
247 248
    fileio.rmdirSync(ExperienceDataPath);
    fileio.mkdirSync(ExperienceDataPath);
T
tianwenzhe 已提交
249 250 251 252 253 254 255
    let fd = fileio.openSync(ColorBackPath, 0o102, 0o666);
    for(let i = 0; i < this.TestCaseList.length; i++) {
      let log = (this.TestCaseList[i].title+';'+'#ff808080'+';').toString();
      fileio.writeSync(fd,log);
    }
  }
  ReadTextParseXml(){
T
tianwenzhe 已提交
256
    let ReportPath = path1 + '/ExperienceReport.txt';
T
tianwenzhe 已提交
257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274
    let dir = fileio.opendirSync(path1);
    while (dir) {
      let OP = fileio.openSync(ReportPath, 0o100 | 0o2002, 0o664);
      let buf = new ArrayBuffer(40960);
      let RD = fileio.readSync(OP,buf);
      console.info("ddddd"+RD);
      let report;
      function ab2str(buf) {
        return report = String.fromCharCode.apply(null,new Uint8Array(buf));
      }
      ab2str(buf);
      let str1 = report.split(";");
      let title = [];
      let result = [];
      for(let i=0; i<Math.floor(str1.length/2); i++){
        title[i] = str1[i*2];
        result[i] = str1[i*2+1];
      }
T
tianwenzhe 已提交
275
      let FailIndex = report.indexOf('false');
T
tianwenzhe 已提交
276 277 278 279
      let FailNum = 0;
      while (FailIndex != -1) {
        console.log(FailIndex);
        FailNum++;
T
tianwenzhe 已提交
280
        FailIndex = report.indexOf('false',FailIndex + 1);
T
tianwenzhe 已提交
281 282
      }
      let failNum = (FailNum).toString();
T
tianwenzhe 已提交
283
      let PassIndex = report.indexOf('true ');
T
tianwenzhe 已提交
284 285 286 287
      let PassNum = 0;
      while (PassIndex != -1) {
        console.log(PassIndex);
        PassNum++;
T
tianwenzhe 已提交
288
        PassIndex = report.indexOf('true ',PassIndex + 1);
T
tianwenzhe 已提交
289 290 291 292 293 294 295 296
      }
      let TestNum = FailNum + PassNum;
      let testNum = (TestNum).toString();
      let arrayBuffer = new ArrayBuffer(40960);
      let bufView = new DataView(arrayBuffer);
      let serializer = new xml.XmlSerializer(bufView);
      serializer.setDeclaration();
      serializer.startElement("testsuite");
T
tianwenzhe 已提交
297
      serializer.setAttributes("name","ActsExperienceTest");
T
tianwenzhe 已提交
298
      serializer.setAttributes("time", "*");
T
tianwenzhe 已提交
299 300 301
      serializer.setAttributes("errors", "0");
      serializer.setAttributes("disabled", "0");
      serializer.setAttributes("failures", failNum);
T
tianwenzhe 已提交
302
      serializer.setAttributes("ignored", "0");
T
tianwenzhe 已提交
303
      serializer.setAttributes("tests", testNum);
T
tianwenzhe 已提交
304
      serializer.setAttributes("message", "*");
T
tianwenzhe 已提交
305
      serializer.setAttributes("modulename", "ActsExperienceTest");
T
tianwenzhe 已提交
306 307 308
      for (let i = 0; i < title.length; i++) {
        serializer.startElement("testcase");
        serializer.setAttributes("name", String(title[i]));
T
tianwenzhe 已提交
309
        serializer.setAttributes("status", "run");
T
tianwenzhe 已提交
310
        serializer.setAttributes("time", "*");
T
tianwenzhe 已提交
311
        serializer.setAttributes("classname", "ActsExperienceTest");
T
tianwenzhe 已提交
312
        serializer.setAttributes("result", String(result[i]));
T
tianwenzhe 已提交
313 314
        serializer.setAttributes("level", "*");
        serializer.setAttributes("message", "*");
T
tianwenzhe 已提交
315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342
        serializer.endElement();
      }
      serializer.endElement();

      let that = new util.TextDecoder('utf-8');
      let array = new Uint8Array(arrayBuffer);
      let serializerStr = that.decode(array);
      console.info(xmlPath);
      try{
        console.error(TAG,"write xmlPath =" +xmlPath);
        var xmlfd = fileIO.openSync(xmlPath, 0o102, 0o666);
        fileIO.writeSync(xmlfd,serializerStr);
      }catch(err){
        console.error(TAG,"read xmlPath =" + xmlPath + "error:" + err);
      }finally{
        fileIO.closeSync(xmlfd);
      }
      return;
    }
  }
}
function filewrite(name1,results,titles){
  let fd = fileio.openSync(txtPath, 0o100 | 0o2002, 0o664);
  let buf = new ArrayBuffer(4096);
  let RD = fileio.readSync(fd,buf);
  console.info("RRRRRRRRRRd"+RD);
  let report = String.fromCharCode.apply(null,new Uint8Array(buf));
  let WriteTitle = (titles).toString();
T
tianwenzhe 已提交
343 344
  let WriteResult = (results).toString();
  let number = WriteTitle.length + WriteResult.length + 2;
T
tianwenzhe 已提交
345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361
  let Index = report.indexOf(WriteTitle);
  let Log = (titles+";"+results+";").toString();
  if (Index == -1){
    fileio.writeSync(fd,Log);
  }
  else if (Index != -1){
    let key = report.substring(Index,Index+number);
    let FD = fileio.openSync(txtPath, 0o102, 0o666);
    report = report.replace(key,Log);
    let buffer = new ArrayBuffer(4096);
    let rd = fileio.readSync(FD,buffer);
    let Report = report.substring(0,rd);
    fileio.closeSync(FD);
    let Fd = fileio.openSync(txtPath, 0o102, 0o666);
    fileio.writeSync(Fd,Report);
  }
}