• 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 <cmath>
17 #include <cstdio>
18 #include <unistd.h>
19 #include <gtest/gtest.h>
20 #include <securec.h>
21 #include "hdf_base.h"
22 #include "osal_time.h"
23 #include "v1_0/sensor_interface_proxy.h"
24 #include "sensor_type.h"
25 #include "sensor_callback_impl.h"
26 
27 using namespace OHOS::HDI::Sensor::V1_0;
28 using namespace testing::ext;
29 
30 namespace {
31     sptr<ISensorInterface>  g_sensorInterface = nullptr;
32     sptr<ISensorCallback> g_traditionalCallback = new SensorCallbackImpl();
33     sptr<ISensorCallback> g_medicalCallback = new SensorCallbackImpl();
34     std::vector<HdfSensorInformation> g_info;
35     struct SensorValueRange {
36         float highThreshold;
37         float lowThreshold;
38     };
39 
40     struct SensorDevelopmentList {
41         int32_t sensorTypeId;
42         char sensorName[SENSOR_NAME_MAX_LEN];
43         int32_t dataForm;    // 0: fixed, 1: range
44         int32_t dataDimension;
45         struct SensorValueRange *valueRange;
46     };
47 
48     struct SensorValueRange g_testRange[] = {{1e5, 0}};
49     struct SensorValueRange g_accelRange[] = {{78, -78}, {78, -78}, {78, -78}};
50     struct SensorValueRange g_alsRange[] = {{10000, 0}};
51     struct SensorValueRange g_pedometerRange[] = {{10000, 0}};
52     struct SensorValueRange g_proximityRange[] = {{5, 0}};
53     struct SensorValueRange g_hallRange[] = {{1, 0}};
54     struct SensorValueRange g_barometerRange[] = {{1100, -1100}, {1100, -1100}};
55     struct SensorValueRange g_magneticRange[] = {{2000, -2000}, {2000, -2000}, {2000, -2000}};
56     struct SensorValueRange g_gyroscopeRange[] = {{35, -35}, {35, -35}, {35, -35}};
57     struct SensorValueRange g_gravityRange[] = {{78, -78}, {78, -78}, {78, -78}};
58 
59     struct SensorDevelopmentList g_sensorList[] = {
60         {SENSOR_TYPE_NONE, "sensor_test",  1, 1, g_testRange},
61         {SENSOR_TYPE_ACCELEROMETER, "accelerometer",  1, 3, g_accelRange},
62         {SENSOR_TYPE_PEDOMETER, "pedometer", 1, 1, g_pedometerRange},
63         {SENSOR_TYPE_PROXIMITY, "proximity",  0, 1, g_proximityRange},
64         {SENSOR_TYPE_HALL, "hallrometer",  0, 1, g_hallRange},
65         {SENSOR_TYPE_BAROMETER, "barometer",  1, 2, g_barometerRange},
66         {SENSOR_TYPE_AMBIENT_LIGHT, "als", 1, 1, g_alsRange},
67         {SENSOR_TYPE_MAGNETIC_FIELD, "magnetometer",  1, 3, g_magneticRange},
68         {SENSOR_TYPE_GYROSCOPE, "gyroscope", 1, 3, g_gyroscopeRange},
69         {SENSOR_TYPE_GRAVITY, "gravity", 1, 3, g_gravityRange}
70     };
71 
72     constexpr int g_listNum = sizeof(g_sensorList) / sizeof(g_sensorList[0]);
73     constexpr int32_t SENSOR_INTERVAL1 = 200000000;
74     constexpr int32_t SENSOR_INTERVAL2 = 20000000;
75     constexpr int32_t SENSOR_POLL_TIME = 1;
76     constexpr int32_t SENSOR_WAIT_TIME = 100;
77     constexpr int32_t ABNORMAL_SENSORID = -1;
78 }
79 
80 class HdfSensorHdiTest : public testing::Test {
81 public:
82     static void SetUpTestCase();
83     static void TearDownTestCase();
84     void SetUp();
85     void TearDown();
86 };
87 
SetUpTestCase()88 void HdfSensorHdiTest::SetUpTestCase()
89 {
90     g_sensorInterface = ISensorInterface::Get();
91 }
92 
TearDownTestCase()93 void HdfSensorHdiTest::TearDownTestCase()
94 {
95 }
96 
SetUp()97 void HdfSensorHdiTest::SetUp()
98 {
99 }
100 
TearDown()101 void HdfSensorHdiTest::TearDown()
102 {
103 }
104 
105 /**
106   * @tc.name: GetSensorClient0001
107   * @tc.desc: Get a client and check whether the client is empty.
108   * @tc.type: FUNC
109   * @tc.require: #I4L3LF
110   */
111 HWTEST_F(HdfSensorHdiTest, GetSensorClient0001, TestSize.Level1)
112 {
113     ASSERT_NE(nullptr, g_sensorInterface);
114 }
115 
116 /**
117   * @tc.name: GetSensorList0001
118   * @tc.desc: Obtains information about all sensors in the system.
119   * @tc.type: FUNC
120   * @tc.require: #I4L3LF
121   */
122 HWTEST_F(HdfSensorHdiTest, GetSensorList0001, TestSize.Level1)
123 {
124     if (g_sensorInterface == nullptr) {
125         ASSERT_NE(nullptr, g_sensorInterface);
126         return;
127     }
128     int32_t ret = g_sensorInterface->GetAllSensorInfo(g_info);
129     EXPECT_EQ(SENSOR_SUCCESS, ret);
130     EXPECT_GT(g_info.size(), 0);
131     printf("get sensor list num[%zu]\n\r", g_info.size());
132 
133     for (auto iter : g_info) {
134         printf("get sensoriId[%d], info name[%s], power[%f]\n\r", iter.sensorId, iter.sensorName.c_str(), iter.power);
135         for (int j =0; j < g_listNum; ++j) {
136             if (iter.sensorId == g_sensorList[j].sensorTypeId) {
137                 EXPECT_GT(iter.sensorName.size(), 0);
138                 break;
139             }
140         }
141     }
142 }
143 
144 /**
145   * @tc.name: RegisterSensorDataCb0001
146   * @tc.desc: Returns 0 if the callback is successfully registered; returns a negative value otherwise.
147   * @tc.type: FUNC
148   * @tc.require: SR000F869M, AR000F869P, AR000F8QNL
149   */
150 HWTEST_F(HdfSensorHdiTest, RegisterSensorDataCb0001, TestSize.Level1)
151 {
152     if (g_sensorInterface == nullptr) {
153         ASSERT_NE(nullptr, g_sensorInterface);
154         return;
155     }
156     int32_t ret = g_sensorInterface->Register(TRADITIONAL_SENSOR_TYPE, g_medicalCallback);
157     EXPECT_EQ(SENSOR_SUCCESS, ret);
158     ret = g_sensorInterface->Unregister(TRADITIONAL_SENSOR_TYPE, g_medicalCallback);
159     EXPECT_EQ(SENSOR_SUCCESS, ret);
160 }
161 
162 /**
163   * @tc.name: RegisterSensorDataCb0002
164   * @tc.desc: Returns 0 if the callback is successfully registered; returns a negative value otherwise.
165   * @tc.type: FUNC
166   * @tc.require: SR000F869M, AR000F869P, AR000F8QNL
167   */
168 HWTEST_F(HdfSensorHdiTest, RegisterSensorDataCb0002, TestSize.Level1)
169 {
170     if (g_sensorInterface == nullptr) {
171         ASSERT_NE(nullptr, g_sensorInterface);
172         return;
173     }
174     int32_t ret = g_sensorInterface->Register(MEDICAL_SENSOR_TYPE, g_medicalCallback);
175     EXPECT_EQ(SENSOR_SUCCESS, ret);
176     ret = g_sensorInterface->Unregister(MEDICAL_SENSOR_TYPE, g_medicalCallback);
177     EXPECT_EQ(SENSOR_SUCCESS, ret);
178 }
179 
180 /**
181   * @tc.name: RegisterDataCb001
182   * @tc.desc: Returns 0 if the callback is successfully registered; returns a negative value otherwise.
183   * @tc.type: FUNC
184   * @tc.require: SR000F869M, AR000F869P, AR000F8QNL
185   */
186 HWTEST_F(HdfSensorHdiTest, RegisterSensorDataCb0003, TestSize.Level1)
187 {
188     if (g_sensorInterface == nullptr) {
189         ASSERT_NE(nullptr, g_sensorInterface);
190         return;
191     }
192     int32_t ret = g_sensorInterface->Register(SENSOR_GROUP_TYPE_MAX, g_medicalCallback);
193     EXPECT_EQ(SENSOR_INVALID_PARAM, ret);
194     ret = g_sensorInterface->Unregister(SENSOR_GROUP_TYPE_MAX, g_medicalCallback);
195     EXPECT_EQ(SENSOR_INVALID_PARAM, ret);
196 }
197 
198 /**
199   * @tc.name: EnableSensor0001
200   * @tc.desc: Enables the sensor unavailable in the sensor list based on the specified sensor ID.
201   * @tc.type: FUNC
202   * @tc.require: #I4L3LF
203   */
204 HWTEST_F(HdfSensorHdiTest, EnableSensor0001, TestSize.Level1)
205 {
206     if (g_sensorInterface == nullptr) {
207         ASSERT_NE(nullptr, g_sensorInterface);
208         return;
209     }
210     int32_t ret = g_sensorInterface->Register(TRADITIONAL_SENSOR_TYPE, g_traditionalCallback);
211     EXPECT_EQ(SENSOR_SUCCESS, ret);
212 
213     EXPECT_GT(g_info.size(), 0);
214 
215     for (auto iter : g_info) {
216         ret = g_sensorInterface->SetBatch(iter.sensorId, SENSOR_INTERVAL1, SENSOR_POLL_TIME);
217         EXPECT_EQ(SENSOR_SUCCESS, ret);
218         ret = g_sensorInterface->Enable(iter.sensorId);
219         EXPECT_EQ(SENSOR_SUCCESS, ret);
220         OsalSleep(SENSOR_POLL_TIME);
221         ret = g_sensorInterface->Disable(iter.sensorId);
222         EXPECT_EQ(SENSOR_SUCCESS, ret);
223     }
224     ret = g_sensorInterface->Unregister(TRADITIONAL_SENSOR_TYPE, g_traditionalCallback);
225     EXPECT_EQ(0, ret);
226     EXPECT_EQ(SensorCallbackImpl::sensorDataFlag, 1);
227     SensorCallbackImpl::sensorDataFlag = 1;
228 }
229 
230 /**
231   * @tc.name: EnableSensor0002
232   * @tc.desc: Enables the sensor available in the sensor list based on the specified sensor ID.
233   * @tc.type: FUNC
234   * @tc.require: #I4L3LF
235   */
236 HWTEST_F(HdfSensorHdiTest, EnableSensor0002, TestSize.Level1)
237 {
238     if (g_sensorInterface == nullptr) {
239         ASSERT_NE(nullptr, g_sensorInterface);
240         return;
241     }
242     int32_t ret = g_sensorInterface->Enable(ABNORMAL_SENSORID);
243     EXPECT_EQ(SENSOR_NOT_SUPPORT, ret);
244     ret = g_sensorInterface->Disable(ABNORMAL_SENSORID);
245     EXPECT_EQ(SENSOR_NOT_SUPPORT, ret);
246 }
247 
248 /**
249   * @tc.name: SetSensorBatch0001
250   * @tc.desc: Sets the sampling time and data report interval for sensors in batches.
251   * @tc.type: FUNC
252   * @tc.require: #I4L3LF
253   */
254 HWTEST_F(HdfSensorHdiTest, SetSensorBatch0001, TestSize.Level1)
255 {
256     if (g_sensorInterface == nullptr) {
257         ASSERT_NE(nullptr, g_sensorInterface);
258         return;
259     }
260     int32_t ret = g_sensorInterface->Register(TRADITIONAL_SENSOR_TYPE, g_traditionalCallback);
261     EXPECT_EQ(SENSOR_SUCCESS, ret);
262 
263     for (auto iter : g_info) {
264         ret = g_sensorInterface->SetBatch(iter.sensorId, SENSOR_INTERVAL2, SENSOR_POLL_TIME);
265         EXPECT_EQ(SENSOR_SUCCESS, ret);
266         ret = g_sensorInterface->Enable(iter.sensorId);
267         EXPECT_EQ(SENSOR_SUCCESS, ret);
268         OsalMSleep(SENSOR_WAIT_TIME);
269         ret = g_sensorInterface->Disable(iter.sensorId);
270         EXPECT_EQ(SENSOR_SUCCESS, ret);
271     }
272 
273     ret = g_sensorInterface->Unregister(TRADITIONAL_SENSOR_TYPE, g_traditionalCallback);
274     EXPECT_EQ(SENSOR_SUCCESS, ret);
275     EXPECT_EQ(SensorCallbackImpl::sensorDataFlag, 1);
276     SensorCallbackImpl::sensorDataFlag = 1;
277 }
278 
279 /** @tc.name: SetSensorBatch0002
280     @tc.desc: Sets the sampling time and data report interval for sensors in batches.
281     @tc.type: FUNC
282     @tc.requrire: #I4L3LF
283     */
284 HWTEST_F(HdfSensorHdiTest, SetSensorBatch0002, TestSize.Level1)
285 {
286     if (g_sensorInterface == nullptr) {
287         ASSERT_NE(nullptr, g_sensorInterface);
288         return;
289     }
290     int32_t ret = g_sensorInterface->SetBatch(ABNORMAL_SENSORID, 0, 0);
291     EXPECT_EQ(SENSOR_NOT_SUPPORT, ret);
292 }
293 
294 /**
295   * @tc.name: SetSensorBatch0003
296   * @tc.desc: Sets the sampling time and data report interval for sensors in batches.
297   * @tc.type: FUNC
298   * @tc.require: #I4L3LF
299   */
300 HWTEST_F(HdfSensorHdiTest, SetSensorBatch0003, TestSize.Level1)
301 {
302     if (g_sensorInterface == nullptr) {
303         ASSERT_NE(nullptr, g_sensorInterface);
304         return;
305     }
306     for (auto iter : g_info) {
307         int32_t ret = g_sensorInterface->SetBatch(iter.sensorId, -1, SENSOR_POLL_TIME);
308         EXPECT_EQ(SENSOR_INVALID_PARAM, ret);
309     }
310 }
311 
312 /**
313   * @tc.name: SetSensorMode0001
314   * @tc.desc: Sets the data reporting mode for the specified sensor.
315   * @tc.type: FUNC
316   * @tc.require: #I4L3LF
317   */
318 HWTEST_F(HdfSensorHdiTest, SetSensorMode0001, TestSize.Level1)
319 {
320     if (g_sensorInterface == nullptr) {
321         ASSERT_NE(nullptr, g_sensorInterface);
322         return;
323     }
324     EXPECT_GT(g_info.size(), 0);
325     for (auto iter : g_info) {
326         int32_t ret = g_sensorInterface->SetBatch(iter.sensorId, SENSOR_INTERVAL1, SENSOR_POLL_TIME);
327         EXPECT_EQ(SENSOR_SUCCESS, ret);
328         if (iter.sensorId == SENSOR_TYPE_HALL) {
329             ret = g_sensorInterface->SetMode(iter.sensorId, SENSOR_MODE_ON_CHANGE);
330             EXPECT_EQ(SENSOR_SUCCESS, ret);
331         } else {
332             ret = g_sensorInterface->SetMode(iter.sensorId, SENSOR_MODE_REALTIME);
333             EXPECT_EQ(SENSOR_SUCCESS, ret);
334         }
335         ret = g_sensorInterface->Enable(iter.sensorId);
336         EXPECT_EQ(SENSOR_SUCCESS, ret);
337         OsalMSleep(SENSOR_WAIT_TIME);
338         ret = g_sensorInterface->Disable(iter.sensorId);
339         EXPECT_EQ(SENSOR_SUCCESS, ret);
340     }
341 }
342 
343 /**
344   * @tc.name: SetSensorMode0002
345   * @tc.desc: Sets the data reporting mode for the specified sensor.The current real-time polling mode is valid.
346   * Other values are invalid.
347   * @tc.type: FUNC
348   * @tc.require: #I4L3LF
349   */
350 HWTEST_F(HdfSensorHdiTest, SetSensorMode0002, TestSize.Level1)
351 {
352     if (g_sensorInterface == nullptr) {
353         ASSERT_NE(nullptr, g_sensorInterface);
354         return;
355     }
356     int32_t ret = g_sensorInterface->SetMode(ABNORMAL_SENSORID, SENSOR_MODE_REALTIME);
357     EXPECT_EQ(SENSOR_NOT_SUPPORT, ret);
358 }
359 
360 /**
361   * @tc.name: SetSensorMode0003
362   * @tc.desc: Sets the data reporting mode for the specified sensor.The current real-time polling mode is valid.
363   * Other values are invalid.
364   * @tc.type: FUNC
365   * @tc.require: #I4L3LF
366   */
367 HWTEST_F(HdfSensorHdiTest, SetSensorMode0003, TestSize.Level1)
368 {
369     if (g_sensorInterface == nullptr) {
370         ASSERT_NE(nullptr, g_sensorInterface);
371         return;
372     }
373     EXPECT_GT(g_info.size(), 0);
374     for (auto iter : g_info) {
375         int32_t ret = g_sensorInterface->SetBatch(iter.sensorId, SENSOR_INTERVAL1, SENSOR_POLL_TIME);
376         EXPECT_EQ(SENSOR_SUCCESS, ret);
377         ret = g_sensorInterface->SetMode(iter.sensorId, SENSOR_MODE_DEFAULT);
378         EXPECT_EQ(SENSOR_FAILURE, ret);
379         ret = g_sensorInterface->Enable(iter.sensorId);
380         EXPECT_EQ(SENSOR_SUCCESS, ret);
381         OsalMSleep(SENSOR_WAIT_TIME);
382         ret = g_sensorInterface->Disable(iter.sensorId);
383         EXPECT_EQ(SENSOR_SUCCESS, ret);
384     }
385 }
386 
387 /**
388   * @tc.name: SetSensorOption0001
389   * @tc.desc: Sets options for the specified sensor, including its measurement range and accuracy.
390   * @tc.type: FUNC
391   * @tc.require: #I4L3LF
392   */
393 HWTEST_F(HdfSensorHdiTest, SetSensorOption0001, TestSize.Level1)
394 {
395     if (g_sensorInterface == nullptr) {
396         ASSERT_NE(nullptr, g_sensorInterface);
397         return;
398     }
399     EXPECT_GT(g_info.size(), 0);
400     for (auto iter : g_info) {
401         int32_t ret = g_sensorInterface->SetOption(iter.sensorId, 0);
402         EXPECT_EQ(SENSOR_SUCCESS, ret);
403     }
404 }
405 
406 /**
407   * @tc.name: SetSensorOption0002
408   * @tc.desc: Sets options for the specified sensor, including its measurement range and accuracy.
409   * @tc.type: FUNC
410   * @tc.require: #I4L3LF
411   */
412 HWTEST_F(HdfSensorHdiTest, SetSensorOption0002, TestSize.Level1)
413 {
414     if (g_sensorInterface == nullptr) {
415         ASSERT_NE(nullptr, g_sensorInterface);
416         return;
417     }
418     int32_t ret = g_sensorInterface->SetOption(ABNORMAL_SENSORID, 0);
419     EXPECT_EQ(SENSOR_NOT_SUPPORT, ret);
420 }
421