diff --git a/frameworks/jskitsimpl/test/unittest/src/BUILD.gn b/frameworks/jskitsimpl/test/unittest/src/BUILD.gn index 4b2d7f0..b0beec0 100644 --- a/frameworks/jskitsimpl/test/unittest/src/BUILD.gn +++ b/frameworks/jskitsimpl/test/unittest/src/BUILD.gn @@ -21,7 +21,7 @@ ohos_js_unittest("ObjectStoreJsTest") { hap_profile = "./config.json" if (is_standard_system) { - certificate_profile = "//test/developertest/signature/openharmony_sx.p7b" + certificate_profile = "./openharmony_sx.p7b" } else { deps = [ "//test/developertest/adapter/examples/app_info/test/common/main:get_app_info_test_lib" ] entry_app_dep = [ "//test/developertest/adapter/examples/app_info/test/common/shell:build_shell_execute" ] diff --git a/frameworks/jskitsimpl/test/unittest/src/ObjectStoreJsunit.test.js b/frameworks/jskitsimpl/test/unittest/src/ObjectStoreJsunit.test.js index f01210e..ea5fd01 100644 --- a/frameworks/jskitsimpl/test/unittest/src/ObjectStoreJsunit.test.js +++ b/frameworks/jskitsimpl/test/unittest/src/ObjectStoreJsunit.test.js @@ -12,50 +12,67 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -import {describe, beforeAll, beforeEach, afterEach, afterAll, it, expect} from 'deccjsunit/index' +import {describe, beforeAll, beforeEach, afterEach, afterAll, it, expect} from 'deccjsunit/index'; import distributedObject from '@ohos.data.distributedDataObject'; +import abilityAccessCtrl from '@ohos.abilityAccessCtrl'; +import bundle from '@ohos.bundle'; -var baseLine = 500; //0.5 second +var baseLine = 3000; //3 second const TAG = "OBJECTSTORE_TEST"; function changeCallback(sessionId, changeData) { - console.info("get init change111" + sessionId + " " + changeData); + console.info("changeCallback start"); if (changeData != null && changeData != undefined) { changeData.forEach(element => { console.info(TAG + "data changed !" + element); }); } - console.info(TAG + "get init change111 end" + sessionId + " " + changeData); + console.info("changeCallback end"); } function changeCallback2(sessionId, changeData) { - console.info("get init change222" + sessionId + " " + changeData); + console.info("changeCallback2 start"); if (changeData != null && changeData != undefined) { changeData.forEach(element => { - console.info(TAG + "data changed !" + element); + console.info(TAG + "data changed !"); }); } - console.info(TAG + "get init change222 end" + sessionId + " " + changeData); + console.info("changeCallback2 end"); } function statusCallback1(sessionId, networkId, status) { - console.info(TAG + "test init change111" + sessionId); + console.info(TAG + "statusCallback1" + sessionId); this.response += "\nstatus changed " + sessionId + " " + status + " " + networkId; } function statusCallback2(sessionId, networkId, status) { - console.info(TAG + "test init change222" + sessionId); + console.info(TAG + "statusCallback2" + sessionId); this.response += "\nstatus changed " + sessionId + " " + status + " " + networkId; } function statusCallback3(sessionId, networkId, status) { - console.info(TAG + "test init change333" + sessionId); + console.info(TAG + "statusCallback3" + sessionId); this.response += "\nstatus changed " + sessionId + " " + status + " " + networkId; } -describe('objectStoreTest', function () { - beforeAll(function () { - console.info(TAG + 'beforeAll') +const TIMEOUT = 1500; +const PERMISSION_USER_SET = 1; +const PERMISSION_USER_NAME = "ohos.permission.DISTRIBUTED_DATASYNC"; +var tokenID = undefined; +async function grantPerm() { + console.info("====grant Permission start===="); + var appInfo = await bundle.getApplicationInfo('com.example.myapplication', 0, 100); + tokenID = appInfo.accessTokenId; + console.info("accessTokenId" + appInfo.accessTokenId + " bundleName:" + appInfo.bundleName); + var atManager = abilityAccessCtrl.createAtManager(); + var result = await atManager.grantUserGrantedPermission(tokenID, PERMISSION_USER_NAME, PERMISSION_USER_SET); + console.info("tokenId" + tokenID + " result:" + result); + console.info("====grant Permission end===="); +} +describe('objectStoreTest',function () { + beforeAll(async function (done) { + await grantPerm(); + done(); }) beforeEach(function () { @@ -79,30 +96,36 @@ describe('objectStoreTest', function () { * @tc.type: FUNC * @tc.require: I4H3LS */ - it('testOn001', 0, function (done) { + it('testOn001', 0, function () { console.log(TAG + "************* testOn001 start *************"); var g_object = distributedObject.createDistributedObject({ name: "Amy", age: 18, isVis: false }); + expect(g_object == undefined).assertEqual(false); g_object.setSessionId("session1"); - if (g_object != undefined && g_object != null) { - expect("session1" == g_object.__sessionId); - } else { - console.log(TAG + "testOn001 joinSession failed"); - } + expect("session1" == g_object.__sessionId).assertEqual(true); console.info(TAG + " start call watch change"); - g_object.on("change", changeCallback); + g_object.on("change", function (sessionId, changeData) { + console.info("testOn001 callback start."); + if (changeData != null && changeData != undefined) { + changeData.forEach(element => { + console.info(TAG + "data changed !" + element); + }); + } + console.info("testOn001 callback end."); + }); + if (g_object != undefined && g_object != null) { g_object.name = "jack1"; g_object.age = 19; g_object.isVis = true; - expect(g_object.name == "jack1"); - expect(g_object.age == 19); + expect(g_object.name == "jack1").assertEqual(true); + expect(g_object.age == 19).assertEqual(true); console.info(TAG + " set data success!"); } else { console.info(TAG + " object is null,set name fail"); } - done() console.log(TAG + "************* testOn001 end *************"); + g_object.setSessionId(""); }) /** @@ -111,28 +134,25 @@ describe('objectStoreTest', function () { * @tc.type: FUNC * @tc.require: I4H3LS */ - it('testOn002', 0, function (done) { + it('testOn002', 0, function () { console.log(TAG + "************* testOn002 start *************"); var g_object = distributedObject.createDistributedObject({ name: "Amy", age: 18, isVis: false }); + expect(g_object == undefined).assertEqual(false); g_object.setSessionId("session2"); - if (g_object != undefined && g_object != null) { - expect("session2" == g_object.__sessionId); - } else { - console.log(TAG + "testOn002 joinSession failed"); - } + expect("session2" == g_object.__sessionId).assertEqual(true); if (g_object != undefined && g_object != null) { g_object.name = "jack1"; g_object.age = 19; g_object.isVis = true; - expect(g_object.name == "jack1"); - expect(g_object.age == 19); + expect(g_object.name == "jack1").assertEqual(true); + expect(g_object.age == 19).assertEqual(true); console.info(TAG + " set data success!"); } else { console.info(TAG + " object is null,set name fail"); } - done() console.log(TAG + "************* testOn002 end *************"); + g_object.setSessionId(""); }) /** @@ -141,35 +161,32 @@ describe('objectStoreTest', function () { * @tc.type: FUNC * @tc.require: I4H3LS */ - it('testOn003', 0, function (done) { + it('testOn003', 0, function () { console.log(TAG + "************* testOn003 start *************"); var g_object = distributedObject.createDistributedObject({ name: "Amy", age: 18, isVis: false }); + expect(g_object == undefined).assertEqual(false); g_object.setSessionId("session3"); - if (g_object != undefined && g_object != null) { - expect("session3" == g_object.__sessionId); - } else { - console.log(TAG + "testOn003 joinSession failed"); - } + expect("session3" == g_object.__sessionId).assertEqual(true); g_object.on("change", changeCallback); console.info(TAG + " start call watch change"); if (g_object != undefined && g_object != null) { g_object.name = "jack1"; g_object.age = 19; g_object.isVis = true; - expect(g_object.name == "jack1"); - expect(g_object.age == 19); + expect(g_object.name == "jack1").assertEqual(true); + expect(g_object.age == 19).assertEqual(true); g_object.name = "jack2"; g_object.age = 20; g_object.isVis = false; - expect(g_object.name == "jack2"); - expect(g_object.age == 20); + expect(g_object.name == "jack2").assertEqual(true); + expect(g_object.age == 20).assertEqual(true); console.info(TAG + " set data success!"); } else { console.info(TAG + " object is null,set name fail"); } - done() console.log(TAG + "************* testOn003 end *************"); + g_object.setSessionId(""); }) /** @@ -178,21 +195,17 @@ describe('objectStoreTest', function () { * @tc.type: FUNC * @tc.require: I4H3LS */ - it('testOn004', 0, function (done) { + it('testOn004', 0, function () { console.log(TAG + "************* testOn004 start *************"); var g_object = distributedObject.createDistributedObject({ name: "Amy", age: 18, isVis: false }); + expect(g_object == undefined).assertEqual(false); g_object.setSessionId("session4"); - if (g_object != undefined && g_object != null) { - expect("session4" == g_object.__sessionId); - } else { - console.log(TAG + "testOn004 joinSession failed"); - } + expect("session4" == g_object.__sessionId).assertEqual(true); g_object.on("change", changeCallback); console.info(TAG + " start call watch change"); console.info(TAG + " end call watch change"); - - done() console.log(TAG + "************* testOn004 end *************"); + g_object.setSessionId(""); }) /** @@ -201,23 +214,21 @@ describe('objectStoreTest', function () { * @tc.type: FUNC * @tc.require: I4H3LS */ - it('testOff001', 0, function (done) { + it('testOff001', 0, function () { console.log(TAG + "************* testOff001 start *************"); var g_object = distributedObject.createDistributedObject({ name: "Amy", age: 18, isVis: false }); + expect(g_object == undefined).assertEqual(false); g_object.setSessionId("session5"); - if (g_object != undefined && g_object != null) { - expect("session5" == g_object.__sessionId); - } else { - console.log(TAG + "testOff001 joinSession failed"); - } + expect("session5" == g_object.__sessionId).assertEqual(true); + g_object.on("change", changeCallback); console.info(TAG + " start call watch change"); if (g_object != undefined && g_object != null) { g_object.name = "jack1"; g_object.age = 19; g_object.isVis = true; - expect(g_object.name == "jack1"); - expect(g_object.age == 19); + expect(g_object.name == "jack1").assertEqual(true); + expect(g_object.age == 19).assertEqual(true); console.info(TAG + " set data success!"); } else { console.info(TAG + " object is null,set name fail"); @@ -228,15 +239,15 @@ describe('objectStoreTest', function () { g_object.name = "jack2"; g_object.age = 20; g_object.isVis = false; - expect(g_object.name == "jack2"); - expect(g_object.age == 20); + expect(g_object.name == "jack2").assertEqual(true); + expect(g_object.age == 20).assertEqual(true); console.info(TAG + " set data success!"); } else { console.info(TAG + " object is null,set name fail"); } - done() console.log(TAG + "************* testOff001 end *************"); + g_object.setSessionId(""); }) /** @@ -245,30 +256,27 @@ describe('objectStoreTest', function () { * @tc.type: FUNC * @tc.require: I4H3LS */ - it('testOff002', 0, function (done) { + it('testOff002', 0, function () { console.log(TAG + "************* testOff002 start *************"); var g_object = distributedObject.createDistributedObject({ name: "Amy", age: 18, isVis: false }); + expect(g_object == undefined).assertEqual(false); g_object.setSessionId("session6"); - if (g_object != undefined && g_object != null) { - expect("session6" == g_object.__sessionId); - } else { - console.log(TAG + "testOff002 joinSession failed"); - } + expect("session6" == g_object.__sessionId).assertEqual(true); g_object.off("change"); console.info(TAG + " end call watch change"); if (g_object != undefined && g_object != null) { g_object.name = "jack1"; g_object.age = 19; g_object.isVis = true; - expect(g_object.name == "jack1"); - expect(g_object.age == 19); + expect(g_object.name == "jack1").assertEqual(true); + expect(g_object.age == 19).assertEqual(true); console.info(TAG + " set data success!"); } else { console.info(TAG + " object is null,set name fail"); } - done() console.log(TAG + "************* testOff002 end *************"); + g_object.setSessionId(""); }) /** @@ -277,22 +285,18 @@ describe('objectStoreTest', function () { * @tc.type: FUNC * @tc.require: I4H3LS */ - it('testMultiObjectOn001', 0, function (done) { + it('testMultiObjectOn001', 0, function () { console.log(TAG + "************* testMultiObjectOn001 start *************"); var g_object = distributedObject.createDistributedObject({ name: "Amy", age: 18, isVis: false }); + expect(g_object == undefined).assertEqual(false); g_object.setSessionId("session7"); - if (g_object != undefined && g_object != null) { - expect("session7" == g_object.__sessionId); - } else { - console.log(TAG + "testMultiObjectOn001 joinSession failed"); - } + expect("session7" == g_object.__sessionId).assertEqual(true); + var test_object = distributedObject.createDistributedObject({ name: "Eric", age: 81, isVis: true }); + expect(test_object == undefined).assertEqual(false); test_object.setSessionId("testSession1"); - if (test_object != undefined && test_object != null) { - expect("testSession1").assertEqual(test_object.__sessionId); - } else { - console.log(TAG + "testMultiObjectOn001 joinSession failed"); - } + expect("testSession1" == test_object.__sessionId).assertEqual(true); + g_object.on("change", changeCallback); test_object.on("change", changeCallback2); console.info(TAG + " start call watch change"); @@ -300,20 +304,21 @@ describe('objectStoreTest', function () { g_object.name = "jack1"; g_object.age = 19; g_object.isVis = true; - expect(g_object.name == "jack1"); - expect(g_object.age == 19); + expect(g_object.name == "jack1").assertEqual(true); + expect(g_object.age == 19).assertEqual(true); g_object.name = "jack2"; g_object.age = 20; g_object.isVis = false; - expect(g_object.name == "jack2"); - expect(g_object.age == 20); + expect(g_object.name == "jack2").assertEqual(true); + expect(g_object.age == 20).assertEqual(true); console.info(TAG + " set data success!"); } else { console.info(TAG + " object is null,set name fail"); } - done() console.log(TAG + "************* testMultiObjectOn001 end *************"); + g_object.setSessionId(""); + test_object.setSessionId(""); }) /** @@ -322,22 +327,20 @@ describe('objectStoreTest', function () { * @tc.type: FUNC * @tc.require: I4H3LS */ - it('testMultiObjectOff001', 0, function (done) { + it('testMultiObjectOff001', 0, function () { console.log(TAG + "************* testMultiObjectOff001 start *************"); var g_object = distributedObject.createDistributedObject({ name: "Amy", age: 18, isVis: false }); + expect(g_object == undefined).assertEqual(false); + g_object.setSessionId("session8"); - if (g_object != undefined && g_object != null) { - expect("session8" == g_object.__sessionId); - } else { - console.log(TAG + "testMultiObjectOn002 joinSession failed"); - } + expect("session8" == g_object.__sessionId).assertEqual(true); + var test_object = distributedObject.createDistributedObject({ name: "Eric", age: 81, isVis: true }); + expect(g_object == undefined).assertEqual(false); + test_object.setSessionId("testSession2"); - if (test_object != undefined && test_object != null) { - expect("testSession2" == test_object.__sessionId); - } else { - console.log(TAG + "testMultiObjectOn002 joinSession failed"); - } + expect("testSession2" == test_object.__sessionId).assertEqual(true); + console.log(TAG + " start call watch change") g_object.on("change", changeCallback); test_object.on("change", changeCallback2); @@ -346,8 +349,8 @@ describe('objectStoreTest', function () { g_object.name = "jack1"; g_object.age = 19; g_object.isVis = true; - expect(g_object.name == "jack1"); - expect(g_object.age == 19); + expect(g_object.name == "jack1").assertEqual(true); + expect(g_object.age == 19).assertEqual(true); console.info(TAG + " set data success!"); } else { console.info(TAG + " object is null,set name fail"); @@ -356,8 +359,8 @@ describe('objectStoreTest', function () { test_object.name = "jack2"; test_object.age = 20; test_object.isVis = false; - expect(test_object.name == "jack2"); - expect(test_object.age == 20); + expect(test_object.name == "jack2").assertEqual(true); + expect(test_object.age == 20).assertEqual(true); console.info(TAG + " set data success!"); } else { console.info(TAG + " object is null,set name fail"); @@ -367,8 +370,8 @@ describe('objectStoreTest', function () { g_object.name = "jack3"; g_object.age = 21; g_object.isVis = false; - expect(g_object.name == "jack3"); - expect(g_object.age == 21); + expect(g_object.name == "jack3").assertEqual(true); + expect(g_object.age == 21).assertEqual(true); console.info(TAG + " set data success!"); } else { console.info(TAG + " object is null,set name fail"); @@ -378,15 +381,15 @@ describe('objectStoreTest', function () { test_object.name = "jack4"; test_object.age = 22; test_object.isVis = true; - expect(test_object.name == "jack4"); - expect(test_object.age == 22); + expect(test_object.name == "jack4").assertEqual(true); + expect(test_object.age == 22).assertEqual(true); console.info(TAG + " set data success!"); } else { console.info(TAG + " object is null,set name fail"); } - - done() console.log(TAG + "************* testMultiObjectOff001 end *************"); + g_object.setSessionId(""); + test_object.setSessionId(""); }) /** @@ -395,46 +398,42 @@ describe('objectStoreTest', function () { * @tc.type: FUNC * @tc.require: I4H3LS */ - it('testChangeSession001', 0, function (done) { + it('testChangeSession001', 0, function () { console.log(TAG + "************* testChangeSession001 start *************"); var g_object = distributedObject.createDistributedObject({ name: "Amy", age: 18, isVis: false }); + expect(g_object == undefined).assertEqual(false); + g_object.setSessionId("session9"); - if (g_object != undefined && g_object != null) { - expect("session9" == g_object.__sessionId); - } else { - console.log(TAG + "testChangeSession001 joinSession session9 failed"); - } + expect("session9" == g_object.__sessionId).assertEqual(true); + g_object.on("change", changeCallback); console.info(TAG + " start call watch change"); if (g_object != undefined && g_object != null) { g_object.name = "jack1"; g_object.age = 19; g_object.isVis = true; - expect(g_object.name == "jack1"); - expect(g_object.age == 19); + expect(g_object.name == "jack1").assertEqual(true); + expect(g_object.age == 19).assertEqual(true); console.info(TAG + " set data success!"); } else { console.info(TAG + " object is null,set name fail"); } g_object.setSessionId("session10"); - if (g_object != undefined && g_object != null) { - expect("session10" == g_object.__sessionId); - } else { - console.log(TAG + "testChangeSession001 joinSession session10 failed"); - } + expect("session10" == g_object.__sessionId).assertEqual(true); + if (g_object != undefined && g_object != null) { g_object.name = "jack2"; g_object.age = 20; g_object.isVis = false; - expect(g_object.name == "jack2"); - expect(g_object.age == 20); + expect(g_object.name == "jack2").assertEqual(true); + expect(g_object.age == 20).assertEqual(true); console.info(TAG + " set data success!"); } else { console.info(TAG + " object is null,set name fail"); } - done() console.log(TAG + "************* testChangeSession001 end *************"); + g_object.setSessionId(""); }) /** @@ -443,22 +442,20 @@ describe('objectStoreTest', function () { * @tc.type: FUNC * @tc.require: I4H3LS */ - it('testUndefinedType001', 0, function (done) { + it('testUndefinedType001', 0, function () { console.log(TAG + "************* testUndefinedType001 start *************"); var undefined_object = distributedObject.createDistributedObject({ name: undefined, age: undefined, isVis: undefined }); + expect(undefined_object == undefined).assertEqual(false); try { undefined_object.setSessionId("session11"); - if (undefined_object != undefined && undefined_object != null) { - expect("session11" == undefined_object.__sessionId); - } else { - console.log(TAG + "testChangeSession001 joinSession session11 failed"); - } + expect("session11" == undefined_object.__sessionId).assertEqual(true); + } catch (error) { console.error(TAG + error); } - done() console.log(TAG + "************* testUndefinedType001 end *************"); + undefined_object.setSessionId(""); }) /** @@ -467,12 +464,11 @@ describe('objectStoreTest', function () { * @tc.type: FUNC * @tc.require: I4H3LS */ - it('testGenSessionId001', 0, function (done) { + it('testGenSessionId001', 0, function () { console.log(TAG + "************* testGenSessionId001 start *************"); var sessionId = distributedObject.genSessionId(); - expect(sessionId != null && sessionId.length > 0 && typeof (sessionId) == 'string'); + expect(sessionId != null && sessionId.length > 0 && typeof (sessionId) == 'string').assertEqual(true); - done() console.log(TAG + "************* testGenSessionId001 end *************"); }) @@ -482,13 +478,12 @@ describe('objectStoreTest', function () { * @tc.type: FUNC * @tc.require: I4H3LS */ - it('testGenSessionId002', 0, function (done) { - console.log(TAG + "************* testGenSessionId001 start *************"); + it('testGenSessionId002', 0, function () { + console.log(TAG + "************* testGenSessionId002 start *************"); var sessionId1 = distributedObject.genSessionId(); var sessionId2 = distributedObject.genSessionId(); - expect(sessionId1 != sessionId2); + expect(sessionId1 != sessionId2).assertEqual(true); - done() console.log(TAG + "************* testGenSessionId002 end *************"); }) @@ -498,14 +493,14 @@ describe('objectStoreTest', function () { * @tc.type: FUNC * @tc.require: I4H3M8 */ - it('testOnStatus001', 0, function (done) { + it('testOnStatus001', 0, function () { console.log(TAG + "************* testOnStatus001 start *************"); console.log(TAG + "start watch status"); var g_object = distributedObject.createDistributedObject({ name: "Amy", age: 18, isVis: false }); + expect(g_object == undefined).assertEqual(false); g_object.on("status", statusCallback1); console.log(TAG + "watch success"); - done() console.log(TAG + "************* testOnStatus001 end *************"); }) @@ -515,10 +510,12 @@ describe('objectStoreTest', function () { * @tc.type: FUNC * @tc.require: I4H3M8 */ - it('testOnStatus002', 0, function (done) { - console.log(TAG + "************* testOnStatus001 start *************"); + it('testOnStatus002', 0, function () { + console.log(TAG + "************* testOnStatus002 start *************"); console.log(TAG + "start watch status"); var g_object = distributedObject.createDistributedObject({ name: "Amy", age: 18, isVis: false }); + expect(g_object == undefined).assertEqual(false); + g_object.on("status", statusCallback1); g_object.on("status", statusCallback2); g_object.on("status", statusCallback3); @@ -527,8 +524,9 @@ describe('objectStoreTest', function () { g_object.off("status", statusCallback1); console.log(TAG + "unwatch success"); - done() console.log(TAG + "************* testOnStatus002 end *************"); + g_object.setSessionId(""); + }) /** @@ -537,10 +535,13 @@ describe('objectStoreTest', function () { * @tc.type: FUNC * @tc.require: I4H3M8 */ - it('testOnStatus002', 0, function (done) { - console.log(TAG + "************* testOnStatus001 start *************"); + it('testOnStatus003', 0, function () { + console.log(TAG + "************* testOnStatus003 start *************"); console.log(TAG + "start watch status"); var g_object = distributedObject.createDistributedObject({ name: "Amy", age: 18, isVis: false }); + expect(g_object == undefined).assertEqual(false); + + expect(g_object.name == "Amy").assertEqual(true); g_object.on("status", statusCallback1); g_object.on("status", statusCallback2); g_object.on("status", statusCallback3); @@ -549,8 +550,9 @@ describe('objectStoreTest', function () { g_object.off("status"); console.log(TAG + "unwatch success"); - done() console.log(TAG + "************* testOnStatus003 end *************"); + g_object.setSessionId(""); + }) /** @@ -559,7 +561,7 @@ describe('objectStoreTest', function () { * @tc.type: FUNC * @tc.require: I4H3M8 */ - it('testComplex001', 0, function (done) { + it('testComplex001', 0, function () { console.log(TAG + "************* testComplex001 start *************"); var complex_object = distributedObject.createDistributedObject({ name: undefined, @@ -567,24 +569,25 @@ describe('objectStoreTest', function () { parent: undefined, list: undefined }); + expect(complex_object == undefined).assertEqual(false); complex_object.setSessionId("session12"); - if (complex_object != undefined && complex_object != null) { - expect("session12" == complex_object.__sessionId); - } else { - console.log(TAG + "testOnComplex001 joinSession session12 failed"); - } + expect("session12" == complex_object.__sessionId).assertEqual(true); + complex_object.name = "jack"; complex_object.age = 19; complex_object.isVis = false; complex_object.parent = { mother: "jack mom", father: "jack Dad" }; - complex_object.list = [{ mother: "jack mom" }, { father: "jack Dad" }]; - expect(complex_object.name == "jack"); - expect(complex_object.age == 19); - expect(complex_object.parent == { mother: "jack1 mom", father: "jack1 Dad" }); - expect(complex_object.list == [{ mother: "jack1 mom", father: "jack1 Dad" }]); + complex_object.list = [{ mother: "jack2 mom2" }, { father: "jack2 Dad2" }]; + expect(complex_object.name == "jack").assertEqual(true); + expect(complex_object.age == 19).assertEqual(true); + expect(complex_object.parent.mother == "jack mom").assertEqual(true); + expect(complex_object.parent.father == "jack Dad").assertEqual(true); + expect(complex_object.list[0].mother == "jack2 mom2").assertEqual(true); + expect(complex_object.list[1].father == "jack2 Dad2").assertEqual(true); - done() console.log(TAG + "************* testComplex001 end *************"); + complex_object.setSessionId(""); + }) /** @@ -593,29 +596,28 @@ describe('objectStoreTest', function () { * @tc.type: FUNC * @tc.require: I4H3M8 */ - it('testMaxSize001', 0, function (done) { + it('testMaxSize001', 0, function () { console.log(TAG + "************* testMaxSize001 start *************"); var g_object = distributedObject.createDistributedObject({ name: "Amy", age: 18, isVis: false }); + expect(g_object == undefined).assertEqual(false); + g_object.setSessionId("session13"); - if (g_object != undefined && g_object != null) { - expect("session13" == g_object.__sessionId); - } else { - console.log(TAG + "testMaxSize001 joinSession session13 failed"); - } + expect("session13" == g_object.__sessionId).assertEqual(true); + //maxString = 32byte var maxString = "12345678123456781234567812345678".repeat(131072); if (g_object != undefined && g_object != null) { g_object.name = maxString; g_object.age = 42; g_object.isVis = false; - expect(g_object.name == maxString); + expect(g_object.name == maxString).assertEqual(false); console.log(TAG + "get/set maxSize string success"); } else { console.info(TAG + " object is null,set name fail"); } - done() console.log(TAG + "************* testMaxSize001 end *************"); + g_object.setSessionId(""); }) /** @@ -624,7 +626,7 @@ describe('objectStoreTest', function () { * @tc.type: FUNC * @tc.require: I4H3M8 */ - it('testPerformance001', 0, function (done) { + it('testPerformance001', 0, function () { console.log(TAG + "************* testPerformance001 start *************"); var complex_object = distributedObject.createDistributedObject({ name: undefined, @@ -632,54 +634,111 @@ describe('objectStoreTest', function () { parent: undefined, list: undefined }); - var st1; - var totalTime = 0; - var setSessionIdTime = 0; - var setTime = 0; - var onTime = 0; - var offTime = 0; + expect(complex_object == undefined).assertEqual(false); + + var startTime = new Date().getTime(); for (var i = 0;i < 100; i++) { - st1 = Date.now(); complex_object.setSessionId("session14"); - setSessionIdTime += Date.now() - st1; - if (complex_object != undefined && complex_object != null) { - expect("session14" == complex_object.__sessionId); - } else { - console.log(TAG + "testPerformance001 joinSession session14 failed"); - } - console.info(TAG + " start call watch change"); - st1 = Date.now(); + expect("session14" == complex_object.__sessionId).assertEqual(true); + complex_object.on("change", changeCallback); - onTime += Date.now() - st1; - console.info(TAG + "on change success"); - st1 = Date.now(); complex_object.name = "jack2"; complex_object.age = 20; complex_object.isVis = false; complex_object.parent = { mother: "jack1 mom1", father: "jack1 Dad1" }; - complex_object.list = [{ mother: "jack1 mom1" }, { father: "jack1 Dad1" }]; - setTime += Date.now() - st1; - expect(complex_object.name == "jack2"); - expect(complex_object.age == 20); - expect(complex_object.parent == { mother: "jack1 mom1", father: "jack1 Dad1" }); - expect(complex_object.list == [{ mother: "jack1 mom1", father: "jack1 Dad1" }]); + complex_object.list = [{ mother: "jack2 mom2" }, { father: "jack2 Dad2" }]; + expect(complex_object.name == "jack2").assertEqual(true); + expect(complex_object.age == 20).assertEqual(true); + expect(complex_object.parent.mother == "jack1 mom1").assertEqual(true); + expect(complex_object.parent.father == "jack1 Dad1").assertEqual(true); + expect(complex_object.list[0].mother == "jack2 mom2").assertEqual(true); + expect(complex_object.list[1].father == "jack2 Dad2").assertEqual(true); console.log(TAG + "start unWatch change"); - st1 = Date.now(); complex_object.off("change"); - offTime += Date.now() - st1; - totalTime += setSessionIdTime; - totalTime += setTime; - totalTime += onTime; - totalTime += offTime; console.log(TAG + "end unWatch success"); + count++; } - console.log(TAG + "totalTime = " + (totalTime / 100)); - expect(totalTime < baseLine); - done() + var endTime = new Date().getTime(); + var totalTime = endTime - startTime; + console.log("testPerformance001 totalTime = " + totalTime); + console.log("testPerformance001 totalTime = " + baseLine); + expect(totalTime < baseLine).assertEqual(true); + console.log(TAG + "************* testPerformance001 end *************"); + complex_object.setSessionId(""); + }) + + /** + * @tc.name: testSave001 + * @tc.desc: test save local + * @tc.type: FUNC + * @tc.require: I4WDAK + */ + it('testSave001', 0, async function () { + console.log(TAG + "************* testSave001 start *************"); + var g_object = distributedObject.createDistributedObject({ name: "Amy", age: 18, isVis: false }); + expect(g_object == undefined).assertEqual(false); + + g_object.setSessionId("tmpsession1"); + expect("tmpsession1" == g_object.__sessionId).assertEqual(true); + + let result = await g_object.save("local"); + expect(result.sessionId == "tmpsession1").assertEqual(true); + expect(result.version == g_object.__version).assertEqual(true); + expect(result.deviceId == "local").assertEqual(true); + + g_object.setSessionId(""); + g_object.name = undefined; + g_object.age = undefined; + g_object.isVis = undefined; + g_object.setSessionId("tmpsession1"); + + expect(g_object.name == "Amy").assertEqual(true); + expect(g_object.age == 18).assertEqual(true); + expect(g_object.isVis == false).assertEqual(true); + console.log(TAG + "************* testSave001 end *************"); + g_object.setSessionId(""); + + }) + + /** + * @tc.name: testRevokeSave001 + * @tc.desc: test save local + * @tc.type: FUNC + * @tc.require: I4WDAK + */ + it('testRevokeSave001', 0, async function () { + console.log(TAG + "************* testRevokeSave001 start *************"); + var g_object = distributedObject.createDistributedObject({ name: "Amy", age: 18, isVis: false }); + expect(g_object == undefined).assertEqual(false); + + g_object.setSessionId("123456"); + expect("123456" == g_object.__sessionId).assertEqual(true); + + let result = await g_object.save("local"); + expect(result.sessionId == "123456").assertEqual(true); + expect(result.version == g_object.__version).assertEqual(true); + expect(result.deviceId == "local").assertEqual(true); + + result = await g_object.revokeSave(); + + g_object.setSessionId(""); + g_object.name = undefined; + g_object.age = undefined; + g_object.isVis = undefined; + g_object.setSessionId("123456"); + + expect(g_object.name == undefined).assertEqual(true); + expect(g_object.age == undefined).assertEqual(true); + expect(g_object.isVis == undefined).assertEqual(true); + + expect(result.sessionId == "123456").assertEqual(true); + + console.log(TAG + "************* testRevokeSave001 end *************"); + g_object.setSessionId(""); + }) console.log(TAG + "*************Unit Test End*************"); -}) - +}) \ No newline at end of file diff --git a/frameworks/jskitsimpl/test/unittest/src/config.json b/frameworks/jskitsimpl/test/unittest/src/config.json index c5d8256..543936c 100644 --- a/frameworks/jskitsimpl/test/unittest/src/config.json +++ b/frameworks/jskitsimpl/test/unittest/src/config.json @@ -23,6 +23,23 @@ "moduleName": "entry", "moduleType": "entry" }, + "defPermissions": [ + { + "availableScope": [], + "grantMode": "user_grant", + "name": "ohos.permission.DISTRIBUTED_DATASYNC" + } + ], + "reqPermissions": [ + { + "name": "ohos.permission.GRANT_SENSITIVE_PERMISSIONS", + "reason": "ceshi" + }, + { + "name": "ohos.permission.DISTRIBUTED_DATASYNC", + "reason": "ceshi" + } + ], "abilities": [ { "visible": true, diff --git a/frameworks/jskitsimpl/test/unittest/src/openharmony_sx.p7b b/frameworks/jskitsimpl/test/unittest/src/openharmony_sx.p7b new file mode 100644 index 0000000..56e8a3d Binary files /dev/null and b/frameworks/jskitsimpl/test/unittest/src/openharmony_sx.p7b differ