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 #include <cmath>
16 #include <cstdio>
17 #include <gtest/gtest.h>
18 #include <securec.h>
19 #include "hdf_base.h"
20 #include "osal_time.h"
21 #include "v1_1/ivibrator_interface.h"
22
23 using namespace OHOS::HDI::Vibrator::V1_1;
24 using namespace testing::ext;
25
26 namespace {
27 uint32_t g_duration = 1000;
28 uint32_t g_noDuration = 0;
29 uint32_t g_sleepTime1 = 2000;
30 uint32_t g_sleepTime2 = 5000;
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 std::string g_timeSequence = "haptic.clock.timer";
36 std::string g_builtIn = "haptic.default.effect";
37 std::string g_arbitraryStr = "arbitraryString";
38 sptr<IVibratorInterface> g_vibratorInterface = nullptr;
39
40 class HdfVibratorHdiServiceTest : public testing::Test {
41 public:
42 static void SetUpTestCase();
43 static void TearDownTestCase();
44 void SetUp();
45 void TearDown();
46 };
47
SetUpTestCase()48 void HdfVibratorHdiServiceTest::SetUpTestCase()
49 {
50 g_vibratorInterface = IVibratorInterface::Get();
51 }
52
TearDownTestCase()53 void HdfVibratorHdiServiceTest::TearDownTestCase()
54 {
55 }
56
SetUp()57 void HdfVibratorHdiServiceTest::SetUp()
58 {
59 }
60
TearDown()61 void HdfVibratorHdiServiceTest::TearDown()
62 {
63 }
64
65 /**
66 * @tc.name: CheckVibratorInstanceIsEmpty
67 * @tc.desc: Creat a vibrator instance. The instance is not empty.
68 * @tc.type: FUNC
69 */
70 HWTEST_F(HdfVibratorHdiServiceTest, SUB_Driver_Sensor_HdiVibrator_0100, Function | MediumTest | Level1)
71 {
72 ASSERT_NE(nullptr, g_vibratorInterface);
73 }
74
75 /**
76 * @tc.name: PerformOneShotVibratorDuration001
77 * @tc.desc: Controls this vibrator to perform a one-shot vibrator at a given duration.
78 * Controls this vibrator to stop the vibrator
79 * @tc.type: FUNC
80 */
81 HWTEST_F(HdfVibratorHdiServiceTest, SUB_Driver_Sensor_HdiVibrator_0200, Function | MediumTest | Level1)
82 {
83 ASSERT_NE(nullptr, g_vibratorInterface);
84
85 int32_t startRet = g_vibratorInterface->StartOnce(g_duration);
86 EXPECT_EQ(startRet, HDF_SUCCESS);
87
88 OsalMSleep(g_sleepTime1);
89
90 int32_t endRet = g_vibratorInterface->Stop(HDF_VIBRATOR_MODE_ONCE);
91 EXPECT_EQ(endRet, HDF_SUCCESS);
92 }
93
94 /**
95 * @tc.name: PerformOneShotVibratorDuration002
96 * @tc.desc: Controls this vibrator to perform a one-shot vibrator at 0 millisecond.
97 * Controls this vibrator to stop the vibrator
98 * @tc.type: FUNC
99 */
100 HWTEST_F(HdfVibratorHdiServiceTest, SUB_Driver_Sensor_HdiVibrator_0300, Function | MediumTest | Level1)
101 {
102 ASSERT_NE(nullptr, g_vibratorInterface);
103
104 int32_t startRet = g_vibratorInterface->StartOnce(g_noDuration);
105 EXPECT_EQ(startRet, HDF_SUCCESS);
106
107 int32_t endRet = g_vibratorInterface->Stop(HDF_VIBRATOR_MODE_ONCE);
108 EXPECT_EQ(endRet, HDF_SUCCESS);
109 }
110
111 /**
112 * @tc.name: ExecuteVibratorEffect001
113 * @tc.desc: Controls this Performing Time Series Vibrator Effects.
114 * Controls this vibrator to stop the vibrator
115 * @tc.type: FUNC
116 */
117 HWTEST_F(HdfVibratorHdiServiceTest, SUB_Driver_Sensor_HdiVibrator_0400, Function | MediumTest | Level1)
118 {
119 ASSERT_NE(nullptr, g_vibratorInterface);
120
121 int32_t startRet = g_vibratorInterface->Start(g_timeSequence);
122 EXPECT_EQ(startRet, HDF_SUCCESS);
123
124 OsalMSleep(g_sleepTime2);
125
126 int32_t endRet = g_vibratorInterface->Stop(HDF_VIBRATOR_MODE_PRESET);
127 EXPECT_EQ(endRet, HDF_SUCCESS);
128 }
129
130 /**
131 * @tc.name: ExecuteVibratorEffect002
132 * @tc.desc: Controls this Performing built-in Vibrator Effects.
133 * Controls this vibrator to stop the vibrator.
134 * @tc.type: FUNC
135 */
136 HWTEST_F(HdfVibratorHdiServiceTest, SUB_Driver_Sensor_HdiVibrator_0500, Function | MediumTest | Level1)
137 {
138 ASSERT_NE(nullptr, g_vibratorInterface);
139
140 int32_t startRet = g_vibratorInterface->Start(g_builtIn);
141 EXPECT_EQ(startRet, HDF_SUCCESS);
142
143 OsalMSleep(g_sleepTime1);
144
145 int32_t endRet = g_vibratorInterface->Stop(HDF_VIBRATOR_MODE_PRESET);
146 EXPECT_EQ(endRet, HDF_SUCCESS);
147 }
148
149 /**
150 * @tc.name: ExecuteVibratorEffect004
151 * @tc.desc: Controls this Performing Time Series Vibrator Effects.
152 * Controls this vibrator to stop the vibrator.
153 * @tc.type: FUNC
154 */
155 HWTEST_F(HdfVibratorHdiServiceTest, SUB_Driver_Sensor_HdiVibrator_0600, Function | MediumTest | Level1)
156 {
157 ASSERT_NE(nullptr, g_vibratorInterface);
158
159 int32_t startRet = g_vibratorInterface->Start(g_timeSequence);
160 EXPECT_EQ(startRet, HDF_SUCCESS);
161
162 OsalMSleep(g_sleepTime2);
163
164 int32_t endRet = g_vibratorInterface->Stop(HDF_VIBRATOR_MODE_BUTT);
165 EXPECT_EQ(endRet, HDF_ERR_INVALID_PARAM);
166
167 endRet = g_vibratorInterface->Stop(HDF_VIBRATOR_MODE_PRESET);
168 EXPECT_EQ(endRet, HDF_SUCCESS);
169 }
170
171 /**
172 * @tc.name: ExecuteVibratorEffect005
173 * @tc.desc: Controls this vibrator to stop the vibrator.
174 * Controls this Performing Time Series Vibrator Effects.
175 * Controls this vibrator to stop the vibrator.
176 * @tc.type: FUNC
177 */
178 HWTEST_F(HdfVibratorHdiServiceTest, SUB_Driver_Sensor_HdiVibrator_0700, Function | MediumTest | Level1)
179 {
180 ASSERT_NE(nullptr, g_vibratorInterface);
181
182 int32_t startRet = g_vibratorInterface->Start(g_timeSequence);
183 EXPECT_EQ(startRet, HDF_SUCCESS);
184
185 OsalMSleep(g_sleepTime2);
186
187 int32_t endRet = g_vibratorInterface->Stop(HDF_VIBRATOR_MODE_PRESET);
188 EXPECT_EQ(endRet, HDF_SUCCESS);
189 }
190
191 /**
192 * @tc.name: ExecuteVibratorEffect006
193 * @tc.desc: Controls this vibrator to stop the vibrator.
194 * Controls this Perform built-in Vibrator Effects.
195 * Controls this vibrator to stop the vibrator.
196 * @tc.type: FUNC
197 */
198 HWTEST_F(HdfVibratorHdiServiceTest, SUB_Driver_Sensor_HdiVibrator_0800, Function | MediumTest | Level1)
199 {
200 ASSERT_NE(nullptr, g_vibratorInterface);
201
202 int32_t startRet = g_vibratorInterface->Start(g_builtIn);
203 EXPECT_EQ(startRet, HDF_SUCCESS);
204
205 OsalMSleep(g_sleepTime2);
206
207 int32_t endRet = g_vibratorInterface->Stop(HDF_VIBRATOR_MODE_PRESET);
208 EXPECT_EQ(endRet, HDF_SUCCESS);
209 }
210
211 /**
212 * @tc.name: ExecuteVibratorEffect007
213 * @tc.desc: Controls this Perform a one-shot vibrator with a arbitrary string.
214 * Controls this vibrator to stop the vibrator.
215 * @tc.type: FUNC
216 */
217 HWTEST_F(HdfVibratorHdiServiceTest, SUB_Driver_Sensor_HdiVibrator_0900, Function | MediumTest | Level1)
218 {
219 ASSERT_NE(nullptr, g_vibratorInterface);
220
221 int32_t startRet = g_vibratorInterface->Start(g_arbitraryStr);
222 EXPECT_EQ(startRet, HDF_ERR_INVALID_PARAM);
223
224 int32_t endRet = g_vibratorInterface->Stop(HDF_VIBRATOR_MODE_ONCE);
225 EXPECT_EQ(endRet, HDF_SUCCESS);
226 }
227
228 /**
229 * @tc.name: GetVibratorInfo_001
230 * @tc.desc: Obtain the vibrator setting strength, frequency capability and
231 * range in the system. Validity check of input parameters.
232 * @tc.type: FUNC
233 */
234 HWTEST_F(HdfVibratorHdiServiceTest, SUB_Driver_Sensor_HdiVibrator_1000, Function | MediumTest | Level1) {
235 ASSERT_NE(nullptr, g_vibratorInterface);
236
237 std::vector<HdfVibratorInfo> info;
238
239 int32_t startRet = g_vibratorInterface->GetVibratorInfo(info);
240 EXPECT_EQ(startRet, HDF_SUCCESS);
241
242 printf("intensity = %d, intensityMaxValue = %d, intensityMinValue = %d\n\t",
243 info[0].isSupportIntensity, info[0].intensityMaxValue, info[0].intensityMinValue);
244 printf("frequency = %d, intensityMaxValue = %d, intensityMinValue = %d\n\t",
245 info[0].isSupportFrequency, info[0].frequencyMaxValue, info[0].frequencyMinValue);
246 }
247
248 /**
249 * @tc.name: EnableVibratorModulation_001
250 * @tc.desc: Start vibrator based on the setting vibration effect.
251 * @tc.type: FUNC
252 */
253 HWTEST_F(HdfVibratorHdiServiceTest, SUB_Driver_Sensor_HdiVibrator_1100, Function | MediumTest | Level1) {
254 ASSERT_NE(nullptr, g_vibratorInterface);
255
256 std::vector<HdfVibratorInfo> info;
257
258 int32_t startRet = g_vibratorInterface->GetVibratorInfo(info);
259 EXPECT_EQ(startRet, HDF_SUCCESS);
260
261 if ((info[0].isSupportIntensity == 1) || (info[0].isSupportFrequency == 1)) {
262 EXPECT_GT(g_duration, 0);
263 EXPECT_GE(g_intensity1, info[0].intensityMinValue);
264 EXPECT_LE(g_intensity1, info[0].intensityMaxValue);
265 EXPECT_GE(g_frequency1, info[0].frequencyMinValue);
266 EXPECT_LE(g_frequency1, info[0].frequencyMaxValue);
267
268 startRet = g_vibratorInterface->EnableVibratorModulation(g_duration, g_intensity1, g_frequency1);
269 EXPECT_EQ(startRet, HDF_SUCCESS);
270 OsalMSleep(g_sleepTime1);
271 startRet = g_vibratorInterface->Stop(HDF_VIBRATOR_MODE_ONCE);
272 EXPECT_EQ(startRet, HDF_SUCCESS);
273 }
274 }
275
276 /**
277 * @tc.name: EnableVibratorModulation_002
278 * @tc.desc: Start vibrator based on the setting vibration effect.
279 * @tc.type: FUNC
280 */
281 HWTEST_F(HdfVibratorHdiServiceTest, SUB_Driver_Sensor_HdiVibrator_1200, Function | MediumTest | Level1) {
282 ASSERT_NE(nullptr, g_vibratorInterface);
283
284 std::vector<HdfVibratorInfo> info;
285
286 int32_t startRet = g_vibratorInterface->GetVibratorInfo(info);
287 EXPECT_EQ(startRet, HDF_SUCCESS);
288
289 if ((info[0].isSupportIntensity == 1) || (info[0].isSupportFrequency == 1)) {
290 startRet = g_vibratorInterface->EnableVibratorModulation(g_noDuration, g_intensity1, g_frequency1);
291 EXPECT_EQ(startRet, -1);
292 }
293 }
294
295 /**
296 * @tc.name: EnableVibratorModulation_003
297 * @tc.desc: Start vibrator based on the setting vibration effect.
298 * @tc.type: FUNC
299 */
300 HWTEST_F(HdfVibratorHdiServiceTest, SUB_Driver_Sensor_HdiVibrator_1300, Function | MediumTest | Level1) {
301 ASSERT_NE(nullptr, g_vibratorInterface);
302
303 std::vector<HdfVibratorInfo> info;
304
305 int32_t startRet = g_vibratorInterface->GetVibratorInfo(info);
306 EXPECT_EQ(startRet, HDF_SUCCESS);
307
308 if ((info[0].isSupportIntensity == 1) || (info[0].isSupportFrequency == 1)) {
309 startRet = g_vibratorInterface->EnableVibratorModulation(g_duration, g_intensity2, g_frequency1);
310 EXPECT_EQ(startRet, -2);
311 }
312 }
313
314 /**
315 * @tc.name: EnableVibratorModulation_004
316 * @tc.desc: Start vibrator based on the setting vibration effect.
317 * @tc.type: FUNC
318 */
319 HWTEST_F(HdfVibratorHdiServiceTest, SUB_Driver_Sensor_HdiVibrator_1400, Function | MediumTest | Level1) {
320 ASSERT_NE(nullptr, g_vibratorInterface);
321
322 std::vector<HdfVibratorInfo> info;
323
324 int32_t startRet = g_vibratorInterface->GetVibratorInfo(info);
325 EXPECT_EQ(startRet, HDF_SUCCESS);
326
327 if ((info[0].isSupportIntensity == 1) || (info[0].isSupportFrequency == 1)) {
328 startRet = g_vibratorInterface->EnableVibratorModulation(g_duration, g_intensity1, g_frequency2);
329 EXPECT_EQ(startRet, -3);
330 }
331 }
332 }