From e0da9b4fea40e812a1dc9c6ee4c1b1d9da8b8e58 Mon Sep 17 00:00:00 2001 From: h00514358 Date: Tue, 26 Oct 2021 09:45:03 +0000 Subject: [PATCH 1/4] Signed-off-by:hellohyh001 Signed-off-by: h00514358 --- .../native/sensor/src/sensor_agent_proxy.cpp | 5 + interfaces/plugin/src/sensor_js.cpp | 4 +- interfaces/plugin/src/sensor_napi_utils.cpp | 7 +- interfaces/plugin/test/unittest/BUILD.gn | 46 +- .../test/unittest/ExampleJsunit.test.js | 598 ++++++++++++++++++ interfaces/plugin/test/unittest/config.json | 61 ++ .../plugin/test/unittest/sensor_js_test.cpp | 595 ----------------- utils/src/report_data_callback.cpp | 3 +- 8 files changed, 677 insertions(+), 642 deletions(-) create mode 100755 interfaces/plugin/test/unittest/ExampleJsunit.test.js create mode 100755 interfaces/plugin/test/unittest/config.json delete mode 100755 interfaces/plugin/test/unittest/sensor_js_test.cpp diff --git a/frameworks/native/sensor/src/sensor_agent_proxy.cpp b/frameworks/native/sensor/src/sensor_agent_proxy.cpp index 09adc437..a61ea3b9 100755 --- a/frameworks/native/sensor/src/sensor_agent_proxy.cpp +++ b/frameworks/native/sensor/src/sensor_agent_proxy.cpp @@ -126,6 +126,10 @@ void SensorAgentProxy::HandleSensorData(struct SensorEvent *events, int32_t num, struct SensorEvent eventStream; for (int32_t i = 0; i < num; ++i) { eventStream = events[i]; + float *data = (float*)eventStream.data; + if (eventStream.sensorTypeId == 1 || eventStream.sensorTypeId == 2 || eventStream.sensorTypeId == 6) { + HiLog::Error(LABEL, "%{public}s data x: %{public}f, y: %{public}f, z: %{public}f", __func__, (*data), *(data + 1), *(data + 2)); + } if (eventStream.data == nullptr || g_subscribeMap[eventStream.sensorTypeId] == nullptr) { HiLog::Error(LABEL, "%{public}s data or sensorUser is nullptr", __func__); return; @@ -192,6 +196,7 @@ int32_t SensorAgentProxy::ActivateSensor(int32_t sensorId, const SensorUser *use g_reportInterval = -1; if (ret != 0) { HiLog::Error(LABEL, "%{public}s enable sensor failed, ret: %{public}d", __func__, ret); + g_subscribeMap.erase(sensorId); return OHOS::Sensors::ERROR; } return OHOS::Sensors::SUCCESS; diff --git a/interfaces/plugin/src/sensor_js.cpp b/interfaces/plugin/src/sensor_js.cpp index 0e51f8aa..8e52bd8d 100755 --- a/interfaces/plugin/src/sensor_js.cpp +++ b/interfaces/plugin/src/sensor_js.cpp @@ -51,7 +51,7 @@ static void DataCallbackImpl(SensorEvent *event) if ((int32_t)onCallbackInfo.first == sensorTypeId) { onCallbackInfo.second->sensorTypeId = sensorTypeId; onCallbackInfo.second->sensorDataLength = event->dataLen; - if (memcpy_s(onCallbackInfo.second->sensorData, sizeof(data), data, sizeof(data)) != EOK) { + if (memcpy_s(onCallbackInfo.second->sensorData, event->dataLen, data, event->dataLen) != EOK) { HiLog::Error(LABEL, "%{public}s copy data failed", __func__); return; } @@ -66,7 +66,7 @@ static void DataCallbackImpl(SensorEvent *event) struct AsyncCallbackInfo *onceCallbackInfo = g_onceCallbackInfos[sensorTypeId]; onceCallbackInfo->sensorTypeId = sensorTypeId; onceCallbackInfo->sensorDataLength = event->dataLen; - if (memcpy_s(onceCallbackInfo->sensorData, sizeof(data), data, sizeof(data)) != EOK) { + if (memcpy_s(onceCallbackInfo->sensorData, event->dataLen, data, event->dataLen) != EOK) { HiLog::Error(LABEL, "%{public}s copy data failed", __func__); return; } diff --git a/interfaces/plugin/src/sensor_napi_utils.cpp b/interfaces/plugin/src/sensor_napi_utils.cpp index e11ae61f..c5f611d8 100755 --- a/interfaces/plugin/src/sensor_napi_utils.cpp +++ b/interfaces/plugin/src/sensor_napi_utils.cpp @@ -119,7 +119,7 @@ void EmitAsyncCallbackWork(AsyncCallbackInfo *asyncCallbackInfo) [](napi_env env, void* data) {}, [](napi_env env, napi_status status, void* data) { HiLog::Debug(LABEL, "%{public}s napi_create_async_work in", __func__); - AsyncCallbackInfo *asyncCallbackInfo = (AsyncCallbackInfo *)data; + AsyncCallbackInfo *asyncCallbackInfo = reinterpret_cast(data); napi_value callback; napi_get_reference_value(env, asyncCallbackInfo->callback[0], &callback); napi_value callResult = nullptr; @@ -145,11 +145,11 @@ void EmitAsyncCallbackWork(AsyncCallbackInfo *asyncCallbackInfo) return; } std::vector sensorAttribute = g_sensorAttributeList[sensorTypeId]; - float *sensorData = asyncCallbackInfo->sensorData; napi_create_object(env, &result[1]); for (size_t i = 0; i < sensorAttribute.size(); i++) { napi_value message = nullptr; - double a = *sensorData; + double a = asyncCallbackInfo->sensorData[i]; + HiLog::Info(LABEL, "%{public}s data id %{public}f", __func__, a); napi_create_double(env, a, &message); napi_set_named_property(env, result[1], sensorAttribute[i].c_str(), message); } @@ -164,7 +164,6 @@ void EmitAsyncCallbackWork(AsyncCallbackInfo *asyncCallbackInfo) asyncCallbackInfo = nullptr; } HiLog::Debug(LABEL, "%{public}s napi_create_async_work left", __func__); - free(result); }, asyncCallbackInfo, &asyncCallbackInfo->asyncWork); napi_queue_async_work(asyncCallbackInfo->env, asyncCallbackInfo->asyncWork); diff --git a/interfaces/plugin/test/unittest/BUILD.gn b/interfaces/plugin/test/unittest/BUILD.gn index a49fc594..8aee9ed3 100755 --- a/interfaces/plugin/test/unittest/BUILD.gn +++ b/interfaces/plugin/test/unittest/BUILD.gn @@ -1,4 +1,4 @@ -# Copyright (c) 2021 Huawei Device Co., Ltd. +# 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 @@ -11,47 +11,15 @@ # See the License for the specific language governing permissions and # limitations under the License. -#import("//build/config/ohos/rules.gni") -import("//build/ohos.gni") import("//build/test.gni") -module_output_path = "sensors/sensor/interfaces" -ohos_unittest("SensorJsTest") { - module_out_path = module_output_path - include_dirs = [ - "//third_party/node/src", - "//native_engine", - "//base/sensors/sensor/interfaces/native/include", - "//base/sensors/sensor/interfaces/plugin/include", - "//utils/native/base/include", - "./include", - "//third_party/libuv/include", - "//foundation/ace/napi", - "//foundation/ace/napi/interfaces/kits", - "//foundation/ace/napi/native_engine", - "//foundation/ace/napi/native_engine/impl/quickjs", - ] - defines = [ - "APP_LOG_TAG = \"sensroJs\"", - "LOG_DOMAIN = 0xD002700", - ] - sources = [ "sensor_js_test.cpp" ] - deps = [ - "//base/sensors/sensor/interfaces/native:sensor_interface_native", - "//base/sensors/sensor/interfaces/plugin:sensor", - "//foundation/ace/napi:ace_napi", - "//foundation/ace/napi/:ace_napi_quickjs", - "//third_party/googletest:gtest", - "//third_party/googletest:gtest_main", - "//third_party/libuv:uv_static", - "//third_party/quickjs:qjs", - "//utils/native/base:utils", - ] - external_deps = [ "hiviewdfx_hilog_native:libhilog" ] - relative_install_dir = "module" - part_name = "sensor" - subsystem_name = "sensors" +module_output_path = "sensor/interfaces" +ohos_js_unittest("SensorJsTest") { + module_out_path = module_output_path + hap_profile = "./config.json" + certificate_profile = "//test/developertest/signature/openharmony_sx.p7b" } + group("unittest") { testonly = true deps = [ ":SensorJsTest" ] diff --git a/interfaces/plugin/test/unittest/ExampleJsunit.test.js b/interfaces/plugin/test/unittest/ExampleJsunit.test.js new file mode 100755 index 00000000..46190c0a --- /dev/null +++ b/interfaces/plugin/test/unittest/ExampleJsunit.test.js @@ -0,0 +1,598 @@ +/* + * 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 () { + beforeAll(function() { + /* + * @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---------------------------'); + function offPromise() { + return new Promise((resolve, reject) => { + sensor.off(1, (error) =>{ + if(error) { + console.info('SensorJsTest001 off error'); + expect(false).assertTrue(); + console.info('setTimeout ..start') + setTimeout(()=>{ + reject(); + }, 500); + } else { + console.info('SensorJsTest001 off success'); + expect(true).assertTrue(); + setTimeout(()=>{ + resolve(); + }, 500); + } + }, 1000) + }) + } + + let promise = new Promise((resolve, reject) => { + sensor.on(1, function(error, data) { + if (error) { + console.info('SensorJsTest001 on error'); + expect(false).assertTrue(); + setTimeout(()=>{ + reject(); + }, 500); + } else { + console.info('SensorJsTest001 on success'); + expect(true).assertTrue(); + setTimeout(()=>{ + resolve(); + }, 500); + } + }); + }) + + await promise.then(() =>{ + return offPromise(); + }, ()=>{ + console.info("SensorJsTest001 reject"); + }) + done(); + }) + + /* + * @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---------------------------'); + function onSensorCallback(error, data) { + if (error) { + console.info('SensorJsTest002 on success'); + expect(true).assertTrue(); + } else { + console.info('SensorJsTest002 on error'); + expect(false).assertTrue(); + } + setTimeout(()=>{ + done(); + }, 500); + } + sensor.on(-1, onSensorCallback); + }) + + /* + * @tc.name:SensorJsTest003 + * @tc.desc:verify app info is not null + * @tc.type: FUNC + * @tc.require: Issue Number + */ + it("SensorJsTest003", 0, async function (done) { + console.info('----------------------SensorJsTest003---------------------------'); + function offPromise() { + return new Promise((resolve, reject) => { + sensor.off(1, (error) =>{ + if(error) { + console.info('SensorJsTest003 off error'); + expect(false).assertTrue(); + setTimeout(()=>{ + done(); + reject(); + }, 500); + } else { + console.info('SensorJsTest003 off success'); + expect(true).assertTrue(); + setTimeout(()=>{ + done(); + resolve(); + }, 500); + } + }, 1000) + }) + } + + let promise = new Promise((resolve, reject) => { + sensor.on(1, function(error, data) { + if (error) { + console.info('SensorJsTest003 on error'); + expect(false).assertTrue(); + setTimeout(()=>{ + reject(); + }, 500); + } else { + console.info('SensorJsTest003 on success'); + expect(true).assertTrue(); + setTimeout(()=>{ + resolve(); + }, 500); + } + }, {'interval': 200000000}); + }) + + await promise.then(() =>{ + return offPromise(); + }, ()=>{ + console.info("SensorJsTest003 reject"); + }) + done(); + }) + + /* + * @tc.name:SensorJsTest004 + * @tc.desc:verify app info is not null + * @tc.type: FUNC + * @tc.require: Issue Number + */ + it("SensorJsTest004", 0, function () { + console.info('----------------------SensorJsTest004---------------------------'); + sensor.on(0, function(){}, {'interval': 100000000}, 5); + expect(true).assertTrue(); + console.info('----------------------SensorJsTest004--------------------------- end'); + }) + + /* + * @tc.name:SensorJsTest005 + * @tc.desc:verify app info is not null + * @tc.type: FUNC + * @tc.require: Issue Number + */ + it("SensorJsTest005", 0, async function (done) { + function onceSensorCallback(error, data) { + if (error) { + console.info('SensorJsTest005 once error'); + expect(false).assertTrue(); + } else { + console.info('SensorJsTest005 once success'); + expect(true).assertTrue(); + } + setTimeout(()=>{ + done(); + }, 500); + } + sensor.once(0, onceSensorCallback); + }) + + /* + * @tc.name:SensorJsTest006 + * @tc.desc:verify app info is not null + * @tc.type: FUNC + * @tc.require: Issue Number + */ + it("SensorJsTest006", 0, async function (done) { + function onceSensorCallback(error, data) { + if (error) { + console.info('SensorJsTest006 on success'); + expect(true).assertTrue(); + } else { + console.info('SensorJsTest006 on error' + data.x); + expect(false).assertTrue(); + } + setTimeout(()=>{ + done(); + }, 500); + } + sensor.once(-1, onceSensorCallback); + }) + + /* + * @tc.name:SensorJsTest007 + * @tc.desc:verify app info is not null + * @tc.type: FUNC + * @tc.require: Issue Number + */ + it("SensorJsTest007", 0, function () { + sensor.once(0, function(){}, 5); + expect(true).assertTrue(); + }) + + /* + * @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 offCallback(error) { + if (error) { + console.info('SensorJsTest008 off success'); + expect(true).assertTrue(); + } else { + console.info('SensorJsTest008 off error'); + expect(false).assertTrue(); + } + setTimeout(()=>{ + done(); + }, 500); + } + sensor.off(-1, offCallback); + }) + + /* + * @tc.name:SensorJsTest009 + * @tc.desc:verify app info is not null + * @tc.type: FUNC + * @tc.require: Issue Number + */ + it("SensorJsTest009", 0, async function (done) { + function offCallback(error) { + if (error) { + console.info('SensorJsTest009 off success'); + expect(true).assertTrue(); + } else { + console.info('SensorJsTest009 off error'); + expect(false).assertTrue(); + } + setTimeout(()=>{ + done(); + }, 500); + } + sensor.off(0, offCallback); + }) + + /* + * @tc.name:SensorJsTest010 + * @tc.desc:verify app info is not null + * @tc.type: FUNC + * @tc.require: Issue Number + */ + it("SensorJsTest010", 0, async function (done) { + function offCallback(error) { + if (error) { + console.info('SensorJsTest010 off success'); + expect(true).assertTrue(); + } else { + console.info('SensorJsTest010 off error'); + expect(false).assertTrue(); + } + setTimeout(()=>{ + done(); + }, 500); + } + sensor.off(1000000, offCallback); + }) + + /* + * @tc.name:SensorJsTest011 + * @tc.desc:verify app info is not null + * @tc.type: FUNC + * @tc.require: Issue Number + */ + it("SensorJsTest011", 0, async function (done) { + function onceSensorCallback(error, data) { + if (error) { + console.info('SensorJsTest011 once success'); + expect(true).assertTrue(); + } else { + console.info('SensorJsTest011 once error' + data.x); + expect(false).assertTrue(); + } + setTimeout(()=>{ + done(); + }, 500); + } + sensor.once(1000000, onceSensorCallback); + }) + + /* + * @tc.name:SensorJsTest012 + * @tc.desc:verify app info is not null + * @tc.type: FUNC + * @tc.require: Issue Number + */ + it("SensorJsTest012", 0, async function (done) { + function offCallback(error) { + if (error) { + console.info('SensorJsTest012 off success'); + expect(true).assertTrue(); + } else { + console.info('SensorJsTest012 off error'); + expect(false).assertTrue(); + } + setTimeout(()=>{ + done(); + }, 500); + } + sensor.on(1000000, offCallback); + }) + + /* + * @tc.name:SensorJsTest013 + * @tc.desc:verify app info is not null + * @tc.type: FUNC + * @tc.require: Issue Number + */ + it("SensorJsTest013", 0, function () { + sensor.off(0, 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---------------------------'); + function offPromise2() { + return new Promise((resolve, reject) => { + sensor.off(0, (error) =>{ + if(error) { + console.info('SensorJsTest014 off2 success'); + expect(true).assertTrue(); + setTimeout(()=>{ + done(); + reject() + }, 500); + } else { + console.info('SensorJsTest014 off2 error'); + expect(false).assertTrue(); + setTimeout(()=>{ + done(); + resolve() + }, 500); + } + }); + }) + } + + function offPromise1() { + return new Promise((resolve, reject) => { + sensor.off(0, (error) =>{ + if(error) { + console.info('SensorJsTest014 off1 error'); + expect(false).assertTrue(); + setTimeout(()=>{ + reject(); + }, 500); + } else { + console.info('SensorJsTest014 off1 success'); + expect(true).assertTrue(); + setTimeout(()=>{ + resolve(); + }, 500); + } + }); + }) + } + + let promise = new Promise((resolve, reject) => { + sensor.on(0, function(error, data) { + if (error) { + console.info('SensorJsTest014 on error'); + expect(false).assertTrue(); + setTimeout(()=>{ + reject(); + }, 500); + } else { + console.info('SensorJsTest014 on success'); + expect(true).assertTrue(); + setTimeout(()=>{ + resolve(); + }, 500); + } + }); + }) + + await promise.then(() =>{ + return offPromise1(); + }).then(()=>{ + return offPromise2(); + }); + done(); + }) + + /* + * @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---------------------------'); + function offPromise() { + return new Promise((resolve, reject) => { + sensor.off(0, (error) =>{ + if(error) { + console.info('SensorJsTest015 off error'); + expect(false).assertTrue(); + setTimeout(()=>{ + done(); + reject(); + }, 500); + } else { + console.info('SensorJsTest015 off success'); + expect(true).assertTrue(); + setTimeout(()=>{ + done(); + resolve(); + }, 500); + } + }); + }) + } + function onPromise2() { + return new Promise((resolve, reject) => { + sensor.on(0, function(error, data) { + if (error) { + console.info('SensorJsTest015 on2 error'); + expect(false).assertTrue(); + setTimeout(()=>{ + reject(); + }, 500); + } else { + console.info('SensorJsTest015 on2 success'); + expect(true).assertTrue(); + setTimeout(()=>{ + resolve(); + }, 500); + } + }); + }) + } + + let onPromise1 = new Promise((resolve, reject) => { + sensor.on(0, function(error, data) { + if (error) { + console.info('SensorJsTest015 on1 error'); + expect(false).assertTrue(); + setTimeout(()=>{ + reject(); + }, 500); + } else { + console.info('SensorJsTest015 on1 success'); + expect(true).assertTrue(); + setTimeout(()=>{ + resolve(); + }, 500); + } + }); + }) + + await onPromise1.then(() =>{ + return onPromise2(); + }).then(()=>{ + return offPromise(); + }); + done(); + }) + + + /* + * @tc.name:SensorJsTest016 + * @tc.desc:verify app info is not null + * @tc.type: FUNC + * @tc.require: Issue Number + */ + it("SensorJsTest016", 0, async function (done) { + console.info('----------------------SensorJsTest016---------------------------'); + function offPromise() { + return new Promise((resolve, reject) => { + sensor.off(0, (error) =>{ + if(error) { + console.info('SensorJsTest016 off error'); + expect(false).assertTrue(); + setTimeout(()=>{ + done(); + reject(); + }, 500); + } else { + console.info('SensorJsTest016 off success'); + expect(true).assertTrue(); + setTimeout(()=>{ + done(); + resolve(); + }, 500); + } + }); + }) + } + function oncePromise() { + return new Promise((resolve, reject) => { + sensor.once(0, function(error, data) { + if (error) { + console.info('SensorJsTest016 once error'); + expect(false).assertTrue(); + setTimeout(()=>{ + reject(); + }, 500); + } else { + console.info('SensorJsTest016 once success'); + expect(true).assertTrue(); + setTimeout(()=>{ + resolve(); + }, 500); + } + }); + }) + } + + let onPromise1 = new Promise((resolve, reject) => { + sensor.on(0, function(error, data) { + if (error) { + console.info('SensorJsTest016 on1 error'); + expect(false).assertTrue(); + setTimeout(()=>{ + reject(); + }, 500); + } else { + console.info('SensorJsTest016 on1 success'); + expect(true).assertTrue(); + setTimeout(()=>{ + resolve(); + }, 500); + } + }); + }) + + await onPromise1.then(() =>{ + return oncePromise(); + }).then(()=>{ + return offPromise(); + }); + done(); + }) +}) diff --git a/interfaces/plugin/test/unittest/config.json b/interfaces/plugin/test/unittest/config.json new file mode 100755 index 00000000..2ca85895 --- /dev/null +++ b/interfaces/plugin/test/unittest/config.json @@ -0,0 +1,61 @@ +{ + "app": { + "bundleName": "com.example.myapplication", + "vendor": "example", + "version": { + "code": 1, + "name": "1.0" + }, + "apiVersion": { + "compatible": 4, + "target": 5 + } + }, + "deviceConfig": {}, + "module": { + "package": "com.example.myapplication", + "name": ".MyApplication", + "deviceType": [ + "phone" + ], + "distro": { + "deliveryWithInstall": true, + "moduleName": "entry", + "moduleType": "entry" + }, + "abilities": [ + { + "visible": true, + "skills": [ + { + "entities": [ + "entity.system.home" + ], + "actions": [ + "action.system.home" + ] + } + ], + "name": "com.example.myapplication.MainAbility", + "icon": "$media:icon", + "description": "$string:mainability_description", + "label": "MyApplication", + "type": "page", + "launchType": "standard" + } + ], + "js": [ + { + "pages": [ + "pages/index/index" + ], + "name": "default", + "window": { + "designWidth": 720, + "autoDesignWidth": false + } + } + ] + } + } + \ No newline at end of file diff --git a/interfaces/plugin/test/unittest/sensor_js_test.cpp b/interfaces/plugin/test/unittest/sensor_js_test.cpp deleted file mode 100755 index 6d00afd0..00000000 --- a/interfaces/plugin/test/unittest/sensor_js_test.cpp +++ /dev/null @@ -1,595 +0,0 @@ -/* - * 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. - */ - -#include -#include -#include "napi/native_api.h" -#include "napi/native_node_api.h" -#include "sensor_napi_utils.h" -#include "sensor_agent.h" -#include "native_engine.h" -#include "quickjs_native_engine.h" -#include "hilog/log.h" - -namespace OHOS { -namespace Sensors { -using namespace testing::ext; -using namespace OHOS::HiviewDFX; -static NativeEngine* g_nativeEngine = nullptr; -namespace { - using namespace OHOS::HiviewDFX; - static constexpr HiLogLabel LABEL = {LOG_CORE, 0xD002708, "SensorJsTest"}; -} - -class SensorJsTest : public testing::Test { -public: - void SetUp(); - void TearDown(); - SensorJsTest(); - virtual ~SensorJsTest(); -protected: - NativeEngine* engine_; -}; - -void SensorJsTest::SetUp() -{ - HiLog::Info(LABEL, "%{public}s begin", __func__); -} - -void SensorJsTest::TearDown() -{ - HiLog::Info(LABEL, "%{public}s end", __func__); -} - -SensorJsTest::SensorJsTest() -{ - JSRuntime* rt = JS_NewRuntime(); - if (rt == nullptr) { - return ; - } - - JSContext* ctx = JS_NewContext(rt); - if (ctx == nullptr) { - return ; - } - - js_std_add_helpers(ctx, 0, nullptr); - g_nativeEngine = new QuickJSNativeEngine(rt, ctx, 0); // default instance id 0 - engine_ = g_nativeEngine; - -} - -SensorJsTest::~SensorJsTest() { - delete g_nativeEngine; - g_nativeEngine = nullptr; -} - -/* - * @tc.name: IsMatchType_001 - * @tc.desc: Match napi_number - * @tc.type: FUNC - * @tc.require:SR000G54IU - */ -HWTEST_F(SensorJsTest, IsMatchType_001, TestSize.Level1) -{ - napi_env env = (napi_env)engine_; - uint32_t cppValue = UINT32_MAX; - napi_value napiValue = nullptr; - napi_create_uint32(env, cppValue, &napiValue); - - bool ret = IsMatchType(napiValue, napi_number, env); - ASSERT_EQ(ret, true); -} - -/* - * @tc.name: IsMatchType_002 - * @tc.desc: Match napi_string - * @tc.type: FUNC - * @tc.require:AR000G54IV - */ -HWTEST_F(SensorJsTest, IsMatchType_002, TestSize.Level1) -{ - // Construction parameters - napi_env env = (napi_env)engine_; - const char cppValue[] = "中文,English,123456,!@#"; - size_t cppValueStrLength = strlen(cppValue); - napi_value napiValue = nullptr; - napi_create_string_utf8(env, cppValue, cppValueStrLength, &napiValue); - - // call function - bool ret = IsMatchType(napiValue, napi_string, env); - ASSERT_EQ(ret, true); -} - -/* - * @tc.name: IsMatchType_003 - * @tc.desc: Match napi_symbol - * @tc.type: FUNC - * @tc.require: AR000G54IV - */ -HWTEST_F(SensorJsTest, IsMatchType_003, TestSize.Level1) -{ - // Construction parameters - napi_env env = (napi_env)engine_; - const char testStr[] = "testSymbol"; - napi_value result = nullptr; - napi_create_string_latin1(env, testStr, strlen(testStr), &result); - napi_value symbolVal = nullptr; - napi_create_symbol(env, result, &symbolVal); - - // call function - bool ret = IsMatchType(symbolVal, napi_symbol, env); - ASSERT_EQ(ret, true); -} - -/* - * @tc.name: IsMatchType_004 - * @tc.desc: Match napi_function - * @tc.type: FUNC - * @tc.require: AR000G54IV - */ -HWTEST_F(SensorJsTest, IsMatchType_004, TestSize.Level1) -{ - // Construction parameters - napi_env env = (napi_env)engine_; - const char* tetScriptStr = "new Date();"; - napi_value testScript = nullptr; - napi_create_string_utf8(env, tetScriptStr, strlen(tetScriptStr), &testScript); - napi_value date = nullptr; - napi_run_script(env, testScript, &date); - napi_value getTimeFunc = nullptr; - napi_get_named_property(env, date, "getTime", &getTimeFunc); - - // call function - bool ret = IsMatchType(getTimeFunc, napi_function, env); - ASSERT_EQ(ret, true); -} - -/* - * @tc.name: IsMatchType_005 - * @tc.desc: Match napi_boolean - * @tc.type: FUNC - * @tc.require: AR000G54IV - */ -HWTEST_F(SensorJsTest, IsMatchType_005, TestSize.Level1) -{ - // Construction parameters - napi_env env = (napi_env)engine_; - napi_value result = nullptr; - napi_get_boolean(env, true, &result); - - // call function - bool ret = IsMatchType(result, napi_boolean, env); - ASSERT_EQ(ret, true); -} - -/* - * @tc.name: GetNapiInt32_001 - * @tc.desc: change int32_t[INT32_MAX] to napi_value - * @tc.type: FUNC - * @tc.require: SR000FU58Q - */ -HWTEST_F(SensorJsTest, GetNapiInt32_001, TestSize.Level1) -{ - // Construction parameters - napi_env env = (napi_env)engine_; - int32_t number = INT32_MAX; - - // call function - napi_value value = GetNapiInt32(number, env); - napi_valuetype ret; - napi_typeof(env, value, &ret); - ASSERT_EQ(ret, napi_number); -} - -/* - * @tc.name: GetNapiInt32_002 - * @tc.desc: change int32_t[INT32_MIN] to napi_value - * @tc.type: FUNC - * @tc.require: AR000FU73V - */ -HWTEST_F(SensorJsTest, GetNapiInt32_002, TestSize.Level1) -{ - // Construction parameters - napi_env env = (napi_env)engine_; - int32_t number = INT32_MIN; - - // call function - napi_value value = GetNapiInt32(number, env); - napi_valuetype ret; - napi_typeof(env, value, &ret); - ASSERT_EQ(ret, napi_number); -} - -/* - * @tc.name: GetNapiInt32_003 - * @tc.desc: change int32_t[true] to napi_value - * @tc.type: FUNC - * @tc.require: AR000FU73V - */ -HWTEST_F(SensorJsTest, GetNapiInt32_003, TestSize.Level1) -{ - // Construction parameters - napi_env env = (napi_env)engine_; - int32_t number = (int32_t)true; - - // call function - napi_value value = GetNapiInt32(number, env); - napi_valuetype ret; - napi_typeof(env, value, &ret); - ASSERT_EQ(ret, napi_number); -} - -/* - * @tc.name: GetNapiInt32_004 - * @tc.desc: change int32_t[true] to napi_value - * @tc.type: FUNC - * @tc.require: AR000FU73V - */ -HWTEST_F(SensorJsTest, GetNapiInt32_004, TestSize.Level1) -{ - // Construction parameters - napi_env env = (napi_env)engine_; - int32_t number = (int32_t)false; - - // call function - napi_value value = GetNapiInt32(number, env); - napi_valuetype ret; - napi_typeof(env, value, &ret); - ASSERT_EQ(ret, napi_number); -} - -/* - * @tc.name: GetNapiInt32_005 - * @tc.desc: change int32_t[char] to napi_value - * @tc.type: FUNC - * @tc.require: AR000FU73V - */ -HWTEST_F(SensorJsTest, GetNapiInt32_005, TestSize.Level1) -{ - // Construction parameters - napi_env env = (napi_env)engine_; - int32_t number = (int32_t)'a'; - - // call function - napi_value value = GetNapiInt32(number, env); - napi_valuetype ret; - napi_typeof(env, value, &ret); - ASSERT_EQ(ret, napi_number); -} - -/* - * @tc.name: NapiGetNamedProperty_001 - * @tc.desc: Parse function in Json - * @tc.type: FUNC - * @tc.require: AR000FU73V - */ -HWTEST_F(SensorJsTest, NapiGetNamedProperty_001, TestSize.Level1) -{ - // Construction parameters - napi_env env = (napi_env)engine_; - const char* tetScriptStr = "new Date();"; - napi_value testScript = nullptr; - napi_create_string_utf8(env, tetScriptStr, strlen(tetScriptStr), &testScript); - napi_value date = nullptr; - napi_run_script(env, testScript, &date); - - // call function - napi_value value = NapiGetNamedProperty(date, "getTime", env); - napi_valuetype ret; - napi_typeof(env, value, &ret); - ASSERT_EQ(ret, napi_function); -} - -/* - * @tc.name: NapiGetNamedProperty_002 - * @tc.desc: Parse function in Json - * @tc.type: FUNC - * @tc.require: AR000FU73V - */ -HWTEST_F(SensorJsTest, NapiGetNamedProperty_002, TestSize.Level1) -{ - // 定义变量 赋值 - napi_env env = (napi_env)engine_; - int32_t status=101; - napi_value message; - napi_create_int32(env, status, &message); - - // 定义变量为对象 - napi_value result; - napi_create_object(env, &result); - - // 赋值给object - napi_set_named_property(env, result, "code", message); - - napi_value value = NapiGetNamedProperty(result, "code", env); - napi_valuetype ret; - napi_typeof(env, value, &ret); - ASSERT_EQ(ret, napi_number); -} - -/* - * @tc.name: NapiGetNamedProperty_003 - * @tc.desc: Parse function in Json - * @tc.type: FUNC - * @tc.require: AR000FU73V - */ -HWTEST_F(SensorJsTest, NapiGetNamedProperty_003, TestSize.Level1) -{ - // 定义变量 赋值 - napi_env env = (napi_env)engine_; - const char status[] = "type001"; - napi_value message; - napi_create_string_utf8(env, status, strlen(status), &message); - - // 定义变量为对象 - napi_value result; - napi_create_object(env, &result); - - // 赋值给object - napi_set_named_property(env, result, "code", message); - - napi_value value = NapiGetNamedProperty(result, "code", env); - napi_valuetype ret; - napi_typeof(env, value, &ret); - ASSERT_EQ(ret, napi_string); -} - -/* - * @tc.name: NapiGetNamedProperty_004 - * @tc.desc: Parse function in Json - * @tc.type: FUNC - * @tc.require: AR000G54IV - */ -HWTEST_F(SensorJsTest, NapiGetNamedProperty_004, TestSize.Level1) -{ - // 定义变量 赋值 - napi_env env = (napi_env)engine_; - napi_value message; - napi_get_boolean(env, true, &message); - - // 定义变量为对象 - napi_value result; - napi_create_object(env, &result); - - // 赋值给object - napi_set_named_property(env, result, "code", message); - - - napi_value value = NapiGetNamedProperty(result, "code", env); - napi_valuetype ret; - napi_typeof(env, value, &ret); - ASSERT_EQ(ret, napi_boolean); -} - -/* - * @tc.name: NapiGetNamedProperty_005 - * @tc.desc: Parse function in Json - * @tc.type: FUNC - * @tc.require: AR000G54IV - */ -HWTEST_F(SensorJsTest, NapiGetNamedProperty_005, TestSize.Level1) -{ - // 定义变量 赋值 - napi_env env = (napi_env)engine_; - const char testStr[] = "testSymbol"; - napi_value resultSymbol = nullptr; - napi_create_string_latin1(env, testStr, strlen(testStr), &resultSymbol); - napi_value message = nullptr; - napi_create_symbol(env, resultSymbol, &message); - - // 定义变量为对象 - napi_value result; - napi_create_object(env, &result); - - // 赋值给object - napi_set_named_property(env, result, "code", message); - - napi_value value = NapiGetNamedProperty(result, "code", env); - napi_valuetype ret; - napi_typeof(env, value, &ret); - ASSERT_EQ(ret, napi_symbol); -} -/* - * @tc.name: GetCppInt32_001 - * @tc.desc: Parse function in Json - * @tc.type: FUNC - * @tc.require: AR000G54IV - */ -HWTEST_F(SensorJsTest, GetCppInt32_001, TestSize.Level1) -{ - // Construction parameters - napi_env env = (napi_env)engine_; - int32_t number = INT32_MAX; - napi_value value; - napi_create_int32(env, number, &value); - - // call function - int32_t ret = GetCppInt32(value, env); - ASSERT_EQ(ret, (int32_t)INT32_MAX); -} - -/* - * @tc.name: GetCppInt32_002 - * @tc.desc: Parse function in Json - * @tc.type: FUNC - * @tc.require: AR000G54IV - */ -HWTEST_F(SensorJsTest, GetCppInt32_002, TestSize.Level1) -{ - // Construction parameters - napi_env env = (napi_env)engine_; - int32_t number = INT32_MIN; - napi_value value; - napi_create_int32(env, number, &value); - - // call function - int32_t ret = GetCppInt32(value, env); - ASSERT_EQ(ret, (int32_t)INT32_MIN); -} - -/* - * @tc.name: GetCppInt32_003 - * @tc.desc: Parse function in Json - * @tc.type: FUNC - * @tc.require: AR000G54IV - */ -HWTEST_F(SensorJsTest, GetCppInt32_003, TestSize.Level1) -{ - // Construction parameters - napi_env env = (napi_env)engine_; - int32_t number = (int32_t)true; - napi_value value; - napi_create_int32(env, number, &value); - - // call function - int32_t ret = GetCppInt32(value, env); - ASSERT_EQ(ret, (int32_t)true); -} - -/* - * @tc.name: GetCppInt32_004 - * @tc.desc: Parse function in Json - * @tc.type: FUNC - * @tc.require: AR000G54IV - */ -HWTEST_F(SensorJsTest, GetCppInt32_004, TestSize.Level1) -{ - // Construction parameters - napi_env env = (napi_env)engine_; - int32_t number = (int32_t)false; - napi_value value; - napi_create_int32(env, number, &value); - - // call function - int32_t ret = GetCppInt32(value, env); - ASSERT_EQ(ret, (int32_t)false); -} -/* - * @tc.name: GetCppInt32_005 - * @tc.desc: Parse function in Json - * @tc.type: FUNC - * @tc.require: AR000G54IV - */ -HWTEST_F(SensorJsTest, GetCppInt32_005, TestSize.Level1) -{ - // Construction parameters - napi_env env = (napi_env)engine_; - int32_t number = (int32_t)'b'; - napi_value value; - napi_create_int32(env, number, &value); - - // call function - int32_t ret = GetCppInt32(value, env); - ASSERT_EQ(ret, (int32_t)'b'); -} - -/* - * @tc.name: GetCppInt64_001 - * @tc.desc: Parse function in Json - * @tc.type: FUNC - * @tc.require: AR000G54IV - */ -HWTEST_F(SensorJsTest, GetCppInt64_001, TestSize.Level1) -{ - // Construction parameters - int64_t tmpInt64Max = 9007199254740991;//9223372036854775807;9007199254740992 - napi_env env = (napi_env)engine_; - int64_t number = tmpInt64Max;//(int64_t)INT64_MAX; - napi_value value; - napi_create_int64(env, number, &value); - - // call function - int64_t ret = GetCppInt64(value, env); - ASSERT_EQ(ret, tmpInt64Max); -} - -/* - * @tc.name: GetCppInt64_002 - * @tc.desc: Parse function in Json - * @tc.type: FUNC - * @tc.require: AR000G54IV - */ -HWTEST_F(SensorJsTest, GetCppInt64_002, TestSize.Level1) -{ - // Construction parameters - napi_env env = (napi_env)engine_; - int64_t number = (int64_t)INT64_MIN; - napi_value value; - napi_create_int64(env, number, &value); - - // call function - int64_t ret = GetCppInt64(value, env); - ASSERT_EQ(ret, (int64_t)INT64_MIN); -} - -/* - * @tc.name: GetCppInt64_003 - * @tc.desc: Parse function in Json - * @tc.type: FUNC - * @tc.require: AR000G54IV - */ -HWTEST_F(SensorJsTest, GetCppInt64_003, TestSize.Level1) -{ - // Construction parameters - napi_env env = (napi_env)engine_; - int64_t number = (int64_t)true; - napi_value value; - napi_create_int64(env, number, &value); - - - // call function - int64_t ret = GetCppInt64(value, env); - ASSERT_EQ(ret, (int64_t)true); -} - -/* - * @tc.name: GetCppInt64_004 - * @tc.desc: Parse function in Json - * @tc.type: FUNC - * @tc.require: AR000G54IV - */ -HWTEST_F(SensorJsTest, GetCppInt64_004, TestSize.Level1) -{ - // Construction parameters - napi_env env = (napi_env)engine_; - int64_t number = (int64_t)false; - napi_value value; - napi_create_int64(env, number, &value); - - // call function - int64_t ret = GetCppInt64(value, env); - ASSERT_EQ(ret, (int64_t)false); -} -/* - * @tc.name: GetCppInt64_005 - * @tc.desc: Parse function in Json - * @tc.type: FUNC - * @tc.require: AR000G54IV - */ -HWTEST_F(SensorJsTest, GetCppInt64_005, TestSize.Level1) -{ - // Construction parameters - napi_env env = (napi_env)engine_; - int64_t number = (int64_t)'c'; - napi_value value; - napi_create_int64(env, number, &value); - - // call function - int64_t ret = GetCppInt64(value, env); - ASSERT_EQ(ret, (int64_t)'c'); -} -} // namespace Sensors -} // namespace OHOS \ No newline at end of file diff --git a/utils/src/report_data_callback.cpp b/utils/src/report_data_callback.cpp index 4b27d868..c409d352 100755 --- a/utils/src/report_data_callback.cpp +++ b/utils/src/report_data_callback.cpp @@ -53,8 +53,6 @@ ReportDataCallback::~ReportDataCallback() int32_t ReportDataCallback::ZReportDataCallback(const struct SensorEvent* event, sptr cb) { - float *data = (float*)event->data; - HiLog::Info(LABEL, "%{public}s in, sensorData: %{public}f", __func__, *(data)); if (cb == nullptr || cb->eventsBuf_.circularBuf == nullptr || event == nullptr) { HiLog::Error(LABEL, "%{public}s callback or circularBuf or event cannot be null", __func__); return ERROR; @@ -68,6 +66,7 @@ int32_t ReportDataCallback::ZReportDataCallback(const struct SensorEvent* event, .dataLen = event->dataLen }; eventCopy.data = new uint8_t[SENSOR_DATA_LENGHT]; + HiLog::Info(LABEL, "%{public}s dataLength: %{public}d", __func__, event->dataLen); if (memcpy_s(eventCopy.data, event->dataLen, event->data, event->dataLen) != EOK) { HiLog::Error(LABEL, "%{public}s copy data failed", __func__); return COPY_ERR; -- Gitee From ee59c10606b9bc676b7bc11c68d4c132a9d7f19b Mon Sep 17 00:00:00 2001 From: h00514358 Date: Tue, 26 Oct 2021 11:19:01 +0000 Subject: [PATCH 2/4] Signed-off-by:hellohyh001 Signed-off-by: h00514358 --- frameworks/native/sensor/src/sensor_agent_proxy.cpp | 3 --- 1 file changed, 3 deletions(-) diff --git a/frameworks/native/sensor/src/sensor_agent_proxy.cpp b/frameworks/native/sensor/src/sensor_agent_proxy.cpp index a61ea3b9..cde2e24c 100755 --- a/frameworks/native/sensor/src/sensor_agent_proxy.cpp +++ b/frameworks/native/sensor/src/sensor_agent_proxy.cpp @@ -127,9 +127,6 @@ void SensorAgentProxy::HandleSensorData(struct SensorEvent *events, int32_t num, for (int32_t i = 0; i < num; ++i) { eventStream = events[i]; float *data = (float*)eventStream.data; - if (eventStream.sensorTypeId == 1 || eventStream.sensorTypeId == 2 || eventStream.sensorTypeId == 6) { - HiLog::Error(LABEL, "%{public}s data x: %{public}f, y: %{public}f, z: %{public}f", __func__, (*data), *(data + 1), *(data + 2)); - } if (eventStream.data == nullptr || g_subscribeMap[eventStream.sensorTypeId] == nullptr) { HiLog::Error(LABEL, "%{public}s data or sensorUser is nullptr", __func__); return; -- Gitee From d072b27c6114edbc23618ece6d83dd1e6e1cc431 Mon Sep 17 00:00:00 2001 From: h00514358 Date: Tue, 26 Oct 2021 11:22:35 +0000 Subject: [PATCH 3/4] Signed-off-by:hellohyh001 Signed-off-by: h00514358 --- interfaces/plugin/test/unittest/ExampleJsunit.test.js | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/interfaces/plugin/test/unittest/ExampleJsunit.test.js b/interfaces/plugin/test/unittest/ExampleJsunit.test.js index 46190c0a..f323d4e4 100755 --- a/interfaces/plugin/test/unittest/ExampleJsunit.test.js +++ b/interfaces/plugin/test/unittest/ExampleJsunit.test.js @@ -55,7 +55,7 @@ describe("SensorJsTest", function () { console.info('----------------------SensorJsTest001---------------------------'); function offPromise() { return new Promise((resolve, reject) => { - sensor.off(1, (error) =>{ + sensor.off(0, (error) =>{ if(error) { console.info('SensorJsTest001 off error'); expect(false).assertTrue(); @@ -75,7 +75,7 @@ describe("SensorJsTest", function () { } let promise = new Promise((resolve, reject) => { - sensor.on(1, function(error, data) { + sensor.on(0, function(error, data) { if (error) { console.info('SensorJsTest001 on error'); expect(false).assertTrue(); @@ -133,7 +133,7 @@ describe("SensorJsTest", function () { console.info('----------------------SensorJsTest003---------------------------'); function offPromise() { return new Promise((resolve, reject) => { - sensor.off(1, (error) =>{ + sensor.off(0, (error) =>{ if(error) { console.info('SensorJsTest003 off error'); expect(false).assertTrue(); @@ -154,7 +154,7 @@ describe("SensorJsTest", function () { } let promise = new Promise((resolve, reject) => { - sensor.on(1, function(error, data) { + sensor.on(0, function(error, data) { if (error) { console.info('SensorJsTest003 on error'); expect(false).assertTrue(); -- Gitee From 023097651ad4f6c0e17f18c88eabae1450dedbc1 Mon Sep 17 00:00:00 2001 From: h00514358 Date: Tue, 26 Oct 2021 11:36:59 +0000 Subject: [PATCH 4/4] Signed-off-by:hellohyh001 Signed-off-by: h00514358 --- frameworks/native/sensor/src/sensor_agent_proxy.cpp | 1 - 1 file changed, 1 deletion(-) diff --git a/frameworks/native/sensor/src/sensor_agent_proxy.cpp b/frameworks/native/sensor/src/sensor_agent_proxy.cpp index cde2e24c..68a273a7 100755 --- a/frameworks/native/sensor/src/sensor_agent_proxy.cpp +++ b/frameworks/native/sensor/src/sensor_agent_proxy.cpp @@ -126,7 +126,6 @@ void SensorAgentProxy::HandleSensorData(struct SensorEvent *events, int32_t num, struct SensorEvent eventStream; for (int32_t i = 0; i < num; ++i) { eventStream = events[i]; - float *data = (float*)eventStream.data; if (eventStream.data == nullptr || g_subscribeMap[eventStream.sensorTypeId] == nullptr) { HiLog::Error(LABEL, "%{public}s data or sensorUser is nullptr", __func__); return; -- Gitee