PreferencesSyncJsunit.test.js 15.0 KB
Newer Older
Y
yanglifeng1217 已提交
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 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369
/*
* Copyright (c) 2023 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 { describe, beforeAll, beforeEach, afterEach, afterAll, it, expect } from '@ohos/hypium';
import data_preferences from '@ohos.data.preferences';
import featureAbility from '@ohos.ability.featureAbility';

const NAME = 'test_preferences_store';
const KEY_TEST_INT_ELEMENT = 'key_test_int';
const KEY_TEST_LONG_ELEMENT = 'key_test_long';
const KEY_TEST_FLOAT_ELEMENT = 'key_test_float';
const KEY_TEST_BOOLEAN_ELEMENT = 'key_test_boolean';
const KEY_TEST_STRING_ELEMENT = 'key_test_string';
const KEY_TEST_NUMBER_ARRAY_ELEMENT = 'key_test_number_array';
const KEY_TEST_STRING_ARRAY_ELEMENT = 'key_test_string_array';
const KEY_TEST_BOOL_ARRAY_ELEMENT = 'key_test_bool_array';
let mPreferences;
let context;

export default function PreferencesSyncTest() {
    describe('PreferencesSyncTest', function () {
        beforeAll(async function () {
            console.info('beforeAll');
            context = featureAbility.getContext();
            mPreferences = await data_preferences.getPreferences(context, NAME);
        })

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

        /**
         * @tc.number SUB_DDM_Preferences_PutSync_0100
         * @tc.name putSync StringArray interface test
         * @tc.desc putSync StringArray interface test
         */
        it('SUB_DDM_Preferences_PutSync_0100', 0, function (done) {
            mPreferences.clearSync();
            const stringArr = ['1', '2', '3'];
            mPreferences.putSync(KEY_TEST_STRING_ARRAY_ELEMENT, stringArr);
            let rc = mPreferences.getSync(KEY_TEST_STRING_ARRAY_ELEMENT, []);
            for (let i = 0; i < stringArr.length; i++) {
                console.info('ttt  stringArr = ' + stringArr[i] + '   rc = ' + rc[i]);
                expect(stringArr[i]).assertEqual(rc[i]);
            }
            done();
        });

        /**
         * @tc.number SUB_DDM_Preferences_PutSync_0200
         * @tc.name putSync numberArr interface test
         * @tc.desc putSync numberArr interface test
         */
        it('SUB_DDM_Preferences_PutSync_0200', 0, async function (done) {
            mPreferences.clearSync();
            const numberArr = [1, 2, 3];
            mPreferences.putSync(KEY_TEST_NUMBER_ARRAY_ELEMENT, numberArr);
            let rc = mPreferences.getSync(KEY_TEST_NUMBER_ARRAY_ELEMENT, []);
            for (let i = 0; i < numberArr.length; i++) {
                console.info('ttt  stringArr = ' + numberArr[i] + '   rc = ' + rc[i]);
                expect(numberArr[i]).assertEqual(rc[i]);
            }
            done();
        });

        /**
         * @tc.number SUB_DDM_Preferences_PutSync_0300
         * @tc.name putSync boolArr interface test
         * @tc.desc putSync boolArr interface test
         */
        it('SUB_DDM_Preferences_PutSync_0300', 0, async function (done) {
            mPreferences.clearSync();
            const boolArr = [true, true, false];
            mPreferences.putSync(KEY_TEST_BOOL_ARRAY_ELEMENT, boolArr);
            let rc = mPreferences.getSync(KEY_TEST_BOOL_ARRAY_ELEMENT, [false, true]);
            for (let i = 0; i < boolArr.length; i++) {
                console.info('ttt  stringArr = ' + boolArr[i] + '   rc = ' + rc[i]);
                expect(boolArr[i]).assertEqual(rc[i]);
            }
            done();
        });

        /**
         * @tc.number SUB_DDM_Preferences_GetAllSync_0100
         * @tc.name getAllSync interface test
         * @tc.desc getAllSync interface test
         */
        it('SUB_DDM_Preferences_GetAllSync_0100', 0, async function (done) {
            mPreferences.clearSync();
            const doubleArr = [11, 22, 33];
            const stringArr = ['11', '22', '33'];
            const boolArr = [true, false, false, true];
            mPreferences.putSync(KEY_TEST_STRING_ARRAY_ELEMENT, stringArr);
            mPreferences.putSync(KEY_TEST_BOOL_ARRAY_ELEMENT, boolArr);
            mPreferences.putSync(KEY_TEST_NUMBER_ARRAY_ELEMENT, doubleArr);
            mPreferences.putSync(KEY_TEST_BOOLEAN_ELEMENT, false);
            mPreferences.putSync(KEY_TEST_STRING_ELEMENT, "123");
            mPreferences.putSync(KEY_TEST_FLOAT_ELEMENT, 123.1);
            await mPreferences.flush();
            let obj = mPreferences.getAllSync();
            expect(false).assertEqual(obj.key_test_boolean);
            expect("123").assertEqual(obj.key_test_string);
            expect(123.1).assertEqual(obj.key_test_float);
            let sArr = obj.key_test_string_array;
            for (let i = 0; i < sArr.length; i++) {
                expect(sArr[i]).assertEqual(stringArr[i]);
            }
            let bArr = obj.key_test_bool_array;
            for (let i = 0; i < bArr.length; i++) {
                expect(bArr[i]).assertEqual(boolArr[i]);
            }
            let nArr = obj.key_test_number_array;
            for (let i = 0; i < nArr.length; i++) {
                expect(nArr[i]).assertEqual(doubleArr[i]);
            }
            done();
        })

        /**
         * @tc.number SUB_DDM_Preferences_ClearSync_0100
         * @tc.name clearSync interface test
         * @tc.desc clearSync interface test
         */
        it('SUB_DDM_Preferences_ClearSync_0100', 0, async function (done) {
            mPreferences.clearSync();
            mPreferences.putSync(KEY_TEST_STRING_ELEMENT, "test");
            await mPreferences.flush();
            mPreferences.clearSync();
            let per = mPreferences.getSync(KEY_TEST_STRING_ELEMENT, "defaultvalue");
            expect("defaultvalue").assertEqual(per);
            done();
        })

        /**
         * @tc.number SUB_DDM_Preferences_HasSync_0100
         * @tc.name hasSync interface test
         * @tc.desc hasSync interface test
         */
        it('SUB_DDM_Preferences_HasSync_0100', 0, async function (done) {
            mPreferences.clearSync();
            mPreferences.putSync(KEY_TEST_STRING_ELEMENT, "test");
            const ret = mPreferences.hasSync(KEY_TEST_STRING_ELEMENT);
            expect(true).assertEqual(ret);
            done();
        })

        /**
         * @tc.number SUB_DDM_Preferences_HasSync_0200
         * @tc.name hasSync interface test
         * @tc.desc hasSync interface test
         */
        it('SUB_DDM_Preferences_HasSync_0200', 0, async function (done) {
            mPreferences.clearSync();
            mPreferences.putSync(KEY_TEST_INT_ELEMENT, 1);
            const ret = mPreferences.hasSync(KEY_TEST_INT_ELEMENT);
            expect(true).assertEqual(ret);
            done();
        })

        /**
         * @tc.number SUB_DDM_Preferences_HasSync_0300
         * @tc.name hasSync interface test
         * @tc.desc hasSync interface test
         */
        it('SUB_DDM_Preferences_HasSync_0300', 0, async function (done) {
            mPreferences.clearSync();
            mPreferences.putSync(KEY_TEST_FLOAT_ELEMENT, 2.0);
            const ret = mPreferences.hasSync(KEY_TEST_FLOAT_ELEMENT);
            expect(true).assertEqual(ret);
            done();
        })

        /**
         * @tc.number SUB_DDM_Preferences_HasSync_0400
         * @tc.name hasSync interface test
         * @tc.desc hasSync interface test
         */
        it('SUB_DDM_Preferences_HasSync_0400', 0, async function (done) {
            mPreferences.clearSync();
            mPreferences.putSync(KEY_TEST_BOOLEAN_ELEMENT, false);
            const ret = mPreferences.hasSync(KEY_TEST_BOOLEAN_ELEMENT);
            expect(true).assertEqual(ret);
            done();
        })

        /**
         * @tc.number SUB_DDM_Preferences_HasSync_0500
         * @tc.name hasSync interface test
         * @tc.desc hasSync interface test
         */
        it('SUB_DDM_Preferences_HasSync_0500', 0, async function (done) {
            mPreferences.clearSync();
            mPreferences.putSync(KEY_TEST_LONG_ELEMENT, 0);
            const ret = mPreferences.hasSync(KEY_TEST_LONG_ELEMENT);
            expect(true).assertEqual(ret);
            done();
        })

        /**
         * @tc.number SUB_DDM_Preferences_GetSync_0100
         * @tc.name putSync interface test
         * @tc.desc putSync interface test
         */
        it('SUB_DDM_Preferences_GetSync_0100', 0, async function (done) {
            mPreferences.clearSync();
            const ret = mPreferences.getSync(KEY_TEST_STRING_ELEMENT, "defaultValue");
            expect('defaultValue').assertEqual(ret);
            done();
        })

        /**
         * @tc.number SUB_DDM_Preferences_GetSync_0200
         * @tc.name putSync interface test
         * @tc.desc putSync interface test
         */
        it('SUB_DDM_Preferences_GetSync_0200', 0, async function (done) {
            mPreferences.clearSync();
            mPreferences.putSync(KEY_TEST_FLOAT_ELEMENT, 3.0);
            const ret = mPreferences.getSync(KEY_TEST_FLOAT_ELEMENT, 0.0);
            expect(3.0).assertEqual(ret);
            done();
        })

        /**
         * @tc.number SUB_DDM_Preferences_GetSync_0300
         * @tc.name putSync interface test
         * @tc.desc putSync interface test
         */
        it('SUB_DDM_Preferences_GetSync_0300', 0, async function (done) {
            mPreferences.clearSync();
            mPreferences.putSync(KEY_TEST_INT_ELEMENT, 3);
            const ret = mPreferences.getSync(KEY_TEST_INT_ELEMENT, 0.0);
            expect(3).assertEqual(ret);
            done();
        })

        /**
         * @tc.number SUB_DDM_Preferences_GetSync_0400
         * @tc.name putSync interface test
         * @tc.desc putSync interface test
         */
        it('SUB_DDM_Preferences_GetSync_0400', 0, async function (done) {
            mPreferences.clearSync();
            mPreferences.putSync(KEY_TEST_LONG_ELEMENT, 3);
            const ret = mPreferences.getSync(KEY_TEST_LONG_ELEMENT, 0);
            expect(3).assertEqual(ret);
            done();
        })

        /**
         * @tc.number SUB_DDM_Preferences_GetSync_0500
         * @tc.name putSync interface test
         * @tc.desc putSync interface test
         */
        it('SUB_DDM_Preferences_GetSync_0500', 0, async function (done) {
            mPreferences.clearSync();
            mPreferences.putSync(KEY_TEST_STRING_ELEMENT, "test");
            await mPreferences.flush();
            const ret = mPreferences.getSync(KEY_TEST_STRING_ELEMENT, "defaultvalue");
            expect('test').assertEqual(ret);
            done();
        })

        /**
         * @tc.number SUB_DDM_Preferences_PutSync_0400
         * @tc.name putSync interface test
         * @tc.desc putSync interface test
         */
        it('SUB_DDM_Preferences_PutSync_0400', 0, async function (done) {
            mPreferences.clearSync();
            mPreferences.putSync(KEY_TEST_BOOLEAN_ELEMENT, true);
            let per = mPreferences.getSync(KEY_TEST_BOOLEAN_ELEMENT, false);
            expect(true).assertEqual(per);
            await mPreferences.flush();
            let per2 = mPreferences.getSync(KEY_TEST_BOOLEAN_ELEMENT, false);
            expect(true).assertEqual(per2);
            done();
        })

        /**
         * @tc.number SUB_DDM_Preferences_PutSync_0500
         * @tc.name putSync interface test
         * @tc.desc putSync interface test
         */
        it('SUB_DDM_Preferences_PutSync_0500', 0, async function (done) {
            mPreferences.clearSync();
            mPreferences.putSync(KEY_TEST_FLOAT_ELEMENT, 4.0);
            let per = mPreferences.getSync(KEY_TEST_FLOAT_ELEMENT, 0.0);
            expect(4.0).assertEqual(per);
            await mPreferences.flush();
            let per2 = mPreferences.getSync(KEY_TEST_FLOAT_ELEMENT, 0.0);
            expect(4.0).assertEqual(per2);
            done();
        })

        /**
         * @tc.number SUB_DDM_Preferences_PutSync_0600
         * @tc.name putSync interface test
         * @tc.desc putSync interface test
         */
        it('SUB_DDM_Preferences_PutSync_0600', 0, async function (done) {
            mPreferences.clearSync();
            mPreferences.putSync(KEY_TEST_INT_ELEMENT, 4);
            let per = mPreferences.getSync(KEY_TEST_INT_ELEMENT, 0);
            expect(4).assertEqual(per);
            await mPreferences.flush();
            let per2 = mPreferences.getSync(KEY_TEST_INT_ELEMENT, 0);
            expect(4).assertEqual(per2);
            done();
        })

        /**
         * @tc.number SUB_DDM_Preferences_PutSync_0700
         * @tc.name putSync interface test
         * @tc.desc putSync interface test
         */
        it('SUB_DDM_Preferences_PutSync_0700', 0, async function (done) {
            mPreferences.clearSync();
            mPreferences.putSync(KEY_TEST_LONG_ELEMENT, 4);
            let per = mPreferences.getSync(KEY_TEST_LONG_ELEMENT, 0);
            expect(4).assertEqual(per);
            await mPreferences.flush();
            let per2 = mPreferences.getSync(KEY_TEST_LONG_ELEMENT, 0);
            expect(4).assertEqual(per2);
            done();
        })

        /**
         * @tc.number SUB_DDM_Preferences_PutSync_0800
         * @tc.name putSync interface test
         * @tc.desc putSync interface test
         */
        it('SUB_DDM_Preferences_PutSync_0800', 0, async function (done) {
            mPreferences.clearSync();
            mPreferences.putSync(KEY_TEST_STRING_ELEMENT, '');
            let per = mPreferences.getSync(KEY_TEST_STRING_ELEMENT, "defaultvalue");
            expect('').assertEqual(per);
            await mPreferences.flush();
            let per2 = mPreferences.getSync(KEY_TEST_STRING_ELEMENT, "defaultvalue");
            expect('').assertEqual(per2);
            done();
        })

        /**
         * @tc.number SUB_DDM_Preferences_deleteSync_0100
         * @tc.name deleteSync interface test
         * @tc.desc deleteSync interface test
         */
        it('SUB_DDM_Preferences_deleteSync_0100', 0, async function (done) {
            mPreferences.putSync(KEY_TEST_STRING_ELEMENT, "abc");
            expect("abc").assertEqual(mPreferences.getSync(KEY_TEST_STRING_ELEMENT, "default"));
            mPreferences.deleteSync(KEY_TEST_STRING_ELEMENT);
            expect("default").assertEqual(mPreferences.getSync(KEY_TEST_STRING_ELEMENT, "default"));
            done();
        })
    })
}