PreferencesHelperJsunit.test.js 5.2 KB
Newer Older
1
/*
W
wangxiaomeng1379 已提交
2
* Copyright (c) 2022-2023 Huawei Device Co., Ltd.
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.
*/
Y
yanglifeng1217 已提交
15
import {describe, beforeAll, beforeEach, afterEach, afterAll, it, expect} from '@ohos/hypium'
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
import dataPreferences from '@ohos.data.preferences'
import featureAbility from '@ohos.ability.featureAbility';

const NAME = 'test_preferences';
const KEY_TEST_STRING_ELEMENT = 'key_test_string';
var mPreferences;
var context;
export default function preferencesHelperTest(){
describe('preferencesHelperTest', function () {
    beforeAll(async function () {
        console.info('beforeAll')
        context = featureAbility.getContext()
        mPreferences = await dataPreferences.getPreferences(context, NAME);
    })

    afterAll(async function () {
        console.info('afterAll')
        await dataPreferences.deletePreferences(context, NAME);
    })

    /**
     * @tc.name getPreferencesSync interface test
L
liangzhenyu123 已提交
38
     * @tc.number SUB_DDM_Preferences_GetPreferences_Helper_0010
39 40 41 42 43 44 45 46 47 48 49 50
     * @tc.desc getPreferencesSync interface test
     */
    it('testGetPreferencesHelper001', 0, async function () {
        mPreferences = await dataPreferences.getPreferences(context, NAME);
        await mPreferences.put('test', 2);
        await mPreferences.flush();
        var value = await mPreferences.get('test', 0);
        expect(value).assertEqual(2);
    })

    /**
     * @tc.name getPreferences interface test
L
liangzhenyu123 已提交
51
     * @tc.number SUB_DDM_Preferences_GetPreferences_Helper_0020
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
     * @tc.desc getPreferences interface test
     */
    it('testGetPreferencesHelper002', 0, async function (done) {
        const promise = dataPreferences.getPreferences(context, NAME);
        promise.then(async (pref) => {
            await pref.put('test', 2);
            await pref.flush();
            var value = await mPreferences.get('test', 0);
            expect(value).assertEqual(2);
        }).catch((err) => {
            expect(null).assertFail();
        });
        await promise;
        done();
    })

    /**
     * @tc.name removePreferencesFromCache interface test
L
liangzhenyu123 已提交
70
     * @tc.number SUB_DDM_Preferences_GetPreferences_Helper_0030
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86
     * @tc.desc removePreferencesFromCache interface test
     */
    it('testRemovePreferencesFromCache001', 0, async function (done) {
        let perf = await dataPreferences.getPreferences(context, NAME);
        perf = null;
        const promise = dataPreferences.removePreferencesFromCache(context, NAME);
        promise.then((pref) => {
        }).catch((err) => {
            expect(null).assertFail();
        });
        await promise;
        done();
    })

    /**
     * @tc.name deletePreferences interface test
L
liangzhenyu123 已提交
87
     * @tc.number SUB_DDM_Preferences_GetPreferences_Helper_0040
88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103
     * @tc.desc deletePreferences interface test
     */
    it('testDeletePreferencesHelper002', 0, async function (done) {
        let perf = await dataPreferences.getPreferences(context, NAME);
        perf = null;
        const promise = dataPreferences.deletePreferences(context, NAME);
        promise.then((pref) => {
        }).catch((err) => {
            expect(null).assertFail();
        });
        await promise;
        done();
    })

    /**
     * @tc.name put interface test
L
liangzhenyu123 已提交
104
     * @tc.number SUB_DDM_Preferences_Put_Helper_0050
105 106 107 108 109 110 111 112 113 114 115 116 117 118
     * @tc.desc put interface test
     */
    it('testPreferencesRegisterObserver001', 0, async function () {
        await mPreferences.clear();
        var observer = function (key) {
            console.info('testPreferencesRegisterObserver001 key' + key);
            expect('abcd').assertEqual(key);
        };
        await mPreferences.on('change', observer);
        await mPreferences.put(KEY_TEST_STRING_ELEMENT, "abcd");
    })

    /**
     * @tc.name repeat on interface test
L
liangzhenyu123 已提交
119
     * @tc.number SUB_DDM_Preferences_On_Helper_0060
120 121 122 123 124 125 126 127 128 129 130 131 132 133 134
     * @tc.desc repeat on interface test
     */
    it('testPreferencesRegisterObserver002', 0, async function () {
        await mPreferences.clear();
        var observer = function (key) {
            console.info('testPreferencesRegisterObserver002 key' + key);
            expect('abc').assertEqual(key);
        };
        await mPreferences.on('change', observer);
        await mPreferences.on('change', observer);
        await mPreferences.put(KEY_TEST_STRING_ELEMENT, "abc");
    })

    /**
     * @tc.name off interface test
L
liangzhenyu123 已提交
135
     * @tc.number SUB_DDM_Preferences_Off_Helper_0070
136 137 138 139 140 141 142 143 144 145 146 147 148
     * @tc.desc off interface test
     */
    it('testPreferencesUnRegisterObserver001', 0, async function () {
        var observer = function (key) {
            console.info('testPreferencesUnRegisterObserver001 key' + key);
            expect('').assertEqual(key);
        };
        await mPreferences.on('change', observer);
        await mPreferences.off('change', observer);
        await mPreferences.put(KEY_TEST_STRING_ELEMENT, "abc");
    })
})
}