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