From 75920a6be874e25ab970f05721ca5e1311a3f530 Mon Sep 17 00:00:00 2001 From: h00514358 Date: Sun, 9 Oct 2022 10:58:11 +0800 Subject: [PATCH] Add sensor test Signed-off-by: h00514358 --- interfaces/plugin/include/sensor_napi_error.h | 1 + interfaces/plugin/src/sensor_js.cpp | 40 ++-- interfaces/plugin/src/sensor_napi_utils.cpp | 11 +- .../test/unittest/ExampleJsunit.test.js | 174 +++++++++++++----- 4 files changed, 152 insertions(+), 74 deletions(-) diff --git a/interfaces/plugin/include/sensor_napi_error.h b/interfaces/plugin/include/sensor_napi_error.h index d5654088..06046d81 100755 --- a/interfaces/plugin/include/sensor_napi_error.h +++ b/interfaces/plugin/include/sensor_napi_error.h @@ -32,6 +32,7 @@ const std::map ERROR_MESSAGES = { napi_value CreateBusinessError(const napi_env &env, const int32_t errCode, const std::string &errMessage); void ThrowErr(const napi_env &env, const int32_t errCode, const std::string &printMsg); +std::optional GetNapiError(int32_t errorCode); } // namespace Sensors } // namespace OHOS #endif // SENSOR_NAPI_ERROR_H \ No newline at end of file diff --git a/interfaces/plugin/src/sensor_js.cpp b/interfaces/plugin/src/sensor_js.cpp index 44b5b19a..245aea79 100644 --- a/interfaces/plugin/src/sensor_js.cpp +++ b/interfaces/plugin/src/sensor_js.cpp @@ -553,9 +553,8 @@ static napi_value TransformCoordinateSystem(napi_env env, napi_callback_info inf SensorAlgorithm sensorAlgorithm; int32_t ret = sensorAlgorithm.TransformCoordinateSystem(inRotationVector, axisX, axisY, outRotationVector); if (ret != OHOS::ERR_OK) { - SEN_HILOGE("Transform coordinate system fail"); - asyncCallbackInfo->type = FAIL; - asyncCallbackInfo->error.code = ret; + ThrowErr(env, ret, "Transform coordinate system fail"); + return nullptr; } else { for (size_t i = 0; i < length; ++i) { asyncCallbackInfo->data.reserveData.reserve[i] = outRotationVector[i]; @@ -621,9 +620,8 @@ static napi_value GetAngleModify(napi_env env, napi_callback_info info) SensorAlgorithm sensorAlgorithm; int32_t ret = sensorAlgorithm.GetAngleModify(curRotationVector, preRotationVector, angleChange); if (ret != OHOS::ERR_OK) { - SEN_HILOGE("Get angle modify fail"); - asyncCallbackInfo->type = FAIL; - asyncCallbackInfo->error.code = ret; + ThrowErr(env, ret, "Get angle modify fail"); + return nullptr; } else { asyncCallbackInfo->data.reserveData.length = ROTATION_VECTOR_LENGTH; for (int32_t i = 0; i < ROTATION_VECTOR_LENGTH; ++i) { @@ -680,9 +678,8 @@ static napi_value GetDirection(napi_env env, napi_callback_info info) SensorAlgorithm sensorAlgorithm; int32_t ret = sensorAlgorithm.GetDirection(rotationMatrix, rotationAngle); if (ret != OHOS::ERR_OK) { - SEN_HILOGE("Get direction fail"); - asyncCallbackInfo->type = FAIL; - asyncCallbackInfo->error.code = ret; + ThrowErr(env, ret, "Get direction fail"); + return nullptr; } else { asyncCallbackInfo->data.reserveData.length = ROTATION_VECTOR_LENGTH; for (int32_t i = 0; i < ROTATION_VECTOR_LENGTH; ++i) { @@ -739,9 +736,8 @@ static napi_value CreateQuaternion(napi_env env, napi_callback_info info) SensorAlgorithm sensorAlgorithm; int32_t ret = sensorAlgorithm.CreateQuaternion(rotationVector, quaternion); if (ret != OHOS::ERR_OK) { - SEN_HILOGE("Create quaternin fail"); - asyncCallbackInfo->type = FAIL; - asyncCallbackInfo->error.code = ret; + ThrowErr(env, ret, "CreateQuaternion fail"); + return nullptr; } else { asyncCallbackInfo->data.reserveData.length = QUATERNION_LENGTH; for (int32_t i = 0; i < QUATERNION_LENGTH; ++i) { @@ -803,9 +799,8 @@ static napi_value GetAltitude(napi_env env, napi_callback_info info) SensorAlgorithm sensorAlgorithm; int32_t ret = sensorAlgorithm.GetAltitude(seaPressure, currentPressure, &altitude); if (ret != OHOS::ERR_OK) { - SEN_HILOGE("Get altitude fail"); - asyncCallbackInfo->type = FAIL; - asyncCallbackInfo->error.code = ret; + ThrowErr(env, ret, "Get altitude fail"); + return nullptr; } else { asyncCallbackInfo->data.reserveData.reserve[0] = altitude; } @@ -859,9 +854,8 @@ static napi_value GetGeomagneticDip(napi_env env, napi_callback_info info) SensorAlgorithm sensorAlgorithm; int32_t ret = sensorAlgorithm.GetGeomagneticDip(inclinationMatrix, &geomagneticDip); if (ret != OHOS::ERR_OK) { - SEN_HILOGE("Get geomagnetic dip fail"); - asyncCallbackInfo->type = FAIL; - asyncCallbackInfo->error.code = ret; + ThrowErr(env, ret, "Get geomagnetic dip fail"); + return nullptr; } else { asyncCallbackInfo->data.reserveData.reserve[0] = geomagneticDip; } @@ -913,9 +907,8 @@ static napi_value CreateRotationAndInclination(const napi_env &env, napi_value a SensorAlgorithm sensorAlgorithm; int32_t ret = sensorAlgorithm.CreateRotationAndInclination(gravity, geomagnetic, rotation, inclination); if (ret != OHOS::ERR_OK) { - SEN_HILOGE("Create rotation and inclination matrix fail"); - asyncCallbackInfo->type = FAIL; - asyncCallbackInfo->error.code = ret; + ThrowErr(env, ret, "Create rotation and inclination matrix fail"); + return nullptr; } else { asyncCallbackInfo->data.reserveData.length = THREE_DIMENSIONAL_MATRIX_LENGTH; for (int32_t i = 0; i < THREE_DIMENSIONAL_MATRIX_LENGTH; ++i) { @@ -968,9 +961,8 @@ static napi_value GetRotationMatrix(const napi_env &env, napi_value args[], size SensorAlgorithm sensorAlgorithm; int32_t ret = sensorAlgorithm.CreateRotationMatrix(rotationVector, rotationMatrix); if (ret != OHOS::ERR_OK) { - SEN_HILOGE("Create rotation matrix fail"); - asyncCallbackInfo->type = FAIL; - asyncCallbackInfo->error.code = ret; + ThrowErr(env, ret, "Create rotation matrix fail"); + return nullptr; } else { asyncCallbackInfo->data.reserveData.length = THREE_DIMENSIONAL_MATRIX_LENGTH; for (int32_t i = 0; i < THREE_DIMENSIONAL_MATRIX_LENGTH; ++i) { diff --git a/interfaces/plugin/src/sensor_napi_utils.cpp b/interfaces/plugin/src/sensor_napi_utils.cpp index 3ae4f794..36592f07 100644 --- a/interfaces/plugin/src/sensor_napi_utils.cpp +++ b/interfaces/plugin/src/sensor_napi_utils.cpp @@ -293,9 +293,14 @@ bool ConvertToFailData(const napi_env &env, sptr asyncCallbac { CALL_LOG_ENTER; CHKPF(asyncCallbackInfo); - result[0] = CreateBusinessError(env, asyncCallbackInfo->error.code, asyncCallbackInfo->error.message); - CHKPF(result[0]); - return true; + int32_t code = asyncCallbackInfo->error.code; + auto msg = GetNapiError(code); + if (!msg) { + SEN_HILOGE("errCode: %{public}d is invalid", code); + return false; + } + result[0] = CreateBusinessError(env, code, msg.value()); + return (result[0] != nullptr); } bool ConvertToSensorData(const napi_env &env, sptr asyncCallbackInfo, napi_value result[2]) diff --git a/interfaces/plugin/test/unittest/ExampleJsunit.test.js b/interfaces/plugin/test/unittest/ExampleJsunit.test.js index b8fd8fb6..fa57bfef 100755 --- a/interfaces/plugin/test/unittest/ExampleJsunit.test.js +++ b/interfaces/plugin/test/unittest/ExampleJsunit.test.js @@ -2215,17 +2215,23 @@ describe("SensorJsTest", function () { * @tc.author: */ it('Sensor_GetDirection_005', 0, async function (done) { - sensor.getOrientation([1,2,3,1,2,3,1,2,3,0]).then((data) => { - for (var i = 0; i { + for (var i = 0; i{ expect(false).assertTrue(); - } - done() - }, (error) =>{ - expect(true).assertTrue(); - console.info("Sensor_GetDirection_005 success") + console.info("Sensor_GetDirection_005 success") + done() + }) + } catch (err) { + expect(err.code).assertEqual(PARAMETER_ERROR_CODE) + expect(err.message).assertEqual(PARAMETER_ERROR_MSG) done() - }) + } }) /** @@ -2850,15 +2856,20 @@ describe("SensorJsTest", function () { */ it('Sensor_CreateQuaterniont_005', 0,async function (done) { console.info('Sensor_CreateQuaterniont_005 start') - sensor.getQuaternion([0.25, 0.14], (error, data) =>{ - if (error) { - console.info('Sensor_CreateQuaterniont_005 failed'); - expect(true).assertTrue(); - } else { - expect(false).assertTrue(); - } + try { + sensor.getQuaternion([0.25, 0.14], (error, data) =>{ + if (error) { + expect(false).assertTrue(); + } else { + expect(false).assertTrue(); + } + done() + }) + } catch (err) { + expect(err.code).assertEqual(PARAMETER_ERROR_CODE) + expect(err.message).assertEqual(PARAMETER_ERROR_MSG) done() - }) + } console.info("Sensor_CreateQuaterniont_005 end") }) @@ -2893,15 +2904,21 @@ describe("SensorJsTest", function () { */ it('Sensor_CreateQuaterniont_007', 0,async function (done) { console.info('Sensor_CreateQuaterniont_007 start') - sensor.getQuaternion([0, 0]).then((data) => { - console.info('Sensor_CreateQuaterniont_007'); - expect(false).assertTrue(); - done() - }, (error) => { - expect(true).assertTrue(); - console.info('promise failed') + try { + sensor.getQuaternion([0, 0]).then((data) => { + console.info('Sensor_CreateQuaterniont_007'); + expect(false).assertTrue(); + done() + }, (error) => { + expect(false).assertTrue(); + console.info('promise failed') + done() + }) + } catch (err) { + expect(err.code).assertEqual(PARAMETER_ERROR_CODE) + expect(err.message).assertEqual(PARAMETER_ERROR_MSG) done() - }) + } console.info("Sensor_CreateQuaterniont_007 end") }) @@ -2958,15 +2975,21 @@ describe("SensorJsTest", function () { */ it('Sensor_CreateQuaterniont_010', 0,async function (done) { console.info('Sensor_CreateQuaterniont_010 start') - sensor.getQuaternion([0.25, 0.14]).then((data) => { - console.info('Sensor_CreateQuaterniont_010'); - expect(false).assertTrue(); - done() - },(error) => { - expect(true).assertTrue(); - console.info('promise failed') + try { + sensor.getQuaternion([0.25, 0.14]).then((data) => { + console.info('Sensor_CreateQuaterniont_010'); + expect(false).assertTrue(); + done() + },(error) => { + expect(false).assertTrue(); + console.info('promise failed') + done() + }) + } catch (err) { + expect(err.code).assertEqual(PARAMETER_ERROR_CODE) + expect(err.message).assertEqual(PARAMETER_ERROR_MSG) done() - }) + } }) /* @@ -3037,7 +3060,7 @@ describe("SensorJsTest", function () { expect(true).assertfalse() done() }, (error) =>{ - expect(true).assertfalse() + expect(false).assertfalse() done() }) } catch (err) { @@ -3061,7 +3084,7 @@ describe("SensorJsTest", function () { expect(true).assertfalse() done() }, (error) =>{ - expect(true).assertfalse() + expect(false).assertfalse() done() }) } catch (err) { @@ -3085,7 +3108,7 @@ describe("SensorJsTest", function () { expect(true).assertfalse() done() }, (error) =>{ - expect(true).assertfalse() + expect(false).assertfalse() done() }) } catch (err) { @@ -3127,16 +3150,20 @@ describe("SensorJsTest", function () { */ it('Sensor_GetGeomagneticDip_002', 0, async function (done) { console.info('Sensor_GetGeomagneticDip_002 start') - sensor.getInclination([1, 2, 3, 4], (error,data) => { - if (error) { - console.info('Sensor_GetGeomagneticDip_002 success'); - expect(true).assertTrue(); - } else { - console.info("Sensor_GetGeomagneticDip_002 failed") - expect(false).assertTrue(); - } + try { + sensor.getInclination([1, 2, 3, 4], (error,data) => { + if (error) { + expect(false).assertTrue(); + } else { + expect(false).assertTrue(); + } + done() + }) + } catch (err) { + expect(err.code).assertEqual(PARAMETER_ERROR_CODE) + expect(err.message).assertEqual(PARAMETER_ERROR_MSG) done() - }) + } console.info("Sensor_GetGeomagneticDip_002 end") }) @@ -3544,7 +3571,6 @@ describe("SensorJsTest", function () { expect(data).assertEqual(getGeomagneticDipResult[0]) } done() - console.info('Sensor_TransformCoordinateSystem_006' + 'lengh:' + data.length); }) } catch(error) { console.info(error); @@ -3607,6 +3633,60 @@ describe("SensorJsTest", function () { console.info("Sensor_TransformCoordinateSystem_008 end") }) + /* + * @tc.name: Sensor_TransformCoordinateSystem_009 + * @tc.desc: Verfication results of the incorrect parameters of test interface. + * @tc.require: I5SWJI + * @tc.author: + */ + it('Sensor_TransformCoordinateSystem_009', 0, async function (done) { + console.info('Sensor_TransformCoordinateSystem_008 start') + try { + sensor.transformRotationMatrix([0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0], {'axisX':1, 'axisY':1}).then((data)=>{ + console.info("Sensor_TransformCoordinateSystem_009" + data) + expect(true).assertfalse() + done() + }, (error)=>{ + expect(true).assertfalse() + done() + }) + } catch(error) { + console.info(error); + expect(error.code).assertEqual(PARAMETER_ERROR_CODE); + expect(error.message).assertEqual(PARAMETER_ERROR_MSG); + done(); + } + console.info("Sensor_TransformCoordinateSystem_009 end") + }) + + /* + * @tc.name: Sensor_TransformCoordinateSystem_010 + * @tc.desc: Verfication results of the incorrect parameters of test interface. + * @tc.require: I5SWJI + * @tc.author: + */ + it('Sensor_TransformCoordinateSystem_010', 0, async function (done) { + console.info('Sensor_TransformCoordinateSystem_010 start') + try { + sensor.transformRotationMatrix([0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0], {'axisX':1, 'axisY':1}, (error, data) => { + if (error) { + console.info('Sensor_TransformCoordinateSystem_010 failed'); + expect(false).assertTrue(); + } else { + console.info("Sensor_TransformCoordinateSystem_010" + data) + expect(data).assertEqual(getGeomagneticDipResult[0]) + } + done() + }) + } catch(error) { + console.info(error); + expect(error.code).assertEqual(PARAMETER_ERROR_CODE); + expect(error.message).assertEqual(PARAMETER_ERROR_MSG); + done(); + } + console.info("Sensor_TransformCoordinateSystem_010 end") + }) + /* * @tc.name: Sensor_GetSensorList_001 * @tc.desc: Verfication results of the incorrect parameters of test interface. @@ -3681,7 +3761,7 @@ describe("SensorJsTest", function () { try { sensor.getSensorList(-1).then(data => { console.info("Sensor_GetSingleSensor_003 " + JSON.stringify(data)); - expect(true).assertTrue(); + expect(false).assertTrue(); done(); }), (error => { console.info(error); -- Gitee