• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 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 "locator_impl_test.h"
17 
18 #include "accesstoken_kit.h"
19 #include "message_parcel.h"
20 #include "nativetoken_kit.h"
21 #include "system_ability_definition.h"
22 #include "token_setproc.h"
23 #include "iremote_object.h"
24 
25 #ifdef FEATURE_GNSS_SUPPORT
26 #include "cached_locations_callback_napi.h"
27 #endif
28 #include "common_utils.h"
29 #include "constant_definition.h"
30 #include "country_code.h"
31 #include "country_code_callback_napi.h"
32 #ifdef FEATURE_GEOCODE_SUPPORT
33 #include "geo_address.h"
34 #endif
35 #ifdef FEATURE_GNSS_SUPPORT
36 #include "gnss_status_callback_napi.h"
37 #include "i_cached_locations_callback.h"
38 #endif
39 #include "location.h"
40 #include "location_sa_load_manager.h"
41 #include "location_switch_callback_napi.h"
42 #include "locator.h"
43 #include "locator_callback_proxy.h"
44 #include "locator_proxy.h"
45 #ifdef FEATURE_GNSS_SUPPORT
46 #include "nmea_message_callback_napi.h"
47 #endif
48 #include "request_config.h"
49 #include "locating_required_data_callback_napi.h"
50 #include "locator_agent.h"
51 #include "permission_manager.h"
52 #include "geofence_request.h"
53 #include "geofence_sdk.h"
54 
55 using namespace testing::ext;
56 
57 namespace OHOS {
58 namespace Location {
59 const int32_t LOCATION_PERM_NUM = 4;
60 const int INVALID_PRIVACY_TYPE = -1;
61 const int32_t DEFAULT_USER = 100;
62 #ifdef FEATURE_GNSS_SUPPORT
63 const int INVALID_CACHED_SIZE = 0;
64 #endif
65 #ifdef FEATURE_GEOCODE_SUPPORT
66 const double MOCK_LATITUDE = 99.0;
67 const double MOCK_LONGITUDE = 100.0;
68 #endif
SetUp()69 void LocatorImplTest::SetUp()
70 {
71     MockNativePermission();
72     LoadSystemAbility();
73     locatorImpl_ = Locator::GetInstance();
74     ASSERT_TRUE(locatorImpl_ != nullptr);
75     callbackStub_ = new (std::nothrow) LocatorCallbackStub();
76     ASSERT_TRUE(callbackStub_ != nullptr);
77 }
78 
TearDown()79 void LocatorImplTest::TearDown()
80 {
81 }
82 
LoadSystemAbility()83 void LocatorImplTest::LoadSystemAbility()
84 {
85     LocationSaLoadManager::GetInstance()->LoadLocationSa(LOCATION_LOCATOR_SA_ID);
86 #ifdef FEATURE_GNSS_SUPPORT
87     LocationSaLoadManager::GetInstance()->LoadLocationSa(LOCATION_GNSS_SA_ID);
88 #endif
89 #ifdef FEATURE_PASSIVE_SUPPORT
90     LocationSaLoadManager::GetInstance()->LoadLocationSa(LOCATION_NOPOWER_LOCATING_SA_ID);
91 #endif
92 #ifdef FEATURE_NETWORK_SUPPORT
93     LocationSaLoadManager::GetInstance()->LoadLocationSa(LOCATION_NETWORK_LOCATING_SA_ID);
94 #endif
95 #ifdef FEATURE_GEOCODE_SUPPORT
96     LocationSaLoadManager::GetInstance()->LoadLocationSa(LOCATION_GEO_CONVERT_SA_ID);
97 #endif
98 }
99 
MockNativePermission()100 void LocatorImplTest::MockNativePermission()
101 {
102     const char *perms[] = {
103         ACCESS_LOCATION.c_str(), ACCESS_APPROXIMATELY_LOCATION.c_str(),
104         ACCESS_BACKGROUND_LOCATION.c_str(), MANAGE_SECURE_SETTINGS.c_str(),
105     };
106     NativeTokenInfoParams infoInstance = {
107         .dcapsNum = 0,
108         .permsNum = LOCATION_PERM_NUM,
109         .aclsNum = 0,
110         .dcaps = nullptr,
111         .perms = perms,
112         .acls = nullptr,
113         .processName = "LocatorImplTest",
114         .aplStr = "system_basic",
115     };
116     tokenId_ = GetAccessTokenId(&infoInstance);
117     SetSelfTokenID(tokenId_);
118     Security::AccessToken::AccessTokenKit::ReloadNativeTokenInfo();
119 }
120 
121 #ifdef FEATURE_GEOCODE_SUPPORT
SetGeocodingMockInfo()122 std::vector<std::shared_ptr<GeocodingMockInfo>> LocatorImplTest::SetGeocodingMockInfo()
123 {
124     std::vector<std::shared_ptr<GeocodingMockInfo>> geoMockInfos;
125     std::shared_ptr<GeocodingMockInfo> geocodingMockInfo =
126         std::make_shared<GeocodingMockInfo>();
127     MessageParcel parcel;
128     parcel.WriteString16(Str8ToStr16("locale"));
129     parcel.WriteDouble(MOCK_LATITUDE); // latitude
130     parcel.WriteDouble(MOCK_LONGITUDE); // longitude
131     parcel.WriteInt32(1);
132     parcel.WriteString("localeLanguage");
133     parcel.WriteString("localeCountry");
134     parcel.WriteInt32(1); // size
135     parcel.WriteInt32(0); // line
136     parcel.WriteString("line");
137     parcel.WriteString("placeName");
138     parcel.WriteString("administrativeArea");
139     parcel.WriteString("subAdministrativeArea");
140     parcel.WriteString("locality");
141     parcel.WriteString("subLocality");
142     parcel.WriteString("roadName");
143     parcel.WriteString("subRoadName");
144     parcel.WriteString("premises");
145     parcel.WriteString("postalCode");
146     parcel.WriteString("countryCode");
147     parcel.WriteString("countryName");
148     parcel.WriteInt32(1); // hasLatitude
149     parcel.WriteDouble(MOCK_LATITUDE); // latitude
150     parcel.WriteInt32(1); // hasLongitude
151     parcel.WriteDouble(MOCK_LONGITUDE); // longitude
152     parcel.WriteString("phoneNumber");
153     parcel.WriteString("addressUrl");
154     parcel.WriteBool(true);
155     geocodingMockInfo->ReadFromParcel(parcel);
156     geoMockInfos.emplace_back(std::move(geocodingMockInfo));
157     return geoMockInfos;
158 }
159 #endif
160 
161 HWTEST_F(LocatorImplTest, locatorImplEnableAbilityV9001, TestSize.Level1)
162 {
163     GTEST_LOG_(INFO)
164         << "LocatorImplTest, locatorImplEnableAbilityV9001, TestSize.Level1";
165     LBSLOGI(LOCATOR, "[LocatorImplTest] locatorImplEnableAbilityV9001 begin");
166     auto switchCallbackHost =
167         sptr<LocationSwitchCallbackNapi>(new (std::nothrow) LocationSwitchCallbackNapi());
168     EXPECT_NE(nullptr, switchCallbackHost);
169     EXPECT_EQ(ERRCODE_SUCCESS, locatorImpl_->RegisterSwitchCallbackV9(switchCallbackHost->AsObject()));
170     sleep(1);
171 
172     EXPECT_EQ(ERRCODE_SUCCESS, locatorImpl_->EnableAbilityV9(false));
173     bool isEnabled = false;
174     EXPECT_EQ(ERRCODE_SUCCESS, locatorImpl_->IsLocationEnabledV9(isEnabled));
175     EXPECT_EQ(false, isEnabled);
176 
177     EXPECT_EQ(ERRCODE_SUCCESS, locatorImpl_->UnregisterSwitchCallbackV9(switchCallbackHost->AsObject()));
178     LBSLOGI(LOCATOR, "[LocatorImplTest] locatorImplEnableAbilityV9001 end");
179 }
180 
181 HWTEST_F(LocatorImplTest, locatorImplEnableAbilityV9002, TestSize.Level1)
182 {
183     GTEST_LOG_(INFO)
184         << "LocatorImplTest, locatorImplEnableAbilityV9002, TestSize.Level1";
185     LBSLOGI(LOCATOR, "[LocatorImplTest] locatorImplEnableAbilityV9002 begin");
186     auto switchCallbackHost =
187         sptr<LocationSwitchCallbackNapi>(new (std::nothrow) LocationSwitchCallbackNapi());
188     EXPECT_NE(nullptr, switchCallbackHost);
189     EXPECT_EQ(ERRCODE_SUCCESS, locatorImpl_->RegisterSwitchCallbackV9(switchCallbackHost->AsObject()));
190     sleep(1);
191 
192     EXPECT_EQ(ERRCODE_SUCCESS, locatorImpl_->EnableAbilityV9(true));
193     bool isEnabled = false;
194     EXPECT_EQ(ERRCODE_SUCCESS, locatorImpl_->IsLocationEnabledV9(isEnabled));
195 
196     EXPECT_EQ(ERRCODE_SUCCESS, locatorImpl_->UnregisterSwitchCallbackV9(switchCallbackHost->AsObject()));
197     LBSLOGI(LOCATOR, "[LocatorImplTest] locatorImplEnableAbilityV9002 end");
198 }
199 
200 HWTEST_F(LocatorImplTest, locatorImplGetCachedLocationV9, TestSize.Level1)
201 {
202     GTEST_LOG_(INFO)
203         << "LocatorImplTest, locatorImplGetCachedLocationV9, TestSize.Level1";
204     LBSLOGI(LOCATOR, "[LocatorImplTest] locatorImplGetCachedLocationV9 begin");
205 
206     locatorImpl_->EnableLocationMockV9(); // mock switch on
207 
208     std::unique_ptr<RequestConfig> requestConfig = std::make_unique<RequestConfig>();
209     requestConfig->SetPriority(PRIORITY_ACCURACY);
210     EXPECT_EQ(ERRCODE_SUCCESS, locatorImpl_->StartLocatingV9(requestConfig, callbackStub_)); // startLocating first
211     sleep(1);
212 
213     int timeInterval = 0;
214     std::vector<std::shared_ptr<Location>> locations;
215     Parcel parcel;
216     parcel.WriteDouble(10.6); // latitude
217     parcel.WriteDouble(10.5); // longitude
218     parcel.WriteDouble(10.4); // altitude
219     parcel.WriteDouble(1.0); // accuracy
220     parcel.WriteDouble(5.0); // speed
221     parcel.WriteDouble(10); // direction
222     parcel.WriteInt64(1611000000); // timestamp
223     parcel.WriteInt64(1611000000); // time since boot
224     parcel.WriteString16(u"additions"); // additions
225     parcel.WriteInt64(1); // additionSize
226     parcel.WriteInt32(0); // isFromMock is false
227     locations.push_back(Location::UnmarshallingShared(parcel));
228     locatorImpl_->SetMockedLocationsV9(timeInterval, locations); // set fake locations
229     sleep(1);
230 
231     std::unique_ptr<Location> loc = std::make_unique<Location>();
232     locatorImpl_->GetCachedLocationV9(loc); // get last location
233     EXPECT_EQ(ERRCODE_SUCCESS, locatorImpl_->StopLocatingV9(callbackStub_));
234 
235     locatorImpl_->DisableLocationMockV9();
236     LBSLOGI(LOCATOR, "[LocatorImplTest] locatorImplGetCachedLocationV9 end");
237 }
238 
239 HWTEST_F(LocatorImplTest, locatorImplDisableLocationMockV9, TestSize.Level1)
240 {
241     GTEST_LOG_(INFO)
242         << "LocatorImplTest, locatorImplDisableLocationMockV9, TestSize.Level1";
243     LBSLOGI(LOCATOR, "[LocatorImplTest] locatorImplDisableLocationMockV9 begin");
244     Locator::GetInstance()->DisableLocationMockV9();
245     LBSLOGI(LOCATOR, "[LocatorImplTest] locatorImplDisableLocationMockV9 end");
246 }
247 
248 HWTEST_F(LocatorImplTest, locatorImplPrivacyStateV9001, TestSize.Level1)
249 {
250     GTEST_LOG_(INFO)
251         << "LocatorImplTest, locatorImplPrivacyStateV9001, TestSize.Level1";
252     LBSLOGI(LOCATOR, "[LocatorImplTest] locatorImplPrivacyStateV9001 begin");
253     bool isConfirmed = false;
254     locatorImpl_->SetLocationPrivacyConfirmStatusV9(INVALID_PRIVACY_TYPE, true);
255     locatorImpl_->IsLocationPrivacyConfirmedV9(INVALID_PRIVACY_TYPE, isConfirmed);
256     LBSLOGI(LOCATOR, "[LocatorImplTest] locatorImplPrivacyStateV9001 end");
257 }
258 
259 #ifdef FEATURE_GNSS_SUPPORT
260 HWTEST_F(LocatorImplTest, locatorImplGetCachedGnssLocationsSizeV9, TestSize.Level1)
261 {
262     GTEST_LOG_(INFO)
263         << "LocatorImplTest, locatorImplGetCachedGnssLocationsSizeV9, TestSize.Level1";
264     LBSLOGI(LOCATOR, "[LocatorImplTest] locatorImplGetCachedGnssLocationsSizeV9 begin");
265     int size = INVALID_CACHED_SIZE;
266     EXPECT_EQ(ERRCODE_NOT_SUPPORTED, locatorImpl_->GetCachedGnssLocationsSizeV9(size));
267     EXPECT_EQ(INVALID_CACHED_SIZE, size);
268     LBSLOGI(LOCATOR, "[LocatorImplTest] locatorImplGetCachedGnssLocationsSizeV9 end");
269 }
270 #endif
271 
272 #ifdef FEATURE_GNSS_SUPPORT
273 HWTEST_F(LocatorImplTest, locatorImplFlushCachedGnssLocationsV9, TestSize.Level1)
274 {
275     GTEST_LOG_(INFO)
276         << "LocatorImplTest, locatorImplFlushCachedGnssLocationsV9, TestSize.Level1";
277     LBSLOGI(LOCATOR, "[LocatorImplTest] locatorImplFlushCachedGnssLocationsV9 begin");
278     EXPECT_EQ(ERRCODE_NOT_SUPPORTED, locatorImpl_->FlushCachedGnssLocationsV9());
279     LBSLOGI(LOCATOR, "[LocatorImplTest] locatorImplFlushCachedGnssLocationsV9 end");
280 }
281 #endif
282 
283 #ifdef FEATURE_GNSS_SUPPORT
284 HWTEST_F(LocatorImplTest, locatorImplSendCommandV9, TestSize.Level1)
285 {
286     GTEST_LOG_(INFO)
287         << "LocatorImplTest, locatorImplSendCommandV9, TestSize.Level1";
288     LBSLOGI(LOCATOR, "[LocatorImplTest] locatorImplSendCommandV9 begin");
289     std::unique_ptr<LocationCommand> command = std::make_unique<LocationCommand>();
290     command->scenario = SCENE_NAVIGATION;
291     command->command = "cmd";
292     EXPECT_EQ(ERRCODE_SUCCESS, locatorImpl_->SendCommandV9(command));
293     LBSLOGI(LOCATOR, "[LocatorImplTest] locatorImplSendCommandV9 end");
294 }
295 #endif
296 
297 
298 HWTEST_F(LocatorImplTest, locatorImplGetIsoCountryCodeV9, TestSize.Level1)
299 {
300     GTEST_LOG_(INFO)
301         << "LocatorImplTest, locatorImplGetIsoCountryCodeV9, TestSize.Level1";
302     LBSLOGI(LOCATOR, "[LocatorImplTest] locatorImplGetIsoCountryCodeV9 begin");
303 
304     auto countryCodeCallbackHost =
305         sptr<CountryCodeCallbackNapi>(new (std::nothrow) CountryCodeCallbackNapi());
306     EXPECT_NE(nullptr, countryCodeCallbackHost);
307     EXPECT_EQ(ERRCODE_SUCCESS, locatorImpl_->RegisterCountryCodeCallbackV9(countryCodeCallbackHost->AsObject()));
308     sleep(1);
309 
310     std::shared_ptr<CountryCode> countryCode = std::make_shared<CountryCode>();
311     EXPECT_EQ(ERRCODE_SUCCESS, locatorImpl_->GetIsoCountryCodeV9(countryCode));
312     ASSERT_TRUE(countryCode != nullptr);
313     LBSLOGI(LOCATOR, "countrycode : %{public}s", countryCode->ToString().c_str());
314     sleep(1);
315     EXPECT_EQ(ERRCODE_SUCCESS, locatorImpl_->UnregisterCountryCodeCallbackV9(countryCodeCallbackHost->AsObject()));
316     LBSLOGI(LOCATOR, "[LocatorImplTest] locatorImplGetIsoCountryCodeV9 end");
317 }
318 
319 HWTEST_F(LocatorImplTest, locatorImplProxyForFreeze, TestSize.Level1)
320 {
321     GTEST_LOG_(INFO)
322         << "LocatorImplTest, locatorImplProxyForFreeze, TestSize.Level1";
323     LBSLOGI(LOCATOR, "[LocatorImplTest] locatorImplProxyForFreeze begin");
324     std::set<int> pidList;
325     pidList.insert(SYSTEM_UID);
326     EXPECT_EQ(ERRCODE_PERMISSION_DENIED, locatorImpl_->ProxyForFreeze(pidList, false));
327     LBSLOGI(LOCATOR, "[LocatorImplTest] locatorImplProxyForFreeze end");
328 }
329 
330 #ifdef FEATURE_GEOCODE_SUPPORT
331 HWTEST_F(LocatorImplTest, locatorImplIsGeoServiceAvailableV9001, TestSize.Level1)
332 {
333     GTEST_LOG_(INFO)
334         << "LocatorImplTest, locatorImplIsGeoServiceAvailableV9001, TestSize.Level1";
335     LBSLOGI(LOCATOR, "[LocatorImplTest] locatorImplIsGeoServiceAvailableV9001 begin");
336     bool isAvailable = true;
337     locatorImpl_->DisableReverseGeocodingMockV9();
338     locatorImpl_->IsGeoServiceAvailableV9(isAvailable);
339     LBSLOGI(LOCATOR, "[LocatorImplTest] locatorImplIsGeoServiceAvailableV9001 end");
340 }
341 #endif
342 
343 #ifdef FEATURE_GEOCODE_SUPPORT
344 HWTEST_F(LocatorImplTest, locatorImplGetAddressByCoordinateV9001, TestSize.Level1)
345 {
346     GTEST_LOG_(INFO)
347         << "LocatorImplTest, locatorImplGetAddressByCoordinateV9001, TestSize.Level1";
348     LBSLOGI(LOCATOR, "[LocatorImplTest] locatorImplGetAddressByCoordinateV9001 begin");
349     MessageParcel request001;
350     std::list<std::shared_ptr<GeoAddress>> geoAddressList001;
351     locatorImpl_->EnableReverseGeocodingMockV9();
352 
353     std::vector<std::shared_ptr<GeocodingMockInfo>> mockInfos = SetGeocodingMockInfo();
354     locatorImpl_->SetReverseGeocodingMockInfoV9(mockInfos);
355     request001.WriteInterfaceToken(LocatorProxy::GetDescriptor());
356     request001.WriteDouble(MOCK_LATITUDE); // latitude
357     request001.WriteDouble(MOCK_LONGITUDE); // longitude
358     request001.WriteInt32(3); // maxItems
359     request001.WriteInt32(1); // locale object size = 1
360     request001.WriteString16(Str8ToStr16("Language")); // locale.getLanguage()
361     request001.WriteString16(Str8ToStr16("Country")); // locale.getCountry()
362     request001.WriteString16(Str8ToStr16("Variant")); // locale.getVariant()
363     request001.WriteString16(Str8ToStr16("")); // ""
364     locatorImpl_->GetAddressByCoordinateV9(request001, geoAddressList001);
365     EXPECT_EQ(true, geoAddressList001.empty());
366 
367     locatorImpl_->DisableReverseGeocodingMockV9();
368     LBSLOGI(LOCATOR, "[LocatorImplTest] locatorImplGetAddressByCoordinateV9001 end");
369 }
370 #endif
371 
372 #ifdef FEATURE_GEOCODE_SUPPORT
373 HWTEST_F(LocatorImplTest, locatorImplGetAddressByCoordinateV9002, TestSize.Level1)
374 {
375     GTEST_LOG_(INFO)
376         << "LocatorImplTest, locatorImplGetAddressByCoordinateV9002, TestSize.Level1";
377     LBSLOGI(LOCATOR, "[LocatorImplTest] locatorImplGetAddressByCoordinateV9002 begin");
378     MessageParcel request002;
379     std::list<std::shared_ptr<GeoAddress>> geoAddressList002;
380     locatorImpl_->DisableReverseGeocodingMockV9();
381 
382     request002.WriteInterfaceToken(LocatorProxy::GetDescriptor());
383     request002.WriteDouble(1.0); // latitude
384     request002.WriteDouble(2.0); // longitude
385     request002.WriteInt32(3); // maxItems
386     request002.WriteInt32(1); // locale object size = 1
387     request002.WriteString16(Str8ToStr16("Language")); // locale.getLanguage()
388     request002.WriteString16(Str8ToStr16("Country")); // locale.getCountry()
389     request002.WriteString16(Str8ToStr16("Variant")); // locale.getVariant()
390     request002.WriteString16(Str8ToStr16("")); // ""
391     EXPECT_EQ(ERRCODE_REVERSE_GEOCODING_FAIL, locatorImpl_->GetAddressByCoordinateV9(request002, geoAddressList002));
392     EXPECT_EQ(true, geoAddressList002.empty());
393     LBSLOGI(LOCATOR, "[LocatorImplTest] locatorImplGetAddressByCoordinateV9002 end");
394 }
395 #endif
396 
397 #ifdef FEATURE_GEOCODE_SUPPORT
398 HWTEST_F(LocatorImplTest, locatorImplGetAddressByLocationNameV9001, TestSize.Level1)
399 {
400     GTEST_LOG_(INFO)
401         << "LocatorImplTest, locatorImplGetAddressByLocationNameV9001, TestSize.Level1";
402     LBSLOGI(LOCATOR, "[LocatorImplTest] locatorImplGetAddressByLocationNameV9001 begin");
403     MessageParcel request003;
404     std::list<std::shared_ptr<GeoAddress>> geoAddressList003;
405     request003.WriteInterfaceToken(LocatorProxy::GetDescriptor());
406     request003.WriteString16(Str8ToStr16("description")); // description
407     request003.WriteDouble(1.0); // minLatitude
408     request003.WriteDouble(2.0); // minLongitude
409     request003.WriteDouble(3.0); // maxLatitude
410     request003.WriteDouble(4.0); // maxLongitude
411     request003.WriteInt32(3); // maxItems
412     request003.WriteInt32(1); // locale object size = 1
413     request003.WriteString16(Str8ToStr16("Language")); // locale.getLanguage()
414     request003.WriteString16(Str8ToStr16("Country")); // locale.getCountry()
415     request003.WriteString16(Str8ToStr16("Variant")); // locale.getVariant()
416     request003.WriteString16(Str8ToStr16("")); // ""
417     EXPECT_EQ(ERRCODE_GEOCODING_FAIL, locatorImpl_->GetAddressByLocationNameV9(request003, geoAddressList003));
418     EXPECT_EQ(true, geoAddressList003.empty());
419     LBSLOGI(LOCATOR, "[LocatorImplTest] locatorImplGetAddressByLocationNameV9001 end");
420 }
421 #endif
422 
423 #ifdef FEATURE_GNSS_SUPPORT
424 HWTEST_F(LocatorImplTest, locatorImplRegisterAndUnregisterCallbackV9001, TestSize.Level1)
425 {
426     GTEST_LOG_(INFO)
427         << "LocatorImplTest, locatorImplRegisterAndUnregisterCallbackV9001, TestSize.Level1";
428     LBSLOGI(LOCATOR, "[LocatorImplTest] locatorImplRegisterAndUnregisterCallbackV9001 begin");
429     auto cachedLocationsCallbackHost =
430         sptr<CachedLocationsCallbackNapi>(new (std::nothrow) CachedLocationsCallbackNapi());
431     EXPECT_NE(nullptr, cachedLocationsCallbackHost);
432     auto cachedCallback = sptr<ICachedLocationsCallback>(cachedLocationsCallbackHost);
433     EXPECT_NE(nullptr, cachedCallback);
434     auto request = std::make_unique<CachedGnssLocationsRequest>();
435     EXPECT_NE(nullptr, request);
436     request->reportingPeriodSec = 10;
437     request->wakeUpCacheQueueFull = true;
438     EXPECT_EQ(ERRCODE_NOT_SUPPORTED, locatorImpl_->RegisterCachedLocationCallbackV9(request, cachedCallback));
439     EXPECT_EQ(ERRCODE_NOT_SUPPORTED, locatorImpl_->UnregisterCachedLocationCallbackV9(cachedCallback));
440     LBSLOGI(LOCATOR, "[LocatorImplTest] locatorImplRegisterAndUnregisterCallbackV9001 end");
441 }
442 #endif
443 
444 #ifdef FEATURE_GNSS_SUPPORT
445 HWTEST_F(LocatorImplTest, locatorImplGnssStatusCallbackV9, TestSize.Level1)
446 {
447     GTEST_LOG_(INFO)
448         << "LocatorImplTest, locatorImplGnssStatusCallbackV9, TestSize.Level1";
449     LBSLOGI(LOCATOR, "[LocatorImplTest] locatorImplGnssStatusCallbackV9 begin");
450     std::unique_ptr<RequestConfig> requestConfig = std::make_unique<RequestConfig>();
451     requestConfig->SetPriority(PRIORITY_ACCURACY);
452     EXPECT_EQ(ERRCODE_SUCCESS, locatorImpl_->StartLocatingV9(requestConfig, callbackStub_)); // startLocating first
453     sleep(1);
454     auto gnssCallbackHost =
455         sptr<GnssStatusCallbackNapi>(new (std::nothrow) GnssStatusCallbackNapi());
456     EXPECT_EQ(ERRCODE_SUCCESS, locatorImpl_->RegisterGnssStatusCallbackV9(gnssCallbackHost->AsObject()));
457     sleep(1);
458     EXPECT_EQ(ERRCODE_SUCCESS, locatorImpl_->UnregisterGnssStatusCallbackV9(gnssCallbackHost->AsObject()));
459     EXPECT_EQ(ERRCODE_SUCCESS, locatorImpl_->StopLocatingV9(callbackStub_)); // after reg, stop locating
460     LBSLOGI(LOCATOR, "[LocatorImplTest] locatorImplGnssStatusCallbackV9 end");
461 }
462 #endif
463 
464 #ifdef FEATURE_GNSS_SUPPORT
465 HWTEST_F(LocatorImplTest, locatorImplNmeaMessageCallbackV9001, TestSize.Level1)
466 {
467     GTEST_LOG_(INFO)
468         << "LocatorImplTest, locatorImplNmeaMessageCallbackV9001, TestSize.Level1";
469     LBSLOGI(LOCATOR, "[LocatorImplTest] locatorImplNmeaMessageCallbackV9001 begin");
470     std::unique_ptr<RequestConfig> requestConfig = std::make_unique<RequestConfig>();
471     requestConfig->SetPriority(PRIORITY_ACCURACY);
472     EXPECT_EQ(ERRCODE_SUCCESS, locatorImpl_->StartLocatingV9(requestConfig, callbackStub_)); // startLocating first
473     sleep(1);
474     auto nmeaCallbackHost =
475         sptr<NmeaMessageCallbackNapi>(new (std::nothrow) NmeaMessageCallbackNapi());
476     EXPECT_EQ(ERRCODE_SUCCESS, locatorImpl_->RegisterNmeaMessageCallbackV9(nmeaCallbackHost->AsObject()));
477     sleep(1);
478     EXPECT_EQ(ERRCODE_SUCCESS, locatorImpl_->UnregisterNmeaMessageCallbackV9(nmeaCallbackHost->AsObject()));
479     EXPECT_EQ(ERRCODE_SUCCESS, locatorImpl_->StopLocatingV9(callbackStub_)); // after reg, stop locating
480     LBSLOGI(LOCATOR, "[LocatorImplTest] locatorImplNmeaMessageCallbackV9001 end");
481 }
482 #endif
483 
484 HWTEST_F(LocatorImplTest, locatorImplResetLocatorProxy001, TestSize.Level1)
485 {
486     GTEST_LOG_(INFO)
487         << "LocatorImplTest, locatorImplResetLocatorProxy001, TestSize.Level1";
488     LBSLOGI(LOCATOR, "[LocatorImplTest] locatorImplResetLocatorProxy001 begin");
489     ASSERT_TRUE(locatorImpl_ != nullptr);
490     wptr<IRemoteObject> remote = nullptr;
491     locatorImpl_->ResetLocatorProxy(remote); // remote is nullptr
492     LBSLOGI(LOCATOR, "[LocatorImplTest] locatorImplResetLocatorProxy001 end");
493 }
494 
495 HWTEST_F(LocatorImplTest, locatorImplOnRemoteDied001, TestSize.Level1)
496 {
497     GTEST_LOG_(INFO)
498         << "LocatorImplTest, locatorImplOnRemoteDied001, TestSize.Level1";
499     LBSLOGI(LOCATOR, "[LocatorImplTest] locatorImplOnRemoteDied001 begin");
500     auto impl = Locator::GetInstance();
501     ASSERT_TRUE(impl != nullptr);
502     auto recipient =
503         sptr<LocatorImpl::LocatorDeathRecipient>(new (std::nothrow) LocatorImpl::LocatorDeathRecipient(*impl));
504     wptr<IRemoteObject> remote;
505     recipient->OnRemoteDied(remote);
506     LBSLOGI(LOCATOR, "[LocatorImplTest] locatorImplOnRemoteDied001 end");
507 }
508 
509 HWTEST_F(LocatorImplTest, locatorImplRegisterLocatingRequiredDataCallback001, TestSize.Level1)
510 {
511     GTEST_LOG_(INFO)
512         << "LocatorImplTest, locatorImplRegisterLocatingRequiredDataCallback001, TestSize.Level1";
513     LBSLOGI(LOCATOR, "[LocatorImplTest] locatorImplRegisterLocatingRequiredDataCallback001 begin");
514     auto singleCallbackHost =
515         sptr<LocatingRequiredDataCallbackNapi>(new (std::nothrow) LocatingRequiredDataCallbackNapi());
516     if (singleCallbackHost) {
517         singleCallbackHost->SetFixNumber(1);
518     }
519     std::unique_ptr<LocatingRequiredDataConfig> requestConfig = std::make_unique<LocatingRequiredDataConfig>();
520     auto callbackPtr = sptr<ILocatingRequiredDataCallback>(singleCallbackHost);
521     locatorImpl_->RegisterLocatingRequiredDataCallback(requestConfig, callbackPtr);
522     locatorImpl_->UnRegisterLocatingRequiredDataCallback(callbackPtr);
523     LBSLOGI(LOCATOR, "[LocatorImplTest] locatorImplRegisterLocatingRequiredDataCallback001 end");
524 }
525 
526 HWTEST_F(LocatorImplTest, locatorImplUnRegisterLocatingRequiredDataCallback001, TestSize.Level1)
527 {
528     GTEST_LOG_(INFO)
529         << "LocatorImplTest, locatorImplUnRegisterLocatingRequiredDataCallback001, TestSize.Level1";
530     LBSLOGI(LOCATOR, "[LocatorImplTest] locatorImplUnRegisterLocatingRequiredDataCallback001 begin");
531     auto singleCallbackHost =
532         sptr<LocatingRequiredDataCallbackNapi>(new (std::nothrow) LocatingRequiredDataCallbackNapi());
533     if (singleCallbackHost) {
534         singleCallbackHost->SetFixNumber(1);
535     }
536     auto callbackPtr = sptr<ILocatingRequiredDataCallback>(singleCallbackHost);
537     locatorImpl_->UnRegisterLocatingRequiredDataCallback(callbackPtr);
538     LBSLOGI(LOCATOR, "[LocatorImplTest] locatorImplUnRegisterLocatingRequiredDataCallback001 end");
539 }
540 
541 HWTEST_F(LocatorImplTest, locatorImplSubscribeLocationError001, TestSize.Level1)
542 {
543     GTEST_LOG_(INFO)
544         << "LocatorImplTest, locatorImplSubscribeLocationError001, TestSize.Level1";
545     LBSLOGI(LOCATOR, "[LocatorImplTest] locatorImplSubscribeLocationError001 begin");
546     auto locatorCallbackHostForTest =
547         sptr<LocatorCallbackNapi>(new (std::nothrow) LocatorCallbackNapi());
548 
549     if (locatorCallbackHostForTest) {
550         locatorCallbackHostForTest->SetFixNumber(1);
551     }
552     sptr<ILocatorCallback> callbackPtr = sptr<ILocatorCallback>(locatorCallbackHostForTest);
553     locatorImpl_->SubscribeLocationError(callbackPtr);
554     locatorImpl_->UnSubscribeLocationError(callbackPtr);
555     LBSLOGI(LOCATOR, "[LocatorImplTest] locatorImplSubscribeLocationError001 end");
556 }
557 
558 HWTEST_F(LocatorImplTest, locatorImplUnSubscribeLocationError001, TestSize.Level1)
559 {
560     GTEST_LOG_(INFO)
561         << "LocatorImplTest, locatorImplUnSubscribeLocationError001, TestSize.Level1";
562     LBSLOGI(LOCATOR, "[LocatorImplTest] locatorImplUnSubscribeLocationError001 begin");
563     auto locatorCallbackHostForTest =
564         sptr<LocatorCallbackNapi>(new (std::nothrow) LocatorCallbackNapi());
565 
566     if (locatorCallbackHostForTest) {
567         locatorCallbackHostForTest->SetFixNumber(1);
568     }
569     sptr<ILocatorCallback> callbackPtr = sptr<ILocatorCallback>(locatorCallbackHostForTest);
570     locatorImpl_->UnSubscribeLocationError(callbackPtr);
571     LBSLOGI(LOCATOR, "[LocatorImplTest] locatorImplUnSubscribeLocationError001 end");
572 }
573 
TestLocationUpdate(const std::unique_ptr<OHOS::Location::Location> & location)574 static void TestLocationUpdate(
575     const std::unique_ptr<OHOS::Location::Location>& location)
576 {
577     LBSLOGI(LOCATOR, "[LocatorImplTest] LocationUpdate enter");
578     EXPECT_EQ(true, location != nullptr);
579 }
580 
TestSvStatusCallback(const std::unique_ptr<OHOS::Location::SatelliteStatus> & statusInfo)581 static void TestSvStatusCallback(
582     const std::unique_ptr<OHOS::Location::SatelliteStatus>& statusInfo)
583 {
584     LBSLOGI(LOCATOR, "[LocatorImplTest] SvStatusCallback begin");
585     EXPECT_EQ(true, statusInfo != nullptr);
586     LBSLOGI(LOCATOR, "[LocatorImplTest] SvStatusCallback end");
587 }
588 
TestNmeaCallback(int64_t timestamp,const std::string msg)589 static void TestNmeaCallback(int64_t timestamp, const std::string msg)
590 {
591     LBSLOGI(LOCATOR, "[LocatorImplTest] NmeaCallback begin");
592     EXPECT_EQ(true, msg != "");
593     LBSLOGI(LOCATOR, "[LocatorImplTest] NmeaCallback end");
594 }
595 
596 HWTEST_F(LocatorImplTest, locatorAgentTest1, TestSize.Level1)
597 {
598     GTEST_LOG_(INFO)
599         << "LocatorImplTest, locatorAgentTest1, TestSize.Level1";
600     LBSLOGI(LOCATOR, "[LocatorImplTest] locatorAgentTest1 begin");
601     auto locatorAgent =
602         LocatorAgentManager::GetInstance();
603     ASSERT_TRUE(locatorAgent != nullptr);
604     static OHOS::Location::LocationCallbackIfaces locationCallback;
605     locationCallback.locationUpdate = TestLocationUpdate;
606     locatorAgent->StartGnssLocating(locationCallback);
607     sleep(1);
608     static OHOS::Location::SvStatusCallbackIfaces svCallback;
609     svCallback.svStatusUpdate = TestSvStatusCallback;
610     locatorAgent->RegisterGnssStatusCallback(svCallback);
611     sleep(1);
612     static OHOS::Location::GnssNmeaCallbackIfaces nmeaCallback;
613     nmeaCallback.nmeaUpdate = TestNmeaCallback;
614     locatorAgent->RegisterNmeaMessageCallback(nmeaCallback);
615     sleep(1);
616 
617     locatorAgent->UnregisterNmeaMessageCallback();
618     locatorAgent->UnregisterGnssStatusCallback();
619     locatorAgent->StopGnssLocating();
620     LBSLOGI(LOCATOR, "[LocatorImplTest] locatorAgentTest1 end");
621 }
622 
623 HWTEST_F(LocatorImplTest, locatorImplCheckEdmPolicy001, TestSize.Level1)
624 {
625     GTEST_LOG_(INFO)
626         << "LocatorImplTest, locatorImplCheckEdmPolicy001, TestSize.Level1";
627     LBSLOGI(LOCATOR, "[LocatorImplTest] locatorImplCheckEdmPolicy001 begin");
628     locatorImpl_->CheckEdmPolicy(true);
629     LBSLOGI(LOCATOR, "[LocatorImplTest] locatorImplCheckEdmPolicy001 end");
630 }
631 
632 HWTEST_F(LocatorImplTest, locatorImplCheckEdmPolicy002, TestSize.Level1)
633 {
634     GTEST_LOG_(INFO)
635         << "LocatorImplTest, locatorImplCheckEdmPolicy002, TestSize.Level1";
636     LBSLOGI(LOCATOR, "[LocatorImplTest] locatorImplCheckEdmPolicy002 begin");
637     locatorImpl_->CheckEdmPolicy(false);
638     LBSLOGI(LOCATOR, "[LocatorImplTest] locatorImplCheckEdmPolicy002 end");
639 }
640 
641 HWTEST_F(LocatorImplTest, CallbackResumeManager001, TestSize.Level1)
642 {
643     GTEST_LOG_(INFO)
644         << "LocatorImplTest, CallbackResumeManager001, TestSize.Level1";
645     LBSLOGI(LOCATOR, "[LocatorImplTest] CallbackResumeManager001 begin");
646     std::shared_ptr<CallbackResumeManager> callbackResumer = std::make_shared<CallbackResumeManager>();
647     callbackResumer->ResumeCallback();
648     LBSLOGI(LOCATOR, "[LocatorImplTest] CallbackResumeManager001 end");
649 }
650 
651 HWTEST_F(LocatorImplTest, OnAddSystemAbility001, TestSize.Level1)
652 {
653     GTEST_LOG_(INFO)
654         << "LocatorImplTest, OnAddSystemAbility001, TestSize.Level1";
655     LBSLOGI(LOCATOR, "[LocatorImplTest] OnAddSystemAbility001 begin");
656     auto saStatusListener = sptr<LocatorSystemAbilityListener>(new LocatorSystemAbilityListener());
657     saStatusListener->OnAddSystemAbility(0, "deviceId");
658     LBSLOGI(LOCATOR, "[LocatorImplTest] OnAddSystemAbility001 end");
659 }
660 
661 HWTEST_F(LocatorImplTest, OnRemoveSystemAbility001, TestSize.Level1)
662 {
663     GTEST_LOG_(INFO)
664         << "LocatorImplTest, OnRemoveSystemAbility001, TestSize.Level1";
665     LBSLOGI(LOCATOR, "[LocatorImplTest] OnRemoveSystemAbility001 begin");
666     auto saStatusListener = sptr<LocatorSystemAbilityListener>(new LocatorSystemAbilityListener());
667     saStatusListener->OnRemoveSystemAbility(0, "deviceId");
668     LBSLOGI(LOCATOR, "[LocatorImplTest] OnRemoveSystemAbility001 end");
669 }
670 
671 HWTEST_F(LocatorImplTest, HasGnssNetworkRequest001, TestSize.Level1)
672 {
673     GTEST_LOG_(INFO)
674         << "LocatorImplTest, HasGnssNetworkRequest001, TestSize.Level1";
675     LBSLOGI(LOCATOR, "[LocatorImplTest] HasGnssNetworkRequest001 begin");
676     locatorImpl_->HasGnssNetworkRequest();
677     LBSLOGI(LOCATOR, "[LocatorImplTest] HasGnssNetworkRequest001 end");
678 }
679 
680 HWTEST_F(LocatorImplTest, locatorImplEnableAbilityForUser, TestSize.Level1)
681 {
682     GTEST_LOG_(INFO)
683         << "LocatorImplTest, locatorImplEnableAbilityForUser, TestSize.Level1";
684     LBSLOGI(LOCATOR, "[LocatorImplTest] locatorImplEnableAbilityForUser begin");
685     EXPECT_EQ(ERRCODE_SUCCESS, locatorImpl_->EnableAbilityForUser(false, DEFAULT_USER));
686     bool isEnabled = false;
687     EXPECT_EQ(ERRCODE_SUCCESS, locatorImpl_->IsLocationEnabledForUser(isEnabled, DEFAULT_USER));
688     EXPECT_EQ(false, isEnabled);
689     EXPECT_EQ(ERRCODE_SUCCESS, locatorImpl_->EnableAbilityForUser(true, DEFAULT_USER));
690     LBSLOGI(LOCATOR, "[LocatorImplTest] locatorImplEnableAbilityForUser end");
691 }
692 }  // namespace Location
693 }  // namespace OHOS
694