1 /*
2  * Copyright (c) 2021-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 "idevmgr_hdi.h"
17 #include <iostream>
18 #include <cmath>
19 #include <cstdio>
20 #include <unistd.h>
21 #include <gtest/gtest.h>
22 #include <securec.h>
23 #ifdef HDF_DRIVERS_INTERFACE_GEOFENCE_ENABLE
24 #include <osal_mem.h>
25 #include "osal_time.h"
26 #include "hdf_sbuf.h"
27 #include "hdf_base.h"
28 #include "hdf_log.h"
29 #include "v2_0/igeofence_interface.h"
30 #include "geofence_callback_impl.h"
31 
32 using namespace OHOS::HDI::Location::Geofence::V2_0;
33 #endif
34 using namespace std;
35 using namespace testing::ext;
36 
37 namespace {
38     #ifdef HDF_DRIVERS_INTERFACE_GEOFENCE_ENABLE
39     sptr<IGeofenceInterface> g_igeofenceHci = nullptr;
40     constexpr const char *AGNSS_SERVICE_NAME = "agnss_interface_service";
41     constexpr const char *GNSS_SERVICE_NAME = "gnss_interface_service";
42     constexpr const char *GEOFENCE_SERVICE_NAME = "geofence_interface_service";
43     #endif
44 }
45 
46 class LocationGeofenceTest: public testing::Test {
47 public:
48     static void SetUpTestCase();
49     static void TearDownTestCase();
50     void SetUp();
51     void TearDown();
52 };
53 
54 #ifdef HDF_DRIVERS_INTERFACE_GEOFENCE_ENABLE
ReportGeofenceAvailability(bool isAvailable)55 int32_t GeofenceCallbackImpl::ReportGeofenceAvailability(bool isAvailable)
56 {
57     (void)isAvailable;
58     return HDF_SUCCESS;
59 }
60 
ReportGeofenceEvent(int32_t fenceIndex, const LocationInfo& location, GeofenceEvent event, int64_t timestamp)61 int32_t GeofenceCallbackImpl::ReportGeofenceEvent(int32_t fenceIndex, const LocationInfo& location, GeofenceEvent event, int64_t timestamp)
62 {
63     (void)fenceIndex;
64     (void)location;
65     (void)event;
66     (void)timestamp;
67     return HDF_SUCCESS;
68 }
69 
ReportGeofenceOperateResult(int32_t fenceIndex, GeofenceOperateType type, GeofenceOperateResult result)70 int32_t GeofenceCallbackImpl::ReportGeofenceOperateResult(int32_t fenceIndex, GeofenceOperateType type, GeofenceOperateResult result)
71 {
72     (void)fenceIndex;
73     (void)type;
74     (void)result;
75     return HDF_SUCCESS;
76 }
77 #endif
78 
SetUpTestCase()79 void LocationGeofenceTest::SetUpTestCase()
80 {
81 #ifdef HDF_DRIVERS_INTERFACE_GEOFENCE_ENABLE
82     auto devmgr = HDI::DeviceManager::V1_0::IDeviceManager::Get();
83     if (devmgr == nullptr) {
84         printf("fail to get devmgr.\n");
85         return;
86     }
87     if (devmgr->LoadDevice(GNSS_SERVICE_NAME) != 0) {
88         printf("Load gnss service failed!\n");
89         return;
90     }
91     if (devmgr->LoadDevice(AGNSS_SERVICE_NAME) != 0) {
92         printf("Load agnss service failed!\n");
93         return;
94     }
95     if (devmgr->LoadDevice(GEOFENCE_SERVICE_NAME) != 0) {
96         printf("Load geofence service failed!\n");
97         return;
98     }
99     g_igeofenceHci = IGeofenceInterface::Get();
100 #endif
101 }
102 
TearDownTestCase()103 void LocationGeofenceTest::TearDownTestCase()
104 {
105 #ifdef HDF_DRIVERS_INTERFACE_GEOFENCE_ENABLE
106     auto devmgr = HDI::DeviceManager::V1_0::IDeviceManager::Get();
107     if (devmgr == nullptr) {
108         printf("fail to get devmgr.\n");
109         return;
110     }
111     if (devmgr->UnloadDevice(GNSS_SERVICE_NAME) != 0) {
112         printf("Load gnss service failed!\n");
113         return;
114     }
115     if (devmgr->UnloadDevice(AGNSS_SERVICE_NAME) != 0) {
116         printf("Load agnss service failed!\n");
117         return;
118     }
119     if (devmgr->UnloadDevice(GEOFENCE_SERVICE_NAME) != 0) {
120         printf("Load geofence service failed!\n");
121         return;
122     }
123 #endif
124 }
125 
SetUp()126 void LocationGeofenceTest::SetUp()
127 {
128 }
129 
TearDown()130 void LocationGeofenceTest::TearDown()
131 {
132 }
133 
134 
135 /**
136   * @tc.name: SetGeofenceCallback0100
137   * @tc.desc: Set callback function for geofence.
138   * @tc.type: FUNC
139   */
HWTEST_F(LocationGeofenceTest, SUB_DriverSystem_EnableGnss_0100, TestSize.Level1)140 HWTEST_F(LocationGeofenceTest, SUB_DriverSystem_EnableGnss_0100, TestSize.Level1)
141 {
142 #ifdef HDF_DRIVERS_INTERFACE_GEOFENCE_ENABLE
143     if (g_igeofenceHci == nullptr) {
144         ASSERT_NE(nullptr, g_igeofenceHci);
145         return;
146     }
147     sptr<IGeofenceCallback> geo_callback = new (std::nothrow) GeofenceCallbackImpl();
148     if (geo_callback == nullptr) {
149         ASSERT_NE(nullptr, geo_callback);
150         return;
151     }
152     int32_t ret = g_igeofenceHci->SetGeofenceCallback(geo_callback);
153     EXPECT_EQ(HDF_SUCCESS, ret);
154 #endif
155 }
156 
157 
158 /**
159   * @tc.name: AddGnssGeofence0100
160   * @tc.desc: Add a geofence.
161   * @tc.type: FUNC
162   */
HWTEST_F(LocationGeofenceTest, SUB_DriverSystem_AddGnssGeofence_0100, TestSize.Level1)163 HWTEST_F(LocationGeofenceTest, SUB_DriverSystem_AddGnssGeofence_0100, TestSize.Level1)
164 {
165 #ifdef HDF_DRIVERS_INTERFACE_GEOFENCE_ENABLE
166     if (g_igeofenceHci == nullptr) {
167         ASSERT_NE(nullptr, g_igeofenceHci);
168         return;
169     }
170     GeofenceInfo fence;
171     fence.fenceIndex = 3;
172     fence.latitude = 118.90;
173     fence.longitude = 15.25;
174     fence.radius = 12.26;
175     GeofenceEvent geoevent = GeofenceEvent::GEOFENCE_EVENT_ENTERED ;
176     int32_t ret = g_igeofenceHci->AddGnssGeofence(fence,geoevent);
177     EXPECT_EQ(HDF_SUCCESS, ret);
178 #endif
179 }
180 
181 
182 /**
183   * @tc.name: DeleteGnssGeofence0100
184   * @tc.desc: Delete a geofence.
185   * @tc.type: FUNC
186   */
HWTEST_F(LocationGeofenceTest, SUB_DriverSystem_DeleteGnssGeofence_0100, TestSize.Level1)187 HWTEST_F(LocationGeofenceTest, SUB_DriverSystem_DeleteGnssGeofence_0100, TestSize.Level1)
188 {
189 #ifdef HDF_DRIVERS_INTERFACE_GEOFENCE_ENABLE
190     if (g_igeofenceHci == nullptr) {
191         ASSERT_NE(nullptr, g_igeofenceHci);
192         return;
193     }
194     int fenceIndex = 5;
195     int32_t ret = g_igeofenceHci->DeleteGnssGeofence(fenceIndex);
196     EXPECT_EQ(HDF_SUCCESS, ret);
197 #endif
198 }
199