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