1 /*
2 * Copyright (c) 2022-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 <cstdio>
17 #include <gtest/gtest.h>
18 #include <securec.h>
19 #include <string>
20 #include "hdf_base.h"
21 #include "osal_time.h"
22 #include "parameters.h"
23 #include "v2_0/ivibrator_interface.h"
24 #define HDF_LOG_TAG "hdi_vibrator_impl_test"
25
26 using namespace std;
27 using namespace OHOS::HDI::Vibrator;
28 using namespace OHOS::HDI::Vibrator::V2_0;
29 using namespace testing::ext;
30
31 namespace {
32 HapticCapacity g_hapticCapacity;
33 sptr<V2_0::IVibratorInterface> g_vibratorInterface = nullptr;
34 }
35
36 class HatsHdfVibratorImplTest : public testing::Test {
37 public:
38 static void SetUpTestCase();
39 static void TearDownTestCase();
40 void SetUp();
41 void TearDown();
42 };
43
SetUpTestCase()44 void HatsHdfVibratorImplTest::SetUpTestCase()
45 {
46 g_vibratorInterface = V2_0::IVibratorInterface::Get();
47 }
TearDownTestCase()48 void HatsHdfVibratorImplTest::TearDownTestCase()
49 {
50 }
SetUp()51 void HatsHdfVibratorImplTest::SetUp()
52 {
53 if (g_vibratorInterface == nullptr) {
54 printf("Motion is not supported ");
55 GTEST_SKIP() << "Device not exist" << std::endl;
56 return;
57 }
58 }
TearDown()59 void HatsHdfVibratorImplTest::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(HatsHdfVibratorImplTest, SUB_Vibrator_HDI_CheckVibratorInstanceIsEmpty_0010, Function | MediumTest | Level1)
69 {
70 ASSERT_NE(nullptr, g_vibratorInterface);
71 }
72
73 /**
74 * @tc.name: EnableCompositeEffectTest001
75 * @tc.desc: Start periodic vibration with custom composite effect
76 * @tc.type: FUNC
77 */
78 HWTEST_F(HatsHdfVibratorImplTest, SUB_Vibrator_HDI_EnableCompositeEffectTest_0010, Function | MediumTest | Level1)
79 {
80 ASSERT_NE(nullptr, g_vibratorInterface);
81 if (g_hapticCapacity.isSupportPresetMapping) {
82 PrimitiveEffect primitiveEffect1 { 0, 60007, 0};
83 PrimitiveEffect primitiveEffect2 { 1000, 60007, 0};
84 PrimitiveEffect primitiveEffect3 { 1000, 60007, 0};
85 CompositeEffect effect1 = {
86 .primitiveEffect = primitiveEffect1
87 };
88 CompositeEffect effect2 = {
89 .primitiveEffect = primitiveEffect2
90 };
91 CompositeEffect effect3 = {
92 .primitiveEffect = primitiveEffect3
93 };
94 std::vector<CompositeEffect> vec;
95 vec.push_back(effect1);
96 vec.push_back(effect2);
97 vec.push_back(effect3);
98 HdfCompositeEffect effect;
99 effect.type = HDF_EFFECT_TYPE_PRIMITIVE;
100 effect.compositeEffects = vec;
101 int32_t ret = g_vibratorInterface -> EnableCompositeEffect({-1, 1}, effect);
102 EXPECT_EQ(HDF_SUCCESS, ret);
103 OsalMSleep(2000);
104 }
105 }
106
107 /**
108 * @tc.name: EnableCompositeEffectTest002
109 * @tc.desc: Start periodic vibration with custom composite effect
110 * @tc.type: FUNC
111 */
112 HWTEST_F(HatsHdfVibratorImplTest, SUB_Vibrator_HDI_EnableCompositeEffectTest_0020, Function | MediumTest | Level1)
113 {
114 ASSERT_NE(nullptr, g_vibratorInterface);
115 if (g_hapticCapacity.isSupportPresetMapping) {
116 PrimitiveEffect primitiveEffect1 { 0, 60007, 0};
117 PrimitiveEffect primitiveEffect2 { 1000, 60007, 0};
118 PrimitiveEffect primitiveEffect3 { 1000, 60007, 0};
119 CompositeEffect effect1 = {
120 .primitiveEffect = primitiveEffect1
121 };
122 CompositeEffect effect2 = {
123 .primitiveEffect = primitiveEffect2
124 };
125 CompositeEffect effect3 = {
126 .primitiveEffect = primitiveEffect3
127 };
128 std::vector<CompositeEffect> vec;
129 vec.push_back(effect1);
130 vec.push_back(effect2);
131 vec.push_back(effect3);
132 HdfCompositeEffect effect;
133 effect.type = HDF_EFFECT_TYPE_PRIMITIVE;
134 effect.compositeEffects = vec;
135 int32_t ret = g_vibratorInterface -> EnableCompositeEffect({-1, 1}, effect);
136 EXPECT_EQ(HDF_SUCCESS, ret);
137 OsalMSleep(1000);
138 ret = g_vibratorInterface -> Stop({-1, 1}, V2_0::HDF_VIBRATOR_MODE_PRESET);
139 EXPECT_EQ(HDF_SUCCESS, ret);
140 }
141 }
142
143 /**
144 * @tc.name: GetEffectInfoTest001
145 * @tc.desc: Get effect information with the given effect type
146 * @tc.type: FUNC
147 */
148 HWTEST_F(HatsHdfVibratorImplTest, SUB_Vibrator_HDI_GetEffectInfoTest_0010, Function | MediumTest | Level1)
149 {
150 ASSERT_NE(nullptr, g_vibratorInterface);
151 HdfEffectInfo effectInfo;
152 int32_t ret = g_vibratorInterface -> GetEffectInfo({-1, 1}, "haptic.pattern.type1", effectInfo);
153 printf("isSupportEffect = [%d]\n\r", effectInfo.isSupportEffect);
154 printf("duration = [%d]\n\r", effectInfo.duration);
155 EXPECT_EQ(ret, HDF_SUCCESS);
156 }
157
158 /**
159 * @tc.name: GetEffectInfoTest002
160 * @tc.desc: Get effect information with the given effect type
161 * @tc.type: FUNC
162 */
163 HWTEST_F(HatsHdfVibratorImplTest, SUB_Vibrator_HDI_GetEffectInfoTest_0020, Function | MediumTest | Level1)
164 {
165 ASSERT_NE(nullptr, g_vibratorInterface);
166 HdfEffectInfo effectInfo;
167 int32_t ret = g_vibratorInterface -> GetEffectInfo({-1, 1}, "invalid.effect.id", effectInfo);
168 EXPECT_EQ(HDF_SUCCESS, ret);
169 EXPECT_EQ(effectInfo.duration, 0);
170 EXPECT_EQ(effectInfo.isSupportEffect, false);
171 }
172
173 /**
174 * @tc.name: IsVibratorRunningTest001
175 * @tc.desc: Get vibration status.
176 * @tc.type: FUNC
177 */
178 HWTEST_F(HatsHdfVibratorImplTest, SUB_Vibrator_HDI_IsVibratorRunningTest_0010, Function | MediumTest | Level1)
179 {
180 ASSERT_NE(nullptr, g_vibratorInterface);
181 if (g_hapticCapacity.isSupportPresetMapping) {
182 PrimitiveEffect primitiveEffect1 { 0, 60007, 0};
183 PrimitiveEffect primitiveEffect2 { 1000, 60007, 0};
184 PrimitiveEffect primitiveEffect3 { 1000, 60007, 0};
185 CompositeEffect effect1 = {
186 .primitiveEffect = primitiveEffect1
187 };
188 CompositeEffect effect2 = {
189 .primitiveEffect = primitiveEffect2
190 };
191 CompositeEffect effect3 = {
192 .primitiveEffect = primitiveEffect3
193 };
194 std::vector<CompositeEffect> vec;
195 vec.push_back(effect1);
196 vec.push_back(effect2);
197 vec.push_back(effect3);
198 HdfCompositeEffect effect;
199 effect.type = HDF_EFFECT_TYPE_PRIMITIVE;
200 effect.compositeEffects = vec;
201 int32_t ret = g_vibratorInterface -> EnableCompositeEffect({-1, 1}, effect);
202 EXPECT_EQ(HDF_SUCCESS, ret);
203 bool state{false};
204 g_vibratorInterface -> IsVibratorRunning({-1, 1}, state);
205 EXPECT_EQ(state, true);
206 OsalMSleep(3000);
207 g_vibratorInterface -> IsVibratorRunning({-1, 1}, state);
208 EXPECT_EQ(state, false);
209 }
210 }
211
212 /**
213 * @tc.name: IsVibratorRunningTest002
214 * @tc.desc: Get vibration status.
215 * @tc.type: FUNC
216 */
217 HWTEST_F(HatsHdfVibratorImplTest, SUB_Vibrator_HDI_IsVibratorRunningTest_0020, Function | MediumTest | Level1)
218 {
219 ASSERT_NE(nullptr, g_vibratorInterface);
220 bool state {false};
221 g_vibratorInterface -> IsVibratorRunning({-1, 1}, state);
222 EXPECT_EQ(state, false);
223 }
224