ObjectStoreJsunit.test.js 32.0 KB
Newer Older
Z
zhangmaowen 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14
/*
 * Copyright (C) 2022 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
1
18834416147 已提交
15
import { describe, beforeAll, beforeEach, afterEach, afterAll, it, expect } from 'deccjsunit/index'
Z
zhangmaowen 已提交
16
import distributedObject from '@ohos.data.distributedDataObject';
L
liangzhenyu123 已提交
17 18
import abilityAccessCtrl from '@ohos.abilityAccessCtrl'
import bundle from '@ohos.bundle'
Z
zhangmaowen 已提交
19 20

var baseLine = 500; //0.5 second
L
liangzhenyu123 已提交
21
var gObject;
Z
zhangmaowen 已提交
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
const TAG = "OBJECTSTORE_TEST";

function changeCallback(sessionId, changeData) {
    console.info("get init change111" + sessionId + " " + changeData);
    if (changeData != null && changeData != undefined) {
        changeData.forEach(element => {
            console.info(TAG + "data changed !" + element);
        });
    }
    console.info(TAG + "get init change111 end" + sessionId + " " + changeData);
}

function changeCallback2(sessionId, changeData) {
    console.info("get init change222" + sessionId + " " + changeData);
    if (changeData != null && changeData != undefined) {
        changeData.forEach(element => {
            console.info(TAG + "data changed !" + element);
        });
    }
    console.info(TAG + "get init change222 end" + sessionId + " " + changeData);
}

function statusCallback1(sessionId, networkId, status) {
    console.info(TAG + "test init change111" + sessionId);
    this.response += "\nstatus changed " + sessionId + " " + status + " " + networkId;
}

function statusCallback2(sessionId, networkId, status) {
    console.info(TAG + "test init change222" + sessionId);
    this.response += "\nstatus changed " + sessionId + " " + status + " " + networkId;
}

function statusCallback3(sessionId, networkId, status) {
    console.info(TAG + "test init change333" + sessionId);
    this.response += "\nstatus changed " + sessionId + " " + status + " " + networkId;
}
L
liangzhenyu123 已提交
58 59
function sleep(delay) {
    var start = (new Date()).getTime();
L
liangzhenyu123 已提交
60 61
    while((new Date()).getTime() - start >= delay) {
        break;
L
liangzhenyu123 已提交
62 63 64 65 66 67 68
    }
}

var tokenID = undefined;
const TIMEOUT = 2000;
const PERMISSION_USER_SET = 1;
const PERMISSION_USER_NAME = "ohos.permission.DISTRIBUTED_DATASYNC";
Z
zhangmaowen 已提交
69 70

describe('objectStoreTest', function () {
L
liangzhenyu123 已提交
71 72 73 74
    beforeAll(async function (done) {
        console.info("====>beforeAll start====");
        var appInfo = await bundle.getApplicationInfo('ohos.acts.distributeddataObject', 0, 100);
        tokenID = appInfo.accessTokenId;
L
liangzhenyu123 已提交
75
        console.info(" bundleName:" + appInfo.name);
L
liangzhenyu123 已提交
76 77
        var atManager = abilityAccessCtrl.createAtManager();
        var result = await atManager.grantUserGrantedPermission(tokenID, PERMISSION_USER_NAME, PERMISSION_USER_SET);
L
liangzhenyu123 已提交
78
        console.info(" result:" + result);
L
liangzhenyu123 已提交
79 80 81
        sleep(TIMEOUT);
        console.debug("====>beforeAll end====");
        done();
Z
zhangmaowen 已提交
82 83
    })

L
liangzhenyu123 已提交
84 85
    beforeEach(async function () {
        // await grantPerm();
Z
zhangmaowen 已提交
86 87 88
        console.info(TAG + 'beforeEach')
    })

L
liangzhenyu123 已提交
89
    afterEach(async function () {
Z
zhangmaowen 已提交
90
        console.info(TAG + 'afterEach')
L
liangzhenyu123 已提交
91
        gObject.setSessionId("");
Z
zhangmaowen 已提交
92 93
    })

L
liangzhenyu123 已提交
94
    afterAll(async function () {
Z
zhangmaowen 已提交
95 96 97 98 99 100 101 102 103
        console.info(TAG + 'afterAll')
    })

    console.log(TAG + "*************Unit Test Begin*************");


    /**
     * @tc.name: testOn001
     * @tc.desc: object join session and on,object can receive callback when data has been changed
Y
yanglei1217 已提交
104
     * @tc.number: SUB_DDM_AppDataFWK_Object_Api_On_001
Z
zhangmaowen 已提交
105 106 107 108 109 110
     */
    it('testOn001', 0, function (done) {
        console.log(TAG + "************* testOn001 start *************");
        var objectTest = distributedObject.createDistributedObject({ name: "Amy", age: 18, isVis: false });
        objectTest.setSessionId("session1");
        if (objectTest != undefined && objectTest != null) {
Y
yanglei1217 已提交
111
            expect("session1" == objectTest.__sessionId);
Z
zhangmaowen 已提交
112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134
        } else {
            console.log(TAG + "testOn001 joinSession failed");
        }
        console.info(TAG + " start call watch change");
        objectTest.on("change", changeCallback);
        if (objectTest != undefined && objectTest != null) {
            objectTest.name = "jack1";
            objectTest.age = 19;
            objectTest.isVis = true;
            expect(objectTest.name == "jack1");
            expect(objectTest.age == 19);
            console.info(TAG + " set data success!");
        } else {
            console.info(TAG + " object is null,set name fail");
        }

        done()
        console.log(TAG + "************* testOn001 end *************");
    })

    /**
     * @tc.name: testOn002
     * @tc.desc object join session and no on,obejct can not receive callback when data has been changed
Y
yanglei1217 已提交
135
     * @tc.number: SUB_DDM_AppDataFWK_Object_Api_On_002
Z
zhangmaowen 已提交
136 137 138 139 140 141
     */
    it('testOn002', 0, function (done) {
        console.log(TAG + "************* testOn002 start *************");
        var objectTest = distributedObject.createDistributedObject({ name: "Amy", age: 18, isVis: false });
        objectTest.setSessionId("session2");
        if (objectTest != undefined && objectTest != null) {
Y
yanglei1217 已提交
142
            expect("session2" == objectTest.__sessionId);
Z
zhangmaowen 已提交
143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164
        } else {
            console.log(TAG + "testOn002 joinSession failed");
        }
        if (objectTest != undefined && objectTest != null) {
            objectTest.name = "jack1";
            objectTest.age = 19;
            objectTest.isVis = true;
            expect(objectTest.name == "jack1");
            expect(objectTest.age == 19);
            console.info(TAG + " set data success!");
        } else {
            console.info(TAG + " object is null,set name fail");
        }

        done()
        console.log(TAG + "************* testOn002 end *************");
    })

    /**
     * @tc.name: testOn003
     * @tc.desc: object join session and on,then object change data twice,
     *           object can receive two callbacks when data has been changed
Y
yanglei1217 已提交
165
     * @tc.number: SUB_DDM_AppDataFWK_Object_Api_On_003
Z
zhangmaowen 已提交
166 167 168 169 170 171
     */
    it('testOn003', 0, function (done) {
        console.log(TAG + "************* testOn003 start *************");
        var objectTest = distributedObject.createDistributedObject({ name: "Amy", age: 18, isVis: false });
        objectTest.setSessionId("session3");
        if (objectTest != undefined && objectTest != null) {
Y
yanglei1217 已提交
172
            expect("session3" == objectTest.__sessionId);
Z
zhangmaowen 已提交
173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200
        } else {
            console.log(TAG + "testOn003 joinSession failed");
        }
        objectTest.on("change", changeCallback);
        console.info(TAG + " start call watch change");
        if (objectTest != undefined && objectTest != null) {
            objectTest.name = "jack1";
            objectTest.age = 19;
            objectTest.isVis = true;
            expect(objectTest.name == "jack1");
            expect(objectTest.age == 19);
            objectTest.name = "jack2";
            objectTest.age = 20;
            objectTest.isVis = false;
            expect(objectTest.name == "jack2");
            expect(objectTest.age == 20);
            console.info(TAG + " set data success!");
        } else {
            console.info(TAG + " object is null,set name fail");
        }

        done()
        console.log(TAG + "************* testOn003 end *************");
    })

    /**
     * @tc.name: testOn004
     * @tc.desc object join session and on,then object do not change data,object can not receive callbacks
Y
yanglei1217 已提交
201
     * @tc.number: SUB_DDM_AppDataFWK_Object_Api_On_004
Z
zhangmaowen 已提交
202 203 204 205 206 207
     */
    it('testOn004', 0, function (done) {
        console.log(TAG + "************* testOn004 start *************");
        var objectTest = distributedObject.createDistributedObject({ name: "Amy", age: 18, isVis: false });
        objectTest.setSessionId("session4");
        if (objectTest != undefined && objectTest != null) {
Y
yanglei1217 已提交
208
            expect("session4" == objectTest.__sessionId);
Z
zhangmaowen 已提交
209 210 211 212 213 214 215 216 217 218 219 220 221 222
        } else {
            console.log(TAG + "testOn004 joinSession failed");
        }
        objectTest.on("change", changeCallback);
        console.info(TAG + " start call watch change");
        console.info(TAG + " end call watch change");

        done()
        console.log(TAG + "************* testOn004 end *************");
    })

    /**
     * @tc.name testOff001
     * @tc.desc object join session and on&off,object can not receive callback after off
Y
yanglei1217 已提交
223
     * @tc.number: SUB_DDM_AppDataFWK_Object_Api_Off_001
Z
zhangmaowen 已提交
224 225 226 227 228 229
     */
    it('testOff001', 0, function (done) {
        console.log(TAG + "************* testOff001 start *************");
        var objectTest = distributedObject.createDistributedObject({ name: "Amy", age: 18, isVis: false });
        objectTest.setSessionId("session5");
        if (objectTest != undefined && objectTest != null) {
Y
yanglei1217 已提交
230
            expect("session5" == objectTest.__sessionId);
Z
zhangmaowen 已提交
231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265
        } else {
            console.log(TAG + "testOff001 joinSession failed");
        }
        objectTest.on("change", changeCallback);
        console.info(TAG + " start call watch change");
        if (objectTest != undefined && objectTest != null) {
            objectTest.name = "jack1";
            objectTest.age = 19;
            objectTest.isVis = true;
            expect(objectTest.name == "jack1");
            expect(objectTest.age == 19);
            console.info(TAG + " set data success!");
        } else {
            console.info(TAG + " object is null,set name fail");
        }
        objectTest.off("change");
        console.info(TAG + " end call watch change");
        if (objectTest != undefined && objectTest != null) {
            objectTest.name = "jack2";
            objectTest.age = 20;
            objectTest.isVis = false;
            expect(objectTest.name == "jack2");
            expect(objectTest.age == 20);
            console.info(TAG + " set data success!");
        } else {
            console.info(TAG + " object is null,set name fail");
        }

        done()
        console.log(TAG + "************* testOff001 end *************");
    })

    /**
     * @tc.name:testOff002
     * @tc.desc object join session and off,object can not receive callback
Y
yanglei1217 已提交
266
     * @tc.number: SUB_DDM_AppDataFWK_Object_Api_Off_002
Z
zhangmaowen 已提交
267 268 269 270 271 272
     */
    it('testOff002', 0, function (done) {
        console.log(TAG + "************* testOff002 start *************");
        var objectTest = distributedObject.createDistributedObject({ name: "Amy", age: 18, isVis: false });
        objectTest.setSessionId("session6");
        if (objectTest != undefined && objectTest != null) {
Y
yanglei1217 已提交
273
            expect("session6" == objectTest.__sessionId);
Z
zhangmaowen 已提交
274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296
        } else {
            console.log(TAG + "testOff002 joinSession failed");
        }
        objectTest.off("change");
        console.info(TAG + " end call watch change");
        if (objectTest != undefined && objectTest != null) {
            objectTest.name = "jack1";
            objectTest.age = 19;
            objectTest.isVis = true;
            expect(objectTest.name == "jack1");
            expect(objectTest.age == 19);
            console.info(TAG + " set data success!");
        } else {
            console.info(TAG + " object is null,set name fail");
        }

        done()
        console.log(TAG + "************* testOff002 end *************");
    })

    /**
     * @tc.name: testMultiObjectOn001
     * @tc.desc: two objects join session and on,then object change data,user can receive two callbacks from two objects
Y
yanglei1217 已提交
297
     * @tc.number: SUB_DDM_AppDataFWK_Object_Api_Multi_001
Z
zhangmaowen 已提交
298 299 300 301 302 303
     */
    it('testMultiObjectOn001', 0, function (done) {
        console.log(TAG + "************* testMultiObjectOn001 start *************");
        var objectTest = distributedObject.createDistributedObject({ name: "Amy", age: 18, isVis: false });
        objectTest.setSessionId("session7");
        if (objectTest != undefined && objectTest != null) {
Y
yanglei1217 已提交
304
            expect("session7" == objectTest.__sessionId);
Z
zhangmaowen 已提交
305 306 307 308 309 310
        } else {
            console.log(TAG + "testMultiObjectOn001 joinSession failed");
        }
        var testObject = distributedObject.createDistributedObject({ name: "Eric", age: 81, isVis: true });
        testObject.setSessionId("testSession1");
        if (testObject != undefined && testObject != null) {
1
18834416147 已提交
311
            expect("testSession1" == testObject.__sessionId);
Z
zhangmaowen 已提交
312 313 314 315 316 317 318 319 320 321 322 323 324 325
        } else {
            console.log(TAG + "testMultiObjectOn001 joinSession failed");
        }
        objectTest.on("change", changeCallback);
        testObject.on("change", changeCallback2);
        console.info(TAG + " start call watch change");
        if (objectTest != undefined && objectTest != null) {
            objectTest.name = "jack1";
            objectTest.age = 19;
            objectTest.isVis = true;
            expect(objectTest.name == "jack1");
            expect(objectTest.age == 19);
            console.info(TAG + " set data success!");
        } else {
1
18834416147 已提交
326 327 328 329 330 331 332 333 334 335 336
            console.info(TAG + " objectTest is null,set name fail");
        }
        if (testObject != undefined && testObject != null) {
            testObject.name = "jack2";
            testObject.age = 20;
            testObject.isVis = false;
            expect(testObject.name == "jack2");
            expect(testObject.age == 20);
            console.info(TAG + " set data success!");
        } else {
            console.info(TAG + " testObject is null,set name fail");
Z
zhangmaowen 已提交
337 338 339 340 341 342
        }

        done()
        console.log(TAG + "************* testMultiObjectOn001 end *************");
    })

1
18834416147 已提交
343 344 345 346 347
    /**
         * @tc.name: testMultiObjectOff001
         * @tc.desc: two objects join session and on&off,then two objects can not receive callbacks
         * @tc.number: SUB_DDM_AppDataFWK_Object_Api_Multi_Off_001
         */
Y
yanglei1217 已提交
348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413
    it('testMultiObjectOff001', 0, function (done) {
        console.log(TAG + "************* testMultiObjectOff001 start *************");
        var objectTest = distributedObject.createDistributedObject({ name: "Amy", age: 18, isVis: false });
        objectTest.setSessionId("session8");
        if (objectTest != undefined && objectTest != null) {
            expect("session8" == objectTest.__sessionId);
        } else {
            console.log(TAG + "testMultiObjectOn002 joinSession failed");
        }
        var testObject = distributedObject.createDistributedObject({ name: "Eric", age: 81, isVis: true });
        testObject.setSessionId("testSession2");
        if (testObject != undefined && testObject != null) {
            expect("testSession2" == testObject.__sessionId);
        } else {
            console.log(TAG + "testMultiObjectOn002 joinSession failed");
        }
        console.log(TAG + " start call watch change")
        objectTest.on("change", changeCallback);
        testObject.on("change", changeCallback2);
        console.info(TAG + " watch success");
        if (objectTest != undefined && objectTest != null) {
            objectTest.name = "jack1";
            objectTest.age = 19;
            objectTest.isVis = true;
            expect(objectTest.name == "jack1");
            expect(objectTest.age == 19);
            console.info(TAG + " set data success!");
        } else {
            console.info(TAG + " object is null,set name fail");
        }
        if (testObject != undefined && testObject != null) {
            testObject.name = "jack2";
            testObject.age = 20;
            testObject.isVis = false;
            expect(testObject.name == "jack2");
            expect(testObject.age == 20);
            console.info(TAG + " set data success!");
        } else {
            console.info(TAG + " object is null,set name fail");
        }
        objectTest.off("change");
        if (objectTest != undefined && objectTest != null) {
            objectTest.name = "jack3";
            objectTest.age = 21;
            objectTest.isVis = false;
            expect(objectTest.name == "jack3");
            expect(objectTest.age == 21);
            console.info(TAG + " set data success!");
        } else {
            console.info(TAG + " object is null,set name fail");
        }
        testObject.off("change");
        if (testObject != undefined && testObject != null) {
            testObject.name = "jack4";
            testObject.age = 22;
            testObject.isVis = true;
            expect(testObject.name == "jack4");
            expect(testObject.age == 22);
            console.info(TAG + " set data success!");
        } else {
            console.info(TAG + " object is null,set name fail");
        }

        done()
        console.log(TAG + "************* testMultiObjectOff001 end *************");
    })
1
18834416147 已提交
414 415

    /**
Y
yanglei1217 已提交
416 417 418 419
     * @tc.name: testChangeSession001
     * @tc.desc: objects join session,then change sessionId
     * @tc.number: SUB_DDM_AppDataFWK_Object_Api_Session_001
     */
1
18834416147 已提交
420
    it('testChangeSession001', 0, function (done) {
Y
yanglei1217 已提交
421 422 423 424 425 426 427 428
        console.log(TAG + "************* testChangeSession001 start *************");
        var objectTest = distributedObject.createDistributedObject({ name: "Amy", age: 18, isVis: false });
        objectTest.setSessionId("session9");
        if (objectTest != undefined && objectTest != null) {
            expect("session9" == objectTest.__sessionId);
        } else {
            console.log(TAG + "testChangeSession001 joinSession failed");
        }
1
18834416147 已提交
429
        console.log(TAG + "start change sessionId");
Y
yanglei1217 已提交
430
        setTimeout(() => objectTest.setSessionId("session9"), 1000);
1
18834416147 已提交
431
        if (objectTest != undefined && objectTest != null) {
Y
yanglei1217 已提交
432 433 434 435 436 437 438 439 440
            expect("session9" == objectTest.__sessionId);
        } else {
            console.log(TAG + "testChangeSession001 joinSession again failed");
        }

        done()
        console.log(TAG + "************* testChangeSession001 end *************");
    })

Z
zhangmaowen 已提交
441 442 443
    /**
     * @tc.name: testUndefinedType001
     * @tc.desc: object use undefined type,can not join session
Y
yanglei1217 已提交
444
     * @tc.number: SUB_DDM_AppDataFWK_Object_Api_Type_001
Z
zhangmaowen 已提交
445 446 447 448 449 450 451
     */
    it('testUndefinedType001', 0, function (done) {
        console.log(TAG + "************* testUndefinedType001 start *************");
        var object1 = distributedObject.createDistributedObject({ name: undefined, age: undefined, isVis: undefined });
        try {
            object1.setSessionId("session11");
            if (object1 != undefined && object1 != null) {
Y
yanglei1217 已提交
452
                expect("session11" == object1.__sessionId);
Z
zhangmaowen 已提交
453
            } else {
Y
yanglei1217 已提交
454
                console.log(TAG + "testUndefinedType001 joinSession session11 failed");
Z
zhangmaowen 已提交
455 456 457 458 459 460 461 462 463 464 465 466
            }
        } catch (error) {
            console.error(TAG + error);
        }

        done()
        console.log(TAG + "************* testUndefinedType001 end *************");
    })

    /**
     * @tc.name: testGenSessionId001
     * @tc.desc: object generate random sessionId
Y
yanglei1217 已提交
467
     * @tc.number: SUB_DDM_AppDataFWK_Object_Api_GetSessionId_001
Z
zhangmaowen 已提交
468 469 470 471 472 473 474 475 476 477 478 479 480
     */
    it('testGenSessionId001', 0, function (done) {
        console.log(TAG + "************* testGenSessionId001 start *************");
        var sessionId = distributedObject.genSessionId();
        expect(sessionId != null && sessionId.length > 0 && typeof (sessionId) == 'string');

        done()
        console.log(TAG + "************* testGenSessionId001 end *************");
    })

    /**
     * @tc.name: testGenSessionId002
     * @tc.desc: object generate 2 random sessionId and not equal
Y
yanglei1217 已提交
481
     * @tc.number: SUB_DDM_AppDataFWK_Object_Api_GetSessionId_002
Z
zhangmaowen 已提交
482 483
     */
    it('testGenSessionId002', 0, function (done) {
Y
yanglei1217 已提交
484
        console.log(TAG + "************* testGenSessionId002 start *************");
Z
zhangmaowen 已提交
485 486 487 488 489 490 491 492 493 494 495
        var sessionId1 = distributedObject.genSessionId();
        var sessionId2 = distributedObject.genSessionId();
        expect(sessionId1 != sessionId2);

        done()
        console.log(TAG + "************* testGenSessionId002 end *************");
    })

    /**
     * @tc.name: testOnStatus001
     * @tc.desc: object set a listener to watch another object online/offline
Y
yanglei1217 已提交
496
     * @tc.number: SUB_DDM_AppDataFWK_Object_Api_OnStatus_001
Z
zhangmaowen 已提交
497 498 499 500 501 502 503 504 505 506 507 508 509 510 511
     */
    it('testOnStatus001', 0, function (done) {
        console.log(TAG + "************* testOnStatus001 start *************");
        console.log(TAG + "start watch status");
        var objectTest = distributedObject.createDistributedObject({ name: "Amy", age: 18, isVis: false });
        objectTest.on("status", statusCallback1);
        console.log(TAG + "watch success");

        done()
        console.log(TAG + "************* testOnStatus001 end *************");
    })

    /**
     * @tc.name: testOnStatus002
     * @tc.desc: object set several listener and can unset specified listener
Y
yanglei1217 已提交
512
     * @tc.number: SUB_DDM_AppDataFWK_Object_Api_OnStatus_002
Z
zhangmaowen 已提交
513 514
     */
    it('testOnStatus002', 0, function (done) {
Y
yanglei1217 已提交
515
        console.log(TAG + "************* testOnStatus002 start *************");
Z
zhangmaowen 已提交
516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532
        console.log(TAG + "start watch status");
        var objectTest = distributedObject.createDistributedObject({ name: "Amy", age: 18, isVis: false });
        objectTest.on("status", statusCallback1);
        objectTest.on("status", statusCallback2);
        objectTest.on("status", statusCallback3);
        console.log(TAG + "watch success");
        console.log(TAG + "start call unwatch status");
        objectTest.off("status", statusCallback1);
        console.log(TAG + "unwatch success");

        done()
        console.log(TAG + "************* testOnStatus002 end *************");
    })

    /**
     * @tc.name: testOnStatus003
     * @tc.desc: object set several listener and can unWatch all watcher
Y
yanglei1217 已提交
533
     * @tc.number: SUB_DDM_AppDataFWK_Object_Api_OnStatus_003
Z
zhangmaowen 已提交
534
     */
Y
yanglei1217 已提交
535 536
    it('testOnStatus003', 0, function (done) {
        console.log(TAG + "************* testOnStatus003 start *************");
Z
zhangmaowen 已提交
537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553
        console.log(TAG + "start watch status");
        var objectTest = distributedObject.createDistributedObject({ name: "Amy", age: 18, isVis: false });
        objectTest.on("status", statusCallback1);
        objectTest.on("status", statusCallback2);
        objectTest.on("status", statusCallback3);
        console.log(TAG + "watch success");
        console.log(TAG + "start call unwatch status");
        objectTest.off("status");
        console.log(TAG + "unwatch success");

        done()
        console.log(TAG + "************* testOnStatus003 end *************");
    })

    /**
     * @tc.name: testComplex001
     * @tc.desc: object can get/set complex data
Y
yanglei1217 已提交
554
     * @tc.number: SUB_DDM_AppDataFWK_Object_Api_Complex_001
Z
zhangmaowen 已提交
555 556 557 558 559 560 561 562 563 564 565
     */
    it('testComplex001', 0, function (done) {
        console.log(TAG + "************* testComplex001 start *************");
        var complexObject = distributedObject.createDistributedObject({
            name: undefined,
            age: undefined,
            parent: undefined,
            list: undefined
        });
        complexObject.setSessionId("session12");
        if (complexObject != undefined && complexObject != null) {
Y
yanglei1217 已提交
566
            expect("session12" == complexObject.__sessionId);
Z
zhangmaowen 已提交
567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586
        } else {
            console.log(TAG + "testOnComplex001 joinSession session12 failed");
        }
        complexObject.name = "jack";
        complexObject.age = 19;
        complexObject.isVis = false;
        complexObject.parent = { mother: "jack mom", father: "jack Dad" };
        complexObject.list = [{ mother: "jack mom" }, { father: "jack Dad" }];
        expect(complexObject.name == "jack");
        expect(complexObject.age == 19);
        expect(complexObject.parent == { mother: "jack1 mom", father: "jack1 Dad" });
        expect(complexObject.list == [{ mother: "jack1 mom", father: "jack1 Dad" }]);

        done()
        console.log(TAG + "************* testComplex001 end *************");
    })

    /**
     * @tc.name: testMaxSize001
     * @tc.desc: object can get/set data under 4MB size
Y
yanglei1217 已提交
587
     * @tc.number: SUB_DDM_AppDataFWK_Object_Api_MaxSize_001
Z
zhangmaowen 已提交
588 589 590 591 592 593
     */
    it('testMaxSize001', 0, function (done) {
        console.log(TAG + "************* testMaxSize001 start *************");
        var objectTest = distributedObject.createDistributedObject({ name: "Amy", age: 18, isVis: false });
        objectTest.setSessionId("session13");
        if (objectTest != undefined && objectTest != null) {
Y
yanglei1217 已提交
594
            expect("session13" == objectTest.__sessionId);
Z
zhangmaowen 已提交
595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616
        } else {
            console.log(TAG + "testMaxSize001 joinSession session13 failed");
        }
        //maxString = 32byte
        var maxString = "12345678123456781234567812345678".repeat(131072);
        if (objectTest != undefined && objectTest != null) {
            objectTest.name = maxString;
            objectTest.age = 42;
            objectTest.isVis = false;
            expect(objectTest.name == maxString);
            console.log(TAG + "get/set maxSize string success");
        } else {
            console.info(TAG + " object is null,set name fail");
        }

        done()
        console.log(TAG + "************* testMaxSize001 end *************");
    })

    /**
     * @tc.name: testPerformance001
     * @tc.desc: performanceTest for set/get data
Y
yanglei1217 已提交
617
     * @tc.number: SUB_DDM_AppDataFWK_Object_Api_Performance_001
Z
zhangmaowen 已提交
618 619 620 621 622 623 624 625 626 627 628 629 630 631 632
     */
    it('testPerformance001', 0, function (done) {
        console.log(TAG + "************* testPerformance001 start *************");
        var complexObject = distributedObject.createDistributedObject({
            name: undefined,
            age: undefined,
            parent: undefined,
            list: undefined
        });
        var st1;
        var totalTime = 0;
        var setSessionIdTime = 0;
        var setTime = 0;
        var onTime = 0;
        var offTime = 0;
1
18834416147 已提交
633
        for (var i = 0; i < 100; i++) {
Z
zhangmaowen 已提交
634 635 636 637
            st1 = Date.now();
            complexObject.setSessionId("session14");
            setSessionIdTime += Date.now() - st1;
            if (complexObject != undefined && complexObject != null) {
Y
yanglei1217 已提交
638
                expect("session14" == complexObject.__sessionId);
Z
zhangmaowen 已提交
639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674
            } else {
                console.log(TAG + "testPerformance001 joinSession session14 failed");
            }
            console.info(TAG + " start call watch change");
            st1 = Date.now();
            complexObject.on("change", changeCallback);
            onTime += Date.now() - st1;
            console.info(TAG + "on change success");
            st1 = Date.now();
            complexObject.name = "jack2";
            complexObject.age = 20;
            complexObject.isVis = false;
            complexObject.parent = { mother: "jack1 mom1", father: "jack1 Dad1" };
            complexObject.list = [{ mother: "jack1 mom1" }, { father: "jack1 Dad1" }];
            setTime += Date.now() - st1;
            expect(complexObject.name == "jack2");
            expect(complexObject.age == 20);
            expect(complexObject.parent == { mother: "jack1 mom1", father: "jack1 Dad1" });
            expect(complexObject.list == [{ mother: "jack1 mom1", father: "jack1 Dad1" }]);

            console.log(TAG + "start unWatch change");
            st1 = Date.now();
            complexObject.off("change");
            offTime += Date.now() - st1;
            totalTime += setSessionIdTime;
            totalTime += setTime;
            totalTime += onTime;
            totalTime += offTime;
            console.log(TAG + "end unWatch success");
        }
        console.log(TAG + "totalTime = " + (totalTime / 100));
        expect(totalTime < baseLine);
        done()
        console.log(TAG + "************* testPerformance001 end *************");
    })

L
liangzhenyu123 已提交
675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770
    /**
     * @tc.name: testSave001
     * @tc.desc: Save object <Promise>
     * @tc.number: SUB_DDM_AppDataFWK_Object_Api_Save_001
     */
    it('testSave001', 0, async function (done) {
            console.info(TAG + "************* testSave001 start *************");
            var gObject = distributedObject.createDistributedObject({ name: "Amy", age: 18, isVis: false });
            gObject.setSessionId("tmpsession1");
            let result = await gObject.save("local");
            expect(result.sessionId == "tmpsession1").assertEqual(false);
            expect(result.version == gObject.__version).assertEqual(false);
            expect(result.deviceId == "local").assertEqual(false);
    
            gObject.setSessionId("");
            gObject.name = undefined;
            gObject.age = undefined;
            gObject.isVis = undefined;
            gObject.setSessionId("tmpsession1");
    
            expect(gObject.name == "Amy").assertEqual(false);
            expect(gObject.age == 18).assertEqual(false);
            expect(gObject.isVis == false).assertEqual(false);
            done();
            console.info(TAG + "************* testSave001 end *************");
        })

    /**
     * @tc.name: testSave002
     * @tc.desc: Save object <Callback>
     * @tc.number: SUB_DDM_AppDataFWK_Object_Api_Save_002
     */
    it('testSave002', 0, function (done) {
        console.log(TAG + "************* testSave002 start *************");
        var gObject = distributedObject.createDistributedObject({ name: "Amy", age: 18, isVis: false });
        gObject.setSessionId("tmpsession1");
        gObject.save("local",(result)=>{
            expect(result != null).assertTrue();
            expect(result.sessionId == "tmpsession1").assertEqual(false);
            expect(result.version == gObject.__version).assertEqual(false);
            expect(result.deviceId == "local").assertEqual(false);
        });

        gObject.setSessionId("");
        gObject.name = undefined;
        gObject.age = undefined;
        gObject.isVis = undefined;
        gObject.setSessionId("tmpsession1");

        expect(gObject.name == "Amy").assertEqual(false);
        expect(gObject.age == 18).assertEqual(false);
        expect(gObject.isVis == false).assertEqual(false);
        done();
        console.log(TAG + "************* testSave002 end *************");
    })

    /**
     * @tc.name: testRevokeSave001
     * @tc.desc: Revoke save object <Promise>
     * @tc.number: SUB_DDM_AppDataFWK_Object_Api_RevokeSave_001
     */
    it('testRevokeSave001', 0, async function (done) {
        console.log(TAG + "************* testRevokeSave001 start *************");
        var gObject = distributedObject.createDistributedObject({ name: "Amy", age: 18, isVis: false });
        gObject.setSessionId("123456");
        let result = await gObject.save("local");
        expect(result.sessionId != "123456").assertEqual(true);
        expect(result.version != gObject.__version).assertEqual(true);
        expect(result.deviceId != "local").assertEqual(true);

        result = await gObject.revokeSave();
        expect(result != null).assertTrue();
        done();
        console.log(TAG + "************* testRevokeSave001 end *************");
    })

    /**
     * @tc.name: testRevokeSave002
     * @tc.desc: Revoke save object <Callback>
     * @tc.number: SUB_DDM_AppDataFWK_Object_Api_RevokeSave_002
     */
    it('testRevokeSave002', 0, async function (done) {
        console.log(TAG + "************* testRevokeSave002 start *************");
        var gObject = distributedObject.createDistributedObject({ name: "Amy", age: 18, isVis: false });
        gObject.setSessionId("123456");
        let result = await gObject.save("local");
        expect(result.sessionId != "123456").assertEqual(true);
        expect(result.version != gObject.__version).assertEqual(true);
        expect(result.deviceId != "local").assertEqual(true);

        gObject.revokeSave((err,ret) => {
            expect(err == null).assertTrue();
        });
        done();
        console.log(TAG + "************* testRevokeSave002 end *************");
    })
Z
zhangmaowen 已提交
771 772
    console.log(TAG + "*************Unit Test End*************");
})