1 /*
2 * Copyright (c) 2023-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 "hdf_base.h"
17 #include "motion_callback_impl.h"
18 #include "osal_time.h"
19 #include "v1_1/imotion_interface.h"
20 #include <cmath>
21 #include <cstdio>
22 #include <gtest/gtest.h>
23 #include <securec.h>
24 #include <unistd.h>
25 #include <vector>
26
27 #define DATA_NUM 12
28 #define DATA_VALUE 6
29
30 using namespace OHOS::HDI::Motion::V1_1;
31 using namespace testing::ext;
32
33 namespace {
34 sptr<OHOS::HDI::Motion::V1_1::IMotionInterface> g_motionInterface = nullptr;
35 sptr<IMotionCallback> g_motionCallback = new MotionCallbackImpl();
36 sptr<IMotionCallback> g_motionCallbackUnregistered = new MotionCallbackImpl();
37 std::vector<uint8_t> g_motionConfigData(DATA_NUM, DATA_VALUE);
38 } // namespace
39
40 class HdfMotionTestAdditional : public testing::Test {
41 public:
42 static void SetUpTestCase();
43 static void TearDownTestCase();
44 void SetUp();
45 void TearDown();
46 };
47
SetUpTestCase()48 void HdfMotionTestAdditional::SetUpTestCase() { g_motionInterface = OHOS::HDI::Motion::V1_1::IMotionInterface::Get(); }
49
TearDownTestCase()50 void HdfMotionTestAdditional::TearDownTestCase() {}
51
SetUp()52 void HdfMotionTestAdditional::SetUp()
53 {
54 if (g_motionInterface == nullptr) {
55 printf("Motion is not supported ");
56 GTEST_SKIP() << "Device not exist" << std::endl;
57 return;
58 }
59 }
60
TearDown()61 void HdfMotionTestAdditional::TearDown() {}
62
63 /**
64 * @tc.number : SUB_Driver_Motion_Hdienablemotion_0100
65 * @tc.name : testHdiEnableMotion001
66 * @tc.desc : Testing the effectiveness of the EnableMotion function
67 */
68 HWTEST_F(HdfMotionTestAdditional, testHdiEnableMotion001, Function | MediumTest | Level1)
69 {
70 if (g_motionInterface == nullptr) {
71 ASSERT_NE(nullptr, g_motionInterface);
72 return;
73 }
74 int32_t ret = g_motionInterface->EnableMotion(OHOS::HDI::Motion::V1_1::HDF_MOTION_CLOSE_TO_EAR);
75 EXPECT_EQ(HDF_SUCCESS, ret);
76 }
77
78 /**
79 * @tc.number : SUB_Driver_Motion_Hdienablemotion_0200
80 * @tc.name : testHdiEnableMotion002
81 * @tc.desc : Testing the effectiveness of the EnableMotion function
82 */
83 HWTEST_F(HdfMotionTestAdditional, testHdiEnableMotion002, Function | MediumTest | Level1)
84 {
85 if (g_motionInterface == nullptr) {
86 ASSERT_NE(nullptr, g_motionInterface);
87 return;
88 }
89 int32_t ret = g_motionInterface->EnableMotion(OHOS::HDI::Motion::V1_1::HDF_MOTION_TYPE_POCKET_MODE);
90 EXPECT_EQ(HDF_SUCCESS, ret);
91 }
92
93 /**
94 * @tc.number : SUB_Driver_Motion_Hdienablemotion_0300
95 * @tc.name : testHdiEnableMotion003
96 * @tc.desc : Testing the effectiveness of the EnableMotion function
97 */
98 HWTEST_F(HdfMotionTestAdditional, testHdiEnableMotion003, Function | MediumTest | Level1)
99 {
100 if (g_motionInterface == nullptr) {
101 ASSERT_NE(nullptr, g_motionInterface);
102 return;
103 }
104 int32_t ret = g_motionInterface->EnableMotion(OHOS::HDI::Motion::V1_1::HDF_MOTION_TYPE_LEAVE_EAR);
105 EXPECT_EQ(HDF_SUCCESS, ret);
106 }
107
108 /**
109 * @tc.number : SUB_Driver_Motion_Hdienablemotion_0400
110 * @tc.name : testHdiEnableMotion004
111 * @tc.desc : Testing the effectiveness of the EnableMotion function
112 */
113 HWTEST_F(HdfMotionTestAdditional, testHdiEnableMotion004, Function | MediumTest | Level1)
114 {
115 if (g_motionInterface == nullptr) {
116 ASSERT_NE(nullptr, g_motionInterface);
117 return;
118 }
119 int32_t ret = g_motionInterface->EnableMotion(OHOS::HDI::Motion::V1_1::HDF_MOTION_TYPE_WRIST_UP);
120 EXPECT_EQ(HDF_SUCCESS, ret);
121 }
122
123 /**
124 * @tc.number : SUB_Driver_Motion_Hdienablemotion_0600
125 * @tc.name : testHdiEnableMotion006
126 * @tc.desc : Testing the effectiveness of the EnableMotion function
127 */
128 HWTEST_F(HdfMotionTestAdditional, testHdiEnableMotion006, Function | MediumTest | Level1)
129 {
130 if (g_motionInterface == nullptr) {
131 ASSERT_NE(nullptr, g_motionInterface);
132 return;
133 }
134
135 int32_t ret = 0;
136 for (size_t i = 0; i < 1000; i++) {
137 ret = g_motionInterface->EnableMotion(OHOS::HDI::Motion::V1_1::HDF_MOTION_CLOSE_TO_EAR);
138 EXPECT_EQ(HDF_SUCCESS, ret);
139 }
140 }
141
142 /**
143 * @tc.number : SUB_Driver_Motion_Hdienablemotion_0700
144 * @tc.name : testHdiEnableMotion007
145 * @tc.desc : Testing the effectiveness of the EnableMotion function
146 */
147 HWTEST_F(HdfMotionTestAdditional, testHdiEnableMotion007, Function | MediumTest | Level2)
148 {
149 if (g_motionInterface == nullptr) {
150 ASSERT_NE(nullptr, g_motionInterface);
151 return;
152 }
153
154 int32_t ret = g_motionInterface->EnableMotion(OHOS::HDI::Motion::V1_1::HDF_MOTION_TYPE_MAX);
155 EXPECT_EQ(HDF_ERR_INVALID_PARAM, ret);
156 }
157
158 /**
159 * @tc.number : SUB_Driver_Motion_Hdienablemotion_0800
160 * @tc.name : testHdiEnableMotion008
161 * @tc.desc : Testing the effectiveness of the EnableMotion function
162 */
163 HWTEST_F(HdfMotionTestAdditional, testHdiEnableMotion008, Function | MediumTest | Level1)
164 {
165 if (g_motionInterface == nullptr) {
166 ASSERT_NE(nullptr, g_motionInterface);
167 return;
168 }
169
170 int32_t ret = g_motionInterface->EnableMotion(0);
171 EXPECT_EQ(HDF_SUCCESS, ret);
172 }
173
174 /**
175 * @tc.number : SUB_Driver_Motion_Hdienablemotion_0900
176 * @tc.name : testHdiEnableMotion009
177 * @tc.desc : Testing the effectiveness of the EnableMotion function
178 */
179 HWTEST_F(HdfMotionTestAdditional, testHdiEnableMotion009, Function | MediumTest | Level2)
180 {
181 if (g_motionInterface == nullptr) {
182 ASSERT_NE(nullptr, g_motionInterface);
183 return;
184 }
185
186 int32_t ret = g_motionInterface->EnableMotion(-1);
187 EXPECT_EQ(HDF_ERR_INVALID_PARAM, ret);
188 }
189
190 /**
191 * @tc.number : SUB_Driver_Motion_Hdidisablemotion_0100
192 * @tc.name : testHdiDisableMotion001
193 * @tc.desc : Testing the effectiveness of the DisableMotion function
194 */
195 HWTEST_F(HdfMotionTestAdditional, testHdiDisableMotion001, Function | MediumTest | Level1)
196 {
197 if (g_motionInterface == nullptr) {
198 ASSERT_NE(nullptr, g_motionInterface);
199 return;
200 }
201
202 int32_t ret = g_motionInterface->DisableMotion(OHOS::HDI::Motion::V1_1::HDF_MOTION_CLOSE_TO_EAR);
203 EXPECT_EQ(HDF_SUCCESS, ret);
204 }
205
206 /**
207 * @tc.number : SUB_Driver_Motion_Hdidisablemotion_0200
208 * @tc.name : testHdiDisableMotion002
209 * @tc.desc : Testing the effectiveness of the DisableMotion function
210 */
211 HWTEST_F(HdfMotionTestAdditional, testHdiDisableMotion002, Function | MediumTest | Level1)
212 {
213 if (g_motionInterface == nullptr) {
214 ASSERT_NE(nullptr, g_motionInterface);
215 return;
216 }
217
218 int32_t ret = g_motionInterface->DisableMotion(OHOS::HDI::Motion::V1_1::HDF_MOTION_TYPE_POCKET_MODE);
219 EXPECT_EQ(HDF_SUCCESS, ret);
220 }
221
222 /**
223 * @tc.number : SUB_Driver_Motion_Hdidisablemotion_0300
224 * @tc.name : testHdiDisableMotion003
225 * @tc.desc : Testing the effectiveness of the DisableMotion function
226 */
227 HWTEST_F(HdfMotionTestAdditional, testHdiDisableMotion003, Function | MediumTest | Level1)
228 {
229 if (g_motionInterface == nullptr) {
230 ASSERT_NE(nullptr, g_motionInterface);
231 return;
232 }
233
234 int32_t ret = g_motionInterface->DisableMotion(OHOS::HDI::Motion::V1_1::HDF_MOTION_TYPE_LEAVE_EAR);
235 EXPECT_EQ(HDF_SUCCESS, ret);
236 }
237
238 /**
239 * @tc.number : SUB_Driver_Motion_Hdidisablemotion_0400
240 * @tc.name : testHdiDisableMotion004
241 * @tc.desc : Testing the effectiveness of the DisableMotion function
242 */
243 HWTEST_F(HdfMotionTestAdditional, testHdiDisableMotion004, Function | MediumTest | Level1)
244 {
245 if (g_motionInterface == nullptr) {
246 ASSERT_NE(nullptr, g_motionInterface);
247 return;
248 }
249
250 int32_t ret = g_motionInterface->DisableMotion(OHOS::HDI::Motion::V1_1::HDF_MOTION_TYPE_WRIST_UP);
251 EXPECT_EQ(HDF_SUCCESS, ret);
252 }
253
254 /**
255 * @tc.number : SUB_Driver_Motion_Hdidisablemotion_0600
256 * @tc.name : testHdiDisableMotion006
257 * @tc.desc : Testing the effectiveness of the DisableMotion function
258 */
259 HWTEST_F(HdfMotionTestAdditional, testHdiDisableMotion006, Function | MediumTest | Level1)
260 {
261 if (g_motionInterface == nullptr) {
262 ASSERT_NE(nullptr, g_motionInterface);
263 return;
264 }
265
266 int32_t ret = 0;
267 for (size_t i = 0; i < 1000; i++) {
268 ret = g_motionInterface->DisableMotion(OHOS::HDI::Motion::V1_1::HDF_MOTION_CLOSE_TO_EAR);
269 EXPECT_EQ(HDF_SUCCESS, ret);
270 }
271 }
272
273 /**
274 * @tc.number : SUB_Driver_Motion_Hdidisablemotion_0700
275 * @tc.name : testHdiDisableMotion007
276 * @tc.desc : Testing the effectiveness of the DisableMotion function
277 */
278 HWTEST_F(HdfMotionTestAdditional, testHdiDisableMotion007, Function | MediumTest | Level2)
279 {
280 if (g_motionInterface == nullptr) {
281 ASSERT_NE(nullptr, g_motionInterface);
282 return;
283 }
284
285 int32_t ret = g_motionInterface->DisableMotion(OHOS::HDI::Motion::V1_1::HDF_MOTION_TYPE_MAX);
286 EXPECT_EQ(HDF_ERR_INVALID_PARAM, ret);
287 }
288
289 /**
290 * @tc.number : SUB_Driver_Motion_Hdidisablemotion_0800
291 * @tc.name : testHdiDisableMotion008
292 * @tc.desc : Testing the effectiveness of the DisableMotion function
293 */
294 HWTEST_F(HdfMotionTestAdditional, testHdiDisableMotion008, Function | MediumTest | Level1)
295 {
296 if (g_motionInterface == nullptr) {
297 ASSERT_NE(nullptr, g_motionInterface);
298 return;
299 }
300
301 int32_t ret = g_motionInterface->DisableMotion(0);
302 EXPECT_EQ(HDF_SUCCESS, ret);
303 }
304
305 /**
306 * @tc.number : SUB_Driver_Motion_Hdidisablemotion_0900
307 * @tc.name : testHdiDisableMotion009
308 * @tc.desc : Testing the effectiveness of the DisableMotion function
309 */
310 HWTEST_F(HdfMotionTestAdditional, testHdiDisableMotion009, Function | MediumTest | Level2)
311 {
312 if (g_motionInterface == nullptr) {
313 ASSERT_NE(nullptr, g_motionInterface);
314 return;
315 }
316
317 int32_t ret = g_motionInterface->DisableMotion(-1);
318 EXPECT_EQ(HDF_ERR_INVALID_PARAM, ret);
319 }
320
321 /**
322 * @tc.number : SUB_Driver_Motion_Hdiregister_0100
323 * @tc.name : testHdiRegister001
324 * @tc.desc : Testing the effectiveness of the Register function
325 */
326 HWTEST_F(HdfMotionTestAdditional, testHdiRegister001, Function | MediumTest | Level2)
327 {
328 if (g_motionInterface == nullptr) {
329 ASSERT_NE(nullptr, g_motionInterface);
330 return;
331 }
332
333 int32_t ret = g_motionInterface->Register(g_motionCallback);
334 EXPECT_EQ(HDF_SUCCESS, ret);
335
336 ret = g_motionInterface->Register(g_motionCallback);
337 EXPECT_EQ(HDF_FAILURE, ret);
338 g_motionInterface->Unregister(g_motionCallback);
339 }
340
341 /**
342 * @tc.number : SUB_Driver_Motion_Hdiregister_0200
343 * @tc.name : testHdiRegister002
344 * @tc.desc : Testing the effectiveness of the Register function
345 */
346 HWTEST_F(HdfMotionTestAdditional, testHdiRegister002, Function | MediumTest | Level1)
347 {
348 if (g_motionInterface == nullptr) {
349 ASSERT_NE(nullptr, g_motionInterface);
350 return;
351 }
352
353 int32_t ret = 0;
354 for (size_t i = 0; i < 1000; i++) {
355 ret = g_motionInterface->Register(g_motionCallback);
356 EXPECT_EQ(HDF_SUCCESS, ret);
357 OsalMSleep(50);
358 ret = g_motionInterface->Unregister(g_motionCallback);
359 EXPECT_EQ(HDF_SUCCESS, ret);
360 OsalMSleep(50);
361 }
362 }
363
364 /**
365 * @tc.number : SUB_Driver_Motion_Hdiunregister_0100
366 * @tc.name : testHdiUnregister001
367 * @tc.desc : Testing the effectiveness of the Unregister function
368 */
369 HWTEST_F(HdfMotionTestAdditional, testHdiUnregister001, Function | MediumTest | Level1)
370 {
371 if (g_motionInterface == nullptr) {
372 ASSERT_NE(nullptr, g_motionInterface);
373 return;
374 }
375
376 int32_t ret = g_motionInterface->Register(g_motionCallback);
377 EXPECT_EQ(HDF_SUCCESS, ret);
378
379 ret = g_motionInterface->Unregister(g_motionCallback);
380 EXPECT_EQ(HDF_SUCCESS, ret);
381 }
382
383 /**
384 * @tc.number : SUB_Driver_Motion_Hdiunregister_0200
385 * @tc.name : testHdiUnregister002
386 * @tc.desc : Testing the effectiveness of the Unregister function
387 */
388 HWTEST_F(HdfMotionTestAdditional, testHdiUnregister002, Function | MediumTest | Level2)
389 {
390 if (g_motionInterface == nullptr) {
391 ASSERT_NE(nullptr, g_motionInterface);
392 return;
393 }
394
395 int32_t ret = g_motionInterface->Register(g_motionCallback);
396 EXPECT_EQ(HDF_SUCCESS, ret);
397
398 ret = g_motionInterface->Unregister(g_motionCallback);
399 EXPECT_EQ(HDF_SUCCESS, ret);
400
401 ret = g_motionInterface->Unregister(g_motionCallback);
402 EXPECT_EQ(HDF_FAILURE, ret);
403 }
404
405 /**
406 * @tc.number : SUB_Driver_Motion_Hdisetmotionconfig_0100
407 * @tc.name : testHdiSetMotionConfig001
408 * @tc.desc : Testing the effectiveness of the SetMotionConfig function
409 */
410 HWTEST_F(HdfMotionTestAdditional, testHdiSetMotionConfig001, Function | MediumTest | Level1)
411 {
412 if (g_motionInterface == nullptr) {
413 ASSERT_NE(nullptr, g_motionInterface);
414 return;
415 }
416 int32_t motionType = OHOS::HDI::Motion::V1_1::HDF_MOTION_TYPE_PICKUP;
417 int32_t ret = g_motionInterface->SetMotionConfig(motionType, g_motionConfigData);
418 EXPECT_EQ(HDF_SUCCESS, ret);
419 }
420
421 /**
422 * @tc.number : SUB_Driver_Motion_Hdisetmotionconfig_0200
423 * @tc.name : testHdiSetMotionConfig002
424 * @tc.desc : Testing the effectiveness of the SetMotionConfig function
425 */
426 HWTEST_F(HdfMotionTestAdditional, testHdiSetMotionConfig002, Function | MediumTest | Level1)
427 {
428 if (g_motionInterface == nullptr) {
429 ASSERT_NE(nullptr, g_motionInterface);
430 return;
431 }
432 int32_t motionType = OHOS::HDI::Motion::V1_1::HDF_MOTION_TYPE_FLIP;
433 int32_t ret = g_motionInterface->SetMotionConfig(motionType, g_motionConfigData);
434 EXPECT_EQ(HDF_SUCCESS, ret);
435 }
436
437 /**
438 * @tc.number : SUB_Driver_Motion_Hdisetmotionconfig_0300
439 * @tc.name : testHdiSetMotionConfig003
440 * @tc.desc : Testing the effectiveness of the SetMotionConfig function
441 */
442 HWTEST_F(HdfMotionTestAdditional, testHdiSetMotionConfig003, Function | MediumTest | Level1)
443 {
444 if (g_motionInterface == nullptr) {
445 ASSERT_NE(nullptr, g_motionInterface);
446 return;
447 }
448 int32_t motionType = OHOS::HDI::Motion::V1_1::HDF_MOTION_CLOSE_TO_EAR;
449 int32_t ret = g_motionInterface->SetMotionConfig(motionType, g_motionConfigData);
450 EXPECT_EQ(HDF_SUCCESS, ret);
451 }
452
453 /**
454 * @tc.number : SUB_Driver_Motion_Hdisetmotionconfig_0400
455 * @tc.name : testHdiSetMotionConfig004
456 * @tc.desc : Testing the effectiveness of the SetMotionConfig function
457 */
458 HWTEST_F(HdfMotionTestAdditional, testHdiSetMotionConfig004, Function | MediumTest | Level1)
459 {
460 if (g_motionInterface == nullptr) {
461 ASSERT_NE(nullptr, g_motionInterface);
462 return;
463 }
464 int32_t motionType = OHOS::HDI::Motion::V1_1::HDF_MOTION_TYPE_SHAKE;
465 int32_t ret = g_motionInterface->SetMotionConfig(motionType, g_motionConfigData);
466 EXPECT_EQ(HDF_SUCCESS, ret);
467 }
468
469 /**
470 * @tc.number : SUB_Driver_Motion_Hdisetmotionconfig_0500
471 * @tc.name : testHdiSetMotionConfig005
472 * @tc.desc : Testing the effectiveness of the SetMotionConfig function
473 */
474 HWTEST_F(HdfMotionTestAdditional, testHdiSetMotionConfig005, Function | MediumTest | Level1)
475 {
476 if (g_motionInterface == nullptr) {
477 ASSERT_NE(nullptr, g_motionInterface);
478 return;
479 }
480 int32_t motionType = OHOS::HDI::Motion::V1_1::HDF_MOTION_TYPE_ROTATION;
481 int32_t ret = g_motionInterface->SetMotionConfig(motionType, g_motionConfigData);
482 EXPECT_EQ(HDF_SUCCESS, ret);
483 }
484
485 /**
486 * @tc.number : SUB_Driver_Motion_Hdisetmotionconfig_0600
487 * @tc.name : testHdiSetMotionConfig006
488 * @tc.desc : Testing the effectiveness of the SetMotionConfig function
489 */
490 HWTEST_F(HdfMotionTestAdditional, testHdiSetMotionConfig006, Function | MediumTest | Level1)
491 {
492 if (g_motionInterface == nullptr) {
493 ASSERT_NE(nullptr, g_motionInterface);
494 return;
495 }
496 int32_t motionType = OHOS::HDI::Motion::V1_1::HDF_MOTION_TYPE_POCKET_MODE;
497 int32_t ret = g_motionInterface->SetMotionConfig(motionType, g_motionConfigData);
498 EXPECT_EQ(HDF_SUCCESS, ret);
499 }
500
501 /**
502 * @tc.number : SUB_Driver_Motion_Hdisetmotionconfig_0700
503 * @tc.name : testHdiSetMotionConfig007
504 * @tc.desc : Testing the effectiveness of the SetMotionConfig function
505 */
506 HWTEST_F(HdfMotionTestAdditional, testHdiSetMotionConfig007, Function | MediumTest | Level1)
507 {
508 if (g_motionInterface == nullptr) {
509 ASSERT_NE(nullptr, g_motionInterface);
510 return;
511 }
512 int32_t motionType = OHOS::HDI::Motion::V1_1::HDF_MOTION_TYPE_LEAVE_EAR;
513 int32_t ret = g_motionInterface->SetMotionConfig(motionType, g_motionConfigData);
514 EXPECT_EQ(HDF_SUCCESS, ret);
515 }
516
517 /**
518 * @tc.number : SUB_Driver_Motion_Hdisetmotionconfig_0900
519 * @tc.name : testHdiSetMotionConfig009
520 * @tc.desc : Testing the effectiveness of the SetMotionConfig function
521 */
522 HWTEST_F(HdfMotionTestAdditional, testHdiSetMotionConfig009, Function | MediumTest | Level2)
523 {
524 if (g_motionInterface == nullptr) {
525 ASSERT_NE(nullptr, g_motionInterface);
526 return;
527 }
528 int32_t motionType = OHOS::HDI::Motion::V1_1::HDF_MOTION_TYPE_MAX;
529 int32_t ret = g_motionInterface->SetMotionConfig(motionType, g_motionConfigData);
530 EXPECT_EQ(HDF_ERR_INVALID_PARAM, ret);
531 }
532