diff --git a/services/native/src/proximity_sensor_controller/proximity_controller_base.cpp b/services/native/src/proximity_sensor_controller/proximity_controller_base.cpp index 5ec3e9268230cf0c2eac230dc9004faaa5d38327..b522a59aa11cac67dc409242a406ffb7ee1ed8fc 100644 --- a/services/native/src/proximity_sensor_controller/proximity_controller_base.cpp +++ b/services/native/src/proximity_sensor_controller/proximity_controller_base.cpp @@ -26,14 +26,33 @@ ProximityControllerBase::ProximityControllerBase(const std::string& name, Sensor { #ifdef HAS_SENSORS_SENSOR_PART POWER_HILOGD(FEATURE_INPUT, "Instance enter"); + callback_ = callback; + name_ = name; + InitProximitySensorUser(); +#endif +} + +ProximityControllerBase::~ProximityControllerBase() +{ +#ifdef HAS_SENSORS_SENSOR_PART + if (IsSupported()) { + UnsubscribeSensor(SENSOR_TYPE_ID_PROXIMITY, &user_); + } +#endif +} + +#ifdef HAS_SENSORS_SENSOR_PART +bool ProximityControllerBase::InitProximitySensorUser() +{ SensorInfo* sensorInfo = nullptr; int32_t count = 0; + const constexpr int32_t PARAM_ZERO = 0; int ret = GetAllSensors(&sensorInfo, &count); - if (ret != 0 || sensorInfo == nullptr) { + if (ret != PARAM_ZERO || sensorInfo == nullptr) { POWER_HILOGE(FEATURE_INPUT, "Get sensors fail, ret=%{public}d", ret); - return; + return false; } - for (int32_t i = 0; i < count; i++) { + for (int32_t i = PARAM_ZERO; i < count; i++) { if (sensorInfo[i].sensorTypeId == SENSOR_TYPE_ID_PROXIMITY) { POWER_HILOGI(FEATURE_INPUT, "Support PROXIMITY sensor"); SetSupported(true); @@ -42,36 +61,25 @@ ProximityControllerBase::ProximityControllerBase(const std::string& name, Sensor } if (!IsSupported()) { POWER_HILOGE(FEATURE_INPUT, "PROXIMITY sensor not support"); - return; + return false; } - if (strcpy_s(user_.name, sizeof(user_.name), name.c_str()) != EOK) { - POWER_HILOGE(FEATURE_INPUT, "strcpy_s user_.name=%{public}s error", name.c_str()); - return; + if (strcpy_s(user_.name, sizeof(user_.name), name_.c_str()) != EOK) { + POWER_HILOGE(FEATURE_INPUT, "strcpy_s user_.name=%{public}s error", name_.c_str()); + return true; } user_.userData = nullptr; - user_.callback = callback; -#endif -} - -ProximityControllerBase::~ProximityControllerBase() -{ -#ifdef HAS_SENSORS_SENSOR_PART - if (IsSupported()) { - UnsubscribeSensor(SENSOR_TYPE_ID_PROXIMITY, &user_); - } -#endif + user_.callback = callback_; + return true; } -#ifdef HAS_SENSORS_SENSOR_PART void ProximityControllerBase::Enable() { POWER_HILOGD(FEATURE_INPUT, "Enter"); SetEnabled(true); - if (!IsSupported()) { - POWER_HILOGE(FEATURE_INPUT, "PROXIMITY sensor not support"); + if (!IsSupported() && !InitProximitySensorUser()) { + POWER_HILOGE(FEATURE_INPUT, "Enable PROXIMITY sensor not support"); return; } - int32_t errorCode = SubscribeSensor(SENSOR_TYPE_ID_PROXIMITY, &user_); if (errorCode != ERR_OK) { POWER_HILOGW(FEATURE_INPUT, "SubscribeSensor PROXIMITY failed, errorCode=%{public}d", errorCode); @@ -92,7 +100,7 @@ void ProximityControllerBase::Disable() POWER_HILOGD(FEATURE_INPUT, "Enter"); SetEnabled(false); if (!IsSupported()) { - POWER_HILOGE(FEATURE_INPUT, "PROXIMITY sensor not support"); + POWER_HILOGE(FEATURE_INPUT, "Disable PROXIMITY sensor not support"); return; } diff --git a/services/native/src/proximity_sensor_controller/proximity_controller_base.h b/services/native/src/proximity_sensor_controller/proximity_controller_base.h index d58731594f17a218492a9f14fa6df3842c5e1c31..7d0b3c8e5f767fe013da4130423a55b98490ddd3 100644 --- a/services/native/src/proximity_sensor_controller/proximity_controller_base.h +++ b/services/native/src/proximity_sensor_controller/proximity_controller_base.h @@ -31,6 +31,7 @@ public: ProximityControllerBase() = default; ProximityControllerBase(const std::string& name, SensorCallbackFunc callback); virtual ~ProximityControllerBase(); + bool InitProximitySensorUser(); void Enable() override; void Disable() override; void OnClose() override {} @@ -39,6 +40,8 @@ public: static const int32_t PROXIMITY_AWAY_SCALAR = 5; static const uint32_t SAMPLING_RATE = 100000000; private: + SensorCallbackFunc callback_ = nullptr; + std::string name_ {}; SensorUser user_ {}; }; } // namespace PowerMgr diff --git a/test/unittest/src/scenario_test/proximity_controller/proximity_controller_base_test.cpp b/test/unittest/src/scenario_test/proximity_controller/proximity_controller_base_test.cpp index a08146d9aab1a35b4af48c75691cacc6b73a1b76..5537dcc73efdb7b1de7c4f65cc31056a3abeac82 100644 --- a/test/unittest/src/scenario_test/proximity_controller/proximity_controller_base_test.cpp +++ b/test/unittest/src/scenario_test/proximity_controller/proximity_controller_base_test.cpp @@ -186,4 +186,168 @@ HWTEST_F(ProximityControllerBaseTest, ProximityControllerBaseTest006, TestSize.L EXPECT_EQ(proximityControllerBase->GetStatus(), 1); POWER_HILOGI(LABEL_TEST, "ProximityControllerBaseTest006 function end!"); } + +/** + * @tc.name: ProximityControllerBaseTest007 + * @tc.desc: Test proximity controller base InitProximitySensorUser normal branch + * @tc.type: FUNC + * @tc.require: ICGV1M + */ +HWTEST_F(ProximityControllerBaseTest, ProximityControllerBaseTest007, TestSize.Level1) +{ + POWER_HILOGI(LABEL_TEST, "ProximityControllerBaseTest007 function start!"); + SensorInfo infos[] {{.sensorTypeId = SENSOR_TYPE_ID_PROXIMITY}}; + g_sensorInfo = infos; + g_count = 1; + std::shared_ptr proximityControllerBase = + std::make_shared("ProximityControllerBaseTest", nullptr); + proximityControllerBase->Enable(); + proximityControllerBase->InitProximitySensorUser(); + EXPECT_TRUE(proximityControllerBase->IsSupported()); + POWER_HILOGI(LABEL_TEST, "ProximityControllerBaseTest007 function end!"); +} + +/** + * @tc.name: ProximityControllerBaseTest008 + * @tc.desc: Test proximity controller base InitProximitySensorUser abnormal branch + * @tc.type: FUNC + * @tc.require: ICGV1M + */ +HWTEST_F(ProximityControllerBaseTest, ProximityControllerBaseTest008, TestSize.Level1) +{ + POWER_HILOGI(LABEL_TEST, "ProximityControllerBaseTest008 function start!"); + SensorInfo infos[] {{.sensorTypeId = SENSOR_TYPE_ID_GESTURE}}; + g_sensorInfo = infos; + g_count = 1; + std::shared_ptr proximityControllerBase = + std::make_shared("ProximityControllerBaseTest", nullptr); + proximityControllerBase->Enable(); + proximityControllerBase->InitProximitySensorUser(); + EXPECT_FALSE(proximityControllerBase->IsSupported()); + POWER_HILOGI(LABEL_TEST, "ProximityControllerBaseTest008 function end!"); +} + +/** + * @tc.name: ProximityControllerBaseTest009 + * @tc.desc: Test proximity controller base InitProximitySensorUser abnormal branch + * @tc.type: FUNC + * @tc.require: ICGV1M + */ +HWTEST_F(ProximityControllerBaseTest, ProximityControllerBaseTest009, TestSize.Level1) +{ + POWER_HILOGI(LABEL_TEST, "ProximityControllerBaseTest009 function start!"); + SensorInfo infos[] {{.sensorTypeId = SENSOR_TYPE_ID_GESTURE}}; + g_sensorInfo = nullptr; + g_count = 1; + std::shared_ptr proximityControllerBase = + std::make_shared("ProximityControllerBaseTest", nullptr); + proximityControllerBase->Enable(); + proximityControllerBase->InitProximitySensorUser(); + EXPECT_FALSE(proximityControllerBase->IsSupported()); + POWER_HILOGI(LABEL_TEST, "ProximityControllerBaseTest009 function end!"); +} + +/** + * @tc.name: ProximityControllerBaseTest010 + * @tc.desc: Test proximity controller base InitProximitySensorUser abnormal branch + * @tc.type: FUNC + * @tc.require: ICGV1M + */ +HWTEST_F(ProximityControllerBaseTest, ProximityControllerBaseTest010, TestSize.Level1) +{ + POWER_HILOGI(LABEL_TEST, "ProximityControllerBaseTest010 function start!"); + SensorInfo infos[] {{.sensorTypeId = SENSOR_TYPE_ID_PROXIMITY}}; + g_sensorInfo = infos; + g_count = 1; + std::shared_ptr proximityControllerBase = std::make_shared( + "This is a very long string, its length is much longer than sixteen", nullptr); + proximityControllerBase->Enable(); + proximityControllerBase->InitProximitySensorUser(); + EXPECT_TRUE(proximityControllerBase->IsSupported()); + POWER_HILOGI(LABEL_TEST, "ProximityControllerBaseTest010 function end!"); +} + +/** + * @tc.name: ProximityControllerBaseTest011 + * @tc.desc: Test proximity controller base InitProximitySensorUser abnormal branch + * @tc.type: FUNC + * @tc.require: ICGV1M + */ +HWTEST_F(ProximityControllerBaseTest, ProximityControllerBaseTest011, TestSize.Level1) +{ + POWER_HILOGI(LABEL_TEST, "ProximityControllerBaseTest011 function start!"); + SensorInfo infos[] {{.sensorTypeId = SENSOR_TYPE_ID_PROXIMITY}}; + g_sensorInfo = infos; + g_count = 1; + std::shared_ptr proximityControllerBase = + std::make_shared("Test", nullptr); + proximityControllerBase->Enable(); + proximityControllerBase->InitProximitySensorUser(); + EXPECT_TRUE(proximityControllerBase->IsSupported()); + POWER_HILOGI(LABEL_TEST, "ProximityControllerBaseTest011 function end!"); +} + +/** + * @tc.name: ProximityControllerBaseTest012 + * @tc.desc: Test proximity controller base InitProximitySensorUser abnormal branch + * @tc.type: FUNC + * @tc.require: ICGV1M + */ +HWTEST_F(ProximityControllerBaseTest, ProximityControllerBaseTest012, TestSize.Level1) +{ + POWER_HILOGI(LABEL_TEST, "ProximityControllerBaseTest012 function start!"); + SensorInfo infos[] {{.sensorTypeId = SENSOR_TYPE_ID_PROXIMITY}}; + g_sensorInfo = nullptr; + g_count = 1; + g_intReturnValue = 1; + std::shared_ptr proximityControllerBase = + std::make_shared("ProximityControllerBaseTest", nullptr); + proximityControllerBase->Enable(); + proximityControllerBase->InitProximitySensorUser(); + EXPECT_FALSE(proximityControllerBase->IsSupported()); + POWER_HILOGI(LABEL_TEST, "ProximityControllerBaseTest012 function end!"); +} + +/** + * @tc.name: ProximityControllerBaseTest013 + * @tc.desc: Test proximity controller base InitProximitySensorUser abnormal branch + * @tc.type: FUNC + * @tc.require: ICGV1M + */ +HWTEST_F(ProximityControllerBaseTest, ProximityControllerBaseTest013, TestSize.Level1) +{ + POWER_HILOGI(LABEL_TEST, "ProximityControllerBaseTest013 function start!"); + SensorInfo infos[] {{.sensorTypeId = SENSOR_TYPE_ID_PROXIMITY}}; + g_sensorInfo = infos; + g_count = 1; + g_intReturnValue = 1; + std::shared_ptr proximityControllerBase = + std::make_shared("ProximityControllerBaseTest", nullptr); + proximityControllerBase->Enable(); + proximityControllerBase->InitProximitySensorUser(); + EXPECT_FALSE(proximityControllerBase->IsSupported()); + POWER_HILOGI(LABEL_TEST, "ProximityControllerBaseTest013 function end!"); +} + +/** + * @tc.name: ProximityControllerBaseTest014 + * @tc.desc: Test proximity controller base InitProximitySensorUser abnormal branch + * @tc.type: FUNC + * @tc.require: ICGV1M + */ +HWTEST_F(ProximityControllerBaseTest, ProximityControllerBaseTest014, TestSize.Level1) +{ + POWER_HILOGI(LABEL_TEST, "ProximityControllerBaseTest014 function start!"); + SensorInfo infos[] {{.sensorTypeId = SENSOR_TYPE_ID_PROXIMITY}}; + g_sensorInfo = infos; + g_count = 1; + g_intReturnValue = 1; + std::shared_ptr proximityControllerBase = + std::make_shared("ProximityControllerBaseTest", nullptr); + g_intReturnValue = 0; + proximityControllerBase->Enable(); + proximityControllerBase->InitProximitySensorUser(); + EXPECT_TRUE(proximityControllerBase->IsSupported()); + POWER_HILOGI(LABEL_TEST, "ProximityControllerBaseTest014 function end!"); +} } // namespace \ No newline at end of file