• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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     }
61 
TearDown()62     void HdfMotionTest::TearDown()
63     {
64     }
65 
66     HWTEST_F(HdfMotionTest, GetMotionClient_001, TestSize.Level1)
67     {
68         ASSERT_NE(nullptr, g_motionInterface);
69     }
70 
71     HWTEST_F(HdfMotionTest, RegisterMotionDataCb_001, TestSize.Level1)
72     {
73         if (g_motionInterface == nullptr) {
74             ASSERT_NE(nullptr, g_motionInterface);
75             return;
76         }
77         int32_t ret = g_motionInterface->Register(g_motionCallback);
78         EXPECT_EQ(HDF_SUCCESS, ret);
79         ret = g_motionInterface->Unregister(g_motionCallback);
80         EXPECT_EQ(0, ret);
81     }
82 
83     HWTEST_F(HdfMotionTest, RegisterMotionDataCb_002, TestSize.Level1)
84     {
85         if (g_motionInterface == nullptr) {
86             ASSERT_NE(nullptr, g_motionInterface);
87             return;
88         }
89         int32_t ret = g_motionInterface->Register(g_motionCallback);
90         EXPECT_EQ(HDF_SUCCESS, ret);
91         ret = g_motionInterface->Register(g_motionCallback);
92         EXPECT_EQ(HDF_FAILURE, ret);
93         ret = g_motionInterface->Unregister(g_motionCallback);
94         EXPECT_EQ(HDF_SUCCESS, ret);
95     }
96 
97     HWTEST_F(HdfMotionTest, RegisterMotionDataCb_003, TestSize.Level1)
98     {
99         if (g_motionInterface == nullptr) {
100             ASSERT_NE(nullptr, g_motionInterface);
101             return;
102         }
103         sptr<IMotionCallback> motionCallback = nullptr;
104         int32_t ret = g_motionInterface->Register(motionCallback);
105         EXPECT_NE(HDF_SUCCESS, ret);
106     }
107 
108     HWTEST_F(HdfMotionTest, RegisterMotionDataCb_004, TestSize.Level1)
109     {
110         if (g_motionInterface == nullptr) {
111             ASSERT_NE(nullptr, g_motionInterface);
112             return;
113         }
114         int32_t ret = g_motionInterface->Unregister(g_motionCallbackUnregistered);
115         EXPECT_EQ(HDF_FAILURE, ret);
116     }
117 
118     HWTEST_F(HdfMotionTest, RegisterMotionDataCb_005, TestSize.Level1)
119     {
120         if (g_motionInterface == nullptr) {
121             ASSERT_NE(nullptr, g_motionInterface);
122             return;
123         }
124         sptr<IMotionCallback> motionCallback = nullptr;
125         int32_t ret = g_motionInterface->Unregister(motionCallback);
126         EXPECT_NE(HDF_SUCCESS, ret);
127     }
128 
129     HWTEST_F(HdfMotionTest, EnableMotion_001, TestSize.Level1)
130     {
131         if (g_motionInterface == nullptr) {
132             ASSERT_NE(nullptr, g_motionInterface);
133             return;
134         }
135 
136         vector<int32_t> vec;
137         vec.push_back(OHOS::HDI::Motion::V1_1::HDF_MOTION_TYPE_PICKUP);
138         vec.push_back(OHOS::HDI::Motion::V1_1::HDF_MOTION_TYPE_FLIP);
139         vec.push_back(OHOS::HDI::Motion::V1_1::HDF_MOTION_TYPE_SHAKE);
140         vec.push_back(OHOS::HDI::Motion::V1_1::HDF_MOTION_TYPE_ROTATION);
141 
142         int32_t ret = g_motionInterface->Register(g_motionCallback);
143         EXPECT_EQ(HDF_SUCCESS, ret);
144 
145         for (size_t i = 0; i < vec.size(); i++) {
146             ret = g_motionInterface->EnableMotion(vec[i]);
147             if (ret == HDF_SUCCESS) {
148                 printf("Motion %d enabled successfully\n", vec[i]);
149             } else {
150                 printf("Motion %d enable failed\n", vec[i]);
151             }
152             EXPECT_EQ(HDF_SUCCESS, ret);
153             OsalSleep(15);
154 
155             ret = g_motionInterface->DisableMotion(vec[i]);
156             if (ret == HDF_SUCCESS) {
157                 printf("Motion %d disabled successfully\n", vec[i]);
158             } else {
159                 printf("Motion %d disable failed\n", vec[i]);
160             }
161             EXPECT_EQ(HDF_SUCCESS, ret);
162             OsalSleep(2);
163         }
164 
165         ret = g_motionInterface->Unregister(g_motionCallback);
166         EXPECT_EQ(HDF_SUCCESS, ret);
167     }
168 
169     HWTEST_F(HdfMotionTest, EnableMotion_002, TestSize.Level1)
170     {
171         if (g_motionInterface == nullptr) {
172             ASSERT_NE(nullptr, g_motionInterface);
173             return;
174         }
175         int32_t ret = g_motionInterface->EnableMotion(OHOS::HDI::Motion::V1_1::HDF_MOTION_TYPE_MAX);
176         EXPECT_NE(HDF_SUCCESS, ret);
177         OsalSleep(2);
178 
179         ret = g_motionInterface->DisableMotion(OHOS::HDI::Motion::V1_1::HDF_MOTION_TYPE_MAX);
180         EXPECT_NE(HDF_SUCCESS, ret);
181     }
182 
183     HWTEST_F(HdfMotionTest, EnableMotion_003, TestSize.Level1)
184     {
185         if (g_motionInterface == nullptr) {
186             ASSERT_NE(nullptr, g_motionInterface);
187             return;
188         }
189         int32_t ret = g_motionInterface->EnableMotion(OHOS::HDI::Motion::V1_1::HDF_MOTION_TYPE_WRIST_UP);
190         EXPECT_EQ(HDF_SUCCESS, ret);
191         OsalSleep(2);
192 
193         ret = g_motionInterface->DisableMotion(OHOS::HDI::Motion::V1_1::HDF_MOTION_TYPE_WRIST_UP);
194         EXPECT_EQ(HDF_SUCCESS, ret);
195     }
196 
197     HWTEST_F(HdfMotionTest, EnableMotion_004, TestSize.Level1)
198     {
199         if (g_motionInterface == nullptr) {
200             ASSERT_NE(nullptr, g_motionInterface);
201             return;
202         }
203         int32_t motionType = -1;
204         int32_t ret = g_motionInterface->EnableMotion(motionType);
205         EXPECT_NE(HDF_SUCCESS, ret);
206         OsalSleep(2);
207 
208         ret = g_motionInterface->DisableMotion(motionType);
209         EXPECT_NE(HDF_SUCCESS, ret);
210     }
211 
212     HWTEST_F(HdfMotionTest, DisableMotion_001, TestSize.Level1)
213     {
214         if (g_motionInterface == nullptr) {
215             ASSERT_NE(nullptr, g_motionInterface);
216             return;
217         }
218         int32_t ret = g_motionInterface->Register(g_motionCallback);
219         EXPECT_EQ(HDF_SUCCESS, ret);
220 
221         ret = g_motionInterface->EnableMotion(OHOS::HDI::Motion::V1_1::HDF_MOTION_TYPE_PICKUP);
222         EXPECT_EQ(HDF_SUCCESS, ret);
223         ret = g_motionInterface->DisableMotion(OHOS::HDI::Motion::V1_1::HDF_MOTION_TYPE_PICKUP);
224         EXPECT_EQ(HDF_SUCCESS, ret);
225 
226         ret = g_motionInterface->Unregister(g_motionCallback);
227         EXPECT_EQ(HDF_SUCCESS, ret);
228     }
229 
230     HWTEST_F(HdfMotionTest, DisableMotion_002, TestSize.Level1)
231     {
232         if (g_motionInterface == nullptr) {
233             ASSERT_NE(nullptr, g_motionInterface);
234             return;
235         }
236         int32_t ret = g_motionInterface->DisableMotion(OHOS::HDI::Motion::V1_1::HDF_MOTION_TYPE_MAX);
237         EXPECT_EQ(HDF_ERR_INVALID_PARAM, ret);
238     }
239 
240     HWTEST_F(HdfMotionTest, DisableMotion_003, TestSize.Level1)
241     {
242         if (g_motionInterface == nullptr) {
243             ASSERT_NE(nullptr, g_motionInterface);
244             return;
245         }
246         int32_t ret = g_motionInterface->DisableMotion(OHOS::HDI::Motion::V1_1::HDF_MOTION_TYPE_PICKUP);
247         EXPECT_EQ(HDF_SUCCESS, ret);
248     }
249 
250     HWTEST_F(HdfMotionTest, DisableMotion_004, TestSize.Level1)
251     {
252         if (g_motionInterface == nullptr) {
253             ASSERT_NE(nullptr, g_motionInterface);
254             return;
255         }
256         int32_t motionType = -1;
257         int32_t ret = g_motionInterface->DisableMotion(motionType);
258         EXPECT_NE(HDF_SUCCESS, ret);
259     }
260 
261     HWTEST_F(HdfMotionTest, SetMotionConfig_001, TestSize.Level1)
262     {
263         if (g_motionInterface == nullptr) {
264             ASSERT_NE(nullptr, g_motionInterface);
265             return;
266         }
267         int32_t motionType = -1;
268         int32_t ret = g_motionInterface->SetMotionConfig(motionType, g_motionConfigData);
269         EXPECT_NE(HDF_SUCCESS, ret);
270     }
271 
272     HWTEST_F(HdfMotionTest, EnableMotion_TYPE_WRIST_DOWN, TestSize.Level1)
273     {
274         if (g_motionInterface == nullptr) {
275             ASSERT_NE(nullptr, g_motionInterface);
276             return;
277         }
278         int32_t motionType = OHOS::HDI::Motion::V1_1::HDF_MOTION_TYPE_WRIST_DOWN;
279         int32_t ret = g_motionInterface->EnableMotion(motionType);
280         EXPECT_NE(HDF_SUCCESS, ret);
281         HDF_LOGI("EnableMotion %{public}d ret %{public}d\n", motionType, ret);
282 
283         ret = g_motionInterface->DisableMotion(motionType);
284         EXPECT_NE(HDF_SUCCESS, ret);
285         HDF_LOGI("DisableMotion %{public}d ret %{public}d\n", motionType, ret);
286     }
287 
288     HWTEST_F(HdfMotionTest, EnableMotion_TYPE_WAVE, TestSize.Level1)
289     {
290         if (g_motionInterface == nullptr) {
291             ASSERT_NE(nullptr, g_motionInterface);
292             return;
293         }
294         int32_t motionType = OHOS::HDI::Motion::V1_1::HDF_MOTION_TYPE_WAVE;
295         int32_t ret = g_motionInterface->EnableMotion(motionType);
296         EXPECT_EQ(HDF_SUCCESS, ret);
297         HDF_LOGI("EnableMotion %{public}d ret %{public}d\n", motionType, ret);
298 
299         ret = g_motionInterface->DisableMotion(motionType);
300         EXPECT_EQ(HDF_SUCCESS, ret);
301         HDF_LOGI("DisableMotion %{public}d ret %{public}d\n", motionType, ret);
302     }
303 
304     HWTEST_F(HdfMotionTest, EnableMotion_TYPE_STEP_COUNTER, TestSize.Level1)
305     {
306         if (g_motionInterface == nullptr) {
307             ASSERT_NE(nullptr, g_motionInterface);
308             return;
309         }
310         int32_t motionType = OHOS::HDI::Motion::V1_1::HDF_MOTION_TYPE_STEP_COUNTER;
311         int32_t ret = g_motionInterface->EnableMotion(motionType);
312         EXPECT_EQ(HDF_SUCCESS, ret);
313         HDF_LOGI("EnableMotion %{public}d ret %{public}d\n", motionType, ret);
314 
315         ret = g_motionInterface->DisableMotion(motionType);
316         EXPECT_EQ(HDF_SUCCESS, ret);
317         HDF_LOGI("DisableMotion %{public}d ret %{public}d\n", motionType, ret);
318     }
319 
320     HWTEST_F(HdfMotionTest, EnableMotion_TYPE_TOUCH_LINK, TestSize.Level1)
321     {
322         if (g_motionInterface == nullptr) {
323             ASSERT_NE(nullptr, g_motionInterface);
324             return;
325         }
326         int32_t motionType = OHOS::HDI::Motion::V1_1::HDF_MOTION_TYPE_TOUCH_LINK;
327         int32_t ret = g_motionInterface->EnableMotion(motionType);
328         EXPECT_NE(HDF_SUCCESS, ret);
329         HDF_LOGI("EnableMotion %{public}d ret %{public}d\n", motionType, ret);
330 
331         ret = g_motionInterface->DisableMotion(motionType);
332         EXPECT_NE(HDF_SUCCESS, ret);
333         HDF_LOGI("DisableMotion %{public}d ret %{public}d\n", motionType, ret);
334     }
335 
336     HWTEST_F(HdfMotionTest, EnableMotion_TYPE_HOVER, TestSize.Level1)
337     {
338         if (g_motionInterface == nullptr) {
339             ASSERT_NE(nullptr, g_motionInterface);
340             return;
341         }
342         int32_t motionType = OHOS::HDI::Motion::V1_1::HDF_MOTION_TYPE_HOVER;
343         int32_t ret = g_motionInterface->EnableMotion(motionType);
344         EXPECT_EQ(HDF_SUCCESS, ret);
345         HDF_LOGI("EnableMotion %{public}d ret %{public}d\n", motionType, ret);
346 
347         ret = g_motionInterface->DisableMotion(motionType);
348         EXPECT_EQ(HDF_SUCCESS, ret);
349         HDF_LOGI("DisableMotion %{public}d ret %{public}d\n", motionType, ret);
350     }
351 
352     HWTEST_F(HdfMotionTest, EnableMotion_TYPE_PUT_IN_POCKET, TestSize.Level1)
353     {
354         if (g_motionInterface == nullptr) {
355             ASSERT_NE(nullptr, g_motionInterface);
356             return;
357         }
358         int32_t motionType = OHOS::HDI::Motion::V1_1::HDF_MOTION_TYPE_PUT_IN_POCKET;
359         int32_t ret = g_motionInterface->EnableMotion(motionType);
360         EXPECT_EQ(HDF_SUCCESS, ret);
361         HDF_LOGI("EnableMotion %{public}d ret %{public}d\n", motionType, ret);
362 
363         ret = g_motionInterface->DisableMotion(motionType);
364         EXPECT_EQ(HDF_SUCCESS, ret);
365         HDF_LOGI("DisableMotion %{public}d ret %{public}d\n", motionType, ret);
366     }
367 
368     HWTEST_F(HdfMotionTest, EnableMotion_TYPE_RESERVED, TestSize.Level1)
369     {
370         if (g_motionInterface == nullptr) {
371             ASSERT_NE(nullptr, g_motionInterface);
372             return;
373         }
374         int32_t motionType = OHOS::HDI::Motion::V1_1::HDF_MOTION_TYPE_RESERVED;
375         int32_t ret = g_motionInterface->EnableMotion(motionType);
376         EXPECT_EQ(HDF_SUCCESS, ret);
377         HDF_LOGI("EnableMotion %{public}d ret %{public}d\n", motionType, ret);
378 
379         ret = g_motionInterface->DisableMotion(motionType);
380         EXPECT_EQ(HDF_SUCCESS, ret);
381         HDF_LOGI("DisableMotion %{public}d ret %{public}d\n", motionType, ret);
382     }
383 }