diff --git a/frameworks/js/napi/src/sensor_napi_utils.cpp b/frameworks/js/napi/src/sensor_napi_utils.cpp index 77efe702dc3b3e8ac972f6fcd6868856d739acf3..dbc0cc982c70aa023be48728870c5af6be4ba491 100644 --- a/frameworks/js/napi/src/sensor_napi_utils.cpp +++ b/frameworks/js/napi/src/sensor_napi_utils.cpp @@ -160,7 +160,7 @@ std::map> g_sensorAttributeList = { { 0, { "x" } }, { SENSOR_TYPE_ID_ACCELEROMETER, { "x", "y", "z" } }, { SENSOR_TYPE_ID_GYROSCOPE, { "x", "y", "z" } }, - { SENSOR_TYPE_ID_AMBIENT_LIGHT, { "intensity" } }, + { SENSOR_TYPE_ID_AMBIENT_LIGHT, { "intensity", "colorTemperature", "infraredLuminance" } }, { SENSOR_TYPE_ID_MAGNETIC_FIELD, { "x", "y", "z" } }, { SENSOR_TYPE_ID_BAROMETER, { "pressure" } }, { SENSOR_TYPE_ID_HALL, { "status" } }, diff --git a/interfaces/inner_api/sensor_agent_type.h b/interfaces/inner_api/sensor_agent_type.h index 4312d5b9ac0bb94d7a500154b61e763f0a4f4ffd..8e3f73705564cf5cee898c167c9fd61ba33dab5b 100644 --- a/interfaces/inner_api/sensor_agent_type.h +++ b/interfaces/inner_api/sensor_agent_type.h @@ -100,6 +100,7 @@ typedef enum SensorTypeId { SENSOR_TYPE_ID_DEVICE_ORIENTATION = 279, /**< Device orientation sensor */ SENSOR_TYPE_ID_WEAR_DETECTION = 280, /**< Wear detection sensor */ SENSOR_TYPE_ID_ACCELEROMETER_UNCALIBRATED = 281, /**< Uncalibrated acceleration sensor */ + SENSOR_TYPE_ID_RPC = 282, /**< Radio power control sensor */ SENSOR_TYPE_ID_MAX = 30, /**< Maximum number of sensor type IDs*/ } SensorTypeId; @@ -417,6 +418,8 @@ typedef struct ProximityData { */ typedef struct AmbientLightData { float intensity = 0.0; + float colorTemperature = 0.0; + float infraredLuminance = 0.0; } AmbientLightData; /** @@ -462,6 +465,16 @@ typedef struct SarData { float absorptionRatio = 0.0; } SarData; +/** + * @brief Defines the struct of the data reported by the RPC sensor. + * This sensor measures the radio power control. + */ +typedef struct RPCData { + float absorptionRatio = 0.0; + float threshold = 0.0; + float offset = 0.0; +} RPCData; + /** * @brief Defines the struct of the data reported by the posture sensor. * This sensor measures the angle between two screens, in degrees. The angle ranges from 0 to 180. diff --git a/services/src/sensor_dump.cpp b/services/src/sensor_dump.cpp index 01feacb2f67d36ec778f5f1ed9bc29605921028c..78e50f5298450822ecb7da87c08742ed0b2c6965 100644 --- a/services/src/sensor_dump.cpp +++ b/services/src/sensor_dump.cpp @@ -86,6 +86,7 @@ std::unordered_map SensorDump::sensorMap_ = { { SENSOR_TYPE_ID_POSTURE, "POSTURE" }, { SENSOR_TYPE_ID_HEADPOSTURE, "HEAD POSTURE" }, { SENSOR_TYPE_ID_DROP_DETECTION, "DROP DETECTION" }, + { SENSOR_TYPE_ID_RPC, "RPC" }, }; void SensorDump::RunSensorDump(int32_t fd, int32_t optionIndex, const std::vector &args, char **argv) @@ -292,8 +293,6 @@ void SensorDump::DumpCurrentTime(int32_t fd) int32_t SensorDump::GetDataDimension(int32_t sensorId) { switch (sensorId) { - case SENSOR_TYPE_ID_AMBIENT_LIGHT: - case SENSOR_TYPE_ID_AMBIENT_LIGHT1: case SENSOR_TYPE_ID_BAROMETER: case SENSOR_TYPE_ID_HALL: case SENSOR_TYPE_ID_HALL_EXT: diff --git a/test/unittest/interfaces/inner_api/BUILD.gn b/test/unittest/interfaces/inner_api/BUILD.gn index 4ddbdd632a98baf36a9b445685e210669afb0482..2333683a37595ce373da6b13736c24e88c4f937b 100644 --- a/test/unittest/interfaces/inner_api/BUILD.gn +++ b/test/unittest/interfaces/inner_api/BUILD.gn @@ -210,6 +210,34 @@ ohos_unittest("DropDetectionTest") { "ipc:ipc_single", ] } + +ohos_unittest("RPCSensorTest") { + module_out_path = "sensor/interfaces/inner_api" + + sources = [ + "$SUBSYSTEM_DIR/test/unittest/interfaces/inner_api/rpc_sensor_test.cpp", + ] + + include_dirs = [ + "$SUBSYSTEM_DIR/utils/common/include", + "$SUBSYSTEM_DIR/interfaces/inner_api", + "$SUBSYSTEM_DIR/frameworks/native/include", + ] + + deps = [ + "$SUBSYSTEM_DIR/frameworks/native:sensor_target", + "$SUBSYSTEM_DIR/utils/common:libsensor_utils", + "//third_party/googletest:gmock_main", + "//third_party/googletest:gtest_main", + ] + + external_deps = [ + "c_utils:utils", + "hilog:libhilog", + "ipc:ipc_single", + ] +} + group("unittest") { testonly = true deps = [ @@ -217,6 +245,7 @@ group("unittest") { ":DropDetectionTest", ":HeadPostureTest", ":PostureTest", + ":RPCSensorTest", ":SensorAgentTest", ":SensorAlgorithmTest", ":SensorPowerTest", diff --git a/test/unittest/interfaces/inner_api/rpc_sensor_test.cpp b/test/unittest/interfaces/inner_api/rpc_sensor_test.cpp new file mode 100644 index 0000000000000000000000000000000000000000..8a196c593b1fe0ff97d20811cc4854d5c9e1547d --- /dev/null +++ b/test/unittest/interfaces/inner_api/rpc_sensor_test.cpp @@ -0,0 +1,308 @@ +/* + * Copyright (c) 2024 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 + +#include "sensor_agent.h" +#include "sensor_agent_type.h" +#include "sensor_errors.h" + +#undef LOG_TAG +#define LOG_TAG "RPCSensorTest" + +namespace OHOS { +namespace Sensors { +using namespace testing::ext; +using namespace OHOS::HiviewDFX; + +namespace { +std::atomic_bool g_existRPC = false; +} // namespace + +class RPCSensorTest : public testing::Test { +public: + static void SetUpTestCase(); + static void TearDownTestCase(); + void SetUp(); + void TearDown(); +}; + +void RPCSensorTest::SetUpTestCase() +{ + SensorInfo *sensorInfo = nullptr; + int32_t count = 0; + int32_t ret = GetAllSensors(&sensorInfo, &count); + ASSERT_EQ(ret, OHOS::Sensors::SUCCESS); + if (sensorInfo == nullptr || count == 0) { + SEN_HILOGE("sensorInfo is nullptr or count is 0"); + return; + } + for (int32_t i = 0; i < count; ++i) { + if (sensorInfo[i].sensorId == SENSOR_TYPE_ID_RPC) { + g_existRPC = true; + SEN_HILOGD("Exist rpc sensor"); + break; + } + } + SEN_HILOGD("Not exist rpc sensor"); +} + +void RPCSensorTest::TearDownTestCase() {} + +void RPCSensorTest::SetUp() {} + +void RPCSensorTest::TearDown() {} + +void RPCDataCallbackImpl(SensorEvent *event) +{ + if (event == nullptr) { + SEN_HILOGE("event is nullptr"); + return; + } + if (event[0].data == nullptr) { + SEN_HILOGE("event[0].data is nullptr"); + return; + } + if (event[0].dataLen < sizeof(RPCData)) { + SEN_HILOGE("Event dataLen less than rpc data size, event.dataLen:%{public}u", event[0].dataLen); + return; + } + RPCData *rpcData = reinterpret_cast(event[0].data); + if (rpcData == nullptr) { + SEN_HILOGE("rpcData is nullptr"); + return; + } + SEN_HILOGD("sensorId:%{public}d, version:%{public}d, dataLen:%{public}u," + "absorptionRatio:%{public}f, threshold:%{public}f, offset:%{public}f", event[0].sensorTypeId, event[0].version, + event[0].dataLen, rpcData->absorptionRatio, rpcData->threshold, rpcData->offset); +} + +void RPCDataCallbackImpl2(SensorEvent *event) +{ + if (event == nullptr) { + SEN_HILOGE("event is nullptr"); + return; + } + if (event[0].data == nullptr) { + SEN_HILOGE("event[0].data is nullptr"); + return; + } + if (event[0].dataLen < sizeof(RPCData)) { + SEN_HILOGE("Event dataLen less than rpc data size, event.dataLen:%{public}u", event[0].dataLen); + return; + } + RPCData *rpcData = reinterpret_cast(event[0].data); + if (rpcData == nullptr) { + SEN_HILOGE("rpcData is nullptr"); + return; + } + SEN_HILOGD("sensorId:%{public}d, version:%{public}d, dataLen:%{public}u," + "absorptionRatio:%{public}f, threshold:%{public}f, offset:%{public}f", event[0].sensorTypeId, event[0].version, + event[0].dataLen, rpcData->absorptionRatio, rpcData->threshold, rpcData->offset); +} + +HWTEST_F(RPCSensorTest, RPCSensorTest_001, TestSize.Level1) +{ + SEN_HILOGI("RPCSensorTest_001 enter"); + if (g_existRPC) { + ASSERT_NE(ActivateSensor(SENSOR_TYPE_ID_RPC, nullptr), OHOS::Sensors::SUCCESS); + } +} + +HWTEST_F(RPCSensorTest, RPCSensorTest_002, TestSize.Level1) +{ + SEN_HILOGI("RPCSensorTest_002 enter"); + if (g_existRPC) { + SensorUser user; + user.callback = RPCDataCallbackImpl; + ASSERT_NE(ActivateSensor(-1, &user), OHOS::Sensors::SUCCESS); + } +} + +HWTEST_F(RPCSensorTest, RPCSensorTest_003, TestSize.Level1) +{ + SEN_HILOGI("RPCSensorTest_003 enter"); + if (g_existRPC) { + SensorUser user; + user.callback = nullptr; + ASSERT_NE(ActivateSensor(SENSOR_TYPE_ID_RPC, &user), OHOS::Sensors::SUCCESS); + } +} + +HWTEST_F(RPCSensorTest, RPCSensorTest_004, TestSize.Level1) +{ + SEN_HILOGI("RPCSensorTest_004 enter"); + if (g_existRPC) { + ASSERT_NE(DeactivateSensor(SENSOR_TYPE_ID_RPC, nullptr), OHOS::Sensors::SUCCESS); + } +} + +HWTEST_F(RPCSensorTest, RPCSensorTest_005, TestSize.Level1) +{ + SEN_HILOGI("RPCSensorTest_005 enter"); + if (g_existRPC) { + SensorUser user; + user.callback = RPCDataCallbackImpl; + ASSERT_NE(DeactivateSensor(-1, &user), OHOS::Sensors::SUCCESS); + } +} + +HWTEST_F(RPCSensorTest, RPCSensorTest_006, TestSize.Level1) +{ + SEN_HILOGI("RPCSensorTest_006 enter"); + if (g_existRPC) { + SensorUser user; + user.callback = nullptr; + ASSERT_NE(DeactivateSensor(SENSOR_TYPE_ID_RPC, &user), OHOS::Sensors::SUCCESS); + } +} + +HWTEST_F(RPCSensorTest, RPCSensorTest_007, TestSize.Level1) +{ + SEN_HILOGI("RPCSensorTest_007 enter"); + if (g_existRPC) { + ASSERT_NE(SetBatch(SENSOR_TYPE_ID_RPC, nullptr, 100000000, 100000000), OHOS::Sensors::SUCCESS); + } +} + +HWTEST_F(RPCSensorTest, RPCSensorTest_008, TestSize.Level1) +{ + SEN_HILOGI("RPCSensorTest_008 enter"); + if (g_existRPC) { + SensorUser user; + user.callback = RPCDataCallbackImpl; + ASSERT_NE(SetBatch(-1, &user, 100000000, 100000000), OHOS::Sensors::SUCCESS); + } +} + +HWTEST_F(RPCSensorTest, RPCSensorTest_009, TestSize.Level1) +{ + SEN_HILOGI("RPCSensorTest_009 enter"); + if (g_existRPC) { + SensorUser user; + user.callback = nullptr; + ASSERT_NE(SetBatch(SENSOR_TYPE_ID_RPC, &user, 100000000, 100000000), OHOS::Sensors::SUCCESS); + } +} + +HWTEST_F(RPCSensorTest, RPCSensorTest_010, TestSize.Level1) +{ + SEN_HILOGI("RPCSensorTest_010 enter"); + if (g_existRPC) { + SensorUser user; + user.callback = RPCDataCallbackImpl; + ASSERT_NE(SetBatch(SENSOR_TYPE_ID_RPC, &user, -1, -1), OHOS::Sensors::SUCCESS); + } +} + +HWTEST_F(RPCSensorTest, RPCSensorTest_011, TestSize.Level1) +{ + SEN_HILOGI("RPCSensorTest_011 enter"); + if (g_existRPC) { + ASSERT_NE(SubscribeSensor(SENSOR_TYPE_ID_RPC, nullptr), OHOS::Sensors::SUCCESS); + } +} + +HWTEST_F(RPCSensorTest, RPCSensorTest_012, TestSize.Level1) +{ + SEN_HILOGI("RPCSensorTest_012 enter"); + if (g_existRPC) { + SensorUser user; + user.callback = RPCDataCallbackImpl; + ASSERT_NE(SubscribeSensor(-1, &user), OHOS::Sensors::SUCCESS); + } +} + +HWTEST_F(RPCSensorTest, RPCSensorTest_013, TestSize.Level1) +{ + SEN_HILOGI("RPCSensorTest_013 enter"); + if (g_existRPC) { + SensorUser user; + user.callback = nullptr; + ASSERT_NE(SubscribeSensor(SENSOR_TYPE_ID_RPC, &user), OHOS::Sensors::SUCCESS); + } +} + +HWTEST_F(RPCSensorTest, RPCSensorTest_014, TestSize.Level1) +{ + SEN_HILOGI("RPCSensorTest_014 enter"); + if (g_existRPC) { + ASSERT_NE(UnsubscribeSensor(SENSOR_TYPE_ID_RPC, nullptr), OHOS::Sensors::SUCCESS); + } +} + +HWTEST_F(RPCSensorTest, RPCSensorTest_015, TestSize.Level1) +{ + SEN_HILOGI("RPCSensorTest_015 enter"); + if (g_existRPC) { + SensorUser user; + user.callback = RPCDataCallbackImpl; + ASSERT_NE(UnsubscribeSensor(-1, &user), OHOS::Sensors::SUCCESS); + } +} + +HWTEST_F(RPCSensorTest, RPCSensorTest_016, TestSize.Level1) +{ + SEN_HILOGI("RPCSensorTest_016 enter"); + if (g_existRPC) { + SensorUser user; + user.callback = nullptr; + ASSERT_NE(UnsubscribeSensor(SENSOR_TYPE_ID_RPC, &user), OHOS::Sensors::SUCCESS); + } +} + +HWTEST_F(RPCSensorTest, RPCSensorTest_017, TestSize.Level1) +{ + SEN_HILOGI("RPCSensorTest_017 enter"); + if (g_existRPC) { + SensorUser user; + user.callback = RPCDataCallbackImpl; + ASSERT_EQ(SubscribeSensor(SENSOR_TYPE_ID_RPC, &user), OHOS::Sensors::SUCCESS); + ASSERT_EQ(SetBatch(SENSOR_TYPE_ID_RPC, &user, 100000000, 100000000), OHOS::Sensors::SUCCESS); + ASSERT_EQ(ActivateSensor(SENSOR_TYPE_ID_RPC, &user), OHOS::Sensors::SUCCESS); + std::this_thread::sleep_for(std::chrono::milliseconds(1000)); + ASSERT_EQ(DeactivateSensor(SENSOR_TYPE_ID_RPC, &user), OHOS::Sensors::SUCCESS); + ASSERT_EQ(UnsubscribeSensor(SENSOR_TYPE_ID_RPC, &user), OHOS::Sensors::SUCCESS); + } +} + +HWTEST_F(RPCSensorTest, RPCSensorTest_018, TestSize.Level1) +{ + SEN_HILOGI("RPCSensorTest_018 enter"); + if (g_existRPC) { + SensorUser user; + user.callback = RPCDataCallbackImpl; + ASSERT_EQ(SubscribeSensor(SENSOR_TYPE_ID_RPC, &user), OHOS::Sensors::SUCCESS); + ASSERT_EQ(SetBatch(SENSOR_TYPE_ID_RPC, &user, 100000000, 100000000), OHOS::Sensors::SUCCESS); + ASSERT_EQ(ActivateSensor(SENSOR_TYPE_ID_RPC, &user), OHOS::Sensors::SUCCESS); + std::this_thread::sleep_for(std::chrono::milliseconds(1000)); + ASSERT_EQ(DeactivateSensor(SENSOR_TYPE_ID_RPC, &user), OHOS::Sensors::SUCCESS); + ASSERT_EQ(UnsubscribeSensor(SENSOR_TYPE_ID_RPC, &user), OHOS::Sensors::SUCCESS); + + SensorUser user2; + user2.callback = RPCDataCallbackImpl2; + ASSERT_EQ(SubscribeSensor(SENSOR_TYPE_ID_RPC, &user2), OHOS::Sensors::SUCCESS); + ASSERT_EQ(SetBatch(SENSOR_TYPE_ID_RPC, &user2, 200000000, 200000000), OHOS::Sensors::SUCCESS); + ASSERT_EQ(ActivateSensor(SENSOR_TYPE_ID_RPC, &user2), OHOS::Sensors::SUCCESS); + std::this_thread::sleep_for(std::chrono::milliseconds(1000)); + ASSERT_EQ(DeactivateSensor(SENSOR_TYPE_ID_RPC, &user2), OHOS::Sensors::SUCCESS); + ASSERT_EQ(UnsubscribeSensor(SENSOR_TYPE_ID_RPC, &user2), OHOS::Sensors::SUCCESS); + } +} +} // namespace Sensors +} // namespace OHOS diff --git a/test/unittest/interfaces/js/sensor/SensorJsSync.test.js b/test/unittest/interfaces/js/sensor/SensorJsSync.test.js new file mode 100644 index 0000000000000000000000000000000000000000..56bb3a2884e6c1b60880b90dddbbcdc002dcb87e --- /dev/null +++ b/test/unittest/interfaces/js/sensor/SensorJsSync.test.js @@ -0,0 +1,113 @@ +/* + * Copyright (C) 2024 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 CommonConstants from './CommonConstants'; +import sensor from '@ohos.sensor' + +import {describe, beforeAll, beforeEach, afterEach, afterAll, it, expect} from 'deccjsunit/index' + +describe("SensorSyncTest", function () { + beforeAll(async function() { + /* + * @tc.setup: setup invoked before all testcases + */ + console.info('beforeAll called') + }) + + afterAll(function() { + /* + * @tc.teardown: teardown invoked after all testcases + */ + console.info('afterAll called') + }) + + beforeEach(function() { + /* + * @tc.setup: setup invoked before each testcases + */ + console.info('beforeEach called') + }) + + afterEach(function() { + /* + * @tc.teardown: teardown invoked after each testcases + */ + console.info('afterEach called') + }) + + /* + * @tc.name: SensorSyncTest_001 + * @tc.desc: verify sensor sync interface + * @tc.size: MediumTest + * @tc.type: Function + * @tc.level: Level 1 + * @tc.require: Issue Number + * @tc.number: SensorSyncTest_001 + */ + it("SensorSyncTest_001", 0, async function (done) { + console.info('----------------------SensorSyncTest_001---------------------------'); + try { + let ret = sensor.getSingleSensorSync(sensor.SensorId.ACCELEROMETER); + console.info('getSingleSensorSync: ' + JSON.stringify(ret)); + done(); + } catch (err) { + console.error('getSingleSensorSync err: ' + JSON.stringify(err)); + done(); + } + }) + + /* + * @tc.name: SensorSyncTest_002 + * @tc.desc: verify sensor sync interface + * @tc.size: MediumTest + * @tc.type: Function + * @tc.level: Level 1 + * @tc.require: Issue Number + * @tc.number: SensorSyncTest_002 + */ + it("SensorSyncTest_002", 0, async function (done) { + console.info('----------------------SensorSyncTest_002---------------------------'); + try { + let ret = sensor.getSingleSensorSync(-1); + expect(false).assertTrue(); + done(); + } catch (err) { + console.error('getSingleSensorSync err: ' + JSON.stringify(err)); + expect(true).assertTrue(); + done(); + } + }) + + /* + * @tc.name: SensorSyncTest_003 + * @tc.desc: verify sensor sync interface + * @tc.size: MediumTest + * @tc.type: Function + * @tc.level: Level 1 + * @tc.require: Issue Number + * @tc.number: SensorSyncTest_003 + */ + it("SensorSyncTest_003", 0, async function (done) { + console.info('----------------------SensorSyncTest_003---------------------------'); + try { + let ret = sensor.getSensorListSync(); + console.info('getSensorListSync: ' + JSON.stringify(ret)); + done(); + } catch (err) { + console.error('getSensorListSync err: ' + JSON.stringify(err)); + expect(err.code).assertEqual(CommonConstants.SERVICE_EXCEPTION_CODE); + done(); + } + }) +}) \ No newline at end of file diff --git a/test/unittest/interfaces/js/sensor/common/CommonConstants.js b/test/unittest/interfaces/js/sensor/common/CommonConstants.js index bc05051cdda5bcd6e1ef95ffb70579dccf01d802..912bede76ce6e8a87413bf0898fcba08063c9a0e 100755 --- a/test/unittest/interfaces/js/sensor/common/CommonConstants.js +++ b/test/unittest/interfaces/js/sensor/common/CommonConstants.js @@ -35,7 +35,12 @@ export default class CommonConstants { /** * Exception code of service */ - static SERVICE_EXCEPTION_CODE = 14500101 + static SERVICE_EXCEPTION_CODE = 14500101; + + /** + * Exception code of sensor not supported + */ + static SENSOR_NO_SUPPORT = 14500102; /** * eps diff --git a/test/unittest/interfaces/kits/sensor_native_test.cpp b/test/unittest/interfaces/kits/sensor_native_test.cpp index eaf6a1a8052f3771d30774d4a55711e5c8498d3b..650165dab01a41b74001ec4bb2202d201ecdf31f 100644 --- a/test/unittest/interfaces/kits/sensor_native_test.cpp +++ b/test/unittest/interfaces/kits/sensor_native_test.cpp @@ -32,40 +32,16 @@ namespace OHOS { namespace Sensors { using namespace testing::ext; using namespace OHOS::HiviewDFX; -using namespace Security::AccessToken; -using Security::AccessToken::AccessTokenID; namespace { -constexpr Sensor_Type SENSOR_ID { SENSOR_TYPE_ACCELEROMETER }; +constexpr Sensor_Type SENSOR_ID { SENSOR_TYPE_AMBIENT_LIGHT }; constexpr uint32_t SENSOR_NAME_LENGTH_MAX = 64; constexpr int64_t SENSOR_SAMPLE_PERIOD = 200000000; constexpr int32_t SLEEP_TIME_MS = 1000; constexpr int64_t INVALID_VALUE = -1; constexpr float INVALID_RESOLUTION = -1.0F; - -PermissionStateFull g_infoManagerTestState = { - .permissionName = "ohos.permission.ACCELEROMETER", - .isGeneral = true, - .resDeviceID = {"local"}, - .grantStatus = {PermissionState::PERMISSION_GRANTED}, - .grantFlags = {1} -}; - -HapPolicyParams g_infoManagerTestPolicyPrams = { - .apl = APL_NORMAL, - .domain = "test.domain", - .permList = {}, - .permStateList = {g_infoManagerTestState} -}; - -HapInfoParams g_infoManagerTestInfoParms = { - .userID = 1, - .bundleName = "sensoragent_test", - .instIndex = 0, - .appIDDesc = "sensorAgentTest" -}; - Sensor_Subscriber *g_user = nullptr; +std::atomic_bool g_existAmbientLight = false; } // namespace class SensorAgentTest : public testing::Test { @@ -74,34 +50,41 @@ public: static void TearDownTestCase(); void SetUp(); void TearDown(); -private: - static AccessTokenID tokenID_; }; -AccessTokenID SensorAgentTest::tokenID_ = 0; - void SensorAgentTest::SetUpTestCase() { - AccessTokenIDEx tokenIdEx = {0}; - tokenIdEx = AccessTokenKit::AllocHapToken(g_infoManagerTestInfoParms, g_infoManagerTestPolicyPrams); - tokenID_ = tokenIdEx.tokenIdExStruct.tokenID; - ASSERT_NE(0, tokenID_); - ASSERT_EQ(0, SetSelfTokenID(tokenID_)); -} - -void SensorAgentTest::TearDownTestCase() -{ - int32_t ret = AccessTokenKit::DeleteToken(tokenID_); - if (tokenID_ != 0) { - ASSERT_EQ(RET_SUCCESS, ret); + SEN_HILOGI("SensorAgentTest SetUpTestCase in"); + uint32_t count = 0; + int32_t ret = OH_Sensor_GetInfos(nullptr, &count); + ASSERT_EQ(ret, SENSOR_SUCCESS); + Sensor_Info **sensors = OH_Sensor_CreateInfos(count); + ASSERT_NE(sensors, nullptr); + ret = OH_Sensor_GetInfos(sensors, &count); + ASSERT_EQ(ret, SENSOR_SUCCESS); + for (uint32_t i = 0; i < count; ++i) { + Sensor_Type sensorType; + ret = OH_SensorInfo_GetType(sensors[i], &sensorType); + ASSERT_EQ(ret, SENSOR_SUCCESS); + if (sensorType == SENSOR_TYPE_AMBIENT_LIGHT) { + g_existAmbientLight = true; + SEN_HILOGI("Exist ambient light sensor"); + break; + } + } + if (!g_existAmbientLight) { + SEN_HILOGI("Not exist ambient light sensor"); } + ret = OH_Sensor_DestroyInfos(sensors, count); + ASSERT_EQ(ret, SENSOR_SUCCESS); + SEN_HILOGI("SensorAgentTest SetUpTestCase end"); } -void SensorAgentTest::SetUp() -{} +void SensorAgentTest::TearDownTestCase() {} + +void SensorAgentTest::SetUp() {} -void SensorAgentTest::TearDown() -{} +void SensorAgentTest::TearDown() {} void SensorDataCallbackImpl(Sensor_Event *event) { @@ -122,9 +105,10 @@ void SensorDataCallbackImpl(Sensor_Event *event) uint32_t length = 0; ret = OH_SensorEvent_GetData(event, &data, &length); ASSERT_EQ(ret, SENSOR_SUCCESS); - SEN_HILOGI("sensorType:%{public}d, dataLen:%{public}d, accuracy:%{public}d" - "x:%{public}f, y:%{public}f, z:%{public}f", sensorType, length, accuracy, - data[0], data[1], data[2]); + SEN_HILOGI("sensorType:%{public}d, dataLen:%{public}d, accuracy:%{public}d", sensorType, length, accuracy); + for (uint32_t i = 0; i < length; ++i) { + SEN_HILOGI("data[%{public}d]:%{public}f", i, data[i]); + } } HWTEST_F(SensorAgentTest, OH_Sensor_GetInfos_001, TestSize.Level1) @@ -178,33 +162,35 @@ HWTEST_F(SensorAgentTest, OH_Sensor_GetInfos_002, TestSize.Level1) HWTEST_F(SensorAgentTest, OH_Sensor_Subscribe_001, TestSize.Level1) { SEN_HILOGI("OH_Sensor_Subscribe_001 in"); - g_user = OH_Sensor_CreateSubscriber(); - int32_t ret = OH_SensorSubscriber_SetCallback(g_user, SensorDataCallbackImpl); - ASSERT_EQ(ret, SENSOR_SUCCESS); + if (g_existAmbientLight) { + g_user = OH_Sensor_CreateSubscriber(); + int32_t ret = OH_SensorSubscriber_SetCallback(g_user, SensorDataCallbackImpl); + ASSERT_EQ(ret, SENSOR_SUCCESS); - Sensor_SubscriptionId *id = OH_Sensor_CreateSubscriptionId(); - ret = OH_SensorSubscriptionId_SetType(id, SENSOR_ID); - ASSERT_EQ(ret, SENSOR_SUCCESS); + Sensor_SubscriptionId *id = OH_Sensor_CreateSubscriptionId(); + ret = OH_SensorSubscriptionId_SetType(id, SENSOR_ID); + ASSERT_EQ(ret, SENSOR_SUCCESS); - Sensor_SubscriptionAttribute *attr = OH_Sensor_CreateSubscriptionAttribute(); - ret = OH_SensorSubscriptionAttribute_SetSamplingInterval(attr, SENSOR_SAMPLE_PERIOD); - ASSERT_EQ(ret, SENSOR_SUCCESS); + Sensor_SubscriptionAttribute *attr = OH_Sensor_CreateSubscriptionAttribute(); + ret = OH_SensorSubscriptionAttribute_SetSamplingInterval(attr, SENSOR_SAMPLE_PERIOD); + ASSERT_EQ(ret, SENSOR_SUCCESS); - ret = OH_Sensor_Subscribe(id, attr, g_user); - ASSERT_EQ(ret, SENSOR_SUCCESS); + ret = OH_Sensor_Subscribe(id, attr, g_user); + ASSERT_EQ(ret, SENSOR_SUCCESS); - std::this_thread::sleep_for(std::chrono::milliseconds(SLEEP_TIME_MS)); - ret = OH_Sensor_Unsubscribe(id, g_user); - ASSERT_EQ(ret, SENSOR_SUCCESS); - if (id != nullptr) { - OH_Sensor_DestroySubscriptionId(id); - } - if (attr != nullptr) { - OH_Sensor_DestroySubscriptionAttribute(attr); - } - if (g_user != nullptr) { - OH_Sensor_DestroySubscriber(g_user); - g_user = nullptr; + std::this_thread::sleep_for(std::chrono::milliseconds(SLEEP_TIME_MS)); + ret = OH_Sensor_Unsubscribe(id, g_user); + ASSERT_EQ(ret, SENSOR_SUCCESS); + if (id != nullptr) { + OH_Sensor_DestroySubscriptionId(id); + } + if (attr != nullptr) { + OH_Sensor_DestroySubscriptionAttribute(attr); + } + if (g_user != nullptr) { + OH_Sensor_DestroySubscriber(g_user); + g_user = nullptr; + } } }