common.js 7.7 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 21

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

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

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

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

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

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

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

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

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

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

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