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