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 return HDF_SUCCESS;
54 }
55
ReportGeofenceEvent(int32_t fenceIndex,const LocationInfo & location,GeofenceEvent event,int64_t timestamp)56 int32_t GeofenceCallbackImpl::ReportGeofenceEvent(int32_t fenceIndex, const LocationInfo& location, GeofenceEvent event, int64_t timestamp)
57 {
58 return HDF_SUCCESS;
59 }
60
ReportGeofenceOperateResult(int32_t fenceIndex,GeofenceOperateType type,GeofenceOperateResult result)61 int32_t GeofenceCallbackImpl::ReportGeofenceOperateResult(int32_t fenceIndex, GeofenceOperateType type, GeofenceOperateResult result)
62 {
63 return HDF_SUCCESS;
64 }
65
SetUpTestCase()66 void LocationGeofenceTest::SetUpTestCase()
67 {
68 auto devmgr = HDI::DeviceManager::V1_0::IDeviceManager::Get();
69 if (devmgr == nullptr) {
70 printf("fail to get devmgr.\n");
71 return;
72 }
73 if (devmgr->LoadDevice(GNSS_SERVICE_NAME) != 0) {
74 printf("Load gnss service failed!\n");
75 return;
76 }
77 if (devmgr->LoadDevice(AGNSS_SERVICE_NAME) != 0) {
78 printf("Load agnss service failed!\n");
79 return;
80 }
81 if (devmgr->LoadDevice(GEOFENCE_SERVICE_NAME) != 0) {
82 printf("Load geofence service failed!\n");
83 return;
84 }
85 g_igeofenceHci = IGeofenceInterface::Get();
86 }
87
TearDownTestCase()88 void LocationGeofenceTest::TearDownTestCase()
89 {
90 auto devmgr = HDI::DeviceManager::V1_0::IDeviceManager::Get();
91 if (devmgr == nullptr) {
92 printf("fail to get devmgr.\n");
93 return;
94 }
95 if (devmgr->UnloadDevice(GNSS_SERVICE_NAME) != 0) {
96 printf("Load gnss service failed!\n");
97 return;
98 }
99 if (devmgr->UnloadDevice(AGNSS_SERVICE_NAME) != 0) {
100 printf("Load agnss service failed!\n");
101 return;
102 }
103 if (devmgr->UnloadDevice(GEOFENCE_SERVICE_NAME) != 0) {
104 printf("Load geofence service failed!\n");
105 return;
106 }
107 }
108
SetUp()109 void LocationGeofenceTest::SetUp()
110 {
111 }
112
TearDown()113 void LocationGeofenceTest::TearDown()
114 {
115 }
116
117
118 /**
119 * @tc.name: SetGeofenceCallback0100
120 * @tc.desc: Set callback function for geofence.
121 * @tc.type: FUNC
122 */
123 HWTEST_F(LocationGeofenceTest, SUB_DriverSystem_EnableGnss_0100, TestSize.Level1)
124 {
125 if (g_igeofenceHci == nullptr) {
126 ASSERT_NE(nullptr, g_igeofenceHci);
127 return;
128 }
129 sptr<IGeofenceCallback> geo_callback = new (std::nothrow) GeofenceCallbackImpl();
130 if (geo_callback == nullptr) {
131 ASSERT_NE(nullptr, geo_callback);
132 return;
133 }
134 int32_t ret = g_igeofenceHci->SetGeofenceCallback(geo_callback);
135 EXPECT_EQ(HDF_SUCCESS, ret);
136 }
137
138
139 /**
140 * @tc.name: AddGnssGeofence0100
141 * @tc.desc: Add a geofence.
142 * @tc.type: FUNC
143 */
144 HWTEST_F(LocationGeofenceTest, SUB_DriverSystem_AddGnssGeofence_0100, TestSize.Level1)
145 {
146 if (g_igeofenceHci == nullptr) {
147 ASSERT_NE(nullptr, g_igeofenceHci);
148 return;
149 }
150 GeofenceInfo fence;
151 fence.fenceIndex = 3;
152 fence.latitude = 118.90;
153 fence.longitude = 15.25;
154 fence.radius = 12.26;
155 GeofenceEvent geoevent = GeofenceEvent::GEOFENCE_EVENT_UNCERTAIN ;
156 int32_t ret = g_igeofenceHci->AddGnssGeofence(fence,geoevent);
157 EXPECT_EQ(HDF_SUCCESS, ret);
158 }
159
160
161 /**
162 * @tc.name: DeleteGnssGeofence0100
163 * @tc.desc: Delete a geofence.
164 * @tc.type: FUNC
165 */
166 HWTEST_F(LocationGeofenceTest, SUB_DriverSystem_DeleteGnssGeofence_0100, TestSize.Level1)
167 {
168 if (g_igeofenceHci == nullptr) {
169 ASSERT_NE(nullptr, g_igeofenceHci);
170 return;
171 }
172 int fenceIndex = 5;
173 int32_t ret = g_igeofenceHci->DeleteGnssGeofence(fenceIndex);
174 EXPECT_EQ(HDF_SUCCESS, ret);
175 }
176