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