1 /*
2  * Copyright (c) 2023 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 <gtest/gtest.h>
17 #include <gtest/hwext/gtest-multithread.h>
18 
19 #define private public
20 #include "app_running_manager.h"
21 #include "child_process_record.h"
22 #undef private
23 #include "hilog_tag_wrapper.h"
24 #include "window_visibility_info.h"
25 
26 using namespace testing;
27 using namespace testing::ext;
28 using namespace testing::mt;
29 
30 namespace OHOS {
31 namespace AppExecFwk {
32 namespace {
33 constexpr int32_t DEBUGINFOS_SIZE = 0;
34 constexpr int32_t ABILITYTOKENS_SIZE = 0;
35 constexpr int32_t RECORD_ID = 1;
36 constexpr uint32_t WINDOW_ID = 100;
37 constexpr pid_t PID = 10;
38 constexpr int32_t RECORD_MAP_SIZE = 1;
39 constexpr int32_t DEBUG_INFOS_SIZE = 1;
40 constexpr int32_t ABILITY_TOKENS_SIZE = 1;
41 }
42 class AppRunningManagerTest : public testing::Test {
43 public:
44     static void SetUpTestCase();
45     static void TearDownTestCase();
46     void SetUp() override;
47     void TearDown() override;
48 };
49 
SetUpTestCase(void)50 void AppRunningManagerTest::SetUpTestCase(void)
51 {}
52 
TearDownTestCase(void)53 void AppRunningManagerTest::TearDownTestCase(void)
54 {}
55 
SetUp()56 void AppRunningManagerTest::SetUp()
57 {}
58 
TearDown()59 void AppRunningManagerTest::TearDown()
60 {}
61 
62 /**
63  * @tc.name: AppRunningManager_SetAttachAppDebug_0100
64  * @tc.desc: Test the state of AttachAppDebug
65  * @tc.type: FUNC
66  */
67 HWTEST_F(AppRunningManagerTest, AppRunningManager_SetAttachAppDebug_0100, TestSize.Level1)
68 {
69     auto appRunningManager = std::make_shared<AppRunningManager>();
70     EXPECT_NE(appRunningManager, nullptr);
71     std::string bundleName;
72     std::shared_ptr<ApplicationInfo> appInfo = std::make_shared<ApplicationInfo>();
73     int32_t recordId = RECORD_ID;
74     std::string processName;
75     auto appRunningRecord = std::make_shared<AppRunningRecord>(appInfo, recordId, processName);
76     appRunningManager->appRunningRecordMap_.insert(make_pair(RECORD_ID, appRunningRecord));
77     appRunningManager->SetAttachAppDebug(bundleName, true);
78     for (const auto &item : appRunningManager->appRunningRecordMap_) {
79         const auto &appRecord = item.second;
80         if (appRecord->GetBundleName() == bundleName) {
81             appRecord->SetAttachDebug(true);
82             EXPECT_EQ(appRecord->isAttachDebug_, true);
83         }
84     }
85 }
86 
87 /**
88  * @tc.name: AppRunningManager_SetAttachAppDebug_0200
89  * @tc.desc: Test the state of AttachAppDebug
90  * @tc.type: FUNC
91  */
92 HWTEST_F(AppRunningManagerTest, AppRunningManager_SetAttachAppDebug_0200, TestSize.Level1)
93 {
94     auto appRunningManager = std::make_shared<AppRunningManager>();
95     EXPECT_NE(appRunningManager, nullptr);
96     std::string bundleName;
97     bool isAttachDebug = true;
98     std::shared_ptr<ApplicationInfo> appInfo = std::make_shared<ApplicationInfo>();
99     int32_t recordId = RECORD_ID;
100     std::string processName;
101     auto appRunningRecord = std::make_shared<AppRunningRecord>(appInfo, recordId, processName);
102     appRunningManager->appRunningRecordMap_.insert(make_pair(RECORD_ID, appRunningRecord));
103     appRunningManager->SetAttachAppDebug(bundleName, isAttachDebug);
104     for (const auto &item : appRunningManager->appRunningRecordMap_) {
105         const auto &appRecord = item.second;
106         if (appRecord->GetBundleName() == bundleName) {
107             appRecord->SetAttachDebug(true);
108             EXPECT_EQ(appRecord->isAttachDebug_, true);
109         }
110     }
111 }
112 
113 /**
114  * @tc.name: AppRunningManager_GetAppDebugInfoByBundleName_0100
115  * @tc.desc: Test the state of GetAppDebugInfoByBundleName
116  * @tc.type: FUNC
117  */
118 HWTEST_F(AppRunningManagerTest, AppRunningManager_GetAppDebugInfoByBundleName_0100, TestSize.Level1)
119 {
120     auto appRunningManager = std::make_shared<AppRunningManager>();
121     EXPECT_NE(appRunningManager, nullptr);
122     std::string bundleName;
123     std::vector<AppDebugInfo> debugInfos;
124     bool isDetachDebug = true;
125     std::shared_ptr<ApplicationInfo> appInfo = std::make_shared<ApplicationInfo>();
126     int32_t recordId = RECORD_ID;
127     std::string processName;
128     auto appRunningRecord = std::make_shared<AppRunningRecord>(appInfo, recordId, processName);
129     appRunningManager->appRunningRecordMap_.insert(make_pair(RECORD_ID, appRunningRecord));
130     appRunningManager->GetAppDebugInfosByBundleName(bundleName, isDetachDebug);
131     EXPECT_EQ(appRunningManager->appRunningRecordMap_.size(), RECORD_MAP_SIZE);
132     for (const auto &item : appRunningManager->appRunningRecordMap_) {
133         const auto &appRecord = item.second;
134         AppDebugInfo debugInfo;
135         debugInfos.emplace_back(debugInfo);
136         EXPECT_EQ(debugInfos.size(), DEBUG_INFOS_SIZE);
137     }
138 }
139 
140 /**
141  * @tc.name: AppRunningManager_GetAbilityTokensByBundleName_0100
142  * @tc.desc: Test the state of GetAbilityTokensByBundleName
143  * @tc.type: FUNC
144  */
145 HWTEST_F(AppRunningManagerTest, AppRunningManager_GetAbilityTokensByBundleName_0100, TestSize.Level1)
146 {
147     auto appRunningManager = std::make_shared<AppRunningManager>();
148     EXPECT_NE(appRunningManager, nullptr);
149     std::string bundleName;
150     std::vector<sptr<IRemoteObject>> abilityTokens;
151     std::shared_ptr<ApplicationInfo> appInfo = std::make_shared<ApplicationInfo>();
152     int32_t recordId = RECORD_ID;
153     std::string processName;
154     auto appRunningRecord = std::make_shared<AppRunningRecord>(appInfo, recordId, processName);
155     appRunningManager->appRunningRecordMap_.insert(make_pair(RECORD_ID, appRunningRecord));
156     appRunningManager->GetAbilityTokensByBundleName(bundleName, abilityTokens);
157     for (const auto &item : appRunningManager->appRunningRecordMap_) {
158         const auto &appRecord = item.second;
159         for (const auto &token : appRecord->GetAbilities()) {
160             abilityTokens.emplace_back(token.first);
161             EXPECT_EQ(abilityTokens.size(), ABILITY_TOKENS_SIZE);
162         }
163     }
164 }
165 
166 /**
167  * @tc.name: AppRunningManager_OnWindowVisibilityChanged_0100
168  * @tc.desc: verify the function of OnWindowVisibilityChanged : set windowIds
169  * @tc.type: FUNC
170  */
171 HWTEST_F(AppRunningManagerTest, AppRunningManager_OnWindowVisibilityChanged_0100, TestSize.Level1)
172 {
173     // 1. create ApprunningManager
174     auto appRunningManager = std::make_shared<AppRunningManager>();
175     EXPECT_NE(appRunningManager, nullptr);
176 
177     // 2. createAppRunningRecord and put it into appRunningRecordMap_ of AppRunningManager
178     std::string processName = "processName";
179     std::string appName = "appName";
180     auto appInfo = std::make_shared<ApplicationInfo>();
181     appInfo->name = appName;
182     int32_t recordId = AppRecordId::Create();
183     auto appRunningRecord = std::make_shared<AppRunningRecord>(appInfo, recordId, processName);
184     EXPECT_NE(appRunningRecord, nullptr);
185     appRunningRecord->curState_ = ApplicationState::APP_STATE_BACKGROUND;
186     appRunningRecord->GetPriorityObject()->SetPid(PID);
187     appRunningManager->appRunningRecordMap_.emplace(recordId, appRunningRecord);
188 
189     // 3. construct WindowVisibilityInfos
190     std::vector<sptr<OHOS::Rosen::WindowVisibilityInfo>> windowVisibilityInfos;
191     auto info = new (std::nothrow) Rosen::WindowVisibilityInfo();
192     EXPECT_NE(info, nullptr);
193     info->windowId_ = WINDOW_ID;
194     info->pid_ = PID;
195     info->visibilityState_ = Rosen::WindowVisibilityState::WINDOW_VISIBILITY_STATE_NO_OCCLUSION;
196     windowVisibilityInfos.push_back(info);
197 
198     // 4. verify the function
199     appRunningManager->OnWindowVisibilityChanged(windowVisibilityInfos);
200     EXPECT_FALSE(appRunningManager->appRunningRecordMap_.empty());
201     EXPECT_FALSE(appRunningManager->appRunningRecordMap_.at(1)->windowIds_.empty());
202 }
203 
204 /**
205  * @tc.name: AppRunningManager_GetAppRunningRecordByChildProcessPid_0100
206  * @tc.desc: Test GetAppRunningRecordByChildProcessPid works
207  * @tc.type: FUNC
208  */
209 HWTEST_F(AppRunningManagerTest, AppRunningManager_GetAppRunningRecordByChildProcessPid_0100, TestSize.Level1)
210 {
211     TAG_LOGD(AAFwkTag::TEST, "AppRunningManager_GetAppRunningRecordByChildProcessPid_0100 called.");
212     auto appRunningManager = std::make_shared<AppRunningManager>();
213     EXPECT_NE(appRunningManager, nullptr);
214 
215     auto appInfo = std::make_shared<ApplicationInfo>();
216     auto appRecord = std::make_shared<AppRunningRecord>(appInfo, RECORD_ID, "com.example.child");
217     ChildProcessRequest request;
218     request.srcEntry = "./ets/AProcess.ts";
219     auto childRecord = ChildProcessRecord::CreateChildProcessRecord(PID, request, appRecord);
220     pid_t childPid = 201;
221     childRecord->pid_ = childPid;
222     appRecord->AddChildProcessRecord(childPid, childRecord);
223     appRunningManager->appRunningRecordMap_.insert(make_pair(RECORD_ID, appRecord));
224 
225     auto record = appRunningManager->GetAppRunningRecordByChildProcessPid(childPid);
226     EXPECT_NE(record, nullptr);
227 }
228 
229 /**
230  * @tc.name: AppRunningManager_IsChildProcessReachLimit_0100
231  * @tc.desc: Test IsChildProcessReachLimit works
232  * @tc.type: FUNC
233  */
234 HWTEST_F(AppRunningManagerTest, AppRunningManager_IsChildProcessReachLimit_0100, TestSize.Level1)
235 {
236     TAG_LOGD(AAFwkTag::TEST, "AppRunningManager_IsChildProcessReachLimit_0100 called.");
237     auto appRunningManager = std::make_shared<AppRunningManager>();
238     EXPECT_NE(appRunningManager, nullptr);
239 
240     appRunningManager->IsChildProcessReachLimit(1);
241     auto record = appRunningManager->GetAppRunningRecordByChildProcessPid(123);
242     EXPECT_EQ(record, nullptr);
243 }
244 
245 /**
246  * @tc.name: AppRunningManager_UpdateConfiguration_0100
247  * @tc.desc: Test UpdateConfiguration works
248  * @tc.type: FUNC
249  */
250 HWTEST_F(AppRunningManagerTest, AppRunningManager_UpdateConfiguration_0100, TestSize.Level1)
251 {
252     auto appRunningManager = std::make_shared<AppRunningManager>();
253     EXPECT_NE(appRunningManager, nullptr);
254     std::string bundleName;
255     std::vector<AppDebugInfo> debugInfos;
256     bool isDetachDebug = true;
257     std::shared_ptr<ApplicationInfo> appInfo = std::make_shared<ApplicationInfo>();
258     int32_t recordId = 1;
259     std::string processName;
260     Configuration config;
261     auto appRunningRecord = std::make_shared<AppRunningRecord>(appInfo, recordId, processName);
262     appRunningManager->appRunningRecordMap_.emplace(recordId, appRunningRecord);
263     appRunningManager->appRunningRecordMap_.emplace(++recordId, nullptr);
264     appRunningRecord->SetState(ApplicationState::APP_STATE_READY);
265     appRunningManager->appRunningRecordMap_.emplace(++recordId, appRunningRecord);
266     appInfo->name = "com.huawei.shell_assistant";
267     appRunningRecord = std::make_shared<AppRunningRecord>(appInfo, recordId, processName);
268     appRunningManager->appRunningRecordMap_.emplace(++recordId, appRunningRecord);
269     EXPECT_EQ(appRunningManager->appRunningRecordMap_.size(), recordId);
270     auto ret = appRunningManager->UpdateConfiguration(config);
271     EXPECT_EQ(ret, ERR_OK);
272 }
273 
274 /**
275  * @tc.name: AppRunningManager_UpdateConfiguration_0200
276  * @tc.desc: Test UpdateConfiguration config storage
277  * @tc.type: FUNC
278  */
279 HWTEST_F(AppRunningManagerTest, AppRunningManager_UpdateConfiguration_0200, TestSize.Level1)
280 {
281     auto appRunningManager = std::make_shared<AppRunningManager>();
282     EXPECT_NE(appRunningManager, nullptr);
283     Configuration config;
284     config.AddItem(AAFwk::GlobalConfigurationKey::SYSTEM_COLORMODE, ConfigurationInner::COLOR_MODE_LIGHT);
285     auto ret = appRunningManager->UpdateConfiguration(config);
286     EXPECT_EQ(ret, ERR_OK);
287     EXPECT_NE(appRunningManager->configuration_, nullptr);
288     EXPECT_EQ(appRunningManager->configuration_->GetItem(AAFwk::GlobalConfigurationKey::SYSTEM_COLORMODE),
289         ConfigurationInner::COLOR_MODE_LIGHT);
290 }
291 
292 /**
293  * @tc.name: AppRunningManager_UpdateConfiguration_0300
294  * @tc.desc: Test UpdateConfiguration delayed
295  * @tc.type: FUNC
296  */
297 HWTEST_F(AppRunningManagerTest, AppRunningManager_UpdateConfiguration_0300, TestSize.Level1)
298 {
299     auto appRunningManager = std::make_shared<AppRunningManager>();
300     EXPECT_NE(appRunningManager, nullptr);
301     std::shared_ptr<ApplicationInfo> appInfo = std::make_shared<ApplicationInfo>();
302     int32_t recordId = 1;
303     std::string processName;
304     Configuration config;
305     auto appRunningRecord = std::make_shared<AppRunningRecord>(appInfo, recordId, processName);
306     appRunningManager->appRunningRecordMap_.emplace(recordId, appRunningRecord);
307     appRunningRecord = std::make_shared<AppRunningRecord>(appInfo, recordId, processName);
308     appRunningRecord->SetState(ApplicationState::APP_STATE_BACKGROUND);
309     appRunningManager->appRunningRecordMap_.emplace(++recordId, appRunningRecord);
310     auto ret = appRunningManager->UpdateConfiguration(config);
311     EXPECT_EQ(ret, ERR_OK);
312     EXPECT_EQ(appRunningManager->updateConfigurationDelayedMap_[0], false);
313     EXPECT_EQ(appRunningManager->updateConfigurationDelayedMap_[1], true);
314 }
315 
316 /**
317  * @tc.name: RemoveAppRunningRecordById_0100
318  * @tc.desc: Remove app running record by id.
319  * @tc.type: FUNC
320  */
321 HWTEST_F(AppRunningManagerTest, RemoveAppRunningRecordById_0100, TestSize.Level1)
322 {
323     auto appRunningManager = std::make_shared<AppRunningManager>();
324     ASSERT_NE(appRunningManager, nullptr);
325 
326     std::shared_ptr<ApplicationInfo> appInfo = std::make_shared<ApplicationInfo>();
327     std::string processName = "test.ProcessName";
328     BundleInfo bundleInfo;
329     auto appRecord = appRunningManager->CreateAppRunningRecord(appInfo, processName, bundleInfo);
330     ASSERT_NE(appRecord, nullptr);
331 
332     int32_t uiExtensionAbilityId = 1000;
333     pid_t hostPid = 1001;
334     pid_t providerPid = 1002;
335     appRunningManager->AddUIExtensionLauncherItem(uiExtensionAbilityId, hostPid, providerPid);
336 
337     appRunningManager->RemoveAppRunningRecordById(appRecord->GetRecordId());
338 }
339 
340 /**
341  * @tc.name: AppRunningRecord_0100
342  * @tc.desc: AppRunningRecord test multi-thread.
343  * @tc.type: FUNC
344  */
345 HWTEST_F(AppRunningManagerTest, AppRunningRecord_0100, TestSize.Level1)
346 {
347     static std::shared_ptr<AppRunningManager> appRunningManager_Record0100 = std::make_shared<AppRunningManager>();
348     ASSERT_NE(appRunningManager_Record0100, nullptr);
__anonb0a5bd360202() 349     auto task = []() {
350         TAG_LOGI(AAFwkTag::TEST, "Running in thread %{public}d.", gettid());
351         std::shared_ptr<ApplicationInfo> appInfo = std::make_shared<ApplicationInfo>();
352         static std::string processName = "test.ProcessName";
353         BundleInfo bundleInfo;
354         auto appRecord = appRunningManager_Record0100->CreateAppRunningRecord(appInfo, processName, bundleInfo);
355         ASSERT_NE(appRecord, nullptr);
356         processName += "a";
357 
358         static int32_t uiExtensionAbilityId = 1;
359         static pid_t hostPid = 100000;
360         static pid_t providerPid = 200000;
361         appRunningManager_Record0100->AddUIExtensionLauncherItem(uiExtensionAbilityId, hostPid, providerPid);
362         uiExtensionAbilityId++;
363         hostPid += 2;
364         providerPid += 3;
365 
366         appRunningManager_Record0100->RemoveUIExtensionLauncherItem(hostPid);
367         appRunningManager_Record0100->RemoveUIExtensionLauncherItem(providerPid);
368 
369         std::vector<pid_t> hostPids;
370         std::vector<pid_t> providerPids;
371         appRunningManager_Record0100->GetAllUIExtensionRootHostPid(hostPid, providerPids);
372         appRunningManager_Record0100->GetAllUIExtensionProviderPid(providerPid, hostPids);
373 
374         appRunningManager_Record0100->RemoveAppRunningRecordById(appRecord->GetRecordId());
375     };
376 
377     SET_THREAD_NUM(100);
378     GTEST_RUN_TASK(task);
379 }
380 
381 /**
382  * @tc.name: UIExtensionReleationship_0100
383  * @tc.desc: Root host pid and provider pid map test.
384  * @tc.type: FUNC
385  */
386 HWTEST_F(AppRunningManagerTest, UIExtensionReleationship_0100, TestSize.Level1)
387 {
388     auto appRunningManager = std::make_shared<AppRunningManager>();
389     ASSERT_NE(appRunningManager, nullptr);
390 
391     int32_t uiExtensionAbilityId = 1000;
392     pid_t hostPid = 1001;
393     pid_t providerPid = 1002;
394     appRunningManager->AddUIExtensionLauncherItem(uiExtensionAbilityId, hostPid, providerPid);
395 
396     std::vector<pid_t> hostPids;
397     EXPECT_EQ(appRunningManager->GetAllUIExtensionRootHostPid(providerPid, hostPids), ERR_OK);
398     EXPECT_EQ(hostPids.size(), 1);
399     EXPECT_EQ(hostPids[0], hostPid);
400     hostPids.clear();
401 
402     std::vector<pid_t> providerPids;
403     EXPECT_EQ(appRunningManager->GetAllUIExtensionProviderPid(hostPid, providerPids), ERR_OK);
404     EXPECT_EQ(providerPids.size(), 1);
405     EXPECT_EQ(providerPids[0], providerPid);
406     providerPids.clear();
407 
408     EXPECT_EQ(appRunningManager->RemoveUIExtensionLauncherItem(hostPid), ERR_OK);
409     EXPECT_EQ(appRunningManager->RemoveUIExtensionLauncherItem(providerPid), ERR_OK);
410 
411     // Get after remove.
412     EXPECT_EQ(appRunningManager->GetAllUIExtensionRootHostPid(providerPid, hostPids), ERR_OK);
413     EXPECT_EQ(hostPids.size(), 0);
414     EXPECT_EQ(appRunningManager->GetAllUIExtensionProviderPid(hostPid, providerPids), ERR_OK);
415     EXPECT_EQ(providerPids.size(), 0);
416 }
417 
418 /**
419  * @tc.name: UIExtensionReleationship_0200
420  * @tc.desc: Root host pid and provider pid map test.
421  * @tc.type: FUNC
422  */
423 HWTEST_F(AppRunningManagerTest, UIExtensionReleationship_0200, TestSize.Level1)
424 {
425     auto appRunningManager = std::make_shared<AppRunningManager>();
426     ASSERT_NE(appRunningManager, nullptr);
427 
428     int32_t uiExtensionAbilityId = 1000;
429     pid_t hostPid = 1001;
430     pid_t providerPid = 1001; // same with host pid, ie uiability and uiextensionability in same process.
431     appRunningManager->AddUIExtensionLauncherItem(uiExtensionAbilityId, hostPid, providerPid);
432 
433     std::vector<pid_t> hostPids;
434     EXPECT_EQ(appRunningManager->GetAllUIExtensionRootHostPid(providerPid, hostPids), ERR_OK);
435     EXPECT_EQ(hostPids.size(), 1);
436     EXPECT_EQ(hostPids[0], hostPid);
437     hostPids.clear();
438 
439     std::vector<pid_t> providerPids;
440     EXPECT_EQ(appRunningManager->GetAllUIExtensionProviderPid(hostPid, providerPids), ERR_OK);
441     EXPECT_EQ(providerPids.size(), 1);
442     EXPECT_EQ(providerPids[0], providerPid);
443     providerPids.clear();
444 
445     EXPECT_EQ(appRunningManager->RemoveUIExtensionLauncherItem(hostPid), ERR_OK);
446     EXPECT_EQ(appRunningManager->RemoveUIExtensionLauncherItem(providerPid), ERR_OK);
447 
448     // Get after remove.
449     EXPECT_EQ(appRunningManager->GetAllUIExtensionRootHostPid(providerPid, hostPids), ERR_OK);
450     EXPECT_EQ(hostPids.size(), 0);
451     EXPECT_EQ(appRunningManager->GetAllUIExtensionProviderPid(hostPid, providerPids), ERR_OK);
452     EXPECT_EQ(providerPids.size(), 0);
453 }
454 
455 /**
456  * @tc.name: UIExtensionReleationship_0300
457  * @tc.desc: Root host pid and provider pid map test.
458  * @tc.type: FUNC
459  */
460 HWTEST_F(AppRunningManagerTest, UIExtensionReleationship_0300, TestSize.Level1)
461 {
462     auto appRunningManager = std::make_shared<AppRunningManager>();
463     ASSERT_NE(appRunningManager, nullptr);
464 
465     int32_t uiExtensionAbilityIdA = 1;
466     int32_t uiExtensionAbilityIdB = 2;
467     pid_t hostPid = 1001;
468     pid_t providerPidA = 2001;
469     pid_t providerPidB = 2002; // a root host start two uiextensionability.
470     appRunningManager->AddUIExtensionLauncherItem(uiExtensionAbilityIdA, hostPid, providerPidA);
471     appRunningManager->AddUIExtensionLauncherItem(uiExtensionAbilityIdB, hostPid, providerPidB);
472 
473     std::vector<pid_t> hostPids;
474     EXPECT_EQ(appRunningManager->GetAllUIExtensionRootHostPid(providerPidA, hostPids), ERR_OK);
475     EXPECT_EQ(hostPids.size(), 1);
476     EXPECT_EQ(hostPids[0], hostPid);
477     hostPids.clear();
478 
479     EXPECT_EQ(appRunningManager->GetAllUIExtensionRootHostPid(providerPidB, hostPids), ERR_OK);
480     EXPECT_EQ(hostPids.size(), 1);
481     EXPECT_EQ(hostPids[0], hostPid);
482     hostPids.clear();
483 
484     std::vector<pid_t> providerPids;
485     EXPECT_EQ(appRunningManager->GetAllUIExtensionProviderPid(hostPid, providerPids), ERR_OK);
486     EXPECT_EQ(providerPids.size(), 2);
487     EXPECT_EQ(providerPids[0], providerPidA);
488     EXPECT_EQ(providerPids[1], providerPidB);
489     providerPids.clear();
490 
491     EXPECT_EQ(appRunningManager->RemoveUIExtensionLauncherItem(providerPidA), ERR_OK);
492 
493     // Get after remove one provider
494     EXPECT_EQ(appRunningManager->GetAllUIExtensionRootHostPid(providerPidA, hostPids), ERR_OK);
495     EXPECT_EQ(hostPids.size(), 0);
496     EXPECT_EQ(appRunningManager->GetAllUIExtensionRootHostPid(providerPidB, hostPids), ERR_OK);
497     EXPECT_EQ(hostPids.size(), 1);
498     EXPECT_EQ(hostPids[0], hostPid);
499     hostPids.clear();
500 
501     EXPECT_EQ(appRunningManager->GetAllUIExtensionProviderPid(hostPid, providerPids), ERR_OK);
502     EXPECT_EQ(providerPids.size(), 1);
503     EXPECT_EQ(providerPids[0], providerPidB);
504 }
505 
506 /**
507  * @tc.name: UIExtensionReleationship_0400
508  * @tc.desc: Root host pid and provider pid map test.
509  * @tc.type: FUNC
510  */
511 HWTEST_F(AppRunningManagerTest, UIExtensionReleationship_0400, TestSize.Level1)
512 {
513     auto appRunningManager = std::make_shared<AppRunningManager>();
514     ASSERT_NE(appRunningManager, nullptr);
515 
516     int32_t uiExtensionAbilityIdA = 1;
517     int32_t uiExtensionAbilityIdB = 2;
518     pid_t hostPid = 1001;
519     pid_t providerPidA = 2001;
520     pid_t providerPidB = 2002; // a root host start two uiextensionability.
521     appRunningManager->AddUIExtensionLauncherItem(uiExtensionAbilityIdA, hostPid, providerPidA);
522     appRunningManager->AddUIExtensionLauncherItem(uiExtensionAbilityIdB, hostPid, providerPidB);
523 
524     EXPECT_EQ(appRunningManager->RemoveUIExtensionLauncherItem(hostPid), ERR_OK);
525 
526     // Get after remove one provider
527     std::vector<pid_t> hostPids;
528     EXPECT_EQ(appRunningManager->GetAllUIExtensionRootHostPid(providerPidA, hostPids), ERR_OK);
529     EXPECT_EQ(hostPids.size(), 0);
530     EXPECT_EQ(appRunningManager->GetAllUIExtensionRootHostPid(providerPidB, hostPids), ERR_OK);
531     EXPECT_EQ(hostPids.size(), 0);
532 
533     std::vector<pid_t> providerPids;
534     EXPECT_EQ(appRunningManager->GetAllUIExtensionRootHostPid(hostPid, providerPids), ERR_OK);
535     EXPECT_EQ(providerPids.size(), 0);
536 }
537 
538 /**
539  * @tc.name: UIExtensionReleationship_0500
540  * @tc.desc: Root host pid and provider pid map test.
541  * @tc.type: FUNC
542  */
543 HWTEST_F(AppRunningManagerTest, UIExtensionReleationship_0500, TestSize.Level1)
544 {
545     auto appRunningManager = std::make_shared<AppRunningManager>();
546     ASSERT_NE(appRunningManager, nullptr);
547 
548     int32_t uiExtensionAbilityIdA = 1;
549     int32_t uiExtensionAbilityIdB = 2;
550     pid_t hostPidA = 1001;
551     pid_t hostPidB = 1002;
552     pid_t providerPid = 2001; // a uiextensionability has two root host info.
553     appRunningManager->AddUIExtensionLauncherItem(uiExtensionAbilityIdA, hostPidA, providerPid);
554     appRunningManager->AddUIExtensionLauncherItem(uiExtensionAbilityIdB, hostPidB, providerPid);
555 
556     std::vector<pid_t> hostPids;
557     EXPECT_EQ(appRunningManager->GetAllUIExtensionRootHostPid(providerPid, hostPids), ERR_OK);
558     EXPECT_EQ(hostPids.size(), 2);
559     EXPECT_EQ(hostPids[0], hostPidA);
560     EXPECT_EQ(hostPids[1], hostPidB);
561     hostPids.clear();
562 
563     std::vector<pid_t> providerPids;
564     EXPECT_EQ(appRunningManager->GetAllUIExtensionProviderPid(hostPidA, providerPids), ERR_OK);
565     EXPECT_EQ(providerPids.size(), 1);
566     EXPECT_EQ(providerPids[0], providerPid);
567     providerPids.clear();
568     EXPECT_EQ(appRunningManager->GetAllUIExtensionProviderPid(hostPidB, providerPids), ERR_OK);
569     EXPECT_EQ(providerPids.size(), 1);
570     EXPECT_EQ(providerPids[0], providerPid);
571     providerPids.clear();
572 
573     EXPECT_EQ(appRunningManager->RemoveUIExtensionLauncherItem(providerPid), ERR_OK);
574 
575     // Get after remove one provider
576     EXPECT_EQ(appRunningManager->GetAllUIExtensionRootHostPid(providerPid, hostPids), ERR_OK);
577     EXPECT_EQ(hostPids.size(), 0);
578 
579     EXPECT_EQ(appRunningManager->GetAllUIExtensionProviderPid(hostPidA, providerPids), ERR_OK);
580     EXPECT_EQ(providerPids.size(), 0);
581     EXPECT_EQ(appRunningManager->GetAllUIExtensionProviderPid(hostPidB, providerPids), ERR_OK);
582     EXPECT_EQ(providerPids.size(), 0);
583 }
584 
585 /**
586  * @tc.name: UIExtensionReleationship_0600
587  * @tc.desc: Root host pid and provider pid map test.
588  * @tc.type: FUNC
589  */
590 HWTEST_F(AppRunningManagerTest, UIExtensionReleationship_0600, TestSize.Level1)
591 {
592     auto appRunningManager = std::make_shared<AppRunningManager>();
593     ASSERT_NE(appRunningManager, nullptr);
594 
595     int32_t uiExtensionAbilityIdA = 1;
596     int32_t uiExtensionAbilityIdB = 2;
597     pid_t hostPidA = 1001;
598     pid_t hostPidB = 1002;
599     pid_t providerPid = 2001; // a uiextensionability has two root host info.
600     appRunningManager->AddUIExtensionLauncherItem(uiExtensionAbilityIdA, hostPidA, providerPid);
601     appRunningManager->AddUIExtensionLauncherItem(uiExtensionAbilityIdB, hostPidB, providerPid);
602 
603     std::vector<pid_t> hostPids;
604     EXPECT_EQ(appRunningManager->GetAllUIExtensionRootHostPid(providerPid, hostPids), ERR_OK);
605     EXPECT_EQ(hostPids.size(), 2);
606     EXPECT_EQ(hostPids[0], hostPidA);
607     EXPECT_EQ(hostPids[1], hostPidB);
608     hostPids.clear();
609 
610     std::vector<pid_t> providerPids;
611     EXPECT_EQ(appRunningManager->GetAllUIExtensionProviderPid(hostPidA, providerPids), ERR_OK);
612     EXPECT_EQ(providerPids.size(), 1);
613     EXPECT_EQ(providerPids[0], providerPid);
614     providerPids.clear();
615     EXPECT_EQ(appRunningManager->GetAllUIExtensionProviderPid(hostPidB, providerPids), ERR_OK);
616     EXPECT_EQ(providerPids.size(), 1);
617     EXPECT_EQ(providerPids[0], providerPid);
618     providerPids.clear();
619 
620     EXPECT_EQ(appRunningManager->RemoveUIExtensionLauncherItem(hostPidA), ERR_OK);
621 
622     // Get after remove one provider
623     EXPECT_EQ(appRunningManager->GetAllUIExtensionRootHostPid(providerPid, hostPids), ERR_OK);
624     EXPECT_EQ(hostPids.size(), 1);
625     EXPECT_EQ(hostPids[0], hostPidB); // cause host pid A has removed.
626 
627     EXPECT_EQ(appRunningManager->GetAllUIExtensionProviderPid(hostPidA, providerPids), ERR_OK);
628     EXPECT_EQ(providerPids.size(), 0);
629     EXPECT_EQ(appRunningManager->GetAllUIExtensionProviderPid(hostPidB, providerPids), ERR_OK);
630     EXPECT_EQ(providerPids.size(), 1);
631     EXPECT_EQ(providerPids[0], providerPid);
632     providerPids.clear();
633 
634     EXPECT_EQ(appRunningManager->RemoveUIExtensionLauncherItemById(uiExtensionAbilityIdB), ERR_OK);
635     EXPECT_EQ(appRunningManager->GetAllUIExtensionProviderPid(hostPidB, providerPids), ERR_OK);
636     EXPECT_EQ(providerPids.size(), 0);
637 }
638 
639 /**
640  * @tc.name: UIExtensionReleationship_0700
641  * @tc.desc: Root host pid and provider pid map test.
642  * @tc.type: FUNC
643  */
644 HWTEST_F(AppRunningManagerTest, UIExtensionReleationship_0700, TestSize.Level1)
645 {
646     static std::shared_ptr<AppRunningManager> appRunningManager_Rel0700 = std::make_shared<AppRunningManager>();
647     ASSERT_NE(appRunningManager_Rel0700, nullptr);
__anonb0a5bd360302() 648     auto task = []() {
649         TAG_LOGI(AAFwkTag::TEST, "Running in thread %{public}d.", gettid());
650         static int32_t uiExtensionAbilityId = 1;
651         static pid_t hostPid = 100000;
652         static pid_t providerPid = 200000;
653         appRunningManager_Rel0700->AddUIExtensionLauncherItem(uiExtensionAbilityId, hostPid, providerPid);
654         uiExtensionAbilityId++;
655         hostPid += 2;
656         providerPid += 3;
657 
658         appRunningManager_Rel0700->RemoveUIExtensionLauncherItemById(uiExtensionAbilityId);
659         appRunningManager_Rel0700->RemoveUIExtensionLauncherItem(hostPid);
660         appRunningManager_Rel0700->RemoveUIExtensionLauncherItem(providerPid);
661 
662         std::vector<pid_t> hostPids;
663         std::vector<pid_t> providerPids;
664         appRunningManager_Rel0700->GetAllUIExtensionRootHostPid(hostPid, providerPids);
665         appRunningManager_Rel0700->GetAllUIExtensionProviderPid(providerPid, hostPids);
666     };
667 
668     SET_THREAD_NUM(100);
669     GTEST_RUN_TASK(task);
670 }
671 
672 /**
673  * @tc.name: IsAppProcessesAllCached_0100
674  * @tc.desc: MultiProcess application cache check test.
675  * @tc.type: FUNC
676  */
677 HWTEST_F(AppRunningManagerTest, IsAppProcessesAllCached_0100, TestSize.Level1)
678 {
679     static std::shared_ptr<AppRunningManager> appRunningManager = std::make_shared<AppRunningManager>();
680     ASSERT_NE(appRunningManager, nullptr);
681     std::string bundleName;
682     std::shared_ptr<ApplicationInfo> appInfo = std::make_shared<ApplicationInfo>();
683     appInfo->bundleName = "com.tdd.cacheprocesstest";
684     appInfo->apiTargetVersion = 12;
685     appInfo->uid = 1010101;
686     int32_t recordId1 = RECORD_ID;
687     int32_t recordId2 = RECORD_ID + 1;
688     std::string processName = "com.tdd.cacheprocesstest";
689     auto appRunningRecord1 = std::make_shared<AppRunningRecord>(appInfo, recordId1, processName);
690     appRunningRecord1->SetUid(appInfo->uid);
691     appRunningRecord1->SetSupportedProcessCache(true);
692     appRunningRecord1->SetUIAbilityLaunched(true);
693     auto appRunningRecord2 = std::make_shared<AppRunningRecord>(appInfo, recordId2, processName);
694     appRunningRecord2->SetUid(appInfo->uid);
695     appRunningRecord2->SetSupportedProcessCache(true);
696     appRunningRecord2->SetUIAbilityLaunched(true);
697 
698     appRunningManager->appRunningRecordMap_.insert(make_pair(recordId1, appRunningRecord1));
699     std::set<std::shared_ptr<AppRunningRecord>> cachedSet;
700     cachedSet.insert(appRunningRecord1);
701     EXPECT_EQ(appRunningManager->IsAppProcessesAllCached(appInfo->bundleName, appInfo->uid, cachedSet), true);
702 
703     appRunningManager->appRunningRecordMap_.insert(make_pair(recordId2, appRunningRecord2));
704     EXPECT_EQ(appRunningManager->IsAppProcessesAllCached(appInfo->bundleName, appInfo->uid, cachedSet), false);
705 }
706 } // namespace AppExecFwk
707 } // namespace OHOS
708