PreferencesCallBackJsunit.test.js 19.4 KB
Newer Older
L
liangzhenyu123 已提交
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
/*
* Copyright (c) 2021 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 'deccjsunit/index'
import dataPreferences from '@ohos.data.preferences'
import featureAbility from '@ohos.ability.featureAbility';

const NAME = 'test_preferences';
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';
var mPreferences;
var context;

const TAG = '[PREFERENCES_CALLBACK_JSUNIT_TEST]'
export default function preferencesCallBackTest(){
describe('preferencesCallBackTest', 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 clear callback interface test
L
liangzhenyu123 已提交
47
     * @tc.number SUB_DDM_Preferences_Clear_CallBack_0010
L
liangzhenyu123 已提交
48 49 50 51 52 53 54 55 56 57 58 59 60 61
     * @tc.desc clear callback interface test
     */
    it('testPreferencesClear0012', 0, async function (done) {
        await mPreferences.put(KEY_TEST_STRING_ELEMENT, "test");
        await mPreferences.flush();
        await mPreferences.clear(async function (err, ret) {
            let pre = await mPreferences.get(KEY_TEST_STRING_ELEMENT, "defaultvalue")
            expect("defaultvalue").assertEqual(pre);
            done();
        });
    })

    /**
     * @tc.name has string callback interface test
L
liangzhenyu123 已提交
62
     * @tc.number SUB_DDM_Preferences_Has_CallBack_0020
L
liangzhenyu123 已提交
63 64 65 66 67 68 69 70 71 72 73 74
     * @tc.desc has string callback interface test
     */
    it('testPreferencesHasKey0032', 0, async function (done) {
        await mPreferences.put(KEY_TEST_STRING_ELEMENT, "test");
        await mPreferences.has(KEY_TEST_STRING_ELEMENT, function (err, ret) {
            expect(true).assertEqual(ret);
            done();
        })
    })

    /**
     * @tc.name has int callback interface test
L
liangzhenyu123 已提交
75
     * @tc.number SUB_DDM_Preferences_Has_CallBack_0030
L
liangzhenyu123 已提交
76 77 78 79 80 81 82 83 84 85 86 87
     * @tc.desc has int callback interface test
     */
    it('testPreferencesHasKey0033', 0, async function (done) {
        await mPreferences.put(KEY_TEST_INT_ELEMENT, 1);
        await mPreferences.has(KEY_TEST_INT_ELEMENT, function (err, ret) {
            expect(true).assertEqual(ret);
            done();
        })
    })

    /**
     * @tc.name has float callback interface test
L
liangzhenyu123 已提交
88
     * @tc.number SUB_DDM_Preferences_Has_CallBack_0040
L
liangzhenyu123 已提交
89 90 91 92 93 94 95 96 97 98 99 100
     * @tc.desc has float callback interface test
     */
    it('testPreferencesHasKey0034', 0, async function (done) {
        await mPreferences.put(KEY_TEST_FLOAT_ELEMENT, 1.1);
        await mPreferences.has(KEY_TEST_FLOAT_ELEMENT, function (err, ret) {
            expect(true).assertEqual(ret);
            done();
        })
    })

    /**
     * @tc.name has long callback interface test
L
liangzhenyu123 已提交
101
     * @tc.number SUB_DDM_Preferences_Has_CallBack_0050
L
liangzhenyu123 已提交
102 103 104 105 106 107 108 109 110 111 112 113
     * @tc.desc has long callback interface test
     */
    it('testPreferencesHasKey0035', 0, async function (done) {
        await mPreferences.put(KEY_TEST_LONG_ELEMENT, 0);
        await mPreferences.has(KEY_TEST_LONG_ELEMENT, function (err, ret) {
            expect(true).assertEqual(ret);
            done();
        })
    })

    /**
     * @tc.name has boolean callback interface test
L
liangzhenyu123 已提交
114
     * @tc.number SUB_DDM_Preferences_Has_CallBack_0060
L
liangzhenyu123 已提交
115 116 117 118 119 120 121 122 123 124 125 126
     * @tc.desc has boolean callback interface test
     */
    it('testPreferencesHasKey0036', 0, async function (done) {
        await mPreferences.put(KEY_TEST_BOOLEAN_ELEMENT, false);
        await mPreferences.has(KEY_TEST_BOOLEAN_ELEMENT, function (err, ret) {
            expect(true).assertEqual(ret);
            done();
        })
    })

    /**
     * @tc.name get defaultValue callback interface test
L
liangzhenyu123 已提交
127
     * @tc.number SUB_DDM_Preferences_Get_CallBack_0070
L
liangzhenyu123 已提交
128 129 130 131 132 133 134 135 136 137 138 139
     * @tc.desc get defaultValue callback interface test
     */
    it('testPreferencesGetDefValue0062', 0, async function (done) {
        await mPreferences.clear();
        await mPreferences.get(KEY_TEST_STRING_ELEMENT, "defaultValue", function (err, ret) {
            expect('defaultValue').assertEqual(ret);
            done();
        })
    })

    /**
     * @tc.name get float callback interface test
L
liangzhenyu123 已提交
140
     * @tc.number SUB_DDM_Preferences_Get_CallBack_0080
L
liangzhenyu123 已提交
141 142 143 144 145 146 147 148 149 150 151 152 153
     * @tc.desc get float callback interface test
     */
    it('testPreferencesGetFloat0072', 0, async function (done) {
        await mPreferences.clear();
        await mPreferences.put(KEY_TEST_FLOAT_ELEMENT, 3.0);
        await mPreferences.get(KEY_TEST_FLOAT_ELEMENT, 0.0, function (err, ret) {
            expect(3.0).assertEqual(ret);
            done();
        })
    })

    /**
     * @tc.name get int callback interface test
L
liangzhenyu123 已提交
154
     * @tc.number SUB_DDM_Preferences_Get_CallBack_0090
L
liangzhenyu123 已提交
155 156 157 158 159 160 161 162 163 164 165 166 167
     * @tc.desc get int callback interface test
     */
    it('testPreferencesGetInt0082', 0, async function (done) {
        await mPreferences.clear();
        await mPreferences.put(KEY_TEST_INT_ELEMENT, 3);
        await mPreferences.get(KEY_TEST_INT_ELEMENT, 0.0, function (err, ret) {
            expect(3).assertEqual(ret);
            done();
        })
    })

    /**
     * @tc.name get long callback interface test
L
liangzhenyu123 已提交
168
     * @tc.number SUB_DDM_Preferences_Get_CallBack_0100
L
liangzhenyu123 已提交
169 170 171 172 173 174 175 176 177 178 179 180 181 182 183
     * @tc.desc get long callback interface test
     */
    it('testPreferencesGetLong0092', 0, async function (done) {
        await mPreferences.clear();
        await mPreferences.put(KEY_TEST_LONG_ELEMENT, 3);
        let pref = await mPreferences.get(KEY_TEST_LONG_ELEMENT, 0)
        expect(3).assertEqual(pref);
        await mPreferences.get(KEY_TEST_LONG_ELEMENT, 0, function (err, ret) {
            expect(3).assertEqual(ret);
            done();
        });
    })

    /**
     * @tc.name get String callback interface test
L
liangzhenyu123 已提交
184
     * @tc.number SUB_DDM_Preferences_Get_CallBack_0110
L
liangzhenyu123 已提交
185 186 187 188 189 190 191 192 193 194 195 196 197 198
     * @tc.desc get String callback interface test
     */
    it('testPreferencesGetString102', 0, async function (done) {
        await mPreferences.clear();
        await mPreferences.put(KEY_TEST_STRING_ELEMENT, "test");
        await mPreferences.flush();
        await mPreferences.get(KEY_TEST_STRING_ELEMENT, "defaultvalue", function (err, ret) {
            expect('test').assertEqual(ret);
            done();
        });
    })

    /**
     * @tc.name put boolean callback interface test
L
liangzhenyu123 已提交
199
     * @tc.number SUB_DDM_Preferences_Put_CallBack_0120
L
liangzhenyu123 已提交
200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215
     * @tc.desc put boolean callback interface test
     */
    it('testPreferencesPutBoolean0122', 0, async function (done) {
        await mPreferences.clear();
        await mPreferences.put(KEY_TEST_BOOLEAN_ELEMENT, true, async function (err, ret) {
            let pre = await mPreferences.get(KEY_TEST_BOOLEAN_ELEMENT, false);
            expect(true).assertEqual(pre);
            await mPreferences.flush();
            let pre2 = await mPreferences.get(KEY_TEST_BOOLEAN_ELEMENT, false)
            expect(true).assertEqual(pre2);
            done();
        });
    })

    /**
     * @tc.name put float callback interface test
L
liangzhenyu123 已提交
216
     * @tc.number SUB_DDM_Preferences_Put_CallBack_0130
L
liangzhenyu123 已提交
217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232
     * @tc.desc put float callback interface test
     */
    it('testPreferencesPutFloat0132', 0, async function (done) {
        await mPreferences.clear();
        await mPreferences.put(KEY_TEST_FLOAT_ELEMENT, 4.0, async function (err, ret) {
            let pre = await mPreferences.get(KEY_TEST_FLOAT_ELEMENT, 0.0);
            expect(4.0).assertEqual(pre);
            await mPreferences.flush();
            let pre2 = await mPreferences.get(KEY_TEST_FLOAT_ELEMENT, 0.0);
            expect(4.0).assertEqual(pre2);
            done();
        });
    })

    /**
     * @tc.name put int callback interface test
L
liangzhenyu123 已提交
233
     * @tc.number SUB_DDM_Preferences_Put_CallBack_0140
L
liangzhenyu123 已提交
234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249
     * @tc.desc put int callback interface test
     */
    it('testPreferencesPutInt0142', 0, async function (done) {
        await mPreferences.clear();
        await mPreferences.put(KEY_TEST_INT_ELEMENT, 4, async function (err, ret) {
            let pre = await mPreferences.get(KEY_TEST_INT_ELEMENT, 0);
            expect(4).assertEqual(pre);
            await mPreferences.flush();
            let pre2 = await mPreferences.get(KEY_TEST_INT_ELEMENT, 0);
            expect(4).assertEqual(pre2);
            done();
        });
    })

    /**
     * @tc.name put long callback interface test
L
liangzhenyu123 已提交
250
     * @tc.number SUB_DDM_Preferences_Put_CallBack_0150
L
liangzhenyu123 已提交
251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267
     * @tc.desc put long callback interface test
     */
    it('testPreferencesPutLong0152', 0, async function (done) {
        await mPreferences.clear();
        await mPreferences.put(KEY_TEST_LONG_ELEMENT, 4);
        await mPreferences.put(KEY_TEST_LONG_ELEMENT, 4, async function (err, ret) {
            let pre = await mPreferences.get(KEY_TEST_LONG_ELEMENT, 0);
            expect(4).assertEqual(pre);
            await mPreferences.flush();
            let pre2 = await mPreferences.get(KEY_TEST_LONG_ELEMENT, 0);
            expect(4).assertEqual(pre2);
            done();
        });
    })

    /**
     * @tc.name put String callback interface test
L
liangzhenyu123 已提交
268
     * @tc.number SUB_DDM_Preferences_Put_CallBack_0160
L
liangzhenyu123 已提交
269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284
     * @tc.desc put String callback interface test
     */
    it('testPreferencesPutString0162', 0, async function (done) {
        await mPreferences.clear();
        await mPreferences.put(KEY_TEST_STRING_ELEMENT, '', async function (err, ret) {
            let pre = await mPreferences.get(KEY_TEST_STRING_ELEMENT, "defaultvalue")
            expect('').assertEqual(pre);
            await mPreferences.flush();
            let pre2 = await mPreferences.get(KEY_TEST_STRING_ELEMENT, "defaultvalue")
            expect('').assertEqual(pre2);
            done();
        });
    })

    /**
     * @tc.name clear、put、get、flush String callback interface test
L
liangzhenyu123 已提交
285
     * @tc.number SUB_DDM_Preferences_Flush_CallBack_0170
L
liangzhenyu123 已提交
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
     * @tc.desc flush String callback interface test
     */
     it('testPreferencesCallback0172', 0, function (done) {
        console.log(TAG + "testPreferencesCallback0172 begin.");
        mPreferences.clear(function (err, val) {
            if(err){
                expect(null).assertEqual();
            }
            mPreferences.put(KEY_TEST_STRING_ELEMENT, '', function (err, ret) {
                if(err){
                    expect(null).assertEqual();
                }
                mPreferences.get(KEY_TEST_STRING_ELEMENT, "defaultvalue", function (err, pre) {
                    if(err){
                        expect(null).assertEqual();
                    }
                    expect('').assertEqual(pre);
                    mPreferences.flush(function (err, val) {
                        if(err){
                            expect(null).assertEqual();
                        }
                        mPreferences.get(KEY_TEST_STRING_ELEMENT, "defaultvalue", function (err, pre2) {
                            if(err){
                                expect(null).assertEqual();
                            }
                            expect('').assertEqual(pre2);
                            done();
                            console.log(TAG + "testPreferencesCallback0172 end.");
                        })
                    });
                })
            });
        });
    })

    /**
     * @tc.name put StringArray callback interface test
L
liangzhenyu123 已提交
323
     * @tc.number SUB_DDM_Preferences_Put_CallBack_0180
L
liangzhenyu123 已提交
324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340
     * @tc.desc put String callback interface test
     */
    it('testPreferencesPutStringArray0001', 0, async function (done) {
        await mPreferences.clear();
        var stringArr = ['11', '22', '33']
        await mPreferences.put(KEY_TEST_STRING_ARRAY_ELEMENT, stringArr, async function (err, ret) {
            let pre = await mPreferences.get(KEY_TEST_STRING_ARRAY_ELEMENT, ['123', '321'])
            for (let i = 0; i < stringArr.length; i++) {
                expect(stringArr[i]).assertEqual(pre[i]);
            }

            done();
        });
    })

    /**
     * @tc.name put NumberArray callback interface test
L
liangzhenyu123 已提交
341
     * @tc.number SUB_DDM_Preferences_Put_CallBack_0190
L
liangzhenyu123 已提交
342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358
     * @tc.desc put String callback interface test
     */
    it('testPreferencesPutNumArray0001', 0, async function (done) {
        await mPreferences.clear();
        var doubleArr = [11, 22, 33]
        await mPreferences.put(KEY_TEST_NUMBER_ARRAY_ELEMENT, doubleArr, async function (err, ret) {
            let pre = await mPreferences.get(KEY_TEST_NUMBER_ARRAY_ELEMENT, [123, 321])
            for (let i = 0; i < doubleArr.length; i++) {
                expect(doubleArr[i]).assertEqual(pre[i]);
            }

            done();
        });
    })

    /**
     * @tc.name put BoolArray callback interface test
L
liangzhenyu123 已提交
359
     * @tc.number SUB_DDM_Preferences_Put_CallBack_0200
L
liangzhenyu123 已提交
360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376
     * @tc.desc put String callback interface test
     */
    it('testPreferencesPutBoolArray0001', 0, async function (done) {
        await mPreferences.clear();
        let boolArr = [true, false, false, true]
        await mPreferences.put(KEY_TEST_BOOL_ARRAY_ELEMENT, boolArr, async function (err, ret) {
            let pre = await mPreferences.get(KEY_TEST_BOOL_ARRAY_ELEMENT, [true, false])
            for (let i = 0; i < boolArr.length; i++) {
                expect(boolArr[i]).assertEqual(pre[i]);
            }

            done();
        });
    })

    /**
     * @tc.name getAll callback interface test
L
liangzhenyu123 已提交
377
     * @tc.number SUB_DDM_Preferences_GetAll_CallBack_0210
L
liangzhenyu123 已提交
378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418
     * @tc.desc getAll callback interface test
     */
    it('testPreferencesGetAll0001', 0, async function (done) {
        await mPreferences.clear();
        let doubleArr = [11, 22, 33]
        let stringArr = ['11', '22', '33']
        let boolArr = [true, false, false, true]
        await mPreferences.put(KEY_TEST_STRING_ARRAY_ELEMENT, stringArr)
        await mPreferences.put(KEY_TEST_BOOL_ARRAY_ELEMENT, boolArr)
        await mPreferences.put(KEY_TEST_NUMBER_ARRAY_ELEMENT, doubleArr)
        await mPreferences.put(KEY_TEST_BOOLEAN_ELEMENT, false)
        await mPreferences.put(KEY_TEST_STRING_ELEMENT, "123")
        await mPreferences.put(KEY_TEST_FLOAT_ELEMENT, 123.1)

        await mPreferences.flush()

        await mPreferences.getAll(function (err, obj) {
            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.name getPreferences callback interface test
L
liangzhenyu123 已提交
419
     * @tc.number SUB_DDM_Preferences_GetPreferencesTest_CallBack_0220
L
liangzhenyu123 已提交
420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437
     * @tc.desc getPreferences callback interface test
    */
    it('testPreferencesGetPreferences0001', 0, async function (done) {
        const NAME = 'getPreferencesTest'
        await dataPreferences.getPreferences(context, NAME, (err, preferences) => {
            if(err != null){
                console.info(TAG + "Get preferences err: " + err)
                expect(false).assertTrue(); 
            }else{
                console.info(TAG + "Get preferences success")
                expect(preferences != null).assertTrue();
            }
        })
        done()
    })

    /**
     * @tc.name deletePreferences callback interface test
L
liangzhenyu123 已提交
438
     * @tc.number SUB_DDM_Preferences_DeletePreferencesTest_CallBack_0230
L
liangzhenyu123 已提交
439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465
     * @tc.desc deletePreferences callback interface test
    */
     it('testPreferencesDeletePreferences0001', 0, async function (done) {
        const NAME = 'getPreferencesTest'
        await dataPreferences.getPreferences(context, NAME, (err, preferences) => {
            if(err != null){
                console.info(TAG + "Get preferences err: " + err)
                expect(false).assertTrue();
            }else{
                console.info(TAG + "Get preferences success")
                expect(preferences != null).assertTrue();
            }
        })
        await dataPreferences.deletePreferences(context, NAME, (err, data) => {
            if(err != null){
                console.info(TAG + "Delete preferences err: " + err)
                expect(false).assertTrue();
            }else{
                console.info(TAG + "Delete preferences success")
                expect(true).assertTrue();
            }
        })
        done()
    })

    /**
     * @tc.name removePreferencesFromCache interface test
L
liangzhenyu123 已提交
466
     * @tc.number SUB_DDM_Preferences_RemovePreferencesFromCache_CallBack_0240
L
liangzhenyu123 已提交
467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489
     * @tc.desc removePreferencesFromCache interface test
     */
     it('testRemovePreferencesFromCache0001', 0, async function (done) {
        await dataPreferences.getPreferences(context, NAME, (err, preferences) => {
            if(err != null){
                console.info(TAG + "Get preferences err: " + err)
                expect(false).assertTrue();
            }else{
                console.info(TAG + "Get preferences success")
                expect(preferences != null).assertTrue();
            }
        })
        await dataPreferences.removePreferencesFromCache(context, NAME, (err ,data) => {
            if(err != null){
                console.info(TAG + "Remove preferences from cache error: " + err)
                expect(false).assertTrue();
            }else{
                console.info(TAG + "Remove preferences from cache success")
                expect(true).assertTrue();
            }
        })
        done();
    })
L
liangzhenyu123 已提交
490 491 492 493 494 495 496 497 498 499 500 501 502 503

    /**
     * @tc.name get defaultValue callback interface test
     * @tc.number SUB_DDM_Preferences_Get_CallBack_0250
     * @tc.desc get defaultValue callback interface test
     */
     it('testPreferencesGetDefValue0173', 0, async function (done) {
        await mPreferences.clear();
        await mPreferences.get(KEY_TEST_BOOLEAN_ELEMENT, true, function (err, ret) {
            expect(true).assertEqual(ret);
            done();
        })
    })

L
liangzhenyu123 已提交
504
})
505
}