1/*
2 * Copyright (c) 2021-2022 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 <cmath>
17#include <cstdio>
18#include <gtest/gtest.h>
19#include <securec.h>
20#include "hdf_base.h"
21#include "osal_time.h"
22#include "osal_mem.h"
23#include "light_if.h"
24#include "light_type.h"
25
26using namespace testing::ext;
27
28namespace {
29    const struct LightInterface *g_lightDev = nullptr;
30    static struct LightInfo *g_lightInfo = nullptr;
31    static uint32_t g_count = 0;
32    const int32_t g_onTime = 500;
33    const int32_t g_offTime = 500;
34    const int32_t LIGHT_WAIT_TIME = 3;
35    const int32_t g_minLightId = LIGHT_ID_NONE;
36    const int32_t g_maxLightId = LIGHT_ID_BUTT;
37}
38
39class HdfLightTest : public testing::Test {
40public:
41    static void SetUpTestCase();
42    static void TearDownTestCase();
43    void SetUp();
44    void TearDown();
45};
46
47void HdfLightTest::SetUpTestCase()
48{
49    g_lightDev = NewLightInterfaceInstance();
50    if (g_lightDev == nullptr) {
51        printf("test light get Module instance failed\n\r");
52    }
53    int32_t ret = g_lightDev->GetLightInfo(&g_lightInfo, &g_count);
54    if (ret == HDF_FAILURE) {
55        printf("get light information failed\n\r");
56    }
57}
58
59void HdfLightTest::TearDownTestCase()
60{
61    if (g_lightDev != nullptr) {
62        FreeLightInterfaceInstance();
63        g_lightDev = nullptr;
64    }
65}
66
67void HdfLightTest::SetUp()
68{
69}
70
71void HdfLightTest::TearDown()
72{
73}
74
75/**
76  * @tc.name: CheckLightInstanceIsEmpty
77  * @tc.desc: Creat a light instance. The instance is not empty.
78  * @tc.type: FUNC
79  */
80HWTEST_F(HdfLightTest, SUB_DriverSystem_LightHdi_0010, Function | MediumTest | Level1)
81{
82    ASSERT_NE(nullptr, g_lightDev);
83}
84
85/**
86  * @tc.name: GetLightList001
87  * @tc.desc: Obtains information about all lights in the system. Validity check of input parameters.
88  * @tc.type: FUNC
89  */
90HWTEST_F(HdfLightTest, SUB_DriverSystem_LightHdi_0100, Function | MediumTest | Level1)
91{
92    struct LightInfo *info = nullptr;
93
94    if (g_lightInfo == nullptr) {
95        EXPECT_NE(nullptr, g_lightInfo);
96        return;
97    }
98
99    printf("get light list num[%d]\n\r", g_count);
100    info = g_lightInfo;
101
102    for (int i = 0; i < g_count; ++i) {
103        printf("get lightId[%d]\n\r", info->lightId);
104        EXPECT_GE(info->lightId, g_minLightId);
105        EXPECT_LE(info->lightId, g_maxLightId);
106        info++;
107    }
108}
109
110/**
111  * @tc.name: GetLightList002
112  * @tc.desc: Obtains information about all lights in the system. Validity check of input parameters.
113  * @tc.type: FUNC
114  */
115HWTEST_F(HdfLightTest, SUB_DriverSystem_LightHdi_0110 , Function | MediumTest | Level1)
116{
117    int32_t ret = g_lightDev->GetLightInfo(nullptr, &g_count);
118    EXPECT_EQ(HDF_FAILURE, ret);
119    ret = g_lightDev->GetLightInfo(&g_lightInfo, nullptr);
120    EXPECT_EQ(HDF_FAILURE, ret);
121    ret = g_lightDev->GetLightInfo(nullptr, nullptr);
122    EXPECT_EQ(HDF_FAILURE, ret);
123}
124
125/**
126  * @tc.name: EnableLight001
127  * @tc.desc: Enables the light available in the light list based on the specified light id.
128  * @tc.type: FUNC
129  */
130HWTEST_F(HdfLightTest, SUB_DriverSystem_LightHdi_0120, Function | MediumTest | Level1)
131{
132    int32_t i;
133    int32_t ret;
134    struct LightEffect effect;
135    effect.lightColor.colorValue.rgbColor.r = 255;
136    effect.lightColor.colorValue.rgbColor.g = 0;
137    effect.lightColor.colorValue.rgbColor.b = 0;
138    effect.flashEffect.flashMode = LIGHT_FLASH_NONE;
139    effect.flashEffect.onTime = 0;
140    effect.flashEffect.offTime = 0;
141
142    for (i = 0; i < g_count; ++i) {
143        ret = g_lightDev->TurnOnLight(g_lightInfo[i].lightId, &effect);
144        EXPECT_EQ(0, ret);
145
146        OsalSleep(LIGHT_WAIT_TIME);
147
148        ret = g_lightDev->TurnOffLight(g_lightInfo[i].lightId);
149        EXPECT_EQ(0, ret);
150    }
151}
152
153/**
154  * @tc.name: EnableLight002
155  * @tc.desc: Enables the light available in the light list based on the specified light id.
156  * @tc.type: FUNC
157  */
158HWTEST_F(HdfLightTest, SUB_DriverSystem_LightHdi_0130, Function | MediumTest | Level1)
159{
160    int32_t i;
161    int32_t ret;
162    struct LightEffect effect;
163    effect.lightColor.colorValue.rgbColor.r = 255;
164    effect.lightColor.colorValue.rgbColor.g = 0;
165    effect.lightColor.colorValue.rgbColor.b = 0;
166    effect.flashEffect.flashMode = LIGHT_FLASH_BLINK;
167    effect.flashEffect.onTime = g_onTime;
168    effect.flashEffect.offTime = g_offTime;
169
170    for (i = 0; i < g_count; ++i) {
171        ret = g_lightDev->TurnOnLight(g_lightInfo[i].lightId, &effect);
172        EXPECT_EQ(0, ret);
173
174        OsalSleep(LIGHT_WAIT_TIME);
175
176        ret = g_lightDev->TurnOffLight(g_lightInfo[i].lightId);
177        EXPECT_EQ(0, ret);
178    }
179}
180
181/**
182  * @tc.name: EnableLight003
183  * @tc.desc: Enables the light available in the light list based on the specified light id.
184  * @tc.type: FUNC
185  */
186HWTEST_F(HdfLightTest, SUB_DriverSystem_LightHdi_0140, Function | MediumTest | Level1)
187{
188    int32_t i;
189    int32_t ret;
190    uint32_t lightId = LIGHT_ID_BUTT;
191    struct LightEffect effect;
192
193    ret = g_lightDev->TurnOnLight(lightId, &effect);
194    EXPECT_EQ(LIGHT_NOT_SUPPORT, ret);
195
196    for (i = 0; i < g_count; ++i) {
197        effect.lightColor.colorValue.rgbColor.r = 255;
198        effect.lightColor.colorValue.rgbColor.g = 0;
199        effect.lightColor.colorValue.rgbColor.b = 0;
200        effect.flashEffect.flashMode = LIGHT_FLASH_BUTT;
201        effect.flashEffect.onTime = g_onTime;
202        effect.flashEffect.offTime = g_offTime;
203
204        ret = g_lightDev->TurnOnLight(g_lightInfo[i].lightId, &effect);
205        EXPECT_EQ(LIGHT_NOT_FLASH, ret);
206
207        effect.flashEffect.flashMode = LIGHT_FLASH_BLINK;
208        effect.flashEffect.onTime = 0;
209        ret = g_lightDev->TurnOnLight(g_lightInfo[i].lightId, &effect);
210        EXPECT_EQ(LIGHT_NOT_FLASH, ret);
211
212        effect.flashEffect.onTime = g_onTime;
213        effect.flashEffect.offTime = 0;
214        ret = g_lightDev->TurnOnLight(g_lightInfo[i].lightId, &effect);
215        EXPECT_EQ(LIGHT_NOT_FLASH, ret);
216    }
217}
218
219/**
220  * @tc.name: DisableLight001
221  * @tc.desc: Disable the light available in the light list based on the specified light id.
222  * @tc.type: FUNC
223  */
224HWTEST_F(HdfLightTest, SUB_DriverSystem_LightHdi_0150, Function | MediumTest | Level1)
225{
226    uint32_t lightId = LIGHT_ID_BUTT;
227
228    int32_t ret = g_lightDev->TurnOffLight(lightId);
229    EXPECT_EQ(HDF_FAILURE, ret);
230}
231