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 if (g_motionInterface == nullptr) {
60 printf("Motion is not supported ");
61 GTEST_SKIP() << "Device not exist" << std::endl;
62 return;
63 }
64 }
65
TearDown()66 void HdfMotionTest::TearDown()
67 {
68 }
69
70 HWTEST_F(HdfMotionTest, SUB_Driver_Sensor_HdiMotion_0100, Function | MediumTest | Level2)
71 {
72 ASSERT_NE(nullptr, g_motionInterface);
73 }
74
75 HWTEST_F(HdfMotionTest, SUB_Driver_Sensor_HdiMotion_0200, Function | MediumTest | Level2)
76 {
77 if (g_motionInterface == nullptr) {
78 ASSERT_NE(nullptr, g_motionInterface);
79 return;
80 }
81 int32_t ret = g_motionInterface->Register(g_motionCallback);
82 EXPECT_EQ(HDF_SUCCESS, ret);
83 ret = g_motionInterface->Unregister(g_motionCallback);
84 EXPECT_EQ(0, ret);
85 }
86
87 HWTEST_F(HdfMotionTest, SUB_Driver_Sensor_HdiMotion_0300, Function | MediumTest | Level2)
88 {
89 if (g_motionInterface == nullptr) {
90 ASSERT_NE(nullptr, g_motionInterface);
91 return;
92 }
93 int32_t ret = g_motionInterface->Register(g_motionCallback);
94 EXPECT_EQ(HDF_SUCCESS, ret);
95 ret = g_motionInterface->Register(g_motionCallback);
96 EXPECT_EQ(HDF_FAILURE, ret);
97 ret = g_motionInterface->Unregister(g_motionCallback);
98 EXPECT_EQ(HDF_SUCCESS, ret);
99 }
100
101 HWTEST_F(HdfMotionTest, SUB_Driver_Sensor_HdiMotion_0400, Function | MediumTest | Level2)
102 {
103 if (g_motionInterface == nullptr) {
104 ASSERT_NE(nullptr, g_motionInterface);
105 return;
106 }
107 sptr<IMotionCallback> motionCallback = nullptr;
108 int32_t ret = g_motionInterface->Register(motionCallback);
109 EXPECT_NE(HDF_SUCCESS, ret);
110 }
111
112 HWTEST_F(HdfMotionTest, SUB_Driver_Sensor_HdiMotion_0500, Function | MediumTest | Level2)
113 {
114 if (g_motionInterface == nullptr) {
115 ASSERT_NE(nullptr, g_motionInterface);
116 return;
117 }
118 int32_t ret = g_motionInterface->Unregister(g_motionCallbackUnregistered);
119 EXPECT_EQ(HDF_FAILURE, ret);
120 }
121
122 HWTEST_F(HdfMotionTest, SUB_Driver_Sensor_HdiMotion_0600, Function | MediumTest | Level2)
123 {
124 if (g_motionInterface == nullptr) {
125 ASSERT_NE(nullptr, g_motionInterface);
126 return;
127 }
128 sptr<IMotionCallback> motionCallback = nullptr;
129 int32_t ret = g_motionInterface->Unregister(motionCallback);
130 EXPECT_NE(HDF_SUCCESS, ret);
131 }
132
133 HWTEST_F(HdfMotionTest, SUB_Driver_Sensor_HdiMotion_0700, Function | MediumTest | Level2)
134 {
135 if (g_motionInterface == nullptr) {
136 ASSERT_NE(nullptr, g_motionInterface);
137 return;
138 }
139
140 vector<int32_t> vec;
141 vec.push_back(OHOS::HDI::Motion::V1_1::HDF_MOTION_TYPE_PICKUP);
142 vec.push_back(OHOS::HDI::Motion::V1_1::HDF_MOTION_TYPE_FLIP);
143 vec.push_back(OHOS::HDI::Motion::V1_1::HDF_MOTION_TYPE_SHAKE);
144 vec.push_back(OHOS::HDI::Motion::V1_1::HDF_MOTION_TYPE_ROTATION);
145
146 int32_t ret = g_motionInterface->Register(g_motionCallback);
147 EXPECT_EQ(HDF_SUCCESS, ret);
148
149 for (size_t i = 0; i < vec.size(); i++) {
150 ret = g_motionInterface->EnableMotion(vec[i]);
151 if (ret == HDF_SUCCESS) {
152 printf("Motion %d enabled successfully\n", vec[i]);
153 } else {
154 printf("Motion %d enable failed\n", vec[i]);
155 }
156 EXPECT_EQ(HDF_SUCCESS, ret);
157 OsalSleep(15);
158
159 ret = g_motionInterface->DisableMotion(vec[i]);
160 if (ret == HDF_SUCCESS) {
161 printf("Motion %d disabled successfully\n", vec[i]);
162 } else {
163 printf("Motion %d disable failed\n", vec[i]);
164 }
165 EXPECT_EQ(HDF_SUCCESS, ret);
166 OsalSleep(2);
167 }
168
169 ret = g_motionInterface->Unregister(g_motionCallback);
170 EXPECT_EQ(HDF_SUCCESS, ret);
171 }
172
173 HWTEST_F(HdfMotionTest, SUB_Driver_Sensor_HdiMotion_0800, Function | MediumTest | Level2)
174 {
175 if (g_motionInterface == nullptr) {
176 ASSERT_NE(nullptr, g_motionInterface);
177 return;
178 }
179 int32_t ret = g_motionInterface->EnableMotion(OHOS::HDI::Motion::V1_1::HDF_MOTION_TYPE_MAX);
180 EXPECT_NE(HDF_SUCCESS, ret);
181 OsalSleep(2);
182
183 ret = g_motionInterface->DisableMotion(OHOS::HDI::Motion::V1_1::HDF_MOTION_TYPE_MAX);
184 EXPECT_NE(HDF_SUCCESS, ret);
185 }
186
187 HWTEST_F(HdfMotionTest, SUB_Driver_Sensor_HdiMotion_0900, Function | MediumTest | Level2)
188 {
189 if (g_motionInterface == nullptr) {
190 ASSERT_NE(nullptr, g_motionInterface);
191 return;
192 }
193 int32_t ret = g_motionInterface->EnableMotion(OHOS::HDI::Motion::V1_1::HDF_MOTION_TYPE_WRIST_UP);
194 EXPECT_EQ(HDF_SUCCESS, ret);
195 OsalSleep(2);
196
197 ret = g_motionInterface->DisableMotion(OHOS::HDI::Motion::V1_1::HDF_MOTION_TYPE_WRIST_UP);
198 EXPECT_EQ(HDF_SUCCESS, ret);
199 }
200
201 HWTEST_F(HdfMotionTest, SUB_Driver_Sensor_HdiMotion_1000, Function | MediumTest | Level2)
202 {
203 if (g_motionInterface == nullptr) {
204 ASSERT_NE(nullptr, g_motionInterface);
205 return;
206 }
207 int32_t motionType = -1;
208 int32_t ret = g_motionInterface->EnableMotion(motionType);
209 EXPECT_NE(HDF_SUCCESS, ret);
210 OsalSleep(2);
211
212 ret = g_motionInterface->DisableMotion(motionType);
213 EXPECT_NE(HDF_SUCCESS, ret);
214 }
215
216 HWTEST_F(HdfMotionTest, SUB_Driver_Sensor_HdiMotion_1100, Function | MediumTest | Level2)
217 {
218 if (g_motionInterface == nullptr) {
219 ASSERT_NE(nullptr, g_motionInterface);
220 return;
221 }
222 int32_t ret = g_motionInterface->Register(g_motionCallback);
223 EXPECT_EQ(HDF_SUCCESS, ret);
224
225 ret = g_motionInterface->EnableMotion(OHOS::HDI::Motion::V1_1::HDF_MOTION_TYPE_PICKUP);
226 EXPECT_EQ(HDF_SUCCESS, ret);
227 ret = g_motionInterface->DisableMotion(OHOS::HDI::Motion::V1_1::HDF_MOTION_TYPE_PICKUP);
228 EXPECT_EQ(HDF_SUCCESS, ret);
229
230 ret = g_motionInterface->Unregister(g_motionCallback);
231 EXPECT_EQ(HDF_SUCCESS, ret);
232 }
233
234 HWTEST_F(HdfMotionTest, SUB_Driver_Sensor_HdiMotion_1200, Function | MediumTest | Level2)
235 {
236 if (g_motionInterface == nullptr) {
237 ASSERT_NE(nullptr, g_motionInterface);
238 return;
239 }
240 int32_t ret = g_motionInterface->DisableMotion(OHOS::HDI::Motion::V1_1::HDF_MOTION_TYPE_MAX);
241 EXPECT_EQ(HDF_ERR_INVALID_PARAM, ret);
242 }
243
244 HWTEST_F(HdfMotionTest, SUB_Driver_Sensor_HdiMotion_1300, Function | MediumTest | Level2)
245 {
246 if (g_motionInterface == nullptr) {
247 ASSERT_NE(nullptr, g_motionInterface);
248 return;
249 }
250 int32_t ret = g_motionInterface->DisableMotion(OHOS::HDI::Motion::V1_1::HDF_MOTION_TYPE_PICKUP);
251 EXPECT_EQ(HDF_SUCCESS, ret);
252 }
253
254 HWTEST_F(HdfMotionTest, SUB_Driver_Sensor_HdiMotion_1400, Function | MediumTest | Level2)
255 {
256 if (g_motionInterface == nullptr) {
257 ASSERT_NE(nullptr, g_motionInterface);
258 return;
259 }
260 int32_t motionType = -1;
261 int32_t ret = g_motionInterface->DisableMotion(motionType);
262 EXPECT_NE(HDF_SUCCESS, ret);
263 }
264
265 HWTEST_F(HdfMotionTest, SUB_Driver_Sensor_HdiMotion_1500, Function | MediumTest | Level2)
266 {
267 if (g_motionInterface == nullptr) {
268 ASSERT_NE(nullptr, g_motionInterface);
269 return;
270 }
271 int32_t motionType = -1;
272 int32_t ret = g_motionInterface->SetMotionConfig(motionType, g_motionConfigData);
273 EXPECT_NE(HDF_SUCCESS, ret);
274 }
275