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
53 using namespace testing::ext;
54
55 namespace OHOS {
56 namespace Location {
57 const int32_t LOCATION_PERM_NUM = 4;
58 const int INVALID_PRIVACY_TYPE = -1;
59 #ifdef FEATURE_GNSS_SUPPORT
60 const int INVALID_CACHED_SIZE = 0;
61 #endif
62 #ifdef FEATURE_GEOCODE_SUPPORT
63 const double MOCK_LATITUDE = 99.0;
64 const double MOCK_LONGITUDE = 100.0;
65 #endif
SetUp()66 void LocatorImplTest::SetUp()
67 {
68 MockNativePermission();
69 LoadSystemAbility();
70 locatorImpl_ = Locator::GetInstance();
71 ASSERT_TRUE(locatorImpl_ != nullptr);
72 callbackStub_ = new (std::nothrow) LocatorCallbackStub();
73 ASSERT_TRUE(callbackStub_ != nullptr);
74 }
75
TearDown()76 void LocatorImplTest::TearDown()
77 {
78 }
79
LoadSystemAbility()80 void LocatorImplTest::LoadSystemAbility()
81 {
82 DelayedSingleton<LocationSaLoadManager>::GetInstance()->LoadLocationSa(LOCATION_LOCATOR_SA_ID);
83 #ifdef FEATURE_GNSS_SUPPORT
84 DelayedSingleton<LocationSaLoadManager>::GetInstance()->LoadLocationSa(LOCATION_GNSS_SA_ID);
85 #endif
86 #ifdef FEATURE_PASSIVE_SUPPORT
87 DelayedSingleton<LocationSaLoadManager>::GetInstance()->LoadLocationSa(LOCATION_NOPOWER_LOCATING_SA_ID);
88 #endif
89 #ifdef FEATURE_NETWORK_SUPPORT
90 DelayedSingleton<LocationSaLoadManager>::GetInstance()->LoadLocationSa(LOCATION_NETWORK_LOCATING_SA_ID);
91 #endif
92 #ifdef FEATURE_GEOCODE_SUPPORT
93 DelayedSingleton<LocationSaLoadManager>::GetInstance()->LoadLocationSa(LOCATION_GEO_CONVERT_SA_ID);
94 #endif
95 }
96
MockNativePermission()97 void LocatorImplTest::MockNativePermission()
98 {
99 const char *perms[] = {
100 ACCESS_LOCATION.c_str(), ACCESS_APPROXIMATELY_LOCATION.c_str(),
101 ACCESS_BACKGROUND_LOCATION.c_str(), MANAGE_SECURE_SETTINGS.c_str(),
102 };
103 NativeTokenInfoParams infoInstance = {
104 .dcapsNum = 0,
105 .permsNum = LOCATION_PERM_NUM,
106 .aclsNum = 0,
107 .dcaps = nullptr,
108 .perms = perms,
109 .acls = nullptr,
110 .processName = "LocatorImplTest",
111 .aplStr = "system_basic",
112 };
113 tokenId_ = GetAccessTokenId(&infoInstance);
114 SetSelfTokenID(tokenId_);
115 Security::AccessToken::AccessTokenKit::ReloadNativeTokenInfo();
116 }
117
118 #ifdef FEATURE_GEOCODE_SUPPORT
SetGeocodingMockInfo()119 std::vector<std::shared_ptr<GeocodingMockInfo>> LocatorImplTest::SetGeocodingMockInfo()
120 {
121 std::vector<std::shared_ptr<GeocodingMockInfo>> geoMockInfos;
122 std::shared_ptr<GeocodingMockInfo> geocodingMockInfo =
123 std::make_shared<GeocodingMockInfo>();
124 MessageParcel parcel;
125 parcel.WriteString16(Str8ToStr16("locale"));
126 parcel.WriteDouble(MOCK_LATITUDE); // latitude
127 parcel.WriteDouble(MOCK_LONGITUDE); // longitude
128 parcel.WriteInt32(1);
129 parcel.WriteString("localeLanguage");
130 parcel.WriteString("localeCountry");
131 parcel.WriteInt32(1); // size
132 parcel.WriteInt32(0); // line
133 parcel.WriteString("line");
134 parcel.WriteString("placeName");
135 parcel.WriteString("administrativeArea");
136 parcel.WriteString("subAdministrativeArea");
137 parcel.WriteString("locality");
138 parcel.WriteString("subLocality");
139 parcel.WriteString("roadName");
140 parcel.WriteString("subRoadName");
141 parcel.WriteString("premises");
142 parcel.WriteString("postalCode");
143 parcel.WriteString("countryCode");
144 parcel.WriteString("countryName");
145 parcel.WriteInt32(1); // hasLatitude
146 parcel.WriteDouble(MOCK_LATITUDE); // latitude
147 parcel.WriteInt32(1); // hasLongitude
148 parcel.WriteDouble(MOCK_LONGITUDE); // longitude
149 parcel.WriteString("phoneNumber");
150 parcel.WriteString("addressUrl");
151 parcel.WriteBool(true);
152 geocodingMockInfo->ReadFromParcel(parcel);
153 geoMockInfos.emplace_back(std::move(geocodingMockInfo));
154 return geoMockInfos;
155 }
156 #endif
157
158 HWTEST_F(LocatorImplTest, locatorImplEnableAbilityV9001, TestSize.Level1)
159 {
160 GTEST_LOG_(INFO)
161 << "LocatorImplTest, locatorImplEnableAbilityV9001, TestSize.Level1";
162 LBSLOGI(LOCATOR, "[LocatorImplTest] locatorImplEnableAbilityV9001 begin");
163 auto switchCallbackHost =
164 sptr<LocationSwitchCallbackHost>(new (std::nothrow) LocationSwitchCallbackHost());
165 EXPECT_NE(nullptr, switchCallbackHost);
166 EXPECT_EQ(ERRCODE_SUCCESS, locatorImpl_->RegisterSwitchCallbackV9(switchCallbackHost->AsObject()));
167 sleep(1);
168
169 EXPECT_EQ(ERRCODE_SUCCESS, locatorImpl_->EnableAbilityV9(false));
170 bool isEnabled = false;
171 EXPECT_EQ(ERRCODE_SUCCESS, locatorImpl_->IsLocationEnabledV9(isEnabled));
172 EXPECT_EQ(false, isEnabled);
173
174 EXPECT_EQ(ERRCODE_SUCCESS, locatorImpl_->UnregisterSwitchCallbackV9(switchCallbackHost->AsObject()));
175 LBSLOGI(LOCATOR, "[LocatorImplTest] locatorImplEnableAbilityV9001 end");
176 }
177
178 HWTEST_F(LocatorImplTest, locatorImplEnableAbilityV9002, TestSize.Level1)
179 {
180 GTEST_LOG_(INFO)
181 << "LocatorImplTest, locatorImplEnableAbilityV9002, TestSize.Level1";
182 LBSLOGI(LOCATOR, "[LocatorImplTest] locatorImplEnableAbilityV9002 begin");
183 auto switchCallbackHost =
184 sptr<LocationSwitchCallbackHost>(new (std::nothrow) LocationSwitchCallbackHost());
185 EXPECT_NE(nullptr, switchCallbackHost);
186 EXPECT_EQ(ERRCODE_SUCCESS, locatorImpl_->RegisterSwitchCallbackV9(switchCallbackHost->AsObject()));
187 sleep(1);
188
189 EXPECT_EQ(ERRCODE_SUCCESS, locatorImpl_->EnableAbilityV9(true));
190 bool isEnabled = false;
191 EXPECT_EQ(ERRCODE_SUCCESS, locatorImpl_->IsLocationEnabledV9(isEnabled));
192 EXPECT_EQ(true, isEnabled);
193
194 EXPECT_EQ(ERRCODE_SUCCESS, locatorImpl_->UnregisterSwitchCallbackV9(switchCallbackHost->AsObject()));
195 LBSLOGI(LOCATOR, "[LocatorImplTest] locatorImplEnableAbilityV9002 end");
196 }
197
198 HWTEST_F(LocatorImplTest, locatorImplGetCachedLocationV9, TestSize.Level1)
199 {
200 GTEST_LOG_(INFO)
201 << "LocatorImplTest, locatorImplGetCachedLocationV9, TestSize.Level1";
202 LBSLOGI(LOCATOR, "[LocatorImplTest] locatorImplGetCachedLocationV9 begin");
203
204 EXPECT_EQ(ERRCODE_SUCCESS, locatorImpl_->EnableLocationMockV9()); // mock switch on
205
206 std::unique_ptr<RequestConfig> requestConfig = std::make_unique<RequestConfig>();
207 requestConfig->SetPriority(PRIORITY_ACCURACY);
208 EXPECT_EQ(ERRCODE_SUCCESS, locatorImpl_->StartLocatingV9(requestConfig, callbackStub_)); // startLocating first
209 sleep(1);
210
211 int timeInterval = 0;
212 std::vector<std::shared_ptr<Location>> locations;
213 Parcel parcel;
214 parcel.WriteDouble(10.6); // latitude
215 parcel.WriteDouble(10.5); // longitude
216 parcel.WriteDouble(10.4); // altitude
217 parcel.WriteDouble(1.0); // accuracy
218 parcel.WriteDouble(5.0); // speed
219 parcel.WriteDouble(10); // direction
220 parcel.WriteInt64(1611000000); // timestamp
221 parcel.WriteInt64(1611000000); // time since boot
222 parcel.WriteString16(u"additions"); // additions
223 parcel.WriteInt64(1); // additionSize
224 parcel.WriteBool(true); // isFromMock is false
225 parcel.WriteInt32(1); // source type
226 parcel.WriteInt32(0); // floor no.
227 parcel.WriteDouble(1000.0); // floor acc
228 locations.push_back(Location::UnmarshallingShared(parcel));
229 EXPECT_EQ(ERRCODE_SUCCESS, locatorImpl_->SetMockedLocationsV9(timeInterval, locations)); // set fake locations
230 sleep(1);
231
232 std::unique_ptr<Location> loc = std::make_unique<Location>();
233 EXPECT_EQ(ERRCODE_SUCCESS, locatorImpl_->GetCachedLocationV9(loc)); // get last location
234 ASSERT_TRUE(loc != nullptr);
235 EXPECT_EQ(10.6, loc->GetLatitude());
236 EXPECT_EQ(10.5, loc->GetLongitude());
237
238 EXPECT_EQ(ERRCODE_SUCCESS, locatorImpl_->StopLocatingV9(callbackStub_));
239
240 EXPECT_EQ(ERRCODE_SUCCESS, locatorImpl_->DisableLocationMockV9());
241 LBSLOGI(LOCATOR, "[LocatorImplTest] locatorImplGetCachedLocationV9 end");
242 }
243
244 HWTEST_F(LocatorImplTest, locatorImplDisableLocationMockV9, TestSize.Level1)
245 {
246 GTEST_LOG_(INFO)
247 << "LocatorImplTest, locatorImplDisableLocationMockV9, TestSize.Level1";
248 LBSLOGI(LOCATOR, "[LocatorImplTest] locatorImplDisableLocationMockV9 begin");
249 EXPECT_EQ(ERRCODE_SUCCESS, Locator::GetInstance()->DisableLocationMockV9());
250 LBSLOGI(LOCATOR, "[LocatorImplTest] locatorImplDisableLocationMockV9 end");
251 }
252
253 HWTEST_F(LocatorImplTest, locatorImplPrivacyStateV9001, TestSize.Level1)
254 {
255 GTEST_LOG_(INFO)
256 << "LocatorImplTest, locatorImplPrivacyStateV9001, TestSize.Level1";
257 LBSLOGI(LOCATOR, "[LocatorImplTest] locatorImplPrivacyStateV9001 begin");
258 bool isConfirmed = false;
259 EXPECT_EQ(ERRCODE_INVALID_PARAM, locatorImpl_->SetLocationPrivacyConfirmStatusV9(INVALID_PRIVACY_TYPE, true));
260 EXPECT_EQ(ERRCODE_INVALID_PARAM, locatorImpl_->IsLocationPrivacyConfirmedV9(INVALID_PRIVACY_TYPE, isConfirmed));
261 EXPECT_EQ(false, isConfirmed);
262 LBSLOGI(LOCATOR, "[LocatorImplTest] locatorImplPrivacyStateV9001 end");
263 }
264
265 #ifdef FEATURE_GNSS_SUPPORT
266 HWTEST_F(LocatorImplTest, locatorImplGetCachedGnssLocationsSizeV9, TestSize.Level1)
267 {
268 GTEST_LOG_(INFO)
269 << "LocatorImplTest, locatorImplGetCachedGnssLocationsSizeV9, TestSize.Level1";
270 LBSLOGI(LOCATOR, "[LocatorImplTest] locatorImplGetCachedGnssLocationsSizeV9 begin");
271 int size = INVALID_CACHED_SIZE;
272 EXPECT_EQ(ERRCODE_NOT_SUPPORTED, locatorImpl_->GetCachedGnssLocationsSizeV9(size));
273 EXPECT_EQ(INVALID_CACHED_SIZE, size);
274 LBSLOGI(LOCATOR, "[LocatorImplTest] locatorImplGetCachedGnssLocationsSizeV9 end");
275 }
276 #endif
277
278 #ifdef FEATURE_GNSS_SUPPORT
279 HWTEST_F(LocatorImplTest, locatorImplFlushCachedGnssLocationsV9, TestSize.Level1)
280 {
281 GTEST_LOG_(INFO)
282 << "LocatorImplTest, locatorImplFlushCachedGnssLocationsV9, TestSize.Level1";
283 LBSLOGI(LOCATOR, "[LocatorImplTest] locatorImplFlushCachedGnssLocationsV9 begin");
284 EXPECT_EQ(ERRCODE_NOT_SUPPORTED, locatorImpl_->FlushCachedGnssLocationsV9());
285 LBSLOGI(LOCATOR, "[LocatorImplTest] locatorImplFlushCachedGnssLocationsV9 end");
286 }
287 #endif
288
289 #ifdef FEATURE_GNSS_SUPPORT
290 HWTEST_F(LocatorImplTest, locatorImplSendCommandV9, TestSize.Level1)
291 {
292 GTEST_LOG_(INFO)
293 << "LocatorImplTest, locatorImplSendCommandV9, TestSize.Level1";
294 LBSLOGI(LOCATOR, "[LocatorImplTest] locatorImplSendCommandV9 begin");
295 std::unique_ptr<LocationCommand> command = std::make_unique<LocationCommand>();
296 command->scenario = SCENE_NAVIGATION;
297 command->command = "cmd";
298 EXPECT_EQ(ERRCODE_SUCCESS, locatorImpl_->SendCommandV9(command));
299 LBSLOGI(LOCATOR, "[LocatorImplTest] locatorImplSendCommandV9 end");
300 }
301 #endif
302
303 #ifdef FEATURE_GNSS_SUPPORT
304 HWTEST_F(LocatorImplTest, locatorImplRequestFenceV9, TestSize.Level1)
305 {
306 GTEST_LOG_(INFO)
307 << "LocatorImplTest, locatorImplRequestFenceV9, TestSize.Level1";
308 LBSLOGI(LOCATOR, "[LocatorImplTest] locatorImplRequestFenceV9 begin");
309 std::unique_ptr<GeofenceRequest> fenceRequest = std::make_unique<GeofenceRequest>();
310 fenceRequest->scenario = SCENE_NAVIGATION;
311 GeoFence geofence;
312 geofence.latitude = 1.0;
313 geofence.longitude = 2.0;
314 geofence.radius = 3.0;
315 geofence.expiration = 4.0;
316 fenceRequest->geofence = geofence;
317 EXPECT_EQ(ERRCODE_NOT_SUPPORTED, locatorImpl_->AddFenceV9(fenceRequest));
318 EXPECT_EQ(ERRCODE_NOT_SUPPORTED, locatorImpl_->RemoveFenceV9(fenceRequest));
319 LBSLOGI(LOCATOR, "[LocatorImplTest] locatorImplRequestFenceV9 end");
320 }
321 #endif
322
323 HWTEST_F(LocatorImplTest, locatorImplGetIsoCountryCodeV9, TestSize.Level1)
324 {
325 GTEST_LOG_(INFO)
326 << "LocatorImplTest, locatorImplGetIsoCountryCodeV9, TestSize.Level1";
327 LBSLOGI(LOCATOR, "[LocatorImplTest] locatorImplGetIsoCountryCodeV9 begin");
328
329 auto countryCodeCallbackHost =
330 sptr<CountryCodeCallbackHost>(new (std::nothrow) CountryCodeCallbackHost());
331 EXPECT_NE(nullptr, countryCodeCallbackHost);
332 EXPECT_EQ(ERRCODE_SUCCESS, locatorImpl_->RegisterCountryCodeCallbackV9(countryCodeCallbackHost->AsObject()));
333 sleep(1);
334
335 std::shared_ptr<CountryCode> countryCode = std::make_shared<CountryCode>();
336 EXPECT_EQ(ERRCODE_SUCCESS, locatorImpl_->GetIsoCountryCodeV9(countryCode));
337 ASSERT_TRUE(countryCode != nullptr);
338 LBSLOGI(LOCATOR, "countrycode : %{public}s", countryCode->ToString().c_str());
339 sleep(1);
340 EXPECT_EQ(ERRCODE_SUCCESS, locatorImpl_->UnregisterCountryCodeCallbackV9(countryCodeCallbackHost->AsObject()));
341 LBSLOGI(LOCATOR, "[LocatorImplTest] locatorImplGetIsoCountryCodeV9 end");
342 }
343
344 HWTEST_F(LocatorImplTest, locatorImplProxyUidForFreezeV9, TestSize.Level1)
345 {
346 GTEST_LOG_(INFO)
347 << "LocatorImplTest, locatorImplProxyUidForFreezeV9, TestSize.Level1";
348 LBSLOGI(LOCATOR, "[LocatorImplTest] locatorImplProxyUidForFreezeV9 begin");
349 EXPECT_EQ(ERRCODE_PERMISSION_DENIED, locatorImpl_->ProxyUidForFreezeV9(SYSTEM_UID, false));
350 LBSLOGI(LOCATOR, "[LocatorImplTest] locatorImplProxyUidForFreezeV9 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, locatorImplSetResumer001, TestSize.Level1)
519 {
520 GTEST_LOG_(INFO)
521 << "LocatorImplTest, locatorImplSetResumer001, TestSize.Level1";
522 LBSLOGI(LOCATOR, "[LocatorImplTest] locatorImplSetResumer001 begin");
523 std::shared_ptr<MockCallbackResumeManager> callbackResumer = std::make_shared<MockCallbackResumeManager>();
524 locatorImpl_->SetResumer(callbackResumer); //resumer isn't nullptr
525 EXPECT_NE(nullptr, locatorImpl_->resumer_);
526
527 callbackResumer = nullptr;
528 locatorImpl_->SetResumer(callbackResumer); //resumer is nullptr
529 LBSLOGI(LOCATOR, "[LocatorImplTest] locatorImplSetResumer001 end");
530 }
531
532 HWTEST_F(LocatorImplTest, locatorImplOnRemoteDied001, TestSize.Level1)
533 {
534 GTEST_LOG_(INFO)
535 << "LocatorImplTest, locatorImplOnRemoteDied001, TestSize.Level1";
536 LBSLOGI(LOCATOR, "[LocatorImplTest] locatorImplOnRemoteDied001 begin");
537 auto impl = Locator::GetInstance();
538 ASSERT_TRUE(impl != nullptr);
539 auto recipient =
540 sptr<LocatorImpl::LocatorDeathRecipient>(new (std::nothrow) LocatorImpl::LocatorDeathRecipient(*impl));
541 wptr<IRemoteObject> remote;
542 recipient->OnRemoteDied(remote);
543 LBSLOGI(LOCATOR, "[LocatorImplTest] locatorImplOnRemoteDied001 end");
544 }
545
546 HWTEST_F(LocatorImplTest, locatorImplRegisterLocatingRequiredDataCallback001, TestSize.Level1)
547 {
548 GTEST_LOG_(INFO)
549 << "LocatorImplTest, locatorImplRegisterLocatingRequiredDataCallback001, TestSize.Level1";
550 LBSLOGI(LOCATOR, "[LocatorImplTest] locatorImplRegisterLocatingRequiredDataCallback001 begin");
551 auto singleCallbackHost =
552 sptr<LocatingRequiredDataCallbackHost>(new (std::nothrow) LocatingRequiredDataCallbackHost());
553 if (singleCallbackHost) {
554 singleCallbackHost->SetFixNumber(1);
555 }
556 std::unique_ptr<LocatingRequiredDataConfig> requestConfig = std::make_unique<LocatingRequiredDataConfig>();
557 auto callbackPtr = sptr<ILocatingRequiredDataCallback>(singleCallbackHost);
558 locatorImpl_->RegisterLocatingRequiredDataCallback(requestConfig, callbackPtr);
559 LBSLOGI(LOCATOR, "[LocatorImplTest] locatorImplRegisterLocatingRequiredDataCallback001 end");
560 }
561
562 HWTEST_F(LocatorImplTest, locatorImplUnRegisterLocatingRequiredDataCallback001, TestSize.Level1)
563 {
564 GTEST_LOG_(INFO)
565 << "LocatorImplTest, locatorImplUnRegisterLocatingRequiredDataCallback001, TestSize.Level1";
566 LBSLOGI(LOCATOR, "[LocatorImplTest] locatorImplUnRegisterLocatingRequiredDataCallback001 begin");
567 auto singleCallbackHost =
568 sptr<LocatingRequiredDataCallbackHost>(new (std::nothrow) LocatingRequiredDataCallbackHost());
569 if (singleCallbackHost) {
570 singleCallbackHost->SetFixNumber(1);
571 }
572 auto callbackPtr = sptr<ILocatingRequiredDataCallback>(singleCallbackHost);
573 locatorImpl_->UnRegisterLocatingRequiredDataCallback(callbackPtr);
574 LBSLOGI(LOCATOR, "[LocatorImplTest] locatorImplUnRegisterLocatingRequiredDataCallback001 end");
575 }
576
TestLocationUpdate(const std::unique_ptr<OHOS::Location::Location> & location)577 static void TestLocationUpdate(
578 const std::unique_ptr<OHOS::Location::Location>& location)
579 {
580 LBSLOGI(LOCATOR, "[LocatorImplTest] LocationUpdate enter");
581 EXPECT_EQ(true, location != nullptr);
582 }
583
TestSvStatusCallback(const std::unique_ptr<OHOS::Location::SatelliteStatus> & statusInfo)584 static void TestSvStatusCallback(
585 const std::unique_ptr<OHOS::Location::SatelliteStatus>& statusInfo)
586 {
587 LBSLOGI(LOCATOR, "[LocatorImplTest] SvStatusCallback begin");
588 EXPECT_EQ(true, statusInfo != nullptr);
589 LBSLOGI(LOCATOR, "[LocatorImplTest] SvStatusCallback end");
590 }
591
TestNmeaCallback(int64_t timestamp,const std::string msg)592 static void TestNmeaCallback(int64_t timestamp, const std::string msg)
593 {
594 LBSLOGI(LOCATOR, "[LocatorImplTest] NmeaCallback begin");
595 EXPECT_EQ(true, msg != "");
596 LBSLOGI(LOCATOR, "[LocatorImplTest] NmeaCallback end");
597 }
598
599 HWTEST_F(LocatorImplTest, locatorAgentTest1, TestSize.Level1)
600 {
601 GTEST_LOG_(INFO)
602 << "LocatorImplTest, locatorAgentTest1, TestSize.Level1";
603 LBSLOGI(LOCATOR, "[LocatorImplTest] locatorAgentTest1 begin");
604 auto locatorAgent =
605 DelayedSingleton<LocatorAgentManager>::GetInstance();
606 ASSERT_TRUE(locatorAgent != nullptr);
607 static OHOS::Location::LocationCallbackIfaces locationCallback;
608 locationCallback.locationUpdate = TestLocationUpdate;
609 locatorAgent->StartGnssLocating(locationCallback);
610 sleep(1);
611 static OHOS::Location::SvStatusCallbackIfaces svCallback;
612 svCallback.svStatusUpdate = TestSvStatusCallback;
613 locatorAgent->RegisterGnssStatusCallback(svCallback);
614 sleep(1);
615 static OHOS::Location::GnssNmeaCallbackIfaces nmeaCallback;
616 nmeaCallback.nmeaUpdate = TestNmeaCallback;
617 locatorAgent->RegisterNmeaMessageCallback(nmeaCallback);
618 sleep(1);
619
620 locatorAgent->UnregisterNmeaMessageCallback();
621 locatorAgent->UnregisterGnssStatusCallback();
622 locatorAgent->StopGnssLocating();
623 LBSLOGI(LOCATOR, "[LocatorImplTest] locatorAgentTest1 end");
624 }
625
626 HWTEST_F(LocatorImplTest, locatorImplCheckEdmPolicy001, TestSize.Level1)
627 {
628 GTEST_LOG_(INFO)
629 << "LocatorImplTest, locatorImplCheckEdmPolicy001, TestSize.Level1";
630 LBSLOGI(LOCATOR, "[LocatorImplTest] locatorImplCheckEdmPolicy001 begin");
631 locatorImpl_->CheckEdmPolicy(true);
632 LBSLOGI(LOCATOR, "[LocatorImplTest] locatorImplCheckEdmPolicy001 end");
633 }
634
635 HWTEST_F(LocatorImplTest, locatorImplCheckEdmPolicy002, TestSize.Level1)
636 {
637 GTEST_LOG_(INFO)
638 << "LocatorImplTest, locatorImplCheckEdmPolicy002, TestSize.Level1";
639 LBSLOGI(LOCATOR, "[LocatorImplTest] locatorImplCheckEdmPolicy002 begin");
640 locatorImpl_->CheckEdmPolicy(false);
641 LBSLOGI(LOCATOR, "[LocatorImplTest] locatorImplCheckEdmPolicy002 end");
642 }
643 } // namespace Location
644 } // namespace OHOS
645