• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-2025 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 "parameters.h"
22 #include "sensors_errors.h"
23 #include "vibrator_agent.h"
24 
25 #undef LOG_TAG
26 #define LOG_TAG "VibratorAgentSeekTest"
27 
28 namespace OHOS {
29 namespace Sensors {
30 using namespace testing::ext;
31 
32 namespace {
33 constexpr int32_t TIME_WAIT_FOR_OP_TWO_HUNDRED = 200;
34 }
35 
36 class VibratorAgentSeekTest : public testing::Test {
37 public:
38     static void SetUpTestCase();
39     static void TearDownTestCase();
40     void SetUp();
41     void TearDown();
42 };
43 
44 struct FileDescriptor {
FileDescriptorOHOS::Sensors::FileDescriptor45     explicit FileDescriptor(const std::string &path)
46     {
47         fd = open(path.c_str(), O_RDONLY);
48     }
~FileDescriptorOHOS::Sensors::FileDescriptor49     ~FileDescriptor()
50     {
51         close(fd);
52     }
53     int32_t fd;
54 };
55 
SetUpTestCase()56 void VibratorAgentSeekTest::SetUpTestCase()
57 {
58 }
59 
TearDownTestCase()60 void VibratorAgentSeekTest::TearDownTestCase()
61 {
62 }
63 
SetUp()64 void VibratorAgentSeekTest::SetUp()
65 {
66 }
67 
TearDown()68 void VibratorAgentSeekTest::TearDown()
69 {
70     std::this_thread::sleep_for(std::chrono::milliseconds(TIME_WAIT_FOR_OP_TWO_HUNDRED));
71 }
72 
PrintVibratorPackageInfo(const VibratorPackage package,std::string functionName)73 void PrintVibratorPackageInfo(const VibratorPackage package, std::string functionName)
74 {
75     MISC_HILOGD("FunctionName:%{public}s data, package patternNum:%{public}d", functionName.c_str(),
76         package.patternNum);
77     MISC_HILOGD("FunctionName:%{public}s data, package packageDuration:%{public}d", functionName.c_str(),
78         package.packageDuration);
79     for (int32_t i = 0; i < package.patternNum; i++) {
80         MISC_HILOGD("FunctionName:%{public}s data, pattern time:%{public}d", functionName.c_str(),
81             package.patterns[i].time);
82         MISC_HILOGD("FunctionName:%{public}s data, pattern patternDuration:%{public}d", functionName.c_str(),
83             package.patterns[i].patternDuration);
84         MISC_HILOGD("FunctionName:%{public}s data, pattern eventNum:%{public}d", functionName.c_str(),
85             package.patterns[i].eventNum);
86         for (int32_t j = 0; j < package.patterns[i].eventNum; j++) {
87             MISC_HILOGD("FunctionName:%{public}s data, event type:%{public}d", functionName.c_str(),
88                 static_cast<int32_t>(package.patterns[i].events[j].type));
89             MISC_HILOGD("FunctionName:%{public}s data, event time:%{public}d", functionName.c_str(),
90                 package.patterns[i].events[j].time);
91             MISC_HILOGD("FunctionName:%{public}s data, event duration:%{public}d", functionName.c_str(),
92                 package.patterns[i].events[j].duration);
93             MISC_HILOGD("FunctionName:%{public}s data, event intensity:%{public}d", functionName.c_str(),
94                 package.patterns[i].events[j].intensity);
95             MISC_HILOGD("FunctionName:%{public}s data, event frequency:%{public}d", functionName.c_str(),
96                 package.patterns[i].events[j].frequency);
97             MISC_HILOGD("FunctionName:%{public}s data, event index:%{public}d", functionName.c_str(),
98                 package.patterns[i].events[j].index);
99             MISC_HILOGD("FunctionName:%{public}s data, event pointNum:%{public}d", functionName.c_str(),
100                 package.patterns[i].events[j].pointNum);
101             for (int32_t k = 0; k < package.patterns[i].events[j].pointNum; k++) {
102                 MISC_HILOGD("FunctionName:%{public}s data, points time:%{public}d", functionName.c_str(),
103                     package.patterns[i].events[j].points[k].time);
104                 MISC_HILOGD("FunctionName:%{public}s data, points intensity:%{public}d", functionName.c_str(),
105                     package.patterns[i].events[j].points[k].intensity);
106                 MISC_HILOGD("FunctionName:%{public}s data, points frequency:%{public}d", functionName.c_str(),
107                     package.patterns[i].events[j].points[k].frequency);
108             }
109         }
110     }
111 }
112 
113 HWTEST_F(VibratorAgentSeekTest, SeekTimeIsZero, TestSize.Level1)
114 {
115     MISC_HILOGI("SeekTimeIsZero in");
116     bool isSupport = IsSupportVibratorCustom();
117     if (isSupport) {
118         int32_t delayTime{ -1 };
119         int32_t ret = GetDelayTime(delayTime);
120         ASSERT_EQ(ret, 0);
121         MISC_HILOGD("delayTime:%{public}d", delayTime);
122         FileDescriptor fileDescriptor("/data/test/vibrator/coin_drop_seek.json");
123         MISC_HILOGD("fd:%{public}d", fileDescriptor.fd);
124         VibratorFileDescription vfd;
125         VibratorPackage package;
126         struct stat64 statbuf = { 0 };
127         if (fstat64(fileDescriptor.fd, &statbuf) == 0) {
128             vfd.fd = fileDescriptor.fd;
129             vfd.offset = 0;
130             vfd.length = statbuf.st_size;
131             ret = PreProcess(vfd, package);
132             ASSERT_EQ(ret, 0);
133             ASSERT_EQ(package.patternNum, 1);
134             ASSERT_EQ(package.patterns[0].eventNum, 11);
135             // 打印日志:package内容,开始
136             PrintVibratorPackageInfo(package, "SeekTimeIsZero");
137             // 打印日志:package内容,结束
138         }
139         ret = FreeVibratorPackage(package);
140         ASSERT_EQ(ret, 0);
141     } else {
142         ASSERT_EQ(isSupport, false);
143     }
144 }
145 
146 HWTEST_F(VibratorAgentSeekTest, RemainingDurationGreaterThanMinValue, TestSize.Level1)
147 {
148     MISC_HILOGI("RemainDurationGreaterThanMinValue in");
149     bool isSupport = IsSupportVibratorCustom();
150     if (isSupport) {
151         int32_t delayTime{ -1 };
152         int32_t ret = GetDelayTime(delayTime);
153         ASSERT_EQ(ret, 0);
154         MISC_HILOGD("delayTime:%{public}d", delayTime);
155         FileDescriptor fileDescriptor("/data/test/vibrator/coin_drop_seek.json");
156         MISC_HILOGD("fd:%{public}d", fileDescriptor.fd);
157         VibratorFileDescription vfd;
158         VibratorPackage package;
159         VibratorPackage seekPackage;
160         struct stat64 statbuf = { 0 };
161         if (fstat64(fileDescriptor.fd, &statbuf) == 0) {
162             vfd.fd = fileDescriptor.fd;
163             vfd.offset = 0;
164             vfd.length = statbuf.st_size;
165             ret = PreProcess(vfd, package);
166             ASSERT_EQ(ret, 0);
167             ASSERT_EQ(package.patternNum, 1);
168             ASSERT_EQ(package.patterns[0].eventNum, 11);
169             int32_t seekTime = 510;
170             ret = SeekTimeOnPackage(seekTime, package, seekPackage);
171             ASSERT_EQ(ret, 0);
172             ASSERT_EQ(seekPackage.patternNum, 1);
173             ASSERT_EQ(seekPackage.patterns[0].eventNum, 4);
174             // 打印日志:package内容,开始
175             PrintVibratorPackageInfo(seekPackage, "RemainDurationGreaterThanMinValue");
176             // 打印日志:package内容,结束
177         }
178         ret = FreeVibratorPackage(package);
179         ASSERT_EQ(ret, 0);
180         ret = FreeVibratorPackage(seekPackage);
181         ASSERT_EQ(ret, 0);
182     } else {
183         ASSERT_EQ(isSupport, false);
184     }
185 }
186 
187 HWTEST_F(VibratorAgentSeekTest, RemainingDurationEqualMinValue, TestSize.Level1)
188 {
189     MISC_HILOGI("RemainingDurationEqualMinValue in");
190     bool isSupport = IsSupportVibratorCustom();
191     if (isSupport) {
192         int32_t delayTime{ -1 };
193         int32_t ret = GetDelayTime(delayTime);
194         ASSERT_EQ(ret, 0);
195         MISC_HILOGD("delayTime:%{public}d", delayTime);
196         FileDescriptor fileDescriptor("/data/test/vibrator/coin_drop_seek.json");
197         MISC_HILOGD("fd:%{public}d", fileDescriptor.fd);
198         VibratorFileDescription vfd;
199         VibratorPackage package;
200         VibratorPackage seekPackage;
201         struct stat64 statbuf = { 0 };
202         if (fstat64(fileDescriptor.fd, &statbuf) == 0) {
203             vfd.fd = fileDescriptor.fd;
204             vfd.offset = 0;
205             vfd.length = statbuf.st_size;
206             ret = PreProcess(vfd, package);
207             ASSERT_EQ(ret, 0);
208             ASSERT_EQ(package.patternNum, 1);
209             ASSERT_EQ(package.patterns[0].eventNum, 11);
210             int32_t seekTime = 564;
211             ret = SeekTimeOnPackage(seekTime, package, seekPackage);
212             ASSERT_EQ(ret, 0);
213             ASSERT_EQ(seekPackage.patternNum, 1);
214             ASSERT_EQ(seekPackage.patterns[0].eventNum, 4);
215             // 打印日志:package内容,开始
216             PrintVibratorPackageInfo(seekPackage, "RemainingDurationEqualMinValue");
217             // 打印日志:package内容,结束
218         }
219         ret = FreeVibratorPackage(package);
220         ASSERT_EQ(ret, 0);
221         ret = FreeVibratorPackage(seekPackage);
222         ASSERT_EQ(ret, 0);
223     } else {
224         ASSERT_EQ(isSupport, false);
225     }
226 }
227 
228 HWTEST_F(VibratorAgentSeekTest, RemainingDurationLessThanMinValue, TestSize.Level1)
229 {
230     MISC_HILOGI("RemainingDurationLessThanMinValue in");
231     bool isSupport = IsSupportVibratorCustom();
232     if (isSupport) {
233         int32_t delayTime{ -1 };
234         int32_t ret = GetDelayTime(delayTime);
235         ASSERT_EQ(ret, 0);
236         MISC_HILOGD("delayTime:%{public}d", delayTime);
237         FileDescriptor fileDescriptor("/data/test/vibrator/coin_drop_seek.json");
238         MISC_HILOGD("fd:%{public}d", fileDescriptor.fd);
239         VibratorFileDescription vfd;
240         VibratorPackage package;
241         VibratorPackage seekPackage;
242         struct stat64 statbuf = { 0 };
243         if (fstat64(fileDescriptor.fd, &statbuf) == 0) {
244             vfd.fd = fileDescriptor.fd;
245             vfd.offset = 0;
246             vfd.length = statbuf.st_size;
247             ret = PreProcess(vfd, package);
248             ASSERT_EQ(ret, 0);
249             ASSERT_EQ(package.patternNum, 1);
250             ASSERT_EQ(package.patterns[0].eventNum, 11);
251             int32_t seekTime = 565;
252             ret = SeekTimeOnPackage(seekTime, package, seekPackage);
253             ASSERT_EQ(ret, 0);
254             ASSERT_EQ(seekPackage.patternNum, 1);
255             ASSERT_EQ(seekPackage.patterns[0].eventNum, 3);
256             // 打印日志:package内容,开始
257             PrintVibratorPackageInfo(seekPackage, "RemainingDurationLessThanMinValue");
258             // 打印日志:package内容,结束
259         }
260         ret = FreeVibratorPackage(package);
261         ASSERT_EQ(ret, 0);
262         ret = FreeVibratorPackage(seekPackage);
263         ASSERT_EQ(ret, 0);
264     } else {
265         ASSERT_EQ(isSupport, false);
266     }
267 }
268 
269 HWTEST_F(VibratorAgentSeekTest, SeekTimePatternIsZero, TestSize.Level1)
270 {
271     MISC_HILOGI("SeekTimePatternIsZero in");
272     bool isSupport = IsSupportVibratorCustom();
273     if (isSupport) {
274         int32_t delayTime{ -1 };
275         int32_t ret = GetDelayTime(delayTime);
276         ASSERT_EQ(ret, 0);
277         MISC_HILOGD("delayTime:%{public}d", delayTime);
278         FileDescriptor fileDescriptor("/data/test/vibrator/coin_drop_seek.json");
279         MISC_HILOGD("fd:%{public}d", fileDescriptor.fd);
280         VibratorFileDescription vfd;
281         VibratorPackage package;
282         VibratorPackage seekPackage;
283         struct stat64 statbuf = { 0 };
284         if (fstat64(fileDescriptor.fd, &statbuf) == 0) {
285             vfd.fd = fileDescriptor.fd;
286             vfd.offset = 0;
287             vfd.length = statbuf.st_size;
288             ret = PreProcess(vfd, package);
289             ASSERT_EQ(ret, 0);
290             ASSERT_EQ(package.patternNum, 1);
291             ASSERT_EQ(package.patterns[0].eventNum, 11);
292             int32_t seekTime = 1001;
293             ret = SeekTimeOnPackage(seekTime, package, seekPackage);
294             ASSERT_EQ(ret, 0);
295             ASSERT_EQ(seekPackage.patternNum, 0);
296             // 打印日志:package内容,开始
297             MISC_HILOGD("SeekTimePatternIsZero data, package patternNum:%{public}d", seekPackage.patternNum);
298             // 打印日志:package内容,结束
299         }
300         ret = FreeVibratorPackage(package);
301         ASSERT_EQ(ret, 0);
302         ret = FreeVibratorPackage(seekPackage);
303         ASSERT_NE(ret, 0);
304     } else {
305         ASSERT_EQ(isSupport, false);
306     }
307 }
308 
309 HWTEST_F(VibratorAgentSeekTest, SeekTimeOnBetweenEvents, TestSize.Level1)
310 {
311     MISC_HILOGI("SeekTimeOnBetweenEvents in");
312     bool isSupport = IsSupportVibratorCustom();
313     if (isSupport) {
314         int32_t delayTime{ -1 };
315         int32_t ret = GetDelayTime(delayTime);
316         ASSERT_EQ(ret, 0);
317         MISC_HILOGD("delayTime:%{public}d", delayTime);
318         FileDescriptor fileDescriptor("/data/test/vibrator/coin_drop_seek.json");
319         MISC_HILOGD("fd:%{public}d", fileDescriptor.fd);
320         VibratorFileDescription vfd;
321         VibratorPackage package;
322         VibratorPackage seekPackage;
323         struct stat64 statbuf = { 0 };
324         if (fstat64(fileDescriptor.fd, &statbuf) == 0) {
325             vfd.fd = fileDescriptor.fd;
326             vfd.offset = 0;
327             vfd.length = statbuf.st_size;
328             ret = PreProcess(vfd, package);
329             ASSERT_EQ(ret, 0);
330             ASSERT_EQ(package.patternNum, 1);
331             ASSERT_EQ(package.patterns[0].eventNum, 11);
332             int32_t seekTime = 930;
333             ret = SeekTimeOnPackage(seekTime, package, seekPackage);
334             ASSERT_EQ(ret, 0);
335             ASSERT_EQ(seekPackage.patternNum, 1);
336             ASSERT_EQ(seekPackage.patterns[0].eventNum, 2);
337             ASSERT_EQ(seekPackage.patterns[0].events[0].time, 10);
338             // 打印日志:package内容,开始
339             PrintVibratorPackageInfo(seekPackage, "SeekTimeOnBetweenEvents");
340             // 打印日志:package内容,结束
341         }
342         ret = FreeVibratorPackage(package);
343         ASSERT_EQ(ret, 0);
344         ret = FreeVibratorPackage(seekPackage);
345         ASSERT_EQ(ret, 0);
346     } else {
347         ASSERT_EQ(isSupport, false);
348     }
349 }
350 
351 HWTEST_F(VibratorAgentSeekTest, SeekTimeOnThirdPattern, TestSize.Level1)
352 {
353     MISC_HILOGI("SeekTimeOnThirdPattern in");
354     bool isSupport = IsSupportVibratorCustom();
355     if (isSupport) {
356         int32_t delayTime{ -1 };
357         int32_t ret = GetDelayTime(delayTime);
358         ASSERT_EQ(ret, 0);
359         MISC_HILOGD("delayTime:%{public}d", delayTime);
360         FileDescriptor fileDescriptor("/data/test/vibrator/test_128_event.json");
361         MISC_HILOGD("fd:%{public}d", fileDescriptor.fd);
362         VibratorFileDescription vfd;
363         VibratorPackage package;
364         VibratorPackage seekPackage;
365         struct stat64 statbuf = { 0 };
366         if (fstat64(fileDescriptor.fd, &statbuf) == 0) {
367             vfd.fd = fileDescriptor.fd;
368             vfd.offset = 0;
369             vfd.length = statbuf.st_size;
370             ret = PreProcess(vfd, package);
371             ASSERT_EQ(ret, 0);
372             ASSERT_EQ(package.patternNum, 8);
373             ASSERT_EQ(package.patterns[0].eventNum, 16);
374             int32_t seekTime = 1650;
375             ret = SeekTimeOnPackage(seekTime, package, seekPackage);
376             ASSERT_EQ(ret, 0);
377             ASSERT_EQ(seekPackage.patternNum, 6);
378             ASSERT_EQ(seekPackage.patterns[0].eventNum, 15);
379             // 打印日志:package内容,开始
380             PrintVibratorPackageInfo(seekPackage, "SeekTimeOnThirdPattern");
381             // 打印日志:package内容,结束
382         }
383         ret = FreeVibratorPackage(package);
384         ASSERT_EQ(ret, 0);
385         ret = FreeVibratorPackage(seekPackage);
386         ASSERT_EQ(ret, 0);
387     } else {
388         ASSERT_EQ(isSupport, false);
389     }
390 }
391 } // namespace Sensors
392 } // namespace OHOS
393