common.js 7.8 KB
Newer Older
Y
yangbo 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
/*
 * Copyright (C) 2022 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import userfile_manager from '@ohos.filemanagement.userfile_manager';
import abilityAccessCtrl from '@ohos.abilityAccessCtrl';
import bundle from '@ohos.bundle';

const presetsCount = {
L
lwx1121892 已提交
21
    ActsUserFileMgrAlbumJsTest: { albumsCount: 9, assetsCount: 9 },
Y
yangbo 已提交
22 23
    ActsUserFileMgrBaseJsTest: { albumsCount: 12, assetsCount: 24 },
    ActsUserFileMgrFileAssetJsTest: { albumsCount: 45, assetsCount: 116 },
Y
yangbo 已提交
24 25 26 27 28
}

const IMAGE_TYPE = userfile_manager.MediaType.IMAGE;
const VIDEO_TYPE = userfile_manager.MediaType.VIDEO;
const AUDIO_TYPE = userfile_manager.MediaType.AUDIO;
Y
yangbo 已提交
29
const FILE_TYPE = userfile_manager.MediaType.FILE;
Y
yangbo 已提交
30 31

const FILEKEY = userfile_manager.FileKey;
Y
yangbo 已提交
32 33 34
const AUDIOKEY = userfile_manager.AudioKey;
const IMAGEVIDEOKEY = userfile_manager.ImageVideoKey;
const ALBUMKEY = userfile_manager.AlbumKey;
Y
yangbo 已提交
35 36 37 38 39 40 41 42 43 44 45 46 47 48
const sleep = async function sleep(times) {
    if (times == undefined) {
        times = 10
    }
    await new Promise(res => setTimeout(res, times));
}

const allFetchOp = function () {
    return {
        selections: '',
        selectionArgs: [],
    };
}

Y
yangbo 已提交
49
const fileFetchOps = function (testNum, path) {
Y
yangbo 已提交
50
    let ops = {
Y
yangbo 已提交
51
        selections: FILEKEY.RELATIVE_PATH + '= ?',
Y
yangbo 已提交
52 53
        selectionArgs: [path],
    };
Y
yangbo 已提交
54
    console.info(`${testNum}: fileFetchOps${JSON.stringify(ops)}`)
Y
yangbo 已提交
55 56
    return ops
}
Y
yangbo 已提交
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94

const audioFetchOps = function (testNum, path) {
    let ops = {
        selections: AUDIOKEY.RELATIVE_PATH + '= ?',
        selectionArgs: [path],
    };
    console.info(`${testNum}: audioFetchOps${JSON.stringify(ops)}`)
    return ops
}

const imageVideoFetchOps = function (testNum, path) {
    let ops = {
        selections: IMAGEVIDEOKEY.RELATIVE_PATH + '= ?',
        selectionArgs: [path],
    };
    console.info(`${testNum}: imageVideoFetchOps${JSON.stringify(ops)}`)
    return ops
}

const fileNameFetchOps = function (testNum, path, displayName) {
    let ops = {
        selections: FILEKEY.RELATIVE_PATH + '= ? AND ' + FILEKEY.DISPLAY_NAME + '= ?',
        selectionArgs: [path, displayName],
    };
    console.info(`${testNum}: fileNameFetchOps${JSON.stringify(ops)}`)
    return ops
}

const audioNameFetchOps = function (testNum, path, displayName) {
    let ops = {
        selections: AUDIOKEY.RELATIVE_PATH + '= ? AND ' + AUDIOKEY.DISPLAY_NAME + '= ?',
        selectionArgs: [path, displayName],
    };
    console.info(`${testNum}: audioNameFetchOps${JSON.stringify(ops)}`)
    return ops
}

const imageVideoNameFetchOps = function (testNum, path, displayName) {
Y
yangbo 已提交
95
    let ops = {
Y
yangbo 已提交
96
        selections: IMAGEVIDEOKEY.RELATIVE_PATH + '= ? AND ' + IMAGEVIDEOKEY.DISPLAY_NAME + '= ?',
Y
yangbo 已提交
97 98
        selectionArgs: [path, displayName],
    };
Y
yangbo 已提交
99
    console.info(`${testNum}: imageVideoNameFetchOps${JSON.stringify(ops)}`)
Y
yangbo 已提交
100 101 102 103 104
    return ops
}

const albumFetchOps = function (testNum, path, albumName) {
    let ops = {
Y
yangbo 已提交
105
        selections: ALBUMKEY.RELATIVE_PATH + '= ? AND ' + 'bucket_display_name' + '= ?',
Y
yangbo 已提交
106 107
        selectionArgs: [path, albumName],
    };
Y
yangbo 已提交
108
    console.info(`${testNum}: albumFetchOps${JSON.stringify(ops)}`)
Y
yangbo 已提交
109 110 111 112 113
    return ops
}

const checkPresetsAssets = async function (userfilemgr, hapName) {
    console.info('checkPresetsAssets start')
Y
yangbo 已提交
114 115
    let fetchAlbumResult = await userfilemgr.getAlbums([IMAGE_TYPE, VIDEO_TYPE, AUDIO_TYPE], allFetchOp());
    let albumsCount = fetchAlbumResult.getCount();
Y
yangbo 已提交
116 117 118 119 120 121 122 123 124 125 126
    let fetchFileResult = await userfilemgr.getFileAssets([IMAGE_TYPE, VIDEO_TYPE, AUDIO_TYPE],
        allFetchOp());
    let assetsCount = await fetchFileResult.getCount();
    console.info(`${hapName}:: assetsCount: ${assetsCount} albumsCount: ${albumsCount},
            presetsassetsCount: ${presetsCount[hapName].assetsCount} 
            presetsalbumsCount: ${presetsCount[hapName].albumsCount}`);
    console.info('checkPresetsAssets end')
}

const checkAssetsCount = async function (done, testNum, fetchFileResult, expectCount) {
    if (!fetchFileResult) {
Y
yangbo 已提交
127
        console.info(`${testNum}:: fetchFileResult is undefined`);
Y
yangbo 已提交
128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176
        expect(false).assertTrue();
        done();
        return false
    }
    let count = await fetchFileResult.getCount();
    if (count != expectCount) {
        console.info(`${testNum}:: count:expectCount - ${count} : ${expectCount}`);
        expect(count).assertEqual(expectCount);
        done();
    }
    return count == expectCount;
}

const getPermission = async function (name = 'ohos.acts.multimedia.userfilemgr') {
    console.info('getPermission start', name)
    let appInfo = await bundle.getApplicationInfo('ohos.acts.multimedia.userfilemgr', 0, 100);
    let tokenID = appInfo.accessTokenId;
    let atManager = abilityAccessCtrl.createAtManager();
    let result1 = await atManager.grantUserGrantedPermission(tokenID, "ohos.permission.MEDIA_LOCATION", 1);
    let resultReadImageVideo = await atManager.grantUserGrantedPermission(tokenID, "ohos.permission.READ_IMAGEVIDEO", 1);
    let resultReadAudio = await atManager.grantUserGrantedPermission(tokenID, "ohos.permission.READ_AUDIO", 1);
    let resultReadDocument = await atManager.grantUserGrantedPermission(tokenID, "ohos.permission.READ_DOCUMENT", 1);
    let resultWriteImageVideo = await atManager.grantUserGrantedPermission(tokenID, "ohos.permission.WRITE_IMAGEVIDEO", 1);
    let resultWriteAudio = await atManager.grantUserGrantedPermission(tokenID, "ohos.permission.WRITE_AUDIO", 1);
    let resultWriteDocument = await atManager.grantUserGrantedPermission(tokenID, "ohos.permission.WRITE_DOCUMENT", 1);
    let isGranted1 = await atManager.verifyAccessToken(tokenID, "ohos.permission.MEDIA_LOCATION");
    let isGrantedReadImageVideo = await atManager.verifyAccessToken(tokenID, "ohos.permission.READ_IMAGEVIDEO");
    let isGrantedReadAudio = await atManager.verifyAccessToken(tokenID, "ohos.permission.READ_AUDIO");
    let isGrantedReadDocument = await atManager.verifyAccessToken(tokenID, "ohos.permission.READ_DOCUMENT");
    let isGrantedWriteImageVideo = await atManager.verifyAccessToken(tokenID, "ohos.permission.WRITE_IMAGEVIDEO");
    let isGrantedWriteAudio = await atManager.verifyAccessToken(tokenID, "ohos.permission.WRITE_AUDIO");
    let isGrantedWriteDocument = await atManager.verifyAccessToken(tokenID, "ohos.permission.WRITE_DOCUMENT");
    if (result1 != 0 || isGranted1 !=0 || !(resultReadImageVideo == 0 && resultReadAudio == 0 && resultReadDocument == 0) ||
        !(resultWriteImageVideo == 0 && resultWriteAudio == 0 && resultWriteDocument == 0) ||
        !(isGrantedReadImageVideo == 0 && isGrantedReadAudio == 0 && isGrantedReadDocument == 0) ||
        !(isGrantedWriteImageVideo == 0 && isGrantedWriteAudio == 0 && isGrantedWriteDocument == 0)) {
        console.info('getPermission failed')
    }
    console.info('getPermission end')
}

const isNum = function (value) {
    return typeof value === 'number' && !isNaN(value);
}
export {
    getPermission,
    IMAGE_TYPE,
    VIDEO_TYPE,
    AUDIO_TYPE,
Y
yangbo 已提交
177
    FILE_TYPE,
Y
yangbo 已提交
178 179 180
    FILEKEY,
    sleep,
    allFetchOp,
Y
yangbo 已提交
181 182 183 184 185 186
    fileFetchOps,
    audioFetchOps,
    imageVideoFetchOps,
    fileNameFetchOps,
    audioNameFetchOps,
    imageVideoNameFetchOps,
Y
yangbo 已提交
187 188 189 190 191
    albumFetchOps,
    checkPresetsAssets,
    checkAssetsCount,
    isNum,
};