• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2025 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 <gtest/gtest.h>
17 #include <memory>
18 
19 #include "proximity_controller_base.h"
20 #include "power_log.h"
21 
22 using namespace testing::ext;
23 using namespace OHOS::PowerMgr;
24 using namespace OHOS;
25 using namespace std;
26 
27 namespace {
28 SensorInfo* g_sensorInfo = nullptr;
29 int32_t g_count = 0;
30 int32_t g_intReturnValue = 0;
31 int32_t g_intReturnValue1 = 0;
32 } // namespace
33 
GetAllSensors(SensorInfo ** sensorInfo,int32_t * count)34 int32_t GetAllSensors(SensorInfo **sensorInfo, int32_t *count)
35 {
36     *sensorInfo = g_sensorInfo;
37     *count = g_count;
38     return g_intReturnValue;
39 }
40 
SubscribeSensor(int32_t,const SensorUser *)41 int32_t SubscribeSensor(int32_t /* sensorId */, const SensorUser* /* user */)
42 {
43     return g_intReturnValue;
44 }
45 
ActivateSensor(int32_t,const SensorUser *)46 int32_t ActivateSensor(int32_t /* sensorId */, const SensorUser* /* user */)
47 {
48     return g_intReturnValue1;
49 }
50 
51 namespace OHOS {
52 namespace PowerMgr {
53 class ProximityControllerBaseTest : public testing::Test {
54 public:
SetUpTestCase()55     static void SetUpTestCase() {}
TearDownTestCase()56     static void TearDownTestCase() {}
57     void SetUp();
58     void TearDown();
59 };
SetUp()60 void ProximityControllerBaseTest::SetUp()
61 {
62     g_sensorInfo = nullptr;
63     g_count = 0;
64     g_intReturnValue = 0;
65     g_intReturnValue1 = 0;
66 }
67 
TearDown()68 void ProximityControllerBaseTest::TearDown()
69 {
70 }
71 } // namespace PowerMgr
72 } // namespace OHOS
73 
74 namespace {
75 /**
76  * @tc.name: ProximityControllerBaseTest001
77  * @tc.desc: Test proximity controller base abnormal branch
78  * @tc.type: FUNC
79  * @tc.require: ICGV1M
80  */
81 HWTEST_F(ProximityControllerBaseTest, ProximityControllerBaseTest001, TestSize.Level1)
82 {
83     POWER_HILOGI(LABEL_TEST, "ProximityControllerBaseTest001 function start!");
84     std::shared_ptr<ProximityControllerBase> proximityControllerBase =
85         std::make_shared<ProximityControllerBase>("ProximityControllerBaseTest", nullptr);
86     EXPECT_FALSE(proximityControllerBase->IsSupported());
87     g_intReturnValue = 1;
88     proximityControllerBase = std::make_shared<ProximityControllerBase>("ProximityControllerBaseTest", nullptr);
89     EXPECT_FALSE(proximityControllerBase->IsSupported());
90     POWER_HILOGI(LABEL_TEST, "ProximityControllerBaseTest001 function end!");
91 }
92 
93 /**
94  * @tc.name: ProximityControllerBaseTest002
95  * @tc.desc: Test proximity controller base abnormal branch
96  * @tc.type: FUNC
97  * @tc.require: ICGV1M
98  */
99 HWTEST_F(ProximityControllerBaseTest, ProximityControllerBaseTest002, TestSize.Level1)
100 {
101     POWER_HILOGI(LABEL_TEST, "ProximityControllerBaseTest002 function start!");
102     SensorInfo info;
103     g_sensorInfo = &info;
104     std::shared_ptr<ProximityControllerBase> proximityControllerBase =
105         std::make_shared<ProximityControllerBase>("ProximityControllerBaseTest", nullptr);
106     EXPECT_FALSE(proximityControllerBase->IsSupported());
107     POWER_HILOGI(LABEL_TEST, "ProximityControllerBaseTest002 function end!");
108 }
109 
110 /**
111  * @tc.name: ProximityControllerBaseTest003
112  * @tc.desc: Test proximity controller base Disable abnormal branch
113  * @tc.type: FUNC
114  * @tc.require: ICGV1M
115  */
116 HWTEST_F(ProximityControllerBaseTest, ProximityControllerBaseTest003, TestSize.Level1)
117 {
118     POWER_HILOGI(LABEL_TEST, "ProximityControllerBaseTest003 function start!");
119     SensorInfo info;
120     g_sensorInfo = &info;
121     std::shared_ptr<ProximityControllerBase> proximityControllerBase =
122         std::make_shared<ProximityControllerBase>("ProximityControllerBaseTest", nullptr);
123     proximityControllerBase->Disable();
124     EXPECT_FALSE(proximityControllerBase->IsEnabled());
125     POWER_HILOGI(LABEL_TEST, "ProximityControllerBaseTest003 function end!");
126 }
127 
128 /**
129  * @tc.name: ProximityControllerBaseTest004
130  * @tc.desc: Test proximity controller base Enable abnormal branch
131  * @tc.type: FUNC
132  * @tc.require: ICGV1M
133  */
134 HWTEST_F(ProximityControllerBaseTest, ProximityControllerBaseTest004, TestSize.Level1)
135 {
136     POWER_HILOGI(LABEL_TEST, "ProximityControllerBaseTest004 function start!");
137     SensorInfo info;
138     g_sensorInfo = &info;
139     std::shared_ptr<ProximityControllerBase> proximityControllerBase =
140         std::make_shared<ProximityControllerBase>("ProximityControllerBaseTest", nullptr);
141     proximityControllerBase->Enable();
142     EXPECT_TRUE(proximityControllerBase->IsEnabled());
143     POWER_HILOGI(LABEL_TEST, "ProximityControllerBaseTest004 function end!");
144 }
145 
146 /**
147  * @tc.name: ProximityControllerBaseTest005
148  * @tc.desc: Test proximity controller base Enable abnormal branch
149  * @tc.type: FUNC
150  * @tc.require: ICGV1M
151  */
152 HWTEST_F(ProximityControllerBaseTest, ProximityControllerBaseTest005, TestSize.Level1)
153 {
154     POWER_HILOGI(LABEL_TEST, "ProximityControllerBaseTest005 function start!");
155     SensorInfo infos[] {{.sensorTypeId = SENSOR_TYPE_ID_PROXIMITY}};
156     g_sensorInfo = infos;
157     g_count = 1;
158     std::shared_ptr<ProximityControllerBase> proximityControllerBase =
159         std::make_shared<ProximityControllerBase>("ProximityControllerBaseTest", nullptr);
160     EXPECT_TRUE(proximityControllerBase->IsSupported());
161 
162     g_intReturnValue = 1;
163     proximityControllerBase->Enable();
164     EXPECT_TRUE(proximityControllerBase->IsEnabled());
165 
166     g_intReturnValue = 0;
167     g_intReturnValue1 = 1;
168     proximityControllerBase->Enable();
169     EXPECT_TRUE(proximityControllerBase->IsEnabled());
170     POWER_HILOGI(LABEL_TEST, "ProximityControllerBaseTest005 function end!");
171 }
172 
173 /**
174  * @tc.name: ProximityControllerBaseTest006
175  * @tc.desc: Test IProximityController SetStatus
176  * @tc.type: FUNC
177  * @tc.require: ICGV1M
178  */
179 HWTEST_F(ProximityControllerBaseTest, ProximityControllerBaseTest006, TestSize.Level1)
180 {
181     POWER_HILOGI(LABEL_TEST, "ProximityControllerBaseTest006 function start!");
182     std::shared_ptr<ProximityControllerBase> proximityControllerBase =
183         std::make_shared<ProximityControllerBase>("ProximityControllerBaseTest", nullptr);
184     EXPECT_EQ(proximityControllerBase->GetStatus(), 0);
185     proximityControllerBase->SetStatus(1);
186     EXPECT_EQ(proximityControllerBase->GetStatus(), 1);
187     POWER_HILOGI(LABEL_TEST, "ProximityControllerBaseTest006 function end!");
188 }
189 
190 /**
191  * @tc.name: ProximityControllerBaseTest007
192  * @tc.desc: Test proximity controller base InitProximitySensorUser normal branch
193  * @tc.type: FUNC
194  * @tc.require: ICGV1M
195  */
196 HWTEST_F(ProximityControllerBaseTest, ProximityControllerBaseTest007, TestSize.Level1)
197 {
198     POWER_HILOGI(LABEL_TEST, "ProximityControllerBaseTest007 function start!");
199     SensorInfo infos[] {{.sensorTypeId = SENSOR_TYPE_ID_PROXIMITY}};
200     g_sensorInfo = infos;
201     g_count = 1;
202     std::shared_ptr<ProximityControllerBase> proximityControllerBase =
203         std::make_shared<ProximityControllerBase>("ProximityControllerBaseTest", nullptr);
204     proximityControllerBase->Enable();
205     proximityControllerBase->InitProximitySensorUser();
206     EXPECT_TRUE(proximityControllerBase->IsSupported());
207     POWER_HILOGI(LABEL_TEST, "ProximityControllerBaseTest007 function end!");
208 }
209 
210 /**
211  * @tc.name: ProximityControllerBaseTest008
212  * @tc.desc: Test proximity controller base InitProximitySensorUser abnormal branch
213  * @tc.type: FUNC
214  * @tc.require: ICGV1M
215  */
216 HWTEST_F(ProximityControllerBaseTest, ProximityControllerBaseTest008, TestSize.Level1)
217 {
218     POWER_HILOGI(LABEL_TEST, "ProximityControllerBaseTest008 function start!");
219     SensorInfo infos[] {{.sensorTypeId = SENSOR_TYPE_ID_GESTURE}};
220     g_sensorInfo = infos;
221     g_count = 1;
222     std::shared_ptr<ProximityControllerBase> proximityControllerBase =
223         std::make_shared<ProximityControllerBase>("ProximityControllerBaseTest", nullptr);
224     proximityControllerBase->Enable();
225     proximityControllerBase->InitProximitySensorUser();
226     EXPECT_FALSE(proximityControllerBase->IsSupported());
227     POWER_HILOGI(LABEL_TEST, "ProximityControllerBaseTest008 function end!");
228 }
229 
230 /**
231  * @tc.name: ProximityControllerBaseTest009
232  * @tc.desc: Test proximity controller base InitProximitySensorUser abnormal branch
233  * @tc.type: FUNC
234  * @tc.require: ICGV1M
235  */
236 HWTEST_F(ProximityControllerBaseTest, ProximityControllerBaseTest009, TestSize.Level1)
237 {
238     POWER_HILOGI(LABEL_TEST, "ProximityControllerBaseTest009 function start!");
239     SensorInfo infos[] {{.sensorTypeId = SENSOR_TYPE_ID_GESTURE}};
240     g_sensorInfo = nullptr;
241     g_count = 1;
242     std::shared_ptr<ProximityControllerBase> proximityControllerBase =
243         std::make_shared<ProximityControllerBase>("ProximityControllerBaseTest", nullptr);
244     proximityControllerBase->Enable();
245     proximityControllerBase->InitProximitySensorUser();
246     EXPECT_FALSE(proximityControllerBase->IsSupported());
247     POWER_HILOGI(LABEL_TEST, "ProximityControllerBaseTest009 function end!");
248 }
249 
250 /**
251  * @tc.name: ProximityControllerBaseTest010
252  * @tc.desc: Test proximity controller base InitProximitySensorUser abnormal branch
253  * @tc.type: FUNC
254  * @tc.require: ICGV1M
255  */
256 HWTEST_F(ProximityControllerBaseTest, ProximityControllerBaseTest010, TestSize.Level1)
257 {
258     POWER_HILOGI(LABEL_TEST, "ProximityControllerBaseTest010 function start!");
259     SensorInfo infos[] {{.sensorTypeId = SENSOR_TYPE_ID_PROXIMITY}};
260     g_sensorInfo = infos;
261     g_count = 1;
262     std::shared_ptr<ProximityControllerBase> proximityControllerBase = std::make_shared<ProximityControllerBase>(
263         "This is a very long string, its length is much longer than sixteen", nullptr);
264     proximityControllerBase->Enable();
265     proximityControllerBase->InitProximitySensorUser();
266     EXPECT_TRUE(proximityControllerBase->IsSupported());
267     POWER_HILOGI(LABEL_TEST, "ProximityControllerBaseTest010 function end!");
268 }
269 
270 /**
271  * @tc.name: ProximityControllerBaseTest011
272  * @tc.desc: Test proximity controller base InitProximitySensorUser abnormal branch
273  * @tc.type: FUNC
274  * @tc.require: ICGV1M
275  */
276 HWTEST_F(ProximityControllerBaseTest, ProximityControllerBaseTest011, TestSize.Level1)
277 {
278     POWER_HILOGI(LABEL_TEST, "ProximityControllerBaseTest011 function start!");
279     SensorInfo infos[] {{.sensorTypeId = SENSOR_TYPE_ID_PROXIMITY}};
280     g_sensorInfo = infos;
281     g_count = 1;
282     std::shared_ptr<ProximityControllerBase> proximityControllerBase =
283         std::make_shared<ProximityControllerBase>("Test", nullptr);
284     proximityControllerBase->Enable();
285     proximityControllerBase->InitProximitySensorUser();
286     EXPECT_TRUE(proximityControllerBase->IsSupported());
287     POWER_HILOGI(LABEL_TEST, "ProximityControllerBaseTest011 function end!");
288 }
289 
290 /**
291  * @tc.name: ProximityControllerBaseTest012
292  * @tc.desc: Test proximity controller base InitProximitySensorUser abnormal branch
293  * @tc.type: FUNC
294  * @tc.require: ICGV1M
295  */
296 HWTEST_F(ProximityControllerBaseTest, ProximityControllerBaseTest012, TestSize.Level1)
297 {
298     POWER_HILOGI(LABEL_TEST, "ProximityControllerBaseTest012 function start!");
299     SensorInfo infos[] {{.sensorTypeId = SENSOR_TYPE_ID_PROXIMITY}};
300     g_sensorInfo = nullptr;
301     g_count = 1;
302     g_intReturnValue = 1;
303     std::shared_ptr<ProximityControllerBase> proximityControllerBase =
304         std::make_shared<ProximityControllerBase>("ProximityControllerBaseTest", nullptr);
305     proximityControllerBase->Enable();
306     proximityControllerBase->InitProximitySensorUser();
307     EXPECT_FALSE(proximityControllerBase->IsSupported());
308     POWER_HILOGI(LABEL_TEST, "ProximityControllerBaseTest012 function end!");
309 }
310 
311 /**
312  * @tc.name: ProximityControllerBaseTest013
313  * @tc.desc: Test proximity controller base InitProximitySensorUser abnormal branch
314  * @tc.type: FUNC
315  * @tc.require: ICGV1M
316  */
317 HWTEST_F(ProximityControllerBaseTest, ProximityControllerBaseTest013, TestSize.Level1)
318 {
319     POWER_HILOGI(LABEL_TEST, "ProximityControllerBaseTest013 function start!");
320     SensorInfo infos[] {{.sensorTypeId = SENSOR_TYPE_ID_PROXIMITY}};
321     g_sensorInfo = infos;
322     g_count = 1;
323     g_intReturnValue = 1;
324     std::shared_ptr<ProximityControllerBase> proximityControllerBase =
325         std::make_shared<ProximityControllerBase>("ProximityControllerBaseTest", nullptr);
326     proximityControllerBase->Enable();
327     proximityControllerBase->InitProximitySensorUser();
328     EXPECT_FALSE(proximityControllerBase->IsSupported());
329     POWER_HILOGI(LABEL_TEST, "ProximityControllerBaseTest013 function end!");
330 }
331 
332 /**
333  * @tc.name: ProximityControllerBaseTest014
334  * @tc.desc: Test proximity controller base InitProximitySensorUser abnormal branch
335  * @tc.type: FUNC
336  * @tc.require: ICGV1M
337  */
338 HWTEST_F(ProximityControllerBaseTest, ProximityControllerBaseTest014, TestSize.Level1)
339 {
340     POWER_HILOGI(LABEL_TEST, "ProximityControllerBaseTest014 function start!");
341     SensorInfo infos[] {{.sensorTypeId = SENSOR_TYPE_ID_PROXIMITY}};
342     g_sensorInfo = infos;
343     g_count = 1;
344     g_intReturnValue = 1;
345     std::shared_ptr<ProximityControllerBase> proximityControllerBase =
346         std::make_shared<ProximityControllerBase>("ProximityControllerBaseTest", nullptr);
347     g_intReturnValue = 0;
348     proximityControllerBase->Enable();
349     proximityControllerBase->InitProximitySensorUser();
350     EXPECT_TRUE(proximityControllerBase->IsSupported());
351     POWER_HILOGI(LABEL_TEST, "ProximityControllerBaseTest014 function end!");
352 }
353 } // namespace