• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "light_agent.h"
20 #include "sensors_errors.h"
21 
22 namespace OHOS {
23 namespace Sensors {
24 using namespace testing::ext;
25 using namespace OHOS::HiviewDFX;
26 
27 namespace {
28 constexpr int32_t TIME_WAIT_FOR_OP = 2;
29 constexpr HiLogLabel LABEL = { LOG_CORE, MISC_LOG_DOMAIN, "LightAgentTest" };
30 }  // namespace
31 
32 class LightAgentTest : public testing::Test {
33 public:
SetUpTestCase()34     static void SetUpTestCase() {}
TearDownTestCase()35     static void TearDownTestCase() {}
SetUp()36     void SetUp() {}
TearDown()37     void TearDown() {}
38 };
39 
40 LightInfo *g_lightInfo = nullptr;
41 int32_t g_lightId = -1;
42 int32_t g_invalidLightId = -1;
43 int32_t g_lightType = -1;
44 
45 /**
46  * @tc.name: StartLightTest_001
47  * @tc.desc: Verify GetLightList
48  * @tc.type: FUNC
49  * @tc.require: I63TFA
50  */
51 HWTEST_F(LightAgentTest, StartLightTest_001, TestSize.Level1)
52 {
53     CALL_LOG_ENTER;
54     int32_t count = -1;
55     int32_t ret = GetLightList(&g_lightInfo, count);
56     for (int32_t i = 0; i < count; ++i) {
57         MISC_HILOGI("lightId:%{public}d, lightName:%{public}s, lightNumber:%{public}d, lightType:%{public}d",
58             g_lightInfo[i].lightId, g_lightInfo[i].lightName, g_lightInfo[i].lightNumber, g_lightInfo[i].lightType);
59         g_lightId = g_lightInfo[i].lightId;
60         g_lightType = g_lightInfo[i].lightType;
61     }
62     ASSERT_EQ(ret, 0);
63 }
64 
65 /**
66  * @tc.name: StartLightTest_002
67  * @tc.desc: Verify GetLightList
68  * @tc.type: FUNC
69  * @tc.require: I63TFA
70  */
71 HWTEST_F(LightAgentTest, StartLightTest_002, TestSize.Level1)
72 {
73     CALL_LOG_ENTER;
74     int32_t count = -1;
75     int32_t ret = GetLightList(nullptr, count);
76     ASSERT_EQ(ret, -1);
77 }
78 
GetLightColor(LightColor & color,int32_t lightType)79 bool GetLightColor(LightColor &color, int32_t lightType)
80 {
81     switch (lightType) {
82         case LIGHT_TYPE_SINGLE_COLOR: {
83             color.singleColor = 0Xff;
84             return true;
85         }
86         case LIGHT_TYPE_RGB_COLOR: {
87             color.rgbColor = {
88                 .r = 0Xff,
89                 .g = 0Xff,
90                 .b = 0Xff
91             };
92             return true;
93         }
94         case LIGHT_TYPE_WRGB_COLOR: {
95             color.wrgbColor = {
96                 .w = 0Xff,
97                 .r = 0Xff,
98                 .g = 0Xff,
99                 .b = 0Xff
100             };
101             return true;
102         }
103         default: {
104             MISC_HILOGE("lightType:%{public}d invalid", lightType);
105             return false;
106         }
107     }
108 }
109 
110 /**
111  * @tc.name: StartLightTest_003
112  * @tc.desc: Verify TurnOn
113  * @tc.type: FUNC
114  * @tc.require: I63TFA
115  */
116 HWTEST_F(LightAgentTest, StartLightTest_003, TestSize.Level1)
117 {
118     CALL_LOG_ENTER;
119     int32_t powerLightId = 1;
120     TurnOff(powerLightId);
121     LightColor color;
122     bool flag = GetLightColor(color, g_lightType);
123     if (!flag) {
124         ASSERT_FALSE(flag);
125     } else {
126         LightAnimation animation;
127         animation.mode = LIGHT_MODE_DEFAULT;
128         animation.onTime = 50;
129         animation.offTime = 50;
130         int32_t ret = TurnOn(g_lightId, color, animation);
131         std::this_thread::sleep_for(std::chrono::milliseconds(TIME_WAIT_FOR_OP));
132         ASSERT_EQ(ret, 0);
133     }
134 }
135 
136 /**
137  * @tc.name: StartLightTest_004
138  * @tc.desc: Verify TurnOn
139  * @tc.type: FUNC
140  * @tc.require: I63TFA
141  */
142 HWTEST_F(LightAgentTest, StartLightTest_004, TestSize.Level1)
143 {
144     CALL_LOG_ENTER;
145     LightColor color;
146     bool flag = GetLightColor(color, g_lightType);
147     if (!flag) {
148         ASSERT_FALSE(flag);
149     } else {
150         LightAnimation animation;
151         animation.mode = LIGHT_MODE_BUTT;
152         animation.onTime = 50;
153         animation.offTime = 50;
154         int32_t ret = TurnOn(g_lightId, color, animation);
155         ASSERT_EQ(ret, -1);
156     }
157 }
158 
159 /**
160  * @tc.name: StartLightTest_005
161  * @tc.desc: Verify TurnOn
162  * @tc.type: FUNC
163  * @tc.require: I63TFA
164  */
165 HWTEST_F(LightAgentTest, StartLightTest_005, TestSize.Level1)
166 {
167     CALL_LOG_ENTER;
168     LightColor color;
169     bool flag = GetLightColor(color, g_lightType);
170     if (!flag) {
171         ASSERT_FALSE(flag);
172     } else {
173         LightAnimation animation;
174         animation.mode = -1;
175         animation.onTime = 50;
176         animation.offTime = 50;
177         int32_t ret = TurnOn(g_lightId, color, animation);
178         ASSERT_EQ(ret, -1);
179     }
180 }
181 
182 /**
183  * @tc.name: StartLightTest_006
184  * @tc.desc: Verify TurnOn
185  * @tc.type: FUNC
186  * @tc.require: I63TFA
187  */
188 HWTEST_F(LightAgentTest, StartLightTest_006, TestSize.Level1)
189 {
190     CALL_LOG_ENTER;
191     LightColor color;
192     bool flag = GetLightColor(color, g_lightType);
193     if (!flag) {
194         ASSERT_FALSE(flag);
195     } else {
196         LightAnimation animation;
197         animation.mode = LIGHT_MODE_DEFAULT;
198         animation.onTime = -1;
199         animation.offTime = 50;
200         int32_t ret = TurnOn(g_lightId, color, animation);
201         ASSERT_EQ(ret, -1);
202     }
203 }
204 
205 /**
206  * @tc.name: StartLightTest_007
207  * @tc.desc: Verify TurnOn
208  * @tc.type: FUNC
209  * @tc.require: I63TFA
210  */
211 HWTEST_F(LightAgentTest, StartLightTest_007, TestSize.Level1)
212 {
213     CALL_LOG_ENTER;
214     LightColor color;
215     bool flag = GetLightColor(color, g_lightType);
216     if (!flag) {
217         ASSERT_FALSE(flag);
218     } else {
219         LightAnimation animation;
220         animation.mode = LIGHT_MODE_DEFAULT;
221         animation.onTime = 50;
222         animation.offTime = -1;
223         int32_t ret = TurnOn(g_lightId, color, animation);
224         ASSERT_EQ(ret, -1);
225     }
226 }
227 
228 /**
229  * @tc.name: StartLightTest_008
230  * @tc.desc: Verify TurnOn
231  * @tc.type: FUNC
232  * @tc.require: I63TFA
233  */
234 HWTEST_F(LightAgentTest, StartLightTest_008, TestSize.Level1)
235 {
236     CALL_LOG_ENTER;
237     LightColor color;
238     bool flag = GetLightColor(color, g_lightType);
239     if (!flag) {
240         ASSERT_FALSE(flag);
241     } else {
242         LightAnimation animation;
243         animation.mode = LIGHT_MODE_DEFAULT;
244         animation.onTime = 2;
245         animation.offTime = 2;
246         int32_t ret = TurnOn(g_lightId, color, animation);
247         std::this_thread::sleep_for(std::chrono::milliseconds(TIME_WAIT_FOR_OP));
248         ASSERT_EQ(ret, 0);
249     }
250 }
251 
252 /**
253  * @tc.name: StartLightTest_009
254  * @tc.desc: Verify TurnOn
255  * @tc.type: FUNC
256  * @tc.require: I63TFA
257  */
258 HWTEST_F(LightAgentTest, StartLightTest_009, TestSize.Level1)
259 {
260     CALL_LOG_ENTER;
261     LightColor color;
262     bool flag = GetLightColor(color, g_lightType);
263     if (!flag) {
264         ASSERT_FALSE(flag);
265     } else {
266         LightAnimation animation;
267         animation.mode = LIGHT_MODE_DEFAULT;
268         animation.onTime = 2;
269         animation.offTime = 2;
270         int32_t ret = TurnOn(g_invalidLightId, color, animation);
271         ASSERT_EQ(ret, -1);
272     }
273 }
274 
275 /**
276  * @tc.name: StartLightTest_010
277  * @tc.desc: Verify TurnOff
278  * @tc.type: FUNC
279  * @tc.require: I63TFA
280  */
281 HWTEST_F(LightAgentTest, StartLightTest_010, TestSize.Level1)
282 {
283     CALL_LOG_ENTER;
284     int32_t ret = TurnOff(g_lightId);
285     ASSERT_EQ(ret, 0);
286 }
287 
288 /**
289  * @tc.name: StartLightTest_011
290  * @tc.desc: Verify TurnOff
291  * @tc.type: FUNC
292  * @tc.require: I63TFA
293  */
294 HWTEST_F(LightAgentTest, StartLightTest_011, TestSize.Level1)
295 {
296     CALL_LOG_ENTER;
297     int32_t ret = TurnOff(g_invalidLightId);
298     ASSERT_EQ(ret, -1);
299 }
300 }  // namespace Sensors
301 }  // namespace OHOS
302