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 appRunningRecord->SetState(ApplicationState::APP_STATE_READY);
264 appInfo->name = "com.huawei.shell_assistant";
265 appRunningRecord = std::make_shared<AppRunningRecord>(appInfo, recordId, processName);
266 EXPECT_EQ(appRunningManager->appRunningRecordMap_.size(), recordId);
267 auto ret = appRunningManager->UpdateConfiguration(config);
268 EXPECT_EQ(ret, ERR_OK);
269 }
270
271 /**
272 * @tc.name: AppRunningManager_UpdateConfiguration_0200
273 * @tc.desc: Test UpdateConfiguration config storage
274 * @tc.type: FUNC
275 */
276 HWTEST_F(AppRunningManagerTest, AppRunningManager_UpdateConfiguration_0200, TestSize.Level1)
277 {
278 auto appRunningManager = std::make_shared<AppRunningManager>();
279 EXPECT_NE(appRunningManager, nullptr);
280 Configuration config;
281 config.AddItem(AAFwk::GlobalConfigurationKey::SYSTEM_COLORMODE, ConfigurationInner::COLOR_MODE_LIGHT);
282 auto ret = appRunningManager->UpdateConfiguration(config);
283 EXPECT_EQ(ret, ERR_OK);
284 }
285
286 /**
287 * @tc.name: AppRunningManager_UpdateConfiguration_0300
288 * @tc.desc: Test UpdateConfiguration delayed
289 * @tc.type: FUNC
290 */
291 HWTEST_F(AppRunningManagerTest, AppRunningManager_UpdateConfiguration_0300, TestSize.Level1)
292 {
293 auto appRunningManager = std::make_shared<AppRunningManager>();
294 EXPECT_NE(appRunningManager, nullptr);
295 std::shared_ptr<ApplicationInfo> appInfo = std::make_shared<ApplicationInfo>();
296 int32_t recordId = 1;
297 std::string processName;
298 Configuration config;
299 auto appRunningRecord = std::make_shared<AppRunningRecord>(appInfo, recordId, processName);
300 appRunningManager->appRunningRecordMap_.emplace(recordId, appRunningRecord);
301 appRunningRecord = std::make_shared<AppRunningRecord>(appInfo, recordId, processName);
302 appRunningRecord->SetState(ApplicationState::APP_STATE_BACKGROUND);
303 appRunningManager->appRunningRecordMap_.emplace(++recordId, appRunningRecord);
304 auto ret = appRunningManager->UpdateConfiguration(config);
305 EXPECT_EQ(ret, ERR_OK);
306 EXPECT_EQ(appRunningManager->updateConfigurationDelayedMap_[0], false);
307 EXPECT_EQ(appRunningManager->updateConfigurationDelayedMap_[1], true);
308 }
309
310 /**
311 * @tc.name: RemoveAppRunningRecordById_0100
312 * @tc.desc: Remove app running record by id.
313 * @tc.type: FUNC
314 */
315 HWTEST_F(AppRunningManagerTest, RemoveAppRunningRecordById_0100, TestSize.Level1)
316 {
317 auto appRunningManager = std::make_shared<AppRunningManager>();
318 ASSERT_NE(appRunningManager, nullptr);
319
320 std::shared_ptr<ApplicationInfo> appInfo = std::make_shared<ApplicationInfo>();
321 std::string processName = "test.ProcessName";
322 BundleInfo bundleInfo;
323 auto appRecord = appRunningManager->CreateAppRunningRecord(appInfo, processName, bundleInfo);
324 ASSERT_NE(appRecord, nullptr);
325
326 int32_t uiExtensionAbilityId = 1000;
327 pid_t hostPid = 1001;
328 pid_t providerPid = 1002;
329 appRunningManager->AddUIExtensionLauncherItem(uiExtensionAbilityId, hostPid, providerPid);
330
331 appRunningManager->RemoveAppRunningRecordById(appRecord->GetRecordId());
332 }
333
334 /**
335 * @tc.name: AppRunningRecord_0100
336 * @tc.desc: AppRunningRecord test multi-thread.
337 * @tc.type: FUNC
338 */
339 HWTEST_F(AppRunningManagerTest, AppRunningRecord_0100, TestSize.Level1)
340 {
341 static std::shared_ptr<AppRunningManager> appRunningManager_Record0100 = std::make_shared<AppRunningManager>();
342 ASSERT_NE(appRunningManager_Record0100, nullptr);
__anond6bc36c00202() 343 auto task = []() {
344 TAG_LOGI(AAFwkTag::TEST, "Running in thread %{public}d.", gettid());
345 std::shared_ptr<ApplicationInfo> appInfo = std::make_shared<ApplicationInfo>();
346 static std::string processName = "test.ProcessName";
347 BundleInfo bundleInfo;
348 auto appRecord = appRunningManager_Record0100->CreateAppRunningRecord(appInfo, processName, bundleInfo);
349 ASSERT_NE(appRecord, nullptr);
350 processName += "a";
351
352 static int32_t uiExtensionAbilityId = 1;
353 static pid_t hostPid = 100000;
354 static pid_t providerPid = 200000;
355 appRunningManager_Record0100->AddUIExtensionLauncherItem(uiExtensionAbilityId, hostPid, providerPid);
356 uiExtensionAbilityId++;
357 hostPid += 2;
358 providerPid += 3;
359
360 appRunningManager_Record0100->RemoveUIExtensionLauncherItem(hostPid);
361 appRunningManager_Record0100->RemoveUIExtensionLauncherItem(providerPid);
362
363 std::vector<pid_t> hostPids;
364 std::vector<pid_t> providerPids;
365 appRunningManager_Record0100->GetAllUIExtensionRootHostPid(hostPid, providerPids);
366 appRunningManager_Record0100->GetAllUIExtensionProviderPid(providerPid, hostPids);
367
368 appRunningManager_Record0100->RemoveAppRunningRecordById(appRecord->GetRecordId());
369 };
370
371 SET_THREAD_NUM(100);
372 GTEST_RUN_TASK(task);
373 }
374
375 /**
376 * @tc.name: UIExtensionReleationship_0100
377 * @tc.desc: Root host pid and provider pid map test.
378 * @tc.type: FUNC
379 */
380 HWTEST_F(AppRunningManagerTest, UIExtensionReleationship_0100, TestSize.Level1)
381 {
382 auto appRunningManager = std::make_shared<AppRunningManager>();
383 ASSERT_NE(appRunningManager, nullptr);
384
385 int32_t uiExtensionAbilityId = 1000;
386 pid_t hostPid = 1001;
387 pid_t providerPid = 1002;
388 appRunningManager->AddUIExtensionLauncherItem(uiExtensionAbilityId, hostPid, providerPid);
389
390 std::vector<pid_t> hostPids;
391 EXPECT_EQ(appRunningManager->GetAllUIExtensionRootHostPid(providerPid, hostPids), ERR_OK);
392 EXPECT_EQ(hostPids.size(), 1);
393 EXPECT_EQ(hostPids[0], hostPid);
394 hostPids.clear();
395
396 std::vector<pid_t> providerPids;
397 EXPECT_EQ(appRunningManager->GetAllUIExtensionProviderPid(hostPid, providerPids), ERR_OK);
398 EXPECT_EQ(providerPids.size(), 1);
399 EXPECT_EQ(providerPids[0], providerPid);
400 providerPids.clear();
401
402 EXPECT_EQ(appRunningManager->RemoveUIExtensionLauncherItem(hostPid), ERR_OK);
403 EXPECT_EQ(appRunningManager->RemoveUIExtensionLauncherItem(providerPid), ERR_OK);
404
405 // Get after remove.
406 EXPECT_EQ(appRunningManager->GetAllUIExtensionRootHostPid(providerPid, hostPids), ERR_OK);
407 EXPECT_EQ(hostPids.size(), 0);
408 EXPECT_EQ(appRunningManager->GetAllUIExtensionProviderPid(hostPid, providerPids), ERR_OK);
409 EXPECT_EQ(providerPids.size(), 0);
410 }
411
412 /**
413 * @tc.name: UIExtensionReleationship_0200
414 * @tc.desc: Root host pid and provider pid map test.
415 * @tc.type: FUNC
416 */
417 HWTEST_F(AppRunningManagerTest, UIExtensionReleationship_0200, TestSize.Level1)
418 {
419 auto appRunningManager = std::make_shared<AppRunningManager>();
420 ASSERT_NE(appRunningManager, nullptr);
421
422 int32_t uiExtensionAbilityId = 1000;
423 pid_t hostPid = 1001;
424 pid_t providerPid = 1001; // same with host pid, ie uiability and uiextensionability in same process.
425 appRunningManager->AddUIExtensionLauncherItem(uiExtensionAbilityId, hostPid, providerPid);
426
427 std::vector<pid_t> hostPids;
428 EXPECT_EQ(appRunningManager->GetAllUIExtensionRootHostPid(providerPid, hostPids), ERR_OK);
429 EXPECT_EQ(hostPids.size(), 1);
430 EXPECT_EQ(hostPids[0], hostPid);
431 hostPids.clear();
432
433 std::vector<pid_t> providerPids;
434 EXPECT_EQ(appRunningManager->GetAllUIExtensionProviderPid(hostPid, providerPids), ERR_OK);
435 EXPECT_EQ(providerPids.size(), 1);
436 EXPECT_EQ(providerPids[0], providerPid);
437 providerPids.clear();
438
439 EXPECT_EQ(appRunningManager->RemoveUIExtensionLauncherItem(hostPid), ERR_OK);
440 EXPECT_EQ(appRunningManager->RemoveUIExtensionLauncherItem(providerPid), ERR_OK);
441
442 // Get after remove.
443 EXPECT_EQ(appRunningManager->GetAllUIExtensionRootHostPid(providerPid, hostPids), ERR_OK);
444 EXPECT_EQ(hostPids.size(), 0);
445 EXPECT_EQ(appRunningManager->GetAllUIExtensionProviderPid(hostPid, providerPids), ERR_OK);
446 EXPECT_EQ(providerPids.size(), 0);
447 }
448
449 /**
450 * @tc.name: UIExtensionReleationship_0300
451 * @tc.desc: Root host pid and provider pid map test.
452 * @tc.type: FUNC
453 */
454 HWTEST_F(AppRunningManagerTest, UIExtensionReleationship_0300, TestSize.Level1)
455 {
456 auto appRunningManager = std::make_shared<AppRunningManager>();
457 ASSERT_NE(appRunningManager, nullptr);
458
459 int32_t uiExtensionAbilityIdA = 1;
460 int32_t uiExtensionAbilityIdB = 2;
461 pid_t hostPid = 1001;
462 pid_t providerPidA = 2001;
463 pid_t providerPidB = 2002; // a root host start two uiextensionability.
464 appRunningManager->AddUIExtensionLauncherItem(uiExtensionAbilityIdA, hostPid, providerPidA);
465 appRunningManager->AddUIExtensionLauncherItem(uiExtensionAbilityIdB, hostPid, providerPidB);
466
467 std::vector<pid_t> hostPids;
468 EXPECT_EQ(appRunningManager->GetAllUIExtensionRootHostPid(providerPidA, hostPids), ERR_OK);
469 EXPECT_EQ(hostPids.size(), 1);
470 EXPECT_EQ(hostPids[0], hostPid);
471 hostPids.clear();
472
473 EXPECT_EQ(appRunningManager->GetAllUIExtensionRootHostPid(providerPidB, hostPids), ERR_OK);
474 EXPECT_EQ(hostPids.size(), 1);
475 EXPECT_EQ(hostPids[0], hostPid);
476 hostPids.clear();
477
478 std::vector<pid_t> providerPids;
479 EXPECT_EQ(appRunningManager->GetAllUIExtensionProviderPid(hostPid, providerPids), ERR_OK);
480 EXPECT_EQ(providerPids.size(), 2);
481 EXPECT_EQ(providerPids[0], providerPidA);
482 EXPECT_EQ(providerPids[1], providerPidB);
483 providerPids.clear();
484
485 EXPECT_EQ(appRunningManager->RemoveUIExtensionLauncherItem(providerPidA), ERR_OK);
486
487 // Get after remove one provider
488 EXPECT_EQ(appRunningManager->GetAllUIExtensionRootHostPid(providerPidA, hostPids), ERR_OK);
489 EXPECT_EQ(hostPids.size(), 0);
490 EXPECT_EQ(appRunningManager->GetAllUIExtensionRootHostPid(providerPidB, hostPids), ERR_OK);
491 EXPECT_EQ(hostPids.size(), 1);
492 EXPECT_EQ(hostPids[0], hostPid);
493 hostPids.clear();
494
495 EXPECT_EQ(appRunningManager->GetAllUIExtensionProviderPid(hostPid, providerPids), ERR_OK);
496 EXPECT_EQ(providerPids.size(), 1);
497 EXPECT_EQ(providerPids[0], providerPidB);
498 }
499
500 /**
501 * @tc.name: UIExtensionReleationship_0400
502 * @tc.desc: Root host pid and provider pid map test.
503 * @tc.type: FUNC
504 */
505 HWTEST_F(AppRunningManagerTest, UIExtensionReleationship_0400, TestSize.Level1)
506 {
507 auto appRunningManager = std::make_shared<AppRunningManager>();
508 ASSERT_NE(appRunningManager, nullptr);
509
510 int32_t uiExtensionAbilityIdA = 1;
511 int32_t uiExtensionAbilityIdB = 2;
512 pid_t hostPid = 1001;
513 pid_t providerPidA = 2001;
514 pid_t providerPidB = 2002; // a root host start two uiextensionability.
515 appRunningManager->AddUIExtensionLauncherItem(uiExtensionAbilityIdA, hostPid, providerPidA);
516 appRunningManager->AddUIExtensionLauncherItem(uiExtensionAbilityIdB, hostPid, providerPidB);
517
518 EXPECT_EQ(appRunningManager->RemoveUIExtensionLauncherItem(hostPid), ERR_OK);
519
520 // Get after remove one provider
521 std::vector<pid_t> hostPids;
522 EXPECT_EQ(appRunningManager->GetAllUIExtensionRootHostPid(providerPidA, hostPids), ERR_OK);
523 EXPECT_EQ(hostPids.size(), 0);
524 EXPECT_EQ(appRunningManager->GetAllUIExtensionRootHostPid(providerPidB, hostPids), ERR_OK);
525 EXPECT_EQ(hostPids.size(), 0);
526
527 std::vector<pid_t> providerPids;
528 EXPECT_EQ(appRunningManager->GetAllUIExtensionRootHostPid(hostPid, providerPids), ERR_OK);
529 EXPECT_EQ(providerPids.size(), 0);
530 }
531
532 /**
533 * @tc.name: UIExtensionReleationship_0500
534 * @tc.desc: Root host pid and provider pid map test.
535 * @tc.type: FUNC
536 */
537 HWTEST_F(AppRunningManagerTest, UIExtensionReleationship_0500, TestSize.Level1)
538 {
539 auto appRunningManager = std::make_shared<AppRunningManager>();
540 ASSERT_NE(appRunningManager, nullptr);
541
542 int32_t uiExtensionAbilityIdA = 1;
543 int32_t uiExtensionAbilityIdB = 2;
544 pid_t hostPidA = 1001;
545 pid_t hostPidB = 1002;
546 pid_t providerPid = 2001; // a uiextensionability has two root host info.
547 appRunningManager->AddUIExtensionLauncherItem(uiExtensionAbilityIdA, hostPidA, providerPid);
548 appRunningManager->AddUIExtensionLauncherItem(uiExtensionAbilityIdB, hostPidB, providerPid);
549
550 std::vector<pid_t> hostPids;
551 EXPECT_EQ(appRunningManager->GetAllUIExtensionRootHostPid(providerPid, hostPids), ERR_OK);
552 EXPECT_EQ(hostPids.size(), 2);
553 EXPECT_EQ(hostPids[0], hostPidA);
554 EXPECT_EQ(hostPids[1], hostPidB);
555 hostPids.clear();
556
557 std::vector<pid_t> providerPids;
558 EXPECT_EQ(appRunningManager->GetAllUIExtensionProviderPid(hostPidA, providerPids), ERR_OK);
559 EXPECT_EQ(providerPids.size(), 1);
560 EXPECT_EQ(providerPids[0], providerPid);
561 providerPids.clear();
562 EXPECT_EQ(appRunningManager->GetAllUIExtensionProviderPid(hostPidB, providerPids), ERR_OK);
563 EXPECT_EQ(providerPids.size(), 1);
564 EXPECT_EQ(providerPids[0], providerPid);
565 providerPids.clear();
566
567 EXPECT_EQ(appRunningManager->RemoveUIExtensionLauncherItem(providerPid), ERR_OK);
568
569 // Get after remove one provider
570 EXPECT_EQ(appRunningManager->GetAllUIExtensionRootHostPid(providerPid, hostPids), ERR_OK);
571 EXPECT_EQ(hostPids.size(), 0);
572
573 EXPECT_EQ(appRunningManager->GetAllUIExtensionProviderPid(hostPidA, providerPids), ERR_OK);
574 EXPECT_EQ(providerPids.size(), 0);
575 EXPECT_EQ(appRunningManager->GetAllUIExtensionProviderPid(hostPidB, providerPids), ERR_OK);
576 EXPECT_EQ(providerPids.size(), 0);
577 }
578
579 /**
580 * @tc.name: UIExtensionReleationship_0600
581 * @tc.desc: Root host pid and provider pid map test.
582 * @tc.type: FUNC
583 */
584 HWTEST_F(AppRunningManagerTest, UIExtensionReleationship_0600, TestSize.Level1)
585 {
586 auto appRunningManager = std::make_shared<AppRunningManager>();
587 ASSERT_NE(appRunningManager, nullptr);
588
589 int32_t uiExtensionAbilityIdA = 1;
590 int32_t uiExtensionAbilityIdB = 2;
591 pid_t hostPidA = 1001;
592 pid_t hostPidB = 1002;
593 pid_t providerPid = 2001; // a uiextensionability has two root host info.
594 appRunningManager->AddUIExtensionLauncherItem(uiExtensionAbilityIdA, hostPidA, providerPid);
595 appRunningManager->AddUIExtensionLauncherItem(uiExtensionAbilityIdB, hostPidB, providerPid);
596
597 std::vector<pid_t> hostPids;
598 EXPECT_EQ(appRunningManager->GetAllUIExtensionRootHostPid(providerPid, hostPids), ERR_OK);
599 EXPECT_EQ(hostPids.size(), 2);
600 EXPECT_EQ(hostPids[0], hostPidA);
601 EXPECT_EQ(hostPids[1], hostPidB);
602 hostPids.clear();
603
604 std::vector<pid_t> providerPids;
605 EXPECT_EQ(appRunningManager->GetAllUIExtensionProviderPid(hostPidA, providerPids), ERR_OK);
606 EXPECT_EQ(providerPids.size(), 1);
607 EXPECT_EQ(providerPids[0], providerPid);
608 providerPids.clear();
609 EXPECT_EQ(appRunningManager->GetAllUIExtensionProviderPid(hostPidB, providerPids), ERR_OK);
610 EXPECT_EQ(providerPids.size(), 1);
611 EXPECT_EQ(providerPids[0], providerPid);
612 providerPids.clear();
613
614 EXPECT_EQ(appRunningManager->RemoveUIExtensionLauncherItem(hostPidA), ERR_OK);
615
616 // Get after remove one provider
617 EXPECT_EQ(appRunningManager->GetAllUIExtensionRootHostPid(providerPid, hostPids), ERR_OK);
618 EXPECT_EQ(hostPids.size(), 1);
619 EXPECT_EQ(hostPids[0], hostPidB); // cause host pid A has removed.
620
621 EXPECT_EQ(appRunningManager->GetAllUIExtensionProviderPid(hostPidA, providerPids), ERR_OK);
622 EXPECT_EQ(providerPids.size(), 0);
623 EXPECT_EQ(appRunningManager->GetAllUIExtensionProviderPid(hostPidB, providerPids), ERR_OK);
624 EXPECT_EQ(providerPids.size(), 1);
625 EXPECT_EQ(providerPids[0], providerPid);
626 providerPids.clear();
627
628 EXPECT_EQ(appRunningManager->RemoveUIExtensionLauncherItemById(uiExtensionAbilityIdB), ERR_OK);
629 EXPECT_EQ(appRunningManager->GetAllUIExtensionProviderPid(hostPidB, providerPids), ERR_OK);
630 EXPECT_EQ(providerPids.size(), 0);
631 }
632
633 /**
634 * @tc.name: UIExtensionReleationship_0700
635 * @tc.desc: Root host pid and provider pid map test.
636 * @tc.type: FUNC
637 */
638 HWTEST_F(AppRunningManagerTest, UIExtensionReleationship_0700, TestSize.Level1)
639 {
640 static std::shared_ptr<AppRunningManager> appRunningManager_Rel0700 = std::make_shared<AppRunningManager>();
641 ASSERT_NE(appRunningManager_Rel0700, nullptr);
__anond6bc36c00302() 642 auto task = []() {
643 TAG_LOGI(AAFwkTag::TEST, "Running in thread %{public}d.", gettid());
644 static int32_t uiExtensionAbilityId = 1;
645 static pid_t hostPid = 100000;
646 static pid_t providerPid = 200000;
647 appRunningManager_Rel0700->AddUIExtensionLauncherItem(uiExtensionAbilityId, hostPid, providerPid);
648 uiExtensionAbilityId++;
649 hostPid += 2;
650 providerPid += 3;
651
652 appRunningManager_Rel0700->RemoveUIExtensionLauncherItemById(uiExtensionAbilityId);
653 appRunningManager_Rel0700->RemoveUIExtensionLauncherItem(hostPid);
654 appRunningManager_Rel0700->RemoveUIExtensionLauncherItem(providerPid);
655
656 std::vector<pid_t> hostPids;
657 std::vector<pid_t> providerPids;
658 appRunningManager_Rel0700->GetAllUIExtensionRootHostPid(hostPid, providerPids);
659 appRunningManager_Rel0700->GetAllUIExtensionProviderPid(providerPid, hostPids);
660 };
661
662 SET_THREAD_NUM(100);
663 GTEST_RUN_TASK(task);
664 }
665
666 /**
667 * @tc.name: IsAppProcessesAllCached_0100
668 * @tc.desc: MultiProcess application cache check test.
669 * @tc.type: FUNC
670 */
671 HWTEST_F(AppRunningManagerTest, IsAppProcessesAllCached_0100, TestSize.Level1)
672 {
673 static std::shared_ptr<AppRunningManager> appRunningManager = std::make_shared<AppRunningManager>();
674 ASSERT_NE(appRunningManager, nullptr);
675 std::string bundleName;
676 std::shared_ptr<ApplicationInfo> appInfo = std::make_shared<ApplicationInfo>();
677 appInfo->bundleName = "com.tdd.cacheprocesstest";
678 appInfo->apiTargetVersion = 12;
679 appInfo->uid = 1010101;
680 int32_t recordId1 = RECORD_ID;
681 int32_t recordId2 = RECORD_ID + 1;
682 std::string processName = "com.tdd.cacheprocesstest";
683 auto appRunningRecord1 = std::make_shared<AppRunningRecord>(appInfo, recordId1, processName);
684 appRunningRecord1->SetUid(appInfo->uid);
685 appRunningRecord1->SetSupportedProcessCache(true);
686 appRunningRecord1->SetUIAbilityLaunched(true);
687 auto appRunningRecord2 = std::make_shared<AppRunningRecord>(appInfo, recordId2, processName);
688 appRunningRecord2->SetUid(appInfo->uid);
689 appRunningRecord2->SetSupportedProcessCache(true);
690 appRunningRecord2->SetUIAbilityLaunched(true);
691
692 appRunningManager->appRunningRecordMap_.insert(make_pair(recordId1, appRunningRecord1));
693 std::set<std::shared_ptr<AppRunningRecord>> cachedSet;
694 cachedSet.insert(appRunningRecord1);
695 EXPECT_EQ(appRunningManager->IsAppProcessesAllCached(appInfo->bundleName, appInfo->uid, cachedSet), true);
696
697 appRunningManager->appRunningRecordMap_.insert(make_pair(recordId2, appRunningRecord2));
698 EXPECT_EQ(appRunningManager->IsAppProcessesAllCached(appInfo->bundleName, appInfo->uid, cachedSet), false);
699 }
700 } // namespace AppExecFwk
701 } // namespace OHOS
702