1 /*
2 * Copyright (c) 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
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 "v1_0/vibrator_interface_proxy.h"
23
24 using namespace OHOS::HDI::Vibrator::V1_0;
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 std::string g_timeSequence = "haptic.clock.timer";
33 std::string g_builtIn = "haptic.default.effect";
34 std::string g_arbitraryStr = "arbitraryString";
35 sptr<IVibratorInterface> g_vibratorInterface = nullptr;
36 }
37
38 class HdfVibratorHdiServiceTest : public testing::Test {
39 public:
40 static void SetUpTestCase();
41 static void TearDownTestCase();
42 void SetUp();
43 void TearDown();
44 };
45
SetUpTestCase()46 void HdfVibratorHdiServiceTest::SetUpTestCase()
47 {
48 g_vibratorInterface = IVibratorInterface::Get();
49 }
50
TearDownTestCase()51 void HdfVibratorHdiServiceTest::TearDownTestCase()
52 {
53 }
54
SetUp()55 void HdfVibratorHdiServiceTest::SetUp()
56 {
57 }
58
TearDown()59 void HdfVibratorHdiServiceTest::TearDown()
60 {
61 }
62
63 /**
64 * @tc.name: CheckVibratorInstanceIsEmpty
65 * @tc.desc: Creat a vibrator instance. The instance is not empty.
66 * @tc.type: FUNC
67 */
68 HWTEST_F(HdfVibratorHdiServiceTest, CheckVibratorInstanceIsEmpty, Function | MediumTest | Level1)
69 {
70 ASSERT_NE(nullptr, g_vibratorInterface);
71 }
72
73 /**
74 * @tc.name: PerformOneShotVibratorDuration001
75 * @tc.desc: Controls this vibrator to perform a one-shot vibrator at a given duration.
76 * Controls this vibrator to stop the vibrator
77 * @tc.type: FUNC
78 */
79 HWTEST_F(HdfVibratorHdiServiceTest, PerformOneShotVibratorDuration001, Function | MediumTest | Level1)
80 {
81 ASSERT_NE(nullptr, g_vibratorInterface);
82
83 int32_t startRet = g_vibratorInterface->StartOnce(g_duration);
84 EXPECT_EQ(startRet, HDF_SUCCESS);
85
86 OsalMSleep(g_sleepTime1);
87
88 int32_t endRet = g_vibratorInterface->Stop(HDF_VIBRATOR_MODE_ONCE);
89 EXPECT_EQ(endRet, HDF_SUCCESS);
90 }
91
92 /**
93 * @tc.name: PerformOneShotVibratorDuration002
94 * @tc.desc: Controls this vibrator to perform a one-shot vibrator at 0 millisecond.
95 * Controls this vibrator to stop the vibrator
96 * @tc.type: FUNC
97 */
98 HWTEST_F(HdfVibratorHdiServiceTest, PerformOneShotVibratorDuration002, Function | MediumTest | Level1)
99 {
100 ASSERT_NE(nullptr, g_vibratorInterface);
101
102 int32_t startRet = g_vibratorInterface->StartOnce(g_noDuration);
103 EXPECT_EQ(startRet, HDF_ERR_INVALID_PARAM);
104
105 int32_t endRet = g_vibratorInterface->Stop(HDF_VIBRATOR_MODE_ONCE);
106 EXPECT_EQ(endRet, HDF_SUCCESS);
107 }
108
109 /**
110 * @tc.name: ExecuteVibratorEffect001
111 * @tc.desc: Controls this Performing Time Series Vibrator Effects.
112 * Controls this vibrator to stop the vibrator
113 * @tc.type: FUNC
114 */
115 HWTEST_F(HdfVibratorHdiServiceTest, ExecuteVibratorEffect001, Function | MediumTest | Level1)
116 {
117 ASSERT_NE(nullptr, g_vibratorInterface);
118
119 int32_t startRet = g_vibratorInterface->Start(g_timeSequence);
120 EXPECT_EQ(startRet, HDF_SUCCESS);
121
122 OsalMSleep(g_sleepTime2);
123
124 int32_t endRet = g_vibratorInterface->Stop(HDF_VIBRATOR_MODE_PRESET);
125 EXPECT_EQ(endRet, HDF_SUCCESS);
126 }
127
128 /**
129 * @tc.name: ExecuteVibratorEffect002
130 * @tc.desc: Controls this Performing built-in Vibrator Effects.
131 * Controls this vibrator to stop the vibrator.
132 * @tc.type: FUNC
133 */
134 HWTEST_F(HdfVibratorHdiServiceTest, ExecuteVibratorEffect002, Function | MediumTest | Level1)
135 {
136 ASSERT_NE(nullptr, g_vibratorInterface);
137
138 int32_t startRet = g_vibratorInterface->Start(g_builtIn);
139 EXPECT_EQ(startRet, HDF_SUCCESS);
140
141 OsalMSleep(g_sleepTime1);
142
143 int32_t endRet = g_vibratorInterface->Stop(HDF_VIBRATOR_MODE_PRESET);
144 EXPECT_EQ(endRet, HDF_SUCCESS);
145 }
146
147 /**
148 * @tc.name: ExecuteVibratorEffect004
149 * @tc.desc: Controls this Performing Time Series Vibrator Effects.
150 * Controls this vibrator to stop the vibrator.
151 * @tc.type: FUNC
152 */
153 HWTEST_F(HdfVibratorHdiServiceTest, ExecuteVibratorEffect004, Function | MediumTest | Level1)
154 {
155 ASSERT_NE(nullptr, g_vibratorInterface);
156
157 int32_t startRet = g_vibratorInterface->Start(g_timeSequence);
158 EXPECT_EQ(startRet, HDF_SUCCESS);
159
160 OsalMSleep(g_sleepTime2);
161
162 int32_t endRet = g_vibratorInterface->Stop(HDF_VIBRATOR_MODE_BUTT);
163 EXPECT_EQ(endRet, HDF_FAILURE);
164
165 endRet = g_vibratorInterface->Stop(HDF_VIBRATOR_MODE_PRESET);
166 EXPECT_EQ(endRet, HDF_SUCCESS);
167 }
168
169 /**
170 * @tc.name: ExecuteVibratorEffect005
171 * @tc.desc: Controls this vibrator to stop the vibrator.
172 * Controls this Performing Time Series Vibrator Effects.
173 * Controls this vibrator to stop the vibrator.
174 * @tc.type: FUNC
175 */
176 HWTEST_F(HdfVibratorHdiServiceTest, ExecuteVibratorEffect005, Function | MediumTest | Level1)
177 {
178 ASSERT_NE(nullptr, g_vibratorInterface);
179
180 int32_t startRet = g_vibratorInterface->Start(g_timeSequence);
181 EXPECT_EQ(startRet, HDF_SUCCESS);
182
183 OsalMSleep(g_sleepTime2);
184
185 int32_t endRet = g_vibratorInterface->Stop(HDF_VIBRATOR_MODE_PRESET);
186 EXPECT_EQ(endRet, HDF_SUCCESS);
187 }
188
189 /**
190 * @tc.name: ExecuteVibratorEffect006
191 * @tc.desc: Controls this vibrator to stop the vibrator.
192 * Controls this Perform built-in Vibrator Effects.
193 * Controls this vibrator to stop the vibrator.
194 * @tc.type: FUNC
195 */
196 HWTEST_F(HdfVibratorHdiServiceTest, ExecuteVibratorEffect006, Function | MediumTest | Level1)
197 {
198 ASSERT_NE(nullptr, g_vibratorInterface);
199
200 int32_t startRet = g_vibratorInterface->Start(g_builtIn);
201 EXPECT_EQ(startRet, HDF_SUCCESS);
202
203 OsalMSleep(g_sleepTime2);
204
205 int32_t endRet = g_vibratorInterface->Stop(HDF_VIBRATOR_MODE_PRESET);
206 EXPECT_EQ(endRet, HDF_SUCCESS);
207 }
208
209 /**
210 * @tc.name: ExecuteVibratorEffect007
211 * @tc.desc: Controls this Perform a one-shot vibrator with a arbitrary string.
212 * Controls this vibrator to stop the vibrator.
213 * @tc.type: FUNC
214 */
215 HWTEST_F(HdfVibratorHdiServiceTest, ExecuteVibratorEffect007, Function | MediumTest | Level1)
216 {
217 ASSERT_NE(nullptr, g_vibratorInterface);
218
219 int32_t startRet = g_vibratorInterface->Start(g_arbitraryStr);
220 EXPECT_EQ(startRet, HDF_ERR_INVALID_PARAM);
221
222 int32_t endRet = g_vibratorInterface->Stop(HDF_VIBRATOR_MODE_ONCE);
223 EXPECT_EQ(endRet, HDF_SUCCESS);
224 }
225