common.js 7.9 KB
Newer Older
Y
yangbo 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
/*
 * 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.
 */

Y
yangbo 已提交
16
import userFileManager from '@ohos.filemanagement.userFileManager';
Y
yangbo 已提交
17 18
import abilityAccessCtrl from '@ohos.abilityAccessCtrl';
import bundle from '@ohos.bundle';
Y
yangbo 已提交
19
import dataSharePredicates from '@ohos.data.dataSharePredicates';
Y
yangbo 已提交
20
import mediaLibrary from '@ohos.multimedia.mediaLibrary';
Y
yangbo 已提交
21 22

const presetsCount = {
Y
yangbo 已提交
23 24 25
    ActsUserFileMgrAlbumJsTest: { albumsCount: 3, assetsCount: 3 },
    ActsUserFileMgrBaseJsTest: { albumsCount: 9, assetsCount: 18 },
    ActsUserFileMgrFileAssetJsTest: { albumsCount: 45, assetsCount: 87 },
Y
yangbo 已提交
26 27
}

Y
yangbo 已提交
28 29 30
const IMAGE_TYPE = userFileManager.FileType.IMAGE;
const VIDEO_TYPE = userFileManager.FileType.VIDEO;
const AUDIO_TYPE = userFileManager.FileType.AUDIO;
Y
yangbo 已提交
31

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

const allFetchOp = function () {
Y
yangbo 已提交
44
    let predicates = new dataSharePredicates.DataSharePredicates();
Y
yangbo 已提交
45
    return {
Y
yangbo 已提交
46
        fetchColumns: [],
Y
yangbo 已提交
47
        predicates: predicates
Y
yangbo 已提交
48 49 50
    };
}

Y
yangbo 已提交
51
const audioFetchOps = function (testNum, path) {
Y
yangbo 已提交
52
    let predicates = new dataSharePredicates.DataSharePredicates();
Y
yangbo 已提交
53
    predicates.equalTo(FILEKEY.RELATIVE_PATH, path);
Y
yangbo 已提交
54
    let ops = {
Y
yangbo 已提交
55
        fetchColumns: [],
Y
yangbo 已提交
56
        predicates: predicates
Y
yangbo 已提交
57
    };
Y
yangbo 已提交
58
    console.info(`${testNum} queryOps: ${FILEKEY.RELATIVE_PATH} = ${path}`);
Y
yangbo 已提交
59 60 61 62
    return ops
}

const imageVideoFetchOps = function (testNum, path) {
Y
yangbo 已提交
63
    let predicates = new dataSharePredicates.DataSharePredicates();
Y
yangbo 已提交
64
    predicates.equalTo(FILEKEY.RELATIVE_PATH, path);
Y
yangbo 已提交
65
    let ops = {
Y
yangbo 已提交
66
        fetchColumns: [],
Y
yangbo 已提交
67
        predicates: predicates
Y
yangbo 已提交
68
    };
Y
yangbo 已提交
69
    console.info(`${testNum} queryOps: ${FILEKEY.RELATIVE_PATH} = ${path}`);
Y
yangbo 已提交
70 71 72 73
    return ops
}

const audioNameFetchOps = function (testNum, path, displayName) {
Y
yangbo 已提交
74
    let predicates = new dataSharePredicates.DataSharePredicates();
Y
yangbo 已提交
75
    predicates.equalTo(FILEKEY.RELATIVE_PATH, path)
Y
yangbo 已提交
76
        .equalTo(AUDIOKEY.DISPLAY_NAME, displayName);
Y
yangbo 已提交
77
    let ops = {
Y
yangbo 已提交
78
        fetchColumns: [],
Y
yangbo 已提交
79
        predicates: predicates
Y
yangbo 已提交
80
    };
Y
yangbo 已提交
81
    console.info(`${testNum} queryOps: ${FILEKEY.RELATIVE_PATH} = ${path} AND display_name = ${displayName}`);
Y
yangbo 已提交
82 83 84 85
    return ops
}

const imageVideoNameFetchOps = function (testNum, path, displayName) {
Y
yangbo 已提交
86
    let predicates = new dataSharePredicates.DataSharePredicates();
Y
yangbo 已提交
87
    predicates.equalTo(FILEKEY.RELATIVE_PATH, path)
Y
yangbo 已提交
88
        .equalTo(IMAGEVIDEOKEY.DISPLAY_NAME, displayName);
Y
yangbo 已提交
89
    let ops = {
Y
yangbo 已提交
90
        fetchColumns: [],
Y
yangbo 已提交
91
        predicates: predicates
Y
yangbo 已提交
92
    };
Y
yangbo 已提交
93
    console.info(`${testNum} queryOps: ${FILEKEY.RELATIVE_PATH} = ${path} AND display_name = ${displayName}`);
Y
yangbo 已提交
94 95 96 97
    return ops
}

const albumFetchOps = function (testNum, path, albumName) {
Y
yangbo 已提交
98
    let predicates = new dataSharePredicates.DataSharePredicates();
Y
yangbo 已提交
99
    predicates.equalTo(FILEKEY.RELATIVE_PATH, path)
Y
yangbo 已提交
100
        .equalTo("bucket_display_name", albumName);
Y
yangbo 已提交
101
    let ops = {
Y
yangbo 已提交
102
        predicates: predicates
Y
yangbo 已提交
103
    };
Y
yangbo 已提交
104
    console.info(`${testNum} queryOps: ${FILEKEY.RELATIVE_PATH} = ${path} AND bucket_display_name = ${albumName}`);
Y
yangbo 已提交
105 106 107 108 109
    return ops
}

const checkPresetsAssets = async function (userfilemgr, hapName) {
    console.info('checkPresetsAssets start')
Y
yangbo 已提交
110
    let fetchAlbumResult = await userfilemgr.getPhotoAlbums(allFetchOp());
Y
yangbo 已提交
111
    let albumsCount = fetchAlbumResult.getCount();
Y
yangbo 已提交
112 113 114
    let fetchPhotoResult = await userfilemgr.getPhotoAssets(allFetchOp());
    let fetchAudioResult = await userfilemgr.getAudioAssets(allFetchOp());
    let assetsCount = fetchPhotoResult.getCount() + fetchAudioResult.getCount();
Y
yangbo 已提交
115 116 117 118 119 120
    console.info(`${hapName}:: assetsCount: ${assetsCount} albumsCount: ${albumsCount},
            presetsassetsCount: ${presetsCount[hapName].assetsCount} 
            presetsalbumsCount: ${presetsCount[hapName].albumsCount}`);
    console.info('checkPresetsAssets end')
}

Y
yangbo 已提交
121 122 123
const checkAssetsCount = async function (done, testNum, fetchAssetResult, expectCount) {
    if (!fetchAssetResult) {
        console.info(`${testNum}:: fetchAssetResult is undefined`);
Y
yangbo 已提交
124 125 126 127
        expect(false).assertTrue();
        done();
        return false
    }
Y
yangbo 已提交
128
    let count = await fetchAssetResult.getCount();
Y
yangbo 已提交
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
    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,
    sleep,
    allFetchOp,
Y
yangbo 已提交
175 176 177 178
    audioFetchOps,
    imageVideoFetchOps,
    audioNameFetchOps,
    imageVideoNameFetchOps,
Y
yangbo 已提交
179 180 181 182 183
    albumFetchOps,
    checkPresetsAssets,
    checkAssetsCount,
    isNum,
};