From 988055f5fd5997628762879d6f08325565f3df5c Mon Sep 17 00:00:00 2001 From: liangzhao Date: Wed, 3 Jul 2024 03:32:21 +0000 Subject: [PATCH] fix loop operation for ability in ability cache Signed-off-by: liangzhao --- .../include/ability_cache_manager.h | 40 +++++ .../include/ability_connect_manager.h | 1 + .../abilitymgr/src/ability_cache_manager.cpp | 97 ++++++++++- .../src/ability_connect_manager.cpp | 132 ++++++++++++-- .../ability_cache_manager_test.cpp | 164 ++++++++++++++++++ 5 files changed, 414 insertions(+), 20 deletions(-) diff --git a/services/abilitymgr/include/ability_cache_manager.h b/services/abilitymgr/include/ability_cache_manager.h index fb23674f35..1c2538288a 100644 --- a/services/abilitymgr/include/ability_cache_manager.h +++ b/services/abilitymgr/include/ability_cache_manager.h @@ -21,6 +21,8 @@ #include #include +#include "ability_config.h" +#include "ability_info.h" #include "ability_record.h" namespace OHOS { namespace AAFwk { @@ -31,6 +33,7 @@ namespace AAFwk { class AbilityCacheManager { public: using AbilityInfo = OHOS::AppExecFwk::AbilityInfo; + using AbilityType = OHOS::AppExecFwk::AbilityType; /** * Get ability cache manager. * @return AbilityCacheManager @@ -70,6 +73,43 @@ public: * @return AbilityRecord if one is matched, otherwise nullptr. */ std::shared_ptr FindRecordByToken(const sptr &token); + + /** + * Get all the abilities of current ability cache manager. + * @return AbilityRecord list. + */ + std::list> GetAbilityList(); + + /** + * Get a single ability by sessionId from ability cache manager. + * @param assertSessionId the ability assertSessionId to be searched in cache manager. + * @return AbilityRecord if one is matched, otherwise nullptr. + */ + std::shared_ptr FindRecordBySessionId(const std::string &assertSessionId); + + /** + * Get a single ability by serviceKey from ability cache manager. + * @param serviceKey the ability serviceKey to be searched in cache manager. + * @return AbilityRecord if one is matched, otherwise nullptr. + */ + std::shared_ptr FindRecordByServiceKey(const std::string &serviceKey); + + /** + * Remove the launcher death recipient from ability cache manager. + */ + void RemoveLauncherDeathRecipient(); + + /** + * Sign the restart flag by bundleName of ability from ability cache manager. + * @param bundleName the ability bundleName to be searched in cache manager. + */ + void SignRestartAppFlag(const std::string &bundleName); + + /** + * Delete the invalid ability by bundleName from ability cache manager. + * @param bundleName the ability bundleName to be searched in cache manager. + */ + void DeleteInvalidServiceRecord(const std::string &bundleName); private: AbilityCacheManager(); ~AbilityCacheManager(); diff --git a/services/abilitymgr/include/ability_connect_manager.h b/services/abilitymgr/include/ability_connect_manager.h index 526a98850b..a0f8871ce6 100644 --- a/services/abilitymgr/include/ability_connect_manager.h +++ b/services/abilitymgr/include/ability_connect_manager.h @@ -590,6 +590,7 @@ private: void KeepAbilityAlive(const std::shared_ptr &abilityRecord, int32_t currentUserId); void ProcessEliminateAbilityRecord(std::shared_ptr eliminateRecord); + std::string GetServiceKey(const std::shared_ptr &service); private: const std::string TASK_ON_CALLBACK_DIED = "OnCallbackDiedTask"; diff --git a/services/abilitymgr/src/ability_cache_manager.cpp b/services/abilitymgr/src/ability_cache_manager.cpp index b6e3763d04..cde717d481 100644 --- a/services/abilitymgr/src/ability_cache_manager.cpp +++ b/services/abilitymgr/src/ability_cache_manager.cpp @@ -21,6 +21,8 @@ namespace OHOS { namespace AAFwk { +const std::string FRS_APP_INDEX = "ohos.extra.param.key.frs_index"; +const std::string FRS_BUNDLE_NAME = "com.ohos.formrenderservice"; AbilityCacheManager::AbilityCacheManager() {} @@ -194,12 +196,14 @@ std::shared_ptr AbilityCacheManager::FindRecordByToken(const sptr TAG_LOGE(AAFwkTag::ABILITYMGR, "The param token is nullptr for FindRecordByToken operation."); return nullptr; } + std::lock_guard lock(mutex_); auto it = devRecLru_.begin(); while (it != devRecLru_.end()) { sptr srcToken = (*it)->GetToken(); if (srcToken == token) { std::shared_ptr &abilityRecord = *it; - TAG_LOGD(AAFwkTag::ABILITYMGR, "Find the ability from lru, service:%{public}s, extension type %{public}d", + TAG_LOGD(AAFwkTag::ABILITYMGR, + "Find the ability by token from lru, service:%{public}s, extension type %{public}d", abilityRecord->GetURI().c_str(), abilityRecord->GetAbilityInfo().extensionAbilityType); return abilityRecord; } else { @@ -208,5 +212,96 @@ std::shared_ptr AbilityCacheManager::FindRecordByToken(const sptr } return nullptr; } + +std::list> AbilityCacheManager::GetAbilityList() +{ + std::lock_guard lock(mutex_); + return devRecLru_; +} + +std::shared_ptr AbilityCacheManager::FindRecordBySessionId(const std::string &assertSessionId) +{ + std::lock_guard lock(mutex_); + auto it = devRecLru_.begin(); + while (it != devRecLru_.end()) { + auto assertSessionStr = (*it)->GetWant().GetStringParam(Want::PARAM_ASSERT_FAULT_SESSION_ID); + if (assertSessionStr == assertSessionId) { + std::shared_ptr &abilityRecord = *it; + TAG_LOGD(AAFwkTag::ABILITYMGR, + "Find the ability by sessionId from lru, service:%{public}s, extension type %{public}d", + abilityRecord->GetURI().c_str(), abilityRecord->GetAbilityInfo().extensionAbilityType); + return abilityRecord; + } else { + it++; + } + } + return nullptr; +} + +std::shared_ptr AbilityCacheManager::FindRecordByServiceKey(const std::string &serviceKey) +{ + std::lock_guard lock(mutex_); + auto it = devRecLru_.begin(); + while (it != devRecLru_.end()) { + std::string curServiceKey = (*it)->GetURI(); + if (FRS_BUNDLE_NAME == (*it)->GetAbilityInfo().bundleName) { + curServiceKey = curServiceKey + std::to_string((*it)->GetWant().GetIntParam(FRS_APP_INDEX, 0)); + } + if (curServiceKey.compare(serviceKey) == 0) { + std::shared_ptr &abilityRecord = *it; + TAG_LOGD(AAFwkTag::ABILITYMGR, + "Find the ability by serviceKey from lru, service:%{public}s, extension type %{public}d", + abilityRecord->GetURI().c_str(), abilityRecord->GetAbilityInfo().extensionAbilityType); + return abilityRecord; + } else { + it++; + } + } + return nullptr; +} + +void AbilityCacheManager::RemoveLauncherDeathRecipient() +{ + std::lock_guard lock(mutex_); + auto it = devRecLru_.begin(); + while (it != devRecLru_.end()) { + auto targetExtension = *it; + if (targetExtension != nullptr && targetExtension->GetAbilityInfo().type == AbilityType::EXTENSION && + ((targetExtension->GetAbilityInfo().name == AbilityConfig::LAUNCHER_ABILITY_NAME && + targetExtension->GetAbilityInfo().bundleName == AbilityConfig::LAUNCHER_BUNDLE_NAME) || + targetExtension->IsSceneBoard())) { + targetExtension->RemoveAbilityDeathRecipient(); + return; + } + it++; + } +} + +void AbilityCacheManager::SignRestartAppFlag(const std::string &bundleName) +{ + std::lock_guard lock(mutex_); + auto it = devRecLru_.begin(); + while (it != devRecLru_.end()) { + auto abilityRecord = *it; + if (abilityRecord != nullptr && abilityRecord->GetApplicationInfo().bundleName == bundleName) { + abilityRecord->SetRestartAppFlag(true); + } + it++; + } +} + +void AbilityCacheManager::DeleteInvalidServiceRecord(const std::string &bundleName) +{ + std::lock_guard lock(mutex_); + auto it = devRecLru_.begin(); + while (it != devRecLru_.end()) { + auto abilityRecord = *it; + if (abilityRecord != nullptr && abilityRecord->GetApplicationInfo().bundleName == bundleName) { + RemoveAbilityRecInProcList(abilityRecord); + RemoveAbilityRecInDevList(abilityRecord); + } + it++; + } +} } // namespace AAFwk } // namespace OHOS \ No newline at end of file diff --git a/services/abilitymgr/src/ability_connect_manager.cpp b/services/abilitymgr/src/ability_connect_manager.cpp index 269ec90ad0..c68d5853ed 100644 --- a/services/abilitymgr/src/ability_connect_manager.cpp +++ b/services/abilitymgr/src/ability_connect_manager.cpp @@ -765,7 +765,8 @@ int AbilityConnectManager::DisconnectAbilityLocked(const sptr abilityRecord) { TAG_LOGI(AAFwkTag::ABILITYMGR, "terminate record called."); - if (!GetAbilityRecordById(abilityRecord->GetRecordId())) { + if (!GetAbilityRecordById(abilityRecord->GetRecordId()) && + !AbilityCacheManager::GetInstance().FindRecordByToken(abilityRecord->GetToken())) { return; } auto timeoutTask = [abilityRecord, connectManager = shared_from_this()]() { @@ -892,6 +893,22 @@ void AbilityConnectManager::OnAppStateChanged(const AppInfo &info) } } }); + + auto cacheAbilityList = AbilityCacheManager::GetInstance().GetAbilityList(); + std::for_each(cacheAbilityList.begin(), cacheAbilityList.end(), [&info](std::shared_ptr &service) { + if (service && (info.processName == service->GetAbilityInfo().process || + info.processName == service->GetApplicationInfo().bundleName)) { + auto appName = service->GetApplicationInfo().name; + auto uid = service->GetAbilityInfo().applicationInfo.uid; + auto isExist = [&appName, &uid](const AppData &appData) { + return appData.appName == appName && appData.uid == uid; + }; + auto iter = std::find_if(info.appData.begin(), info.appData.end(), isExist); + if (iter != info.appData.end()) { + service->SetAppState(info.state); + } + } + }); } int AbilityConnectManager::AbilityTransitionDone(const sptr &token, int state) @@ -2175,7 +2192,7 @@ void AbilityConnectManager::HandleNotifyAssertFaultDialogDied(const std::shared_ void AbilityConnectManager::CloseAssertDialog(const std::string &assertSessionId) { TAG_LOGD(AAFwkTag::ABILITYMGR, "Called"); - sptr token; + std::shared_ptr abilityRecord = nullptr; { std::lock_guard lock(serviceMapMutex_); for (const auto &item : serviceMap_) { @@ -2185,14 +2202,26 @@ void AbilityConnectManager::CloseAssertDialog(const std::string &assertSessionId auto assertSessionStr = item.second->GetWant().GetStringParam(Want::PARAM_ASSERT_FAULT_SESSION_ID); if (assertSessionStr == assertSessionId) { - TAG_LOGD(AAFwkTag::ABILITYMGR, "Terminate assert fault dialog called."); - terminatingExtensionMap_.emplace(item.first, item.second); - token = item.second->GetToken(); + abilityRecord = item.second; serviceMap_.erase(item.first); break; } } } + if (abilityRecord == nullptr) { + abilityRecord = AbilityCacheManager::GetInstance().FindRecordBySessionId(assertSessionId); + AbilityCacheManager::GetInstance().Remove(abilityRecord); + } + if (abilityRecord == nullptr) { + return; + } + TAG_LOGD(AAFwkTag::ABILITYMGR, "Terminate assert fault dialog called."); + std::string serviceKey = abilityRecord->GetURI(); + if (FRS_BUNDLE_NAME == abilityRecord->GetAbilityInfo().bundleName) { + serviceKey = serviceKey + std::to_string(abilityRecord->GetWant().GetIntParam(FRS_APP_INDEX, 0)); + } + terminatingExtensionMap_.emplace(serviceKey, abilityRecord); + sptr token = abilityRecord->GetToken(); if (token != nullptr) { std::lock_guard lock(serialMutex_); TerminateAbilityLocked(token); @@ -2273,10 +2302,20 @@ void AbilityConnectManager::RestartAbility(const std::shared_ptr } } +std::string AbilityConnectManager::GetServiceKey(const std::shared_ptr &service) +{ + std::string serviceKey = service->GetURI(); + if (FRS_BUNDLE_NAME == service->GetAbilityInfo().bundleName) { + serviceKey = serviceKey + std::to_string(service->GetWant().GetIntParam(FRS_APP_INDEX, 0)); + } + return serviceKey; +} + void AbilityConnectManager::DumpState(std::vector &info, bool isClient, const std::string &args) { TAG_LOGI(AAFwkTag::ABILITYMGR, "args:%{public}s.", args.c_str()); auto serviceMapBack = GetServiceMap(); + auto cacheList = AbilityCacheManager::GetInstance().GetAbilityList(); if (!args.empty()) { auto it = std::find_if(serviceMapBack.begin(), serviceMapBack.end(), [&args](const auto &service) { return service.first.compare(args) == 0; @@ -2287,7 +2326,21 @@ void AbilityConnectManager::DumpState(std::vector &info, bool isCli it->second->DumpService(info, isClient); } } else { - info.emplace_back(args + ": Nothing to dump."); + info.emplace_back(args + ": Nothing to dump from serviceMap."); + } + + std::string serviceKey; + auto iter = std::find_if(cacheList.begin(), cacheList.end(), [&args, &serviceKey, this](const auto &service) { + serviceKey = GetServiceKey(service); + return serviceKey.compare(args) == 0; + }); + if (iter != cacheList.end()) { + info.emplace_back("uri [ " + serviceKey + " ]"); + if (*iter != nullptr) { + (*iter)->DumpService(info, isClient); + } + } else { + info.emplace_back(args + ": Nothing to dump from lru cache."); } } else { info.emplace_back(" ExtensionRecords:"); @@ -2297,6 +2350,13 @@ void AbilityConnectManager::DumpState(std::vector &info, bool isCli service.second->DumpService(info, isClient); } } + for (auto &&service : cacheList) { + std::string serviceKey = GetServiceKey(service); + info.emplace_back(" uri [" + serviceKey + "]"); + if (service != nullptr) { + service->DumpService(info, isClient); + } + } } } @@ -2314,11 +2374,19 @@ void AbilityConnectManager::DumpStateByUri(std::vector &info, bool info.emplace_back("uri [ " + it->first + " ]"); extensionAbilityRecord = it->second; } else { - info.emplace_back(args + ": Nothing to dump."); + info.emplace_back(args + ": Nothing to dump from serviceMap."); } } if (extensionAbilityRecord != nullptr) { extensionAbilityRecord->DumpService(info, params, isClient); + return; + } + extensionAbilityRecord = AbilityCacheManager::GetInstance().FindRecordByServiceKey(args); + if (extensionAbilityRecord != nullptr) { + info.emplace_back("uri [ " + args + " ]"); + extensionAbilityRecord->DumpService(info, params, isClient); + } else { + info.emplace_back(args + ": Nothing to dump from lru cache."); } } @@ -2345,6 +2413,25 @@ void AbilityConnectManager::GetExtensionRunningInfos(int upperLimit, std::vector } }; std::for_each(serviceMapBack.begin(), serviceMapBack.end(), queryInfo); + + auto cacheAbilityList = AbilityCacheManager::GetInstance().GetAbilityList(); + auto queryInfoForCache = [&](std::shared_ptr &service) { + if (static_cast(info.size()) >= upperLimit) { + return; + } + CHECK_POINTER(service); + + if (isPerm) { + GetExtensionRunningInfo(service, userId, info); + } else { + auto callingTokenId = IPCSkeleton::GetCallingTokenID(); + auto tokenID = service->GetApplicationInfo().accessTokenId; + if (callingTokenId == tokenID) { + GetExtensionRunningInfo(service, userId, info); + } + } + }; + std::for_each(cacheAbilityList.begin(), cacheAbilityList.end(), queryInfoForCache); } void AbilityConnectManager::GetAbilityRunningInfos(std::vector &info, bool isPerm) @@ -2429,15 +2516,18 @@ void AbilityConnectManager::PauseExtensions() void AbilityConnectManager::RemoveLauncherDeathRecipient() { TAG_LOGI(AAFwkTag::ABILITYMGR, "Call."); - std::lock_guard lock(serviceMapMutex_); - for (auto it = serviceMap_.begin(); it != serviceMap_.end(); ++it) { - auto targetExtension = it->second; - if (targetExtension != nullptr && targetExtension->GetAbilityInfo().type == AbilityType::EXTENSION && - (IsLauncher(targetExtension) || targetExtension->IsSceneBoard())) { - targetExtension->RemoveAbilityDeathRecipient(); - break; + { + std::lock_guard lock(serviceMapMutex_); + for (auto it = serviceMap_.begin(); it != serviceMap_.end(); ++it) { + auto targetExtension = it->second; + if (targetExtension != nullptr && targetExtension->GetAbilityInfo().type == AbilityType::EXTENSION && + (IsLauncher(targetExtension) || targetExtension->IsSceneBoard())) { + targetExtension->RemoveAbilityDeathRecipient(); + return; + } } } + AbilityCacheManager::GetInstance().RemoveLauncherDeathRecipient(); } bool AbilityConnectManager::IsLauncher(std::shared_ptr serviceExtension) const @@ -2938,13 +3028,16 @@ std::shared_ptr AbilityConnectManager::GetUIExtensionRootH void AbilityConnectManager::SignRestartAppFlag(const std::string &bundleName) { - std::lock_guard lock(serviceMapMutex_); - for (auto &[key, abilityRecord] : serviceMap_) { - if (abilityRecord == nullptr || abilityRecord->GetApplicationInfo().bundleName != bundleName) { - continue; + { + std::lock_guard lock(serviceMapMutex_); + for (auto &[key, abilityRecord] : serviceMap_) { + if (abilityRecord == nullptr || abilityRecord->GetApplicationInfo().bundleName != bundleName) { + continue; + } + abilityRecord->SetRestartAppFlag(true); } - abilityRecord->SetRestartAppFlag(true); } + AbilityCacheManager::GetInstance().SignRestartAppFlag(bundleName); } void AbilityConnectManager::DeleteInvalidServiceRecord(const std::string &bundleName) @@ -2959,6 +3052,7 @@ void AbilityConnectManager::DeleteInvalidServiceRecord(const std::string &bundle ++it; } } + AbilityCacheManager::GetInstance().DeleteInvalidServiceRecord(bundleName); } bool AbilityConnectManager::AddToServiceMap(const std::string &key, std::shared_ptr abilityRecord) diff --git a/test/unittest/ability_cache_manager_test/ability_cache_manager_test.cpp b/test/unittest/ability_cache_manager_test/ability_cache_manager_test.cpp index e164f9f3be..e5a5c92bbb 100644 --- a/test/unittest/ability_cache_manager_test/ability_cache_manager_test.cpp +++ b/test/unittest/ability_cache_manager_test/ability_cache_manager_test.cpp @@ -500,5 +500,169 @@ HWTEST_F(AbilityCacheManagerTest, AbilityCacheManagerFindByToken_001, TestSize.L EXPECT_EQ(recGet->GetRecordId(), recId); } +/** + * @tc.name: AbilityCacheManagerGetAbilityList_001 + * @tc.desc: Put a single ability record into cache and get ability list + * @tc.type: FUNC + * @tc.require: + */ +HWTEST_F(AbilityCacheManagerTest, AbilityCacheManagerGetAbilityList_001, TestSize.Level0) +{ + OHOS::AAFwk::AbilityCacheManager::GetInstance().Init(10, 5); + OHOS::AppExecFwk::AbilityInfo abilityInfo; + abilityInfo.moduleName = "TestModuleName"; + abilityInfo.bundleName = "TestBundleName"; + OHOS::AppExecFwk::ApplicationInfo applicationInfo; + applicationInfo.accessTokenId = 0; + Want want; + auto abilityRecord_ = std::make_shared(want, abilityInfo, applicationInfo); + abilityRecord_->Init(); + int recId = abilityRecord_->GetRecordId(); + std::shared_ptr rec = OHOS::AAFwk::AbilityCacheManager::GetInstance().Put(abilityRecord_); + EXPECT_EQ(rec, nullptr); + auto abilityList = OHOS::AAFwk::AbilityCacheManager::GetInstance().GetAbilityList(); + EXPECT_EQ(abilityList.size(), 1); + auto recordFind = *(abilityList.begin()); + EXPECT_EQ(recordFind->GetApplicationInfo().accessTokenId, applicationInfo.accessTokenId); + EXPECT_EQ(recordFind->GetAbilityInfo().moduleName, abilityInfo.moduleName); + EXPECT_EQ(recordFind->GetAbilityInfo().bundleName, abilityInfo.bundleName); + EXPECT_EQ(recordFind->GetRecordId(), recId); + OHOS::AAFwk::AbilityCacheManager::GetInstance().Remove(abilityRecord_); +} + +/** + * @tc.name: AbilityCacheManagerFindBySessionId_001 + * @tc.desc: Put a single ability record into cache and find it, find will not remove cache + * @tc.type: FUNC + * @tc.require: + */ +HWTEST_F(AbilityCacheManagerTest, AbilityCacheManagerFindBySessionId_001, TestSize.Level0) +{ + OHOS::AAFwk::AbilityCacheManager::GetInstance().Init(10, 5); + OHOS::AppExecFwk::AbilityInfo abilityInfo; + std::string sessionId = "TestSessionId"; + abilityInfo.moduleName = "TestModuleName"; + abilityInfo.bundleName = "TestBundleName"; + OHOS::AppExecFwk::ApplicationInfo applicationInfo; + applicationInfo.accessTokenId = 0; + Want want; + want.SetParam(Want::PARAM_ASSERT_FAULT_SESSION_ID, sessionId); + auto abilityRecord_ = std::make_shared(want, abilityInfo, applicationInfo); + abilityRecord_->Init(); + int recId = abilityRecord_->GetRecordId(); + std::shared_ptr rec = OHOS::AAFwk::AbilityCacheManager::GetInstance().Put(abilityRecord_); + EXPECT_EQ(rec, nullptr); + auto recordFind = OHOS::AAFwk::AbilityCacheManager::GetInstance().FindRecordBySessionId(sessionId); + EXPECT_EQ(recordFind->GetApplicationInfo().accessTokenId, applicationInfo.accessTokenId); + EXPECT_EQ(recordFind->GetAbilityInfo().moduleName, abilityInfo.moduleName); + EXPECT_EQ(recordFind->GetAbilityInfo().bundleName, abilityInfo.bundleName); + EXPECT_EQ(recordFind->GetRecordId(), recId); + AbilityRequest abilityRequest; + abilityRequest.abilityInfo = abilityInfo; + abilityRequest.appInfo = applicationInfo; + auto recGet = OHOS::AAFwk::AbilityCacheManager::GetInstance().Get(abilityRequest); + EXPECT_EQ(recGet->GetApplicationInfo().accessTokenId, applicationInfo.accessTokenId); + EXPECT_EQ(recGet->GetAbilityInfo().moduleName, abilityInfo.moduleName); + EXPECT_EQ(recGet->GetAbilityInfo().bundleName, abilityInfo.bundleName); + EXPECT_EQ(recGet->GetRecordId(), recId); +} + +/** + * @tc.name: AbilityCacheManagerFindByServiceKey_001 + * @tc.desc: Put a single ability record into cache and find it, find will not remove cache + * @tc.type: FUNC + * @tc.require: + */ +HWTEST_F(AbilityCacheManagerTest, AbilityCacheManagerFindByServiceKey_001, TestSize.Level0) +{ + OHOS::AAFwk::AbilityCacheManager::GetInstance().Init(10, 5); + OHOS::AppExecFwk::AbilityInfo abilityInfo; + abilityInfo.moduleName = "TestModuleName"; + abilityInfo.bundleName = "TestBundleName"; + OHOS::AppExecFwk::ApplicationInfo applicationInfo; + applicationInfo.accessTokenId = 0; + Want want; + auto abilityRecord_ = std::make_shared(want, abilityInfo, applicationInfo); + abilityRecord_->Init(); + int recId = abilityRecord_->GetRecordId(); + std::string serviceKey = abilityRecord_->GetURI(); + std::shared_ptr rec = OHOS::AAFwk::AbilityCacheManager::GetInstance().Put(abilityRecord_); + EXPECT_EQ(rec, nullptr); + auto recordFind = OHOS::AAFwk::AbilityCacheManager::GetInstance().FindRecordByServiceKey(serviceKey); + EXPECT_EQ(recordFind->GetApplicationInfo().accessTokenId, applicationInfo.accessTokenId); + EXPECT_EQ(recordFind->GetAbilityInfo().moduleName, abilityInfo.moduleName); + EXPECT_EQ(recordFind->GetAbilityInfo().bundleName, abilityInfo.bundleName); + EXPECT_EQ(recordFind->GetRecordId(), recId); + AbilityRequest abilityRequest; + abilityRequest.abilityInfo = abilityInfo; + abilityRequest.appInfo = applicationInfo; + auto recGet = OHOS::AAFwk::AbilityCacheManager::GetInstance().Get(abilityRequest); + EXPECT_EQ(recGet->GetApplicationInfo().accessTokenId, applicationInfo.accessTokenId); + EXPECT_EQ(recGet->GetAbilityInfo().moduleName, abilityInfo.moduleName); + EXPECT_EQ(recGet->GetAbilityInfo().bundleName, abilityInfo.bundleName); + EXPECT_EQ(recGet->GetRecordId(), recId); +} + +/** + * @tc.name: AbilityCacheManagerSignRestartAppFlag_001 + * @tc.desc: Put a single ability record into cache and sign restart app flag + * @tc.type: FUNC + * @tc.require: + */ +HWTEST_F(AbilityCacheManagerTest, AbilityCacheManagerSignRestartAppFlag_001, TestSize.Level0) +{ + OHOS::AAFwk::AbilityCacheManager::GetInstance().Init(10, 5); + OHOS::AppExecFwk::AbilityInfo abilityInfo; + abilityInfo.moduleName = "TestModuleName"; + abilityInfo.bundleName = "TestBundleName"; + OHOS::AppExecFwk::ApplicationInfo applicationInfo; + applicationInfo.accessTokenId = 0; + applicationInfo.bundleName = abilityInfo.bundleName; + Want want; + auto abilityRecord_ = std::make_shared(want, abilityInfo, applicationInfo); + abilityRecord_->Init(); + int recId = abilityRecord_->GetRecordId(); + std::shared_ptr rec = OHOS::AAFwk::AbilityCacheManager::GetInstance().Put(abilityRecord_); + EXPECT_EQ(rec, nullptr); + OHOS::AAFwk::AbilityCacheManager::GetInstance().SignRestartAppFlag(applicationInfo.bundleName); + AbilityRequest abilityRequest; + abilityRequest.abilityInfo = abilityInfo; + abilityRequest.appInfo = applicationInfo; + auto recordFind = OHOS::AAFwk::AbilityCacheManager::GetInstance().Get(abilityRequest); + EXPECT_EQ(recordFind->GetRestartAppFlag(), true); +} + +/** + * @tc.name: AbilityCacheManagerDeleteInvalidRecord_001 + * @tc.desc: Put a single ability record into cache and delete it by bundleName + * @tc.type: FUNC + * @tc.require: + */ +HWTEST_F(AbilityCacheManagerTest, AbilityCacheManagerDeleteInvalidRecord_001, TestSize.Level0) +{ + OHOS::AAFwk::AbilityCacheManager::GetInstance().Init(10, 5); + OHOS::AppExecFwk::AbilityInfo abilityInfo; + abilityInfo.moduleName = "TestModuleName"; + abilityInfo.bundleName = "TestBundleName"; + OHOS::AppExecFwk::ApplicationInfo applicationInfo; + applicationInfo.accessTokenId = 0; + Want want; + auto abilityRecord_ = std::make_shared(want, abilityInfo, applicationInfo); + abilityRecord_->Init(); + int recId = abilityRecord_->GetRecordId(); + std::shared_ptr rec = OHOS::AAFwk::AbilityCacheManager::GetInstance().Put(abilityRecord_); + EXPECT_EQ(rec, nullptr); + AbilityRequest abilityRequest; + abilityRequest.abilityInfo = abilityInfo; + abilityRequest.appInfo = applicationInfo; + rec = OHOS::AAFwk::AbilityCacheManager::GetInstance().Get(abilityRequest); + EXPECT_EQ(rec->GetApplicationInfo().accessTokenId, applicationInfo.accessTokenId); + EXPECT_EQ(rec->GetAbilityInfo().moduleName, abilityInfo.moduleName); + EXPECT_EQ(rec->GetAbilityInfo().bundleName, abilityInfo.bundleName); + EXPECT_EQ(rec->GetRecordId(), recId); + OHOS::AAFwk::AbilityCacheManager::GetInstance().DeleteInvalidServiceRecord(abilityInfo.bundleName); + rec = OHOS::AAFwk::AbilityCacheManager::GetInstance().Get(abilityRequest); + EXPECT_EQ(rec, nullptr); +} } // namespace AAFwk } // namespace OHOS