get-file-system-manager.test.js 43.4 KB
Newer Older
W
WOSHIMAHAIFENG 已提交
1
jest.setTimeout(50000);
杜庆泉's avatar
杜庆泉 已提交
2
const PAGE_PATH = '/pages/API/get-file-system-manager/get-file-system-manager'
3 4 5


describe('ExtApi-FileManagerTest', () => {
6
  if (process.env.uniTestPlatformInfo.indexOf('web') > -1 || process.env.UNI_AUTOMATOR_APP_WEBVIEW == 'true') {
雪洛's avatar
雪洛 已提交
7 8 9 10 11
    it('dummyTest', () => {
      expect(1).toBe(1)
    })
    return
  }
12
  let page;
W
WOSHIMAHAIFENG 已提交
13 14 15 16
  let mBasePath;
  let mGlobalTempPath;
  let mGlobalRootPath

W
WOSHIMAHAIFENG 已提交
17

18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
  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')
    })
W
WOSHIMAHAIFENG 已提交
35 36 37
    await page.setData({
      done: false
    })
38 39 40 41 42 43
    return isDone
  }

  it('USER_DATA_PATH test', async () => {
    // 测试 USER_DATA_PATH
    let globalUserDataPath = await getData('globalUserDataPath')
W
WOSHIMAHAIFENG 已提交
44 45 46
    mBasePath = await getData('basePath')
    mGlobalRootPath = await getData('globalRootPath')
    mGlobalTempPath = await getData('globalTempPath')
47 48

    await page.setData({
W
WOSHIMAHAIFENG 已提交
49
      logAble: false,
50
      recursiveVal: true,
W
WOSHIMAHAIFENG 已提交
51
      copyToBasePath: globalUserDataPath,
52
      basePath: globalUserDataPath,
W
WOSHIMAHAIFENG 已提交
53 54 55 56 57 58
      rmDirFile: 'a',
      readDir: 'a',
      writeFile: 'a/1.txt',
      readFile: 'a/1.txt',
      unlinkFile: 'a/1.txt',
      writeFileContent: '锄禾日当午,汗滴禾下土,谁知盘中餐,粒粒皆辛苦'
59 60 61
    })

    // 先清除文件,需要清除全部可能存在的历史测试文件,避免运行失败
62
    const btnUnLinkFileButton = await page.$('#btn-clear-file')
63 64 65 66 67
    await btnUnLinkFileButton.tap()
    await isDone()


    // 清除文件夹
68
    const btnRmDirButton = await page.$('#btn-remove-dir')
69 70 71
    await btnRmDirButton.tap()
    await isDone()
    // 重新创建测试目录
72
    const btnMkdDirButton = await page.$('#btn-mkdir')
73 74 75
    await btnMkdDirButton.tap()
    await isDone()

76
    const btnReadDirButton = await page.$('#btn-read-dir')
77 78 79 80 81 82 83 84 85 86 87 88 89
    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,
W
WOSHIMAHAIFENG 已提交
90
      mkdirFile: 'a/b/c'
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 116 117 118 119 120 121 122
    })


    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\"]")

    // 测试写入文件
123
    const btnWriteFileButton = await page.$('#btn-write-file')
124 125 126 127 128 129
    await btnWriteFileButton.tap()
    await isDone()
    // 检查目录列表数量
    await btnReadDirButton.tap()
    await isDone()
    fileListComplete = await getData('fileListComplete')
130 131
    fileListComplete.sort()
    expect(JSON.stringify(fileListComplete)).toEqual("[\"1.txt\",\"b\"]")
132
    fileListSuccess = await getData('fileListSuccess')
133 134
    fileListSuccess.sort()
    expect(JSON.stringify(fileListSuccess)).toEqual("[\"1.txt\",\"b\"]")
135
    // 获取和对比 文件内容
136
    const btnReadFileButton = await page.$('#btn-read-file')
137 138 139 140 141 142 143 144
    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.",
W
WOSHIMAHAIFENG 已提交
145
      getFileInfoAlgorithm: "md5"
146 147 148 149 150 151 152
    })
    await btnWriteFileButton.tap()
    await isDone()

    await btnReadFileButton.tap()
    await isDone()
    readFileRet = await getData('readFileRet')
W
WOSHIMAHAIFENG 已提交
153 154 155
    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."
    )
156

157
    const btnGetFileInfoButton = await page.$('#btn-get-file-info')
158 159 160 161 162 163 164 165 166
    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({
W
WOSHIMAHAIFENG 已提交
167
      getFileInfoAlgorithm: "sha1"
168 169 170 171 172 173 174 175 176 177 178 179 180
    })

    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({

W
WOSHIMAHAIFENG 已提交
181 182
      copyFromFile: "a/1.txt",
      copyToFile: "a/2.txt"
183
    })
184
    const btnCopyFileButton = await page.$('#btn-copy-file')
185 186 187 188 189 190 191 192 193
    await btnCopyFileButton.tap()
    await isDone()


    await btnReadDirButton.tap()
    await isDone()

    // 1.txt 2.txt 两个文件都存在
    fileListComplete = await getData('fileListComplete')
194 195
    fileListComplete.sort()
    expect(JSON.stringify(fileListComplete)).toEqual("[\"1.txt\",\"2.txt\",\"b\"]")
196
    fileListSuccess = await getData('fileListSuccess')
197 198
    fileListSuccess.sort()
    expect(JSON.stringify(fileListSuccess)).toEqual("[\"1.txt\",\"2.txt\",\"b\"]")
199 200 201

    // 测试 rename
    await page.setData({
W
WOSHIMAHAIFENG 已提交
202 203
      renameFromFile: "a/2.txt",
      renameToFile: "a/3.txt"
204 205
    })

206
    const btnRenameFileButton = await page.$('#btn-rename-file')
207 208 209 210 211 212 213 214
    await btnRenameFileButton.tap()
    await isDone()

    await btnReadDirButton.tap()
    await isDone()

    // 1.txt 3.txt 两个文件都存在
    fileListComplete = await getData('fileListComplete')
215 216
    fileListComplete.sort()
    expect(JSON.stringify(fileListComplete)).toEqual("[\"1.txt\",\"3.txt\",\"b\"]")
217
    fileListSuccess = await getData('fileListSuccess')
218 219
    fileListSuccess.sort()
    expect(JSON.stringify(fileListSuccess)).toEqual("[\"1.txt\",\"3.txt\",\"b\"]")
220 221 222 223 224
  });

  it('TEMP_PATH test', async () => {
    // 测试 TEMP_PATH
    let globalTempPath = await getData('globalTempPath')
225 226 227 228

    let version = process.env.uniTestPlatformInfo
    version = parseInt(version.split(" ")[1])
    let testDirName = "我们经历了一场兵慌马乱的战争.1@2#3$4%5^6&7*8(9)0+-qwertyuiopasdfghjklzxcvbnm;,"
W
WOSHIMAHAIFENG 已提交
229
    if (version < 6) {
230 231 232 233
      // android 6 以下文件名不能包含特殊字符
      testDirName = "我们经历了一场兵慌马乱的战争"
    }

234
    await page.setData({
W
WOSHIMAHAIFENG 已提交
235
      logAble: false,
236 237
      recursiveVal: true,
      basePath: globalTempPath,
W
WOSHIMAHAIFENG 已提交
238 239
      copyToBasePath: globalTempPath,
      rmDirFile: 'a',
240
      mkdirFile: 'a',
W
WOSHIMAHAIFENG 已提交
241
      unlinkFile: 'a/' + testDirName + '/中文路径/张三/name/中文文件.mock'
242 243 244 245
    })


    // 先清除文件,需要清除全部可能存在的历史测试文件,避免运行失败
246
    const btnUnLinkFileButton = await page.$('#btn-unlink-file')
247 248 249 250 251
    await btnUnLinkFileButton.tap()
    await isDone()


    // 清除文件夹
252
    const btnRmDirButton = await page.$('#btn-remove-dir')
253 254 255 256
    await btnRmDirButton.tap()
    await isDone()

    // 重新创建测试目录
257
    const btnMkdDirButton = await page.$('#btn-mkdir')
258 259 260
    await btnMkdDirButton.tap()
    await isDone()

261
    const btnReadDirButton = await page.$('#btn-read-dir')
262 263 264 265 266 267
    await btnReadDirButton.tap()
    await isDone()


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

    // 测试 创建多层级文件目录
    await page.setData({
      recursiveVal: true,
W
WOSHIMAHAIFENG 已提交
275
      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',
276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291
    })

    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,
W
WOSHIMAHAIFENG 已提交
292
      mkdirFile: 'a/' + testDirName + '/中文路径/张三/name',
293 294 295 296 297 298 299 300 301
    })
    await btnMkdDirButton.tap()
    await isDone()

    await btnReadDirButton.tap()
    await isDone()

    // 期望通过 recursive = true的 文件夹删除,得到一个空的 /a 目录
    fileListComplete = await getData('fileListComplete')
302
    fileListComplete.sort()
303
    expect(JSON.stringify(fileListComplete)).toEqual("[\"b\",\"" + testDirName + "\"]")
304
    fileListSuccess = await getData('fileListSuccess')
305
    fileListSuccess.sort()
306
    expect(JSON.stringify(fileListSuccess)).toEqual("[\"b\",\"" + testDirName + "\"]")
307 308 309

    /**
     * 从资源文件中读取图片为base64,测试写入较大文件场景
310
     * 'static/test-image/logo.ico' 注意,依赖这个资源文件,不能删除
311 312
     */
    await page.setData({
313
      basePath: "",
W
WOSHIMAHAIFENG 已提交
314 315
      readFile: 'static/test-image/logo.ico',
      readFileEncoding: 'base64'
316 317 318 319
    })


    // 获取和对比 文件内容
320
    const btnReadFileButton = await page.$('#btn-read-file')
321 322 323
    await btnReadFileButton.tap()
    await isDone()
    let readFileRet = await getData('readFileRet')
324
    expect(readFileRet.length).toEqual(208544)
325
    let endStr = readFileRet.substring(readFileRet.length - 10)
326
    expect(endStr).toEqual("///////w==")
327 328 329

    await page.setData({
      basePath: globalTempPath,
W
WOSHIMAHAIFENG 已提交
330 331
      writeFile: 'a/' + testDirName + '/中文路径/张三/name/中文文件.mock',
      writeFileContent: readFileRet
332 333 334
    })


335
    const btnWriteFileButton = await page.$('#btn-write-file')
336 337 338 339 340
    await btnWriteFileButton.tap()
    await isDone()

    // 获取文件列表,判断是否写入成功,同时置空base64内容 避免影响实时查看状态
    await page.setData({
W
WOSHIMAHAIFENG 已提交
341 342 343
      readDir: 'a/' + testDirName + '/中文路径/张三/name',
      readFileRet: '',
      writeFileContent: ''
344 345 346 347 348 349 350 351 352 353 354 355 356
    })

    // 检查目录列表数量
    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({
W
WOSHIMAHAIFENG 已提交
357 358
      getFileInfoFile: 'a/' + testDirName + '/中文路径/张三/name/中文文件.mock',
      getFileInfoAlgorithm: "md5",
359 360
    })

361
    const btnGetFileInfoButton = await page.$('#btn-get-file-info')
362 363 364 365
    await btnGetFileInfoButton.tap()
    await isDone()

    let getFileInfoSize = await getData('getFileInfoSize')
366
    expect(getFileInfoSize).toEqual(208544)
367
    let getFileInfoDigest = await getData('getFileInfoDigest')
368
    expect(getFileInfoDigest).toEqual("486f75ea76625f8c103cac4bc9c49511")
369 370 371

    // 切换为 sha1
    await page.setData({
W
WOSHIMAHAIFENG 已提交
372
      getFileInfoAlgorithm: "sha1"
373 374 375 376 377 378
    })

    await btnGetFileInfoButton.tap()
    await isDone()

    getFileInfoSize = await getData('getFileInfoSize')
379
    expect(getFileInfoSize).toEqual(208544)
380
    getFileInfoDigest = await getData('getFileInfoDigest')
381
    expect(getFileInfoDigest).toEqual("1830169a16e7c860beff4a3b0975ba0b6f775f9e")
382 383 384

    // 测试不支持的摘要算法,期望返回错误
    await page.setData({
W
WOSHIMAHAIFENG 已提交
385
      getFileInfoAlgorithm: "sha256"
386 387 388 389 390 391 392 393 394 395 396 397 398
    })

    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({
W
WOSHIMAHAIFENG 已提交
399 400
      renameFromFile: "a/" + testDirName + "/中文路径/张三/name/中文文件.mock",
      renameToFile: "a/没有提前创建的目录/3.txt"
401 402
    })

403
    const btnRenameFileButton = await page.$('#btn-rename-file')
404 405 406 407 408 409 410 411 412 413 414
    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,
W
WOSHIMAHAIFENG 已提交
415
      mkdirFile: 'a/提前创建的目录',
416 417 418 419 420 421
    })

    await btnMkdDirButton.tap()
    await isDone()

    await page.setData({
W
WOSHIMAHAIFENG 已提交
422
      readDir: 'a',
423 424 425 426 427 428
    })

    await btnReadDirButton.tap()
    await isDone()

    fileListComplete = await getData('fileListComplete')
429
    fileListComplete.sort()
430
    expect(JSON.stringify(fileListComplete)).toEqual("[\"b\",\"" + testDirName + "\",\"提前创建的目录\"]")
431
    fileListSuccess = await getData('fileListSuccess')
432
    fileListSuccess.sort()
433
    expect(JSON.stringify(fileListSuccess)).toEqual("[\"b\",\"" + testDirName + "\",\"提前创建的目录\"]")
434 435 436


    await page.setData({
W
WOSHIMAHAIFENG 已提交
437 438
      copyFromFile: "a/" + testDirName + "/中文路径/张三/name/中文文件.mock",
      copyToFile: "a/提前创建的目录/4.txt"
439 440
    })

W
WOSHIMAHAIFENG 已提交
441 442 443
    const btnCopyFileButton = await page.$('#btn-copy-file')
    await btnCopyFileButton.tap()
    await isDone()
444 445

    await page.setData({
W
WOSHIMAHAIFENG 已提交
446
      readDir: 'a/提前创建的目录',
447 448 449 450 451 452 453 454 455 456 457
    })

    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({
W
WOSHIMAHAIFENG 已提交
458 459
      unlinkFile: 'a/提前创建的目录/4.txt',
      rmDirFile: 'a/提前创建的目录'
460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482
    })
    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,
W
WOSHIMAHAIFENG 已提交
483
      logAble: false,
484
      basePath: globalRootPath,
W
WOSHIMAHAIFENG 已提交
485 486 487 488 489
      readDir: 'a',
      rmDirFile: 'a',
      mkdirFile: 'a',
      accessFile: 'a/从代码目录拷贝的资源.png',
      unlinkFile: 'a/从代码目录拷贝的资源.png'
490 491 492 493
    })


    // 先清除文件,需要清除全部可能存在的历史测试文件,避免运行失败
494
    const btnClearFileButton = await page.$('#btn-clear-file')
495
    await btnClearFileButton.tap()
496 497 498 499
    await isDone()


    // 清除文件夹
500
    const btnRmDirButton = await page.$('#btn-remove-dir')
501 502 503 504
    await btnRmDirButton.tap()
    await isDone()

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

509
    const btnReadDirButton = await page.$('#btn-read-dir')
510 511 512 513 514 515 516 517 518 519
    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('[]')


    // 检查资源文件,此时不存在
520
    const btnAccessFileButton = await page.$('#btn-access-file')
521 522 523 524 525 526 527 528 529
    await btnAccessFileButton.tap()
    await isDone()

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


    // 准备从资源目录拷贝png
    await page.setData({
530
      basePath: "",
W
WOSHIMAHAIFENG 已提交
531 532
      unlinkFile: 'static/test-image/logo.ico',
      accessFile: 'static/test-image/logo.ico',
533 534 535 536 537 538 539 540 541
    })
    // 检查资源文件,期望存在
    await btnAccessFileButton.tap()
    await isDone()

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

    // 尝试删除资源,期望失败
542
    const btnUnLinkFileButton = await page.$('#btn-unlink-file')
543 544 545 546 547 548 549 550 551 552
    await btnUnLinkFileButton.tap()
    await isDone()

    await btnAccessFileButton.tap()
    await isDone()

    accessFileRet = await getData("accessFileRet")
    expect(accessFileRet).toEqual('access:ok')
    // 复制资源到 root目录
    await page.setData({
W
WOSHIMAHAIFENG 已提交
553 554 555
      copyToBasePath: globalRootPath,
      copyFromFile: "static/test-image/logo.ico",
      copyToFile: "a/从代码目录拷贝的资源.png"
556
    })
557
    const btnCopyFileButton = await page.$('#btn-copy-file')
558 559 560 561 562
    await btnCopyFileButton.tap()
    await isDone()

    // 检查期望 root 目录中图片文件存在
    await page.setData({
W
WOSHIMAHAIFENG 已提交
563 564 565 566
      basePath: globalRootPath,
      unlinkFile: 'a/从代码目录拷贝的资源.png',
      accessFile: 'a/从代码目录拷贝的资源.png',
      rmDirFile: 'a',
567 568 569 570 571 572 573 574 575 576
    })
    await btnAccessFileButton.tap()
    await isDone()

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

    await btnUnLinkFileButton.tap()
    await isDone()

577 578
    await btnAccessFileButton.tap()
    await isDone()
579 580 581 582

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

583
    // 从页面的按钮触发一次文件复制
584
    const btnCopyStaticFileButton = await page.$('#btn-copyStatic-file')
585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607
    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("[]")

608 609 610
  });


611 612 613 614 615 616 617
  it('write and read', async () => {
    /**
     * 测试writefile readfile 各个参数是否符合预期
     */
    let globalTempPath = await getData('globalTempPath')
    await page.setData({
      recursiveVal: true,
W
WOSHIMAHAIFENG 已提交
618
      logAble: false,
619
      basePath: globalTempPath,
W
WOSHIMAHAIFENG 已提交
620 621 622 623 624 625 626 627 628 629 630
      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"
631 632 633
    })

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

    // 清除文件夹
639
    const btnRmDirButton = await page.$('#btn-remove-dir')
640 641 642 643
    await btnRmDirButton.tap()
    await isDone()

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

648
    const btnReadDirButton = await page.$('#btn-read-dir')
649 650 651 652 653 654 655 656 657
    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 写入内容
658
    const btnWriteFileButton = await page.$('#btn-write-file')
659 660 661
    await btnWriteFileButton.tap()
    await isDone()

662
    const btnReadFileButton = await page.$('#btn-read-file')
663 664 665 666 667
    await btnReadFileButton.tap()
    await isDone()
    let readFileRet = await getData('readFileRet')
    expect(readFileRet).toEqual("我爱北京天安门,天安门前太阳升")

668
    const btnGetFileInfoButton = await page.$('#btn-get-file-info')
669 670 671 672 673 674 675 676 677 678
    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({
W
WOSHIMAHAIFENG 已提交
679
      readFileEncoding: "base64",
680 681 682 683 684 685 686 687 688
    })

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

    await page.setData({
W
WOSHIMAHAIFENG 已提交
689 690 691
      writeFileContent: "丙辰中秋,欢饮达旦,大醉,作此篇,兼怀子由。明月几时有?把酒问青天。不知天上宫阙,今夕是何年。我欲乘风归去,又恐琼楼玉宇,高处不胜寒。起舞弄清影,何似在人间",
      writeFileEncoding: "ascii",
      readFileEncoding: "base64",
692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707
    })

    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')
W
WOSHIMAHAIFENG 已提交
708 709 710
    expect(readFileRet).toEqual(
      "GbAtywwibr7mDCeJDFxkxwx8AFAxAg4I4PYJH4pS7lIpAg3lKQqrGQzKFS9VdAIRMljOUrsMyFA8fImHDNgEDdzSAnceBAVxDFU8KLr0"
    )
711 712 713

    // 尝试写入合法ascii
    await page.setData({
W
WOSHIMAHAIFENG 已提交
714 715 716
      writeFileContent: "hello jack.hello marry.",
      writeFileEncoding: "ascii",
      readFileEncoding: "ascii",
717 718 719 720 721 722 723 724 725 726 727 728
    })

    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({
W
WOSHIMAHAIFENG 已提交
729 730 731
      writeFileContent: "5LiZ6L6w5Lit56eL77yM5qyi6aWu6L6+5pem77yM5aSn6YaJ77yM5L2c5q2k56+H77yM5YW85oCA5a2Q55Sx44CC5piO5pyI5Yeg5pe25pyJ77yf5oqK6YWS6Zeu6Z2S5aSp44CC5LiN55+l5aSp5LiK5a6r6ZiZ77yM5LuK5aSV5piv5L2V5bm044CC5oiR5qyy5LmY6aOO5b2S5Y6777yM5Y+I5oGQ55C85qW8546J5a6H77yM6auY5aSE5LiN6IOc5a+S44CC6LW36Iie5byE5riF5b2x77yM5L2V5Ly85Zyo5Lq66Ze0",
      writeFileEncoding: "base64",
      readFileEncoding: "utf-8",
732 733 734 735 736 737 738 739
    })

    await btnWriteFileButton.tap()
    await isDone()

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

    await page.setData({
W
WOSHIMAHAIFENG 已提交
744
      readFileEncoding: "base64",
745 746 747 748 749
    })

    await btnReadFileButton.tap()
    await isDone()
    readFileRet = await getData('readFileRet')
W
WOSHIMAHAIFENG 已提交
750 751 752
    expect(readFileRet).toEqual(
      "5LiZ6L6w5Lit56eL77yM5qyi6aWu6L6+5pem77yM5aSn6YaJ77yM5L2c5q2k56+H77yM5YW85oCA5a2Q55Sx44CC5piO5pyI5Yeg5pe25pyJ77yf5oqK6YWS6Zeu6Z2S5aSp44CC5LiN55+l5aSp5LiK5a6r6ZiZ77yM5LuK5aSV5piv5L2V5bm044CC5oiR5qyy5LmY6aOO5b2S5Y6777yM5Y+I5oGQ55C85qW8546J5a6H77yM6auY5aSE5LiN6IOc5a+S44CC6LW36Iie5byE5riF5b2x77yM5L2V5Ly85Zyo5Lq66Ze0"
    )
753 754

  });
755

756 757 758 759 760 761
  it('stat and asset test', async () => {
    // 测试 USER_DATA_PATH //globalTempPath
    let globalRootPath = await getData('globalRootPath')

    await page.setData({
      recursiveVal: true,
W
WOSHIMAHAIFENG 已提交
762
      copyToBasePath: globalRootPath,
763
      basePath: globalRootPath,
W
WOSHIMAHAIFENG 已提交
764 765 766 767
      globalTempPath: globalRootPath,
      rmDirFile: 'a',
      mkdirFile: 'a',
      unlinkFile: 'a/1.txt',
768 769 770
    })

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

    await page.setData({
W
WOSHIMAHAIFENG 已提交
776
      unlinkFile: 'a/2.txt',
777 778 779 780 781
    })
    await btnUnLinkFileButton.tap()
    await isDone()

    await page.setData({
W
WOSHIMAHAIFENG 已提交
782
      unlinkFile: 'a/m/3.txt',
783 784 785 786 787
    })
    await btnUnLinkFileButton.tap()
    await isDone()

    // // 清除文件夹
788
    const btnRmDirButton = await page.$('#btn-remove-dir')
789 790 791
    await btnRmDirButton.tap()
    await isDone()
    // // 重新创建测试目录
792
    const btnMkdDirButton = await page.$('#btn-mkdir')
793 794 795
    await btnMkdDirButton.tap()
    await isDone()

796
    const btnReadDirButton = await page.$('#btn-read-dir')
797 798 799 800 801 802 803 804 805 806 807 808 809
    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: "锄禾日当午,汗滴禾下土,谁知盘中餐,粒粒皆辛苦",
W
WOSHIMAHAIFENG 已提交
810 811 812 813
      writeFileEncoding: "utf-8",
      writeFile: 'a/1.txt',
      recursiveVal: false,
      statFile: 'a/1.txt',
814 815 816 817 818
    })

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

819
    const btnWriteFileButton = await page.$('#btn-write-file')
820 821 822
    await btnWriteFileButton.tap()
    await isDone()

W
WOSHIMAHAIFENG 已提交
823
    let btnStatFileButton = await page.$('#btn-stat-file')
824 825 826 827 828 829
    await btnStatFileButton.tap()
    await isDone()

    // 读取单个文件信息
    let statsRet = await getData('statsRet')
    expect(statsRet.length).toEqual(1)
830
    expect(statsRet[0].path).toMatch(new RegExp('.*/a/1.txt$'))
831
    expect(statsRet[0].stats.size).toEqual(69)
W
WOSHIMAHAIFENG 已提交
832 833 834 835 836 837 838 839 840 841 842 843 844 845 846
    if (isAndroid()) {
      // 写入一个文件
      await page.setData({
        statsRet: ''
      })
      btnStatFileButton = await page.$('#btn-stat-file-sync')
      await btnStatFileButton.tap()
      await isDone()

      // 读取单个文件信息
      statsRet = await getData('statsRet')
      expect(statsRet.length).toEqual(1)
      expect(statsRet[0].path).toMatch(new RegExp('.*/a/1.txt$'))
      expect(statsRet[0].stats.size).toEqual(69)
    }
W
WOSHIMAHAIFENG 已提交
847

848 849 850 851 852
    /**
     * 创建子目录和子目录文件,测试recursive参数
     */
    await page.setData({
      writeFileContent: "1234567890",
W
WOSHIMAHAIFENG 已提交
853 854
      writeFileEncoding: "ascii",
      writeFile: 'a/2.txt',
855
      basePath: globalRootPath,
W
WOSHIMAHAIFENG 已提交
856 857 858
      recursiveVal: false,
      statFile: 'a',
      mkdirFile: 'a/m',
859 860 861 862 863 864 865 866 867 868 869 870 871
    })


    await btnWriteFileButton.tap()
    await isDone()

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

    // 复制一份文件到 /a/m/3.txt
    await page.setData({
      //  asset 只能正式版测试,这里只能模拟返回路径
W
WOSHIMAHAIFENG 已提交
872 873 874
      basePath: '',
      copyFromFile: 'static/test-image/logo.ico',
      copyToFile: 'a/m/3.txt',
875
    })
876
    const btnCopyFileButton = await page.$('#btn-copy-file')
877 878 879 880 881 882
    await btnCopyFileButton.tap()
    await isDone()


    await page.setData({
      basePath: globalRootPath,
W
WOSHIMAHAIFENG 已提交
883 884
      recursiveVal: true,
      statFile: 'a',
885 886 887 888 889 890 891
    })

    await btnStatFileButton.tap()
    await isDone()

    // 读取全部文件信息
    statsRet = await getData('statsRet')
892

W
WOSHIMAHAIFENG 已提交
893 894
    statsRet.sort(function(a, b) {
      if (a.path > b.path) {
895
        return 1
W
WOSHIMAHAIFENG 已提交
896
      } else if (a.path < b.path) {
897 898 899 900
        return -1
      }
      return 0
    })
901 902
    console.log(statsRet)
    expect(statsRet.length).toEqual(5)
903 904
    expect(statsRet[0].path).toMatch(new RegExp('.*/a$'))
    // expect(statsRet[0].stats.size).toEqual(0)
905

906
    expect(statsRet[2].path).toMatch(new RegExp('.*/a/2.txt$'))
907 908
    expect(statsRet[2].stats.size).toEqual(10)

909 910
    expect(statsRet[4].path).toMatch(new RegExp('.*/a/m/3.txt$'))
    expect(statsRet[4].stats.size).toEqual(156406)
911

912

913 914
    // 清理文件,避免影响其他测试用例
    await page.setData({
W
WOSHIMAHAIFENG 已提交
915
      unlinkFile: 'a/1.txt',
916 917 918 919 920
    })
    await btnUnLinkFileButton.tap()
    await isDone()

    await page.setData({
W
WOSHIMAHAIFENG 已提交
921
      unlinkFile: 'a/2.txt',
922 923 924 925 926
    })
    await btnUnLinkFileButton.tap()
    await isDone()

    await page.setData({
W
WOSHIMAHAIFENG 已提交
927 928 929 930
      unlinkFile: 'a/m/3.txt',
      rmDirFile: 'a',
      readDir: 'a',
      recursiveVal: true,
931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947
    })
    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('[]')

  });
W
WOSHIMAHAIFENG 已提交
948
  it('appendFileTest', async () => {
W
WOSHIMAHAIFENG 已提交
949
    if (!isAndroid()) {
W
WOSHIMAHAIFENG 已提交
950 951 952 953
      return
    }


W
WOSHIMAHAIFENG 已提交
954
    let basePath = await getData('basePath')
W
WOSHIMAHAIFENG 已提交
955

W
WOSHIMAHAIFENG 已提交
956
    await page.setData({
W
WOSHIMAHAIFENG 已提交
957
      basePath: mBasePath,
W
WOSHIMAHAIFENG 已提交
958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985
      recursiveVal: true,
      logAble: false,
      rmDirFile: 'appendfile',
      mkdirFile: 'appendfile',
      writeFileContent: "我爱北京天安门,天安门前太阳升",
      appendFileContent: "再说一遍",
      writeFileEncoding: "utf-8",
      readFileEncoding: "utf-8",
      readFile: 'appendfile/appendfile.txt',
      unlinkFile: 'appendfile/appendfile.txt',
      writeFile: 'appendfile/appendfile.txt',
    })

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

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

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

W
WOSHIMAHAIFENG 已提交
986
    // 先用utf-8 写入内容
W
WOSHIMAHAIFENG 已提交
987 988 989 990 991 992
    const btnWriteFileButton = await page.$('#btn-write-file')

    await btnWriteFileButton.tap()
    await isDone()


W
WOSHIMAHAIFENG 已提交
993
    //追加内容
W
WOSHIMAHAIFENG 已提交
994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007
    const btnAppendFileButton = await page.$('#btn-append-file')
    await btnAppendFileButton.tap()
    await isDone()

    const btnReadFileButton = await page.$('#btn-read-file')
    await btnReadFileButton.tap()
    await isDone()
    let readFileRet = await getData('readFileRet')
    expect(readFileRet).toEqual("我爱北京天安门,天安门前太阳升再说一遍")
  });
  //nlinkSyncTest mkdirSyncTest appendFileTest writeFileSyncTest readFileSyncTest rmdirSyncTest readDirSyncTest accessFileSyncTest
  //renameFileSync copyFileSyncTest appendFileSyncTest truncateFileTest truncateFileSyncTest
  it('sync test',
    async () => {
W
WOSHIMAHAIFENG 已提交
1008
      if (!isAndroid()) {
W
WOSHIMAHAIFENG 已提交
1009 1010 1011 1012
        return
      }


W
WOSHIMAHAIFENG 已提交
1013
      await page.setData({
W
WOSHIMAHAIFENG 已提交
1014
        basePath: mBasePath,
W
WOSHIMAHAIFENG 已提交
1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031
        recursiveVal: false,
        logAble: false,
        rmDirFile: 'sync',
        mkdirFile: 'sync',
        writeFileContent: "我爱北京天安门,天安门前太阳升",
        appendFileContent: "再说一遍",
        writeFileEncoding: "utf-8",
        readFileEncoding: "utf-8",
        readDir: 'sync',
        rmDirFile: 'sync',
        unlinkFile: 'sync/sync.txt',
        readFile: 'sync/sync.txt',
        writeFile: 'sync/sync.txt',
        accessFile: 'sync/sync.txt',
        renameToFile: 'sync/sync.txt',
        renameFromFile: 'sync/sync.txt',
      })
W
WOSHIMAHAIFENG 已提交
1032
      await clearDir('sync')
W
WOSHIMAHAIFENG 已提交
1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099
      // 重新创建测试目录,期望通过 recursive = true的 文件夹删除,得到一个空的  目录
      const btnMkdDirButton = await page.$('#btn-mkdir-sync')
      await btnMkdDirButton.tap()
      await isDone()

      const btnReadDirButton = await page.$('#btn-read-dir-sync')
      await btnReadDirButton.tap()
      await isDone()
      const fileListSuccess = await getData('fileListSuccess')
      expect(JSON.stringify(fileListSuccess)).toEqual('[]')

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

      let btnAccessFileButton = await page.$('#btn-access-file-sync')
      await btnAccessFileButton.tap()
      await isDone()
      let accessFileRet = await getData("accessFileRet")
      expect(accessFileRet).toEqual('access:ok')

      //重新命名文件
      const btnRenameFileButton = await page.$('#btn-rename-file-sync')
      await btnRenameFileButton.tap()
      await isDone()
      let renameFileRet = await getData("renameFileRet")
      expect(renameFileRet).toEqual("rename:ok")


      //追加内容
      let btnAppendFileButton = await page.$('#btn-append-file')
      await btnAppendFileButton.tap()
      await isDone()

      btnAppendFileButton = await page.$('#btn-append-file-sync')
      await btnAppendFileButton.tap()


      let btnReadFileButton = await page.$('#btn-read-file-sync')
      await btnReadFileButton.tap()
      await isDone()
      let readFileRet = await getData('readFileRet')
      expect(readFileRet).toEqual("我爱北京天安门,天安门前太阳升再说一遍再说一遍")

      //truncateFileTest
      let btnTruncateFile = await page.$('#btn-truncate-file')
      await btnTruncateFile.tap()
      await isDone()
      btnReadFileButton = await page.$('#btn-read-file-sync')
      await btnReadFileButton.tap()
      await isDone()
      readFileRet = await getData('readFileRet')
      expect(readFileRet).toEqual("我爱北京天安门")

      btnTruncateFile = await page.$('#btn-truncate-file-sync')
      await btnTruncateFile.tap()
      await isDone()
      btnReadFileButton = await page.$('#btn-read-file-sync')
      await btnReadFileButton.tap()
      await isDone()
      readFileRet = await getData('readFileRet')
      expect(readFileRet).toEqual("我爱北京")


      // 测试 copyfile
      await page.setData({
W
WOSHIMAHAIFENG 已提交
1100 1101
        basePath: mBasePath,
        copyToBasePath: mBasePath,
W
WOSHIMAHAIFENG 已提交
1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114
        copyFromFile: "sync/sync.txt",
        copyToFile: "sync/syncto.txt",
        accessFile: "sync/syncto.txt"
      })
      const btnCopyFileButton = await page.$('#btn-copy-file-sync')
      await btnCopyFileButton.tap()
      await isDone()
      btnAccessFileButton = await page.$('#btn-access-file-sync')
      await btnAccessFileButton.tap()
      await isDone()
      accessFileRet = await getData("accessFileRet")
      expect(accessFileRet).toEqual('access:ok')

W
WOSHIMAHAIFENG 已提交
1115
      await clearDir('sync')
W
WOSHIMAHAIFENG 已提交
1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134
    });

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

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

  async function clearDir(dir) {
    // 最后需要清楚所有文件避免测试失败
    await page.setData({
      rmDirFile: dir,
    })
W
WOSHIMAHAIFENG 已提交
1135 1136 1137 1138 1139
    const btnUnLinkFileButton = await page.$('#btn-clear-file')
    await btnUnLinkFileButton.tap()
    await isDone()

    const btnClear = await page.$('#btn-remove-dir')
W
WOSHIMAHAIFENG 已提交
1140 1141 1142
    await btnClear.tap()
    await isDone()
  }
W
WOSHIMAHAIFENG 已提交
1143 1144 1145

  function isAndroid() {
    if (process.env.uniTestPlatformInfo.indexOf('web') > -1 || process.env.UNI_AUTOMATOR_APP_WEBVIEW === 'true') {
1146
      expect(1).toBe(1)
W
WOSHIMAHAIFENG 已提交
1147 1148 1149
      return false
    }
    if (process.env.uniTestPlatformInfo.toLocaleLowerCase().startsWith('ios')) {
1150
      expect(1).toBe(1)
W
WOSHIMAHAIFENG 已提交
1151 1152 1153 1154
      return false
    }
    return true
  }
W
WOSHIMAHAIFENG 已提交
1155

W
WOSHIMAHAIFENG 已提交
1156 1157
  it('getSavedFileListTest',
    async () => {
W
WOSHIMAHAIFENG 已提交
1158
      if (!isAndroid()) {
W
WOSHIMAHAIFENG 已提交
1159 1160
        return
      }
W
WOSHIMAHAIFENG 已提交
1161 1162 1163 1164
      // await page.setData({
      //   logAble: false,
      //   basePath: mBasePath
      // })
W
WOSHIMAHAIFENG 已提交
1165 1166 1167
      // console.log('getSavedFileListTest', 'start')
      // await clearDir('')
      // console.log('getSavedFileListTest', 'end')
W
WOSHIMAHAIFENG 已提交
1168
      await page.setData({
W
WOSHIMAHAIFENG 已提交
1169
        logAble: false,
W
WOSHIMAHAIFENG 已提交
1170 1171 1172 1173
        basePath: mGlobalTempPath,
        temFile: 'save3/2.txt',
        mkdirFile: 'save3',
        writeFile: 'save3/2.txt',
W
WOSHIMAHAIFENG 已提交
1174 1175 1176 1177
        accessFile: '2.txt'
      })
      await createFile()
      await page.setData({
W
WOSHIMAHAIFENG 已提交
1178
        basePath: mBasePath,
W
WOSHIMAHAIFENG 已提交
1179 1180 1181 1182 1183 1184 1185 1186
        writeFile: 'save/2.txt',
      })
      btnSaveFile = await page.$('#btn-save-file-sync')
      await btnSaveFile.tap()

      let btnSavedFileList = await page.$('#btn-getsaved-filelist')
      await btnSavedFileList.tap()
      await isDone()
1187 1188
      let fileListSuccess = await getData("getSavedFileListRet")
      expect(fileListSuccess).toEqual('getSavedFileList:ok')
W
WOSHIMAHAIFENG 已提交
1189 1190 1191 1192
    });

  it('removeSavedFileTest',
    async () => {
W
WOSHIMAHAIFENG 已提交
1193
      if (!isAndroid()) {
W
WOSHIMAHAIFENG 已提交
1194 1195
        return
      }
W
WOSHIMAHAIFENG 已提交
1196
      await page.setData({
W
WOSHIMAHAIFENG 已提交
1197
        logAble: false,
W
WOSHIMAHAIFENG 已提交
1198
        basePath: mBasePath
W
WOSHIMAHAIFENG 已提交
1199
      })
W
WOSHIMAHAIFENG 已提交
1200
      await clearDir('save4')
W
WOSHIMAHAIFENG 已提交
1201
      await page.setData({
W
WOSHIMAHAIFENG 已提交
1202
        logAble: false,
W
WOSHIMAHAIFENG 已提交
1203 1204 1205 1206 1207
        basePath: mGlobalTempPath,
        temFile: 'save4/saveSync.txt',
        mkdirFile: 'save4',
        writeFile: 'save4/saveSync.txt',
        accessFile: 'saveSync.txt'
W
WOSHIMAHAIFENG 已提交
1208 1209 1210 1211
      })
      await createFile()
      let btnRemoveSavedFileRet = await page.$('#btn-remove-saved-file')
      await btnRemoveSavedFileRet.tap()
W
WOSHIMAHAIFENG 已提交
1212
      await isDone()
W
WOSHIMAHAIFENG 已提交
1213 1214
      let removeSavedFileRet = await getData("removeSavedFileRet")
      expect(removeSavedFileRet).toEqual('removeSavedFile:ok')
W
WOSHIMAHAIFENG 已提交
1215

W
WOSHIMAHAIFENG 已提交
1216 1217 1218
    });

  //openFiletest openFileSynctest closeTest closeTestSync writeTest writeSyncTest
W
WOSHIMAHAIFENG 已提交
1219
  it('openFiletest', async () => {
W
WOSHIMAHAIFENG 已提交
1220
    if (!isAndroid()) {
W
WOSHIMAHAIFENG 已提交
1221 1222 1223
      return
    }

W
WOSHIMAHAIFENG 已提交
1224
    await page.setData({
W
WOSHIMAHAIFENG 已提交
1225
      basePath: mBasePath,
W
WOSHIMAHAIFENG 已提交
1226
      logAble: false,
W
WOSHIMAHAIFENG 已提交
1227 1228 1229 1230
      mkdirFile: 'fd',
      writeFile: 'fd/1.txt',
      readFile: 'fd/1.txt'
    })
W
WOSHIMAHAIFENG 已提交
1231
    await clearDir('fd')
W
WOSHIMAHAIFENG 已提交
1232
    await createFile()
W
WOSHIMAHAIFENG 已提交
1233
    console.log('openFiletest', '2')
W
WOSHIMAHAIFENG 已提交
1234 1235 1236 1237 1238 1239 1240 1241 1242
    //openFiletest
    let btnOpenFile = await page.$('#btn-open-file')
    await btnOpenFile.tap()
    await isDone()
    let fd = await getData("fd")
    expect(fd).not.toBe('');
    await page.setData({
      fd: '',
    })
W
WOSHIMAHAIFENG 已提交
1243
    console.log('openFiletest', '3')
W
WOSHIMAHAIFENG 已提交
1244 1245 1246 1247 1248 1249
    //openFileSynctest
    btnOpenFile = await page.$('#btn-open-file-sync')
    await btnOpenFile.tap()
    await isDone()
    fd = await getData("fd")
    expect(fd).not.toBe('');
W
WOSHIMAHAIFENG 已提交
1250
    console.log('openFiletest', '4')
W
WOSHIMAHAIFENG 已提交
1251 1252 1253
  });
  // closeTest closeTestSync
  it('closeTest', async () => {
W
WOSHIMAHAIFENG 已提交
1254
    if (!isAndroid()) {
W
WOSHIMAHAIFENG 已提交
1255 1256 1257 1258
      return
    }


W
WOSHIMAHAIFENG 已提交
1259

W
WOSHIMAHAIFENG 已提交
1260
    await page.setData({
W
WOSHIMAHAIFENG 已提交
1261
      basePath: mBasePath,
W
WOSHIMAHAIFENG 已提交
1262
      logAble: false,
W
WOSHIMAHAIFENG 已提交
1263 1264 1265 1266
      mkdirFile: 'fd',
      writeFile: 'fd/1.txt',
      readFile: 'fd/1.txt'
    })
W
WOSHIMAHAIFENG 已提交
1267
    await clearDir('fd')
W
WOSHIMAHAIFENG 已提交
1268
    await createFile()
W
WOSHIMAHAIFENG 已提交
1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284
    //closeTest
    let btnCloseFile = await page.$('#btn-close-file')
    await btnCloseFile.tap()
    await isDone()
    let closeFileRet = await getData("closeFileRet")
    expect(closeFileRet).toEqual('close:ok')
    await page.setData({
      closeFileRet: '',
    })
    //closeTestSync
    btnCloseFile = await page.$('#btn-close-file-sync')
    await btnCloseFile.tap()
    await isDone()
    closeFileRet = await getData("closeFileRet")
    expect(closeFileRet).toEqual('close:ok')

W
WOSHIMAHAIFENG 已提交
1285 1286 1287
  });
  //writeTest writeSyncTest
  it('writeTest', async () => {
W
WOSHIMAHAIFENG 已提交
1288
    if (!isAndroid()) {
W
WOSHIMAHAIFENG 已提交
1289 1290
      return
    }
1291
    console.log('writeTest', 'start')
W
WOSHIMAHAIFENG 已提交
1292

W
WOSHIMAHAIFENG 已提交
1293
    await page.setData({
W
WOSHIMAHAIFENG 已提交
1294
      basePath: mBasePath,
W
WOSHIMAHAIFENG 已提交
1295
      logAble: false,
W
WOSHIMAHAIFENG 已提交
1296
      mkdirFile: 'fd',
W
WOSHIMAHAIFENG 已提交
1297 1298 1299 1300
      writeFile: 'fd/1.txt',
      readFile: 'fd/1.txt',
      writeData: '我是一只小小鸟'
    })
W
WOSHIMAHAIFENG 已提交
1301
    await clearDir('fd')
W
WOSHIMAHAIFENG 已提交
1302
    await createFile()
1303
    console.log('writeTest', '1')
W
WOSHIMAHAIFENG 已提交
1304 1305 1306 1307 1308
    let btnWrite = await page.$('#btn-write')
    await btnWrite.tap()
    await isDone()
    let bytesWritten = await getData("bytesWritten")
    expect(bytesWritten).toEqual(7)
1309
    console.log('writeTest', '2')
W
WOSHIMAHAIFENG 已提交
1310 1311 1312 1313 1314 1315 1316
    //writeSyncTest
    await page.setData({
      writeFile: 'fd/1.txt',
      readFile: 'fd/1.txt',
      writeData: '我是'
    })

1317
    btnWrite = await page.$('#btn-write-sync')
W
WOSHIMAHAIFENG 已提交
1318 1319
    await btnWrite.tap()
    await isDone()
W
WOSHIMAHAIFENG 已提交
1320
    bytesWritten = await getData("bytesWritten")
W
WOSHIMAHAIFENG 已提交
1321
    expect(bytesWritten).toEqual(2)
1322
    console.log('writeTest', '3')
W
WOSHIMAHAIFENG 已提交
1323 1324 1325 1326 1327 1328
    //fstatTest
    let btnFstat = await page.$('#btn-fstat-file')
    await btnFstat.tap()
    await isDone()
    let fstat = await getData("fstat")
    expect(fstat.size > 0).toBe(true)
1329
    console.log('writeTest', '4')
W
WOSHIMAHAIFENG 已提交
1330 1331 1332 1333 1334 1335 1336

    //fstatSyncTest
    btnFstat = await page.$('#btn-fstat-file-sync')
    await btnFstat.tap()
    await isDone()
    fstat = await getData("fstat")
    expect(fstat.size > 0).toBe(true)
1337
    console.log('writeTest', '5')
W
WOSHIMAHAIFENG 已提交
1338 1339 1340 1341 1342 1343 1344 1345 1346 1347

    //ftruncateFileTest
    let btnFTruncateFile = await page.$('#btn-ftruncate-file')
    await btnFTruncateFile.tap()
    await isDone()
    let ftruncateRet = await getData("ftruncateRet")
    expect(fstat).not.toEqual('ftruncate:ok')
    await page.setData({
      ftruncate: '',
    })
1348 1349
    console.log('writeTest', '6')

W
WOSHIMAHAIFENG 已提交
1350 1351 1352 1353 1354 1355
    //ftruncateFileSyncTest
    btnFTruncateFile = await page.$('#btn-ftruncate-file-sync')
    await btnFTruncateFile.tap()
    await isDone()
    ftruncateRet = await getData("ftruncateRet")
    expect(fstat).not.toEqual('ftruncate:ok')
1356
    console.log('writeTest', '7')
W
WOSHIMAHAIFENG 已提交
1357
  });
1358 1359 1360 1361 1362 1363
  //writeTest writeSyncTest
  it('ftruncateFileTest', async () => {
    if (!isAndroid()) {
      return
    }
    console.log('ftruncateFileTest', 'start')
W
WOSHIMAHAIFENG 已提交
1364

1365
    await page.setData({
W
WOSHIMAHAIFENG 已提交
1366
      basePath: mBasePath,
1367 1368 1369 1370 1371 1372
      logAble: false,
      mkdirFile: 'fd',
      writeFile: 'fd/1.txt',
      readFile: 'fd/1.txt',
      writeData: '我是一只小小鸟我是'
    })
W
WOSHIMAHAIFENG 已提交
1373
    await clearDir('fd')
1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391
    await createFile()
    console.log('ftruncateFileTest', '1')
    btnWrite = await page.$('#btn-write-sync')
    await btnWrite.tap()
    await isDone()
    bytesWritten = await getData("bytesWritten")
    expect(bytesWritten).toEqual(9)
    console.log('ftruncateFileTest', '3')
    //ftruncateFileTest
    let btnFTruncateFile = await page.$('#btn-ftruncate-file')
    await btnFTruncateFile.tap()
    await isDone()
    let ftruncateRet = await getData("ftruncateRet")
    expect(ftruncateRet).toEqual('ftruncate:ok')
    await page.setData({
      ftruncate: '',
    })
    console.log('ftruncateFileTest', '6')
W
WOSHIMAHAIFENG 已提交
1392

1393 1394 1395 1396 1397 1398 1399 1400
    //ftruncateFileSyncTest
    btnFTruncateFile = await page.$('#btn-ftruncate-file-sync')
    await btnFTruncateFile.tap()
    await isDone()
    ftruncateRet = await getData("ftruncateRet")
    expect(ftruncateRet).toEqual('ftruncate:ok')
    console.log('ftruncateFileTest', '7')
  });
1401
});