1 /*
2 * Copyright (c) 2021 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_sleepTime1 = 2000;
31 uint32_t g_sleepTime2 = 5000;
32 const char *g_timeSequence = "haptic.clock.timer";
33 const char *g_builtIn = "haptic.default.effect";
34 const char *g_arbitraryStr = "arbitraryString";
35 const struct VibratorInterface *g_vibratorDev = nullptr;
36 }
37
38 class HdfVibratorTest : public testing::Test {
39 public:
40 static void SetUpTestCase();
41 static void TearDownTestCase();
42 void SetUp();
43 void TearDown();
44 };
45
SetUpTestCase()46 void HdfVibratorTest::SetUpTestCase()
47 {
48 g_vibratorDev = NewVibratorInterfaceInstance();
49 }
50
TearDownTestCase()51 void HdfVibratorTest::TearDownTestCase()
52 {
53 if(g_vibratorDev != nullptr){
54 FreeVibratorInterfaceInstance();
55 g_vibratorDev = nullptr;
56 }
57 }
58
SetUp()59 void HdfVibratorTest::SetUp()
60 {
61 }
62
TearDown()63 void HdfVibratorTest::TearDown()
64 {
65 }
66
67 /**
68 *@tc.name: CheckVibratorInstanceIsEmpty
69 *@tc.desc: Creat a vibrator instance. The instance is not empty.
70 *@tc.type: FUNC
71 *@tc.require: SR000FK1JM,AR000FK1J0,AR000FK1JP
72 */
73 HWTEST_F(HdfVibratorTest, CheckVibratorInstanceIsEmpty, TestSize.Level1)
74 {
75 ASSERT_NE(nullptr, g_vibratorDev);
76 }
77
78 /**
79 *@tc.name: PerformOneShotVibratorDuration001
80 *@tc.desc: Controls this vibrator to perform a one-shot vibrator at a given duration.
81 Controls this vibrator to stop the vibrator
82 *@tc.type: FUNC
83 *@tc.require: AR000FK1JN,AR000FK1J0
84 */
85 HWTEST_F(HdfVibratorTest, PerformOneShotVibratorDuration001, TestSize.Level1)
86 {
87 ASSERT_NE(nullptr, g_vibratorDev);
88
89 int32_t startRet = g_vibratorDev->StartOnce(g_duration);
90 EXPECT_EQ(startRet, HDF_SUCCESS);
91
92 OsalMSleep(g_sleepTime1);
93
94 int32_t endRet = g_vibratorDev->Stop(VIBRATOR_MODE_ONCE);
95 EXPECT_EQ(endRet, HDF_SUCCESS);
96 }
97
98 /**
99 *@tc.name: PerformOneShotVibratorDuration002
100 *@tc.desc: Controls this vibrator to perform a one-shot vibrator at 0 millisecond.
101 Controls this vibrator to stop the vibrator
102 *@tc.type: FUNC
103 *@tc.require: AR000FK1JN,AR000FK1JP
104 */
105 HWTEST_F(HdfVibratorTest, PerformOneShotVibratorDuration002, TestSize.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: ExecuteVibratorEffect001
118 *@tc.desc: Controls this Performing Time Series Vibrator Effects.
119 Controls this vibrator to stop the vibrator
120 *@tc.type: FUNC
121 *@tc.require: AR000FK1JN,AR000FK1J0
122 */
123 HWTEST_F(HdfVibratorTest, ExecuteVibratorEffect001, TestSize.Level1)
124 {
125 ASSERT_NE(nullptr, g_vibratorDev);
126
127 int32_t startRet = g_vibratorDev->Start(g_timeSequence);
128 EXPECT_EQ(startRet, HDF_SUCCESS);
129
130 OsalMSleep(g_sleepTime2);
131
132 int32_t endRet = g_vibratorDev->Stop(VIBRATOR_MODE_PRESET);
133 EXPECT_EQ(endRet, HDF_SUCCESS);
134 }
135
136 /**
137 *@tc.name: ExecuteVibratorEffect002
138 *@tc.desc: Controls this Performing built-in Vibrator Effects.
139 Controls this vibrator to stop the vibrator.
140 *@tc.type: FUNC
141 *@tc.require: AR000FK1J0,AR000FK1JP
142 */
143 HWTEST_F(HdfVibratorTest, ExecuteVibratorEffect002, TestSize.Level1)
144 {
145 ASSERT_NE(nullptr, g_vibratorDev);
146
147 int32_t startRet = g_vibratorDev->Start(g_builtIn);
148 EXPECT_EQ(startRet, HDF_SUCCESS);
149
150 OsalMSleep(g_sleepTime1);
151
152 int32_t endRet = g_vibratorDev->Stop(VIBRATOR_MODE_PRESET);
153 EXPECT_EQ(endRet, HDF_SUCCESS);
154 }
155
156 /**
157 *@tc.name: ExecuteVibratorEffect003
158 *@tc.desc: Controls this Perform a Empty vibrator effect.
159 Controls this vibrator to stop the vibrator.
160 *@tc.type: FUNC
161 *@tc.require: AR000FK1J0,AR000FK1JP
162 */
163 HWTEST_F(HdfVibratorTest, ExecuteVibratorEffect003, TestSize.Level1)
164 {
165 ASSERT_NE(nullptr, g_vibratorDev);
166
167 int32_t startRet = g_vibratorDev->Start(NULL);
168 EXPECT_EQ(startRet, HDF_ERR_INVALID_PARAM);
169
170 int32_t endRet = g_vibratorDev->Stop(VIBRATOR_MODE_ONCE);
171 EXPECT_EQ(endRet, HDF_SUCCESS);
172 }
173
174 /**
175 *@tc.name: ExecuteVibratorEffect004
176 *@tc.desc: Controls this Performing Time Series Vibrator Effects.
177 Controls this vibrator to stop the vibrator.
178 *@tc.type: FUNC
179 *@tc.require: AR000FK1JN,AR000FK1JP
180 */
181 HWTEST_F(HdfVibratorTest, ExecuteVibratorEffect004, TestSize.Level1)
182 {
183 ASSERT_NE(nullptr, g_vibratorDev);
184
185 int32_t startRet = g_vibratorDev->Start(g_timeSequence);
186 EXPECT_EQ(startRet, HDF_SUCCESS);
187
188 OsalMSleep(g_sleepTime2);
189
190 int32_t endRet = g_vibratorDev->Stop(VIBRATOR_MODE_BUTT);
191 EXPECT_EQ(endRet, HDF_FAILURE);
192
193 endRet = g_vibratorDev->Stop(VIBRATOR_MODE_PRESET);
194 EXPECT_EQ(endRet, HDF_SUCCESS);
195 }
196
197 /**
198 *@tc.name: ExecuteVibratorEffect005
199 *@tc.desc: Controls this vibrator to stop the vibrator.
200 Controls this Performing Time Series Vibrator Effects.
201 Controls this vibrator to stop the vibrator.
202 *@tc.type: FUNC
203 *@tc.require: AR000FK1JN,AR000FK1JP
204 */
205 HWTEST_F(HdfVibratorTest, ExecuteVibratorEffect005, TestSize.Level1)
206 {
207 ASSERT_NE(nullptr, g_vibratorDev);
208
209 int32_t startRet = g_vibratorDev->Start(g_timeSequence);
210 EXPECT_EQ(startRet, HDF_SUCCESS);
211
212 OsalMSleep(g_sleepTime2);
213
214 int32_t endRet = g_vibratorDev->Stop(VIBRATOR_MODE_PRESET);
215 EXPECT_EQ(endRet, HDF_SUCCESS);
216 }
217
218 /**
219 *@tc.name: ExecuteVibratorEffect006
220 *@tc.desc: Controls this vibrator to stop the vibrator.
221 Controls this Perform built-in Vibrator Effects.
222 Controls this vibrator to stop the vibrator.
223 *@tc.type: FUNC
224 *@tc.require: AR000FK1JN,AR000FK1JP
225 */
226 HWTEST_F(HdfVibratorTest, ExecuteVibratorEffect006, TestSize.Level1)
227 {
228 ASSERT_NE(nullptr, g_vibratorDev);
229
230 int32_t startRet = g_vibratorDev->Start(g_builtIn);
231 EXPECT_EQ(startRet, HDF_SUCCESS);
232
233 OsalMSleep(g_sleepTime2);
234
235 int32_t endRet = g_vibratorDev->Stop(VIBRATOR_MODE_PRESET);
236 EXPECT_EQ(endRet, HDF_SUCCESS);
237 }
238
239 /**
240 *@tc.name: ExecuteVibratorEffect007
241 *@tc.desc: Controls this Perform a one-shot vibrator with a arbitrary string.
242 Controls this vibrator to stop the vibrator.
243 *@tc.type: FUNC
244 *@tc.require: AR000FK1JN,AR000FK1J0,AR000FK1JP
245 */
246 HWTEST_F(HdfVibratorTest, ExecuteVibratorEffect007, TestSize.Level1)
247 {
248 ASSERT_NE(nullptr, g_vibratorDev);
249
250 int32_t startRet = g_vibratorDev->Start(g_arbitraryStr);
251 EXPECT_EQ(startRet, HDF_ERR_INVALID_PARAM);
252
253 int32_t endRet = g_vibratorDev->Stop(VIBRATOR_MODE_ONCE);
254 EXPECT_EQ(endRet, HDF_SUCCESS);
255 }
256