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 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
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 }
53 int32_t ret = g_lightDev->GetLightInfo(&g_lightInfo, &g_count);
54 if (ret == HDF_FAILURE) {
55 printf("get light informations failed\n\r");
56 }
57 }
58
TearDownTestCase()59 void HdfLightTest::TearDownTestCase()
60 {
61 if (g_lightDev != nullptr) {
62 FreeLightInterfaceInstance();
63 g_lightDev = nullptr;
64 }
65 }
66
SetUp()67 void HdfLightTest::SetUp()
68 {
69 }
70
TearDown()71 void HdfLightTest::TearDown()
72 {
73 }
74
75 /**
76 * @tc.name: SUB_DriverSystem_LightHdi_0010
77 * @tc.desc: Creat a light instance. The instance is not empty.
78 * @tc.type: FUNC
79 */
80 HWTEST_F(HdfLightTest, CheckLightInstanceIsEmpty, Function | MediumTest | Level1)
81 {
82 ASSERT_NE(nullptr, g_lightDev);
83 }
84
85 /**
86 * @tc.name: SUB_DriverSystem_LightHdi_0020
87 * @tc.desc: Obtains information about all lights in the system. Validity check of input parameters.
88 * @tc.type: FUNC
89 */
90 HWTEST_F(HdfLightTest, GetLightList001, 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: SUB_DriverSystem_LightHdi_0030
112 * @tc.desc: Obtains information about all lights in the system. Validity check of input parameters.
113 * @tc.type: FUNC
114 */
115 HWTEST_F(HdfLightTest, GetLightList002, 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: SUB_DriverSystem_LightHdi_0040
127 * @tc.desc: Enables the light available in the light list based on the specified light id.
128 * @tc.type: FUNC
129 */
130 HWTEST_F(HdfLightTest, EnableLight001, Function | MediumTest | Level1)
131 {
132 int32_t i;
133 int32_t ret;
134 struct LightEffect effect;
135 effect.lightBrightness = 0x80000000;
136 effect.flashEffect.flashMode = LIGHT_FLASH_NONE;
137 effect.flashEffect.onTime = 0;
138 effect.flashEffect.offTime = 0;
139
140 for (i = 0; i < g_count; ++i) {
141
142 ret = g_lightDev->TurnOnLight(g_lightInfo[i].lightId, &effect);
143 EXPECT_EQ(0, ret);
144
145 OsalSleep(LIGHT_WAIT_TIME);
146
147 ret = g_lightDev->TurnOffLight(g_lightInfo[i].lightId);
148 EXPECT_EQ(0, ret);
149 }
150 }
151
152 /**
153 * @tc.name: SUB_DriverSystem_LightHdi_0050
154 * @tc.desc: Enables the light available in the light list based on the specified light id.
155 * @tc.type: FUNC
156 */
157 HWTEST_F(HdfLightTest, EnableLight002, Function | MediumTest | Level1)
158 {
159 int32_t i;
160 int32_t ret;
161 struct LightEffect effect;
162 effect.lightBrightness = 0x80000000;
163 effect.flashEffect.flashMode = LIGHT_FLASH_TIMED;
164 effect.flashEffect.onTime = g_onTime;
165 effect.flashEffect.offTime = g_offTime;
166
167 for (i = 0; i < g_count; ++i) {
168 ret = g_lightDev->TurnOnLight(g_lightInfo[i].lightId, &effect);
169 EXPECT_EQ(0, ret);
170
171 OsalSleep(LIGHT_WAIT_TIME);
172
173 ret = g_lightDev->TurnOffLight(g_lightInfo[i].lightId);
174 EXPECT_EQ(0, ret);
175 }
176 }
177
178 /**
179 * @tc.name: SUB_DriverSystem_LightHdi_0060
180 * @tc.desc: Enables the light available in the light list based on the specified light id.
181 * @tc.type: FUNC
182 */
183 HWTEST_F(HdfLightTest, EnableLight003, Function | MediumTest | Level1)
184 {
185 int32_t i;
186 int32_t ret;
187 uint32_t lightId = LIGHT_ID_BUTT;
188 struct LightEffect effect;
189
190 ret = g_lightDev->TurnOnLight(lightId, &effect);
191 EXPECT_EQ(LIGHT_NOT_SUPPORT, ret);
192
193 for (i = 0; i < g_count; ++i) {
194 effect.lightBrightness = 0x80000000;
195 effect.flashEffect.flashMode = LIGHT_FLASH_BUTT;
196 effect.flashEffect.onTime = g_onTime;
197 effect.flashEffect.offTime = g_offTime;
198
199 ret = g_lightDev->TurnOnLight(g_lightInfo[i].lightId, &effect);
200 EXPECT_EQ(LIGHT_NOT_FLASH, ret);
201
202 effect.flashEffect.flashMode = LIGHT_FLASH_TIMED;
203 effect.flashEffect.onTime = 0;
204 ret = g_lightDev->TurnOnLight(g_lightInfo[i].lightId, &effect);
205 EXPECT_EQ(LIGHT_NOT_FLASH, ret);
206
207 effect.flashEffect.onTime = g_onTime;
208 effect.flashEffect.offTime = 0;
209 ret = g_lightDev->TurnOnLight(g_lightInfo[i].lightId, &effect);
210 EXPECT_EQ(LIGHT_NOT_FLASH, ret);
211 }
212 }
213
214 /**
215 * @tc.name: SUB_DriverSystem_LightHdi_0070
216 * @tc.desc: Disable the light available in the light list based on the specified light id.
217 * @tc.type: FUNC
218 */
219 HWTEST_F(HdfLightTest, DisableLight001, Function | MediumTest | Level1)
220 {
221 uint32_t lightId = LIGHT_ID_BUTT;
222
223 int32_t ret = g_lightDev->TurnOffLight(lightId);
224 EXPECT_EQ(HDF_FAILURE, ret);
225 }
226