1 /*
2 * Copyright (c) 2022-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 <cmath>
17 #include <cstdio>
18 #include <unistd.h>
19 #include <gtest/gtest.h>
20 #include <securec.h>
21 #include <vector>
22 #include "hdf_base.h"
23 #include "hdf_log.h"
24 #include "osal_time.h"
25 #include "v1_1/imotion_interface.h"
26 #include "motion_callback_impl.h"
27
28 #define DATA_NUM 12
29 #define DATA_VALUE 6
30
31 using namespace OHOS::HDI::Motion::V1_1;
32 using namespace testing::ext;
33
34 namespace {
35 sptr<OHOS::HDI::Motion::V1_1::IMotionInterface> g_motionInterface = nullptr;
36 sptr<IMotionCallback> g_motionCallback = new MotionCallbackImpl();
37 sptr<IMotionCallback> g_motionCallbackUnregistered = new MotionCallbackImpl();
38 std::vector<uint8_t> g_motionConfigData(DATA_NUM, DATA_VALUE);
39 }
40
41 class HdfMotionTest : public testing::Test {
42 public:
43 static void SetUpTestCase();
44 static void TearDownTestCase();
45 void SetUp();
46 void TearDown();
47 };
48
SetUpTestCase()49 void HdfMotionTest::SetUpTestCase()
50 {
51 g_motionInterface = OHOS::HDI::Motion::V1_1::IMotionInterface::Get();
52 }
53
TearDownTestCase()54 void HdfMotionTest::TearDownTestCase()
55 {
56 }
57
SetUp()58 void HdfMotionTest::SetUp()
59 {
60 if (g_motionInterface == nullptr) {
61 printf("Motion is not supported ");
62 GTEST_SKIP() << "Device not exist" << std::endl;
63 return;
64 }
65 }
66
TearDown()67 void HdfMotionTest::TearDown()
68 {
69 }
70
71 HWTEST_F(HdfMotionTest, SUB_Driver_Sensor_HdiMotion_0100, Function | MediumTest | Level2)
72 {
73 ASSERT_NE(nullptr, g_motionInterface);
74 }
75
76 HWTEST_F(HdfMotionTest, SUB_Driver_Sensor_HdiMotion_0200, Function | MediumTest | Level2)
77 {
78 if (g_motionInterface == nullptr) {
79 ASSERT_NE(nullptr, g_motionInterface);
80 return;
81 }
82 int32_t ret = g_motionInterface->Register(g_motionCallback);
83 EXPECT_EQ(HDF_SUCCESS, ret);
84 ret = g_motionInterface->Unregister(g_motionCallback);
85 EXPECT_EQ(0, ret);
86 }
87
88 HWTEST_F(HdfMotionTest, SUB_Driver_Sensor_HdiMotion_0300, Function | MediumTest | Level2)
89 {
90 if (g_motionInterface == nullptr) {
91 ASSERT_NE(nullptr, g_motionInterface);
92 return;
93 }
94 int32_t ret = g_motionInterface->Register(g_motionCallback);
95 EXPECT_EQ(HDF_SUCCESS, ret);
96 ret = g_motionInterface->Register(g_motionCallback);
97 EXPECT_EQ(HDF_FAILURE, ret);
98 ret = g_motionInterface->Unregister(g_motionCallback);
99 EXPECT_EQ(HDF_SUCCESS, ret);
100 }
101
102 HWTEST_F(HdfMotionTest, SUB_Driver_Sensor_HdiMotion_0400, Function | MediumTest | Level2)
103 {
104 if (g_motionInterface == nullptr) {
105 ASSERT_NE(nullptr, g_motionInterface);
106 return;
107 }
108 sptr<IMotionCallback> motionCallback = nullptr;
109 int32_t ret = g_motionInterface->Register(motionCallback);
110 EXPECT_NE(HDF_SUCCESS, ret);
111 }
112
113 HWTEST_F(HdfMotionTest, SUB_Driver_Sensor_HdiMotion_0500, Function | MediumTest | Level2)
114 {
115 if (g_motionInterface == nullptr) {
116 ASSERT_NE(nullptr, g_motionInterface);
117 return;
118 }
119 int32_t ret = g_motionInterface->Unregister(g_motionCallbackUnregistered);
120 EXPECT_EQ(HDF_FAILURE, ret);
121 }
122
123 HWTEST_F(HdfMotionTest, SUB_Driver_Sensor_HdiMotion_0600, Function | MediumTest | Level2)
124 {
125 if (g_motionInterface == nullptr) {
126 ASSERT_NE(nullptr, g_motionInterface);
127 return;
128 }
129 sptr<IMotionCallback> motionCallback = nullptr;
130 int32_t ret = g_motionInterface->Unregister(motionCallback);
131 EXPECT_NE(HDF_SUCCESS, ret);
132 }
133
134 HWTEST_F(HdfMotionTest, SUB_Driver_Sensor_HdiMotion_0700, Function | MediumTest | Level2)
135 {
136 if (g_motionInterface == nullptr) {
137 ASSERT_NE(nullptr, g_motionInterface);
138 return;
139 }
140
141 vector<int32_t> vec;
142 vec.push_back(OHOS::HDI::Motion::V1_1::HDF_MOTION_TYPE_PICKUP);
143 vec.push_back(OHOS::HDI::Motion::V1_1::HDF_MOTION_TYPE_FLIP);
144 vec.push_back(OHOS::HDI::Motion::V1_1::HDF_MOTION_TYPE_SHAKE);
145 vec.push_back(OHOS::HDI::Motion::V1_1::HDF_MOTION_TYPE_ROTATION);
146
147 int32_t ret = g_motionInterface->Register(g_motionCallback);
148 EXPECT_EQ(HDF_SUCCESS, ret);
149
150 for (size_t i = 0; i < vec.size(); i++) {
151 ret = g_motionInterface->EnableMotion(vec[i]);
152 if (ret == HDF_SUCCESS) {
153 printf("Motion %d enabled successfully\n", vec[i]);
154 } else {
155 printf("Motion %d enable failed\n", vec[i]);
156 }
157 EXPECT_EQ(HDF_SUCCESS, ret);
158 OsalSleep(15);
159
160 ret = g_motionInterface->DisableMotion(vec[i]);
161 if (ret == HDF_SUCCESS) {
162 printf("Motion %d disabled successfully\n", vec[i]);
163 } else {
164 printf("Motion %d disable failed\n", vec[i]);
165 }
166 EXPECT_EQ(HDF_SUCCESS, ret);
167 OsalSleep(2);
168 }
169
170 ret = g_motionInterface->Unregister(g_motionCallback);
171 EXPECT_EQ(HDF_SUCCESS, ret);
172 }
173
174 HWTEST_F(HdfMotionTest, SUB_Driver_Sensor_HdiMotion_0800, Function | MediumTest | Level2)
175 {
176 if (g_motionInterface == nullptr) {
177 ASSERT_NE(nullptr, g_motionInterface);
178 return;
179 }
180 int32_t ret = g_motionInterface->EnableMotion(OHOS::HDI::Motion::V1_1::HDF_MOTION_TYPE_MAX);
181 EXPECT_NE(HDF_SUCCESS, ret);
182 OsalSleep(2);
183
184 ret = g_motionInterface->DisableMotion(OHOS::HDI::Motion::V1_1::HDF_MOTION_TYPE_MAX);
185 EXPECT_NE(HDF_SUCCESS, ret);
186 }
187
188 HWTEST_F(HdfMotionTest, SUB_Driver_Sensor_HdiMotion_0900, Function | MediumTest | Level2)
189 {
190 if (g_motionInterface == nullptr) {
191 ASSERT_NE(nullptr, g_motionInterface);
192 return;
193 }
194 int32_t ret = g_motionInterface->EnableMotion(OHOS::HDI::Motion::V1_1::HDF_MOTION_TYPE_WRIST_UP);
195 EXPECT_EQ(HDF_SUCCESS, ret);
196 OsalSleep(2);
197
198 ret = g_motionInterface->DisableMotion(OHOS::HDI::Motion::V1_1::HDF_MOTION_TYPE_WRIST_UP);
199 EXPECT_EQ(HDF_SUCCESS, ret);
200 }
201
202 HWTEST_F(HdfMotionTest, SUB_Driver_Sensor_HdiMotion_1000, Function | MediumTest | Level2)
203 {
204 if (g_motionInterface == nullptr) {
205 ASSERT_NE(nullptr, g_motionInterface);
206 return;
207 }
208 int32_t motionType = -1;
209 int32_t ret = g_motionInterface->EnableMotion(motionType);
210 EXPECT_NE(HDF_SUCCESS, ret);
211 OsalSleep(2);
212
213 ret = g_motionInterface->DisableMotion(motionType);
214 EXPECT_NE(HDF_SUCCESS, ret);
215 }
216
217 HWTEST_F(HdfMotionTest, SUB_Driver_Sensor_HdiMotion_1100, Function | MediumTest | Level2)
218 {
219 if (g_motionInterface == nullptr) {
220 ASSERT_NE(nullptr, g_motionInterface);
221 return;
222 }
223 int32_t ret = g_motionInterface->Register(g_motionCallback);
224 EXPECT_EQ(HDF_SUCCESS, ret);
225
226 ret = g_motionInterface->EnableMotion(OHOS::HDI::Motion::V1_1::HDF_MOTION_TYPE_PICKUP);
227 EXPECT_EQ(HDF_SUCCESS, ret);
228 ret = g_motionInterface->DisableMotion(OHOS::HDI::Motion::V1_1::HDF_MOTION_TYPE_PICKUP);
229 EXPECT_EQ(HDF_SUCCESS, ret);
230
231 ret = g_motionInterface->Unregister(g_motionCallback);
232 EXPECT_EQ(HDF_SUCCESS, ret);
233 }
234
235 HWTEST_F(HdfMotionTest, SUB_Driver_Sensor_HdiMotion_1200, Function | MediumTest | Level2)
236 {
237 if (g_motionInterface == nullptr) {
238 ASSERT_NE(nullptr, g_motionInterface);
239 return;
240 }
241 int32_t ret = g_motionInterface->DisableMotion(OHOS::HDI::Motion::V1_1::HDF_MOTION_TYPE_MAX);
242 EXPECT_EQ(HDF_ERR_INVALID_PARAM, ret);
243 }
244
245 HWTEST_F(HdfMotionTest, SUB_Driver_Sensor_HdiMotion_1300, Function | MediumTest | Level2)
246 {
247 if (g_motionInterface == nullptr) {
248 ASSERT_NE(nullptr, g_motionInterface);
249 return;
250 }
251 int32_t ret = g_motionInterface->DisableMotion(OHOS::HDI::Motion::V1_1::HDF_MOTION_TYPE_PICKUP);
252 EXPECT_EQ(HDF_SUCCESS, ret);
253 }
254
255 HWTEST_F(HdfMotionTest, SUB_Driver_Sensor_HdiMotion_1400, Function | MediumTest | Level2)
256 {
257 if (g_motionInterface == nullptr) {
258 ASSERT_NE(nullptr, g_motionInterface);
259 return;
260 }
261 int32_t motionType = -1;
262 int32_t ret = g_motionInterface->DisableMotion(motionType);
263 EXPECT_NE(HDF_SUCCESS, ret);
264 }
265
266 HWTEST_F(HdfMotionTest, SUB_Driver_Sensor_HdiMotion_1500, Function | MediumTest | Level2)
267 {
268 if (g_motionInterface == nullptr) {
269 ASSERT_NE(nullptr, g_motionInterface);
270 return;
271 }
272 int32_t motionType = -1;
273 int32_t ret = g_motionInterface->SetMotionConfig(motionType, g_motionConfigData);
274 EXPECT_NE(HDF_SUCCESS, ret);
275 }
276
277 HWTEST_F(HdfMotionTest, SUB_Driver_Sensor_HdiMotion_1600, TestSize.Level1)
278 {
279 if (g_motionInterface == nullptr) {
280 ASSERT_NE(nullptr, g_motionInterface);
281 return;
282 }
283 int32_t motionType = OHOS::HDI::Motion::V1_1::HDF_MOTION_TYPE_WRIST_DOWN;
284 int32_t ret = g_motionInterface->EnableMotion(motionType);
285 EXPECT_NE(HDF_SUCCESS, ret);
286 HDF_LOGI("EnableMotion %{public}d ret %{public}d\n", motionType, ret);
287
288 ret = g_motionInterface->DisableMotion(motionType);
289 EXPECT_NE(HDF_SUCCESS, ret);
290 HDF_LOGI("DisableMotion %{public}d ret %{public}d\n", motionType, ret);
291 }
292
293 HWTEST_F(HdfMotionTest, SUB_Driver_Sensor_HdiMotion_1700, TestSize.Level1)
294 {
295 if (g_motionInterface == nullptr) {
296 ASSERT_NE(nullptr, g_motionInterface);
297 return;
298 }
299 int32_t motionType = OHOS::HDI::Motion::V1_1::HDF_MOTION_TYPE_WAVE;
300 int32_t ret = g_motionInterface->EnableMotion(motionType);
301 EXPECT_EQ(HDF_SUCCESS, ret);
302 HDF_LOGI("EnableMotion %{public}d ret %{public}d\n", motionType, ret);
303
304 ret = g_motionInterface->DisableMotion(motionType);
305 EXPECT_EQ(HDF_SUCCESS, ret);
306 HDF_LOGI("DisableMotion %{public}d ret %{public}d\n", motionType, ret);
307 }
308
309 HWTEST_F(HdfMotionTest, SUB_Driver_Sensor_HdiMotion_1800, TestSize.Level1)
310 {
311 if (g_motionInterface == nullptr) {
312 ASSERT_NE(nullptr, g_motionInterface);
313 return;
314 }
315 int32_t motionType = OHOS::HDI::Motion::V1_1::HDF_MOTION_TYPE_STEP_COUNTER;
316 int32_t ret = g_motionInterface->EnableMotion(motionType);
317 EXPECT_EQ(HDF_SUCCESS, ret);
318 HDF_LOGI("EnableMotion %{public}d ret %{public}d\n", motionType, ret);
319
320 ret = g_motionInterface->DisableMotion(motionType);
321 EXPECT_EQ(HDF_SUCCESS, ret);
322 HDF_LOGI("DisableMotion %{public}d ret %{public}d\n", motionType, ret);
323 }
324
325 HWTEST_F(HdfMotionTest, SUB_Driver_Sensor_HdiMotion_1900, TestSize.Level1)
326 {
327 if (g_motionInterface == nullptr) {
328 ASSERT_NE(nullptr, g_motionInterface);
329 return;
330 }
331 int32_t motionType = OHOS::HDI::Motion::V1_1::HDF_MOTION_TYPE_TOUCH_LINK;
332 int32_t ret = g_motionInterface->EnableMotion(motionType);
333 EXPECT_NE(HDF_SUCCESS, ret);
334 HDF_LOGI("EnableMotion %{public}d ret %{public}d\n", motionType, ret);
335
336 ret = g_motionInterface->DisableMotion(motionType);
337 EXPECT_NE(HDF_SUCCESS, ret);
338 HDF_LOGI("DisableMotion %{public}d ret %{public}d\n", motionType, ret);
339 }
340
341 HWTEST_F(HdfMotionTest, SUB_Driver_Sensor_HdiMotion_2000, TestSize.Level1)
342 {
343 if (g_motionInterface == nullptr) {
344 ASSERT_NE(nullptr, g_motionInterface);
345 return;
346 }
347 int32_t motionType = OHOS::HDI::Motion::V1_1::HDF_MOTION_TYPE_HOVER;
348 int32_t ret = g_motionInterface->EnableMotion(motionType);
349 EXPECT_EQ(HDF_SUCCESS, ret);
350 HDF_LOGI("EnableMotion %{public}d ret %{public}d\n", motionType, ret);
351
352 ret = g_motionInterface->DisableMotion(motionType);
353 EXPECT_EQ(HDF_SUCCESS, ret);
354 HDF_LOGI("DisableMotion %{public}d ret %{public}d\n", motionType, ret);
355 }
356
357 HWTEST_F(HdfMotionTest, SUB_Driver_Sensor_HdiMotion_2100, TestSize.Level1)
358 {
359 if (g_motionInterface == nullptr) {
360 ASSERT_NE(nullptr, g_motionInterface);
361 return;
362 }
363 int32_t motionType = OHOS::HDI::Motion::V1_1::HDF_MOTION_TYPE_PUT_IN_POCKET;
364 int32_t ret = g_motionInterface->EnableMotion(motionType);
365 EXPECT_EQ(HDF_SUCCESS, ret);
366 HDF_LOGI("EnableMotion %{public}d ret %{public}d\n", motionType, ret);
367
368 ret = g_motionInterface->DisableMotion(motionType);
369 EXPECT_EQ(HDF_SUCCESS, ret);
370 HDF_LOGI("DisableMotion %{public}d ret %{public}d\n", motionType, ret);
371 }
372
373 HWTEST_F(HdfMotionTest, SUB_Driver_Sensor_HdiMotion_2200, TestSize.Level1)
374 {
375 if (g_motionInterface == nullptr) {
376 ASSERT_NE(nullptr, g_motionInterface);
377 return;
378 }
379 int32_t motionType = OHOS::HDI::Motion::V1_1::HDF_MOTION_TYPE_RESERVED;
380 int32_t ret = g_motionInterface->EnableMotion(motionType);
381 EXPECT_EQ(HDF_SUCCESS, ret);
382 HDF_LOGI("EnableMotion %{public}d ret %{public}d\n", motionType, ret);
383
384 ret = g_motionInterface->DisableMotion(motionType);
385 EXPECT_EQ(HDF_SUCCESS, ret);
386 HDF_LOGI("DisableMotion %{public}d ret %{public}d\n", motionType, ret);
387 }