1 /*
2 * Copyright (c) 2022-2023 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 "v1_1/ivibrator_interface.h"
24 #define HDF_LOG_TAG "hdi_vibrator_impl_test"
25
26 using namespace std;
27 using namespace OHOS::HDI::Vibrator::V1_1;
28 using namespace testing::ext;
29
30 namespace {
31 const std::string DEVICETYPE_KEY = "const.product.devicetype";
32 const std::string DEVICETYPE_TYPE = "phone";
33 sptr<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 = IVibratorInterface::Get();
47 }
TearDownTestCase()48 void HatsHdfVibratorImplTest::TearDownTestCase()
49 {
50 }
SetUp()51 void HatsHdfVibratorImplTest::SetUp()
52 {
53 }
TearDown()54 void HatsHdfVibratorImplTest::TearDown()
55 {
56 }
57
58 /**
59 * @tc.name: CheckVibratorInstanceIsEmpty
60 * @tc.desc: Creat a vibrator instance. The instance is not empty.
61 * @tc.type: FUNC
62 */
63 HWTEST_F(HatsHdfVibratorImplTest, SUB_Vibrator_HDI_CheckVibratorInstanceIsEmpty_0010, Function | MediumTest | Level1)
64 {
65 ASSERT_NE(nullptr, g_vibratorInterface);
66 }
67
68 /**
69 * @tc.name: EnableCompositeEffectTest001
70 * @tc.desc: Start periodic vibration with custom composite effect
71 * @tc.type: FUNC
72 */
73 HWTEST_F(HatsHdfVibratorImplTest, SUB_Vibrator_HDI_EnableCompositeEffectTest_0010, Function | MediumTest | Level1)
74 {
75 ASSERT_NE(nullptr, g_vibratorInterface);
76 if (OHOS::system::GetParameter(PHONE_KEY, "") == PHONE_TYPE) {
77 PrimitiveEffect primitiveEffect1 { 0, 60007, 0};
78 PrimitiveEffect primitiveEffect2 { 1000, 60007, 0};
79 PrimitiveEffect primitiveEffect3 { 1000, 60007, 0};
80 CompositeEffect effect1 = {
81 .primitiveEffect = primitiveEffect1
82 };
83 CompositeEffect effect2 = {
84 .primitiveEffect = primitiveEffect2
85 };
86 CompositeEffect effect3 = {
87 .primitiveEffect = primitiveEffect3
88 };
89 std::vector<CompositeEffect> vec;
90 vec.push_back(effect1);
91 vec.push_back(effect2);
92 vec.push_back(effect3);
93 HdfCompositeEffect effect;
94 effect.type = HDF_EFFECT_TYPE_PRIMITIVE;
95 effect.compositeEffects = vec;
96 int32_t ret = g_vibratorInterface -> EnableCompositeEffect(effect);
97 EXPECT_EQ(HDF_SUCCESS, ret);
98 OsalMSleep(2000);
99 }
100 }
101
102 /**
103 * @tc.name: EnableCompositeEffectTest002
104 * @tc.desc: Start periodic vibration with custom composite effect
105 * @tc.type: FUNC
106 */
107 HWTEST_F(HatsHdfVibratorImplTest, SUB_Vibrator_HDI_EnableCompositeEffectTest_0020, Function | MediumTest | Level1)
108 {
109 ASSERT_NE(nullptr, g_vibratorInterface);
110 if (OHOS::system::GetParameter(DEVICETYPE_KEY, "") == PHONE_TYPE) {
111 PrimitiveEffect primitiveEffect1 { 0, 60007, 0};
112 PrimitiveEffect primitiveEffect2 { 1000, 60007, 0};
113 PrimitiveEffect primitiveEffect3 { 1000, 60007, 0};
114 CompositeEffect effect1 = {
115 .primitiveEffect = primitiveEffect1
116 };
117 CompositeEffect effect2 = {
118 .primitiveEffect = primitiveEffect2
119 };
120 CompositeEffect effect3 = {
121 .primitiveEffect = primitiveEffect3
122 };
123 std::vector<CompositeEffect> vec;
124 vec.push_back(effect1);
125 vec.push_back(effect2);
126 vec.push_back(effect3);
127 HdfCompositeEffect effect;
128 effect.type = HDF_EFFECT_TYPE_PRIMITIVE;
129 effect.compositeEffects = vec;
130 int32_t ret = g_vibratorInterface -> EnableCompositeEffect(effect);
131 EXPECT_EQ(HDF_SUCCESS, ret);
132 OsalMSleep(1000);
133 ret = g_vibratorInterface -> Stop(HDF_VIBRATOR_MODE_PRESET);
134 EXPECT_EQ(HDF_SUCCESS, ret);
135 }
136 }
137
138 /**
139 * @tc.name: GetEffectInfoTest001
140 * @tc.desc: Get effect information with the given effect type
141 * @tc.type: FUNC
142 */
143 HWTEST_F(HatsHdfVibratorImplTest, SUB_Vibrator_HDI_GetEffectInfoTest_0010, Function | MediumTest | Level1)
144 {
145 ASSERT_NE(nullptr, g_vibratorInterface);
146 HdfEffectInfo effectInfo;
147 int32_t ret = g_vibratorInterface -> GetEffectInfo("haptic.pattern.type1", effectInfo);
148 EXPECT_EQ(effectInfo.duration, 1900);
149 EXPECT_EQ(effectInfo.isSupportEffect, true);
150 EXPECT_EQ(HDF_SUCCESS, ret);
151 }
152
153 /**
154 * @tc.name: GetEffectInfoTest002
155 * @tc.desc: Get effect information with the given effect type
156 * @tc.type: FUNC
157 */
158 HWTEST_F(HatsHdfVibratorImplTest, SUB_Vibrator_HDI_GetEffectInfoTest_0020, Function | MediumTest | Level1)
159 {
160 ASSERT_NE(nullptr, g_vibratorInterface);
161 HdfEffectInfo effectInfo;
162 int32_t ret = g_vibratorInterface -> GetEffectInfo("invalid.effect.id", effectInfo);
163 EXPECT_EQ(HDF_SUCCESS, ret);
164 EXPECT_EQ(effectInfo.duration, 0);
165 EXPECT_EQ(effectInfo.isSupportEffect, false);
166 }
167
168 /**
169 * @tc.name: IsVibratorRunningTest001
170 * @tc.desc: Get vibration status.
171 * @tc.type: FUNC
172 */
173 HWTEST_F(HatsHdfVibratorImplTest, SUB_Vibrator_HDI_IsVibratorRunningTest_0010, Function | MediumTest | Level1)
174 {
175 ASSERT_NE(nullptr, g_vibratorInterface);
176 if (OHOS::system::GetParameter(DEVICETYPE_KEY, "") == PHONE_TYPE) {
177 PrimitiveEffect primitiveEffect1 { 0, 60007, 0};
178 PrimitiveEffect primitiveEffect2 { 1000, 60007, 0};
179 PrimitiveEffect primitiveEffect3 { 1000, 60007, 0};
180 CompositeEffect effect1 = {
181 .primitiveEffect = primitiveEffect1
182 };
183 CompositeEffect effect2 = {
184 .primitiveEffect = primitiveEffect2
185 };
186 CompositeEffect effect3 = {
187 .primitiveEffect = primitiveEffect3
188 };
189 std::vector<CompositeEffect> vec;
190 vec.push_back(effect1);
191 vec.push_back(effect2);
192 vec.push_back(effect3);
193 HdfCompositeEffect effect;
194 effect.type = HDF_EFFECT_TYPE_PRIMITIVE;
195 effect.compositeEffects = vec;
196 int32_t ret = g_vibratorInterface -> EnableCompositeEffect(effect);
197 EXPECT_EQ(HDF_SUCCESS, ret);
198 bool state{false};
199 g_vibratorInterface -> IsVibratorRunning(state);
200 EXPECT_EQ(state, true);
201 OsalMSleep(3000);
202 g_vibratorInterface -> IsVibratorRunning(state);
203 EXPECT_EQ(state, false);
204 }
205 }
206
207 /**
208 * @tc.name: IsVibratorRunningTest002
209 * @tc.desc: Get vibration status.
210 * @tc.type: FUNC
211 */
212 HWTEST_F(HatsHdfVibratorImplTest, SUB_Vibrator_HDI_IsVibratorRunningTest_0020, Function | MediumTest | Level1)
213 {
214 ASSERT_NE(nullptr, g_vibratorInterface);
215 bool state {false};
216 g_vibratorInterface -> IsVibratorRunning(state);
217 EXPECT_EQ(state, false);
218 }