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_service_test.h"
17
18 #include <cstdlib>
19 #include <singleton.h>
20
21 #include "accesstoken_kit.h"
22 #include "bundle_mgr_interface.h"
23 #include "bundle_mgr_proxy.h"
24 #include "if_system_ability_manager.h"
25 #include "ipc_skeleton.h"
26 #include "iservice_registry.h"
27 #include "nativetoken_kit.h"
28 #include "system_ability_definition.h"
29 #include "token_setproc.h"
30
31 #include "app_identity.h"
32 #ifdef FEATURE_GNSS_SUPPORT
33 #include "cached_locations_callback_host.h"
34 #endif
35 #include "common_utils.h"
36 #include "constant_definition.h"
37 #include "country_code.h"
38 #include "country_code_callback_host.h"
39 #ifdef FEATURE_GEOCODE_SUPPORT
40 #include "geo_address.h"
41 #endif
42 #ifdef FEATURE_GNSS_SUPPORT
43 #include "gnss_status_callback_host.h"
44 #endif
45 #include "i_locator.h"
46 #include "location.h"
47 #include "location_log.h"
48 #include "location_sa_load_manager.h"
49 #include "location_switch_callback_host.h"
50 #include "locator.h"
51 #include "locator_callback_host.h"
52 #include "locator_callback_proxy.h"
53 #include "locator_skeleton.h"
54 #ifdef FEATURE_GNSS_SUPPORT
55 #include "nmea_message_callback_host.h"
56 #endif
57
58 using namespace testing::ext;
59
60 namespace OHOS {
61 namespace Location {
62 const int32_t LOCATION_PERM_NUM = 4;
63 #ifdef FEATURE_GEOCODE_SUPPORT
64 const double MOCK_LATITUDE = 99.0;
65 const double MOCK_LONGITUDE = 100.0;
66 #endif
67 const int REQUEST_MAX_NUM = 3;
68 const int UNKNOWN_SERVICE_ID = -1;
69 const std::string ARGS_HELP = "-h";
SetUp()70 void LocatorServiceTest::SetUp()
71 {
72 /*
73 * @tc.setup: Get system ability's pointer and get sa proxy object.
74 */
75 LoadSystemAbility();
76 MockNativePermission();
77 sptr<ISystemAbilityManager> systemAbilityManager =
78 SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
79 EXPECT_NE(nullptr, systemAbilityManager);
80 sptr<IRemoteObject> object = systemAbilityManager->GetSystemAbility(LOCATION_LOCATOR_SA_ID);
81 EXPECT_NE(nullptr, object);
82 proxy_ = new (std::nothrow) LocatorProxy(object);
83 EXPECT_NE(nullptr, proxy_);
84 callbackStub_ = new (std::nothrow) LocatorCallbackStub();
85 EXPECT_NE(nullptr, callbackStub_);
86 backgroundProxy_ = DelayedSingleton<LocatorBackgroundProxy>::GetInstance();
87 EXPECT_NE(nullptr, backgroundProxy_);
88 request_ = std::make_shared<Request>();
89 EXPECT_NE(nullptr, request_);
90 requestManager_ = DelayedSingleton<RequestManager>::GetInstance();
91 EXPECT_NE(nullptr, requestManager_);
92 request_->SetLocatorCallBack(callbackStub_);
93 request_->SetUid(SYSTEM_UID);
94 request_->SetPid(getpid());
95 SetStartUpConfirmed(true);
96 ChangedLocationMode(true);
97 }
98
TearDown()99 void LocatorServiceTest::TearDown()
100 {
101 /*
102 * @tc.teardown: release memory.
103 */
104 proxy_ = nullptr;
105 callbackStub_ = nullptr;
106 backgroundProxy_ = nullptr;
107 DelayedSingleton<LocatorBackgroundProxy>::DestroyInstance();
108 requestManager_ = nullptr;
109 DelayedSingleton<RequestManager>::DestroyInstance();
110 }
111
LoadSystemAbility()112 void LocatorServiceTest::LoadSystemAbility()
113 {
114 DelayedSingleton<LocationSaLoadManager>::GetInstance()->LoadLocationSa(LOCATION_LOCATOR_SA_ID);
115 #ifdef FEATURE_GNSS_SUPPORT
116 DelayedSingleton<LocationSaLoadManager>::GetInstance()->LoadLocationSa(LOCATION_GNSS_SA_ID);
117 #endif
118 #ifdef FEATURE_PASSIVE_SUPPORT
119 DelayedSingleton<LocationSaLoadManager>::GetInstance()->LoadLocationSa(LOCATION_NOPOWER_LOCATING_SA_ID);
120 #endif
121 #ifdef FEATURE_NETWORK_SUPPORT
122 DelayedSingleton<LocationSaLoadManager>::GetInstance()->LoadLocationSa(LOCATION_NETWORK_LOCATING_SA_ID);
123 #endif
124 #ifdef FEATURE_GEOCODE_SUPPORT
125 DelayedSingleton<LocationSaLoadManager>::GetInstance()->LoadLocationSa(LOCATION_GEO_CONVERT_SA_ID);
126 #endif
127 }
128
MockNativePermission()129 void LocatorServiceTest::MockNativePermission()
130 {
131 const char *perms[] = {
132 ACCESS_LOCATION.c_str(), ACCESS_APPROXIMATELY_LOCATION.c_str(),
133 ACCESS_BACKGROUND_LOCATION.c_str(), MANAGE_SECURE_SETTINGS.c_str(),
134 };
135 NativeTokenInfoParams infoInstance = {
136 .dcapsNum = 0,
137 .permsNum = LOCATION_PERM_NUM,
138 .aclsNum = 0,
139 .dcaps = nullptr,
140 .perms = perms,
141 .acls = nullptr,
142 .processName = "LocatorTest",
143 .aplStr = "system_basic",
144 };
145 tokenId_ = GetAccessTokenId(&infoInstance);
146 SetSelfTokenID(tokenId_);
147 Security::AccessToken::AccessTokenKit::ReloadNativeTokenInfo();
148 }
149
SetStartUpConfirmed(bool isAuthorized)150 void LocatorServiceTest::SetStartUpConfirmed(bool isAuthorized)
151 {
152 std::string value = isAuthorized ? "1" : "0";
153 std::string executeCmd = "settings put SECURE high_accuracy_startup_comfirm " + value;
154 system(executeCmd.c_str());
155 }
156
ChangedLocationMode(bool isEnable)157 void LocatorServiceTest::ChangedLocationMode(bool isEnable)
158 {
159 std::string value = isEnable ? "3" : "0";
160 std::string executeCmd = "settings put SECURE location_mode " + value;
161 system(executeCmd.c_str());
162 }
163
StartAndStopForLocating(MessageParcel & data)164 bool LocatorServiceTest::StartAndStopForLocating(MessageParcel& data)
165 {
166 auto locatorImpl = Locator::GetInstance();
167 std::unique_ptr<RequestConfig> requestConfig = RequestConfig::Unmarshalling(data);
168 locatorImpl->StartLocating(requestConfig, callbackStub_);
169 locatorImpl->StopLocating(callbackStub_);
170 return true;
171 }
172
173 #ifdef FEATURE_GEOCODE_SUPPORT
SetGeocodingMockInfo()174 std::vector<std::shared_ptr<GeocodingMockInfo>> LocatorServiceTest::SetGeocodingMockInfo()
175 {
176 std::vector<std::shared_ptr<GeocodingMockInfo>> geoMockInfos;
177 std::shared_ptr<GeocodingMockInfo> geocodingMockInfo =
178 std::make_shared<GeocodingMockInfo>();
179 MessageParcel parcel;
180 parcel.WriteString16(Str8ToStr16("locale"));
181 parcel.WriteDouble(MOCK_LATITUDE); // latitude
182 parcel.WriteDouble(MOCK_LONGITUDE); // longitude
183 parcel.WriteInt32(1);
184 parcel.WriteString("localeLanguage");
185 parcel.WriteString("localeCountry");
186 parcel.WriteInt32(1); // size
187 parcel.WriteInt32(0); // line
188 parcel.WriteString("line");
189 parcel.WriteString("placeName");
190 parcel.WriteString("administrativeArea");
191 parcel.WriteString("subAdministrativeArea");
192 parcel.WriteString("locality");
193 parcel.WriteString("subLocality");
194 parcel.WriteString("roadName");
195 parcel.WriteString("subRoadName");
196 parcel.WriteString("premises");
197 parcel.WriteString("postalCode");
198 parcel.WriteString("countryCode");
199 parcel.WriteString("countryName");
200 parcel.WriteInt32(1); // hasLatitude
201 parcel.WriteDouble(MOCK_LATITUDE); // latitude
202 parcel.WriteInt32(1); // hasLongitude
203 parcel.WriteDouble(MOCK_LONGITUDE); // longitude
204 parcel.WriteString("phoneNumber");
205 parcel.WriteString("addressUrl");
206 parcel.WriteBool(true);
207 geocodingMockInfo->ReadFromParcel(parcel);
208 geoMockInfos.emplace_back(std::move(geocodingMockInfo));
209 return geoMockInfos;
210 }
211 #endif
212
213 /*
214 * @tc.name: CheckSwitchState001
215 * @tc.desc: Check location switch state expect success
216 * @tc.type: FUNC
217 */
218 HWTEST_F(LocatorServiceTest, CheckSwitchState001, TestSize.Level1)
219 {
220 /*
221 * @tc.steps: step1. Call system ability and check switch state whether available.
222 * @tc.expected: step1. get switch state is available.
223 */
224 GTEST_LOG_(INFO)
225 << "LocatorServiceTest, CheckSwitchState001, TestSize.Level1";
226 LBSLOGI(LOCATOR, "[LocatorServiceTest] CheckSwitchState001 begin");
227 int result = proxy_->GetSwitchState();
228 EXPECT_EQ(true, (result == ENABLED || result == DISABLED));
229 LBSLOGI(LOCATOR, "[LocatorServiceTest] CheckSwitchState001 end");
230 }
231
232 /*
233 * @tc.name: CheckLocatingForScenario001
234 * @tc.desc: Check start locating based on scenario expect success
235 * @tc.type: FUNC
236 */
237 HWTEST_F(LocatorServiceTest, CheckLocatingForScenario001, TestSize.Level1)
238 {
239 /*
240 * @tc.steps: step1. Call system ability and start locating for SCENE_VEHICLE_NAVIGATION.
241 * @tc.expected: step1. get reply state is successful.
242 */
243 GTEST_LOG_(INFO)
244 << "LocatorServiceTest, CheckLocatingForScenario001, TestSize.Level1";
245 LBSLOGI(LOCATOR, "[LocatorServiceTest] CheckLocatingForScenario001 begin");
246 std::unique_ptr<RequestConfig> requestConfig = std::make_unique<RequestConfig>();
247 requestConfig->SetScenario(SCENE_NAVIGATION);
248 MessageParcel data;
249 requestConfig->Marshalling(data);
250 bool ret = StartAndStopForLocating(data);
251 EXPECT_EQ(true, ret);
252 LBSLOGI(LOCATOR, "[LocatorServiceTest] CheckLocatingForScenario001 end");
253 }
254
255 /*
256 * @tc.name: CheckLocatingForConfig001
257 * @tc.desc: Check start locating based on config expect success
258 * @tc.type: FUNC
259 */
260 HWTEST_F(LocatorServiceTest, CheckLocatingForConfig001, TestSize.Level1)
261 {
262 /*
263 * @tc.steps: step1. Call system ability and start locating for HIGHT_ACCURACY/HIGHT_POWER_COST.
264 * @tc.expected: step1. get reply state is successful.
265 */
266 GTEST_LOG_(INFO)
267 << "LocatorServiceTest, CheckLocatingForConfig001, TestSize.Level1";
268 LBSLOGI(LOCATOR, "[LocatorServiceTest] CheckLocatingForConfig001 begin");
269 std::unique_ptr<RequestConfig> requestConfig = std::make_unique<RequestConfig>();
270 requestConfig->SetPriority(PRIORITY_ACCURACY);
271 MessageParcel data;
272 requestConfig->Marshalling(data);
273 bool ret = StartAndStopForLocating(data);
274 EXPECT_EQ(true, ret);
275 LBSLOGI(LOCATOR, "[LocatorServiceTest] CheckLocatingForConfig001 end");
276 }
277
278 /*
279 * @tc.name: CheckStopLocating001
280 * @tc.desc: Check stop locating with illegal param and expect fail
281 * @tc.type: FUNC
282 */
283 HWTEST_F(LocatorServiceTest, CheckStopLocating001, TestSize.Level1)
284 {
285 /*
286 * @tc.steps: step1. Call system ability and stop locating whit illegal param.
287 * @tc.expected: step1. get reply state is false.
288 */
289 GTEST_LOG_(INFO)
290 << "LocatorServiceTest, CheckStopLocating001, TestSize.Level1";
291 LBSLOGI(LOCATOR, "[LocatorServiceTest] CheckStopLocating001 begin");
292 auto locatorImpl = Locator::GetInstance();
293 ASSERT_TRUE(locatorImpl != nullptr);
294 locatorImpl->StopLocating(callbackStub_);
295 LBSLOGI(LOCATOR, "[LocatorServiceTest] CheckStopLocating001 end");
296 }
297
298 /*
299 * @tc.name: CheckGetCacheLocation001
300 * @tc.desc: Check get cache location and expect success
301 * @tc.type: FUNC
302 */
303 HWTEST_F(LocatorServiceTest, CheckGetCacheLocation001, TestSize.Level1)
304 {
305 /*
306 * @tc.steps: step1. Call system ability and get cache location.
307 * @tc.expected: step1. get reply state is true.
308 */
309 GTEST_LOG_(INFO)
310 << "LocatorServiceTest, CheckGetCacheLocation001, TestSize.Level1";
311 LBSLOGI(LOCATOR, "[LocatorServiceTest] CheckGetCacheLocation001 begin");
312 MessageParcel data;
313 MessageParcel reply;
314 bool ret = false;
315 if (proxy_->GetSwitchState() == 1) {
316 proxy_->GetCacheLocation(reply);
317 ret = reply.ReadInt32() == REPLY_CODE_SECURITY_EXCEPTION;
318 EXPECT_EQ(false, ret);
319 } else {
320 proxy_->GetCacheLocation(reply);
321 ret = reply.ReadInt32() == REPLY_CODE_SECURITY_EXCEPTION;
322 EXPECT_EQ(false, ret);
323 }
324 LBSLOGI(LOCATOR, "[LocatorServiceTest] CheckGetCacheLocation001 end");
325 }
326
327 /*
328 * @tc.name: IsCallbackInProxyTest001
329 * @tc.desc: Check if callback is in the proxy callback list
330 * @tc.type: FUNC
331 */
332 HWTEST_F(LocatorServiceTest, IsCallbackInProxyTest001, TestSize.Level1)
333 {
334 /*
335 * @tc.steps: step1. Check if callback is in the proxy callback list
336 * @tc.expected: step1. return false
337 */
338 GTEST_LOG_(INFO)
339 << "LocatorServiceTest, IsCallbackInProxyTest001, TestSize.Level1";
340 LBSLOGI(LOCATOR, "[LocatorServiceTest] IsCallbackInProxyTest001 begin");
341 bool result = backgroundProxy_->IsCallbackInProxy(callbackStub_);
342 EXPECT_EQ(false, result);
343 LBSLOGI(LOCATOR, "[LocatorServiceTest] IsCallbackInProxyTest001 end");
344 }
345
346 /*
347 * @tc.name: OnSuspendTest001
348 * @tc.desc: Test the function of the process enter and exit frozen state
349 * @tc.type: FUNC
350 */
351 HWTEST_F(LocatorServiceTest, OnSuspendTest001, TestSize.Level1)
352 {
353 /*
354 * @tc.steps: step1. Call the onsuspend function, the process enter frozen state
355 * @tc.expected: step1. return true, the callback of the process is in the proxy list
356 * @tc.steps: step2. Call the onsuspend function, the process exit frozen state
357 * @tc.expected: step2. return false, the callback of the process is remove from the proxy list
358 */
359 GTEST_LOG_(INFO)
360 << "LocatorServiceTest, OnSuspendTest001, TestSize.Level1";
361 LBSLOGI(LOCATOR, "[LocatorServiceTest] OnSuspendTest001 begin");
362 backgroundProxy_->OnSuspend(request_, 0);
363 bool result = backgroundProxy_->IsCallbackInProxy(callbackStub_);
364 // no location permission
365 EXPECT_EQ(false, result);
366 backgroundProxy_->OnSuspend(request_, 1);
367 result = backgroundProxy_->IsCallbackInProxy(callbackStub_);
368 EXPECT_EQ(false, result);
369 LBSLOGI(LOCATOR, "[LocatorServiceTest] OnSuspendTest001 end");
370 }
371
372 /*
373 * @tc.name: OnPermissionChanged001
374 * @tc.desc: Test the function onPermissionChanged and OnDeleteRequestRecord
375 * @tc.type: FUNC
376 */
377 HWTEST_F(LocatorServiceTest, OnPermissionChanged001, TestSize.Level1)
378 {
379 /*
380 * @tc.steps: step1. Call the onsuspend function, the process enter frozen state
381 * @tc.steps: step2. Call onPermissionChanged, the process exit frozen state
382 * @tc.expected: step2. return true, the callback of the process is in the proxy list
383 */
384 GTEST_LOG_(INFO)
385 << "LocatorServiceTest, OnPermissionChanged001, TestSize.Level1";
386 LBSLOGI(LOCATOR, "[LocatorServiceTest] OnPermissionChanged001 begin");
387 backgroundProxy_->OnSuspend(request_, 0);
388 requestManager_->HandlePermissionChanged(IPCSkeleton::GetCallingTokenID());
389 bool result = backgroundProxy_->IsCallbackInProxy(callbackStub_);
390 // no location permission
391 EXPECT_EQ(false, result);
392 backgroundProxy_->OnDeleteRequestRecord(request_);
393 result = backgroundProxy_->IsCallbackInProxy(callbackStub_);
394 EXPECT_EQ(false, result);
395 LBSLOGI(LOCATOR, "[LocatorServiceTest] OnPermissionChanged001 end");
396 }
397
398 /*
399 * @tc.name: OnSaStateChange001
400 * @tc.desc: Test the function OnSaStateChange
401 * @tc.type: FUNC
402 */
403 HWTEST_F(LocatorServiceTest, OnSaStateChange001, TestSize.Level1)
404 {
405 /*
406 * @tc.steps: step1. Call the onsuspend function, the process enter frozen state
407 * @tc.steps: step2. Call OnSaStateChange, disable locator ability
408 * @tc.expected: step2. return true, do not change proxy list
409 * @tc.steps: step3. Call OnSaStateChange, enable locator ability
410 * @tc.expected: step3. return true, do not change proxy list
411 */
412 GTEST_LOG_(INFO)
413 << "LocatorServiceTest, OnSaStateChange001, TestSize.Level1";
414 LBSLOGI(LOCATOR, "[LocatorServiceTest] OnSaStateChange001 begin");
415 backgroundProxy_->OnSuspend(request_, 0);
416 bool result = backgroundProxy_->IsCallbackInProxy(callbackStub_);
417 // no location permission
418 EXPECT_EQ(false, result);
419 backgroundProxy_->OnSaStateChange(false);
420 result = backgroundProxy_->IsCallbackInProxy(callbackStub_);
421 // no location permission
422 EXPECT_EQ(false, result);
423 backgroundProxy_->OnSaStateChange(true);
424 result = backgroundProxy_->IsCallbackInProxy(callbackStub_);
425 // no location permission
426 EXPECT_EQ(false, result);
427 backgroundProxy_->OnDeleteRequestRecord(request_);
428 LBSLOGI(LOCATOR, "[LocatorServiceTest] OnSaStateChange001 end");
429 }
430
431 /*
432 * @tc.name: UpdateSaAbility001
433 * @tc.desc: Test update sa ability
434 * @tc.type: FUNC
435 */
436 HWTEST_F(LocatorServiceTest, UpdateSaAbility001, TestSize.Level1)
437 {
438 /*
439 * @tc.steps: step1. test update sa ability
440 * @tc.expected: step1. no exception happens
441 */
442 GTEST_LOG_(INFO)
443 << "LocatorServiceTest, UpdateSaAbility001, TestSize.Level1";
444 LBSLOGI(LOCATOR, "[LocatorServiceTest] UpdateSaAbility001 begin");
445 ASSERT_TRUE(proxy_ != nullptr);
446 proxy_->UpdateSaAbility();
447 LBSLOGI(LOCATOR, "[LocatorServiceTest] UpdateSaAbility001 end");
448 }
449
450 /*
451 * @tc.name: SetEnableAndDisable001
452 * @tc.desc: Test disable and enable system ability
453 * @tc.type: FUNC
454 */
455 HWTEST_F(LocatorServiceTest, SetEnableAndDisable001, TestSize.Level1)
456 {
457 /*
458 * @tc.steps: step1.test the switch enable and disable function
459 * @tc.expected: step1. switch set should be true, or setting will return error.
460 */
461 GTEST_LOG_(INFO)
462 << "LocatorServiceTest, SetEnableAndDisable001, TestSize.Level1";
463 LBSLOGI(LOCATOR, "[LocatorServiceTest] SetEnableAndDisable001 begin");
464 bool ret = false;
465 if (proxy_->GetSwitchState() == 1) {
466 proxy_->EnableAbility(false); // if the state is false
467 ret = proxy_ -> GetSwitchState() == 0 ? true : false;
468 EXPECT_EQ(true, ret);
469 // reset the state
470 proxy_->EnableAbility(true);
471 } else {
472 proxy_->EnableAbility(true); // if the state is false
473 ret = proxy_ -> GetSwitchState() == 1 ? true : false;
474 EXPECT_EQ(true, ret);
475 // reset the state
476 proxy_->EnableAbility(false);
477 }
478 LBSLOGI(LOCATOR, "[LocatorServiceTest] SetEnableAndDisable001 end");
479 }
480
481 /*
482 * @tc.name: RegisterSwitchCallback001
483 * @tc.desc: Test register switch callback if client is null
484 * @tc.type: FUNC
485 */
486 HWTEST_F(LocatorServiceTest, RegisterSwitchCallback001, TestSize.Level1)
487 {
488 /*
489 * @tc.steps: step1.the client is null.
490 */
491 GTEST_LOG_(INFO)
492 << "LocatorServiceTest, RegisterSwitchCallback001, TestSize.Level1";
493 LBSLOGI(LOCATOR, "[LocatorServiceTest] RegisterSwitchCallback001 begin");
494 pid_t callinguid = 1;
495 sptr<IRemoteObject> client = nullptr;
496
497 /*
498 * @tc.steps: step2. test register switch callback
499 * @tc.expected: log exception: "register an invalid switch callback"
500 */
501 ASSERT_TRUE(proxy_ != nullptr);
502 proxy_->RegisterSwitchCallback(client, callinguid);
503 LBSLOGI(LOCATOR, "[LocatorServiceTest] RegisterSwitchCallback001 end");
504 }
505
506 /*
507 * @tc.name: RegisterSwitchCallback002
508 * @tc.desc: Test register and unregister switch callback if client is not null
509 * @tc.type: FUNC
510 */
511 HWTEST_F(LocatorServiceTest, RegisterAndUnregisterSwitchCallback001, TestSize.Level1)
512 {
513 /*
514 * @tc.steps: step1. give the calling uid
515 */
516 GTEST_LOG_(INFO)
517 << "LocatorServiceTest, RegisterAndUnregisterSwitchCallback001, TestSize.Level1";
518 LBSLOGI(LOCATOR, "[LocatorServiceTest] RegisterAndUnregisterSwitchCallback001 begin");
519 pid_t callinguid = 1;
520
521 /*
522 * @tc.steps: step2. test register switch callback
523 * @tc.expected: no exception happens.
524 */
525 ASSERT_TRUE(proxy_ != nullptr);
526 proxy_->RegisterSwitchCallback(callbackStub_->AsObject(), callinguid);
527
528 /*
529 * @tc.steps: step3. test unregister switch callback
530 * @tc.steps: step4. continue to test unregister switch callback
531 * @tc.expected: no exception happens.
532 */
533 proxy_->UnregisterSwitchCallback(callbackStub_->AsObject()); // the callback has been restored in the map
534
535 proxy_->UnregisterSwitchCallback(callbackStub_->AsObject()); // the map is empty
536 LBSLOGI(LOCATOR, "[LocatorServiceTest] RegisterAndUnregisterSwitchCallback001 end");
537 }
538
539 /*
540 * @tc.name: UnregisterSwitchCallback001
541 * @tc.desc: Test unregister switch callback if client is null
542 * @tc.type: FUNC
543 */
544 HWTEST_F(LocatorServiceTest, UnregisterSwitchCallback001, TestSize.Level1)
545 {
546 /*
547 * @tc.steps: step1.the client is null.
548 */
549 GTEST_LOG_(INFO)
550 << "LocatorServiceTest, UnregisterSwitchCallback001, TestSize.Level1";
551 LBSLOGI(LOCATOR, "[LocatorServiceTest] UnregisterSwitchCallback001 begin");
552 sptr<IRemoteObject> client = nullptr;
553
554 /*
555 * @tc.steps: step2. test unregister switch callback
556 * @tc.expected: log exception: LOCATOR: unregister an invalid switch callback
557 */
558 ASSERT_TRUE(proxy_ != nullptr);
559 proxy_->UnregisterSwitchCallback(client);
560 LBSLOGI(LOCATOR, "[LocatorServiceTest] UnregisterSwitchCallback001 end");
561 }
562
563 /*
564 * @tc.name: RegisterNmeaMessageCallback001
565 * @tc.desc: Test register nmea message callback if client is null
566 * @tc.type: FUNC
567 */
568 #ifdef FEATURE_GNSS_SUPPORT
569 HWTEST_F(LocatorServiceTest, RegisterNmeaMessageCallback001, TestSize.Level1)
570 {
571 /*
572 * @tc.steps: step1.the client is null.
573 */
574 GTEST_LOG_(INFO)
575 << "LocatorServiceTest, RegisterNmeaMessageCallback001, TestSize.Level1";
576 LBSLOGI(LOCATOR, "[LocatorServiceTest] RegisterNmeaMessageCallback001 begin");
577 pid_t uid = 1;
578 sptr<IRemoteObject> client = nullptr;
579
580 /*
581 * @tc.steps: step2. test register nmea message callback
582 * @tc.expected: log info : "register an invalid nmea callback".
583 */
584 ASSERT_TRUE(proxy_ != nullptr);
585 proxy_->RegisterNmeaMessageCallback(client, uid);
586 LBSLOGI(LOCATOR, "[LocatorServiceTest] RegisterNmeaMessageCallback001 end");
587 }
588 #endif
589
590 /*
591 * @tc.name: RegisterAndUnregisterNmeaMessageCallback001
592 * @tc.desc: Test register nmea message callback and then unregister twice , the first will unreg success,
593 * and the second will not return error.
594 * @tc.type: FUNC
595 */
596 #ifdef FEATURE_GNSS_SUPPORT
597 HWTEST_F(LocatorServiceTest, RegisterAndUnregisterNmeaMessageCallback001, TestSize.Level1)
598 {
599 /*
600 * @tc.steps: step1.the client is not null.
601 */
602 GTEST_LOG_(INFO)
603 << "LocatorServiceTest, RegisterAndUnregisterNmeaMessageCallback001, TestSize.Level1";
604 LBSLOGI(LOCATOR, "[LocatorServiceTest] RegisterAndUnregisterNmeaMessageCallback001 begin");
605 pid_t uid = 1;
606
607 /*
608 * @tc.steps: step2. test register nmea message callback
609 * @tc.expected: no exception happens
610 */
611 ASSERT_TRUE(proxy_ != nullptr);
612 proxy_->RegisterNmeaMessageCallback(callbackStub_->AsObject(), uid);
613
614 /*
615 * @tc.steps: step3. test unregister nmea message callback
616 * @tc.steps: step4. continue to test unregister nmea message callback
617 * @tc.expected: no exception happens.
618 */
619 proxy_->UnregisterNmeaMessageCallback(callbackStub_->AsObject()); // callback in map
620 proxy_->UnregisterNmeaMessageCallback(callbackStub_->AsObject()); // map is empty
621 LBSLOGI(LOCATOR, "[LocatorServiceTest] RegisterAndUnregisterNmeaMessageCallback001 end");
622 }
623 #endif
624
625 /*
626 * @tc.name: UnregisterNmeaMessageCallback001
627 * @tc.desc: Test unregister nmea message callback if client is null
628 * @tc.type: FUNC
629 */
630 #ifdef FEATURE_GNSS_SUPPORT
631 HWTEST_F(LocatorServiceTest, UnregisterNmeaMessageCallback001, TestSize.Level1)
632 {
633 /*
634 * @tc.steps: step1.the client is null.
635 */
636 GTEST_LOG_(INFO)
637 << "LocatorServiceTest, UnregisterNmeaMessageCallback001, TestSize.Level1";
638 LBSLOGI(LOCATOR, "[LocatorServiceTest] UnregisterNmeaMessageCallback001 begin");
639 sptr<IRemoteObject> client = nullptr;
640
641 /*
642 * @tc.steps: step2. test unregister nmea message callback
643 * @tc.expected: log info : "unregister an invalid nmea callback".
644 */
645 ASSERT_TRUE(proxy_ != nullptr);
646 proxy_->UnregisterNmeaMessageCallback(client);
647 LBSLOGI(LOCATOR, "[LocatorServiceTest] UnregisterNmeaMessageCallback001 end");
648 }
649 #endif
650
651 /*
652 * @tc.name: GetAddressByLocationName001
653 * @tc.desc: Test get address by location name
654 * @tc.type: FUNC
655 */
656 #ifdef FEATURE_GEOCODE_SUPPORT
657 HWTEST_F(LocatorServiceTest, GetAddressByLocationName001, TestSize.Level1)
658 {
659 /*
660 * @tc.steps: step1.the client is null.
661 */
662 GTEST_LOG_(INFO)
663 << "LocatorServiceTest, GetAddressByLocationName001, TestSize.Level1";
664 LBSLOGI(LOCATOR, "[LocatorServiceTest] GetAddressByLocationName001 begin");
665 MessageParcel data;
666 MessageParcel reply;
667 data.WriteInterfaceToken(LocatorProxy::GetDescriptor());
668 data.WriteString16(Str8ToStr16("")); // description
669 data.WriteDouble(10.0); // minLatitude
670 data.WriteDouble(1.0); // minLongitude
671 data.WriteDouble(10.0); // maxLatitude
672 data.WriteDouble(10.0); // maxLongitude
673 data.WriteInt32(10); // maxItems
674 data.WriteInt32(1); // locale object size = 1
675 data.WriteString16(Str8ToStr16("ZH")); // locale.getLanguage()
676 data.WriteString16(Str8ToStr16("cn")); // locale.getCountry()
677 data.WriteString16(Str8ToStr16("")); // locale.getVariant()
678 data.WriteString16(Str8ToStr16("")); // ""
679
680 /*
681 * @tc.steps: step2. test get address by location name
682 * @tc.expected: return REPLY_CODE_NO_EXCEPTION.
683 */
684 proxy_->GetAddressByLocationName(data, reply);
685 EXPECT_EQ(ERRCODE_GEOCODING_FAIL, reply.ReadInt32());
686 LBSLOGI(LOCATOR, "[LocatorServiceTest] GetAddressByLocationName001 end");
687 }
688 #endif
689
690 /*
691 * @tc.name: RegisterGnssStatusCallback001
692 * @tc.desc: Test register gnss status callback if client is null
693 * @tc.type: FUNC
694 */
695 #ifdef FEATURE_GNSS_SUPPORT
696 HWTEST_F(LocatorServiceTest, RegisterGnssStatusCallback001, TestSize.Level1)
697 {
698 /*
699 * @tc.steps: step1.the client is null.
700 */
701 GTEST_LOG_(INFO)
702 << "LocatorServiceTest, RegisterGnssStatusCallback001, TestSize.Level1";
703 LBSLOGI(LOCATOR, "[LocatorServiceTest] RegisterGnssStatusCallback001 begin");
704 pid_t lastCallingUid = 1;
705 sptr<IRemoteObject> client = nullptr;
706
707 /*
708 * @tc.steps: step2. test register gnss status callback
709 * @tc.expected: log info : "SendRegisterMsgToRemote callback is nullptr".
710 */
711 ASSERT_TRUE(proxy_ != nullptr);
712 proxy_->RegisterGnssStatusCallback(client, lastCallingUid);
713 LBSLOGI(LOCATOR, "[LocatorServiceTest] RegisterGnssStatusCallback001 end");
714 }
715 #endif
716
717 /*
718 * @tc.name: RegisterAndUnregisterGnssStatusCallback001
719 * @tc.desc: Test register and unregister gnss status callback if client is not null
720 * @tc.type: FUNC
721 */
722 #ifdef FEATURE_GNSS_SUPPORT
723 HWTEST_F(LocatorServiceTest, RegisterAndUnregisterGnssStatusCallback001, TestSize.Level1)
724 {
725 /*
726 * @tc.steps: step1. give the last calling uid
727 */
728 GTEST_LOG_(INFO)
729 << "LocatorServiceTest, RegisterAndUnregisterGnssStatusCallback001, TestSize.Level1";
730 LBSLOGI(LOCATOR, "[LocatorServiceTest] RegisterAndUnregisterGnssStatusCallback001 begin");
731 pid_t lastCallingUid = 1;
732
733 /*
734 * @tc.steps: step2. test register gnss status callback
735 * @tc.expected: no exception happens.
736 */
737 ASSERT_TRUE(proxy_ != nullptr);
738 proxy_->RegisterGnssStatusCallback(callbackStub_->AsObject(), lastCallingUid);
739
740 /*
741 * @tc.steps: step3. test unregister gnss status callback
742 * @tc.steps: step4. continue to test unregister gnss status callback
743 * @tc.expected: no exception happens
744 */
745 proxy_->UnregisterGnssStatusCallback(callbackStub_->AsObject()); // callback in map
746 proxy_->UnregisterGnssStatusCallback(callbackStub_->AsObject()); // map is empty
747 LBSLOGI(LOCATOR, "[LocatorServiceTest] RegisterAndUnregisterGnssStatusCallback001 end");
748 }
749 #endif
750
751 /*
752 * @tc.name: UnregisterGnssStatusCallback001
753 * @tc.desc: Test unregister gnss status callback if client is null
754 * @tc.type: FUNC
755 */
756 #ifdef FEATURE_GNSS_SUPPORT
757 HWTEST_F(LocatorServiceTest, UnregisterGnssStatusCallback001, TestSize.Level1)
758 {
759 /*
760 * @tc.steps: step1.the client is null.
761 */
762 GTEST_LOG_(INFO)
763 << "LocatorServiceTest, UnregisterGnssStatusCallback001, TestSize.Level1";
764 LBSLOGI(LOCATOR, "[LocatorServiceTest] UnregisterGnssStatusCallback001 begin");
765 sptr<IRemoteObject> client = nullptr;
766
767 /*
768 * @tc.steps: step2. test unregister gnss status callback
769 * @tc.expected: log info : "unregister an invalid gnssStatus callback".
770 */
771 ASSERT_TRUE(proxy_ != nullptr);
772 proxy_->UnregisterGnssStatusCallback(client);
773 LBSLOGI(LOCATOR, "[LocatorServiceTest] UnregisterGnssStatusCallback001 end");
774 }
775 #endif
776
777 /*
778 * @tc.name: GetAddressByCoordinate001
779 * @tc.desc: Test get address by coordinate
780 * @tc.type: FUNC
781 */
782 #ifdef FEATURE_GEOCODE_SUPPORT
783 HWTEST_F(LocatorServiceTest, GetAddressByCoordinate001, TestSize.Level1)
784 {
785 /*
786 * @tc.steps: step1. build the data.
787 */
788 GTEST_LOG_(INFO)
789 << "LocatorServiceTest, GetAddressByCoordinate001, TestSize.Level1";
790 LBSLOGI(LOCATOR, "[LocatorServiceTest] GetAddressByCoordinate001 begin");
791 MessageParcel reply;
792 MessageParcel data;
793 data.WriteInterfaceToken(LocatorProxy::GetDescriptor());
794 data.WriteDouble(10.5); // latitude
795 data.WriteDouble(30.2); // longitude
796 data.WriteInt32(10); // maxItems
797 data.WriteInt32(1); // locale object size = 1
798 data.WriteString16(Str8ToStr16("ZH")); // locale.getLanguage()
799 data.WriteString16(Str8ToStr16("cn")); // locale.getCountry()
800 data.WriteString16(Str8ToStr16("")); // locale.getVariant()
801 data.WriteString16(Str8ToStr16("")); // ""
802
803 /*
804 * @tc.steps: step2. test get address by coordinate.
805 * @tc.expected: step2. get reply state is true.
806 */
807 proxy_->GetAddressByCoordinate(data, reply);
808 EXPECT_EQ(ERRCODE_REVERSE_GEOCODING_FAIL, reply.ReadInt32());
809
810 LBSLOGI(LOCATOR, "[LocatorServiceTest] GetAddressByCoordinate001 end");
811 }
812 #endif
813
814 /*
815 * @tc.name: SetAndCheckLocationPrivacyConfirmStatus001
816 * @tc.desc: Test set and check the status
817 * @tc.type: FUNC
818 */
819 HWTEST_F(LocatorServiceTest, SetAndCheckLocationPrivacyConfirmStatus001, TestSize.Level1)
820 {
821 /*
822 * @tc.steps: step1. set PRIVACY_TYPE_OTHERS type status true.
823 * @tc.steps: step2. set PRIVACY_TYPE_STARTUP type status false.
824 * @tc.steps: step3. set PRIVACY_TYPE_CORE_LOCATION type default.
825 */
826 GTEST_LOG_(INFO)
827 << "LocatorServiceTest, SetAndCheckLocationPrivacyConfirmStatus001, TestSize.Level1";
828 LBSLOGI(LOCATOR, "[LocatorServiceTest] SetAndCheckLocationPrivacyConfirmStatus001 begin");
829 EXPECT_EQ(ERRCODE_SUCCESS, proxy_->SetLocationPrivacyConfirmStatus(PRIVACY_TYPE_OTHERS, true));
830 EXPECT_EQ(ERRCODE_SUCCESS, proxy_->SetLocationPrivacyConfirmStatus(PRIVACY_TYPE_STARTUP, false));
831
832 /*
833 * @tc.steps: step4. location privacy confirm should be true when the type is PRIVACY_TYPE_OTHERS.
834 * @tc.steps: step5. location privacy confirm should be false when the type is PRIVACY_TYPE_STARTUP.
835 * @tc.steps: step6. location privacy confirm should be false when the type is PRIVACY_TYPE_CORE_LOCATION.
836 * @tc.steps: step7. location privacy confirm should be false when the type is invalid.
837 * @tc.expected: no exception happens
838 */
839 EXPECT_EQ(true, proxy_->IsLocationPrivacyConfirmed(PRIVACY_TYPE_OTHERS));
840 EXPECT_EQ(false, proxy_->IsLocationPrivacyConfirmed(PRIVACY_TYPE_STARTUP));
841 EXPECT_EQ(false, proxy_->IsLocationPrivacyConfirmed(PRIVACY_TYPE_CORE_LOCATION));
842 EXPECT_EQ(false, proxy_->IsLocationPrivacyConfirmed(-1));
843 LBSLOGI(LOCATOR, "[LocatorServiceTest] SetAndCheckLocationPrivacyConfirmStatus001 end");
844 }
845
846 /*
847 * @tc.name: RegisterAndUnregisterCachedLocationCallback001
848 * @tc.desc: Test register and unregister cached location callback if the params are not null.
849 * @tc.type: FUNC
850 */
851 #ifdef FEATURE_GNSS_SUPPORT
852 HWTEST_F(LocatorServiceTest, RegisterAndUnregisterCachedLocationCallback001, TestSize.Level1)
853 {
854 /*
855 * @tc.steps: step1. give the calling uid, cached call back, request config
856 */
857 GTEST_LOG_(INFO)
858 << "LocatorServiceTest, RegisterAndUnregisterCachedLocationCallback001, TestSize.Level1";
859 LBSLOGI(LOCATOR, "[LocatorServiceTest] RegisterAndUnregisterCachedLocationCallback001 begin");
860 std::unique_ptr<CachedGnssLocationsRequest> requestConfig = std::make_unique<CachedGnssLocationsRequest>();
861 auto cachedCallbackHost = sptr<CachedLocationsCallbackHost>(new (std::nothrow) CachedLocationsCallbackHost());
862 auto cachedCallback = sptr<ICachedLocationsCallback>(cachedCallbackHost);
863 std::string bundleName = "test";
864
865 /*
866 * @tc.steps: step2. test register cached location callback
867 * @tc.expected: no exception happens.
868 */
869 EXPECT_EQ(REPLY_CODE_NO_EXCEPTION,
870 proxy_->RegisterCachedLocationCallback(requestConfig, cachedCallback, bundleName));
871
872 /*
873 * @tc.steps: step3. test unregister cached location callback
874 * @tc.steps: step4. continue to test unregister cached location callback
875 * @tc.expected: no exception happens
876 */
877 EXPECT_EQ(REPLY_CODE_NO_EXCEPTION,
878 proxy_->UnregisterCachedLocationCallback(cachedCallback)); // the callback has been restored in the map
879 EXPECT_EQ(REPLY_CODE_NO_EXCEPTION,
880 proxy_->UnregisterCachedLocationCallback(cachedCallback)); // the map is empty
881 LBSLOGI(LOCATOR, "[LocatorServiceTest] RegisterAndUnregisterCachedLocationCallback001 end");
882 }
883 #endif
884
885 /*
886 * @tc.name: RegisterCachedLocationCallback001
887 * @tc.desc: Test register cached location callback if params are null.
888 * @tc.type: FUNC
889 */
890 #ifdef FEATURE_GNSS_SUPPORT
891 HWTEST_F(LocatorServiceTest, RegisterCachedLocationCallback001, TestSize.Level1)
892 {
893 /*
894 * @tc.steps: step1. give the calling uid, cached call back, request config
895 */
896 GTEST_LOG_(INFO)
897 << "LocatorServiceTest, RegisterCachedLocationCallback001, TestSize.Level1";
898 LBSLOGI(LOCATOR, "[LocatorServiceTest] RegisterCachedLocationCallback001 begin");
899 std::unique_ptr<CachedGnssLocationsRequest> requestConfig = nullptr;
900 sptr<ICachedLocationsCallback> cachedCallback = nullptr;
901 std::string bundleName = "test";
902
903 /*
904 * @tc.steps: step2. test register cached location callback
905 * @tc.expected: no exception happens.
906 */
907 EXPECT_EQ(REPLY_CODE_NO_EXCEPTION,
908 proxy_->RegisterCachedLocationCallback(requestConfig, cachedCallback, bundleName));
909 LBSLOGI(LOCATOR, "[LocatorServiceTest] RegisterCachedLocationCallback001 end");
910 }
911 #endif
912
913 /*
914 * @tc.name: GetCachedGnssLocationsSize001
915 * @tc.desc: Test get cached gnss location size
916 * @tc.type: FUNC
917 */
918 #ifdef FEATURE_GNSS_SUPPORT
919 HWTEST_F(LocatorServiceTest, GetCachedGnssLocationsSize001, TestSize.Level1)
920 {
921 /*
922 * @tc.steps: step1. test get cached gnss location size.
923 * @tc.expected: step1. get the true size.
924 */
925 GTEST_LOG_(INFO)
926 << "LocatorServiceTest, GetCachedGnssLocationsSize001, TestSize.Level1";
927 LBSLOGI(LOCATOR, "[LocatorServiceTest] GetCachedGnssLocationsSize001 begin");
928 MessageParcel reply;
929 if (proxy_->GetSwitchState() == 1) {
930 EXPECT_EQ(0, proxy_->GetCachedGnssLocationsSize()); // not support now
931 } else {
932 EXPECT_EQ(0, proxy_->GetCachedGnssLocationsSize()); // switch is off
933 }
934 LBSLOGI(LOCATOR, "[LocatorServiceTest] GetCachedGnssLocationsSize001 end");
935 }
936 #endif
937
938 /*
939 * @tc.name: FlushCachedGnssLocations001
940 * @tc.desc: Test flush cached gnss location
941 * @tc.type: FUNC
942 */
943 #ifdef FEATURE_GNSS_SUPPORT
944 HWTEST_F(LocatorServiceTest, FlushCachedGnssLocations001, TestSize.Level1)
945 {
946 /*
947 * @tc.steps: step1. test flush cached gnss location
948 * @tc.expected: step1. get the true size.
949 */
950 GTEST_LOG_(INFO)
951 << "LocatorServiceTest, FlushCachedGnssLocations001, TestSize.Level1";
952 LBSLOGI(LOCATOR, "[LocatorServiceTest] FlushCachedGnssLocations001 begin");
953 EXPECT_EQ(ERRCODE_NOT_SUPPORTED, proxy_->FlushCachedGnssLocations());
954 LBSLOGI(LOCATOR, "[LocatorServiceTest] FlushCachedGnssLocations001 end");
955 }
956 #endif
957
958 /*
959 * @tc.name: SendCommand001
960 * @tc.desc: Test send command
961 * @tc.type: FUNC
962 */
963 #ifdef FEATURE_GNSS_SUPPORT
964 HWTEST_F(LocatorServiceTest, SendCommand001, TestSize.Level1)
965 {
966 /*
967 * @tc.steps: step1. build location command
968 */
969 GTEST_LOG_(INFO)
970 << "LocatorServiceTest, SendCommand001, TestSize.Level1";
971 LBSLOGI(LOCATOR, "[LocatorServiceTest] SendCommand001 begin");
972 MessageParcel data;
973 std::unique_ptr<LocationCommand> locationCommand = std::make_unique<LocationCommand>();
974 locationCommand->scenario = data.ReadInt32();
975 locationCommand->command = data.ReadBool();
976
977 /*
978 * @tc.steps: step2. test send command.
979 * @tc.expected: current function is empty, nothing happens
980 */
981 ASSERT_TRUE(proxy_ != nullptr);
982 proxy_->SendCommand(locationCommand);
983 LBSLOGI(LOCATOR, "[LocatorServiceTest] SendCommand001 end");
984 }
985 #endif
986
987 /*
988 * @tc.name: AddFence001
989 * @tc.desc: Test add fence
990 * @tc.type: FUNC
991 */
992 #ifdef FEATURE_GNSS_SUPPORT
993 HWTEST_F(LocatorServiceTest, AddFence001, TestSize.Level1)
994 {
995 /*
996 * @tc.steps: step1. build geo fence request
997 */
998 GTEST_LOG_(INFO)
999 << "LocatorServiceTest, AddFence001, TestSize.Level1";
1000 LBSLOGI(LOCATOR, "[LocatorServiceTest] AddFence001 begin");
1001 std::unique_ptr<GeofenceRequest> request = std::make_unique<GeofenceRequest>();
1002 request->scenario = 2; // scenario
1003 request->geofence.latitude = 35.1; // latitude
1004 request->geofence.longitude = 40.2; // longitude
1005 request->geofence.radius = 2.2; // radius
1006 request->geofence.expiration = 12.2; // expiration
1007
1008 /*
1009 * @tc.steps: step2. test add fence
1010 * @tc.expected: no exception happens
1011 */
1012 ASSERT_TRUE(proxy_ != nullptr);
1013 proxy_->AddFence(request);
1014 LBSLOGI(LOCATOR, "[LocatorServiceTest] AddFence001 end");
1015 }
1016 #endif
1017
1018 /*
1019 * @tc.name: RemoveFence001
1020 * @tc.desc: Test add fence
1021 * @tc.type: FUNC
1022 */
1023 #ifdef FEATURE_GNSS_SUPPORT
1024 HWTEST_F(LocatorServiceTest, RemoveFence001, TestSize.Level1)
1025 {
1026 /*
1027 * @tc.steps: step1. build geo fence request
1028 */
1029 GTEST_LOG_(INFO)
1030 << "LocatorServiceTest, RemoveFence001, TestSize.Level1";
1031 LBSLOGI(LOCATOR, "[LocatorServiceTest] RemoveFence001 begin");
1032 std::unique_ptr<GeofenceRequest> request = std::make_unique<GeofenceRequest>();
1033 request->scenario = 2; // scenario
1034 request->geofence.latitude = 35.1; // latitude
1035 request->geofence.longitude = 40.2; // longitude
1036 request->geofence.radius = 2.2; // radius
1037 request->geofence.expiration = 12.2; // expiration
1038
1039 /*
1040 * @tc.steps: step2. test remove fence
1041 * @tc.expected: no exception happens
1042 */
1043 ASSERT_TRUE(proxy_ != nullptr);
1044 proxy_->RemoveFence(request);
1045 LBSLOGI(LOCATOR, "[LocatorServiceTest] RemoveFence001 end");
1046 }
1047 #endif
1048
1049 /*
1050 * @tc.name: EnableLocationMock001
1051 * @tc.desc: Test enable location mock in SCENE_CAR_HAILING scenario
1052 * @tc.type: FUNC
1053 */
1054 HWTEST_F(LocatorServiceTest, EnableLocationMock001, TestSize.Level1)
1055 {
1056 /*
1057 * @tc.steps: step1. test enable location mock
1058 * @tc.expected: no exception happens
1059 */
1060 GTEST_LOG_(INFO)
1061 << "LocatorServiceTest, EnableLocationMock001, TestSize.Level1";
1062 LBSLOGI(LOCATOR, "[LocatorServiceTest] EnableLocationMock001 begin");
1063 EXPECT_EQ(true, proxy_->EnableLocationMock());
1064 LBSLOGI(LOCATOR, "[LocatorServiceTest] EnableLocationMock001 end");
1065 }
1066
1067 /*
1068 * @tc.name: DisableLocationMock001
1069 * @tc.desc: Test disable location mock in SCENE_CAR_HAILING scenario
1070 * @tc.type: FUNC
1071 */
1072 HWTEST_F(LocatorServiceTest, DisableLocationMock001, TestSize.Level1)
1073 {
1074 /*
1075 * @tc.steps: step1. test disable location mock
1076 * @tc.expected: no exception happens
1077 */
1078 GTEST_LOG_(INFO)
1079 << "LocatorServiceTest, DisableLocationMock001, TestSize.Level1";
1080 LBSLOGI(LOCATOR, "[LocatorServiceTest] DisableLocationMock001 begin");
1081 EXPECT_EQ(true, proxy_->DisableLocationMock());
1082 LBSLOGI(LOCATOR, "[LocatorServiceTest] DisableLocationMock001 end");
1083 }
1084
1085 /*
1086 * @tc.name: SetMockedLocations001
1087 * @tc.desc: Test set location mock in different scenarioes
1088 * @tc.type: FUNC
1089 */
1090 HWTEST_F(LocatorServiceTest, SetMockedLocations001, TestSize.Level1)
1091 {
1092 /*
1093 * @tc.steps: step1. prepare mock info
1094 */
1095 GTEST_LOG_(INFO)
1096 << "LocatorServiceTest, SetMockedLocations001, TestSize.Level1";
1097 LBSLOGI(LOCATOR, "[LocatorServiceTest] SetMockedLocations001 begin");
1098 int timeInterval = 2;
1099 std::vector<std::shared_ptr<Location>> mockLocationArray;
1100 Parcel parcel;
1101 for (int i = 0; i < 2; i++) {
1102 parcel.WriteDouble(10.6); // latitude
1103 parcel.WriteDouble(10.5); // longitude
1104 parcel.WriteDouble(10.4); // altitude
1105 parcel.WriteDouble(1.0); // accuracy
1106 parcel.WriteDouble(5.0); // speed
1107 parcel.WriteDouble(10); // direction
1108 parcel.WriteInt64(1611000000); // timestamp
1109 parcel.WriteInt64(1611000000); // time since boot
1110 parcel.WriteString16(u"additions"); // additions
1111 parcel.WriteInt64(1); // additionSize
1112 parcel.WriteBool(true); // isFromMock
1113 parcel.WriteInt32(1); // source type
1114 parcel.WriteInt32(0); // floor no.
1115 parcel.WriteDouble(1000.0); // floor acc
1116 mockLocationArray.push_back(Location::UnmarshallingShared(parcel));
1117 }
1118
1119 /*
1120 * @tc.steps: step2. test set mocked locations for different scenarioes
1121 * @tc.expected: no exception happens
1122 */
1123 EXPECT_EQ(true, proxy_->SetMockedLocations(timeInterval, mockLocationArray));
1124 LBSLOGI(LOCATOR, "[LocatorServiceTest] SetMockedLocations001 end");
1125 }
1126
1127 /*
1128 * @tc.name: EnableReverseGeocodingMock001
1129 * @tc.desc: Test enable reverse geocoding mock
1130 * @tc.type: FUNC
1131 */
1132 #ifdef FEATURE_GEOCODE_SUPPORT
1133 HWTEST_F(LocatorServiceTest, EnableReverseGeocodingMock001, TestSize.Level1)
1134 {
1135 /*
1136 * @tc.steps: step1. test enable reverse geocoding mock
1137 * @tc.expected: no exception happens
1138 */
1139 GTEST_LOG_(INFO)
1140 << "LocatorServiceTest, EnableReverseGeocodingMock001, TestSize.Level1";
1141 LBSLOGI(LOCATOR, "[LocatorServiceTest] EnableReverseGeocodingMock001 begin");
1142 EXPECT_EQ(true, proxy_->EnableReverseGeocodingMock());
1143 LBSLOGI(LOCATOR, "[LocatorServiceTest] EnableReverseGeocodingMock001 end");
1144 }
1145 #endif
1146
1147 /*
1148 * @tc.name: DisableReverseGeocodingMock001
1149 * @tc.desc: Test disable reverse geocoding mock
1150 * @tc.type: FUNC
1151 */
1152 #ifdef FEATURE_GEOCODE_SUPPORT
1153 HWTEST_F(LocatorServiceTest, DisableReverseGeocodingMock001, TestSize.Level1)
1154 {
1155 /*
1156 * @tc.steps: step1. test disable reverse geocoding mock
1157 * @tc.expected: no exception happens
1158 */
1159 GTEST_LOG_(INFO)
1160 << "LocatorServiceTest, DisableReverseGeocodingMock001, TestSize.Level1";
1161 LBSLOGI(LOCATOR, "[LocatorServiceTest] DisableReverseGeocodingMock001 begin");
1162 EXPECT_EQ(true, proxy_->DisableReverseGeocodingMock());
1163 LBSLOGI(LOCATOR, "[LocatorServiceTest] DisableReverseGeocodingMock001 end");
1164 }
1165 #endif
1166
1167 /*
1168 * @tc.name: SetReverseGeocodingMockInfo001
1169 * @tc.desc: Test set reverse geocoding mock info
1170 * @tc.type: FUNC
1171 */
1172 #ifdef FEATURE_GEOCODE_SUPPORT
1173 HWTEST_F(LocatorServiceTest, SetReverseGeocodingMockInfo001, TestSize.Level1)
1174 {
1175 /*
1176 * @tc.steps: step1. prepare mock info
1177 */
1178 GTEST_LOG_(INFO)
1179 << "LocatorServiceTest, SetReverseGeocodingMockInfo001, TestSize.Level1";
1180 LBSLOGI(LOCATOR, "[LocatorServiceTest] SetReverseGeocodingMockInfo001 begin");
1181 std::vector<std::shared_ptr<GeocodingMockInfo>> mockInfo;
1182 std::shared_ptr<GeocodingMockInfo> info = std::make_shared<GeocodingMockInfo>();
1183 Parcel data;
1184 data.WriteString16(Str8ToStr16("locale")); // locale
1185 data.WriteDouble(10.5); // latitude
1186 data.WriteDouble(10.6); // longitude
1187 data.WriteInt32(2); // maxItems
1188 info->ReadFromParcel(data);
1189 mockInfo.push_back(info);
1190 /*
1191 * @tc.steps: step2. test set reverse geocoding mock info
1192 * @tc.expected: no exception happens
1193 */
1194 EXPECT_EQ(true, proxy_->SetReverseGeocodingMockInfo(mockInfo));
1195 LBSLOGI(LOCATOR, "[LocatorServiceTest] SetReverseGeocodingMockInfo001 end");
1196 }
1197 #endif
1198
1199 /*
1200 * @tc.name: CheckPermission001
1201 * @tc.desc: Test the function CheckPermission
1202 * @tc.type: FUNC
1203 * @tc.require: issueI5OSHX
1204 */
1205 HWTEST_F(LocatorServiceTest, CheckPermission001, TestSize.Level1)
1206 {
1207 /*
1208 * @tc.steps: step1. get callingTokenId and callingFirstTokenid.
1209 * @tc.steps: step2. Call GetPermissionLevel and get permission level.
1210 * @tc.expected: step1. get permission level is PERMISSION_ACCURATE.
1211 */
1212 GTEST_LOG_(INFO)
1213 << "LocatorServiceTest, CheckPermission001, TestSize.Level1";
1214 LBSLOGI(LOCATOR, "[LocatorServiceTest] CheckPermission001 begin");
1215 uint32_t callingTokenId = IPCSkeleton::GetCallingTokenID();
1216 uint32_t callingFirstTokenid = IPCSkeleton::GetFirstTokenID();
1217 int permissionLevel = CommonUtils::GetPermissionLevel(callingTokenId, callingFirstTokenid);
1218 EXPECT_EQ(PERMISSION_ACCURATE, permissionLevel);
1219 LBSLOGI(LOCATOR, "[LocatorServiceTest] CheckPermission001 end");
1220 }
1221
1222 /*
1223 * @tc.name: RegisterAppStateObserver001
1224 * @tc.desc: Test the function register app state observer
1225 * @tc.type: FUNC
1226 * @tc.require: issueI5PX7W
1227 */
1228 HWTEST_F(LocatorServiceTest, RegisterAppStateObserver001, TestSize.Level1)
1229 {
1230 /*
1231 * @tc.steps: step1. get the request manager
1232 * @tc.steps: step2. register app state observer
1233 * @tc.expected: return false, permission denied
1234 */
1235 GTEST_LOG_(INFO)
1236 << "LocatorServiceTest, RegisterAppStateObserver001, TestSize.Level1";
1237 LBSLOGI(LOCATOR, "[LocatorServiceTest] RegisterAppStateObserver001 begin");
1238 bool ret = backgroundProxy_->RegisterAppStateObserver();
1239 EXPECT_EQ(true, ret);
1240 LBSLOGI(LOCATOR, "[LocatorServiceTest] RegisterAppStateObserver001 end");
1241 }
1242
1243 /*
1244 * @tc.name: UnregisterAppStateObserver001
1245 * @tc.desc: Test the function unregister app state observer
1246 * @tc.type: FUNC
1247 * @tc.require: issueI5PX7W
1248 */
1249 HWTEST_F(LocatorServiceTest, UnregisterAppStateObserver001, TestSize.Level1)
1250 {
1251 /*
1252 * @tc.steps: step1. get the request manager
1253 * @tc.steps: step2. unregister app state observer
1254 * @tc.expected: return true, unreg process is success
1255 */
1256 GTEST_LOG_(INFO)
1257 << "LocatorServiceTest, UnregisterAppStateObserver001, TestSize.Level1";
1258 LBSLOGI(LOCATOR, "[LocatorServiceTest] UnregisterAppStateObserver001 begin");
1259 bool ret = backgroundProxy_->UnregisterAppStateObserver();
1260 EXPECT_EQ(true, ret);
1261 LBSLOGI(LOCATOR, "[LocatorServiceTest] UnregisterAppStateObserver001 end");
1262 }
1263
1264 /*
1265 * @tc.name: UpdateListOnRequestChange001
1266 * @tc.desc: Test update list on request change in normal scenario
1267 * @tc.type: FUNC
1268 * @tc.require: issueI5PX7W
1269 */
1270 HWTEST_F(LocatorServiceTest, UpdateListOnRequestChange001, TestSize.Level1)
1271 {
1272 /*
1273 * @tc.steps: step1. get user id
1274 * @tc.steps: step2. get uid by bundle name and userId
1275 */
1276 GTEST_LOG_(INFO)
1277 << "LocatorServiceTest, UpdateListOnRequestChange001, TestSize.Level1";
1278 LBSLOGI(LOCATOR, "[LocatorServiceTest] UpdateListOnRequestChange001 begin");
1279 int32_t userId = 0;
1280 CommonUtils::GetCurrentUserId(userId);
1281
1282 sptr<ISystemAbilityManager> smgr = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
1283 EXPECT_NE(nullptr, smgr);
1284 sptr<IRemoteObject> remoteObject = smgr->GetSystemAbility(BUNDLE_MGR_SERVICE_SYS_ABILITY_ID);
1285 EXPECT_NE(nullptr, remoteObject);
1286 sptr<AppExecFwk::IBundleMgr> bundleMgrProxy(new AppExecFwk::BundleMgrProxy(remoteObject));
1287 EXPECT_NE(nullptr, bundleMgrProxy);
1288 std::string name = "ohos.global.systemres";
1289 int32_t uid = bundleMgrProxy->GetUidByBundleName(name, userId);
1290
1291 LBSLOGD(LOCATOR, "bundleName : %{public}s, uid = %{public}d", name.c_str(), uid);
1292
1293 request_->SetUid(uid);
1294 request_->SetPackageName(name);
1295
1296 /*
1297 * @tc.steps: step3. test update list on request change function
1298 * @tc.expected: normal scenario covered
1299 */
1300 backgroundProxy_->UpdateListOnRequestChange(request_);
1301 LBSLOGI(LOCATOR, "[LocatorServiceTest] UpdateListOnRequestChange001 end");
1302 }
1303
1304 /*
1305 * @tc.name: UpdateListOnRequestChange002
1306 * @tc.desc: Test update list on request change in abnormal scenario
1307 * @tc.type: FUNC
1308 * @tc.require: issueI5PX7W
1309 */
1310 HWTEST_F(LocatorServiceTest, UpdateListOnRequestChange002, TestSize.Level1)
1311 {
1312 /*
1313 * @tc.steps: step1. cannot find bundle name if uid is not restored
1314 * @tc.expected: early return because bundleName can not be found
1315 */
1316 GTEST_LOG_(INFO)
1317 << "LocatorServiceTest, UpdateListOnRequestChange002, TestSize.Level1";
1318 LBSLOGI(LOCATOR, "[LocatorServiceTest] UpdateListOnRequestChange002 begin");
1319 ASSERT_TRUE(backgroundProxy_ != nullptr);
1320 backgroundProxy_->UpdateListOnRequestChange(request_);
1321
1322 /*
1323 * @tc.steps: step2. request is null
1324 * @tc.expected: early return because request is nullptr
1325 */
1326 backgroundProxy_->UpdateListOnRequestChange(nullptr);
1327 LBSLOGI(LOCATOR, "[LocatorServiceTest] UpdateListOnRequestChange002 end");
1328 }
1329
1330 HWTEST_F(LocatorServiceTest, locatorImpl001, TestSize.Level1)
1331 {
1332 GTEST_LOG_(INFO)
1333 << "LocatorServiceTest, locatorImpl001, TestSize.Level1";
1334 LBSLOGI(LOCATOR, "[LocatorServiceTest] locatorImpl001 begin");
1335 auto locatorImpl = Locator::GetInstance();
1336 EXPECT_NE(nullptr, locatorImpl);
1337 locatorImpl->ShowNotification();
1338 locatorImpl->RequestPermission();
1339 locatorImpl->RequestEnableLocation();
1340
1341 locatorImpl->EnableAbility(false);
1342 EXPECT_EQ(false, locatorImpl->IsLocationEnabled());
1343 locatorImpl->EnableAbility(true);
1344 EXPECT_EQ(true, locatorImpl->IsLocationEnabled());
1345
1346 EXPECT_NE(nullptr, locatorImpl->GetCachedLocation());
1347
1348 locatorImpl->SetLocationPrivacyConfirmStatus(1, true);
1349 EXPECT_EQ(true, locatorImpl->IsLocationPrivacyConfirmed(1));
1350 EXPECT_EQ(ERRCODE_INVALID_PARAM, locatorImpl->SetLocationPrivacyConfirmStatus(-1, true));
1351 EXPECT_EQ(false, locatorImpl->IsLocationPrivacyConfirmed(-1));
1352 #ifdef FEATURE_GNSS_SUPPORT
1353 EXPECT_EQ(0, locatorImpl->GetCachedGnssLocationsSize());
1354 EXPECT_EQ(ERRCODE_NOT_SUPPORTED, locatorImpl->FlushCachedGnssLocations());
1355 std::unique_ptr<LocationCommand> command = std::make_unique<LocationCommand>();
1356 command->scenario = SCENE_NAVIGATION;
1357 command->command = "cmd";
1358 EXPECT_EQ(true, locatorImpl->SendCommand(command));
1359 std::unique_ptr<GeofenceRequest> fenceRequest = std::make_unique<GeofenceRequest>();
1360 fenceRequest->scenario = SCENE_NAVIGATION;
1361 GeoFence geofence;
1362 geofence.latitude = 1.0;
1363 geofence.longitude = 2.0;
1364 geofence.radius = 3.0;
1365 geofence.expiration = 4.0;
1366 fenceRequest->geofence = geofence;
1367 EXPECT_EQ(true, locatorImpl->AddFence(fenceRequest));
1368 EXPECT_EQ(true, locatorImpl->RemoveFence(fenceRequest));
1369 #endif
1370 EXPECT_NE(nullptr, locatorImpl->GetIsoCountryCode());
1371
1372 EXPECT_EQ(true, locatorImpl->ProxyUidForFreeze(1000, false));
1373
1374 int timeInterval = 2;
1375 EXPECT_EQ(true, locatorImpl->EnableLocationMock());
1376 std::vector<std::shared_ptr<Location>> locations;
1377 EXPECT_EQ(true, locatorImpl->SetMockedLocations(timeInterval, locations));
1378 EXPECT_EQ(true, locatorImpl->DisableLocationMock());
1379 LBSLOGI(LOCATOR, "[LocatorServiceTest] locatorImpl001 end");
1380 }
1381
1382 #ifdef FEATURE_GEOCODE_SUPPORT
1383 HWTEST_F(LocatorServiceTest, locatorImplGeocodingMock001, TestSize.Level1)
1384 {
1385 GTEST_LOG_(INFO)
1386 << "LocatorServiceTest, locatorImplGeocodingMock001, TestSize.Level1";
1387 LBSLOGI(LOCATOR, "[LocatorServiceTest] locatorImplGeocodingMock001 begin");
1388 auto locatorImpl = Locator::GetInstance();
1389 EXPECT_NE(nullptr, locatorImpl);
1390 EXPECT_EQ(true, locatorImpl->EnableReverseGeocodingMock());
1391 std::vector<std::shared_ptr<GeocodingMockInfo>> mockInfos = SetGeocodingMockInfo();
1392 EXPECT_EQ(true, locatorImpl->SetReverseGeocodingMockInfo(mockInfos));
1393 EXPECT_EQ(true, locatorImpl->DisableReverseGeocodingMock());
1394 LBSLOGI(LOCATOR, "[LocatorServiceTest] locatorImplGeocodingMock001 end");
1395 }
1396 #endif
1397
1398 #ifdef FEATURE_GEOCODE_SUPPORT
1399 HWTEST_F(LocatorServiceTest, locatorImplGetAddressByCoordinate001, TestSize.Level1)
1400 {
1401 GTEST_LOG_(INFO)
1402 << "LocatorServiceTest, locatorImplGetAddressByCoordinate001, TestSize.Level1";
1403 LBSLOGI(LOCATOR, "[LocatorServiceTest] locatorImplGetAddressByCoordinate001 begin");
1404 auto locatorImpl = Locator::GetInstance();
1405 EXPECT_NE(nullptr, locatorImpl);
1406 MessageParcel request001;
1407 std::list<std::shared_ptr<GeoAddress>> geoAddressList001;
1408 EXPECT_EQ(true, locatorImpl->EnableReverseGeocodingMock());
1409
1410 std::vector<std::shared_ptr<GeocodingMockInfo>> mockInfos = SetGeocodingMockInfo();
1411 EXPECT_EQ(true, locatorImpl->SetReverseGeocodingMockInfo(mockInfos));
1412 request001.WriteInterfaceToken(LocatorProxy::GetDescriptor());
1413 request001.WriteDouble(MOCK_LATITUDE); // latitude
1414 request001.WriteDouble(MOCK_LONGITUDE); // longitude
1415 request001.WriteInt32(3); // maxItems
1416 request001.WriteInt32(1); // locale object size = 1
1417 request001.WriteString16(Str8ToStr16("Language")); // locale.getLanguage()
1418 request001.WriteString16(Str8ToStr16("Country")); // locale.getCountry()
1419 request001.WriteString16(Str8ToStr16("Variant")); // locale.getVariant()
1420 request001.WriteString16(Str8ToStr16("")); // ""
1421 locatorImpl->GetAddressByCoordinate(request001, geoAddressList001);
1422 EXPECT_EQ(true, geoAddressList001.empty());
1423 LBSLOGI(LOCATOR, "[LocatorServiceTest] locatorImplGetAddressByCoordinate001 end");
1424 }
1425 #endif
1426
1427 #ifdef FEATURE_GEOCODE_SUPPORT
1428 HWTEST_F(LocatorServiceTest, locatorImplGetAddressByCoordinate002, TestSize.Level1)
1429 {
1430 GTEST_LOG_(INFO)
1431 << "LocatorServiceTest, locatorImplGetAddressByCoordinate002, TestSize.Level1";
1432 LBSLOGI(LOCATOR, "[LocatorServiceTest] locatorImplGetAddressByCoordinate002 begin");
1433 auto locatorImpl = Locator::GetInstance();
1434 EXPECT_NE(nullptr, locatorImpl);
1435 MessageParcel request002;
1436 std::list<std::shared_ptr<GeoAddress>> geoAddressList002;
1437 EXPECT_EQ(true, locatorImpl->DisableReverseGeocodingMock());
1438 request002.WriteInterfaceToken(LocatorProxy::GetDescriptor());
1439 request002.WriteDouble(1.0); // latitude
1440 request002.WriteDouble(2.0); // longitude
1441 request002.WriteInt32(3); // maxItems
1442 request002.WriteInt32(1); // locale object size = 1
1443 request002.WriteString16(Str8ToStr16("Language")); // locale.getLanguage()
1444 request002.WriteString16(Str8ToStr16("Country")); // locale.getCountry()
1445 request002.WriteString16(Str8ToStr16("Variant")); // locale.getVariant()
1446 request002.WriteString16(Str8ToStr16("")); // ""
1447 locatorImpl->GetAddressByCoordinate(request002, geoAddressList002);
1448 EXPECT_EQ(true, geoAddressList002.empty());
1449 LBSLOGI(LOCATOR, "[LocatorServiceTest] locatorImplGetAddressByCoordinate002 end");
1450 }
1451 #endif
1452
1453 #ifdef FEATURE_GEOCODE_SUPPORT
1454 HWTEST_F(LocatorServiceTest, locatorImplGetAddressByLocationName001, TestSize.Level1)
1455 {
1456 GTEST_LOG_(INFO)
1457 << "LocatorServiceTest, locatorImplGetAddressByLocationName001, TestSize.Level1";
1458 LBSLOGI(LOCATOR, "[LocatorServiceTest] locatorImplGetAddressByLocationName001 begin");
1459 auto locatorImpl = Locator::GetInstance();
1460 EXPECT_NE(nullptr, locatorImpl);
1461 MessageParcel request003;
1462 std::list<std::shared_ptr<GeoAddress>> geoAddressList003;
1463 request003.WriteInterfaceToken(LocatorProxy::GetDescriptor());
1464 request003.WriteString16(Str8ToStr16("description")); // description
1465 request003.WriteDouble(1.0); // minLatitude
1466 request003.WriteDouble(2.0); // minLongitude
1467 request003.WriteDouble(3.0); // maxLatitude
1468 request003.WriteDouble(4.0); // maxLongitude
1469 request003.WriteInt32(3); // maxItems
1470 request003.WriteInt32(1); // locale object size = 1
1471 request003.WriteString16(Str8ToStr16("Language")); // locale.getLanguage()
1472 request003.WriteString16(Str8ToStr16("Country")); // locale.getCountry()
1473 request003.WriteString16(Str8ToStr16("Variant")); // locale.getVariant()
1474 request003.WriteString16(Str8ToStr16("")); // ""
1475 locatorImpl->GetAddressByLocationName(request003, geoAddressList003);
1476 EXPECT_EQ(true, geoAddressList003.empty());
1477 LBSLOGI(LOCATOR, "[LocatorServiceTest] locatorImplGetAddressByLocationName001 end");
1478 }
1479 #endif
1480
1481 HWTEST_F(LocatorServiceTest, locatorImplRegisterAndUnregisterCallback001, TestSize.Level1)
1482 {
1483 GTEST_LOG_(INFO)
1484 << "LocatorServiceTest, locatorImplRegisterAndUnregisterCallback001, TestSize.Level1";
1485 LBSLOGI(LOCATOR, "[LocatorServiceTest] locatorImplRegisterAndUnregisterCallback001 begin");
1486 auto locatorImpl = Locator::GetInstance();
1487 EXPECT_NE(nullptr, locatorImpl);
1488 auto switchCallbackHost =
1489 sptr<LocationSwitchCallbackHost>(new (std::nothrow) LocationSwitchCallbackHost());
1490 EXPECT_NE(nullptr, switchCallbackHost);
1491 EXPECT_EQ(true, locatorImpl->RegisterSwitchCallback(switchCallbackHost->AsObject(), 1000));
1492 EXPECT_EQ(true, locatorImpl->UnregisterSwitchCallback(switchCallbackHost->AsObject()));
1493 #ifdef FEATURE_GNSS_SUPPORT
1494 auto gnssCallbackHost =
1495 sptr<GnssStatusCallbackHost>(new (std::nothrow) GnssStatusCallbackHost());
1496 EXPECT_NE(nullptr, gnssCallbackHost);
1497 EXPECT_EQ(true, locatorImpl->RegisterGnssStatusCallback(gnssCallbackHost->AsObject(), 1000));
1498 EXPECT_EQ(true, locatorImpl->UnregisterGnssStatusCallback(gnssCallbackHost->AsObject()));
1499 auto nmeaCallbackHost =
1500 sptr<NmeaMessageCallbackHost>(new (std::nothrow) NmeaMessageCallbackHost());
1501 EXPECT_NE(nullptr, nmeaCallbackHost);
1502 EXPECT_EQ(true, locatorImpl->RegisterNmeaMessageCallback(nmeaCallbackHost->AsObject(), 1000));
1503 EXPECT_EQ(true, locatorImpl->UnregisterNmeaMessageCallback(nmeaCallbackHost->AsObject()));
1504 #endif
1505 auto countryCodeCallbackHost =
1506 sptr<CountryCodeCallbackHost>(new (std::nothrow) CountryCodeCallbackHost());
1507 EXPECT_NE(nullptr, countryCodeCallbackHost);
1508 EXPECT_EQ(true, locatorImpl->RegisterCountryCodeCallback(countryCodeCallbackHost->AsObject(), 1000));
1509 EXPECT_EQ(true, locatorImpl->UnregisterCountryCodeCallback(countryCodeCallbackHost->AsObject()));
1510 #ifdef FEATURE_GNSS_SUPPORT
1511 auto cachedLocationsCallbackHost =
1512 sptr<CachedLocationsCallbackHost>(new (std::nothrow) CachedLocationsCallbackHost());
1513 EXPECT_NE(nullptr, cachedLocationsCallbackHost);
1514 auto cachedCallback = sptr<ICachedLocationsCallback>(cachedLocationsCallbackHost);
1515 EXPECT_NE(nullptr, cachedCallback);
1516 auto request = std::make_unique<CachedGnssLocationsRequest>();
1517 EXPECT_NE(nullptr, request);
1518 request->reportingPeriodSec = 10;
1519 request->wakeUpCacheQueueFull = true;
1520 locatorImpl->RegisterCachedLocationCallback(request, cachedCallback);
1521 locatorImpl->UnregisterCachedLocationCallback(cachedCallback);
1522 #endif
1523 LBSLOGI(LOCATOR, "[LocatorServiceTest] locatorImplRegisterAndUnregisterCallback001 end");
1524 }
1525
1526 HWTEST_F(LocatorServiceTest, locatorServiceStartAndStop001, TestSize.Level1)
1527 {
1528 auto locatorAbility =
1529 sptr<LocatorAbility>(new (std::nothrow) LocatorAbility());
1530 locatorAbility->OnStart();
1531 EXPECT_EQ(ServiceRunningState::STATE_RUNNING, locatorAbility->QueryServiceState());
1532 locatorAbility->OnStart(); // after state running
1533
1534 locatorAbility->OnStop();
1535 EXPECT_EQ(ServiceRunningState::STATE_NOT_START, locatorAbility->QueryServiceState());
1536 locatorAbility->OnStart(); // after stop
1537 EXPECT_EQ(ServiceRunningState::STATE_RUNNING, locatorAbility->QueryServiceState());
1538 }
1539
1540 HWTEST_F(LocatorServiceTest, locatorServiceStartAndStopSA001, TestSize.Level1)
1541 {
1542 GTEST_LOG_(INFO)
1543 << "LocatorServiceTest, locatorServiceStartAndStopSA001, TestSize.Level1";
1544 LBSLOGI(LOCATOR, "[LocatorServiceTest] locatorServiceStartAndStopSA001 begin");
1545 auto locatorAbility =
1546 sptr<LocatorAbility>(new (std::nothrow) LocatorAbility());
1547 locatorAbility->OnAddSystemAbility(UNKNOWN_SERVICE_ID, "device-id");
1548
1549 locatorAbility->RegisterAction();
1550 EXPECT_EQ(true, locatorAbility->isActionRegistered);
1551 locatorAbility->OnAddSystemAbility(COMMON_EVENT_SERVICE_ID, "device-id");
1552
1553 locatorAbility->OnRemoveSystemAbility(UNKNOWN_SERVICE_ID, "device-id");
1554 LBSLOGI(LOCATOR, "[LocatorServiceTest] locatorServiceStartAndStopSA001 end");
1555 }
1556
1557 HWTEST_F(LocatorServiceTest, locatorServiceInitSaAbility001, TestSize.Level1)
1558 {
1559 GTEST_LOG_(INFO)
1560 << "LocatorServiceTest, locatorServiceInitSaAbility001, TestSize.Level1";
1561 LBSLOGI(LOCATOR, "[LocatorServiceTest] locatorServiceInitSaAbility001 begin");
1562 auto locatorAbility =
1563 sptr<LocatorAbility>(new (std::nothrow) LocatorAbility());
1564 ASSERT_TRUE(locatorAbility != nullptr);
1565 locatorAbility->InitSaAbility();
1566 LBSLOGI(LOCATOR, "[LocatorServiceTest] locatorServiceInitSaAbility001 end");
1567 }
1568
1569 HWTEST_F(LocatorServiceTest, locatorServiceInitRequestManager001, TestSize.Level1)
1570 {
1571 GTEST_LOG_(INFO)
1572 << "LocatorServiceTest, locatorServiceInitRequestManager001, TestSize.Level1";
1573 LBSLOGI(LOCATOR, "[LocatorServiceTest] locatorServiceInitRequestManager001 begin");
1574 auto locatorAbility =
1575 sptr<LocatorAbility>(new (std::nothrow) LocatorAbility());
1576 locatorAbility->InitRequestManagerMap();
1577 EXPECT_EQ(REQUEST_MAX_NUM, locatorAbility->GetRequests()->size());
1578 LBSLOGI(LOCATOR, "[LocatorServiceTest] locatorServiceInitRequestManager001 end");
1579 }
1580
1581 HWTEST_F(LocatorServiceTest, locatorServiceUpdateSaAbility001, TestSize.Level1)
1582 {
1583 GTEST_LOG_(INFO)
1584 << "LocatorServiceTest, locatorServiceUpdateSaAbility001, TestSize.Level1";
1585 LBSLOGI(LOCATOR, "[LocatorServiceTest] locatorServiceUpdateSaAbility001 begin");
1586 auto locatorAbility =
1587 sptr<LocatorAbility>(new (std::nothrow) LocatorAbility());
1588 EXPECT_EQ(ERRCODE_SUCCESS, locatorAbility->UpdateSaAbility());
1589 LBSLOGI(LOCATOR, "[LocatorServiceTest] locatorServiceUpdateSaAbility001 end");
1590 }
1591
1592 HWTEST_F(LocatorServiceTest, locatorServiceEnableAndDisable001, TestSize.Level1)
1593 {
1594 GTEST_LOG_(INFO)
1595 << "LocatorServiceTest, locatorServiceEnableAndDisable001, TestSize.Level1";
1596 LBSLOGI(LOCATOR, "[LocatorServiceTest] locatorServiceEnableAndDisable001 begin");
1597 auto locatorAbility =
1598 sptr<LocatorAbility>(new (std::nothrow) LocatorAbility());
1599 int state = DISABLED;
1600 EXPECT_EQ(ERRCODE_SUCCESS, locatorAbility->EnableAbility(true));
1601 EXPECT_EQ(ERRCODE_SUCCESS, locatorAbility->GetSwitchState(state));
1602 EXPECT_EQ(ENABLED, state);
1603
1604 EXPECT_EQ(ERRCODE_SUCCESS, locatorAbility->EnableAbility(false));
1605 EXPECT_EQ(ERRCODE_SUCCESS, locatorAbility->GetSwitchState(state));
1606 EXPECT_EQ(DISABLED, state);
1607
1608 EXPECT_EQ(ERRCODE_SUCCESS, locatorAbility->EnableAbility(true));
1609 EXPECT_EQ(ERRCODE_SUCCESS, locatorAbility->GetSwitchState(state));
1610 EXPECT_EQ(ENABLED, state);
1611 LBSLOGI(LOCATOR, "[LocatorServiceTest] locatorServiceEnableAndDisable001 end");
1612 }
1613
1614 #ifdef FEATURE_GNSS_SUPPORT
1615 HWTEST_F(LocatorServiceTest, locatorServiceCallbackRegAndUnreg001, TestSize.Level1)
1616 {
1617 GTEST_LOG_(INFO)
1618 << "LocatorServiceTest, locatorServiceCallbackRegAndUnreg001, TestSize.Level1";
1619 LBSLOGI(LOCATOR, "[LocatorServiceTest] locatorServiceCallbackRegAndUnreg001 begin");
1620 auto locatorAbility =
1621 sptr<LocatorAbility>(new (std::nothrow) LocatorAbility());
1622 auto cachedLocationsCallbackHost =
1623 sptr<CachedLocationsCallbackHost>(new (std::nothrow) CachedLocationsCallbackHost());
1624 auto cachedCallback = sptr<ICachedLocationsCallback>(cachedLocationsCallbackHost);
1625 auto cachedRequest = std::make_unique<CachedGnssLocationsRequest>();
1626 // uid pid not match locationhub process
1627 EXPECT_EQ(ERRCODE_PERMISSION_DENIED,
1628 locatorAbility->RegisterCachedLocationCallback(cachedRequest, cachedCallback, "unit.test"));
1629
1630 sleep(1);
1631 EXPECT_EQ(ERRCODE_PERMISSION_DENIED,
1632 locatorAbility->UnregisterCachedLocationCallback(cachedCallback)); // uid pid not match locationhub process
1633 sleep(1);
1634 LBSLOGI(LOCATOR, "[LocatorServiceTest] locatorServiceCallbackRegAndUnreg001 end");
1635 }
1636 #endif
1637
1638 HWTEST_F(LocatorServiceTest, locatorServiceSwitchCallback001, TestSize.Level1)
1639 {
1640 GTEST_LOG_(INFO)
1641 << "LocatorServiceTest, locatorServiceSwitchCallback001, TestSize.Level1";
1642 LBSLOGI(LOCATOR, "[LocatorServiceTest] locatorServiceSwitchCallback001 begin");
1643 auto locatorAbility =
1644 sptr<LocatorAbility>(new (std::nothrow) LocatorAbility());
1645 auto switchCallbackHost =
1646 sptr<LocationSwitchCallbackHost>(new (std::nothrow) LocationSwitchCallbackHost());
1647 locatorAbility->OnStart();
1648 EXPECT_EQ(ERRCODE_INVALID_PARAM, locatorAbility->RegisterSwitchCallback(nullptr, SYSTEM_UID));
1649 EXPECT_EQ(ERRCODE_SUCCESS, locatorAbility->RegisterSwitchCallback(switchCallbackHost, SYSTEM_UID));
1650 EXPECT_EQ(ERRCODE_INVALID_PARAM, locatorAbility->UnregisterSwitchCallback(nullptr));
1651 EXPECT_EQ(ERRCODE_SUCCESS, locatorAbility->UnregisterSwitchCallback(switchCallbackHost));
1652 LBSLOGI(LOCATOR, "[LocatorServiceTest] locatorServiceSwitchCallback001 end");
1653 }
1654
1655 #ifdef FEATURE_GNSS_SUPPORT
1656 HWTEST_F(LocatorServiceTest, locatorServiceGnssStatusCallback001, TestSize.Level1)
1657 {
1658 GTEST_LOG_(INFO)
1659 << "LocatorServiceTest, locatorServiceGnssStatusCallback001, TestSize.Level1";
1660 LBSLOGI(LOCATOR, "[LocatorServiceTest] locatorServiceGnssStatusCallback001 begin");
1661 auto locatorAbility =
1662 sptr<LocatorAbility>(new (std::nothrow) LocatorAbility());
1663
1664 auto gnssCallbackHost =
1665 sptr<GnssStatusCallbackHost>(new (std::nothrow) GnssStatusCallbackHost());
1666 // uid pid not match locationhub process
1667 EXPECT_EQ(ERRCODE_PERMISSION_DENIED,
1668 locatorAbility->RegisterGnssStatusCallback(nullptr, SYSTEM_UID)); // invalid callback
1669 EXPECT_EQ(ERRCODE_PERMISSION_DENIED, locatorAbility->RegisterGnssStatusCallback(gnssCallbackHost, SYSTEM_UID));
1670 EXPECT_EQ(ERRCODE_PERMISSION_DENIED, locatorAbility->UnregisterGnssStatusCallback(nullptr)); // invalid callback
1671 EXPECT_EQ(ERRCODE_PERMISSION_DENIED, locatorAbility->UnregisterGnssStatusCallback(gnssCallbackHost));
1672 LBSLOGI(LOCATOR, "[LocatorServiceTest] locatorServiceGnssStatusCallback001 end");
1673 }
1674 #endif
1675
1676 #ifdef FEATURE_GNSS_SUPPORT
1677 HWTEST_F(LocatorServiceTest, locatorServiceNmeaMessageCallback001, TestSize.Level1)
1678 {
1679 GTEST_LOG_(INFO)
1680 << "LocatorServiceTest, locatorServiceNmeaMessageCallback001, TestSize.Level1";
1681 LBSLOGI(LOCATOR, "[LocatorServiceTest] locatorServiceNmeaMessageCallback001 begin");
1682 auto locatorAbility =
1683 sptr<LocatorAbility>(new (std::nothrow) LocatorAbility());
1684 auto nmeaCallbackHost =
1685 sptr<NmeaMessageCallbackHost>(new (std::nothrow) NmeaMessageCallbackHost());
1686 EXPECT_EQ(ERRCODE_PERMISSION_DENIED,
1687 locatorAbility->RegisterNmeaMessageCallback(nullptr, SYSTEM_UID)); // invalid callback
1688 EXPECT_EQ(ERRCODE_PERMISSION_DENIED, locatorAbility->RegisterNmeaMessageCallback(nmeaCallbackHost, SYSTEM_UID));
1689 EXPECT_EQ(ERRCODE_PERMISSION_DENIED, locatorAbility->UnregisterNmeaMessageCallback(nullptr)); // invalid callback
1690 EXPECT_EQ(ERRCODE_PERMISSION_DENIED, locatorAbility->UnregisterNmeaMessageCallback(nmeaCallbackHost));
1691 LBSLOGI(LOCATOR, "[LocatorServiceTest] locatorServiceNmeaMessageCallback001 end");
1692 }
1693 #endif
1694
1695 HWTEST_F(LocatorServiceTest, locatorServicePrivacyConfirmStatus001, TestSize.Level1)
1696 {
1697 GTEST_LOG_(INFO)
1698 << "LocatorServiceTest, locatorServicePrivacyConfirmStatus001, TestSize.Level1";
1699 LBSLOGI(LOCATOR, "[LocatorServiceTest] locatorServicePrivacyConfirmStatus001 begin");
1700 auto locatorAbility =
1701 sptr<LocatorAbility>(new (std::nothrow) LocatorAbility());
1702 EXPECT_EQ(ERRCODE_SUCCESS,
1703 locatorAbility->SetLocationPrivacyConfirmStatus(PRIVACY_TYPE_STARTUP, true));
1704 bool isConfirmed = false;
1705 EXPECT_EQ(ERRCODE_SUCCESS, locatorAbility->IsLocationPrivacyConfirmed(PRIVACY_TYPE_STARTUP, isConfirmed));
1706 EXPECT_EQ(true, isConfirmed);
1707 LBSLOGI(LOCATOR, "[LocatorServiceTest] locatorServicePrivacyConfirmStatus001 end");
1708 }
1709
1710 #ifdef FEATURE_GNSS_SUPPORT
1711 HWTEST_F(LocatorServiceTest, locatorServiceSendCommand001, TestSize.Level1)
1712 {
1713 GTEST_LOG_(INFO)
1714 << "LocatorServiceTest, locatorServiceSendCommand001, TestSize.Level1";
1715 LBSLOGI(LOCATOR, "[LocatorServiceTest] locatorServiceSendCommand001 begin");
1716 auto locatorAbility =
1717 sptr<LocatorAbility>(new (std::nothrow) LocatorAbility());
1718 std::unique_ptr<LocationCommand> command = std::make_unique<LocationCommand>();
1719 command->scenario = SCENE_NAVIGATION;
1720 command->command = "cmd";
1721 // uid pid not match locationhub process
1722 EXPECT_EQ(ERRCODE_PERMISSION_DENIED, locatorAbility->SendCommand(command));
1723 LBSLOGI(LOCATOR, "[LocatorServiceTest] locatorServiceSendCommand001 end");
1724 }
1725 #endif
1726
1727 #ifdef FEATURE_GNSS_SUPPORT
1728 HWTEST_F(LocatorServiceTest, locatorServiceFence001, TestSize.Level1)
1729 {
1730 GTEST_LOG_(INFO)
1731 << "LocatorServiceTest, locatorServiceFence001, TestSize.Level1";
1732 LBSLOGI(LOCATOR, "[LocatorServiceTest] locatorServiceFence001 begin");
1733 auto locatorAbility =
1734 sptr<LocatorAbility>(new (std::nothrow) LocatorAbility());
1735 std::unique_ptr<GeofenceRequest> fenceRequest = std::make_unique<GeofenceRequest>();
1736 fenceRequest->scenario = SCENE_NAVIGATION;
1737 GeoFence geofence;
1738 geofence.latitude = 1.0;
1739 geofence.longitude = 2.0;
1740 geofence.radius = 3.0;
1741 geofence.expiration = 4.0;
1742 fenceRequest->geofence = geofence;
1743 // uid pid not match locationhub process
1744 EXPECT_EQ(ERRCODE_PERMISSION_DENIED, locatorAbility->AddFence(fenceRequest));
1745 EXPECT_EQ(ERRCODE_PERMISSION_DENIED, locatorAbility->RemoveFence(fenceRequest));
1746 LBSLOGI(LOCATOR, "[LocatorServiceTest] locatorServiceFence001 end");
1747 }
1748 #endif
1749
1750 HWTEST_F(LocatorServiceTest, locatorServiceLocationMock001, TestSize.Level1)
1751 {
1752 GTEST_LOG_(INFO)
1753 << "LocatorServiceTest, locatorServiceLocationMock001, TestSize.Level1";
1754 LBSLOGI(LOCATOR, "[LocatorServiceTest] locatorServiceLocationMock001 begin");
1755 auto locatorAbility =
1756 sptr<LocatorAbility>(new (std::nothrow) LocatorAbility());
1757 int timeInterval = 2;
1758 EXPECT_EQ(ERRCODE_SUCCESS, locatorAbility->EnableLocationMock());
1759 std::vector<std::shared_ptr<Location>> locations;
1760 EXPECT_EQ(ERRCODE_SUCCESS, locatorAbility->SetMockedLocations(timeInterval, locations));
1761
1762 EXPECT_EQ(ERRCODE_SUCCESS, locatorAbility->DisableLocationMock());
1763 EXPECT_EQ(ERRCODE_SUCCESS, locatorAbility->SetMockedLocations(timeInterval, locations));
1764 LBSLOGI(LOCATOR, "[LocatorServiceTest] locatorServiceLocationMock001 end");
1765 }
1766
1767 HWTEST_F(LocatorServiceTest, locatorServiceReportLocationStatus001, TestSize.Level1)
1768 {
1769 GTEST_LOG_(INFO)
1770 << "LocatorServiceTest, locatorServiceReportLocationStatus001, TestSize.Level1";
1771 LBSLOGI(LOCATOR, "[LocatorServiceTest] locatorServiceReportLocationStatus001 begin");
1772 auto locatorAbility =
1773 sptr<LocatorAbility>(new (std::nothrow) LocatorAbility());
1774 int state = DISABLED;
1775 EXPECT_EQ(ERRCODE_SUCCESS, locatorAbility->GetSwitchState(state));
1776 if (state == ENABLED) {
1777 EXPECT_EQ(REPLY_CODE_NO_EXCEPTION, locatorAbility->ReportLocationStatus(callbackStub_, 0));
1778 } else {
1779 EXPECT_EQ(REPLY_CODE_EXCEPTION, locatorAbility->ReportLocationStatus(callbackStub_, 0));
1780 }
1781 LBSLOGI(LOCATOR, "[LocatorServiceTest] locatorServiceReportLocationStatus001 end");
1782 }
1783
1784 HWTEST_F(LocatorServiceTest, locatorServiceReportErrorStatus001, TestSize.Level1)
1785 {
1786 GTEST_LOG_(INFO)
1787 << "LocatorServiceTest, locatorServiceReportErrorStatus001, TestSize.Level1";
1788 LBSLOGI(LOCATOR, "[LocatorServiceTest] locatorServiceReportErrorStatus001 begin");
1789 auto locatorAbility =
1790 sptr<LocatorAbility>(new (std::nothrow) LocatorAbility());
1791 int state = DISABLED;
1792 EXPECT_EQ(ERRCODE_SUCCESS, locatorAbility->GetSwitchState(state));
1793 if (state == ENABLED) {
1794 EXPECT_EQ(REPLY_CODE_NO_EXCEPTION, locatorAbility->ReportErrorStatus(callbackStub_, 0));
1795 } else {
1796 EXPECT_EQ(REPLY_CODE_EXCEPTION, locatorAbility->ReportErrorStatus(callbackStub_, 0));
1797 }
1798 LBSLOGI(LOCATOR, "[LocatorServiceTest] locatorServiceReportErrorStatus001 end");
1799 }
1800
1801 HWTEST_F(LocatorServiceTest, locatorServiceGetReceivers001, TestSize.Level1)
1802 {
1803 GTEST_LOG_(INFO)
1804 << "LocatorServiceTest, locatorServiceGetReceivers001, TestSize.Level1";
1805 LBSLOGI(LOCATOR, "[LocatorServiceTest] locatorServiceGetReceivers001 begin");
1806 auto locatorAbility =
1807 sptr<LocatorAbility>(new (std::nothrow) LocatorAbility());
1808 EXPECT_EQ(0, locatorAbility->GetReceivers()->size());
1809 LBSLOGI(LOCATOR, "[LocatorServiceTest] locatorServiceGetReceivers001 end");
1810 }
1811
1812 HWTEST_F(LocatorServiceTest, locatorServiceProxyUidForFreeze001, TestSize.Level1)
1813 {
1814 GTEST_LOG_(INFO)
1815 << "LocatorServiceTest, locatorServiceProxyUidForFreeze001, TestSize.Level1";
1816 LBSLOGI(LOCATOR, "[LocatorServiceTest] locatorServiceProxyUidForFreeze001 begin");
1817 auto locatorAbility =
1818 sptr<LocatorAbility>(new (std::nothrow) LocatorAbility());
1819 EXPECT_EQ(ERRCODE_SUCCESS, locatorAbility->ProxyUidForFreeze(SYSTEM_UID, true));
1820 EXPECT_EQ(true, locatorAbility->IsProxyUid(SYSTEM_UID));
1821 EXPECT_EQ(ERRCODE_SUCCESS, locatorAbility->ProxyUidForFreeze(SYSTEM_UID, false));
1822 EXPECT_EQ(false, locatorAbility->IsProxyUid(SYSTEM_UID));
1823
1824 EXPECT_EQ(ERRCODE_SUCCESS, locatorAbility->ProxyUidForFreeze(SYSTEM_UID, true));
1825 EXPECT_EQ(true, locatorAbility->IsProxyUid(SYSTEM_UID));
1826 EXPECT_EQ(ERRCODE_SUCCESS, locatorAbility->ResetAllProxy());
1827 EXPECT_EQ(false, locatorAbility->IsProxyUid(SYSTEM_UID));
1828 LBSLOGI(LOCATOR, "[LocatorServiceTest] locatorServiceProxyUidForFreeze001 end");
1829 }
1830
1831 HWTEST_F(LocatorServiceTest, LocatorAbilityStubDump001, TestSize.Level1)
1832 {
1833 GTEST_LOG_(INFO)
1834 << "LocatorServiceTest, LocatorAbilityStubDump001, TestSize.Level1";
1835 LBSLOGI(LOCATOR, "[LocatorServiceTest] LocatorAbilityStubDump001 begin");
1836 auto locatorAbility = sptr<LocatorAbility>(new (std::nothrow) LocatorAbility());
1837 int32_t fd = 0;
1838 std::vector<std::u16string> args;
1839 std::u16string arg1 = Str8ToStr16("arg1");
1840 args.emplace_back(arg1);
1841 std::u16string arg2 = Str8ToStr16("arg2");
1842 args.emplace_back(arg2);
1843 std::u16string arg3 = Str8ToStr16("arg3");
1844 args.emplace_back(arg3);
1845 std::u16string arg4 = Str8ToStr16("arg4");
1846 args.emplace_back(arg4);
1847 EXPECT_EQ(ERR_OK, locatorAbility->Dump(fd, args));
1848
1849 std::vector<std::u16string> emptyArgs;
1850 EXPECT_EQ(ERR_OK, locatorAbility->Dump(fd, emptyArgs));
1851
1852 std::vector<std::u16string> helpArgs;
1853 std::u16string helpArg1 = Str8ToStr16(ARGS_HELP);
1854 helpArgs.emplace_back(helpArg1);
1855 EXPECT_EQ(ERR_OK, locatorAbility->Dump(fd, helpArgs));
1856 LBSLOGI(LOCATOR, "[LocatorServiceTest] LocatorAbilityStubDump001 end");
1857 }
1858
1859 HWTEST_F(LocatorServiceTest, LocatorAbilityGetProxyMap001, TestSize.Level1)
1860 {
1861 GTEST_LOG_(INFO)
1862 << "LocatorServiceTest, LocatorAbilityGetProxyMap001, TestSize.Level1";
1863 LBSLOGI(LOCATOR, "[LocatorServiceTest] LocatorAbilityGetProxyMap001 begin");
1864 auto locatorAbility = sptr<LocatorAbility>(new (std::nothrow) LocatorAbility());
1865 ASSERT_TRUE(locatorAbility != nullptr);
1866 locatorAbility->GetProxyMap();
1867 LBSLOGI(LOCATOR, "[LocatorServiceTest] LocatorAbilityGetProxyMap001 end");
1868 }
1869
1870 HWTEST_F(LocatorServiceTest, LocatorAbilityGetProxyMap002, TestSize.Level1)
1871 {
1872 GTEST_LOG_(INFO)
1873 << "LocatorServiceTest, LocatorAbilityGetProxyMap002, TestSize.Level1";
1874 LBSLOGI(LOCATOR, "[LocatorServiceTest] LocatorAbilityGetProxyMap002 begin");
1875 auto locatorAbility = sptr<LocatorAbility>(new (std::nothrow) LocatorAbility());
1876 ASSERT_TRUE(locatorAbility != nullptr);
1877 locatorAbility->InitRequestManagerMap();
1878 LBSLOGI(LOCATOR, "[LocatorServiceTest] LocatorAbilityGetProxyMap002 end");
1879 }
1880
1881 HWTEST_F(LocatorServiceTest, locatorServicePreProxyUidForFreeze001, TestSize.Level1)
1882 {
1883 GTEST_LOG_(INFO)
1884 << "LocatorServiceTest, locatorServiceProxyUidForFreeze001, TestSize.Level1";
1885 LBSLOGI(LOCATOR, "[LocatorServiceTest] locatorServiceProxyUidForFreeze001 begin");
1886 auto locatorAbility =
1887 sptr<LocatorAbility>(new (std::nothrow) LocatorAbility());
1888 AppIdentity identity;
1889 MessageParcel data;
1890 MessageParcel reply;
1891 identity.SetUid(100);
1892 EXPECT_EQ(ERRCODE_PERMISSION_DENIED, locatorAbility->PreProxyUidForFreeze(data, reply, identity));
1893 EXPECT_EQ(ERRCODE_PERMISSION_DENIED, locatorAbility->PreResetAllProxy(data, reply, identity));
1894 LBSLOGI(LOCATOR, "[LocatorServiceTest] locatorServiceProxyUidForFreeze001 end");
1895 }
1896
1897 HWTEST_F(LocatorServiceTest, locatorServiceOnAddSystemAbility001, TestSize.Level1)
1898 {
1899 GTEST_LOG_(INFO)
1900 << "LocatorServiceTest, locatorServiceOnAddSystemAbility001, TestSize.Level1";
1901 LBSLOGI(LOCATOR, "[LocatorServiceTest] locatorServiceOnAddSystemAbility001 begin");
1902 auto locatorAbility =
1903 sptr<LocatorAbility>(new (std::nothrow) LocatorAbility());
1904 locatorAbility->locatorEventSubscriber_ = nullptr;
1905 locatorAbility->OnAddSystemAbility(COMMON_EVENT_SERVICE_ID, "device-id");
1906 LBSLOGI(LOCATOR, "[LocatorServiceTest] locatorServiceOnAddSystemAbility001 end");
1907 }
1908
1909 HWTEST_F(LocatorServiceTest, locatorServiceOnRemoveSystemAbility001, TestSize.Level1)
1910 {
1911 GTEST_LOG_(INFO)
1912 << "LocatorServiceTest, locatorServiceOnRemoveSystemAbility001, TestSize.Level1";
1913 LBSLOGI(LOCATOR, "[LocatorServiceTest] locatorServiceOnRemoveSystemAbility001 begin");
1914 auto locatorAbility =
1915 sptr<LocatorAbility>(new (std::nothrow) LocatorAbility());
1916 locatorAbility->locatorEventSubscriber_ = nullptr;
1917 locatorAbility->OnRemoveSystemAbility(COMMON_EVENT_SERVICE_ID, "device-id");
1918
1919 OHOS::EventFwk::MatchingSkills matchingSkills;
1920 matchingSkills.AddEvent(MODE_CHANGED_EVENT);
1921 OHOS::EventFwk::CommonEventSubscribeInfo subscriberInfo(matchingSkills);
1922 locatorAbility->locatorEventSubscriber_ = std::make_shared<LocatorEventSubscriber>(subscriberInfo);
1923 locatorAbility->OnRemoveSystemAbility(COMMON_EVENT_SERVICE_ID, "device-id");
1924 LBSLOGI(LOCATOR, "[LocatorServiceTest] locatorServiceOnRemoveSystemAbility001 end");
1925 }
1926
1927 HWTEST_F(LocatorServiceTest, locatorServiceInit001, TestSize.Level1)
1928 {
1929 GTEST_LOG_(INFO)
1930 << "LocatorServiceTest, locatorServiceInit001, TestSize.Level1";
1931 LBSLOGI(LOCATOR, "[LocatorServiceTest] locatorServiceInit001 begin");
1932 auto locatorAbility =
1933 sptr<LocatorAbility>(new (std::nothrow) LocatorAbility());
1934 locatorAbility->registerToAbility_ = true;
1935 locatorAbility->Init();
1936 LBSLOGI(LOCATOR, "[LocatorServiceTest] locatorServiceInit001 end");
1937 }
1938
1939 HWTEST_F(LocatorServiceTest, locatorServiceInitRequestManagerMap001, TestSize.Level1)
1940 {
1941 GTEST_LOG_(INFO)
1942 << "LocatorServiceTest, locatorServiceInitRequestManagerMap001, TestSize.Level1";
1943 LBSLOGI(LOCATOR, "[LocatorServiceTest] locatorServiceInitRequestManagerMap001 begin");
1944 auto locatorAbility =
1945 sptr<LocatorAbility>(new (std::nothrow) LocatorAbility());
1946 locatorAbility->requests_ = nullptr;
1947 locatorAbility->InitRequestManagerMap();
1948 locatorAbility->requests_ = std::make_shared<std::map<std::string, std::list<std::shared_ptr<Request>>>>();
1949 locatorAbility->GetActiveRequestNum();
1950 LBSLOGI(LOCATOR, "[LocatorServiceTest] locatorServiceInitRequestManagerMap001 end");
1951 }
1952
1953 HWTEST_F(LocatorServiceTest, locatorServiceInitSaAbility002, TestSize.Level1)
1954 {
1955 GTEST_LOG_(INFO)
1956 << "LocatorServiceTest, locatorServiceInitSaAbility002, TestSize.Level1";
1957 LBSLOGI(LOCATOR, "[LocatorServiceTest] locatorServiceInitSaAbility002 begin");
1958 auto locatorAbility =
1959 sptr<LocatorAbility>(new (std::nothrow) LocatorAbility());
1960 locatorAbility->proxyMap_ = nullptr;
1961 locatorAbility->InitSaAbility();
1962
1963 locatorAbility->proxyMap_ = std::make_shared<std::map<std::string, sptr<IRemoteObject>>>();
1964 locatorAbility->InitSaAbility();
1965 LBSLOGI(LOCATOR, "[LocatorServiceTest] locatorServiceInitSaAbility002 end");
1966 }
1967
1968 HWTEST_F(LocatorServiceTest, locatorServiceSendLocationMockMsgToGnssSa001, TestSize.Level1)
1969 {
1970 GTEST_LOG_(INFO)
1971 << "LocatorServiceTest, locatorServiceSendLocationMockMsgToGnssSa001, TestSize.Level1";
1972 LBSLOGI(LOCATOR, "[LocatorServiceTest] locatorServiceSendLocationMockMsgToGnssSa001 begin");
1973 auto locatorAbility =
1974 sptr<LocatorAbility>(new (std::nothrow) LocatorAbility());
1975 std::vector<std::shared_ptr<OHOS::Location::Location>> locations;
1976 locatorAbility->SendLocationMockMsgToGnssSa(nullptr, 0, locations, 0);
1977 locatorAbility->SendLocationMockMsgToNetworkSa(nullptr, 0, locations, 0);
1978 locatorAbility->SendLocationMockMsgToPassiveSa(nullptr, 0, locations, 0);
1979 LBSLOGI(LOCATOR, "[LocatorServiceTest] locatorServiceSendLocationMockMsgToGnssSa001 end");
1980 }
1981
1982 HWTEST_F(LocatorServiceTest, locatorServiceStartLocating001, TestSize.Level1)
1983 {
1984 GTEST_LOG_(INFO)
1985 << "LocatorServiceTest, locatorServiceStartLocating001, TestSize.Level1";
1986 LBSLOGI(LOCATOR, "[LocatorServiceTest] locatorServiceStartLocating001 begin");
1987 auto locatorAbility =
1988 sptr<LocatorAbility>(new (std::nothrow) LocatorAbility());
1989
1990 locatorAbility->proxyMap_ = std::make_shared<std::map<std::string, sptr<IRemoteObject>>>();
1991 std::unique_ptr<RequestConfig> requestConfig = std::make_unique<RequestConfig>();
1992 sptr<ILocatorCallback> callbackStub = new (std::nothrow) LocatorCallbackStub();
1993 AppIdentity identity;
1994 locatorAbility->StartLocating(requestConfig, callbackStub, identity);
1995
1996 sptr<IRemoteObject> objectGnss = CommonUtils::GetRemoteObject(LOCATION_GNSS_SA_ID, CommonUtils::InitDeviceId());
1997 locatorAbility->proxyMap_->insert(make_pair(GNSS_ABILITY, objectGnss));
1998 locatorAbility->reportManager_ = nullptr;
1999 locatorAbility->requestManager_ = nullptr;
2000 locatorAbility->StartLocating(requestConfig, callbackStub, identity);
2001 LBSLOGI(LOCATOR, "[LocatorServiceTest] locatorServiceStartLocating001 end");
2002 }
2003
2004 HWTEST_F(LocatorServiceTest, locatorServiceGetCacheLocation001, TestSize.Level1)
2005 {
2006 GTEST_LOG_(INFO)
2007 << "LocatorServiceTest, locatorServiceGetCacheLocation001, TestSize.Level1";
2008 LBSLOGI(LOCATOR, "[LocatorServiceTest] locatorServiceGetCacheLocation001 begin");
2009 auto locatorAbility =
2010 sptr<LocatorAbility>(new (std::nothrow) LocatorAbility());
2011 AppIdentity identity;
2012 MessageParcel parcel;
2013 parcel.WriteDouble(0.0); // latitude
2014 parcel.WriteDouble(0.0); // longitude
2015 parcel.WriteDouble(14.0); // altitude
2016 parcel.WriteDouble(1000.0); // accuracy
2017 parcel.WriteDouble(10.0); // speed
2018 parcel.WriteDouble(90.0); // direction
2019 parcel.WriteInt64(1000000000); // timeStamp
2020 parcel.WriteInt64(1000000000); // timeSinceBoot
2021 parcel.WriteString16(u"additions"); // additions
2022 parcel.WriteInt64(1); // additionSize
2023 parcel.WriteBool(false); // isFromMock
2024 parcel.WriteInt32(1); // source type
2025 parcel.WriteInt32(0); // floor no.
2026 parcel.WriteDouble(1000.0); // floor acc
2027 std::unique_ptr<Location> location = std::make_unique<Location>();
2028 location->ReadFromParcel(parcel);
2029 EXPECT_EQ(ERRCODE_LOCATING_FAIL, locatorAbility->GetCacheLocation(location, identity));
2030 LBSLOGI(LOCATOR, "[LocatorServiceTest] locatorServiceGetCacheLocation001 end");
2031 }
2032
2033 HWTEST_F(LocatorServiceTest, locatorServiceReportLocation001, TestSize.Level1)
2034 {
2035 GTEST_LOG_(INFO)
2036 << "LocatorServiceTest, locatorServiceReportLocation001, TestSize.Level1";
2037 LBSLOGI(LOCATOR, "[LocatorServiceTest] locatorServiceReportLocation001 begin");
2038 auto locatorAbility =
2039 sptr<LocatorAbility>(new (std::nothrow) LocatorAbility());
2040 AppIdentity identity;
2041 locatorAbility->requests_ = nullptr;
2042 std::unique_ptr<Location> location = std::make_unique<Location>();
2043 EXPECT_EQ(ERRCODE_SERVICE_UNAVAILABLE, locatorAbility->ReportLocation(location, "test"));
2044 LBSLOGI(LOCATOR, "[LocatorServiceTest] locatorServiceReportLocation001 end");
2045 }
2046
2047 HWTEST_F(LocatorServiceTest, locatorServiceRegisterPermissionCallback001, TestSize.Level1)
2048 {
2049 GTEST_LOG_(INFO)
2050 << "LocatorServiceTest, locatorServiceReportLocation001, TestSize.Level1";
2051 LBSLOGI(LOCATOR, "[LocatorServiceTest] locatorServiceReportLocation001 begin");
2052 auto locatorAbility =
2053 sptr<LocatorAbility>(new (std::nothrow) LocatorAbility());
2054
2055 locatorAbility->permissionMap_ = nullptr;
2056 std::vector<std::string> permissionNameList;
2057 locatorAbility->RegisterPermissionCallback(0, permissionNameList);
2058 locatorAbility->UnregisterPermissionCallback(0);
2059 LBSLOGI(LOCATOR, "[LocatorServiceTest] locatorServiceReportLocation001 end");
2060 }
2061 } // namespace Location
2062 } // namespace OHOS
2063