PreferencesPromiseJsunit.test.js 16.4 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.
*/
15 16
import { describe, beforeAll, beforeEach, afterEach, afterAll, it, expect } from '@ohos/hypium';
import dataPreferences from '@ohos.data.preferences';
17 18 19 20 21 22 23 24
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';
25 26 27
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';
Y
yanglifeng1217 已提交
28 29
let mPreferences;
let context;
30 31

export default function preferencesPromiseTest(){
32 33
    describe('preferencesPromiseTest', function () {
        beforeAll(async function () {
34 35
            console.info('beforeAll');
            context = featureAbility.getContext();
36
            mPreferences = await dataPreferences.getPreferences(context, NAME);
37 38
        })

39
        afterAll(async function () {
40
            console.info('afterAll');
41 42
            await dataPreferences.deletePreferences(context, NAME);
        })
43

44 45 46 47 48 49 50
        /**
         * @tc.name put StringArray promise interface test
         * @tc.number SUB_DDM_Preferences_Put_Promise_0010
         * @tc.desc put StringArray promise interface test
         */
        it('testPreferencesPutStringArray0131', 0, async function (done) {
            await mPreferences.clear();
51
            var stringArr = ['1', '2', '3'];
Y
yanglifeng1217 已提交
52 53
            await mPreferences.put(KEY_TEST_STRING_ARRAY_ELEMENT, stringArr);
            await mPreferences.get(KEY_TEST_STRING_ARRAY_ELEMENT, ['123', '321']).then((pre) => {
54 55 56
                for (let i = 0; i < stringArr.length; i++) {
                    expect(stringArr[i]).assertEqual(pre[i]);
                }
Y
yanglifeng1217 已提交
57
                done();
58 59
            }).catch((err) => {
                expect(null).assertFail();
Y
yanglifeng1217 已提交
60
                done();
61
            })
62 63
        });

64 65 66 67 68 69 70
        /**
         * @tc.name put NumberArray promise interface test
         * @tc.number SUB_DDM_Preferences_Put_Promise_0020
         * @tc.desc put NumberArray promise interface test
         */
        it('testPreferencesPutNumberArray0132', 0, async function (done) {
            await mPreferences.clear();
71
            var numberArr = [11, 22, 33, 44, 55];
Y
yanglifeng1217 已提交
72 73
            await mPreferences.put(KEY_TEST_NUMBER_ARRAY_ELEMENT, numberArr);
            await mPreferences.get(KEY_TEST_NUMBER_ARRAY_ELEMENT, [123, 321]).then((pre) => {
74 75 76
                for (let i = 0; i < numberArr.length; i++) {
                    expect(numberArr[i]).assertEqual(pre[i]);
                }
Y
yanglifeng1217 已提交
77
                done();
78 79
            }).catch((err) => {
                expect(null).assertFail();
Y
yanglifeng1217 已提交
80
                done();
81
            })
82 83
        });

84 85 86 87 88 89 90
        /**
         * @tc.name put BoolArray promise interface test
         * @tc.number SUB_DDM_Preferences_Put_Promise_0030
         * @tc.desc put BoolArray promise interface test
         */
        it('testPreferencesPutBoolArray0133', 0, async function (done) {
            await mPreferences.clear();
91
            var boolArr = [true, true, false];
Y
yanglifeng1217 已提交
92 93
            await mPreferences.put(KEY_TEST_BOOL_ARRAY_ELEMENT, boolArr);
            await mPreferences.get(KEY_TEST_BOOL_ARRAY_ELEMENT, [false, true]).then((pre) => {
94 95 96
                for (let i = 0; i < boolArr.length; i++) {
                    expect(boolArr[i]).assertEqual(pre[i]);
                }
Y
yanglifeng1217 已提交
97
                done();
98 99
            }).catch((err) => {
                expect(null).assertFail();
Y
yanglifeng1217 已提交
100
                done();
101
            })
102 103
        });

104 105 106 107 108 109 110
        /**
         * @tc.name getAll promise interface test
         * @tc.number SUB_DDM_Preferences_GetAll_Promise_0040
         * @tc.desc getAll promise interface test
         */
        it('testPreferencesGetAll0001', 0, async function (done) {
            await mPreferences.clear();
111 112 113 114 115 116 117 118 119 120 121 122
            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();

Y
yanglifeng1217 已提交
123
            await mPreferences.getAll().then((obj) => {
124 125 126 127
                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;
128 129 130 131
                for (let i = 0; i < sArr.length; i++) {
                    expect(sArr[i]).assertEqual(stringArr[i]);
                }

132
                let bArr = obj.key_test_bool_array;
133 134 135 136 137 138 139 140
                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]);
                }
Y
yanglifeng1217 已提交
141
                done();
142 143
            }).catch((err) => {
                expect(null).assertFail();
Y
yanglifeng1217 已提交
144
                done();
145 146
            })
        })
147

148 149 150 151 152 153 154 155
        /**
         * @tc.name clear promise interface test
         * @tc.number SUB_DDM_Preferences_Clear_Promise_0050
         * @tc.desc clear promise interface test
         */
        it('testPreferencesClear0011', 0, async function (done) {
            await mPreferences.put(KEY_TEST_STRING_ELEMENT, "test");
            await mPreferences.flush();
Y
yanglifeng1217 已提交
156
            await mPreferences.clear().then(async (ret) => {
157 158
                let per = await mPreferences.get(KEY_TEST_STRING_ELEMENT, "defaultvalue");
                expect("defaultvalue").assertEqual(per);
Y
yanglifeng1217 已提交
159
                done();
160 161
            }).catch((err) => {
                expect(null).assertFail();
Y
yanglifeng1217 已提交
162
                done();
163 164
            });
        })
165

166 167 168 169 170 171 172
        /**
         * @tc.name has string interface test
         * @tc.number SUB_DDM_Preferences_Has_Promise_0060
         * @tc.desc has string interface test
         */
        it('testPreferencesHasKey0031', 0, async function (done) {
            await mPreferences.put(KEY_TEST_STRING_ELEMENT, "test");
Y
yanglifeng1217 已提交
173
            await mPreferences.has(KEY_TEST_STRING_ELEMENT).then((ret) => {
174
                expect(true).assertEqual(ret);
Y
yanglifeng1217 已提交
175
                done();
176 177
            }).catch((err) => {
                expect(null).assertFail();
Y
yanglifeng1217 已提交
178
                done();
179 180
            });
        })
181

182 183 184 185 186 187 188
        /**
         * @tc.name has int interface test
         * @tc.number SUB_DDM_Preferences_Has_Promise_0070
         * @tc.desc has int interface test
         */
        it('testPreferencesHasKey0032', 0, async function (done) {
            await mPreferences.put(KEY_TEST_INT_ELEMENT, 1);
Y
yanglifeng1217 已提交
189
            await mPreferences.has(KEY_TEST_INT_ELEMENT).then((ret) => {
190
                expect(true).assertEqual(ret);
Y
yanglifeng1217 已提交
191
                done();
192 193
            }).catch((err) => {
                expect(null).assertFail();
Y
yanglifeng1217 已提交
194
                done();
195 196 197
            });
            done();
        })
198

199 200 201 202 203 204 205
        /**
         * @tc.name has float interface test
         * @tc.number SUB_DDM_Preferences_Put_Promise_0080
         * @tc.desc has float interface test
         */
        it('testPreferencesHasKey0033', 0, async function (done) {
            await mPreferences.put(KEY_TEST_FLOAT_ELEMENT, 2.0);
Y
yanglifeng1217 已提交
206
            await mPreferences.has(KEY_TEST_FLOAT_ELEMENT).then((ret) => {
207
                expect(true).assertEqual(ret);
Y
yanglifeng1217 已提交
208
                done();
209 210
            }).catch((err) => {
                expect(null).assertFail();
Y
yanglifeng1217 已提交
211
                done();
212 213
            });
        })
214

215 216 217 218 219 220 221
        /**
         * @tc.name has boolean interface test
         * @tc.number SUB_DDM_Preferences_Has_Promise_0090
         * @tc.desc has boolean interface test
         */
        it('testPreferencesHasKey0034', 0, async function (done) {
            await mPreferences.put(KEY_TEST_BOOLEAN_ELEMENT, false);
Y
yanglifeng1217 已提交
222
            await mPreferences.has(KEY_TEST_BOOLEAN_ELEMENT).then((ret) => {
223
                expect(true).assertEqual(ret);
Y
yanglifeng1217 已提交
224
                done();
225 226
            }).catch((err) => {
                expect(null).assertFail();
Y
yanglifeng1217 已提交
227
                done();
228 229
            });
        })
230

231 232 233 234 235 236 237
        /**
         * @tc.name has long interface test
         * @tc.number SUB_DDM_Preferences_Put_Promise_0100
         * @tc.desc has long interface test
         */
        it('testPreferencesHasKey0035', 0, async function (done) {
            await mPreferences.put(KEY_TEST_LONG_ELEMENT, 0);
Y
yanglifeng1217 已提交
238
            await mPreferences.has(KEY_TEST_LONG_ELEMENT).then((ret) => {
239
                expect(true).assertEqual(ret);
Y
yanglifeng1217 已提交
240
                done();
241 242
            }).catch((err) => {
                expect(null).assertFail();
Y
yanglifeng1217 已提交
243
                done();
244
            });
Y
yanglifeng1217 已提交
245

246
        })
247

248 249 250 251 252 253 254
        /**
         * @tc.name get string promise interface test
         * @tc.number SUB_DDM_Preferences_Get_Promise_0110
         * @tc.desc get string promise interface test
         */
        it('testPreferencesGetDefValue0061', 0, async function (done) {
            await mPreferences.clear();
Y
yanglifeng1217 已提交
255
            await mPreferences.get(KEY_TEST_STRING_ELEMENT, "defaultValue").then((ret) => {
256
                expect('defaultValue').assertEqual(ret);
Y
yanglifeng1217 已提交
257
                done();
258 259
            }).catch((err) => {
                expect(null).assertFail();
Y
yanglifeng1217 已提交
260
                done();
261 262 263 264 265 266 267 268 269 270 271
            });
        })

        /**
         * @tc.name get float promise interface test
         * @tc.number SUB_DDM_Preferences_Get_Promise_0120
         * @tc.desc get float promise interface test
         */
        it('testPreferencesGetFloat0071', 0, async function (done) {
            await mPreferences.clear();
            await mPreferences.put(KEY_TEST_FLOAT_ELEMENT, 3.0);
Y
yanglifeng1217 已提交
272
            await mPreferences.get(KEY_TEST_FLOAT_ELEMENT, 0.0).then((ret) => {
273
                expect(3.0).assertEqual(ret);
Y
yanglifeng1217 已提交
274
                done();
275 276
            }).catch((err) => {
                expect(null).assertFail();
Y
yanglifeng1217 已提交
277
                done();
278 279 280 281 282 283 284 285 286 287 288
            });
        })

        /**
         * @tc.name get int promise interface test
         * @tc.number SUB_DDM_Preferences_Put_Promise_0130
         * @tc.desc get int promise interface test
         */
        it('testPreferencesGetInt0081', 0, async function (done) {
            await mPreferences.clear();
            await mPreferences.put(KEY_TEST_INT_ELEMENT, 3);
Y
yanglifeng1217 已提交
289
            await mPreferences.get(KEY_TEST_INT_ELEMENT, 0.0).then((ret) => {
290
                expect(3).assertEqual(ret);
Y
yanglifeng1217 已提交
291
                done();
292 293
            }).catch((err) => {
                expect(null).assertFail();
Y
yanglifeng1217 已提交
294
                done();
295 296 297 298 299 300 301 302 303 304 305
            });
        })

        /**
         * @tc.name get long promise interface test
         * @tc.number SUB_DDM_Preferences_Put_Promise_0140
         * @tc.desc get long promise interface test
         */
        it('testPreferencesGetLong0091', 0, async function (done) {
            await mPreferences.clear();
            await mPreferences.put(KEY_TEST_LONG_ELEMENT, 3);
Y
yanglifeng1217 已提交
306
            await mPreferences.get(KEY_TEST_LONG_ELEMENT, 0).then((ret) => {
307
                expect(3).assertEqual(ret);
Y
yanglifeng1217 已提交
308
                done();
309 310
            }).catch((err) => {
                expect(null).assertFail();
Y
yanglifeng1217 已提交
311
                done();
312 313 314 315 316 317 318 319 320 321 322 323
            });
        })

        /**
         * @tc.name get String promise interface test
         * @tc.number SUB_DDM_Preferences_Get_Promise_0150
         * @tc.desc get String promise interface test
         */
        it('tesPreferencesGetString101', 0, async function (done) {
            await mPreferences.clear();
            await mPreferences.put(KEY_TEST_STRING_ELEMENT, "test");
            await mPreferences.flush();
Y
yanglifeng1217 已提交
324
            await mPreferences.get(KEY_TEST_STRING_ELEMENT, "defaultvalue").then((ret) => {
325
                expect('test').assertEqual(ret);
Y
yanglifeng1217 已提交
326
                done();
327 328
            }).catch((err) => {
                expect(null).assertFail();
Y
yanglifeng1217 已提交
329
                done();
330 331
            });
        })
332

333 334 335 336 337 338 339
        /**
         * @tc.name put boolean promise interface test
         * @tc.number SUB_DDM_Preferences_Put_Promise_0160
         * @tc.desc put boolean promise interface test
         */
        it('testPreferencesPutBoolean0121', 0, async function (done) {
            await mPreferences.clear();
Y
yanglifeng1217 已提交
340
            await mPreferences.put(KEY_TEST_BOOLEAN_ELEMENT, true);
341 342 343 344 345
            let per = await mPreferences.get(KEY_TEST_BOOLEAN_ELEMENT, false);
            expect(true).assertEqual(per);
            await mPreferences.flush();
            let per2 = await mPreferences.get(KEY_TEST_BOOLEAN_ELEMENT, false);
            expect(true).assertEqual(per2);
346 347
            done();
        })
348

349 350 351 352 353 354 355
        /**
         * @tc.name put float promise interface test
         * @tc.number SUB_DDM_Preferences_Put_Promise_0170
         * @tc.desc put float promise interface test
         */
        it('testPreferencesPutFloat0131', 0, async function (done) {
            await mPreferences.clear();
Y
yanglifeng1217 已提交
356
            await mPreferences.put(KEY_TEST_FLOAT_ELEMENT, 4.0);
357 358 359 360 361
            let per = await mPreferences.get(KEY_TEST_FLOAT_ELEMENT, 0.0);
            expect(4.0).assertEqual(per);
            await mPreferences.flush();
            let per2 = await mPreferences.get(KEY_TEST_FLOAT_ELEMENT, 0.0);
            expect(4.0).assertEqual(per2);
362 363
            done();
        })
364

365 366 367 368 369 370 371
        /**
         * @tc.name put int promise interface test
         * @tc.number SUB_DDM_Preferences_Put_Promise_0180
         * @tc.desc put int promise interface test
         */
        it('testPreferencesPutInt0141', 0, async function (done) {
            await mPreferences.clear();
Y
yanglifeng1217 已提交
372
            await mPreferences.put(KEY_TEST_INT_ELEMENT, 4);
373 374 375 376 377
            let per = await mPreferences.get(KEY_TEST_INT_ELEMENT, 0);
            expect(4).assertEqual(per);
            await mPreferences.flush();
            let per2 = await mPreferences.get(KEY_TEST_INT_ELEMENT, 0);
            expect(4).assertEqual(per2);
378 379
            done();
        })
380

381 382 383 384 385 386
        /**
         * @tc.name put long promise interface test
         * @tc.number SUB_DDM_Preferences_Put_Promise_0190
         * @tc.desc put long promise interface test
         */
        it('testPreferencesPutLong0151', 0, async function (done) {
Y
yanglifeng1217 已提交
387
            await mPreferences.put(KEY_TEST_LONG_ELEMENT, 4);
388 389 390 391 392
            let per = await mPreferences.get(KEY_TEST_LONG_ELEMENT, 0);
            expect(4).assertEqual(per);
            await mPreferences.flush();
            let per2 = await mPreferences.get(KEY_TEST_LONG_ELEMENT, 0);
            expect(4).assertEqual(per2);
393 394
            done();
        })
395

396 397 398 399 400 401
        /**
         * @tc.name put String promise interface test
         * @tc.number SUB_DDM_Preferences_Put_Promise_0200
         * @tc.desc put String promise interface test
         */
        it('testPreferencesPutString0161', 0, async function (done) {
Y
yanglifeng1217 已提交
402
            await mPreferences.put(KEY_TEST_STRING_ELEMENT, '');
403 404 405 406 407
            let per = await mPreferences.get(KEY_TEST_STRING_ELEMENT, "defaultvalue")
            expect('').assertEqual(per);
            await mPreferences.flush();
            let per2 = await mPreferences.get(KEY_TEST_STRING_ELEMENT, "defaultvalue")
            expect('').assertEqual(per2);
408 409
            done();
        })
L
liangzhenyu123 已提交
410

411 412 413 414 415 416 417
        /**
         * @tc.name get string promise interface test
         * @tc.number SUB_DDM_Preferences_Get_Promise_0210
         * @tc.desc get string promise interface test
         */
        it('testPreferencesGetDefValue00162', 0, async function (done) {
            await mPreferences.clear();
Y
yanglifeng1217 已提交
418
            await mPreferences.get(KEY_TEST_BOOLEAN_ELEMENT, true).then((ret) => {
419
                expect(true).assertEqual(ret);
Y
yanglifeng1217 已提交
420
                done();
421 422
            }).catch((err) => {
                expect(null).assertFail();
Y
yanglifeng1217 已提交
423
                done();
424 425
            });
        })
L
liangzhenyu123 已提交
426
    })
427
}