common.js 8.7 KB
Newer Older
W
weiyufeng 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
/*
 * 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 mediaLibrary from '@ohos.multimedia.mediaLibrary';
import abilityAccessCtrl from '@ohos.abilityAccessCtrl';
import bundle from '@ohos.bundle';

const presetsCount = {
C
caochuan 已提交
20 21 22 23 24 25 26 27 28 29
    ActsMediaLibraryAlbum: { albumsCount: 7, assetsCount: 19 },
    ActsMediaLibraryFavorite: { albumsCount: 6, assetsCount: 32 },
    ActsMediaLibraryAlbumFileResultCb: { albumsCount: 5, assetsCount: 118 },
    ActsMediaLibraryFile: { albumsCount: 6, assetsCount: 21 },
    ActsMediaLibraryFileAsset: { albumsCount: 27, assetsCount: 72 },
    ActsMediaLibraryFileAssetUri: { albumsCount: 3, assetsCount: 6 },
    ActsMediaLibraryFileKey: { albumsCount: 2, assetsCount: 2 },
    ActsMediaLibraryFileResult: { albumsCount: 4, assetsCount: 13 },
    ActsMediaLibraryGetThumbnail: { albumsCount: 3, assetsCount: 3 },
    ActsMediaLibraryBase: { albumsCount: 11, assetsCount: 11 },
W
weiyufeng 已提交
30 31 32 33 34 35 36 37 38 39
}

const IMAGE_TYPE = mediaLibrary.MediaType.IMAGE;
const VIDEO_TYPE = mediaLibrary.MediaType.VIDEO;
const AUDIO_TYPE = mediaLibrary.MediaType.AUDIO;
const FILE_TYPE = mediaLibrary.MediaType.FILE;

const FILEKEY = mediaLibrary.FileKey;
const { RELATIVE_PATH, ALBUM_NAME, MEDIA_TYPE } = FILEKEY

C
caochuan 已提交
40 41 42 43 44 45 46 47 48 49 50
const sleep = async function sleep(times) {
    if (!times) {
        times = 10;
    }
    await new Promise((res) => setTimeout(res, times));
};

const allFetchOp = function (others) {
    if (!others) {
        others = {};
    }
W
weiyufeng 已提交
51 52 53 54 55 56 57
    return {
        selections: '',
        selectionArgs: [],
        ...others
    };
}

C
caochuan 已提交
58 59 60 61
const fetchOps = function (testNum, path, type, others) {
    if (!others) {
        others = {};
    }
C
cc 已提交
62
    let ops = {
W
weiyufeng 已提交
63 64 65 66
        selections: FILEKEY.RELATIVE_PATH + '= ? AND ' + FILEKEY.MEDIA_TYPE + '=?',
        selectionArgs: [path, type.toString()],
        ...others
    };
C
cc 已提交
67 68
    console.info(`${testNum}: fetchOps${JSON.stringify(ops)}`)
    return ops
W
weiyufeng 已提交
69
}
C
cc 已提交
70 71
const nameFetchOps = function (testNum, path, title, type) {
    let ops = {
W
weiyufeng 已提交
72 73 74
        selections: FILEKEY.RELATIVE_PATH + '= ? AND ' + FILEKEY.TITLE + '= ? AND ' + FILEKEY.MEDIA_TYPE + '=?',
        selectionArgs: [path, title, type.toString()],
    };
C
cc 已提交
75 76
    console.info(`${testNum}: fetchOps${JSON.stringify(ops)}`)
    return ops
W
weiyufeng 已提交
77 78
}

C
cc 已提交
79 80
const idFetchOps = function (testNum, albumId) {
    let ops = {
W
weiyufeng 已提交
81 82 83
        selections: FILEKEY.ALBUM_ID + '= ?',
        selectionArgs: [albumId + ''],
    };
C
cc 已提交
84 85
    console.info(`${testNum}: fetchOps${JSON.stringify(ops)}`)
    return ops
W
weiyufeng 已提交
86 87
}

C
caochuan 已提交
88 89 90 91 92 93 94 95 96
const fileIdFetchOps = function (testNum, id) {
    let ops = {
        selections: FILEKEY.ID + "= ?",
        selectionArgs: [id + ""],
    };
    console.info(`${testNum}: fetchOps${JSON.stringify(ops)}`);
    return ops;
};

C
caochuan 已提交
97 98 99 100
const albumFetchOps = function (testNum, path, albumName, type, others) {
    if (!others) {
        others = { order: FILEKEY.DATE_ADDED + " DESC", };
    }
C
cc 已提交
101
    let ops = {
W
weiyufeng 已提交
102 103 104 105
        selections: RELATIVE_PATH + '= ? AND ' + ALBUM_NAME + '= ? AND ' + MEDIA_TYPE + '= ?',
        selectionArgs: [path, albumName, type.toString()],
        ...others
    };
C
cc 已提交
106 107
    console.info(`${testNum}: fetchOps${JSON.stringify(ops)}`)
    return ops
W
weiyufeng 已提交
108 109 110
}

// albums of two resource types
C
caochuan 已提交
111 112 113 114
const albumTwoTypesFetchOps = function (testNum, paths, albumName, types, others) {
    if (!others) {
        others = { order: FILEKEY.DATE_ADDED + " DESC" };
    }
W
weiyufeng 已提交
115 116 117 118 119 120 121 122 123 124
    try {
        let ops = {
            selections: '(' + RELATIVE_PATH + '= ? or ' +
                RELATIVE_PATH + '= ? ) AND ' +
                ALBUM_NAME + '= ? AND (' +
                MEDIA_TYPE + '= ? or ' +
                MEDIA_TYPE + '= ?)',
            selectionArgs: [paths[0], paths[1], albumName, types[0].toString(), types[1].toString()],
            ...others
        };
C
cc 已提交
125
        console.info(`${testNum}: fetchOps${JSON.stringify(ops)}`)
W
weiyufeng 已提交
126 127 128 129 130 131 132
        return ops
    } catch (error) {
        console.info(`albumTwoTypesFetchOps :: error: ${error}`);
    }
}

// albums of three resource types
C
caochuan 已提交
133 134 135 136
const albumThreeTypesFetchOps = function (testNum, paths, albumName, types, others) {
    if (!others) {
        others = { order: FILEKEY.DATE_ADDED + " DESC" };
    }
W
weiyufeng 已提交
137 138 139 140 141 142 143 144 145 146 147 148 149
    try {
        let ops = {
            selections: '(' + RELATIVE_PATH + '= ? or ' +
                RELATIVE_PATH + '= ? or ' +
                RELATIVE_PATH + '= ? ) AND ' +
                ALBUM_NAME + '= ? AND (' +
                MEDIA_TYPE + '= ? or ' +
                MEDIA_TYPE + '= ? or ' +
                MEDIA_TYPE + '= ?)',
            selectionArgs: [paths[0], paths[1], paths[2], albumName,
            types[0].toString(), types[1].toString(), types[2].toString()],
            ...others
        };
C
cc 已提交
150
        console.info(`${testNum}: fetchOps${JSON.stringify(ops)}`)
W
weiyufeng 已提交
151 152 153 154 155 156 157 158 159 160 161 162
        return ops
    } catch (error) {
        console.info(`albumThreeTypesFetchOps :: error: ${error}`);
    }
}

const checkPresetsAssets = async function (media, hapName) {
    console.info('checkPresetsAssets start')
    let albumList = await media.getAlbums(allFetchOp());
    let albumsCount = albumList.length;
    let fetchFileResult = await media.getFileAssets(allFetchOp());
    let assetsCount = await fetchFileResult.getCount();
C
cc 已提交
163
    console.info(`${hapName}:: assetsCount: ${assetsCount} albumsCount: ${albumsCount},
C
caochuan 已提交
164 165
            presetsassetsCount: ${presetsCount[hapName].assetsCount} 
            presetsalbumsCount: ${presetsCount[hapName].albumsCount}`);
W
weiyufeng 已提交
166 167 168 169 170 171 172 173 174 175 176 177
    console.info('checkPresetsAssets end')
}

const checkAssetsCount = async function (done, testNum, fetchFileResult, expectCount) {
    if (!fetchFileResult) {
        console.info(`${testNum}:: fetchFileResult error:`);
        expect(false).assertTrue();
        done();
        return false
    }
    let count = await fetchFileResult.getCount();
    if (count != expectCount) {
C
cc 已提交
178
        console.info(`${testNum}:: count:expectCount - ${count} : ${expectCount}`);
W
weiyufeng 已提交
179 180 181 182 183 184 185 186 187 188 189 190 191 192 193
        expect(count).assertEqual(expectCount);
        done();
    }
    return count == expectCount;
}

const checkAlbumsCount = function (done, testNum, albumList, expectCount) {
    if (!Array.isArray(albumList)) {
        console.info(`${testNum}:: albumList error:`);
        expect(false).assertTrue();
        done();
        return false
    }
    let albumsCount = albumList.length;
    if (albumsCount != expectCount) {
C
cc 已提交
194
        console.info(`${testNum}:: albumsCount: expectCount - ${albumsCount} : ${expectCount}`);
W
weiyufeng 已提交
195 196 197 198 199 200
        expect(albumsCount).assertEqual(expectCount);
        done();
    }
    return albumsCount == expectCount;
}

C
caochuan 已提交
201 202 203 204
const getPermission = async function (name) {
    if (!name) {
        name = "ohos.acts.multimedia.mediaLibrary";
    }
W
weiyufeng 已提交
205
    console.info('getPermission start', name)
C
caochuan 已提交
206
    let appInfo = await bundle.getApplicationInfo(name, 0, 100);
W
weiyufeng 已提交
207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245
    let tokenID = appInfo.accessTokenId;
    let atManager = abilityAccessCtrl.createAtManager();
    let result1 = await atManager.grantUserGrantedPermission(tokenID, "ohos.permission.MEDIA_LOCATION", 1);
    let result2 = await atManager.grantUserGrantedPermission(tokenID, "ohos.permission.READ_MEDIA", 1);
    let result3 = await atManager.grantUserGrantedPermission(tokenID, "ohos.permission.WRITE_MEDIA", 1);
    let isGranted1 = await atManager.verifyAccessToken(tokenID, "ohos.permission.MEDIA_LOCATION");
    let isGranted2 = await atManager.verifyAccessToken(tokenID, "ohos.permission.READ_MEDIA");
    let isGranted3 = await atManager.verifyAccessToken(tokenID, "ohos.permission.WRITE_MEDIA");
    if (!(result1 == 0 && result2 == 0 && result3 == 0) || !(isGranted1 == 0 && isGranted2 == 0 && isGranted3 == 0)) {
        console.info('getPermission failed')
    }
    console.info('getPermission end')
}

const MODIFY_ERROR_CODE_01 = '-1000';

const isNum = function (value) {
    return typeof value === 'number' && !isNaN(value);
}
export {
    getPermission,
    IMAGE_TYPE,
    VIDEO_TYPE,
    AUDIO_TYPE,
    FILE_TYPE,
    FILEKEY,
    sleep,
    allFetchOp,
    fetchOps,
    nameFetchOps,
    idFetchOps,
    albumFetchOps,
    albumTwoTypesFetchOps,
    albumThreeTypesFetchOps,
    checkPresetsAssets,
    checkAssetsCount,
    checkAlbumsCount,
    MODIFY_ERROR_CODE_01,
    isNum,
C
caochuan 已提交
246 247
    fileIdFetchOps,
};