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