1/* 2 * Copyright (c) 2023 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15import { describe, beforeAll, beforeEach, afterEach, afterAll, it, expect } from "@ohos/hypium"; 16import dataPreferences from "@ohos.data.preferences"; 17import common from "@ohos.app.ability.common"; 18 19const NAME = "test_preferences"; 20const NAME1 = "getPreferencesTest"; 21const KEY_TEST_INT_ELEMENT = "key_test_int"; 22const KEY_TEST_LONG_ELEMENT = "key_test_long"; 23const KEY_TEST_FLOAT_ELEMENT = "key_test_float"; 24const KEY_TEST_BOOLEAN_ELEMENT = "key_test_boolean"; 25const KEY_TEST_STRING_ELEMENT = "key_test_string"; 26const KEY_TEST_NUMBER_ARRAY_ELEMENT = "key_test_number_array"; 27const KEY_TEST_STRING_ARRAY_ELEMENT = "key_test_string_array"; 28const KEY_TEST_BOOL_ARRAY_ELEMENT = "key_test_bool_array"; 29let mPreferences; 30const TAG = "[PREFERENCES_CALLBACK_JSUNIT_TEST]"; 31let context: common.UIAbilityContext ; 32 33export default function preferencesCallBackTest() { 34 describe("preferencesCallBackTest", function () { 35 beforeAll(async function () { 36 console.info("beforeAll"); 37 context = AppStorage.get<common.UIAbilityContext>("TestAbilityContext") as common.UIAbilityContext; 38 console.info("context=======>"+context); 39 mPreferences = await dataPreferences.getPreferences(context, NAME); 40 }); 41 42 afterAll(async function () { 43 console.info("afterAll"); 44 await dataPreferences.deletePreferences(context, NAME); 45 }); 46 47 /** 48 * @tc.number : SUB_DistributeData_CrossPlatform_SDK_Preferences_GetPreferencesJSAPITest_1100 49 * @tc.name : testPreferencesGetPreferences000 50 * @tc.desc : GetPreferences callback interface test 51 * @tc.size : MediumTest 52 * @tc.type : Function 53 * @tc.level : Level 2 54 */ 55 it("testPreferencesGetPreferences000", 0, async function (done) { 56 dataPreferences.getPreferences(context, NAME1, async (err, preferences) => { 57 if (err) { 58 console.info(TAG + "Get preferences err: " + err); 59 expect(!err).assertTrue(); 60 } else { 61 console.info(TAG + "Get preferences success" + preferences); 62 await preferences.put(KEY_TEST_STRING_ELEMENT, "test"); 63 preferences.get(KEY_TEST_STRING_ELEMENT, "defaultvalue", function (err, ret) { 64 expect("test").assertEqual(ret); 65 }); 66 await dataPreferences.deletePreferences(context, NAME1); 67 done(); 68 } 69 }); 70 }); 71 72 /** 73 * @tc.number : SUB_DistributeData_CrossPlatform_SDK_Preferences_GetPreferencesJSAPITest_0700 74 * @tc.name : testPreferencesGetPreferences002 75 * @tc.desc : GetPreferences callback interface test 76 * @tc.size : MediumTest 77 * @tc.type : Function 78 * @tc.level : Level 2 79 */ 80 it("testPreferencesGetPreferences002", 0, async function (done) { 81 try { 82 dataPreferences.getPreferences(context, null, (err, preferences) => { 83 expect(!err).assertFail(); 84 done(); 85 }); 86 } catch (err) { 87 console.info("testPreferencesGetPreferences002 fail: " + err.message + "code: " + err.code); 88 expect("401").assertEqual(err.code); 89 done(); 90 } 91 }); 92 93 /** 94 * @tc.number : SUB_DistributeData_CrossPlatform_SDK_Preferences_GetPreferencesJSAPITest_0800 95 * @tc.name : testPreferencesGetPreferences003 96 * @tc.desc : GetPreferences callback interface test 97 * @tc.size : MediumTest 98 * @tc.type : Function 99 * @tc.level : Level 2 100 */ 101 it("testPreferencesGetPreferences003", 0, async function (done) { 102 dataPreferences.getPreferences(context, { 103 name: NAME1 104 }, async (err, preferences) => { 105 if (err) { 106 console.info(TAG + "Get preferences err: " + err); 107 expect(!err).assertTrue(); 108 } else { 109 console.info(TAG + "Get preferences success" + preferences); 110 await preferences.put(KEY_TEST_STRING_ELEMENT, "test"); 111 preferences.get(KEY_TEST_STRING_ELEMENT, "defaultvalue", function (err, ret) { 112 expect("test").assertEqual(ret); 113 }); 114 await dataPreferences.deletePreferences(context, NAME1); 115 done(); 116 } 117 }); 118 }); 119 120 /** 121 * @tc.number : SUB_DistributeData_CrossPlatform_SDK_Preferences_GetPreferencesJSAPITest_0900 122 * @tc.name : testPreferencesGetPreferences004 123 * @tc.desc : GetPreferences callback interface test 124 * @tc.size : MediumTest 125 * @tc.type : Function 126 * @tc.level : Level 2 127 */ 128 it("testPreferencesGetPreferences004", 0, async function (done) { 129 try { 130 dataPreferences.getPreferences(context, { 131 name: null 132 }, (err, preferences) => { 133 expect(!err).assertFail(); 134 done(); 135 }); 136 } catch (err) { 137 console.info("testPreferencesGetPreferences004 fail: " + err.message + "code: " + err.code); 138 expect("401").assertEqual(err.code); 139 done(); 140 } 141 }); 142 143 /** 144 * @tc.number : SUB_DistributeData_CrossPlatform_SDK_Preferences_DeleteJSAPITest_0400 145 * @tc.name : testPreferencesDeletePreferences000 146 * @tc.desc : DeletePreferences callback interface test 147 * @tc.size : MediumTest 148 * @tc.type : Function 149 * @tc.level : Level 2 150 */ 151 it("testPreferencesDeletePreferences000", 0, async function (done) { 152 dataPreferences.getPreferences(context, NAME1, (err, preferences) => { 153 if (err) { 154 console.info(TAG + "Get preferences err: " + err); 155 expect(!err).assertTrue(); 156 } else { 157 console.info(TAG + "Get preferences success"); 158 expect(preferences !== null).assertTrue(); 159 dataPreferences.deletePreferences(context, NAME1, (err) => { 160 console.info(TAG + "Delete preferences success"); 161 expect(!err).assertTrue(); 162 done(); 163 }); 164 } 165 }); 166 }); 167 168 /** 169 * @tc.number : SUB_DistributeData_CrossPlatform_SDK_Preferences_DeleteJSAPITest_0500 170 * @tc.name : testPreferencesDeletePreferences001 171 * @tc.desc : DeletePreferences callback interface test 172 * @tc.size : MediumTest 173 * @tc.type : Function 174 * @tc.level : Level 2 175 */ 176 it("testPreferencesDeletePreferences001", 0, async function (done) { 177 dataPreferences.getPreferences(context, NAME1, (err, preferences) => { 178 if (err) { 179 console.info(TAG + "Get preferences err: " + err); 180 expect(!err).assertFail(); 181 } else { 182 console.info(TAG + "Get preferences success"); 183 expect(preferences !== null).assertTrue(); 184 try { 185 dataPreferences.deletePreferences(context, null, (err) => { 186 expect(!err).assertFail(); 187 done(); 188 }); 189 expect(!err).assertFail(); 190 done(); 191 } catch (err) { 192 console.info("testPreferencesDeletePreferences001 fail: " + err.message + "code: " + err.code); 193 expect('401').assertEqual(err.code); 194 done(); 195 } 196 } 197 }); 198 }); 199 200 /** 201 * @tc.number : SUB_DistributeData_CrossPlatform_SDK_Preferences_DeleteJSAPITest_1600 202 * @tc.name : testPreferencesDeletePreferences003 203 * @tc.desc : DeletePreferences callback interface test 204 * @tc.size : MediumTest 205 * @tc.type : Function 206 * @tc.level : Level 2 207 */ 208 it("testPreferencesDeletePreferences003", 0, async function (done) { 209 dataPreferences.getPreferences(context, NAME1, (err, preferences) => { 210 if (err) { 211 console.info(TAG + "Get preferences err: " + err); 212 expect(!err).assertTrue(); 213 done(); 214 } else { 215 console.info(TAG + "Get preferences success"); 216 expect(preferences !== null).assertTrue(); 217 try { 218 preferences.put(KEY_TEST_STRING_ELEMENT, "test", (err) => { 219 if (err) { 220 console.info(TAG + "Put preferences err: " + err); 221 return; 222 } 223 console.info(TAG + "Put preferences success"); 224 preferences.get(KEY_TEST_STRING_ELEMENT, "defaultvalue", function (err, ret) { 225 expect("test").assertEqual(ret); 226 dataPreferences.deletePreferences(context, "getPreferencesTest1", (err) => { 227 preferences.get(KEY_TEST_STRING_ELEMENT, "defaultvalue", function (err, pre) { 228 expect("test").assertEqual(pre); 229 done(); 230 }); 231 }); 232 }); 233 }); 234 235 expect(preferences !== null).assertTrue(); 236 done(); 237 } catch (err) { 238 console.info("testPreferencesDeletePreferences003 fail: " + err.message + "code: " + err.code); 239 expect(!err).assertTrue(); 240 done(); 241 } 242 } 243 }); 244 }); 245 246 /** 247 * @tc.number : SUB_DistributeData_CrossPlatform_SDK_Preferences_DeleteJSAPITest_1300 248 * @tc.name : testPreferencesDeletePreferences004 249 * @tc.desc : DeletePreferences callback interface test 250 * @tc.size : MediumTest 251 * @tc.type : Function 252 * @tc.level : Level 2 253 */ 254 it("testPreferencesDeletePreferences004", 0, async function (done) { 255 dataPreferences.getPreferences(context, { 256 name: NAME1 257 }, (err, preferences) => { 258 if (err) { 259 console.info(TAG + "Get preferences err: " + err); 260 expect(!err).assertTrue(); 261 } else { 262 console.info(TAG + "Get preferences success"); 263 expect(preferences !== null).assertTrue(); 264 dataPreferences.deletePreferences(context, { 265 name: NAME1 266 }, (err) => { 267 console.info(TAG + "Delete preferences success"); 268 expect(!err).assertTrue(); 269 done(); 270 }); 271 } 272 }); 273 }); 274 275 /** 276 * @tc.number : SUB_DistributeData_CrossPlatform_SDK_Preferences_DeleteJSAPITest_1400 277 * @tc.name : testPreferencesDeletePreferences005 278 * @tc.desc : DeletePreferences callback interface test 279 * @tc.size : MediumTest 280 * @tc.type : Function 281 * @tc.level : Level 2 282 */ 283 it("testPreferencesDeletePreferences005", 0, async function (done) { 284 dataPreferences.getPreferences(context, { 285 name: NAME1 286 }, (err, preferences) => { 287 if (err) { 288 console.info(TAG + "Get preferences err: " + err); 289 expect(!err).assertTrue(); 290 } else { 291 console.info(TAG + "Get preferences success"); 292 expect(preferences !== null).assertTrue(); 293 try { 294 dataPreferences.deletePreferences(context, { 295 name: null 296 }, (err) => { 297 expect(!err).assertFail(); 298 done(); 299 }); 300 expect(!err).assertFail(); 301 done(); 302 } catch (err) { 303 console.info("testPreferencesDeletePreferences005 fail: " + err.message + "code: " + err.code); 304 expect('401').assertEqual(err.code); 305 done(); 306 } 307 } 308 }); 309 }); 310 311 /** 312 * @tc.number : SUB_DistributeData_CrossPlatform_SDK_Preferences_DeleteJSAPITest_1200 313 * @tc.name : testPreferencesDeletePreferences006 314 * @tc.desc : DeletePreferences callback interface test 315 * @tc.size : MediumTest 316 * @tc.type : Function 317 * @tc.level : Level 2 318 */ 319 it("testPreferencesDeletePreferences006", 0, async function (done) { 320 dataPreferences.getPreferences(context, { 321 name: NAME1 322 }, (err, preferences) => { 323 if (err) { 324 console.info(TAG + "Get preferences err: " + err); 325 expect(!err).assertTrue(); 326 done(); 327 } else { 328 console.info(TAG + "Get preferences success"); 329 expect(preferences !== null).assertTrue(); 330 try { 331 preferences.put(KEY_TEST_STRING_ELEMENT, "test", (err) => { 332 if (err) { 333 console.info(TAG + "Put preferences err: " + err); 334 return; 335 } 336 console.info(TAG + "Put preferences success"); 337 preferences.get(KEY_TEST_STRING_ELEMENT, "defaultvalue", function (err, ret) { 338 expect("test").assertEqual(ret); 339 dataPreferences.deletePreferences(context, { 340 name: "getPreferencesTest1" 341 }, (err) => { 342 preferences.get(KEY_TEST_STRING_ELEMENT, "defaultvalue", function (err, pre) { 343 expect("test").assertEqual(pre); 344 done(); 345 }); 346 }); 347 }); 348 }); 349 expect(preferences !== null).assertTrue(); 350 done(); 351 } catch (err) { 352 console.info("testPreferencesDeletePreferences006 fail: " + err.message + "code: " + err.code); 353 expect(!err).assertTrue(); 354 done(); 355 } 356 } 357 }); 358 }); 359 360 /** 361 * @tc.number : SUB_DistributeData_CrossPlatform_SDK_Preferences_RemoveJSAPITest_0900 362 * @tc.name : testRemovePreferencesFromCache000 363 * @tc.desc : RemovePreferencesFromCache callback interface test 364 * @tc.size : MediumTest 365 * @tc.type : Function 366 * @tc.level : Level 2 367 */ 368 it("testRemovePreferencesFromCache000", 0, async function (done) { 369 dataPreferences.getPreferences(context, NAME, (err, preferences) => { 370 if (err) { 371 console.info(TAG + "Get preferences err: " + err); 372 expect(!err).assertTrue(); 373 } else { 374 console.info(TAG + "Get preferences success"); 375 expect(preferences !== null).assertTrue(); 376 dataPreferences.removePreferencesFromCache(context, NAME, (err) => { 377 console.info(TAG + "Remove preferences from cache success"); 378 expect(!err).assertTrue(); 379 done(); 380 }); 381 } 382 }); 383 }); 384 385 /** 386 * @tc.number : SUB_DistributeData_CrossPlatform_SDK_Preferences_RemoveJSAPITest_0200 387 * @tc.name : testRemovePreferencesFromCache001 388 * @tc.desc : RemovePreferencesFromCache callback interface test 389 * @tc.size : MediumTest 390 * @tc.type : Function 391 * @tc.level : Level 2 392 */ 393 it("testRemovePreferencesFromCache001", 0, async function (done) { 394 dataPreferences.getPreferences(context, NAME, (err, preferences) => { 395 if (err) { 396 console.info(TAG + "Get preferences err: " + err); 397 expect(!err).assertTrue(); 398 } else { 399 console.info(TAG + "Get preferences success"); 400 expect(preferences !== null).assertTrue(); 401 try { 402 dataPreferences.removePreferencesFromCache(context, null, (err) => { 403 if (err) { 404 console.info(TAG + "Remove preferences from cache error: " + err); 405 expect(!err).assertFail(); 406 done(); 407 } 408 }); 409 expect(!err).assertFail(); 410 done(); 411 } catch (err) { 412 console.info("testRemovePreferencesFromCache001 fail: " + err.message + "code: " + err.code); 413 expect('401').assertEqual(err.code); 414 done(); 415 } 416 } 417 }); 418 }); 419 420 /** 421 * @tc.number : SUB_DistributeData_CrossPlatform_SDK_Preferences_RemoveJSAPITest_0100 422 * @tc.name : testRemovePreferencesFromCache003 423 * @tc.desc : RemovePreferencesFromCache callback interface test 424 * @tc.size : MediumTest 425 * @tc.type : Function 426 * @tc.level : Level 2 427 */ 428 it("testRemovePreferencesFromCache003", 0, async function (done) { 429 dataPreferences.getPreferences(context, NAME, (err, preferences) => { 430 if (err) { 431 console.info(TAG + "Get preferences err: " + err); 432 expect(!err).assertTrue(); 433 done(); 434 } else { 435 console.info(TAG + "Get preferences success"); 436 expect(preferences !== null).assertTrue(); 437 try { 438 preferences.put(KEY_TEST_STRING_ELEMENT, "test", (err) => { 439 if (err) { 440 console.info(TAG + "Put preferences err: " + err); 441 return; 442 } 443 console.info(TAG + "Put preferences success"); 444 preferences.get(KEY_TEST_STRING_ELEMENT, "defaultvalue", function (err, ret) { 445 expect("test").assertEqual(ret); 446 dataPreferences.removePreferencesFromCache(context, "getPreferencesTest1", (err) => { 447 preferences.get(KEY_TEST_STRING_ELEMENT, "defaultvalue", function (err, pre) { 448 expect("test").assertEqual(pre); 449 done(); 450 }); 451 }); 452 }); 453 }); 454 } catch (err) { 455 console.info("testRemovePreferencesFromCache003 fail: " + err.message + "code: " + err.code); 456 expect(!err).assertTrue(); 457 done(); 458 } 459 } 460 }); 461 }); 462 463 /** 464 * @tc.number : SUB_DistributeData_CrossPlatform_SDK_Preferences_RemoveJSAPITest_0400 465 * @tc.name : testRemovePreferencesFromCache004 466 * @tc.desc : RemovePreferencesFromCache callback interface test 467 * @tc.size : MediumTest 468 * @tc.type : Function 469 * @tc.level : Level 2 470 */ 471 it("testRemovePreferencesFromCache004", 0, async function (done) { 472 dataPreferences.getPreferences(context, { 473 name: NAME 474 }, (err, preferences) => { 475 if (err) { 476 console.info(TAG + "Get preferences err: " + err); 477 expect(!err).assertTrue(); 478 } else { 479 console.info(TAG + "Get preferences success"); 480 expect(preferences !== null).assertTrue(); 481 dataPreferences.removePreferencesFromCache(context, { 482 name: NAME 483 }, (err) => { 484 console.info(TAG + "Remove preferences from cache success"); 485 expect(!err).assertTrue(); 486 done(); 487 }); 488 done(); 489 } 490 }); 491 }); 492 493 /** 494 * @tc.number : SUB_DistributeData_CrossPlatform_SDK_Preferences_RemoveJSAPITest_0400 495 * @tc.name : testRemovePreferencesFromCache005 496 * @tc.desc : RemovePreferencesFromCache callback interface test 497 * @tc.size : MediumTest 498 * @tc.type : Function 499 * @tc.level : Level 2 500 */ 501 it("testRemovePreferencesFromCache005", 0, async function (done) { 502 dataPreferences.getPreferences(context, { 503 name: NAME 504 }, (err, preferences) => { 505 if (err) { 506 console.info(TAG + "Get preferences err: " + err); 507 expect(!err).assertTrue(); 508 } else { 509 console.info(TAG + "Get preferences success"); 510 expect(preferences !== null).assertTrue(); 511 try { 512 dataPreferences.removePreferencesFromCache(context, { 513 name: null 514 }, (err) => { 515 expect(!err).assertFail(); 516 done(); 517 }); 518 expect(!err).assertFail(); 519 done(); 520 } catch (err) { 521 console.info("testRemovePreferencesFromCache005 fail: " + err.message + "code: " + err.code); 522 expect("401").assertEqual(err.code); 523 done(); 524 } 525 } 526 }); 527 }); 528 529 /** 530 * @tc.number : SUB_DistributeData_CrossPlatform_SDK_Preferences_RemoveJSAPITest_0500 531 * @tc.name : testRemovePreferencesFromCache006 532 * @tc.desc : RemovePreferencesFromCache callback interface test 533 * @tc.size : MediumTest 534 * @tc.type : Function 535 * @tc.level : Level 2 536 */ 537 it("testRemovePreferencesFromCache006", 0, async function (done) { 538 dataPreferences.getPreferences(context, NAME, (err, preferences) => { 539 if (err) { 540 console.info(TAG + "Get preferences err: " + err); 541 expect(!err).assertTrue(); 542 } else { 543 console.info(TAG + "Get preferences success"); 544 expect(preferences !== null).assertTrue(); 545 try { 546 preferences.put(KEY_TEST_STRING_ELEMENT, "test", (err) => { 547 if (err) { 548 console.info(TAG + "Put preferences err: " + err); 549 return; 550 } 551 console.info(TAG + "Put preferences success"); 552 preferences.get(KEY_TEST_STRING_ELEMENT, "defaultvalue", function (err, ret) { 553 expect("test").assertEqual(ret); 554 dataPreferences.removePreferencesFromCache(context, { 555 name: "getPreferencesTest1" 556 }, (err) => { 557 preferences.get(KEY_TEST_STRING_ELEMENT, "defaultvalue", function (err, pre) { 558 expect("test").assertEqual(pre); 559 done(); 560 }); 561 }); 562 }); 563 }); 564 } catch (err) { 565 console.info("testRemovePreferencesFromCache006 fail: " + err.message + "code: " + err.code); 566 expect(!err).assertTrue(); 567 done(); 568 } 569 } 570 }); 571 }); 572 573 /** 574 * @tc.number : SUB_DistributeData_CrossPlatform_SDK_Preferences_GetJSAPITest_3200 575 * @tc.name : testPreferencesGetDefaultValue0000 576 * @tc.desc : Get defaultValue callback interface test 577 * @tc.size : MediumTest 578 * @tc.type : Method 579 * @tc.level : Level 2 580 */ 581 it("testPreferencesGetDefaultValue0001", 0, async function (done) { 582 await mPreferences.clear(); 583 await mPreferences.get(KEY_TEST_STRING_ELEMENT, "defaultValue", function (err, ret) { 584 expect("defaultValue").assertEqual(ret); 585 done(); 586 }); 587 }); 588 589 /** 590 * @tc.number : SUB_DistributeData_CrossPlatform_SDK_Preferences_GetJSAPITest_3300 591 * @tc.name : testPreferencesGetFloat0000 592 * @tc.desc : Get float callback interface test 593 * @tc.size : MediumTest 594 * @tc.type : Method 595 * @tc.level : Level 2 596 */ 597 it("testPreferencesGetFloat0000", 0, async function (done) { 598 await mPreferences.clear(); 599 await mPreferences.put(KEY_TEST_FLOAT_ELEMENT, 3.0); 600 await mPreferences.get(KEY_TEST_FLOAT_ELEMENT, 0.0, function (err, ret) { 601 expect(3.0).assertEqual(ret); 602 done(); 603 }); 604 }); 605 606 /** 607 * @tc.number : SUB_DistributeData_CrossPlatform_SDK_Preferences_GetJSAPITest_3400 608 * @tc.name : testPreferencesGetInt0000 609 * @tc.desc : Get int callback interface test 610 * @tc.size : MediumTest 611 * @tc.type : Method 612 * @tc.level : Level 2 613 */ 614 it("testPreferencesGetInt0000", 0, async function (done) { 615 await mPreferences.clear(); 616 await mPreferences.put(KEY_TEST_INT_ELEMENT, 3); 617 await mPreferences.get(KEY_TEST_INT_ELEMENT, 0.0, function (err, ret) { 618 expect(3).assertEqual(ret); 619 done(); 620 }); 621 }); 622 623 /** 624 * @tc.number : SUB_DistributeData_CrossPlatform_SDK_Preferences_GetJSAPITest_3500 625 * @tc.name : testPreferencesGetLong0000 626 * @tc.desc : Get long callback interface test 627 * @tc.size : MediumTest 628 * @tc.type : Method 629 * @tc.level : Level 2 630 */ 631 it("testPreferencesGetLong0000", 0, async function (done) { 632 await mPreferences.clear(); 633 await mPreferences.put(KEY_TEST_LONG_ELEMENT, 3); 634 await mPreferences.get(KEY_TEST_LONG_ELEMENT, 0, function (err, ret) { 635 expect(3).assertEqual(ret); 636 done(); 637 }); 638 }); 639 640 /** 641 * @tc.number : SUB_DistributeData_CrossPlatform_SDK_Preferences_GetJSAPITest_3800 642 643 * @tc.name : testPreferencesGetString0000 644 * @tc.desc : Get String callback interface test 645 * @tc.size : MediumTest 646 * @tc.type : Method 647 * @tc.level : Level 2 648 */ 649 it("testPreferencesGetString0000", 0, async function (done) { 650 await mPreferences.clear(); 651 await mPreferences.put(KEY_TEST_STRING_ELEMENT, "test"); 652 await mPreferences.flush(); 653 await mPreferences.get(KEY_TEST_STRING_ELEMENT, "defaultvalue", function (err, ret) { 654 expect("test").assertEqual(ret); 655 done(); 656 }); 657 }); 658 659 /** 660 * @tc.number : SUB_DistributeData_CrossPlatform_SDK_Preferences_GetJSAPITest_3100 661 662 * @tc.name : testPreferencesGetBoolean0000 663 * @tc.desc : Get Boolean callback interface test 664 * @tc.size : MediumTest 665 * @tc.type : Method 666 * @tc.level : Level 2 667 */ 668 it("testPreferencesGetBoolean0000", 0, async function (done) { 669 await mPreferences.clear(); 670 await mPreferences.put(KEY_TEST_STRING_ELEMENT, true); 671 await mPreferences.get(KEY_TEST_BOOLEAN_ELEMENT, true, function (err, ret) { 672 expect(true).assertEqual(ret); 673 done(); 674 }); 675 }); 676 677 /** 678 * @tc.number : SUB_DistributeData_CrossPlatform_SDK_Preferences_GetJSAPITest_3700 679 * @tc.name : testPreferencesGetStringArray0000 680 * @tc.desc : Get StringArray callback interface test 681 * @tc.size : MediumTest 682 * @tc.type : Method 683 * @tc.level : Level 2 684 */ 685 it("testPreferencesGetStringArray0000", 0, async function (done) { 686 await mPreferences.clear(); 687 var stringArr = ["11", "22", "33"]; 688 await mPreferences.put(KEY_TEST_STRING_ARRAY_ELEMENT, stringArr); 689 mPreferences.get(KEY_TEST_STRING_ARRAY_ELEMENT, ["123", "321"], function (err, pre) { 690 for (let i = 0; i < stringArr.length; i++) { 691 expect(stringArr[i]).assertEqual(pre[i]); 692 } 693 done(); 694 }); 695 }); 696 697 /** 698 * @tc.number : SUB_DistributeData_CrossPlatform_SDK_Preferences_GetJSAPITest_3600 699 700 * @tc.name : testPreferencesGetNumArray0000 701 * @tc.desc : Get NumArray callback interface test 702 * @tc.size : MediumTest 703 * @tc.type : Method 704 * @tc.level : Level 2 705 */ 706 it("testPreferencesGetNumArray0000", 0, async function (done) { 707 await mPreferences.clear(); 708 var doubleArr = [11, 22, 33]; 709 await mPreferences.put(KEY_TEST_NUMBER_ARRAY_ELEMENT, doubleArr); 710 mPreferences.get(KEY_TEST_NUMBER_ARRAY_ELEMENT, [123, 321], function (err, pre) { 711 for (let i = 0; i < doubleArr.length; i++) { 712 expect(doubleArr[i]).assertEqual(pre[i]); 713 } 714 done(); 715 }); 716 }); 717 718 /** 719 * @tc.number : SUB_DistributeData_CrossPlatform_SDK_Preferences_GetJSAPITest_3000 720 * @tc.name : testPreferencesGetBoolArray0000 721 * @tc.desc : Get BoolArray callback interface test 722 * @tc.size : MediumTest 723 * @tc.type : Method 724 * @tc.level : Level 2 725 */ 726 it("testPreferencesGetBoolArray0000", 0, async function (done) { 727 await mPreferences.clear(); 728 let boolArr = [true, false, false, true]; 729 await mPreferences.put(KEY_TEST_BOOL_ARRAY_ELEMENT, boolArr); 730 mPreferences.get(KEY_TEST_BOOL_ARRAY_ELEMENT, [true, false], function (err, pre) { 731 for (let i = 0; i < boolArr.length; i++) { 732 expect(boolArr[i]).assertEqual(pre[i]); 733 } 734 done(); 735 }); 736 }); 737 738 /** 739 * @tc.number : SUB_DistributeData_CrossPlatform_SDK_Preferences_GetJSAPITest_1700 740 * @tc.name : testPreferencesGetAll0000 741 * @tc.desc : GetAll callback interface test 742 * @tc.size : MediumTest 743 * @tc.type : Method 744 * @tc.level : Level 2 745 */ 746 it("testPreferencesGetAll0000", 0, async function (done) { 747 await mPreferences.clear(); 748 let doubleArr = [11, 22, 33]; 749 let stringArr = ["11", "22", "33"]; 750 let boolArr = [true, false, false, true]; 751 await mPreferences.put(KEY_TEST_STRING_ARRAY_ELEMENT, stringArr); 752 await mPreferences.put(KEY_TEST_BOOL_ARRAY_ELEMENT, boolArr); 753 await mPreferences.put(KEY_TEST_NUMBER_ARRAY_ELEMENT, doubleArr); 754 await mPreferences.put(KEY_TEST_BOOLEAN_ELEMENT, false); 755 await mPreferences.put(KEY_TEST_STRING_ELEMENT, "123"); 756 await mPreferences.put(KEY_TEST_FLOAT_ELEMENT, 123.1); 757 758 await mPreferences.flush(); 759 760 await mPreferences.getAll(function (err, obj) { 761 expect(false).assertEqual(obj.key_test_boolean); 762 expect("123").assertEqual(obj.key_test_string); 763 expect(123.1).assertEqual(obj.key_test_float); 764 let sArr = obj.key_test_string_array; 765 for (let i = 0; i < sArr.length; i++) { 766 expect(sArr[i]).assertEqual(stringArr[i]); 767 } 768 769 let bArr = obj.key_test_bool_array; 770 for (let i = 0; i < bArr.length; i++) { 771 expect(bArr[i]).assertEqual(boolArr[i]); 772 } 773 774 let nArr = obj.key_test_number_array; 775 for (let i = 0; i < nArr.length; i++) { 776 expect(nArr[i]).assertEqual(doubleArr[i]); 777 } 778 779 done(); 780 }); 781 }); 782 783 /** 784 * @tc.number : SUB_DistributeData_CrossPlatform_SDK_Preferences_PutJSAPITest_2900 785 * @tc.name : testPreferencesPutFloat0000 786 * @tc.desc : Put float callback interface test 787 * @tc.size : MediumTest 788 * @tc.type : Method 789 * @tc.level : Level 2 790 */ 791 it("testPreferencesPutFloat0000", 0, async function (done) { 792 await mPreferences.clear(); 793 await mPreferences.put(KEY_TEST_FLOAT_ELEMENT, 4.0, async function (err, ret) { 794 let pre = await mPreferences.get(KEY_TEST_FLOAT_ELEMENT, 0.0); 795 expect(4.0).assertEqual(pre); 796 await mPreferences.flush(); 797 let pre2 = await mPreferences.get(KEY_TEST_FLOAT_ELEMENT, 0.0); 798 expect(4.0).assertEqual(pre2); 799 done(); 800 }); 801 }); 802 803 /** 804 * @tc.number : SUB_DistributeData_CrossPlatform_SDK_Preferences_PutJSAPITest_3000 805 * @tc.name : testPreferencesPutInt0000 806 * @tc.desc : Put int callback interface test 807 * @tc.size : MediumTest 808 * @tc.type : Method 809 * @tc.level : Level 2 810 */ 811 it("testPreferencesPutInt0000", 0, async function (done) { 812 await mPreferences.clear(); 813 await mPreferences.put(KEY_TEST_INT_ELEMENT, 4, async function (err, ret) { 814 let pre = await mPreferences.get(KEY_TEST_INT_ELEMENT, 0); 815 expect(4).assertEqual(pre); 816 await mPreferences.flush(); 817 let pre2 = await mPreferences.get(KEY_TEST_INT_ELEMENT, 0); 818 expect(4).assertEqual(pre2); 819 done(); 820 }); 821 }); 822 823 /** 824 * @tc.number : SUB_DistributeData_CrossPlatform_SDK_Preferences_PutJSAPITest_3100 825 * @tc.name : testPreferencesPutLong0000 826 * @tc.desc : Put long callback interface test 827 * @tc.size : MediumTest 828 * @tc.type : Method 829 * @tc.level : Level 2 830 */ 831 it("testPreferencesPutLong0000", 0, async function (done) { 832 await mPreferences.clear(); 833 await mPreferences.put(KEY_TEST_LONG_ELEMENT, 4); 834 await mPreferences.put(KEY_TEST_LONG_ELEMENT, 4, async function (err, ret) { 835 let pre = await mPreferences.get(KEY_TEST_LONG_ELEMENT, 0); 836 expect(4).assertEqual(pre); 837 await mPreferences.flush(); 838 let pre2 = await mPreferences.get(KEY_TEST_LONG_ELEMENT, 0); 839 expect(4).assertEqual(pre2); 840 done(); 841 }); 842 }); 843 844 /** 845 * @tc.number : SUB_DistributeData_CrossPlatform_SDK_Preferences_PutJSAPITest_3400 846 * @tc.name : testPreferencesPutString0000 847 * @tc.desc : Put String callback interface test 848 * @tc.size : MediumTest 849 * @tc.type : Method 850 * @tc.level : Level 2 851 */ 852 it("testPreferencesPutString0000", 0, async function (done) { 853 await mPreferences.clear(); 854 await mPreferences.put(KEY_TEST_STRING_ELEMENT, "", async function (err, ret) { 855 let pre = await mPreferences.get(KEY_TEST_STRING_ELEMENT, "defaultvalue"); 856 expect("").assertEqual(pre); 857 await mPreferences.flush(); 858 let pre2 = await mPreferences.get(KEY_TEST_STRING_ELEMENT, "defaultvalue"); 859 expect("").assertEqual(pre2); 860 done(); 861 }); 862 }); 863 864 /** 865 * @tc.number : SUB_DistributeData_CrossPlatform_SDK_Preferences_PutJSAPITest_2800 866 * @tc.name : testPreferencesPutBoolean0000 867 * @tc.desc : Put Boolean callback interface test 868 * @tc.size : MediumTest 869 * @tc.type : Method 870 * @tc.level : Level 2 871 */ 872 it("testPreferencesPutBoolean0000", 0, async function (done) { 873 await mPreferences.clear(); 874 await mPreferences.put(KEY_TEST_BOOLEAN_ELEMENT, true, async function (err, ret) { 875 let pre = await mPreferences.get(KEY_TEST_BOOLEAN_ELEMENT, false); 876 expect(true).assertEqual(pre); 877 await mPreferences.flush(); 878 let pre2 = await mPreferences.get(KEY_TEST_BOOLEAN_ELEMENT, false); 879 expect(true).assertEqual(pre2); 880 done(); 881 }); 882 }); 883 884 /** 885 * @tc.number : SUB_DistributeData_CrossPlatform_SDK_Preferences_PutJSAPITest_3300 886 * @tc.name : testPreferencesPutStringArray0000 887 * @tc.desc : Put StringArray callback interface test 888 * @tc.size : MediumTest 889 * @tc.type : Method 890 * @tc.level : Level 2 891 */ 892 it("testPreferencesPutStringArray0000", 0, async function (done) { 893 await mPreferences.clear(); 894 var stringArr = ["11", "22", "33"]; 895 await mPreferences.put(KEY_TEST_STRING_ARRAY_ELEMENT, stringArr, async function (err, ret) { 896 let pre = await mPreferences.get(KEY_TEST_STRING_ARRAY_ELEMENT, ["123", "321"]); 897 for (let i = 0; i < stringArr.length; i++) { 898 expect(stringArr[i]).assertEqual(pre[i]); 899 } 900 901 done(); 902 }); 903 }); 904 905 /** 906 * @tc.number : SUB_DistributeData_CrossPlatform_SDK_Preferences_PutJSAPITest_3200 907 * @tc.name : testPreferencesPutNumArray0000 908 * @tc.desc : Put NumberArray callback interface test 909 * @tc.size : MediumTest 910 * @tc.type : Method 911 * @tc.level : Level 2 912 */ 913 it("testPreferencesPutNumArray0000", 0, async function (done) { 914 await mPreferences.clear(); 915 var doubleArr = [11, 22, 33]; 916 await mPreferences.put(KEY_TEST_NUMBER_ARRAY_ELEMENT, doubleArr, async function (err, ret) { 917 let pre = await mPreferences.get(KEY_TEST_NUMBER_ARRAY_ELEMENT, [123, 321]); 918 for (let i = 0; i < doubleArr.length; i++) { 919 expect(doubleArr[i]).assertEqual(pre[i]); 920 } 921 922 done(); 923 }); 924 }); 925 926 /** 927 * @tc.number : SUB_DistributeData_CrossPlatform_SDK_Preferences_PutJSAPITest_2700 928 * @tc.name : testPreferencesPutBoolArray0000 929 * @tc.desc : Put BoolArray callback interface test 930 * @tc.size : MediumTest 931 * @tc.type : Method 932 * @tc.level : Level 2 933 */ 934 it("testPreferencesPutBoolArray0000", 0, async function (done) { 935 await mPreferences.clear(); 936 let boolArr = [true, false, false, true]; 937 await mPreferences.put(KEY_TEST_BOOL_ARRAY_ELEMENT, boolArr, async function (err, ret) { 938 let pre = await mPreferences.get(KEY_TEST_BOOL_ARRAY_ELEMENT, [true, false]); 939 for (let i = 0; i < boolArr.length; i++) { 940 expect(boolArr[i]).assertEqual(pre[i]); 941 } 942 943 done(); 944 }); 945 }); 946 947 /** 948 * @tc.number : SUB_DistributeData_CrossPlatform_SDK_Preferences_HasJSAPITest_1400 949 * @tc.name : testPreferencesHasKey0000 950 * @tc.desc : Has float callback interface test 951 * @tc.size : MediumTest 952 * @tc.type : Method 953 * @tc.level : Level 2 954 */ 955 it("testPreferencesHasKey0000", 0, async function (done) { 956 await mPreferences.put(KEY_TEST_FLOAT_ELEMENT, 1.1); 957 await mPreferences.has(KEY_TEST_FLOAT_ELEMENT, function (err, ret) { 958 expect(true).assertEqual(ret); 959 done(); 960 }); 961 }); 962 963 /** 964 * @tc.number : SUB_DistributeData_CrossPlatform_SDK_Preferences_HasJSAPITest_1500 965 * @tc.name : testPreferencesHasKey0001 966 * @tc.desc : Has int callback interface test 967 * @tc.size : MediumTest 968 * @tc.type : Method 969 * @tc.level : Level 2 970 */ 971 it("testPreferencesHasKey0001", 0, async function (done) { 972 await mPreferences.put(KEY_TEST_INT_ELEMENT, 1); 973 await mPreferences.has(KEY_TEST_INT_ELEMENT, function (err, ret) { 974 expect(true).assertEqual(ret); 975 done(); 976 }); 977 }); 978 979 /** 980 * @tc.number : SUB_DistributeData_CrossPlatform_SDK_Preferences_HasJSAPITest_1600 981 * @tc.name : testPreferencesHasKey0002 982 * @tc.desc : Has long callback interface test 983 * @tc.size : MediumTest 984 * @tc.type : Method 985 * @tc.level : Level 2 986 */ 987 it("testPreferencesHasKey0002", 0, async function (done) { 988 await mPreferences.put(KEY_TEST_LONG_ELEMENT, 0); 989 await mPreferences.has(KEY_TEST_LONG_ELEMENT, function (err, ret) { 990 expect(true).assertEqual(ret); 991 done(); 992 }); 993 }); 994 995 /** 996 * @tc.number : SUB_DistributeData_CrossPlatform_SDK_Preferences_HasJSAPITest_1700 997 * @tc.name : testPreferencesHasKey0003 998 * @tc.desc : Has string callback interface test 999 * @tc.size : MediumTest 1000 * @tc.type : Method 1001 * @tc.level : Level 2 1002 */ 1003 it("testPreferencesHasKey0003", 0, async function (done) { 1004 await mPreferences.put(KEY_TEST_STRING_ELEMENT, "test"); 1005 await mPreferences.has(KEY_TEST_STRING_ELEMENT, function (err, ret) { 1006 expect(true).assertEqual(ret); 1007 done(); 1008 }); 1009 }); 1010 1011 /** 1012 * @tc.number : SUB_DistributeData_CrossPlatform_SDK_Preferences_HasJSAPITest_1300 1013 * @tc.name : testPreferencesHasKey0004 1014 * @tc.desc : Has boolean callback interface test 1015 * @tc.size : MediumTest 1016 * @tc.type : Method 1017 * @tc.level : Level 2 1018 */ 1019 it("testPreferencesHasKey0004", 0, async function (done) { 1020 await mPreferences.put(KEY_TEST_BOOLEAN_ELEMENT, false); 1021 await mPreferences.has(KEY_TEST_BOOLEAN_ELEMENT, function (err, ret) { 1022 expect(true).assertEqual(ret); 1023 done(); 1024 }); 1025 }); 1026 1027 /** 1028 * @tc.number : SUB_DistributeData_CrossPlatform_SDK_Preferences_ClearJSAPITest_0500 1029 * @tc.name : testPreferencesClear0000 1030 * @tc.desc : Clear callback interface test 1031 * @tc.size : MediumTest 1032 * @tc.type : Method 1033 * @tc.level : Level 2 1034 */ 1035 it("testPreferencesClear0000", 0, async function (done) { 1036 await mPreferences.clear(); 1037 let doubleArr = [11, 22, 33]; 1038 let stringArr = ["11", "22", "33"]; 1039 let boolArr = [true, false, false, true]; 1040 await mPreferences.put(KEY_TEST_STRING_ARRAY_ELEMENT, stringArr); 1041 await mPreferences.put(KEY_TEST_BOOL_ARRAY_ELEMENT, boolArr); 1042 await mPreferences.put(KEY_TEST_NUMBER_ARRAY_ELEMENT, doubleArr); 1043 await mPreferences.put(KEY_TEST_BOOLEAN_ELEMENT, false); 1044 await mPreferences.put(KEY_TEST_STRING_ELEMENT, "123"); 1045 await mPreferences.put(KEY_TEST_FLOAT_ELEMENT, 123.1); 1046 1047 await mPreferences.flush(); 1048 1049 let obj = mPreferences.getAllSync(); 1050 expect(false).assertEqual(obj.key_test_boolean); 1051 expect("123").assertEqual(obj.key_test_string); 1052 expect(123.1).assertEqual(obj.key_test_float); 1053 let sArr = obj.key_test_string_array; 1054 for (let i = 0; i < sArr.length; i++) { 1055 expect(sArr[i]).assertEqual(stringArr[i]); 1056 } 1057 1058 let bArr = obj.key_test_bool_array; 1059 for (let i = 0; i < bArr.length; i++) { 1060 expect(bArr[i]).assertEqual(boolArr[i]); 1061 } 1062 1063 let nArr = obj.key_test_number_array; 1064 for (let i = 0; i < nArr.length; i++) { 1065 expect(nArr[i]).assertEqual(doubleArr[i]); 1066 } 1067 1068 await mPreferences.clear(async function (err) { 1069 let ret = mPreferences.getAllSync(); 1070 console.log("testPreferencesClear0000 end." + JSON.stringify(ret.key_test_string)); 1071 expect(undefined).assertEqual(ret.key_test_boolean); 1072 expect(undefined).assertEqual(ret.key_test_string); 1073 expect(undefined).assertEqual(ret.key_test_float); 1074 expect(undefined).assertEqual(ret.key_test_string_array); 1075 expect(undefined).assertEqual(ret.key_test_bool_array); 1076 expect(undefined).assertEqual(ret.key_test_number_array); 1077 done(); 1078 }); 1079 }); 1080 1081 /** 1082 * @tc.number : SUB_DistributeData_CrossPlatform_SDK_Preferences_DeleteJSAPITest_2300 1083 * @tc.name : testPreferencesDelete0000 1084 * @tc.desc : Delete callback interface test 1085 * @tc.size : MediumTest 1086 * @tc.type : Method 1087 * @tc.level : Level 2 1088 */ 1089 it("testPreferencesDelete0000", 0, async function (done) { 1090 await mPreferences.put(KEY_TEST_FLOAT_ELEMENT, 3.0); 1091 let ret = await mPreferences.get(KEY_TEST_FLOAT_ELEMENT, 0.0); 1092 expect(3.0).assertEqual(ret); 1093 await mPreferences.flush(); 1094 mPreferences.delete(KEY_TEST_FLOAT_ELEMENT, async (err, ret) => { 1095 if (err) { 1096 console.info("Failed to delete the key KEY_TEST_STRING_ELEMENT. code =" + err.code + ", message =" + err.message); 1097 expect(!err).assertTrue(); 1098 } 1099 console.info("Succeeded in deleting the key KEY_TEST_STRING_ELEMENT."); 1100 let per = await mPreferences.get(KEY_TEST_FLOAT_ELEMENT, 0.0); 1101 expect(0.0).assertEqual(per); 1102 done(); 1103 }); 1104 }); 1105 1106 /** 1107 * @tc.number : SUB_DistributeData_CrossPlatform_SDK_Preferences_FlushJSAPITest_0400 1108 * @tc.name : testPreferencesFlush0000 1109 * @tc.desc : Flush callback interface test 1110 * @tc.size : MediumTest 1111 * @tc.type : Method 1112 * @tc.level : Level 2 1113 */ 1114 it("testPreferencesFlush0000", 0, async function (done) { 1115 await mPreferences.clear(); 1116 await mPreferences.put(KEY_TEST_STRING_ELEMENT, "test"); 1117 await mPreferences.flush(); 1118 mPreferences.flush(async function (err, val) { 1119 if (err) { 1120 console.info("Failed to flush. code =" + err.code + ", message =" + err.message); 1121 expect(!err).assertTrue(); 1122 } 1123 console.info("Succeeded in flushing."); 1124 expect(err === undefined).assertTrue(); 1125 let per = await mPreferences.get(KEY_TEST_STRING_ELEMENT, "defaultvalue"); 1126 expect("test").assertEqual(per); 1127 done(); 1128 }); 1129 }); 1130 1131 /** 1132 * @tc.number : SUB_DistributeData_CrossPlatform_SDK_Preferences_JSAPITest_0200 1133 * @tc.name : testPreferencesCallback0000 1134 * @tc.desc : Clear、Put、Get、Flush String callback interface test 1135 * @tc.size : MediumTest 1136 * @tc.type : Method 1137 * @tc.level : Level 2 1138 */ 1139 it("testPreferencesCallback0000", 0, function (done) { 1140 console.log(TAG + "testPreferencesCallback0000 begin."); 1141 mPreferences.clear(function (err, val) { 1142 if (err) { 1143 expect(!err).assertTrue(); 1144 } 1145 mPreferences.put(KEY_TEST_STRING_ELEMENT, "123", function (err) { 1146 if (err) { 1147 expect(!err).assertTrue(); 1148 } 1149 mPreferences.get(KEY_TEST_STRING_ELEMENT, "defaultvalue", function (err, pre) { 1150 if (err) { 1151 expect(!err).assertTrue(); 1152 } 1153 expect("123").assertEqual(pre); 1154 mPreferences.flush(function (err) { 1155 if (err) { 1156 expect(!err).assertTrue(); 1157 } 1158 mPreferences.get(KEY_TEST_STRING_ELEMENT, "defaultvalue", function (err, pre2) { 1159 if (err) { 1160 expect(!err).assertTrue(); 1161 } 1162 expect("123").assertEqual(pre2); 1163 done(); 1164 console.log(TAG + "testPreferencesCallback0000 end."); 1165 }); 1166 }); 1167 }); 1168 }); 1169 }); 1170 }); 1171 }); 1172} 1173