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 <gtest/gtest.h>
19 #include <securec.h>
20 #include "hdf_base.h"
21 #include "osal_time.h"
22 #include "sensor_if.h"
23 #include "sensor_type.h"
24 
25 using namespace testing::ext;
26 
27 namespace {
28     const int32_t SENSOR_ID = 0;
29     const int32_t SENSOR_INTERVAL = 1000000000;
30     const int32_t SENSOR_POLL_TIME = 5;
31     const int32_t SENSOR_USEC_TIME = 1000000;
32     const int32_t SENSOR_MSEC_TIME = 1000;
33     const int32_t SENSOR_COMMON_TIME = 2000;
34     const struct SensorInterface *g_sensorPerformanceDev = nullptr;
35 
SensorTestDataCallback(const struct SensorEvents * event)36     int SensorTestDataCallback(const struct SensorEvents *event)
37     {
38         static int32_t sensorDataFlag = 0;
39 
40         if (event == nullptr || event->data == nullptr) {
41             return -1;
42         }
43         float *data = reinterpret_cast<float*>(event->data);
44 
45         if (event->sensorId == SENSOR_ID) {
46             printf("time [%lld] sensor id [%d] data [%f]\n\r", event->timestamp, event->sensorId, *(data));
47             if (fabs(*data) > 1e-5) {
48                 sensorDataFlag = 1;
49             }
50         }
51 
52         return 0;
53     }
54 }
55 
56 class HdfSensorPerformanceTest : public testing::Test {
57 public:
58     static void SetUpTestCase();
59     static void TearDownTestCase();
60     void SetUp();
61     void TearDown();
62 };
63 
SetUpTestCase()64 void HdfSensorPerformanceTest::SetUpTestCase()
65 {
66     g_sensorPerformanceDev = NewSensorInterfaceInstance();
67     if (g_sensorPerformanceDev == nullptr) {
68         printf("test sensorHdi get Module instance failed\n\r");
69     }
70 }
71 
TearDownTestCase()72 void HdfSensorPerformanceTest::TearDownTestCase()
73 {
74     if (g_sensorPerformanceDev != nullptr) {
75         FreeSensorInterfaceInstance();
76         g_sensorPerformanceDev = nullptr;
77     }
78 }
79 
SetUp()80 void HdfSensorPerformanceTest::SetUp()
81 {
82 }
83 
TearDown()84 void HdfSensorPerformanceTest::TearDown()
85 {
86 }
87 
88 /**
89   * @tc.name: SensorHdiRegister001
90   * @tc.desc: Interface performance test.
91   * @tc.type: FUNC
92   * @tc.require: AR000F869N
93   */
94 HWTEST_F(HdfSensorPerformanceTest, SensorHdiRegister001, TestSize.Level1)
95 {
96     int timeUsed = 0;
97     struct timespec tv1 = (struct timespec){0};
98     struct timespec tv2 = (struct timespec){0};
99 
100     clock_gettime(CLOCK_REALTIME, &tv1);
101     int ret = g_sensorPerformanceDev->Register(0, SensorTestDataCallback);
102     clock_gettime(CLOCK_REALTIME, &tv2);
103     timeUsed = ((tv2.tv_sec * SENSOR_USEC_TIME + tv2.tv_nsec / SENSOR_MSEC_TIME) -
104         (tv1.tv_sec * SENSOR_USEC_TIME + tv1.tv_nsec / SENSOR_MSEC_TIME));
105     EXPECT_GE(SENSOR_COMMON_TIME, timeUsed);
106     EXPECT_EQ(0, ret);
107 }
108 
109 /**
110   * @tc.name: SensorHdiGetAllSensors001
111   * @tc.desc: Interface performance test.
112   * @tc.type: FUNC
113   * @tc.require: AR000F869O
114   */
115 HWTEST_F(HdfSensorPerformanceTest, SensorHdiGetAllSensors001, TestSize.Level1)
116 {
117     int32_t count = 0;
118     struct SensorInformation *sensorInfo = nullptr;
119     int timeUsed = 0;
120     struct timespec tv1 = (struct timespec){0};
121     struct timespec tv2 = (struct timespec){0};
122 
123     clock_gettime(CLOCK_REALTIME, &tv1);
124     int ret = g_sensorPerformanceDev->GetAllSensors(&sensorInfo, &count);
125     clock_gettime(CLOCK_REALTIME, &tv2);
126     timeUsed = ((tv2.tv_sec * SENSOR_USEC_TIME + tv2.tv_nsec / SENSOR_MSEC_TIME) -
127         (tv1.tv_sec * SENSOR_USEC_TIME + tv1.tv_nsec / SENSOR_MSEC_TIME));
128     EXPECT_GE(SENSOR_COMMON_TIME, timeUsed);
129     EXPECT_EQ(0, ret);
130 }
131 
132 /**
133   * @tc.name: SensorHdiEnable001
134   * @tc.desc: Interface performance test.
135   * @tc.type: FUNC
136   * @tc.require: AR000F869P
137   */
138 HWTEST_F(HdfSensorPerformanceTest, SensorHdiEnable001, TestSize.Level1)
139 {
140     int timeUsed = 0;
141     struct timespec tv1 = (struct timespec){0};
142     struct timespec tv2 = (struct timespec){0};
143 
144     clock_gettime(CLOCK_REALTIME, &tv1);
145     int ret = g_sensorPerformanceDev->Enable(SENSOR_ID);
146     clock_gettime(CLOCK_REALTIME, &tv2);
147     timeUsed = ((tv2.tv_sec * SENSOR_USEC_TIME + tv2.tv_nsec / SENSOR_MSEC_TIME) -
148         (tv1.tv_sec * SENSOR_USEC_TIME + tv1.tv_nsec / SENSOR_MSEC_TIME));
149     EXPECT_GE(SENSOR_COMMON_TIME, timeUsed);
150     EXPECT_EQ(0, ret);
151 }
152 
153 /**
154   * @tc.name: SensorHdiSetBatch001
155   * @tc.desc: Interface performance test.
156   * @tc.type: FUNC
157   * @tc.require: AR000F869Q
158   */
159 HWTEST_F(HdfSensorPerformanceTest, SensorHdiSetBatch001, TestSize.Level1)
160 {
161     int timeUsed = 0;
162     struct timespec tv1 = (struct timespec){0};
163     struct timespec tv2 = (struct timespec){0};
164 
165     clock_gettime(CLOCK_REALTIME, &tv1);
166     int ret = g_sensorPerformanceDev->SetBatch(SENSOR_ID, SENSOR_INTERVAL, SENSOR_POLL_TIME);
167     clock_gettime(CLOCK_REALTIME, &tv2);
168     timeUsed = ((tv2.tv_sec * SENSOR_USEC_TIME + tv2.tv_nsec / SENSOR_MSEC_TIME) -
169         (tv1.tv_sec * SENSOR_USEC_TIME + tv1.tv_nsec / SENSOR_MSEC_TIME));
170 
171     OsalSleep(SENSOR_POLL_TIME);
172 
173     EXPECT_GE(SENSOR_COMMON_TIME, timeUsed);
174     EXPECT_EQ(0, ret);
175 }
176 
177 /**
178   * @tc.name: SensorHdiDisable001
179   * @tc.desc: Interface performance test.
180   * @tc.type: FUNC
181   * @tc.require: AR000F869U
182   */
183 HWTEST_F(HdfSensorPerformanceTest, SensorHdiDisable001, TestSize.Level1)
184 {
185     int timeUsed = 0;
186     struct timespec tv1 = (struct timespec){0};
187     struct timespec tv2 = (struct timespec){0};
188 
189     clock_gettime(CLOCK_REALTIME, &tv1);
190     int ret = g_sensorPerformanceDev->Disable(SENSOR_ID);
191     clock_gettime(CLOCK_REALTIME, &tv2);
192     timeUsed = ((tv2.tv_sec * SENSOR_USEC_TIME + tv2.tv_nsec / SENSOR_MSEC_TIME) -
193         (tv1.tv_sec * SENSOR_USEC_TIME + tv1.tv_nsec / SENSOR_MSEC_TIME));
194     EXPECT_GE(SENSOR_COMMON_TIME, timeUsed);
195     EXPECT_EQ(0, ret);
196 }
197 
198 /**
199   * @tc.name: SensorHdiSetMode001
200   * @tc.desc: Interface performance test.
201   * @tc.type: FUNC
202   * @tc.require: AR000F869R
203   */
204 HWTEST_F(HdfSensorPerformanceTest, SensorHdiSetMode001, TestSize.Level1)
205 {
206     int timeUsed = 0;
207     struct timespec tv1 = (struct timespec){0};
208     struct timespec tv2 = (struct timespec){0};
209 
210     clock_gettime(CLOCK_REALTIME, &tv1);
211     int ret = g_sensorPerformanceDev->SetMode(SENSOR_ID, 1);
212     clock_gettime(CLOCK_REALTIME, &tv2);
213     timeUsed = ((tv2.tv_sec * SENSOR_USEC_TIME + tv2.tv_nsec / SENSOR_MSEC_TIME) -
214         (tv1.tv_sec * SENSOR_USEC_TIME + tv1.tv_nsec / SENSOR_MSEC_TIME));
215     EXPECT_GE(SENSOR_COMMON_TIME, timeUsed);
216     EXPECT_EQ(0, ret);
217 }
218 
219 /**
220   * @tc.name: SensorHdiSetOption001
221   * @tc.desc: Interface performance test.
222   * @tc.type: FUNC
223   * @tc.require: AR000F869S
224   */
225 HWTEST_F(HdfSensorPerformanceTest, SensorHdiSetOption001, TestSize.Level1)
226 {
227     int timeUsed = 0;
228     struct timespec tv1 = (struct timespec){0};
229     struct timespec tv2 = (struct timespec){0};
230 
231     clock_gettime(CLOCK_REALTIME, &tv1);
232     int ret = g_sensorPerformanceDev->SetOption(SENSOR_ID, 1);
233     clock_gettime(CLOCK_REALTIME, &tv2);
234     timeUsed = ((tv2.tv_sec * SENSOR_USEC_TIME + tv2.tv_nsec / SENSOR_MSEC_TIME) -
235         (tv1.tv_sec * SENSOR_USEC_TIME + tv1.tv_nsec / SENSOR_MSEC_TIME));
236     EXPECT_GE(SENSOR_COMMON_TIME, timeUsed);
237     EXPECT_EQ(0, ret);
238 }
239 
240 /**
241   * @tc.name: SensorHdiRegister001
242   * @tc.desc: Interface performance test.
243   * @tc.type: FUNC
244   * @tc.require: AR000F869T
245   */
246 HWTEST_F(HdfSensorPerformanceTest, SensorHdiUnregister001, TestSize.Level1)
247 {
248     int timeUsed = 0;
249     struct timespec tv1 = (struct timespec){0};
250     struct timespec tv2 = (struct timespec){0};
251 
252     clock_gettime(CLOCK_REALTIME, &tv1);
253     int ret = g_sensorPerformanceDev->Unregister(0, SensorTestDataCallback);
254     clock_gettime(CLOCK_REALTIME, &tv2);
255     timeUsed = ((tv2.tv_sec * SENSOR_USEC_TIME + tv2.tv_nsec / SENSOR_MSEC_TIME) -
256         (tv1.tv_sec * SENSOR_USEC_TIME + tv1.tv_nsec / SENSOR_MSEC_TIME));
257     EXPECT_GE(SENSOR_COMMON_TIME, timeUsed);
258     EXPECT_EQ(0, ret);
259 }
260