• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021 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 "gtest/gtest.h"
17 #include "hilog/log.h"
18 #include "sensor_agent.h"
19 #include "sensor_agent_type.h"
20 
21 #undef LOG_TAG
22 #define LOG_TAG "SENSOR_LITE"
23 #undef SENSOR_ERROR_INVALID_PARAM
24 #define SENSOR_ERROR_INVALID_PARAM (-3)
25 
26 using namespace std;
27 using namespace testing::ext;
28 
29 #define NULLPTR ((void *)0)
30 
31 namespace OHOS {
32 class SensorAgentInterfacesTest : public testing::Test {
33 protected:
SetUpTestCase(void)34     static void SetUpTestCase(void) {}
TearDownTestCase(void)35     static void TearDownTestCase(void) {}
36 };
37 
SensorDataCallbackImpl(SensorEvent * event)38 void SensorDataCallbackImpl(SensorEvent *event)
39 {
40     HILOG_DEBUG(HILOG_MODULE_APP, "SensorDataCallbackImpl called");
41     if (event == NULLPTR) {
42         return;
43     }
44     float *sensorData = (float *)event->data;
45     for (int32_t i = 0; i < (int32_t)(event->dataLen / sizeof(uint8_t *)); i++) {
46         HILOG_DEBUG(HILOG_MODULE_APP, "SensorDataCallbackImpl data: %f", *(sensorData + i));
47     }
48 }
49 
50 /**
51  * @tc.name: GetAllSensorsInterfaceTest001
52  * @tc.desc: Test the interface for obtaining the sensor list in normal scenarios.
53  * @tc.type: FUNC
54  * @tc.require: AR000F46AD
55  */
56 HWTEST_F(SensorAgentInterfacesTest, GetAllSensorsInterfaceTest001, TestSize.Level1)
57 {
58     HILOG_INFO(HILOG_MODULE_APP, "GetAllSensorsInterfaceTest001 called");
59     SensorInfo *sensorInfo = (SensorInfo *)NULLPTR;
60     int32_t count = 0;
61     int32_t ret = GetAllSensors(&sensorInfo, &count);
62     HILOG_INFO(HILOG_MODULE_APP, "GetAllSensorsInterfaceTest001 sensor lists count: %d", count);
63     for (int i = 0; i < count; i++) {
64         HILOG_INFO(HILOG_MODULE_APP, "%s %s %s %s %d %d %f %f %f!",
65             (sensorInfo + i)->sensorName, (sensorInfo + i)->vendorName,
66             (sensorInfo + i)->firmwareVersion, (sensorInfo + i)->hardwareVersion,
67             (sensorInfo + i)->sensorTypeId, (sensorInfo + i)->sensorId,
68             (sensorInfo + i)->maxRange, (sensorInfo + i)->precision,
69             (sensorInfo + i)->power);
70     }
71     EXPECT_EQ(ret, 0) << "GetAllSensors ret = " << ret << endl;
72 };
73 
74 /**
75  * @tc.name: GetAllSensorsInterfaceTest002
76  * @tc.desc: Test the interface for obtaining the sensor list in abnormal scenarios.
77  * @tc.type: FUNC
78  * @tc.require: AR000F46AH
79  */
80 HWTEST_F(SensorAgentInterfacesTest, GetAllSensorsInterfaceTest002, TestSize.Level1)
81 {
82     HILOG_INFO(HILOG_MODULE_APP, "GetAllSensorsInterfaceTest002 called");
83     SensorInfo **sensorInfo = (SensorInfo **)NULLPTR;
84     int32_t ret = GetAllSensors(sensorInfo, (int32_t *)NULLPTR);
85     EXPECT_EQ(ret, SENSOR_ERROR_INVALID_PARAM) << "GetAllSensors ret = " << ret << endl;
86 };
87 
88 /**
89  * @tc.name: GetAllSensorsInterfaceTest003
90  * @tc.desc: Test the interface for obtaining the sensor list in abnormal scenarios.
91  * @tc.type: FUNC
92  * @tc.require: AR000F46AH
93  */
94 HWTEST_F(SensorAgentInterfacesTest, GetAllSensorsInterfaceTest003, TestSize.Level1)
95 {
96     HILOG_INFO(HILOG_MODULE_APP, "GetAllSensorsInterfaceTest003 called");
97     int32_t count = 0;
98     SensorInfo **sensorInfo = (SensorInfo **)NULLPTR;
99     int32_t ret = GetAllSensors(sensorInfo, &count);
100     EXPECT_EQ(ret, SENSOR_ERROR_INVALID_PARAM) << "GetAllSensors ret = " << ret << endl;
101 };
102 
103 /**
104  * @tc.name: GetAllSensorsInterfaceTest004
105  * @tc.desc: Test the interface for obtaining the sensor list in abnormal scenarios.
106  * @tc.type: FUNC
107  * @tc.require: AR000F46AH
108  */
109 HWTEST_F(SensorAgentInterfacesTest, GetAllSensorsInterfaceTest004, TestSize.Level1)
110 {
111     HILOG_INFO(HILOG_MODULE_APP, "GetAllSensorsInterfaceTest004 called");
112     SensorInfo *sensorInfo = (SensorInfo *)NULLPTR;
113     int32_t ret = GetAllSensors(&sensorInfo, (int32_t *)NULLPTR);
114     EXPECT_EQ(ret, SENSOR_ERROR_INVALID_PARAM) << "GetAllSensors ret = " << ret << endl;
115 };
116 
117 /**
118  * @tc.name: ActivateSensorInterfaceTest001
119  * @tc.desc: Test the enable sensor interface, the input parameter sensorid is set to -1.
120  * @tc.type: FUNC
121  * @tc.require: AR000F46AI
122  */
123 HWTEST_F(SensorAgentInterfacesTest, ActivateSensorInterfaceTest001, TestSize.Level1)
124 {
125     HILOG_INFO(HILOG_MODULE_APP, "ActivateSensorInterfaceTest001 called");
126     SensorUser sensorUser;
127     int32_t ret = ActivateSensor(-1, &sensorUser);
128     EXPECT_EQ(ret, SENSOR_ERROR_INVALID_PARAM) << "ActivateSensor ret = " << ret << endl;
129 };
130 
131 /**
132  * @tc.name: ActivateSensorInterfaceTest002
133  * @tc.desc: Test the enable sensor interface, the input parameter sensorUser is set to NULL.
134  * @tc.type: FUNC
135  * @tc.require: AR000F46AI
136  */
137 HWTEST_F(SensorAgentInterfacesTest, ActivateSensorInterfaceTest002, TestSize.Level1)
138 {
139     HILOG_INFO(HILOG_MODULE_APP, "ActivateSensorInterfaceTest002 called");
140     SensorUser *sensorUser = (SensorUser *)NULLPTR;
141     int32_t ret = ActivateSensor(0, sensorUser);
142     EXPECT_EQ(ret, SENSOR_ERROR_INVALID_PARAM) << "ActivateSensor ret = " << ret << endl;
143 };
144 
145 /**
146  * @tc.name: ActivateSensorInterfaceTest003
147  * @tc.desc: Test the enable sensor interface, the process is to obtain the sensor list, register, enable, disable,
148  * and deregister the sensor. The enabled sensor ID is 1.
149  * @tc.type: FUNC
150  * @tc.require: AR000F46AI
151  */
152 HWTEST_F(SensorAgentInterfacesTest, ActivateSensorInterfaceTest003, TestSize.Level1)
153 {
154     HILOG_INFO(HILOG_MODULE_APP, "ActivateSensorInterfaceTest003 called");
155     SensorUser sensorUser;
156     sensorUser.callback = SensorDataCallbackImpl;
157     SensorInfo *sensorInfo = (SensorInfo *)NULLPTR;
158     int32_t count = 0;
159     int32_t ret = GetAllSensors(&sensorInfo, &count);
160     EXPECT_EQ(ret, 0) << "GetAllSensors ret = " << ret << endl;
161     sleep(1);
162 
163     ret = ActivateSensor(0, &sensorUser);
164     EXPECT_EQ(ret, 0) << "ActivateSensor ret = " << ret << endl;
165     sleep(3);
166 
167     ret = DeactivateSensor(0, &sensorUser);
168     EXPECT_EQ(ret, 0) << "DeactivateSensor ret = " << ret << endl;
169     sleep(1);
170 };
171 
172 /**
173  * @tc.name: DeactivateSensorInterfaceTest001
174  * @tc.desc: Disable sensor, the input parameter sensorUser is set to NULL.
175  * @tc.type: FUNC
176  * @tc.require: AR000F46AJ
177  */
178 HWTEST_F(SensorAgentInterfacesTest, DeactivateSensorInterfaceTest001, TestSize.Level1)
179 {
180     HILOG_INFO(HILOG_MODULE_APP, "DeactivateSensorInterfaceTest001 called");
181     SensorUser *sensorUser = (SensorUser *)NULLPTR;
182     int32_t ret = DeactivateSensor(0, sensorUser);
183     EXPECT_EQ(ret, SENSOR_ERROR_INVALID_PARAM) << "DeactivateSensor ret = " << ret << endl;
184 };
185 
186 /**
187  * @tc.name: DeactivateSensorInterfaceTest002
188  * @tc.desc: Disable sensor, the input parameter sensor id is -1.
189  * @tc.type: FUNC
190  * @tc.require: AR000F46AJ
191  */
192 HWTEST_F(SensorAgentInterfacesTest, DeactivateSensorInterfaceTest002, TestSize.Level1)
193 {
194     HILOG_INFO(HILOG_MODULE_APP, "DeactivateSensorInterfaceTest002 called");
195     SensorUser sensorUser;
196     int32_t ret = DeactivateSensor(-1, &sensorUser);
197     EXPECT_EQ(ret, SENSOR_ERROR_INVALID_PARAM) << "DeactivateSensor ret = " << ret << endl;
198 };
199 
200 /**
201  * @tc.name: SetBatchInterfaceTest001
202  * @tc.desc: Set sensor param, sensorUser is NULL.
203  * @tc.type: FUNC
204  * @tc.require: AR000F46AD
205  */
206 HWTEST_F(SensorAgentInterfacesTest, SetBatchInterfaceTest001, TestSize.Level1)
207 {
208     HILOG_INFO(HILOG_MODULE_APP, "SetBatchInterfaceTest001 called");
209     SensorUser *sensorUser = (SensorUser *)nullptr;
210     int32_t ret = SetBatch(0, sensorUser, 200, 3000);
211     EXPECT_EQ(ret, SENSOR_ERROR_INVALID_PARAM) << "SetBatch ret = " << ret << endl;
212 };
213 
214 /**
215  * @tc.name: SetBatchInterfaceTest002
216  * @tc.desc: Set sensor param.
217  * @tc.type: FUNC
218  * @tc.require: AR000F46AD
219  */
220 HWTEST_F(SensorAgentInterfacesTest, SetBatchInterfaceTest002, TestSize.Level1)
221 {
222     HILOG_INFO(HILOG_MODULE_APP, "SetBatchInterfaceTest002 called");
223     SensorUser sensorUser;
224     int32_t ret = SetBatch(0, &sensorUser, -1, 3000);
225     EXPECT_EQ(ret, SENSOR_ERROR_INVALID_PARAM) << "SetBatch ret = " << ret << endl;
226 };
227 
228 /**
229  * @tc.name: SetBatchInterfaceTest003
230  * @tc.desc: Set sensor param.
231  * @tc.type: FUNC
232  * @tc.require: AR000F46AF
233  */
234 HWTEST_F(SensorAgentInterfacesTest, SetBatchInterfaceTest003, TestSize.Level1)
235 {
236     HILOG_INFO(HILOG_MODULE_APP, "SetBatchInterfaceTest003 called");
237     SensorUser sensorUser;
238     int32_t ret = SetBatch(0, &sensorUser, 200, 3000);
239     EXPECT_EQ(ret, 0) << "SetBatch ret = " << ret << endl;
240 };
241 
242 /**
243  * @tc.name: SetBatchInterfaceTest004
244  * @tc.desc: Set sensor param.
245  * @tc.type: FUNC
246  * @tc.require: AR000F46AF
247  */
248 HWTEST_F(SensorAgentInterfacesTest, SetBatchInterfaceTest004, TestSize.Level1)
249 {
250     HILOG_INFO(HILOG_MODULE_APP, "SetBatchInterfaceTest004 called");
251     SensorUser sensorUser;
252     int32_t ret = SetBatch(0, &sensorUser, 200, -1);
253     EXPECT_EQ(ret, SENSOR_ERROR_INVALID_PARAM) << "SetBatch ret = " << ret << endl;
254 };
255 
256 /**
257  * @tc.name: SetBatchInterfaceTest005
258  * @tc.desc: Set sensor param.
259  * @tc.type: FUNC
260  * @tc.require: AR000F46AF
261  */
262 HWTEST_F(SensorAgentInterfacesTest, SetBatchInterfaceTest005, TestSize.Level1)
263 {
264     HILOG_INFO(HILOG_MODULE_APP, "SetBatchInterfaceTest005 called");
265     SensorUser sensorUser;
266     int32_t ret = SetBatch(-1, &sensorUser, 200, 3000);
267     EXPECT_EQ(ret, SENSOR_ERROR_INVALID_PARAM) << "SetBatch ret = " << ret << endl;
268 };
269 
270 /**
271  * @tc.name: SubscribeSensorInterfaceTest001
272  * @tc.desc: SubscribeSensor sensor.
273  * @tc.type: FUNC
274  * @tc.require: AR000F46AL
275  */
276 HWTEST_F(SensorAgentInterfacesTest, SubscribeSensorInterfaceTest001, TestSize.Level1)
277 {
278     HILOG_INFO(HILOG_MODULE_APP, "SubscribeSensorInterfaceTest001 called");
279     SensorInfo *sensorInfo = (SensorInfo *)NULLPTR;
280     int32_t count = 0;
281     int32_t ret = GetAllSensors(&sensorInfo, &count);
282     EXPECT_EQ(ret, 0) << "GetAllSensors ret = " << ret << endl;
283     sleep(1);
284 
285     SensorUser sensorUser;
286     sensorUser.callback = SensorDataCallbackImpl;
287     ret = SubscribeSensor(0, &sensorUser);
288     EXPECT_EQ(ret, 0) << "SubscribeSensor ret = " << ret << endl;
289 
290     ret = UnsubscribeSensor(0, &sensorUser);
291     EXPECT_EQ(ret, 0) << "UnsubscribeSensor ret = " << ret << endl;
292 };
293 
294 /**
295  * @tc.name: SubscribeSensorInterfaceTest002
296  * @tc.desc: SubscribeSensor sensor.
297  * @tc.type: FUNC
298  * @tc.require: AR000F46AL
299  */
300 HWTEST_F(SensorAgentInterfacesTest, SubscribeSensorInterfaceTest002, TestSize.Level1)
301 {
302     HILOG_INFO(HILOG_MODULE_APP, "SubscribeSensorInterfaceTest002 called");
303     SensorUser *sensorUser = (SensorUser *)NULLPTR;
304     int32_t ret = SubscribeSensor(0, sensorUser);
305     EXPECT_EQ(ret, SENSOR_ERROR_INVALID_PARAM) << "SubscribeSensor ret = " << ret << endl;
306 };
307 
308 /**
309  * @tc.name: SubscribeSensorInterfaceTest003
310  * @tc.desc: SubscribeSensor sensor.
311  * @tc.type: FUNC
312  * @tc.require: AR000F46AL
313  */
314 HWTEST_F(SensorAgentInterfacesTest, SubscribeSensorInterfaceTest003, TestSize.Level1)
315 {
316     HILOG_INFO(HILOG_MODULE_APP, "SubscribeSensorInterfaceTest003 called");
317     SensorUser sensorUser;
318     sensorUser.callback = SensorDataCallbackImpl;
319     int32_t ret = SubscribeSensor(-1, &sensorUser);
320     EXPECT_EQ(ret, SENSOR_ERROR_INVALID_PARAM) << "SubscribeSensor ret = " << ret << endl;
321 };
322 
323 /**
324  * @tc.name: UnSubscribeSensorInterfaceTest001
325  * @tc.desc: UnSubscribeSensor sensor.
326  * @tc.type: FUNC
327  * @tc.require: AR000F46AM
328  */
329 HWTEST_F(SensorAgentInterfacesTest, UnSubscribeSensorInterfaceTest001, TestSize.Level1)
330 {
331     HILOG_INFO(HILOG_MODULE_APP, "UnSubscribeSensorInterfaceTest001 called");
332     SensorUser *sensorUser = (SensorUser *)NULLPTR;
333     int32_t ret = UnsubscribeSensor(0, sensorUser);
334     EXPECT_EQ(ret, SENSOR_ERROR_INVALID_PARAM) << "UnsubscribeSensor ret = " << ret << endl;
335 };
336 
337 /**
338  * @tc.name: UnSubscribeSensorInterfaceTest002
339  * @tc.desc: UnSubscribeSensor sensor.
340  * @tc.type: FUNC
341  * @tc.require: AR000F46AM
342  */
343 HWTEST_F(SensorAgentInterfacesTest, UnSubscribeSensorInterfaceTest002, TestSize.Level1)
344 {
345     HILOG_INFO(HILOG_MODULE_APP, "UnSubscribeSensorInterfaceTest002 called");
346     SensorUser sensorUser;
347     sensorUser.callback = SensorDataCallbackImpl;
348     int32_t ret = UnsubscribeSensor(-1, &sensorUser);
349     EXPECT_EQ(ret, SENSOR_ERROR_INVALID_PARAM) << "UnsubscribeSensor ret = " << ret << endl;
350 };
351 
352 /**
353  * @tc.name: UnSubscribeSensorInterfaceTest003
354  * @tc.desc: UnSubscribeSensor sensor.
355  * @tc.type: FUNC
356  * @tc.require: SR000F46AK
357  */
358 HWTEST_F(SensorAgentInterfacesTest, UnSubscribeSensorInterfaceTest003, TestSize.Level1)
359 {
360     HILOG_INFO(HILOG_MODULE_APP, "DataReportInterfaceTest001 called");
361     SensorUser sensorUser;
362     sensorUser.callback = SensorDataCallbackImpl;
363     SensorInfo *sensorInfo = (SensorInfo *)NULLPTR;
364     int32_t count = 0;
365 
366     int32_t ret = GetAllSensors(&sensorInfo, &count);
367     EXPECT_EQ(ret, 0) << "GetAllSensorsTest001 ret = " << ret << endl;
368     sleep(1);
369 
370     ret = UnsubscribeSensor(0, &sensorUser);
371     EXPECT_EQ(ret, SENSOR_ERROR_INVALID_PARAM) << "UnsubscribeSensor ret = " << ret << endl;
372 };
373 
374 /**
375  * @tc.name: DataReportInterfaceTest001
376  * @tc.desc: Sensor data report.
377  * @tc.type: FUNC
378  * @tc.require: AR000F46AO
379  */
380 HWTEST_F(SensorAgentInterfacesTest, DataReportInterfaceTest001, TestSize.Level0)
381 {
382     HILOG_INFO(HILOG_MODULE_APP, "DataReportInterfaceTest001 called");
383     SensorUser sensorUser;
384     sensorUser.callback = SensorDataCallbackImpl;
385 
386     SensorInfo *sensorInfo = (SensorInfo *)NULLPTR;
387     int32_t count = 0;
388 
389     int32_t ret = GetAllSensors(&sensorInfo, &count);
390     EXPECT_EQ(ret, 0) << "GetAllSensors ret = " << ret << endl;
391     sleep(1);
392 
393     ret = SubscribeSensor(0, &sensorUser);
394     EXPECT_EQ(ret, 0) << "SubscribeSensor ret = " << ret << endl;
395     sleep(1);
396 
397     ret = ActivateSensor(0, &sensorUser);
398     EXPECT_EQ(ret, 0) << "ActivateSensor ret = " << ret << endl;
399     sleep(1);
400 
401     ret = DeactivateSensor(0, &sensorUser);
402     EXPECT_EQ(ret, 0) << "DeactivateSensor ret = " << ret << endl;
403     sleep(1);
404 
405     ret = UnsubscribeSensor(0, &sensorUser);
406     EXPECT_EQ(ret, 0) << "UnsubscribeSensor ret = " << ret << endl;
407 };
408 
409 /**
410  * @tc.name: DataReportInterfaceTest002
411  * @tc.desc: Sensor data report.
412  * @tc.type: FUNC
413  * @tc.require: AR000F46AO
414  */
415 HWTEST_F(SensorAgentInterfacesTest, DataReportInterfaceTest002, TestSize.Level1)
416 {
417     HILOG_INFO(HILOG_MODULE_APP, "DataReportInterfaceTest002 called");
418     SensorUser sensorUser;
419     sensorUser.callback = SensorDataCallbackImpl;
420 
421     SensorInfo *sensorInfo = (SensorInfo *)NULLPTR;
422     int32_t count = 0;
423 
424     int32_t ret = GetAllSensors(&sensorInfo, &count);
425     EXPECT_EQ(ret, 0) << "GetAllSensors ret = " << ret << endl;
426     sleep(1);
427 
428     ret = ActivateSensor(0, &sensorUser);
429     EXPECT_EQ(ret, 0) << "ActivateSensor ret = " << ret << endl;
430     sleep(1);
431 
432     ret = SubscribeSensor(0, &sensorUser);
433     EXPECT_EQ(ret, 0) << "SubscribeSensor ret = " << ret << endl;
434     sleep(1);
435 
436     ret = DeactivateSensor(0, &sensorUser);
437     EXPECT_EQ(ret, 0) << "DeactivateSensor ret = " << ret << endl;
438     sleep(1);
439 
440     ret = UnsubscribeSensor(0, &sensorUser);
441     EXPECT_EQ(ret, 0) << "UnsubscribeSensor ret = " << ret << endl;
442 };
443 
444 /**
445  * @tc.name: DataReportInterfaceTest003
446  * @tc.desc: Sensor data report.
447  * @tc.type: FUNC
448  * @tc.require: AR000F46AO
449  */
450 HWTEST_F(SensorAgentInterfacesTest, DataReportInterfaceTest003, TestSize.Level1)
451 {
452     HILOG_INFO(HILOG_MODULE_APP, "DataReportInterfaceTest003 called");
453     SensorUser sensorUser;
454     sensorUser.callback = SensorDataCallbackImpl;
455 
456     SensorInfo *sensorInfo = (SensorInfo *)NULLPTR;
457     int32_t count = 0;
458 
459     int32_t ret = GetAllSensors(&sensorInfo, &count);
460     EXPECT_EQ(ret, 0) << "GetAllSensors ret = " << ret << endl;
461     sleep(1);
462 
463     ret = SubscribeSensor(0, &sensorUser);
464     EXPECT_EQ(ret, 0) << "SubscribeSensor ret = " << ret << endl;
465     sleep(1);
466 
467     ret = ActivateSensor(0, &sensorUser);
468     EXPECT_EQ(ret, 0) << "ActivateSensor ret = " << ret << endl;
469     sleep(1);
470 
471     ret = DeactivateSensor(0, &sensorUser);
472     EXPECT_EQ(ret, 0) << "DeactivateSensor ret = " << ret << endl;
473     sleep(1);
474 
475     ret = UnsubscribeSensor(0, &sensorUser);
476     EXPECT_EQ(ret, 0) << "UnsubscribeSensor ret = " << ret << endl;
477 
478     ret = UnsubscribeSensor(0, &sensorUser);
479     EXPECT_EQ(ret, SENSOR_ERROR_INVALID_PARAM) << "UnsubscribeSensor ret = " << ret << endl;
480 };
481 }