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