getAlbumsPromise.test.ets 7.0 KB
Newer Older
Y
yangbo 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 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 172 173 174 175 176 177 178 179
/*
 * 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 userfile_manager from '@ohos.filemanagement.userfile_manager';
import { describe, beforeAll, beforeEach, afterEach, afterAll, it, expect } from 'deccjsunit/index';

import {
    sleep,
    IMAGE_TYPE,
    VIDEO_TYPE,
    AUDIO_TYPE,
    allFetchOp,
    albumFetchOps,
    checkPresetsAssets,
    checkAlbumsCount,
} from '../../../../../../common';

export default function getAlbumsPromise(abilityContext) {
    describe('getAlbumsPromise', function () {
        const userfile_mgr = userfile_manager.getUserFileMgr(abilityContext);
        beforeAll(async function () {
            console.info('beforeAll case');
            await checkPresetsAssets(userfile_mgr, 'ActsUserFileMgrAlbum');
        });
        beforeEach(function () {
            console.info('beforeEach case');
        });
        afterEach(async function () {
            console.info('afterEach case');
            await sleep()
        });
        afterAll(function () {
            console.info('afterAll case');
        });

        function printAlbumMessage(testNum, album) {
            console.info(`${testNum}
                album.albumName: ${album.albumName}
                album.albumUri: ${album.albumUri}
                album.dateModified: ${album.dateModified}
                album.count: ${album.count}
                album.relativePath: ${album.relativePath}
                album.coverUri: ${album.coverUri}`);
        }

        const props = {
            albumName: 'Static',
            albumUri: 'datashare:///userfile_mgr/album/',
            count: 1
        }
        const checkProps = async function (done, testNum, album, relativePaths) {
            printAlbumMessage(testNum, album);
            if (album.coverUri == undefined) {
                console.info(`${testNum}, album.coverUri is undefined`);
                expect(false).assertTrue();
                done();
                return;
            }
            expect(album.albumName).assertEqual(props.albumName);
            expect(album.count).assertEqual(props.count);
            if (Array.isArray(relativePaths)) {
                let i = relativePaths.indexOf(album.relativePath);
                if (i > -1) {
                    relativePaths.splice(i, 1)
                } else {
                    expect(false).assertTrue();
                    done();
                }
            } else {
                expect(album.relativePath).assertEqual(relativePaths);
            }
        }
        const checkAlbumInfo = async function (done, testNum, media_type, fetchOp, relativePaths, expectAlbumCount = 1) {
            try {
                const albumList = await userfile_mgr.getAlbums(media_type, fetchOp);
                const albumCountPass = await checkAlbumsCount(done, testNum, albumList, expectAlbumCount);
                if (!albumCountPass) return;
                const album = albumList[0];
                checkProps(done, testNum, album, relativePaths);
                done();
            } catch (error) {
                console.info(`${testNum}, failed: ${error}`);
                expect(false).assertTrue();
                done();
            }
        }

        /**
         * @tc.number    : SUB_USERFILE_MGR_GETALBUMS_PROMISE_01
         * @tc.name      : getAlbums
         * @tc.desc      : Image type getAlbums by relativePath && albumName, print all album info
         * @tc.size      : MEDIUM
         * @tc.type      : Function
         * @tc.level     : Level 0
         */
        it('SUB_USERFILE_MGR_GETALBUMS_PROMISE_01', 0, async function (done) {
            let testNum = 'SUB_USERFILE_MGR_GETALBUMS_PROMISE_01';
            let media_type = [IMAGE_TYPE];
            let currentFetchOp = albumFetchOps(testNum, 'Pictures/', 'Static');
            let relativePaths = 'Pictures/';
            await checkAlbumInfo(done, testNum, media_type, currentFetchOp, relativePaths);
        });

        /**
         * @tc.number    : SUB_USERFILE_MGR_GETALBUMS_PROMISE_02
         * @tc.name      : getAlbums
         * @tc.desc      : Image type getAlbums by relativePath && albumName, print all album info
         * @tc.size      : MEDIUM
         * @tc.type      : Function
         * @tc.level     : Level 0
         */
        it('SUB_USERFILE_MGR_GETALBUMS_PROMISE_02', 0, async function (done) {
            let testNum = 'SUB_USERFILE_MGR_GETALBUMS_PROMISE_02';
            let media_type = [AUDIO_TYPE];
            let currentFetchOp = albumFetchOps(testNum, 'Audios/', 'Static');
            let relativePaths = 'Audios/';
            await checkAlbumInfo(done, testNum, media_type, currentFetchOp, relativePaths);
        });

        /**
         * @tc.number    : SUB_USERFILE_MGR_GETALBUMS_PROMISE_03
         * @tc.name      : getAlbums
         * @tc.desc      : Image type getAlbums by relativePath && albumName, print all album info
         * @tc.size      : MEDIUM
         * @tc.type      : Function
         * @tc.level     : Level 0
         */
        it('SUB_USERFILE_MGR_GETALBUMS_PROMISE_03', 0, async function (done) {
            let testNum = 'SUB_USERFILE_MGR_GETALBUMS_PROMISE_03';
            let media_type = [VIDEO_TYPE];
            let currentFetchOp = albumFetchOps(testNum, 'Videos/', 'Static');
            let relativePaths = 'Videos/';
            await checkAlbumInfo(done, testNum, media_type, currentFetchOp, relativePaths);
        });

        /**
         * @tc.number    : SUB_USERFILE_MGR_GETALBUMS_PROMISE_04
         * @tc.name      : getAlbums
         * @tc.desc      : getAlbums by abnormal
         * @tc.size      : MEDIUM
         * @tc.type      : Function
         * @tc.level     : Level 0
         */
        it('SUB_USERFILE_MGR_GETALBUMS_PROMISE_04', 0, async function (done) {
            let testNum = 'SUB_USERFILE_MGR_GETALBUMS_PROMISE_04';
            try {
                let media_type = [-1];
                let currentFetchOp = allFetchOp();
                try {
                    await userfile_mgr.getAlbums(media_type, currentFetchOp);
                    expect(false).assertTrue();
                    done();
                } catch (error) {
                    console.info(`${testNum}, error1: ${error}`);
                    expect(true).assertTrue();
                    done();
                }
            } catch (error) {
                console.info(`${testNum}, error2: ${error}`);
                expect(false).assertTrue();
                done();
            }
        });
    });
}