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