From f4cc85a4a63bcd6f27d1366364e326132097bc3b Mon Sep 17 00:00:00 2001 From: xinking129 Date: Thu, 14 Dec 2023 10:10:28 +0800 Subject: [PATCH 1/6] modify code Signed-off-by: xinking129 --- frameworks/native/appkit/app/context_deal.cpp | 12 ++-- services/abilitymgr/include/ability_util.h | 9 +-- .../include/implicit_start_processor.h | 1 - .../src/ability_connect_manager.cpp | 6 +- .../abilitymgr/src/ability_interceptor.cpp | 10 +-- .../src/ability_manager_service.cpp | 2 +- services/abilitymgr/src/ability_record.cpp | 2 +- .../abilitymgr/src/data_ability_manager.cpp | 2 +- .../abilitymgr/src/dialog_session_record.cpp | 2 +- .../abilitymgr/src/free_install_manager.cpp | 6 +- .../src/implicit_start_processor.cpp | 16 ++--- .../abilitymgr/src/mission_list_manager.cpp | 2 +- .../src/system_dialog_scheduler.cpp | 4 +- .../appmgr/include/app_mgr_service_inner.h | 3 - services/appmgr/src/app_mgr_service.cpp | 4 +- services/appmgr/src/app_mgr_service_inner.cpp | 71 +++++-------------- .../appmgr/src/app_state_observer_manager.cpp | 2 +- services/appmgr/src/remote_client_manager.cpp | 13 ---- .../appmgrrest_fuzzer/appmgrrest_fuzzer.cpp | 3 - .../common/ams/app_recent_list_test/BUILD.gn | 5 +- .../ams_app_recent_list_module_test.cpp | 51 ++++++++++--- ...app_running_processes_info_module_test.cpp | 62 ++++++++++++---- .../common/ams/app_service_flow_test/BUILD.gn | 1 + .../ams_app_service_flow_module_test.cpp | 53 +++++++++++--- .../ability_interceptor_test.cpp | 2 +- .../ams_mgr_kill_process_test.cpp | 2 - .../ams_mgr_scheduler_test.cpp | 2 - .../app_mgr_service_inner_test.cpp | 55 +------------- .../app_running_processes_info_test.cpp | 3 - 29 files changed, 189 insertions(+), 217 deletions(-) diff --git a/frameworks/native/appkit/app/context_deal.cpp b/frameworks/native/appkit/app/context_deal.cpp index 2159ed299d8..d11c9cb554a 100644 --- a/frameworks/native/appkit/app/context_deal.cpp +++ b/frameworks/native/appkit/app/context_deal.cpp @@ -219,12 +219,12 @@ sptr ContextDeal::GetAbilityManager() std::string ContextDeal::GetAppType() { - auto ptr = GetBundleManager(); - if (ptr == nullptr) { + auto bundleMgrHelper = GetBundleManager(); + if (bundleMgrHelper == nullptr) { HILOG_ERROR("GetAppType failed to get bundle manager service"); return ""; } - std::string retString = ptr->GetAppType(applicationInfo_->bundleName); + std::string retString = bundleMgrHelper->GetAppType(applicationInfo_->bundleName); return retString; } @@ -453,8 +453,8 @@ int ContextDeal::GetColorMode() bool ContextDeal::HapModuleInfoRequestInit() { - auto ptr = GetBundleManager(); - if (ptr == nullptr) { + auto bundleMgrHelper = GetBundleManager(); + if (bundleMgrHelper == nullptr) { HILOG_ERROR("Failed to get bundle manager service."); return false; } @@ -465,7 +465,7 @@ bool ContextDeal::HapModuleInfoRequestInit() } hapModuleInfoLocal_ = std::make_shared(); - if (!ptr->GetHapModuleInfo(*abilityInfo_.get(), *hapModuleInfoLocal_)) { + if (!bundleMgrHelper->GetHapModuleInfo(*abilityInfo_.get(), *hapModuleInfoLocal_)) { HILOG_ERROR("Failed, will retval false value."); return false; } diff --git a/services/abilitymgr/include/ability_util.h b/services/abilitymgr/include/ability_util.h index f2abedab305..9182503743b 100644 --- a/services/abilitymgr/include/ability_util.h +++ b/services/abilitymgr/include/ability_util.h @@ -165,11 +165,6 @@ static constexpr int64_t MICROSECONDS = 1000000; // MICROSECONDS mean 10^6 mi return false; } -[[maybe_unused]] static std::shared_ptr GetBundleManagerHelper() -{ - return DelayedSingleton::GetInstance(); -} - #ifndef SUPPORT_ERMS [[maybe_unused]] static sptr CheckEcologicalRuleMgr() { @@ -215,7 +210,7 @@ static constexpr int64_t MICROSECONDS = 1000000; // MICROSECONDS mean 10^6 mi HILOG_ERROR("get invalid inputs"); return false; } - auto bundleMgrHelper = AbilityUtil::GetBundleManagerHelper(); + auto bundleMgrHelper = DelayedSingleton::GetInstance(); if (bundleMgrHelper == nullptr) { HILOG_ERROR("Get bundle manager helper failed."); return false; @@ -252,7 +247,7 @@ static constexpr int64_t MICROSECONDS = 1000000; // MICROSECONDS mean 10^6 mi [[maybe_unused]] static bool IsStartIncludeAtomicService(const Want &want, const int32_t userId) { - auto bundleMgrHelper = AbilityUtil::GetBundleManagerHelper(); + auto bundleMgrHelper = DelayedSingleton::GetInstance(); if (bundleMgrHelper == nullptr) { HILOG_ERROR("Get bundle manager helper failed."); return false; diff --git a/services/abilitymgr/include/implicit_start_processor.h b/services/abilitymgr/include/implicit_start_processor.h index 18915e876cc..49f1358ca9a 100644 --- a/services/abilitymgr/include/implicit_start_processor.h +++ b/services/abilitymgr/include/implicit_start_processor.h @@ -72,7 +72,6 @@ private: int GenerateAbilityRequestByAction(int32_t userId, AbilityRequest &request, std::vector &dialogAppInfos, std::string &deviceType, bool isMoreHapList); std::string MatchTypeAndUri(const AAFwk::Want &want); - std::shared_ptr GetBundleManagerHelper(); std::vector splitStr(const std::string& str, char delimiter); int queryBmsAppInfos(AbilityRequest &request, int32_t userId, std::vector &dialogAppInfos); diff --git a/services/abilitymgr/src/ability_connect_manager.cpp b/services/abilitymgr/src/ability_connect_manager.cpp index 74b825ee922..f8524d752de 100644 --- a/services/abilitymgr/src/ability_connect_manager.cpp +++ b/services/abilitymgr/src/ability_connect_manager.cpp @@ -716,7 +716,7 @@ int AbilityConnectManager::AbilityTransitionDone(const sptr &toke void AbilityConnectManager::ProcessPreload(const std::shared_ptr &record) const { - auto bundleMgrHelper = AbilityUtil::GetBundleManagerHelper(); + auto bundleMgrHelper = DelayedSingleton::GetInstance(); CHECK_POINTER(bundleMgrHelper); auto abilityInfo = record->GetAbilityInfo(); Want want; @@ -1669,7 +1669,7 @@ bool AbilityConnectManager::IsAbilityNeedKeepAlive(const std::shared_ptrGetAbilityInfo().name == iter->second) { return true; } - auto bundleMgrHelper = AbilityUtil::GetBundleManagerHelper(); + auto bundleMgrHelper = DelayedSingleton::GetInstance(); CHECK_POINTER_AND_RETURN(bundleMgrHelper, false); std::vector bundleInfos; bool getBundleInfos = bundleMgrHelper->GetBundleInfos( @@ -1955,7 +1955,7 @@ void AbilityConnectManager::GetExtensionRunningInfo(std::shared_ptrGetElementName(); - auto bundleMgrHelper = AbilityUtil::GetBundleManagerHelper(); + auto bundleMgrHelper = DelayedSingleton::GetInstance(); CHECK_POINTER(bundleMgrHelper); std::vector extensionInfos; diff --git a/services/abilitymgr/src/ability_interceptor.cpp b/services/abilitymgr/src/ability_interceptor.cpp index 3b35bc8b75a..60ff561fab5 100644 --- a/services/abilitymgr/src/ability_interceptor.cpp +++ b/services/abilitymgr/src/ability_interceptor.cpp @@ -90,7 +90,7 @@ ErrCode CrowdTestInterceptor::DoProcess(const Want &want, int requestCode, int32 bool CrowdTestInterceptor::CheckCrowdtest(const Want &want, int32_t userId) { // get bms - auto bundleMgrHelper = AbilityUtil::GetBundleManagerHelper(); + auto bundleMgrHelper = DelayedSingleton::GetInstance(); if (bundleMgrHelper == nullptr) { HILOG_ERROR("The bundleMgrHelper is nullptr."); return false; @@ -164,7 +164,7 @@ bool ControlInterceptor::CheckControl(const Want &want, int32_t userId, AppExecFwk::AppRunningControlRuleResult &controlRule) { // get bms - auto bundleMgrHelper = AbilityUtil::GetBundleManagerHelper(); + auto bundleMgrHelper = DelayedSingleton::GetInstance(); if (bundleMgrHelper == nullptr) { HILOG_ERROR("The bundleMgrHelper is nullptr."); return false; @@ -257,7 +257,7 @@ bool DisposedRuleInterceptor::CheckControl(const Want &want, int32_t userId, AppExecFwk::DisposedRule &disposedRule) { // get bms - auto bundleMgrHelper = AbilityUtil::GetBundleManagerHelper(); + auto bundleMgrHelper = DelayedSingleton::GetInstance(); if (bundleMgrHelper == nullptr) { HILOG_ERROR("The bundleMgrHelper is nullptr."); return false; @@ -368,7 +368,7 @@ void EcologicalRuleInterceptor::GetEcologicalCallerInfo(const Want &want, ErmsCa callerInfo.targetAppType = TYPE_HARMONY_INVALID; callerInfo.callerAppType = TYPE_HARMONY_INVALID; - auto bundleMgrHelper = AbilityUtil::GetBundleManagerHelper(); + auto bundleMgrHelper = DelayedSingleton::GetInstance(); if (bundleMgrHelper == nullptr) { HILOG_ERROR("The bundleMgrHelper is nullptr."); return; @@ -443,7 +443,7 @@ ErrCode AbilityJumpInterceptor::DoProcess(const Want &want, int requestCode, int return ERR_OK; } // get bms - auto bundleMgrHelper = AbilityUtil::GetBundleManagerHelper(); + auto bundleMgrHelper = DelayedSingleton::GetInstance(); if (bundleMgrHelper == nullptr) { HILOG_ERROR("The bundleMgrHelper is nullptr."); return ERR_OK; diff --git a/services/abilitymgr/src/ability_manager_service.cpp b/services/abilitymgr/src/ability_manager_service.cpp index c3b578a0163..1ca9700b5ba 100644 --- a/services/abilitymgr/src/ability_manager_service.cpp +++ b/services/abilitymgr/src/ability_manager_service.cpp @@ -5436,7 +5436,7 @@ int AbilityManagerService::UninstallApp(const std::string &bundleName, int32_t u std::shared_ptr AbilityManagerService::GetBundleManager() { if (bundleMgrHelper_ == nullptr) { - bundleMgrHelper_ = AbilityUtil::GetBundleManagerHelper(); + bundleMgrHelper_ = DelayedSingleton::GetInstance(); } return bundleMgrHelper_; } diff --git a/services/abilitymgr/src/ability_record.cpp b/services/abilitymgr/src/ability_record.cpp index 9a0d32f4726..9341d8ea403 100644 --- a/services/abilitymgr/src/ability_record.cpp +++ b/services/abilitymgr/src/ability_record.cpp @@ -2781,7 +2781,7 @@ void AbilityRecord::GrantUriPermission(Want &want, std::string targetBundleName, HILOG_WARN("Do not call uriPermissionMgr."); return; } - auto bundleMgrHelper = AbilityUtil::GetBundleManagerHelper(); + auto bundleMgrHelper = DelayedSingleton::GetInstance(); CHECK_POINTER_IS_NULLPTR(bundleMgrHelper); if (IsDmsCall(want)) { GrantDmsUriPermission(want, targetBundleName); diff --git a/services/abilitymgr/src/data_ability_manager.cpp b/services/abilitymgr/src/data_ability_manager.cpp index f214b96f13c..666bc927921 100644 --- a/services/abilitymgr/src/data_ability_manager.cpp +++ b/services/abilitymgr/src/data_ability_manager.cpp @@ -630,7 +630,7 @@ void DataAbilityManager::GetAbilityRunningInfos(std::vector void DataAbilityManager::RestartDataAbility(const std::shared_ptr &abilityRecord) { // restart data ability if necessary - auto bundleMgrHelper = AbilityUtil::GetBundleManagerHelper(); + auto bundleMgrHelper = DelayedSingleton::GetInstance(); CHECK_POINTER(bundleMgrHelper); std::vector bundleInfos; bool getBundleInfos = bundleMgrHelper->GetBundleInfos( diff --git a/services/abilitymgr/src/dialog_session_record.cpp b/services/abilitymgr/src/dialog_session_record.cpp index ceada41556a..d293638f842 100644 --- a/services/abilitymgr/src/dialog_session_record.cpp +++ b/services/abilitymgr/src/dialog_session_record.cpp @@ -97,7 +97,7 @@ void DialogSessionRecord::ClearAllDialogContexts() bool DialogSessionRecord::QueryDialogAppInfo(DialogAbilityInfo &dialogAbilityInfo, int32_t userId) { std::string bundleName = dialogAbilityInfo.bundleName; - auto bundleMgrHelper = AbilityUtil::GetBundleManagerHelper(); + auto bundleMgrHelper = DelayedSingleton::GetInstance(); CHECK_POINTER_AND_RETURN(bundleMgrHelper, ERR_INVALID_VALUE); AppExecFwk::ApplicationInfo appInfo; bool ret = IN_PROCESS_CALL(bundleMgrHelper->GetApplicationInfo(bundleName, diff --git a/services/abilitymgr/src/free_install_manager.cpp b/services/abilitymgr/src/free_install_manager.cpp index 9a598c4a55a..67c4af46c8b 100644 --- a/services/abilitymgr/src/free_install_manager.cpp +++ b/services/abilitymgr/src/free_install_manager.cpp @@ -96,7 +96,7 @@ int FreeInstallManager::StartFreeInstall(const Want &want, int32_t userId, int r freeInstallList_.push_back(info); } sptr callback = new AtomicServiceStatusCallback(weak_from_this(), isAsync); - auto bundleMgrHelper = AbilityUtil::GetBundleManagerHelper(); + auto bundleMgrHelper = DelayedSingleton::GetInstance(); CHECK_POINTER_AND_RETURN(bundleMgrHelper, GET_ABILITY_SERVICE_FAILED); AppExecFwk::AbilityInfo abilityInfo = {}; constexpr auto flag = AppExecFwk::AbilityInfoFlag::GET_ABILITY_INFO_WITH_APPLICATION; @@ -335,7 +335,7 @@ int FreeInstallManager::FreeInstallAbilityFromRemote(const Want &want, const spt int FreeInstallManager::ConnectFreeInstall(const Want &want, int32_t userId, const sptr &callerToken, const std::string& localDeviceId) { - auto bundleMgrHelper = AbilityUtil::GetBundleManagerHelper(); + auto bundleMgrHelper = DelayedSingleton::GetInstance(); CHECK_POINTER_AND_RETURN(bundleMgrHelper, GET_ABILITY_SERVICE_FAILED); std::string wantDeviceId = want.GetElement().GetDeviceID(); if (!(localDeviceId == wantDeviceId || wantDeviceId.empty())) { @@ -414,7 +414,7 @@ void FreeInstallManager::PostUpgradeAtomicServiceTask(int resultCode, const Want std::string nameKey = want.GetElement().GetBundleName() + want.GetElement().GetModuleName(); if (timeStampMap.find(nameKey) == timeStampMap.end() || sptr->GetTimeStamp() - timeStampMap[nameKey] > UPDATE_ATOMOIC_SERVICE_TASK_TIMER) { - auto bundleMgrHelper = AbilityUtil::GetBundleManagerHelper(); + auto bundleMgrHelper = DelayedSingleton::GetInstance(); CHECK_POINTER(bundleMgrHelper); bundleMgrHelper->UpgradeAtomicService(want, userId); timeStampMap.emplace(nameKey, sptr->GetTimeStamp()); diff --git a/services/abilitymgr/src/implicit_start_processor.cpp b/services/abilitymgr/src/implicit_start_processor.cpp index 605006a0968..3f7ec5d59d0 100644 --- a/services/abilitymgr/src/implicit_start_processor.cpp +++ b/services/abilitymgr/src/implicit_start_processor.cpp @@ -237,7 +237,7 @@ int ImplicitStartProcessor::GenerateAbilityRequestByAction(int32_t userId, { HILOG_DEBUG("%{public}s.", __func__); // get abilityinfos from bms - auto bundleMgrHelper = GetBundleManagerHelper(); + auto bundleMgrHelper = DelayedSingleton::GetInstance(); CHECK_POINTER_AND_RETURN(bundleMgrHelper, GET_ABILITY_SERVICE_FAILED); auto abilityInfoFlag = AppExecFwk::AbilityInfoFlag::GET_ABILITY_INFO_DEFAULT | AppExecFwk::AbilityInfoFlag::GET_ABILITY_INFO_WITH_SKILL_URI; @@ -320,7 +320,7 @@ int ImplicitStartProcessor::GenerateAbilityRequestByAction(int32_t userId, } int ImplicitStartProcessor::queryBmsAppInfos(AbilityRequest &request, int32_t userId, std::vector &dialogAppInfos) { - auto bundleMgrHelper = GetBundleManagerHelper(); + auto bundleMgrHelper = DelayedSingleton::GetInstance(); std::vector bmsApps; auto abilityInfoFlag = AppExecFwk::AbilityInfoFlag::GET_ABILITY_INFO_DEFAULT | AppExecFwk::AbilityInfoFlag::GET_ABILITY_INFO_WITH_SKILL_URI; @@ -440,17 +440,9 @@ int ImplicitStartProcessor::CallStartAbilityInner(int32_t userId, return ret; } -std::shared_ptr ImplicitStartProcessor::GetBundleManagerHelper() -{ - if (iBundleManagerHelper_ == nullptr) { - iBundleManagerHelper_ = AbilityUtil::GetBundleManagerHelper(); - } - return iBundleManagerHelper_; -} - sptr ImplicitStartProcessor::GetDefaultAppProxy() { - auto bundleMgrHelper = GetBundleManagerHelper(); + auto bundleMgrHelper = DelayedSingleton::GetInstance(); if (bundleMgrHelper == nullptr) { HILOG_ERROR("The bundleMgrHelper is nullptr."); return nullptr; @@ -497,7 +489,7 @@ void ImplicitStartProcessor::GetEcologicalCallerInfo(const Want &want, ErmsCalle callerInfo.targetAppType = TYPE_HARMONY_INVALID; callerInfo.callerAppType = TYPE_HARMONY_INVALID; - auto bundleMgrHelper = GetBundleManagerHelper(); + auto bundleMgrHelper = DelayedSingleton::GetInstance(); if (bundleMgrHelper == nullptr) { HILOG_ERROR("Get Bubndle manager helper failed."); return; diff --git a/services/abilitymgr/src/mission_list_manager.cpp b/services/abilitymgr/src/mission_list_manager.cpp index 53848a1b18a..2d12addc525 100644 --- a/services/abilitymgr/src/mission_list_manager.cpp +++ b/services/abilitymgr/src/mission_list_manager.cpp @@ -2894,7 +2894,7 @@ void MissionListManager::CompleteFirstFrameDrawing(const sptr &ab void MissionListManager::ProcessPreload(const std::shared_ptr &record) const { - auto bundleMgrHelper = AbilityUtil::GetBundleManagerHelper(); + auto bundleMgrHelper = DelayedSingleton::GetInstance(); CHECK_POINTER(bundleMgrHelper); auto abilityInfo = record->GetAbilityInfo(); Want want; diff --git a/services/abilitymgr/src/system_dialog_scheduler.cpp b/services/abilitymgr/src/system_dialog_scheduler.cpp index 0573160d33c..b90cce519ce 100644 --- a/services/abilitymgr/src/system_dialog_scheduler.cpp +++ b/services/abilitymgr/src/system_dialog_scheduler.cpp @@ -374,7 +374,7 @@ Want SystemDialogScheduler::GetSelectorDialogWant(const std::vector::GetInstance(); if (bundleMgrHelper == nullptr) { HILOG_ERROR("The bundleMgrHelper is nullptr."); return targetWant; @@ -434,7 +434,7 @@ Want SystemDialogScheduler::GetPcSelectorDialogWant(const std::vector::GetInstance(); if (bundleMgrHelper == nullptr) { HILOG_ERROR("The bundleMgrHelper is nullptr."); return targetWant; diff --git a/services/appmgr/include/app_mgr_service_inner.h b/services/appmgr/include/app_mgr_service_inner.h index cf633b47eb1..5061a5e2917 100644 --- a/services/appmgr/include/app_mgr_service_inner.h +++ b/services/appmgr/include/app_mgr_service_inner.h @@ -1067,9 +1067,6 @@ private: */ int64_t SystemTimeMillisecond(); - // Test add the bundle manager instance. - void SetBundleManagerHelper(const std::shared_ptr &bundleMgrHelper); - void HandleTerminateApplicationTimeOut(const int64_t eventId); void HandleAddAbilityStageTimeOut(const int64_t eventId); diff --git a/services/appmgr/src/app_mgr_service.cpp b/services/appmgr/src/app_mgr_service.cpp index 6e2d01bf4f1..6a8041c47d7 100644 --- a/services/appmgr/src/app_mgr_service.cpp +++ b/services/appmgr/src/app_mgr_service.cpp @@ -304,7 +304,7 @@ int32_t AppMgrService::ClearUpApplicationData(const std::string &bundleName, con HILOG_ERROR("The remoteClientManager is nullptr."); return ERR_INVALID_OPERATION; } - auto bundleMgrHelper = remoteClientManager->GetBundleManagerHelper(); + auto bundleMgrHelper = DelayedSingleton::GetInstance(); if (bundleMgrHelper == nullptr) { HILOG_ERROR("The bundleMgrHelper is nullptr."); return ERR_INVALID_OPERATION; @@ -498,7 +498,7 @@ int AppMgrService::FinishUserTest(const std::string &msg, const int64_t &resultC HILOG_ERROR("The remoteClientManager is nullptr."); return ERR_INVALID_OPERATION; } - auto bundleMgrHelper = remoteClientManager->GetBundleManagerHelper(); + auto bundleMgrHelper = DelayedSingleton::GetInstance(); if (bundleMgrHelper == nullptr) { HILOG_ERROR("The bundleMgrHelper is nullptr."); return ERR_INVALID_OPERATION; diff --git a/services/appmgr/src/app_mgr_service_inner.cpp b/services/appmgr/src/app_mgr_service_inner.cpp index 614791a0f96..3c75d3e8d23 100644 --- a/services/appmgr/src/app_mgr_service_inner.cpp +++ b/services/appmgr/src/app_mgr_service_inner.cpp @@ -513,7 +513,7 @@ bool AppMgrServiceInner::GetBundleAndHapInfo(const AbilityInfo &abilityInfo, int32_t appIndex) const { HITRACE_METER_NAME(HITRACE_TAG_ABILITY_MANAGER, __PRETTY_FUNCTION__); - auto bundleMgrHelper = remoteClientManager_->GetBundleManagerHelper(); + auto bundleMgrHelper = DelayedSingleton::GetInstance(); if (bundleMgrHelper == nullptr) { HILOG_ERROR("The bundleMgrHelper is nullptr."); return false; @@ -797,12 +797,7 @@ int32_t AppMgrServiceInner::UpdateApplicationInfoInstalled(const std::string &bu return result; } - if (remoteClientManager_ == nullptr) { - HILOG_ERROR("The remoteClientManager_ fail."); - return ERR_NO_INIT; - } - - auto bundleMgrHelper = remoteClientManager_->GetBundleManagerHelper(); + auto bundleMgrHelper = DelayedSingleton::GetInstance(); if (bundleMgrHelper == nullptr) { HILOG_ERROR("The bundleMgrHelper is nullptr."); return ERR_NO_INIT; @@ -864,11 +859,7 @@ int32_t AppMgrServiceInner::KillApplicationByUid(const std::string &bundleName, int64_t startTime = SystemTimeMillisecond(); std::list pids; - if (remoteClientManager_ == nullptr) { - HILOG_ERROR("The remoteClientManager_ is nullptr."); - return ERR_NO_INIT; - } - auto bundleMgrHelper = remoteClientManager_->GetBundleManagerHelper(); + auto bundleMgrHelper = DelayedSingleton::GetInstance(); if (bundleMgrHelper == nullptr) { HILOG_ERROR("The bundleMgrHelper is nullptr."); return ERR_NO_INIT; @@ -992,12 +983,7 @@ int32_t AppMgrServiceInner::KillApplicationByUserId(const std::string &bundleNam HILOG_ERROR("%{public}s: Permission verification failed", __func__); return ERR_PERMISSION_DENIED; } - - if (remoteClientManager_ == nullptr) { - HILOG_ERROR("remoteClientManager_ fail"); - return ERR_NO_INIT; - } - auto bundleMgrHelper = remoteClientManager_->GetBundleManagerHelper(); + auto bundleMgrHelper = DelayedSingleton::GetInstance(); if (bundleMgrHelper == nullptr) { HILOG_ERROR("The bundleMgrHelper is nullptr."); return ERR_NO_INIT; @@ -1016,11 +1002,7 @@ int32_t AppMgrServiceInner::KillApplicationByUserIdLocked(const std::string &bun int result = ERR_OK; int64_t startTime = SystemTimeMillisecond(); std::list pids; - if (remoteClientManager_ == nullptr) { - HILOG_ERROR("The remoteClientManager_ is nullptr."); - return ERR_NO_INIT; - } - auto bundleMgrHelper = remoteClientManager_->GetBundleManagerHelper(); + auto bundleMgrHelper = DelayedSingleton::GetInstance(); if (bundleMgrHelper == nullptr) { HILOG_ERROR("The bundleMgrHelper is nullptr."); return ERR_NO_INIT; @@ -1070,7 +1052,7 @@ void AppMgrServiceInner::ClearUpApplicationDataByUserId( HILOG_ERROR("invalid callerUid:%{public}d", callerUid); return; } - auto bundleMgrHelper = remoteClientManager_->GetBundleManagerHelper(); + auto bundleMgrHelper = DelayedSingleton::GetInstance(); if (bundleMgrHelper == nullptr) { HILOG_ERROR("The bundleMgrHelper is nullptr."); return; @@ -1581,16 +1563,6 @@ void AppMgrServiceInner::SetAppSpawnClient(std::shared_ptr spawn remoteClientManager_->SetSpawnClient(std::move(spawnClient)); } -void AppMgrServiceInner::SetBundleManagerHelper(const std::shared_ptr &bundleMgrHelper) -{ - if (remoteClientManager_ == nullptr) { - HILOG_ERROR("The remoteClientManager_ is nullptr."); - return; - } - - remoteClientManager_->SetBundleManagerHelper(bundleMgrHelper); -} - void AppMgrServiceInner::RegisterAppStateCallback(const sptr &callback) { pid_t callingPid = IPCSkeleton::GetCallingPid(); @@ -1957,11 +1929,7 @@ void AppMgrServiceInner::SetOverlayInfo(const std::string &bundleName, const int32_t userId, AppSpawnStartMsg &startMsg) { - if (remoteClientManager_ == nullptr) { - HILOG_ERROR("The remoteClientManager_ is nullptr."); - return; - } - auto bundleMgrHelper = remoteClientManager_->GetBundleManagerHelper(); + auto bundleMgrHelper = DelayedSingleton::GetInstance(); if (bundleMgrHelper == nullptr) { HILOG_ERROR("The bundleMgrHelper is nullptr."); return; @@ -2054,7 +2022,7 @@ void AppMgrServiceInner::StartProcess(const std::string &appName, const std::str return; } - auto bundleMgrHelper = remoteClientManager_->GetBundleManagerHelper(); + auto bundleMgrHelper = DelayedSingleton::GetInstance(); if (bundleMgrHelper == nullptr) { HILOG_ERROR("Get bundle manager helper fail."); appRunningManager_->RemoveAppRunningRecordById(appRecord->GetRecordId()); @@ -2761,8 +2729,9 @@ bool AppMgrServiceInner::CheckRemoteClient() return false; } - if (!remoteClientManager_->GetBundleManagerHelper()) { - HILOG_ERROR("Get bundle manager helper fail."); + auto bundleMgrHelper = DelayedSingleton::GetInstance(); + if (bundleMgrHelper == nullptr) { + HILOG_ERROR("The bundleMgrHelper is nullptr."); return false; } return true; @@ -2794,7 +2763,7 @@ void AppMgrServiceInner::RestartResidentProcess(std::shared_ptrGetBundleManagerHelper(); + auto bundleMgrHelper = DelayedSingleton::GetInstance(); BundleInfo bundleInfo; auto callerUid = IPCSkeleton::GetCallingUid(); auto userId = GetUserIdByUid(callerUid); @@ -4407,12 +4376,9 @@ void AppMgrServiceInner::TimeoutNotifyApp(int32_t pid, int32_t uid, int32_t AppMgrServiceInner::NotifyAppFaultBySA(const AppFaultDataBySA &faultData) { - if (remoteClientManager_ == nullptr) { - HILOG_ERROR("The remoteClientManager_ is nullptr."); - return ERR_NO_INIT; - } std::string callerBundleName; - if (auto bundleMgrHelper = remoteClientManager_->GetBundleManagerHelper(); bundleMgrHelper != nullptr) { + auto bundleMgrHelper = DelayedSingleton::GetInstance(); + if (bundleMgrHelper != nullptr) { int32_t callingUid = IPCSkeleton::GetCallingUid(); IN_PROCESS_CALL(bundleMgrHelper->GetNameForUid(callingUid, callerBundleName)); } @@ -4537,7 +4503,7 @@ int32_t AppMgrServiceInner::IsApplicationRunning(const std::string &bundleName, int32_t AppMgrServiceInner::StartNativeProcessForDebugger(const AAFwk::Want &want) const { - auto&& bundleMgrHelper = remoteClientManager_->GetBundleManagerHelper(); + auto&& bundleMgrHelper = DelayedSingleton::GetInstance(); if (bundleMgrHelper == nullptr) { HILOG_ERROR("Get bundle manager helper error."); return ERR_INVALID_OPERATION; @@ -4688,12 +4654,7 @@ int32_t AppMgrServiceInner::GetRunningProcessInformation( HILOG_ERROR("The appRunningManager is nullptr!"); return ERR_NO_INIT; } - - if (remoteClientManager_ == nullptr) { - HILOG_ERROR("The remoteClientManager_ is nullptr!"); - return ERR_NO_INIT; - } - auto bundleMgrHelper = remoteClientManager_->GetBundleManagerHelper(); + auto bundleMgrHelper = DelayedSingleton::GetInstance(); if (bundleMgrHelper == nullptr) { HILOG_ERROR("The bundleMgrHelper is nullptr!"); return ERR_NO_INIT; diff --git a/services/appmgr/src/app_state_observer_manager.cpp b/services/appmgr/src/app_state_observer_manager.cpp index 9f03d716bf5..8e906d60cbf 100644 --- a/services/appmgr/src/app_state_observer_manager.cpp +++ b/services/appmgr/src/app_state_observer_manager.cpp @@ -813,7 +813,7 @@ AppStateData AppStateObserverManager::WrapAppStateData(const std::shared_ptr remoteClientManager = std::make_shared(); - auto bundleMgr = remoteClientManager->GetBundleManagerHelper(); + auto bundleMgr = DelayedSingleton::GetInstance(); std::string callerBundleName; if (bundleMgr != nullptr && IN_PROCESS_CALL(bundleMgr->GetNameForUid(appRecord->GetCallerUid(), callerBundleName)) == ERR_OK) { diff --git a/services/appmgr/src/remote_client_manager.cpp b/services/appmgr/src/remote_client_manager.cpp index b2a1035b079..777ea5929d0 100644 --- a/services/appmgr/src/remote_client_manager.cpp +++ b/services/appmgr/src/remote_client_manager.cpp @@ -43,19 +43,6 @@ void RemoteClientManager::SetSpawnClient(const std::shared_ptr & appSpawnClient_ = appSpawnClient; } -std::shared_ptr RemoteClientManager::GetBundleManagerHelper() -{ - if (bundleManagerHelper_ == nullptr) { - bundleManagerHelper_ = DelayedSingleton::GetInstance(); - } - return bundleManagerHelper_; -} - -void RemoteClientManager::SetBundleManagerHelper(const std::shared_ptr &bundleMgrHelper) -{ - bundleManagerHelper_ = bundleMgrHelper; -} - std::shared_ptr RemoteClientManager::GetNWebSpawnClient() { return nwebSpawnClient_; diff --git a/test/fuzztest/appmgrrest_fuzzer/appmgrrest_fuzzer.cpp b/test/fuzztest/appmgrrest_fuzzer/appmgrrest_fuzzer.cpp index cdb1954f2f6..d4d7d165376 100755 --- a/test/fuzztest/appmgrrest_fuzzer/appmgrrest_fuzzer.cpp +++ b/test/fuzztest/appmgrrest_fuzzer/appmgrrest_fuzzer.cpp @@ -116,12 +116,9 @@ bool DoSomethingInterestingWithMyAPI(const char* data, size_t size) appSpawnSocket.ReadMessage(reinterpret_cast(*data), len); appSpawnSocket.CloseAppSpawnConnection(); RemoteClientManager remoteClientManager; - std::shared_ptr bundleManagerHelper = nullptr; - remoteClientManager.SetBundleManagerHelper(bundleManagerHelper); std::shared_ptr appSpawnClientptr; remoteClientManager.SetSpawnClient(appSpawnClientptr); remoteClientManager.GetSpawnClient(); - remoteClientManager.GetBundleManagerHelper(); remoteClientManager.GetNWebSpawnClient(); std::shared_ptr owner; WindowFocusChangedListener windowFocusChangedListener(owner, handler); diff --git a/test/moduletest/common/ams/app_recent_list_test/BUILD.gn b/test/moduletest/common/ams/app_recent_list_test/BUILD.gn index 6cadac3137c..69bbc6d6be4 100644 --- a/test/moduletest/common/ams/app_recent_list_test/BUILD.gn +++ b/test/moduletest/common/ams/app_recent_list_test/BUILD.gn @@ -1,4 +1,4 @@ -# Copyright (c) 2021-2022 Huawei Device Co., Ltd. +# Copyright (c) 2021-2023 Huawei Device Co., Ltd. # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at @@ -19,6 +19,7 @@ ohos_moduletest("AmsAppRecentListModuleTest") { module_out_path = module_output_path include_dirs = [ + "${ability_runtime_test_path}/mock/common/include", "${ability_runtime_test_path}/mock/services_appmgr_test/include", "${ability_runtime_path}/interfaces/kits/native/appkit/ability_bundle_manager_helper", ] @@ -30,6 +31,8 @@ ohos_moduletest("AmsAppRecentListModuleTest") { "${ability_runtime_services_path}/common:perm_verification", "${ability_runtime_services_path}/common:task_handler_wrap", "${ability_runtime_test_path}/moduletest/common/ams:appmgr_mst_source", + "//third_party/googletest:gmock_main", + "//third_party/googletest:gtest_main", ] external_deps = [ diff --git a/test/moduletest/common/ams/app_recent_list_test/ams_app_recent_list_module_test.cpp b/test/moduletest/common/ams/app_recent_list_test/ams_app_recent_list_module_test.cpp index 7b3aa05665b..34e4fd5760a 100644 --- a/test/moduletest/common/ams/app_recent_list_test/ams_app_recent_list_module_test.cpp +++ b/test/moduletest/common/ams/app_recent_list_test/ams_app_recent_list_module_test.cpp @@ -13,20 +13,24 @@ * limitations under the License. */ +#include +#include + #define private public #include "app_mgr_service_inner.h" +#include "iservice_registry.h" #undef private -#include -#include - -#include "refbase.h" +#include "bundle_mgr_interface.h" #include "hilog_wrapper.h" #include "iremote_object.h" -#include "mock_bundle_manager.h" #include "mock_ability_token.h" #include "mock_app_scheduler.h" #include "mock_app_spawn_client.h" +#include "mock_bundle_installer_service.h" +#include "mock_bundle_manager_service.h" +#include "mock_system_ability_manager.h" +#include "refbase.h" using namespace testing::ext; using testing::_; @@ -43,6 +47,9 @@ const int32_t INDEX_NUM_3 = 3; const int32_t INDEX_NUM_10 = 10; const std::string TEST_APP_NAME = "com.ohos.test.helloworld"; const std::string TEST_ABILITY_NAME = "test_ability_"; +constexpr int32_t BUNDLE_MGR_SERVICE_SYS_ABILITY_ID = 401; +sptr mockBundleInstaller = new (std::nothrow) MockBundleInstallerService(); +sptr mockBundleMgr = new (std::nothrow) MockBundleManagerService(); } // namespace class AmsAppRecentListModuleTest : public testing::Test { @@ -51,6 +58,10 @@ public: static void TearDownTestCase(); void SetUp(); void TearDown(); + void MockBundleInstallerAndSA(); + void MockBundleInstaller(); + sptr iSystemAbilityMgr_ = nullptr; + sptr mockSystemAbility_ = nullptr; protected: const std::shared_ptr GetApplicationByIndex(const int32_t index) const; @@ -59,7 +70,6 @@ protected: std::shared_ptr serviceInner_{ nullptr }; sptr mockToken_{ nullptr }; - std::shared_ptr mockBundleMgr{ nullptr }; }; void AmsAppRecentListModuleTest::SetUpTestCase() @@ -72,12 +82,31 @@ void AmsAppRecentListModuleTest::SetUp() { serviceInner_.reset(new (std::nothrow) AppMgrServiceInner()); serviceInner_->Init(); - mockBundleMgr = DelayedSingleton::GetInstance(); - serviceInner_->SetBundleManagerHelper(mockBundleMgr); + mockSystemAbility_ = new (std::nothrow) AppExecFwk::MockSystemAbilityManager(); + iSystemAbilityMgr_ = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager(); + SystemAbilityManagerClient::GetInstance().systemAbilityManager_ = mockSystemAbility_; } void AmsAppRecentListModuleTest::TearDown() -{} +{ + SystemAbilityManagerClient::GetInstance().systemAbilityManager_ = iSystemAbilityMgr_; +} + +void AmsAppRecentListModuleTest::MockBundleInstallerAndSA() +{ + auto mockGetBundleInstaller = []() { return mockBundleInstaller; }; + auto mockGetSystemAbility = [bms = mockBundleMgr, saMgr = iSystemAbilityMgr_](int32_t systemAbilityId) { + if (systemAbilityId == BUNDLE_MGR_SERVICE_SYS_ABILITY_ID) { + return bms->AsObject(); + } else { + return saMgr->GetSystemAbility(systemAbilityId); + } + }; + EXPECT_CALL(*mockBundleMgr, GetBundleInstaller()).WillOnce(testing::Invoke(mockGetBundleInstaller)); + EXPECT_CALL(*mockSystemAbility_, GetSystemAbility(testing::_)) + .WillOnce(testing::Invoke(mockGetSystemAbility)) + .WillRepeatedly(testing::Invoke(mockGetSystemAbility)); +} const std::shared_ptr AmsAppRecentListModuleTest::GetApplicationByIndex(const int32_t index) const { @@ -141,6 +170,10 @@ void AmsAppRecentListModuleTest::CreateAppRecentList(const int32_t appNum) */ HWTEST_F(AmsAppRecentListModuleTest, Create_Recent_List_001, TestSize.Level1) { + MockBundleInstallerAndSA(); + EXPECT_CALL(*mockBundleMgr, GetHapModuleInfo(testing::_, testing::_, testing::_)) + .WillOnce(testing::Return(true)) + .WillRepeatedly(testing::Return(true)); HILOG_INFO("Create_Recent_List_001 start"); EXPECT_TRUE(serviceInner_->GetRecentAppList().empty()); CreateAppRecentList(INDEX_NUM_10); diff --git a/test/moduletest/common/ams/app_running_processes_info_module_test/app_running_processes_info_module_test.cpp b/test/moduletest/common/ams/app_running_processes_info_module_test/app_running_processes_info_module_test.cpp index 06741c94300..e1cc07ece16 100644 --- a/test/moduletest/common/ams/app_running_processes_info_module_test/app_running_processes_info_module_test.cpp +++ b/test/moduletest/common/ams/app_running_processes_info_module_test/app_running_processes_info_module_test.cpp @@ -12,26 +12,31 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -#define private public -#include "app_running_record.h" -#undef private + #include #include -#include "iremote_object.h" -#include "app_record_id.h" -#include "app_scheduler_proxy.h" -#include "app_scheduler_host.h" + #define private public #include "app_mgr_service_inner.h" +#include "app_running_record.h" +#include "iservice_registry.h" #undef private -#include "mock_application.h" + #include "ability_info.h" #include "application_info.h" -#include "mock_bundle_manager.h" +#include "app_record_id.h" +#include "app_scheduler_host.h" +#include "app_scheduler_proxy.h" +#include "iremote_object.h" #include "mock_ability_token.h" +#include "mock_application.h" #include "mock_app_scheduler.h" #include "mock_app_spawn_client.h" +#include "mock_bundle_installer_service.h" +#include "mock_bundle_manager_service.h" #include "mock_native_token.h" +#include "mock_system_ability_manager.h" +#include "singleton.h" using namespace testing::ext; using OHOS::iface_cast; @@ -43,13 +48,20 @@ using testing::InvokeWithoutArgs; namespace OHOS { namespace AppExecFwk { +namespace { +constexpr int32_t BUNDLE_MGR_SERVICE_SYS_ABILITY_ID = 401; +sptr mockBundleInstaller = new (std::nothrow) MockBundleInstallerService(); +sptr mockBundleMgr = new (std::nothrow) MockBundleManagerService(); +} // namespace class AppRunningProcessesInfoModuleTest : public testing::Test { public: static void SetUpTestCase(); static void TearDownTestCase(); void SetUp(); void TearDown(); - + void MockBundleInstallerAndSA() const; + sptr iSystemAbilityMgr_ = nullptr; + sptr mockSystemAbility_ = nullptr; protected: std::string GetTestAppName(const unsigned long num) const { @@ -129,7 +141,6 @@ protected: } std::unique_ptr service_{ nullptr }; - std::shared_ptr mockBundleMgr_{ nullptr }; sptr GetMockToken() const { @@ -166,12 +177,31 @@ void AppRunningProcessesInfoModuleTest::SetUp() { service_.reset(new (std::nothrow) AppMgrServiceInner()); mockToken_ = new (std::nothrow) MockAbilityToken(); - mockBundleMgr_ = DelayedSingleton::GetInstance(); - service_->SetBundleManagerHelper(mockBundleMgr_); + mockSystemAbility_ = new (std::nothrow) AppExecFwk::MockSystemAbilityManager(); + iSystemAbilityMgr_ = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager(); + SystemAbilityManagerClient::GetInstance().systemAbilityManager_ = mockSystemAbility_; } void AppRunningProcessesInfoModuleTest::TearDown() -{} +{ + SystemAbilityManagerClient::GetInstance().systemAbilityManager_ = iSystemAbilityMgr_; +} + +void AppRunningProcessesInfoModuleTest::MockBundleInstallerAndSA() const +{ + auto mockGetBundleInstaller = []() { return mockBundleInstaller; }; + auto mockGetSystemAbility = [&](int32_t systemAbilityId) { + if (systemAbilityId == BUNDLE_MGR_SERVICE_SYS_ABILITY_ID) { + return mockBundleMgr->AsObject(); + } else { + return iSystemAbilityMgr_->GetSystemAbility(systemAbilityId); + } + }; + EXPECT_CALL(*mockBundleMgr, GetBundleInstaller()).WillOnce(testing::Invoke(mockGetBundleInstaller)); + EXPECT_CALL(*mockSystemAbility_, GetSystemAbility(testing::_)) + .WillOnce(testing::Invoke(mockGetSystemAbility)) + .WillRepeatedly(testing::Invoke(mockGetSystemAbility)); +} /* * Feature: AppMgrServiceInner @@ -183,6 +213,10 @@ void AppRunningProcessesInfoModuleTest::TearDown() */ HWTEST_F(AppRunningProcessesInfoModuleTest, ApplicationStart_001, TestSize.Level1) { + MockBundleInstallerAndSA(); + EXPECT_CALL(*mockBundleMgr, GetHapModuleInfo(testing::_, testing::_, testing::_)) + .WillOnce(testing::Return(true)) + .WillRepeatedly(testing::Return(true)); // init AppRunningRecord unsigned long index = 0L; int uid = 100; diff --git a/test/moduletest/common/ams/app_service_flow_test/BUILD.gn b/test/moduletest/common/ams/app_service_flow_test/BUILD.gn index 7638a9d9c82..e9e33afe388 100644 --- a/test/moduletest/common/ams/app_service_flow_test/BUILD.gn +++ b/test/moduletest/common/ams/app_service_flow_test/BUILD.gn @@ -20,6 +20,7 @@ ohos_moduletest("AmsAppServiceFlowModuleTest") { module_out_path = module_output_path include_dirs = [ + "${ability_runtime_test_path}/mock/common/include", "${ability_runtime_path}/interfaces/kits/native/appkit/ability_bundle_manager_helper", "${ability_runtime_test_path}/mock/services_appmgr_test/include", ] diff --git a/test/moduletest/common/ams/app_service_flow_test/ams_app_service_flow_module_test.cpp b/test/moduletest/common/ams/app_service_flow_test/ams_app_service_flow_module_test.cpp index 60b85696507..c8a5119ad5b 100644 --- a/test/moduletest/common/ams/app_service_flow_test/ams_app_service_flow_module_test.cpp +++ b/test/moduletest/common/ams/app_service_flow_test/ams_app_service_flow_module_test.cpp @@ -13,22 +13,25 @@ * limitations under the License. */ #include - #include -#include "iremote_object.h" -#include "refbase.h" - -#include "app_launch_data.h" #define private public #include "app_mgr_service_inner.h" +#include "iservice_registry.h" #undef private + + +#include "app_launch_data.h" #include "hilog_wrapper.h" -#include "mock_ability_token.h" -#include "mock_bundle_manager.h" +#include "iremote_object.h" #include "mock_ability_token.h" #include "mock_app_scheduler.h" #include "mock_app_spawn_client.h" +#include "mock_bundle_installer_service.h" +#include "mock_bundle_manager_service.h" +#include "mock_system_ability_manager.h" +#include "refbase.h" +#include "singleton.h" using namespace testing::ext; using testing::_; @@ -39,6 +42,9 @@ namespace OHOS { namespace AppExecFwk { namespace { const uint32_t CYCLE_NUMBER = 10; +constexpr int32_t BUNDLE_MGR_SERVICE_SYS_ABILITY_ID = 401; +sptr mockBundleInstaller = new (std::nothrow) MockBundleInstallerService(); +sptr mockBundleMgr = new (std::nothrow) MockBundleManagerService(); } // namespace struct TestApplicationPreRunningRecord { TestApplicationPreRunningRecord( @@ -64,6 +70,9 @@ public: static void TearDownTestCase(); void SetUp(); void TearDown(); + void MockBundleInstallerAndSA() const; + sptr iSystemAbilityMgr_ = nullptr; + sptr mockSystemAbility_ = nullptr; protected: sptr TestCreateApplicationClient(const std::shared_ptr& appRecord) const; @@ -74,7 +83,6 @@ protected: protected: std::shared_ptr serviceInner_ = nullptr; std::shared_ptr handler_ = nullptr; - std::shared_ptr mockBundleMgr_{ nullptr }; }; void AmsAppServiceFlowModuleTest::SetUpTestCase() @@ -92,12 +100,31 @@ void AmsAppServiceFlowModuleTest::SetUp() handler_ = std::make_shared(taskHandler, serviceInner_); serviceInner_->SetTaskHandler(taskHandler); serviceInner_->SetEventHandler(handler_); - mockBundleMgr_ = DelayedSingleton::GetInstance(); - serviceInner_->SetBundleManagerHelper(mockBundleMgr_); + mockSystemAbility_ = new (std::nothrow) AppExecFwk::MockSystemAbilityManager(); + iSystemAbilityMgr_ = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager(); + SystemAbilityManagerClient::GetInstance().systemAbilityManager_ = mockSystemAbility_; } void AmsAppServiceFlowModuleTest::TearDown() -{} +{ + SystemAbilityManagerClient::GetInstance().systemAbilityManager_ = iSystemAbilityMgr_; +} + +void AmsAppServiceFlowModuleTest::MockBundleInstallerAndSA() const +{ + auto mockGetBundleInstaller = []() { return mockBundleInstaller; }; + auto mockGetSystemAbility = [&](int32_t systemAbilityId) { + if (systemAbilityId == BUNDLE_MGR_SERVICE_SYS_ABILITY_ID) { + return mockBundleMgr->AsObject(); + } else { + return iSystemAbilityMgr_->GetSystemAbility(systemAbilityId); + } + }; + EXPECT_CALL(*mockBundleMgr, GetBundleInstaller()).WillOnce(testing::Invoke(mockGetBundleInstaller)); + EXPECT_CALL(*mockSystemAbility_, GetSystemAbility(testing::_)) + .WillOnce(testing::Invoke(mockGetSystemAbility)) + .WillRepeatedly(testing::Invoke(mockGetSystemAbility)); +} sptr AmsAppServiceFlowModuleTest::TestCreateApplicationClient( const std::shared_ptr& appRecord) const @@ -164,6 +191,10 @@ TestApplicationPreRunningRecord AmsAppServiceFlowModuleTest::TestCreateApplicati */ HWTEST_F(AmsAppServiceFlowModuleTest, ServiceFlow_BackKey_001, TestSize.Level1) { + MockBundleInstallerAndSA(); + EXPECT_CALL(*mockBundleMgr, GetHapModuleInfo(testing::_, testing::_, testing::_)) + .WillOnce(testing::Return(true)) + .WillRepeatedly(testing::Return(true)); HILOG_INFO("AmsAppServiceFlowModuleTest ServiceFlow_BackKey_001 start"); sptr abilityA1Token; TestApplicationPreRunningRecord testAppA = TestCreateApplicationRecordAndSetState("abilityA1", "appA", diff --git a/test/unittest/ability_interceptor_test/ability_interceptor_test.cpp b/test/unittest/ability_interceptor_test/ability_interceptor_test.cpp index 08e8ca56c0c..ee1939ebbb5 100644 --- a/test/unittest/ability_interceptor_test/ability_interceptor_test.cpp +++ b/test/unittest/ability_interceptor_test/ability_interceptor_test.cpp @@ -206,7 +206,7 @@ HWTEST_F(AbilityInterceptorTest, ControlInterceptor_005, TestSize.Level1) ElementName element("", "com.test.control", "MainAbility"); want.SetElement(element); int userId = 100; - auto bundleMgrHelper = AbilityUtil::GetBundleManagerHelper(); + auto bundleMgrHelper = DelayedSingleton::GetInstance(); // make appControlRule become nullptr by crowdtest interceptor executer->AddInterceptor(std::make_shared()); executer->AddInterceptor(std::make_shared()); diff --git a/test/unittest/ams_mgr_kill_process_test/ams_mgr_kill_process_test.cpp b/test/unittest/ams_mgr_kill_process_test/ams_mgr_kill_process_test.cpp index 542b19674d1..685a783d07f 100644 --- a/test/unittest/ams_mgr_kill_process_test/ams_mgr_kill_process_test.cpp +++ b/test/unittest/ams_mgr_kill_process_test/ams_mgr_kill_process_test.cpp @@ -64,8 +64,6 @@ std::shared_ptr AmsMgrKillProcessTest::GetAppMgrServiceInner { auto appMgrServiceInner = std::make_shared(); EXPECT_NE(appMgrServiceInner, nullptr); - auto bundleMgr = DelayedSingleton::GetInstance(); - appMgrServiceInner->remoteClientManager_->SetBundleManagerHelper(bundleMgr); return appMgrServiceInner; } diff --git a/test/unittest/ams_mgr_scheduler_test/ams_mgr_scheduler_test.cpp b/test/unittest/ams_mgr_scheduler_test/ams_mgr_scheduler_test.cpp index e7afdd9734d..3459eab6106 100644 --- a/test/unittest/ams_mgr_scheduler_test/ams_mgr_scheduler_test.cpp +++ b/test/unittest/ams_mgr_scheduler_test/ams_mgr_scheduler_test.cpp @@ -782,9 +782,7 @@ HWTEST_F(AmsMgrSchedulerTest, GetRunningProcessInfoByPid_001, TestSize.Level0) */ HWTEST_F(AmsMgrSchedulerTest, StartSpecifiedAbility_001, TestSize.Level0) { - auto mockBundleMgr = DelayedSingleton::GetInstance(); auto remoteClientManager = std::make_shared(); - remoteClientManager->SetBundleManagerHelper(mockBundleMgr); auto amsMgrServiceInner = std::make_shared(); amsMgrServiceInner->remoteClientManager_ = remoteClientManager; auto amsMgrScheduler = std::make_unique(amsMgrServiceInner, nullptr); diff --git a/test/unittest/app_mgr_service_inner_test/app_mgr_service_inner_test.cpp b/test/unittest/app_mgr_service_inner_test/app_mgr_service_inner_test.cpp index 340659bfee8..fe8fd26d2c9 100644 --- a/test/unittest/app_mgr_service_inner_test/app_mgr_service_inner_test.cpp +++ b/test/unittest/app_mgr_service_inner_test/app_mgr_service_inner_test.cpp @@ -378,7 +378,6 @@ HWTEST_F(AppMgrServiceInnerTest, LoadAbility_001, TestSize.Level0) auto appMgrServiceInner1 = std::make_shared(); EXPECT_NE(appMgrServiceInner1, nullptr); - appMgrServiceInner1->remoteClientManager_->SetBundleManagerHelper(nullptr); appMgrServiceInner1->LoadAbility(token, nullptr, abilityInfo_, applicationInfo_, nullptr); auto appMgrServiceInner2 = std::make_shared(); @@ -507,8 +506,6 @@ HWTEST_F(AppMgrServiceInnerTest, GetBundleAndHapInfo_001, TestSize.Level0) HapModuleInfo hapModuleInfo; appMgrServiceInner->GetBundleAndHapInfo(*abilityInfo_, applicationInfo_, bundleInfo, hapModuleInfo, 1); - appMgrServiceInner->remoteClientManager_->SetBundleManagerHelper(nullptr); - appMgrServiceInner->GetBundleAndHapInfo(*abilityInfo_, applicationInfo_, bundleInfo, hapModuleInfo, 1); HILOG_INFO("GetBundleAndHapInfo_001 end"); } @@ -796,9 +793,6 @@ HWTEST_F(AppMgrServiceInnerTest, KillApplicationByUid_001, TestSize.Level0) std::string bundleName = "test_bundleName"; appMgrServiceInner->KillApplicationByUid(bundleName, 0); - appMgrServiceInner->remoteClientManager_->SetBundleManagerHelper(nullptr); - appMgrServiceInner->KillApplicationByUid(bundleName, 0); - appMgrServiceInner->remoteClientManager_ = nullptr; appMgrServiceInner->KillApplicationByUid(bundleName, 0); @@ -844,10 +838,6 @@ HWTEST_F(AppMgrServiceInnerTest, KillApplicationByUserId_001, TestSize.Level0) int result = appMgrServiceInner->KillApplicationByUserId(bundleName, 0); EXPECT_EQ(result, 0); - appMgrServiceInner->remoteClientManager_->SetBundleManagerHelper(nullptr); - appMgrServiceInner->KillApplicationByUserId(bundleName, 0); - EXPECT_EQ(result, 0); - appMgrServiceInner->remoteClientManager_ = nullptr; appMgrServiceInner->KillApplicationByUserId(bundleName, 0); EXPECT_EQ(result, 0); @@ -875,10 +865,6 @@ HWTEST_F(AppMgrServiceInnerTest, KillApplicationByUserIdLocked_001, TestSize.Lev int result = appMgrServiceInner->KillApplicationByUserIdLocked(bundleName, 0); EXPECT_EQ(result, 0); - appMgrServiceInner->remoteClientManager_->SetBundleManagerHelper(nullptr); - appMgrServiceInner->KillApplicationByUserIdLocked(bundleName, 0); - EXPECT_EQ(result, 0); - appMgrServiceInner->remoteClientManager_ = nullptr; appMgrServiceInner->KillApplicationByUserIdLocked(bundleName, 0); EXPECT_EQ(result, 0); @@ -928,9 +914,6 @@ HWTEST_F(AppMgrServiceInnerTest, ClearUpApplicationDataByUserId_001, TestSize.Le appMgrServiceInner->appRunningManager_ = nullptr; appMgrServiceInner->ClearUpApplicationDataByUserId(bundleName, 1, 1, 0); - appMgrServiceInner->remoteClientManager_->SetBundleManagerHelper(nullptr); - appMgrServiceInner->ClearUpApplicationDataByUserId(bundleName, 1, 1, 0); - HILOG_INFO("ClearUpApplicationDataByUserId_001 end"); } @@ -1409,27 +1392,6 @@ HWTEST_F(AppMgrServiceInnerTest, SetAppSpawnClient_001, TestSize.Level0) HILOG_INFO("SetAppSpawnClient_001 end"); } -/** - * @tc.name: SetBundleManager_001 - * @tc.desc: set bundle manager. - * @tc.type: FUNC - * @tc.require: issueI5W4S7 - */ -HWTEST_F(AppMgrServiceInnerTest, SetBundleManager_001, TestSize.Level0) -{ - HILOG_INFO("SetBundleManager_001 start"); - auto appMgrServiceInner = std::make_shared(); - EXPECT_NE(appMgrServiceInner, nullptr); - - std::shared_ptr bundleManager; - appMgrServiceInner->SetBundleManagerHelper(bundleManager); - - appMgrServiceInner->remoteClientManager_ = nullptr; - appMgrServiceInner->SetBundleManagerHelper(bundleManager); - - HILOG_INFO("SetBundleManager_001 end"); -} - /** * @tc.name: RegisterAppStateCallback_001 * @tc.desc: register app state call back. @@ -1737,9 +1699,6 @@ HWTEST_F(AppMgrServiceInnerTest, StartProcess_001, TestSize.Level0) appMgrServiceInner->StartProcess(appName, processName, 0, appRecord, 0, bundleName, 0, false); appMgrServiceInner->StartProcess(appName, processName, 0, appRecord, 0, bundleName, 1, false); - appMgrServiceInner->SetBundleManagerHelper(nullptr); - appMgrServiceInner->StartProcess(appName, processName, 0, appRecord, 0, bundleName, 0); - appMgrServiceInner->SetAppSpawnClient(nullptr); appMgrServiceInner->StartProcess(appName, processName, 0, nullptr, 0, bundleName, 0); appMgrServiceInner->StartProcess(appName, processName, 0, appRecord, 0, bundleName, 0); @@ -2204,10 +2163,7 @@ HWTEST_F(AppMgrServiceInnerTest, CheckRemoteClient_001, TestSize.Level0) appMgrServiceInner->CheckRemoteClient(); appMgrServiceInner->remoteClientManager_->SetSpawnClient(nullptr); - appMgrServiceInner->CheckRemoteClient(); - - appMgrServiceInner->remoteClientManager_->SetBundleManagerHelper(nullptr); - appMgrServiceInner->CheckRemoteClient(); + appMgrServiceInner->CheckRemoteClient() appMgrServiceInner->remoteClientManager_ = nullptr; appMgrServiceInner->CheckRemoteClient(); @@ -2560,10 +2516,7 @@ HWTEST_F(AppMgrServiceInnerTest, StartSpecifiedAbility_001, TestSize.Level0) appMgrServiceInner->StartSpecifiedAbility(want, *abilityInfo_); abilityInfo_->applicationInfo = *applicationInfo_; - appMgrServiceInner->StartSpecifiedAbility(want, *abilityInfo_); - - appMgrServiceInner->remoteClientManager_->SetBundleManagerHelper(nullptr); - appMgrServiceInner->StartSpecifiedAbility(want, *abilityInfo_); + appMgrServiceInner->StartSpecifiedAbility(want, *abilityInfo_) appMgrServiceInner->remoteClientManager_ = nullptr; appMgrServiceInner->StartSpecifiedAbility(want, *abilityInfo_); @@ -3385,10 +3338,6 @@ HWTEST_F(AppMgrServiceInnerTest, GetRunningProcessInformation_001, TestSize.Leve int32_t ret = appMgrServiceInner->GetRunningProcessInformation(bundleName, userId, info); EXPECT_EQ(ret, ERR_OK); - appMgrServiceInner->remoteClientManager_ = nullptr; - ret = appMgrServiceInner->GetRunningProcessInformation(bundleName, userId, info); - EXPECT_EQ(ret, ERR_NO_INIT); - appMgrServiceInner->appRunningManager_ = nullptr; ret = appMgrServiceInner->GetRunningProcessInformation(bundleName, userId, info); EXPECT_EQ(ret, ERR_NO_INIT); diff --git a/test/unittest/app_running_processes_info_test/app_running_processes_info_test.cpp b/test/unittest/app_running_processes_info_test/app_running_processes_info_test.cpp index c09b3b072c5..2f138f4a123 100644 --- a/test/unittest/app_running_processes_info_test/app_running_processes_info_test.cpp +++ b/test/unittest/app_running_processes_info_test/app_running_processes_info_test.cpp @@ -67,8 +67,6 @@ public: void MockBundleInstaller(); sptr iSystemAbilityMgr_ = nullptr; sptr mockSystemAbility_ = nullptr; - std::shared_ptr bundleMgrClient = - DelayedSingleton::GetInstance(); protected: static const std::string GetTestProcessName() @@ -126,7 +124,6 @@ void AppRunningProcessesInfoTest::SetUp() mockSystemAbility_ = new (std::nothrow) AppExecFwk::MockSystemAbilityManager(); iSystemAbilityMgr_ = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager(); SystemAbilityManagerClient::GetInstance().systemAbilityManager_ = mockSystemAbility_; - service_->SetBundleManagerHelper(bundleMgrClient); } void AppRunningProcessesInfoTest::TearDown() -- Gitee From 634206f87b97dcc39ec4bd1839b2957b93812ef5 Mon Sep 17 00:00:00 2001 From: xinking129 Date: Thu, 14 Dec 2023 10:52:22 +0800 Subject: [PATCH 2/6] modify code Signed-off-by: xinking129 --- services/appmgr/include/remote_client_manager.h | 14 -------------- services/appmgr/src/app_mgr_service_inner.cpp | 4 ++-- 2 files changed, 2 insertions(+), 16 deletions(-) diff --git a/services/appmgr/include/remote_client_manager.h b/services/appmgr/include/remote_client_manager.h index 1e088e39319..131770bf37f 100644 --- a/services/appmgr/include/remote_client_manager.h +++ b/services/appmgr/include/remote_client_manager.h @@ -43,20 +43,6 @@ public: */ void SetSpawnClient(const std::shared_ptr &appSpawnClient); - /** - * GetBundleManager, Get bundle management services. - * - * @return the bundle management services instance. - */ - std::shared_ptr GetBundleManagerHelper(); - - /** - * @brief Setting bundle management instance. - * - * @param appSpawnClient, the bundle management instance. - */ - void SetBundleManagerHelper(const std::shared_ptr &bundleMgrHelper); - std::shared_ptr GetNWebSpawnClient(); private: diff --git a/services/appmgr/src/app_mgr_service_inner.cpp b/services/appmgr/src/app_mgr_service_inner.cpp index 3c75d3e8d23..a8a5b415840 100644 --- a/services/appmgr/src/app_mgr_service_inner.cpp +++ b/services/appmgr/src/app_mgr_service_inner.cpp @@ -3619,8 +3619,8 @@ bool AppMgrServiceInner::CheckCallerIsAppGallery() HILOG_ERROR("Get app running record by calling pid failed. callingPId: %{public}d", callerPid); return false; } - auto bundleMgrHelper = remoteClientManager_->GetBundleManagerHelper(); - if (!bundleMgrHelper) { + auto bundleMgrHelper = DelayedSingleton::GetInstance(); + if (bundleMgrHelper == nullptr) { HILOG_ERROR("The bundleMgrHelper is nullptr."); return false; } -- Gitee From 2049e28cb3560aedce653f8a1bf5bdcf9cf98c51 Mon Sep 17 00:00:00 2001 From: xinking129 Date: Thu, 14 Dec 2023 13:38:21 +0800 Subject: [PATCH 3/6] modify code Signed-off-by: xinking129 --- .../app_running_processes_info_module_test.cpp | 6 +++--- .../ams_app_service_flow_module_test.cpp | 6 +++--- .../app_mgr_service_inner_test.cpp | 4 ++-- 3 files changed, 8 insertions(+), 8 deletions(-) diff --git a/test/moduletest/common/ams/app_running_processes_info_module_test/app_running_processes_info_module_test.cpp b/test/moduletest/common/ams/app_running_processes_info_module_test/app_running_processes_info_module_test.cpp index e1cc07ece16..04f1ba06fa1 100644 --- a/test/moduletest/common/ams/app_running_processes_info_module_test/app_running_processes_info_module_test.cpp +++ b/test/moduletest/common/ams/app_running_processes_info_module_test/app_running_processes_info_module_test.cpp @@ -190,11 +190,11 @@ void AppRunningProcessesInfoModuleTest::TearDown() void AppRunningProcessesInfoModuleTest::MockBundleInstallerAndSA() const { auto mockGetBundleInstaller = []() { return mockBundleInstaller; }; - auto mockGetSystemAbility = [&](int32_t systemAbilityId) { + auto mockGetSystemAbility = [bms = mockBundleMgr, saMgr = iSystemAbilityMgr_](int32_t systemAbilityId) { if (systemAbilityId == BUNDLE_MGR_SERVICE_SYS_ABILITY_ID) { - return mockBundleMgr->AsObject(); + return bms->AsObject(); } else { - return iSystemAbilityMgr_->GetSystemAbility(systemAbilityId); + return saMgr->GetSystemAbility(systemAbilityId); } }; EXPECT_CALL(*mockBundleMgr, GetBundleInstaller()).WillOnce(testing::Invoke(mockGetBundleInstaller)); diff --git a/test/moduletest/common/ams/app_service_flow_test/ams_app_service_flow_module_test.cpp b/test/moduletest/common/ams/app_service_flow_test/ams_app_service_flow_module_test.cpp index c8a5119ad5b..58aa052bdf1 100644 --- a/test/moduletest/common/ams/app_service_flow_test/ams_app_service_flow_module_test.cpp +++ b/test/moduletest/common/ams/app_service_flow_test/ams_app_service_flow_module_test.cpp @@ -113,11 +113,11 @@ void AmsAppServiceFlowModuleTest::TearDown() void AmsAppServiceFlowModuleTest::MockBundleInstallerAndSA() const { auto mockGetBundleInstaller = []() { return mockBundleInstaller; }; - auto mockGetSystemAbility = [&](int32_t systemAbilityId) { + auto mockGetSystemAbility = [bms = mockBundleMgr, saMgr = iSystemAbilityMgr_](int32_t systemAbilityId) { if (systemAbilityId == BUNDLE_MGR_SERVICE_SYS_ABILITY_ID) { - return mockBundleMgr->AsObject(); + return bms->AsObject(); } else { - return iSystemAbilityMgr_->GetSystemAbility(systemAbilityId); + return saMgr->GetSystemAbility(systemAbilityId); } }; EXPECT_CALL(*mockBundleMgr, GetBundleInstaller()).WillOnce(testing::Invoke(mockGetBundleInstaller)); diff --git a/test/unittest/app_mgr_service_inner_test/app_mgr_service_inner_test.cpp b/test/unittest/app_mgr_service_inner_test/app_mgr_service_inner_test.cpp index fe8fd26d2c9..de5b9ccba28 100644 --- a/test/unittest/app_mgr_service_inner_test/app_mgr_service_inner_test.cpp +++ b/test/unittest/app_mgr_service_inner_test/app_mgr_service_inner_test.cpp @@ -2163,7 +2163,7 @@ HWTEST_F(AppMgrServiceInnerTest, CheckRemoteClient_001, TestSize.Level0) appMgrServiceInner->CheckRemoteClient(); appMgrServiceInner->remoteClientManager_->SetSpawnClient(nullptr); - appMgrServiceInner->CheckRemoteClient() + appMgrServiceInner->CheckRemoteClient(); appMgrServiceInner->remoteClientManager_ = nullptr; appMgrServiceInner->CheckRemoteClient(); @@ -2516,7 +2516,7 @@ HWTEST_F(AppMgrServiceInnerTest, StartSpecifiedAbility_001, TestSize.Level0) appMgrServiceInner->StartSpecifiedAbility(want, *abilityInfo_); abilityInfo_->applicationInfo = *applicationInfo_; - appMgrServiceInner->StartSpecifiedAbility(want, *abilityInfo_) + appMgrServiceInner->StartSpecifiedAbility(want, *abilityInfo_); appMgrServiceInner->remoteClientManager_ = nullptr; appMgrServiceInner->StartSpecifiedAbility(want, *abilityInfo_); -- Gitee From 6e22b76ba416ebdcdc88d7104550a63858b7c151 Mon Sep 17 00:00:00 2001 From: xinking129 Date: Tue, 9 Jan 2024 20:00:30 +0800 Subject: [PATCH 4/6] modify code Signed-off-by: xinking129 --- services/appmgr/src/app_mgr_service_inner.cpp | 3 --- 1 file changed, 3 deletions(-) diff --git a/services/appmgr/src/app_mgr_service_inner.cpp b/services/appmgr/src/app_mgr_service_inner.cpp index 10ae152b387..57881500d68 100644 --- a/services/appmgr/src/app_mgr_service_inner.cpp +++ b/services/appmgr/src/app_mgr_service_inner.cpp @@ -4424,16 +4424,13 @@ int32_t AppMgrServiceInner::NotifyAppFaultBySA(const AppFaultDataBySA &faultData HILOG_ERROR("no such AppRunningRecord"); return ERR_INVALID_VALUE; } - FaultData transformedFaultData = ConvertDataTypes(faultData); int32_t uid = record->GetUid(); std::string bundleName = record->GetBundleName(); - if (faultData.errorObject.name == "appRecovery") { AppRecoveryNotifyApp(pid, bundleName, faultData.faultType, "appRecovery"); return ERR_OK; } - if (transformedFaultData.timeoutMarkers.empty()) { transformedFaultData.timeoutMarkers = "notifyFault:" + transformedFaultData.errorObject.name + std::to_string(pid) + "-" + std::to_string(SystemTimeMillisecond()); -- Gitee From c7238357907f6309138a2eb8bab5dfb606c1c327 Mon Sep 17 00:00:00 2001 From: xinking129 Date: Tue, 30 Jan 2024 14:09:11 +0800 Subject: [PATCH 5/6] modify code Signed-off-by: xinking129 --- test/moduletest/common/ams/app_recent_list_test/BUILD.gn | 2 -- 1 file changed, 2 deletions(-) diff --git a/test/moduletest/common/ams/app_recent_list_test/BUILD.gn b/test/moduletest/common/ams/app_recent_list_test/BUILD.gn index 69bbc6d6be4..4086c21b3d2 100644 --- a/test/moduletest/common/ams/app_recent_list_test/BUILD.gn +++ b/test/moduletest/common/ams/app_recent_list_test/BUILD.gn @@ -31,8 +31,6 @@ ohos_moduletest("AmsAppRecentListModuleTest") { "${ability_runtime_services_path}/common:perm_verification", "${ability_runtime_services_path}/common:task_handler_wrap", "${ability_runtime_test_path}/moduletest/common/ams:appmgr_mst_source", - "//third_party/googletest:gmock_main", - "//third_party/googletest:gtest_main", ] external_deps = [ -- Gitee From 22d8c449b7541e4d6446e729aacc7f1f794a9656 Mon Sep 17 00:00:00 2001 From: xinking129 Date: Mon, 5 Feb 2024 21:08:09 +0800 Subject: [PATCH 6/6] modify code Signed-off-by: xinking129 --- services/abilitymgr/src/app_exit_reason_helper.cpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/services/abilitymgr/src/app_exit_reason_helper.cpp b/services/abilitymgr/src/app_exit_reason_helper.cpp index cabd044174e..dd09c649e0a 100644 --- a/services/abilitymgr/src/app_exit_reason_helper.cpp +++ b/services/abilitymgr/src/app_exit_reason_helper.cpp @@ -54,11 +54,11 @@ int32_t AppExitReasonHelper::RecordAppExitReason(const ExitReason &exitReason) return ERR_INVALID_VALUE; } - auto bms = AbilityUtil::GetBundleManagerHelper(); - CHECK_POINTER_AND_RETURN(bms, ERR_NULL_OBJECT); + auto bundleMgrHelper = DelayedSingleton::GetInstance(); + CHECK_POINTER_AND_RETURN(bundleMgrHelper, ERR_NULL_OBJECT); std::string bundleName; int32_t callerUid = IPCSkeleton::GetCallingUid(); - if (IN_PROCESS_CALL(bms->GetNameForUid(callerUid, bundleName)) != ERR_OK) { + if (IN_PROCESS_CALL(bundleMgrHelper->GetNameForUid(callerUid, bundleName)) != ERR_OK) { HILOG_ERROR("Get Bundle Name failed."); return GET_BUNDLE_INFO_FAILED; } -- Gitee