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 <cstdio>
17
18 #include "gtest/gtest.h"
19 #include "hilog/log.h"
20 #include "sensor_agent.h"
21 #include "sensor_agent_type.h"
22
23 #undef LOG_TAG
24 #define LOG_TAG "SENSOR_LITE"
25 #undef SENSOR_ERROR_INVALID_PARAM
26 #define SENSOR_ERROR_INVALID_PARAM (-3)
27
28 using namespace std;
29 using namespace testing::ext;
30
31 #define NULLPTR ((void *)0)
32
33 namespace OHOS {
34 class SensorAgentInterfacesTest : public testing::Test {
35 protected:
SetUpTestCase(void)36 static void SetUpTestCase(void) {}
TearDownTestCase(void)37 static void TearDownTestCase(void) {}
38 };
39
SensorDataCallbackImpl(SensorEvent * event)40 void SensorDataCallbackImpl(SensorEvent *event)
41 {
42 HILOG_DEBUG(HILOG_MODULE_APP, "SensorDataCallbackImpl called");
43 if (event == NULLPTR) {
44 return;
45 }
46 float *sensorData = (float *)event->data;
47 for (int32_t i = 0; i < (int32_t)(event->dataLen / sizeof(uint8_t *)); i++) {
48 HILOG_DEBUG(HILOG_MODULE_APP, "SensorDataCallbackImpl data: %f", *(sensorData + i));
49 }
50 }
51
52 /**
53 * @tc.number : SUB_DSS_SensorsSystem_lite_GetAllSensors_0010
54 * @tc.name : GetAllSensors Interface Test
55 * @tc.desc : Test the interface for obtaining the sensor list
56 */
57 HWTEST_F(SensorAgentInterfacesTest, GetAllSensorsInterfaceTest001, Function | MediumTest | Level0)
58 {
59 HILOG_INFO(HILOG_MODULE_APP, "GetAllSensorsInterfaceTest001 called");
60 SensorInfo *sensorInfo = (SensorInfo *)NULLPTR;
61 int32_t count = 0;
62 int32_t ret = GetAllSensors(&sensorInfo, &count);
63 HILOG_INFO(HILOG_MODULE_APP, "GetAllSensorsInterfaceTest001 sensor lists count: %d", count);
64 for (int i = 0; i < count; i++) {
65 HILOG_INFO(HILOG_MODULE_APP, "%s %s %s %s %d %d %f %f %f!",
66 (sensorInfo + i)->sensorName, (sensorInfo + i)->vendorName,
67 (sensorInfo + i)->firmwareVersion, (sensorInfo + i)->hardwareVersion,
68 (sensorInfo + i)->sensorTypeId, (sensorInfo + i)->sensorId,
69 (sensorInfo + i)->maxRange, (sensorInfo + i)->precision,
70 (sensorInfo + i)->power);
71 }
72 EXPECT_EQ(ret, 0) << "GetAllSensors ret = " << ret << endl;
73 };
74
75 /**
76 * @tc.number : SUB_DSS_SensorsSystem_lite_GetAllSensors_0020
77 * @tc.name : GetAllSensors Interface Test failed by count
78 * @tc.desc : Test the interface for obtaining the sensor list
79 */
80 HWTEST_F(SensorAgentInterfacesTest, GetAllSensorsInterfaceTest002, Function | MediumTest | 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.number : SUB_DSS_SensorsSystem_lite_GetAllSensors_0030
90 * @tc.name : GetAllSensors Interface Test failed by sensorinfo
91 * @tc.desc : Test the interface for obtaining the sensor list
92 */
93 HWTEST_F(SensorAgentInterfacesTest, GetAllSensorsInterfaceTest003, Function | MediumTest | Level2)
94 {
95 HILOG_INFO(HILOG_MODULE_APP, "GetAllSensorsInterfaceTest003 called");
96 int32_t count = 0;
97 SensorInfo **sensorInfo = (SensorInfo **)NULLPTR;
98 int32_t ret = GetAllSensors(sensorInfo, &count);
99 EXPECT_EQ(ret, SENSOR_ERROR_INVALID_PARAM) << "GetAllSensors ret = " << ret << endl;
100 };
101
102 /**
103 * @tc.number : SUB_DSS_SensorsSystem_lite_Enable_0010
104 * @tc.name : Activate Sensor Interface Test failed by sensorId
105 * @tc.desc : Test the enable sensor interface
106 */
107 HWTEST_F(SensorAgentInterfacesTest, ActivateSensorInterfaceTest001, Function | MediumTest | Level1)
108 {
109 HILOG_INFO(HILOG_MODULE_APP, "ActivateSensorInterfaceTest001 called");
110 SensorUser sensorUser;
111 int32_t ret = ActivateSensor(-1, &sensorUser);
112 EXPECT_EQ(ret, SENSOR_ERROR_INVALID_PARAM) << "ActivateSensor ret = " << ret << endl;
113 };
114
115 /**
116 * @tc.number : SUB_DSS_SensorsSystem_lite_Enable_0020
117 * @tc.name : Activate Sensor Interface Test failed by sensorUser
118 * @tc.desc : Test the enable sensor interface
119 */
120 HWTEST_F(SensorAgentInterfacesTest, ActivateSensorInterfaceTest002, Function | MediumTest | Level2)
121 {
122 HILOG_INFO(HILOG_MODULE_APP, "ActivateSensorInterfaceTest002 called");
123 SensorUser *sensorUser = (SensorUser *)NULLPTR;
124 int32_t ret = ActivateSensor(0, sensorUser);
125 EXPECT_EQ(ret, SENSOR_ERROR_INVALID_PARAM) << "ActivateSensor ret = " << ret << endl;
126 };
127
128 /**
129 * @tc.number : SUB_DSS_SensorsSystem_lite_Enable_0030
130 * @tc.name : enable Sensor Interface Test and disable
131 * @tc.desc : Test the enable sensor interface
132 */
133 HWTEST_F(SensorAgentInterfacesTest, ActivateSensorInterfaceTest003, Function | MediumTest | Level2)
134 {
135 HILOG_INFO(HILOG_MODULE_APP, "ActivateSensorInterfaceTest003 called");
136 SensorUser sensorUser;
137 sensorUser.callback = SensorDataCallbackImpl;
138 SensorInfo *sensorInfo = (SensorInfo *)NULLPTR;
139 int32_t count = 0;
140 int32_t ret = GetAllSensors(&sensorInfo, &count);
141 EXPECT_EQ(ret, 0) << "GetAllSensors ret = " << ret << endl;
142 sleep(1);
143
144 ret = ActivateSensor(0, &sensorUser);
145 EXPECT_EQ(ret, 0) << "ActivateSensor ret = " << ret << endl;
146 sleep(3);
147
148 ret = DeactivateSensor(0, &sensorUser);
149 EXPECT_EQ(ret, 0) << "DeactivateSensor ret = " << ret << endl;
150 sleep(1);
151 };
152
153 /**
154 * @tc.number : SUB_DSS_SensorsSystem_lite_SetBatch_0010
155 * @tc.name : SetBatch Interface Test failed
156 * @tc.desc : Set sensor param.
157 */
158 HWTEST_F(SensorAgentInterfacesTest, SetBatchInterfaceTest002, Function | MediumTest | Level1)
159 {
160 HILOG_INFO(HILOG_MODULE_APP, "SetBatchInterfaceTest002 called");
161 SensorUser sensorUser;
162 int32_t ret = SetBatch(0, &sensorUser, -1, 3000);
163 EXPECT_EQ(ret, SENSOR_ERROR_INVALID_PARAM) << "SetBatch ret = " << ret << endl;
164 };
165
166 /**
167 * @tc.number : SUB_DSS_SensorsSystem_lite_SetBatch_0020
168 * @tc.name : SetBatch Interface Test success
169 * @tc.desc : Set sensor param.
170 */
171 HWTEST_F(SensorAgentInterfacesTest, SetBatchInterfaceTest003, Function | MediumTest | Level2)
172 {
173 HILOG_INFO(HILOG_MODULE_APP, "SetBatchInterfaceTest003 called");
174 SensorUser sensorUser;
175 int32_t ret = SetBatch(0, &sensorUser, 200, 3000);
176 EXPECT_EQ(ret, 0) << "SetBatch ret = " << ret << endl;
177 };
178
179 /**
180 * @tc.number : SUB_DSS_SensorsSystem_lite_Register_0010
181 * @tc.name : SubscribeSensor Interface Test
182 * @tc.desc : SubscribeSensor sensor.
183 */
184 HWTEST_F(SensorAgentInterfacesTest, SubscribeSensorInterfaceTest001, Function | MediumTest | Level1)
185 {
186 HILOG_INFO(HILOG_MODULE_APP, "SubscribeSensorInterfaceTest001 called");
187 SensorInfo *sensorInfo = (SensorInfo *)NULLPTR;
188 int32_t count = 0;
189 int32_t ret = GetAllSensors(&sensorInfo, &count);
190 EXPECT_EQ(ret, 0) << "GetAllSensors ret = " << ret << endl;
191 sleep(1);
192
193 SensorUser sensorUser;
194 sensorUser.callback = SensorDataCallbackImpl;
195 ret = SubscribeSensor(0, &sensorUser);
196 EXPECT_EQ(ret, 0) << "SubscribeSensor ret = " << ret << endl;
197
198 ret = UnsubscribeSensor(0, &sensorUser);
199 EXPECT_EQ(ret, 0) << "UnsubscribeSensor ret = " << ret << endl;
200 };
201
202 /**
203 * @tc.number : SUB_DSS_SensorsSystem_lite_Register_0020
204 * @tc.name : UnSubscribeSensor Interface Test
205 * @tc.desc : UnSubscribeSensor sensor.
206 */
207 HWTEST_F(SensorAgentInterfacesTest, UnSubscribeSensorInterfaceTest001, Function | MediumTest | Level1)
208 {
209 HILOG_INFO(HILOG_MODULE_APP, "UnSubscribeSensorInterfaceTest001 called");
210 SensorUser *sensorUser = (SensorUser *)NULLPTR;
211 int32_t ret = UnsubscribeSensor(0, sensorUser);
212 EXPECT_EQ(ret, SENSOR_ERROR_INVALID_PARAM) << "UnsubscribeSensor ret = " << ret << endl;
213 };
214
215 /**
216 * @tc.number : SUB_DSS_SensorsSystem_lite_Register_0030
217 * @tc.name : DataReportInterfaceTest get register enable and disable sensor
218 * @tc.desc : Sensor data report.
219 */
220 HWTEST_F(SensorAgentInterfacesTest, DataReportInterfaceTest001, Function | MediumTest | Level3)
221 {
222 HILOG_INFO(HILOG_MODULE_APP, "DataReportInterfaceTest001 called");
223 SensorUser sensorUser;
224 sensorUser.callback = SensorDataCallbackImpl;
225
226 SensorInfo *sensorInfo = (SensorInfo *)NULLPTR;
227 int32_t count = 0;
228
229 int32_t ret = GetAllSensors(&sensorInfo, &count);
230 EXPECT_EQ(ret, 0) << "GetAllSensorsTest001 ret = " << ret << endl;
231 sleep(1);
232
233 ret = SubscribeSensor(0, &sensorUser);
234 EXPECT_EQ(ret, 0) << "SubscribeSensor ret = " << ret << endl;
235 sleep(1);
236
237 ret = ActivateSensor(0, &sensorUser);
238 EXPECT_EQ(ret, 0) << "ActivateSensor ret = " << ret << endl;
239 sleep(1);
240
241 ret = DeactivateSensor(0, &sensorUser);
242 EXPECT_EQ(ret, 0) << "DeactivateSensor ret = " << ret << endl;
243 sleep(1);
244
245 ret = UnsubscribeSensor(0, &sensorUser);
246 EXPECT_EQ(ret, 0) << "UnsubscribeSensor ret = " << ret << endl;
247 };
248 }