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
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 int32_t ret = AccessTokenKit::DeleteToken(tokenID_);
89 if (tokenID_ != 0) {
90 ASSERT_EQ(RET_SUCCESS, ret);
91 }
92 }
93
94 /**
95 * @tc.name: StartLightTest_001
96 * @tc.desc: Verify GetLightList
97 * @tc.type: FUNC
98 * @tc.require: I63TFA
99 */
HWTEST_F(LightAgentTest, StartLightTest_001, TestSize.Level1)100 HWTEST_F(LightAgentTest, StartLightTest_001, TestSize.Level1)
101 {
102 CALL_LOG_ENTER;
103 int32_t count = -1;
104 int32_t ret = GetLightList(&g_lightInfo, count);
105 for (int32_t i = 0; i < count; ++i) {
106 MISC_HILOGI("lightId:%{public}d, lightName:%{public}s, lightNumber:%{public}d, lightType:%{public}d",
107 g_lightInfo[i].lightId, g_lightInfo[i].lightName, g_lightInfo[i].lightNumber, g_lightInfo[i].lightType);
108 g_lightId = g_lightInfo[i].lightId;
109 g_lightType = g_lightInfo[i].lightType;
110 }
111 ASSERT_EQ(ret, 0);
112 }
113
114 /**
115 * @tc.name: StartLightTest_002
116 * @tc.desc: Verify GetLightList
117 * @tc.type: FUNC
118 * @tc.require: I63TFA
119 */
HWTEST_F(LightAgentTest, StartLightTest_002, TestSize.Level1)120 HWTEST_F(LightAgentTest, StartLightTest_002, TestSize.Level1)
121 {
122 CALL_LOG_ENTER;
123 int32_t count = -1;
124 int32_t ret = GetLightList(nullptr, count);
125 ASSERT_EQ(ret, -1);
126 }
127
GetLightColor(LightColor &color, int32_t lightType)128 bool GetLightColor(LightColor &color, int32_t lightType)
129 {
130 switch (lightType) {
131 case LIGHT_TYPE_SINGLE_COLOR: {
132 color.singleColor = 0Xff;
133 return true;
134 }
135 case LIGHT_TYPE_RGB_COLOR: {
136 color.rgbColor = {
137 .r = 0Xff,
138 .g = 0Xff,
139 .b = 0Xff
140 };
141 return true;
142 }
143 case LIGHT_TYPE_WRGB_COLOR: {
144 color.wrgbColor = {
145 .w = 0Xff,
146 .r = 0Xff,
147 .g = 0Xff,
148 .b = 0Xff
149 };
150 return true;
151 }
152 default: {
153 MISC_HILOGE("lightType:%{public}d invalid", lightType);
154 return false;
155 }
156 }
157 }
158
159 /**
160 * @tc.name: StartLightTest_003
161 * @tc.desc: Verify TurnOn
162 * @tc.type: FUNC
163 * @tc.require: I63TFA
164 */
HWTEST_F(LightAgentTest, StartLightTest_003, TestSize.Level1)165 HWTEST_F(LightAgentTest, StartLightTest_003, TestSize.Level1)
166 {
167 CALL_LOG_ENTER;
168 int32_t powerLightId = 1;
169 TurnOff(powerLightId);
170 LightColor color;
171 bool flag = GetLightColor(color, g_lightType);
172 if (!flag) {
173 ASSERT_FALSE(flag);
174 } else {
175 LightAnimation animation;
176 animation.mode = LIGHT_MODE_DEFAULT;
177 animation.onTime = 50;
178 animation.offTime = 50;
179 int32_t ret = TurnOn(g_lightId, color, animation);
180 std::this_thread::sleep_for(std::chrono::milliseconds(TIME_WAIT_FOR_OP));
181 ASSERT_EQ(ret, 0);
182 }
183 }
184
185 /**
186 * @tc.name: StartLightTest_004
187 * @tc.desc: Verify TurnOn
188 * @tc.type: FUNC
189 * @tc.require: I63TFA
190 */
HWTEST_F(LightAgentTest, StartLightTest_004, TestSize.Level1)191 HWTEST_F(LightAgentTest, StartLightTest_004, TestSize.Level1)
192 {
193 CALL_LOG_ENTER;
194 LightColor color;
195 bool flag = GetLightColor(color, g_lightType);
196 if (!flag) {
197 ASSERT_FALSE(flag);
198 } else {
199 LightAnimation animation;
200 animation.mode = LIGHT_MODE_BUTT;
201 animation.onTime = 50;
202 animation.offTime = 50;
203 int32_t ret = TurnOn(g_lightId, color, animation);
204 ASSERT_EQ(ret, -1);
205 }
206 }
207
208 /**
209 * @tc.name: StartLightTest_005
210 * @tc.desc: Verify TurnOn
211 * @tc.type: FUNC
212 * @tc.require: I63TFA
213 */
HWTEST_F(LightAgentTest, StartLightTest_005, TestSize.Level1)214 HWTEST_F(LightAgentTest, StartLightTest_005, TestSize.Level1)
215 {
216 CALL_LOG_ENTER;
217 LightColor color;
218 bool flag = GetLightColor(color, g_lightType);
219 if (!flag) {
220 ASSERT_FALSE(flag);
221 } else {
222 LightAnimation animation;
223 animation.mode = -1;
224 animation.onTime = 50;
225 animation.offTime = 50;
226 int32_t ret = TurnOn(g_lightId, color, animation);
227 ASSERT_EQ(ret, -1);
228 }
229 }
230
231 /**
232 * @tc.name: StartLightTest_006
233 * @tc.desc: Verify TurnOn
234 * @tc.type: FUNC
235 * @tc.require: I63TFA
236 */
HWTEST_F(LightAgentTest, StartLightTest_006, TestSize.Level1)237 HWTEST_F(LightAgentTest, StartLightTest_006, TestSize.Level1)
238 {
239 CALL_LOG_ENTER;
240 LightColor color;
241 bool flag = GetLightColor(color, g_lightType);
242 if (!flag) {
243 ASSERT_FALSE(flag);
244 } else {
245 LightAnimation animation;
246 animation.mode = LIGHT_MODE_DEFAULT;
247 animation.onTime = -1;
248 animation.offTime = 50;
249 int32_t ret = TurnOn(g_lightId, color, animation);
250 ASSERT_EQ(ret, -1);
251 }
252 }
253
254 /**
255 * @tc.name: StartLightTest_007
256 * @tc.desc: Verify TurnOn
257 * @tc.type: FUNC
258 * @tc.require: I63TFA
259 */
HWTEST_F(LightAgentTest, StartLightTest_007, TestSize.Level1)260 HWTEST_F(LightAgentTest, StartLightTest_007, TestSize.Level1)
261 {
262 CALL_LOG_ENTER;
263 LightColor color;
264 bool flag = GetLightColor(color, g_lightType);
265 if (!flag) {
266 ASSERT_FALSE(flag);
267 } else {
268 LightAnimation animation;
269 animation.mode = LIGHT_MODE_DEFAULT;
270 animation.onTime = 50;
271 animation.offTime = -1;
272 int32_t ret = TurnOn(g_lightId, color, animation);
273 ASSERT_EQ(ret, -1);
274 }
275 }
276
277 /**
278 * @tc.name: StartLightTest_008
279 * @tc.desc: Verify TurnOn
280 * @tc.type: FUNC
281 * @tc.require: I63TFA
282 */
HWTEST_F(LightAgentTest, StartLightTest_008, TestSize.Level1)283 HWTEST_F(LightAgentTest, StartLightTest_008, TestSize.Level1)
284 {
285 CALL_LOG_ENTER;
286 LightColor color;
287 bool flag = GetLightColor(color, g_lightType);
288 if (!flag) {
289 ASSERT_FALSE(flag);
290 } else {
291 LightAnimation animation;
292 animation.mode = LIGHT_MODE_DEFAULT;
293 animation.onTime = 2;
294 animation.offTime = 2;
295 int32_t ret = TurnOn(g_lightId, color, animation);
296 std::this_thread::sleep_for(std::chrono::milliseconds(TIME_WAIT_FOR_OP));
297 ASSERT_EQ(ret, 0);
298 }
299 }
300
301 /**
302 * @tc.name: StartLightTest_009
303 * @tc.desc: Verify TurnOn
304 * @tc.type: FUNC
305 * @tc.require: I63TFA
306 */
HWTEST_F(LightAgentTest, StartLightTest_009, TestSize.Level1)307 HWTEST_F(LightAgentTest, StartLightTest_009, TestSize.Level1)
308 {
309 CALL_LOG_ENTER;
310 LightColor color;
311 bool flag = GetLightColor(color, g_lightType);
312 if (!flag) {
313 ASSERT_FALSE(flag);
314 } else {
315 LightAnimation animation;
316 animation.mode = LIGHT_MODE_DEFAULT;
317 animation.onTime = 2;
318 animation.offTime = 2;
319 int32_t ret = TurnOn(g_invalidLightId, color, animation);
320 ASSERT_EQ(ret, -1);
321 }
322 }
323
324 /**
325 * @tc.name: StartLightTest_010
326 * @tc.desc: Verify TurnOff
327 * @tc.type: FUNC
328 * @tc.require: I63TFA
329 */
HWTEST_F(LightAgentTest, StartLightTest_010, TestSize.Level1)330 HWTEST_F(LightAgentTest, StartLightTest_010, TestSize.Level1)
331 {
332 CALL_LOG_ENTER;
333 int32_t ret = TurnOff(g_lightId);
334 ASSERT_EQ(ret, 0);
335 }
336
337 /**
338 * @tc.name: StartLightTest_011
339 * @tc.desc: Verify TurnOff
340 * @tc.type: FUNC
341 * @tc.require: I63TFA
342 */
HWTEST_F(LightAgentTest, StartLightTest_011, TestSize.Level1)343 HWTEST_F(LightAgentTest, StartLightTest_011, TestSize.Level1)
344 {
345 CALL_LOG_ENTER;
346 int32_t ret = TurnOff(g_invalidLightId);
347 ASSERT_EQ(ret, -1);
348 }
349 } // namespace Sensors
350 } // namespace OHOS
351