get-file-system-manager.test.js 29.3 KB
Newer Older
杜庆泉's avatar
杜庆泉 已提交
1
const PAGE_PATH = '/pages/API/get-file-system-manager/get-file-system-manager'
2 3 4


describe('ExtApi-FileManagerTest', () => {
雪洛's avatar
雪洛 已提交
5 6 7 8 9 10
  if (process.env.uniTestPlatformInfo.indexOf('web') > -1) {
    it('dummyTest', () => {
      expect(1).toBe(1)
    })
    return
  }
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
  let page;
  function getData(key = '') {
    return new Promise(async (resolve, reject) => {
      const data = await page.data()
      resolve(key ? data[key] : data)
    })
  }


  beforeAll(async () => {
    page = await program.reLaunch(PAGE_PATH)
    await page.waitFor(600);
  });

  async function isDone() {
    let isDone = await page.waitFor(async () => {
      return await page.data('done')
    })
    await page.setData({done: false})
    return isDone
  }

  it('USER_DATA_PATH test', async () => {
    // 测试 USER_DATA_PATH
    let globalUserDataPath = await getData('globalUserDataPath')

    await page.setData({
38
      logAble:false,
39 40 41 42
      recursiveVal: true,
      copyToBasePath:globalUserDataPath,
      basePath: globalUserDataPath,
      rmDirFile:'a',
43 44 45 46 47
      readDir:'a',
      writeFile:'a/1.txt',
      readFile:'a/1.txt',
      unlinkFile:'a/1.txt',
      writeFileContent:'锄禾日当午,汗滴禾下土,谁知盘中餐,粒粒皆辛苦'
48 49 50
    })

    // 先清除文件,需要清除全部可能存在的历史测试文件,避免运行失败
51
    const btnUnLinkFileButton = await page.$('#btn-clear-file')
52 53 54 55 56
    await btnUnLinkFileButton.tap()
    await isDone()


    // 清除文件夹
57
    const btnRmDirButton = await page.$('#btn-remove-dir')
58 59 60
    await btnRmDirButton.tap()
    await isDone()
    // 重新创建测试目录
61
    const btnMkdDirButton = await page.$('#btn-mkdir')
62 63 64
    await btnMkdDirButton.tap()
    await isDone()

65
    const btnReadDirButton = await page.$('#btn-read-dir')
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 110 111
    await btnReadDirButton.tap()
    await isDone()


    // 期望通过 recursive = true的 文件夹删除,得到一个空的 /a 目录
    let fileListComplete = await getData('fileListComplete')
    expect(JSON.stringify(fileListComplete)).toEqual('[]')
    let fileListSuccess = await getData('fileListSuccess')
    expect(JSON.stringify(fileListSuccess)).toEqual('[]')

    // 先测试 recursive = false 文件夹创建,期望失败
    await page.setData({
      recursiveVal: false,
      mkdirFile:'a/b/c'
    })


    await btnMkdDirButton.tap()
    await isDone()


    let lastFailError = await getData('lastFailError')
    expect(lastFailError.errCode).toEqual(1300002)
    expect(lastFailError.errMsg).toEqual('no such file or directory:unifile://usr/a/b/c')
    let lastCompleteError = await getData('lastCompleteError')
    expect(lastCompleteError.errCode).toEqual(1300002)
    expect(lastCompleteError.errMsg).toEqual('no such file or directory:unifile://usr/a/b/c')


    // 测试 recursive = true 期望文件夹创建成功
    await page.setData({
      recursiveVal: true
    })
    await btnMkdDirButton.tap()
    await isDone()

    await btnReadDirButton.tap()
    await isDone()

    // 期望通过 recursive = true的 文件夹删除,得到一个空的 /a 目录
    fileListComplete = await getData('fileListComplete')
    expect(JSON.stringify(fileListComplete)).toEqual("[\"b\"]")
    fileListSuccess = await getData('fileListSuccess')
    expect(JSON.stringify(fileListSuccess)).toEqual("[\"b\"]")

    // 测试写入文件
112
    const btnWriteFileButton = await page.$('#btn-write-file')
113 114 115 116 117 118 119 120 121 122
    await btnWriteFileButton.tap()
    await isDone()
    // 检查目录列表数量
    await btnReadDirButton.tap()
    await isDone()
    fileListComplete = await getData('fileListComplete')
    expect(JSON.stringify(fileListComplete)).toEqual("[\"b\",\"1.txt\"]")
    fileListSuccess = await getData('fileListSuccess')
    expect(JSON.stringify(fileListSuccess)).toEqual("[\"b\",\"1.txt\"]")
    // 获取和对比 文件内容
123
    const btnReadFileButton = await page.$('#btn-read-file')
124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141
    await btnReadFileButton.tap()
    await isDone()
    let readFileRet = await getData('readFileRet')
    expect(readFileRet).toEqual("锄禾日当午,汗滴禾下土,谁知盘中餐,粒粒皆辛苦")

    // 更换文件内容 获取和对比 文件md5和sha1
    await page.setData({
      writeFileContent: "If you were a teardrop;In my eye,For fear of losing you,I would never cry.And if the golden sun,Should cease to shine its light,Just one smile from you,Would make my whole world bright.",
      getFileInfoAlgorithm:"md5"
    })
    await btnWriteFileButton.tap()
    await isDone()

    await btnReadFileButton.tap()
    await isDone()
    readFileRet = await getData('readFileRet')
    expect(readFileRet).toEqual("If you were a teardrop;In my eye,For fear of losing you,I would never cry.And if the golden sun,Should cease to shine its light,Just one smile from you,Would make my whole world bright.")

142
    const btnGetFileInfoButton = await page.$('#btn-get-file-info')
143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168
    await btnGetFileInfoButton.tap()
    await isDone()

    let getFileInfoSize = await getData('getFileInfoSize')
    expect(getFileInfoSize).toEqual(185)
    let getFileInfoDigest = await getData('getFileInfoDigest')
    expect(getFileInfoDigest).toEqual("29ddd02ed3c38ccebb98884eda082cb1")
    // 切换为 sha1
    await page.setData({
      getFileInfoAlgorithm:"sha1"
    })

    await btnGetFileInfoButton.tap()
    await isDone()

    getFileInfoSize = await getData('getFileInfoSize')
    expect(getFileInfoSize).toEqual(185)
    getFileInfoDigest = await getData('getFileInfoDigest')
    expect(getFileInfoDigest).toEqual("ebef4e75783e0db499fc260d120e695005bead8a")

    // 测试 copyfile
    await page.setData({

      copyFromFile:"a/1.txt",
      copyToFile:"a/2.txt"
    })
169
    const btnCopyFileButton = await page.$('#btn-copy-file')
170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188
    await btnCopyFileButton.tap()
    await isDone()


    await btnReadDirButton.tap()
    await isDone()

    // 1.txt 2.txt 两个文件都存在
    fileListComplete = await getData('fileListComplete')
    expect(JSON.stringify(fileListComplete)).toEqual("[\"b\",\"1.txt\",\"2.txt\"]")
    fileListSuccess = await getData('fileListSuccess')
    expect(JSON.stringify(fileListSuccess)).toEqual("[\"b\",\"1.txt\",\"2.txt\"]")

    // 测试 rename
    await page.setData({
      renameFromFile:"a/2.txt",
      renameToFile:"a/3.txt"
    })

189
    const btnRenameFileButton = await page.$('#btn-rename-file')
190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207
    await btnRenameFileButton.tap()
    await isDone()

    await btnReadDirButton.tap()
    await isDone()

    // 1.txt 3.txt 两个文件都存在
    fileListComplete = await getData('fileListComplete')
    expect(JSON.stringify(fileListComplete)).toEqual("[\"b\",\"1.txt\",\"3.txt\"]")
    fileListSuccess = await getData('fileListSuccess')
    expect(JSON.stringify(fileListSuccess)).toEqual("[\"b\",\"1.txt\",\"3.txt\"]")

  });

  it('TEMP_PATH test', async () => {
    // 测试 TEMP_PATH
    let globalTempPath = await getData('globalTempPath')
    await page.setData({
208
      logAble:false,
209 210 211 212
      recursiveVal: true,
      basePath: globalTempPath,
      copyToBasePath:globalTempPath,
      rmDirFile:'a',
213
      mkdirFile: 'a',
214 215 216 217 218
      unlinkFile:'a/我们经历了一场兵慌马乱的战争.1@2#3$4%5^6&7*8(9)0+-qwertyuiopasdfghjklzxcvbnm;,/中文路径/张三/name/中文文件.mock'
    })


    // 先清除文件,需要清除全部可能存在的历史测试文件,避免运行失败
219
    const btnUnLinkFileButton = await page.$('#btn-unlink-file')
220 221 222 223 224
    await btnUnLinkFileButton.tap()
    await isDone()


    // 清除文件夹
225
    const btnRmDirButton = await page.$('#btn-remove-dir')
226 227 228 229
    await btnRmDirButton.tap()
    await isDone()

    // 重新创建测试目录
230
    const btnMkdDirButton = await page.$('#btn-mkdir')
231 232 233
    await btnMkdDirButton.tap()
    await isDone()

234
    const btnReadDirButton = await page.$('#btn-read-dir')
235 236 237 238 239 240
    await btnReadDirButton.tap()
    await isDone()


    // 期望通过 recursive = true的 文件夹删除,得到一个空的 /a 目录
    let fileListComplete = await getData('fileListComplete')
241
    expect(JSON.stringify(fileListComplete)).toEqual("[]")
242
    let fileListSuccess = await getData('fileListSuccess')
243
    expect(JSON.stringify(fileListSuccess)).toEqual("[]")
244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280

    // 测试 创建多层级文件目录
    await page.setData({
      recursiveVal: true,
      mkdirFile:'a/b/c/d/e/f/g/h/i/g/k/l/m/n/o/p/q/r/s/t/u/v/w/x/y/z/中文路径/张三/test',
    })

    await btnMkdDirButton.tap()
    await isDone()

    await btnReadDirButton.tap()
    await isDone()

    fileListComplete = await getData('fileListComplete')
    expect(JSON.stringify(fileListComplete)).toEqual("[\"b\"]")
    fileListSuccess = await getData('fileListSuccess')
    expect(JSON.stringify(fileListSuccess)).toEqual("[\"b\"]")

    // 测试 创建包含中文特殊符号的目录
    await page.setData({
      recursiveVal: true,
      mkdirFile:'a/我们经历了一场兵慌马乱的战争.1@2#3$4%5^6&7*8(9)0+-qwertyuiopasdfghjklzxcvbnm;,/中文路径/张三/name',
    })
    await btnMkdDirButton.tap()
    await isDone()

    await btnReadDirButton.tap()
    await isDone()

    // 期望通过 recursive = true的 文件夹删除,得到一个空的 /a 目录
    fileListComplete = await getData('fileListComplete')
    expect(JSON.stringify(fileListComplete)).toEqual("[\"b\",\"我们经历了一场兵慌马乱的战争.1@2#3$4%5^6&7*8(9)0+-qwertyuiopasdfghjklzxcvbnm;,\"]")
    fileListSuccess = await getData('fileListSuccess')
    expect(JSON.stringify(fileListSuccess)).toEqual("[\"b\",\"我们经历了一场兵慌马乱的战争.1@2#3$4%5^6&7*8(9)0+-qwertyuiopasdfghjklzxcvbnm;,\"]")

    /**
     * 从资源文件中读取图片为base64,测试写入较大文件场景
281
     * 'static/test-image/logo.ico' 注意,依赖这个资源文件,不能删除
282 283
     */
    await page.setData({
284
      basePath: "",
285
      readFile:'static/test-image/logo.ico',
286
      readFileEncoding:'base64'
287 288 289 290
    })


    // 获取和对比 文件内容
291
    const btnReadFileButton = await page.$('#btn-read-file')
292 293 294
    await btnReadFileButton.tap()
    await isDone()
    let readFileRet = await getData('readFileRet')
295
    expect(readFileRet.length).toEqual(208544)
296
    let endStr = readFileRet.substring(readFileRet.length - 10)
297
    expect(endStr).toEqual("///////w==")
298 299 300 301 302 303 304 305

    await page.setData({
      basePath: globalTempPath,
      writeFile:'a/我们经历了一场兵慌马乱的战争.1@2#3$4%5^6&7*8(9)0+-qwertyuiopasdfghjklzxcvbnm;,/中文路径/张三/name/中文文件.mock',
      writeFileContent:readFileRet
    })


306
    const btnWriteFileButton = await page.$('#btn-write-file')
307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331
    await btnWriteFileButton.tap()
    await isDone()

    // 获取文件列表,判断是否写入成功,同时置空base64内容 避免影响实时查看状态
    await page.setData({
      readDir:'a/我们经历了一场兵慌马乱的战争.1@2#3$4%5^6&7*8(9)0+-qwertyuiopasdfghjklzxcvbnm;,/中文路径/张三/name',
      readFileRet:'',
      writeFileContent:''
    })

    // 检查目录列表数量
    await btnReadDirButton.tap()
    await isDone()
    fileListComplete = await getData('fileListComplete')
    expect(JSON.stringify(fileListComplete)).toEqual("[\"中文文件.mock\"]")
    fileListSuccess = await getData('fileListSuccess')
    expect(JSON.stringify(fileListSuccess)).toEqual("[\"中文文件.mock\"]")


    // 更换文件内容 获取和对比 文件md5和sha1
    await page.setData({
      getFileInfoFile:'a/我们经历了一场兵慌马乱的战争.1@2#3$4%5^6&7*8(9)0+-qwertyuiopasdfghjklzxcvbnm;,/中文路径/张三/name/中文文件.mock',
      getFileInfoAlgorithm:"md5",
    })

332
    const btnGetFileInfoButton = await page.$('#btn-get-file-info')
333 334 335 336
    await btnGetFileInfoButton.tap()
    await isDone()

    let getFileInfoSize = await getData('getFileInfoSize')
337
    expect(getFileInfoSize).toEqual(208544)
338
    let getFileInfoDigest = await getData('getFileInfoDigest')
339
    expect(getFileInfoDigest).toEqual("486f75ea76625f8c103cac4bc9c49511")
340 341 342 343 344 345 346 347 348 349

    // 切换为 sha1
    await page.setData({
      getFileInfoAlgorithm:"sha1"
    })

    await btnGetFileInfoButton.tap()
    await isDone()

    getFileInfoSize = await getData('getFileInfoSize')
350
    expect(getFileInfoSize).toEqual(208544)
351
    getFileInfoDigest = await getData('getFileInfoDigest')
352
    expect(getFileInfoDigest).toEqual("1830169a16e7c860beff4a3b0975ba0b6f775f9e")
353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373

    // 测试不支持的摘要算法,期望返回错误
    await page.setData({
      getFileInfoAlgorithm:"sha256"
    })

    await btnGetFileInfoButton.tap()
    await isDone()

    let lastFailError = await getData('lastFailError')
    expect(lastFailError.errCode).toEqual(1300022)
    let lastCompleteError = await getData('lastCompleteError')
    expect(lastCompleteError.errCode).toEqual(1300022)


    // rename 到一个没有提前创建过的目录,期望返回错误
    await page.setData({
      renameFromFile:"a/我们经历了一场兵慌马乱的战争.1@2#3$4%5^6&7*8(9)0+-qwertyuiopasdfghjklzxcvbnm;,/中文路径/张三/name/中文文件.mock",
      renameToFile:"a/没有提前创建的目录/3.txt"
    })

374
    const btnRenameFileButton = await page.$('#btn-rename-file')
375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410
    await btnRenameFileButton.tap()
    await isDone()

    lastFailError = await getData('lastFailError')
    expect(lastFailError.errCode).toEqual(1300002)
    lastCompleteError = await getData('lastCompleteError')
    expect(lastCompleteError.errCode).toEqual(1300002)

    // 非递归创建一级目录。期望成功
    await page.setData({
      recursiveVal: false,
      mkdirFile:'a/提前创建的目录',
    })

    await btnMkdDirButton.tap()
    await isDone()

    await page.setData({
      readDir:'a',
    })

    await btnReadDirButton.tap()
    await isDone()

    fileListComplete = await getData('fileListComplete')
    expect(JSON.stringify(fileListComplete)).toEqual("[\"b\",\"我们经历了一场兵慌马乱的战争.1@2#3$4%5^6&7*8(9)0+-qwertyuiopasdfghjklzxcvbnm;,\",\"提前创建的目录\"]")
    fileListSuccess = await getData('fileListSuccess')
    expect(JSON.stringify(fileListSuccess)).toEqual("[\"b\",\"我们经历了一场兵慌马乱的战争.1@2#3$4%5^6&7*8(9)0+-qwertyuiopasdfghjklzxcvbnm;,\",\"提前创建的目录\"]")


    await page.setData({

      copyFromFile:"a/我们经历了一场兵慌马乱的战争.1@2#3$4%5^6&7*8(9)0+-qwertyuiopasdfghjklzxcvbnm;,/中文路径/张三/name/中文文件.mock",
      copyToFile:"a/提前创建的目录/4.txt"
    })

411
      const btnCopyFileButton = await page.$('#btn-copy-file')
412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427
      await btnCopyFileButton.tap()
      await isDone()

    await page.setData({
      readDir:'a/提前创建的目录',
    })

    await btnReadDirButton.tap()
    await isDone()

    fileListComplete = await getData('fileListComplete')
    expect(JSON.stringify(fileListComplete)).toEqual("[\"4.txt\"]")
    fileListSuccess = await getData('fileListSuccess')
    expect(JSON.stringify(fileListSuccess)).toEqual("[\"4.txt\"]")

    await page.setData({
428 429
      unlinkFile:'a/提前创建的目录/4.txt',
      rmDirFile:'a/提前创建的目录'
430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452
    })
    await btnUnLinkFileButton.tap()
    await isDone()

    await btnReadDirButton.tap()
    await isDone()

    fileListComplete = await getData('fileListComplete')
    expect(JSON.stringify(fileListComplete)).toEqual("[]")
    fileListSuccess = await getData('fileListSuccess')
    expect(JSON.stringify(fileListSuccess)).toEqual("[]")

  });



  it('CROSS DIR test', async () => {
    /**
     * 跨越用户目录和代码资源目录
     */
    let globalRootPath = await getData('globalRootPath')
    await page.setData({
      recursiveVal: true,
453
      logAble:false,
454 455 456 457 458 459 460 461 462 463
      basePath: globalRootPath,
      readDir:'a',
      rmDirFile:'a',
      mkdirFile:'a',
      accessFile:'a/从代码目录拷贝的资源.png',
      unlinkFile:'a/从代码目录拷贝的资源.png'
    })


    // 先清除文件,需要清除全部可能存在的历史测试文件,避免运行失败
464
    const btnClearFileButton = await page.$('#btn-clear-file')
465
    await btnClearFileButton.tap()
466 467 468 469
    await isDone()


    // 清除文件夹
470
    const btnRmDirButton = await page.$('#btn-remove-dir')
471 472 473 474
    await btnRmDirButton.tap()
    await isDone()

    // 重新创建测试目录,期望通过 recursive = true的 文件夹删除,得到一个空的 /a 目录
475
    const btnMkdDirButton = await page.$('#btn-mkdir')
476 477 478
    await btnMkdDirButton.tap()
    await isDone()

479
    const btnReadDirButton = await page.$('#btn-read-dir')
480 481 482 483 484 485 486 487 488 489
    await btnReadDirButton.tap()
    await isDone()

    let fileListComplete = await getData('fileListComplete')
    expect(JSON.stringify(fileListComplete)).toEqual('[]')
    let fileListSuccess = await getData('fileListSuccess')
    expect(JSON.stringify(fileListSuccess)).toEqual('[]')


    // 检查资源文件,此时不存在
490
    const btnAccessFileButton = await page.$('#btn-access-file')
491 492 493 494 495 496 497 498 499
    await btnAccessFileButton.tap()
    await isDone()

    let accessFileRet = await getData("accessFileRet")
    expect(accessFileRet).toEqual('')


    // 准备从资源目录拷贝png
    await page.setData({
500
      basePath: "",
501 502
      unlinkFile:'static/test-image/logo.ico',
      accessFile:'static/test-image/logo.ico',
503 504 505 506 507 508 509 510 511
    })
    // 检查资源文件,期望存在
    await btnAccessFileButton.tap()
    await isDone()

    accessFileRet = await getData("accessFileRet")
    expect(accessFileRet).toEqual('access:ok')

    // 尝试删除资源,期望失败
512
    const btnUnLinkFileButton = await page.$('#btn-unlink-file')
513 514 515 516 517 518 519 520 521 522 523
    await btnUnLinkFileButton.tap()
    await isDone()

    await btnAccessFileButton.tap()
    await isDone()

    accessFileRet = await getData("accessFileRet")
    expect(accessFileRet).toEqual('access:ok')
    // 复制资源到 root目录
    await page.setData({
      copyToBasePath:globalRootPath,
524
      copyFromFile:"static/test-image/logo.ico",
525 526
      copyToFile:"a/从代码目录拷贝的资源.png"
    })
527
    const btnCopyFileButton = await page.$('#btn-copy-file')
528 529 530 531 532 533 534 535
    await btnCopyFileButton.tap()
    await isDone()

    // 检查期望 root 目录中图片文件存在
    await page.setData({
      basePath:globalRootPath,
      unlinkFile:'a/从代码目录拷贝的资源.png',
      accessFile:'a/从代码目录拷贝的资源.png',
536
      rmDirFile:'a',
537 538 539 540 541 542 543 544 545 546
    })
    await btnAccessFileButton.tap()
    await isDone()

    accessFileRet = await getData("accessFileRet")
    expect(accessFileRet).toEqual('access:ok')

    await btnUnLinkFileButton.tap()
    await isDone()

547 548
    await btnAccessFileButton.tap()
    await isDone()
549 550 551 552

    accessFileRet = await getData("accessFileRet")
    expect(accessFileRet).toEqual('')

553
    // 从页面的按钮触发一次文件复制
554
    const btnCopyStaticFileButton = await page.$('#btn-copyStatic-file')
555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577
    await btnCopyStaticFileButton.tap()
    await isDone()

    await btnReadDirButton.tap()
    await isDone()

    fileListComplete = await getData('fileListComplete')
    expect(JSON.stringify(fileListComplete)).toEqual("[\"mock.json\"]")
    fileListSuccess = await getData('fileListSuccess')
    expect(JSON.stringify(fileListSuccess)).toEqual("[\"mock.json\"]")

    // 从页面的按钮触发一次文件清空
    await btnClearFileButton.tap()
    await isDone()

    await btnReadDirButton.tap()
    await isDone()

    fileListComplete = await getData('fileListComplete')
    expect(JSON.stringify(fileListComplete)).toEqual("[]")
    fileListSuccess = await getData('fileListSuccess')
    expect(JSON.stringify(fileListSuccess)).toEqual("[]")

578 579 580
  });


581 582 583 584 585 586 587
  it('write and read', async () => {
    /**
     * 测试writefile readfile 各个参数是否符合预期
     */
    let globalTempPath = await getData('globalTempPath')
    await page.setData({
      recursiveVal: true,
588
      logAble:false,
589 590 591 592 593 594 595 596 597 598 599 600 601 602 603
      basePath: globalTempPath,
      readDir:'d',
      rmDirFile:'d',
      mkdirFile:'d',
      writeFileContent:"我爱北京天安门,天安门前太阳升",
      writeFileEncoding:"utf-8",
      readFileEncoding:"utf-8",
      unlinkFile:'d/write.bing',
      writeFile:'d/write.bing',
      readFile:'d/write.bing',
      getFileInfoFile:'d/write.bing',
      getFileInfoAlgorithm:"sha1"
    })

    // 先清除文件,需要清除全部可能存在的历史测试文件,避免运行失败
604
    const btnUnLinkFileButton = await page.$('#btn-unlink-file')
605 606 607 608
    await btnUnLinkFileButton.tap()
    await isDone()

    // 清除文件夹
609
    const btnRmDirButton = await page.$('#btn-remove-dir')
610 611 612 613
    await btnRmDirButton.tap()
    await isDone()

    // 重新创建测试目录,期望通过 recursive = true的 文件夹删除,得到一个空的 /a 目录
614
    const btnMkdDirButton = await page.$('#btn-mkdir')
615 616 617
    await btnMkdDirButton.tap()
    await isDone()

618
    const btnReadDirButton = await page.$('#btn-read-dir')
619 620 621 622 623 624 625 626 627
    await btnReadDirButton.tap()
    await isDone()

    let fileListComplete = await getData('fileListComplete')
    expect(JSON.stringify(fileListComplete)).toEqual('[]')
    let fileListSuccess = await getData('fileListSuccess')
    expect(JSON.stringify(fileListSuccess)).toEqual('[]')

    // 先用utf-8 写入内容
628
    const btnWriteFileButton = await page.$('#btn-write-file')
629 630 631
    await btnWriteFileButton.tap()
    await isDone()

632
    const btnReadFileButton = await page.$('#btn-read-file')
633 634 635 636 637
    await btnReadFileButton.tap()
    await isDone()
    let readFileRet = await getData('readFileRet')
    expect(readFileRet).toEqual("我爱北京天安门,天安门前太阳升")

638
    const btnGetFileInfoButton = await page.$('#btn-get-file-info')
639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719
    await btnGetFileInfoButton.tap()
    await isDone()

    let getFileInfoSize = await getData('getFileInfoSize')
    expect(getFileInfoSize).toEqual(45)
    let getFileInfoDigest = await getData('getFileInfoDigest')
    expect(getFileInfoDigest).toEqual("2ae9c7672ff6c1e7c7e6a0bb4e74a6f06b39350b")

    // 尝试读取base64 信息
    await page.setData({
      readFileEncoding:"base64",
    })

    await btnReadFileButton.tap()
    await isDone()
    readFileRet = await getData('readFileRet')
    expect(readFileRet).toEqual("5oiR54ix5YyX5Lqs5aSp5a6J6Zeo77yM5aSp5a6J6Zeo5YmN5aSq6Ziz5Y2H")
    // 测试ascii,需要特别测试 ascii 写入非法字符的情况,因为微信的常量字符编码和android原生有差异。

    await page.setData({
      writeFileContent:"丙辰中秋,欢饮达旦,大醉,作此篇,兼怀子由。明月几时有?把酒问青天。不知天上宫阙,今夕是何年。我欲乘风归去,又恐琼楼玉宇,高处不胜寒。起舞弄清影,何似在人间",
      writeFileEncoding:"ascii",
      readFileEncoding:"base64",
    })

    await btnWriteFileButton.tap()
    await isDone()

    await btnGetFileInfoButton.tap()
    await isDone()

    getFileInfoSize = await getData('getFileInfoSize')
    expect(getFileInfoSize).toEqual(78)
    getFileInfoDigest = await getData('getFileInfoDigest')
    expect(getFileInfoDigest).toEqual("4ac7a65055628818341c2ad86ddc4205d8503801")

    await btnReadFileButton.tap()
    await isDone()
    readFileRet = await getData('readFileRet')
    expect(readFileRet).toEqual("GbAtywwibr7mDCeJDFxkxwx8AFAxAg4I4PYJH4pS7lIpAg3lKQqrGQzKFS9VdAIRMljOUrsMyFA8fImHDNgEDdzSAnceBAVxDFU8KLr0")

    // 尝试写入合法ascii
    await page.setData({
      writeFileContent:"hello jack.hello marry.",
      writeFileEncoding:"ascii",
      readFileEncoding:"ascii",
    })

    await btnWriteFileButton.tap()
    await isDone()

    await btnReadFileButton.tap()
    await isDone()
    readFileRet = await getData('readFileRet')
    expect(readFileRet).toEqual("hello jack.hello marry.")

    // 写入base64 获取 中文
    await page.setData({
      writeFileContent:"5LiZ6L6w5Lit56eL77yM5qyi6aWu6L6+5pem77yM5aSn6YaJ77yM5L2c5q2k56+H77yM5YW85oCA5a2Q55Sx44CC5piO5pyI5Yeg5pe25pyJ77yf5oqK6YWS6Zeu6Z2S5aSp44CC5LiN55+l5aSp5LiK5a6r6ZiZ77yM5LuK5aSV5piv5L2V5bm044CC5oiR5qyy5LmY6aOO5b2S5Y6777yM5Y+I5oGQ55C85qW8546J5a6H77yM6auY5aSE5LiN6IOc5a+S44CC6LW36Iie5byE5riF5b2x77yM5L2V5Ly85Zyo5Lq66Ze0",
      writeFileEncoding:"base64",
      readFileEncoding:"utf-8",
    })

    await btnWriteFileButton.tap()
    await isDone()

    await btnReadFileButton.tap()
    await isDone()
    readFileRet = await getData('readFileRet')
    expect(readFileRet).toEqual("丙辰中秋,欢饮达旦,大醉,作此篇,兼怀子由。明月几时有?把酒问青天。不知天上宫阙,今夕是何年。我欲乘风归去,又恐琼楼玉宇,高处不胜寒。起舞弄清影,何似在人间")

    await page.setData({
      readFileEncoding:"base64",
    })

    await btnReadFileButton.tap()
    await isDone()
    readFileRet = await getData('readFileRet')
    expect(readFileRet).toEqual("5LiZ6L6w5Lit56eL77yM5qyi6aWu6L6+5pem77yM5aSn6YaJ77yM5L2c5q2k56+H77yM5YW85oCA5a2Q55Sx44CC5piO5pyI5Yeg5pe25pyJ77yf5oqK6YWS6Zeu6Z2S5aSp44CC5LiN55+l5aSp5LiK5a6r6ZiZ77yM5LuK5aSV5piv5L2V5bm044CC5oiR5qyy5LmY6aOO5b2S5Y6777yM5Y+I5oGQ55C85qW8546J5a6H77yM6auY5aSE5LiN6IOc5a+S44CC6LW36Iie5byE5riF5b2x77yM5L2V5Ly85Zyo5Lq66Ze0")

  });
720

721 722 723 724 725 726 727 728 729 730 731 732 733 734
  it('stat and asset test', async () => {
    // 测试 USER_DATA_PATH //globalTempPath
    let globalRootPath = await getData('globalRootPath')

    await page.setData({
      recursiveVal: true,
      copyToBasePath:globalRootPath,
      basePath: globalRootPath,
      rmDirFile:'a',
      mkdirFile:'a',
      unlinkFile:'a/1.txt',
    })

    // 先清除文件,需要清除全部可能存在的历史测试文件,避免运行失败
735
    const btnUnLinkFileButton = await page.$('#btn-unlink-file')
736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751
    await btnUnLinkFileButton.tap()
    await isDone()

    await page.setData({
      unlinkFile:'a/2.txt',
    })
    await btnUnLinkFileButton.tap()
    await isDone()

    await page.setData({
      unlinkFile:'a/m/3.txt',
    })
    await btnUnLinkFileButton.tap()
    await isDone()

    // // 清除文件夹
752
    const btnRmDirButton = await page.$('#btn-remove-dir')
753 754 755
    await btnRmDirButton.tap()
    await isDone()
    // // 重新创建测试目录
756
    const btnMkdDirButton = await page.$('#btn-mkdir')
757 758 759
    await btnMkdDirButton.tap()
    await isDone()

760
    const btnReadDirButton = await page.$('#btn-read-dir')
761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782
    await btnReadDirButton.tap()
    await isDone()


    // 期望通过 recursive = true的 文件夹删除,得到一个空的 /a 目录
    let fileListComplete = await getData('fileListComplete')
    expect(JSON.stringify(fileListComplete)).toEqual('[]')
    let fileListSuccess = await getData('fileListSuccess')
    expect(JSON.stringify(fileListSuccess)).toEqual('[]')

    // 写入一个文件
    await page.setData({
      writeFileContent: "锄禾日当午,汗滴禾下土,谁知盘中餐,粒粒皆辛苦",
      writeFileEncoding:"utf-8",
      writeFile:'a/1.txt',
      recursiveVal:false,
      statFile:'a/1.txt',
    })

    let lastFailError = await getData('lastFailError')
    console.log(lastFailError)

783
    const btnWriteFileButton = await page.$('#btn-write-file')
784 785 786
    await btnWriteFileButton.tap()
    await isDone()

787
    const btnStatFileButton = await page.$('#btn-stat-file')
788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824
    await btnStatFileButton.tap()
    await isDone()

    // 读取单个文件信息
    let statsRet = await getData('statsRet')
    expect(statsRet.length).toEqual(1)
    expect(statsRet[0].path).toEqual('/storage/emulated/0/Android/data/io.dcloud.uniappx/a/1.txt')
    expect(statsRet[0].stats.size).toEqual(69)

    /**
     * 创建子目录和子目录文件,测试recursive参数
     */
    await page.setData({
      writeFileContent: "1234567890",
      writeFileEncoding:"ascii",
      writeFile:'a/2.txt',
      basePath: globalRootPath,
      recursiveVal:false,
      statFile:'a',
      mkdirFile:'a/m',
    })


    await btnWriteFileButton.tap()
    await isDone()

    // 创建子目录
    await btnMkdDirButton.tap()
    await isDone()

    // 复制一份文件到 /a/m/3.txt
    await page.setData({
      //  asset 只能正式版测试,这里只能模拟返回路径
      basePath:'',
      copyFromFile:'file:///android_asset/uni-uts/uni-prompt/toast_error.png',
      copyToFile:'a/m/3.txt',
    })
825
    const btnCopyFileButton = await page.$('#btn-copy-file')
826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851
    await btnCopyFileButton.tap()
    await isDone()


    await page.setData({
      basePath: globalRootPath,
      recursiveVal:true,
      statFile:'a',
    })

    await btnStatFileButton.tap()
    await isDone()

    // 读取全部文件信息
    statsRet = await getData('statsRet')
    console.log(statsRet)
    expect(statsRet.length).toEqual(5)
    expect(statsRet[0].path).toEqual('/storage/emulated/0/Android/data/io.dcloud.uniappx/a')
    expect(statsRet[0].stats.size).toEqual(0)

    expect(statsRet[2].path).toEqual('/storage/emulated/0/Android/data/io.dcloud.uniappx/a/2.txt')
    expect(statsRet[2].stats.size).toEqual(10)

    expect(statsRet[4].path).toEqual('/storage/emulated/0/Android/data/io.dcloud.uniappx/a/m/3.txt')
    expect(statsRet[4].stats.size).toEqual(5842)

852

853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887
    // 清理文件,避免影响其他测试用例
    await page.setData({
      unlinkFile:'a/1.txt',
    })
    await btnUnLinkFileButton.tap()
    await isDone()

    await page.setData({
      unlinkFile:'a/2.txt',
    })
    await btnUnLinkFileButton.tap()
    await isDone()

    await page.setData({
      unlinkFile:'a/m/3.txt',
      rmDirFile:'a',
      readDir:'a',
      recursiveVal:true,
    })
    await btnUnLinkFileButton.tap()
    await isDone()

    await btnRmDirButton.tap()
    await isDone()

    await btnReadDirButton.tap()
    await isDone()

    // 期望通过 recursive = true的 文件夹删除,得到一个空的 /a 目录
    fileListComplete = await getData('fileListComplete')
    expect(JSON.stringify(fileListComplete)).toEqual('[]')
    fileListSuccess = await getData('fileListSuccess')
    expect(JSON.stringify(fileListSuccess)).toEqual('[]')

  });
杜庆泉's avatar
杜庆泉 已提交
888

889
});