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