1 /*
2 * Copyright (c) 2021-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 <gtest/gtest.h>
17 #include <thread>
18
19 #include "accesstoken_kit.h"
20 #include "nativetoken_kit.h"
21 #include "token_setproc.h"
22
23 #include "vibrator_agent.h"
24 #include "sensors_errors.h"
25
26 namespace OHOS {
27 namespace Sensors {
28 using namespace testing::ext;
29 using namespace OHOS::HiviewDFX;
30 using namespace Security::AccessToken;
31 using Security::AccessToken::AccessTokenID;
32
33 namespace {
34 constexpr HiLogLabel LABEL = { LOG_CORE, MISC_LOG_DOMAIN, "VibratorAgentTest" };
35 PermissionDef infoManagerTestPermDef_ = {
36 .permissionName = "ohos.permission.VIBRATE",
37 .bundleName = "accesstoken_test",
38 .grantMode = 1,
39 .label = "label",
40 .labelId = 1,
41 .description = "test vibrator agent",
42 .descriptionId = 1,
43 .availableLevel = APL_NORMAL
44 };
45
46 PermissionStateFull infoManagerTestState_ = {
47 .grantFlags = {1},
48 .grantStatus = {PermissionState::PERMISSION_GRANTED},
49 .isGeneral = true,
50 .permissionName = "ohos.permission.VIBRATE",
51 .resDeviceID = {"local"}
52 };
53
54 HapPolicyParams infoManagerTestPolicyPrams_ = {
55 .apl = APL_NORMAL,
56 .domain = "test.domain",
57 .permList = {infoManagerTestPermDef_},
58 .permStateList = {infoManagerTestState_}
59 };
60
61 HapInfoParams infoManagerTestInfoParms_ = {
62 .bundleName = "vibratoragent_test",
63 .userID = 1,
64 .instIndex = 0,
65 .appIDDesc = "vibratorAgentTest"
66 };
67 } // namespace
68
69 class VibratorAgentTest : public testing::Test {
70 public:
71 static void SetUpTestCase();
72 static void TearDownTestCase();
73 void SetUp();
74 void TearDown();
75
76 private:
77 static AccessTokenID tokenID_;
78 };
79
80 AccessTokenID VibratorAgentTest::tokenID_ = 0;
81
SetUpTestCase()82 void VibratorAgentTest::SetUpTestCase()
83 {
84 AccessTokenIDEx tokenIdEx = {0};
85 tokenIdEx = AccessTokenKit::AllocHapToken(infoManagerTestInfoParms_, infoManagerTestPolicyPrams_);
86 tokenID_ = tokenIdEx.tokenIdExStruct.tokenID;
87 ASSERT_NE(0, tokenID_);
88 ASSERT_EQ(0, SetSelfTokenID(tokenID_));
89 }
90
TearDownTestCase()91 void VibratorAgentTest::TearDownTestCase()
92 {
93 int32_t ret = AccessTokenKit::DeleteToken(tokenID_);
94 if (tokenID_ != 0) {
95 ASSERT_EQ(RET_SUCCESS, ret);
96 }
97 }
98
SetUp()99 void VibratorAgentTest::SetUp()
100 {}
101
TearDown()102 void VibratorAgentTest::TearDown()
103 {}
104
105 HWTEST_F(VibratorAgentTest, StartVibratorTest_001, TestSize.Level1)
106 {
107 HiLog::Info(LABEL, "%{public}s begin", __func__);
108 int32_t ret = StartVibrator("haptic.clock.timer");
109 ASSERT_EQ(ret, 0);
110 }
111
112 HWTEST_F(VibratorAgentTest, StartVibratorTest_002, TestSize.Level1)
113 {
114 HiLog::Info(LABEL, "%{public}s begin", __func__);
115 int32_t ret = StartVibrator("");
116 ASSERT_NE(ret, 0);
117 }
118
119 HWTEST_F(VibratorAgentTest, StartVibratorTest_003, TestSize.Level1)
120 {
121 HiLog::Info(LABEL, "%{public}s begin", __func__);
122 int32_t ret = StartVibrator(nullptr);
123 ASSERT_NE(ret, 0);
124 }
125
126 HWTEST_F(VibratorAgentTest, StartVibratorOnceTest_001, TestSize.Level1)
127 {
128 HiLog::Info(LABEL, "%{public}s begin", __func__);
129 int32_t ret = StartVibratorOnce(300);
130 ASSERT_EQ(ret, 0);
131 }
132
133 HWTEST_F(VibratorAgentTest, StartVibratorOnceTest_002, TestSize.Level1)
134 {
135 HiLog::Info(LABEL, "%{public}s begin", __func__);
136 int32_t ret = StartVibratorOnce(0);
137 ASSERT_NE(ret, 0);
138 }
139
140 HWTEST_F(VibratorAgentTest, StartVibratorOnceTest_003, TestSize.Level1)
141 {
142 HiLog::Info(LABEL, "%{public}s begin", __func__);
143 int32_t ret = StartVibratorOnce(1800000);
144 ASSERT_EQ(ret, 0);
145 }
146
147 HWTEST_F(VibratorAgentTest, StartVibratorOnceTest_004, TestSize.Level1)
148 {
149 HiLog::Info(LABEL, "%{public}s begin", __func__);
150 int32_t ret = StartVibratorOnce(1800001);
151 ASSERT_NE(ret, 0);
152 }
153
154 HWTEST_F(VibratorAgentTest, StopVibratorTest_001, TestSize.Level1)
155 {
156 HiLog::Info(LABEL, "%{public}s begin", __func__);
157 int32_t ret = StopVibrator("time");
158 ASSERT_EQ(ret, 0);
159 }
160
161 HWTEST_F(VibratorAgentTest, StopVibratorTest_002, TestSize.Level1)
162 {
163 HiLog::Info(LABEL, "%{public}s begin", __func__);
164 int32_t ret = StopVibrator("preset");
165 ASSERT_NE(ret, 0);
166 }
167
168 HWTEST_F(VibratorAgentTest, StopVibratorTest_003, TestSize.Level1)
169 {
170 HiLog::Info(LABEL, "%{public}s begin", __func__);
171 int32_t ret = StopVibrator("");
172 ASSERT_NE(ret, 0);
173 }
174
175 HWTEST_F(VibratorAgentTest, StopVibratorTest_004, TestSize.Level1)
176 {
177 HiLog::Info(LABEL, "%{public}s begin", __func__);
178 int32_t ret = StopVibrator(nullptr);
179 ASSERT_NE(ret, 0);
180 }
181
182 HWTEST_F(VibratorAgentTest, StopVibratorTest_005, TestSize.Level1)
183 {
184 HiLog::Info(LABEL, "%{public}s begin", __func__);
185 int32_t ret = StartVibratorOnce(300);
186 ASSERT_EQ(ret, 0);
187 ret = StopVibrator("time");
188 ASSERT_EQ(ret, 0);
189 }
190
191 HWTEST_F(VibratorAgentTest, SetLoopCount_001, TestSize.Level1)
192 {
193 HiLog::Info(LABEL, "%{public}s begin", __func__);
194 bool ret = SetLoopCount(300);
195 ASSERT_TRUE(ret);
196 }
197
198 HWTEST_F(VibratorAgentTest, SetLoopCount_002, TestSize.Level1)
199 {
200 HiLog::Info(LABEL, "%{public}s begin", __func__);
201 bool ret = SetLoopCount(-1);
202 ASSERT_FALSE(ret);
203 }
204
205 HWTEST_F(VibratorAgentTest, SetLoopCount_003, TestSize.Level1)
206 {
207 HiLog::Info(LABEL, "%{public}s begin", __func__);
208 bool ret = SetLoopCount(0);
209 ASSERT_FALSE(ret);
210 }
211
212 HWTEST_F(VibratorAgentTest, SetUsage_001, TestSize.Level1)
213 {
214 HiLog::Info(LABEL, "%{public}s begin", __func__);
215 bool ret = SetUsage(0);
216 ASSERT_TRUE(ret);
217 }
218
219 HWTEST_F(VibratorAgentTest, SetUsage_002, TestSize.Level1)
220 {
221 HiLog::Info(LABEL, "%{public}s begin", __func__);
222 bool ret = SetUsage(-1);
223 ASSERT_FALSE(ret);
224 }
225
226 HWTEST_F(VibratorAgentTest, SetUsage_003, TestSize.Level1)
227 {
228 HiLog::Info(LABEL, "%{public}s begin", __func__);
229 bool ret = SetUsage(USAGE_MAX);
230 ASSERT_FALSE(ret);
231 }
232 } // namespace Sensors
233 } // namespace OHOS
234