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 uint32_t SENSOR_NAME_LENGTH_MAX = 64;
39 constexpr int64_t SENSOR_SAMPLE_PERIOD = 200000000;
40 constexpr int32_t SLEEP_TIME_MS = 1000;
41 constexpr int64_t INVALID_VALUE = -1;
42 constexpr float INVALID_RESOLUTION = -1.0F;
43 Sensor_Subscriber *g_user = nullptr;
44 std::atomic_bool g_existAmbientLight = false;
45 } // namespace
46
47 class SensorAgentTest : public testing::Test {
48 public:
49 static void SetUpTestCase();
50 static void TearDownTestCase();
51 void SetUp();
52 void TearDown();
53 };
54
SetUpTestCase()55 void SensorAgentTest::SetUpTestCase()
56 {
57 SEN_HILOGI("SensorAgentTest SetUpTestCase in");
58 uint32_t count = 0;
59 int32_t ret = OH_Sensor_GetInfos(nullptr, &count);
60 ASSERT_EQ(ret, SENSOR_SUCCESS);
61 Sensor_Info **sensors = OH_Sensor_CreateInfos(count);
62 ASSERT_NE(sensors, nullptr);
63 ret = OH_Sensor_GetInfos(sensors, &count);
64 ASSERT_EQ(ret, SENSOR_SUCCESS);
65 for (uint32_t i = 0; i < count; ++i) {
66 Sensor_Type sensorType;
67 ret = OH_SensorInfo_GetType(sensors[i], &sensorType);
68 ASSERT_EQ(ret, SENSOR_SUCCESS);
69 if (sensorType == SENSOR_TYPE_AMBIENT_LIGHT) {
70 g_existAmbientLight = true;
71 SEN_HILOGI("SensorAgentTest SetUpTestCase ,Exist AmbientLight sensor");
72 break;
73 }
74 }
75 if (!g_existAmbientLight) {
76 SEN_HILOGI("SensorAgentTest SetUpTestCase ,Not exist AmbientLight sensor");
77 }
78 ret = OH_Sensor_DestroyInfos(sensors, count);
79 ASSERT_EQ(ret, SENSOR_SUCCESS);
80 SEN_HILOGI("SensorAgentTest SetUpTestCase end");
81 }
82
TearDownTestCase()83 void SensorAgentTest::TearDownTestCase() {}
84
SetUp()85 void SensorAgentTest::SetUp() {}
86
TearDown()87 void SensorAgentTest::TearDown() {}
88
SensorDataCallbackImpl(Sensor_Event * event)89 void SensorDataCallbackImpl(Sensor_Event *event)
90 {
91 if (event == nullptr) {
92 SEN_HILOGE("event is null");
93 return;
94 }
95 int64_t timestamp = INVALID_VALUE;
96 int32_t ret = OH_SensorEvent_GetTimestamp(event, ×tamp);
97 ASSERT_EQ(ret, SENSOR_SUCCESS);
98 Sensor_Type sensorType;
99 ret = OH_SensorEvent_GetType(event, &sensorType);
100 ASSERT_EQ(ret, SENSOR_SUCCESS);
101 Sensor_Accuracy accuracy = SENSOR_ACCURACY_UNRELIABLE;
102 ret = OH_SensorEvent_GetAccuracy(event, &accuracy);
103 ASSERT_EQ(ret, SENSOR_SUCCESS);
104 float *data = nullptr;
105 uint32_t length = 0;
106 ret = OH_SensorEvent_GetData(event, &data, &length);
107 ASSERT_EQ(ret, SENSOR_SUCCESS);
108 SEN_HILOGI("sensorType:%{public}d, dataLen:%{public}d, accuracy:%{public}d", sensorType, length, accuracy);
109 for (uint32_t i = 0; i < length; ++i) {
110 SEN_HILOGI("data[%{public}d]:%{public}f", i, data[i]);
111 }
112 }
113
114 HWTEST_F(SensorAgentTest, OH_Sensor_GetInfos_001, TestSize.Level1)
115 {
116 SEN_HILOGI("OH_Sensor_GetInfos_001 in");
117 uint32_t count = 0;
118 int32_t ret = OH_Sensor_GetInfos(nullptr, &count);
119 ASSERT_EQ(ret, SENSOR_SUCCESS);
120 Sensor_Info **sensors = OH_Sensor_CreateInfos(count);
121 ASSERT_NE(sensors, nullptr);
122 ret = OH_Sensor_GetInfos(sensors, &count);
123 ASSERT_EQ(ret, SENSOR_SUCCESS);
124 for (uint32_t i = 0; i < count; ++i) {
125 char sensorName[SENSOR_NAME_LENGTH_MAX] = {};
126 uint32_t length = SENSOR_NAME_LENGTH_MAX;
127 ret = OH_SensorInfo_GetName(sensors[i], sensorName, &length);
128 ASSERT_EQ(ret, SENSOR_SUCCESS);
129 char vendorName[SENSOR_NAME_LENGTH_MAX] = {};
130 length = SENSOR_NAME_LENGTH_MAX;
131 ret = OH_SensorInfo_GetVendorName(sensors[i], vendorName, &length);
132 ASSERT_EQ(ret, SENSOR_SUCCESS);
133 Sensor_Type sensorType;
134 ret = OH_SensorInfo_GetType(sensors[i], &sensorType);
135 ASSERT_EQ(ret, SENSOR_SUCCESS);
136 float resolution = INVALID_RESOLUTION;
137 ret = OH_SensorInfo_GetResolution(sensors[i], &resolution);
138 ASSERT_EQ(ret, SENSOR_SUCCESS);
139 int64_t minSamplePeriod = INVALID_VALUE;
140 ret = OH_SensorInfo_GetMinSamplingInterval(sensors[i], &minSamplePeriod);
141 ASSERT_EQ(ret, SENSOR_SUCCESS);
142 int64_t maxSamplePeriod = INVALID_VALUE;
143 ret = OH_SensorInfo_GetMaxSamplingInterval(sensors[i], &maxSamplePeriod);
144 ASSERT_EQ(ret, SENSOR_SUCCESS);
145 SEN_HILOGI("sensorType:%{public}d, sensorName:%{public}s, vendorName:%{public}s,"
146 "resolution:%{public}f, minSamplePeriod:%{public}" PRId64 "maxSamplePeriod:%{public}" PRId64,
147 static_cast<int32_t>(sensorType), sensorName, vendorName,
148 resolution, minSamplePeriod, maxSamplePeriod);
149 }
150 ret = OH_Sensor_DestroyInfos(sensors, count);
151 ASSERT_EQ(ret, SENSOR_SUCCESS);
152 }
153
154 HWTEST_F(SensorAgentTest, OH_Sensor_GetInfos_002, TestSize.Level1)
155 {
156 SEN_HILOGI("OH_Sensor_GetInfos_003 in");
157 Sensor_Info *sensors { nullptr };
158 int32_t ret = OH_Sensor_GetInfos(&sensors, nullptr);
159 ASSERT_NE(ret, SENSOR_SUCCESS);
160 }
161
162 HWTEST_F(SensorAgentTest, OH_Sensor_Subscribe_001, TestSize.Level1)
163 {
164 SEN_HILOGI("OH_Sensor_Subscribe_001 in");
165 if (g_existAmbientLight) {
166 g_user = OH_Sensor_CreateSubscriber();
167 int32_t ret = OH_SensorSubscriber_SetCallback(g_user, SensorDataCallbackImpl);
168 ASSERT_EQ(ret, SENSOR_SUCCESS);
169
170 Sensor_SubscriptionId *id = OH_Sensor_CreateSubscriptionId();
171 ret = OH_SensorSubscriptionId_SetType(id, SENSOR_ID);
172 ASSERT_EQ(ret, SENSOR_SUCCESS);
173
174 Sensor_SubscriptionAttribute *attr = OH_Sensor_CreateSubscriptionAttribute();
175 ret = OH_SensorSubscriptionAttribute_SetSamplingInterval(attr, SENSOR_SAMPLE_PERIOD);
176 ASSERT_EQ(ret, SENSOR_SUCCESS);
177
178 ret = OH_Sensor_Subscribe(id, attr, g_user);
179 ASSERT_EQ(ret, SENSOR_SUCCESS);
180
181 std::this_thread::sleep_for(std::chrono::milliseconds(SLEEP_TIME_MS));
182 ret = OH_Sensor_Unsubscribe(id, g_user);
183 ASSERT_EQ(ret, SENSOR_SUCCESS);
184 if (id != nullptr) {
185 OH_Sensor_DestroySubscriptionId(id);
186 }
187 if (attr != nullptr) {
188 OH_Sensor_DestroySubscriptionAttribute(attr);
189 }
190 if (g_user != nullptr) {
191 OH_Sensor_DestroySubscriber(g_user);
192 g_user = nullptr;
193 }
194 }
195 }
196
197 HWTEST_F(SensorAgentTest, OH_Sensor_Subscribe_002, TestSize.Level1)
198 {
199 SEN_HILOGI("OH_Sensor_Subscribe_002 in");
200 Sensor_SubscriptionId *id = OH_Sensor_CreateSubscriptionId();
201 int32_t ret = OH_SensorSubscriptionId_SetType(id, SENSOR_ID);
202 ASSERT_EQ(ret, SENSOR_SUCCESS);
203
204 Sensor_SubscriptionAttribute *attr = OH_Sensor_CreateSubscriptionAttribute();
205 ret = OH_SensorSubscriptionAttribute_SetSamplingInterval(attr, SENSOR_SAMPLE_PERIOD);
206 ASSERT_EQ(ret, SENSOR_SUCCESS);
207
208 ret = OH_Sensor_Subscribe(id, attr, nullptr);
209 ASSERT_EQ(ret, SENSOR_PARAMETER_ERROR);
210 if (id != nullptr) {
211 OH_Sensor_DestroySubscriptionId(id);
212 }
213 if (attr != nullptr) {
214 OH_Sensor_DestroySubscriptionAttribute(attr);
215 }
216 }
217
218 HWTEST_F(SensorAgentTest, OH_Sensor_Subscribe_003, TestSize.Level1)
219 {
220 SEN_HILOGI("OH_Sensor_Subscribe_003 in");
221 g_user = OH_Sensor_CreateSubscriber();
222 int32_t ret = OH_SensorSubscriber_SetCallback(g_user, SensorDataCallbackImpl);
223 ASSERT_EQ(ret, SENSOR_SUCCESS);
224
225 Sensor_SubscriptionAttribute *attr = OH_Sensor_CreateSubscriptionAttribute();
226 ret = OH_SensorSubscriptionAttribute_SetSamplingInterval(attr, SENSOR_SAMPLE_PERIOD);
227 ASSERT_EQ(ret, SENSOR_SUCCESS);
228
229 ret = OH_Sensor_Subscribe(nullptr, attr, g_user);
230 ASSERT_EQ(ret, SENSOR_PARAMETER_ERROR);
231 if (attr != nullptr) {
232 OH_Sensor_DestroySubscriptionAttribute(attr);
233 }
234 if (g_user != nullptr) {
235 OH_Sensor_DestroySubscriber(g_user);
236 g_user = nullptr;
237 }
238 }
239
240 HWTEST_F(SensorAgentTest, OH_Sensor_Subscribe_004, TestSize.Level1)
241 {
242 SEN_HILOGI("OH_Sensor_Subscribe_004 in");
243 g_user = OH_Sensor_CreateSubscriber();
244 int32_t ret = OH_SensorSubscriber_SetCallback(g_user, SensorDataCallbackImpl);
245 ASSERT_EQ(ret, SENSOR_SUCCESS);
246
247 Sensor_SubscriptionId *id = OH_Sensor_CreateSubscriptionId();
248 ret = OH_SensorSubscriptionId_SetType(id, SENSOR_ID);
249 ASSERT_EQ(ret, SENSOR_SUCCESS);
250
251 ret = OH_Sensor_Subscribe(id, nullptr, g_user);
252 ASSERT_EQ(ret, SENSOR_PARAMETER_ERROR);
253 if (id != nullptr) {
254 OH_Sensor_DestroySubscriptionId(id);
255 }
256 if (g_user != nullptr) {
257 OH_Sensor_DestroySubscriber(g_user);
258 g_user = nullptr;
259 }
260 }
261
262 HWTEST_F(SensorAgentTest, OH_Sensor_Unsubscribe_001, TestSize.Level1)
263 {
264 SEN_HILOGI("OH_Sensor_Unsubscribe_001 in");
265 g_user = OH_Sensor_CreateSubscriber();
266 int32_t ret = OH_SensorSubscriber_SetCallback(g_user, SensorDataCallbackImpl);
267 ASSERT_EQ(ret, SENSOR_SUCCESS);
268
269 ret = OH_Sensor_Unsubscribe(nullptr, g_user);
270 ASSERT_EQ(ret, SENSOR_PARAMETER_ERROR);
271 if (g_user != nullptr) {
272 OH_Sensor_DestroySubscriber(g_user);
273 g_user = nullptr;
274 }
275 }
276
277 HWTEST_F(SensorAgentTest, OH_Sensor_Unsubscribe_002, TestSize.Level1)
278 {
279 SEN_HILOGI("OH_Sensor_Unsubscribe_002 in");
280 Sensor_SubscriptionId *id = OH_Sensor_CreateSubscriptionId();
281 int32_t ret = OH_SensorSubscriptionId_SetType(id, SENSOR_ID);
282 ASSERT_EQ(ret, SENSOR_SUCCESS);
283
284 ret = OH_Sensor_Unsubscribe(id, nullptr);
285 ASSERT_EQ(ret, SENSOR_PARAMETER_ERROR);
286 if (id != nullptr) {
287 OH_Sensor_DestroySubscriptionId(id);
288 }
289 }
290
291 HWTEST_F(SensorAgentTest, OH_SensorSubscriptionId_SetType_001, TestSize.Level1)
292 {
293 SEN_HILOGI("OH_SensorSubscriptionId_SetType_001 in");
294 int32_t ret = OH_SensorSubscriptionId_SetType(nullptr, SENSOR_ID);
295 ASSERT_EQ(ret, SENSOR_PARAMETER_ERROR);
296 }
297
298 HWTEST_F(SensorAgentTest, OH_SensorSubscriptionId_GetType_001, TestSize.Level1)
299 {
300 SEN_HILOGI("OH_SensorSubscriptionId_GetType_001 in");
301 Sensor_Type type;
302 int32_t ret = OH_SensorSubscriptionId_GetType(nullptr, &type);
303 ASSERT_EQ(ret, SENSOR_PARAMETER_ERROR);
304 }
305
306 HWTEST_F(SensorAgentTest, OH_SensorSubscriptionId_GetType_002, TestSize.Level1)
307 {
308 SEN_HILOGI("OH_SensorSubscriptionId_GetType_002 in");
309 Sensor_SubscriptionId *id = OH_Sensor_CreateSubscriptionId();
310 int32_t ret = OH_SensorSubscriptionId_GetType(id, nullptr);
311 ASSERT_EQ(ret, SENSOR_PARAMETER_ERROR);
312 if (id != nullptr) {
313 OH_Sensor_DestroySubscriptionId(id);
314 }
315 }
316
317 HWTEST_F(SensorAgentTest, OH_SensorSubscriptionAttribute_SetSamplingInterval_001,
318 TestSize.Level1)
319 {
320 SEN_HILOGI("OH_SensorSubscriptionAttribute_SetSamplingInterval_001 in");
321 int32_t ret = OH_SensorSubscriptionAttribute_SetSamplingInterval(nullptr,
322 SENSOR_SAMPLE_PERIOD);
323 ASSERT_EQ(ret, SENSOR_PARAMETER_ERROR);
324 }
325
326 HWTEST_F(SensorAgentTest, OH_SensorSubscriptionAttribute_SetSamplingInterval_002,
327 TestSize.Level1)
328 {
329 SEN_HILOGI("OH_SensorSubscriptionAttribute_SetSamplingInterval_002 in");
330 Sensor_SubscriptionAttribute *attr = OH_Sensor_CreateSubscriptionAttribute();
331 int32_t ret = OH_SensorSubscriptionAttribute_SetSamplingInterval(attr, INVALID_VALUE);
332 ASSERT_EQ(ret, SENSOR_PARAMETER_ERROR);
333 if (attr != nullptr) {
334 OH_Sensor_DestroySubscriptionAttribute(attr);
335 }
336 }
337
338 HWTEST_F(SensorAgentTest, OH_SensorSubscriptionAttribute_GetSamplingInterval_001,
339 TestSize.Level1)
340 {
341 SEN_HILOGI("OH_SensorSubscriptionAttribute_GetSamplingInterval_001 in");
342 int64_t samplingInterval = 0;
343 int32_t ret = OH_SensorSubscriptionAttribute_GetSamplingInterval(nullptr,
344 &samplingInterval);
345 ASSERT_EQ(ret, SENSOR_PARAMETER_ERROR);
346 }
347
348 HWTEST_F(SensorAgentTest, OH_SensorSubscriptionAttribute_GetSamplingInterval_002,
349 TestSize.Level1)
350 {
351 SEN_HILOGI("OH_SensorSubscriptionAttribute_GetSamplingInterval_002 in");
352 Sensor_SubscriptionAttribute *attr = OH_Sensor_CreateSubscriptionAttribute();
353 int32_t ret = OH_SensorSubscriptionAttribute_GetSamplingInterval(attr, nullptr);
354 ASSERT_EQ(ret, SENSOR_PARAMETER_ERROR);
355 if (attr != nullptr) {
356 OH_Sensor_DestroySubscriptionAttribute(attr);
357 }
358 }
359
360 HWTEST_F(SensorAgentTest, OH_SensorSubscriber_SetCallback_001, TestSize.Level1)
361 {
362 SEN_HILOGI("OH_SensorSubscriber_SetCallback_001 in");
363 int32_t ret = OH_SensorSubscriber_SetCallback(nullptr, SensorDataCallbackImpl);
364 ASSERT_EQ(ret, SENSOR_PARAMETER_ERROR);
365 }
366
367 HWTEST_F(SensorAgentTest, OH_SensorSubscriber_SetCallback_002, TestSize.Level1)
368 {
369 SEN_HILOGI("OH_SensorSubscriber_SetCallback_002 in");
370 g_user = OH_Sensor_CreateSubscriber();
371 int32_t ret = OH_SensorSubscriber_SetCallback(g_user, nullptr);
372 ASSERT_EQ(ret, SENSOR_PARAMETER_ERROR);
373 }
374
375 HWTEST_F(SensorAgentTest, OH_SensorSubscriber_GetCallback_001, TestSize.Level1)
376 {
377 SEN_HILOGI("OH_SensorSubscriber_GetCallback_001 in");
378 Sensor_EventCallback callback;
379 int32_t ret = OH_SensorSubscriber_GetCallback(nullptr, &callback);
380 ASSERT_EQ(ret, SENSOR_PARAMETER_ERROR);
381 }
382
383 HWTEST_F(SensorAgentTest, OH_SensorSubscriber_GetCallback_002, TestSize.Level1)
384 {
385 SEN_HILOGI("OH_SensorSubscriber_GetCallback_002 in");
386 g_user = OH_Sensor_CreateSubscriber();
387 int32_t ret = OH_SensorSubscriber_GetCallback(g_user, nullptr);
388 ASSERT_EQ(ret, SENSOR_PARAMETER_ERROR);
389 if (g_user != nullptr) {
390 OH_Sensor_DestroySubscriber(g_user);
391 }
392 }
393 } // namespace Sensors
394 } // namespace OHOS
395