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