ArkUI_index.ets 13.3 KB
Newer Older
T
tianwenzhe 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
/*
 * Copyright (c) 2022 Huawei Device Co., Ltd.
 * 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.
 */
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';
T
tianwenzhe 已提交
21
import data_preferences from '@ohos.data.preferences';
T
tianwenzhe 已提交
22 23 24

const TAG = 'ParseXML';
let path = globalThis.dir;
T
tianwenzhe 已提交
25
let path1 = path + '/ArkUI';
T
tianwenzhe 已提交
26

T
tianwenzhe 已提交
27 28
let VarColor = [];
let ColorBackPath = path + '/ArkUIColorBack.txt';
T
tianwenzhe 已提交
29 30
let txtPath = path1 + '/ArkUIReport.txt';
let xmlPath =path1 + '/ArkUITest.xml';
T
tianwenzhe 已提交
31 32 33 34 35 36 37 38

@Entry
@Component
struct IndexPage {
  @State ClearAll : boolean = false;
  private current : number = undefined;
  @State count : number = 0;
  @State result : string = '';
T
tianwenzhe 已提交
39
  @State TEST : number = 0;
T
tianwenzhe 已提交
40
  private TestCaseList = [
T
tianwenzhe 已提交
41
    {title:'ScrollingList',uri:'pages/ArkUI/ScrollListTest'},
T
tianwenzhe 已提交
42 43 44 45 46 47 48 49 50 51 52 53 54 55
    {title:'TouchMove',uri:'pages/ArkUI/TouchMoveTest'},
    {title:'PinchGesture',uri:'pages/ArkUI/PinchGestureTest'},
    {title:'RotationGesture',uri:'pages/ArkUI/RotationGestureTest'},
    {title:'SwipeGesture',uri:'pages/ArkUI/SwipeGestureTest'},
    {title:'LongPressGesture',uri:'pages/ArkUI/LongPressGesture'},
    {title:'TapGesture',uri:'pages/ArkUI/TapGesture'},
    {title:'PanGesture',uri:'pages/ArkUI/PanGesture'},
    {title:'CanvasFillStyle',uri:'pages/ArkUI/CanvasFillStyle'},
    {title:'CanvasLineWidth',uri:'pages/ArkUI/CanvasLineWidth'},
    {title:'CanvasStrokeStyle',uri:'pages/ArkUI/CanvasStrokeStyle'},
    {title:'CanvasLineCap',uri:'pages/ArkUI/CanvasLineCap'},
    {title:'CanvasLineJoin',uri:'pages/ArkUI/CanvasLineJoin'},
    {title:'CanvasFont',uri:'pages/ArkUI/CanvasFont'},
    {title:'CanvasTextAlign',uri:'pages/ArkUI/CanvasTextAlign'},
T
tianwenzhe 已提交
56
    {title:'CanvasTextBaseline',uri:'pages/ArkUI/CanvasTextBaseline'},
T
tianwenzhe 已提交
57 58 59 60 61 62 63 64
    {title:'CanvasGlobalAlpha',uri:'pages/ArkUI/CanvasGlobalAlpha'},
    {title:'CanvasLineDashOffset',uri:'pages/ArkUI/CanvasLineDashOffset'},
    {title:'CanvasGlobalCompositeOperation',uri:'pages/ArkUI/CanvasGlobalCompositeOperation'},
    {title:'CanvasShadowBlur',uri:'pages/ArkUI/CanvasShadowBlur'},
    {title:'CanvasShadowColor',uri:'pages/ArkUI/CanvasShadowColor'},
    {title:'CanvasShadowOffsetX',uri:'pages/ArkUI/CanvasShadowOffsetX'},
    {title:'CanvasShadowOffsetY',uri:'pages/ArkUI/CanvasShadowOffsetY'},
  ]
T
tianwenzhe 已提交
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109
  @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;
    });
    promise = preferences.get('ArkUI', 0);
    await promise.then((data) => {
      Test = data;
      console.info("Succeeded in getting value of 'ArkUI'. Data: " + data);
    });

    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);
    }
    promise = preferences.put('ArkUI', 1);
    promise.then(() => {
      console.info("Succeeded in putting value of 'ArkUI'.");
    });
    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);

T
tianwenzhe 已提交
110 111 112 113 114
    if (this.count === 1){
      this.result = router.getParams()['result'];
      let titles = router.getParams()['title'];
      let name1 = '刚刚点进了哪个用例:'+ titles;
      let results = this.result;
T
tianwenzhe 已提交
115 116 117 118
      let WriteTitle = (titles).toString();
      let number = WriteTitle.length + 11;
      let Index = ColorBack.indexOf(WriteTitle);

T
tianwenzhe 已提交
119
      if (this.result === 'true '){
T
tianwenzhe 已提交
120
        this.ColorObject[this.current] = '#ff008000';
T
tianwenzhe 已提交
121 122 123 124 125 126 127 128 129 130
        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);
131
        filewrite(name1,results,titles);
T
tianwenzhe 已提交
132
      }
T
tianwenzhe 已提交
133
      else if (this.result === 'false'){
T
tianwenzhe 已提交
134
        this.ColorObject[this.current] = '#ffff0000';
T
tianwenzhe 已提交
135 136 137 138 139 140 141 142 143 144
        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);
T
tianwenzhe 已提交
145 146
        filewrite(name1,results,titles);
      }
147
      else {
T
tianwenzhe 已提交
148 149 150 151 152 153 154 155 156
        this.ColorObject[this.current] = this.ColorObject[this.current];
      }
    }
  }
  build(){
    Column(){
      Row() {
        Button() {
          Image($r('app.media.ic_public_back')).width('20vp').height('18vp')
157
        }.backgroundColor(Color.Black).size({ width: '40vp', height: '30vp' })
T
tianwenzhe 已提交
158 159 160 161 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 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232
        .onClick(() => {
          router.back();
        })
        Row(){
          Text('ArkUI SubSystem')
            .fontColor(Color.White)
            .fontSize('22fp')
        }.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({
              message: "报告已生成,如需查看通过命令行输入'hdc_std file recv /data/app/el2/100/base/com.example.actsvalidator/haps/entry/files/ArkUI/ArkUITest.xml -本地路径'",
              confirm:{
                value:'OK',
                action:()=>{
                  prompt.showToast({
                    message: '报告已生成', duration: 1000
                  })
                }
              },
              cancel: () => {
                prompt.showToast({
                  message: '报告已生成', duration: 1000
                });
              }
            })
          })
        }
      }.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)
T
tianwenzhe 已提交
233
            }.width('100%').height(50).alignItems(VerticalAlign.Center).backgroundColor(this.count===0&&this.TEST===0?'#ff808080':this.ColorObject[index])
T
tianwenzhe 已提交
234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249
            .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);
T
tianwenzhe 已提交
250 251 252 253 254
    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);
    }
T
tianwenzhe 已提交
255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276
  }
  ReadTextParseXml(){
    let ReportPath = path1 + '/ArkUIReport.txt';
    let dir = fileio.opendirSync(path1);
    console.info("ssssss"+dir);
    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 已提交
277
      let FailIndex = report.indexOf('false');
T
tianwenzhe 已提交
278 279 280 281
      let FailNum = 0;
      while (FailIndex != -1) {
        console.log(FailIndex);
        FailNum++;
T
tianwenzhe 已提交
282
        FailIndex = report.indexOf('false',FailIndex + 1);
T
tianwenzhe 已提交
283 284
      }
      let failNum = (FailNum).toString();
T
tianwenzhe 已提交
285
      let PassIndex = report.indexOf('true ');
T
tianwenzhe 已提交
286 287 288 289
      let PassNum = 0;
      while (PassIndex != -1) {
        console.log(PassIndex);
        PassNum++;
T
tianwenzhe 已提交
290
        PassIndex = report.indexOf('true ',PassIndex + 1);
T
tianwenzhe 已提交
291 292 293 294 295 296 297 298 299
      }
      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");
      serializer.setAttributes("name","ActsArkUITest");
T
tianwenzhe 已提交
300
      serializer.setAttributes("time", "*");
T
tianwenzhe 已提交
301 302 303
      serializer.setAttributes("errors", "0");
      serializer.setAttributes("disabled", "0");
      serializer.setAttributes("failures", failNum);
T
tianwenzhe 已提交
304
      serializer.setAttributes("ignored", "0");
T
tianwenzhe 已提交
305
      serializer.setAttributes("tests", testNum);
T
tianwenzhe 已提交
306
      serializer.setAttributes("message", "*");
T
tianwenzhe 已提交
307 308 309 310
      serializer.setAttributes("modulename", "ActsArkUITest");
      for (let i = 0; i < title.length; i++) {
        serializer.startElement("testcase");
        serializer.setAttributes("name", String(title[i]));
T
tianwenzhe 已提交
311
        serializer.setAttributes("status", "run");
T
tianwenzhe 已提交
312
        serializer.setAttributes("time", "*");
T
tianwenzhe 已提交
313
        serializer.setAttributes("classname", "ActsArkUITest");
T
tianwenzhe 已提交
314
        serializer.setAttributes("result", String(result[i]));
T
tianwenzhe 已提交
315 316
        serializer.setAttributes("level", "*");
        serializer.setAttributes("message", "*");
T
tianwenzhe 已提交
317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338
        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){
T
tianwenzhe 已提交
339
  let fd = fileio.openSync(txtPath, 0o100 | 0o2002, 0o664);
T
tianwenzhe 已提交
340 341 342 343 344
  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 已提交
345 346
  let WriteResult = (results).toString();
  let number = WriteTitle.length + WriteResult.length + 2;
T
tianwenzhe 已提交
347 348 349 350 351 352 353
  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);
T
tianwenzhe 已提交
354
    let FD = fileio.openSync(txtPath, 0o102, 0o666);
T
tianwenzhe 已提交
355 356 357 358 359
    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);
T
tianwenzhe 已提交
360
    let Fd = fileio.openSync(txtPath, 0o102, 0o666);
T
tianwenzhe 已提交
361 362 363
    fileio.writeSync(Fd,Report);
  }
}