common.js 10.0 KB
Newer Older
W
weiyufeng 已提交
1
/*
Y
update  
yangbo 已提交
2
 * Copyright (C) 2022-2023 Huawei Device Co., Ltd.
W
weiyufeng 已提交
3 4 5 6 7 8 9 10 11 12 13 14
 * 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.
 */
C
caochuan 已提交
15 16 17
import mediaLibrary from "@ohos.multimedia.mediaLibrary";
import abilityAccessCtrl from "@ohos.abilityAccessCtrl";
import bundle from "@ohos.bundle";
18
import uitest from "@ohos.UiTest";
W
weiyufeng 已提交
19
const presetsCount = {
20
    ActsMediaLibraryAlbumTest: { albumsCount: 15, assetsCount: 27 },
21
    ActsMediaLibraryBaseTest: { albumsCount: 11, assetsCount: 14 },
C
caochuan 已提交
22
    ActsMediaLibraryFavoriteTest: { albumsCount: 6, assetsCount: 32 },
23
    ActsMediaLibraryFileTest: { albumsCount: 6, assetsCount: 28 },
24
    ActsMediaLibraryFileAssetTest: { albumsCount: 27, assetsCount: 116 },
C
caochuan 已提交
25
    ActsMediaLibraryFileKeyTest: { albumsCount: 2, assetsCount: 2 },
26 27 28 29
    ActsMediaLibraryFileResultTest: { albumsCount: 3, assetsCount: 112 },
    ActsMediaLibraryGetThumbnailTest: { albumsCount: 3, assetsCount: 3 },
    ActsMediaLibraryMediafetchoptionsTest: { albumsCount: 3, assetsCount: 8 },
    ActsMediaLibraryTrashJsTest: { albumsCount: 6, assetsCount: 24 },
C
caochuan 已提交
30
};
W
weiyufeng 已提交
31 32 33 34 35 36 37

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;
C
caochuan 已提交
38
const { RELATIVE_PATH, ALBUM_NAME, MEDIA_TYPE } = FILEKEY;
W
weiyufeng 已提交
39

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
    return {
C
caochuan 已提交
52
        selections: "",
W
weiyufeng 已提交
53
        selectionArgs: [],
C
caochuan 已提交
54
        ...others,
W
weiyufeng 已提交
55
    };
C
caochuan 已提交
56
};
W
weiyufeng 已提交
57

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

C
cc 已提交
79 80
const idFetchOps = function (testNum, albumId) {
    let ops = {
C
caochuan 已提交
81 82
        selections: FILEKEY.ALBUM_ID + "= ?",
        selectionArgs: [albumId + ""],
W
weiyufeng 已提交
83
    };
C
caochuan 已提交
84 85 86
    console.info(`${testNum}: fetchOps${JSON.stringify(ops)}`);
    return ops;
};
W
weiyufeng 已提交
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;
};

Y
yangbo 已提交
97
const albumFetchOps = function (testNum, albumName, others) {
C
cc 已提交
98
    let ops = {
Y
yangbo 已提交
99 100
        selections: ALBUM_NAME + "= ?",
        selectionArgs: [albumName],
C
caochuan 已提交
101
        ...others,
W
weiyufeng 已提交
102
    };
C
caochuan 已提交
103 104 105
    console.info(`${testNum}: fetchOps${JSON.stringify(ops)}`);
    return ops;
};
W
weiyufeng 已提交
106 107

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

// albums of three resource types
C
caochuan 已提交
137 138 139 140
const albumThreeTypesFetchOps = function (testNum, paths, albumName, types, others) {
    if (!others) {
        others = { order: FILEKEY.DATE_ADDED + " DESC" };
    }
W
weiyufeng 已提交
141 142
    try {
        let ops = {
C
caochuan 已提交
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
            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,
W
weiyufeng 已提交
169
        };
Y
yangbo 已提交
170
        console.info(`${testNum}: fetchOps ${JSON.stringify(ops)}`);
C
caochuan 已提交
171
        return ops;
W
weiyufeng 已提交
172 173 174
    } catch (error) {
        console.info(`albumThreeTypesFetchOps :: error: ${error}`);
    }
C
caochuan 已提交
175
};
W
weiyufeng 已提交
176

Y
yangbo 已提交
177 178 179 180 181 182 183 184 185
const fileFetchOption = function (testNum, selections, selectionArgs) {
    let ops = {
        selections: selections,
        selectionArgs: selectionArgs,
    };
    console.info(`${testNum} fetchOps: ${JSON.stringify(ops)}`);
    return ops;
}

W
weiyufeng 已提交
186
const checkPresetsAssets = async function (media, hapName) {
C
caochuan 已提交
187
    console.info("checkPresetsAssets start");
W
weiyufeng 已提交
188 189 190 191
    let albumList = await media.getAlbums(allFetchOp());
    let albumsCount = albumList.length;
    let fetchFileResult = await media.getFileAssets(allFetchOp());
    let assetsCount = await fetchFileResult.getCount();
192 193
    let presetsassetsCount = presetsCount[hapName].assetsCount;
    let presetsalbumsCount = presetsCount[hapName].albumsCount;
C
caochuan 已提交
194
    if (assetsCount != presetsCount[hapName].assetsCount || albumsCount != presetsCount[hapName].albumsCount) {
195 196
        console.info(`${hapName} checkPresetsAssets failed; 
            assetsCount : presetsassetsCount = ${assetsCount} : ${presetsassetsCount}
C
caochuan 已提交
197
            albumsCount : presetsalbumsCount = ${albumsCount} : ${presetsalbumsCount}`);
梁家熙 已提交
198
        fetchFileResult.close();
199
    } else {
C
caochuan 已提交
200
        console.info(`${hapName} checkPresetsAssets passed`);
梁家熙 已提交
201
        fetchFileResult.close();
202
    }
C
caochuan 已提交
203
};
W
weiyufeng 已提交
204 205 206 207 208 209

const checkAssetsCount = async function (done, testNum, fetchFileResult, expectCount) {
    if (!fetchFileResult) {
        console.info(`${testNum}:: fetchFileResult error:`);
        expect(false).assertTrue();
        done();
C
caochuan 已提交
210
        return false;
W
weiyufeng 已提交
211 212 213
    }
    let count = await fetchFileResult.getCount();
    if (count != expectCount) {
C
cc 已提交
214
        console.info(`${testNum}:: count:expectCount - ${count} : ${expectCount}`);
W
weiyufeng 已提交
215 216 217 218
        expect(count).assertEqual(expectCount);
        done();
    }
    return count == expectCount;
C
caochuan 已提交
219
};
W
weiyufeng 已提交
220 221 222 223 224 225

const checkAlbumsCount = function (done, testNum, albumList, expectCount) {
    if (!Array.isArray(albumList)) {
        console.info(`${testNum}:: albumList error:`);
        expect(false).assertTrue();
        done();
C
caochuan 已提交
226
        return false;
W
weiyufeng 已提交
227 228 229
    }
    let albumsCount = albumList.length;
    if (albumsCount != expectCount) {
C
cc 已提交
230
        console.info(`${testNum}:: albumsCount: expectCount - ${albumsCount} : ${expectCount}`);
W
weiyufeng 已提交
231 232 233 234
        expect(albumsCount).assertEqual(expectCount);
        done();
    }
    return albumsCount == expectCount;
C
caochuan 已提交
235
};
W
weiyufeng 已提交
236

C
caochuan 已提交
237
const getPermission = async function (name, context) {
C
caochuan 已提交
238 239 240
    if (!name) {
        name = "ohos.acts.multimedia.mediaLibrary";
    }
C
caochuan 已提交
241 242 243 244
    console.info("getPermission start", name);

    let permissions = ["ohos.permission.MEDIA_LOCATION", "ohos.permission.READ_MEDIA", "ohos.permission.WRITE_MEDIA"];

245 246 247 248 249 250 251 252 253
    let atManager = abilityAccessCtrl.createAtManager();
    try {
        atManager.requestPermissionsFromUser(context, permissions, (err, data) => {
            console.info(`getPermission requestPermissionsFromUser ${JSON.stringify(data)}`);
        });
    } catch (err) {
        console.log(`get permission catch err -> ${JSON.stringify(err)}`);
    }
    await sleep(1000);
C
caochuan 已提交
254
    let driver = uitest.Driver.create();
255 256
    
    await sleep(2000);
C
caochuan 已提交
257 258
    let button = await driver.findComponent(uitest.ON.text("允许"));
    await button.click();
259
    await sleep(2000);
C
caochuan 已提交
260

C
caochuan 已提交
261
    let appInfo = await bundle.getApplicationInfo(name, 0, 100);
W
weiyufeng 已提交
262
    let tokenID = appInfo.accessTokenId;
263
    
W
weiyufeng 已提交
264 265 266
    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");
Y
yangbo 已提交
267
    if (!(isGranted1 == 0 && isGranted2 == 0 && isGranted3 == 0)) {
C
caochuan 已提交
268
        console.info("getPermission failed");
W
weiyufeng 已提交
269
    }
C
caochuan 已提交
270 271
    console.info("getPermission end");
};
W
weiyufeng 已提交
272

C
caochuan 已提交
273
const MODIFY_ERROR_CODE_01 = "-1000";
W
weiyufeng 已提交
274 275

const isNum = function (value) {
C
caochuan 已提交
276 277
    return typeof value === "number" && !isNaN(value);
};
W
weiyufeng 已提交
278 279 280 281 282 283 284 285 286 287 288 289 290
export {
    getPermission,
    IMAGE_TYPE,
    VIDEO_TYPE,
    AUDIO_TYPE,
    FILE_TYPE,
    FILEKEY,
    sleep,
    allFetchOp,
    fetchOps,
    nameFetchOps,
    idFetchOps,
    albumFetchOps,
Y
yangbo 已提交
291
    fileFetchOption,
W
weiyufeng 已提交
292 293 294 295 296 297 298
    albumTwoTypesFetchOps,
    albumThreeTypesFetchOps,
    checkPresetsAssets,
    checkAssetsCount,
    checkAlbumsCount,
    MODIFY_ERROR_CODE_01,
    isNum,
C
caochuan 已提交
299 300
    fileIdFetchOps,
};