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/light_interface_proxy.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 = 30;
30 constexpr int32_t g_minLightId = HDF_LIGHT_ID_BATTERY;
31 constexpr int32_t g_maxLightId = HDF_LIGHT_ID_ATTENTION;
32 constexpr int32_t g_onTime = 500;
33 constexpr int32_t g_offTime = 500;
34 sptr<ILightInterface> g_lightInterface = nullptr;
35 }
36
37 class HdfLightHdiTest : public testing::Test {
38 public:
39 static void SetUpTestCase();
40 static void TearDownTestCase();
41 void SetUp();
42 void TearDown();
43 };
44
SetUpTestCase()45 void HdfLightHdiTest::SetUpTestCase()
46 {
47 g_lightInterface = ILightInterface::Get();
48 }
49
TearDownTestCase()50 void HdfLightHdiTest::TearDownTestCase()
51 {
52 }
53
SetUp()54 void HdfLightHdiTest::SetUp()
55 {
56 }
57
TearDown()58 void HdfLightHdiTest::TearDown()
59 {
60 }
61
62 /**
63 * @tc.name: CheckLightInstanceIsEmpty
64 * @tc.desc: Creat a light instance. The instance is not empty.
65 * @tc.type: FUNC
66 * @tc.require: #I4NN4Z
67 */
68 HWTEST_F(HdfLightHdiTest, CheckLightInstanceIsEmpty, TestSize.Level1)
69 {
70 ASSERT_NE(nullptr, g_lightInterface);
71 }
72
73 /**
74 * @tc.name: GetLightInfo001
75 * @tc.desc: Get light info.
76 * @tc.type: FUNC
77 * @tc.require: #I4NN4Z
78 */
79 HWTEST_F(HdfLightHdiTest, GetLightInfo001, TestSize.Level1)
80 {
81 ASSERT_NE(nullptr, g_lightInterface);
82
83 std::vector<HdfLightInfo> info;
84 int32_t ret = g_lightInterface->GetLightInfo(info);
85 EXPECT_EQ(0, ret);
86 printf("get light list num[%zu]\n\r", info.size());
87
88 for (auto iter : info)
89 {
90 EXPECT_GE(iter.lightId, g_minLightId);
91 EXPECT_LE(iter.lightId, g_maxLightId);
92 }
93 }
94
95 /**
96 * @tc.name: TurnOnLight001
97 * @tc.desc: TurnOnLight.
98 * @tc.type: FUNC
99 * @tc.require: #I4NN4Z
100 */
101 HWTEST_F(HdfLightHdiTest, TurnOnLight001, TestSize.Level1)
102 {
103 ASSERT_NE(nullptr, g_lightInterface);
104
105 std::vector<HdfLightInfo> info;
106 int32_t ret = g_lightInterface->GetLightInfo(info);
107 EXPECT_EQ(HDF_SUCCESS, ret);
108 printf("get light list num[%zu]\n\r", info.size());
109
110 for (auto iter : info)
111 {
112 EXPECT_GE(iter.lightId, g_minLightId);
113 EXPECT_LE(iter.lightId, g_maxLightId);
114
115 HdfLightEffect effect;
116 effect.lightBrightness = 0xFFFF0000;
117 effect.flashEffect.flashMode = HDF_LIGHT_FLASH_NONE;
118 int32_t ret = g_lightInterface->TurnOnLight(iter.lightId, effect);
119 EXPECT_EQ(HDF_SUCCESS, ret);
120 OsalSleep(g_sleepTime);
121 ret = g_lightInterface->TurnOffLight(iter.lightId);
122 EXPECT_EQ(HDF_SUCCESS, ret);
123 }
124 }
125
126 /**
127 * @tc.name: TurnOnLight002
128 * @tc.desc: TurnOnLight.
129 * @tc.type: FUNC
130 * @tc.require: #I4NN4Z
131 */
132 HWTEST_F(HdfLightHdiTest, TurnOnLight002, TestSize.Level1)
133 {
134 ASSERT_NE(nullptr, g_lightInterface);
135
136 std::vector<HdfLightInfo> info;
137 int32_t ret = g_lightInterface->GetLightInfo(info);
138 EXPECT_EQ(0, ret);
139 printf("get light list num[%zu]\n\r", info.size());
140
141 for (auto iter : info)
142 {
143 EXPECT_GE(iter.lightId, g_minLightId);
144 EXPECT_LE(iter.lightId, g_maxLightId);
145
146 HdfLightEffect effect;
147 effect.lightBrightness = 0xFFFF0000;
148 effect.flashEffect.flashMode = HDF_LIGHT_FLASH_BUTT;
149 int32_t ret = g_lightInterface->TurnOnLight(iter.lightId, effect);
150 EXPECT_EQ(LIGHT_NOT_FLASH, ret);
151 }
152 }
153
154 /**
155 * @tc.name: TurnOnLight003
156 * @tc.desc: TurnOnLight.
157 * @tc.type: FUNC
158 * @tc.require: #I4NN4Z
159 */
160 HWTEST_F(HdfLightHdiTest, TurnOnLight003, TestSize.Level1)
161 {
162 ASSERT_NE(nullptr, g_lightInterface);
163
164 std::vector<HdfLightInfo> info;
165 int32_t ret = g_lightInterface->GetLightInfo(info);
166 EXPECT_EQ(0, ret);
167 printf("get light list num[%zu]\n\r", info.size());
168
169 for (auto iter : info)
170 {
171 EXPECT_GE(iter.lightId, g_minLightId);
172 EXPECT_LE(iter.lightId, g_maxLightId);
173
174 HdfLightEffect effect;
175 effect.lightBrightness = 0xFFFF0000;
176 effect.flashEffect.flashMode = HDF_LIGHT_FLASH_TIMED;
177 effect.flashEffect.onTime = g_onTime;
178 effect.flashEffect.offTime = g_offTime;
179 int32_t ret = g_lightInterface->TurnOnLight(iter.lightId, effect);
180 EXPECT_EQ(HDF_SUCCESS, ret);
181 OsalSleep(g_sleepTime);
182 ret = g_lightInterface->TurnOffLight(iter.lightId);
183 EXPECT_EQ(HDF_SUCCESS, ret);
184 }
185 }
186
187 /**
188 * @tc.name: DisableLight001
189 * @tc.desc: DisableLight.
190 * @tc.type: FUNC
191 * @tc.require: #I4NN4Z
192 */
193 HWTEST_F(HdfLightHdiTest, DisableLight001, TestSize.Level1)
194 {
195 ASSERT_NE(nullptr, g_lightInterface);
196
197 HdfLightEffect effect;
198 int32_t ret = g_lightInterface->TurnOnLight(HDF_LIGHT_ID_BUTT, effect);
199 EXPECT_EQ(LIGHT_NOT_SUPPORT, ret);
200 ret = g_lightInterface->TurnOffLight(HDF_LIGHT_ID_BUTT);
201 EXPECT_EQ(LIGHT_NOT_SUPPORT, ret);
202 }
203