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