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 <gtest/gtest.h>
17 #include <thread>
18
19 #include "accesstoken_kit.h"
20 #include "nativetoken_kit.h"
21 #include "token_setproc.h"
22 #include "light_agent.h"
23 #include "sensors_errors.h"
24
25 #undef LOG_TAG
26 #define LOG_TAG "LightAgentTest"
27
28 namespace OHOS {
29 namespace Sensors {
30 using namespace testing::ext;
31 using namespace Security::AccessToken;
32 using Security::AccessToken::AccessTokenID;
33
34 namespace {
35 constexpr int32_t TIME_WAIT_FOR_OP = 2;
36 PermissionDef g_infoManagerTestPermDef = {
37 .permissionName = "ohos.permission.SYSTEM_LIGHT_CONTROL",
38 .bundleName = "accesstoken_test",
39 .grantMode = 1,
40 .label = "label",
41 .labelId = 1,
42 .description = "test light agent",
43 .descriptionId = 1,
44 .availableLevel = APL_NORMAL
45 };
46
47 PermissionStateFull g_infoManagerTestState = {
48 .grantFlags = {1},
49 .grantStatus = {PermissionState::PERMISSION_GRANTED},
50 .isGeneral = true,
51 .permissionName = "ohos.permission.SYSTEM_LIGHT_CONTROL",
52 .resDeviceID = {"local"}
53 };
54
55 HapPolicyParams g_infoManagerTestPolicyPrams = {
56 .apl = APL_NORMAL,
57 .domain = "test.domain",
58 .permList = {g_infoManagerTestPermDef},
59 .permStateList = {g_infoManagerTestState}
60 };
61
62 HapInfoParams g_infoManagerTestInfoParms = {
63 .bundleName = "lightagent_test",
64 .userID = 1,
65 .instIndex = 0,
66 .appIDDesc = "LightAgentTest"
67 };
68 } // namespace
69
70 class LightAgentTest : public testing::Test {
71 public:
72 static void SetUpTestCase();
73 static void TearDownTestCase();
SetUp()74 void SetUp() {}
TearDown()75 void TearDown() {}
76 private:
77 static AccessTokenID tokenID_;
78 };
79
80 AccessTokenID LightAgentTest::tokenID_ = 0;
81
82 LightInfo *g_lightInfo = nullptr;
83 int32_t g_lightId = -1;
84 int32_t g_invalidLightId = -1;
85 int32_t g_lightType = -1;
86
SetUpTestCase()87 void LightAgentTest::SetUpTestCase()
88 {
89 AccessTokenIDEx tokenIdEx = {0};
90 tokenIdEx = AccessTokenKit::AllocHapToken(g_infoManagerTestInfoParms, g_infoManagerTestPolicyPrams);
91 tokenID_ = tokenIdEx.tokenIdExStruct.tokenID;
92 ASSERT_NE(0, tokenID_);
93 ASSERT_EQ(0, SetSelfTokenID(tokenID_));
94 }
95
TearDownTestCase()96 void LightAgentTest::TearDownTestCase()
97 {
98 int32_t ret = AccessTokenKit::DeleteToken(tokenID_);
99 if (tokenID_ != 0) {
100 ASSERT_EQ(RET_SUCCESS, ret);
101 }
102 }
103
104 /**
105 * @tc.name: StartLightTest_001
106 * @tc.desc: Verify GetLightList
107 * @tc.type: FUNC
108 * @tc.require: I63TFA
109 */
110 HWTEST_F(LightAgentTest, StartLightTest_001, TestSize.Level1)
111 {
112 CALL_LOG_ENTER;
113 int32_t count = -1;
114 int32_t ret = GetLightList(&g_lightInfo, count);
115 for (int32_t i = 0; i < count; ++i) {
116 MISC_HILOGI("lightId:%{public}d, lightName:%{public}s, lightNumber:%{public}d, lightType:%{public}d",
117 g_lightInfo[i].lightId, g_lightInfo[i].lightName, g_lightInfo[i].lightNumber, g_lightInfo[i].lightType);
118 g_lightId = g_lightInfo[i].lightId;
119 g_lightType = g_lightInfo[i].lightType;
120 }
121 ASSERT_EQ(ret, 0);
122 }
123
124 /**
125 * @tc.name: StartLightTest_002
126 * @tc.desc: Verify GetLightList
127 * @tc.type: FUNC
128 * @tc.require: I63TFA
129 */
130 HWTEST_F(LightAgentTest, StartLightTest_002, TestSize.Level1)
131 {
132 CALL_LOG_ENTER;
133 int32_t count = -1;
134 int32_t ret = GetLightList(nullptr, count);
135 ASSERT_EQ(ret, -1);
136 }
137
GetLightColor(LightColor & color,int32_t lightType)138 bool GetLightColor(LightColor &color, int32_t lightType)
139 {
140 switch (lightType) {
141 case LIGHT_TYPE_SINGLE_COLOR: {
142 color.singleColor = 0Xff;
143 return true;
144 }
145 case LIGHT_TYPE_RGB_COLOR: {
146 color.rgbColor = {
147 .r = 0Xff,
148 .g = 0Xff,
149 .b = 0Xff
150 };
151 return true;
152 }
153 case LIGHT_TYPE_WRGB_COLOR: {
154 color.wrgbColor = {
155 .w = 0Xff,
156 .r = 0Xff,
157 .g = 0Xff,
158 .b = 0Xff
159 };
160 return true;
161 }
162 default: {
163 MISC_HILOGE("lightType:%{public}d invalid", lightType);
164 return false;
165 }
166 }
167 }
168
169 /**
170 * @tc.name: StartLightTest_003
171 * @tc.desc: Verify TurnOn
172 * @tc.type: FUNC
173 * @tc.require: I63TFA
174 */
175 HWTEST_F(LightAgentTest, StartLightTest_003, TestSize.Level1)
176 {
177 CALL_LOG_ENTER;
178 int32_t powerLightId = 1;
179 TurnOff(powerLightId);
180 LightColor color;
181 bool flag = GetLightColor(color, g_lightType);
182 if (!flag) {
183 ASSERT_FALSE(flag);
184 } else {
185 LightAnimation animation;
186 animation.mode = LIGHT_MODE_DEFAULT;
187 animation.onTime = 50;
188 animation.offTime = 50;
189 int32_t ret = TurnOn(g_lightId, color, animation);
190 std::this_thread::sleep_for(std::chrono::milliseconds(TIME_WAIT_FOR_OP));
191 ASSERT_EQ(ret, 0);
192 }
193 }
194
195 /**
196 * @tc.name: StartLightTest_004
197 * @tc.desc: Verify TurnOn
198 * @tc.type: FUNC
199 * @tc.require: I63TFA
200 */
201 HWTEST_F(LightAgentTest, StartLightTest_004, TestSize.Level1)
202 {
203 CALL_LOG_ENTER;
204 LightColor color;
205 bool flag = GetLightColor(color, g_lightType);
206 if (!flag) {
207 ASSERT_FALSE(flag);
208 } else {
209 LightAnimation animation;
210 animation.mode = LIGHT_MODE_BUTT;
211 animation.onTime = 50;
212 animation.offTime = 50;
213 int32_t ret = TurnOn(g_lightId, color, animation);
214 ASSERT_EQ(ret, -1);
215 }
216 }
217
218 /**
219 * @tc.name: StartLightTest_005
220 * @tc.desc: Verify TurnOn
221 * @tc.type: FUNC
222 * @tc.require: I63TFA
223 */
224 HWTEST_F(LightAgentTest, StartLightTest_005, TestSize.Level1)
225 {
226 CALL_LOG_ENTER;
227 LightColor color;
228 bool flag = GetLightColor(color, g_lightType);
229 if (!flag) {
230 ASSERT_FALSE(flag);
231 } else {
232 LightAnimation animation;
233 animation.mode = -1;
234 animation.onTime = 50;
235 animation.offTime = 50;
236 int32_t ret = TurnOn(g_lightId, color, animation);
237 ASSERT_EQ(ret, -1);
238 }
239 }
240
241 /**
242 * @tc.name: StartLightTest_006
243 * @tc.desc: Verify TurnOn
244 * @tc.type: FUNC
245 * @tc.require: I63TFA
246 */
247 HWTEST_F(LightAgentTest, StartLightTest_006, TestSize.Level1)
248 {
249 CALL_LOG_ENTER;
250 LightColor color;
251 bool flag = GetLightColor(color, g_lightType);
252 if (!flag) {
253 ASSERT_FALSE(flag);
254 } else {
255 LightAnimation animation;
256 animation.mode = LIGHT_MODE_DEFAULT;
257 animation.onTime = -1;
258 animation.offTime = 50;
259 int32_t ret = TurnOn(g_lightId, color, animation);
260 ASSERT_EQ(ret, -1);
261 }
262 }
263
264 /**
265 * @tc.name: StartLightTest_007
266 * @tc.desc: Verify TurnOn
267 * @tc.type: FUNC
268 * @tc.require: I63TFA
269 */
270 HWTEST_F(LightAgentTest, StartLightTest_007, TestSize.Level1)
271 {
272 CALL_LOG_ENTER;
273 LightColor color;
274 bool flag = GetLightColor(color, g_lightType);
275 if (!flag) {
276 ASSERT_FALSE(flag);
277 } else {
278 LightAnimation animation;
279 animation.mode = LIGHT_MODE_DEFAULT;
280 animation.onTime = 50;
281 animation.offTime = -1;
282 int32_t ret = TurnOn(g_lightId, color, animation);
283 ASSERT_EQ(ret, -1);
284 }
285 }
286
287 /**
288 * @tc.name: StartLightTest_008
289 * @tc.desc: Verify TurnOn
290 * @tc.type: FUNC
291 * @tc.require: I63TFA
292 */
293 HWTEST_F(LightAgentTest, StartLightTest_008, TestSize.Level1)
294 {
295 CALL_LOG_ENTER;
296 LightColor color;
297 bool flag = GetLightColor(color, g_lightType);
298 if (!flag) {
299 ASSERT_FALSE(flag);
300 } else {
301 LightAnimation animation;
302 animation.mode = LIGHT_MODE_DEFAULT;
303 animation.onTime = 2;
304 animation.offTime = 2;
305 int32_t ret = TurnOn(g_lightId, color, animation);
306 std::this_thread::sleep_for(std::chrono::milliseconds(TIME_WAIT_FOR_OP));
307 ASSERT_EQ(ret, 0);
308 }
309 }
310
311 /**
312 * @tc.name: StartLightTest_009
313 * @tc.desc: Verify TurnOn
314 * @tc.type: FUNC
315 * @tc.require: I63TFA
316 */
317 HWTEST_F(LightAgentTest, StartLightTest_009, TestSize.Level1)
318 {
319 CALL_LOG_ENTER;
320 LightColor color;
321 bool flag = GetLightColor(color, g_lightType);
322 if (!flag) {
323 ASSERT_FALSE(flag);
324 } else {
325 LightAnimation animation;
326 animation.mode = LIGHT_MODE_DEFAULT;
327 animation.onTime = 2;
328 animation.offTime = 2;
329 int32_t ret = TurnOn(g_invalidLightId, color, animation);
330 ASSERT_EQ(ret, -1);
331 }
332 }
333
334 /**
335 * @tc.name: StartLightTest_010
336 * @tc.desc: Verify TurnOff
337 * @tc.type: FUNC
338 * @tc.require: I63TFA
339 */
340 HWTEST_F(LightAgentTest, StartLightTest_010, TestSize.Level1)
341 {
342 CALL_LOG_ENTER;
343 int32_t ret = TurnOff(g_lightId);
344 ASSERT_EQ(ret, 0);
345 }
346
347 /**
348 * @tc.name: StartLightTest_011
349 * @tc.desc: Verify TurnOff
350 * @tc.type: FUNC
351 * @tc.require: I63TFA
352 */
353 HWTEST_F(LightAgentTest, StartLightTest_011, TestSize.Level1)
354 {
355 CALL_LOG_ENTER;
356 int32_t ret = TurnOff(g_invalidLightId);
357 ASSERT_EQ(ret, -1);
358 }
359 } // namespace Sensors
360 } // namespace OHOS
361