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 <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 "v1_0/ilight_interface.h"
23 #include "light_type.h"
24 
25 using namespace OHOS::HDI::Light::V1_0;
26 using namespace testing::ext;
27 
28 namespace {
29     constexpr uint32_t g_sleepTime = 3;
30     constexpr int32_t MIN_VALUE = 0;
31     constexpr int32_t MAX_VALUE = 255;
32     std::vector<HdfLightInfo> g_info;
33     sptr<ILightInterface> g_lightInterface = nullptr;
34 }
35 
36 class HdfLightHdiServiceTest : public testing::Test {
37 public:
38     static void SetUpTestCase();
39     static void TearDownTestCase();
40     void SetUp();
41     void TearDown();
42 };
43 
SetUpTestCase()44 void HdfLightHdiServiceTest::SetUpTestCase()
45 {
46     g_lightInterface = ILightInterface::Get();
47 }
48 
TearDownTestCase()49 void HdfLightHdiServiceTest::TearDownTestCase()
50 {
51 }
52 
SetUp()53 void HdfLightHdiServiceTest::SetUp()
54 {
55 }
56 
TearDown()57 void HdfLightHdiServiceTest::TearDown()
58 {
59 }
60 
InitConfig(HdfLightEffect &effect)61 void InitConfig(HdfLightEffect &effect)
62 {
63     effect.lightColor.colorValue.rgbColor.r = MIN_VALUE;
64     effect.lightColor.colorValue.rgbColor.g = MIN_VALUE;
65     effect.lightColor.colorValue.rgbColor.b = MIN_VALUE;
66     effect.flashEffect.flashMode = HDF_LIGHT_FLASH_NONE;
67 }
68 
69 /**
70   * @tc.name: CheckLightInstanceIsEmpty
71   * @tc.desc: Create a light instance. The instance is not empty.
72   * @tc.type: FUNC
73   */
HWTEST_F(HdfLightHdiServiceTest, SUB_Driver_Sensor_HdiLight_2400, Function | MediumTest | Level1)74 HWTEST_F(HdfLightHdiServiceTest, SUB_Driver_Sensor_HdiLight_2400, Function | MediumTest | Level1)
75 {
76     ASSERT_NE(nullptr, g_lightInterface);
77 }
78 
79 /**
80   * @tc.name: GetLightInfo001
81   * @tc.desc: Get light info.
82   * @tc.type: FUNC
83   */
HWTEST_F(HdfLightHdiServiceTest, SUB_Driver_Sensor_HdiLight_2500, Function | MediumTest | Level1)84 HWTEST_F(HdfLightHdiServiceTest, SUB_Driver_Sensor_HdiLight_2500, Function | MediumTest | Level1)
85 {
86     ASSERT_NE(nullptr, g_lightInterface);
87 
88     int32_t ret = g_lightInterface->GetLightInfo(g_info);
89     EXPECT_EQ(HDF_SUCCESS, ret);
90     EXPECT_GT(g_info.size(), 0);
91     printf("get light list num[%zu]\n\r", g_info.size());
92 
93     for (auto iter : g_info) {
94         printf("lightId[%d], lightName[%s], lightNumber[%d]\n\r",\
95             iter.lightId, iter.lightName.c_str(), iter.lightNumber);
96     }
97 }
98 
99 /**
100   * @tc.name: TurnOnLightRed001
101   * @tc.desc: Turn on the light always on red.
102   * @tc.type: FUNC
103   */
HWTEST_F(HdfLightHdiServiceTest, SUB_Driver_Sensor_HdiLight_2600, Function | MediumTest | Level1)104 HWTEST_F(HdfLightHdiServiceTest, SUB_Driver_Sensor_HdiLight_2600, Function | MediumTest | Level1)
105 {
106     ASSERT_NE(nullptr, g_lightInterface);
107 
108     HdfLightEffect effect;
109     InitConfig(effect);
110     effect.lightColor.colorValue.rgbColor.r = MAX_VALUE;
111 
112     int32_t ret = g_lightInterface->TurnOnLight(g_info[0].lightId, effect);
113     EXPECT_EQ(HDF_SUCCESS, ret);
114 
115     OsalSleep(g_sleepTime);
116 
117     ret = g_lightInterface->TurnOffLight(g_info[0].lightId);
118     EXPECT_EQ(HDF_SUCCESS, ret);
119 }
120 
121 /**
122   * @tc.name: TurnOnLightGreen001
123   * @tc.desc: Turn on the light always on green.
124   * @tc.type: FUNC
125   */
HWTEST_F(HdfLightHdiServiceTest, SUB_Driver_Sensor_HdiLight_2700, Function | MediumTest | Level1)126 HWTEST_F(HdfLightHdiServiceTest, SUB_Driver_Sensor_HdiLight_2700, Function | MediumTest | Level1)
127 {
128     ASSERT_NE(nullptr, g_lightInterface);
129 
130     HdfLightEffect effect;
131     InitConfig(effect);
132     effect.lightColor.colorValue.rgbColor.g = MAX_VALUE;
133 
134     int32_t ret = g_lightInterface->TurnOnLight(g_info[0].lightId, effect);
135     EXPECT_EQ(HDF_SUCCESS, ret);
136 
137     OsalSleep(g_sleepTime);
138 
139     ret = g_lightInterface->TurnOffLight(g_info[0].lightId);
140     EXPECT_EQ(HDF_SUCCESS, ret);
141 }
142 /**
143   * @tc.name: TurnOnLightBlue001
144   * @tc.desc: Turn on the light always on blue.
145   * @tc.type: FUNC
146   */
HWTEST_F(HdfLightHdiServiceTest, SUB_Driver_Sensor_HdiLight_2800, Function | MediumTest | Level1)147 HWTEST_F(HdfLightHdiServiceTest, SUB_Driver_Sensor_HdiLight_2800, Function | MediumTest | Level1)
148 {
149     ASSERT_NE(nullptr, g_lightInterface);
150 
151     HdfLightEffect effect;
152     InitConfig(effect);
153     effect.lightColor.colorValue.rgbColor.b = MAX_VALUE;
154 
155     int32_t ret = g_lightInterface->TurnOnLight(g_info[0].lightId, effect);
156     EXPECT_EQ(HDF_SUCCESS, ret);
157 
158     OsalSleep(g_sleepTime);
159 
160     ret = g_lightInterface->TurnOffLight(g_info[0].lightId);
161     EXPECT_EQ(HDF_SUCCESS, ret);
162 }
163 
164 /**
165   * @tc.name: TurnOnLightAbnormal001
166   * @tc.desc: Abnormal flashmode.
167   * @tc.type: FUNC
168   */
HWTEST_F(HdfLightHdiServiceTest, SUB_Driver_Sensor_HdiLight_2900, Function | MediumTest | Level1)169 HWTEST_F(HdfLightHdiServiceTest, SUB_Driver_Sensor_HdiLight_2900, Function | MediumTest | Level1)
170 {
171     ASSERT_NE(nullptr, g_lightInterface);
172 
173     HdfLightEffect effect;
174     InitConfig(effect);
175     effect.lightColor.colorValue.rgbColor.r = MAX_VALUE;
176     effect.lightColor.colorValue.rgbColor.g = MAX_VALUE;
177     effect.lightColor.colorValue.rgbColor.b = MAX_VALUE;
178     effect.flashEffect.flashMode = -1;
179 
180     int32_t ret = g_lightInterface->TurnOnLight(g_info[0].lightId, effect);
181     EXPECT_NE(HDF_SUCCESS, ret);
182 
183     ret = g_lightInterface->TurnOffLight(g_info[0].lightId);
184     EXPECT_EQ(HDF_SUCCESS, ret);
185 }
186 
187 /**
188   * @tc.name: TurnOnLightAbnormal002
189   * @tc.desc: Abnormal flashmode.
190   * @tc.type: FUNC
191   */
HWTEST_F(HdfLightHdiServiceTest, SUB_Driver_Sensor_HdiLight_3000, Function | MediumTest | Level1)192 HWTEST_F(HdfLightHdiServiceTest, SUB_Driver_Sensor_HdiLight_3000, Function | MediumTest | Level1)
193 {
194     ASSERT_NE(nullptr, g_lightInterface);
195 
196     HdfLightEffect effect;
197     InitConfig(effect);
198     effect.lightColor.colorValue.rgbColor.r = MAX_VALUE;
199     effect.lightColor.colorValue.rgbColor.g = MAX_VALUE;
200     effect.lightColor.colorValue.rgbColor.b = MAX_VALUE;
201     effect.flashEffect.flashMode = HDF_LIGHT_FLASH_BUTT;
202 
203     int32_t ret = g_lightInterface->TurnOnLight(g_info[0].lightId, effect);
204     EXPECT_NE(HDF_SUCCESS, ret);
205 
206     ret = g_lightInterface->TurnOffLight(g_info[0].lightId);
207     EXPECT_EQ(HDF_SUCCESS, ret);
208 }
209 
210 /**
211   * @tc.name: TurnOnLightAbnormal003
212   * @tc.desc: Abnormal lightID.
213   * @tc.type: FUNC
214   */
HWTEST_F(HdfLightHdiServiceTest, SUB_Driver_Sensor_HdiLight_3100, Function | MediumTest | Level1)215 HWTEST_F(HdfLightHdiServiceTest, SUB_Driver_Sensor_HdiLight_3100, Function | MediumTest | Level1)
216 {
217     ASSERT_NE(nullptr, g_lightInterface);
218 
219     HdfLightEffect effect;
220     InitConfig(effect);
221     effect.lightColor.colorValue.rgbColor.r = MAX_VALUE;
222     effect.lightColor.colorValue.rgbColor.g = MAX_VALUE;
223     effect.lightColor.colorValue.rgbColor.b = MAX_VALUE;
224 
225     int32_t ret = g_lightInterface->TurnOnLight(-1, effect);
226     EXPECT_NE(HDF_SUCCESS, ret);
227 }
228 
229 /**
230   * @tc.name: TurnOnLightAbnormal004
231   * @tc.desc: Abnormal lightID.
232   * @tc.type: FUNC
233   */
HWTEST_F(HdfLightHdiServiceTest, SUB_Driver_Sensor_HdiLight_3200, Function | MediumTest | Level1)234 HWTEST_F(HdfLightHdiServiceTest, SUB_Driver_Sensor_HdiLight_3200, Function | MediumTest | Level1)
235 {
236     ASSERT_NE(nullptr, g_lightInterface);
237 
238     HdfLightEffect effect;
239     InitConfig(effect);
240     effect.lightColor.colorValue.rgbColor.r = MAX_VALUE;
241     effect.lightColor.colorValue.rgbColor.g = MAX_VALUE;
242     effect.lightColor.colorValue.rgbColor.b = MAX_VALUE;
243 
244     int32_t ret = g_lightInterface->TurnOnLight(LIGHT_ID_BUTT, effect);
245     EXPECT_NE(HDF_SUCCESS, ret);
246 }