getAlbumsPromise.test.ets 5.5 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 19 20
import { describe, beforeAll, beforeEach, afterEach, afterAll, it, expect } from 'deccjsunit/index';

import {
    sleep,
Y
yangbo 已提交
21
    THROW_ERROR_CODE,
Y
yangbo 已提交
22 23
    albumFetchOps,
    checkPresetsAssets,
Y
yangbo 已提交
24
    checkAssetsCount,
Y
yangbo 已提交
25 26
} from '../../../../../../common';

Y
yangbo 已提交
27 28
export default function getAlbumsPromiseTest(abilityContext) {
    describe('getAlbumsPromiseTest', function () {
Y
yangbo 已提交
29
        const userfilemgr = userFileManager.getUserFileMgr(abilityContext);
Y
yangbo 已提交
30 31
        beforeAll(async function () {
            console.info('beforeAll case');
Y
yangbo 已提交
32
            await checkPresetsAssets(userfilemgr, 'ActsUserFileMgrAlbumJsTest');
Y
yangbo 已提交
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
        });
        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 = {
Y
yangbo 已提交
56 57 58
            albumName: 'myAlbum',
            albumUri: 'datashare:///userfilemgr/album/',
            count: 1
Y
yangbo 已提交
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81
        }
        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);
            }
        }
Y
yangbo 已提交
82
        const checkAlbumInfo = async function (done, testNum, fetchOp, relativePaths, expectAlbumCount = 1) {
Y
yangbo 已提交
83
            try {
Y
yangbo 已提交
84
                const fetchAlbumResult = await userfilemgr.getPhotoAlbums(fetchOp);
Y
yangbo 已提交
85
                const albumCountPass = await checkAssetsCount(done, testNum, fetchAlbumResult, expectAlbumCount);
Y
yangbo 已提交
86
                if (!albumCountPass) return;
Y
yangbo 已提交
87
                const album = await fetchAlbumResult.getFirstObject();
Y
yangbo 已提交
88
                checkProps(done, testNum, album, relativePaths);
Y
yangbo 已提交
89
                fetchAlbumResult.close();
Y
yangbo 已提交
90 91 92 93 94 95 96 97
                done();
            } catch (error) {
                console.info(`${testNum}, failed: ${error}`);
                expect(false).assertTrue();
                done();
            }
        }

Y
yangbo 已提交
98 99 100 101 102 103 104 105 106 107 108 109 110 111
        const getPhotoAlbumsErrCodeTest = async function (done, testNum) {
            try {
                let invalidOps : userFileManager.FetchOptions;
                let fetchAlbumResult = await userfilemgr.getPhotoAlbums(invalidOps);
                expect(false).assertTrue();
                fetchAlbumResult.close();
                done();
            } catch (error) {
                console.info(`${testNum}, errCode :${JSON.stringify(error)}`);
                expect(error.code).assertEqual(THROW_ERROR_CODE.toString());
                done();
            }
        }

Y
yangbo 已提交
112
        /**
Y
yangbo 已提交
113 114 115
         * @tc.number    : SUB_USERFILE_MGR_GETPHOTOALBUMS_PROMISE_01
         * @tc.name      : getPhotoAlbums
         * @tc.desc      : getPhotoAlbums by relativePath && albumName, print all album info
Y
yangbo 已提交
116 117
         * @tc.size      : MEDIUM
         * @tc.type      : Function
118
         * @tc.level     : Level 1
Y
yangbo 已提交
119
         */
120
        it('SUB_USERFILE_MGR_GETPHOTOALBUMS_PROMISE_01', 1, async function (done) {
Y
yangbo 已提交
121 122
            let testNum = 'SUB_USERFILE_MGR_GETPHOTOALBUMS_PROMISE_01';
            let currentFetchOp = albumFetchOps(testNum, 'Pictures/', 'myAlbum');
Y
yangbo 已提交
123
            let relativePaths = 'Pictures/';
Y
yangbo 已提交
124
            await checkAlbumInfo(done, testNum, currentFetchOp, relativePaths);
Y
yangbo 已提交
125
        });
Y
yangbo 已提交
126 127 128 129 130 131 132

        /**
         * @tc.number    : SUB_USERFILE_MGR_GETPHOTOALBUMS_ERRCODE_PROMISE_01
         * @tc.name      : getPhotoAlbums
         * @tc.desc      : getPhotoAlbums by invalid
         * @tc.size      : MEDIUM
         * @tc.type      : Function
133
         * @tc.level     : Level 3
Y
yangbo 已提交
134
         */
135
         it('SUB_USERFILE_MGR_GETPHOTOALBUMS_ERRCODE_PROMISE_01', 3, async function (done) {
Y
yangbo 已提交
136 137 138
            let testNum = 'SUB_USERFILE_MGR_GETPHOTOALBUMS_ERRCODE_PROMISE_01';
            await getPhotoAlbumsErrCodeTest(done, testNum);
        });
Y
yangbo 已提交
139 140 141 142
    });
}