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