ObjectStoreJsunit.test.js 32.1 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
        console.info(TAG + 'afterAll')
    })

L
liangzhenyu123 已提交
98
    console.info(TAG + "*************Unit Test Begin*************");
Z
zhangmaowen 已提交
99 100 101 102 103


    /**
     * @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
     */
    it('testOn001', 0, function (done) {
L
liangzhenyu123 已提交
107
        console.info(TAG + "************* testOn001 start *************");
Z
zhangmaowen 已提交
108 109 110
        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
        } else {
L
liangzhenyu123 已提交
113
            console.info(TAG + "testOn001 joinSession failed");
Z
zhangmaowen 已提交
114 115 116 117 118 119 120 121 122 123 124 125 126 127 128
        }
        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()
L
liangzhenyu123 已提交
129
        console.info(TAG + "************* testOn001 end *************");
Z
zhangmaowen 已提交
130 131 132 133 134
    })

    /**
     * @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
     */
    it('testOn002', 0, function (done) {
L
liangzhenyu123 已提交
138
        console.info(TAG + "************* testOn002 start *************");
Z
zhangmaowen 已提交
139 140 141
        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
        } else {
L
liangzhenyu123 已提交
144
            console.info(TAG + "testOn002 joinSession failed");
Z
zhangmaowen 已提交
145 146 147 148 149 150 151 152 153 154 155 156 157
        }
        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()
L
liangzhenyu123 已提交
158
        console.info(TAG + "************* testOn002 end *************");
Z
zhangmaowen 已提交
159 160 161 162 163 164
    })

    /**
     * @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
     */
    it('testOn003', 0, function (done) {
L
liangzhenyu123 已提交
168
        console.info(TAG + "************* testOn003 start *************");
Z
zhangmaowen 已提交
169 170 171
        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
        } else {
L
liangzhenyu123 已提交
174
            console.info(TAG + "testOn003 joinSession failed");
Z
zhangmaowen 已提交
175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194
        }
        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()
L
liangzhenyu123 已提交
195
        console.info(TAG + "************* testOn003 end *************");
Z
zhangmaowen 已提交
196 197 198 199 200
    })

    /**
     * @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
     */
    it('testOn004', 0, function (done) {
L
liangzhenyu123 已提交
204
        console.info(TAG + "************* testOn004 start *************");
Z
zhangmaowen 已提交
205 206 207
        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
        } else {
L
liangzhenyu123 已提交
210
            console.info(TAG + "testOn004 joinSession failed");
Z
zhangmaowen 已提交
211 212 213 214 215 216
        }
        objectTest.on("change", changeCallback);
        console.info(TAG + " start call watch change");
        console.info(TAG + " end call watch change");

        done()
L
liangzhenyu123 已提交
217
        console.info(TAG + "************* testOn004 end *************");
Z
zhangmaowen 已提交
218 219 220 221 222
    })

    /**
     * @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
     */
    it('testOff001', 0, function (done) {
L
liangzhenyu123 已提交
226
        console.info(TAG + "************* testOff001 start *************");
Z
zhangmaowen 已提交
227 228 229
        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
        } else {
L
liangzhenyu123 已提交
232
            console.info(TAG + "testOff001 joinSession failed");
Z
zhangmaowen 已提交
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
        }
        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()
L
liangzhenyu123 已提交
260
        console.info(TAG + "************* testOff001 end *************");
Z
zhangmaowen 已提交
261 262 263 264 265
    })

    /**
     * @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
     */
    it('testOff002', 0, function (done) {
L
liangzhenyu123 已提交
269
        console.info(TAG + "************* testOff002 start *************");
Z
zhangmaowen 已提交
270 271 272
        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
        } else {
L
liangzhenyu123 已提交
275
            console.info(TAG + "testOff002 joinSession failed");
Z
zhangmaowen 已提交
276 277 278 279 280 281 282 283 284 285 286 287 288 289 290
        }
        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()
L
liangzhenyu123 已提交
291
        console.info(TAG + "************* testOff002 end *************");
Z
zhangmaowen 已提交
292 293 294 295 296
    })

    /**
     * @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
     */
    it('testMultiObjectOn001', 0, function (done) {
L
liangzhenyu123 已提交
300
        console.info(TAG + "************* testMultiObjectOn001 start *************");
Z
zhangmaowen 已提交
301 302 303
        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
        } else {
L
liangzhenyu123 已提交
306
            console.info(TAG + "testMultiObjectOn001 joinSession failed");
Z
zhangmaowen 已提交
307 308 309 310
        }
        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
        } else {
L
liangzhenyu123 已提交
313
            console.info(TAG + "testMultiObjectOn001 joinSession failed");
Z
zhangmaowen 已提交
314 315 316 317 318 319 320 321 322 323 324 325
        }
        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
        }

        done()
L
liangzhenyu123 已提交
340
        console.info(TAG + "************* testMultiObjectOn001 end *************");
Z
zhangmaowen 已提交
341 342
    })

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
    it('testMultiObjectOff001', 0, function (done) {
L
liangzhenyu123 已提交
349
        console.info(TAG + "************* testMultiObjectOff001 start *************");
Y
yanglei1217 已提交
350 351 352 353 354
        var objectTest = distributedObject.createDistributedObject({ name: "Amy", age: 18, isVis: false });
        objectTest.setSessionId("session8");
        if (objectTest != undefined && objectTest != null) {
            expect("session8" == objectTest.__sessionId);
        } else {
L
liangzhenyu123 已提交
355
            console.info(TAG + "testMultiObjectOn002 joinSession failed");
Y
yanglei1217 已提交
356 357 358 359 360 361
        }
        var testObject = distributedObject.createDistributedObject({ name: "Eric", age: 81, isVis: true });
        testObject.setSessionId("testSession2");
        if (testObject != undefined && testObject != null) {
            expect("testSession2" == testObject.__sessionId);
        } else {
L
liangzhenyu123 已提交
362
            console.info(TAG + "testMultiObjectOn002 joinSession failed");
Y
yanglei1217 已提交
363
        }
L
liangzhenyu123 已提交
364
        console.info(TAG + " start call watch change")
Y
yanglei1217 已提交
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
        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()
L
liangzhenyu123 已提交
412
        console.info(TAG + "************* testMultiObjectOff001 end *************");
Y
yanglei1217 已提交
413
    })
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) {
L
liangzhenyu123 已提交
421
        console.info(TAG + "************* testChangeSession001 start *************");
Y
yanglei1217 已提交
422 423 424 425 426
        var objectTest = distributedObject.createDistributedObject({ name: "Amy", age: 18, isVis: false });
        objectTest.setSessionId("session9");
        if (objectTest != undefined && objectTest != null) {
            expect("session9" == objectTest.__sessionId);
        } else {
L
liangzhenyu123 已提交
427
            console.info(TAG + "testChangeSession001 joinSession failed");
Y
yanglei1217 已提交
428
        }
L
liangzhenyu123 已提交
429
        console.info(TAG + "start change sessionId");
Y
yanglei1217 已提交
430
        setTimeout(() => objectTest.setSessionId("session9"), 1000);
1
18834416147 已提交
431
        if (objectTest != undefined && objectTest != null) {
Y
yanglei1217 已提交
432 433
            expect("session9" == objectTest.__sessionId);
        } else {
L
liangzhenyu123 已提交
434
            console.info(TAG + "testChangeSession001 joinSession again failed");
Y
yanglei1217 已提交
435 436 437
        }

        done()
L
liangzhenyu123 已提交
438
        console.info(TAG + "************* testChangeSession001 end *************");
Y
yanglei1217 已提交
439 440
    })

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
     */
    it('testUndefinedType001', 0, function (done) {
L
liangzhenyu123 已提交
447
        console.info(TAG + "************* testUndefinedType001 start *************");
Z
zhangmaowen 已提交
448 449 450 451
        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 {
L
liangzhenyu123 已提交
454
                console.info(TAG + "testUndefinedType001 joinSession session11 failed");
Z
zhangmaowen 已提交
455 456 457 458 459 460
            }
        } catch (error) {
            console.error(TAG + error);
        }

        done()
L
liangzhenyu123 已提交
461
        console.info(TAG + "************* testUndefinedType001 end *************");
Z
zhangmaowen 已提交
462 463 464 465 466
    })

    /**
     * @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
     */
    it('testGenSessionId001', 0, function (done) {
L
liangzhenyu123 已提交
470
        console.info(TAG + "************* testGenSessionId001 start *************");
Z
zhangmaowen 已提交
471 472 473 474
        var sessionId = distributedObject.genSessionId();
        expect(sessionId != null && sessionId.length > 0 && typeof (sessionId) == 'string');

        done()
L
liangzhenyu123 已提交
475
        console.info(TAG + "************* testGenSessionId001 end *************");
Z
zhangmaowen 已提交
476 477 478 479 480
    })

    /**
     * @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) {
L
liangzhenyu123 已提交
484
        console.info(TAG + "************* testGenSessionId002 start *************");
Z
zhangmaowen 已提交
485 486 487 488 489
        var sessionId1 = distributedObject.genSessionId();
        var sessionId2 = distributedObject.genSessionId();
        expect(sessionId1 != sessionId2);

        done()
L
liangzhenyu123 已提交
490
        console.info(TAG + "************* testGenSessionId002 end *************");
Z
zhangmaowen 已提交
491 492 493 494 495
    })

    /**
     * @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
     */
    it('testOnStatus001', 0, function (done) {
L
liangzhenyu123 已提交
499 500
        console.info(TAG + "************* testOnStatus001 start *************");
        console.info(TAG + "start watch status");
Z
zhangmaowen 已提交
501 502
        var objectTest = distributedObject.createDistributedObject({ name: "Amy", age: 18, isVis: false });
        objectTest.on("status", statusCallback1);
L
liangzhenyu123 已提交
503
        console.info(TAG + "watch success");
Z
zhangmaowen 已提交
504 505

        done()
L
liangzhenyu123 已提交
506
        console.info(TAG + "************* testOnStatus001 end *************");
Z
zhangmaowen 已提交
507 508 509 510 511
    })

    /**
     * @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) {
L
liangzhenyu123 已提交
515 516
        console.info(TAG + "************* testOnStatus002 start *************");
        console.info(TAG + "start watch status");
Z
zhangmaowen 已提交
517 518 519 520
        var objectTest = distributedObject.createDistributedObject({ name: "Amy", age: 18, isVis: false });
        objectTest.on("status", statusCallback1);
        objectTest.on("status", statusCallback2);
        objectTest.on("status", statusCallback3);
L
liangzhenyu123 已提交
521 522
        console.info(TAG + "watch success");
        console.info(TAG + "start call unwatch status");
Z
zhangmaowen 已提交
523
        objectTest.off("status", statusCallback1);
L
liangzhenyu123 已提交
524
        console.info(TAG + "unwatch success");
Z
zhangmaowen 已提交
525 526

        done()
L
liangzhenyu123 已提交
527
        console.info(TAG + "************* testOnStatus002 end *************");
Z
zhangmaowen 已提交
528 529 530 531 532
    })

    /**
     * @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
    it('testOnStatus003', 0, function (done) {
L
liangzhenyu123 已提交
536 537
        console.info(TAG + "************* testOnStatus003 start *************");
        console.info(TAG + "start watch status");
Z
zhangmaowen 已提交
538 539 540 541
        var objectTest = distributedObject.createDistributedObject({ name: "Amy", age: 18, isVis: false });
        objectTest.on("status", statusCallback1);
        objectTest.on("status", statusCallback2);
        objectTest.on("status", statusCallback3);
L
liangzhenyu123 已提交
542 543
        console.info(TAG + "watch success");
        console.info(TAG + "start call unwatch status");
Z
zhangmaowen 已提交
544
        objectTest.off("status");
L
liangzhenyu123 已提交
545
        console.info(TAG + "unwatch success");
Z
zhangmaowen 已提交
546 547

        done()
L
liangzhenyu123 已提交
548
        console.info(TAG + "************* testOnStatus003 end *************");
Z
zhangmaowen 已提交
549 550 551 552 553
    })

    /**
     * @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
     */
    it('testComplex001', 0, function (done) {
L
liangzhenyu123 已提交
557
        console.info(TAG + "************* testComplex001 start *************");
Z
zhangmaowen 已提交
558 559 560 561 562 563 564 565
        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
        } else {
L
liangzhenyu123 已提交
568
            console.info(TAG + "testOnComplex001 joinSession session12 failed");
Z
zhangmaowen 已提交
569 570 571 572 573 574 575 576 577 578 579 580
        }
        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()
L
liangzhenyu123 已提交
581
        console.info(TAG + "************* testComplex001 end *************");
Z
zhangmaowen 已提交
582 583 584 585 586
    })

    /**
     * @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
     */
    it('testMaxSize001', 0, function (done) {
L
liangzhenyu123 已提交
590
        console.info(TAG + "************* testMaxSize001 start *************");
Z
zhangmaowen 已提交
591 592 593
        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
        } else {
L
liangzhenyu123 已提交
596
            console.info(TAG + "testMaxSize001 joinSession session13 failed");
Z
zhangmaowen 已提交
597 598 599 600 601 602 603 604
        }
        //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);
L
liangzhenyu123 已提交
605
            console.info(TAG + "get/set maxSize string success");
Z
zhangmaowen 已提交
606 607 608 609 610
        } else {
            console.info(TAG + " object is null,set name fail");
        }

        done()
L
liangzhenyu123 已提交
611
        console.info(TAG + "************* testMaxSize001 end *************");
Z
zhangmaowen 已提交
612 613 614 615 616
    })

    /**
     * @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
     */
    it('testPerformance001', 0, function (done) {
L
liangzhenyu123 已提交
620
        console.info(TAG + "************* testPerformance001 start *************");
Z
zhangmaowen 已提交
621 622 623 624 625 626 627 628 629 630 631 632
        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
            } else {
L
liangzhenyu123 已提交
640
                console.info(TAG + "testPerformance001 joinSession session14 failed");
Z
zhangmaowen 已提交
641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658
            }
            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" }]);

L
liangzhenyu123 已提交
659
            console.info(TAG + "start unWatch change");
Z
zhangmaowen 已提交
660 661 662 663 664 665 666
            st1 = Date.now();
            complexObject.off("change");
            offTime += Date.now() - st1;
            totalTime += setSessionIdTime;
            totalTime += setTime;
            totalTime += onTime;
            totalTime += offTime;
L
liangzhenyu123 已提交
667
            console.info(TAG + "end unWatch success");
Z
zhangmaowen 已提交
668
        }
L
liangzhenyu123 已提交
669
        console.info(TAG + "totalTime = " + (totalTime / 100));
Z
zhangmaowen 已提交
670 671
        expect(totalTime < baseLine);
        done()
L
liangzhenyu123 已提交
672
        console.info(TAG + "************* testPerformance001 end *************");
Z
zhangmaowen 已提交
673 674
    })

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
    /**
     * @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) {
L
liangzhenyu123 已提交
708
        console.info(TAG + "************* testSave002 start *************");
L
liangzhenyu123 已提交
709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727
        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();
L
liangzhenyu123 已提交
728
        console.info(TAG + "************* testSave002 end *************");
L
liangzhenyu123 已提交
729 730 731 732 733 734 735 736
    })

    /**
     * @tc.name: testRevokeSave001
     * @tc.desc: Revoke save object <Promise>
     * @tc.number: SUB_DDM_AppDataFWK_Object_Api_RevokeSave_001
     */
    it('testRevokeSave001', 0, async function (done) {
L
liangzhenyu123 已提交
737
        console.info(TAG + "************* testRevokeSave001 start *************");
L
liangzhenyu123 已提交
738 739 740 741 742 743 744 745 746 747
        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();
L
liangzhenyu123 已提交
748
        console.info(TAG + "************* testRevokeSave001 end *************");
L
liangzhenyu123 已提交
749 750 751 752 753 754 755 756
    })

    /**
     * @tc.name: testRevokeSave002
     * @tc.desc: Revoke save object <Callback>
     * @tc.number: SUB_DDM_AppDataFWK_Object_Api_RevokeSave_002
     */
    it('testRevokeSave002', 0, async function (done) {
L
liangzhenyu123 已提交
757
        console.info(TAG + "************* testRevokeSave002 start *************");
L
liangzhenyu123 已提交
758 759 760 761 762 763 764 765 766 767 768
        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();
L
liangzhenyu123 已提交
769
        console.info(TAG + "************* testRevokeSave002 end *************");
L
liangzhenyu123 已提交
770
    })
L
liangzhenyu123 已提交
771
    console.info(TAG + "*************Unit Test End*************");
Z
zhangmaowen 已提交
772
})