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