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 = 30;
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 return;
53 }
54 int32_t ret = g_lightDev->GetLightInfo(&g_lightInfo, &g_count);
55 if (ret == HDF_FAILURE) {
56 printf("get light informations 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: Creat 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[%d]\n\r", g_count);
103 info = g_lightInfo;
104
105 for (int i = 0; i < g_count; ++i) {
106 printf("get lightId[%d]\n\r", info->lightId);
107 EXPECT_GE(info->lightId, g_minLightId);
108 EXPECT_LE(info->lightId, g_maxLightId);
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 int32_t i;
138 int32_t ret;
139 struct LightEffect effect;
140 effect.lightBrightness = 0x80000000;
141 effect.flashEffect.flashMode = LIGHT_FLASH_NONE;
142 effect.flashEffect.onTime = 0;
143 effect.flashEffect.offTime = 0;
144
145 for (i = 0; i < g_count; ++i) {
146
147 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 }
156
157 /**
158 * @tc.name: EnableLight002
159 * @tc.desc: Enables the light available in the light list based on the specified light id.
160 * @tc.type: FUNC
161 * @tc.require: SR000F869M, AR000F869R, AR000F8QNL
162 */
163 HWTEST_F(HdfLightTest, EnableLight002, TestSize.Level1)
164 {
165 int32_t i;
166 int32_t ret;
167 struct LightEffect effect;
168 effect.lightBrightness = 0x80000000;
169 effect.flashEffect.flashMode = LIGHT_FLASH_TIMED;
170 effect.flashEffect.onTime = g_onTime;
171 effect.flashEffect.offTime = g_offTime;
172
173 for (i = 0; i < g_count; ++i) {
174 ret = g_lightDev->TurnOnLight(g_lightInfo[i].lightId, &effect);
175 EXPECT_EQ(0, ret);
176
177 OsalSleep(LIGHT_WAIT_TIME);
178
179 ret = g_lightDev->TurnOffLight(g_lightInfo[i].lightId);
180 EXPECT_EQ(0, ret);
181 }
182 }
183
184 /**
185 * @tc.name: EnableLight003
186 * @tc.desc: Enables the light available in the light list based on the specified light id.
187 * @tc.type: FUNC
188 * @tc.require: SR000F869M, AR000F869R, AR000F8QNL
189 */
190 HWTEST_F(HdfLightTest, EnableLight003, TestSize.Level1)
191 {
192 int32_t i;
193 int32_t ret;
194 uint32_t lightId = LIGHT_ID_BUTT;
195 struct LightEffect effect;
196
197 ret = g_lightDev->TurnOnLight(lightId, &effect);
198 EXPECT_EQ(LIGHT_NOT_SUPPORT, ret);
199
200 for (i = 0; i < g_count; ++i) {
201 effect.lightBrightness = 0x80000000;
202 effect.flashEffect.flashMode = LIGHT_FLASH_BUTT;
203 effect.flashEffect.onTime = g_onTime;
204 effect.flashEffect.offTime = g_offTime;
205
206 ret = g_lightDev->TurnOnLight(g_lightInfo[i].lightId, &effect);
207 EXPECT_EQ(LIGHT_NOT_FLASH, ret);
208
209 effect.flashEffect.flashMode = LIGHT_FLASH_TIMED;
210 effect.flashEffect.onTime = 0;
211 ret = g_lightDev->TurnOnLight(g_lightInfo[i].lightId, &effect);
212 EXPECT_EQ(LIGHT_NOT_FLASH, ret);
213
214 effect.flashEffect.onTime = g_onTime;
215 effect.flashEffect.offTime = 0;
216 ret = g_lightDev->TurnOnLight(g_lightInfo[i].lightId, &effect);
217 EXPECT_EQ(LIGHT_NOT_FLASH, ret);
218 }
219 }
220
221 /**
222 * @tc.name: DisableLight001
223 * @tc.desc: Disable the light available in the light list based on the specified light id.
224 * @tc.type: FUNC
225 * @tc.require: SR000F869M, AR000F869R, AR000F8QNL
226 */
227 HWTEST_F(HdfLightTest, DisableLight001, TestSize.Level1)
228 {
229 uint32_t lightId = LIGHT_ID_BUTT;
230
231 int32_t ret = g_lightDev->TurnOffLight(lightId);
232 EXPECT_EQ(HDF_FAILURE, ret);
233 }
234