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 "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 HdfLightHdiCommonTest : public testing::Test {
37 public:
38     static void SetUpTestCase();
39     static void TearDownTestCase();
40     void SetUp();
41     void TearDown();
42 };
43 
SetUpTestCase()44 void HdfLightHdiCommonTest::SetUpTestCase()
45 {
46     g_lightInterface = ILightInterface::Get();
47 }
48 
TearDownTestCase()49 void HdfLightHdiCommonTest::TearDownTestCase()
50 {
51 }
52 
SetUp()53 void HdfLightHdiCommonTest::SetUp()
54 {
55 }
56 
TearDown()57 void HdfLightHdiCommonTest::TearDown()
58 {
59 }
60 
InitConfig(HdfLightEffect & effect)61 static 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   * @tc.require: #I4NN4Z
74   */
75 HWTEST_F(HdfLightHdiCommonTest, CheckLightInstanceIsEmpty, TestSize.Level1)
76 {
77     ASSERT_NE(nullptr, g_lightInterface);
78 }
79 
80 /**
81   * @tc.name: GetLightInfo001
82   * @tc.desc: Get light info.
83   * @tc.type: FUNC
84   * @tc.require: #I4NN4Z
85   */
86 HWTEST_F(HdfLightHdiCommonTest, GetLightInfo_001, TestSize.Level1)
87 {
88     ASSERT_NE(nullptr, g_lightInterface);
89 
90     int32_t ret = g_lightInterface->GetLightInfo(g_info);
91     EXPECT_EQ(HDF_SUCCESS, ret);
92     EXPECT_GT(g_info.size(), 0);
93     printf("get light list num[%zu]\n\r", g_info.size());
94 
95     for (auto iter : g_info) {
96         printf("lightId[%d], lightName[%s], lightNumber[%d]\n\r",\
97             iter.lightId, iter.lightName.c_str(), iter.lightNumber);
98     }
99 }
100 
101 /**
102   * @tc.name: TurnOnLightRed001
103   * @tc.desc: Turn on the light always on red.
104   * @tc.type: FUNC
105   * @tc.require: #I4NN4Z
106   */
107 HWTEST_F(HdfLightHdiCommonTest, TurnOnLightRed_001, TestSize.Level1)
108 {
109     ASSERT_NE(nullptr, g_lightInterface);
110 
111     HdfLightEffect effect;
112     InitConfig(effect);
113     effect.lightColor.colorValue.rgbColor.r = MAX_VALUE;
114 
115     int32_t ret = g_lightInterface->TurnOnLight(g_info[0].lightId, effect);
116     EXPECT_EQ(HDF_SUCCESS, ret);
117 
118     OsalSleep(g_sleepTime);
119 
120     ret = g_lightInterface->TurnOffLight(g_info[0].lightId);
121     EXPECT_EQ(HDF_SUCCESS, ret);
122 }
123 
124 /**
125   * @tc.name: TurnOnLightGreen001
126   * @tc.desc: Turn on the light always on green.
127   * @tc.type: FUNC
128   * @tc.require: #I4NN4Z
129   */
130 HWTEST_F(HdfLightHdiCommonTest, TurnOnLightGreen_001, TestSize.Level1)
131 {
132     ASSERT_NE(nullptr, g_lightInterface);
133 
134     HdfLightEffect effect;
135     InitConfig(effect);
136     effect.lightColor.colorValue.rgbColor.g = MAX_VALUE;
137 
138     int32_t ret = g_lightInterface->TurnOnLight(g_info[0].lightId, effect);
139     EXPECT_EQ(HDF_SUCCESS, ret);
140 
141     OsalSleep(g_sleepTime);
142 
143     ret = g_lightInterface->TurnOffLight(g_info[0].lightId);
144     EXPECT_EQ(HDF_SUCCESS, ret);
145 }
146 /**
147   * @tc.name: TurnOnLightBlue001
148   * @tc.desc: Turn on the light always on blue.
149   * @tc.type: FUNC
150   * @tc.require: #I4NN4Z
151   */
152 HWTEST_F(HdfLightHdiCommonTest, TurnOnLightBlue_001, TestSize.Level1)
153 {
154     ASSERT_NE(nullptr, g_lightInterface);
155 
156     HdfLightEffect effect;
157     InitConfig(effect);
158     effect.lightColor.colorValue.rgbColor.b = MAX_VALUE;
159 
160     int32_t ret = g_lightInterface->TurnOnLight(g_info[0].lightId, effect);
161     EXPECT_EQ(HDF_SUCCESS, ret);
162 
163     OsalSleep(g_sleepTime);
164 
165     ret = g_lightInterface->TurnOffLight(g_info[0].lightId);
166     EXPECT_EQ(HDF_SUCCESS, ret);
167 }
168 
169 /**
170   * @tc.name: TurnOnLightAbnormal001
171   * @tc.desc: Abnormal flashmode.
172   * @tc.type: FUNC
173   * @tc.require: #I4NN4Z
174   */
175 HWTEST_F(HdfLightHdiCommonTest, TurnOnLightAbnormal_001, TestSize.Level1)
176 {
177     ASSERT_NE(nullptr, g_lightInterface);
178 
179     HdfLightEffect effect;
180     InitConfig(effect);
181     effect.lightColor.colorValue.rgbColor.r = MAX_VALUE;
182     effect.lightColor.colorValue.rgbColor.g = MAX_VALUE;
183     effect.lightColor.colorValue.rgbColor.b = MAX_VALUE;
184     effect.flashEffect.flashMode = -1;
185 
186     int32_t ret = g_lightInterface->TurnOnLight(g_info[0].lightId, effect);
187     EXPECT_NE(HDF_SUCCESS, ret);
188 
189     ret = g_lightInterface->TurnOffLight(g_info[0].lightId);
190     EXPECT_EQ(HDF_SUCCESS, ret);
191 }
192 
193 /**
194   * @tc.name: TurnOnLightAbnormal002
195   * @tc.desc: Abnormal flashmode.
196   * @tc.type: FUNC
197   * @tc.require: #I4NN4Z
198   */
199 HWTEST_F(HdfLightHdiCommonTest, TurnOnLightAbnormal_002, TestSize.Level1)
200 {
201     ASSERT_NE(nullptr, g_lightInterface);
202 
203     HdfLightEffect effect;
204     InitConfig(effect);
205     effect.lightColor.colorValue.rgbColor.r = MAX_VALUE;
206     effect.lightColor.colorValue.rgbColor.g = MAX_VALUE;
207     effect.lightColor.colorValue.rgbColor.b = MAX_VALUE;
208     effect.flashEffect.flashMode = HDF_LIGHT_FLASH_BUTT;
209 
210     int32_t ret = g_lightInterface->TurnOnLight(g_info[0].lightId, effect);
211     EXPECT_NE(HDF_SUCCESS, ret);
212 
213     ret = g_lightInterface->TurnOffLight(g_info[0].lightId);
214     EXPECT_EQ(HDF_SUCCESS, ret);
215 }
216 
217 /**
218   * @tc.name: TurnOnLightAbnormal003
219   * @tc.desc: Abnormal lightID.
220   * @tc.type: FUNC
221   * @tc.require: #I4NN4Z
222   */
223 HWTEST_F(HdfLightHdiCommonTest, TurnOnLightAbnormal_003, TestSize.Level1)
224 {
225     ASSERT_NE(nullptr, g_lightInterface);
226 
227     HdfLightEffect effect;
228     InitConfig(effect);
229     effect.lightColor.colorValue.rgbColor.r = MAX_VALUE;
230     effect.lightColor.colorValue.rgbColor.g = MAX_VALUE;
231     effect.lightColor.colorValue.rgbColor.b = MAX_VALUE;
232 
233     int32_t ret = g_lightInterface->TurnOnLight(-1, effect);
234     EXPECT_NE(HDF_SUCCESS, ret);
235 }
236 
237 /**
238   * @tc.name: TurnOnLightAbnormal004
239   * @tc.desc: Abnormal lightID.
240   * @tc.type: FUNC
241   * @tc.require: #I4NN4Z
242   */
243 HWTEST_F(HdfLightHdiCommonTest, TurnOnLightAbnormal_004, TestSize.Level1)
244 {
245     ASSERT_NE(nullptr, g_lightInterface);
246 
247     HdfLightEffect effect;
248     InitConfig(effect);
249     effect.lightColor.colorValue.rgbColor.r = MAX_VALUE;
250     effect.lightColor.colorValue.rgbColor.g = MAX_VALUE;
251     effect.lightColor.colorValue.rgbColor.b = MAX_VALUE;
252 
253     int32_t ret = g_lightInterface->TurnOnLight(LIGHT_ID_BUTT, effect);
254     EXPECT_NE(HDF_SUCCESS, ret);
255 }