提交 d76ce473 编写于 作者: W WOSHIMAHAIFENG

添加filemanager 测试用例

上级 aad5875e
......@@ -9,6 +9,7 @@ describe('ExtApi-FileManagerTest', () => {
return
}
let page;
function getData(key = '') {
return new Promise(async (resolve, reject) => {
const data = await page.data()
......@@ -26,7 +27,9 @@ describe('ExtApi-FileManagerTest', () => {
let isDone = await page.waitFor(async () => {
return await page.data('done')
})
await page.setData({done: false})
await page.setData({
done: false
})
return isDone
}
......@@ -35,16 +38,16 @@ describe('ExtApi-FileManagerTest', () => {
let globalUserDataPath = await getData('globalUserDataPath')
await page.setData({
logAble:false,
logAble: false,
recursiveVal: true,
copyToBasePath:globalUserDataPath,
copyToBasePath: globalUserDataPath,
basePath: globalUserDataPath,
rmDirFile:'a',
readDir:'a',
writeFile:'a/1.txt',
readFile:'a/1.txt',
unlinkFile:'a/1.txt',
writeFileContent:'锄禾日当午,汗滴禾下土,谁知盘中餐,粒粒皆辛苦'
rmDirFile: 'a',
readDir: 'a',
writeFile: 'a/1.txt',
readFile: 'a/1.txt',
unlinkFile: 'a/1.txt',
writeFileContent: '锄禾日当午,汗滴禾下土,谁知盘中餐,粒粒皆辛苦'
})
// 先清除文件,需要清除全部可能存在的历史测试文件,避免运行失败
......@@ -76,7 +79,7 @@ describe('ExtApi-FileManagerTest', () => {
// 先测试 recursive = false 文件夹创建,期望失败
await page.setData({
recursiveVal: false,
mkdirFile:'a/b/c'
mkdirFile: 'a/b/c'
})
......@@ -131,7 +134,7 @@ describe('ExtApi-FileManagerTest', () => {
// 更换文件内容 获取和对比 文件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"
getFileInfoAlgorithm: "md5"
})
await btnWriteFileButton.tap()
await isDone()
......@@ -139,7 +142,9 @@ describe('ExtApi-FileManagerTest', () => {
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.")
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."
)
const btnGetFileInfoButton = await page.$('#btn-get-file-info')
await btnGetFileInfoButton.tap()
......@@ -151,7 +156,7 @@ describe('ExtApi-FileManagerTest', () => {
expect(getFileInfoDigest).toEqual("29ddd02ed3c38ccebb98884eda082cb1")
// 切换为 sha1
await page.setData({
getFileInfoAlgorithm:"sha1"
getFileInfoAlgorithm: "sha1"
})
await btnGetFileInfoButton.tap()
......@@ -165,8 +170,8 @@ describe('ExtApi-FileManagerTest', () => {
// 测试 copyfile
await page.setData({
copyFromFile:"a/1.txt",
copyToFile:"a/2.txt"
copyFromFile: "a/1.txt",
copyToFile: "a/2.txt"
})
const btnCopyFileButton = await page.$('#btn-copy-file')
await btnCopyFileButton.tap()
......@@ -186,8 +191,8 @@ describe('ExtApi-FileManagerTest', () => {
// 测试 rename
await page.setData({
renameFromFile:"a/2.txt",
renameToFile:"a/3.txt"
renameFromFile: "a/2.txt",
renameToFile: "a/3.txt"
})
const btnRenameFileButton = await page.$('#btn-rename-file')
......@@ -213,19 +218,19 @@ describe('ExtApi-FileManagerTest', () => {
let version = process.env.uniTestPlatformInfo
version = parseInt(version.split(" ")[1])
let testDirName = "我们经历了一场兵慌马乱的战争.1@2#3$4%5^6&7*8(9)0+-qwertyuiopasdfghjklzxcvbnm;,"
if(version < 6){
if (version < 6) {
// android 6 以下文件名不能包含特殊字符
testDirName = "我们经历了一场兵慌马乱的战争"
}
await page.setData({
logAble:false,
logAble: false,
recursiveVal: true,
basePath: globalTempPath,
copyToBasePath:globalTempPath,
rmDirFile:'a',
copyToBasePath: globalTempPath,
rmDirFile: 'a',
mkdirFile: 'a',
unlinkFile:'a/'+ testDirName +'/中文路径/张三/name/中文文件.mock'
unlinkFile: 'a/' + testDirName + '/中文路径/张三/name/中文文件.mock'
})
......@@ -259,7 +264,7 @@ describe('ExtApi-FileManagerTest', () => {
// 测试 创建多层级文件目录
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',
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()
......@@ -276,7 +281,7 @@ describe('ExtApi-FileManagerTest', () => {
// 测试 创建包含中文特殊符号的目录
await page.setData({
recursiveVal: true,
mkdirFile:'a/'+ testDirName + '/中文路径/张三/name',
mkdirFile: 'a/' + testDirName + '/中文路径/张三/name',
})
await btnMkdDirButton.tap()
await isDone()
......@@ -298,8 +303,8 @@ describe('ExtApi-FileManagerTest', () => {
*/
await page.setData({
basePath: "",
readFile:'static/test-image/logo.ico',
readFileEncoding:'base64'
readFile: 'static/test-image/logo.ico',
readFileEncoding: 'base64'
})
......@@ -314,8 +319,8 @@ describe('ExtApi-FileManagerTest', () => {
await page.setData({
basePath: globalTempPath,
writeFile:'a/' + testDirName + '/中文路径/张三/name/中文文件.mock',
writeFileContent:readFileRet
writeFile: 'a/' + testDirName + '/中文路径/张三/name/中文文件.mock',
writeFileContent: readFileRet
})
......@@ -325,9 +330,9 @@ describe('ExtApi-FileManagerTest', () => {
// 获取文件列表,判断是否写入成功,同时置空base64内容 避免影响实时查看状态
await page.setData({
readDir:'a/' + testDirName + '/中文路径/张三/name',
readFileRet:'',
writeFileContent:''
readDir: 'a/' + testDirName + '/中文路径/张三/name',
readFileRet: '',
writeFileContent: ''
})
// 检查目录列表数量
......@@ -341,8 +346,8 @@ describe('ExtApi-FileManagerTest', () => {
// 更换文件内容 获取和对比 文件md5和sha1
await page.setData({
getFileInfoFile:'a/' + testDirName + '/中文路径/张三/name/中文文件.mock',
getFileInfoAlgorithm:"md5",
getFileInfoFile: 'a/' + testDirName + '/中文路径/张三/name/中文文件.mock',
getFileInfoAlgorithm: "md5",
})
const btnGetFileInfoButton = await page.$('#btn-get-file-info')
......@@ -356,7 +361,7 @@ describe('ExtApi-FileManagerTest', () => {
// 切换为 sha1
await page.setData({
getFileInfoAlgorithm:"sha1"
getFileInfoAlgorithm: "sha1"
})
await btnGetFileInfoButton.tap()
......@@ -369,7 +374,7 @@ describe('ExtApi-FileManagerTest', () => {
// 测试不支持的摘要算法,期望返回错误
await page.setData({
getFileInfoAlgorithm:"sha256"
getFileInfoAlgorithm: "sha256"
})
await btnGetFileInfoButton.tap()
......@@ -383,8 +388,8 @@ describe('ExtApi-FileManagerTest', () => {
// rename 到一个没有提前创建过的目录,期望返回错误
await page.setData({
renameFromFile:"a/" + testDirName + "/中文路径/张三/name/中文文件.mock",
renameToFile:"a/没有提前创建的目录/3.txt"
renameFromFile: "a/" + testDirName + "/中文路径/张三/name/中文文件.mock",
renameToFile: "a/没有提前创建的目录/3.txt"
})
const btnRenameFileButton = await page.$('#btn-rename-file')
......@@ -399,14 +404,14 @@ describe('ExtApi-FileManagerTest', () => {
// 非递归创建一级目录。期望成功
await page.setData({
recursiveVal: false,
mkdirFile:'a/提前创建的目录',
mkdirFile: 'a/提前创建的目录',
})
await btnMkdDirButton.tap()
await isDone()
await page.setData({
readDir:'a',
readDir: 'a',
})
await btnReadDirButton.tap()
......@@ -421,16 +426,16 @@ describe('ExtApi-FileManagerTest', () => {
await page.setData({
copyFromFile:"a/" + testDirName + "/中文路径/张三/name/中文文件.mock",
copyToFile:"a/提前创建的目录/4.txt"
copyFromFile: "a/" + testDirName + "/中文路径/张三/name/中文文件.mock",
copyToFile: "a/提前创建的目录/4.txt"
})
const btnCopyFileButton = await page.$('#btn-copy-file')
await btnCopyFileButton.tap()
await isDone()
const btnCopyFileButton = await page.$('#btn-copy-file')
await btnCopyFileButton.tap()
await isDone()
await page.setData({
readDir:'a/提前创建的目录',
readDir: 'a/提前创建的目录',
})
await btnReadDirButton.tap()
......@@ -442,8 +447,8 @@ describe('ExtApi-FileManagerTest', () => {
expect(JSON.stringify(fileListSuccess)).toEqual("[\"4.txt\"]")
await page.setData({
unlinkFile:'a/提前创建的目录/4.txt',
rmDirFile:'a/提前创建的目录'
unlinkFile: 'a/提前创建的目录/4.txt',
rmDirFile: 'a/提前创建的目录'
})
await btnUnLinkFileButton.tap()
await isDone()
......@@ -467,13 +472,13 @@ describe('ExtApi-FileManagerTest', () => {
let globalRootPath = await getData('globalRootPath')
await page.setData({
recursiveVal: true,
logAble:false,
logAble: false,
basePath: globalRootPath,
readDir:'a',
rmDirFile:'a',
mkdirFile:'a',
accessFile:'a/从代码目录拷贝的资源.png',
unlinkFile:'a/从代码目录拷贝的资源.png'
readDir: 'a',
rmDirFile: 'a',
mkdirFile: 'a',
accessFile: 'a/从代码目录拷贝的资源.png',
unlinkFile: 'a/从代码目录拷贝的资源.png'
})
......@@ -515,8 +520,8 @@ describe('ExtApi-FileManagerTest', () => {
// 准备从资源目录拷贝png
await page.setData({
basePath: "",
unlinkFile:'static/test-image/logo.ico',
accessFile:'static/test-image/logo.ico',
unlinkFile: 'static/test-image/logo.ico',
accessFile: 'static/test-image/logo.ico',
})
// 检查资源文件,期望存在
await btnAccessFileButton.tap()
......@@ -537,9 +542,9 @@ describe('ExtApi-FileManagerTest', () => {
expect(accessFileRet).toEqual('access:ok')
// 复制资源到 root目录
await page.setData({
copyToBasePath:globalRootPath,
copyFromFile:"static/test-image/logo.ico",
copyToFile:"a/从代码目录拷贝的资源.png"
copyToBasePath: globalRootPath,
copyFromFile: "static/test-image/logo.ico",
copyToFile: "a/从代码目录拷贝的资源.png"
})
const btnCopyFileButton = await page.$('#btn-copy-file')
await btnCopyFileButton.tap()
......@@ -547,10 +552,10 @@ describe('ExtApi-FileManagerTest', () => {
// 检查期望 root 目录中图片文件存在
await page.setData({
basePath:globalRootPath,
unlinkFile:'a/从代码目录拷贝的资源.png',
accessFile:'a/从代码目录拷贝的资源.png',
rmDirFile:'a',
basePath: globalRootPath,
unlinkFile: 'a/从代码目录拷贝的资源.png',
accessFile: 'a/从代码目录拷贝的资源.png',
rmDirFile: 'a',
})
await btnAccessFileButton.tap()
await isDone()
......@@ -602,19 +607,19 @@ describe('ExtApi-FileManagerTest', () => {
let globalTempPath = await getData('globalTempPath')
await page.setData({
recursiveVal: true,
logAble:false,
logAble: false,
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"
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"
})
// 先清除文件,需要清除全部可能存在的历史测试文件,避免运行失败
......@@ -663,7 +668,7 @@ describe('ExtApi-FileManagerTest', () => {
// 尝试读取base64 信息
await page.setData({
readFileEncoding:"base64",
readFileEncoding: "base64",
})
await btnReadFileButton.tap()
......@@ -673,9 +678,9 @@ describe('ExtApi-FileManagerTest', () => {
// 测试ascii,需要特别测试 ascii 写入非法字符的情况,因为微信的常量字符编码和android原生有差异。
await page.setData({
writeFileContent:"丙辰中秋,欢饮达旦,大醉,作此篇,兼怀子由。明月几时有?把酒问青天。不知天上宫阙,今夕是何年。我欲乘风归去,又恐琼楼玉宇,高处不胜寒。起舞弄清影,何似在人间",
writeFileEncoding:"ascii",
readFileEncoding:"base64",
writeFileContent: "丙辰中秋,欢饮达旦,大醉,作此篇,兼怀子由。明月几时有?把酒问青天。不知天上宫阙,今夕是何年。我欲乘风归去,又恐琼楼玉宇,高处不胜寒。起舞弄清影,何似在人间",
writeFileEncoding: "ascii",
readFileEncoding: "base64",
})
await btnWriteFileButton.tap()
......@@ -692,13 +697,15 @@ describe('ExtApi-FileManagerTest', () => {
await btnReadFileButton.tap()
await isDone()
readFileRet = await getData('readFileRet')
expect(readFileRet).toEqual("GbAtywwibr7mDCeJDFxkxwx8AFAxAg4I4PYJH4pS7lIpAg3lKQqrGQzKFS9VdAIRMljOUrsMyFA8fImHDNgEDdzSAnceBAVxDFU8KLr0")
expect(readFileRet).toEqual(
"GbAtywwibr7mDCeJDFxkxwx8AFAxAg4I4PYJH4pS7lIpAg3lKQqrGQzKFS9VdAIRMljOUrsMyFA8fImHDNgEDdzSAnceBAVxDFU8KLr0"
)
// 尝试写入合法ascii
await page.setData({
writeFileContent:"hello jack.hello marry.",
writeFileEncoding:"ascii",
readFileEncoding:"ascii",
writeFileContent: "hello jack.hello marry.",
writeFileEncoding: "ascii",
readFileEncoding: "ascii",
})
await btnWriteFileButton.tap()
......@@ -711,9 +718,9 @@ describe('ExtApi-FileManagerTest', () => {
// 写入base64 获取 中文
await page.setData({
writeFileContent:"5LiZ6L6w5Lit56eL77yM5qyi6aWu6L6+5pem77yM5aSn6YaJ77yM5L2c5q2k56+H77yM5YW85oCA5a2Q55Sx44CC5piO5pyI5Yeg5pe25pyJ77yf5oqK6YWS6Zeu6Z2S5aSp44CC5LiN55+l5aSp5LiK5a6r6ZiZ77yM5LuK5aSV5piv5L2V5bm044CC5oiR5qyy5LmY6aOO5b2S5Y6777yM5Y+I5oGQ55C85qW8546J5a6H77yM6auY5aSE5LiN6IOc5a+S44CC6LW36Iie5byE5riF5b2x77yM5L2V5Ly85Zyo5Lq66Ze0",
writeFileEncoding:"base64",
readFileEncoding:"utf-8",
writeFileContent: "5LiZ6L6w5Lit56eL77yM5qyi6aWu6L6+5pem77yM5aSn6YaJ77yM5L2c5q2k56+H77yM5YW85oCA5a2Q55Sx44CC5piO5pyI5Yeg5pe25pyJ77yf5oqK6YWS6Zeu6Z2S5aSp44CC5LiN55+l5aSp5LiK5a6r6ZiZ77yM5LuK5aSV5piv5L2V5bm044CC5oiR5qyy5LmY6aOO5b2S5Y6777yM5Y+I5oGQ55C85qW8546J5a6H77yM6auY5aSE5LiN6IOc5a+S44CC6LW36Iie5byE5riF5b2x77yM5L2V5Ly85Zyo5Lq66Ze0",
writeFileEncoding: "base64",
readFileEncoding: "utf-8",
})
await btnWriteFileButton.tap()
......@@ -722,16 +729,19 @@ describe('ExtApi-FileManagerTest', () => {
await btnReadFileButton.tap()
await isDone()
readFileRet = await getData('readFileRet')
expect(readFileRet).toEqual("丙辰中秋,欢饮达旦,大醉,作此篇,兼怀子由。明月几时有?把酒问青天。不知天上宫阙,今夕是何年。我欲乘风归去,又恐琼楼玉宇,高处不胜寒。起舞弄清影,何似在人间")
expect(readFileRet).toEqual(
"丙辰中秋,欢饮达旦,大醉,作此篇,兼怀子由。明月几时有?把酒问青天。不知天上宫阙,今夕是何年。我欲乘风归去,又恐琼楼玉宇,高处不胜寒。起舞弄清影,何似在人间")
await page.setData({
readFileEncoding:"base64",
readFileEncoding: "base64",
})
await btnReadFileButton.tap()
await isDone()
readFileRet = await getData('readFileRet')
expect(readFileRet).toEqual("5LiZ6L6w5Lit56eL77yM5qyi6aWu6L6+5pem77yM5aSn6YaJ77yM5L2c5q2k56+H77yM5YW85oCA5a2Q55Sx44CC5piO5pyI5Yeg5pe25pyJ77yf5oqK6YWS6Zeu6Z2S5aSp44CC5LiN55+l5aSp5LiK5a6r6ZiZ77yM5LuK5aSV5piv5L2V5bm044CC5oiR5qyy5LmY6aOO5b2S5Y6777yM5Y+I5oGQ55C85qW8546J5a6H77yM6auY5aSE5LiN6IOc5a+S44CC6LW36Iie5byE5riF5b2x77yM5L2V5Ly85Zyo5Lq66Ze0")
expect(readFileRet).toEqual(
"5LiZ6L6w5Lit56eL77yM5qyi6aWu6L6+5pem77yM5aSn6YaJ77yM5L2c5q2k56+H77yM5YW85oCA5a2Q55Sx44CC5piO5pyI5Yeg5pe25pyJ77yf5oqK6YWS6Zeu6Z2S5aSp44CC5LiN55+l5aSp5LiK5a6r6ZiZ77yM5LuK5aSV5piv5L2V5bm044CC5oiR5qyy5LmY6aOO5b2S5Y6777yM5Y+I5oGQ55C85qW8546J5a6H77yM6auY5aSE5LiN6IOc5a+S44CC6LW36Iie5byE5riF5b2x77yM5L2V5Ly85Zyo5Lq66Ze0"
)
});
......@@ -741,12 +751,12 @@ describe('ExtApi-FileManagerTest', () => {
await page.setData({
recursiveVal: true,
copyToBasePath:globalRootPath,
copyToBasePath: globalRootPath,
basePath: globalRootPath,
globalTempPath:globalRootPath,
rmDirFile:'a',
mkdirFile:'a',
unlinkFile:'a/1.txt',
globalTempPath: globalRootPath,
rmDirFile: 'a',
mkdirFile: 'a',
unlinkFile: 'a/1.txt',
})
// 先清除文件,需要清除全部可能存在的历史测试文件,避免运行失败
......@@ -755,13 +765,13 @@ describe('ExtApi-FileManagerTest', () => {
await isDone()
await page.setData({
unlinkFile:'a/2.txt',
unlinkFile: 'a/2.txt',
})
await btnUnLinkFileButton.tap()
await isDone()
await page.setData({
unlinkFile:'a/m/3.txt',
unlinkFile: 'a/m/3.txt',
})
await btnUnLinkFileButton.tap()
await isDone()
......@@ -789,10 +799,10 @@ describe('ExtApi-FileManagerTest', () => {
// 写入一个文件
await page.setData({
writeFileContent: "锄禾日当午,汗滴禾下土,谁知盘中餐,粒粒皆辛苦",
writeFileEncoding:"utf-8",
writeFile:'a/1.txt',
recursiveVal:false,
statFile:'a/1.txt',
writeFileEncoding: "utf-8",
writeFile: 'a/1.txt',
recursiveVal: false,
statFile: 'a/1.txt',
})
let lastFailError = await getData('lastFailError')
......@@ -802,7 +812,7 @@ describe('ExtApi-FileManagerTest', () => {
await btnWriteFileButton.tap()
await isDone()
const btnStatFileButton = await page.$('#btn-stat-file')
let btnStatFileButton = await page.$('#btn-stat-file')
await btnStatFileButton.tap()
await isDone()
......@@ -812,17 +822,31 @@ describe('ExtApi-FileManagerTest', () => {
expect(statsRet[0].path).toMatch(new RegExp('.*/a/1.txt$'))
expect(statsRet[0].stats.size).toEqual(69)
// 写入一个文件
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)
/**
* 创建子目录和子目录文件,测试recursive参数
*/
await page.setData({
writeFileContent: "1234567890",
writeFileEncoding:"ascii",
writeFile:'a/2.txt',
writeFileEncoding: "ascii",
writeFile: 'a/2.txt',
basePath: globalRootPath,
recursiveVal:false,
statFile:'a',
mkdirFile:'a/m',
recursiveVal: false,
statFile: 'a',
mkdirFile: 'a/m',
})
......@@ -836,9 +860,9 @@ describe('ExtApi-FileManagerTest', () => {
// 复制一份文件到 /a/m/3.txt
await page.setData({
// asset 只能正式版测试,这里只能模拟返回路径
basePath:'',
copyFromFile:'static/test-image/logo.ico',
copyToFile:'a/m/3.txt',
basePath: '',
copyFromFile: 'static/test-image/logo.ico',
copyToFile: 'a/m/3.txt',
})
const btnCopyFileButton = await page.$('#btn-copy-file')
await btnCopyFileButton.tap()
......@@ -847,8 +871,8 @@ describe('ExtApi-FileManagerTest', () => {
await page.setData({
basePath: globalRootPath,
recursiveVal:true,
statFile:'a',
recursiveVal: true,
statFile: 'a',
})
await btnStatFileButton.tap()
......@@ -857,10 +881,10 @@ describe('ExtApi-FileManagerTest', () => {
// 读取全部文件信息
statsRet = await getData('statsRet')
statsRet.sort(function(a, b){
if (a.path > b.path){
statsRet.sort(function(a, b) {
if (a.path > b.path) {
return 1
} else if (a.path < b.path){
} else if (a.path < b.path) {
return -1
}
return 0
......@@ -879,22 +903,22 @@ describe('ExtApi-FileManagerTest', () => {
// 清理文件,避免影响其他测试用例
await page.setData({
unlinkFile:'a/1.txt',
unlinkFile: 'a/1.txt',
})
await btnUnLinkFileButton.tap()
await isDone()
await page.setData({
unlinkFile:'a/2.txt',
unlinkFile: 'a/2.txt',
})
await btnUnLinkFileButton.tap()
await isDone()
await page.setData({
unlinkFile:'a/m/3.txt',
rmDirFile:'a',
readDir:'a',
recursiveVal:true,
unlinkFile: 'a/m/3.txt',
rmDirFile: 'a',
readDir: 'a',
recursiveVal: true,
})
await btnUnLinkFileButton.tap()
await isDone()
......@@ -913,4 +937,363 @@ describe('ExtApi-FileManagerTest', () => {
});
it('appendFileTest', async () => {
await page.setData({
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()
// // 先用utf-8 写入内容
const btnWriteFileButton = await page.$('#btn-write-file')
await btnWriteFileButton.tap()
await isDone()
// //追加内容
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 () => {
await page.setData({
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',
})
// 先清除文件,需要清除全部可能存在的历史测试文件,避免运行失败
const btnUnLinkFileButton = await page.$('#btn-unlink-file-sync')
await btnUnLinkFileButton.tap()
await isDone()
//清除文件夹
const btnRmDirButton = await page.$('#btn-remove-dir-sync')
await btnRmDirButton.tap()
await isDone()
// 重新创建测试目录,期望通过 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({
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')
clearDir('sync')
});
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,
})
const btnClear = await page.$('#btn-clear-file')
await btnClear.tap()
await isDone()
}
//saveFileTest saveFileSyncTest getSavedFileListTest removeSavedFileTest
it('savefile test',
async () => {
let globalTempPath = await getData('globalTempPath')
let basePath = await getData('basePath')
await page.setData({
basePath: basePath
})
await clearDir('')
await page.setData({
basePath: globalTempPath,
temFile: 'save/1.txt',
mkdirFile: 'save',
writeFile: 'save/1.txt',
accessFile: '1.txt'
})
await createFile()
await page.setData({
basePath: basePath,
temFile: 'save/1.txt',
})
let btnSaveFile = await page.$('#btn-save-file')
await btnSaveFile.tap()
await isDone()
let saveFileRet = await getData("saveFileRet")
expect(saveFileRet).not.toBe('');
await page.setData({
saveFileRet: ''
})
await page.setData({
basePath: globalTempPath,
temFile: 'save/2.txt',
mkdirFile: 'save',
writeFile: 'save/2.txt',
accessFile: '2.txt'
})
await createFile()
await page.setData({
basePath: basePath,
writeFile: 'save/2.txt',
})
btnSaveFile = await page.$('#btn-save-file-sync')
await btnSaveFile.tap()
await isDone()
let btnRemoveSavedFileRet = await page.$('#btn-remove-saved-file')
await btnRemoveSavedFileRet.tap()
let removeSavedFileRet = await getData("removeSavedFileRet")
expect(removeSavedFileRet).toEqual('removeSavedFile:ok')
await page.setData({
removeSavedFileRet: ''
})
let btnSavedFileList = await page.$('#btn-getsaved-filelist')
await btnSavedFileList.tap()
await isDone()
let fileListSuccess = await getData("fileListSuccess")
expect(fileListSuccess.length).toEqual(3)
await clearDir('')
});
//openFiletest openFileSynctest closeTest closeTestSync writeTest writeSyncTest
it('fd test', async () => {
await clearDir('')
await page.setData({
mkdirFile: 'fd',
writeFile: 'fd/1.txt',
readFile: 'fd/1.txt'
})
await createFile()
//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: '',
})
//openFileSynctest
btnOpenFile = await page.$('#btn-open-file-sync')
await btnOpenFile.tap()
await isDone()
fd = await getData("fd")
expect(fd).not.toBe('');
//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')
//writeTest
await page.setData({
writeFile: 'fd/1.txt',
readFile: 'fd/1.txt',
writeData: '我是一只小小鸟'
})
let btnWrite = await page.$('#btn-write')
await btnWrite.tap()
await isDone()
let bytesWritten = await getData("bytesWritten")
expect(bytesWritten).toEqual(7)
//writeSyncTest
await page.setData({
writeFile: 'fd/1.txt',
readFile: 'fd/1.txt',
writeData: '我是'
})
btnWrite = await page.$('#btn-write')
await btnWrite.tap()
await isDone()
bytesWritten = await getData("bytesWritten")
expect(bytesWritten).toEqual(2)
//fstatTest
let btnFstat = await page.$('#btn-fstat-file')
await btnFstat.tap()
await isDone()
let fstat = await getData("fstat")
expect(fstat.size > 0).toBe(true)
//fstatSyncTest
btnFstat = await page.$('#btn-fstat-file-sync')
await btnFstat.tap()
await isDone()
fstat = await getData("fstat")
expect(fstat.size > 0).toBe(true)
//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: '',
})
//ftruncateFileSyncTest
btnFTruncateFile = await page.$('#btn-ftruncate-file-sync')
await btnFTruncateFile.tap()
await isDone()
ftruncateRet = await getData("ftruncateRet")
expect(fstat).not.toEqual('ftruncate:ok')
});
});
<template>
<!-- #ifdef APP -->
<scroll-view class="page-scroll-view">
<scroll-view style="flex: 1;">
<!-- #endif -->
<text>显示简易操作日志,详细日志需真机运行查看</text><button size="mini" @click="log=''">清空日志</button>
<text style="margin: 2px; padding: 2px; border: 1px solid #000000;">{{ log }}</text>
......@@ -17,6 +17,45 @@
<button type="primary" @tap="copyStaticToFilesTest" id="btn-copyStatic-file">从static目录复制文件到a目录</button>
<button type="primary" @tap="unlinkAllFileTest" id="btn-clear-file">删除文件夹{{rmDirFile}}下的所有文件</button>
<button type="primary" @tap="rmdirTest" id="btn-remove-dir">删除文件夹{{rmDirFile}}</button>
<button type="primary" @tap="statFileInfoSyncTest"
id="btn-stat-file-sync">同步递归获取目录files的Stats对象{{statFile}}</button>
<button type="primary" @tap="appendFileTest" id="btn-append-file">在文件{{readFile}}结尾追加内容</button>
<button type="primary" @tap="appendFileSyncTest" id="btn-append-file-sync">同步在文件{{readFile}}结尾追加内容</button>
<button type="primary" @tap="writeFileSyncTest" id="btn-write-file-sync">同步覆盖写入文件{{writeFile}}</button>
<button type="primary" @tap="readFileSyncTest" id="btn-read-file-sync">同步读取文件{{readFile}}</button>
<button type="primary" @tap="unlinkSyncTest" id="btn-unlink-file-sync">同步删除文件{{unlinkFile}}</button>
<button type="primary" @tap="mkdirSyncTest" id="btn-mkdir-sync">同步创建文件夹{{mkdirFile}}</button>
<button type="primary" @tap="rmdirSyncTest" id="btn-remove-dir-sync">同步删除文件夹{{rmDirFile}}</button>
<button type="primary" @tap="readDirSyncTest" id="btn-read-dir-sync">同步读取文件夹{{readDir}}</button>
<button type="primary" @tap="accessFileSyncTest" id="btn-access-file-sync">同步判断文件{{accessFile}}是否存在</button>
<button type="primary" @tap="renameFileSync"
id="btn-rename-file-sync">同步重命名文件{{renameFromFile}}到{{renameToFile}}</button>
<button type="primary" @tap="copyFileSyncTest"
id="btn-copy-file-sync">同步复制文件{{copyFromFile}}到{{copyToFile}}</button>
<button type="primary" @tap="saveFileTest" id="btn-save-file">保存临时文件到本地</button>
<button type="primary" @tap="saveFileSyncTest" id="btn-save-file-sync">同步保存临时文件到本地</button>
<button type="primary" @tap="removeSavedFileTest" id="btn-remove-saved-file">删除已保存的本地文件</button>
<button type="primary" @tap="unzipFileTest" id="btn-unzip-file-sync">解压文件</button>
<button type="primary" @tap="getSavedFileListTest" id="btn-getsaved-filelist">获取该已保存的本地缓存文件列表</button>
<button type="primary" @tap="truncateFileTest" id="btn-truncate-file">对文件{{writeFile}}内容进行截断操作</button>
<button type="primary" @tap="truncateFileSyncTest" id="btn-truncate-file-sync">同步对文件{{writeFile}}内容进行截断操作</button>
<button type="primary" @tap="readCompressedFileTest" id="btn-compressed-file">读取指定压缩类型的本地文件内容</button>
<button type="primary" @tap="readCompressedFileSyncTest" id="btn-compressed-file-sync">同步读取指定压缩类型的本地文件内容</button>
<button type="primary" @tap="openFileTest" id="btn-open-file">打开文件{{readFile}},返回描述符</button>
<button type="primary" @tap="openFileSyncTest" id="btn-open-file-sync">同步打开文件{{readFile}},返回描述符</button>
<button type="primary" @tap="closeTest" id="btn-close-file">通过文件描述符关闭文件{{readFile}}</button>
<button type="primary" @tap="closeSyncTest" id="btn-close-file-sync">通过文件描述符同步关闭文件{{readFile}}</button>
<button type="primary" @tap="writeTest" id="btn-write">通过文件描述符写入文件{{readFile}}</button>
<button type="primary" @tap="writeSyncTest" id="btn-read-file">同步通过文件描述符写入文件{{readFile}}</button>
<button type="primary" @tap="fstatTest" id="btn-fstat-file">通过文件描述符获取{{statFile}}的状态信息</button>
<button type="primary" @tap="fstatSyncTest" id="btn-fstat-file-sync">同步通过文件描述符获取{{statFile}}的状态信息</button>
<button type="primary" @tap="ftruncateFileTest" id="btn-ftruncate-file">通过文件描述符对文件{{writeFile}}内容进行截断操作</button>
<button type="primary" @tap="ftruncateFileSyncTest"
id="btn-ftruncate-file-sync">同步通过文件描述符对文件{{writeFile}}内容进行截断操作</button>
<!-- #ifdef APP -->
</scroll-view>
<!-- #endif -->
......@@ -40,12 +79,16 @@
readDir: 'a',
readFileRet: "",
writeFileContent: "中文 en.\r\n\t换行",
appendFileContent: "append content",
getFileInfoAlgorithm: "md5",
getFileInfoSize: -1,
getFileInfoDigest: "",
unlinkFile: 'a/1.txt',
accessFile: 'a/1.txt',
writeFile: 'a/1.txt',
writeData: 'insert data哈哈哈',
brFile: 'a/1.txt.br',
temFile: 'a/1.txt',
copyFromFile: 'a/1.txt',
copyToFile: 'a/2.txt',
renameFromFile: 'a/2.txt',
......@@ -60,6 +103,16 @@
writeFileEncoding: "utf-8",
readFileEncoding: "utf-8",
statsRet: [] as Array<FileStats>,
unzipFile: 'zip/1.zip',
targetZip: "unzip",
renameFileRet: '',
saveFileRet: '',
removeSavedFileRet: '',
fd: '',
closeFileRet: '',
bytesWritten: 0,
fstat: new Stats(),
ftruncateRet:'',
/**
* 待测试的全局环境变量
*/
......@@ -76,7 +129,6 @@
methods: {
statFileInfoTest: function (_ : any) {
const fileManager = uni.getFileSystemManager()
fileManager.stat({
// path: `${this.basePath}${this.statFile}`, //USER_DATA_PATH
path: `${this.globalTempPath}${this.statFile}`, //CACHE_PATH
......@@ -172,7 +224,7 @@
fileManager.rename({
oldPath: `${this.basePath}${this.renameFromFile}`,
newPath: `${this.basePath}${this.renameToFile}`,
success: (res : FileManagerSuccessResult) => {
success: (res) => {
if (this.logAble) {
this.log += 'renameFileTest success:' + JSON.stringify(res) + '\n\n'
}
......@@ -232,13 +284,13 @@
filePath: `${this.basePath}${this.writeFile}`,
data: this.writeFileContent,
encoding: this.writeFileEncoding,
success: (res : FileManagerSuccessResult) => {
success: (res) => {
if (this.logAble) {
this.log += 'writeFileTest success:' + JSON.stringify(res) + '\n\n'
}
console.log('success', res)
},
fail: (res : UniError) => {
fail: (res : UniError) => {
if (this.logAble) {
this.log += 'writeFileTest fail:' + JSON.stringify(res) + '\n\n'
}
......@@ -251,6 +303,7 @@
if (res instanceof UniError) {
this.lastCompleteError = res
}
}
} as WriteFileOptions)
},
......@@ -422,7 +475,7 @@
console.log('fail unlink', res)
this.lastFailError = res
},
complete : (res : any) => {
complete: (res : any) => {
if (res instanceof UniError) {
this.lastCompleteError = res
}
......@@ -432,12 +485,12 @@
} as UnLinkOptions)
});
},
fail : (res : UniError) => {
fail: (res : UniError) => {
this.log += 'unlinkAllFileTest fail:' + JSON.stringify(res) + '\n\n'
console.log('fail to readdir', res)
this.lastFailError = res
},
complete : (res : any) => {
complete: (res : any) => {
console.log("complete readdir", res)
this.done = true
if (res instanceof UniError) {
......@@ -454,7 +507,7 @@
fileManager.copyFile({
srcPath: "/static/list-mock/mock.json",
destPath: `${this.copyToBasePath}/a/mock.json`,
success: (res : FileManagerSuccessResult) => {
success: (res : FileManagerSuccessResult) => {
if (this.logAble) {
this.log += 'copyFileTest success:' + JSON.stringify(res) + '\n\n'
}
......@@ -476,7 +529,719 @@
}
} as CopyFileOptions)
},
}
//start
appendFileTest: function (_ : any) {
const fileManager = uni.getFileSystemManager()
fileManager.appendFile({
filePath: `${this.basePath}${this.writeFile}`,
data: this.appendFileContent,
encoding: this.writeFileEncoding,
success: (res : FileManagerSuccessResult) => {
if (this.logAble) {
this.log += 'appendFileTest success:' + JSON.stringify(res) + '\n\n'
}
console.log('success', res)
},
fail: (res : UniError) => {
if (this.logAble) {
this.log += 'appendFileTest fail:' + JSON.stringify(res) + '\n\n'
}
console.log('fail')
this.lastFailError = res
},
complete: (res : any) => {
this.done = true
console.log("complete")
if (res instanceof UniError) {
this.lastCompleteError = res
}
}
} as AppendFileOptions)
},
writeFileSyncTest: function (_ : any) {
try {
const fileManager = uni.getFileSystemManager()
fileManager.writeFileSync(`${this.basePath}${this.writeFile}`, this.writeFileContent, this.writeFileEncoding)
if (this.logAble) {
this.log += 'writeFileSyncTest success:' + '\n\n'
}
this.done = true
} catch (e) {
if (this.logAble) {
this.log += 'writeFileSyncTest fail:' + e + '\n\n'
}
this.done = true
}
},
readFileSyncTest: function () {
try {
const fileManager = uni.getFileSystemManager()
let data = fileManager.readFileSync(
`${this.basePath}${this.readFile}`,
this.readFileEncoding)
if (this.logAble) {
this.log += 'readFileSyncTest result:' + data + '\n\n'
}
this.done = true
this.readFileRet = data
} catch (e) {
if (this.logAble) {
this.log += 'readFileSyncTest fail:' + e + '\n\n'
}
this.done = true
}
},
unlinkSyncTest: function () {
try {
const fileManager = uni.getFileSystemManager()
fileManager.unlinkSync(
`${this.basePath}${this.unlinkFile}`)
this.done = true
} catch (e) {
if (this.logAble) {
this.log += 'unlinkSyncTest fail:' + e + '\n\n'
}
this.done = true
}
},
mkdirSyncTest: function () {
// 准备测试数据
try {
const fileManager = uni.getFileSystemManager()
fileManager.mkdirSync(`${this.basePath}${this.mkdirFile}`, this.recursiveVal)
this.done = true
} catch (e) {
this.done = true
if (this.logAble) {
this.log += 'mkdirSyncTest fail:' + e + '\n\n'
}
}
},
rmdirSyncTest: function () {
try {
const fileManager = uni.getFileSystemManager()
fileManager.rmdirSync(
`${this.basePath}${this.rmDirFile}`,
this.recursiveVal)
this.done = true
} catch (e) {
if (this.logAble) {
this.log += 'rmdirSyncTest fail:' + e + '\n\n'
}
this.done = true
}
},
readDirSyncTest: function () {
try {
const fileManager = uni.getFileSystemManager()
let res = fileManager.readdirSync(
`${this.basePath}${this.readDir}`)
if (this.logAble) {
this.log += 'readDirTest success:' + JSON.stringify(res) + '\n\n'
}
if (res != null) {
this.fileListSuccess = res
}
this.done = true
} catch (e) {
if (this.logAble) {
this.log += 'rmdirSyncTest fail:' + e + '\n\n'
}
this.done = true
}
},
accessFileSyncTest: function () {
this.accessFileRet = ''
const fileManager = uni.getFileSystemManager()
try {
fileManager.accessSync(`${this.basePath}${this.accessFile}`)
this.done = true
this.accessFileRet = 'access:ok'
} catch (e) {
if (this.logAble) {
this.log += 'rmdirSyncTest fail:' + e + '\n\n'
}
this.done = true
}
},
renameFileSync: function () {
const fileManager = uni.getFileSystemManager()
try {
fileManager.renameSync(`${this.basePath}${this.renameFromFile}`,
`${this.basePath}${this.renameToFile}`)
this.done = true
this.renameFileRet = "rename:ok"
} catch (e) {
if (this.logAble) {
this.log += 'rmdirSyncTest fail:' + e + '\n\n'
}
console.log('renameSync:' + e)
this.done = true
}
},
copyFileSyncTest: function () {
const fileManager = uni.getFileSystemManager()
try {
fileManager.copyFileSync(
`${this.basePath}${this.copyFromFile}`,
`${this.copyToBasePath}${this.copyToFile}`)
this.done = true
} catch (e) {
if (this.logAble) {
this.log += 'rmdirSyncTest fail:' + e + '\n\n'
}
this.done = true
}
},
appendFileSyncTest: function (_ : any) {
const fileManager = uni.getFileSystemManager()
try {
fileManager.appendFileSync(
`${this.basePath}${this.writeFile}`,
this.appendFileContent,
this.writeFileEncoding)
this.done = true
} catch (e) {
if (this.logAble) {
this.log += 'rmdirSyncTest fail:' + e + '\n\n'
}
this.done = true
}
},
saveFileTest: function (_ : any) {
const fileManager = uni.getFileSystemManager()
console.log('=====' + uni.env.USER_DATA_PATH)
fileManager.saveFile({
tempFilePath: `${this.globalTempPath}${this.temFile}`,
// filePath:`${this.basePath}local/`,
success: (res : SaveFileSuccessResult) => {
if (this.logAble) {
this.log += 'saveFileTest success:' + JSON.stringify(res) + '\n\n'
}
console.log('success', res)
this.saveFileRet = res.savedFilePath
},
fail: (res : UniError) => {
if (this.logAble) {
this.log += 'saveFileTest fail:' + JSON.stringify(res) + '\n\n'
}
console.log('fail', res)
this.lastFailError = res
},
complete: (res) => {
this.done = true
}
} as SaveFileOptions)
},
saveFileSyncTest: function (_ : any) {
const fileManager = uni.getFileSystemManager()
try {
fileManager.saveFileSync(
`${this.globalTempPath}${this.temFile}`, `${this.basePath}local/`)
// filePath:`${this.basePath}local/`,)
this.done = true
//todo 后面打开
// this.saveFileRet=res
} catch (e) {
console.log(e)
this.done = true
}
},
unzipFileTest: function (_ : any) {
const fileManager = uni.getFileSystemManager()
fileManager.unzip({
zipFilePath: `${this.basePath}${this.unzipFile}`,
targetPath: `${this.basePath}${this.targetZip}`,
success: (res : FileManagerSuccessResult) => {
if (this.logAble) {
this.log += 'saveFileTest success:' + JSON.stringify(res) + '\n\n'
}
console.log('success', res)
},
fail: (res : UniError) => {
if (this.logAble) {
this.log += 'saveFileTest fail:' + JSON.stringify(res) + '\n\n'
}
console.log('fail', res)
this.lastFailError = res
},
complete: (res) => {
this.done = true
}
} as UnzipFileOptions)
},
getSavedFileListTest: function () {
const fileManager = uni.getFileSystemManager()
fileManager.getSavedFileList({
success: (res : GetSavedFileListResult) => {
if (this.logAble) {
this.log += 'getSavedFileListTest success:' + JSON.stringify(res) + '\n\n'
}
console.log("success", res)
this.fileListSuccess = res.fileList
},
fail: (res : UniError) => {
if (this.logAble) {
this.log += 'getSavedFileListTest fail:' + JSON.stringify(res) + '\n\n'
}
console.log('fail', res)
this.lastFailError = res
},
complete: (res : any) => {
console.log("complete", res)
this.done = true
if (res instanceof UniError) {
this.lastCompleteError = res
} else {
this.fileListComplete = (res as GetSavedFileListResult).fileList
}
}
} as GetSavedFileListOptions)
},
truncateFileTest() {
const fileManager = uni.getFileSystemManager()
fileManager.truncate({
filePath: `${this.basePath}${this.writeFile}`,
length: 7,
success: (res : FileManagerSuccessResult) => {
if (this.logAble) {
this.log += 'getSavedFileListTest success:' + JSON.stringify(res) + '\n\n'
}
console.log("success", res)
},
fail: (res : UniError) => {
if (this.logAble) {
this.log += 'getSavedFileListTest fail:' + JSON.stringify(res) + '\n\n'
}
console.log('fail', res)
this.lastFailError = res
},
complete: (res : any) => {
console.log("complete", res)
this.done = true
if (res instanceof UniError) {
this.lastCompleteError = res
}
}
} as TruncateFileOptions)
},
truncateFileSyncTest() {
const fileManager = uni.getFileSystemManager()
try {
fileManager.truncateSync(
`${this.basePath}${this.writeFile}`,
4)
this.done = true
} catch (e) {
console.log(e)
this.done = true
}
},
readCompressedFileTest() {
const fileManager = uni.getFileSystemManager()
fileManager.readCompressedFile({
filePath: `${this.basePath}${this.brFile}`,
compressionAlgorithm: "br",
success: (res : ReadCompressedFileResult) => {
if (this.logAble) {
this.log += 'readCompressedFileTest success:' + JSON.stringify(res) + '\n\n'
}
console.log("success", res)
},
fail: (res : UniError) => {
if (this.logAble) {
this.log += 'readCompressedFileTest fail:' + JSON.stringify(res) + '\n\n'
}
console.log('fail', res)
this.lastFailError = res
},
complete: (res) => {
this.done = true
}
} as ReadCompressedFileOptions)
},
readCompressedFileSyncTest() {
console.log('readCompressedFileSyncTest')
const fileManager = uni.getFileSystemManager()
try {
let data = fileManager.readCompressedFileSync(
`${this.basePath}${this.brFile}`,
"br")
if (this.logAble) {
this.log += data
}
this.done = true
} catch (e) {
if (this.logAble) {
this.log += 'rmdirSyncTest fail:' + e + '\n\n'
}
this.done = true
}
},
removeSavedFileTest() {
console.log("removeSavedFileTest enter")
const fileManager = uni.getFileSystemManager()
fileManager.removeSavedFile({
filePath: `${this.basePath}${this.writeFile}`,
success: (res : FileManagerSuccessResult) => {
if (this.logAble) {
this.log += 'removeSavedFileTest success:' + JSON.stringify(res) + '\n\n'
}
this.removeSavedFileRet = res.errMsg
console.log("removeSavedFileTest success", res)
},
fail: (res : UniError) => {
if (this.logAble) {
this.log += 'removeSavedFileTest fail:' + JSON.stringify(res) + '\n\n'
}
console.log('removeSavedFileTest fail', res)
},
complete: (res) => {
this.done = true
}
} as RemoveSavedFileOptions)
},
statFileInfoSyncTest: function (_ : any) {
const fileManager = uni.getFileSystemManager()
try {
let res = fileManager.statSync(
// path: `${this.basePath}${this.statFile}`, //USER_DATA_PATH
`${this.globalTempPath}${this.statFile}`, //CACHE_PATH
this.recursiveVal)
if (this.logAble) {
this.log += 'statFileInfoTest success:' + JSON.stringify(res) + '\n\n'
}
this.statsRet = res
this.done = true
} catch (e) {
if (this.logAble) {
this.log += 'rmdirSyncTest fail:' + e + '\n\n'
}
this.done = true
}
},
openFileTest() {
const fileManager = uni.getFileSystemManager()
fileManager.open({
filePath: `${this.basePath}${this.readFile}`,
flag: "a",
success: (res : OpenFileSuccessResult) => {
if (this.logAble) {
this.log += 'openFileTest success:' + JSON.stringify(res) + '\n\n'
}
console.log("success", res)
this.fd = res.fd
},
fail: (res : UniError) => {
if (this.logAble) {
this.log += 'openFileTest fail:' + JSON.stringify(res) + '\n\n'
}
console.log('fail', res)
this.lastFailError = res
},
complete: (res) => {
this.done = true
}
} as OpenFileOptions)
},
openFileSyncTest() : string {
const fileManager = uni.getFileSystemManager()
try {
let fd = fileManager.openSync({
filePath: `${this.basePath}${this.readFile}`,
flag: "r",
} as OpenFileSyncOptions)
if (this.logAble) {
this.log += 'openFileSyncTest success:' + fd + '\n\n'
}
this.done = true
this.fd = fd
return fd
} catch (e) {
if (this.logAble) {
this.log += 'openFileSyncTest fail:' + JSON.stringify(e) + '\n\n'
}
console.log('fail', e)
this.done = true
}
return ""
},
closeSyncTest() {
console.log('closeSyncTest')
const fileManager = uni.getFileSystemManager()
fileManager.open({
filePath: `${this.basePath}${this.readFile}`,
flag: "r+",
success: (res : OpenFileSuccessResult) => {
console.log("success", res)
if (res.fd.length <= 0) {
this.done = true
return
}
try {
console.log('closeSync')
fileManager.closeSync({
fd: res.fd
} as CloseSyncOptions)
this.log += 'closeSyncTest success:' + '\n\n'
this.done = true
this.closeFileRet = "close:ok"
} catch (e) {
if (this.logAble) {
this.log += 'closeSyncTest fail:' + JSON.stringify(e) + '\n\n'
}
console.log('fail', e)
this.done = true
}
},
fail: (res : UniError) => {
if (this.logAble) {
this.log += 'openFileTest fail:' + JSON.stringify(res) + '\n\n'
}
console.log('fail', res)
this.lastFailError = res
this.done = true
},
complete: (res) => {
}
} as OpenFileOptions)
},
closeTest() {
const fileManager = uni.getFileSystemManager()
fileManager.open({
filePath: `${this.basePath}${this.readFile}`,
flag: "r+",
success: (res : OpenFileSuccessResult) => {
console.log("success", res)
if (res.fd.length <= 0) {
this.done = true
return
}
fileManager.close({
fd: res.fd,
success: (res : FileManagerSuccessResult) => {
if (this.logAble) {
this.log += 'closeTest success:' + JSON.stringify(res) + '\n\n'
}
this.closeFileRet = res.errMsg
console.log("success", res)
},
fail: (res : UniError) => {
if (this.logAble) {
this.log += 'closeTest fail:' + JSON.stringify(res) + '\n\n'
}
console.log('fail', res)
this.lastFailError = res
},
complete: (res) => {
this.done = true
}
} as CloseOptions)
},
fail: (res : UniError) => {
if (this.logAble) {
this.log += 'openFileTest fail:' + JSON.stringify(res) + '\n\n'
}
console.log('fail', res)
this.lastFailError = res
this.done = true
}
} as OpenFileOptions)
},
writeTest() {
const fileManager = uni.getFileSystemManager()
fileManager.open({
filePath: `${this.basePath}${this.readFile}`,
flag: "r+",
success: (res : OpenFileSuccessResult) => {
console.log("success", res)
if (res.fd.length <= 0) {
this.done = true
return
}
fileManager.write({
fd: res.fd,
data: this.writeData,
encoding: "utf-8",
success: (res : WriteResult) => {
if (this.logAble) {
this.log += 'writeTest success:' + JSON.stringify(res) + '\n\n'
}
console.log("success", res)
this.bytesWritten = res.bytesWritten
},
fail: (res : UniError) => {
if (this.logAble) {
this.log += 'writeTest fail:' + JSON.stringify(res) + '\n\n'
}
console.log('fail', res)
this.lastFailError = res
},
complete: (res) => {
this.done = true
}
} as WriteOptions)
},
fail: (res : UniError) => {
if (this.logAble) {
this.log += 'openFileTest fail:' + JSON.stringify(res) + '\n\n'
}
console.log('fail', res)
this.lastFailError = res
this.done = true
}
} as OpenFileOptions)
},
writeSyncTest() {
const fileManager = uni.getFileSystemManager()
fileManager.open({
filePath: `${this.basePath}${this.readFile}`,
flag: "r+",
success: (res : OpenFileSuccessResult) => {
console.log("success", res)
if (res.fd.length <= 0) {
this.done = true
return
}
try {
let ret = fileManager.writeSync({
fd: res.fd,
data: this.writeData,
encoding: "utf-8"
} as WriteSyncOptions)
if (this.logAble) {
this.log += 'writeTest success:' + JSON.stringify(ret) + '\n\n'
}
console.log("success", ret)
this.done = true
this.bytesWritten = ret.bytesWritten
} catch (e) {
if (this.logAble) {
this.log += 'writeTest fail:' + JSON.stringify(e) + '\n\n'
}
console.log('fail', e)
this.done = true
}
},
fail: (res : UniError) => {
if (this.logAble) {
this.log += 'openFileTest fail:' + JSON.stringify(res) + '\n\n'
}
console.log('fail', res)
this.lastFailError = res
this.done = true
}
} as OpenFileOptions)
},
fstatTest() {
const fileManager = uni.getFileSystemManager()
fileManager.fstat({
fd: this.openFileSyncTest(),
success: (res : FStatSuccessResult) => {
if (this.logAble) {
this.log += 'fstatTest success:' + JSON.stringify(res) + '\n\n'
}
console.log("success", res)
this.fstat = res.stats
},
fail: (res : UniError) => {
if (this.logAble) {
this.log += 'fstatTest fail:' + JSON.stringify(res) + '\n\n'
}
console.log('fail', res)
this.lastFailError = res
},
complete: (res) => {
this.done = true
}
} as FStatOptions)
},
fstatSyncTest() {
const fileManager = uni.getFileSystemManager()
try {
let stat =
fileManager.fstatSync({
fd: this.openFileSyncTest(),
} as FStatSyncOptions)
if (this.logAble) {
this.log += 'fstatSyncTest success:' + JSON.stringify(stat) + '\n\n'
}
this.done = true
this.fstat = stat
} catch (e) {
if (this.logAble) {
this.log += 'fstatSyncTest fail:' + JSON.stringify(e) + '\n\n'
}
this.done = true
}
},
ftruncateFileTest() {
const fileManager = uni.getFileSystemManager()
fileManager.ftruncate({
fd: this.openFileSyncTest(),
length: 6,
success: (res : FileManagerSuccessResult) => {
if (this.logAble) {
this.log += 'ftruncateFileTest success:' + JSON.stringify(res) + '\n\n'
}
this.ftruncateRet=res.errMsg
},
fail: (res : UniError) => {
if (this.logAble) {
this.log += 'ftruncateFileTest fail:' + JSON.stringify(res) + '\n\n'
}
console.log('fail', res)
this.lastFailError = res
},
complete: (res : any) => {
console.log("complete", res)
this.done = true
if (res instanceof UniError) {
this.lastCompleteError = res
}
}
} as FTruncateFileOptions)
},
ftruncateFileSyncTest() {
const fileManager = uni.getFileSystemManager()
try {
fileManager.ftruncateSync({
fd: this.openFileSyncTest(),
length: 4
} as FTruncateFileSyncOptions)
if (this.logAble) {
this.log += 'ftruncateFileSyncTest success:' + '\n\n'
}
this.done = true
this.ftruncateRet=''
} catch (e) {
if (this.logAble) {
this.log += 'ftruncateFileSyncTest fail:' + JSON.stringify(e) + '\n\n'
}
this.done = true
}
}
},
}
</script>
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册