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