diff --git a/interfaces/plugin/test/ExampleJsunit.test_acc.js b/interfaces/plugin/test/ExampleJsunit.test_acc.js index e863c356d464a02efaf64d7b278dd25c72138cff..e308fd102061c61ce923e0d46801a3135442b8ba 100644 --- a/interfaces/plugin/test/ExampleJsunit.test_acc.js +++ b/interfaces/plugin/test/ExampleJsunit.test_acc.js @@ -24,7 +24,7 @@ describe("SensorJsTest", function () { expect(typeof(data.z)).assertEqual("number"); } - function callback2() { + function callback2(data) { console.info("callback2" + JSON.stringify(data)); expect(typeof(data.x)).assertEqual("number"); expect(typeof(data.y)).assertEqual("number"); @@ -66,9 +66,9 @@ describe("SensorJsTest", function () { */ it("SensorJsTest001", 0, async function (done) { console.info('----------------------SensorJsTest001---------------------------'); - sensor.on(1, callback); + sensor.on(sensor.SensorType.SENSOR_TYPE_ID_ACCELEROMETER, callback); setTimeout(()=>{ - sensor.off(1); + sensor.off(sensor.SensorType.SENSOR_TYPE_ID_ACCELEROMETER); done(); }, 500); }) @@ -81,14 +81,11 @@ describe("SensorJsTest", function () { */ it("SensorJsTest002", 0, async function (done) { console.info('----------------------SensorJsTest002---------------------------'); - function onSensorCallback(data) { - console.info('SensorJsTest002 on error'); - expect(false).assertTrue(); - done(); - } - sensor.on(-1, onSensorCallback); + sensor.on(sensor.SensorType.SENSOR_TYPE_ID_ACCELEROMETER, callback, {'interval': 100000000}); setTimeout(()=>{ - expect(true).assertTrue(); + console.info('----------------------SensorJsTest002 off in---------------------------'); + sensor.off(sensor.SensorType.SENSOR_TYPE_ID_ACCELEROMETER); + console.info('----------------------SensorJsTest002 off end---------------------------'); done(); }, 500); }) @@ -101,13 +98,18 @@ describe("SensorJsTest", function () { */ it("SensorJsTest003", 0, async function (done) { console.info('----------------------SensorJsTest003---------------------------'); - sensor.on(1, callback, {'interval': 100000000}); - setTimeout(()=>{ - console.info('----------------------SensorJsTest003 off in---------------------------'); - sensor.off(1); - console.info('----------------------SensorJsTest003 off end---------------------------'); + function onSensorCallback(data) { + console.info('SensorJsTest003 on error'); + expect(false).assertTrue(); done(); - }, 500); + } + try { + sensor.on(sensor.SensorType.SENSOR_TYPE_ID_ACCELEROMETER, onSensorCallback, {'interval': 100000000}, 5); + } catch (error) { + console.info(error); + expect(true).assertTrue(); + done(); + } }) /* @@ -116,19 +118,12 @@ describe("SensorJsTest", function () { * @tc.type: FUNC * @tc.require: Issue Number */ - it("SensorJsTest004", 0, function () { - console.info('----------------------SensorJsTest004---------------------------'); - function onSensorCallback(data) { - console.info('SensorJsTest004 on error'); - expect(false).assertTrue(); - done(); - } - sensor.on(1, onSensorCallback, {'interval': 100000000}, 5); + it("SensorJsTest004", 0, async function (done) { + sensor.once(sensor.SensorType.SENSOR_TYPE_ID_ACCELEROMETER, callback); setTimeout(()=>{ expect(true).assertTrue(); done(); }, 500); - console.info('----------------------SensorJsTest004--------------------------- end'); }) /* @@ -138,11 +133,18 @@ describe("SensorJsTest", function () { * @tc.require: Issue Number */ it("SensorJsTest005", 0, async function (done) { - sensor.once(1, callback); - setTimeout(()=>{ + function onceSensorCallback(data) { + console.info('SensorJsTest005 on error'); + expect(false).assertTrue(); + done(); + } + try{ + sensor.once(sensor.SensorType.SENSOR_TYPE_ID_ACCELEROMETER, onceSensorCallback, 5); + } catch (error) { + console.info(error); expect(true).assertTrue(); done(); - }, 500); + } }) /* @@ -152,16 +154,13 @@ describe("SensorJsTest", function () { * @tc.require: Issue Number */ it("SensorJsTest006", 0, async function (done) { - function onceSensorCallback(data) { - console.info('SensorJsTest006 on error'); - expect(false).assertTrue(); - done(); - } - sensor.once(-1, onceSensorCallback); - setTimeout(()=>{ + try { + sensor.off(string, ""); + } catch (error) { + console.info(error); expect(true).assertTrue(); done(); - }, 500); + } }) /* @@ -170,13 +169,14 @@ describe("SensorJsTest", function () { * @tc.type: FUNC * @tc.require: Issue Number */ - it("SensorJsTest007", 0, function () { - function onceSensorCallback(data) { + it("SensorJsTest007", 0, async function (done) { + function onSensorCallback(data) { console.info('SensorJsTest007 on error'); expect(false).assertTrue(); done(); } - sensor.once(1, onceSensorCallback, 5); + sensor.on(sensor.SensorType.SENSOR_TYPE_ID_ACCELEROMETER, onSensorCallback); + sensor.off(sensor.SensorType.SENSOR_TYPE_ID_ACCELEROMETER, onSensorCallback); setTimeout(()=>{ expect(true).assertTrue(); done(); @@ -190,11 +190,18 @@ describe("SensorJsTest", function () { * @tc.require: Issue Number */ it("SensorJsTest008", 0, async function (done) { - sensor.off(-1, callback); - setTimeout(()=>{ + function onSensorCallback(data) { + console.info('SensorJsTest008 on error'); + expect(false).assertTrue(); + done(); + } + try { + sensor.off(1000000, onSensorCallback); + } catch (error) { + console.info(error); expect(true).assertTrue(); done(); - }, 500); + } }) /* @@ -204,17 +211,14 @@ describe("SensorJsTest", function () { * @tc.require: Issue Number */ it("SensorJsTest009", 0, async function (done) { - function onSensorCallback(data) { - console.info('SensorJsTest009 on error'); - expect(false).assertTrue(); - done(); - } - sensor.on(1, onSensorCallback); - sensor.off(1, onSensorCallback); + sensor.on(sensor.SensorType.SENSOR_TYPE_ID_ACCELEROMETER, callback); + sensor.on(sensor.SensorType.SENSOR_TYPE_ID_ACCELEROMETER, callback2); setTimeout(()=>{ - expect(true).assertTrue(); + console.info('----------------------SensorJsTest009 off in---------------------------'); + sensor.off(sensor.SensorType.SENSOR_TYPE_ID_ACCELEROMETER); + console.info('----------------------SensorJsTest009 off end---------------------------'); done(); - }, 500); + }, 1000); }) /* @@ -224,16 +228,19 @@ describe("SensorJsTest", function () { * @tc.require: Issue Number */ it("SensorJsTest010", 0, async function (done) { - function onSensorCallback(data) { - console.info('SensorJsTest010 on error'); - expect(false).assertTrue(); - done(); - } - sensor.off(1000000, onSensorCallback); + sensor.on(sensor.SensorType.SENSOR_TYPE_ID_ACCELEROMETER, callback); + sensor.on(sensor.SensorType.SENSOR_TYPE_ID_ACCELEROMETER, callback2); setTimeout(()=>{ - expect(true).assertTrue(); - done(); + console.info('----------------------SensorJsTest010 off in---------------------------'); + sensor.off(sensor.SensorType.SENSOR_TYPE_ID_ACCELEROMETER, callback); + console.info('----------------------SensorJsTest010 off end---------------------------'); }, 500); + setTimeout(()=>{ + console.info('----------------------SensorJsTest010 off in---------------------------'); + sensor.off(sensor.SensorType.SENSOR_TYPE_ID_ACCELEROMETER, callback2); + console.info('----------------------SensorJsTest010 off end---------------------------'); + done(); + }, 1000); }) /* @@ -243,11 +250,12 @@ describe("SensorJsTest", function () { * @tc.require: Issue Number */ it("SensorJsTest011", 0, async function (done) { - sensor.on(1, callback); - sensor.on(1, callback2); + console.info('----------------------SensorJsTest011---------------------------'); + sensor.on(sensor.SensorType.SENSOR_TYPE_ID_ACCELEROMETER, callback, {'interval': 100000000}); + sensor.once(sensor.SensorType.SENSOR_TYPE_ID_ACCELEROMETER, callback2); setTimeout(()=>{ console.info('----------------------SensorJsTest011 off in---------------------------'); - sensor.off(1); + sensor.off(sensor.SensorType.SENSOR_TYPE_ID_ACCELEROMETER); console.info('----------------------SensorJsTest011 off end---------------------------'); done(); }, 1000); @@ -260,81 +268,37 @@ describe("SensorJsTest", function () { * @tc.require: Issue Number */ it("SensorJsTest012", 0, async function (done) { - sensor.on(1, callback); - sensor.on(1, callback2); + console.info('----------------------SensorJsTest012---------------------------'); + sensor.on(sensor.SensorType.SENSOR_TYPE_ID_ACCELEROMETER, callback, {'interval': 100000000}); + sensor.on(sensor.SensorType.SENSOR_TYPE_ID_ACCELEROMETER, callback2, {'interval': 100000000}); setTimeout(()=>{ console.info('----------------------SensorJsTest012 off in---------------------------'); - sensor.off(1, callback); - sensor.off(1, callback2); + sensor.off(sensor.SensorType.SENSOR_TYPE_ID_ACCELEROMETER, callback); console.info('----------------------SensorJsTest012 off end---------------------------'); - done(); - }, 1000); - }) - - /* - * @tc.name:SensorJsTest013 - * @tc.desc:verify app info is not null - * @tc.type: FUNC - * @tc.require: Issue Number - */ - it("SensorJsTest013", 0, function () { - sensor.off(1, 5); - expect(true).assertTrue(); - }) - - /* - * @tc.name:SensorJsTest014 - * @tc.desc:verify app info is not null - * @tc.type: FUNC - * @tc.require: Issue Number - */ - it("SensorJsTest014", 0, async function (done) { - console.info('----------------------SensorJsTest014---------------------------'); - sensor.on(1, callback, {'interval': 100000000}); - sensor.once(1, callback2); - setTimeout(()=>{ - console.info('----------------------SensorJsTest014 off in---------------------------'); - sensor.off(1, callback); - sensor.off(1, callback2); - console.info('----------------------SensorJsTest014 off end---------------------------'); - done(); - }, 1000); - }) - - /* - * @tc.name:SensorJsTest015 - * @tc.desc:verify app info is not null - * @tc.type: FUNC - * @tc.require: Issue Number - */ - it("SensorJsTest015", 0, async function (done) { - console.info('----------------------SensorJsTest015---------------------------'); - sensor.on(1, callback, {'interval': 100000000}); - sensor.on(1, callback2, {'interval': 100000000}); + }, 500); setTimeout(()=>{ - console.info('----------------------SensorJsTest015 off in---------------------------'); - sensor.off(1, callback); - sensor.off(1, callback2); - console.info('----------------------SensorJsTest015 off end---------------------------'); + console.info('----------------------SensorJsTest012 off in---------------------------'); + sensor.off(sensor.SensorType.SENSOR_TYPE_ID_ACCELEROMETER, callback2); + console.info('----------------------SensorJsTest012 off end---------------------------'); done(); }, 1000); }) /* - * @tc.name:SensorJsTest016 + * @tc.name:SensorJsTest013 * @tc.desc:verify app info is not null * @tc.type: FUNC * @tc.require: Issue Number */ - it("SensorJsTest016", 0, async function (done) { - console.info('----------------------SensorJsTest016---------------------------'); - sensor.on(1, callback, {'interval': 100000000}); - sensor.on(1, callback2, {'interval': 100000000}); + it("SensorJsTest013", 0, async function (done) { + console.info('----------------------SensorJsTest013---------------------------'); + sensor.on(sensor.SensorType.SENSOR_TYPE_ID_ACCELEROMETER, callback, {'interval': 100000000}); + sensor.on(sensor.SensorType.SENSOR_TYPE_ID_ACCELEROMETER, callback2, {'interval': 100000000}); setTimeout(()=>{ - console.info('----------------------SensorJsTest016 off in---------------------------'); - sensor.off(1); - console.info('----------------------SensorJsTest016 off end---------------------------'); + console.info('----------------------SensorJsTest013 off in---------------------------'); + sensor.off(sensor.SensorType.SENSOR_TYPE_ID_ACCELEROMETER); + console.info('----------------------SensorJsTest013 off end---------------------------'); done(); }, 1000); }) -}) +}) \ No newline at end of file diff --git a/interfaces/plugin/test/ExampleJsunit.test_ambientlight.js b/interfaces/plugin/test/ExampleJsunit.test_ambientlight.js new file mode 100644 index 0000000000000000000000000000000000000000..95c157dc1703cbdaa5cc0fce43d80dec2900ba6d --- /dev/null +++ b/interfaces/plugin/test/ExampleJsunit.test_ambientlight.js @@ -0,0 +1,301 @@ +/* + * Copyright (C) 2021 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. + */ +import sensor from '@ohos.sensor' + +import {describe, beforeAll, beforeEach, afterEach, afterAll, it, expect} from 'deccjsunit/index' + +describe("SensorJsTest", function () { + function callback(data) { + console.info("callback" + JSON.stringify(data)); + expect(typeof(data.intensity)).assertEqual("number"); + } + + function callback2(data) { + console.info("callback2" + JSON.stringify(data)); + expect(typeof(data.intensity)).assertEqual("number"); + } + + beforeAll(function(data) { + /* + * @tc.setup: setup invoked before all testcases + */ + console.info('beforeAll caled') + }) + + afterAll(function() { + /* + * @tc.teardown: teardown invoked after all testcases + */ + console.info('afterAll caled') + }) + + beforeEach(function() { + /* + * @tc.setup: setup invoked before each testcases + */ + console.info('beforeEach caled') + }) + + afterEach(function() { + /* + * @tc.teardown: teardown invoked after each testcases + */ + console.info('afterEach caled') + }) + + /* + * @tc.name:SensorJsTest001 + * @tc.desc:verify app info is not null + * @tc.type: FUNC + * @tc.require: Issue Number + */ + it("SensorJsTest001", 0, async function (done) { + console.info('----------------------SensorJsTest001---------------------------'); + sensor.on(sensor.SensorType.SENSOR_TYPE_ID_AMBIENT_LIGHT, callback); + setTimeout(()=>{ + sensor.off(sensor.SensorType.SENSOR_TYPE_ID_AMBIENT_LIGHT); + done(); + }, 500); + }) + + /* + * @tc.name:SensorJsTest002 + * @tc.desc:verify app info is not null + * @tc.type: FUNC + * @tc.require: Issue Number + */ + it("SensorJsTest002", 0, async function (done) { + console.info('----------------------SensorJsTest002---------------------------'); + sensor.on(sensor.SensorType.SENSOR_TYPE_ID_AMBIENT_LIGHT, callback, {'interval': 100000000}); + setTimeout(()=>{ + console.info('----------------------SensorJsTest002 off in---------------------------'); + sensor.off(sensor.SensorType.SENSOR_TYPE_ID_AMBIENT_LIGHT); + console.info('----------------------SensorJsTest002 off end---------------------------'); + done(); + }, 500); + }) + + /* + * @tc.name:SensorJsTest003 + * @tc.desc:verify app info is not null + * @tc.type: FUNC + * @tc.require: Issue Number + */ + it("SensorJsTest003", 0, function (done) { + console.info('----------------------SensorJsTest003---------------------------'); + function onSensorCallback(data) { + console.info('SensorJsTest003 on error'); + expect(false).assertTrue(); + done(); + } + try { + sensor.on(sensor.SensorType.SENSOR_TYPE_ID_AMBIENT_LIGHT, onSensorCallback, {'interval': 100000000}, 5); + } catch (error) { + console.info(error); + expect(true).assertTrue(); + done(); + } + }) + + /* + * @tc.name:SensorJsTest004 + * @tc.desc:verify app info is not null + * @tc.type: FUNC + * @tc.require: Issue Number + */ + it("SensorJsTest004", 0, async function (done) { + sensor.once(sensor.SensorType.SENSOR_TYPE_ID_AMBIENT_LIGHT, callback); + setTimeout(()=>{ + expect(true).assertTrue(); + done(); + }, 500); + }) + + /* + * @tc.name:SensorJsTest005 + * @tc.desc:verify app info is not null + * @tc.type: FUNC + * @tc.require: Issue Number + */ + it("SensorJsTest005", 0, function (done) { + function onceSensorCallback(data) { + console.info('SensorJsTest005 on error'); + expect(false).assertTrue(); + done(); + } + try{ + sensor.once(sensor.SensorType.SENSOR_TYPE_ID_AMBIENT_LIGHT, onceSensorCallback, 5); + } catch (error) { + console.info(error); + expect(true).assertTrue(); + done(); + } + }) + + /* + * @tc.name:SensorJsTest006 + * @tc.desc:verify app info is not null + * @tc.type: FUNC + * @tc.require: Issue Number + */ + it("SensorJsTest006", 0, async function (done) { + try { + sensor.off(string, ""); + } catch (error) { + console.info(error); + expect(true).assertTrue(); + done(); + } + }) + + /* + * @tc.name:SensorJsTest007 + * @tc.desc:verify app info is not null + * @tc.type: FUNC + * @tc.require: Issue Number + */ + it("SensorJsTest007", 0, async function (done) { + function onSensorCallback(data) { + console.info('SensorJsTest007 on error'); + expect(false).assertTrue(); + done(); + } + sensor.on(sensor.SensorType.SENSOR_TYPE_ID_AMBIENT_LIGHT, onSensorCallback); + sensor.off(sensor.SensorType.SENSOR_TYPE_ID_AMBIENT_LIGHT, onSensorCallback); + setTimeout(()=>{ + expect(true).assertTrue(); + done(); + }, 500); + }) + + /* + * @tc.name:SensorJsTest008 + * @tc.desc:verify app info is not null + * @tc.type: FUNC + * @tc.require: Issue Number + */ + it("SensorJsTest008", 0, async function (done) { + function onSensorCallback(data) { + console.info('SensorJsTest008 on error'); + expect(false).assertTrue(); + done(); + } + try { + sensor.off(1000000, onSensorCallback); + } catch (error) { + console.info(error); + expect(true).assertTrue(); + done(); + } + }) + + /* + * @tc.name:SensorJsTest009 + * @tc.desc:verify app info is not null + * @tc.type: FUNC + * @tc.require: Issue Number + */ + it("SensorJsTest009", 0, async function (done) { + sensor.on(sensor.SensorType.SENSOR_TYPE_ID_AMBIENT_LIGHT, callback); + sensor.on(sensor.SensorType.SENSOR_TYPE_ID_AMBIENT_LIGHT, callback2); + setTimeout(()=>{ + console.info('----------------------SensorJsTest009 off in---------------------------'); + sensor.off(sensor.SensorType.SENSOR_TYPE_ID_AMBIENT_LIGHT); + console.info('----------------------SensorJsTest009 off end---------------------------'); + done(); + }, 1000); + }) + + /* + * @tc.name:SensorJsTest010 + * @tc.desc:verify app info is not null + * @tc.type: FUNC + * @tc.require: Issue Number + */ + it("SensorJsTest010", 0, async function (done) { + sensor.on(sensor.SensorType.SENSOR_TYPE_ID_AMBIENT_LIGHT, callback); + sensor.on(sensor.SensorType.SENSOR_TYPE_ID_AMBIENT_LIGHT, callback2); + setTimeout(()=>{ + console.info('----------------------SensorJsTest010 off in---------------------------'); + sensor.off(sensor.SensorType.SENSOR_TYPE_ID_AMBIENT_LIGHT, callback); + console.info('----------------------SensorJsTest010 off end---------------------------'); + }, 500); + setTimeout(()=>{ + console.info('----------------------SensorJsTest010 off in---------------------------'); + sensor.off(sensor.SensorType.SENSOR_TYPE_ID_AMBIENT_LIGHT, callback2); + console.info('----------------------SensorJsTest010 off end---------------------------'); + done(); + }, 1000); + }) + + /* + * @tc.name:SensorJsTest011 + * @tc.desc:verify app info is not null + * @tc.type: FUNC + * @tc.require: Issue Number + */ + it("SensorJsTest011", 0, async function (done) { + console.info('----------------------SensorJsTest011---------------------------'); + sensor.on(sensor.SensorType.SENSOR_TYPE_ID_AMBIENT_LIGHT, callback, {'interval': 100000000}); + sensor.once(sensor.SensorType.SENSOR_TYPE_ID_AMBIENT_LIGHT, callback2); + setTimeout(()=>{ + console.info('----------------------SensorJsTest011 off in---------------------------'); + sensor.off(sensor.SensorType.SENSOR_TYPE_ID_AMBIENT_LIGHT); + console.info('----------------------SensorJsTest011 off end---------------------------'); + done(); + }, 1000); + }) + + /* + * @tc.name:SensorJsTest012 + * @tc.desc:verify app info is not null + * @tc.type: FUNC + * @tc.require: Issue Number + */ + it("SensorJsTest012", 0, async function (done) { + console.info('----------------------SensorJsTest012---------------------------'); + sensor.on(sensor.SensorType.SENSOR_TYPE_ID_AMBIENT_LIGHT, callback, {'interval': 100000000}); + sensor.on(sensor.SensorType.SENSOR_TYPE_ID_AMBIENT_LIGHT, callback2, {'interval': 100000000}); + setTimeout(()=>{ + console.info('----------------------SensorJsTest012 off in---------------------------'); + sensor.off(sensor.SensorType.SENSOR_TYPE_ID_AMBIENT_LIGHT, callback); + console.info('----------------------SensorJsTest012 off end---------------------------'); + }, 500); + setTimeout(()=>{ + console.info('----------------------SensorJsTest012 off in---------------------------'); + sensor.off(sensor.SensorType.SENSOR_TYPE_ID_AMBIENT_LIGHT, callback2); + console.info('----------------------SensorJsTest012 off end---------------------------'); + done(); + }, 1000); + }) + + /* + * @tc.name:SensorJsTest013 + * @tc.desc:verify app info is not null + * @tc.type: FUNC + * @tc.require: Issue Number + */ + it("SensorJsTest013", 0, async function (done) { + console.info('----------------------SensorJsTest013---------------------------'); + sensor.on(sensor.SensorType.SENSOR_TYPE_ID_AMBIENT_LIGHT, callback, {'interval': 100000000}); + sensor.on(sensor.SensorType.SENSOR_TYPE_ID_AMBIENT_LIGHT, callback2, {'interval': 100000000}); + setTimeout(()=>{ + console.info('----------------------SensorJsTest013 off in---------------------------'); + sensor.off(sensor.SensorType.SENSOR_TYPE_ID_AMBIENT_LIGHT); + console.info('----------------------SensorJsTest013 off end---------------------------'); + done(); + }, 1000); + }) +}) \ No newline at end of file diff --git a/interfaces/plugin/test/unittest/config.json b/interfaces/plugin/test/unittest/config.json index 2ca858952259de571e07105b05eca8eaed0d7db4..b4bc5993bb2744aa49d7b454fbb54ded36840da8 100755 --- a/interfaces/plugin/test/unittest/config.json +++ b/interfaces/plugin/test/unittest/config.json @@ -13,6 +13,18 @@ }, "deviceConfig": {}, "module": { + "reqPermissions": [ + { + "name": "ohos.permission.ACCELEROMETER", + "reason": "", + "usedScene": { + "ability": [ + ".MainAbility" + ], + "when": "inuse" + } + } + ], "package": "com.example.myapplication", "name": ".MyApplication", "deviceType": [