1 /* 2 * Copyright (c) 2021-2023 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16 #include <cmath> 17 #include <cstdio> 18 #include <unistd.h> 19 #include <gtest/gtest.h> 20 #include <securec.h> 21 #include "hdf_base.h" 22 #include "osal_time.h" 23 #include "v2_1/isensor_interface.h" 24 #include "sensor_type.h" 25 #include "sensor_callback_impl.h" 26 #include "sensor_callback_impl_test_v2_1.h" 27 #include "sensor_callback_impl_test.h" 28 #include "sensor_uhdf_log.h" 29 #include "sensor_trace.h" 30 31 using namespace OHOS::HDI::Sensor::V2_1; 32 using namespace OHOS::HDI::Sensor; 33 using namespace testing::ext; 34 35 namespace { 36 sptr<V2_1::ISensorInterface> g_sensorInterface = nullptr; 37 sptr<V2_0::ISensorCallback> g_traditionalCallback = new SensorCallbackImpl(); 38 sptr<V2_0::ISensorCallback> g_traditionalCallbackTest = new SensorCallbackImplTest(); 39 sptr<V2_1::ISensorCallback> g_traditionalCallbackTestV2_1 = new SensorCallbackImplTestV2_1(); 40 sptr<V2_0::ISensorCallback> g_medicalCallback = new SensorCallbackImpl(); 41 std::vector<HdfSensorInformation> g_info; 42 std::vector<HdfSensorEvents> g_events; 43 struct SensorValueRange { 44 float highThreshold; 45 float lowThreshold; 46 }; 47 48 struct SensorDevelopmentList { 49 int32_t sensorTypeId; 50 char sensorName[SENSOR_NAME_MAX_LEN]; 51 int32_t dataForm; // 0: fixed, 1: range 52 int32_t dataDimension; 53 struct SensorValueRange *valueRange; 54 }; 55 56 struct SensorValueRange g_testRange[] = {{1e5, 0}}; 57 struct SensorValueRange g_accelRange[] = {{78, -78}, {78, -78}, {78, -78}}; 58 struct SensorValueRange g_alsRange[] = {{10000000, 0}}; 59 struct SensorValueRange g_pedometerRange[] = {{10000, 0}}; 60 struct SensorValueRange g_proximityRange[] = {{5, 0}}; 61 struct SensorValueRange g_hallRange[] = {{2, 0}}; 62 struct SensorValueRange g_barometerRange[] = {{1100, -1100}, {1100, -1100}}; 63 struct SensorValueRange g_magneticRange[] = {{2000, -2000}, {2000, -2000}, {2000, -2000}}; 64 struct SensorValueRange g_gyroscopeRange[] = {{35, -35}, {35, -35}, {35, -35}}; 65 struct SensorValueRange g_gravityRange[] = {{78, -78}, {78, -78}, {78, -78}}; 66 struct SensorValueRange g_humidityRange[] = {{100, 0}}; 67 struct SensorValueRange g_temperatureRange[] = {{125, -40}}; 68 69 struct SensorDevelopmentList g_sensorList[] = { 70 {SENSOR_TYPE_NONE, "sensor_test", 1, 1, g_testRange}, 71 {SENSOR_TYPE_ACCELEROMETER, "accelerometer", 1, 3, g_accelRange}, 72 {SENSOR_TYPE_PEDOMETER, "pedometer", 1, 1, g_pedometerRange}, 73 {SENSOR_TYPE_PROXIMITY, "proximity", 0, 1, g_proximityRange}, 74 {SENSOR_TYPE_HALL, "hallrometer", 1, 1, g_hallRange}, 75 {SENSOR_TYPE_BAROMETER, "barometer", 1, 2, g_barometerRange}, 76 {SENSOR_TYPE_AMBIENT_LIGHT, "als", 1, 1, g_alsRange}, 77 {SENSOR_TYPE_MAGNETIC_FIELD, "magnetometer", 1, 3, g_magneticRange}, 78 {SENSOR_TYPE_GYROSCOPE, "gyroscope", 1, 3, g_gyroscopeRange}, 79 {SENSOR_TYPE_GRAVITY, "gravity", 1, 3, g_gravityRange}, 80 {SENSOR_TYPE_HUMIDITY, "humidity", 1, 1, g_humidityRange}, 81 {SENSOR_TYPE_TEMPERATURE, "tenperature", 1, 1, g_temperatureRange} 82 }; 83 84 constexpr int g_listNum = sizeof(g_sensorList) / sizeof(g_sensorList[0]); 85 constexpr int64_t SENSOR_INTERVAL1 = 200000000; 86 constexpr int64_t SENSOR_INTERVAL2 = 20000000; 87 constexpr int64_t SENSOR_INTERVAL3 = 40000000; 88 constexpr int64_t SENSOR_INTERVAL4 = 20000000; 89 constexpr int32_t SENSOR_POLL_TIME = 1; 90 constexpr int32_t SENSOR_WAIT_TIME = 100; 91 constexpr int32_t SENSOR_WAIT_TIME2 = 1000; 92 constexpr int32_t ABNORMAL_SENSORID = -1; 93 constexpr int32_t RATE_LEVEL = 50; 94 95 96 class HdfSensorHdiTest : public testing::Test { 97 public: 98 static void SetUpTestCase(); 99 static void TearDownTestCase(); 100 void SetUp(); 101 void TearDown(); 102 }; 103 SetUpTestCase()104 void HdfSensorHdiTest::SetUpTestCase() 105 { 106 g_sensorInterface = V2_1::ISensorInterface::Get(); 107 } 108 TearDownTestCase()109 void HdfSensorHdiTest::TearDownTestCase() 110 { 111 } 112 SetUp()113 void HdfSensorHdiTest::SetUp() 114 { 115 } 116 TearDown()117 void HdfSensorHdiTest::TearDown() 118 { 119 } 120 121 /** 122 * @tc.name: GetSensorClient0001 123 * @tc.desc: Get a client and check whether the client is empty. 124 * @tc.type: FUNC 125 * @tc.require: #I4L3LF 126 */ 127 HWTEST_F(HdfSensorHdiTest, GetSensorClient0001, TestSize.Level1) 128 { 129 ASSERT_NE(nullptr, g_sensorInterface); 130 } 131 132 /** 133 * @tc.name: GetSensorList0001 134 * @tc.desc: Obtains information about all sensors in the system. 135 * @tc.type: FUNC 136 * @tc.require: #I4L3LF 137 */ 138 HWTEST_F(HdfSensorHdiTest, GetSensorList0001, TestSize.Level1) 139 { 140 if (g_sensorInterface == nullptr) { 141 ASSERT_NE(nullptr, g_sensorInterface); 142 return; 143 } 144 int32_t ret = g_sensorInterface->GetAllSensorInfo(g_info); 145 EXPECT_EQ(SENSOR_SUCCESS, ret); 146 EXPECT_GT(g_info.size(), 0); 147 printf("get sensor list num[%zu]\n\r", g_info.size()); 148 149 for (auto iter : g_info) { 150 printf("get sensoriId[%d], info name[%s], power[%f]\n\r", iter.sensorId, 151 iter.sensorName.c_str(), iter.power); 152 for (int j =0; j < g_listNum; ++j) { 153 if (iter.sensorId == g_sensorList[j].sensorTypeId) { 154 EXPECT_GT(iter.sensorName.size(), 0); 155 break; 156 } 157 } 158 } 159 } 160 161 /** 162 * @tc.name: RegisterSensorDataCb0001 163 * @tc.desc: Returns 0 if the callback is successfully registered; returns a negative value otherwise. 164 * @tc.type: FUNC 165 * @tc.require: SR000F869M, AR000F869P, AR000F8QNL 166 */ 167 HWTEST_F(HdfSensorHdiTest, RegisterSensorDataCb0001, TestSize.Level1) 168 { 169 if (g_sensorInterface == nullptr) { 170 ASSERT_NE(nullptr, g_sensorInterface); 171 return; 172 } 173 int32_t ret = g_sensorInterface->Register(TRADITIONAL_SENSOR_TYPE, g_traditionalCallback); 174 EXPECT_EQ(SENSOR_SUCCESS, ret); 175 ret = g_sensorInterface->Unregister(TRADITIONAL_SENSOR_TYPE, g_traditionalCallback); 176 EXPECT_EQ(SENSOR_SUCCESS, ret); 177 } 178 179 /** 180 * @tc.name: RegisterSensorDataCb0002 181 * @tc.desc: Returns 0 if the callback is successfully registered; returns a negative value otherwise. 182 * @tc.type: FUNC 183 * @tc.require: SR000F869M, AR000F869P, AR000F8QNL 184 */ 185 HWTEST_F(HdfSensorHdiTest, RegisterSensorDataCb0002, TestSize.Level1) 186 { 187 if (g_sensorInterface == nullptr) { 188 ASSERT_NE(nullptr, g_sensorInterface); 189 return; 190 } 191 int32_t ret = g_sensorInterface->Register(MEDICAL_SENSOR_TYPE, g_medicalCallback); 192 EXPECT_EQ(SENSOR_SUCCESS, ret); 193 ret = g_sensorInterface->Unregister(MEDICAL_SENSOR_TYPE, g_medicalCallback); 194 EXPECT_EQ(SENSOR_SUCCESS, ret); 195 } 196 197 /** 198 * @tc.name: RegisterDataCb001 199 * @tc.desc: Returns 0 if the callback is successfully registered; returns a negative value otherwise. 200 * @tc.type: FUNC 201 * @tc.require: SR000F869M, AR000F869P, AR000F8QNL 202 */ 203 HWTEST_F(HdfSensorHdiTest, RegisterSensorDataCb0003, TestSize.Level1) 204 { 205 if (g_sensorInterface == nullptr) { 206 ASSERT_NE(nullptr, g_sensorInterface); 207 return; 208 } 209 int32_t ret = g_sensorInterface->Register(SENSOR_GROUP_TYPE_MAX, g_medicalCallback); 210 EXPECT_EQ(SENSOR_INVALID_PARAM, ret); 211 ret = g_sensorInterface->Unregister(SENSOR_GROUP_TYPE_MAX, g_medicalCallback); 212 EXPECT_EQ(SENSOR_INVALID_PARAM, ret); 213 } 214 215 /** 216 * @tc.name: EnableSensor0001 217 * @tc.desc: Enables the sensor unavailable in the sensor list based on the specified sensor ID. 218 * @tc.type: FUNC 219 * @tc.require: #I4L3LF 220 */ 221 HWTEST_F(HdfSensorHdiTest, EnableSensor0001, TestSize.Level1) 222 { 223 if (g_sensorInterface == nullptr) { 224 ASSERT_NE(nullptr, g_sensorInterface); 225 return; 226 } 227 int32_t ret = g_sensorInterface->Register(TRADITIONAL_SENSOR_TYPE, g_traditionalCallback); 228 EXPECT_EQ(SENSOR_SUCCESS, ret); 229 230 EXPECT_GT(g_info.size(), 0); 231 232 for (auto iter : g_info) { 233 ret = g_sensorInterface->SetBatch(iter.sensorId, SENSOR_INTERVAL1, SENSOR_POLL_TIME); 234 EXPECT_EQ(SENSOR_SUCCESS, ret); 235 ret = g_sensorInterface->Enable(iter.sensorId); 236 EXPECT_EQ(SENSOR_SUCCESS, ret); 237 OsalMSleep(SENSOR_WAIT_TIME); 238 ret = g_sensorInterface->Disable(iter.sensorId); 239 EXPECT_EQ(SENSOR_SUCCESS, ret); 240 } 241 ret = g_sensorInterface->Unregister(TRADITIONAL_SENSOR_TYPE, g_traditionalCallback); 242 EXPECT_EQ(0, ret); 243 EXPECT_EQ(SensorCallbackImpl::sensorDataFlag, 1); 244 SensorCallbackImpl::sensorDataFlag = 1; 245 } 246 247 /** 248 * @tc.name: EnableSensor0002 249 * @tc.desc: Enables the sensor available in the sensor list based on the specified sensor ID. 250 * @tc.type: FUNC 251 * @tc.require: #I4L3LF #I8FJ2I 252 */ 253 HWTEST_F(HdfSensorHdiTest, EnableSensor0002, TestSize.Level1) 254 { 255 if (g_sensorInterface == nullptr) { 256 ASSERT_NE(nullptr, g_sensorInterface); 257 return; 258 } 259 int32_t ret = g_sensorInterface->Enable(ABNORMAL_SENSORID); 260 EXPECT_EQ(SENSOR_NOT_SUPPORT, ret); 261 ret = g_sensorInterface->Disable(ABNORMAL_SENSORID); 262 EXPECT_EQ(SENSOR_NOT_SUPPORT, ret); 263 } 264 265 /** 266 * @tc.name: SetSensorBatch0001 267 * @tc.desc: Sets the sampling time and data report interval for sensors in batches. 268 * @tc.type: FUNC 269 * @tc.require: #I4L3LF 270 */ 271 HWTEST_F(HdfSensorHdiTest, SetSensorBatch0001, TestSize.Level1) 272 { 273 if (g_sensorInterface == nullptr) { 274 ASSERT_NE(nullptr, g_sensorInterface); 275 return; 276 } 277 int32_t ret = g_sensorInterface->Register(TRADITIONAL_SENSOR_TYPE, g_traditionalCallback); 278 EXPECT_EQ(SENSOR_SUCCESS, ret); 279 280 for (auto iter : g_info) { 281 ret = g_sensorInterface->SetBatch(iter.sensorId, SENSOR_INTERVAL2, SENSOR_POLL_TIME); 282 EXPECT_EQ(SENSOR_SUCCESS, ret); 283 ret = g_sensorInterface->Enable(iter.sensorId); 284 EXPECT_EQ(SENSOR_SUCCESS, ret); 285 OsalMSleep(SENSOR_WAIT_TIME); 286 ret = g_sensorInterface->Disable(iter.sensorId); 287 EXPECT_EQ(SENSOR_SUCCESS, ret); 288 } 289 290 ret = g_sensorInterface->Unregister(TRADITIONAL_SENSOR_TYPE, g_traditionalCallback); 291 EXPECT_EQ(SENSOR_SUCCESS, ret); 292 EXPECT_EQ(SensorCallbackImpl::sensorDataFlag, 1); 293 SensorCallbackImpl::sensorDataFlag = 1; 294 } 295 296 /** @tc.name: SetSensorBatch0002 297 @tc.desc: Sets the sampling time and data report interval for sensors in batches. 298 @tc.type: FUNC 299 @tc.requrire: #I4L3LF 300 */ 301 HWTEST_F(HdfSensorHdiTest, SetSensorBatch0002, TestSize.Level1) 302 { 303 if (g_sensorInterface == nullptr) { 304 ASSERT_NE(nullptr, g_sensorInterface); 305 return; 306 } 307 int32_t ret = g_sensorInterface->SetBatch(ABNORMAL_SENSORID, 0, 0); 308 EXPECT_EQ(SENSOR_NOT_SUPPORT, ret); 309 } 310 311 /** 312 * @tc.name: SetSensorBatch0003 313 * @tc.desc: Sets the sampling time and data report interval for sensors in batches. 314 * @tc.type: FUNC 315 * @tc.require: #I4L3LF 316 */ 317 HWTEST_F(HdfSensorHdiTest, SetSensorBatch0003, TestSize.Level1) 318 { 319 if (g_sensorInterface == nullptr) { 320 ASSERT_NE(nullptr, g_sensorInterface); 321 return; 322 } 323 for (auto iter : g_info) { 324 int32_t ret = g_sensorInterface->SetBatch(iter.sensorId, -1, SENSOR_POLL_TIME); 325 EXPECT_EQ(SENSOR_INVALID_PARAM, ret); 326 } 327 } 328 329 /** 330 * @tc.name: SetSensorMode0001 331 * @tc.desc: Sets the data reporting mode for the specified sensor. 332 * @tc.type: FUNC 333 * @tc.require: #I4L3LF 334 */ 335 HWTEST_F(HdfSensorHdiTest, SetSensorMode0001, TestSize.Level1) 336 { 337 if (g_sensorInterface == nullptr) { 338 ASSERT_NE(nullptr, g_sensorInterface); 339 return; 340 } 341 EXPECT_GT(g_info.size(), 0); 342 for (auto iter : g_info) { 343 int32_t ret = g_sensorInterface->SetBatch(iter.sensorId, SENSOR_INTERVAL1, SENSOR_POLL_TIME); 344 EXPECT_EQ(SENSOR_SUCCESS, ret); 345 if (iter.sensorId == SENSOR_TYPE_HALL) { 346 ret = g_sensorInterface->SetMode(iter.sensorId, SENSOR_MODE_ON_CHANGE); 347 EXPECT_EQ(SENSOR_SUCCESS, ret); 348 } else { 349 ret = g_sensorInterface->SetMode(iter.sensorId, SENSOR_MODE_REALTIME); 350 EXPECT_EQ(SENSOR_SUCCESS, ret); 351 } 352 ret = g_sensorInterface->Enable(iter.sensorId); 353 EXPECT_EQ(SENSOR_SUCCESS, ret); 354 OsalMSleep(SENSOR_WAIT_TIME); 355 ret = g_sensorInterface->Disable(iter.sensorId); 356 EXPECT_EQ(SENSOR_SUCCESS, ret); 357 } 358 } 359 360 /** 361 * @tc.name: SetSensorMode0002 362 * @tc.desc: Sets the data reporting mode for the specified sensor.The current real-time polling mode is valid. 363 * Other values are invalid. 364 * @tc.type: FUNC 365 * @tc.require: #I4L3LF 366 */ 367 HWTEST_F(HdfSensorHdiTest, SetSensorMode0002, TestSize.Level1) 368 { 369 if (g_sensorInterface == nullptr) { 370 ASSERT_NE(nullptr, g_sensorInterface); 371 return; 372 } 373 int32_t ret = g_sensorInterface->SetMode(ABNORMAL_SENSORID, SENSOR_MODE_REALTIME); 374 EXPECT_EQ(SENSOR_NOT_SUPPORT, ret); 375 } 376 377 /** 378 * @tc.name: SetSensorMode0003 379 * @tc.desc: Sets the data reporting mode for the specified sensor.The current real-time polling mode is valid. 380 * Other values are invalid. 381 * @tc.type: FUNC 382 * @tc.require: #I4L3LF 383 */ 384 HWTEST_F(HdfSensorHdiTest, SetSensorMode0003, TestSize.Level1) 385 { 386 if (g_sensorInterface == nullptr) { 387 ASSERT_NE(nullptr, g_sensorInterface); 388 return; 389 } 390 EXPECT_GT(g_info.size(), 0); 391 for (auto iter : g_info) { 392 int32_t ret = g_sensorInterface->SetBatch(iter.sensorId, SENSOR_INTERVAL1, SENSOR_POLL_TIME); 393 EXPECT_EQ(SENSOR_SUCCESS, ret); 394 ret = g_sensorInterface->SetMode(iter.sensorId, SENSOR_MODE_DEFAULT); 395 EXPECT_EQ(SENSOR_FAILURE, ret); 396 ret = g_sensorInterface->Enable(iter.sensorId); 397 EXPECT_EQ(SENSOR_SUCCESS, ret); 398 OsalMSleep(SENSOR_WAIT_TIME); 399 ret = g_sensorInterface->Disable(iter.sensorId); 400 EXPECT_EQ(SENSOR_SUCCESS, ret); 401 } 402 } 403 404 /** 405 * @tc.name: SetSensorOption0001 406 * @tc.desc: Sets options for the specified sensor, including its measurement range and accuracy. 407 * @tc.type: FUNC 408 * @tc.require: #I4L3LF 409 */ 410 HWTEST_F(HdfSensorHdiTest, SetSensorOption0001, TestSize.Level1) 411 { 412 if (g_sensorInterface == nullptr) { 413 ASSERT_NE(nullptr, g_sensorInterface); 414 return; 415 } 416 EXPECT_GT(g_info.size(), 0); 417 for (auto iter : g_info) { 418 int32_t ret = g_sensorInterface->SetOption(iter.sensorId, 0); 419 EXPECT_EQ(SENSOR_SUCCESS, ret); 420 } 421 } 422 423 /** 424 * @tc.name: SetSensorOption0002 425 * @tc.desc: Sets options for the specified sensor, including its measurement range and accuracy. 426 * @tc.type: FUNC 427 * @tc.require: #I4L3LF 428 */ 429 HWTEST_F(HdfSensorHdiTest, SetSensorOption0002, TestSize.Level1) 430 { 431 if (g_sensorInterface == nullptr) { 432 ASSERT_NE(nullptr, g_sensorInterface); 433 return; 434 } 435 int32_t ret = g_sensorInterface->SetOption(ABNORMAL_SENSORID, 0); 436 EXPECT_EQ(SENSOR_NOT_SUPPORT, ret); 437 } 438 439 /** 440 * @tc.name: ReadSensorData0001 441 * @tc.desc: Read event data for the specified sensor. 442 * @tc.type: FUNC 443 * @tc.require: #I4L3LF 444 */ 445 HWTEST_F(HdfSensorHdiTest, ReadSensorData0001, TestSize.Level1) 446 { 447 ASSERT_NE(nullptr, g_sensorInterface); 448 449 EXPECT_GT(g_info.size(), 0); 450 for (auto iter : g_info) { 451 int32_t ret = g_sensorInterface->Enable(iter.sensorId); 452 EXPECT_EQ(SENSOR_SUCCESS, ret); 453 OsalMSleep(SENSOR_WAIT_TIME); 454 ret = g_sensorInterface->ReadData(iter.sensorId, g_events); 455 EXPECT_EQ(SENSOR_SUCCESS, ret); 456 ret = g_sensorInterface->Disable(iter.sensorId); 457 EXPECT_EQ(SENSOR_SUCCESS, ret); 458 } 459 } 460 461 /** 462 * @tc.name: SetSdcSensor 463 * @tc.desc: Read event data for the specified sensor. 464 * @tc.type: FUNC 465 * @tc.require: #I4L3LF 466 */ 467 HWTEST_F(HdfSensorHdiTest, SetSdcSensor, TestSize.Level1) 468 { 469 ASSERT_NE(nullptr, g_sensorInterface); 470 471 EXPECT_GT(g_info.size(), 0); 472 for (auto iter : g_info) { 473 int32_t ret = g_sensorInterface->SetSdcSensor(iter.sensorId, true, RATE_LEVEL); 474 EXPECT_EQ(SENSOR_SUCCESS, ret); 475 OsalMSleep(SENSOR_WAIT_TIME); 476 ret = g_sensorInterface->SetSdcSensor(iter.sensorId, false, RATE_LEVEL); 477 EXPECT_EQ(SENSOR_SUCCESS, ret); 478 } 479 } 480 481 /** 482 * @tc.name: GetSdcSensorInfo 483 * @tc.desc: Read event data for the specified sensor. 484 * @tc.type: FUNC 485 * @tc.require: #I4L3LF 486 */ 487 HWTEST_F(HdfSensorHdiTest, GetSdcSensorInfo, TestSize.Level1) 488 { 489 ASSERT_NE(nullptr, g_sensorInterface); 490 491 EXPECT_GT(g_info.size(), 0); 492 std::vector<OHOS::HDI::Sensor::V2_0::SdcSensorInfo> sdcSensorInfo; 493 int32_t ret = g_sensorInterface->GetSdcSensorInfo(sdcSensorInfo); 494 EXPECT_EQ(SENSOR_SUCCESS, ret); 495 std::string infoMsg = "["; 496 for (auto it : sdcSensorInfo) { 497 if (infoMsg != "[") { 498 infoMsg += ", "; 499 } 500 infoMsg += "{"; 501 infoMsg += "offset = " + std::to_string(it.offset) + ", "; 502 infoMsg += "sensorId = " + std::to_string(it.sensorId) + ", "; 503 infoMsg += "ddrSize = " + std::to_string(it.ddrSize) + ", "; 504 infoMsg += "minRateLevel = " + std::to_string(it.minRateLevel) + ", "; 505 infoMsg += "maxRateLevel = " + std::to_string(it.maxRateLevel) + ", "; 506 infoMsg += "memAddr = " + std::to_string(it.memAddr) + ", "; 507 infoMsg += "reserved = " + std::to_string(it.reserved); 508 infoMsg += "}"; 509 } 510 infoMsg += "]"; 511 HDF_LOGI("%{public}s: sdcSensorInfo = %{public}s", __func__, infoMsg.c_str()); 512 } 513 514 /** 515 * @tc.name: ReportFrequencyTest0001 516 * @tc.desc: Sets the sampling time and data report interval for sensors in batches. 517 * @tc.type: FUNC 518 * @tc.require: #I4L3LF 519 */ 520 HWTEST_F(HdfSensorHdiTest, ReportFrequencyTest0001, TestSize.Level1) 521 { 522 HDF_LOGI("enter the ReportFrequencyTest0001 function"); 523 ASSERT_NE(nullptr, g_sensorInterface); 524 525 int32_t ret = g_sensorInterface->Register(TRADITIONAL_SENSOR_TYPE, g_traditionalCallback); 526 EXPECT_EQ(SENSOR_SUCCESS, ret); 527 528 EXPECT_GT(g_info.size(), 0); 529 int32_t sensorId = g_info[0].sensorId; 530 HDF_LOGI("sensorId is %{public}d", sensorId); 531 532 ret = g_sensorInterface->SetBatch(sensorId, SENSOR_INTERVAL1, SENSOR_INTERVAL1); 533 EXPECT_EQ(SENSOR_SUCCESS, ret); 534 535 ret = g_sensorInterface->Enable(sensorId); 536 EXPECT_EQ(SENSOR_SUCCESS, ret); 537 538 OsalMSleep(SENSOR_WAIT_TIME2); 539 540 ret = g_sensorInterface->Disable(sensorId); 541 EXPECT_EQ(SENSOR_SUCCESS, ret); 542 543 ret = g_sensorInterface->Unregister(TRADITIONAL_SENSOR_TYPE, g_traditionalCallback); 544 EXPECT_EQ(SENSOR_SUCCESS, ret); 545 546 EXPECT_EQ(SensorCallbackImpl::sensorDataFlag, 1); 547 SensorCallbackImpl::sensorDataFlag = 1; 548 } 549 550 /** 551 * @tc.name: ReportFrequencyTest0002 552 * @tc.desc: Sets the sampling time and data report interval for sensors in batches. 553 * @tc.type: FUNC 554 * @tc.require: #I4L3LF 555 */ 556 HWTEST_F(HdfSensorHdiTest, ReportFrequencyTest0002, TestSize.Level1) 557 { 558 HDF_LOGI("enter the ReportFrequencyTest0002 function"); 559 ASSERT_NE(nullptr, g_sensorInterface); 560 561 int32_t ret = g_sensorInterface->Register(TRADITIONAL_SENSOR_TYPE, g_traditionalCallback); 562 EXPECT_EQ(SENSOR_SUCCESS, ret); 563 564 EXPECT_GT(g_info.size(), 0); 565 int32_t sensorId = g_info[0].sensorId; 566 HDF_LOGI("sensorId is %{public}d", sensorId); 567 568 ret = g_sensorInterface->SetBatch(sensorId, SENSOR_INTERVAL3, SENSOR_INTERVAL1); 569 EXPECT_EQ(SENSOR_SUCCESS, ret); 570 571 ret = g_sensorInterface->Enable(sensorId); 572 EXPECT_EQ(SENSOR_SUCCESS, ret); 573 574 OsalMSleep(SENSOR_WAIT_TIME2); 575 576 ret = g_sensorInterface->Disable(sensorId); 577 EXPECT_EQ(SENSOR_SUCCESS, ret); 578 579 ret = g_sensorInterface->Unregister(TRADITIONAL_SENSOR_TYPE, g_traditionalCallback); 580 EXPECT_EQ(SENSOR_SUCCESS, ret); 581 582 EXPECT_EQ(SensorCallbackImpl::sensorDataFlag, 1); 583 SensorCallbackImpl::sensorDataFlag = 1; 584 } 585 586 /** 587 * @tc.name: ReportFrequencyTest0003 588 * @tc.desc: Sets the sampling time and data report interval for sensors in batches. 589 * @tc.type: FUNC 590 * @tc.require: #I4L3LF 591 */ 592 HWTEST_F(HdfSensorHdiTest, ReportFrequencyTest0003, TestSize.Level1) 593 { 594 HDF_LOGI("enter the ReportFrequencyTest0003 function"); 595 ASSERT_NE(nullptr, g_sensorInterface); 596 597 int32_t ret = g_sensorInterface->Register(TRADITIONAL_SENSOR_TYPE, g_traditionalCallback); 598 EXPECT_EQ(SENSOR_SUCCESS, ret); 599 600 EXPECT_GT(g_info.size(), 0); 601 int32_t sensorId = g_info[0].sensorId; 602 HDF_LOGI("sensorId is %{public}d", sensorId); 603 604 ret = g_sensorInterface->SetBatch(sensorId, SENSOR_INTERVAL4, SENSOR_INTERVAL1); 605 EXPECT_EQ(SENSOR_SUCCESS, ret); 606 607 ret = g_sensorInterface->Enable(sensorId); 608 EXPECT_EQ(SENSOR_SUCCESS, ret); 609 610 OsalMSleep(SENSOR_WAIT_TIME2); 611 612 ret = g_sensorInterface->Disable(sensorId); 613 EXPECT_EQ(SENSOR_SUCCESS, ret); 614 615 ret = g_sensorInterface->Unregister(TRADITIONAL_SENSOR_TYPE, g_traditionalCallback); 616 EXPECT_EQ(SENSOR_SUCCESS, ret); 617 618 EXPECT_EQ(SensorCallbackImpl::sensorDataFlag, 1); 619 SensorCallbackImpl::sensorDataFlag = 1; 620 } 621 622 /** 623 * @tc.name: SetSdcSensor_001 624 * @tc.desc: Read event data for the specified sensor. 625 * @tc.type: FUNC 626 * @tc.require: #I4L3LF 627 */ 628 HWTEST_F(HdfSensorHdiTest, SetSdcSensor_001, TestSize.Level1) 629 { 630 SENSOR_TRACE; 631 HDF_LOGI("enter the SetSdcSensor_001 function"); 632 ASSERT_NE(nullptr, g_sensorInterface); 633 int32_t ret; 634 EXPECT_GT(g_info.size(), 0); 635 for (auto iter : g_info) { 636 ret = g_sensorInterface->SetSdcSensor(iter.sensorId, true, RATE_LEVEL); 637 EXPECT_EQ(SENSOR_SUCCESS, ret); 638 ret = g_sensorInterface->Disable(iter.sensorId); 639 EXPECT_EQ(SENSOR_SUCCESS, ret); 640 ret = g_sensorInterface->SetSdcSensor(iter.sensorId, false, RATE_LEVEL); 641 EXPECT_EQ(SENSOR_SUCCESS, ret); 642 } 643 } 644 645 /** 646 * @tc.name: EnableButUnregisterTest 647 * @tc.desc: Read event data for the specified sensor. 648 * @tc.type: FUNC 649 * @tc.require: #I4L3LF 650 */ 651 HWTEST_F(HdfSensorHdiTest, EnableButUnregisterTest, TestSize.Level1) 652 { 653 SENSOR_TRACE; 654 ASSERT_NE(nullptr, g_sensorInterface); 655 HDF_LOGI("enter the EnableButUnregisterTest function"); 656 657 int32_t ret = g_sensorInterface->Register(TRADITIONAL_SENSOR_TYPE, g_traditionalCallback); 658 EXPECT_EQ(SENSOR_SUCCESS, ret); 659 EXPECT_GT(g_info.size(), 0); 660 for (auto iter : g_info) { 661 int32_t ret = g_sensorInterface->Enable(iter.sensorId); 662 EXPECT_EQ(SENSOR_SUCCESS, ret); 663 } 664 ret = g_sensorInterface->Unregister(TRADITIONAL_SENSOR_TYPE, g_traditionalCallback); 665 EXPECT_EQ(SENSOR_SUCCESS, ret); 666 OsalMSleep(SENSOR_WAIT_TIME2); 667 for (auto iter : g_info) { 668 int32_t ret = g_sensorInterface->Disable(iter.sensorId); 669 EXPECT_EQ(SENSOR_SUCCESS, ret); 670 } 671 } 672 673 /** 674 * @tc.name: SensorCallbackImplFailureTest 675 * @tc.desc: Read event data for the specified sensor. 676 * @tc.type: FUNC 677 * @tc.require: #I4L3LF 678 */ 679 HWTEST_F(HdfSensorHdiTest, SensorCallbackImplFailureTest, TestSize.Level1) 680 { 681 SENSOR_TRACE; 682 ASSERT_NE(nullptr, g_sensorInterface); 683 HDF_LOGI("enter the SensorCallbackImplFailureTest function"); 684 685 int32_t ret = g_sensorInterface->Register(TRADITIONAL_SENSOR_TYPE, g_traditionalCallbackTest); 686 EXPECT_EQ(SENSOR_SUCCESS, ret); 687 EXPECT_GT(g_info.size(), 0); 688 for (auto iter : g_info) { 689 int32_t ret = g_sensorInterface->Enable(iter.sensorId); 690 EXPECT_EQ(SENSOR_SUCCESS, ret); 691 } 692 OsalMSleep(SENSOR_WAIT_TIME2); 693 for (auto iter : g_info) { 694 int32_t ret = g_sensorInterface->Disable(iter.sensorId); 695 EXPECT_EQ(SENSOR_SUCCESS, ret); 696 } 697 ret = g_sensorInterface->Unregister(TRADITIONAL_SENSOR_TYPE, g_traditionalCallback); 698 EXPECT_EQ(SENSOR_SUCCESS, ret); 699 } 700 701 /** 702 * @tc.name: V2_1_EnableSensor 703 * @tc.desc: Enables the sensor unavailable in the sensor list based on the specified sensor ID. 704 * @tc.type: FUNC 705 * @tc.require: #I4L3LF 706 */ 707 HWTEST_F(HdfSensorHdiTest, V2_1_EnableSensor, TestSize.Level1) 708 { 709 HDF_LOGI("enter the V2_1_EnableSensor function"); 710 ASSERT_NE(nullptr, g_sensorInterface); 711 712 int32_t ret = g_sensorInterface->RegisterAsync(TRADITIONAL_SENSOR_TYPE, g_traditionalCallbackTestV2_1); 713 EXPECT_EQ(SENSOR_SUCCESS, ret); 714 715 for (auto iter : g_info) { 716 ret = g_sensorInterface->SetBatch(iter.sensorId, SENSOR_INTERVAL1, SENSOR_POLL_TIME); 717 EXPECT_EQ(SENSOR_SUCCESS, ret); 718 ret = g_sensorInterface->Enable(iter.sensorId); 719 EXPECT_EQ(SENSOR_SUCCESS, ret); 720 } 721 722 OsalMSleep(SENSOR_WAIT_TIME2); 723 724 for (auto iter : g_info) { 725 ret = g_sensorInterface->Disable(iter.sensorId); 726 EXPECT_EQ(SENSOR_SUCCESS, ret); 727 } 728 ret = g_sensorInterface->UnregisterAsync(TRADITIONAL_SENSOR_TYPE, g_traditionalCallbackTestV2_1); 729 EXPECT_EQ(SENSOR_SUCCESS, ret); 730 } 731 }