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 <gtest/gtest.h>
19 #include <securec.h>
20 #include "hdf_base.h"
21 #include "osal_time.h"
22 #include "v1_1/ivibrator_interface.h"
23 #include "vibrator_type.h"
24
25 using namespace OHOS::HDI::Vibrator::V1_1;
26 using namespace testing::ext;
27
28 namespace {
29 uint32_t g_duration = 1000;
30 uint32_t g_noDuration = 0;
31 uint32_t g_sleepTime1 = 2000;
32 uint32_t g_sleepTime2 = 5000;
33 int32_t g_intensity1 = 30;
34 int32_t g_intensity2 = -30;
35 int32_t g_frequency1 = 200;
36 int32_t g_frequency2 = -200;
37 constexpr int32_t MIN_DURATION = 0;
38 constexpr int32_t MAX_DURATION = 3600000;
39 std::string g_timeSequence = "haptic.clock.timer";
40 std::string g_builtIn = "haptic.default.effect";
41 std::string g_arbitraryStr = "arbitraryString";
42 sptr<IVibratorInterface> g_vibratorInterface = nullptr;
43 }
44
45 class HdfVibratorHdiTest : public testing::Test {
46 public:
47 static void SetUpTestCase();
48 static void TearDownTestCase();
49 void SetUp();
50 void TearDown();
51 };
52
SetUpTestCase()53 void HdfVibratorHdiTest::SetUpTestCase()
54 {
55 g_vibratorInterface = IVibratorInterface::Get();
56 }
57
TearDownTestCase()58 void HdfVibratorHdiTest::TearDownTestCase()
59 {
60 }
61
SetUp()62 void HdfVibratorHdiTest::SetUp()
63 {
64 }
65
TearDown()66 void HdfVibratorHdiTest::TearDown()
67 {
68 }
69
70 /**
71 * @tc.name: CheckVibratorInstanceIsEmpty
72 * @tc.desc: Create a vibrator instance. The instance is not empty.
73 * @tc.type: FUNC
74 * @tc.require: #I4NN4Z
75 */
76 HWTEST_F(HdfVibratorHdiTest, CheckVibratorInstanceIsEmpty, TestSize.Level1)
77 {
78 ASSERT_NE(nullptr, g_vibratorInterface);
79 }
80
81 /**
82 * @tc.name: PerformOneShotVibratorDuration_001
83 * @tc.desc: Controls this vibrator to perform a one-shot vibrator at a given duration.
84 * Controls this vibrator to stop the vibrator
85 * @tc.type: FUNC
86 * @tc.require: #I4NN4Z
87 */
88 HWTEST_F(HdfVibratorHdiTest, PerformOneShotVibratorDuration_001, TestSize.Level1)
89 {
90 ASSERT_NE(nullptr, g_vibratorInterface);
91
92 int32_t startRet = g_vibratorInterface->StartOnce(g_duration);
93 EXPECT_EQ(startRet, HDF_SUCCESS);
94
95 OsalMSleep(g_sleepTime1);
96
97 int32_t endRet = g_vibratorInterface->Stop(HDF_VIBRATOR_MODE_ONCE);
98 EXPECT_EQ(endRet, HDF_SUCCESS);
99 }
100
101 /**
102 * @tc.name: PerformOneShotVibratorDuration_002
103 * @tc.desc: Controls this vibrator to perform a one-shot vibrator at 0 millisecond.
104 * Controls this vibrator to stop the vibrator
105 * @tc.type: FUNC
106 * @tc.require: #I4NN4Z
107 */
108 HWTEST_F(HdfVibratorHdiTest, PerformOneShotVibratorDuration_002, TestSize.Level1)
109 {
110 ASSERT_NE(nullptr, g_vibratorInterface);
111
112 int32_t startRet = g_vibratorInterface->StartOnce(g_noDuration);
113 EXPECT_EQ(startRet, HDF_SUCCESS);
114
115 int32_t endRet = g_vibratorInterface->Stop(HDF_VIBRATOR_MODE_ONCE);
116 EXPECT_EQ(endRet, HDF_SUCCESS);
117 }
118
119 /**
120 * @tc.name: ExecuteVibratorEffect_001
121 * @tc.desc: Controls this Performing Time Series Vibrator Effects.
122 * Controls this vibrator to stop the vibrator
123 * @tc.type: FUNC
124 * @tc.require: #I4NN4Z
125 */
126 HWTEST_F(HdfVibratorHdiTest, ExecuteVibratorEffect_001, TestSize.Level1)
127 {
128 ASSERT_NE(nullptr, g_vibratorInterface);
129
130 int32_t startRet = g_vibratorInterface->Start(g_timeSequence);
131 EXPECT_EQ(startRet, HDF_SUCCESS);
132
133 OsalMSleep(g_sleepTime2);
134
135 int32_t endRet = g_vibratorInterface->Stop(HDF_VIBRATOR_MODE_PRESET);
136 EXPECT_EQ(endRet, HDF_SUCCESS);
137 }
138
139 /**
140 * @tc.name: ExecuteVibratorEffect_002
141 * @tc.desc: Controls this Performing built-in Vibrator Effects.
142 * Controls this vibrator to stop the vibrator.
143 * @tc.type: FUNC
144 * @tc.require: #I4NN4Z
145 */
146 HWTEST_F(HdfVibratorHdiTest, ExecuteVibratorEffect_002, TestSize.Level1)
147 {
148 ASSERT_NE(nullptr, g_vibratorInterface);
149
150 int32_t startRet = g_vibratorInterface->Start(g_builtIn);
151 EXPECT_EQ(startRet, HDF_SUCCESS);
152
153 OsalMSleep(g_sleepTime1);
154
155 int32_t endRet = g_vibratorInterface->Stop(HDF_VIBRATOR_MODE_PRESET);
156 EXPECT_EQ(endRet, HDF_SUCCESS);
157 }
158
159 /**
160 * @tc.name: ExecuteVibratorEffect_004
161 * @tc.desc: Controls this Performing Time Series Vibrator Effects.
162 * Controls this vibrator to stop the vibrator.
163 * @tc.type: FUNC
164 * @tc.require: #I4NN4Z
165 */
166 HWTEST_F(HdfVibratorHdiTest, ExecuteVibratorEffect_004, TestSize.Level1)
167 {
168 ASSERT_NE(nullptr, g_vibratorInterface);
169
170 int32_t startRet = g_vibratorInterface->Start(g_timeSequence);
171 EXPECT_EQ(startRet, HDF_SUCCESS);
172
173 OsalMSleep(g_sleepTime2);
174
175 int32_t endRet = g_vibratorInterface->Stop(HDF_VIBRATOR_MODE_BUTT);
176 EXPECT_EQ(endRet, HDF_FAILURE);
177
178 endRet = g_vibratorInterface->Stop(HDF_VIBRATOR_MODE_PRESET);
179 EXPECT_EQ(endRet, HDF_SUCCESS);
180 }
181
182 /**
183 * @tc.name: ExecuteVibratorEffect_005
184 * @tc.desc: Controls this vibrator to stop the vibrator.
185 * Controls this Performing Time Series Vibrator Effects.
186 * Controls this vibrator to stop the vibrator.
187 * @tc.type: FUNC
188 * @tc.require: #I4NN4Z
189 */
190 HWTEST_F(HdfVibratorHdiTest, ExecuteVibratorEffect_005, TestSize.Level1)
191 {
192 ASSERT_NE(nullptr, g_vibratorInterface);
193
194 int32_t startRet = g_vibratorInterface->Start(g_timeSequence);
195 EXPECT_EQ(startRet, HDF_SUCCESS);
196
197 OsalMSleep(g_sleepTime2);
198
199 int32_t endRet = g_vibratorInterface->Stop(HDF_VIBRATOR_MODE_PRESET);
200 EXPECT_EQ(endRet, HDF_SUCCESS);
201 }
202
203 /**
204 * @tc.name: ExecuteVibratorEffect_006
205 * @tc.desc: Controls this vibrator to stop the vibrator.
206 * Controls this Perform built-in Vibrator Effects.
207 * Controls this vibrator to stop the vibrator.
208 * @tc.type: FUNC
209 * @tc.require: #I4NN4Z
210 */
211 HWTEST_F(HdfVibratorHdiTest, ExecuteVibratorEffect_006, TestSize.Level1)
212 {
213 ASSERT_NE(nullptr, g_vibratorInterface);
214
215 int32_t startRet = g_vibratorInterface->Start(g_builtIn);
216 EXPECT_EQ(startRet, HDF_SUCCESS);
217
218 OsalMSleep(g_sleepTime2);
219
220 int32_t endRet = g_vibratorInterface->Stop(HDF_VIBRATOR_MODE_PRESET);
221 EXPECT_EQ(endRet, HDF_SUCCESS);
222 }
223
224 /**
225 * @tc.name: ExecuteVibratorEffect_007
226 * @tc.desc: Controls this Perform a one-shot vibrator with an arbitrary string.
227 * Controls this vibrator to stop the vibrator.
228 * @tc.type: FUNC
229 * @tc.require: #I4NN4Z
230 */
231 HWTEST_F(HdfVibratorHdiTest, ExecuteVibratorEffect_007, TestSize.Level1)
232 {
233 ASSERT_NE(nullptr, g_vibratorInterface);
234
235 int32_t startRet = g_vibratorInterface->Start(g_arbitraryStr);
236 EXPECT_EQ(startRet, HDF_ERR_INVALID_PARAM);
237
238 int32_t endRet = g_vibratorInterface->Stop(HDF_VIBRATOR_MODE_ONCE);
239 EXPECT_EQ(endRet, HDF_SUCCESS);
240 }
241
242 /**
243 * @tc.name: GetVibratorInfo_001
244 * @tc.desc: Obtain the vibrator setting strength, frequency capability and range in the system.
245 * Validity check of input parameters.
246 * @tc.type: FUNC
247 * @tc.require: #I4NN4Z
248 */
249 HWTEST_F(HdfVibratorHdiTest, GetVibratorInfo_001, TestSize.Level1)
250 {
251 uint32_t majorVer;
252 uint32_t minorVer;
253 if (g_vibratorInterface->GetVersion(majorVer, minorVer) != HDF_SUCCESS) {
254 printf("get version failed!\n\t");
255 return;
256 }
257
258 if (majorVer > 0 && minorVer <= 0) {
259 printf("version not support!\n\t");
260 return;
261 }
262 ASSERT_NE(nullptr, g_vibratorInterface);
263
264 std::vector<HdfVibratorInfo> info;
265
266 int32_t startRet = g_vibratorInterface->GetVibratorInfo(info);
267 EXPECT_EQ(startRet, HDF_SUCCESS);
268
269 printf("intensity = %d, intensityMaxValue = %d, intensityMinValue = %d\n\t",
270 info[0].isSupportIntensity, info[0].intensityMaxValue, info[0].intensityMinValue);
271 printf("frequency = %d, intensityMaxValue = %d, intensityMinValue = %d\n\t",
272 info[0].isSupportFrequency, info[0].frequencyMaxValue, info[0].frequencyMinValue);
273 }
274
275 /**
276 * @tc.name: EnableVibratorModulation_001
277 * @tc.desc: Start vibrator based on the setting vibration effect.
278 * @tc.type: FUNC
279 * @tc.require: #I4NN4Z
280 */
281 HWTEST_F(HdfVibratorHdiTest, EnableVibratorModulation_001, TestSize.Level1)
282 {
283 uint32_t majorVer;
284 uint32_t minorVer;
285 if (g_vibratorInterface->GetVersion(majorVer, minorVer) != HDF_SUCCESS) {
286 printf("get version failed!\n\t");
287 return;
288 }
289
290 if (majorVer > 0 && minorVer <= 0) {
291 printf("version not support!\n\t");
292 return;
293 }
294 ASSERT_NE(nullptr, g_vibratorInterface);
295
296 std::vector<HdfVibratorInfo> info;
297
298 int32_t startRet = g_vibratorInterface->GetVibratorInfo(info);
299 EXPECT_EQ(startRet, HDF_SUCCESS);
300
301 if ((info[0].isSupportIntensity == 1) || (info[0].isSupportFrequency == 1)) {
302 EXPECT_GT(g_duration, 0);
303 EXPECT_GE(g_intensity1, info[0].intensityMinValue);
304 EXPECT_LE(g_intensity1, info[0].intensityMaxValue);
305 EXPECT_GE(g_frequency1, info[0].frequencyMinValue);
306 EXPECT_LE(g_frequency1, info[0].frequencyMaxValue);
307
308 startRet = g_vibratorInterface->EnableVibratorModulation(g_duration, g_intensity1, g_frequency1);
309 EXPECT_EQ(startRet, HDF_SUCCESS);
310 OsalMSleep(g_sleepTime1);
311 startRet = g_vibratorInterface->Stop(HDF_VIBRATOR_MODE_ONCE);
312 EXPECT_EQ(startRet, HDF_SUCCESS);
313 }
314 }
315
316 /**
317 * @tc.name: EnableVibratorModulation_002
318 * @tc.desc: Start vibrator based on the setting vibration effect.
319 * Validity check of input parameters.
320 * @tc.type: FUNC
321 * @tc.require: #I4NN4Z
322 */
323 HWTEST_F(HdfVibratorHdiTest, EnableVibratorModulation_002, TestSize.Level1)
324 {
325 uint32_t majorVer;
326 uint32_t minorVer;
327 if (g_vibratorInterface->GetVersion(majorVer, minorVer) != HDF_SUCCESS) {
328 printf("get version failed!\n\t");
329 return;
330 }
331
332 if (majorVer > 0 && minorVer <= 0) {
333 printf("version not support!\n\t");
334 return;
335 }
336 ASSERT_NE(nullptr, g_vibratorInterface);
337
338 std::vector<HdfVibratorInfo> info;
339
340 int32_t startRet = g_vibratorInterface->GetVibratorInfo(info);
341 EXPECT_EQ(startRet, HDF_SUCCESS);
342
343 if ((info[0].isSupportIntensity == 1) || (info[0].isSupportFrequency == 1)) {
344 startRet = g_vibratorInterface->EnableVibratorModulation(g_noDuration, g_intensity1, g_frequency1);
345 EXPECT_EQ(startRet, VIBRATOR_NOT_PERIOD);
346 }
347 }
348
349 /**
350 * @tc.name: EnableVibratorModulation_003
351 * @tc.desc: Start vibrator based on the setting vibration effect.
352 * Validity check of input parameters.
353 * @tc.type: FUNC
354 * @tc.require: #I4NN4Z
355 */
356 HWTEST_F(HdfVibratorHdiTest, EnableVibratorModulation_003, TestSize.Level1)
357 {
358 uint32_t majorVer;
359 uint32_t minorVer;
360 if (g_vibratorInterface->GetVersion(majorVer, minorVer) != HDF_SUCCESS) {
361 printf("get version failed!\n\t");
362 return;
363 }
364
365 if (majorVer > 0 && minorVer <= 0) {
366 printf("version not support!\n\t");
367 return;
368 }
369 ASSERT_NE(nullptr, g_vibratorInterface);
370
371 std::vector<HdfVibratorInfo> info;
372
373 int32_t startRet = g_vibratorInterface->GetVibratorInfo(info);
374 EXPECT_EQ(startRet, HDF_SUCCESS);
375
376 if ((info[0].isSupportIntensity == 1) || (info[0].isSupportFrequency == 1)) {
377 startRet = g_vibratorInterface->EnableVibratorModulation(g_duration, g_intensity2, g_frequency1);
378 EXPECT_EQ(startRet, VIBRATOR_NOT_INTENSITY);
379 }
380 }
381
382 /**
383 * @tc.name: EnableVibratorModulation_004
384 * @tc.desc: Start vibrator based on the setting vibration effect.
385 * Validity check of input parameters.
386 * @tc.type: FUNC
387 * @tc.require: #I4NN4Z
388 */
389 HWTEST_F(HdfVibratorHdiTest, EnableVibratorModulation_004, TestSize.Level1)
390 {
391 uint32_t majorVer;
392 uint32_t minorVer;
393 if (g_vibratorInterface->GetVersion(majorVer, minorVer) != HDF_SUCCESS) {
394 printf("get version failed!\n\t");
395 return;
396 }
397
398 if (majorVer > 0 && minorVer <= 0) {
399 printf("version not support!\n\t");
400 return;
401 }
402 ASSERT_NE(nullptr, g_vibratorInterface);
403
404 std::vector<HdfVibratorInfo> info;
405
406 int32_t startRet = g_vibratorInterface->GetVibratorInfo(info);
407 EXPECT_EQ(startRet, HDF_SUCCESS);
408
409 if ((info[0].isSupportIntensity == 1) || (info[0].isSupportFrequency == 1)) {
410 startRet = g_vibratorInterface->EnableVibratorModulation(g_duration, g_intensity1, g_frequency2);
411 EXPECT_EQ(startRet, VIBRATOR_NOT_FREQUENCY);
412 }
413 }
414
415 /**
416 * @tc.name: GetEffectInfo_001
417 * @tc.desc: Get effect information with the given effect type.
418 * @tc.type: FUNC
419 * @tc.require:I6FBDQ
420 */
421 HWTEST_F(HdfVibratorHdiTest, GetEffectInfo_001, TestSize.Level1)
422 {
423 ASSERT_NE(nullptr, g_vibratorInterface);
424
425 HdfEffectInfo effectInfo;
426 int32_t ret = g_vibratorInterface->GetEffectInfo("haptic.clock.timer", effectInfo);
427 printf("isSupportEffect = [%d]\n\r", effectInfo.isSupportEffect);
428 printf("duration = [%d]\n\r", effectInfo.duration);
429 EXPECT_EQ(ret, HDF_SUCCESS);
430 if (effectInfo.isSupportEffect) {
431 EXPECT_GT(effectInfo.duration, MIN_DURATION);
432 EXPECT_LE(effectInfo.duration, MAX_DURATION);
433 }
434 }
435
436 /**
437 * @tc.name: GetEffectInfo_002
438 * @tc.desc: Get effect information with the given effect type.
439 * @tc.type: FUNC
440 * @tc.require:#I6FBDQ
441 */
442 HWTEST_F(HdfVibratorHdiTest, GetEffectInfo_002, TestSize.Level1)
443 {
444 ASSERT_NE(nullptr, g_vibratorInterface);
445
446 HdfEffectInfo effectInfo;
447 int32_t ret = g_vibratorInterface->GetEffectInfo("invaild effect id", effectInfo);
448 printf("isSupportEffect = [%d]\n\r", effectInfo.isSupportEffect);
449 printf("duration = [%d]\n\r", effectInfo.duration);
450 EXPECT_EQ(ret, HDF_SUCCESS);
451 EXPECT_EQ(effectInfo.isSupportEffect, false);
452 EXPECT_EQ(effectInfo.duration, 0);
453 }