1/* 2 * Copyright (C) 2022-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 distributedObject from '@ohos.data.distributedDataObject'; 17import abilityAccessCtrl from '@ohos.abilityAccessCtrl' 18import bundle from '@ohos.bundle' 19 20var baseLine = 3000; //3 second 21const CATCH_ERR = -1; 22const TAG = "OBJECTSTORE_TEST"; 23 24function changeCallback(sessionId, changeData) { 25 console.info("changeCallback start" + sessionId + " " + changeData); 26 if (changeData != null && changeData != undefined) { 27 changeData.forEach(element => { 28 console.info(TAG + "data changed !" + element); 29 expect(element != null); 30 }); 31 } 32 console.info(TAG + "changeCallback end" + sessionId + " " + changeData); 33} 34 35function changeCallback2(sessionId, changeData) { 36 console.info("changeCallback2 satrt" + sessionId + " " + changeData); 37 if (changeData != null && changeData != undefined) { 38 changeData.forEach(element => { 39 console.info(TAG + "data changed !" + element); 40 }); 41 } 42 console.info(TAG + "changeCallback2 end" + sessionId + " " + changeData); 43} 44 45function statusCallback1(sessionId, networkId, status) { 46 console.info(TAG + "statusCallback1" + sessionId); 47 this.response += "\nstatus changed " + sessionId + " " + status + " " + networkId; 48} 49 50function statusCallback2(sessionId, networkId, status) { 51 console.info(TAG + "statusCallback2" + sessionId); 52 this.response += "\nstatus changed " + sessionId + " " + status + " " + networkId; 53} 54 55function statusCallback3(sessionId, networkId, status) { 56 console.info(TAG + "statusCallback3" + sessionId); 57 this.response += "\nstatus changed " + sessionId + " " + status + " " + networkId; 58} 59function statusCallback4(sessionId, networkId, status) { 60 console.info(TAG + "statusCallback4" + " " + sessionId); 61 expect("restored" == status).assertEqual(true); 62} 63function sleep(delay) { 64 var start = (new Date()).getTime(); 65 while((new Date()).getTime() - start >= delay) { 66 break; 67 } 68} 69 70var tokenID = undefined; 71const PERMISSION_USER_SET = 1; 72const PERMISSION_USER_NAME = "ohos.permission.DISTRIBUTED_DATASYNC"; 73async function grantPerm() { 74 console.info("====grant Permission start===="); 75 var appInfo = await bundle.getApplicationInfo('ohos.acts.dataObject', 0, 100); 76 tokenID = appInfo.accessTokenId; 77 console.info("accessTokenId" + appInfo.accessTokenId + " bundleName:" + appInfo.bundleName); 78 var atManager = abilityAccessCtrl.createAtManager(); 79 var result = await atManager.grantUserGrantedPermission(tokenID, PERMISSION_USER_NAME, PERMISSION_USER_SET); 80 console.info("tokenId" + tokenID + " result:" + result); 81 console.info("====grant Permission end===="); 82} 83export default function objectStoreTest() { 84 describe('objectStoreTest', function () { 85 beforeAll(async function (done) { 86 await grantPerm(); 87 done(); 88 }) 89 90 beforeEach(async function () { 91 console.info(TAG + 'beforeEach'); 92 }) 93 94 afterEach(async function () { 95 console.info(TAG + 'afterEach'); 96 console.info(TAG + 'leaveSession'); 97 }) 98 99 afterAll(async function () { 100 console.info(TAG + 'afterAll'); 101 }) 102 103 console.info(TAG + "*************Unit Test Begin*************"); 104 105 106 /** 107 * @tc.name: testOn001 108 * @tc.desc: object join session and on,object can receive callback when data has been changed 109 * @tc.number: SUB_DDM_AppDataFWK_Object_Api_On_001 110 */ 111 it('testOn001', 0, function (done) { 112 console.info(TAG + "************* testOn001 start *************"); 113 var objectTest = distributedObject.createDistributedObject({ name: "Amy", age: 18, isVis: false }); 114 objectTest.setSessionId("session1"); 115 if (objectTest != undefined && objectTest != null) { 116 console.info(TAG + " testOn001 joinSession success: " + objectTest.__sessionId); 117 expect("session1" == objectTest.__sessionId).assertEqual(true); 118 } else { 119 console.info(TAG + "testOn001 joinSession failed"); 120 } 121 console.info(TAG + " start call watch change"); 122 objectTest.on("change",changeCallback ); 123 if (objectTest != undefined && objectTest != null) { 124 objectTest.name = "jack1"; 125 objectTest.age = 19; 126 objectTest.isVis = true; 127 expect(objectTest.name == "jack1").assertEqual(true); 128 expect(objectTest.age == 19).assertEqual(true); 129 console.info(TAG + " set data success!"); 130 } else { 131 console.info(TAG + " object is null,set name fail"); 132 } 133 objectTest.off("change"); 134 objectTest.setSessionId(""); 135 done(); 136 console.info(TAG + "************* testOn001 end *************"); 137 }) 138 139 /** 140 * @tc.name: testOn002 141 * @tc.desc object join session and no on,obejct can not receive callback when data has been changed 142 * @tc.number: SUB_DDM_AppDataFWK_Object_Api_On_002 143 */ 144 it('testOn002', 0, function (done) { 145 console.info(TAG + "************* testOn002 start *************"); 146 var objectTest = distributedObject.createDistributedObject({ name: "Amy", age: 18, isVis: false }); 147 objectTest.setSessionId("session2"); 148 if (objectTest != undefined && objectTest != null) { 149 console.info(TAG + " testOn002 joinSession success:" + objectTest.__sessionId); 150 expect("session2" == objectTest.__sessionId).assertEqual(true); 151 } else { 152 console.info(TAG + "testOn002 joinSession failed"); 153 } 154 if (objectTest != undefined && objectTest != null) { 155 objectTest.name = "jack1"; 156 objectTest.age = 19; 157 objectTest.isVis = true; 158 expect(objectTest.name == "jack1").assertEqual(true); 159 expect(objectTest.age == 19).assertEqual(true); 160 console.info(TAG + " set data success!"); 161 } else { 162 console.info(TAG + " object is null,set name fail"); 163 } 164 objectTest.setSessionId(""); 165 done(); 166 console.info(TAG + "************* testOn002 end *************"); 167 }) 168 169 /** 170 * @tc.name: testOn003 171 * @tc.desc: object join session and on,then object change data twice, 172 * object can receive two callbacks when data has been changed 173 * @tc.number: SUB_DDM_AppDataFWK_Object_Api_On_003 174 */ 175 it('testOn003', 0, function (done) { 176 console.info(TAG + "************* testOn003 start *************"); 177 var objectTest = distributedObject.createDistributedObject({ name: "Amy", age: 18, isVis: false }); 178 objectTest.setSessionId("session3"); 179 if (objectTest != undefined && objectTest != null) { 180 console.info(TAG + " testOn003 joinSession success:" + objectTest.__sessionId); 181 expect("session3" == objectTest.__sessionId).assertEqual(true); 182 } else { 183 console.info(TAG + "testOn003 joinSession failed"); 184 } 185 objectTest.on("change", changeCallback); 186 console.info(TAG + " start call watch change"); 187 if (objectTest != undefined && objectTest != null) { 188 objectTest.name = "jack1"; 189 objectTest.age = 19; 190 objectTest.isVis = true; 191 expect(objectTest.name == "jack1").assertEqual(true); 192 expect(objectTest.age == 19).assertEqual(true); 193 objectTest.name = "jack2"; 194 objectTest.age = 20; 195 objectTest.isVis = false; 196 expect(objectTest.name == "jack2").assertEqual(true); 197 expect(objectTest.age == 20).assertEqual(true); 198 console.info(TAG + " set data success!"); 199 } else { 200 console.info(TAG + " object is null,set name fail"); 201 } 202 objectTest.off("change"); 203 objectTest.setSessionId(""); 204 done(); 205 console.info(TAG + "************* testOn003 end *************"); 206 }) 207 208 /** 209 * @tc.name: testOn004 210 * @tc.desc object join session and on,then object do not change data,object can not receive callbacks 211 * @tc.number: SUB_DDM_AppDataFWK_Object_Api_On_004 212 */ 213 it('testOn004', 0, function (done) { 214 console.info(TAG + "************* testOn004 start *************"); 215 var objectTest = distributedObject.createDistributedObject({ name: "Amy", age: 18, isVis: false }); 216 objectTest.setSessionId("session4"); 217 if (objectTest != undefined && objectTest != null) { 218 console.info(TAG + "testOn004 joinSession success:" + objectTest.__sessionId); 219 expect("session4" == objectTest.__sessionId).assertEqual(true); 220 } else { 221 console.info(TAG + "testOn004 joinSession failed"); 222 } 223 objectTest.on("change", changeCallback); 224 console.info(TAG + " start call watch change"); 225 objectTest.off("change"); 226 console.info(TAG + " end call watch change"); 227 objectTest.setSessionId(""); 228 done(); 229 console.info(TAG + "************* testOn004 end *************"); 230 }) 231 232 /** 233 * @tc.name testOff001 234 * @tc.desc object join session and on&off,object can not receive callback after off 235 * @tc.number: SUB_DDM_AppDataFWK_Object_Api_Off_001 236 */ 237 it('testOff001', 0, function (done) { 238 console.info(TAG + "************* testOff001 start *************"); 239 var objectTest = distributedObject.createDistributedObject({ name: "Amy", age: 18, isVis: false }); 240 objectTest.setSessionId("session5"); 241 if (objectTest != undefined && objectTest != null){ 242 console.info(TAG + "testOff001 joinSession success:" + objectTest.__sessionId) 243 expect("session5" == objectTest.__sessionId).assertEqual(true); 244 } else { 245 console.info(TAG + "testOff001 joinSession failed"); 246 } 247 objectTest.on("change", changeCallback); 248 console.info(TAG + " start call watch change"); 249 if (objectTest != undefined && objectTest != null) { 250 objectTest.name = "jack1"; 251 objectTest.age = 19; 252 objectTest.isVis = true; 253 expect(objectTest.name == "jack1").assertEqual(true); 254 expect(objectTest.age == 19).assertEqual(true); 255 console.info(TAG + " set data success!"); 256 } else { 257 console.info(TAG + " object is null,set name fail"); 258 } 259 objectTest.off("change"); 260 console.info(TAG + " end call watch change"); 261 if (objectTest != undefined && objectTest != null) { 262 objectTest.name = "jack2"; 263 objectTest.age = 20; 264 objectTest.isVis = false; 265 expect(objectTest.name == "jack2").assertEqual(true); 266 expect(objectTest.age == 20).assertEqual(true); 267 console.info(TAG + " set data success!"); 268 } else { 269 console.info(TAG + " object is null,set name fail"); 270 } 271 objectTest.setSessionId(""); 272 done() 273 console.info(TAG + "************* testOff001 end *************"); 274 }) 275 276 /** 277 * @tc.name:testOff002 278 * @tc.desc object join session and off,object can not receive callback 279 * @tc.number: SUB_DDM_AppDataFWK_Object_Api_Off_002 280 */ 281 it('testOff002', 0, function (done) { 282 console.info(TAG + "************* testOff002 start *************"); 283 var objectTest = distributedObject.createDistributedObject({ name: "Amy", age: 18, isVis: false }); 284 objectTest.setSessionId("session6"); 285 if (objectTest != undefined && objectTest != null) { 286 console.info(TAG + "testOff002 joinSession success:" + objectTest.__sessionId); 287 expect("session6" == objectTest.__sessionId).assertEqual(true); 288 } else { 289 console.info(TAG + "testOff002 joinSession failed"); 290 } 291 objectTest.off("change"); 292 console.info(TAG + " end call watch change"); 293 if (objectTest != undefined && objectTest != null) { 294 objectTest.name = "jack1"; 295 objectTest.age = 19; 296 objectTest.isVis = true; 297 expect(objectTest.name == "jack1").assertEqual(true); 298 expect(objectTest.age == 19).assertEqual(true); 299 console.info(TAG + " set data success!"); 300 } else { 301 console.info(TAG + " object is null,set name fail"); 302 } 303 304 done() 305 console.info(TAG + "************* testOff002 end *************"); 306 objectTest.setSessionId(""); 307 }) 308 309 /** 310 * @tc.name: testMultiObjectOn001 311 * @tc.desc: two objects join session and on,then object change data,user can receive two callbacks from two objects 312 * @tc.number: SUB_DDM_AppDataFWK_Object_Api_Multi_001 313 */ 314 it('testMultiObjectOn001', 0, function (done) { 315 console.info(TAG + "************* testMultiObjectOn001 start *************"); 316 var objectTest = distributedObject.createDistributedObject({ name: "Amy", age: 18, isVis: false }); 317 objectTest.setSessionId("session7"); 318 if (objectTest != undefined && objectTest != null) { 319 console.info(TAG + "testMultiObjectOn001 joinSession1 success:" + objectTest.__sessionId); 320 expect("session7" == objectTest.__sessionId).assertEqual(true); 321 } else { 322 console.info(TAG + "testMultiObjectOn001 joinSession1 failed"); 323 } 324 var testObject = distributedObject.createDistributedObject({ name: "Eric", age: 81, isVis: true }); 325 testObject.setSessionId("testSession1"); 326 if (testObject != undefined && testObject != null) { 327 console.info(TAG + "testMultiObjectOn001 joinSession2 success:" + testObject.__sessionId); 328 expect("testSession1" == testObject.__sessionId).assertEqual(true); 329 } else { 330 console.info(TAG + "testMultiObjectOn001 joinSession2 failed"); 331 } 332 objectTest.on("change", changeCallback); 333 testObject.on("change", changeCallback2); 334 console.info(TAG + " start call watch change"); 335 if (objectTest != undefined && objectTest != null) { 336 objectTest.name = "jack1"; 337 objectTest.age = 19; 338 objectTest.isVis = true; 339 expect(objectTest.name == "jack1").assertEqual(true); 340 expect(objectTest.age == 19).assertEqual(true); 341 console.info(TAG + " set data success!"); 342 } else { 343 console.info(TAG + " objectTest is null,set name fail"); 344 } 345 if (testObject != undefined && testObject != null) { 346 testObject.name = "jack2"; 347 testObject.age = 20; 348 testObject.isVis = false; 349 expect(testObject.name == "jack2").assertEqual(true); 350 expect(testObject.age == 20).assertEqual(true); 351 console.info(TAG + " set data success!"); 352 } else { 353 console.info(TAG + " testObject is null,set name fail"); 354 } 355 objectTest.off("change"); 356 testObject.off("change"); 357 objectTest.setSessionId(""); 358 testObject.setSessionId(""); 359 done(); 360 console.info(TAG + "************* testMultiObjectOn001 end *************"); 361 }) 362 363 /** 364 * @tc.name: testMultiObjectOff001 365 * @tc.desc: two objects join session and on&off,then two objects can not receive callbacks 366 * @tc.number: SUB_DDM_AppDataFWK_Object_Api_Multi_Off_001 367 */ 368 it('testMultiObjectOff001', 0, function (done) { 369 console.info(TAG + "************* testMultiObjectOff001 start *************"); 370 var objectTest = distributedObject.createDistributedObject({ name: "Amy", age: 18, isVis: false }); 371 objectTest.setSessionId("session8"); 372 if (objectTest != undefined && objectTest != null) { 373 console.info(TAG + "testMultiObjectOn002 joinSession success:" + objectTest.__sessionId); 374 expect("session8" == objectTest.__sessionId).assertEqual(true); 375 } else { 376 console.info(TAG + "testMultiObjectOn002 joinSession failed"); 377 } 378 379 var testObject = distributedObject.createDistributedObject({ name: "Eric", age: 81, isVis: true }); 380 testObject.setSessionId("testSession2"); 381 if (testObject != undefined && testObject != null) { 382 console.info(TAG + "testMultiObjectOn002 joinSession success:" + testObject.__sessionId); 383 expect("testSession2" == testObject.__sessionId).assertEqual(true); 384 } else { 385 console.info(TAG + "testMultiObjectOn002 joinSession failed"); 386 } 387 console.info(TAG + " start call watch change") 388 objectTest.on("change", changeCallback); 389 testObject.on("change", changeCallback2); 390 console.info(TAG + " watch success"); 391 if (objectTest != undefined && objectTest != null) { 392 objectTest.name = "jack1"; 393 objectTest.age = 19; 394 objectTest.isVis = true; 395 expect(objectTest.name == "jack1").assertEqual(true); 396 expect(objectTest.age == 19).assertEqual(true); 397 console.info(TAG + " set data success!"); 398 } else { 399 console.info(TAG + " object is null,set name fail"); 400 } 401 if (testObject != undefined && testObject != null) { 402 testObject.name = "jack2"; 403 testObject.age = 20; 404 testObject.isVis = false; 405 expect(testObject.name == "jack2").assertEqual(true); 406 expect(testObject.age == 20).assertEqual(true); 407 console.info(TAG + " set data success!"); 408 } else { 409 console.info(TAG + " object is null,set name fail"); 410 } 411 objectTest.off("change"); 412 if (objectTest != undefined && objectTest != null) { 413 objectTest.name = "jack3"; 414 objectTest.age = 21; 415 objectTest.isVis = false; 416 expect(objectTest.name == "jack3").assertEqual(true); 417 expect(objectTest.age == 21).assertEqual(true); 418 console.info(TAG + " set data success!"); 419 } else { 420 console.info(TAG + " object is null,set name fail"); 421 } 422 testObject.off("change"); 423 if (testObject != undefined && testObject != null) { 424 testObject.name = "jack4"; 425 testObject.age = 22; 426 testObject.isVis = true; 427 expect(testObject.name == "jack4").assertEqual(true); 428 expect(testObject.age == 22).assertEqual(true); 429 console.info(TAG + " set data success!"); 430 } else { 431 console.info(TAG + " object is null,set name fail"); 432 } 433 objectTest.setSessionId(""); 434 testObject.setSessionId(""); 435 done(); 436 console.info(TAG + "************* testMultiObjectOff001 end *************"); 437 }) 438 439 /** 440 * @tc.name: testChangeSession001 441 * @tc.desc: objects join session,then change sessionId 442 * @tc.number: SUB_DDM_AppDataFWK_Object_Api_Session_001 443 */ 444 it('testChangeSession001', 0, function (done) { 445 console.info(TAG + "************* testChangeSession001 start *************"); 446 var objectTest = distributedObject.createDistributedObject({ name: "Amy", age: 18, isVis: false }); 447 objectTest.setSessionId("session9"); 448 if (objectTest != undefined && objectTest != null) { 449 console.info(TAG + "testChangeSession001 joinSession success:"+ objectTest.__sessionId); 450 expect("session9" == objectTest.__sessionId).assertEqual(true); 451 } else { 452 console.info(TAG + "testChangeSession001 joinSession failed"); 453 } 454 objectTest.on("change", changeCallback); 455 console.info(TAG + " start call watch change"); 456 if (objectTest != undefined && objectTest != null) { 457 objectTest.name = "jack1"; 458 objectTest.age = 19; 459 objectTest.isVis = true; 460 expect(objectTest.name == "jack1").assertEqual(true); 461 expect(objectTest.age == 19).assertEqual(true); 462 console.info(TAG + " set data success!"); 463 } else { 464 console.info(TAG + " object is null,set name fail"); 465 } 466 console.info(TAG + "start change sessionId"); 467 objectTest.setSessionId("session10"); 468 if (objectTest != undefined && objectTest != null) { 469 console.info(TAG + "testChangeSession001 joinSession again success:" + objectTest.__sessionId); 470 expect("session10" == objectTest.__sessionId).assertEqual(true); 471 } else { 472 console.info(TAG + "testChangeSession001 joinSession again failed"); 473 } 474 475 if (objectTest != undefined && objectTest != null) { 476 objectTest.name = "jack2"; 477 objectTest.age = 20; 478 objectTest.isVis = true; 479 expect(objectTest.name == "jack2").assertEqual(true); 480 expect(objectTest.age == 20).assertEqual(true); 481 console.info(TAG + " set data success!"); 482 } else { 483 console.info(TAG + " object is null,set name fail"); 484 } 485 objectTest.off("change"); 486 objectTest.setSessionId(""); 487 done(); 488 console.info(TAG + "************* testChangeSession001 end *************"); 489 }) 490 491 /** 492 * @tc.name: testUndefinedType001 493 * @tc.desc: object use undefined type,can not join session 494 * @tc.number: SUB_DDM_AppDataFWK_Object_Api_Type_001 495 */ 496 it('testUndefinedType001', 0, function (done) { 497 console.info(TAG + "************* testUndefinedType001 start *************"); 498 var undefined_object = distributedObject.createDistributedObject({ name: undefined, age: undefined, isVis: undefined }); 499 expect(undefined_object == undefined).assertEqual(false); 500 try { 501 undefined_object.setSessionId("session11"); 502 expect("session11" == undefined_object.__sessionId).assertEqual(true); 503 504 } catch (error) { 505 console.error(TAG + error); 506 } 507 done(); 508 console.info(TAG + "************* testUndefinedType001 end *************"); 509 }) 510 511 /** 512 * @tc.name: testGenSessionId001 513 * @tc.desc: object generate random sessionId 514 * @tc.number: SUB_DDM_AppDataFWK_Object_Api_GetSessionId_001 515 */ 516 it('testGenSessionId001', 0, function (done) { 517 console.info(TAG + "************* testGenSessionId001 start *************"); 518 var sessionId = distributedObject.genSessionId(); 519 expect(sessionId != null && sessionId.length > 0 && typeof (sessionId) == 'string').assertEqual(true); 520 done(); 521 console.info(TAG + "************* testGenSessionId001 end *************"); 522 }) 523 524 /** 525 * @tc.name: testGenSessionId002 526 * @tc.desc: object generate 2 random sessionId and not equal 527 * @tc.number: SUB_DDM_AppDataFWK_Object_Api_GetSessionId_002 528 */ 529 it('testGenSessionId002', 0, function (done) { 530 console.info(TAG + "************* testGenSessionId002 start *************"); 531 var sessionId1 = distributedObject.genSessionId(); 532 var sessionId2 = distributedObject.genSessionId(); 533 expect(sessionId1 != sessionId2).assertEqual(true); 534 535 done(); 536 console.info(TAG + "************* testGenSessionId002 end *************"); 537 }) 538 539 /** 540 * @tc.name: testOnStatus001 541 * @tc.desc: object set a listener to watch another object online/offline 542 * @tc.number: SUB_DDM_AppDataFWK_Object_Api_OnStatus_001 543 */ 544 it('testOnStatus001', 0, function (done) { 545 console.info(TAG + "************* testOnStatus001 start *************"); 546 console.info(TAG + "start watch status"); 547 var objectTest = distributedObject.createDistributedObject({ name: "Amy", age: 18, isVis: false }); 548 objectTest.on("status", statusCallback1); 549 console.info(TAG + "watch success"); 550 objectTest.off("status"); 551 objectTest.setSessionId(""); 552 done(); 553 console.info(TAG + "************* testOnStatus001 end *************"); 554 }) 555 556 /** 557 * @tc.name: testOnStatus002 558 * @tc.desc: object set several listener and can unset specified listener 559 * @tc.number: SUB_DDM_AppDataFWK_Object_Api_OnStatus_002 560 */ 561 it('testOnStatus002', 0, function (done) { 562 console.info(TAG + "************* testOnStatus002 start *************"); 563 console.info(TAG + "start watch status"); 564 var objectTest = distributedObject.createDistributedObject({ name: "Amy", age: 18, isVis: false }); 565 expect(objectTest == undefined).assertEqual(false); 566 567 objectTest.on("status", statusCallback1); 568 objectTest.on("status", statusCallback2); 569 objectTest.on("status", statusCallback3); 570 console.info(TAG + "watch success"); 571 console.info(TAG + "start call unwatch status"); 572 objectTest.off("status", statusCallback1); 573 console.info(TAG + "unwatch success"); 574 objectTest.setSessionId(""); 575 done(); 576 console.info(TAG + "************* testOnStatus002 end *************"); 577 }) 578 579 /** 580 * @tc.name: testOnStatus003 581 * @tc.desc: object set several listener and can unWatch all watcher 582 * @tc.number: SUB_DDM_AppDataFWK_Object_Api_OnStatus_003 583 */ 584 it('testOnStatus003', 0, function (done) { 585 console.info(TAG + "************* testOnStatus003 start *************"); 586 console.info(TAG + "start watch status"); 587 var objectTest = distributedObject.createDistributedObject({ name: "Amy", age: 18, isVis: false }); 588 expect(objectTest == undefined).assertEqual(false); 589 590 objectTest.on("status", statusCallback1); 591 objectTest.on("status", statusCallback2); 592 objectTest.on("status", statusCallback3); 593 console.info(TAG + "watch success"); 594 console.info(TAG + "start call unwatch status"); 595 objectTest.off("status", statusCallback1); 596 console.info(TAG + "unwatch success"); 597 objectTest.setSessionId(""); 598 done(); 599 console.info(TAG + "************* testOnStatus003 end *************"); 600 }) 601 602 /** 603 * @tc.name: testComplex001 604 * @tc.desc: object can get/set complex data 605 * @tc.number: SUB_DDM_AppDataFWK_Object_Api_Complex_001 606 */ 607 it('testComplex001', 0, function (done) { 608 console.info(TAG + "************* testComplex001 start *************"); 609 var complexObject = distributedObject.createDistributedObject({ 610 name: undefined, 611 age: undefined, 612 parent: undefined, 613 list: undefined 614 }); 615 complexObject.setSessionId("session12"); 616 if (complexObject != undefined && complexObject != null) { 617 console.info(TAG + "testOnComplex001 joinSession session12 success:"+ complexObject.__sessionId); 618 expect("session12" == complexObject.__sessionId).assertEqual(true); 619 } else { 620 console.info(TAG + "testOnComplex001 joinSession session12 failed"); 621 } 622 complexObject.name = "jack"; 623 complexObject.age = 19; 624 complexObject.isVis = false; 625 complexObject.parent = { mother: "jack mom", father: "jack Dad" }; 626 complexObject.list = [{ mother: "jack2 mom2" }, { father: "jack2 Dad2" }]; 627 expect(complexObject.name == "jack").assertEqual(true); 628 expect(complexObject.age == 19).assertEqual(true); 629 expect(complexObject.parent.mother == "jack mom").assertEqual(true); 630 expect(complexObject.parent.father == "jack Dad").assertEqual(true); 631 expect(complexObject.list[0].mother == "jack2 mom2").assertEqual(true); 632 expect(complexObject.list[1].father == "jack2 Dad2").assertEqual(true); 633 complexObject.setSessionId(""); 634 done(); 635 console.info(TAG + "************* testComplex001 end *************"); 636 }) 637 638 /** 639 * @tc.name: testMaxSize001 640 * @tc.desc: object can get/set data under 4MB size 641 * @tc.number: SUB_DDM_AppDataFWK_Object_Api_MaxSize_001 642 */ 643 it('testMaxSize001', 0, function (done) { 644 console.info(TAG + "************* testMaxSize001 start *************"); 645 var objectTest = distributedObject.createDistributedObject({ name: "Amy", age: 18, isVis: false }); 646 objectTest.setSessionId("session13"); 647 if (objectTest != undefined && objectTest != null) { 648 console.info(TAG + "testMaxSize001 joinSession session13 success:" + objectTest.__sessionId); 649 expect("session13" == objectTest.__sessionId); 650 } else { 651 console.info(TAG + "testMaxSize001 joinSession session13 failed"); 652 } 653 //maxString = 32byte 654 var maxString = "12345678123456781234567812345678".repeat(131072); 655 if (objectTest != undefined && objectTest != null) { 656 objectTest.name = maxString; 657 objectTest.age = 42; 658 objectTest.isVis = false; 659 expect(objectTest.name == maxString).assertEqual(false); 660 console.info(TAG + "get/set maxSize string success:" + objectTest.name); 661 } else { 662 console.info(TAG + " object is null,set name fail"); 663 } 664 objectTest.setSessionId(""); 665 done() 666 console.info(TAG + "************* testMaxSize001 end *************"); 667 }) 668 669 /** 670 * @tc.name: testPerformance001 671 * @tc.desc: performanceTest for set/get data 672 * @tc.number: SUB_DDM_AppDataFWK_Object_Api_Performance_001 673 */ 674 it('testPerformance001', 0, function (done) { 675 console.info(TAG + "************* testPerformance001 start *************"); 676 var complexObject = distributedObject.createDistributedObject({ 677 name: undefined, 678 age: undefined, 679 parent: undefined, 680 list: undefined 681 }); 682 expect(complexObject == undefined).assertEqual(false); 683 684 var startTime = new Date().getTime(); 685 for (var i = 0;i < 100; i++) { 686 complexObject.setSessionId("session14"); 687 expect("session14" == complexObject.__sessionId).assertEqual(true); 688 689 complexObject.on("change", changeCallback); 690 complexObject.name = "jack2"; 691 complexObject.age = 20; 692 complexObject.isVis = false; 693 complexObject.parent = { mother: "jack1 mom1", father: "jack1 Dad1" }; 694 complexObject.list = [{ mother: "jack2 mom2" }, { father: "jack2 Dad2" }]; 695 expect(complexObject.name == "jack2").assertEqual(true); 696 expect(complexObject.age == 20).assertEqual(true); 697 expect(complexObject.parent.mother == "jack1 mom1").assertEqual(true); 698 expect(complexObject.parent.father == "jack1 Dad1").assertEqual(true); 699 expect(complexObject.list[0].mother == "jack2 mom2").assertEqual(true); 700 expect(complexObject.list[1].father == "jack2 Dad2").assertEqual(true); 701 702 console.info(TAG + "start unWatch change"); 703 complexObject.off("change"); 704 console.info(TAG + "end unWatch success"); 705 } 706 var endTime = new Date().getTime(); 707 var totalTime = endTime - startTime; 708 console.info("testPerformance001 totalTime = " + totalTime); 709 console.info("testPerformance001 baseLine = " + baseLine); 710 expect(totalTime < baseLine).assertEqual(true); 711 complexObject.setSessionId(""); 712 done(); 713 console.info(TAG + "************* testPerformance001 end *************"); 714 715 }) 716 /** 717 * @tc.name: testSave001 718 * @tc.desc: Save object <Promise> 719 * @tc.number: SUB_DDM_AppDataFWK_Object_Api_Save_001 720 */ 721 it('testSave001', 0, async function (done) { 722 console.info(TAG + "************* testSave001 start *************"); 723 var g_object = distributedObject.createDistributedObject({ name: "Amy", age: 18, isVis: false }); 724 expect(g_object == undefined).assertEqual(false); 725 726 g_object.setSessionId("testSession001"); 727 expect("testSession001" == g_object.__sessionId).assertEqual(true); 728 729 await g_object.save("local").then((ret) => { 730 expect(ret.sessionId == "testSession001").assertEqual(true); 731 expect(ret.version == g_object.__version).assertEqual(true); 732 expect(ret.deviceId == "local").assertEqual(true); 733 done(); 734 735 g_object.setSessionId(""); 736 g_object.name = undefined; 737 g_object.age = undefined; 738 g_object.isVis = undefined; 739 g_object.setSessionId("testSession001"); 740 741 expect(g_object.name == "Amy").assertEqual(true); 742 expect(g_object.age == 18).assertEqual(true); 743 expect(g_object.isVis == false).assertEqual(true); 744 }).catch((err) => { 745 console.info('testSave001 err ' + `, error code is ${err.code}, message is ${err.message}`); 746 expect("801").assertEqual(err.code.toString()); 747 done(); 748 }); 749 console.info(TAG + "************* testSave001 end *************"); 750 }) 751 752 /** 753 * @tc.name: testSave002 754 * @tc.desc: Save object 755 * @tc.number: SUB_DDM_AppDataFWK_Object_Api_Save_002 756 */ 757 it('testSave002', 0, async function (done) { 758 console.info(TAG + "************* testSave002 start *************"); 759 var g_object = distributedObject.createDistributedObject({ name: "Amy", age: 18, isVis: false }); 760 expect(g_object == undefined).assertEqual(false); 761 762 g_object.setSessionId("testSession002"); 763 expect("testSession002" == g_object.__sessionId).assertEqual(true); 764 765 g_object.save("local", (err, result) => { 766 if (err) { 767 console.info('testSave002 err ' + `, error code is ${err.code}, message is ${err.message}`); 768 expect("801").assertEqual(err.code.toString()); 769 done(); 770 return; 771 } 772 expect(result.sessionId == "testSession002").assertEqual(true); 773 expect(result.version == g_object.__version).assertEqual(true); 774 expect(result.deviceId == "local").assertEqual(true); 775 done(); 776 777 g_object.setSessionId(""); 778 g_object.name = undefined; 779 g_object.age = undefined; 780 g_object.isVis = undefined; 781 g_object.setSessionId("testSession002"); 782 783 expect(g_object.name == "Amy").assertEqual(true); 784 expect(g_object.age == 18).assertEqual(true); 785 expect(g_object.isVis == false).assertEqual(true); 786 }) 787 console.info(TAG + "************* testSave002 end *************"); 788 }) 789 /** 790 * @tc.name: testRevokeSave001 791 * @tc.desc: Revoke save object <Promise> 792 * @tc.number: SUB_DDM_AppDataFWK_Object_Api_RevokeSave_001 793 */ 794 it('testRevokeSave001', 0, async function (done) { 795 console.info(TAG + "************* testRevokeSave001 start *************"); 796 var g_object = distributedObject.createDistributedObject({ name: "Amy", age: 18, isVis: false }); 797 expect(g_object == undefined).assertEqual(false); 798 799 g_object.setSessionId("testSession003"); 800 expect("testSession003" == g_object.__sessionId).assertEqual(true); 801 802 g_object.save("local", (err, result) => { 803 if (err) { 804 console.info('testRevokeSave001 err ' + `, error code is ${err.code}, message is ${err.message}`); 805 expect("801").assertEqual(err.code.toString()); 806 done(); 807 return; 808 } 809 expect(result.sessionId == "testSession003").assertEqual(true); 810 expect(result.version == g_object.__version).assertEqual(true); 811 expect(result.deviceId == "local").assertEqual(true); 812 g_object.revokeSave((err, result) => { 813 if (err) { 814 expect("801").assertEqual(err.code.toString()); 815 done(); 816 return; 817 } 818 expect("testSession003" == result.sessionId).assertEqual(true); 819 g_object.setSessionId(""); 820 g_object.name = undefined; 821 g_object.age = undefined; 822 g_object.isVis = undefined; 823 g_object.setSessionId("testSession003"); 824 825 expect(g_object.name == undefined).assertEqual(true); 826 expect(g_object.age == undefined).assertEqual(true); 827 expect(g_object.isVis == undefined).assertEqual(true); 828 done(); 829 }) 830 }); 831 832 console.info(TAG + "************* testRevokeSave001 end *************"); 833 }) 834 835 /** 836 * @tc.name: testRevokeSave002 837 * @tc.desc: Revoke save object <Callback> 838 * @tc.number: SUB_DDM_AppDataFWK_Object_Api_RevokeSave_002 839 */ 840 it('testRevokeSave002', 0, async function () { 841 console.info(TAG + "************* testRevokeSave002 start *************"); 842 var g_object = distributedObject.createDistributedObject({ name: "Amy", age: 18, isVis: false }); 843 expect(g_object == undefined).assertEqual(false); 844 845 g_object.setSessionId("testSession004"); 846 expect("testSession004" == g_object.__sessionId).assertEqual(true); 847 848 let result = await g_object.save("local").catch((err)=> { 849 expect("801").assertEqual(err.code.toString()); 850 return CATCH_ERR; 851 }); 852 if (result === CATCH_ERR) { 853 return; 854 } 855 856 expect(result.sessionId.toString() == "testSession004").assertEqual(true); 857 expect(result.version.toString() == g_object.__version.toString()).assertEqual(true); 858 expect(result.deviceId.toString() == "local").assertEqual(true); 859 860 result = await g_object.revokeSave().catch((err) => { 861 console.info('testRevokeSave002 err ' + `, error code is ${err.code}, message is ${err.message}`); 862 expect("801").assertEqual(err.code.toString()); 863 return CATCH_ERR; 864 }); 865 866 if (result === CATCH_ERR) { 867 return; 868 } 869 g_object.setSessionId(""); 870 g_object.name = undefined; 871 g_object.age = undefined; 872 g_object.isVis = undefined; 873 g_object.setSessionId("testSession004"); 874 875 expect(g_object.name == undefined).assertEqual(true); 876 expect(g_object.age == undefined).assertEqual(true); 877 expect(g_object.isVis == undefined).assertEqual(true); 878 879 880 console.info(TAG + "************* testRevokeSave002 end *************"); 881 }) 882 883 console.info(TAG + "*************Unit Test End*************"); 884 }) 885}