• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1/*
2 * Copyright (C) 2022 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 *     http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15import mediaLibrary from "@ohos.multimedia.mediaLibrary";
16import abilityAccessCtrl from "@ohos.abilityAccessCtrl";
17import bundle from "@ohos.bundle";
18import uitest from "@ohos.UiTest";
19const presetsCount = {
20    ActsMediaLibraryAlbumTest: { albumsCount: 15, assetsCount: 27 },
21    ActsMediaLibraryBaseTest: { albumsCount: 11, assetsCount: 14 },
22    ActsMediaLibraryFavoriteTest: { albumsCount: 6, assetsCount: 32 },
23    ActsMediaLibraryFileTest: { albumsCount: 6, assetsCount: 28 },
24    ActsMediaLibraryFileAssetTest: { albumsCount: 27, assetsCount: 116 },
25    ActsMediaLibraryFileKeyTest: { albumsCount: 2, assetsCount: 2 },
26    ActsMediaLibraryFileResultTest: { albumsCount: 3, assetsCount: 112 },
27    ActsMediaLibraryGetThumbnailTest: { albumsCount: 3, assetsCount: 3 },
28    ActsMediaLibraryMediafetchoptionsTest: { albumsCount: 3, assetsCount: 8 },
29    ActsMediaLibraryTrashJsTest: { albumsCount: 6, assetsCount: 24 },
30};
31
32const IMAGE_TYPE = mediaLibrary.MediaType.IMAGE;
33const VIDEO_TYPE = mediaLibrary.MediaType.VIDEO;
34const AUDIO_TYPE = mediaLibrary.MediaType.AUDIO;
35const FILE_TYPE = mediaLibrary.MediaType.FILE;
36
37const FILEKEY = mediaLibrary.FileKey;
38const { RELATIVE_PATH, ALBUM_NAME, MEDIA_TYPE } = FILEKEY;
39
40const sleep = async function sleep(times) {
41    if (!times) {
42        times = 10;
43    }
44    await new Promise((res) => setTimeout(res, times));
45};
46
47const allFetchOp = function (others) {
48    if (!others) {
49        others = {};
50    }
51    return {
52        selections: "",
53        selectionArgs: [],
54        ...others,
55    };
56};
57
58const fetchOps = function (testNum, path, type, others) {
59    if (!others) {
60        others = {};
61    }
62    let ops = {
63        selections: FILEKEY.RELATIVE_PATH + "= ? AND " + FILEKEY.MEDIA_TYPE + "=?",
64        selectionArgs: [path, type.toString()],
65        ...others,
66    };
67    console.info(`${testNum}: fetchOps${JSON.stringify(ops)}`);
68    return ops;
69};
70const nameFetchOps = function (testNum, path, display_name, type) {
71    let ops = {
72        selections: FILEKEY.RELATIVE_PATH + "= ? AND " + FILEKEY.DISPLAY_NAME + "= ? AND " + FILEKEY.MEDIA_TYPE + "=?",
73        selectionArgs: [path, display_name, type.toString()],
74    };
75    console.info(`${testNum}: fetchOps${JSON.stringify(ops)}`);
76    return ops;
77};
78
79const idFetchOps = function (testNum, albumId) {
80    let ops = {
81        selections: FILEKEY.ALBUM_ID + "= ?",
82        selectionArgs: [albumId + ""],
83    };
84    console.info(`${testNum}: fetchOps${JSON.stringify(ops)}`);
85    return ops;
86};
87
88const fileIdFetchOps = function (testNum, id) {
89    let ops = {
90        selections: FILEKEY.ID + "= ?",
91        selectionArgs: [id + ""],
92    };
93    console.info(`${testNum}: fetchOps${JSON.stringify(ops)}`);
94    return ops;
95};
96
97const albumFetchOps = function (testNum, path, albumName, type, others) {
98    if (!others) {
99        others = { order: FILEKEY.DATE_ADDED + " DESC" };
100    }
101    let ops = {
102        selections: RELATIVE_PATH + "= ? AND " + ALBUM_NAME + "= ? AND " + MEDIA_TYPE + "= ?",
103        selectionArgs: [path, albumName, type.toString()],
104        ...others,
105    };
106    console.info(`${testNum}: fetchOps${JSON.stringify(ops)}`);
107    return ops;
108};
109
110// albums of two resource types
111const albumTwoTypesFetchOps = function (testNum, paths, albumName, types, others) {
112    if (!others) {
113        others = { order: FILEKEY.DATE_ADDED + " DESC" };
114    }
115    try {
116        let ops = {
117            selections:
118                "(" +
119                RELATIVE_PATH +
120                "= ? or " +
121                RELATIVE_PATH +
122                "= ? ) AND " +
123                ALBUM_NAME +
124                "= ? AND (" +
125                MEDIA_TYPE +
126                "= ? or " +
127                MEDIA_TYPE +
128                "= ?)",
129            selectionArgs: [paths[0], paths[1], albumName, types[0].toString(), types[1].toString()],
130            ...others,
131        };
132        console.info(`${testNum}: fetchOps${JSON.stringify(ops)}`);
133        return ops;
134    } catch (error) {
135        console.info(`albumTwoTypesFetchOps :: error: ${error}`);
136    }
137};
138
139// albums of three resource types
140const albumThreeTypesFetchOps = function (testNum, paths, albumName, types, others) {
141    if (!others) {
142        others = { order: FILEKEY.DATE_ADDED + " DESC" };
143    }
144    try {
145        let ops = {
146            selections:
147                "(" +
148                RELATIVE_PATH +
149                "= ? or " +
150                RELATIVE_PATH +
151                "= ? or " +
152                RELATIVE_PATH +
153                "= ? ) AND " +
154                ALBUM_NAME +
155                "= ? AND (" +
156                MEDIA_TYPE +
157                "= ? or " +
158                MEDIA_TYPE +
159                "= ? or " +
160                MEDIA_TYPE +
161                "= ?)",
162            selectionArgs: [
163                paths[0],
164                paths[1],
165                paths[2],
166                albumName,
167                types[0].toString(),
168                types[1].toString(),
169                types[2].toString(),
170            ],
171            ...others,
172        };
173        console.info(`${testNum}: fetchOps${JSON.stringify(ops)}`);
174        return ops;
175    } catch (error) {
176        console.info(`albumThreeTypesFetchOps :: error: ${error}`);
177    }
178};
179
180const checkPresetsAssets = async function (media, hapName) {
181    console.info("checkPresetsAssets start");
182    let albumList = await media.getAlbums(allFetchOp());
183    let albumsCount = albumList.length;
184    let fetchFileResult = await media.getFileAssets(allFetchOp());
185    let assetsCount = await fetchFileResult.getCount();
186    let presetsassetsCount = presetsCount[hapName].assetsCount;
187    let presetsalbumsCount = presetsCount[hapName].albumsCount;
188    if (assetsCount != presetsCount[hapName].assetsCount || albumsCount != presetsCount[hapName].albumsCount) {
189        console.info(`${hapName} checkPresetsAssets failed;
190            assetsCount : presetsassetsCount = ${assetsCount} : ${presetsassetsCount}
191            albumsCount : presetsalbumsCount = ${albumsCount} : ${presetsalbumsCount}`);
192    } else {
193        console.info(`${hapName} checkPresetsAssets passed`);
194    }
195};
196
197const checkAssetsCount = async function (done, testNum, fetchFileResult, expectCount) {
198    if (!fetchFileResult) {
199        console.info(`${testNum}:: fetchFileResult error:`);
200        expect(false).assertTrue();
201        done();
202        return false;
203    }
204    let count = await fetchFileResult.getCount();
205    if (count != expectCount) {
206        console.info(`${testNum}:: count:expectCount - ${count} : ${expectCount}`);
207        expect(count).assertEqual(expectCount);
208        done();
209    }
210    return count == expectCount;
211};
212
213const checkAlbumsCount = function (done, testNum, albumList, expectCount) {
214    if (!Array.isArray(albumList)) {
215        console.info(`${testNum}:: albumList error:`);
216        expect(false).assertTrue();
217        done();
218        return false;
219    }
220    let albumsCount = albumList.length;
221    if (albumsCount != expectCount) {
222        console.info(`${testNum}:: albumsCount: expectCount - ${albumsCount} : ${expectCount}`);
223        expect(albumsCount).assertEqual(expectCount);
224        done();
225    }
226    return albumsCount == expectCount;
227};
228
229const getPermission = async function (name, context) {
230    if (!name) {
231        name = "ohos.acts.multimedia.mediaLibrary";
232    }
233    console.info("getPermission start", name);
234
235    let permissions = ["ohos.permission.MEDIA_LOCATION", "ohos.permission.READ_MEDIA", "ohos.permission.WRITE_MEDIA"];
236    context.requestPermissionsFromUser(permissions, (data) => {
237        console.info(`getPermission requestPermissionsFromUser ${JSON.stringify(data)}`);
238    });
239
240    let driver = uitest.Driver.create();
241    await sleep(500);
242
243    for (let i = 0; i < 10; i++) {
244        await sleep(500);
245        let button = await driver.findComponent(uitest.ON.text("允许"));
246        if (button != undefined) {
247            await button.click();
248            break;
249        }
250    }
251
252    let appInfo = await bundle.getApplicationInfo(name, 0, 100);
253    let tokenID = appInfo.accessTokenId;
254    let atManager = abilityAccessCtrl.createAtManager();
255    let isGranted1 = await atManager.verifyAccessToken(tokenID, "ohos.permission.MEDIA_LOCATION");
256    let isGranted2 = await atManager.verifyAccessToken(tokenID, "ohos.permission.READ_MEDIA");
257    let isGranted3 = await atManager.verifyAccessToken(tokenID, "ohos.permission.WRITE_MEDIA");
258    if (!(isGranted1 == 0 && isGranted2 == 0 && isGranted3 == 0)) {
259        console.info("getPermission failed");
260    }
261    console.info("getPermission end");
262};
263
264const MODIFY_ERROR_CODE_01 = "-1000";
265
266const isNum = function (value) {
267    return typeof value === "number" && !isNaN(value);
268};
269export {
270    getPermission,
271    IMAGE_TYPE,
272    VIDEO_TYPE,
273    AUDIO_TYPE,
274    FILE_TYPE,
275    FILEKEY,
276    sleep,
277    allFetchOp,
278    fetchOps,
279    nameFetchOps,
280    idFetchOps,
281    albumFetchOps,
282    albumTwoTypesFetchOps,
283    albumThreeTypesFetchOps,
284    checkPresetsAssets,
285    checkAssetsCount,
286    checkAlbumsCount,
287    MODIFY_ERROR_CODE_01,
288    isNum,
289    fileIdFetchOps,
290};
291