diff --git a/frameworks/kits/ability/ability_runtime/src/local_call_container.cpp b/frameworks/kits/ability/ability_runtime/src/local_call_container.cpp index a1ac33cca6d94ae58deb8a6d71aec1aeed5011fa..22f8463272e8d66b4054aea7928dd8a71e2749d5 100644 --- a/frameworks/kits/ability/ability_runtime/src/local_call_container.cpp +++ b/frameworks/kits/ability/ability_runtime/src/local_call_container.cpp @@ -63,9 +63,9 @@ int LocalCallContainer::StartAbilityInner( HILOG_ERROR("LocalCallContainer::Resolve abilityClient is nullptr"); return ERR_INVALID_VALUE; } - sptr connect = iface_cast(this); + sptr connect = iface_cast(this->AsObject()); HILOG_DEBUG("start ability by call, abilityClient->StartAbilityByCall call"); - return abilityClient->StartAbilityByCall(want, this, callerToken); + return abilityClient->StartAbilityByCall(want, connect, callerToken); } // already finish call request. HILOG_DEBUG("start ability by call, callback->InvokeCallBack(remote) begin"); @@ -105,7 +105,7 @@ int LocalCallContainer::Release(const std::shared_ptr& callback) HILOG_ERROR("LocalCallContainer::Resolve abilityClient is nullptr"); return ERR_INVALID_VALUE; } - sptr connect = iface_cast(this); + sptr connect = iface_cast(this->AsObject()); if (abilityClient->ReleaseAbility(connect, elementName) != ERR_OK) { HILOG_ERROR("ReleaseAbility failed."); return ERR_INVALID_VALUE; diff --git a/frameworks/kits/ability/native/include/ability_runtime/js_ability.h b/frameworks/kits/ability/native/include/ability_runtime/js_ability.h index 989495a67579ef4c723e0fcb4f6e5ca04309ef37..3b740e5726fd51c0ae598fb5dfa64fff6cbb87d1 100755 --- a/frameworks/kits/ability/native/include/ability_runtime/js_ability.h +++ b/frameworks/kits/ability/native/include/ability_runtime/js_ability.h @@ -77,6 +77,7 @@ private: JsRuntime &jsRuntime_; std::shared_ptr shellContextRef_; std::unique_ptr jsAbilityObj_; + sptr remoteCallee_; }; } // namespace AbilityRuntime } // namespace OHOS diff --git a/frameworks/kits/ability/native/src/ability_runtime/js_ability.cpp b/frameworks/kits/ability/native/src/ability_runtime/js_ability.cpp index 5e6d332fa6309d805aa0c84d5f0bfb7e387fe9c9..484d2f1855196b694f47d714441ea0ea84f7e522 100755 --- a/frameworks/kits/ability/native/src/ability_runtime/js_ability.cpp +++ b/frameworks/kits/ability/native/src/ability_runtime/js_ability.cpp @@ -370,6 +370,12 @@ sptr JsAbility::CallRequest() HILOG_WARN("JsAbility::CallRequest Obj is nullptr"); return nullptr; } + + if (remoteCallee_ != nullptr) { + HILOG_INFO("JsAbility::CallRequest get Callee remoteObj."); + return remoteCallee_; + } + HandleScope handleScope(jsRuntime_); HILOG_DEBUG("JsAbility::CallRequest set runtime scope."); auto& nativeEngine = jsRuntime_.GetNativeEngine(); @@ -403,8 +409,9 @@ sptr JsAbility::CallRequest() HILOG_ERROR("JsAbility::CallRequest obj is nullptr"); } + remoteCallee_ = remoteObj; HILOG_INFO("JsAbility::CallRequest end."); - return remoteObj; + return remoteCallee_; } void JsAbility::OnRequestPermissionsFromUserResult( diff --git a/frameworks/kits/ability/native/src/ability_runtime/js_caller_complex.cpp b/frameworks/kits/ability/native/src/ability_runtime/js_caller_complex.cpp index 76b16908e1db9803802758bc4062d9a01ce0a494..699329cb22fc730619d579427c8eb6b14a5b3505 100644 --- a/frameworks/kits/ability/native/src/ability_runtime/js_caller_complex.cpp +++ b/frameworks/kits/ability/native/src/ability_runtime/js_caller_complex.cpp @@ -58,6 +58,7 @@ public: return; } std::unique_ptr(static_cast(data)); + HILOG_ERROR("JsCallerComplex::%{public}s is called.", __func__); } static NativeValue* JsRelease(NativeEngine* engine, NativeCallbackInfo* info) @@ -112,7 +113,7 @@ private: } auto task = [notify = this, &str] () { notify->OnReleaseNotifyTask(str); }; - handler_->PostTask(task, "OnReleaseNotify"); + handler_->PostSyncTask(task, "OnReleaseNotify"); HILOG_DEBUG("OnReleaseNotify end"); } diff --git a/frameworks/kits/ability/native/src/new_ability_impl.cpp b/frameworks/kits/ability/native/src/new_ability_impl.cpp index 78f08b58884b401cc5fee58db5dc66810965056e..0fc4d7d53fddade706c7ccbfaa0aa7ad9c16f4c7 100644 --- a/frameworks/kits/ability/native/src/new_ability_impl.cpp +++ b/frameworks/kits/ability/native/src/new_ability_impl.cpp @@ -95,8 +95,10 @@ bool NewAbilityImpl::AbilityTransaction(const Want &want, const AAFwk::LifeCycle break; } case AAFwk::ABILITY_STATE_BACKGROUND_NEW: { + if (lifecycleState_ != ABILITY_STATE_STARTED_NEW) { + ret = false; + } Background(); - ret = false; break; } default: { diff --git a/interfaces/innerkits/app_manager/src/appmgr/app_mgr_proxy.cpp b/interfaces/innerkits/app_manager/src/appmgr/app_mgr_proxy.cpp index 37514372260839865a9ac3afd9892d38eb555433..22a1a1630599520510809b66f0ccb5a3ee85e432 100644 --- a/interfaces/innerkits/app_manager/src/appmgr/app_mgr_proxy.cpp +++ b/interfaces/innerkits/app_manager/src/appmgr/app_mgr_proxy.cpp @@ -260,10 +260,10 @@ int32_t AppMgrProxy::GetProcessRunningInfosByUserId(std::vector remote = Remote(); if (remote == nullptr) { APP_LOGE("Remote() is NULL"); @@ -570,10 +570,10 @@ int AppMgrProxy::GetAbilityRecordsByProcessID(const int pid, std::vector { public: - Mission(int32_t id, const std::shared_ptr abilityRecord, const std::string &missionName = ""); + Mission(int32_t id, const std::shared_ptr abilityRecord, const std::string &missionName = "", + int32_t startMethod = 0); Mission(const std::shared_ptr &mission); virtual ~Mission(); @@ -120,8 +121,25 @@ public: */ void Dump(std::vector &info); + /** + * @brief whether it is a form ByCall start-up + * + * @return true form BaCall start-up, false other + */ + bool IsStartByCall(); + + /** + * @brief update mission id + * + * @param id mission id. + * @param method start method. + * @return Returns true on success, false on failure + */ + bool UpdateMissionId(int32_t id, int32_t method); + private: int32_t missionId_; + int32_t startMethod_; std::shared_ptr abilityRecord_; std::string missionName_; std::weak_ptr ownerMissionList_; diff --git a/services/abilitymgr/src/ability_manager_service.cpp b/services/abilitymgr/src/ability_manager_service.cpp index 4c7cf21c3858fbbc692d634fa2669d7f423c0c3d..51b3e157797dc4732e0696c07d4124e9b09c4fa9 100644 --- a/services/abilitymgr/src/ability_manager_service.cpp +++ b/services/abilitymgr/src/ability_manager_service.cpp @@ -3475,6 +3475,18 @@ int AbilityManagerService::StartAbilityByCall( return RESOLVE_CALL_NO_PERMISSIONS; } + HILOG_DEBUG("abilityInfo.applicationInfo.singleUser is %{public}s", + abilityRequest.abilityInfo.applicationInfo.singleUser ? "true" : "false"); + if (abilityRequest.abilityInfo.applicationInfo.singleUser) { + auto missionListManager = GetListManagerByUserId(U0_USER_ID); + if (missionListManager == nullptr) { + HILOG_ERROR("missionListManager is Null. userId=%{public}d", U0_USER_ID); + return ERR_INVALID_VALUE; + } + + return missionListManager->ResolveLocked(abilityRequest); + } + return currentMissionListManager_->ResolveLocked(abilityRequest); } diff --git a/services/abilitymgr/src/ability_record.cpp b/services/abilitymgr/src/ability_record.cpp index 2e556f359b9e00a9a0bae2b7d5a101ecd3c4bc30..03a20a15aaa462806fd5322a69877ccefab4bf2f 100644 --- a/services/abilitymgr/src/ability_record.cpp +++ b/services/abilitymgr/src/ability_record.cpp @@ -694,6 +694,7 @@ std::shared_ptr AbilityRecord::GetResult() const void AbilityRecord::SendResult() { HILOG_INFO("Send result."); + std::lock_guard guard(lock_); CHECK_POINTER(scheduler_); CHECK_POINTER(result_); scheduler_->SendResult(result_->requestCode_, result_->resultCode_, result_->resultWant_); @@ -705,6 +706,10 @@ void AbilityRecord::SendResult() void AbilityRecord::SendResultToCallers() { for (auto caller : GetCallerRecordList()) { + if (caller == nullptr) { + HILOG_WARN("Caller record is nullptr."); + continue; + } std::shared_ptr callerAbilityRecord = caller->GetCaller(); if (callerAbilityRecord != nullptr && callerAbilityRecord->GetResult() != nullptr) { callerAbilityRecord->SendResult(); @@ -715,6 +720,10 @@ void AbilityRecord::SendResultToCallers() void AbilityRecord::SaveResultToCallers(const int resultCode, const Want *resultWant) { for (auto caller : GetCallerRecordList()) { + if (caller == nullptr) { + HILOG_WARN("Caller record is nullptr."); + continue; + } std::shared_ptr callerAbilityRecord = caller->GetCaller(); if (callerAbilityRecord != nullptr) { callerAbilityRecord->SetResult( @@ -1062,6 +1071,7 @@ void AbilityRecord::OnSchedulerDied(const wptr &remote) if (mission) { HILOG_WARN("On scheduler died. Is app not response Reason:%{public}d", mission->IsANRState()); } + std::lock_guard guard(lock_); CHECK_POINTER(scheduler_); auto object = remote.promote(); diff --git a/services/abilitymgr/src/ability_scheduler_proxy.cpp b/services/abilitymgr/src/ability_scheduler_proxy.cpp index 557d9c42a522b16d0c59541443ef2f3fd02792c3..17fcd71a727c675107cb6a1fa056f7c5a0087d8c 100755 --- a/services/abilitymgr/src/ability_scheduler_proxy.cpp +++ b/services/abilitymgr/src/ability_scheduler_proxy.cpp @@ -1032,10 +1032,9 @@ sptr AbilitySchedulerProxy::CallRequest() HILOG_ERROR("CallRequest failed, err %{public}d", result); return nullptr; } - - auto call = reply.ReadParcelable(); - if (!call) { - HILOG_ERROR("CallRequest error"); + auto call = reply.ReadRemoteObject(); + if (call == nullptr) { + HILOG_ERROR("CallRequest failed, err remoteObject is nullptr"); return nullptr; } diff --git a/services/abilitymgr/src/ability_scheduler_stub.cpp b/services/abilitymgr/src/ability_scheduler_stub.cpp index 89e19940414d40019d3d127e4c5bf403428909a0..1b8057897fe52cc40f902fa1523757e356fbdd18 100644 --- a/services/abilitymgr/src/ability_scheduler_stub.cpp +++ b/services/abilitymgr/src/ability_scheduler_stub.cpp @@ -614,8 +614,8 @@ int AbilitySchedulerStub::CallRequestInner(MessageParcel &data, MessageParcel &r return ERR_INVALID_VALUE; } - if (!reply.WriteParcelable(call)) { - HILOG_ERROR("call request write failed."); + if (!reply.WriteRemoteObject(call)) { + HILOG_ERROR("Connect done remote object error."); return ERR_INVALID_VALUE; } diff --git a/services/abilitymgr/src/call_record.cpp b/services/abilitymgr/src/call_record.cpp index c4f7ca30979b8d42eb3b681569b1469f7274be91..934e4194bc5a8762a2496b6bd94a2f81cc542abf 100644 --- a/services/abilitymgr/src/call_record.cpp +++ b/services/abilitymgr/src/call_record.cpp @@ -140,8 +140,6 @@ bool CallRecord::SchedulerDisConnectDone() void CallRecord::OnCallStubDied(const wptr & remote) { HILOG_WARN("callstub is died. id:%{public}d", recordId_); - auto object = remote.promote(); - CHECK_POINTER(object); auto abilityManagerService = DelayedSingleton::GetInstance(); CHECK_POINTER(abilityManagerService); diff --git a/services/abilitymgr/src/inner_mission_info.cpp b/services/abilitymgr/src/inner_mission_info.cpp index 6231b346fce388e389a010501dfaa0ac76402157..2198f0df7ed3b311e87ef5494c2b2e88ea14512e 100644 --- a/services/abilitymgr/src/inner_mission_info.cpp +++ b/services/abilitymgr/src/inner_mission_info.cpp @@ -25,6 +25,7 @@ std::string InnerMissionInfo::ToJsonStr() const nlohmann::json value; value["MissionName"] = missionName; value["IsSingleton"] = isSingletonMode; + value["StartMethod"] = startMethod; value["MissionId"] = missionInfo.id; value["RunningState"] = missionInfo.runningState; value["LockedState"] = missionInfo.lockedState; @@ -71,6 +72,10 @@ bool InnerMissionInfo::FromJsonStr(const std::string &jsonStr) return false; } isSingletonMode = value["IsSingleton"].get(); + if (!CheckJsonNode("StartMethod", JsonType::NUMBER)) { + return false; + } + startMethod = value["StartMethod"].get(); if (!CheckJsonNode("MissionId", JsonType::NUMBER)) { return false; } diff --git a/services/abilitymgr/src/mission.cpp b/services/abilitymgr/src/mission.cpp index 7113b70418a0b8dec6fcd8163ceccf0b09dab28e..20fdda11f1c2a716db269ff25210602b89c5a99a 100644 --- a/services/abilitymgr/src/mission.cpp +++ b/services/abilitymgr/src/mission.cpp @@ -17,8 +17,9 @@ namespace OHOS { namespace AAFwk { -Mission::Mission(int32_t id, const std::shared_ptr abilityRecord, const std::string &missionName) - : missionId_(id), abilityRecord_(abilityRecord), missionName_(missionName) +Mission::Mission(int32_t id, const std::shared_ptr abilityRecord, const std::string &missionName, + int32_t startMethod) + : missionId_(id), startMethod_(startMethod), abilityRecord_(abilityRecord), missionName_(missionName) { } @@ -29,6 +30,7 @@ Mission::Mission(const std::shared_ptr &mission) } missionId_ = mission->missionId_; + startMethod_ = mission->startMethod_; abilityRecord_ = mission->abilityRecord_; missionName_ = mission->missionName_; ownerMissionList_ = mission->ownerMissionList_; @@ -115,5 +117,21 @@ void Mission::Dump(std::vector &info) } } +bool Mission::IsStartByCall() +{ + return static_cast(StartMethod::START_CALL) == startMethod_; +} + +bool Mission::UpdateMissionId(int32_t id, int32_t method) +{ + if (method == startMethod_ && id > 0) { + return false; + } + + startMethod_ = method; + missionId_ = id; + return true; +} + } // namespace AAFwk } // namespace OHOS diff --git a/services/abilitymgr/src/mission_info_mgr.cpp b/services/abilitymgr/src/mission_info_mgr.cpp index 5dbb0d3d63b06bbd12c3ef3101e008614d01b0c0..224364b1b2cf0976000d8ff0b896816fdc12c328 100644 --- a/services/abilitymgr/src/mission_info_mgr.cpp +++ b/services/abilitymgr/src/mission_info_mgr.cpp @@ -179,6 +179,16 @@ bool MissionInfoMgr::DeleteAllMissionInfos(const std::shared_ptr(startMethod)) { + case StartMethod::START_CALL: + return true; + default: + return false; + } +} + int MissionInfoMgr::GetMissionInfos(int32_t numMax, std::vector &missionInfos) { HILOG_INFO("GetMissionInfos, numMax:%{public}d", numMax); @@ -190,6 +200,11 @@ int MissionInfoMgr::GetMissionInfos(int32_t numMax, std::vector &mi if (static_cast(missionInfos.size()) >= numMax) { break; } + + if (DoesNotShowInTheMissionList(mission.startMethod)) { + HILOG_INFO("MissionId[%{public}d] don't show in mission list", mission.missionInfo.id); + continue; + } MissionInfo info = mission.missionInfo; missionInfos.emplace_back(info); } @@ -215,6 +230,12 @@ int MissionInfoMgr::GetMissionInfoById(int32_t missionId, MissionInfo &missionIn HILOG_ERROR("no such mission:%{public}d", missionId); return -1; } + + if (DoesNotShowInTheMissionList((*it).startMethod)) { + HILOG_INFO("MissionId[%{public}d] don't show in mission list", (*it).missionInfo.id); + return -1; + } + HILOG_INFO("GetMissionInfoById, find missionId missionId:%{public}d", missionId); missionInfo = (*it).missionInfo; return 0; diff --git a/services/abilitymgr/src/mission_list_manager.cpp b/services/abilitymgr/src/mission_list_manager.cpp index 7f27af14e24704375f28ee8f4462c6f296897b3a..b220cab98686632df52908b8ae4dcc420d3293f9 100644 --- a/services/abilitymgr/src/mission_list_manager.cpp +++ b/services/abilitymgr/src/mission_list_manager.cpp @@ -286,10 +286,36 @@ int MissionListManager::StartAbilityLocked(const std::shared_ptr } } +static int32_t CallType2StartMethod(int32_t callType) +{ + switch (callType) { + case AbilityCallType::INVALID_TYPE: + return static_cast(StartMethod::START_NORMAL); + case AbilityCallType::CALL_REQUEST_TYPE: + return static_cast(StartMethod::START_CALL); + default: + break; + } + return -1; +} + +static bool CallTypeFilter(int32_t callType) +{ + switch (callType) { + case AbilityCallType::CALL_REQUEST_TYPE: + return true; + default: + break; + } + return false; +} + void MissionListManager::GetTargetMissionAndAbility(const AbilityRequest &abilityRequest, std::shared_ptr &targetMission, std::shared_ptr &targetRecord) { + auto startMethod = CallType2StartMethod(abilityRequest.callType); + HILOG_DEBUG("GetTargetMissionAndAbility called startMethod is %{public}d.", startMethod); auto reUsedMission = GetReusedMission(abilityRequest); if (reUsedMission) { HILOG_DEBUG("find reused mission in running list."); @@ -299,7 +325,13 @@ void MissionListManager::GetTargetMissionAndAbility(const AbilityRequest &abilit targetRecord->SetWant(abilityRequest.want); targetRecord->SetIsNewWant(true); } - return; + + if (!(targetMission->IsStartByCall() + && !CallTypeFilter(startMethod))) { + HILOG_DEBUG("mission exists. No update required"); + return; + } + HILOG_DEBUG("mission exists. need to be updated"); } // no reused mission, create a new one. @@ -319,6 +351,7 @@ void MissionListManager::GetTargetMissionAndAbility(const AbilityRequest &abilit info.missionName = missionName; info.isSingletonMode = isSingleton; + info.startMethod = startMethod; info.missionInfo.runningState = 0; info.missionInfo.continuable = abilityRequest.abilityInfo.continuable; info.missionInfo.time = Time2str(time(0)); @@ -332,10 +365,22 @@ void MissionListManager::GetTargetMissionAndAbility(const AbilityRequest &abilit return; } } - targetRecord = AbilityRecord::CreateAbilityRecord(abilityRequest); - targetMission = std::make_shared(info.missionInfo.id, targetRecord, missionName); - targetRecord->SetUseNewMission(); - targetRecord->SetMission(targetMission); + + if (targetMission == nullptr) { + HILOG_DEBUG("Make new mission data."); + targetRecord = AbilityRecord::CreateAbilityRecord(abilityRequest); + targetMission = std::make_shared(info.missionInfo.id, targetRecord, missionName, startMethod); + targetRecord->SetUseNewMission(); + targetRecord->SetMission(targetMission); + } else { + HILOG_DEBUG("Update old mission data."); + auto state = targetMission->UpdateMissionId(info.missionInfo.id, startMethod); + if (!state) { + HILOG_INFO("targetMission UpdateMissionId(%{public}d, %{public}d) failed", info.missionInfo.id, + startMethod); + } + HILOG_DEBUG("Update MissionId UpdateMissionId(%{public}d, %{public}d) end", info.missionInfo.id, startMethod); + } if (abilityRequest.abilityInfo.launchMode == AppExecFwk::LaunchMode::SPECIFIED) { targetRecord->SetSpecifiedFlag(abilityRequest.specifiedFlag); diff --git a/services/appmgr/src/app_mgr_service.cpp b/services/appmgr/src/app_mgr_service.cpp index 24f851d2f52d382c6da6cf1dd460730ece826443..05cd47dc156db744d2f013f613a9f9d6f9b532ef 100755 --- a/services/appmgr/src/app_mgr_service.cpp +++ b/services/appmgr/src/app_mgr_service.cpp @@ -36,7 +36,7 @@ namespace OHOS { namespace AppExecFwk { namespace { static const int EXPERIENCE_MEM_THRESHOLD = 20; -static const int APP_MS_TimeOut = 60; +static const int APP_MS_TIMEOUT = 60; static const float PERCENTAGE = 100.0; const std::string TASK_ATTACH_APPLICATION = "AttachApplicationTask"; const std::string TASK_APPLICATION_FOREGROUNDED = "ApplicationForegroundedTask"; @@ -150,7 +150,7 @@ ErrCode AppMgrService::Init() APP_LOGE("init failed without ams scheduler"); return ERR_INVALID_OPERATION; } - if (HiviewDFX::Watchdog::GetInstance().AddThread("APPMSWatchdog", handler_, APP_MS_TimeOut) != 0) { + if (HiviewDFX::Watchdog::GetInstance().AddThread("APPMSWatchdog", handler_, APP_MS_TIMEOUT) != 0) { APP_LOGE("HiviewDFX::Watchdog::GetInstance AddThread Fail"); } APP_LOGI("init success"); diff --git a/services/appmgr/test/BUILD.gn b/services/appmgr/test/BUILD.gn index 4f1eee18f96464d897832a25faa96ee5add1874c..b8fdede6cfe8873c5af676e1ebd18cb768bf9747 100644 --- a/services/appmgr/test/BUILD.gn +++ b/services/appmgr/test/BUILD.gn @@ -81,6 +81,7 @@ ohos_source_set("appmgr_test_source") { cflags += [ "-DBINDER_IPC_32BIT" ] } deps = [ + "${services_path}/common:perm_verification", "//base/global/i18n_standard/frameworks/intl:intl_util", "//base/security/permission/interfaces/innerkits/permission_standard/permissionsdk:libpermissionsdk_standard", "//foundation/aafwk/standard/interfaces/innerkits/app_manager:app_manager", @@ -112,6 +113,9 @@ group("unittest") { "unittest/ams_app_state_callback_test:unittest", "unittest/ams_app_workflow_test:unittest", "unittest/ams_ipc_interface_test:unittest", + + # "unittest/ams_lmks_client_test:unittest", + "unittest/ams_mgr_kill_process_test:unittest", "unittest/ams_mgr_scheduler_test:unittest", "unittest/ams_recent_app_list_test:unittest", "unittest/ams_service_app_spawn_client_test:unittest", @@ -121,5 +125,6 @@ group("unittest") { "unittest/ams_service_load_ability_process_test:unittest", "unittest/ams_service_startup_test:unittest", "unittest/app_mgr_service_event_handler_test:unittest", + "unittest/app_running_processes_info_test:unittest", ] } diff --git a/services/appmgr/test/mock/src/mock_bundle_manager.cpp b/services/appmgr/test/mock/src/mock_bundle_manager.cpp index ca3ec28c311c6baa4388604409ed6cb968bbdbc5..fe0d3f33cf5c717130974418497dd48d8b293990 100644 --- a/services/appmgr/test/mock/src/mock_bundle_manager.cpp +++ b/services/appmgr/test/mock/src/mock_bundle_manager.cpp @@ -140,6 +140,30 @@ bool BundleMgrService::GetBundleInfo( bundleInfo.jointUserId = ""; bundleInfo.appId = bundleName + "_xxx"; } + if (bundleName == "KeepAliveApplication") { + HapModuleInfo hapModuleInfo; + hapModuleInfo.moduleName = "KeepAliveApplication"; + ApplicationInfo appInfo; + appInfo.name = "KeepAliveApp"; + appInfo.bundleName = bundleName; + appInfo.uid = 2100; + bundleInfo.uid = 2100; + bundleInfo.name = bundleName; + bundleInfo.applicationInfo = appInfo; + bundleInfo.hapModuleInfos.push_back(hapModuleInfo); + } + if (bundleName == "KeepAliveApplication1") { + HapModuleInfo hapModuleInfo; + hapModuleInfo.moduleName = "KeepAliveApplication1"; + ApplicationInfo appInfo; + appInfo.name = "KeepAliveApp1"; + appInfo.bundleName = bundleName; + appInfo.uid = 2101; + bundleInfo.uid = 2101; + bundleInfo.name = bundleName; + bundleInfo.applicationInfo = appInfo; + bundleInfo.hapModuleInfos.push_back(hapModuleInfo); + } return true; } bool BundleMgrService::GetBundleGids(const std::string &bundleName, std::vector &gids) @@ -156,6 +180,7 @@ bool BundleMgrService::GetBundleGids(const std::string &bundleName, std::vector< bool BundleMgrService::GetBundleInfos( const BundleFlag flag, std::vector &bundleInfos, int32_t userId) { + GTEST_LOG_(INFO) << "BundleMgrService::GetBundleInfos"; bundleInfos = bundleInfos_; return true; } @@ -242,12 +267,18 @@ void BundleMgrService::MakingPackageData() void BundleMgrService::MakingResidentProcData() { int appUid = 2100; + int appUid1 = 2101; GTEST_LOG_(INFO) << "MakingResidentProcData()"; BundleInfo bundleInfo; bundleInfo.uid = appUid; bundleInfo.name = "KeepAliveApplication"; + BundleInfo bundleInfo1; + bundleInfo1.uid = appUid1; + bundleInfo1.name = "KeepAliveApplication1"; + bundleInfos_.emplace_back(bundleInfo); + bundleInfos_.emplace_back(bundleInfo1); } } // namespace AppExecFwk } // namespace OHOS diff --git a/services/appmgr/test/unittest/ams_ability_running_record_test/BUILD.gn b/services/appmgr/test/unittest/ams_ability_running_record_test/BUILD.gn index aa631564e0a968e8abac396be4dbfc94b00cbc96..5a98e62e9046f276720146fc4e0b0416e5cf4d5b 100644 --- a/services/appmgr/test/unittest/ams_ability_running_record_test/BUILD.gn +++ b/services/appmgr/test/unittest/ams_ability_running_record_test/BUILD.gn @@ -53,6 +53,7 @@ ohos_unittest("AmsAbilityRunningRecordTest") { cflags += [ "-DBINDER_IPC_32BIT" ] } deps = [ + "${aafwk_path}/interfaces/innerkits/ability_manager:ability_manager", "${appexecfwk_path}/libs/libeventhandler:libeventhandler_target", "${innerkits_path}/uri_permission:uri_permission_mgr", "${services_path}/appmgr/test:appmgr_test_source", diff --git a/services/appmgr/test/unittest/ams_app_life_cycle_test/ams_app_life_cycle_test.cpp b/services/appmgr/test/unittest/ams_app_life_cycle_test/ams_app_life_cycle_test.cpp index 60033cec96290b4e5e08409a0c0aad6897677a94..f82c0cf621e0d20e1a17e1373b54b7cc8b37f2bb 100644 --- a/services/appmgr/test/unittest/ams_app_life_cycle_test/ams_app_life_cycle_test.cpp +++ b/services/appmgr/test/unittest/ams_app_life_cycle_test/ams_app_life_cycle_test.cpp @@ -1831,6 +1831,60 @@ HWTEST_F(AmsAppLifeCycleTest, ClearUpApplicationData_001, TestSize.Level1) serviceInner_->ClearUpApplicationData(appRecord->GetBundleName(), appRecord->GetUid(), NEW_PID); } +/* + * Feature: AMS + * Function: AppLifeCycle + * SubFunction: ClearUpApplicationData + * FunctionPoints: ClearUpApplicationData + * CaseDescription: clear the application data. + */ +HWTEST_F(AmsAppLifeCycleTest, ClearUpApplicationData_002, TestSize.Level1) +{ + const pid_t NEW_PID = 1234; + auto abilityInfo = GetAbilityInfoByIndex("110"); + auto appInfo = GetApplication(); + sptr token = GetMockToken(); + + auto appRecord = StartProcessAndLoadAbility(token, nullptr, abilityInfo, appInfo, NEW_PID); + appRecord->SetUid(101); + + EXPECT_CALL(*mockBundleMgr, CleanBundleDataFiles(_, _)).Times(1).WillOnce(Return(101)); + EXPECT_CALL(*mockBundleMgr, GetUidByBundleName(_, _)).Times(1); + + sptr mockAppScheduler = new MockAppScheduler(); + sptr client = iface_cast(mockAppScheduler.GetRefPtr()); + appRecord->SetApplicationClient(client); + + serviceInner_->ClearUpApplicationData(appRecord->GetBundleName(), appRecord->GetUid(), NEW_PID); +} + +/* + * Feature: AMS + * Function: AppLifeCycle + * SubFunction: ClearUpApplicationData + * FunctionPoints: ClearUpApplicationData + * CaseDescription: clear the application data. + */ +HWTEST_F(AmsAppLifeCycleTest, ClearUpApplicationData_003, TestSize.Level1) +{ + const pid_t NEW_PID = 1234; + auto abilityInfo = GetAbilityInfoByIndex("110"); + auto appInfo = GetApplication(); + sptr token = GetMockToken(); + + auto appRecord = StartProcessAndLoadAbility(token, nullptr, abilityInfo, appInfo, NEW_PID); + appRecord->SetUid(101); + + EXPECT_CALL(*mockBundleMgr, CleanBundleDataFiles(_, _)).Times(1).WillOnce(Return(0)); + EXPECT_CALL(*mockBundleMgr, GetUidByBundleName(_, _)).Times(0); + + sptr mockAppScheduler = new MockAppScheduler(); + sptr client = iface_cast(mockAppScheduler.GetRefPtr()); + appRecord->SetApplicationClient(client); + + serviceInner_->ClearUpApplicationData(appRecord->GetBundleName(), appRecord->GetUid(), NEW_PID); +} + /* * Feature: AMS * Function: AppLifeCycle::CreateAppRunningRecord @@ -1902,8 +1956,8 @@ HWTEST_F(AmsAppLifeCycleTest, CheckAppRunningRecordIsExist_001, TestSize.Level1) * Feature: AMS * Function: AppLifeCycle::StartResidentProcess * SubFunction: NA - * FunctionPoints: NA - * CaseDescription: NA + * FunctionPoints: start resident process + * CaseDescription: try to start a resident process */ HWTEST_F(AmsAppLifeCycleTest, StartResidentProcess_001, TestSize.Level1) { @@ -1952,8 +2006,8 @@ HWTEST_F(AmsAppLifeCycleTest, StartResidentProcess_001, TestSize.Level1) * Feature: AMS * Function: AppLifeCycle::StartResidentProcess * SubFunction: NA - * FunctionPoints: NA - * CaseDescription: NA + * FunctionPoints: start resident process + * CaseDescription: try to start a resident process */ HWTEST_F(AmsAppLifeCycleTest, StartResidentProcess_002, TestSize.Level1) { @@ -2004,12 +2058,29 @@ HWTEST_F(AmsAppLifeCycleTest, StartResidentProcess_002, TestSize.Level1) EXPECT_TRUE(recordId == id); } +/* + * Feature: AMS + * Function: AppLifeCycle::StartResidentProcess + * SubFunction: NA + * FunctionPoints: start resident process + * CaseDescription: try to start resident process + */ +HWTEST_F(AmsAppLifeCycleTest, StartResidentProcess_003, TestSize.Level1) +{ + std::vector infos; + serviceInner_->appRunningManager_->ClearAppRunningRecordMap(); + + // EMPTY VECTOR + serviceInner_->StartResidentProcess(infos, -1); + + EXPECT_TRUE(serviceInner_->appRunningManager_->GetAppRunningRecordMap().empty()); +} /* * Feature: AMS * Function: AppLifeCycle::RestartResidentProcess * SubFunction: NA - * FunctionPoints: NA - * CaseDescription: NA + * FunctionPoints: Guaranteed to re-pull + * CaseDescription: Restart the abnormal resident process */ HWTEST_F(AmsAppLifeCycleTest, RestartResidentProcess_001, TestSize.Level1) { @@ -2046,5 +2117,152 @@ HWTEST_F(AmsAppLifeCycleTest, RestartResidentProcess_001, TestSize.Level1) appRecord = serviceInner_->appRunningManager_->CheckAppRunningRecordIsExist(appName, proc, uid, bundleInfo); EXPECT_FALSE(appRecord); } + +/* + * Feature: AMS + * Function: AppLifeCycle::RestartResidentProcess + * SubFunction: NA + * FunctionPoints: Guaranteed to re-pull + * CaseDescription: Restart the abnormal resident process + */ +HWTEST_F(AmsAppLifeCycleTest, RestartResidentProcess_002, TestSize.Level1) +{ + pid_t pid = 123; + sptr token = GetMockToken(); + std::string appName = "KeepAliveApp"; + std::string proc = "KeepAliveApplication"; + int uid = 2100; + + HapModuleInfo hapModuleInfo; + hapModuleInfo.moduleName = "KeepAliveApplication"; + std::vector infos; + BundleInfo info; + info.name = proc; + info.uid = uid; + info.hapModuleInfos.push_back(hapModuleInfo); + + ApplicationInfo appInfo; + appInfo.name = "KeepAliveApp"; + appInfo.bundleName = "KeepAliveApplication"; + appInfo.uid = 2100; + info.applicationInfo = appInfo; + infos.push_back(info); + BundleInfo bundleInfo; + bundleInfo.appId = "com.ohos.test.helloworld_code123"; + + auto appRecord = serviceInner_->appRunningManager_->CheckAppRunningRecordIsExist(appName, proc, uid, bundleInfo); + EXPECT_FALSE(appRecord); + + MockAppSpawnClient *mockClientPtrT = new (std::nothrow) MockAppSpawnClient(); + EXPECT_TRUE(mockClientPtrT); + EXPECT_CALL(*mockClientPtrT, StartProcess(_, _)).Times(1).WillOnce(DoAll(SetArgReferee<1>(pid), Return(ERR_OK))); + + serviceInner_->SetAppSpawnClient(std::unique_ptr(mockClientPtrT)); + + serviceInner_->StartResidentProcess(infos, -1); + + appRecord = serviceInner_->appRunningManager_->CheckAppRunningRecordIsExist(appName, proc, uid, bundleInfo); + EXPECT_TRUE(appRecord); + + // Restart the existing application when there is no abnormality + serviceInner_->RestartResidentProcess(appRecord); + + auto newAppRecord = serviceInner_->appRunningManager_->CheckAppRunningRecordIsExist(appName, proc, uid, bundleInfo); + EXPECT_TRUE(newAppRecord); + EXPECT_TRUE(appRecord == newAppRecord); + EXPECT_TRUE(appRecord->GetRecordId() == newAppRecord->GetRecordId()); +} + +/* + * Feature: AMS + * Function: AppLifeCycle::UpdateConfiguration + * SubFunction: NA + * FunctionPoints: Environmental Change Notification + * CaseDescription: Determine the default value + */ +HWTEST_F(AmsAppLifeCycleTest, UpdateConfiguration_001, TestSize.Level1) +{ + serviceInner_->Init(); + auto configMgr = serviceInner_->GetConfiguration(); + EXPECT_TRUE(configMgr); + auto language = configMgr->GetItem(GlobalConfigurationKey::SYSTEM_LANGUAGE); + // has default value + EXPECT_TRUE(!language.empty()); +} + +/* + * Feature: AMS + * Function: AppLifeCycle::UpdateConfiguration + * SubFunction: NA + * FunctionPoints: Environmental Change Notification + * CaseDescription: Trigger an environment change notification + */ +HWTEST_F(AmsAppLifeCycleTest, UpdateConfiguration_002, TestSize.Level1) +{ + auto testLanguge = "ch-zh"; + auto configUpdate = [testLanguge](const Configuration &config) { + auto l = config.GetItem(GlobalConfigurationKey::SYSTEM_LANGUAGE); + EXPECT_TRUE(testLanguge == l); + }; + + auto testAppPreRecord = + CreateTestApplicationRecord(AbilityState::ABILITY_STATE_FOREGROUND, ApplicationState::APP_STATE_FOREGROUND); + + EXPECT_CALL(*(testAppPreRecord.mockAppScheduler_), ScheduleConfigurationUpdated(_)) + .Times(1) + .WillOnce(testing::Invoke(configUpdate)); + + Configuration config; + config.AddItem(GlobalConfigurationKey::SYSTEM_LANGUAGE, testLanguge); + serviceInner_->UpdateConfiguration(config); +} + +/* + * Feature: AMS + * Function: AppLifeCycle::UpdateConfiguration + * SubFunction: NA + * FunctionPoints: Environmental Change Notification + * CaseDescription: Trigger an environment change notification + */ +HWTEST_F(AmsAppLifeCycleTest, UpdateConfiguration_003, TestSize.Level1) +{ + auto testLanguge = "ch-zh"; + auto configUpdate = [testLanguge](const Configuration &config) { + auto l = config.GetItem(GlobalConfigurationKey::SYSTEM_LANGUAGE); + EXPECT_TRUE(testLanguge == l); + }; + + auto testAppPreRecord = + CreateTestApplicationRecord(AbilityState::ABILITY_STATE_FOREGROUND, ApplicationState::APP_STATE_FOREGROUND); + + EXPECT_CALL(*(testAppPreRecord.mockAppScheduler_), ScheduleConfigurationUpdated(_)) + .Times(1) + .WillOnce(testing::Invoke(configUpdate)); + + Configuration config; + config.AddItem(GlobalConfigurationKey::SYSTEM_LANGUAGE, testLanguge); + serviceInner_->UpdateConfiguration(config); + serviceInner_->UpdateConfiguration(config); +} + +/* + * Feature: AMS + * Function: AppLifeCycle::GetGlobalConfiguration + * SubFunction: initialization + * FunctionPoints: NA + * CaseDescription: Initialize a persistent environment variable object + */ +HWTEST_F(AmsAppLifeCycleTest, GetGlobalConfiguration_001, TestSize.Level1) +{ + auto configMgr = serviceInner_->GetConfiguration(); + EXPECT_TRUE(configMgr); + + auto language = configMgr->GetItem(GlobalConfigurationKey::SYSTEM_LANGUAGE); + EXPECT_TRUE(language.empty()); + + serviceInner_->GetGlobalConfiguration(); + language = configMgr->GetItem(GlobalConfigurationKey::SYSTEM_LANGUAGE); + EXPECT_TRUE(!language.empty()); +} } // namespace AppExecFwk } // namespace OHOS \ No newline at end of file diff --git a/services/appmgr/test/unittest/ams_app_mgr_client_test/ams_app_mgr_client_test.cpp b/services/appmgr/test/unittest/ams_app_mgr_client_test/ams_app_mgr_client_test.cpp index 6b5595f19fd473bbe2446f3502f3a0e73f4a3235..3b4ed4c33ba4a2c02672f9a799ae3049b10341a3 100644 --- a/services/appmgr/test/unittest/ams_app_mgr_client_test/ams_app_mgr_client_test.cpp +++ b/services/appmgr/test/unittest/ams_app_mgr_client_test/ams_app_mgr_client_test.cpp @@ -366,5 +366,68 @@ HWTEST_F(AmsAppMgrClientTest, AppMgrClient_013, TestSize.Level1) EXPECT_EQ(AppMgrResultCode::ERROR_SERVICE_NOT_CONNECTED, client_->KillProcessByAbilityToken(token)); APP_LOGI("ams_app_mgr_client_test_013 end"); } + +/* + * Feature: AppMgrService + * Function: AppMgrClient::ClearUpApplicationData + * SubFunction: ClearUpApplicationData + * FunctionPoints: AppMgrClient ClearUpApplicationData interface + * EnvConditions: Mobile that can run ohos test framework + * CaseDescription: clear the application data. + */ +HWTEST_F(AmsAppMgrClientTest, AppMgrClient_014, TestSize.Level1) +{ + sptr token; + EXPECT_EQ(AppMgrResultCode::RESULT_OK, client_->ConnectAppMgrService()); + EXPECT_CALL(*(static_cast(client_->remote_.GetRefPtr())), ClearUpApplicationData(_)) + .Times(1) + .WillOnce(Return(ERR_NO_MEMORY)); + EXPECT_EQ(AppMgrResultCode::ERROR_SERVICE_NOT_READY, client_->ClearUpApplicationData("com.test")); +} + +/* + * Feature: AppMgrService + * Function: AppMgrClient::ClearUpApplicationData + * SubFunction: ClearUpApplicationData + * FunctionPoints: AppMgrClient ClearUpApplicationData interface + * EnvConditions: Mobile that can run ohos test framework + * CaseDescription: clear the application data. + */ +HWTEST_F(AmsAppMgrClientTest, AppMgrClient_015, TestSize.Level1) +{ + sptr token; + EXPECT_EQ(AppMgrResultCode::RESULT_OK, client_->ConnectAppMgrService()); + sptr appMgr(new MockAppMgrService()); + EXPECT_CALL(*(static_cast(client_->remote_.GetRefPtr())), ClearUpApplicationData(_)) + .Times(1) + .WillOnce(Return(ERR_OK)); + EXPECT_EQ(AppMgrResultCode::RESULT_OK, client_->ClearUpApplicationData("com.test")); +} + +/* + * Feature: AppMgrService + * Function: AppMgrClient::UpdateConfiguration + * SubFunction: RegisterAppStateCallback Function + * FunctionPoints: AppMgrClient UpdateConfiguration interface + * EnvConditions: Mobile that can run ohos test framework + * CaseDescription: Notify app of configuration change + */ +HWTEST_F(AmsAppMgrClientTest, AppMgrClient_016, TestSize.Level1) +{ + APP_LOGI("UpdateConfiguration start"); + EXPECT_EQ(AppMgrResultCode::RESULT_OK, client_->ConnectAppMgrService()); + sptr amsMgrScheduler(new MockAmsMgrScheduler()); + + EXPECT_CALL(*(static_cast(amsMgrScheduler.GetRefPtr())), UpdateConfiguration(_)) + .Times(1); + + EXPECT_CALL(*(static_cast(client_->remote_.GetRefPtr())), GetAmsMgr()) + .Times(1) + .WillOnce(Return(amsMgrScheduler)); + + Configuration config; + EXPECT_EQ(AppMgrResultCode::RESULT_OK, client_->UpdateConfiguration(config)); + APP_LOGI("UpdateConfiguration end"); +} } // namespace AppExecFwk } // namespace OHOS \ No newline at end of file diff --git a/services/appmgr/test/unittest/ams_app_running_record_test/BUILD.gn b/services/appmgr/test/unittest/ams_app_running_record_test/BUILD.gn index 481fb3f8b6aff19ca26bc4b8bb040a18aab463dc..fd1f8d28ded20e39b614cec663a6d43a09636a81 100644 --- a/services/appmgr/test/unittest/ams_app_running_record_test/BUILD.gn +++ b/services/appmgr/test/unittest/ams_app_running_record_test/BUILD.gn @@ -50,6 +50,7 @@ ohos_unittest("AmsAppRunningRecordTest") { cflags += [ "-DBINDER_IPC_32BIT" ] } deps = [ + "${aafwk_path}/interfaces/innerkits/ability_manager:ability_manager", "${aafwk_path}/interfaces/innerkits/app_manager:app_manager", "${appexecfwk_path}/libs/libeventhandler:libeventhandler_target", "${innerkits_path}/uri_permission:uri_permission_mgr", diff --git a/services/appmgr/test/unittest/ams_app_running_record_test/ams_app_running_record_test.cpp b/services/appmgr/test/unittest/ams_app_running_record_test/ams_app_running_record_test.cpp index 5cd8f60743f1004546d2d30b68f69b3e52a0b343..ab16f6c38a12bad595a1febfb8530c8a4d22623f 100644 --- a/services/appmgr/test/unittest/ams_app_running_record_test/ams_app_running_record_test.cpp +++ b/services/appmgr/test/unittest/ams_app_running_record_test/ams_app_running_record_test.cpp @@ -208,12 +208,12 @@ HWTEST_F(AmsAppRunningRecordTest, CreateAppRunningRecord_002, TestSize.Level1) EXPECT_TRUE(service_ != nullptr); // Create sptr token = GetMockToken(); - auto record = service_->CreateAppRunningRecord(GetMockToken(), nullptr, appInfo, abilityInfo, - GetTestProcessName(), bundleInfo, hapModuleInfo, nullptr); + auto record = service_->CreateAppRunningRecord( + GetMockToken(), nullptr, appInfo, abilityInfo, GetTestProcessName(), bundleInfo, hapModuleInfo, nullptr); record->SetUid(1010); // Get - auto record1 = service_->CreateAppRunningRecord(GetMockToken(), nullptr, appInfo, abilityInfo, - GetTestProcessName(), bundleInfo, hapModuleInfo, nullptr); + auto record1 = service_->CreateAppRunningRecord( + GetMockToken(), nullptr, appInfo, abilityInfo, GetTestProcessName(), bundleInfo, hapModuleInfo, nullptr); EXPECT_TRUE(record1 != nullptr); EXPECT_EQ(record1->GetName(), GetTestAppName()); EXPECT_EQ(record1->GetProcessName(), GetTestProcessName()); @@ -242,16 +242,22 @@ HWTEST_F(AmsAppRunningRecordTest, CreateAppRunningRecord_003, TestSize.Level1) HapModuleInfo hapModuleInfo; hapModuleInfo.moduleName = "module789"; EXPECT_TRUE(service_ != nullptr); - auto record = service_->CreateAppRunningRecord(GetMockToken(), nullptr, appInfo, abilityInfo, - GetTestProcessName(), bundleInfo, hapModuleInfo, nullptr); + auto record = service_->CreateAppRunningRecord( + GetMockToken(), nullptr, appInfo, abilityInfo, GetTestProcessName(), bundleInfo, hapModuleInfo, nullptr); record->SetUid(1010); auto anotherAbilityInfo = std::make_shared(); anotherAbilityInfo->name = "Another_ability"; anotherAbilityInfo->applicationInfo.uid = 1010; sptr anotherToken = new (std::nothrow) MockAbilityToken(); - auto record1 = service_->CreateAppRunningRecord(GetMockToken(), anotherToken, appInfo, anotherAbilityInfo, - GetTestProcessName(), bundleInfo, hapModuleInfo, nullptr); + auto record1 = service_->CreateAppRunningRecord(GetMockToken(), + anotherToken, + appInfo, + anotherAbilityInfo, + GetTestProcessName(), + bundleInfo, + hapModuleInfo, + nullptr); EXPECT_EQ(record1->GetName(), GetTestAppName()); EXPECT_EQ(record1->GetProcessName(), GetTestProcessName()); @@ -280,8 +286,8 @@ HWTEST_F(AmsAppRunningRecordTest, CreateAppRunningRecord_004, TestSize.Level1) hapModuleInfo.moduleName = "module789"; EXPECT_TRUE(service_ != nullptr); // Create - auto record = service_->CreateAppRunningRecord(GetMockToken(), nullptr, nullptr, abilityInfo, - GetTestProcessName(), bundleInfo, hapModuleInfo, nullptr); + auto record = service_->CreateAppRunningRecord( + GetMockToken(), nullptr, nullptr, abilityInfo, GetTestProcessName(), bundleInfo, hapModuleInfo, nullptr); EXPECT_TRUE(record == nullptr); } @@ -801,8 +807,8 @@ HWTEST_F(AmsAppRunningRecordTest, LaunchAbilityForApp_002, TestSize.Level1) const int EXPECT_ABILITY_LAUNCH_TIME = 3; EXPECT_TRUE(service_ != nullptr); - std::shared_ptr record = service_->CreateAppRunningRecord(GetMockToken(), nullptr, appInfo, - abilityInfo, GetTestProcessName(), bundleInfo, hapModuleInfo, nullptr); + std::shared_ptr record = service_->CreateAppRunningRecord( + GetMockToken(), nullptr, appInfo, abilityInfo, GetTestProcessName(), bundleInfo, hapModuleInfo, nullptr); auto abilityRecord = record->GetAbilityRunningRecord(GetTestAbilityName()); EXPECT_TRUE(abilityRecord != nullptr); @@ -851,8 +857,8 @@ HWTEST_F(AmsAppRunningRecordTest, LaunchAbilityForApp_003, TestSize.Level1) hapModuleInfo.moduleName = "module789"; EXPECT_TRUE(service_ != nullptr); - std::shared_ptr record = service_->CreateAppRunningRecord(GetMockToken(), nullptr, appInfo, - abilityInfo, GetTestProcessName(), bundleInfo, hapModuleInfo, nullptr); + std::shared_ptr record = service_->CreateAppRunningRecord( + GetMockToken(), nullptr, appInfo, abilityInfo, GetTestProcessName(), bundleInfo, hapModuleInfo, nullptr); record->SetState(ApplicationState::APP_STATE_READY); record->SetApplicationClient(GetMockedAppSchedulerClient()); auto abilityRecord = record->GetAbilityRunningRecord(GetTestAbilityName()); @@ -897,8 +903,8 @@ HWTEST_F(AmsAppRunningRecordTest, LaunchAbilityForApp_004, TestSize.Level1) hapModuleInfo.moduleName = "module789"; EXPECT_TRUE(service_ != nullptr); - std::shared_ptr record = service_->CreateAppRunningRecord(GetMockToken(), nullptr, appInfo, - abilityInfo, GetTestProcessName(), bundleInfo, hapModuleInfo, nullptr); + std::shared_ptr record = service_->CreateAppRunningRecord( + GetMockToken(), nullptr, appInfo, abilityInfo, GetTestProcessName(), bundleInfo, hapModuleInfo, nullptr); auto abilityRecord = record->GetAbilityRunningRecord(GetTestAbilityName()); EXPECT_TRUE(abilityRecord != nullptr); @@ -945,8 +951,8 @@ HWTEST_F(AmsAppRunningRecordTest, LaunchAbilityForApp_005, TestSize.Level1) const int EXPECT_ABILITY_LAUNCH_TIME = 2; EXPECT_TRUE(service_ != nullptr); - std::shared_ptr record = service_->CreateAppRunningRecord(GetMockToken(), nullptr, appInfo, - abilityInfo, GetTestProcessName(), bundleInfo, hapModuleInfo, nullptr); + std::shared_ptr record = service_->CreateAppRunningRecord( + GetMockToken(), nullptr, appInfo, abilityInfo, GetTestProcessName(), bundleInfo, hapModuleInfo, nullptr); auto abilityRecord = record->GetAbilityRunningRecord(GetTestAbilityName()); EXPECT_TRUE(abilityRecord != nullptr); @@ -1014,8 +1020,8 @@ HWTEST_F(AmsAppRunningRecordTest, TerminateAbility_002, TestSize.Level1) appInfo->name = GetTestAppName(); appInfo->bundleName = GetTestAppName(); - std::shared_ptr record = service_->CreateAppRunningRecord(GetMockToken(), nullptr, appInfo, - abilityInfo, GetTestProcessName(), bundleInfo, hapModuleInfo, nullptr); + std::shared_ptr record = service_->CreateAppRunningRecord( + GetMockToken(), nullptr, appInfo, abilityInfo, GetTestProcessName(), bundleInfo, hapModuleInfo, nullptr); auto abilityRecord = record->GetAbilityRunningRecord(GetTestAbilityName()); EXPECT_TRUE(abilityRecord != nullptr); @@ -1067,8 +1073,8 @@ HWTEST_F(AmsAppRunningRecordTest, GetAbilityRunningRecord_001, TestSize.Level1) appInfo->name = GetTestAppName(); appInfo->bundleName = GetTestAppName(); - std::shared_ptr record = service_->CreateAppRunningRecord(GetMockToken(), nullptr, appInfo, - abilityInfo, GetTestProcessName(), bundleInfo, hapModuleInfo, nullptr); + std::shared_ptr record = service_->CreateAppRunningRecord( + GetMockToken(), nullptr, appInfo, abilityInfo, GetTestProcessName(), bundleInfo, hapModuleInfo, nullptr); auto abilityRecord = record->GetAbilityRunningRecord(GetTestAbilityName()); EXPECT_TRUE(abilityRecord != nullptr); @@ -1118,8 +1124,8 @@ HWTEST_F(AmsAppRunningRecordTest, SetUid_GetUid_001, TestSize.Level1) HapModuleInfo hapModuleInfo; hapModuleInfo.moduleName = "module789"; EXPECT_TRUE(service_ != nullptr); - auto record = service_->CreateAppRunningRecord(GetMockToken(), nullptr, appInfo, abilityInfo, - GetTestProcessName(), bundleInfo, hapModuleInfo, nullptr); + auto record = service_->CreateAppRunningRecord( + GetMockToken(), nullptr, appInfo, abilityInfo, GetTestProcessName(), bundleInfo, hapModuleInfo, nullptr); EXPECT_TRUE(record != nullptr); record->SetUid(102); @@ -1153,8 +1159,8 @@ HWTEST_F(AmsAppRunningRecordTest, OnAbilityStateChanged_001, TestSize.Level1) HapModuleInfo hapModuleInfo; hapModuleInfo.moduleName = "module789"; - std::shared_ptr record = service_->CreateAppRunningRecord(GetMockToken(), nullptr, appInfo, - abilityInfo, GetTestProcessName(), bundleInfo, hapModuleInfo, nullptr); + std::shared_ptr record = service_->CreateAppRunningRecord( + GetMockToken(), nullptr, appInfo, abilityInfo, GetTestProcessName(), bundleInfo, hapModuleInfo, nullptr); auto moduleRecord = record->GetModuleRecordByModuleName(appInfo->bundleName, hapModuleInfo.moduleName); EXPECT_TRUE(moduleRecord != nullptr); auto abilityRecord = record->GetAbilityRunningRecord(GetTestAbilityName()); @@ -1383,7 +1389,6 @@ HWTEST_F(AmsAppRunningRecordTest, GetAbilities_002, TestSize.Level1) * EnvConditions: Mobile that can run ohos test framework * CaseDescription: Remove ModuleRecord */ - HWTEST_F(AmsAppRunningRecordTest, RemoveModuleRecord_001, TestSize.Level1) { auto abilityInfo = std::make_shared(); @@ -1417,5 +1422,255 @@ HWTEST_F(AmsAppRunningRecordTest, RemoveModuleRecord_001, TestSize.Level1) auto moduleRecordList = record->GetAllModuleRecord(); EXPECT_TRUE(moduleRecordList.size() == 1); } + +/* + * Feature: AbilityManagerService + * Function: UpdateConfiguration + * SubFunction: NA + * FunctionPoints: Environmental Change Notification + * EnvConditions: NA + * CaseDescription: Make an environment object and update + */ +HWTEST_F(AmsAppRunningRecordTest, UpdateConfiguration_001, TestSize.Level1) +{ + auto testLanguge = std::string("ch-zh"); + auto configUpdate = [testLanguge](const Configuration &config) { + auto l = config.GetItem(GlobalConfigurationKey::SYSTEM_LANGUAGE); + EXPECT_TRUE(testLanguge == l); + }; + + Configuration config; + config.AddItem(GlobalConfigurationKey::SYSTEM_LANGUAGE, testLanguge); + auto record = GetTestAppRunningRecord(); + EXPECT_CALL(*mockAppSchedulerClient_, ScheduleConfigurationUpdated(_)) + .Times(1) + .WillOnce(testing::Invoke(configUpdate)); + + record->UpdateConfiguration(config); +} + +/* + * Feature: AbilityManagerService + * Function: UpdateConfiguration + * SubFunction: NA + * FunctionPoints: Environmental Change Notification + * EnvConditions: NA + * CaseDescription: Make an environment object and update + */ +HWTEST_F(AmsAppRunningRecordTest, UpdateConfiguration_002, TestSize.Level1) +{ + auto test = std::string("colour"); + auto configUpdate = [test](const Configuration &config) { + auto l = config.GetItem(GlobalConfigurationKey::SYSTEM_COLORMODE); + EXPECT_TRUE(test == l); + }; + + Configuration config; + config.AddItem(GlobalConfigurationKey::SYSTEM_COLORMODE, test); + auto record = GetTestAppRunningRecord(); + EXPECT_CALL(*mockAppSchedulerClient_, ScheduleConfigurationUpdated(_)) + .Times(1) + .WillOnce(testing::Invoke(configUpdate)); + + record->UpdateConfiguration(config); +} + +/* + * Feature: AMS + * Function: SetSpecifiedAbilityFlagAndWant + * SubFunction: SetSpecifiedAbilityFlagAndWant + * FunctionPoints: check params + * EnvConditions: Mobile that can run ohos test framework + * CaseDescription: Remove ModuleRecord + */ +HWTEST_F(AmsAppRunningRecordTest, SetSpecifiedAbilityFlagAndWant_001, TestSize.Level1) +{ + auto appInfo = std::make_shared(); + appInfo->name = GetTestAppName(); + appInfo->bundleName = GetTestAppName(); + int32_t recordId = 11; + std::string processName = "processName"; + auto record = std::make_shared(appInfo, recordId, processName); + + bool flag = true; + AAFwk::Want want; + std::string moduleName = "module123"; + record->SetSpecifiedAbilityFlagAndWant(flag, want, moduleName); + EXPECT_TRUE(record->isSpecifiedAbility_ == flag); + EXPECT_TRUE(record->moduleName_ == moduleName); +} + +/* + * Feature: AMS + * Function: IsStartSpecifiedAbility + * SubFunction: IsStartSpecifiedAbility + * FunctionPoints: check params + * EnvConditions: Mobile that can run ohos test framework + * CaseDescription: Remove ModuleRecord + */ +HWTEST_F(AmsAppRunningRecordTest, IsStartSpecifiedAbility_001, TestSize.Level1) +{ + auto appInfo = std::make_shared(); + appInfo->name = GetTestAppName(); + appInfo->bundleName = GetTestAppName(); + int32_t recordId = 11; + std::string processName = "processName"; + auto record = std::make_shared(appInfo, recordId, processName); + + bool flag = true; + AAFwk::Want want; + std::string moduleName = "module123"; + record->SetSpecifiedAbilityFlagAndWant(flag, want, moduleName); + EXPECT_TRUE(record->IsStartSpecifiedAbility() == flag); +} + +/* + * Feature: AMS + * Function: GetSpecifiedWant + * SubFunction: GetSpecifiedWant + * FunctionPoints: check params + * EnvConditions: Mobile that can run ohos test framework + * CaseDescription: Remove ModuleRecord + */ +HWTEST_F(AmsAppRunningRecordTest, GetSpecifiedWant_001, TestSize.Level1) +{ + auto appInfo = std::make_shared(); + appInfo->name = GetTestAppName(); + appInfo->bundleName = GetTestAppName(); + int32_t recordId = 11; + std::string processName = "processName"; + auto record = std::make_shared(appInfo, recordId, processName); + + bool flag = true; + Want want; + want.SetElementName("DemoDeviceId", "DemoBundleName", "DemoAbilityName"); + std::string moduleName = "module123"; + record->SetSpecifiedAbilityFlagAndWant(flag, want, moduleName); + EXPECT_TRUE(record->GetSpecifiedWant().GetBundle() == want.GetBundle()); +} + +/* + * Feature: AMS + * Function: RegisterStartSpecifiedAbilityResponse + * SubFunction: RegisterStartSpecifiedAbilityResponse + * FunctionPoints: check params + * EnvConditions: Mobile that can run ohos test framework + * CaseDescription: Remove ModuleRecord + */ +HWTEST_F(AmsAppRunningRecordTest, RegisterStartSpecifiedAbilityResponse_001, TestSize.Level1) +{ + sptr response; + service_->RegisterStartSpecifiedAbilityResponse(response); + EXPECT_TRUE(service_->startSpecifiedAbilityResponse_ == response); +} + +/* + * Feature: AMS + * Function: StartSpecifiedAbility + * SubFunction: StartSpecifiedAbility + * FunctionPoints: check params + * EnvConditions: Mobile that can run ohos test framework + * CaseDescription: Remove ModuleRecord + */ +HWTEST_F(AmsAppRunningRecordTest, StartSpecifiedAbility_001, TestSize.Level1) +{ + auto abilityInfo = std::make_shared(); + abilityInfo->name = GetTestAbilityName(); + auto appInfo = std::make_shared(); + appInfo->name = GetTestAppName(); + BundleInfo bundleInfo; + bundleInfo.appId = "com.ohos.test.helloworld_code123"; + bundleInfo.jointUserId = "joint456"; + HapModuleInfo hapModuleInfo; + hapModuleInfo.moduleName = "module789"; + EXPECT_TRUE(service_ != nullptr); + auto record = service_->CreateAppRunningRecord( + GetMockToken(), nullptr, appInfo, abilityInfo, GetTestProcessName(), bundleInfo, hapModuleInfo, nullptr); + + EXPECT_TRUE(record != nullptr); + EXPECT_EQ(record->GetName(), GetTestAppName()); + EXPECT_EQ(record->GetProcessName(), GetTestProcessName()); + + Want want; + want.SetElementName("DemoDeviceIdB", "DemoBundleNameB", "DemoAbilityNameB"); + service_->StartSpecifiedAbility(want, *abilityInfo); + + auto modules = record->GetModules(); + EXPECT_EQ(modules.size(), 1); +} + +/* + * Feature: AMS + * Function: StartSpecifiedAbility + * SubFunction: StartSpecifiedAbility + * FunctionPoints: check params + * EnvConditions: Mobile that can run ohos test framework + * CaseDescription: Remove ModuleRecord + */ +HWTEST_F(AmsAppRunningRecordTest, StartSpecifiedAbility_002, TestSize.Level1) +{ + auto abilityInfo = std::make_shared(); + abilityInfo->name = GetTestAbilityName(); + auto appInfo = std::make_shared(); + appInfo->name = GetTestAppName(); + BundleInfo bundleInfo; + bundleInfo.appId = "com.ohos.test.helloworld_code123"; + bundleInfo.jointUserId = "joint456"; + HapModuleInfo hapModuleInfo; + hapModuleInfo.moduleName = "module789"; + EXPECT_TRUE(service_ != nullptr); + auto record = service_->CreateAppRunningRecord( + GetMockToken(), nullptr, appInfo, abilityInfo, GetTestProcessName(), bundleInfo, hapModuleInfo, nullptr); + + EXPECT_TRUE(record != nullptr); + EXPECT_EQ(record->GetName(), GetTestAppName()); + EXPECT_EQ(record->GetProcessName(), GetTestProcessName()); + + auto abilityInfo1 = std::make_shared(); + abilityInfo1->name = "test_ability_name_2"; + Want want; + want.SetElementName("DemoDeviceId", "DemoBundleName", "DemoAbilityName"); + service_->StartSpecifiedAbility(want, *abilityInfo1); + + auto modules = record->GetModules(); + EXPECT_EQ(modules.size(), 1); +} + +/* + * Feature: AMS + * Function: LaunchApplication + * SubFunction: LaunchApplication + * FunctionPoints: check params + * EnvConditions: Mobile that can run ohos test framework + * CaseDescription: Remove ModuleRecord + */ +HWTEST_F(AmsAppRunningRecordTest, Specified_LaunchApplication_001, TestSize.Level1) +{ + auto abilityInfo = std::make_shared(); + abilityInfo->name = GetTestAbilityName(); + abilityInfo->applicationName = GetTestAppName(); + abilityInfo->process = GetTestAppName(); + abilityInfo->applicationInfo.bundleName = GetTestAppName(); + + auto appInfo = std::make_shared(); + appInfo->bundleName = GetTestAppName(); + BundleInfo bundleInfo; + bundleInfo.appId = "com.ohos.test.helloworld_code123"; + bundleInfo.jointUserId = "joint456"; + HapModuleInfo hapModuleInfo; + hapModuleInfo.moduleName = "module789"; + + EXPECT_TRUE(service_ != nullptr); + std::shared_ptr record = service_->CreateAppRunningRecord( + GetMockToken(), nullptr, appInfo, abilityInfo, GetTestProcessName(), bundleInfo, hapModuleInfo, nullptr); + auto abilityRecord = record->GetAbilityRunningRecord(GetTestAbilityName()); + EXPECT_TRUE(abilityRecord != nullptr); + + record->SetApplicationClient(GetMockedAppSchedulerClient()); + record->isSpecifiedAbility_ = true; + service_->LaunchApplication(record); + auto ability = record->GetAbilityRunningRecord(GetTestAbilityName()); + EXPECT_TRUE(ability->GetState() != AbilityState::ABILITY_STATE_READY); +} } // namespace AppExecFwk } // namespace OHOS diff --git a/services/appmgr/test/unittest/ams_mgr_kill_process_test/BUILD.gn b/services/appmgr/test/unittest/ams_mgr_kill_process_test/BUILD.gn new file mode 100644 index 0000000000000000000000000000000000000000..fa89374231aae07e555b5ecafa280dc4ac7f7181 --- /dev/null +++ b/services/appmgr/test/unittest/ams_mgr_kill_process_test/BUILD.gn @@ -0,0 +1,66 @@ +# Copyright (c) 2021 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 +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +import("//build/test.gni") +import("//foundation/aafwk/standard/aafwk.gni") + +module_output_path = "ability_runtime/appmgrservice" + +mock_sources = + [ "${aafwk_path}/services/appmgr/test/mock/src/mock_bundle_manager.cpp" ] + +ohos_unittest("AmsMgrKillProcessTest") { + module_out_path = module_output_path + + include_dirs = [ "${aafwk_path}/services/appmgr/test/mock/include" ] + + sources = [ "ams_mgr_kill_process_test.cpp" ] + sources += mock_sources + + configs = [ + "${aafwk_path}/frameworks/kits/ability/native:ability_config", + "${aafwk_path}/services/appmgr:appmgr_config", + ] + + cflags = [] + if (target_cpu == "arm") { + cflags += [ "-DBINDER_IPC_32BIT" ] + } + + deps = [ + "${aafwk_path}/frameworks/kits/appkit:appkit_native", + "${aafwk_path}/interfaces/innerkits/app_manager:app_manager", + "${aafwk_path}/services/appmgr:libams", + "${appexecfwk_path}/common:libappexecfwk_common", + "//third_party/googletest:gmock_main", + "//third_party/googletest:gtest_main", + ] + + external_deps = [ + "ability_base:want", + "bundle_framework:appexecfwk_base", + "bundle_framework:appexecfwk_core", + "eventhandler:libeventhandler", + "hiviewdfx_hilog_native:libhilog", + "ipc:ipc_core", + "safwk:system_ability_fwk", + "samgr_standard:samgr_proxy", + "utils_base:utils", + ] +} + +group("unittest") { + testonly = true + + deps = [ ":AmsMgrKillProcessTest" ] +} diff --git a/services/appmgr/test/unittest/ams_mgr_kill_process_test/ams_mgr_kill_process_test.cpp b/services/appmgr/test/unittest/ams_mgr_kill_process_test/ams_mgr_kill_process_test.cpp new file mode 100644 index 0000000000000000000000000000000000000000..e85f21b5b074d253542f8291c83f7601215d8176 --- /dev/null +++ b/services/appmgr/test/unittest/ams_mgr_kill_process_test/ams_mgr_kill_process_test.cpp @@ -0,0 +1,121 @@ +/* + * Copyright (c) 2021 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 + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include + +#define private public +#include "ams_mgr_scheduler.h" +#undef private +#include "app_log_wrapper.h" +#include "app_mgr_interface.h" +#include "mock_bundle_manager.h" +#include "system_ability_definition.h" +#include "sys_mgr_client.h" + +using namespace testing::ext; +using namespace OHOS; +using namespace OHOS::AppExecFwk; + +namespace { +const std::string STRING_BUNDLE_NAME = "com.example.bundle"; + +constexpr int ACCOUNT_ID = 100; +} // namespace + +class AmsMgrKillProcessTest : public testing::Test { +public: + static void SetUpTestCase(void); + static void TearDownTestCase(void); + void SetUp(void) override; + void TearDown(void) override; + + std::shared_ptr GetAppMgrServiceInner(void); + sptr GetAppMgrProxy(void); +}; + +void AmsMgrKillProcessTest::SetUpTestCase(void) +{} + +void AmsMgrKillProcessTest::TearDownTestCase(void) +{} + +void AmsMgrKillProcessTest::SetUp(void) +{} + +void AmsMgrKillProcessTest::TearDown(void) +{} + +std::shared_ptr AmsMgrKillProcessTest::GetAppMgrServiceInner(void) +{ + auto appMgrServiceInner = std::make_shared(); + EXPECT_NE(appMgrServiceInner, nullptr); + + sptr bundleMgr = new BundleMgrService(); + appMgrServiceInner->remoteClientManager_->SetBundleManager(bundleMgr); + + return appMgrServiceInner; +} + +sptr AmsMgrKillProcessTest::GetAppMgrProxy(void) +{ + auto instance = DelayedSingleton::GetInstance(); + EXPECT_NE(instance, nullptr); + + auto object = instance->GetSystemAbility(APP_MGR_SERVICE_ID); + EXPECT_NE(object, nullptr); + + auto proxy = iface_cast(object); + EXPECT_NE(proxy, nullptr); + + return proxy; +} + +/* + * Feature: AppMgrServiceInner + * Function: KillApplicationByUserId + * SubFunction: NA + * FunctionPoints:Kill process + * EnvConditions: NA + * CaseDescription: creat AppMgrServiceInner object, call function. + */ +HWTEST_F(AmsMgrKillProcessTest, KillProcess_0100, TestSize.Level0) +{ + APP_LOGI("AmsMgrKillProcessTest_KillProcess_0100"); + + auto appMgrServiceInner = GetAppMgrServiceInner(); + EXPECT_NE(appMgrServiceInner, nullptr); + + ErrCode result = appMgrServiceInner->KillApplicationByUserId(STRING_BUNDLE_NAME, ACCOUNT_ID); + EXPECT_EQ(result, ERR_OK); +} + +/* + * Feature: AppMgrProxy + * Function: KillApplicationByUserId + * SubFunction: NA + * FunctionPoints:Kill process + * EnvConditions: NA + * CaseDescription: creat AppMgrProxy object, call function. + */ +HWTEST_F(AmsMgrKillProcessTest, KillProcess_0200, TestSize.Level0) +{ + APP_LOGI("AmsMgrKillProcessTest_KillProcess_0200"); + + auto proxy = GetAppMgrProxy(); + EXPECT_NE(proxy, nullptr); + + ErrCode result = proxy->GetAmsMgr()->KillProcessWithAccount(STRING_BUNDLE_NAME, ACCOUNT_ID); + EXPECT_EQ(result, ERR_OK); +} diff --git a/services/appmgr/test/unittest/ams_service_load_ability_process_test/ams_service_load_ability_process_test.cpp b/services/appmgr/test/unittest/ams_service_load_ability_process_test/ams_service_load_ability_process_test.cpp index 1790a7297bc63ca93534f528a396aa09caaec158..e5a0e354bc0b1f22872f9f2bce8104d3ca10bf6b 100644 --- a/services/appmgr/test/unittest/ams_service_load_ability_process_test/ams_service_load_ability_process_test.cpp +++ b/services/appmgr/test/unittest/ams_service_load_ability_process_test/ams_service_load_ability_process_test.cpp @@ -216,26 +216,18 @@ HWTEST_F(AmsServiceLoadAbilityProcessTest, LoadAbility_002, TestSize.Level1) appInfo2->bundleName = "com.ohos.test.special"; const pid_t PID2 = 2234; - APP_LOGI("xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"); StartLoadAbility(token2, token, abilityInfo2, appInfo2, PID2); - APP_LOGI("00000000000000000000000000000000000000"); const uint32_t EXPECT_MAP_SIZE = 2; EXPECT_EQ(recordMap.size(), EXPECT_MAP_SIZE); - APP_LOGI("11111111111111111111111111111111111111111"); auto record2 = service_->appRunningManager_->CheckAppRunningRecordIsExist( appInfo2->name, "com.ohos.test.special", appInfo2->uid, bundleInfo); - APP_LOGI("22222222222222222222222222222222222222"); EXPECT_NE(record2, nullptr); CHECK_POINTER_IS_NULLPTR(record2); - APP_LOGI("3333333333333333333333333333333333333"); EXPECT_EQ(record2->GetState(), ApplicationState::APP_STATE_CREATE); - APP_LOGI("444444444444444444444444444444444444444444444"); auto abilityRecord2 = record2->GetAbilityRunningRecordByToken(token2); - APP_LOGI("5555555555555555555555555555555555555"); EXPECT_NE(abilityRecord2, nullptr); CHECK_POINTER_IS_NULLPTR(abilityRecord2); - APP_LOGI("666666666666666666666666666666666"); EXPECT_EQ(abilityRecord2->GetState(), AbilityState::ABILITY_STATE_CREATE); APP_LOGI("AmsServiceLoadAbilityProcessTest LoadAbility_002 end"); } diff --git a/services/appmgr/test/unittest/app_running_processes_info_test/BUILD.gn b/services/appmgr/test/unittest/app_running_processes_info_test/BUILD.gn new file mode 100644 index 0000000000000000000000000000000000000000..4cb46f2197e37e53757724958046a169111cd7b6 --- /dev/null +++ b/services/appmgr/test/unittest/app_running_processes_info_test/BUILD.gn @@ -0,0 +1,75 @@ +# Copyright (c) 2021 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 +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +import("//build/test.gni") +import("//foundation/aafwk/standard/aafwk.gni") + +module_output_path = "ability_runtime/appmgrservice" + +ohos_unittest("AppRunningProcessesInfoTest") { + module_out_path = module_output_path + + include_dirs = [ + "//foundation/aafwk/standard/interfaces/innerkits/base/include", + "//foundation/aafwk/standard/interfaces/innerkits/want/include", + "//foundation/appexecfwk/standard/interfaces/innerkits/appexecfwk_core/include", + "//foundation/distributedschedule/dmsfwk/services/dtbschedmgr/include", + "//foundation/distributedschedule/samgr/interfaces/innerkits/samgr_proxy/include", + "//foundation/distributedschedule/samgr/adapter/interfaces/innerkits/include/", + "//foundation/aafwk/standard/interfaces/innerkits/want/include/ohos/aafwk/content", + "//base/account/os_account/frameworks/common/database/include", + "//base/account/os_account/frameworks/common/account_error/include", + ] + + sources = [ + "${services_path}/appmgr/src/ability_running_record.cpp", + "${services_path}/appmgr/src/app_mgr_service_inner.cpp", + "${services_path}/appmgr/src/app_running_record.cpp", + "${services_path}/appmgr/src/app_spawn_client.cpp", + "${services_path}/appmgr/src/app_spawn_msg_wrapper.cpp", + "${services_path}/appmgr/src/app_spawn_socket.cpp", + "${services_path}/appmgr/test/mock/src/mock_bundle_manager.cpp", + ] + + sources += [ "app_running_processes_info_test.cpp" ] + + configs = [ "${appexecfwk_path}/libs/libeventhandler:libeventhandler_config" ] + cflags = [] + if (target_cpu == "arm") { + cflags += [ "-DBINDER_IPC_32BIT" ] + } + deps = [ + "${aafwk_path}/interfaces/innerkits/ability_manager:ability_manager", + "${aafwk_path}/interfaces/innerkits/app_manager:app_manager", + "${appexecfwk_path}/libs/libeventhandler:libeventhandler_target", + "${innerkits_path}/uri_permission:uri_permission_mgr", + "${services_path}/appmgr/test:appmgr_test_source", + "//base/account/os_account/frameworks/osaccount/native:os_account_innerkits", + "//foundation/aafwk/standard/services/appmgr:libams", + "//foundation/appexecfwk/standard/interfaces/innerkits/appexecfwk_core:appexecfwk_core", + ] + + external_deps = [ + "appspawn:appspawn_socket_client", + "bytrace_standard:bytrace_core", + "hisysevent_native:libhisysevent", + "hiviewdfx_hilog_native:libhilog", + "ipc:ipc_core", + ] +} + +group("unittest") { + testonly = true + + deps = [ ":AppRunningProcessesInfoTest" ] +} diff --git a/services/appmgr/test/unittest/app_running_processes_info_test/app_running_processes_info_test.cpp b/services/appmgr/test/unittest/app_running_processes_info_test/app_running_processes_info_test.cpp new file mode 100644 index 0000000000000000000000000000000000000000..0785dcac996613aee9dd1414e0920773a871c70d --- /dev/null +++ b/services/appmgr/test/unittest/app_running_processes_info_test/app_running_processes_info_test.cpp @@ -0,0 +1,365 @@ +/* + * Copyright (c) 2021 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 + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#define private public +#include "app_mgr_service_inner.h" +#undef private + +#include +#include +#include "iremote_object.h" +#include "refbase.h" +#include "application_info.h" +#include "app_log_wrapper.h" +#include "app_record_id.h" +#include "app_scheduler_host.h" +#include "ability_info.h" +#include "ability_running_record.h" +#include "mock_app_scheduler.h" +#include "mock_ability_token.h" +#include "mock_app_spawn_client.h" +#include "mock_app_mgr_service_inner.h" +#include "mock_iapp_state_callback.h" +#include "mock_bundle_manager.h" +#include "mock_application.h" + +using namespace testing::ext; +using OHOS::iface_cast; +using OHOS::IRemoteObject; +using OHOS::sptr; +using testing::_; +using testing::Invoke; +using testing::InvokeWithoutArgs; +using testing::Return; +using testing::SetArgReferee; + +namespace OHOS { +namespace AppExecFwk { +class AppRunningProcessesInfoTest : public testing::Test { +public: + static void SetUpTestCase(); + static void TearDownTestCase(); + void SetUp(); + void TearDown(); + +protected: + static const std::string GetTestProcessName() + { + return "com.ohos.test.helloworld"; + } + static const std::string GetTestAppName() + { + return "com.ohos.test.helloworld"; + } + static const std::string GetTestAbilityName() + { + return "test_ability_name"; + } + static int GetTestUid() + { + // a valid inner uid value which is not border value. + const static int VALID_UID_VALUE = 1010; + return VALID_UID_VALUE; + } + + std::shared_ptr GetTestAppRunningRecord(); + sptr GetMockedAppSchedulerClient() const; + std::shared_ptr StartLoadAbility(const sptr &token, + const std::shared_ptr &abilityInfo, const std::shared_ptr &appInfo, + const pid_t newPid) const; + sptr GetMockToken() const + { + return mock_token_; + } + +protected: + std::shared_ptr testAbilityRecord_; + sptr client_; + sptr mockAppSchedulerClient_; + std::shared_ptr testAppRecord_; + std::unique_ptr service_; + sptr mock_token_; + sptr mockBundleMgr; +}; + +void AppRunningProcessesInfoTest::SetUpTestCase() +{} + +void AppRunningProcessesInfoTest::TearDownTestCase() +{} + +void AppRunningProcessesInfoTest::SetUp() +{ + mockAppSchedulerClient_ = new (std::nothrow) MockAppScheduler(); + service_.reset(new (std::nothrow) AppMgrServiceInner()); + mock_token_ = new (std::nothrow) MockAbilityToken(); + client_ = iface_cast(mockAppSchedulerClient_.GetRefPtr()); + mockBundleMgr = new (std::nothrow) BundleMgrService(); + service_->SetBundleManager(mockBundleMgr); +} + +void AppRunningProcessesInfoTest::TearDown() +{ + testAbilityRecord_.reset(); + testAppRecord_.reset(); +} + +sptr AppRunningProcessesInfoTest::GetMockedAppSchedulerClient() const +{ + if (client_) { + return client_; + } + return nullptr; +} + +std::shared_ptr AppRunningProcessesInfoTest::GetTestAppRunningRecord() +{ + if (!testAppRecord_) { + auto appInfo = std::make_shared(); + appInfo->name = GetTestAppName(); + testAppRecord_ = std::make_shared(appInfo, AppRecordId::Create(), GetTestProcessName()); + testAppRecord_->SetApplicationClient(GetMockedAppSchedulerClient()); + auto abilityInfo = std::make_shared(); + abilityInfo->name = GetTestAbilityName(); + HapModuleInfo hapModuleInfo; + hapModuleInfo.moduleName = "module789"; + testAppRecord_->AddModule(appInfo, abilityInfo, GetMockToken(), hapModuleInfo, nullptr); + } + return testAppRecord_; +} + +std::shared_ptr AppRunningProcessesInfoTest::StartLoadAbility(const sptr &token, + const std::shared_ptr &abilityInfo, const std::shared_ptr &appInfo, + const pid_t newPid) const +{ + std::shared_ptr mockClientPtr = std::make_shared(); + service_->SetAppSpawnClient(mockClientPtr); + EXPECT_CALL(*mockClientPtr, StartProcess(_, _)).Times(1).WillOnce(DoAll(SetArgReferee<1>(newPid), Return(ERR_OK))); + + service_->LoadAbility(token, nullptr, abilityInfo, appInfo, nullptr); + + BundleInfo bundleInfo; + bundleInfo.appId = "com.ohos.test.helloworld_code123"; + + auto record = service_->appRunningManager_->CheckAppRunningRecordIsExist( + appInfo->name, GetTestProcessName(), appInfo->uid, bundleInfo); + + EXPECT_TRUE(record); + auto clent = GetMockedAppSchedulerClient(); + record->SetApplicationClient(clent); + EXPECT_EQ(record->GetPriorityObject()->GetPid(), newPid); + EXPECT_NE(record->GetApplicationClient(), nullptr); + return record; +} + +/* + * Feature: AppMgrServiceInner + * Function: GetRunningProcessInfoByToken + * SubFunction: NA + * FunctionPoints: get running process info by token. + * EnvConditions: NA + * CaseDescription: creat apprunningrecord, set record state, call query function. + */ +HWTEST_F(AppRunningProcessesInfoTest, UpdateAppRunningRecord_001, TestSize.Level1) +{ + auto abilityInfo = std::make_shared(); + abilityInfo->name = GetTestAbilityName(); + auto appInfo = std::make_shared(); + appInfo->name = GetTestAppName(); + BundleInfo bundleInfo; + bundleInfo.appId = "com.ohos.test.helloworld_code123"; + bundleInfo.jointUserId = "joint456"; + HapModuleInfo hapModuleInfo; + hapModuleInfo.moduleName = "module789"; + EXPECT_TRUE(service_ != nullptr); + auto record = service_->CreateAppRunningRecord( + GetMockToken(), nullptr, appInfo, abilityInfo, GetTestProcessName(), bundleInfo, hapModuleInfo, nullptr); + EXPECT_TRUE(record != nullptr); + record->SetState(ApplicationState::APP_STATE_FOREGROUND); + record->SetApplicationClient(GetMockedAppSchedulerClient()); + RunningProcessInfo info; + service_->GetRunningProcessInfoByToken(GetMockToken(), info); + EXPECT_TRUE(info.processName_ == GetTestProcessName()); +} + +/* + * Feature: AppMgrServiceInner + * Function: GetAllRunningProcesses + * SubFunction: NA + * FunctionPoints: get running process info by token. + * EnvConditions: NA + * CaseDescription: creat apprunningrecord, set record state, call query function. + */ +HWTEST_F(AppRunningProcessesInfoTest, UpdateAppRunningRecord_002, TestSize.Level1) +{ + auto abilityInfo = std::make_shared(); + int uid = 0; + abilityInfo->name = GetTestAbilityName(); + abilityInfo->applicationInfo.uid = uid; + auto appInfo = std::make_shared(); + appInfo->name = GetTestAppName(); + appInfo->uid = uid; + BundleInfo bundleInfo; + HapModuleInfo hapModuleInfo; + EXPECT_TRUE(service_->GetBundleAndHapInfo(*abilityInfo, appInfo, bundleInfo, hapModuleInfo)); + EXPECT_TRUE(service_ != nullptr); + auto record = service_->CreateAppRunningRecord( + GetMockToken(), nullptr, appInfo, abilityInfo, GetTestProcessName(), bundleInfo, hapModuleInfo, nullptr); + EXPECT_TRUE(record != nullptr); + + record->SetUid(uid); + EXPECT_TRUE(record != nullptr) << ",create apprunningrecord fail!"; + + sptr mockApplication(new MockApplication()); + sptr client = iface_cast(mockApplication); + record->SetApplicationClient(client); + EXPECT_CALL(*mockApplication, ScheduleLaunchApplication(_, _)) + .Times(1) + .WillOnce(Invoke(mockApplication.GetRefPtr(), &MockApplication::LaunchApplication)); + Configuration config; + record->LaunchApplication(config); + mockApplication->Wait(); + + EXPECT_CALL(*mockApplication, ScheduleForegroundApplication()) + .Times(1) + .WillOnce(InvokeWithoutArgs(mockApplication.GetRefPtr(), &MockApplication::Post)); + // application enter in foreground and check the result + record->ScheduleForegroundRunning(); + mockApplication->Wait(); + + // update application state and check the state + record->SetState(ApplicationState::APP_STATE_FOREGROUND); + auto newRecord = service_->appRunningManager_->CheckAppRunningRecordIsExist( + appInfo->name, GetTestProcessName(), appInfo->uid, bundleInfo); + EXPECT_TRUE(newRecord); + newRecord->SetUid(uid); + auto stateFromRec = newRecord->GetState(); + EXPECT_EQ(stateFromRec, ApplicationState::APP_STATE_FOREGROUND); + + std::vector info; + size_t infoCount {0}; + auto res = service_->GetAllRunningProcesses(info); + EXPECT_TRUE (res == ERR_OK); + EXPECT_TRUE(info.size() == infoCount); +} + +/* + * Feature: AppMgrServiceInner + * Function: GetAllRunningProcesses + * SubFunction: NA + * FunctionPoints: get running process info by token. + * EnvConditions: NA + * CaseDescription: creat two apprunningrecords, set record state, call query function. + */ +HWTEST_F(AppRunningProcessesInfoTest, UpdateAppRunningRecord_003, TestSize.Level1) +{ + auto abilityInfo = std::make_shared(); + abilityInfo->name = GetTestAbilityName(); + auto appInfo = std::make_shared(); + appInfo->name = GetTestAppName(); + int uid = 0; + BundleInfo bundleInfo; + bundleInfo.appId = "com.ohos.test.helloworld_code123"; + bundleInfo.jointUserId = "joint456"; + HapModuleInfo hapModuleInfo; + hapModuleInfo.moduleName = "module789"; + EXPECT_TRUE(service_ != nullptr); + auto record = service_->CreateAppRunningRecord( + GetMockToken(), nullptr, appInfo, abilityInfo, GetTestProcessName(), bundleInfo, hapModuleInfo, nullptr); + EXPECT_TRUE(record != nullptr); + + record->SetUid(uid); + EXPECT_TRUE(record != nullptr) << ",create apprunningrecord fail!"; + + sptr mockApplication(new MockApplication()); + sptr client = iface_cast(mockApplication); + record->SetApplicationClient(client); + EXPECT_CALL(*mockApplication, ScheduleLaunchApplication(_, _)) + .Times(1) + .WillOnce(Invoke(mockApplication.GetRefPtr(), &MockApplication::LaunchApplication)); + Configuration config; + record->LaunchApplication(config); + mockApplication->Wait(); + + EXPECT_CALL(*mockApplication, ScheduleForegroundApplication()) + .Times(1) + .WillOnce(InvokeWithoutArgs(mockApplication.GetRefPtr(), &MockApplication::Post)); + // application enter in foreground and check the result + record->ScheduleForegroundRunning(); + mockApplication->Wait(); + + // update application state and check the state + record->SetState(ApplicationState::APP_STATE_FOREGROUND); + auto newRecord = service_->appRunningManager_->CheckAppRunningRecordIsExist( + appInfo->name, GetTestProcessName(), appInfo->uid, bundleInfo); + EXPECT_TRUE(newRecord); + newRecord->SetUid(0); + auto stateFromRec = newRecord->GetState(); + EXPECT_EQ(stateFromRec, ApplicationState::APP_STATE_FOREGROUND); + + auto abilityInfo2 = std::make_shared(); + abilityInfo2->name = GetTestAbilityName() + "2"; + abilityInfo2->applicationInfo.uid = uid; + auto appInfo2 = std::make_shared(); + appInfo2->name = GetTestAppName() + "2"; + appInfo2->uid = uid; + BundleInfo bundleInfo2; + HapModuleInfo hapModuleInfo2; + EXPECT_TRUE(service_->GetBundleAndHapInfo(*abilityInfo2, appInfo2, bundleInfo2, hapModuleInfo2)); + EXPECT_TRUE(service_ != nullptr); + auto mock_token = new (std::nothrow) MockAbilityToken(); + auto record2 = service_->CreateAppRunningRecord( + mock_token, nullptr, appInfo2, abilityInfo2, GetTestProcessName() + "2", bundleInfo2, hapModuleInfo2, nullptr); + EXPECT_TRUE(record != nullptr); + record2->SetUid(uid); + + std::vector info; + size_t infoCount {0}; + auto res = service_->GetAllRunningProcesses(info); + EXPECT_TRUE (res == ERR_OK); + EXPECT_TRUE(info.size() == infoCount); +} + +/* + * Feature: AppMgrServiceInner + * Function: GetRunningProcessInfoByToken + * SubFunction: NA + * FunctionPoints: get running process info by token. + * EnvConditions: NA + * CaseDescription: creat apprunningrecords, set record state, call query function. + */ +HWTEST_F(AppRunningProcessesInfoTest, UpdateAppRunningRecord_004, TestSize.Level1) +{ + auto abilityInfo = std::make_shared(); + abilityInfo->name = GetTestAbilityName(); + auto appInfo = std::make_shared(); + appInfo->name = GetTestAppName(); + BundleInfo bundleInfo; + bundleInfo.appId = "com.ohos.test.helloworld_code123"; + bundleInfo.jointUserId = "joint456"; + HapModuleInfo hapModuleInfo; + hapModuleInfo.moduleName = "module789"; + EXPECT_TRUE(service_ != nullptr); + auto record = service_->CreateAppRunningRecord( + GetMockToken(), nullptr, appInfo, abilityInfo, GetTestProcessName(), bundleInfo, hapModuleInfo, nullptr); + EXPECT_TRUE(record != nullptr); + record->SetState(ApplicationState::APP_STATE_BACKGROUND); + record->SetApplicationClient(GetMockedAppSchedulerClient()); + RunningProcessInfo info; + service_->appRunningManager_->GetRunningProcessInfoByToken(GetMockToken(), info); + EXPECT_TRUE(info.processName_ == GetTestProcessName()); +} +} // namespace AppExecFwk +} // namespace OHOS \ No newline at end of file