• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #include "locator_background_proxy_test.h"
17 
18 #include "accesstoken_kit.h"
19 #include "app_state_data.h"
20 #include "bundle_mgr_interface.h"
21 #include "bundle_mgr_proxy.h"
22 #include "common_event_manager.h"
23 #include "common_event_subscriber.h"
24 #include "common_event_support.h"
25 #include "if_system_ability_manager.h"
26 #include "ipc_skeleton.h"
27 #include "iservice_registry.h"
28 #include "message_parcel.h"
29 #include "nativetoken_kit.h"
30 #include "system_ability_definition.h"
31 #include "token_setproc.h"
32 #include "want.h"
33 
34 #include "common_utils.h"
35 #include "constant_definition.h"
36 #include "location.h"
37 #include "location_log.h"
38 #include "locator_callback_host.h"
39 #include "locator_callback_proxy.h"
40 
41 using namespace testing::ext;
42 namespace OHOS {
43 namespace Location {
44 using Want = OHOS::AAFwk::Want;
45 const int32_t LOCATION_PERM_NUM = 4;
46 const int VAL_UID = 20010044;
SetUp()47 void LocatorBackgroundProxyTest::SetUp()
48 {
49     MockNativePermission();
50 }
51 
TearDown()52 void LocatorBackgroundProxyTest::TearDown()
53 {
54 }
55 
MockNativePermission()56 void LocatorBackgroundProxyTest::MockNativePermission()
57 {
58     const char *perms[] = {
59         ACCESS_LOCATION.c_str(), ACCESS_APPROXIMATELY_LOCATION.c_str(),
60         ACCESS_BACKGROUND_LOCATION.c_str(), MANAGE_SECURE_SETTINGS.c_str(),
61     };
62     NativeTokenInfoParams infoInstance = {
63         .dcapsNum = 0,
64         .permsNum = LOCATION_PERM_NUM,
65         .aclsNum = 0,
66         .dcaps = nullptr,
67         .perms = perms,
68         .acls = nullptr,
69         .processName = "LocatorTest",
70         .aplStr = "system_basic",
71     };
72     tokenId_ = GetAccessTokenId(&infoInstance);
73     SetSelfTokenID(tokenId_);
74     Security::AccessToken::AccessTokenKit::ReloadNativeTokenInfo();
75 }
76 
77 HWTEST_F(LocatorBackgroundProxyTest, AppStateChangeCallbackTest001, TestSize.Level1)
78 {
79     GTEST_LOG_(INFO)
80         << "LocatorBackgroundProxyTest, AppStateChangeCallbackTest001, TestSize.Level1";
81     LBSLOGI(LOCATOR_BACKGROUND_PROXY, "[LocatorBackgroundProxyTest] AppStateChangeCallbackTest001 begin");
82     auto appStateObserver =
83         sptr<AppStateChangeCallback>(new (std::nothrow) AppStateChangeCallback());
84     int32_t state = static_cast<int32_t>(AppExecFwk::ApplicationState::APP_STATE_FOREGROUND);
85     AppExecFwk::AppStateData appStateData;
86     appStateData.uid = 1;
87     appStateData.pid = 0;
88     appStateData.state = state;
89     appStateObserver->OnForegroundApplicationChanged(appStateData);
90     LBSLOGI(LOCATOR_BACKGROUND_PROXY, "[LocatorBackgroundProxyTest] AppStateChangeCallbackTest001 end");
91 }
92 
93 HWTEST_F(LocatorBackgroundProxyTest, UpdateListOnRequestChangeTest001, TestSize.Level1)
94 {
95     GTEST_LOG_(INFO)
96         << "LocatorBackgroundProxyTest, UpdateListOnRequestChangeTest001, TestSize.Level1";
97     LBSLOGI(LOCATOR_BACKGROUND_PROXY, "[LocatorBackgroundProxyTest] UpdateListOnRequestChangeTest001 begin");
98     auto locatorBackgroundProxy = DelayedSingleton<LocatorBackgroundProxy>::GetInstance();
99     EXPECT_NE(nullptr, locatorBackgroundProxy);
100     std::shared_ptr<Request> request1 = std::make_shared<Request>();
101     request1->SetUid(1000);
102     request1->SetPid(0);
103     request1->SetTokenId(tokenId_);
104     request1->SetFirstTokenId(0);
105     request1->SetPackageName("LocatorBackgroundProxyTest");
106     locatorBackgroundProxy->UpdateListOnRequestChange(request1);
107     LBSLOGI(LOCATOR_BACKGROUND_PROXY, "[LocatorBackgroundProxyTest] UpdateListOnRequestChangeTest001 end");
108 }
109 
110 HWTEST_F(LocatorBackgroundProxyTest, UpdateListOnRequestChangeTest002, TestSize.Level1)
111 {
112     GTEST_LOG_(INFO)
113         << "LocatorBackgroundProxyTest, UpdateListOnRequestChangeTest002, TestSize.Level1";
114     LBSLOGI(LOCATOR_BACKGROUND_PROXY, "[LocatorBackgroundProxyTest] UpdateListOnRequestChangeTest002 begin");
115     auto locatorBackgroundProxy = DelayedSingleton<LocatorBackgroundProxy>::GetInstance();
116     EXPECT_NE(nullptr, locatorBackgroundProxy);
117     locatorBackgroundProxy->UpdateListOnRequestChange(nullptr);
118     LBSLOGI(LOCATOR_BACKGROUND_PROXY, "[LocatorBackgroundProxyTest] UpdateListOnRequestChangeTest002 end");
119 }
120 
121 HWTEST_F(LocatorBackgroundProxyTest, OnSuspendTest001, TestSize.Level1)
122 {
123     GTEST_LOG_(INFO)
124         << "LocatorBackgroundProxyTest, OnSuspendTest001, TestSize.Level1";
125     LBSLOGI(LOCATOR_BACKGROUND_PROXY, "[LocatorBackgroundProxyTest] OnSuspendTest001 begin");
126     auto locatorBackgroundProxy = DelayedSingleton<LocatorBackgroundProxy>::GetInstance();
127     EXPECT_NE(nullptr, locatorBackgroundProxy);
128     std::shared_ptr<Request> request1 = std::make_shared<Request>();
129     request1->SetUid(1000);
130     request1->SetPid(0);
131     request1->SetTokenId(tokenId_);
132     request1->SetFirstTokenId(0);
133     request1->SetPackageName("LocatorBackgroundProxyTest");
134     locatorBackgroundProxy->OnSuspend(request1, true); // cant find uid in requestMap
135 
136     locatorBackgroundProxy->OnSuspend(request1, false); // cant find uid in requestMap
137     LBSLOGI(LOCATOR_BACKGROUND_PROXY, "[LocatorBackgroundProxyTest] OnSuspendTest001 end");
138 }
139 
140 HWTEST_F(LocatorBackgroundProxyTest, OnSuspendTest002, TestSize.Level1)
141 {
142     GTEST_LOG_(INFO)
143         << "LocatorBackgroundProxyTest, OnSuspendTest002, TestSize.Level1";
144     LBSLOGI(LOCATOR_BACKGROUND_PROXY, "[LocatorBackgroundProxyTest] OnSuspendTest002 begin");
145     auto locatorBackgroundProxy = DelayedSingleton<LocatorBackgroundProxy>::GetInstance();
146     EXPECT_NE(nullptr, locatorBackgroundProxy);
147     std::shared_ptr<Request> request1 = std::make_shared<Request>();
148     request1->SetUid(VAL_UID);
149     request1->SetPid(0);
150     request1->SetTokenId(tokenId_);
151     request1->SetFirstTokenId(0);
152     request1->SetPackageName("name");
153     auto requestConfig = std::make_unique<RequestConfig>();
154     EXPECT_NE(nullptr, requestConfig);
155     requestConfig->SetPriority(PRIORITY_FAST_FIRST_FIX);
156     requestConfig->SetFixNumber(0);
157     request1->SetRequestConfig(*requestConfig);
158     locatorBackgroundProxy->OnSuspend(request1, true); // cant find request in list
159     locatorBackgroundProxy->OnSuspend(request1, false); // add to requestsList
160     locatorBackgroundProxy->OnSuspend(request1, false); // max num is 1, cant add request
161     locatorBackgroundProxy->OnSuspend(request1, true); // remove from requestList
162     LBSLOGI(LOCATOR_BACKGROUND_PROXY, "[LocatorBackgroundProxyTest] OnSuspendTest002 end");
163 }
164 
165 HWTEST_F(LocatorBackgroundProxyTest, OnSuspendTest003, TestSize.Level1)
166 {
167     GTEST_LOG_(INFO)
168         << "LocatorBackgroundProxyTest, OnSuspendTest003, TestSize.Level1";
169     LBSLOGI(LOCATOR_BACKGROUND_PROXY, "[LocatorBackgroundProxyTest] OnSuspendTest003 begin");
170     auto locatorBackgroundProxy = DelayedSingleton<LocatorBackgroundProxy>::GetInstance();
171     EXPECT_NE(nullptr, locatorBackgroundProxy);
172     std::shared_ptr<Request> request1 = std::make_shared<Request>();
173     request1->SetUid(VAL_UID);
174     request1->SetPid(0);
175     request1->SetTokenId(tokenId_);
176     request1->SetFirstTokenId(0);
177     request1->SetPackageName("name");
178     auto requestConfig = std::make_unique<RequestConfig>();
179     EXPECT_NE(nullptr, requestConfig);
180     requestConfig->SetPriority(PRIORITY_FAST_FIRST_FIX);
181     requestConfig->SetFixNumber(1); // fix number is 1
182     request1->SetRequestConfig(*requestConfig);
183     locatorBackgroundProxy->OnSuspend(request1, false); // add to requestsList
184     LBSLOGI(LOCATOR_BACKGROUND_PROXY, "[LocatorBackgroundProxyTest] OnSuspendTest003 end");
185 }
186 
187 HWTEST_F(LocatorBackgroundProxyTest, OnSuspendTest004, TestSize.Level1)
188 {
189     GTEST_LOG_(INFO)
190         << "LocatorBackgroundProxyTest, OnSuspendTest004, TestSize.Level1";
191     LBSLOGI(LOCATOR_BACKGROUND_PROXY, "[LocatorBackgroundProxyTest] OnSuspendTest004 begin");
192     auto locatorBackgroundProxy = DelayedSingleton<LocatorBackgroundProxy>::GetInstance();
193     EXPECT_NE(nullptr, locatorBackgroundProxy);
194     std::shared_ptr<Request> request1 = std::make_shared<Request>();
195     request1->SetUid(VAL_UID);
196     request1->SetPid(0);
197     request1->SetTokenId(0); // invalid token id
198     request1->SetFirstTokenId(0);
199     request1->SetPackageName("name");
200     auto requestConfig = std::make_unique<RequestConfig>();
201     EXPECT_NE(nullptr, requestConfig);
202     requestConfig->SetPriority(PRIORITY_FAST_FIRST_FIX);
203     requestConfig->SetFixNumber(0);
204     request1->SetRequestConfig(*requestConfig);
205     locatorBackgroundProxy->OnSuspend(request1, false); // permission denied, cant add to requestsList
206     locatorBackgroundProxy->OnSuspend(request1, true); // permission denied, cant remove from requestList
207     LBSLOGI(LOCATOR_BACKGROUND_PROXY, "[LocatorBackgroundProxyTest] OnSuspendTest004 end");
208 }
209 
210 HWTEST_F(LocatorBackgroundProxyTest, OnSaStateChangeTest001, TestSize.Level1)
211 {
212     GTEST_LOG_(INFO)
213         << "LocatorBackgroundProxyTest, OnSaStateChangeTest001, TestSize.Level1";
214     LBSLOGI(LOCATOR_BACKGROUND_PROXY, "[LocatorBackgroundProxyTest] OnSaStateChangeTest001 begin");
215     auto locatorBackgroundProxy = DelayedSingleton<LocatorBackgroundProxy>::GetInstance();
216     EXPECT_NE(nullptr, locatorBackgroundProxy);
217     locatorBackgroundProxy->OnSaStateChange(true);
218 
219     locatorBackgroundProxy->OnSaStateChange(false);
220 
221     locatorBackgroundProxy->OnSaStateChange(true);
222     LBSLOGI(LOCATOR_BACKGROUND_PROXY, "[LocatorBackgroundProxyTest] OnSaStateChangeTest001 end");
223 }
224 
225 HWTEST_F(LocatorBackgroundProxyTest, OnDeleteRequestRecord001, TestSize.Level1)
226 {
227     GTEST_LOG_(INFO)
228         << "LocatorBackgroundProxyTest, OnDeleteRequestRecord001, TestSize.Level1";
229     LBSLOGI(LOCATOR_BACKGROUND_PROXY, "[LocatorBackgroundProxyTest] OnDeleteRequestRecord001 begin");
230     auto locatorBackgroundProxy = DelayedSingleton<LocatorBackgroundProxy>::GetInstance();
231     EXPECT_NE(nullptr, locatorBackgroundProxy);
232     std::shared_ptr<Request> request1 = std::make_shared<Request>();
233     request1->SetUid(1000);
234     request1->SetPid(0);
235     request1->SetTokenId(tokenId_);
236     request1->SetFirstTokenId(0);
237     request1->SetPackageName("LocatorBackgroundProxyTest");
238     locatorBackgroundProxy->OnDeleteRequestRecord(request1);
239     LBSLOGI(LOCATOR_BACKGROUND_PROXY, "[LocatorBackgroundProxyTest] OnDeleteRequestRecord001 end");
240 }
241 
242 HWTEST_F(LocatorBackgroundProxyTest, IsCallbackInProxyTest001, TestSize.Level1)
243 {
244     GTEST_LOG_(INFO)
245         << "LocatorBackgroundProxyTest, IsCallbackInProxyTest001, TestSize.Level1";
246     LBSLOGI(LOCATOR_BACKGROUND_PROXY, "[LocatorBackgroundProxyTest] IsCallbackInProxyTest001 begin");
247     auto locatorBackgroundProxy = DelayedSingleton<LocatorBackgroundProxy>::GetInstance();
248     EXPECT_NE(nullptr, locatorBackgroundProxy);
249     sptr<LocatorCallbackHost> locatorCallbackHost =
250         sptr<LocatorCallbackHost>(new (std::nothrow)LocatorCallbackHost());
251     auto callback = sptr<ILocatorCallback>(locatorCallbackHost);
252     EXPECT_EQ(false, locatorBackgroundProxy->IsCallbackInProxy(callback));
253 
254     std::shared_ptr<Request> request1 = std::make_shared<Request>();
255     request1->SetUid(1000);
256     request1->SetPid(0);
257     request1->SetTokenId(tokenId_);
258     request1->SetFirstTokenId(0);
259     request1->SetLocatorCallBack(callback);
260     request1->SetPackageName("LocatorBackgroundProxyTest");
261     auto requestConfig1 = std::make_unique<RequestConfig>();
262     EXPECT_NE(nullptr, requestConfig1);
263     requestConfig1->SetPriority(PRIORITY_FAST_FIRST_FIX);
264     requestConfig1->SetMaxAccuracy(1000.0);
265     requestConfig1->SetFixNumber(0);
266     request1->SetRequestConfig(*requestConfig1);
267     locatorBackgroundProxy->OnSuspend(request1, false);
268     EXPECT_EQ(false, locatorBackgroundProxy->IsCallbackInProxy(callback));
269     LBSLOGI(LOCATOR_BACKGROUND_PROXY, "[LocatorBackgroundProxyTest] IsCallbackInProxyTest001 end");
270 }
271 
272 HWTEST_F(LocatorBackgroundProxyTest, IsAppBackgroundTest001, TestSize.Level1)
273 {
274     GTEST_LOG_(INFO)
275         << "LocatorBackgroundProxyTest, IsAppBackgroundTest001, TestSize.Level1";
276     LBSLOGI(LOCATOR_BACKGROUND_PROXY, "[LocatorBackgroundProxyTest] IsAppBackgroundTest001 begin");
277     auto locatorBackgroundProxy = DelayedSingleton<LocatorBackgroundProxy>::GetInstance();
278     EXPECT_NE(nullptr, locatorBackgroundProxy);
279     EXPECT_EQ(true, locatorBackgroundProxy->IsAppBackground("LocatorBackgroundProxyTest"));
280     LBSLOGI(LOCATOR_BACKGROUND_PROXY, "[LocatorBackgroundProxyTest] IsAppBackgroundTest001 end");
281 }
282 
283 HWTEST_F(LocatorBackgroundProxyTest, RegisterAppStateObserverTest001, TestSize.Level1)
284 {
285     GTEST_LOG_(INFO)
286         << "LocatorBackgroundProxyTest, RegisterAppStateObserverTest001, TestSize.Level1";
287     LBSLOGI(LOCATOR_BACKGROUND_PROXY, "[LocatorBackgroundProxyTest] RegisterAppStateObserverTest001 begin");
288     auto locatorBackgroundProxy = DelayedSingleton<LocatorBackgroundProxy>::GetInstance();
289     EXPECT_NE(nullptr, locatorBackgroundProxy);
290     EXPECT_EQ(true, locatorBackgroundProxy->UnregisterAppStateObserver()); // unreg first
291     EXPECT_EQ(true, locatorBackgroundProxy->RegisterAppStateObserver());
292     EXPECT_EQ(true, locatorBackgroundProxy->RegisterAppStateObserver()); // register again
293     EXPECT_EQ(true, locatorBackgroundProxy->UnregisterAppStateObserver());
294     LBSLOGI(LOCATOR_BACKGROUND_PROXY, "[LocatorBackgroundProxyTest] RegisterAppStateObserverTest001 end");
295 }
296 
297 HWTEST_F(LocatorBackgroundProxyTest, UpdateListOnUserSwitchTest001, TestSize.Level1)
298 {
299     GTEST_LOG_(INFO)
300         << "LocatorBackgroundProxyTest, UpdateListOnUserSwitchTest001, TestSize.Level1";
301     LBSLOGI(LOCATOR_BACKGROUND_PROXY, "[LocatorBackgroundProxyTest] UpdateListOnUserSwitchTest001 begin");
302     auto locatorBackgroundProxy = DelayedSingleton<LocatorBackgroundProxy>::GetInstance();
303     ASSERT_TRUE(locatorBackgroundProxy != nullptr);
304     int userId = 0;
305     CommonUtils::GetCurrentUserId(userId);
306     locatorBackgroundProxy->UpdateListOnUserSwitch(userId);
307     EXPECT_EQ(userId, locatorBackgroundProxy->curUserId_);
308     LBSLOGI(LOCATOR_BACKGROUND_PROXY, "[LocatorBackgroundProxyTest] UpdateListOnUserSwitchTest001 end");
309 }
310 
311 HWTEST_F(LocatorBackgroundProxyTest, UpdateListOnUserSwitchTest002, TestSize.Level1)
312 {
313     GTEST_LOG_(INFO)
314         << "LocatorBackgroundProxyTest, UpdateListOnUserSwitchTest002, TestSize.Level1";
315     LBSLOGI(LOCATOR_BACKGROUND_PROXY, "[LocatorBackgroundProxyTest] UpdateListOnUserSwitchTest002 begin");
316     auto locatorBackgroundProxy = DelayedSingleton<LocatorBackgroundProxy>::GetInstance();
317     ASSERT_TRUE(locatorBackgroundProxy != nullptr);
318     locatorBackgroundProxy->UpdateListOnUserSwitch(0); // requestsMap cant find userid
319     LBSLOGI(LOCATOR_BACKGROUND_PROXY, "[LocatorBackgroundProxyTest] UpdateListOnUserSwitchTest002 end");
320 }
321 
322 HWTEST_F(LocatorBackgroundProxyTest, IsCallbackInProxyTest002, TestSize.Level1)
323 {
324     GTEST_LOG_(INFO)
325         << "LocatorBackgroundProxyTest, IsCallbackInProxyTest001, TestSize.Level1";
326     LBSLOGI(LOCATOR_BACKGROUND_PROXY, "[LocatorBackgroundProxyTest] IsCallbackInProxyTest001 begin");
327     auto locatorBackgroundProxy = DelayedSingleton<LocatorBackgroundProxy>::GetInstance();
328     ASSERT_TRUE(locatorBackgroundProxy != nullptr);
329     std::shared_ptr<Request> request = std::make_shared<Request>();
330     request->SetLocatorCallBack(locatorBackgroundProxy->callback_);
331     locatorBackgroundProxy->requestsList_->push_back(request);
332     EXPECT_EQ(true,
333         locatorBackgroundProxy->IsCallbackInProxy(locatorBackgroundProxy->callback_));
334     sptr<ILocatorCallback> newCallback = new (std::nothrow) LocatorCallbackStub();
335     EXPECT_EQ(false,
336         locatorBackgroundProxy->IsCallbackInProxy(newCallback));
337     LBSLOGI(LOCATOR_BACKGROUND_PROXY, "[LocatorBackgroundProxyTest] IsCallbackInProxyTest001 end");
338 }
339 
340 HWTEST_F(LocatorBackgroundProxyTest, OnUserSwitchTest001, TestSize.Level1)
341 {
342     GTEST_LOG_(INFO)
343         << "LocatorBackgroundProxyTest, OnUserSwitchTest001, TestSize.Level1";
344     LBSLOGI(LOCATOR_BACKGROUND_PROXY, "[LocatorBackgroundProxyTest] OnUserSwitchTest001 begin");
345     auto locatorBackgroundProxy = DelayedSingleton<LocatorBackgroundProxy>::GetInstance();
346     ASSERT_TRUE(locatorBackgroundProxy != nullptr);
347     int userId = 0;
348     CommonUtils::GetCurrentUserId(userId);
349     std::shared_ptr<Request> request = std::make_shared<Request>();
350     locatorBackgroundProxy->requestsList_->push_back(request);
351     locatorBackgroundProxy->OnUserSwitch(userId);
352     LBSLOGI(LOCATOR_BACKGROUND_PROXY, "[LocatorBackgroundProxyTest] OnUserSwitchTest001 end");
353 }
354 
355 HWTEST_F(LocatorBackgroundProxyTest, OnUserSwitchTest002, TestSize.Level1)
356 {
357     GTEST_LOG_(INFO)
358         << "LocatorBackgroundProxyTest, OnUserSwitchTest002, TestSize.Level1";
359     LBSLOGI(LOCATOR_BACKGROUND_PROXY, "[LocatorBackgroundProxyTest] OnUserSwitchTest002 begin");
360     auto locatorBackgroundProxy = DelayedSingleton<LocatorBackgroundProxy>::GetInstance();
361     ASSERT_TRUE(locatorBackgroundProxy != nullptr);
362     int userId = 0;
363     CommonUtils::GetCurrentUserId(userId);
364     locatorBackgroundProxy->requestsList_->clear();
365     locatorBackgroundProxy->OnUserSwitch(userId);
366     LBSLOGI(LOCATOR_BACKGROUND_PROXY, "[LocatorBackgroundProxyTest] OnUserSwitchTest002 end");
367 }
368 
369 HWTEST_F(LocatorBackgroundProxyTest, OnUserRemoveTest001, TestSize.Level1)
370 {
371     GTEST_LOG_(INFO)
372         << "LocatorBackgroundProxyTest, OnUserRemoveTest001, TestSize.Level1";
373     LBSLOGI(LOCATOR_BACKGROUND_PROXY, "[LocatorBackgroundProxyTest] OnUserRemoveTest001 begin");
374     auto locatorBackgroundProxy = DelayedSingleton<LocatorBackgroundProxy>::GetInstance();
375     ASSERT_TRUE(locatorBackgroundProxy != nullptr);
376     int userId = 0;
377     CommonUtils::GetCurrentUserId(userId);
378     locatorBackgroundProxy->OnUserRemove(userId);
379     LBSLOGI(LOCATOR_BACKGROUND_PROXY, "[LocatorBackgroundProxyTest] OnUserRemoveTest001 end");
380 }
381 
382 HWTEST_F(LocatorBackgroundProxyTest, OnUserRemoveTest002, TestSize.Level1)
383 {
384     GTEST_LOG_(INFO)
385         << "LocatorBackgroundProxyTest, OnUserRemoveTest002, TestSize.Level1";
386     LBSLOGI(LOCATOR_BACKGROUND_PROXY, "[LocatorBackgroundProxyTest] OnUserRemoveTest002 begin");
387     auto locatorBackgroundProxy = DelayedSingleton<LocatorBackgroundProxy>::GetInstance();
388     ASSERT_TRUE(locatorBackgroundProxy != nullptr);
389     locatorBackgroundProxy->OnUserRemove(0);
390     LBSLOGI(LOCATOR_BACKGROUND_PROXY, "[LocatorBackgroundProxyTest] OnUserRemoveTest002 end");
391 }
392 
393 HWTEST_F(LocatorBackgroundProxyTest, CheckMaxRequestNumTest001, TestSize.Level1)
394 {
395     GTEST_LOG_(INFO)
396         << "LocatorBackgroundProxyTest, CheckMaxRequestNumTest001, TestSize.Level1";
397     LBSLOGI(LOCATOR_BACKGROUND_PROXY, "[LocatorBackgroundProxyTest] CheckMaxRequestNumTest001 begin");
398     auto locatorBackgroundProxy = DelayedSingleton<LocatorBackgroundProxy>::GetInstance();
399     ASSERT_TRUE(locatorBackgroundProxy != nullptr);
400     locatorBackgroundProxy->CheckMaxRequestNum(0, "PKG_FOR_TEST"); // cant find userId
401     LBSLOGI(LOCATOR_BACKGROUND_PROXY, "[LocatorBackgroundProxyTest] CheckMaxRequestNumTest001 end");
402 }
403 
404 HWTEST_F(LocatorBackgroundProxyTest, CheckMaxRequestNumTest002, TestSize.Level1)
405 {
406     GTEST_LOG_(INFO)
407         << "LocatorBackgroundProxyTest, CheckMaxRequestNumTest002, TestSize.Level1";
408     LBSLOGI(LOCATOR_BACKGROUND_PROXY, "[LocatorBackgroundProxyTest] CheckMaxRequestNumTest002 begin");
409     auto locatorBackgroundProxy = DelayedSingleton<LocatorBackgroundProxy>::GetInstance();
410     ASSERT_TRUE(locatorBackgroundProxy != nullptr);
411     int32_t userId = locatorBackgroundProxy->GetUserId(VAL_UID);
412     auto list = std::make_shared<std::list<std::shared_ptr<Request>>>();
413     auto request = std::make_shared<Request>();
414     request->SetUid(VAL_UID);
415     request->SetPackageName("PKG_FOR_TEST");
416     list->push_back(request);
417     (locatorBackgroundProxy->requestsMap_)->insert(make_pair(userId, list));
418     EXPECT_EQ(false, locatorBackgroundProxy->CheckMaxRequestNum(VAL_UID, "PKG_FOR_TEST")); // matched request
419     LBSLOGI(LOCATOR_BACKGROUND_PROXY, "[LocatorBackgroundProxyTest] CheckMaxRequestNumTest002 end");
420 }
421 
422 HWTEST_F(LocatorBackgroundProxyTest, CheckMaxRequestNumTest003, TestSize.Level1)
423 {
424     GTEST_LOG_(INFO)
425         << "LocatorBackgroundProxyTest, CheckMaxRequestNumTest003, TestSize.Level1";
426     LBSLOGI(LOCATOR_BACKGROUND_PROXY, "[LocatorBackgroundProxyTest] CheckMaxRequestNumTest003 begin");
427     auto locatorBackgroundProxy = DelayedSingleton<LocatorBackgroundProxy>::GetInstance();
428     ASSERT_TRUE(locatorBackgroundProxy != nullptr);
429     int32_t userId = locatorBackgroundProxy->GetUserId(VAL_UID);
430     auto list = std::make_shared<std::list<std::shared_ptr<Request>>>();
431     auto request = std::make_shared<Request>();
432     request->SetUid(-1);
433     request->SetPackageName("PKG_FOR_TEST");
434     list->push_back(request);
435     (locatorBackgroundProxy->requestsMap_)->insert(make_pair(userId, list));
436     EXPECT_EQ(false, locatorBackgroundProxy->CheckMaxRequestNum(VAL_UID, "PKG_FOR_TEST")); // uid is wrong
437     LBSLOGI(LOCATOR_BACKGROUND_PROXY, "[LocatorBackgroundProxyTest] CheckMaxRequestNumTest003 end");
438 }
439 
440 HWTEST_F(LocatorBackgroundProxyTest, CheckMaxRequestNumTest004, TestSize.Level1)
441 {
442     GTEST_LOG_(INFO)
443         << "LocatorBackgroundProxyTest, CheckMaxRequestNumTest004, TestSize.Level1";
444     LBSLOGI(LOCATOR_BACKGROUND_PROXY, "[LocatorBackgroundProxyTest] CheckMaxRequestNumTest004 begin");
445     auto locatorBackgroundProxy = DelayedSingleton<LocatorBackgroundProxy>::GetInstance();
446     ASSERT_TRUE(locatorBackgroundProxy != nullptr);
447     int32_t userId = locatorBackgroundProxy->GetUserId(VAL_UID);
448     auto list = std::make_shared<std::list<std::shared_ptr<Request>>>();
449     auto request = std::make_shared<Request>();
450     request->SetUid(VAL_UID);
451     request->SetPackageName("INVALID_PKG_NAME");
452     list->push_back(request);
453     (locatorBackgroundProxy->requestsMap_)->insert(make_pair(userId, list));
454     EXPECT_EQ(false, locatorBackgroundProxy->CheckMaxRequestNum(VAL_UID, "PKG_FOR_TEST")); // pkg name is wrong
455     LBSLOGI(LOCATOR_BACKGROUND_PROXY, "[LocatorBackgroundProxyTest] CheckMaxRequestNumTest004 end");
456 }
457 
458 HWTEST_F(LocatorBackgroundProxyTest, MLocatorCallbackTest001, TestSize.Level1)
459 {
460     GTEST_LOG_(INFO)
461         << "LocatorBackgroundProxyTest, MLocatorCallbackTest002, TestSize.Level1";
462     LBSLOGI(LOCATOR_BACKGROUND_PROXY, "[LocatorBackgroundProxyTest] MLocatorCallbackTest002 begin");
463     auto locatorBackgroundProxy = DelayedSingleton<LocatorBackgroundProxy>::GetInstance();
464     ASSERT_TRUE(locatorBackgroundProxy != nullptr);
465     auto callback =
466         sptr<LocatorBackgroundProxy::mLocatorCallback>(new (std::nothrow) LocatorBackgroundProxy::mLocatorCallback());
467     ASSERT_TRUE(callback != nullptr);
468     auto request = std::make_shared<Request>();
469     request->SetUid(VAL_UID);
470     request->SetPackageName("PKG_NAME");
471     (locatorBackgroundProxy->requestsList_)->clear();
472 
473     auto location = std::make_unique<Location>();
474     MessageParcel parcel;
475     parcel.WriteDouble(1.0); // latitude
476     parcel.WriteDouble(2.0); // longitude
477     parcel.WriteDouble(3.0); // altitude
478     parcel.WriteDouble(4.0); // accuracy
479     parcel.WriteDouble(5.0); // speed
480     parcel.WriteDouble(6.0); // direction
481     parcel.WriteInt64(1000000000); // timeStamp
482     parcel.WriteInt64(1000000000); // timeSinceBoot
483     parcel.WriteString16(u"additions"); // additions
484     parcel.WriteInt64(1); // additionSize
485     parcel.WriteBool(true); // isFromMock
486     parcel.WriteInt32(1); // source type
487     parcel.WriteInt32(0); // floor no.
488     parcel.WriteDouble(1000.0); // floor acc
489     EXPECT_NE(nullptr, location);
490     location->ReadFromParcel(parcel);
491     callback->OnLocationReport(location);
492     LBSLOGI(LOCATOR_BACKGROUND_PROXY, "[LocatorBackgroundProxyTest] MLocatorCallbackTest002 end");
493 }
494 
495 HWTEST_F(LocatorBackgroundProxyTest, MLocatorCallbackTest003, TestSize.Level1)
496 {
497     GTEST_LOG_(INFO)
498         << "LocatorBackgroundProxyTest, MLocatorCallbackTest003, TestSize.Level1";
499     LBSLOGI(LOCATOR_BACKGROUND_PROXY, "[LocatorBackgroundProxyTest] MLocatorCallbackTest003 begin");
500     auto callback =
501         sptr<LocatorBackgroundProxy::mLocatorCallback>(new (std::nothrow) LocatorBackgroundProxy::mLocatorCallback());
502     ASSERT_TRUE(callback != nullptr);
503     callback->OnLocatingStatusChange(0);
504     LBSLOGI(LOCATOR_BACKGROUND_PROXY, "[LocatorBackgroundProxyTest] MLocatorCallbackTest003 end");
505 }
506 
507 HWTEST_F(LocatorBackgroundProxyTest, MLocatorCallbackTest004, TestSize.Level1)
508 {
509     GTEST_LOG_(INFO)
510         << "LocatorBackgroundProxyTest, MLocatorCallbackTest004, TestSize.Level1";
511     LBSLOGI(LOCATOR_BACKGROUND_PROXY, "[LocatorBackgroundProxyTest] MLocatorCallbackTest004 begin");
512     auto callback =
513         sptr<LocatorBackgroundProxy::mLocatorCallback>(new (std::nothrow) LocatorBackgroundProxy::mLocatorCallback());
514     ASSERT_TRUE(callback != nullptr);
515     callback->OnErrorReport(0);
516     LBSLOGI(LOCATOR_BACKGROUND_PROXY, "[LocatorBackgroundProxyTest] MLocatorCallbackTest004 end");
517 }
518 
519 HWTEST_F(LocatorBackgroundProxyTest, UserSwitchSubscriberOnReceiveEventTest001, TestSize.Level1)
520 {
521     GTEST_LOG_(INFO)
522         << "LocatorBackgroundProxyTest, UserSwitchSubscriberOnReceiveEventTest001, TestSize.Level1";
523     LBSLOGI(LOCATOR_BACKGROUND_PROXY, "[LocatorBackgroundProxyTest] UserSwitchSubscriberOnReceiveEventTest001 begin");
524     OHOS::EventFwk::MatchingSkills matchingSkills;
525     matchingSkills.AddEvent(OHOS::EventFwk::CommonEventSupport::COMMON_EVENT_USER_SWITCHED);
526     OHOS::EventFwk::CommonEventSubscribeInfo subscriberInfo(matchingSkills);
527     std::shared_ptr<LocatorBackgroundProxy::UserSwitchSubscriber> subscriber =
528         std::make_shared<LocatorBackgroundProxy::UserSwitchSubscriber>(subscriberInfo);
529     Want want;
530     want.SetAction(OHOS::EventFwk::CommonEventSupport::COMMON_EVENT_USER_SWITCHED);
531     OHOS::EventFwk::CommonEventData event;
532     event.SetWant(want);
533     event.SetCode(0);
534     subscriber->OnReceiveEvent(event);
535     LBSLOGI(LOCATOR_BACKGROUND_PROXY, "[LocatorBackgroundProxyTest] UserSwitchSubscriberOnReceiveEventTest001 end");
536 }
537 
538 HWTEST_F(LocatorBackgroundProxyTest, UserSwitchSubscriberOnReceiveEventTest002, TestSize.Level1)
539 {
540     GTEST_LOG_(INFO)
541         << "LocatorBackgroundProxyTest, UserSwitchSubscriberOnReceiveEventTest002, TestSize.Level1";
542     LBSLOGI(LOCATOR_BACKGROUND_PROXY, "[LocatorBackgroundProxyTest] UserSwitchSubscriberOnReceiveEventTest002 begin");
543     OHOS::EventFwk::MatchingSkills matchingSkills;
544     matchingSkills.AddEvent(OHOS::EventFwk::CommonEventSupport::COMMON_EVENT_USER_SWITCHED);
545     OHOS::EventFwk::CommonEventSubscribeInfo subscriberInfo(matchingSkills);
546     std::shared_ptr<LocatorBackgroundProxy::UserSwitchSubscriber> subscriber =
547         std::make_shared<LocatorBackgroundProxy::UserSwitchSubscriber>(subscriberInfo);
548     Want want;
549     want.SetAction(OHOS::EventFwk::CommonEventSupport::COMMON_EVENT_USER_REMOVED);
550     OHOS::EventFwk::CommonEventData event;
551     event.SetWant(want);
552     event.SetCode(0);
553     subscriber->OnReceiveEvent(event);
554     auto locatorBackgroundProxy = DelayedSingleton<LocatorBackgroundProxy>::GetInstance();
555     ASSERT_TRUE(locatorBackgroundProxy != nullptr);
556     auto iter = (locatorBackgroundProxy->requestsMap_)->find(0);
557     EXPECT_EQ(true, iter == (locatorBackgroundProxy->requestsMap_)->end());
558     LBSLOGI(LOCATOR_BACKGROUND_PROXY, "[LocatorBackgroundProxyTest] UserSwitchSubscriberOnReceiveEventTest002 end");
559 }
560 }  // namespace Location
561 }  // namespace OHOS