1 /*
2  * Copyright (c) 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 
26 using namespace testing::ext;
27 
28 namespace {
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 ON_TIME = 500;
33     const int32_t OFF_TIME = 500;
34     const int32_t LIGHT_WAIT_TIME = 2;
35     const int32_t MIN_LIGHT_ID = LIGHT_ID_NONE;
36     const int32_t MAX_LIGHT_ID = LIGHT_ID_BUTT;
37 }
38 
39 class HdfLightTest : public testing::Test {
40 public:
41     static void SetUpTestCase();
42     static void TearDownTestCase();
43     void SetUp();
44     void TearDown();
45 };
46 
SetUpTestCase()47 void HdfLightTest::SetUpTestCase()
48 {
49     g_lightDev = NewLightInterfaceInstance();
50     if (g_lightDev == nullptr) {
51         printf("test light get Module instance failed\n\r");
52         return;
53     }
54     int32_t ret = g_lightDev->GetLightInfo(&g_lightInfo, &g_count);
55     if (ret == HDF_FAILURE) {
56         printf("get light information failed\n\r");
57     }
58 }
59 
TearDownTestCase()60 void HdfLightTest::TearDownTestCase()
61 {
62     if (g_lightDev != nullptr) {
63         FreeLightInterfaceInstance();
64         g_lightDev = nullptr;
65     }
66 }
67 
SetUp()68 void HdfLightTest::SetUp()
69 {
70 }
71 
TearDown()72 void HdfLightTest::TearDown()
73 {
74 }
75 
76 /**
77   * @tc.name: CheckLightInstanceIsEmpty
78   * @tc.desc: Create a light instance. The instance is not empty.
79   * @tc.type: FUNC
80   * @tc.require: #I4NN4Z
81   */
82 HWTEST_F(HdfLightTest, CheckLightInstanceIsEmpty, TestSize.Level1)
83 {
84     ASSERT_NE(nullptr, g_lightDev);
85 }
86 
87 /**
88   * @tc.name: GetLightList001
89   * @tc.desc: Obtains information about all lights in the system. Validity check of input parameters.
90   * @tc.type: FUNC
91   * @tc.require: SR000F869M, AR000F869Q
92   */
93 HWTEST_F(HdfLightTest, GetLightList001, TestSize.Level1)
94 {
95     struct LightInfo *info = nullptr;
96 
97     if (g_lightInfo == nullptr) {
98         EXPECT_NE(nullptr, g_lightInfo);
99         return;
100     }
101 
102     printf("get light list num[%u]\n\r", g_count);
103     info = g_lightInfo;
104 
105     for (uint32_t i = 0; i < g_count; ++i) {
106         printf("get lightId[%u]\n\r", info->lightId);
107         EXPECT_GE(info->lightId, MIN_LIGHT_ID);
108         EXPECT_LE(info->lightId, MAX_LIGHT_ID);
109         info++;
110     }
111 }
112 
113 /**
114   * @tc.name: GetLightList002
115   * @tc.desc: Obtains information about all lights in the system. Validity check of input parameters.
116   * @tc.type: FUNC
117   * @tc.require: SR000F869M, AR000F869Q
118   */
119 HWTEST_F(HdfLightTest, GetLightList002, TestSize.Level1)
120 {
121     int32_t ret = g_lightDev->GetLightInfo(nullptr, &g_count);
122     EXPECT_EQ(HDF_FAILURE, ret);
123     ret = g_lightDev->GetLightInfo(&g_lightInfo, nullptr);
124     EXPECT_EQ(HDF_FAILURE, ret);
125     ret = g_lightDev->GetLightInfo(nullptr, nullptr);
126     EXPECT_EQ(HDF_FAILURE, ret);
127 }
128 
129 /**
130   * @tc.name: EnableLight001
131   * @tc.desc: Enables the light available in the light list based on the specified light id.
132   * @tc.type: FUNC
133   * @tc.require: SR000F869M, AR000F869R, AR000F8QNL
134   */
135 HWTEST_F(HdfLightTest, EnableLight001, TestSize.Level1)
136 {
137     uint32_t i;
138     struct LightEffect effect;
139     effect.flashEffect.flashMode = LIGHT_FLASH_NONE;
140     effect.flashEffect.onTime = 0;
141     effect.flashEffect.offTime = 0;
142 
143     for (i = 0; i < g_count; ++i) {
144         effect.lightColor.colorValue.rgbColor.r = 255;
145         effect.lightColor.colorValue.rgbColor.g = 0;
146         effect.lightColor.colorValue.rgbColor.b = 0;
147         int32_t ret = g_lightDev->TurnOnLight(g_lightInfo[i].lightId, &effect);
148         EXPECT_EQ(0, ret);
149 
150         OsalSleep(LIGHT_WAIT_TIME);
151 
152         ret = g_lightDev->TurnOffLight(g_lightInfo[i].lightId);
153         EXPECT_EQ(0, ret);
154 
155         effect.lightColor.colorValue.rgbColor.r = 0;
156         effect.lightColor.colorValue.rgbColor.g = 255;
157         effect.lightColor.colorValue.rgbColor.b = 0;
158         ret = g_lightDev->TurnOnLight(g_lightInfo[i].lightId, &effect);
159         EXPECT_EQ(0, ret);
160 
161         OsalSleep(LIGHT_WAIT_TIME);
162 
163         ret = g_lightDev->TurnOffLight(g_lightInfo[i].lightId);
164         EXPECT_EQ(0, ret);
165     }
166 }
167 
168 /**
169   * @tc.name: EnableLight002
170   * @tc.desc: Enables the light available in the light list based on the specified light id.
171   * @tc.type: FUNC
172   * @tc.require: SR000F869M, AR000F869R, AR000F8QNL
173   */
174 HWTEST_F(HdfLightTest, EnableLight002, TestSize.Level1)
175 {
176     uint32_t i;
177     struct LightEffect effect;
178     effect.flashEffect.flashMode = LIGHT_FLASH_BLINK;
179     effect.flashEffect.onTime = ON_TIME;
180     effect.flashEffect.offTime = OFF_TIME;
181 
182     for (i = 0; i < g_count; ++i) {
183         effect.lightColor.colorValue.rgbColor.r = 255;
184         effect.lightColor.colorValue.rgbColor.g = 0;
185         effect.lightColor.colorValue.rgbColor.b = 0;
186         int32_t ret = g_lightDev->TurnOnLight(g_lightInfo[i].lightId, &effect);
187         EXPECT_EQ(0, ret);
188 
189         OsalSleep(LIGHT_WAIT_TIME);
190 
191         ret = g_lightDev->TurnOffLight(g_lightInfo[i].lightId);
192         EXPECT_EQ(0, ret);
193 
194         effect.lightColor.colorValue.rgbColor.r = 0;
195         effect.lightColor.colorValue.rgbColor.g = 255;
196         effect.lightColor.colorValue.rgbColor.b = 0;
197         ret = g_lightDev->TurnOnLight(g_lightInfo[i].lightId, &effect);
198         EXPECT_EQ(0, ret);
199 
200         OsalSleep(LIGHT_WAIT_TIME);
201 
202         ret = g_lightDev->TurnOffLight(g_lightInfo[i].lightId);
203         EXPECT_EQ(0, ret);
204 
205         effect.lightColor.colorValue.rgbColor.r = 255;
206         effect.lightColor.colorValue.rgbColor.g = 255;
207         effect.lightColor.colorValue.rgbColor.b = 0;
208         ret = g_lightDev->TurnOnLight(g_lightInfo[i].lightId, &effect);
209         EXPECT_EQ(0, ret);
210 
211         OsalSleep(LIGHT_WAIT_TIME);
212 
213         ret = g_lightDev->TurnOffLight(g_lightInfo[i].lightId);
214         EXPECT_EQ(0, ret);
215     }
216 }
217 
218 /**
219   * @tc.name: EnableLight003
220   * @tc.desc: Enables the light available in the light list based on the specified light id.
221   * @tc.type: FUNC
222   * @tc.require: SR000F869M, AR000F869R, AR000F8QNL
223   */
224 HWTEST_F(HdfLightTest, EnableLight003, TestSize.Level1)
225 {
226     uint32_t i;
227     int32_t ret;
228     uint32_t lightId = LIGHT_ID_BUTT;
229     struct LightEffect effect;
230 
231     ret = g_lightDev->TurnOnLight(lightId, &effect);
232     EXPECT_EQ(LIGHT_NOT_SUPPORT, ret);
233 
234     for (i = 0; i < g_count; ++i) {
235         effect.lightColor.colorValue.rgbColor.r = 255;
236         effect.lightColor.colorValue.rgbColor.g = 0;
237         effect.lightColor.colorValue.rgbColor.b = 0;
238         effect.flashEffect.flashMode = LIGHT_FLASH_BUTT;
239         effect.flashEffect.onTime = ON_TIME;
240         effect.flashEffect.offTime = OFF_TIME;
241 
242         ret = g_lightDev->TurnOnLight(g_lightInfo[i].lightId, &effect);
243         EXPECT_EQ(LIGHT_NOT_FLASH, ret);
244 
245         effect.flashEffect.flashMode = LIGHT_FLASH_GRADIENT;
246         effect.flashEffect.onTime = 0;
247         ret = g_lightDev->TurnOnLight(g_lightInfo[i].lightId, &effect);
248         EXPECT_EQ(LIGHT_NOT_FLASH, ret);
249 
250         effect.flashEffect.onTime = ON_TIME;
251         effect.flashEffect.offTime = 0;
252         ret = g_lightDev->TurnOnLight(g_lightInfo[i].lightId, &effect);
253         EXPECT_EQ(LIGHT_NOT_FLASH, ret);
254     }
255 }
256 
257 /**
258   * @tc.name: DisableLight001
259   * @tc.desc: Disable the light available in the light list based on the specified light id.
260   * @tc.type: FUNC
261   * @tc.require: SR000F869M, AR000F869R, AR000F8QNL
262   */
263 HWTEST_F(HdfLightTest, DisableLight001, TestSize.Level1)
264 {
265     uint32_t lightId = LIGHT_ID_BUTT;
266 
267     int32_t ret = g_lightDev->TurnOffLight(lightId);
268     EXPECT_EQ(HDF_FAILURE, ret);
269 }
270