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