• 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 
HWTEST_F(SensorAgentTest,OH_Sensor_GetInfos_001,Level3)117 HWTEST_F(SensorAgentTest, OH_Sensor_GetInfos_001, Level3)
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 
HWTEST_F(SensorAgentTest,OH_Sensor_GetInfos_002,Level3)157 HWTEST_F(SensorAgentTest, OH_Sensor_GetInfos_002, Level3)
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 
HWTEST_F(SensorAgentTest,OH_Sensor_Subscribe_001,Level3)165 HWTEST_F(SensorAgentTest, OH_Sensor_Subscribe_001, Level3)
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 
HWTEST_F(SensorAgentTest,OH_Sensor_Subscribe_002,Level3)200 HWTEST_F(SensorAgentTest, OH_Sensor_Subscribe_002, Level3)
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 
HWTEST_F(SensorAgentTest,OH_Sensor_Subscribe_003,Level3)221 HWTEST_F(SensorAgentTest, OH_Sensor_Subscribe_003, Level3)
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 
HWTEST_F(SensorAgentTest,OH_Sensor_Subscribe_004,Level3)243 HWTEST_F(SensorAgentTest, OH_Sensor_Subscribe_004, Level3)
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 
HWTEST_F(SensorAgentTest,OH_Sensor_Subscribe_005,Level3)265 HWTEST_F(SensorAgentTest, OH_Sensor_Subscribe_005, Level3)
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 
HWTEST_F(SensorAgentTest,OH_Sensor_Subscribe_006,Level3)310 HWTEST_F(SensorAgentTest, OH_Sensor_Subscribe_006, Level3)
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 
HWTEST_F(SensorAgentTest,OH_Sensor_Subscribe_007,Level3)331 HWTEST_F(SensorAgentTest, OH_Sensor_Subscribe_007, Level3)
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 
HWTEST_F(SensorAgentTest,OH_Sensor_Subscribe_008,Level3)353 HWTEST_F(SensorAgentTest, OH_Sensor_Subscribe_008, Level3)
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 
HWTEST_F(SensorAgentTest,OH_Sensor_Subscribe_009,Level3)375 HWTEST_F(SensorAgentTest, OH_Sensor_Subscribe_009, Level3)
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 
HWTEST_F(SensorAgentTest,OH_Sensor_Subscribe_010,Level3)420 HWTEST_F(SensorAgentTest, OH_Sensor_Subscribe_010, Level3)
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 
HWTEST_F(SensorAgentTest,OH_Sensor_Subscribe_011,Level3)441 HWTEST_F(SensorAgentTest, OH_Sensor_Subscribe_011, Level3)
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 
HWTEST_F(SensorAgentTest,OH_Sensor_Subscribe_012,Level3)463 HWTEST_F(SensorAgentTest, OH_Sensor_Subscribe_012, Level3)
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 
HWTEST_F(SensorAgentTest,OH_Sensor_Subscribe_013,Level3)485 HWTEST_F(SensorAgentTest, OH_Sensor_Subscribe_013, Level3)
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 
HWTEST_F(SensorAgentTest,OH_Sensor_Subscribe_014,Level3)525 HWTEST_F(SensorAgentTest, OH_Sensor_Subscribe_014, Level3)
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 
HWTEST_F(SensorAgentTest,OH_Sensor_Unsubscribe_001,Level3)565 HWTEST_F(SensorAgentTest, OH_Sensor_Unsubscribe_001, Level3)
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 
HWTEST_F(SensorAgentTest,OH_Sensor_Unsubscribe_002,Level3)580 HWTEST_F(SensorAgentTest, OH_Sensor_Unsubscribe_002, Level3)
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 
HWTEST_F(SensorAgentTest,OH_Sensor_Unsubscribe_003,Level3)594 HWTEST_F(SensorAgentTest, OH_Sensor_Unsubscribe_003, Level3)
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 
HWTEST_F(SensorAgentTest,OH_Sensor_Unsubscribe_004,Level3)609 HWTEST_F(SensorAgentTest, OH_Sensor_Unsubscribe_004, Level3)
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 
HWTEST_F(SensorAgentTest,OH_Sensor_Unsubscribe_005,Level3)623 HWTEST_F(SensorAgentTest, OH_Sensor_Unsubscribe_005, Level3)
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 
HWTEST_F(SensorAgentTest,OH_Sensor_Unsubscribe_006,Level3)638 HWTEST_F(SensorAgentTest, OH_Sensor_Unsubscribe_006, Level3)
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 
HWTEST_F(SensorAgentTest,OH_SensorSubscriptionId_SetType_001,Level3)652 HWTEST_F(SensorAgentTest, OH_SensorSubscriptionId_SetType_001, Level3)
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 
HWTEST_F(SensorAgentTest,OH_SensorSubscriptionId_SetType_002,Level3)659 HWTEST_F(SensorAgentTest, OH_SensorSubscriptionId_SetType_002, Level3)
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 
HWTEST_F(SensorAgentTest,OH_SensorSubscriptionId_SetType_003,Level3)666 HWTEST_F(SensorAgentTest, OH_SensorSubscriptionId_SetType_003, Level3)
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 
HWTEST_F(SensorAgentTest,OH_SensorSubscriptionId_GetType_001,Level3)673 HWTEST_F(SensorAgentTest, OH_SensorSubscriptionId_GetType_001, Level3)
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 
HWTEST_F(SensorAgentTest,OH_SensorSubscriptionId_GetType_002,Level3)681 HWTEST_F(SensorAgentTest, OH_SensorSubscriptionId_GetType_002, Level3)
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 
HWTEST_F(SensorAgentTest,OH_SensorSubscriptionAttribute_SetSamplingInterval_001,Level3)692 HWTEST_F(SensorAgentTest, OH_SensorSubscriptionAttribute_SetSamplingInterval_001, Level3)
693 {
694     SEN_HILOGI("OH_SensorSubscriptionAttribute_SetSamplingInterval_001 in");
695     int32_t ret = OH_SensorSubscriptionAttribute_SetSamplingInterval(nullptr,
696         SENSOR_SAMPLE_PERIOD);
697     ASSERT_EQ(ret, SENSOR_PARAMETER_ERROR);
698 }
699 
HWTEST_F(SensorAgentTest,OH_SensorSubscriptionAttribute_SetSamplingInterval_002,Level3)700 HWTEST_F(SensorAgentTest, OH_SensorSubscriptionAttribute_SetSamplingInterval_002, Level3)
701 {
702     SEN_HILOGI("OH_SensorSubscriptionAttribute_SetSamplingInterval_002 in");
703     Sensor_SubscriptionAttribute *attr = OH_Sensor_CreateSubscriptionAttribute();
704     int32_t ret = OH_SensorSubscriptionAttribute_SetSamplingInterval(attr, INVALID_VALUE);
705     ASSERT_EQ(ret, SENSOR_PARAMETER_ERROR);
706     if (attr != nullptr) {
707         OH_Sensor_DestroySubscriptionAttribute(attr);
708     }
709 }
710 
HWTEST_F(SensorAgentTest,OH_SensorSubscriptionAttribute_GetSamplingInterval_001,Level3)711 HWTEST_F(SensorAgentTest, OH_SensorSubscriptionAttribute_GetSamplingInterval_001, Level3)
712 {
713     SEN_HILOGI("OH_SensorSubscriptionAttribute_GetSamplingInterval_001 in");
714     int64_t samplingInterval = 0;
715     int32_t ret = OH_SensorSubscriptionAttribute_GetSamplingInterval(nullptr,
716         &samplingInterval);
717     ASSERT_EQ(ret, SENSOR_PARAMETER_ERROR);
718 }
719 
HWTEST_F(SensorAgentTest,OH_SensorSubscriptionAttribute_GetSamplingInterval_002,Level3)720 HWTEST_F(SensorAgentTest, OH_SensorSubscriptionAttribute_GetSamplingInterval_002, Level3)
721 {
722     SEN_HILOGI("OH_SensorSubscriptionAttribute_GetSamplingInterval_002 in");
723     Sensor_SubscriptionAttribute *attr = OH_Sensor_CreateSubscriptionAttribute();
724     int32_t ret = OH_SensorSubscriptionAttribute_GetSamplingInterval(attr, nullptr);
725     ASSERT_EQ(ret, SENSOR_PARAMETER_ERROR);
726     if (attr != nullptr) {
727         OH_Sensor_DestroySubscriptionAttribute(attr);
728     }
729 }
730 
HWTEST_F(SensorAgentTest,OH_SensorSubscriber_SetCallback_001,Level3)731 HWTEST_F(SensorAgentTest, OH_SensorSubscriber_SetCallback_001, Level3)
732 {
733     SEN_HILOGI("OH_SensorSubscriber_SetCallback_001 in");
734     int32_t ret = OH_SensorSubscriber_SetCallback(nullptr, SensorDataCallbackImpl);
735     ASSERT_EQ(ret, SENSOR_PARAMETER_ERROR);
736 }
737 
HWTEST_F(SensorAgentTest,OH_SensorSubscriber_SetCallback_002,Level3)738 HWTEST_F(SensorAgentTest, OH_SensorSubscriber_SetCallback_002, Level3)
739 {
740     SEN_HILOGI("OH_SensorSubscriber_SetCallback_002 in");
741     g_user = OH_Sensor_CreateSubscriber();
742     int32_t ret = OH_SensorSubscriber_SetCallback(g_user, nullptr);
743     ASSERT_EQ(ret, SENSOR_PARAMETER_ERROR);
744 }
745 
HWTEST_F(SensorAgentTest,OH_SensorSubscriber_GetCallback_001,Level3)746 HWTEST_F(SensorAgentTest, OH_SensorSubscriber_GetCallback_001, Level3)
747 {
748     SEN_HILOGI("OH_SensorSubscriber_GetCallback_001 in");
749     Sensor_EventCallback callback;
750     int32_t ret = OH_SensorSubscriber_GetCallback(nullptr, &callback);
751     ASSERT_EQ(ret, SENSOR_PARAMETER_ERROR);
752 }
753 
HWTEST_F(SensorAgentTest,OH_SensorSubscriber_GetCallback_002,Level3)754 HWTEST_F(SensorAgentTest, OH_SensorSubscriber_GetCallback_002, Level3)
755 {
756     SEN_HILOGI("OH_SensorSubscriber_GetCallback_002 in");
757     g_user = OH_Sensor_CreateSubscriber();
758     int32_t ret = OH_SensorSubscriber_GetCallback(g_user, nullptr);
759     ASSERT_EQ(ret, SENSOR_PARAMETER_ERROR);
760     if (g_user != nullptr) {
761         OH_Sensor_DestroySubscriber(g_user);
762     }
763 }
764 }  // namespace Sensors
765 }  // namespace OHOS
766