1 /*
2  * Copyright (c) 2022 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 "osal_time.h"
24 #include "v1_1/imotion_interface.h"
25 #include "motion_callback_impl.h"
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 }
39 
40 class HdfMotionTest : public testing::Test {
41 public:
42     static void SetUpTestCase();
43     static void TearDownTestCase();
44     void SetUp();
45     void TearDown();
46 };
47 
SetUpTestCase()48 void HdfMotionTest::SetUpTestCase()
49 {
50     g_motionInterface = OHOS::HDI::Motion::V1_1::IMotionInterface::Get();
51 }
52 
TearDownTestCase()53 void HdfMotionTest::TearDownTestCase()
54 {
55 }
56 
SetUp()57 void HdfMotionTest::SetUp()
58 {
59 }
60 
TearDown()61 void HdfMotionTest::TearDown()
62 {
63 }
64 
65 HWTEST_F(HdfMotionTest, GetMotionClient_001, TestSize.Level1)
66 {
67     ASSERT_NE(nullptr, g_motionInterface);
68 }
69 
70 HWTEST_F(HdfMotionTest, RegisterMotionDataCb_001, TestSize.Level1)
71 {
72     if (g_motionInterface == nullptr) {
73         ASSERT_NE(nullptr, g_motionInterface);
74         return;
75     }
76     int32_t ret = g_motionInterface->Register(g_motionCallback);
77     EXPECT_EQ(HDF_SUCCESS, ret);
78     ret = g_motionInterface->Unregister(g_motionCallback);
79     EXPECT_EQ(0, ret);
80 }
81 
82 HWTEST_F(HdfMotionTest, RegisterMotionDataCb_002, TestSize.Level1)
83 {
84     if (g_motionInterface == nullptr) {
85         ASSERT_NE(nullptr, g_motionInterface);
86         return;
87     }
88     int32_t ret = g_motionInterface->Register(g_motionCallback);
89     EXPECT_EQ(HDF_SUCCESS, ret);
90     ret = g_motionInterface->Register(g_motionCallback);
91     EXPECT_EQ(HDF_FAILURE, ret);
92     ret = g_motionInterface->Unregister(g_motionCallback);
93     EXPECT_EQ(HDF_SUCCESS, ret);
94 }
95 
96 HWTEST_F(HdfMotionTest, RegisterMotionDataCb_003, TestSize.Level1)
97 {
98     if (g_motionInterface == nullptr) {
99         ASSERT_NE(nullptr, g_motionInterface);
100         return;
101     }
102     sptr<IMotionCallback> motionCallback = nullptr;
103     int32_t ret = g_motionInterface->Register(motionCallback);
104     EXPECT_NE(HDF_SUCCESS, ret);
105 }
106 
107 HWTEST_F(HdfMotionTest, RegisterMotionDataCb_004, TestSize.Level1)
108 {
109     if (g_motionInterface == nullptr) {
110         ASSERT_NE(nullptr, g_motionInterface);
111         return;
112     }
113     int32_t ret = g_motionInterface->Unregister(g_motionCallbackUnregistered);
114     EXPECT_EQ(HDF_FAILURE, ret);
115 }
116 
117 HWTEST_F(HdfMotionTest, RegisterMotionDataCb_005, TestSize.Level1)
118 {
119     if (g_motionInterface == nullptr) {
120         ASSERT_NE(nullptr, g_motionInterface);
121         return;
122     }
123     sptr<IMotionCallback> motionCallback = nullptr;
124     int32_t ret = g_motionInterface->Unregister(motionCallback);
125     EXPECT_NE(HDF_SUCCESS, ret);
126 }
127 
128 HWTEST_F(HdfMotionTest, EnableMotion_001, TestSize.Level1)
129 {
130     if (g_motionInterface == nullptr) {
131         ASSERT_NE(nullptr, g_motionInterface);
132         return;
133     }
134 
135     vector<int32_t> vec;
136     vec.push_back(OHOS::HDI::Motion::V1_1::HDF_MOTION_TYPE_PICKUP);
137     vec.push_back(OHOS::HDI::Motion::V1_1::HDF_MOTION_TYPE_FLIP);
138     vec.push_back(OHOS::HDI::Motion::V1_1::HDF_MOTION_TYPE_SHAKE);
139     vec.push_back(OHOS::HDI::Motion::V1_1::HDF_MOTION_TYPE_ROTATION);
140 
141     int32_t ret = g_motionInterface->Register(g_motionCallback);
142     EXPECT_EQ(HDF_SUCCESS, ret);
143 
144     for (size_t i = 0; i < vec.size(); i++) {
145         ret = g_motionInterface->EnableMotion(vec[i]);
146         if (ret == HDF_SUCCESS) {
147             printf("Motion %d enabled successfully\n", vec[i]);
148         } else {
149             printf("Motion %d enable failed\n", vec[i]);
150         }
151         EXPECT_EQ(HDF_SUCCESS, ret);
152         OsalSleep(15);
153 
154         ret = g_motionInterface->DisableMotion(vec[i]);
155         if (ret == HDF_SUCCESS) {
156             printf("Motion %d disabled successfully\n", vec[i]);
157         } else {
158             printf("Motion %d disable failed\n", vec[i]);
159         }
160         EXPECT_EQ(HDF_SUCCESS, ret);
161         OsalSleep(2);
162     }
163 
164     ret = g_motionInterface->Unregister(g_motionCallback);
165     EXPECT_EQ(HDF_SUCCESS, ret);
166 }
167 
168 HWTEST_F(HdfMotionTest, EnableMotion_002, TestSize.Level1)
169 {
170     if (g_motionInterface == nullptr) {
171         ASSERT_NE(nullptr, g_motionInterface);
172         return;
173     }
174     int32_t ret = g_motionInterface->EnableMotion(OHOS::HDI::Motion::V1_1::HDF_MOTION_TYPE_MAX);
175     EXPECT_NE(HDF_SUCCESS, ret);
176     OsalSleep(2);
177 
178     ret = g_motionInterface->DisableMotion(OHOS::HDI::Motion::V1_1::HDF_MOTION_TYPE_MAX);
179     EXPECT_NE(HDF_SUCCESS, ret);
180 }
181 
182 HWTEST_F(HdfMotionTest, EnableMotion_003, TestSize.Level1)
183 {
184     if (g_motionInterface == nullptr) {
185         ASSERT_NE(nullptr, g_motionInterface);
186         return;
187     }
188     int32_t ret = g_motionInterface->EnableMotion(OHOS::HDI::Motion::V1_1::HDF_MOTION_TYPE_WRIST_UP);
189     EXPECT_EQ(HDF_SUCCESS, ret);
190     OsalSleep(2);
191 
192     ret = g_motionInterface->DisableMotion(OHOS::HDI::Motion::V1_1::HDF_MOTION_TYPE_WRIST_UP);
193     EXPECT_EQ(HDF_SUCCESS, ret);
194 }
195 
196 HWTEST_F(HdfMotionTest, EnableMotion_004, TestSize.Level1)
197 {
198     if (g_motionInterface == nullptr) {
199         ASSERT_NE(nullptr, g_motionInterface);
200         return;
201     }
202     int32_t motionType = -1;
203     int32_t ret = g_motionInterface->EnableMotion(motionType);
204     EXPECT_NE(HDF_SUCCESS, ret);
205     OsalSleep(2);
206 
207     ret = g_motionInterface->DisableMotion(motionType);
208     EXPECT_NE(HDF_SUCCESS, ret);
209 }
210 
211 HWTEST_F(HdfMotionTest, DisableMotion_001, TestSize.Level1)
212 {
213     if (g_motionInterface == nullptr) {
214         ASSERT_NE(nullptr, g_motionInterface);
215         return;
216     }
217     int32_t ret = g_motionInterface->Register(g_motionCallback);
218     EXPECT_EQ(HDF_SUCCESS, ret);
219 
220     ret = g_motionInterface->EnableMotion(OHOS::HDI::Motion::V1_1::HDF_MOTION_TYPE_PICKUP);
221     EXPECT_EQ(HDF_SUCCESS, ret);
222     ret = g_motionInterface->DisableMotion(OHOS::HDI::Motion::V1_1::HDF_MOTION_TYPE_PICKUP);
223     EXPECT_EQ(HDF_SUCCESS, ret);
224 
225     ret = g_motionInterface->Unregister(g_motionCallback);
226     EXPECT_EQ(HDF_SUCCESS, ret);
227 }
228 
229 HWTEST_F(HdfMotionTest, DisableMotion_002, TestSize.Level1)
230 {
231     if (g_motionInterface == nullptr) {
232         ASSERT_NE(nullptr, g_motionInterface);
233         return;
234     }
235     int32_t ret = g_motionInterface->DisableMotion(OHOS::HDI::Motion::V1_1::HDF_MOTION_TYPE_MAX);
236     EXPECT_EQ(HDF_ERR_INVALID_PARAM, ret);
237 }
238 
239 HWTEST_F(HdfMotionTest, DisableMotion_003, TestSize.Level1)
240 {
241     if (g_motionInterface == nullptr) {
242         ASSERT_NE(nullptr, g_motionInterface);
243         return;
244     }
245     int32_t ret = g_motionInterface->DisableMotion(OHOS::HDI::Motion::V1_1::HDF_MOTION_TYPE_PICKUP);
246     EXPECT_EQ(HDF_SUCCESS, ret);
247 }
248 
249 HWTEST_F(HdfMotionTest, DisableMotion_004, TestSize.Level1)
250 {
251     if (g_motionInterface == nullptr) {
252         ASSERT_NE(nullptr, g_motionInterface);
253         return;
254     }
255     int32_t motionType = -1;
256     int32_t ret = g_motionInterface->DisableMotion(motionType);
257     EXPECT_NE(HDF_SUCCESS, ret);
258 }
259 
260 HWTEST_F(HdfMotionTest, SetMotionConfig_001, TestSize.Level1)
261 {
262     if (g_motionInterface == nullptr) {
263         ASSERT_NE(nullptr, g_motionInterface);
264         return;
265     }
266     int32_t motionType = -1;
267     int32_t ret = g_motionInterface->SetMotionConfig(motionType, g_motionConfigData);
268     EXPECT_NE(HDF_SUCCESS, ret);
269 }