1 /*
2 * Copyright (c) 2021 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 <gtest/gtest.h>
19 #include <securec.h>
20 #include "hdf_base.h"
21 #include "osal_time.h"
22 #include "vibrator_if.h"
23 #include "vibrator_type.h"
24
25 using namespace testing::ext;
26
27 namespace {
28 uint32_t g_duration = 1000;
29 uint32_t g_noDuration = 0;
30 uint32_t g_sleepTime1 = 2000;
31 uint32_t g_sleepTime2 = 5000;
32 int32_t g_intensity1 = 30;
33 int32_t g_intensity2 = -30;
34 int32_t g_frequency1 = 200;
35 int32_t g_frequency2 = -200;
36 const char *g_timeSequence = "haptic.clock.timer";
37 const char *g_builtIn = "haptic.default.effect";
38 const char *g_arbitraryStr = "arbitraryString";
39 const struct VibratorInterface *g_vibratorDev = nullptr;
40 static struct VibratorInfo *g_vibratorInfo = nullptr;
41 }
42
43 class HdfVibratorTest : public testing::Test {
44 public:
45 static void SetUpTestCase();
46 static void TearDownTestCase();
47 void SetUp();
48 void TearDown();
49 };
50
SetUpTestCase()51 void HdfVibratorTest::SetUpTestCase()
52 {
53 g_vibratorDev = NewVibratorInterfaceInstance();
54 }
55
TearDownTestCase()56 void HdfVibratorTest::TearDownTestCase()
57 {
58 if (g_vibratorDev != nullptr) {
59 FreeVibratorInterfaceInstance();
60 g_vibratorDev = nullptr;
61 }
62 }
63
SetUp()64 void HdfVibratorTest::SetUp()
65 {
66 }
67
TearDown()68 void HdfVibratorTest::TearDown()
69 {
70 }
71
72 /**
73 * @tc.name: CheckVibratorInstanceIsEmpty
74 * @tc.desc: Create a vibrator instance. The instance is not empty.
75 * @tc.type: FUNC
76 * @tc.require: SR000FK1JM,AR000FK1J0,AR000FK1JP
77 */
78 HWTEST_F(HdfVibratorTest, CheckVibratorInstanceIsEmpty, TestSize.Level1)
79 {
80 ASSERT_NE(nullptr, g_vibratorDev);
81 }
82
83 /**
84 * @tc.name: PerformOneShotVibratorDuration_001
85 * @tc.desc: Controls this vibrator to perform a one-shot vibrator at a given duration.
86 Controls this vibrator to stop the vibrator
87 * @tc.type: FUNC
88 * @tc.require: AR000FK1JN,AR000FK1J0
89 */
90 HWTEST_F(HdfVibratorTest, PerformOneShotVibratorDuration_001, TestSize.Level1)
91 {
92 ASSERT_NE(nullptr, g_vibratorDev);
93
94 int32_t startRet = g_vibratorDev->StartOnce(g_duration);
95 EXPECT_EQ(startRet, HDF_SUCCESS);
96
97 OsalMSleep(g_sleepTime1);
98
99 int32_t endRet = g_vibratorDev->Stop(VIBRATOR_MODE_ONCE);
100 EXPECT_EQ(endRet, HDF_SUCCESS);
101 }
102
103 /**
104 * @tc.name: PerformOneShotVibratorDuration_002
105 * @tc.desc: Controls this vibrator to perform a one-shot vibrator at 0 millisecond.
106 Controls this vibrator to stop the vibrator
107 * @tc.type: FUNC
108 * @tc.require: AR000FK1JN,AR000FK1JP
109 */
110 HWTEST_F(HdfVibratorTest, PerformOneShotVibratorDuration_002, TestSize.Level1)
111 {
112 ASSERT_NE(nullptr, g_vibratorDev);
113
114 int32_t startRet = g_vibratorDev->StartOnce(g_noDuration);
115 EXPECT_EQ(startRet, HDF_ERR_INVALID_PARAM);
116
117 int32_t endRet = g_vibratorDev->Stop(VIBRATOR_MODE_ONCE);
118 EXPECT_EQ(endRet, HDF_SUCCESS);
119 }
120
121 /**
122 * @tc.name: ExecuteVibratorEffect_001
123 * @tc.desc: Controls this Performing Time Series Vibrator Effects.
124 Controls this vibrator to stop the vibrator
125 * @tc.type: FUNC
126 * @tc.require: AR000FK1JN,AR000FK1J0
127 */
128 HWTEST_F(HdfVibratorTest, ExecuteVibratorEffect_001, TestSize.Level1)
129 {
130 ASSERT_NE(nullptr, g_vibratorDev);
131
132 int32_t startRet = g_vibratorDev->Start(g_timeSequence);
133 EXPECT_EQ(startRet, HDF_SUCCESS);
134
135 OsalMSleep(g_sleepTime2);
136
137 int32_t endRet = g_vibratorDev->Stop(VIBRATOR_MODE_PRESET);
138 EXPECT_EQ(endRet, HDF_SUCCESS);
139 }
140
141 /**
142 * @tc.name: ExecuteVibratorEffect_002
143 * @tc.desc: Controls this Performing built-in Vibrator Effects.
144 Controls this vibrator to stop the vibrator.
145 * @tc.type: FUNC
146 * @tc.require: AR000FK1J0,AR000FK1JP
147 */
148 HWTEST_F(HdfVibratorTest, ExecuteVibratorEffect_002, TestSize.Level1)
149 {
150 ASSERT_NE(nullptr, g_vibratorDev);
151
152 int32_t startRet = g_vibratorDev->Start(g_builtIn);
153 EXPECT_EQ(startRet, HDF_SUCCESS);
154
155 OsalMSleep(g_sleepTime1);
156
157 int32_t endRet = g_vibratorDev->Stop(VIBRATOR_MODE_PRESET);
158 EXPECT_EQ(endRet, HDF_SUCCESS);
159 }
160
161 /**
162 * @tc.name: ExecuteVibratorEffect_003
163 * @tc.desc: Controls this Perform a Empty vibrator effect.
164 Controls this vibrator to stop the vibrator.
165 * @tc.type: FUNC
166 * @tc.require: AR000FK1J0,AR000FK1JP
167 */
168 HWTEST_F(HdfVibratorTest, ExecuteVibratorEffect_003, TestSize.Level1)
169 {
170 ASSERT_NE(nullptr, g_vibratorDev);
171
172 int32_t startRet = g_vibratorDev->Start(nullptr);
173 EXPECT_EQ(startRet, HDF_ERR_INVALID_PARAM);
174
175 int32_t endRet = g_vibratorDev->Stop(VIBRATOR_MODE_ONCE);
176 EXPECT_EQ(endRet, HDF_SUCCESS);
177 }
178
179 /**
180 * @tc.name: ExecuteVibratorEffect_004
181 * @tc.desc: Controls this Performing Time Series Vibrator Effects.
182 Controls this vibrator to stop the vibrator.
183 * @tc.type: FUNC
184 * @tc.require: AR000FK1JN,AR000FK1JP
185 */
186 HWTEST_F(HdfVibratorTest, ExecuteVibratorEffect_004, TestSize.Level1)
187 {
188 ASSERT_NE(nullptr, g_vibratorDev);
189
190 int32_t startRet = g_vibratorDev->Start(g_timeSequence);
191 EXPECT_EQ(startRet, HDF_SUCCESS);
192
193 OsalMSleep(g_sleepTime2);
194
195 int32_t endRet = g_vibratorDev->Stop(VIBRATOR_MODE_BUTT);
196 EXPECT_EQ(endRet, HDF_FAILURE);
197
198 endRet = g_vibratorDev->Stop(VIBRATOR_MODE_PRESET);
199 EXPECT_EQ(endRet, HDF_SUCCESS);
200 }
201
202 /**
203 * @tc.name: ExecuteVibratorEffect_005
204 * @tc.desc: Controls this vibrator to stop the vibrator.
205 Controls this Performing Time Series Vibrator Effects.
206 Controls this vibrator to stop the vibrator.
207 * @tc.type: FUNC
208 * @tc.require: AR000FK1JN,AR000FK1JP
209 */
210 HWTEST_F(HdfVibratorTest, ExecuteVibratorEffect_005, TestSize.Level1)
211 {
212 ASSERT_NE(nullptr, g_vibratorDev);
213
214 int32_t startRet = g_vibratorDev->Start(g_timeSequence);
215 EXPECT_EQ(startRet, HDF_SUCCESS);
216
217 OsalMSleep(g_sleepTime2);
218
219 int32_t endRet = g_vibratorDev->Stop(VIBRATOR_MODE_PRESET);
220 EXPECT_EQ(endRet, HDF_SUCCESS);
221 }
222
223 /**
224 * @tc.name: ExecuteVibratorEffect_006
225 * @tc.desc: Controls this vibrator to stop the vibrator.
226 Controls this Perform built-in Vibrator Effects.
227 Controls this vibrator to stop the vibrator.
228 * @tc.type: FUNC
229 * @tc.require: AR000FK1JN,AR000FK1JP
230 */
231 HWTEST_F(HdfVibratorTest, ExecuteVibratorEffect_006, TestSize.Level1)
232 {
233 ASSERT_NE(nullptr, g_vibratorDev);
234
235 int32_t startRet = g_vibratorDev->Start(g_builtIn);
236 EXPECT_EQ(startRet, HDF_SUCCESS);
237
238 OsalMSleep(g_sleepTime2);
239
240 int32_t endRet = g_vibratorDev->Stop(VIBRATOR_MODE_PRESET);
241 EXPECT_EQ(endRet, HDF_SUCCESS);
242 }
243
244 /**
245 * @tc.name: ExecuteVibratorEffect_007
246 * @tc.desc: Controls this Perform a one-shot vibrator with an arbitrary string.
247 Controls this vibrator to stop the vibrator.
248 * @tc.type: FUNC
249 * @tc.require: AR000FK1JN,AR000FK1J0,AR000FK1JP
250 */
251 HWTEST_F(HdfVibratorTest, ExecuteVibratorEffect_007, TestSize.Level1)
252 {
253 ASSERT_NE(nullptr, g_vibratorDev);
254
255 int32_t startRet = g_vibratorDev->Start(g_arbitraryStr);
256 EXPECT_EQ(startRet, HDF_ERR_INVALID_PARAM);
257
258 int32_t endRet = g_vibratorDev->Stop(VIBRATOR_MODE_ONCE);
259 EXPECT_EQ(endRet, HDF_SUCCESS);
260 }
261
262 /**
263 * @tc.name: GetVibratorInfo_001
264 * @tc.desc: Obtain the vibrator setting strength, frequency capability and range in the system.
265 * Validity check of input parameters.
266 * @tc.type: FUNC
267 * @tc.require: AR000FK1JN,AR000FK1J0,AR000FK1JP
268 */
269 HWTEST_F(HdfVibratorTest, GetVibratorInfo_001, TestSize.Level1)
270 {
271 ASSERT_NE(nullptr, g_vibratorDev);
272
273 int32_t startRet = g_vibratorDev->GetVibratorInfo(&g_vibratorInfo);
274 EXPECT_EQ(startRet, HDF_SUCCESS);
275 EXPECT_NE(g_vibratorInfo, nullptr);
276
277 printf("intensity = %d, intensityMaxValue = %d, intensityMinValue = %d\n\t",
278 g_vibratorInfo->isSupportIntensity, g_vibratorInfo->intensityMaxValue, g_vibratorInfo->intensityMinValue);
279 printf("frequency = %d, frequencyMaxValue = %d, frequencyMinValue = %d\n\t",
280 g_vibratorInfo->isSupportFrequency, g_vibratorInfo->frequencyMaxValue, g_vibratorInfo->frequencyMinValue);
281 }
282
283 /**
284 * @tc.name: GetVibratorInfo_002
285 * @tc.desc: Obtain the vibrator setting strength, frequency capability and range in the system.
286 * Validity check of input parameters.
287 * @tc.type: FUNC
288 * @tc.require: AR000FK1JN,AR000FK1J0,AR000FK1JP
289 */
290 HWTEST_F(HdfVibratorTest, GetVibratorInfo_002, TestSize.Level1)
291 {
292 ASSERT_NE(nullptr, g_vibratorDev);
293
294 int32_t startRet = g_vibratorDev->GetVibratorInfo(nullptr);
295 EXPECT_EQ(startRet, HDF_FAILURE);
296 }
297
298 /**
299 * @tc.name: EnableVibratorModulation_001
300 * @tc.desc: Start vibrator based on the setting vibration effect.
301 * @tc.type: FUNC
302 * @tc.require: AR000FK1JN,AR000FK1J0,AR000FK1JP
303 */
304 HWTEST_F(HdfVibratorTest, EnableVibratorModulation_001, TestSize.Level1)
305 {
306 int32_t startRet;
307 ASSERT_NE(nullptr, g_vibratorDev);
308 EXPECT_GT(g_duration, 0);
309
310 if ((g_vibratorInfo->isSupportIntensity == 1) || (g_vibratorInfo->isSupportFrequency == 1)) {
311 EXPECT_GE(g_intensity1, g_vibratorInfo->intensityMinValue);
312 EXPECT_LE(g_intensity1, g_vibratorInfo->intensityMaxValue);
313 EXPECT_GE(g_frequency1, g_vibratorInfo->frequencyMinValue);
314 EXPECT_LE(g_frequency1, g_vibratorInfo->frequencyMaxValue);
315
316 startRet = g_vibratorDev->EnableVibratorModulation(g_duration, g_intensity1, g_frequency1);
317 EXPECT_EQ(startRet, HDF_SUCCESS);
318 OsalMSleep(g_sleepTime1);
319 startRet = g_vibratorDev->Stop(VIBRATOR_MODE_ONCE);
320 EXPECT_EQ(startRet, HDF_SUCCESS);
321 }
322 }
323
324 /**
325 * @tc.name: EnableVibratorModulation_002
326 * @tc.desc: Start vibrator based on the setting vibration effect.
327 * Validity check of input parameters.
328 * @tc.type: FUNC
329 * @tc.require: AR000FK1JN,AR000FK1J0,AR000FK1JP
330 */
331 HWTEST_F(HdfVibratorTest, EnableVibratorModulation_002, TestSize.Level1)
332 {
333 ASSERT_NE(nullptr, g_vibratorDev);
334 int32_t startRet;
335 if ((g_vibratorInfo->isSupportIntensity == 1) || (g_vibratorInfo->isSupportFrequency == 1)) {
336 startRet = g_vibratorDev->EnableVibratorModulation(g_noDuration, g_intensity1, g_frequency1);
337 EXPECT_EQ(startRet, VIBRATOR_NOT_PERIOD);
338 }
339 }
340
341 /**
342 * @tc.name: EnableVibratorModulation_003
343 * @tc.desc: Start vibrator based on the setting vibration effect.
344 * Validity check of input parameters.
345 * @tc.type: FUNC
346 * @tc.require: AR000FK1JN,AR000FK1J0,AR000FK1JP
347 */
348 HWTEST_F(HdfVibratorTest, EnableVibratorModulation_003, TestSize.Level1)
349 {
350 ASSERT_NE(nullptr, g_vibratorDev);
351 int32_t startRet;
352
353 if ((g_vibratorInfo->isSupportIntensity == 1) || (g_vibratorInfo->isSupportFrequency == 1)) {
354 startRet = g_vibratorDev->EnableVibratorModulation(g_duration, g_intensity2, g_frequency1);
355 EXPECT_EQ(startRet, VIBRATOR_NOT_INTENSITY);
356 }
357 }
358
359 /**
360 * @tc.name: EnableVibratorModulation_004
361 * @tc.desc: Start vibrator based on the setting vibration effect.
362 * Validity check of input parameters.
363 * @tc.type: FUNC
364 * @tc.require: AR000FK1JN,AR000FK1J0,AR000FK1JP
365 */
366 HWTEST_F(HdfVibratorTest, EnableVibratorModulation_004, TestSize.Level1)
367 {
368 ASSERT_NE(nullptr, g_vibratorDev);
369 int32_t startRet;
370
371 if ((g_vibratorInfo->isSupportIntensity == 1) || (g_vibratorInfo->isSupportFrequency == 1)) {
372 startRet = g_vibratorDev->EnableVibratorModulation(g_duration, g_intensity1, g_frequency2);
373 EXPECT_EQ(startRet, VIBRATOR_NOT_FREQUENCY);
374 }
375 }