1 /*
2 * Copyright (c) 2021-2024 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 <fcntl.h>
17 #include <gtest/gtest.h>
18 #include <string>
19 #include <thread>
20
21 #include "accesstoken_kit.h"
22 #include "nativetoken_kit.h"
23 #include "parameters.h"
24 #include "token_setproc.h"
25
26 #include "sensors_errors.h"
27 #include "vibrator_agent.h"
28
29 #undef LOG_TAG
30 #define LOG_TAG "VibratorAgentTest"
31
32 namespace OHOS {
33 namespace Sensors {
34 using namespace testing::ext;
35 using namespace Security::AccessToken;
36 using Security::AccessToken::AccessTokenID;
37
38 namespace {
39 constexpr int32_t TIME_WAIT_FOR_OP = 500;
40 constexpr int32_t TIME_WAIT_FOR_OP_EACH_CASE = 200;
41 constexpr int32_t INTENSITY_HIGH = 100;
42 constexpr int32_t INTENSITY_MEDIUM = 50;
43 constexpr int32_t INTENSITY_LOW = 20;
44 constexpr int32_t INTENSITY_INVALID = -1;
45
46 PermissionStateFull g_infoManagerTestState = {
47 .grantFlags = {1},
48 .grantStatus = {PermissionState::PERMISSION_GRANTED},
49 .isGeneral = true,
50 .permissionName = "ohos.permission.VIBRATE",
51 .resDeviceID = {"local"}
52 };
53
54 HapPolicyParams g_infoManagerTestPolicyPrams = {
55 .apl = APL_NORMAL,
56 .domain = "test.domain",
57 .permList = {},
58 .permStateList = {g_infoManagerTestState}
59 };
60
61 HapInfoParams g_infoManagerTestInfoParms = {
62 .bundleName = "vibratoragent_test",
63 .userID = 1,
64 .instIndex = 0,
65 .appIDDesc = "vibratorAgentTest"
66 };
67 } // namespace
68
69 class VibratorAgentTest : public testing::Test {
70 public:
71 static void SetUpTestCase();
72 static void TearDownTestCase();
73 void SetUp();
74 void TearDown();
75
76 private:
77 static AccessTokenID tokenID_;
78 };
79
80 struct FileDescriptor {
FileDescriptorOHOS::Sensors::FileDescriptor81 explicit FileDescriptor(const std::string &path)
82 {
83 fd = open(path.c_str(), O_RDONLY);
84 }
~FileDescriptorOHOS::Sensors::FileDescriptor85 ~FileDescriptor()
86 {
87 close(fd);
88 }
89 int32_t fd;
90 };
91
92 AccessTokenID VibratorAgentTest::tokenID_ = 0;
93
SetUpTestCase()94 void VibratorAgentTest::SetUpTestCase()
95 {
96 AccessTokenIDEx tokenIdEx = {0};
97 tokenIdEx = AccessTokenKit::AllocHapToken(g_infoManagerTestInfoParms, g_infoManagerTestPolicyPrams);
98 tokenID_ = tokenIdEx.tokenIdExStruct.tokenID;
99 ASSERT_NE(0, tokenID_);
100 ASSERT_EQ(0, SetSelfTokenID(tokenID_));
101 }
102
TearDownTestCase()103 void VibratorAgentTest::TearDownTestCase()
104 {
105 int32_t ret = AccessTokenKit::DeleteToken(tokenID_);
106 if (tokenID_ != 0) {
107 ASSERT_EQ(RET_SUCCESS, ret);
108 }
109 }
110
SetUp()111 void VibratorAgentTest::SetUp()
112 {}
113
TearDown()114 void VibratorAgentTest::TearDown()
115 {
116 std::this_thread::sleep_for(std::chrono::milliseconds(TIME_WAIT_FOR_OP_EACH_CASE));
117 }
118
IsSupportVibratorEffect(const char * effectId)119 bool IsSupportVibratorEffect(const char* effectId)
120 {
121 bool state { false };
122 IsSupportEffect(effectId, &state);
123 return state;
124 }
125
126 HWTEST_F(VibratorAgentTest, StartVibratorTest_001, TestSize.Level1)
127 {
128 MISC_HILOGI("StartVibratorTest_001 in");
129 if (IsSupportVibratorEffect(VIBRATOR_TYPE_CLOCK_TIMER)) {
130 int32_t ret = StartVibrator(VIBRATOR_TYPE_CLOCK_TIMER);
131 ASSERT_EQ(ret, 0);
132 } else {
133 ASSERT_EQ(0, 0);
134 }
135 }
136
137 HWTEST_F(VibratorAgentTest, StartVibratorTest_002, TestSize.Level1)
138 {
139 MISC_HILOGI("StartVibratorTest_002 in");
140 int32_t ret = StartVibrator("");
141 ASSERT_NE(ret, 0);
142 }
143
144 HWTEST_F(VibratorAgentTest, StartVibratorTest_003, TestSize.Level1)
145 {
146 MISC_HILOGI("StartVibratorTest_003 in");
147 int32_t ret = StartVibrator(nullptr);
148 ASSERT_NE(ret, 0);
149 }
150
151 HWTEST_F(VibratorAgentTest, StartVibratorOnceTest_001, TestSize.Level1)
152 {
153 MISC_HILOGI("StartVibratorOnceTest_001 in");
154 int32_t ret = StartVibratorOnce(300);
155 ASSERT_EQ(ret, 0);
156 }
157
158 HWTEST_F(VibratorAgentTest, StartVibratorOnceTest_002, TestSize.Level1)
159 {
160 MISC_HILOGI("StartVibratorOnceTest_002 in");
161 int32_t ret = StartVibratorOnce(0);
162 ASSERT_NE(ret, 0);
163 }
164
165 HWTEST_F(VibratorAgentTest, StartVibratorOnceTest_003, TestSize.Level1)
166 {
167 MISC_HILOGI("StartVibratorOnceTest_003 in");
168 int32_t ret = StartVibratorOnce(1800000);
169 ASSERT_EQ(ret, 0);
170 }
171
172 HWTEST_F(VibratorAgentTest, StartVibratorOnceTest_004, TestSize.Level1)
173 {
174 MISC_HILOGI("StartVibratorOnceTest_004 in");
175 int32_t ret = StartVibratorOnce(1800001);
176 ASSERT_NE(ret, 0);
177 }
178
179 HWTEST_F(VibratorAgentTest, StopVibratorTest_001, TestSize.Level1)
180 {
181 MISC_HILOGI("StopVibratorTest_001 in");
182 int32_t ret = StopVibrator("time");
183 ASSERT_EQ(ret, 0);
184 }
185
186 HWTEST_F(VibratorAgentTest, StopVibratorTest_002, TestSize.Level1)
187 {
188 MISC_HILOGI("StopVibratorTest_002 in");
189 int32_t ret = StopVibrator("preset");
190 ASSERT_NE(ret, 0);
191 }
192
193 HWTEST_F(VibratorAgentTest, StopVibratorTest_003, TestSize.Level1)
194 {
195 MISC_HILOGI("StopVibratorTest_003 in");
196 int32_t ret = StopVibrator("");
197 ASSERT_NE(ret, 0);
198 }
199
200 HWTEST_F(VibratorAgentTest, StopVibratorTest_004, TestSize.Level1)
201 {
202 MISC_HILOGI("StopVibratorTest_004 in");
203 int32_t ret = StopVibrator(nullptr);
204 ASSERT_NE(ret, 0);
205 }
206
207 HWTEST_F(VibratorAgentTest, StopVibratorTest_005, TestSize.Level1)
208 {
209 MISC_HILOGI("StopVibratorTest_005 in");
210 int32_t ret = StartVibratorOnce(300);
211 ASSERT_EQ(ret, 0);
212 ret = StopVibrator("time");
213 ASSERT_EQ(ret, 0);
214 }
215
216 HWTEST_F(VibratorAgentTest, SetLoopCount_001, TestSize.Level1)
217 {
218 MISC_HILOGI("SetLoopCount_001 in");
219 bool ret = SetLoopCount(300);
220 ASSERT_TRUE(ret);
221 }
222
223 HWTEST_F(VibratorAgentTest, SetLoopCount_002, TestSize.Level1)
224 {
225 MISC_HILOGI("SetLoopCount_002 in");
226 bool ret = SetLoopCount(-1);
227 ASSERT_FALSE(ret);
228 }
229
230 HWTEST_F(VibratorAgentTest, SetLoopCount_003, TestSize.Level1)
231 {
232 MISC_HILOGI("SetLoopCount_003 in");
233 bool ret = SetLoopCount(0);
234 ASSERT_FALSE(ret);
235 }
236
237 HWTEST_F(VibratorAgentTest, SetUsage_001, TestSize.Level1)
238 {
239 MISC_HILOGI("SetUsage_001 in");
240 bool ret = SetUsage(0);
241 ASSERT_TRUE(ret);
242 }
243
244 HWTEST_F(VibratorAgentTest, SetUsage_002, TestSize.Level1)
245 {
246 MISC_HILOGI("SetUsage_002 in");
247 bool ret = SetUsage(-1);
248 ASSERT_FALSE(ret);
249 }
250
251 HWTEST_F(VibratorAgentTest, SetUsage_003, TestSize.Level1)
252 {
253 MISC_HILOGI("SetUsage_003 in");
254 bool ret = SetUsage(USAGE_MAX);
255 ASSERT_FALSE(ret);
256 }
257
258 HWTEST_F(VibratorAgentTest, PlayVibratorCustom_001, TestSize.Level1)
259 {
260 MISC_HILOGI("PlayVibratorCustom_001 in");
261 if (IsSupportVibratorCustom()) {
262 FileDescriptor fileDescriptor("/data/test/vibrator/coin_drop.json");
263 MISC_HILOGD("Test fd:%{public}d", fileDescriptor.fd);
264 struct stat64 statbuf = { 0 };
265 if (fstat64(fileDescriptor.fd, &statbuf) == 0) {
266 int32_t ret = PlayVibratorCustom(fileDescriptor.fd, 0, statbuf.st_size);
267 ASSERT_EQ(ret, 0);
268 }
269 std::this_thread::sleep_for(std::chrono::milliseconds(TIME_WAIT_FOR_OP));
270 } else {
271 ASSERT_EQ(0, 0);
272 }
273 Cancel();
274 }
275
276 HWTEST_F(VibratorAgentTest, PlayVibratorCustom_002, TestSize.Level1)
277 {
278 MISC_HILOGI("PlayVibratorCustom_002 in");
279 if (IsSupportVibratorCustom()) {
280 FileDescriptor fileDescriptor("/data/test/vibrator/on_carpet.json");
281 MISC_HILOGD("Test fd:%{public}d", fileDescriptor.fd);
282 struct stat64 statbuf = { 0 };
283 if (fstat64(fileDescriptor.fd, &statbuf) == 0) {
284 int32_t ret = PlayVibratorCustom(fileDescriptor.fd, 0, statbuf.st_size);
285 ASSERT_EQ(ret, 0);
286 }
287 std::this_thread::sleep_for(std::chrono::milliseconds(TIME_WAIT_FOR_OP));
288 } else {
289 ASSERT_EQ(0, 0);
290 }
291 Cancel();
292 }
293
294 HWTEST_F(VibratorAgentTest, PlayVibratorCustom_003, TestSize.Level1)
295 {
296 MISC_HILOGI("PlayVibratorCustom_003 in");
297 if (IsSupportVibratorCustom() && IsSupportVibratorEffect(VIBRATOR_TYPE_FAIL)) {
298 bool flag = SetLoopCount(2);
299 ASSERT_TRUE(flag);
300 int32_t ret = StartVibrator(VIBRATOR_TYPE_FAIL);
301 ASSERT_EQ(ret, 0);
302 FileDescriptor fileDescriptor("/data/test/vibrator/on_carpet.json");
303 MISC_HILOGD("Test fd:%{public}d", fileDescriptor.fd);
304 struct stat64 statbuf = { 0 };
305 if (fstat64(fileDescriptor.fd, &statbuf) == 0) {
306 ret = PlayVibratorCustom(fileDescriptor.fd, 0, statbuf.st_size);
307 ASSERT_NE(ret, 0);
308 }
309 std::this_thread::sleep_for(std::chrono::milliseconds(TIME_WAIT_FOR_OP));
310 } else {
311 ASSERT_EQ(0, 0);
312 }
313 Cancel();
314 }
315
316 HWTEST_F(VibratorAgentTest, PlayVibratorCustom_004, TestSize.Level1)
317 {
318 MISC_HILOGI("PlayVibratorCustom_004 in");
319 if (IsSupportVibratorCustom() && IsSupportVibratorEffect(VIBRATOR_TYPE_FAIL)) {
320 bool flag = SetUsage(USAGE_ALARM);
321 ASSERT_TRUE(flag);
322 int32_t ret = StartVibrator(VIBRATOR_TYPE_FAIL);
323 ASSERT_EQ(ret, 0);
324 FileDescriptor fileDescriptor("/data/test/vibrator/on_carpet.json");
325 MISC_HILOGD("Test fd:%{public}d", fileDescriptor.fd);
326 struct stat64 statbuf = { 0 };
327 if (fstat64(fileDescriptor.fd, &statbuf) == 0) {
328 ret = PlayVibratorCustom(fileDescriptor.fd, 0, statbuf.st_size);
329 ASSERT_NE(ret, 0);
330 }
331 std::this_thread::sleep_for(std::chrono::milliseconds(TIME_WAIT_FOR_OP));
332 } else {
333 ASSERT_EQ(0, 0);
334 }
335 Cancel();
336 }
337
338 HWTEST_F(VibratorAgentTest, PlayVibratorCustom_005, TestSize.Level1)
339 {
340 MISC_HILOGI("PlayVibratorCustom_005 in");
341 if (IsSupportVibratorCustom() && IsSupportVibratorEffect(VIBRATOR_TYPE_FAIL)) {
342 bool flag = SetUsage(USAGE_UNKNOWN);
343 ASSERT_TRUE(flag);
344 int32_t ret = StartVibrator(VIBRATOR_TYPE_FAIL);
345 ASSERT_EQ(ret, 0);
346 FileDescriptor fileDescriptor("/data/test/vibrator/on_carpet.json");
347 MISC_HILOGD("Test fd:%{public}d", fileDescriptor.fd);
348 struct stat64 statbuf = { 0 };
349 if (fstat64(fileDescriptor.fd, &statbuf) == 0) {
350 ret = PlayVibratorCustom(fileDescriptor.fd, 0, statbuf.st_size);
351 ASSERT_EQ(ret, 0);
352 }
353 std::this_thread::sleep_for(std::chrono::milliseconds(TIME_WAIT_FOR_OP));
354 } else {
355 ASSERT_EQ(0, 0);
356 }
357 Cancel();
358 }
359
360 HWTEST_F(VibratorAgentTest, PlayVibratorCustom_006, TestSize.Level1)
361 {
362 MISC_HILOGI("PlayVibratorCustom_006 in");
363 if (IsSupportVibratorCustom() && IsSupportVibratorEffect(VIBRATOR_TYPE_FAIL)) {
364 FileDescriptor fileDescriptor("/data/test/vibrator/on_carpet.json");
365 MISC_HILOGD("Test fd:%{public}d", fileDescriptor.fd);
366 struct stat64 statbuf = { 0 };
367 if (fstat64(fileDescriptor.fd, &statbuf) == 0) {
368 bool flag = SetUsage(USAGE_ALARM);
369 ASSERT_TRUE(flag);
370 int32_t ret = PlayVibratorCustom(fileDescriptor.fd, 0, statbuf.st_size);
371 ASSERT_EQ(ret, 0);
372 ret = StartVibrator(VIBRATOR_TYPE_FAIL);
373 ASSERT_NE(ret, 0);
374 }
375 std::this_thread::sleep_for(std::chrono::milliseconds(TIME_WAIT_FOR_OP));
376 } else {
377 ASSERT_EQ(0, 0);
378 }
379 Cancel();
380 }
381
382 HWTEST_F(VibratorAgentTest, PlayVibratorCustom_007, TestSize.Level1)
383 {
384 MISC_HILOGI("PlayVibratorCustom_007 in");
385 if (IsSupportVibratorCustom() && IsSupportVibratorEffect(VIBRATOR_TYPE_FAIL)) {
386 FileDescriptor fileDescriptor("/data/test/vibrator/on_carpet.json");
387 MISC_HILOGD("Test fd:%{public}d", fileDescriptor.fd);
388 struct stat64 statbuf = { 0 };
389 if (fstat64(fileDescriptor.fd, &statbuf) == 0) {
390 bool flag = SetUsage(USAGE_UNKNOWN);
391 ASSERT_TRUE(flag);
392 int32_t ret = PlayVibratorCustom(fileDescriptor.fd, 0, statbuf.st_size);
393 ASSERT_EQ(ret, 0);
394 ret = StartVibrator(VIBRATOR_TYPE_FAIL);
395 ASSERT_EQ(ret, 0);
396 }
397 std::this_thread::sleep_for(std::chrono::milliseconds(TIME_WAIT_FOR_OP));
398 } else {
399 ASSERT_EQ(0, 0);
400 }
401 Cancel();
402 }
403
404 HWTEST_F(VibratorAgentTest, PlayVibratorCustom_008, TestSize.Level1)
405 {
406 MISC_HILOGI("PlayVibratorCustom_008 in");
407 if (IsSupportVibratorCustom()) {
408 bool flag = SetUsage(USAGE_ALARM);
409 ASSERT_TRUE(flag);
410 int32_t ret = StartVibratorOnce(500);
411 ASSERT_EQ(ret, 0);
412 FileDescriptor fileDescriptor("/data/test/vibrator/on_carpet.json");
413 MISC_HILOGD("Test fd:%{public}d", fileDescriptor.fd);
414 struct stat64 statbuf = { 0 };
415 if (fstat64(fileDescriptor.fd, &statbuf) == 0) {
416 ret = PlayVibratorCustom(fileDescriptor.fd, 0, statbuf.st_size);
417 ASSERT_NE(ret, 0);
418 }
419 std::this_thread::sleep_for(std::chrono::milliseconds(TIME_WAIT_FOR_OP));
420 } else {
421 ASSERT_EQ(0, 0);
422 }
423 Cancel();
424 }
425
426 HWTEST_F(VibratorAgentTest, PlayVibratorCustom_009, TestSize.Level1)
427 {
428 MISC_HILOGI("PlayVibratorCustom_009 in");
429 if (IsSupportVibratorCustom()) {
430 bool flag = SetUsage(USAGE_UNKNOWN);
431 ASSERT_TRUE(flag);
432 int32_t ret = StartVibratorOnce(500);
433 ASSERT_EQ(ret, 0);
434 FileDescriptor fileDescriptor("/data/test/vibrator/on_carpet.json");
435 MISC_HILOGD("Test fd:%{public}d", fileDescriptor.fd);
436 struct stat64 statbuf = { 0 };
437 if (fstat64(fileDescriptor.fd, &statbuf) == 0) {
438 ret = PlayVibratorCustom(fileDescriptor.fd, 0, statbuf.st_size);
439 ASSERT_EQ(ret, 0);
440 }
441 std::this_thread::sleep_for(std::chrono::milliseconds(TIME_WAIT_FOR_OP));
442 } else {
443 ASSERT_EQ(0, 0);
444 }
445 Cancel();
446 }
447
448 HWTEST_F(VibratorAgentTest, PlayVibratorCustom_010, TestSize.Level1)
449 {
450 MISC_HILOGI("PlayVibratorCustom_010 in");
451 if (IsSupportVibratorCustom()) {
452 FileDescriptor fileDescriptor("/data/test/vibrator/on_carpet.json");
453 MISC_HILOGD("Test fd:%{public}d", fileDescriptor.fd);
454 struct stat64 statbuf = { 0 };
455 if (fstat64(fileDescriptor.fd, &statbuf) == 0) {
456 bool flag = SetUsage(USAGE_ALARM);
457 ASSERT_TRUE(flag);
458 int32_t ret = PlayVibratorCustom(fileDescriptor.fd, 0, statbuf.st_size);
459 ASSERT_EQ(ret, 0);
460 ret = StartVibratorOnce(500);
461 ASSERT_NE(ret, 0);
462 }
463 std::this_thread::sleep_for(std::chrono::milliseconds(TIME_WAIT_FOR_OP));
464 } else {
465 ASSERT_EQ(0, 0);
466 }
467 Cancel();
468 }
469
470 HWTEST_F(VibratorAgentTest, PlayVibratorCustom_011, TestSize.Level1)
471 {
472 MISC_HILOGI("PlayVibratorCustom_011 in");
473 if (IsSupportVibratorCustom()) {
474 FileDescriptor fileDescriptor("/data/test/vibrator/on_carpet.json");
475 MISC_HILOGD("Test fd:%{public}d", fileDescriptor.fd);
476 struct stat64 statbuf = { 0 };
477 if (fstat64(fileDescriptor.fd, &statbuf) == 0) {
478 bool flag = SetUsage(USAGE_UNKNOWN);
479 ASSERT_TRUE(flag);
480 int32_t ret = PlayVibratorCustom(fileDescriptor.fd, 0, statbuf.st_size);
481 ASSERT_EQ(ret, 0);
482 ret = StartVibratorOnce(500);
483 ASSERT_EQ(ret, 0);
484 }
485 std::this_thread::sleep_for(std::chrono::milliseconds(TIME_WAIT_FOR_OP));
486 } else {
487 ASSERT_EQ(0, 0);
488 }
489 Cancel();
490 }
491
492 HWTEST_F(VibratorAgentTest, PlayVibratorCustom_012, TestSize.Level1)
493 {
494 MISC_HILOGI("PlayVibratorCustom_012 in");
495 if (IsSupportVibratorCustom()) {
496 FileDescriptor fileDescriptor("/data/test/vibrator/test_128_event.json");
497 MISC_HILOGD("Test fd:%{public}d", fileDescriptor.fd);
498 struct stat64 statbuf = { 0 };
499 if (fstat64(fileDescriptor.fd, &statbuf) == 0) {
500 int32_t ret = PlayVibratorCustom(fileDescriptor.fd, 0, statbuf.st_size);
501 ASSERT_EQ(ret, 0);
502 }
503 std::this_thread::sleep_for(std::chrono::milliseconds(TIME_WAIT_FOR_OP));
504 } else {
505 ASSERT_EQ(0, 0);
506 }
507 Cancel();
508 }
509
510 HWTEST_F(VibratorAgentTest, PlayVibratorCustom_013, TestSize.Level1)
511 {
512 MISC_HILOGI("PlayVibratorCustom_013 in");
513 if (IsSupportVibratorCustom()) {
514 FileDescriptor fileDescriptor("/data/test/vibrator/test_invalid_type.json");
515 MISC_HILOGD("Test fd:%{public}d", fileDescriptor.fd);
516 struct stat64 statbuf = { 0 };
517 if (fstat64(fileDescriptor.fd, &statbuf) == 0) {
518 int32_t ret = PlayVibratorCustom(fileDescriptor.fd, 0, statbuf.st_size);
519 ASSERT_NE(ret, 0);
520 }
521 } else {
522 ASSERT_EQ(0, 0);
523 }
524 }
525
526 HWTEST_F(VibratorAgentTest, PlayVibratorCustom_014, TestSize.Level1)
527 {
528 MISC_HILOGI("PlayVibratorCustom_014 in");
529 if (IsSupportVibratorCustom()) {
530 FileDescriptor fileDescriptor("/data/test/vibrator/test_invalid_startTime.json");
531 MISC_HILOGD("Test fd:%{public}d", fileDescriptor.fd);
532 struct stat64 statbuf = { 0 };
533 if (fstat64(fileDescriptor.fd, &statbuf) == 0) {
534 int32_t ret = PlayVibratorCustom(fileDescriptor.fd, 0, statbuf.st_size);
535 ASSERT_NE(ret, 0);
536 }
537 } else {
538 ASSERT_EQ(0, 0);
539 }
540 }
541
542 HWTEST_F(VibratorAgentTest, PlayVibratorCustom_015, TestSize.Level1)
543 {
544 MISC_HILOGI("PlayVibratorCustom_015 in");
545 if (IsSupportVibratorCustom()) {
546 FileDescriptor fileDescriptor("/data/test/vibrator/test_invalid_duration.json");
547 MISC_HILOGD("Test fd:%{public}d", fileDescriptor.fd);
548 struct stat64 statbuf = { 0 };
549 if (fstat64(fileDescriptor.fd, &statbuf) == 0) {
550 int32_t ret = PlayVibratorCustom(fileDescriptor.fd, 0, statbuf.st_size);
551 ASSERT_NE(ret, 0);
552 }
553 } else {
554 ASSERT_EQ(0, 0);
555 }
556 }
557
558 HWTEST_F(VibratorAgentTest, PlayVibratorCustom_016, TestSize.Level1)
559 {
560 MISC_HILOGI("PlayVibratorCustom_016 in");
561 if (IsSupportVibratorCustom()) {
562 FileDescriptor fileDescriptor("/data/test/vibrator/test_invalid_intensity.json");
563 MISC_HILOGD("Test fd:%{public}d", fileDescriptor.fd);
564 struct stat64 statbuf = { 0 };
565 if (fstat64(fileDescriptor.fd, &statbuf) == 0) {
566 int32_t ret = PlayVibratorCustom(fileDescriptor.fd, 0, statbuf.st_size);
567 ASSERT_NE(ret, 0);
568 }
569 } else {
570 ASSERT_EQ(0, 0);
571 }
572 }
573
574 HWTEST_F(VibratorAgentTest, PlayVibratorCustom_017, TestSize.Level1)
575 {
576 MISC_HILOGI("PlayVibratorCustom_017 in");
577 if (IsSupportVibratorCustom()) {
578 FileDescriptor fileDescriptor("/data/test/vibrator/test_invalid_frequency.json");
579 MISC_HILOGD("Test fd:%{public}d", fileDescriptor.fd);
580 struct stat64 statbuf = { 0 };
581 if (fstat64(fileDescriptor.fd, &statbuf) == 0) {
582 int32_t ret = PlayVibratorCustom(fileDescriptor.fd, 0, statbuf.st_size);
583 ASSERT_NE(ret, 0);
584 }
585 } else {
586 ASSERT_EQ(0, 0);
587 }
588 }
589
590 HWTEST_F(VibratorAgentTest, PlayVibratorCustom_018, TestSize.Level1)
591 {
592 MISC_HILOGI("PlayVibratorCustom_018 in");
593 if (IsSupportVibratorCustom()) {
594 FileDescriptor fileDescriptor("/data/test/vibrator/test_129_event.json");
595 MISC_HILOGD("Test fd:%{public}d", fileDescriptor.fd);
596 struct stat64 statbuf = { 0 };
597 if (fstat64(fileDescriptor.fd, &statbuf) == 0) {
598 int32_t ret = PlayVibratorCustom(fileDescriptor.fd, 0, statbuf.st_size);
599 ASSERT_NE(ret, 0);
600 }
601 } else {
602 ASSERT_EQ(0, 0);
603 }
604 }
605
606 HWTEST_F(VibratorAgentTest, PlayVibratorCustom_019, TestSize.Level1)
607 {
608 MISC_HILOGI("PlayVibratorCustom_019 in");
609 if (IsSupportVibratorCustom()) {
610 FileDescriptor fileDescriptor("/data/test/vibrator/test_big_file_size.json");
611 MISC_HILOGD("Test fd:%{public}d", fileDescriptor.fd);
612 struct stat64 statbuf = { 0 };
613 if (fstat64(fileDescriptor.fd, &statbuf) == 0) {
614 int32_t ret = PlayVibratorCustom(fileDescriptor.fd, 0, statbuf.st_size);
615 ASSERT_NE(ret, 0);
616 }
617 } else {
618 ASSERT_EQ(0, 0);
619 }
620 }
621
622 HWTEST_F(VibratorAgentTest, PlayVibratorCustom_020, TestSize.Level1)
623 {
624 MISC_HILOGI("PlayVibratorCustom_020 in");
625 if (IsSupportVibratorCustom()) {
626 FileDescriptor fileDescriptor("/data/test/vibrator/test_event_overlap_1.json");
627 MISC_HILOGD("Test fd:%{public}d", fileDescriptor.fd);
628 struct stat64 statbuf = { 0 };
629 if (fstat64(fileDescriptor.fd, &statbuf) == 0) {
630 int32_t ret = PlayVibratorCustom(fileDescriptor.fd, 0, statbuf.st_size);
631 ASSERT_EQ(ret, 0);
632 }
633 } else {
634 ASSERT_EQ(0, 0);
635 }
636 Cancel();
637 }
638
639 HWTEST_F(VibratorAgentTest, PlayVibratorCustom_021, TestSize.Level1)
640 {
641 MISC_HILOGI("PlayVibratorCustom_021 in");
642 if (IsSupportVibratorCustom()) {
643 FileDescriptor fileDescriptor("/data/test/vibrator/test_event_overlap_2.json");
644 MISC_HILOGD("Test fd:%{public}d", fileDescriptor.fd);
645 struct stat64 statbuf = { 0 };
646 if (fstat64(fileDescriptor.fd, &statbuf) == 0) {
647 int32_t ret = PlayVibratorCustom(fileDescriptor.fd, 0, statbuf.st_size);
648 ASSERT_EQ(ret, 0);
649 }
650 } else {
651 ASSERT_EQ(0, 0);
652 }
653 Cancel();
654 }
655
656 HWTEST_F(VibratorAgentTest, PlayVibratorCustom_022, TestSize.Level1)
657 {
658 MISC_HILOGI("PlayVibratorCustom_022 in");
659 if (IsSupportVibratorCustom()) {
660 FileDescriptor fileDescriptor("/data/test/vibrator/Jet_N2O.he");
661 MISC_HILOGD("Test fd:%{public}d", fileDescriptor.fd);
662 struct stat64 statbuf = { 0 };
663 if (fstat64(fileDescriptor.fd, &statbuf) == 0) {
664 int32_t ret = PlayVibratorCustom(fileDescriptor.fd, 0, statbuf.st_size);
665 ASSERT_EQ(ret, 0);
666 }
667 std::this_thread::sleep_for(std::chrono::milliseconds(TIME_WAIT_FOR_OP));
668 } else {
669 ASSERT_EQ(0, 0);
670 }
671 Cancel();
672 }
673
674 HWTEST_F(VibratorAgentTest, PlayVibratorCustom_023, TestSize.Level1)
675 {
676 MISC_HILOGI("PlayVibratorCustom_023 in");
677 if (IsSupportVibratorCustom()) {
678 FileDescriptor fileDescriptor("/data/test/vibrator/Racing_Start.he");
679 MISC_HILOGD("Test fd:%{public}d", fileDescriptor.fd);
680 struct stat64 statbuf = { 0 };
681 if (fstat64(fileDescriptor.fd, &statbuf) == 0) {
682 int32_t ret = PlayVibratorCustom(fileDescriptor.fd, 0, statbuf.st_size);
683 ASSERT_EQ(ret, 0);
684 }
685 std::this_thread::sleep_for(std::chrono::milliseconds(TIME_WAIT_FOR_OP));
686 } else {
687 ASSERT_EQ(0, 0);
688 }
689 Cancel();
690 }
691
692 HWTEST_F(VibratorAgentTest, SetParameters_001, TestSize.Level1)
693 {
694 MISC_HILOGI("SetParameters_001 in");
695 VibratorParameter parameter = {
696 .intensity = -1,
697 .frequency = -15
698 };
699 bool ret = SetParameters(parameter);
700 ASSERT_FALSE(ret);
701 }
702
703 HWTEST_F(VibratorAgentTest, SetParameters_002, TestSize.Level1)
704 {
705 MISC_HILOGI("SetParameters_002 in");
706 VibratorParameter parameter = {
707 .intensity = 70,
708 .frequency = 150
709 };
710 bool ret = SetParameters(parameter);
711 ASSERT_FALSE(ret);
712 }
713
714 HWTEST_F(VibratorAgentTest, SetParameters_003, TestSize.Level1)
715 {
716 MISC_HILOGI("SetParameters_003 in");
717 if (IsSupportVibratorCustom()) {
718 FileDescriptor fileDescriptor("/data/test/vibrator/coin_drop.json");
719 MISC_HILOGD("Test fd:%{public}d", fileDescriptor.fd);
720 struct stat64 statbuf = { 0 };
721 if (fstat64(fileDescriptor.fd, &statbuf) == 0) {
722 VibratorParameter parameter = {
723 .intensity = 50,
724 .frequency = -15
725 };
726 bool flag = SetParameters(parameter);
727 ASSERT_TRUE(flag);
728 int32_t ret = PlayVibratorCustom(fileDescriptor.fd, 0, statbuf.st_size);
729 ASSERT_EQ(ret, 0);
730 }
731 std::this_thread::sleep_for(std::chrono::milliseconds(TIME_WAIT_FOR_OP));
732 } else {
733 ASSERT_EQ(0, 0);
734 }
735 Cancel();
736 }
737
738 HWTEST_F(VibratorAgentTest, SetParameters_004, TestSize.Level1)
739 {
740 MISC_HILOGI("SetParameters_004 in");
741 if (IsSupportVibratorCustom()) {
742 FileDescriptor fileDescriptor("/data/test/vibrator/on_carpet.json");
743 MISC_HILOGD("Test fd:%{public}d", fileDescriptor.fd);
744 struct stat64 statbuf = { 0 };
745 if (fstat64(fileDescriptor.fd, &statbuf) == 0) {
746 VibratorParameter parameter = {
747 .intensity = 33,
748 .frequency = 55
749 };
750 bool flag = SetParameters(parameter);
751 ASSERT_TRUE(flag);
752 int32_t ret = PlayVibratorCustom(fileDescriptor.fd, 0, statbuf.st_size);
753 ASSERT_EQ(ret, 0);
754 }
755 std::this_thread::sleep_for(std::chrono::milliseconds(TIME_WAIT_FOR_OP));
756 } else {
757 ASSERT_EQ(0, 0);
758 }
759 Cancel();
760 }
761
762 HWTEST_F(VibratorAgentTest, Cancel_001, TestSize.Level1)
763 {
764 MISC_HILOGI("Cancel_001 in");
765 int32_t ret = Cancel();
766 ASSERT_NE(ret, 0);
767 }
768
769 HWTEST_F(VibratorAgentTest, Cancel_002, TestSize.Level1)
770 {
771 MISC_HILOGI("Cancel_002 in");
772 if (IsSupportVibratorCustom()) {
773 FileDescriptor fileDescriptor("/data/test/vibrator/coin_drop.json");
774 MISC_HILOGD("Test fd:%{public}d", fileDescriptor.fd);
775 struct stat64 statbuf = { 0 };
776 if (fstat64(fileDescriptor.fd, &statbuf) == 0) {
777 int32_t ret = PlayVibratorCustom(fileDescriptor.fd, 0, statbuf.st_size);
778 ASSERT_EQ(ret, 0);
779 ret = Cancel();
780 ASSERT_EQ(ret, 0);
781 }
782 } else {
783 ASSERT_EQ(0, 0);
784 }
785 }
786
787 HWTEST_F(VibratorAgentTest, Cancel_003, TestSize.Level1)
788 {
789 MISC_HILOGI("Cancel_003 in");
790 int32_t ret = StartVibratorOnce(500);
791 ASSERT_EQ(ret, 0);
792 ret = Cancel();
793 ASSERT_EQ(ret, 0);
794 }
795
796 HWTEST_F(VibratorAgentTest, Cancel_004, TestSize.Level1)
797 {
798 MISC_HILOGI("Cancel_004 in");
799 if (IsSupportVibratorEffect(VIBRATOR_TYPE_FAIL)) {
800 int32_t ret = StartVibrator(VIBRATOR_TYPE_FAIL);
801 ASSERT_EQ(ret, 0);
802 ret = Cancel();
803 ASSERT_EQ(ret, 0);
804 }
805 ASSERT_TRUE(true);
806 }
807
808 HWTEST_F(VibratorAgentTest, IsSupportEffect_001, TestSize.Level1)
809 {
810 MISC_HILOGI("IsSupportEffect_001 in");
811 bool state { false };
812 int32_t ret = IsSupportEffect(VIBRATOR_TYPE_CLOCK_TIMER, &state);
813 ASSERT_EQ(ret, 0);
814 if (state) {
815 ret = StartVibrator(VIBRATOR_TYPE_CLOCK_TIMER);
816 ASSERT_EQ(ret, 0);
817 Cancel();
818 } else {
819 MISC_HILOGI("Do not support %{public}s", VIBRATOR_TYPE_CLOCK_TIMER);
820 }
821 }
822
823 HWTEST_F(VibratorAgentTest, IsSupportEffect_002, TestSize.Level1)
824 {
825 MISC_HILOGI("IsSupportEffect_002 in");
826 bool state { false };
827 int32_t ret = IsSupportEffect("haptic.xxx.yyy", &state);
828 ASSERT_EQ(ret, 0);
829 ASSERT_FALSE(state);
830 }
831
832 HWTEST_F(VibratorAgentTest, IsSupportEffect_003, TestSize.Level1)
833 {
834 MISC_HILOGI("IsSupportEffect_003 in");
835 bool state { false };
836 int32_t ret = IsSupportEffect(nullptr, &state);
837 ASSERT_NE(ret, 0);
838 ASSERT_FALSE(state);
839 }
840
841 HWTEST_F(VibratorAgentTest, IsSupportEffect_004, TestSize.Level1)
842 {
843 MISC_HILOGI("IsSupportEffect_004 in");
844 bool state { false };
845 int32_t ret = IsSupportEffect(VIBRATOR_TYPE_FAIL, &state);
846 ASSERT_EQ(ret, 0);
847 if (state) {
848 ret = StartVibrator(VIBRATOR_TYPE_FAIL);
849 ASSERT_EQ(ret, 0);
850 Cancel();
851 } else {
852 MISC_HILOGI("Do not support %{public}s", VIBRATOR_TYPE_FAIL);
853 }
854 }
855
856 HWTEST_F(VibratorAgentTest, IsSupportEffect_005, TestSize.Level1)
857 {
858 MISC_HILOGI("IsSupportEffect_005 in");
859 bool state { false };
860 int32_t ret = IsSupportEffect(VIBRATOR_TYPE_CHARGING, &state);
861 ASSERT_EQ(ret, 0);
862 if (state) {
863 ret = StartVibrator(VIBRATOR_TYPE_CHARGING);
864 ASSERT_EQ(ret, 0);
865 Cancel();
866 } else {
867 MISC_HILOGI("Do not support %{public}s", VIBRATOR_TYPE_CHARGING);
868 }
869 }
870
871 HWTEST_F(VibratorAgentTest, IsSupportEffect_006, TestSize.Level1)
872 {
873 MISC_HILOGI("IsSupportEffect_006 in");
874 bool state { false };
875 int32_t ret = IsSupportEffect(VIBRATOR_TYPE_LONG_PRESS_HEAVY, &state);
876 ASSERT_EQ(ret, 0);
877 if (state) {
878 ret = StartVibrator(VIBRATOR_TYPE_LONG_PRESS_HEAVY);
879 ASSERT_EQ(ret, 0);
880 Cancel();
881 } else {
882 MISC_HILOGI("Do not support %{public}s", VIBRATOR_TYPE_LONG_PRESS_HEAVY);
883 }
884 }
885
886 HWTEST_F(VibratorAgentTest, IsSupportEffect_007, TestSize.Level1)
887 {
888 MISC_HILOGI("IsSupportEffect_007 in");
889 bool state { false };
890 int32_t ret = IsSupportEffect(VIBRATOR_TYPE_LONG_PRESS_LIGHT, &state);
891 ASSERT_EQ(ret, 0);
892 if (state) {
893 ret = StartVibrator(VIBRATOR_TYPE_LONG_PRESS_LIGHT);
894 ASSERT_EQ(ret, 0);
895 Cancel();
896 } else {
897 MISC_HILOGI("Do not support %{public}s", VIBRATOR_TYPE_LONG_PRESS_LIGHT);
898 }
899 }
900
901 HWTEST_F(VibratorAgentTest, IsSupportEffect_008, TestSize.Level1)
902 {
903 MISC_HILOGI("IsSupportEffect_008 in");
904 bool state { false };
905 int32_t ret = IsSupportEffect(VIBRATOR_TYPE_LONG_PRESS_MEDIUM, &state);
906 ASSERT_EQ(ret, 0);
907 if (state) {
908 ret = StartVibrator(VIBRATOR_TYPE_LONG_PRESS_MEDIUM);
909 ASSERT_EQ(ret, 0);
910 Cancel();
911 } else {
912 MISC_HILOGI("Do not support %{public}s", VIBRATOR_TYPE_LONG_PRESS_MEDIUM);
913 }
914 }
915
916 HWTEST_F(VibratorAgentTest, IsSupportEffect_009, TestSize.Level1)
917 {
918 MISC_HILOGI("IsSupportEffect_009 in");
919 bool state { false };
920 int32_t ret = IsSupportEffect(VIBRATOR_TYPE_SLIDE_LIGHT, &state);
921 ASSERT_EQ(ret, 0);
922 if (state) {
923 ret = StartVibrator(VIBRATOR_TYPE_SLIDE_LIGHT);
924 ASSERT_EQ(ret, 0);
925 Cancel();
926 } else {
927 MISC_HILOGI("Do not support %{public}s", VIBRATOR_TYPE_SLIDE_LIGHT);
928 }
929 }
930
931 HWTEST_F(VibratorAgentTest, IsSupportEffect_010, TestSize.Level1)
932 {
933 MISC_HILOGI("IsSupportEffect_010 in");
934 bool state { false };
935 int32_t ret = IsSupportEffect(VIBRATOR_TYPE_THRESHOID, &state);
936 ASSERT_EQ(ret, 0);
937 if (state) {
938 ret = StartVibrator(VIBRATOR_TYPE_THRESHOID);
939 ASSERT_EQ(ret, 0);
940 Cancel();
941 } else {
942 MISC_HILOGI("Do not support %{public}s", VIBRATOR_TYPE_THRESHOID);
943 }
944 }
945
946 HWTEST_F(VibratorAgentTest, GetDelayTime_001, TestSize.Level1)
947 {
948 MISC_HILOGI("GetDelayTime_001 in");
949 if (IsSupportVibratorCustom()) {
950 int32_t delayTime { -1 };
951 int32_t ret = GetDelayTime(delayTime);
952 ASSERT_EQ(ret, 0);
953 } else {
954 ASSERT_EQ(0, 0);
955 }
956 }
957
958 HWTEST_F(VibratorAgentTest, PreProcess_001, TestSize.Level1)
959 {
960 MISC_HILOGI("PreProcess_001 in");
961 if (IsSupportVibratorCustom()) {
962 FileDescriptor fileDescriptor("invalid_file_name");
963 MISC_HILOGD("fd:%{public}d", fileDescriptor.fd);
964 VibratorFileDescription vfd;
965 VibratorPackage package;
966 struct stat64 statbuf = { 0 };
967 if (fstat64(fileDescriptor.fd, &statbuf) == 0) {
968 vfd.fd = fileDescriptor.fd;
969 vfd.offset = 0;
970 vfd.length = statbuf.st_size;
971 int32_t ret = PreProcess(vfd, package);
972 ASSERT_NE(ret, 0);
973 ret = FreeVibratorPackage(package);
974 ASSERT_NE(ret, 0);
975 } else {
976 ASSERT_EQ(0, 0);
977 }
978 } else {
979 ASSERT_EQ(0, 0);
980 }
981 }
982
983 HWTEST_F(VibratorAgentTest, PreProcess_002, TestSize.Level1)
984 {
985 MISC_HILOGI("PreProcess_002 in");
986 if (IsSupportVibratorCustom()) {
987 FileDescriptor fileDescriptor("/data/test/vibrator/test_invalid_duration.json");
988 MISC_HILOGD("fd:%{public}d", fileDescriptor.fd);
989 VibratorFileDescription vfd;
990 VibratorPackage package;
991 struct stat64 statbuf = { 0 };
992 if (fstat64(fileDescriptor.fd, &statbuf) == 0) {
993 vfd.fd = fileDescriptor.fd;
994 vfd.offset = 0;
995 vfd.length = statbuf.st_size;
996 int32_t ret = PreProcess(vfd, package);
997 ASSERT_NE(ret, 0);
998 ret = FreeVibratorPackage(package);
999 ASSERT_NE(ret, 0);
1000 } else {
1001 ASSERT_FALSE(true);
1002 }
1003 } else {
1004 ASSERT_EQ(0, 0);
1005 }
1006 }
1007
1008 HWTEST_F(VibratorAgentTest, PreProcess_003, TestSize.Level1)
1009 {
1010 MISC_HILOGI("PreProcess_003 in");
1011 if (IsSupportVibratorCustom()) {
1012 FileDescriptor fileDescriptor("/data/test/vibrator/test_invalid_frequency.json");
1013 MISC_HILOGD("fd:%{public}d", fileDescriptor.fd);
1014 VibratorFileDescription vfd;
1015 VibratorPackage package;
1016 struct stat64 statbuf = { 0 };
1017 if (fstat64(fileDescriptor.fd, &statbuf) == 0) {
1018 vfd.fd = fileDescriptor.fd;
1019 vfd.offset = 0;
1020 vfd.length = statbuf.st_size;
1021 int32_t ret = PreProcess(vfd, package);
1022 ASSERT_NE(ret, 0);
1023 ret = FreeVibratorPackage(package);
1024 ASSERT_NE(ret, 0);
1025 } else {
1026 ASSERT_FALSE(true);
1027 }
1028 } else {
1029 ASSERT_EQ(0, 0);
1030 }
1031 }
1032
1033 HWTEST_F(VibratorAgentTest, PreProcess_004, TestSize.Level1)
1034 {
1035 MISC_HILOGI("PreProcess_004 in");
1036 if (IsSupportVibratorCustom()) {
1037 FileDescriptor fileDescriptor("/data/test/vibrator/test_invalid_intensity.json");
1038 MISC_HILOGD("fd:%{public}d", fileDescriptor.fd);
1039 VibratorFileDescription vfd;
1040 VibratorPackage package;
1041 struct stat64 statbuf = { 0 };
1042 if (fstat64(fileDescriptor.fd, &statbuf) == 0) {
1043 vfd.fd = fileDescriptor.fd;
1044 vfd.offset = 0;
1045 vfd.length = statbuf.st_size;
1046 int32_t ret = PreProcess(vfd, package);
1047 ASSERT_NE(ret, 0);
1048 ret = FreeVibratorPackage(package);
1049 ASSERT_NE(ret, 0);
1050 } else {
1051 ASSERT_FALSE(true);
1052 }
1053 } else {
1054 ASSERT_EQ(0, 0);
1055 }
1056 }
1057
1058 HWTEST_F(VibratorAgentTest, PreProcess_005, TestSize.Level1)
1059 {
1060 MISC_HILOGI("PreProcess_005 in");
1061 if (IsSupportVibratorCustom()) {
1062 FileDescriptor fileDescriptor("/data/test/vibrator/test_invalid_startTime.json");
1063 MISC_HILOGD("fd:%{public}d", fileDescriptor.fd);
1064 VibratorFileDescription vfd;
1065 VibratorPackage package;
1066 struct stat64 statbuf = { 0 };
1067 if (fstat64(fileDescriptor.fd, &statbuf) == 0) {
1068 vfd.fd = fileDescriptor.fd;
1069 vfd.offset = 0;
1070 vfd.length = statbuf.st_size;
1071 int32_t ret = PreProcess(vfd, package);
1072 ASSERT_NE(ret, 0);
1073 ret = FreeVibratorPackage(package);
1074 ASSERT_NE(ret, 0);
1075 } else {
1076 ASSERT_FALSE(true);
1077 }
1078 } else {
1079 ASSERT_EQ(0, 0);
1080 }
1081 }
1082
1083 HWTEST_F(VibratorAgentTest, PreProcess_006, TestSize.Level1)
1084 {
1085 MISC_HILOGI("PreProcess_006 in");
1086 if (IsSupportVibratorCustom()) {
1087 FileDescriptor fileDescriptor("/data/test/vibrator/test_invalid_type.json");
1088 MISC_HILOGD("fd:%{public}d", fileDescriptor.fd);
1089 VibratorFileDescription vfd;
1090 VibratorPackage package;
1091 struct stat64 statbuf = { 0 };
1092 if (fstat64(fileDescriptor.fd, &statbuf) == 0) {
1093 vfd.fd = fileDescriptor.fd;
1094 vfd.offset = 0;
1095 vfd.length = statbuf.st_size;
1096 int32_t ret = PreProcess(vfd, package);
1097 ASSERT_NE(ret, 0);
1098 ret = FreeVibratorPackage(package);
1099 ASSERT_NE(ret, 0);
1100 } else {
1101 ASSERT_FALSE(true);
1102 }
1103 } else {
1104 ASSERT_EQ(0, 0);
1105 }
1106 }
1107
1108 HWTEST_F(VibratorAgentTest, PreProcess_007, TestSize.Level1)
1109 {
1110 MISC_HILOGI("PreProcess_007 in");
1111 if (IsSupportVibratorCustom()) {
1112 FileDescriptor fileDescriptor("/data/test/vibrator/test_event_overlap_1.json");
1113 MISC_HILOGD("fd:%{public}d", fileDescriptor.fd);
1114 VibratorFileDescription vfd;
1115 VibratorPackage package;
1116 struct stat64 statbuf = { 0 };
1117 if (fstat64(fileDescriptor.fd, &statbuf) == 0) {
1118 vfd.fd = fileDescriptor.fd;
1119 vfd.offset = 0;
1120 vfd.length = statbuf.st_size;
1121 int32_t ret = PreProcess(vfd, package);
1122 ASSERT_EQ(ret, 0);
1123 ret = FreeVibratorPackage(package);
1124 ASSERT_EQ(ret, 0);
1125 } else {
1126 ASSERT_FALSE(true);
1127 }
1128 } else {
1129 ASSERT_EQ(0, 0);
1130 }
1131 }
1132
1133 HWTEST_F(VibratorAgentTest, PreProcess_008, TestSize.Level1)
1134 {
1135 MISC_HILOGI("PreProcess_008 in");
1136 if (IsSupportVibratorCustom()) {
1137 FileDescriptor fileDescriptor("/data/test/vibrator/test_event_overlap_2.json");
1138 MISC_HILOGD("fd:%{public}d", fileDescriptor.fd);
1139 VibratorFileDescription vfd;
1140 VibratorPackage package;
1141 struct stat64 statbuf = { 0 };
1142 if (fstat64(fileDescriptor.fd, &statbuf) == 0) {
1143 vfd.fd = fileDescriptor.fd;
1144 vfd.offset = 0;
1145 vfd.length = statbuf.st_size;
1146 int32_t ret = PreProcess(vfd, package);
1147 ASSERT_EQ(ret, 0);
1148 ret = FreeVibratorPackage(package);
1149 ASSERT_EQ(ret, 0);
1150 } else {
1151 ASSERT_FALSE(true);
1152 }
1153 } else {
1154 ASSERT_EQ(0, 0);
1155 }
1156 }
1157
1158 HWTEST_F(VibratorAgentTest, PreProcess_009, TestSize.Level1)
1159 {
1160 MISC_HILOGI("PreProcess_009 in");
1161 if (IsSupportVibratorCustom()) {
1162 FileDescriptor fileDescriptor("/data/test/vibrator/test_129_event.json");
1163 MISC_HILOGD("fd:%{public}d", fileDescriptor.fd);
1164 VibratorFileDescription vfd;
1165 VibratorPackage package;
1166 struct stat64 statbuf = { 0 };
1167 if (fstat64(fileDescriptor.fd, &statbuf) == 0) {
1168 vfd.fd = fileDescriptor.fd;
1169 vfd.offset = 0;
1170 vfd.length = statbuf.st_size;
1171 int32_t ret = PreProcess(vfd, package);
1172 ASSERT_NE(ret, 0);
1173 ret = FreeVibratorPackage(package);
1174 ASSERT_NE(ret, 0);
1175 } else {
1176 ASSERT_FALSE(true);
1177 }
1178 } else {
1179 ASSERT_EQ(0, 0);
1180 }
1181 }
1182
1183 HWTEST_F(VibratorAgentTest, PlayPattern_001, TestSize.Level1)
1184 {
1185 MISC_HILOGI("PlayPattern_001 in");
1186 if (IsSupportVibratorCustom()) {
1187 int32_t delayTime { -1 };
1188 int32_t ret = GetDelayTime(delayTime);
1189 ASSERT_EQ(ret, 0);
1190 MISC_HILOGD("delayTime:%{public}d", delayTime);
1191 FileDescriptor fileDescriptor("/data/test/vibrator/coin_drop.json");
1192 MISC_HILOGD("fd:%{public}d", fileDescriptor.fd);
1193 VibratorFileDescription vfd;
1194 VibratorPackage package;
1195 struct stat64 statbuf = { 0 };
1196 if (fstat64(fileDescriptor.fd, &statbuf) == 0) {
1197 vfd.fd = fileDescriptor.fd;
1198 vfd.offset = 0;
1199 vfd.length = statbuf.st_size;
1200 ret = PreProcess(vfd, package);
1201 ASSERT_EQ(ret, 0);
1202 for (int32_t i = 0; i < package.patternNum; ++i) {
1203 if (i == 0) {
1204 std::this_thread::sleep_for(std::chrono::milliseconds(package.patterns[i].time));
1205 } else {
1206 std::this_thread::sleep_for(std::chrono::milliseconds(package.patterns[i].time) -
1207 std::chrono::milliseconds(package.patterns[i - 1].time));
1208 }
1209 ASSERT_EQ(SetUsage(USAGE_UNKNOWN), true);
1210 MISC_HILOGD("pointNum:%{public}d", package.patterns[i].events[i].pointNum);
1211 ret = PlayPattern(package.patterns[i]);
1212 ASSERT_EQ(ret, 0);
1213 }
1214 }
1215 ret = FreeVibratorPackage(package);
1216 ASSERT_EQ(ret, 0);
1217 Cancel();
1218 } else {
1219 ASSERT_EQ(0, 0);
1220 }
1221 }
1222
1223 HWTEST_F(VibratorAgentTest, PlayPrimitiveEffect_001, TestSize.Level1)
1224 {
1225 MISC_HILOGI("PlayPrimitiveEffect_001 in");
1226 int32_t ret = PlayPrimitiveEffect(nullptr, INTENSITY_HIGH);
1227 ASSERT_EQ(ret, PARAMETER_ERROR);
1228 }
1229
1230 HWTEST_F(VibratorAgentTest, PlayPrimitiveEffect_002, TestSize.Level1)
1231 {
1232 MISC_HILOGI("PlayPrimitiveEffect_002 in");
1233 bool state { false };
1234 int32_t ret = IsSupportEffect(VIBRATOR_TYPE_SLIDE, &state);
1235 ASSERT_EQ(ret, 0);
1236 if (state) {
1237 ret = PlayPrimitiveEffect(VIBRATOR_TYPE_SLIDE, INTENSITY_INVALID);
1238 ASSERT_EQ(ret, PARAMETER_ERROR);
1239 } else {
1240 MISC_HILOGI("Do not support %{public}s", VIBRATOR_TYPE_SLIDE);
1241 }
1242 }
1243
1244 HWTEST_F(VibratorAgentTest, PlayPrimitiveEffect_003, TestSize.Level1)
1245 {
1246 MISC_HILOGI("PlayPrimitiveEffect_003 in");
1247 bool state { false };
1248 int32_t ret = IsSupportEffect(VIBRATOR_TYPE_SLIDE, &state);
1249 ASSERT_EQ(ret, 0);
1250 if (state) {
1251 ret = PlayPrimitiveEffect(VIBRATOR_TYPE_SLIDE, INTENSITY_LOW);
1252 ASSERT_EQ(ret, 0);
1253 std::this_thread::sleep_for(std::chrono::milliseconds(TIME_WAIT_FOR_OP));
1254 Cancel();
1255 } else {
1256 MISC_HILOGI("Do not support %{public}s", VIBRATOR_TYPE_SLIDE);
1257 }
1258 }
1259
1260 HWTEST_F(VibratorAgentTest, PlayPrimitiveEffect_004, TestSize.Level1)
1261 {
1262 MISC_HILOGI("PlayPrimitiveEffect_004 in");
1263 bool state { false };
1264 int32_t ret = IsSupportEffect(VIBRATOR_TYPE_SLIDE, &state);
1265 ASSERT_EQ(ret, 0);
1266 if (state) {
1267 ret = PlayPrimitiveEffect(VIBRATOR_TYPE_SLIDE, INTENSITY_MEDIUM);
1268 ASSERT_EQ(ret, 0);
1269 std::this_thread::sleep_for(std::chrono::milliseconds(TIME_WAIT_FOR_OP));
1270 Cancel();
1271 } else {
1272 MISC_HILOGI("Do not support %{public}s", VIBRATOR_TYPE_SLIDE);
1273 }
1274 }
1275
1276 HWTEST_F(VibratorAgentTest, PlayPrimitiveEffect_005, TestSize.Level1)
1277 {
1278 MISC_HILOGI("PlayPrimitiveEffect_005 in");
1279 bool state { false };
1280 int32_t ret = IsSupportEffect(VIBRATOR_TYPE_SLIDE, &state);
1281 ASSERT_EQ(ret, 0);
1282 if (state) {
1283 ret = PlayPrimitiveEffect(VIBRATOR_TYPE_SLIDE, INTENSITY_HIGH);
1284 ASSERT_EQ(ret, 0);
1285 std::this_thread::sleep_for(std::chrono::milliseconds(TIME_WAIT_FOR_OP));
1286 Cancel();
1287 } else {
1288 MISC_HILOGI("Do not support %{public}s", VIBRATOR_TYPE_SLIDE);
1289 }
1290 }
1291
1292 HWTEST_F(VibratorAgentTest, IsHdHapticSupported_001, TestSize.Level1)
1293 {
1294 MISC_HILOGI("IsHdHapticSupported_001 in");
1295 if (IsSupportVibratorCustom() && IsHdHapticSupported()) {
1296 FileDescriptor fileDescriptor("/data/test/vibrator/coin_drop.json");
1297 MISC_HILOGD("Test fd:%{public}d", fileDescriptor.fd);
1298 struct stat64 statbuf = { 0 };
1299 if (fstat64(fileDescriptor.fd, &statbuf) == 0) {
1300 int32_t ret = PlayVibratorCustom(fileDescriptor.fd, 0, statbuf.st_size);
1301 ASSERT_EQ(ret, 0);
1302 }
1303 std::this_thread::sleep_for(std::chrono::milliseconds(TIME_WAIT_FOR_OP));
1304 } else {
1305 ASSERT_EQ(0, 0);
1306 }
1307 Cancel();
1308 }
1309 } // namespace Sensors
1310 } // namespace OHOS
1311