1 /*
2 * Copyright (c) 2023 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 "string_ex.h"
17 #include <hdf_base.h>
18 #include <hdf_log.h>
19 #include "gnss_interface_impl.h"
20 #include "peripheral_gnss_test.h"
21 #include "location_vendor_lib.h"
22 #include "gnss_measurement_callback_test.h"
23 #include "hdi_gnss_event_callback_mock.h"
24 #include "string_utils.h"
25 #include "v2_0/ignss_interface.h"
26
27 using namespace testing;
28 using namespace testing::ext;
29 using OHOS::HDI::Location::Gnss::V2_0::IGnssInterface;
30 using OHOS::HDI::Location::Gnss::V2_0::IGnssCallback;
31 using OHOS::HDI::Location::Gnss::V2_0::IGnssMeasurementCallback;
32 using OHOS::HDI::Location::Gnss::V2_0::GNSS_START_TYPE_NORMAL;
33 using OHOS::HDI::Location::Gnss::V2_0::GNSS_WORKING_STATUS_NONE;
34 using OHOS::HDI::Location::Gnss::V2_0::GNSS_WORKING_STATUS_SESSION_BEGIN;
35 using OHOS::HDI::Location::Gnss::V2_0::GNSS_WORKING_STATUS_SESSION_END;
36 using OHOS::HDI::Location::Gnss::V2_0::GNSS_WORKING_STATUS_ENGINE_ON;
37 using OHOS::HDI::Location::Gnss::V2_0::GNSS_WORKING_STATUS_ENGINE_OFF;
38 using OHOS::HDI::Location::Gnss::V2_0::GnssAuxiliaryDataType;
39 using OHOS::HDI::Location::Gnss::V2_0::GnssWorkingMode;
40 using OHOS::HDI::Location::Gnss::V2_0::GnssConfigPara;
41 using OHOS::HDI::Location::Gnss::V2_0::GnssRefInfoType;
42 using OHOS::HDI::Location::Gnss::V2_0::GnssRefInfo;
43 using OHOS::HDI::Location::Gnss::V2_0::GnssLocationValidity;
44
45 namespace OHOS {
46 namespace HDI {
47 namespace Location {
48 namespace Gnss {
49 namespace V2_0 {
50
SetUp()51 void PeripheralGnssTest::SetUp()
52 {
53 gnssInstance_ = new (std::nothrow) GnssInterfaceImpl();
54 }
55
TearDown()56 void PeripheralGnssTest::TearDown()
57 {
58 gnssInstance_ = nullptr;
59 LocationVendorInterface::DestroyInstance();
60 }
61
62 HWTEST_F(PeripheralGnssTest, SetGnssConfigParaTest001, TestSize.Level1)
63 {
64 GTEST_LOG_(INFO)
65 << "PeripheralGnssTest, SetGnssConfigParaTest001, TestSize.Level1";
66
67 GnssConfigPara para;
68 para.gnssBasic.gnssMode = GNSS_WORKING_MODE_MS_ASSISTED;
69 gnssInstance_->SetGnssConfigPara(para);
70 EXPECT_NE(gnssInstance_, nullptr);
71 }
72
73 HWTEST_F(PeripheralGnssTest, NmeaCallbackTest001, TestSize.Level1)
74 {
75 GTEST_LOG_(INFO)
76 << "PeripheralGnssTest, NmeaCallbackTest001, TestSize.Level1";
77
78 sptr<ISystemAbilityManager> samgr =
79 SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
80 sptr<IRemoteObject> saObject = samgr->CheckSystemAbility(LOCATION_LOCATOR_SA_ID);
81 auto gnssCallback_ = new (std::nothrow) GnssEventCallbackMock(saObject);
82
83 gnssInstance_->EnableGnss(gnssCallback_);
84 EXPECT_NE(gnssInstance_, nullptr);
85
86 NmeaCallback(0, nullptr, 0);
87 NmeaCallback(0, "nmea_str.", 0);
88 gnssInstance_->DisableGnss();
89 sleep(1);
90 }
91
92 HWTEST_F(PeripheralGnssTest, GetGnssCallbackMethodsTest001, TestSize.Level1)
93 {
94 GTEST_LOG_(INFO)
95 << "PeripheralGnssTest, GetGnssCallbackMethodsTest001, TestSize.Level1";
96
97 // sub reg null brach test
98 GetGnssBasicCallbackMethods(nullptr);
99 GetGnssCacheCallbackMethods(nullptr);
100
101 GetGnssCallbackMethods(nullptr);
102 GnssCallbackStruct device;
103 GetGnssCallbackMethods(&device);
104 EXPECT_NE(gnssInstance_, nullptr);
105 }
106
107 HWTEST_F(PeripheralGnssTest, SvStatusCallbackTest001, TestSize.Level1)
108 {
109 GTEST_LOG_(INFO)
110 << "PeripheralGnssTest, SvStatusCallbackTest001, TestSize.Level1";
111
112 sptr<ISystemAbilityManager> samgr =
113 SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
114 sptr<IRemoteObject> saObject = samgr->CheckSystemAbility(LOCATION_LOCATOR_SA_ID);
115 auto gnssCallback_ = new (std::nothrow) GnssEventCallbackMock(saObject);
116 gnssInstance_->EnableGnss(gnssCallback_);
117 EXPECT_NE(gnssInstance_, nullptr);
118 SvStatusCallback(nullptr);
119 GnssSatelliteStatus gnssSatelliteStatus;
120 gnssSatelliteStatus.satellitesNum = 1;
121 SvStatusCallback(&gnssSatelliteStatus);
122
123 gnssInstance_->DisableGnss();
124 sleep(1);
125 }
126
127 HWTEST_F(PeripheralGnssTest, GnssWorkingStatusUpdateTest001, TestSize.Level1)
128 {
129 GTEST_LOG_(INFO)
130 << "PeripheralGnssTest, GnssWorkingStatusUpdateTest001, TestSize.Level1";
131
132 sptr<ISystemAbilityManager> samgr =
133 SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
134 sptr<IRemoteObject> saObject = samgr->CheckSystemAbility(LOCATION_LOCATOR_SA_ID);
135 auto gnssCallback_ = new (std::nothrow) GnssEventCallbackMock(saObject);
136
137 gnssInstance_->EnableGnss(gnssCallback_);
138 EXPECT_NE(gnssInstance_, nullptr);
139 GnssWorkingStatusUpdate(nullptr);
140 uint16_t statusPtr = 1;
141 GnssWorkingStatusUpdate(&statusPtr);
142 gnssInstance_->DisableGnss();
143 sleep(1);
144 }
145
146 HWTEST_F(PeripheralGnssTest, LocationUpdateTest001, TestSize.Level1)
147 {
148 GTEST_LOG_(INFO)
149 << "PeripheralGnssTest, LocationUpdateTest001, TestSize.Level1";
150
151 sptr<ISystemAbilityManager> samgr =
152 SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
153 sptr<IRemoteObject> saObject = samgr->CheckSystemAbility(LOCATION_LOCATOR_SA_ID);
154 auto gnssCallback_ = new (std::nothrow) GnssEventCallbackMock(saObject);
155
156 gnssInstance_->EnableGnss(gnssCallback_);
157 EXPECT_NE(gnssInstance_, nullptr);
158
159 LocationUpdate(nullptr);
160 GnssLocation location;
161 location.latitude = 31.0;
162 LocationUpdate(&location);
163 gnssInstance_->DisableGnss();
164 sleep(1);
165 }
166
167 HWTEST_F(PeripheralGnssTest, NiNotifyCallbackTest001, TestSize.Level1)
168 {
169 GTEST_LOG_(INFO)
170 << "PeripheralGnssTest, NiNotifyCallbackTest001, TestSize.Level1";
171
172 sptr<ISystemAbilityManager> samgr =
173 SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
174 sptr<IRemoteObject> saObject = samgr->CheckSystemAbility(LOCATION_LOCATOR_SA_ID);
175 auto gnssCallback_ = new (std::nothrow) GnssEventCallbackMock(saObject);
176
177 gnssInstance_->EnableGnss(gnssCallback_);
178 EXPECT_NE(gnssInstance_, nullptr);
179
180 NiNotifyCallback(nullptr);
181 OHOS::HDI::Location::GnssNiNotificationRequest notification;
182 NiNotifyCallback(¬ification);
183 gnssInstance_->DisableGnss();
184 sleep(1);
185 }
186
187 HWTEST_F(PeripheralGnssTest, SendNiUserResponseTest001, TestSize.Level1)
188 {
189 GTEST_LOG_(INFO)
190 << "PeripheralGnssTest, SendNiUserResponseTest001, TestSize.Level1";
191
192 HDF_LOGI("PeripheralGnssTest, SendNiUserResponseTest001, TestSize.Level1");
193 GnssNiResponseCmd userResponse = GnssNiResponseCmd::GNSS_NI_RESPONSE_CMD_ACCEPT;
194 gnssInstance_->SendNiUserResponse(200, userResponse);
195 EXPECT_NE(gnssInstance_, nullptr);
196 gnssInstance_->SendNetworkInitiatedMsg("0", 0);
197 gnssInstance_->SendNetworkInitiatedMsg("0x20", 4);
198 }
199
200 HWTEST_F(PeripheralGnssTest, EnableGnssTest001, TestSize.Level1)
201 {
202 HDF_LOGI("PeripheralGnssTest, EnableGnssTest001, TestSize.Level1");
203
204 sptr<ISystemAbilityManager> samgr =
205 SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
206 sptr<IRemoteObject> saObject = samgr->CheckSystemAbility(LOCATION_LOCATOR_SA_ID);
207 auto gnssCallback_ = new (std::nothrow) GnssEventCallbackMock(saObject);
208
209 gnssInstance_->EnableGnss(gnssCallback_);
210 EXPECT_NE(gnssInstance_, nullptr);
211 HDF_LOGI("PeripheralGnssTest, EnableGnssTest001, TestSize.Level1");
212 gnssInstance_->DisableGnss();
213 sleep(1);
214 }
215
216 HWTEST_F(PeripheralGnssTest, EnableGnssMeasurementTest001, TestSize.Level1)
217 {
218 HDF_LOGI("PeripheralGnssTest, EnableGnssMeasurementTest001, TestSize.Level1");
219
220 sptr<ISystemAbilityManager> samgr =
221 SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
222 sptr<IRemoteObject> saObject = samgr->CheckSystemAbility(LOCATION_LOCATOR_SA_ID);
223 auto gnssCallback_ = new (std::nothrow) GnssEventCallbackMock(saObject);
224
225 gnssInstance_->EnableGnss(gnssCallback_);
226
227 gnssInstance_->EnableGnssMeasurement(nullptr);
228 gnssMeasurementCallback_ = new (std::nothrow) GnssMeasurementCallbackTest(saObject);
229 gnssInstance_->EnableGnssMeasurement(gnssMeasurementCallback_);
230
231 OHOS::HDI::Location::GnssMeasurementInfo gnssMeasurementInfo;
232 gnssMeasurementInfo.size = 2;
233 GnssMeasurementUpdate(&gnssMeasurementInfo);
234 GnssMeasurementUpdate(nullptr);
235
236 HDF_LOGI("PeripheralGnssTest, EnableGnssMeasurementTest001, TestSize.Level1");
237 EXPECT_NE(gnssInstance_, nullptr);
238 gnssInstance_->DisableGnss();
239 sleep(1);
240 }
241
242 HWTEST_F(PeripheralGnssTest, DisableGnssMeasurementTest001, TestSize.Level1)
243 {
244 GTEST_LOG_(INFO)
245 << "PeripheralGnssTest, DisableGnssMeasurementTest001, TestSize.Level1";
246
247 sptr<ISystemAbilityManager> samgr =
248 SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
249 sptr<IRemoteObject> saObject = samgr->CheckSystemAbility(LOCATION_LOCATOR_SA_ID);
250 auto gnssCallback_ = new (std::nothrow) GnssEventCallbackMock(saObject);
251
252 gnssInstance_->EnableGnss(gnssCallback_);
253
254 EXPECT_NE(nullptr, gnssInstance_);
255 if (gnssInstance_ != nullptr) {
256 gnssInstance_->DisableGnssMeasurement();
257 }
258 gnssInstance_->DisableGnss();
259 sleep(1);
260 }
261
262 HWTEST_F(PeripheralGnssTest, SetGnssReferenceInfoTest001, TestSize.Level1)
263 {
264 GTEST_LOG_(INFO)
265 << "PeripheralGnssTest, SetGnssReferenceInfoTest001, TestSize.Level1";
266
267 EXPECT_NE(nullptr, gnssInstance_);
268 int32_t ret = 0;
269
270 if (gnssInstance_ != nullptr) {
271 GnssRefInfo refInfo;
272 refInfo.type = GnssRefInfoType::GNSS_REF_INFO_TIME;
273 ret = gnssInstance_->SetGnssReferenceInfo(refInfo);
274 refInfo.type = GnssRefInfoType::GNSS_REF_INFO_LOCATION;
275 ret = gnssInstance_->SetGnssReferenceInfo(refInfo);
276 refInfo.type = GnssRefInfoType::GNSS_REF_INFO_BEST_LOCATION;
277 ret = gnssInstance_->SetGnssReferenceInfo(refInfo);
278 }
279 HDF_LOGI("test ret:%{public}d", ret);
280 }
281
282 HWTEST_F(PeripheralGnssTest, DeleteAuxiliaryData001, TestSize.Level1)
283 {
284 GTEST_LOG_(INFO)
285 << "PeripheralGnssTest, DeleteAuxiliaryData001, TestSize.Level1";
286
287 EXPECT_NE(nullptr, gnssInstance_);
288
289 if (gnssInstance_ != nullptr) {
290 unsigned short data = 0;
291 gnssInstance_->DeleteAuxiliaryData(data);
292 }
293 }
294
295 HWTEST_F(PeripheralGnssTest, SetPredictGnssDataTest001, TestSize.Level1)
296 {
297 GTEST_LOG_(INFO)
298 << "PeripheralGnssTest, SetPredictGnssDataTest001, TestSize.Level1";
299 EXPECT_NE(nullptr, gnssInstance_);
300 if (gnssInstance_ != nullptr) {
301 std::string data = "test";
302 int32_t ret = gnssInstance_->SetPredictGnssData(data);
303 HDF_LOGI("test ret:%{public}d", ret);
304 }
305 }
306
307 HWTEST_F(PeripheralGnssTest, GetCachedGnssLocationsSizeTest001, TestSize.Level1)
308 {
309 GTEST_LOG_(INFO)
310 << "PeripheralGnssTest, GetCachedGnssLocationsSizeTest001, TestSize.Level1";
311 EXPECT_NE(nullptr, gnssInstance_);
312 if (gnssInstance_ != nullptr) {
313 int32_t size = 0;
314 int32_t ret = gnssInstance_->GetCachedGnssLocationsSize(size);
315 HDF_LOGI("test ret:%{public}d", ret);
316 }
317 }
318
319 HWTEST_F(PeripheralGnssTest, GetCachedGnssLocationsTest001, TestSize.Level1)
320 {
321 GTEST_LOG_(INFO)
322 << "PeripheralGnssTest, GetCachedGnssLocationsTest001, TestSize.Level1";
323 EXPECT_NE(nullptr, gnssInstance_);
324 if (gnssInstance_ != nullptr) {
325 int32_t ret = gnssInstance_->GetCachedGnssLocations();
326 HDF_LOGI("test ret:%{public}d", ret);
327 }
328 }
329
330 HWTEST_F(PeripheralGnssTest, SendNiUserResponseTest002, TestSize.Level1)
331 {
332 GTEST_LOG_(INFO)
333 << "PeripheralGnssTest, SendNiUserResponseTest002, TestSize.Level1";
334 EXPECT_NE(nullptr, gnssInstance_);
335 int32_t gnssNiNotificationId = 0;
336 GnssNiResponseCmd userResponse = GnssNiResponseCmd::GNSS_NI_RESPONSE_CMD_ACCEPT;
337 if (gnssInstance_ != nullptr) {
338 gnssInstance_->SendNiUserResponse(gnssNiNotificationId, userResponse);
339 }
340 }
341
342 HWTEST_F(PeripheralGnssTest, SendNetworkInitiatedMsg001, TestSize.Level1)
343 {
344 GTEST_LOG_(INFO)
345 << "PeripheralGnssTest, SendNetworkInitiatedMsg001, TestSize.Level1";
346 EXPECT_NE(nullptr, gnssInstance_);
347 if (gnssInstance_ != nullptr) {
348 std::string msg = "test";
349 int length = msg.length();
350 int32_t ret = gnssInstance_->SendNetworkInitiatedMsg(msg, length);
351 HDF_LOGI("test ret:%{public}d", ret);
352 }
353 }
354
355 HWTEST_F(PeripheralGnssTest, StartGnssTest001, TestSize.Level1)
356 {
357 GTEST_LOG_(INFO)
358 << "PeripheralGnssTest, StartGnssTest001, TestSize.Level1";
359 int32_t ret = 0;
360
361 ret = gnssInstance_->StartGnss(GNSS_START_TYPE_NORMAL);
362 ret = gnssInstance_->StopGnss(GNSS_START_TYPE_NORMAL);
363 HDF_LOGI("test ret:%{public}d", ret);
364 }
365
366 HWTEST_F(PeripheralGnssTest, DoubleEnableGnssTest001, TestSize.Level1)
367 {
368 GTEST_LOG_(INFO)
369 << "PeripheralGnssTest, DoubleEnableGnssTest001, TestSize.Level1";
370
371 sptr<ISystemAbilityManager> samgr =
372 SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
373 sptr<IRemoteObject> saObject = samgr->CheckSystemAbility(LOCATION_LOCATOR_SA_ID);
374 auto gnssCallback_ = new (std::nothrow) GnssEventCallbackMock(saObject);
375
376 auto ret = gnssInstance_->EnableGnss(gnssCallback_);
377 sleep(1);
378 auto gnssCallback_2 = new (std::nothrow) GnssEventCallbackMock(saObject);
379 gnssInstance_->EnableGnss(gnssCallback_2);
380 gnssInstance_->DisableGnss();
381 HDF_LOGI("test ret:%{public}d", ret);
382 }
383
384 HWTEST_F(PeripheralGnssTest, GetGnssMeasurementCallbackMethodsTest001, TestSize.Level1)
385 {
386 GTEST_LOG_(INFO)
387 << "PeripheralGnssTest, GetGnssMeasurementCallbackMethodsTest001, TestSize.Level1";
388 GnssMeasurementCallbackIfaces device;
389 GetGnssMeasurementCallbackMethods(nullptr);
390 GetGnssMeasurementCallbackMethods(&device);
391 }
392
393 HWTEST_F(PeripheralGnssTest, GetModuleInterfaceTest001, TestSize.Level1)
394 {
395 GTEST_LOG_(INFO)
396 << "PeripheralGnssTest, GetModuleInterfaceTest001, TestSize.Level1";
397
398 auto locationVendorInterface = LocationVendorInterface::GetInstance();
399 locationVendorInterface->GetModuleInterface(0);
400 EXPECT_NE(gnssInstance_, nullptr);
401 }
402
403 HWTEST_F(PeripheralGnssTest, RestGnssTest001, TestSize.Level1)
404 {
405 GTEST_LOG_(INFO)
406 << "PeripheralGnssTest, RestGnssTest001, TestSize.Level1";
407
408 auto gnssImpl = new (std::nothrow) GnssInterfaceImpl();
409 auto ret = gnssImpl->StartGnss(GNSS_START_TYPE_NORMAL);
410 EXPECT_NE(gnssInstance_, nullptr);
411 sptr<ISystemAbilityManager> samgr =
412 SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
413 sptr<IRemoteObject> saObject = samgr->CheckSystemAbility(LOCATION_LOCATOR_SA_ID);
414 gnssMeasurementCallback_ = new (std::nothrow) GnssMeasurementCallbackTest(saObject);
415 gnssImpl->EnableGnssMeasurement(gnssMeasurementCallback_);
416 gnssImpl->ResetGnss();
417 HDF_LOGI("test ret:%{public}d", ret);
418 }
419
420 HWTEST_F(PeripheralGnssTest, HexCharToIntTest001, TestSize.Level1)
421 {
422 GTEST_LOG_(INFO)
423 << "PeripheralGnssTest, HexCharToIntTest001, TestSize.Level1";
424
425 std::string str = "1234";
426 std::vector<uint8_t> ret = StringUtils::HexToByteVector(str);
427 EXPECT_NE(ret.size(), 10);
428 }
429
430 } // V2_0
431 } // Gnss
432 } // Location
433 } // HDI
434 } // OHOS
435