• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2023-2024 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 <cinttypes>
17 #include <memory>
18 #include <gtest/gtest.h>
19 #include <thread>
20 
21 #include "accesstoken_kit.h"
22 #include "nativetoken_kit.h"
23 #include "token_setproc.h"
24 
25 #include "oh_sensor.h"
26 #include "sensor_errors.h"
27 
28 #undef LOG_TAG
29 #define LOG_TAG "SensorAgentTest"
30 
31 namespace OHOS {
32 namespace Sensors {
33 using namespace testing::ext;
34 using namespace OHOS::HiviewDFX;
35 
36 namespace {
37 constexpr Sensor_Type SENSOR_ID { SENSOR_TYPE_AMBIENT_LIGHT };
38 constexpr Sensor_Type SECOND_SENSOR_ID { SENSOR_TYPE_ROTATION_VECTOR };
39 constexpr Sensor_Type THIRD_SENSOR_ID { SENSOR_TYPE_GAME_ROTATION_VECTOR };
40 constexpr uint32_t SENSOR_NAME_LENGTH_MAX = 64;
41 constexpr int64_t SENSOR_SAMPLE_PERIOD = 200000000;
42 constexpr int32_t SLEEP_TIME_MS = 1000;
43 constexpr int64_t INVALID_VALUE = -1;
44 constexpr float INVALID_RESOLUTION = -1.0F;
45 Sensor_Subscriber *g_user = nullptr;
46 Sensor_Subscriber *g_user2 = nullptr;
47 std::atomic_bool g_existAmbientLight = false;
48 }  // namespace
49 
50 class SensorAgentTest : public testing::Test {
51 public:
52     static void SetUpTestCase();
53     static void TearDownTestCase();
54     void SetUp();
55     void TearDown();
56 };
57 
SetUpTestCase()58 void SensorAgentTest::SetUpTestCase()
59 {
60     SEN_HILOGI("SensorAgentTest SetUpTestCase in");
61     uint32_t count = 0;
62     int32_t ret = OH_Sensor_GetInfos(nullptr, &count);
63     ASSERT_EQ(ret, SENSOR_SUCCESS);
64     Sensor_Info **sensors = OH_Sensor_CreateInfos(count);
65     ASSERT_NE(sensors, nullptr);
66     ret = OH_Sensor_GetInfos(sensors, &count);
67     ASSERT_EQ(ret, SENSOR_SUCCESS);
68     for (uint32_t i = 0; i < count; ++i) {
69         Sensor_Type sensorType;
70         ret = OH_SensorInfo_GetType(sensors[i], &sensorType);
71         ASSERT_EQ(ret, SENSOR_SUCCESS);
72         if (sensorType == SENSOR_TYPE_AMBIENT_LIGHT) {
73             g_existAmbientLight = true;
74             SEN_HILOGI("SensorAgentTest SetUpTestCase ,Exist AmbientLight sensor");
75             break;
76         }
77     }
78     if (!g_existAmbientLight) {
79         SEN_HILOGI("SensorAgentTest SetUpTestCase ,Not exist AmbientLight sensor");
80     }
81     ret = OH_Sensor_DestroyInfos(sensors, count);
82     ASSERT_EQ(ret, SENSOR_SUCCESS);
83     SEN_HILOGI("SensorAgentTest SetUpTestCase end");
84 }
85 
TearDownTestCase()86 void SensorAgentTest::TearDownTestCase() {}
87 
SetUp()88 void SensorAgentTest::SetUp() {}
89 
TearDown()90 void SensorAgentTest::TearDown() {}
91 
SensorDataCallbackImpl(Sensor_Event * event)92 void SensorDataCallbackImpl(Sensor_Event *event)
93 {
94     if (event == nullptr) {
95         SEN_HILOGE("event is null");
96         return;
97     }
98     int64_t timestamp = INVALID_VALUE;
99     int32_t ret = OH_SensorEvent_GetTimestamp(event, &timestamp);
100     ASSERT_EQ(ret, SENSOR_SUCCESS);
101     Sensor_Type sensorType;
102     ret = OH_SensorEvent_GetType(event, &sensorType);
103     ASSERT_EQ(ret, SENSOR_SUCCESS);
104     Sensor_Accuracy accuracy = SENSOR_ACCURACY_UNRELIABLE;
105     ret = OH_SensorEvent_GetAccuracy(event, &accuracy);
106     ASSERT_EQ(ret, SENSOR_SUCCESS);
107     float *data = nullptr;
108     uint32_t length = 0;
109     ret = OH_SensorEvent_GetData(event, &data, &length);
110     ASSERT_EQ(ret, SENSOR_SUCCESS);
111     SEN_HILOGI("sensorType:%{public}d, dataLen:%{public}d, accuracy:%{public}d", sensorType, length, accuracy);
112     for (uint32_t i = 0; i < length; ++i) {
113         SEN_HILOGI("data[%{public}d]:%{public}f", i, data[i]);
114     }
115 }
116 
117 HWTEST_F(SensorAgentTest, OH_Sensor_GetInfos_001, TestSize.Level1)
118 {
119     SEN_HILOGI("OH_Sensor_GetInfos_001 in");
120     uint32_t count = 0;
121     int32_t ret = OH_Sensor_GetInfos(nullptr, &count);
122     ASSERT_EQ(ret, SENSOR_SUCCESS);
123     Sensor_Info **sensors = OH_Sensor_CreateInfos(count);
124     ASSERT_NE(sensors, nullptr);
125     ret = OH_Sensor_GetInfos(sensors, &count);
126     ASSERT_EQ(ret, SENSOR_SUCCESS);
127     for (uint32_t i = 0; i < count; ++i) {
128         char sensorName[SENSOR_NAME_LENGTH_MAX] = {};
129         uint32_t length = SENSOR_NAME_LENGTH_MAX;
130         ret = OH_SensorInfo_GetName(sensors[i], sensorName, &length);
131         ASSERT_EQ(ret, SENSOR_SUCCESS);
132         char vendorName[SENSOR_NAME_LENGTH_MAX] = {};
133         length = SENSOR_NAME_LENGTH_MAX;
134         ret = OH_SensorInfo_GetVendorName(sensors[i], vendorName, &length);
135         ASSERT_EQ(ret, SENSOR_SUCCESS);
136         Sensor_Type sensorType;
137         ret = OH_SensorInfo_GetType(sensors[i], &sensorType);
138         ASSERT_EQ(ret, SENSOR_SUCCESS);
139         float resolution = INVALID_RESOLUTION;
140         ret = OH_SensorInfo_GetResolution(sensors[i], &resolution);
141         ASSERT_EQ(ret, SENSOR_SUCCESS);
142         int64_t minSamplePeriod = INVALID_VALUE;
143         ret = OH_SensorInfo_GetMinSamplingInterval(sensors[i], &minSamplePeriod);
144         ASSERT_EQ(ret, SENSOR_SUCCESS);
145         int64_t maxSamplePeriod = INVALID_VALUE;
146         ret = OH_SensorInfo_GetMaxSamplingInterval(sensors[i], &maxSamplePeriod);
147         ASSERT_EQ(ret, SENSOR_SUCCESS);
148         SEN_HILOGI("sensorType:%{public}d, sensorName:%{public}s, vendorName:%{public}s,"
149             "resolution:%{public}f, minSamplePeriod:%{public}" PRId64 "maxSamplePeriod:%{public}" PRId64,
150             static_cast<int32_t>(sensorType), sensorName, vendorName,
151             resolution, minSamplePeriod, maxSamplePeriod);
152     }
153     ret = OH_Sensor_DestroyInfos(sensors, count);
154     ASSERT_EQ(ret, SENSOR_SUCCESS);
155 }
156 
157 HWTEST_F(SensorAgentTest, OH_Sensor_GetInfos_002, TestSize.Level1)
158 {
159     SEN_HILOGI("OH_Sensor_GetInfos_003 in");
160     Sensor_Info *sensors { nullptr };
161     int32_t ret = OH_Sensor_GetInfos(&sensors, nullptr);
162     ASSERT_NE(ret, SENSOR_SUCCESS);
163 }
164 
165 HWTEST_F(SensorAgentTest, OH_Sensor_Subscribe_001, TestSize.Level1)
166 {
167     SEN_HILOGI("OH_Sensor_Subscribe_001 in");
168     if (g_existAmbientLight) {
169         g_user = OH_Sensor_CreateSubscriber();
170         int32_t ret = OH_SensorSubscriber_SetCallback(g_user, SensorDataCallbackImpl);
171         ASSERT_EQ(ret, SENSOR_SUCCESS);
172 
173         Sensor_SubscriptionId *id = OH_Sensor_CreateSubscriptionId();
174         ret = OH_SensorSubscriptionId_SetType(id, SENSOR_ID);
175         ASSERT_EQ(ret, SENSOR_SUCCESS);
176 
177         Sensor_SubscriptionAttribute *attr = OH_Sensor_CreateSubscriptionAttribute();
178         ret = OH_SensorSubscriptionAttribute_SetSamplingInterval(attr, SENSOR_SAMPLE_PERIOD);
179         ASSERT_EQ(ret, SENSOR_SUCCESS);
180 
181         ret = OH_Sensor_Subscribe(id, attr, g_user);
182         ASSERT_EQ(ret, SENSOR_SUCCESS);
183 
184         std::this_thread::sleep_for(std::chrono::milliseconds(SLEEP_TIME_MS));
185         ret = OH_Sensor_Unsubscribe(id, g_user);
186         ASSERT_EQ(ret, SENSOR_SUCCESS);
187         if (id != nullptr) {
188             OH_Sensor_DestroySubscriptionId(id);
189         }
190         if (attr != nullptr) {
191             OH_Sensor_DestroySubscriptionAttribute(attr);
192         }
193         if (g_user != nullptr) {
194             OH_Sensor_DestroySubscriber(g_user);
195             g_user = nullptr;
196         }
197     }
198 }
199 
200 HWTEST_F(SensorAgentTest, OH_Sensor_Subscribe_002, TestSize.Level1)
201 {
202     SEN_HILOGI("OH_Sensor_Subscribe_002 in");
203     Sensor_SubscriptionId *id = OH_Sensor_CreateSubscriptionId();
204     int32_t ret = OH_SensorSubscriptionId_SetType(id, SENSOR_ID);
205     ASSERT_EQ(ret, SENSOR_SUCCESS);
206 
207     Sensor_SubscriptionAttribute *attr = OH_Sensor_CreateSubscriptionAttribute();
208     ret = OH_SensorSubscriptionAttribute_SetSamplingInterval(attr, SENSOR_SAMPLE_PERIOD);
209     ASSERT_EQ(ret, SENSOR_SUCCESS);
210 
211     ret = OH_Sensor_Subscribe(id, attr, nullptr);
212     ASSERT_EQ(ret, SENSOR_PARAMETER_ERROR);
213     if (id != nullptr) {
214         OH_Sensor_DestroySubscriptionId(id);
215     }
216     if (attr != nullptr) {
217         OH_Sensor_DestroySubscriptionAttribute(attr);
218     }
219 }
220 
221 HWTEST_F(SensorAgentTest, OH_Sensor_Subscribe_003, TestSize.Level1)
222 {
223     SEN_HILOGI("OH_Sensor_Subscribe_003 in");
224     g_user = OH_Sensor_CreateSubscriber();
225     int32_t ret = OH_SensorSubscriber_SetCallback(g_user, SensorDataCallbackImpl);
226     ASSERT_EQ(ret, SENSOR_SUCCESS);
227 
228     Sensor_SubscriptionAttribute *attr = OH_Sensor_CreateSubscriptionAttribute();
229     ret = OH_SensorSubscriptionAttribute_SetSamplingInterval(attr, SENSOR_SAMPLE_PERIOD);
230     ASSERT_EQ(ret, SENSOR_SUCCESS);
231 
232     ret = OH_Sensor_Subscribe(nullptr, attr, g_user);
233     ASSERT_EQ(ret, SENSOR_PARAMETER_ERROR);
234     if (attr != nullptr) {
235         OH_Sensor_DestroySubscriptionAttribute(attr);
236     }
237     if (g_user != nullptr) {
238         OH_Sensor_DestroySubscriber(g_user);
239         g_user = nullptr;
240     }
241 }
242 
243 HWTEST_F(SensorAgentTest, OH_Sensor_Subscribe_004, TestSize.Level1)
244 {
245     SEN_HILOGI("OH_Sensor_Subscribe_004 in");
246     g_user = OH_Sensor_CreateSubscriber();
247     int32_t ret = OH_SensorSubscriber_SetCallback(g_user, SensorDataCallbackImpl);
248     ASSERT_EQ(ret, SENSOR_SUCCESS);
249 
250     Sensor_SubscriptionId *id = OH_Sensor_CreateSubscriptionId();
251     ret = OH_SensorSubscriptionId_SetType(id, SENSOR_ID);
252     ASSERT_EQ(ret, SENSOR_SUCCESS);
253 
254     ret = OH_Sensor_Subscribe(id, nullptr, g_user);
255     ASSERT_EQ(ret, SENSOR_PARAMETER_ERROR);
256     if (id != nullptr) {
257         OH_Sensor_DestroySubscriptionId(id);
258     }
259     if (g_user != nullptr) {
260         OH_Sensor_DestroySubscriber(g_user);
261         g_user = nullptr;
262     }
263 }
264 
265 HWTEST_F(SensorAgentTest, OH_Sensor_Subscribe_005, TestSize.Level1)
266 {
267     SEN_HILOGI("OH_Sensor_Subscribe_005 in");
268     uint32_t count = 0;
269     int32_t ret = OH_Sensor_GetInfos(nullptr, &count);
270     ASSERT_EQ(ret, SENSOR_SUCCESS);
271     Sensor_Info **sensors = OH_Sensor_CreateInfos(count);
272     for (uint32_t i = 0; i < count; ++i) {
273         Sensor_Type sensorType;
274         ret = OH_SensorInfo_GetType(sensors[i], &sensorType);
275         ASSERT_EQ(ret, SENSOR_SUCCESS);
276 
277         if (sensorType == THIRD_SENSOR_ID) {
278             g_user = OH_Sensor_CreateSubscriber();
279             ret = OH_SensorSubscriber_SetCallback(g_user, SensorDataCallbackImpl);
280             ASSERT_EQ(ret, SENSOR_SUCCESS);
281 
282             Sensor_SubscriptionId *id = OH_Sensor_CreateSubscriptionId();
283             ret = OH_SensorSubscriptionId_SetType(id, SECOND_SENSOR_ID);
284             ASSERT_EQ(ret, SENSOR_SUCCESS);
285 
286             Sensor_SubscriptionAttribute *attr = OH_Sensor_CreateSubscriptionAttribute();
287             ret = OH_SensorSubscriptionAttribute_SetSamplingInterval(attr, SENSOR_SAMPLE_PERIOD);
288             ASSERT_EQ(ret, SENSOR_SUCCESS);
289 
290             ret = OH_Sensor_Subscribe(id, attr, g_user);
291             ASSERT_EQ(ret, SENSOR_SUCCESS);
292 
293             std::this_thread::sleep_for(std::chrono::milliseconds(SLEEP_TIME_MS));
294             ret = OH_Sensor_Unsubscribe(id, g_user);
295             ASSERT_EQ(ret, SENSOR_SUCCESS);
296             if (id != nullptr) {
297                 OH_Sensor_DestroySubscriptionId(id);
298             }
299             if (attr != nullptr) {
300                 OH_Sensor_DestroySubscriptionAttribute(attr);
301             }
302             if (g_user != nullptr) {
303                 OH_Sensor_DestroySubscriber(g_user);
304                 g_user = nullptr;
305             }
306         }
307     }
308 }
309 
310 HWTEST_F(SensorAgentTest, OH_Sensor_Subscribe_006, TestSize.Level1)
311 {
312     SEN_HILOGI("OH_Sensor_Subscribe_006 in");
313     Sensor_SubscriptionId *id = OH_Sensor_CreateSubscriptionId();
314     int32_t ret = OH_SensorSubscriptionId_SetType(id, SECOND_SENSOR_ID);
315     ASSERT_EQ(ret, SENSOR_SUCCESS);
316 
317     Sensor_SubscriptionAttribute *attr = OH_Sensor_CreateSubscriptionAttribute();
318     ret = OH_SensorSubscriptionAttribute_SetSamplingInterval(attr, SENSOR_SAMPLE_PERIOD);
319     ASSERT_EQ(ret, SENSOR_SUCCESS);
320 
321     ret = OH_Sensor_Subscribe(id, attr, nullptr);
322     ASSERT_EQ(ret, SENSOR_PARAMETER_ERROR);
323     if (id != nullptr) {
324         OH_Sensor_DestroySubscriptionId(id);
325     }
326     if (attr != nullptr) {
327         OH_Sensor_DestroySubscriptionAttribute(attr);
328     }
329 }
330 
331 HWTEST_F(SensorAgentTest, OH_Sensor_Subscribe_007, TestSize.Level1)
332 {
333     SEN_HILOGI("OH_Sensor_Subscribe_007 in");
334     g_user = OH_Sensor_CreateSubscriber();
335     int32_t ret = OH_SensorSubscriber_SetCallback(g_user, SensorDataCallbackImpl);
336     ASSERT_EQ(ret, SENSOR_SUCCESS);
337 
338     Sensor_SubscriptionAttribute *attr = OH_Sensor_CreateSubscriptionAttribute();
339     ret = OH_SensorSubscriptionAttribute_SetSamplingInterval(attr, SENSOR_SAMPLE_PERIOD);
340     ASSERT_EQ(ret, SENSOR_SUCCESS);
341 
342     ret = OH_Sensor_Subscribe(nullptr, attr, g_user);
343     ASSERT_EQ(ret, SENSOR_PARAMETER_ERROR);
344     if (attr != nullptr) {
345         OH_Sensor_DestroySubscriptionAttribute(attr);
346     }
347     if (g_user != nullptr) {
348         OH_Sensor_DestroySubscriber(g_user);
349         g_user = nullptr;
350     }
351 }
352 
353 HWTEST_F(SensorAgentTest, OH_Sensor_Subscribe_008, TestSize.Level1)
354 {
355     SEN_HILOGI("OH_Sensor_Subscribe_008 in");
356     g_user = OH_Sensor_CreateSubscriber();
357     int32_t ret = OH_SensorSubscriber_SetCallback(g_user, SensorDataCallbackImpl);
358     ASSERT_EQ(ret, SENSOR_SUCCESS);
359 
360     Sensor_SubscriptionId *id = OH_Sensor_CreateSubscriptionId();
361     ret = OH_SensorSubscriptionId_SetType(id, SECOND_SENSOR_ID);
362     ASSERT_EQ(ret, SENSOR_SUCCESS);
363 
364     ret = OH_Sensor_Subscribe(id, nullptr, g_user);
365     ASSERT_EQ(ret, SENSOR_PARAMETER_ERROR);
366     if (id != nullptr) {
367         OH_Sensor_DestroySubscriptionId(id);
368     }
369     if (g_user != nullptr) {
370         OH_Sensor_DestroySubscriber(g_user);
371         g_user = nullptr;
372     }
373 }
374 
375 HWTEST_F(SensorAgentTest, OH_Sensor_Subscribe_009, TestSize.Level1)
376 {
377     SEN_HILOGI("OH_Sensor_Subscribe_009 in");
378     uint32_t count = 0;
379     int32_t ret = OH_Sensor_GetInfos(nullptr, &count);
380     ASSERT_EQ(ret, SENSOR_SUCCESS);
381     Sensor_Info **sensors = OH_Sensor_CreateInfos(count);
382     for (uint32_t i = 0; i < count; ++i) {
383         Sensor_Type sensorType;
384         ret = OH_SensorInfo_GetType(sensors[i], &sensorType);
385         ASSERT_EQ(ret, SENSOR_SUCCESS);
386 
387         if (sensorType == THIRD_SENSOR_ID) {
388             g_user = OH_Sensor_CreateSubscriber();
389             ret = OH_SensorSubscriber_SetCallback(g_user, SensorDataCallbackImpl);
390             ASSERT_EQ(ret, SENSOR_SUCCESS);
391 
392             Sensor_SubscriptionId *id = OH_Sensor_CreateSubscriptionId();
393             ret = OH_SensorSubscriptionId_SetType(id, THIRD_SENSOR_ID);
394             ASSERT_EQ(ret, SENSOR_SUCCESS);
395 
396             Sensor_SubscriptionAttribute *attr = OH_Sensor_CreateSubscriptionAttribute();
397             ret = OH_SensorSubscriptionAttribute_SetSamplingInterval(attr, SENSOR_SAMPLE_PERIOD);
398             ASSERT_EQ(ret, SENSOR_SUCCESS);
399 
400             ret = OH_Sensor_Subscribe(id, attr, g_user);
401             ASSERT_EQ(ret, SENSOR_SUCCESS);
402 
403             std::this_thread::sleep_for(std::chrono::milliseconds(SLEEP_TIME_MS));
404             ret = OH_Sensor_Unsubscribe(id, g_user);
405             ASSERT_EQ(ret, SENSOR_SUCCESS);
406             if (id != nullptr) {
407                 OH_Sensor_DestroySubscriptionId(id);
408             }
409             if (attr != nullptr) {
410                 OH_Sensor_DestroySubscriptionAttribute(attr);
411             }
412             if (g_user != nullptr) {
413                 OH_Sensor_DestroySubscriber(g_user);
414                 g_user = nullptr;
415             }
416         }
417     }
418 }
419 
420 HWTEST_F(SensorAgentTest, OH_Sensor_Subscribe_010, TestSize.Level1)
421 {
422     SEN_HILOGI("OH_Sensor_Subscribe_010 in");
423     Sensor_SubscriptionId *id = OH_Sensor_CreateSubscriptionId();
424     int32_t ret = OH_SensorSubscriptionId_SetType(id, THIRD_SENSOR_ID);
425     ASSERT_EQ(ret, SENSOR_SUCCESS);
426 
427     Sensor_SubscriptionAttribute *attr = OH_Sensor_CreateSubscriptionAttribute();
428     ret = OH_SensorSubscriptionAttribute_SetSamplingInterval(attr, SENSOR_SAMPLE_PERIOD);
429     ASSERT_EQ(ret, SENSOR_SUCCESS);
430 
431     ret = OH_Sensor_Subscribe(id, attr, nullptr);
432     ASSERT_EQ(ret, SENSOR_PARAMETER_ERROR);
433     if (id != nullptr) {
434         OH_Sensor_DestroySubscriptionId(id);
435     }
436     if (attr != nullptr) {
437         OH_Sensor_DestroySubscriptionAttribute(attr);
438     }
439 }
440 
441 HWTEST_F(SensorAgentTest, OH_Sensor_Subscribe_011, TestSize.Level1)
442 {
443     SEN_HILOGI("OH_Sensor_Subscribe_011 in");
444     g_user = OH_Sensor_CreateSubscriber();
445     int32_t ret = OH_SensorSubscriber_SetCallback(g_user, SensorDataCallbackImpl);
446     ASSERT_EQ(ret, SENSOR_SUCCESS);
447 
448     Sensor_SubscriptionAttribute *attr = OH_Sensor_CreateSubscriptionAttribute();
449     ret = OH_SensorSubscriptionAttribute_SetSamplingInterval(attr, SENSOR_SAMPLE_PERIOD);
450     ASSERT_EQ(ret, SENSOR_SUCCESS);
451 
452     ret = OH_Sensor_Subscribe(nullptr, attr, g_user);
453     ASSERT_EQ(ret, SENSOR_PARAMETER_ERROR);
454     if (attr != nullptr) {
455         OH_Sensor_DestroySubscriptionAttribute(attr);
456     }
457     if (g_user != nullptr) {
458         OH_Sensor_DestroySubscriber(g_user);
459         g_user = nullptr;
460     }
461 }
462 
463 HWTEST_F(SensorAgentTest, OH_Sensor_Subscribe_012, TestSize.Level1)
464 {
465     SEN_HILOGI("OH_Sensor_Subscribe_012 in");
466     g_user = OH_Sensor_CreateSubscriber();
467     int32_t ret = OH_SensorSubscriber_SetCallback(g_user, SensorDataCallbackImpl);
468     ASSERT_EQ(ret, SENSOR_SUCCESS);
469 
470     Sensor_SubscriptionId *id = OH_Sensor_CreateSubscriptionId();
471     ret = OH_SensorSubscriptionId_SetType(id, THIRD_SENSOR_ID);
472     ASSERT_EQ(ret, SENSOR_SUCCESS);
473 
474     ret = OH_Sensor_Subscribe(id, nullptr, g_user);
475     ASSERT_EQ(ret, SENSOR_PARAMETER_ERROR);
476     if (id != nullptr) {
477         OH_Sensor_DestroySubscriptionId(id);
478     }
479     if (g_user != nullptr) {
480         OH_Sensor_DestroySubscriber(g_user);
481         g_user = nullptr;
482     }
483 }
484 
485 HWTEST_F(SensorAgentTest, OH_Sensor_Subscribe_013, TestSize.Level1)
486 {
487     SEN_HILOGI("OH_Sensor_Subscribe_013 enter");
488     g_user = OH_Sensor_CreateSubscriber();
489     int32_t ret = OH_SensorSubscriber_SetCallback(g_user, SensorDataCallbackImpl);
490     ASSERT_EQ(ret, SENSOR_SUCCESS);
491 
492     Sensor_SubscriptionId *id = OH_Sensor_CreateSubscriptionId();
493     ret = OH_SensorSubscriptionId_SetType(id, SECOND_SENSOR_ID);
494     ASSERT_EQ(ret, SENSOR_SUCCESS);
495 
496     ret = OH_Sensor_Subscribe(id, nullptr, g_user);
497     ASSERT_EQ(ret, SENSOR_PARAMETER_ERROR);
498     if (id != nullptr) {
499         OH_Sensor_DestroySubscriptionId(id);
500     }
501     if (g_user != nullptr) {
502         OH_Sensor_DestroySubscriber(g_user);
503         g_user = nullptr;
504     }
505 
506     g_user2 = OH_Sensor_CreateSubscriber();
507     ret = OH_SensorSubscriber_SetCallback(g_user2, SensorDataCallbackImpl);
508     ASSERT_EQ(ret, SENSOR_SUCCESS);
509 
510     id = OH_Sensor_CreateSubscriptionId();
511     ret = OH_SensorSubscriptionId_SetType(id, SECOND_SENSOR_ID);
512     ASSERT_EQ(ret, SENSOR_SUCCESS);
513 
514     ret = OH_Sensor_Subscribe(id, nullptr, g_user2);
515     ASSERT_EQ(ret, SENSOR_PARAMETER_ERROR);
516     if (id != nullptr) {
517         OH_Sensor_DestroySubscriptionId(id);
518     }
519     if (g_user2 != nullptr) {
520         OH_Sensor_DestroySubscriber(g_user2);
521         g_user2 = nullptr;
522     }
523 }
524 
525 HWTEST_F(SensorAgentTest, OH_Sensor_Subscribe_014, TestSize.Level1)
526 {
527     SEN_HILOGI("OH_Sensor_Subscribe_014 enter");
528     g_user = OH_Sensor_CreateSubscriber();
529     int32_t ret = OH_SensorSubscriber_SetCallback(g_user, SensorDataCallbackImpl);
530     ASSERT_EQ(ret, SENSOR_SUCCESS);
531 
532     Sensor_SubscriptionId *id = OH_Sensor_CreateSubscriptionId();
533     ret = OH_SensorSubscriptionId_SetType(id, THIRD_SENSOR_ID);
534     ASSERT_EQ(ret, SENSOR_SUCCESS);
535 
536     ret = OH_Sensor_Subscribe(id, nullptr, g_user);
537     ASSERT_EQ(ret, SENSOR_PARAMETER_ERROR);
538     if (id != nullptr) {
539         OH_Sensor_DestroySubscriptionId(id);
540     }
541     if (g_user != nullptr) {
542         OH_Sensor_DestroySubscriber(g_user);
543         g_user = nullptr;
544     }
545 
546     g_user2 = OH_Sensor_CreateSubscriber();
547     ret = OH_SensorSubscriber_SetCallback(g_user2, SensorDataCallbackImpl);
548     ASSERT_EQ(ret, SENSOR_SUCCESS);
549 
550     id = OH_Sensor_CreateSubscriptionId();
551     ret = OH_SensorSubscriptionId_SetType(id, THIRD_SENSOR_ID);
552     ASSERT_EQ(ret, SENSOR_SUCCESS);
553 
554     ret = OH_Sensor_Subscribe(id, nullptr, g_user2);
555     ASSERT_EQ(ret, SENSOR_PARAMETER_ERROR);
556     if (id != nullptr) {
557         OH_Sensor_DestroySubscriptionId(id);
558     }
559     if (g_user2 != nullptr) {
560         OH_Sensor_DestroySubscriber(g_user2);
561         g_user2 = nullptr;
562     }
563 }
564 
565 HWTEST_F(SensorAgentTest, OH_Sensor_Unsubscribe_001, TestSize.Level1)
566 {
567     SEN_HILOGI("OH_Sensor_Unsubscribe_001 in");
568     g_user = OH_Sensor_CreateSubscriber();
569     int32_t ret = OH_SensorSubscriber_SetCallback(g_user, SensorDataCallbackImpl);
570     ASSERT_EQ(ret, SENSOR_SUCCESS);
571 
572     ret = OH_Sensor_Unsubscribe(nullptr, g_user);
573     ASSERT_EQ(ret, SENSOR_PARAMETER_ERROR);
574     if (g_user != nullptr) {
575         OH_Sensor_DestroySubscriber(g_user);
576         g_user = nullptr;
577     }
578 }
579 
580 HWTEST_F(SensorAgentTest, OH_Sensor_Unsubscribe_002, TestSize.Level1)
581 {
582     SEN_HILOGI("OH_Sensor_Unsubscribe_002 in");
583     Sensor_SubscriptionId *id = OH_Sensor_CreateSubscriptionId();
584     int32_t ret = OH_SensorSubscriptionId_SetType(id, SENSOR_ID);
585     ASSERT_EQ(ret, SENSOR_SUCCESS);
586 
587     ret = OH_Sensor_Unsubscribe(id, nullptr);
588     ASSERT_EQ(ret, SENSOR_PARAMETER_ERROR);
589     if (id != nullptr) {
590         OH_Sensor_DestroySubscriptionId(id);
591     }
592 }
593 
594 HWTEST_F(SensorAgentTest, OH_Sensor_Unsubscribe_003, TestSize.Level1)
595 {
596     SEN_HILOGI("OH_Sensor_Unsubscribe_003 in");
597     g_user = OH_Sensor_CreateSubscriber();
598     int32_t ret = OH_SensorSubscriber_SetCallback(g_user, SensorDataCallbackImpl);
599     ASSERT_EQ(ret, SENSOR_SUCCESS);
600 
601     ret = OH_Sensor_Unsubscribe(nullptr, g_user);
602     ASSERT_EQ(ret, SENSOR_PARAMETER_ERROR);
603     if (g_user != nullptr) {
604         OH_Sensor_DestroySubscriber(g_user);
605         g_user = nullptr;
606     }
607 }
608 
609 HWTEST_F(SensorAgentTest, OH_Sensor_Unsubscribe_004, TestSize.Level1)
610 {
611     SEN_HILOGI("OH_Sensor_Unsubscribe_004 in");
612     Sensor_SubscriptionId *id = OH_Sensor_CreateSubscriptionId();
613     int32_t ret = OH_SensorSubscriptionId_SetType(id, SECOND_SENSOR_ID);
614     ASSERT_EQ(ret, SENSOR_SUCCESS);
615 
616     ret = OH_Sensor_Unsubscribe(id, nullptr);
617     ASSERT_EQ(ret, SENSOR_PARAMETER_ERROR);
618     if (id != nullptr) {
619         OH_Sensor_DestroySubscriptionId(id);
620     }
621 }
622 
623 HWTEST_F(SensorAgentTest, OH_Sensor_Unsubscribe_005, TestSize.Level1)
624 {
625     SEN_HILOGI("OH_Sensor_Unsubscribe_005 in");
626     g_user = OH_Sensor_CreateSubscriber();
627     int32_t ret = OH_SensorSubscriber_SetCallback(g_user, SensorDataCallbackImpl);
628     ASSERT_EQ(ret, SENSOR_SUCCESS);
629 
630     ret = OH_Sensor_Unsubscribe(nullptr, g_user);
631     ASSERT_EQ(ret, SENSOR_PARAMETER_ERROR);
632     if (g_user != nullptr) {
633         OH_Sensor_DestroySubscriber(g_user);
634         g_user = nullptr;
635     }
636 }
637 
638 HWTEST_F(SensorAgentTest, OH_Sensor_Unsubscribe_006, TestSize.Level1)
639 {
640     SEN_HILOGI("OH_Sensor_Unsubscribe_006 in");
641     Sensor_SubscriptionId *id = OH_Sensor_CreateSubscriptionId();
642     int32_t ret = OH_SensorSubscriptionId_SetType(id, THIRD_SENSOR_ID);
643     ASSERT_EQ(ret, SENSOR_SUCCESS);
644 
645     ret = OH_Sensor_Unsubscribe(id, nullptr);
646     ASSERT_EQ(ret, SENSOR_PARAMETER_ERROR);
647     if (id != nullptr) {
648         OH_Sensor_DestroySubscriptionId(id);
649     }
650 }
651 
652 HWTEST_F(SensorAgentTest, OH_SensorSubscriptionId_SetType_001, TestSize.Level1)
653 {
654     SEN_HILOGI("OH_SensorSubscriptionId_SetType_001 in");
655     int32_t ret = OH_SensorSubscriptionId_SetType(nullptr, SENSOR_ID);
656     ASSERT_EQ(ret, SENSOR_PARAMETER_ERROR);
657 }
658 
659 HWTEST_F(SensorAgentTest, OH_SensorSubscriptionId_SetType_002, TestSize.Level1)
660 {
661     SEN_HILOGI("OH_SensorSubscriptionId_SetType_002 in");
662     int32_t ret = OH_SensorSubscriptionId_SetType(nullptr, SECOND_SENSOR_ID);
663     ASSERT_EQ(ret, SENSOR_PARAMETER_ERROR);
664 }
665 
666 HWTEST_F(SensorAgentTest, OH_SensorSubscriptionId_SetType_003, TestSize.Level1)
667 {
668     SEN_HILOGI("OH_SensorSubscriptionId_SetType_003 in");
669     int32_t ret = OH_SensorSubscriptionId_SetType(nullptr, THIRD_SENSOR_ID);
670     ASSERT_EQ(ret, SENSOR_PARAMETER_ERROR);
671 }
672 
673 HWTEST_F(SensorAgentTest, OH_SensorSubscriptionId_GetType_001, TestSize.Level1)
674 {
675     SEN_HILOGI("OH_SensorSubscriptionId_GetType_001 in");
676     Sensor_Type type;
677     int32_t ret = OH_SensorSubscriptionId_GetType(nullptr, &type);
678     ASSERT_EQ(ret, SENSOR_PARAMETER_ERROR);
679 }
680 
681 HWTEST_F(SensorAgentTest, OH_SensorSubscriptionId_GetType_002, TestSize.Level1)
682 {
683     SEN_HILOGI("OH_SensorSubscriptionId_GetType_002 in");
684     Sensor_SubscriptionId *id = OH_Sensor_CreateSubscriptionId();
685     int32_t ret = OH_SensorSubscriptionId_GetType(id, nullptr);
686     ASSERT_EQ(ret, SENSOR_PARAMETER_ERROR);
687     if (id != nullptr) {
688         OH_Sensor_DestroySubscriptionId(id);
689     }
690 }
691 
692 HWTEST_F(SensorAgentTest, OH_SensorSubscriptionAttribute_SetSamplingInterval_001,
693     TestSize.Level1)
694 {
695     SEN_HILOGI("OH_SensorSubscriptionAttribute_SetSamplingInterval_001 in");
696     int32_t ret = OH_SensorSubscriptionAttribute_SetSamplingInterval(nullptr,
697         SENSOR_SAMPLE_PERIOD);
698     ASSERT_EQ(ret, SENSOR_PARAMETER_ERROR);
699 }
700 
701 HWTEST_F(SensorAgentTest, OH_SensorSubscriptionAttribute_SetSamplingInterval_002,
702     TestSize.Level1)
703 {
704     SEN_HILOGI("OH_SensorSubscriptionAttribute_SetSamplingInterval_002 in");
705     Sensor_SubscriptionAttribute *attr = OH_Sensor_CreateSubscriptionAttribute();
706     int32_t ret = OH_SensorSubscriptionAttribute_SetSamplingInterval(attr, INVALID_VALUE);
707     ASSERT_EQ(ret, SENSOR_PARAMETER_ERROR);
708     if (attr != nullptr) {
709         OH_Sensor_DestroySubscriptionAttribute(attr);
710     }
711 }
712 
713 HWTEST_F(SensorAgentTest, OH_SensorSubscriptionAttribute_GetSamplingInterval_001,
714     TestSize.Level1)
715 {
716     SEN_HILOGI("OH_SensorSubscriptionAttribute_GetSamplingInterval_001 in");
717     int64_t samplingInterval = 0;
718     int32_t ret = OH_SensorSubscriptionAttribute_GetSamplingInterval(nullptr,
719         &samplingInterval);
720     ASSERT_EQ(ret, SENSOR_PARAMETER_ERROR);
721 }
722 
723 HWTEST_F(SensorAgentTest, OH_SensorSubscriptionAttribute_GetSamplingInterval_002,
724     TestSize.Level1)
725 {
726     SEN_HILOGI("OH_SensorSubscriptionAttribute_GetSamplingInterval_002 in");
727     Sensor_SubscriptionAttribute *attr = OH_Sensor_CreateSubscriptionAttribute();
728     int32_t ret = OH_SensorSubscriptionAttribute_GetSamplingInterval(attr, nullptr);
729     ASSERT_EQ(ret, SENSOR_PARAMETER_ERROR);
730     if (attr != nullptr) {
731         OH_Sensor_DestroySubscriptionAttribute(attr);
732     }
733 }
734 
735 HWTEST_F(SensorAgentTest, OH_SensorSubscriber_SetCallback_001, TestSize.Level1)
736 {
737     SEN_HILOGI("OH_SensorSubscriber_SetCallback_001 in");
738     int32_t ret = OH_SensorSubscriber_SetCallback(nullptr, SensorDataCallbackImpl);
739     ASSERT_EQ(ret, SENSOR_PARAMETER_ERROR);
740 }
741 
742 HWTEST_F(SensorAgentTest, OH_SensorSubscriber_SetCallback_002, TestSize.Level1)
743 {
744     SEN_HILOGI("OH_SensorSubscriber_SetCallback_002 in");
745     g_user = OH_Sensor_CreateSubscriber();
746     int32_t ret = OH_SensorSubscriber_SetCallback(g_user, nullptr);
747     ASSERT_EQ(ret, SENSOR_PARAMETER_ERROR);
748 }
749 
750 HWTEST_F(SensorAgentTest, OH_SensorSubscriber_GetCallback_001, TestSize.Level1)
751 {
752     SEN_HILOGI("OH_SensorSubscriber_GetCallback_001 in");
753     Sensor_EventCallback callback;
754     int32_t ret = OH_SensorSubscriber_GetCallback(nullptr, &callback);
755     ASSERT_EQ(ret, SENSOR_PARAMETER_ERROR);
756 }
757 
758 HWTEST_F(SensorAgentTest, OH_SensorSubscriber_GetCallback_002, TestSize.Level1)
759 {
760     SEN_HILOGI("OH_SensorSubscriber_GetCallback_002 in");
761     g_user = OH_Sensor_CreateSubscriber();
762     int32_t ret = OH_SensorSubscriber_GetCallback(g_user, nullptr);
763     ASSERT_EQ(ret, SENSOR_PARAMETER_ERROR);
764     if (g_user != nullptr) {
765         OH_Sensor_DestroySubscriber(g_user);
766     }
767 }
768 }  // namespace Sensors
769 }  // namespace OHOS
770