1/*
2 * Copyright (c) 2023 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 "hdf_base.h"
17#include "light_type.h"
18#include "osal_time.h"
19#include "v1_0/ilight_interface.h"
20#include <cmath>
21#include <cstdio>
22#include <gtest/gtest.h>
23#include <securec.h>
24
25using namespace OHOS::HDI::Light::V1_0;
26using namespace testing::ext;
27
28namespace {
29constexpr uint32_t SLEEPTIME = 3;
30constexpr int32_t MIN_VALUE = 0;
31constexpr int32_t MAX_VALUE = 255;
32std::vector<HdfLightInfo> g_info;
33sptr<ILightInterface> g_lightInterface = nullptr;
34
35class HatsHdfLightCommonTestAdditional : public testing::Test {
36public:
37    static void SetUpTestCase();
38    static void TearDownTestCase();
39    void SetUp();
40    void TearDown();
41};
42
43void HatsHdfLightCommonTestAdditional::SetUpTestCase() { g_lightInterface = ILightInterface::Get(); }
44
45void HatsHdfLightCommonTestAdditional::TearDownTestCase() {}
46
47void HatsHdfLightCommonTestAdditional::SetUp() {}
48
49void HatsHdfLightCommonTestAdditional::TearDown() {}
50
51void InitConfig(HdfLightEffect &effect)
52{
53    effect.lightColor.colorValue.rgbColor.r = MIN_VALUE;
54    effect.lightColor.colorValue.rgbColor.g = MIN_VALUE;
55    effect.lightColor.colorValue.rgbColor.b = MIN_VALUE;
56    effect.flashEffect.flashMode = HDF_LIGHT_FLASH_NONE;
57    effect.flashEffect.onTime = 1;
58    effect.flashEffect.offTime = 1;
59}
60
61/* *
62 * @tc.number: SUB_Driver_Light_GetlightInfo_0200
63 * @tc.name  : testhdiServiceGetLightInfoTimes001
64 * @tc.desc  : Get light info 10times.
65 */
66HWTEST_F(HatsHdfLightCommonTestAdditional, testhdiServiceGetLightInfoTimes001, Function | MediumTest | Level1)
67{
68    int32_t ret;
69
70    ASSERT_NE(nullptr, g_lightInterface);
71
72    for (int i = 0; i < 10; i++) {
73        ret = g_lightInterface->GetLightInfo(g_info);
74        EXPECT_EQ(HDF_SUCCESS, ret);
75        EXPECT_GT(g_info.size(), 0);
76        printf("get light list num[%zu]\n\r", g_info.size());
77
78        for (auto iter : g_info) {
79            printf("lightId[%d], lightName[%s], lightNumber[%d]\n\r", iter.lightId, iter.lightName.c_str(),
80                   iter.lightNumber);
81        }
82    }
83}
84
85/* *
86 * @tc.number: SUB_Driver_Light_TurnOnLight_0500
87 * @tc.name  : testhdiServiceTurnOnLightMAX001
88 * @tc.desc  : Enter the minimum excess value to turn on the red light.
89 */
90HWTEST_F(HatsHdfLightCommonTestAdditional, testhdiServiceTurnOnLightMAX001, Function | MediumTest | Level1)
91{
92    ASSERT_NE(nullptr, g_lightInterface);
93
94    HdfLightEffect effect;
95    InitConfig(effect);
96    effect.lightColor.colorValue.rgbColor.r = MAX_VALUE;
97    effect.lightColor.colorValue.rgbColor.g = MAX_VALUE;
98    effect.lightColor.colorValue.rgbColor.b = MAX_VALUE;
99
100    int32_t ret = g_lightInterface->TurnOnLight(g_info[0].lightId, effect);
101    EXPECT_EQ(HDF_SUCCESS, ret);
102    OsalSleep(SLEEPTIME);
103
104    ret = g_lightInterface->TurnOffLight(g_info[0].lightId);
105    EXPECT_EQ(HDF_SUCCESS, ret);
106}
107
108/* *
109 * @tc.number: SUB_Driver_Light_TurnOnLight_0600
110 * @tc.name  : testhdiServiceTurnOnLightRedMAX001
111 * @tc.desc  : Enter the maximum excess value to turn on the red light.
112 */
113HWTEST_F(HatsHdfLightCommonTestAdditional, testhdiServiceTurnOnLightRedMAX001, Function | MediumTest | Level1)
114{
115    ASSERT_NE(nullptr, g_lightInterface);
116
117    HdfLightEffect effect;
118    InitConfig(effect);
119    effect.lightColor.colorValue.rgbColor.r = MAX_VALUE;
120
121    int32_t ret = g_lightInterface->TurnOnLight(g_info[0].lightId, effect);
122    EXPECT_EQ(HDF_SUCCESS, ret);
123    OsalSleep(SLEEPTIME);
124
125    ret = g_lightInterface->TurnOffLight(g_info[0].lightId);
126    EXPECT_EQ(HDF_SUCCESS, ret);
127}
128
129/* *
130 * @tc.number: SUB_Driver_Light_TurnOnLight_0700
131 * @tc.name  : testhdiServiceTurnOnLightRedMAXTimes001
132 * @tc.desc  : Turn on the light and it's always red 10 times.
133 */
134HWTEST_F(HatsHdfLightCommonTestAdditional, testhdiServiceTurnOnLightRedMAXTimes001, Function | MediumTest | Level1)
135{
136    int32_t ret;
137
138    ASSERT_NE(nullptr, g_lightInterface);
139
140    for (int i = 0; i < 10; i++) {
141        HdfLightEffect effect;
142        InitConfig(effect);
143        effect.lightColor.colorValue.rgbColor.r = MAX_VALUE;
144        ret = g_lightInterface->TurnOnLight(g_info[0].lightId, effect);
145        EXPECT_EQ(HDF_SUCCESS, ret);
146        OsalSleep(SLEEPTIME);
147
148        ret = g_lightInterface->TurnOffLight(g_info[0].lightId);
149        EXPECT_EQ(HDF_SUCCESS, ret);
150    }
151}
152
153/* *
154 * @tc.number: SUB_Driver_Light_TurnOnLight_0800
155 * @tc.name  : testhdiServiceTurnOnLightGreenMAX001
156 * @tc.desc  : Enter the maximum excess value to turn on the green light.
157 */
158HWTEST_F(HatsHdfLightCommonTestAdditional, testhdiServiceTurnOnLightGreenMAX001, Function | MediumTest | Level1)
159{
160    ASSERT_NE(nullptr, g_lightInterface);
161
162    HdfLightEffect effect;
163    InitConfig(effect);
164    effect.lightColor.colorValue.rgbColor.g = MAX_VALUE;
165
166    int32_t ret = g_lightInterface->TurnOnLight(g_info[0].lightId, effect);
167    EXPECT_EQ(HDF_SUCCESS, ret);
168    OsalSleep(SLEEPTIME);
169
170    ret = g_lightInterface->TurnOffLight(g_info[0].lightId);
171    EXPECT_EQ(HDF_SUCCESS, ret);
172}
173
174/* *
175 * @tc.number: SUB_Driver_Light_TurnOnLight_0900
176 * @tc.name  : testhdiServiceTurnOnLightGreenMAXTimes001
177 * @tc.desc  : Turn on the light and it's always green 10 times.
178 */
179HWTEST_F(HatsHdfLightCommonTestAdditional, testhdiServiceTurnOnLightGreenMAXTimes001, Function | MediumTest | Level1)
180{
181    int32_t ret;
182    ASSERT_NE(nullptr, g_lightInterface);
183
184    for (int i = 0; i < 10; i++) {
185        HdfLightEffect effect;
186        InitConfig(effect);
187        effect.lightColor.colorValue.rgbColor.g = MAX_VALUE;
188        ret = g_lightInterface->TurnOnLight(g_info[0].lightId, effect);
189        EXPECT_EQ(HDF_SUCCESS, ret);
190        OsalSleep(SLEEPTIME);
191
192        ret = g_lightInterface->TurnOffLight(g_info[0].lightId);
193        EXPECT_EQ(HDF_SUCCESS, ret);
194    }
195}
196
197/* *
198 * @tc.number: SUB_Driver_Light_TurnOnLight_1000
199 * @tc.name  : testhdiServiceTurnOnLightBlueMAX001
200 * @tc.desc  : Enter the maximum excess value to turn on the blue light.
201 */
202HWTEST_F(HatsHdfLightCommonTestAdditional, testhdiServiceTurnOnLightBlueMAX001, Function | MediumTest | Level1)
203{
204    ASSERT_NE(nullptr, g_lightInterface);
205
206    HdfLightEffect effect;
207    InitConfig(effect);
208    effect.lightColor.colorValue.rgbColor.b = MAX_VALUE;
209
210    int32_t ret = g_lightInterface->TurnOnLight(g_info[0].lightId, effect);
211    EXPECT_EQ(HDF_SUCCESS, ret);
212    OsalSleep(SLEEPTIME);
213
214    ret = g_lightInterface->TurnOffLight(g_info[0].lightId);
215    EXPECT_EQ(HDF_SUCCESS, ret);
216}
217
218/* *
219 * @tc.number: SUB_Driver_Light_TurnOnLight_1100
220 * @tc.name  : testhdiServiceTurnOnLightBlueMAXTimes001
221 * @tc.desc  : Turn on the light and it's always blue 10 times.
222 */
223HWTEST_F(HatsHdfLightCommonTestAdditional, testhdiServiceTurnOnLightBlueMAXTimes001, Function | MediumTest | Level1)
224{
225    int32_t ret;
226    ASSERT_NE(nullptr, g_lightInterface);
227
228    for (int i = 0; i < 10; i++) {
229        HdfLightEffect effect;
230        InitConfig(effect);
231        effect.lightColor.colorValue.rgbColor.b = MAX_VALUE;
232        ret = g_lightInterface->TurnOnLight(g_info[0].lightId, effect);
233        EXPECT_EQ(HDF_SUCCESS, ret);
234        OsalSleep(SLEEPTIME);
235
236        ret = g_lightInterface->TurnOffLight(g_info[0].lightId);
237        EXPECT_EQ(HDF_SUCCESS, ret);
238    }
239}
240
241/* *
242 * @tc.number: SUB_Driver_Light_TurnOnLight_1200
243 * @tc.name  : testhdiServiceTurnOnLightFM001
244 * @tc.desc  : The input is Abnormal flashmode.
245 */
246HWTEST_F(HatsHdfLightCommonTestAdditional, testhdiServiceTurnOnLightFM001, Function | MediumTest | Level1)
247{
248    ASSERT_NE(nullptr, g_lightInterface);
249
250    HdfLightEffect effect;
251    InitConfig(effect);
252    effect.flashEffect.flashMode = 0;
253
254    int32_t ret = g_lightInterface->TurnOnLight(g_info[0].lightId, effect);
255    EXPECT_EQ(HDF_SUCCESS, ret);
256
257    ret = g_lightInterface->TurnOffLight(g_info[0].lightId);
258    EXPECT_EQ(HDF_SUCCESS, ret);
259}
260
261/* *
262 * @tc.number: SUB_Driver_Light_TurnOnLight_1300
263 * @tc.name  : testhdiServiceTurnOnLightTime001
264 * @tc.desc  : Turn on the light Abnormal times.
265 */
266HWTEST_F(HatsHdfLightCommonTestAdditional, testhdiServiceTurnOnLightTime001, Function | MediumTest | Level1)
267{
268    ASSERT_NE(nullptr, g_lightInterface);
269
270    HdfLightEffect effect;
271    InitConfig(effect);
272    effect.flashEffect.onTime = -1;
273    effect.flashEffect.offTime = 10;
274
275    int32_t ret = g_lightInterface->TurnOnLight(g_info[0].lightId, effect);
276    EXPECT_EQ(HDF_SUCCESS, ret);
277
278    ret = g_lightInterface->TurnOffLight(g_info[0].lightId);
279    EXPECT_EQ(HDF_SUCCESS, ret);
280}
281
282/* *
283 * @tc.number: SUB_Driver_Light_TurnOnLight_1400
284 * @tc.name  : testhdiServiceTurnOnLightTime002
285 * @tc.desc  : Turn on the light Abnormal times.
286 */
287HWTEST_F(HatsHdfLightCommonTestAdditional, testhdiServiceTurnOnLightTime002, Function | MediumTest | Level1)
288{
289    ASSERT_NE(nullptr, g_lightInterface);
290
291    HdfLightEffect effect;
292    InitConfig(effect);
293    effect.flashEffect.onTime = 10;
294    effect.flashEffect.offTime = -1;
295
296    int32_t ret = g_lightInterface->TurnOnLight(g_info[0].lightId, effect);
297    EXPECT_EQ(HDF_SUCCESS, ret);
298
299    ret = g_lightInterface->TurnOffLight(g_info[0].lightId);
300    EXPECT_EQ(HDF_SUCCESS, ret);
301}
302
303/* *
304 * @tc.number: SUB_Driver_Light_TurnOnLight_1500
305 * @tc.name  : testhdiServiceTurnOnLightTime003
306 * @tc.desc  : Turn on the light times.
307 */
308HWTEST_F(HatsHdfLightCommonTestAdditional, testhdiServiceTurnOnLightTime003, Function | MediumTest | Level1)
309{
310    ASSERT_NE(nullptr, g_lightInterface);
311
312    HdfLightEffect effect;
313    InitConfig(effect);
314    effect.flashEffect.onTime = 256;
315    effect.flashEffect.offTime = 0;
316
317    int32_t ret = g_lightInterface->TurnOnLight(g_info[0].lightId, effect);
318    EXPECT_EQ(HDF_SUCCESS, ret);
319
320    ret = g_lightInterface->TurnOffLight(g_info[0].lightId);
321    EXPECT_EQ(HDF_SUCCESS, ret);
322}
323
324/* *
325 * @tc.number: SUB_Driver_Light_TurnOnLight_1600
326 * @tc.name  : testhdiServiceTurnOnLightTime004
327 * @tc.desc  : Turn on the light times.
328 */
329HWTEST_F(HatsHdfLightCommonTestAdditional, testhdiServiceTurnOnLightTime004, Function | MediumTest | Level1)
330{
331    ASSERT_NE(nullptr, g_lightInterface);
332
333    HdfLightEffect effect;
334    InitConfig(effect);
335    effect.flashEffect.onTime = 0;
336    effect.flashEffect.offTime = 256;
337
338    int32_t ret = g_lightInterface->TurnOnLight(g_info[0].lightId, effect);
339    EXPECT_EQ(HDF_SUCCESS, ret);
340
341    ret = g_lightInterface->TurnOffLight(g_info[0].lightId);
342    EXPECT_EQ(HDF_SUCCESS, ret);
343}
344
345/* *
346 * @tc.number: SUB_Driver_Light_TurnOnMultiLights_0200
347 * @tc.name  : testhdiServiceTurnOnMultiLightsRedMAX001
348 * @tc.desc  : Turn on the light Abnormal lightid.
349 */
350HWTEST_F(HatsHdfLightCommonTestAdditional, testhdiServiceTurnOnMultiLightsRedMAX001, Function | MediumTest | Level1)
351{
352    ASSERT_NE(nullptr, g_lightInterface);
353
354    std::vector<HdfLightColor> lightColor;
355    struct HdfLightColor light;
356    light.colorValue.rgbColor.r = MAX_VALUE;
357    lightColor.push_back(light);
358
359    int32_t ret = g_lightInterface->TurnOnMultiLights(HDF_LIGHT_ID_BATTERY, lightColor);
360    EXPECT_EQ(HDF_SUCCESS, ret);
361    OsalSleep(SLEEPTIME);
362
363    ret = g_lightInterface->TurnOffLight(HDF_LIGHT_ID_NOTIFICATIONS);
364    EXPECT_EQ(HDF_FAILURE, ret);
365}
366
367/* *
368 * @tc.number: SUB_Driver_Light_TurnOnMultiLights_0300
369 * @tc.name  : testhdiServiceTurnOnMultiLightsTimes001
370 * @tc.desc  : Turn on the light lightid 10 times.
371 */
372HWTEST_F(HatsHdfLightCommonTestAdditional, testhdiServiceTurnOnMultiLightsTimes001, Function | MediumTest | Level1)
373{
374    int32_t ret;
375
376    ASSERT_NE(nullptr, g_lightInterface);
377    for (int i = 0; i < 10; i++) {
378        std::vector<HdfLightColor> lightColor;
379        struct HdfLightColor light;
380        light.colorValue.rgbColor.r = MAX_VALUE;
381        light.colorValue.rgbColor.g = MIN_VALUE;
382        light.colorValue.rgbColor.b = MIN_VALUE;
383        lightColor.push_back(light);
384        ret = g_lightInterface->TurnOnMultiLights(HDF_LIGHT_ID_BATTERY, lightColor);
385        EXPECT_EQ(HDF_SUCCESS, ret);
386        OsalSleep(SLEEPTIME);
387
388        ret = g_lightInterface->TurnOffLight(HDF_LIGHT_ID_BATTERY);
389        EXPECT_EQ(HDF_SUCCESS, ret);
390    }
391}
392
393/* *
394 * @tc.number: SUB_Driver_Light_TurnOffLight_0100
395 * @tc.name  : testhdiServiceTurnOffBattery001
396 * @tc.desc  : Turn Off Light.
397 */
398HWTEST_F(HatsHdfLightCommonTestAdditional, testhdiServiceTurnOffBattery001, Function | MediumTest | Level1)
399{
400    ASSERT_NE(nullptr, g_lightInterface);
401
402    std::vector<HdfLightColor> lightColor;
403    struct HdfLightColor light;
404    light.colorValue.rgbColor.r = MAX_VALUE;
405    lightColor.push_back(light);
406
407    int32_t ret = g_lightInterface->TurnOnMultiLights(HDF_LIGHT_ID_BATTERY, lightColor);
408    EXPECT_EQ(HDF_SUCCESS, ret);
409    OsalSleep(SLEEPTIME);
410
411    ret = g_lightInterface->TurnOffLight(HDF_LIGHT_ID_BATTERY);
412    EXPECT_EQ(HDF_SUCCESS, ret);
413}
414
415/* *
416 * @tc.number: SUB_Driver_Light_TurnOffLight_0200
417 * @tc.name  : testhdiServiceTurnOffNotifications001
418 * @tc.desc  : Turn Off Light.
419 */
420HWTEST_F(HatsHdfLightCommonTestAdditional, testhdiServiceTurnOffNotifications001, Function | MediumTest | Level2)
421{
422    ASSERT_NE(nullptr, g_lightInterface);
423
424    std::vector<HdfLightColor> lightColor;
425    struct HdfLightColor light;
426    light.colorValue.rgbColor.r = MAX_VALUE;
427    lightColor.push_back(light);
428
429    int32_t ret = g_lightInterface->TurnOnMultiLights(HDF_LIGHT_ID_BATTERY, lightColor);
430    EXPECT_EQ(HDF_SUCCESS, ret);
431    OsalSleep(SLEEPTIME);
432
433    ret = g_lightInterface->TurnOffLight(HDF_LIGHT_ID_NOTIFICATIONS);
434    EXPECT_EQ(HDF_FAILURE, ret);
435}
436
437/* *
438 * @tc.number: SUB_Driver_Light_TurnOffLight_0300
439 * @tc.name  : testhdiServiceTurnOffAttention001
440 * @tc.desc  : Turn Off Light.
441 */
442HWTEST_F(HatsHdfLightCommonTestAdditional, testhdiServiceTurnOffAttention001, Function | MediumTest | Level2)
443{
444    ASSERT_NE(nullptr, g_lightInterface);
445
446    std::vector<HdfLightColor> lightColor;
447    struct HdfLightColor light;
448    light.colorValue.rgbColor.r = MAX_VALUE;
449    lightColor.push_back(light);
450
451    int32_t ret = g_lightInterface->TurnOnMultiLights(HDF_LIGHT_ID_BATTERY, lightColor);
452    EXPECT_EQ(HDF_SUCCESS, ret);
453    OsalSleep(SLEEPTIME);
454
455    ret = g_lightInterface->TurnOffLight(HDF_LIGHT_ID_ATTENTION);
456    EXPECT_EQ(HDF_FAILURE, ret);
457}
458
459/* *
460 * @tc.number: SUB_Driver_Light_TurnOffLight_0400
461 * @tc.name  : testhdiServiceTurnOffButt001
462 * @tc.desc  : Turn Off Light.
463 */
464HWTEST_F(HatsHdfLightCommonTestAdditional, testhdiServiceTurnOffButt001, Function | MediumTest | Level2)
465{
466    ASSERT_NE(nullptr, g_lightInterface);
467
468    std::vector<HdfLightColor> lightColor;
469    struct HdfLightColor light;
470    light.colorValue.rgbColor.r = MAX_VALUE;
471    lightColor.push_back(light);
472
473    int32_t ret = g_lightInterface->TurnOnMultiLights(HDF_LIGHT_ID_BUTT, lightColor);
474    EXPECT_EQ(-2, ret);
475    OsalSleep(SLEEPTIME);
476
477    ret = g_lightInterface->TurnOffLight(HDF_LIGHT_ID_BUTT);
478    EXPECT_EQ(HDF_FAILURE, ret);
479}
480
481/* *
482 * @tc.number: SUB_Driver_Light_TurnOffLight_0500
483 * @tc.name  : testhdiServiceTurnOffNoOn001
484 * @tc.desc  : Turn it off when it's not on.
485 */
486HWTEST_F(HatsHdfLightCommonTestAdditional, testhdiServiceTurnOffNoOn001, Function | MediumTest | Level2)
487{
488    ASSERT_NE(nullptr, g_lightInterface);
489
490    std::vector<HdfLightColor> lightColor;
491    struct HdfLightColor light;
492    light.colorValue.rgbColor.r = MAX_VALUE;
493    lightColor.push_back(light);
494
495    int32_t ret = g_lightInterface->TurnOffLight(HDF_LIGHT_ID_BUTT);
496    EXPECT_EQ(HDF_FAILURE, ret);
497}
498} // namespace