Camera_index.ets 12.6 KB
Newer Older
T
tianwenzhe 已提交
1
/*
T
tianwenzhe 已提交
2
 * Copyright (c) 2022-2023 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 26 27 28 29
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;
let path1 = path + '/Camera';

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

@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 = [
Q
qinliwen 已提交
42 43 44 45 46 47
    { title: 'CameraPreviewFormat', uri: 'pages/Camera/CameraPreviewFormat' },
    { title: 'CameraPhotoFormat', uri: 'pages/Camera/CameraPhotoFormat' },
    { title: 'CameraOrientation', uri: 'pages/Camera/CameraOrientation' },
    { title: 'CameraSerialPhoto', uri: 'pages/Camera/CameraSerialPhoto' },
    { title: 'CameraVideo', uri: 'pages/Camera/CameraVideo' },
    { title: 'CameraFlash', uri: 'pages/Camera/CameraFlash' },
T
tianwenzhe 已提交
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 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
  ]
  @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('Camera', 0);
    await promise.then((data) => {
      Test = data;
      console.info("Succeeded in getting value of 'Camera'. 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('Camera', 1);
    promise.then(() => {
      console.info("Succeeded in putting value of 'Camera'.");
    });
    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 已提交
103
      if (this.result === 'true'){
T
tianwenzhe 已提交
104 105 106 107 108 109 110 111 112 113 114 115 116
        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 已提交
117
      else if (this.result === 'false'){
T
tianwenzhe 已提交
118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140
        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')
141
        }.backgroundColor(Color.Black).size({ width: '40vp', height: '30vp' })
T
tianwenzhe 已提交
142 143 144 145
        .onClick(() => {
          router.back();
        })
        Row(){
T
tianwenzhe 已提交
146
          Text('Camera')
T
tianwenzhe 已提交
147 148 149 150 151 152 153 154 155 156 157 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 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260
            .fontColor(Color.White)
            .fontSize('20fp')
        }.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/report1/CameraTest.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)
            }.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);
    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(){
    let ReportPath = path1 + '/CameraReport.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 已提交
261
      let FailIndex = report.indexOf('false');
T
tianwenzhe 已提交
262 263 264 265
      let FailNum = 0;
      while (FailIndex != -1) {
        console.log(FailIndex);
        FailNum++;
T
tianwenzhe 已提交
266
        FailIndex = report.indexOf('false',FailIndex + 1);
T
tianwenzhe 已提交
267 268
      }
      let failNum = (FailNum).toString();
T
tianwenzhe 已提交
269
      let PassIndex = report.indexOf('true');
T
tianwenzhe 已提交
270 271 272 273
      let PassNum = 0;
      while (PassIndex != -1) {
        console.log(PassIndex);
        PassNum++;
T
tianwenzhe 已提交
274
        PassIndex = report.indexOf('true',PassIndex + 1);
T
tianwenzhe 已提交
275 276 277 278 279 280 281 282 283
      }
      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", "ActsCameraTest");
T
tianwenzhe 已提交
284
      serializer.setAttributes("time", "*");
T
tianwenzhe 已提交
285 286 287
      serializer.setAttributes("errors", "0");
      serializer.setAttributes("disabled", "0");
      serializer.setAttributes("failures", failNum);
T
tianwenzhe 已提交
288
      serializer.setAttributes("ignored", "0");
T
tianwenzhe 已提交
289
      serializer.setAttributes("tests", testNum);
T
tianwenzhe 已提交
290
      serializer.setAttributes("message", "*");
T
tianwenzhe 已提交
291 292 293 294
      serializer.setAttributes("modulename", "ActsCameraTest");
      for (let i = 0; i < title.length; i++) {
        serializer.startElement("testcase");
        serializer.setAttributes("name", String(title[i]));
T
tianwenzhe 已提交
295
        serializer.setAttributes("status", "run");
T
tianwenzhe 已提交
296
        serializer.setAttributes("time", "*");
T
tianwenzhe 已提交
297
        serializer.setAttributes("classname", "Camera");
T
tianwenzhe 已提交
298
        serializer.setAttributes("result", String(result[i]));
T
tianwenzhe 已提交
299 300
        serializer.setAttributes("level", "*");
        serializer.setAttributes("message", "*");
T
tianwenzhe 已提交
301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328
        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 已提交
329 330
  let WriteResult = (results).toString();
  let number = WriteTitle.length + WriteResult.length + 2;
T
tianwenzhe 已提交
331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347
  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);
  }
}