diff --git a/interfaces/inner_api/app_manager/include/appmgr/app_mgr_client.h b/interfaces/inner_api/app_manager/include/appmgr/app_mgr_client.h index 46dc8fedb2..62af072291 100644 --- a/interfaces/inner_api/app_manager/include/appmgr/app_mgr_client.h +++ b/interfaces/inner_api/app_manager/include/appmgr/app_mgr_client.h @@ -1113,6 +1113,15 @@ public: */ int32_t PreloadExtension(const AAFwk::Want &want, int32_t appIndex, int32_t userId); + /** + * Get all ability infos + * + * @param pid if pid is -1, query all ability infos, otherwise query ability infos for this pid + * @param infos ability infos + * @return Returns ERR_OK on success, others on failure. + */ + int32_t GetAllAbilityInfos(const int32_t pid, std::vector &infos); + private: void SetServiceManager(std::unique_ptr serviceMgr); /** diff --git a/interfaces/inner_api/app_manager/include/appmgr/app_mgr_interface.h b/interfaces/inner_api/app_manager/include/appmgr/app_mgr_interface.h index c13c58ffdc..7b0929caef 100644 --- a/interfaces/inner_api/app_manager/include/appmgr/app_mgr_interface.h +++ b/interfaces/inner_api/app_manager/include/appmgr/app_mgr_interface.h @@ -1154,6 +1154,18 @@ public: } virtual void SetProcessPrepareExit(int32_t pid) {} + + /** + * Get all ability infos + * + * @param pid if pid is -1, query all ability infos, otherwise query ability infos for this pid + * @param infos ability infos + * @return Returns ERR_OK on success, others on failure. + */ + virtual int32_t GetAllAbilityInfos(const int32_t pid, std::vector &infos) + { + return 0; + } }; } // namespace AppExecFwk } // namespace OHOS diff --git a/interfaces/inner_api/app_manager/include/appmgr/app_mgr_ipc_interface_code.h b/interfaces/inner_api/app_manager/include/appmgr/app_mgr_ipc_interface_code.h index d16ecc5eae..b756ef09a4 100644 --- a/interfaces/inner_api/app_manager/include/appmgr/app_mgr_ipc_interface_code.h +++ b/interfaces/inner_api/app_manager/include/appmgr/app_mgr_ipc_interface_code.h @@ -151,7 +151,8 @@ enum class AppMgrInterfaceCode { DESTROY_IMAGE = 126, NOTIFY_TEMPLATE_PROCESS_DEEP_FROZEN = 127, REGISTER_IMAGE_PROCESS_STATE_OBSERVER = 128, - UNREGISTER_IMAGE_PROCESS_STATE_OBSERVER = 129 + UNREGISTER_IMAGE_PROCESS_STATE_OBSERVER = 129, + GET_ALL_ABILITY_INFOS = 130, }; } // AppExecFwk } // OHOS diff --git a/interfaces/inner_api/app_manager/include/appmgr/app_mgr_proxy.h b/interfaces/inner_api/app_manager/include/appmgr/app_mgr_proxy.h index 10e446c60a..295c6b8e9a 100644 --- a/interfaces/inner_api/app_manager/include/appmgr/app_mgr_proxy.h +++ b/interfaces/inner_api/app_manager/include/appmgr/app_mgr_proxy.h @@ -930,6 +930,15 @@ public: virtual int32_t PreloadExtension(const AAFwk::Want &want, int32_t appIndex, int32_t userId) override; void SetProcessPrepareExit(int32_t pid) override; + + /** + * Get all ability infos + * + * @param pid if pid is -1, query all ability infos, otherwise query ability infos for this pid + * @param infos ability infos + * @return Returns ERR_OK on success, others on failure. + */ + virtual int32_t GetAllAbilityInfos(const int32_t pid, std::vector &infos) override; private: bool SendTransactCmd(AppMgrInterfaceCode code, MessageParcel &data, MessageParcel &reply); bool WriteInterfaceToken(MessageParcel &data); diff --git a/interfaces/inner_api/app_manager/include/appmgr/app_mgr_stub.h b/interfaces/inner_api/app_manager/include/appmgr/app_mgr_stub.h index 466b024e6a..3eca1f6517 100644 --- a/interfaces/inner_api/app_manager/include/appmgr/app_mgr_stub.h +++ b/interfaces/inner_api/app_manager/include/appmgr/app_mgr_stub.h @@ -216,6 +216,7 @@ private: int32_t HandleKillChildProcessByPid(MessageParcel &data, MessageParcel &reply); int32_t HandlePreloadExtension(MessageParcel &data, MessageParcel &reply); int32_t HandleSetProcessPrepareExit(MessageParcel &data, MessageParcel &reply); + int32_t HandleGetAllAbilityInfos(MessageParcel &data, MessageParcel &reply); DISALLOW_COPY_AND_MOVE(AppMgrStub); }; } // namespace AppExecFwk diff --git a/interfaces/inner_api/app_manager/src/appmgr/app_mgr_client.cpp b/interfaces/inner_api/app_manager/src/appmgr/app_mgr_client.cpp index ad762854b0..f2f4c2a97d 100644 --- a/interfaces/inner_api/app_manager/src/appmgr/app_mgr_client.cpp +++ b/interfaces/inner_api/app_manager/src/appmgr/app_mgr_client.cpp @@ -1943,5 +1943,18 @@ int32_t AppMgrClient::PreloadExtension(const AAFwk::Want &want, int32_t appIndex return service->PreloadExtension(want, appIndex, userId); } +int32_t AppMgrClient::GetAllAbilityInfos(const int32_t pid, std::vector &infos) +{ + if (mgrHolder_ == nullptr) { + TAG_LOGE(AAFwkTag::APPMGR, "mgrHolder is nullptr."); + return ERROR_SERVICE_NOT_CONNECTED; + } + sptr service = iface_cast(mgrHolder_->GetRemoteObject()); + if (service == nullptr) { + TAG_LOGE(AAFwkTag::APPMGR, "Service is nullptr."); + return AppMgrResultCode::ERROR_SERVICE_NOT_CONNECTED; + } + return service->GetAllAbilityInfos(pid, infos); +} } // namespace AppExecFwk } // namespace OHOS diff --git a/interfaces/inner_api/app_manager/src/appmgr/app_mgr_proxy.cpp b/interfaces/inner_api/app_manager/src/appmgr/app_mgr_proxy.cpp index 99be0c5dbd..1045dcd4e4 100644 --- a/interfaces/inner_api/app_manager/src/appmgr/app_mgr_proxy.cpp +++ b/interfaces/inner_api/app_manager/src/appmgr/app_mgr_proxy.cpp @@ -2865,5 +2865,42 @@ void AppMgrProxy::SetProcessPrepareExit(int32_t pid) PARCEL_UTIL_WRITE_NORET(data, Int32, pid); PARCEL_UTIL_SENDREQ_NORET(AppMgrInterfaceCode::SET_PROCESS_PREPARE_EXIT, data, reply, option); } + +int32_t AppMgrProxy::GetAllAbilityInfos(const int32_t pid, std::vector &infos) +{ + MessageParcel data; + if (!WriteInterfaceToken(data)) { + TAG_LOGE(AAFwkTag::APPMGR, "Write interface token failed."); + return AAFwk::ERR_WRITE_INTERFACE_TOKEN_FAILED; + } + PARCEL_UTIL_WRITE_RET_INT(data, Int32, pid); + + MessageParcel reply; + MessageOption option(MessageOption::TF_SYNC); + PARCEL_UTIL_SENDREQ_RET_INT(AppMgrInterfaceCode::GET_ALL_ABILITY_INFOS, data, reply, option); + + int32_t ret = reply.ReadInt32(); + if (ret != ERR_OK) { + TAG_LOGE(AAFwkTag::APPMGR, "GetAllAbilityInfos failed, error code: %{public}d", ret); + return ret; + } + + int32_t infosSize = reply.ReadInt32(); + if (infosSize < 0) { + TAG_LOGE(AAFwkTag::APPMGR, "Read Parcelable infosSize failed, size: %{public}d", infosSize); + return ERR_INVALID_VALUE; + } + + for (int32_t idx = 0; idx < infosSize; idx++) { + std::unique_ptr info(reply.ReadParcelable()); + if (!info) { + TAG_LOGE(AAFwkTag::APPMGR, "Read Parcelable info failed at index: %{public}d", idx); + return ERR_INVALID_VALUE; + } + infos.emplace_back(*info); + } + + return ret; +} } // namespace AppExecFwk } // namespace OHOS diff --git a/interfaces/inner_api/app_manager/src/appmgr/app_mgr_stub.cpp b/interfaces/inner_api/app_manager/src/appmgr/app_mgr_stub.cpp index d2b0ae3f02..f58dc41350 100644 --- a/interfaces/inner_api/app_manager/src/appmgr/app_mgr_stub.cpp +++ b/interfaces/inner_api/app_manager/src/appmgr/app_mgr_stub.cpp @@ -253,6 +253,8 @@ int32_t AppMgrStub::OnRemoteRequestInnerFourth(uint32_t code, MessageParcel &dat return HandleIsSpecifiedModuleLoaded(data, reply); case static_cast(AppMgrInterfaceCode::SIGN_RESTART_PROCESS): return HandleSignRestartProcess(data, reply); + case static_cast(AppMgrInterfaceCode::GET_ALL_ABILITY_INFOS): + return HandleGetAllAbilityInfos(data, reply); } return INVALID_FD; } @@ -2360,5 +2362,32 @@ int32_t AppMgrStub::HandleSetProcessPrepareExit(MessageParcel &data, MessageParc SetProcessPrepareExit(pid); return NO_ERROR; } + +int32_t AppMgrStub::HandleGetAllAbilityInfos(MessageParcel &data, MessageParcel &reply) +{ + TAG_LOGD(AAFwkTag::APPMGR, "HandleGetAllAbilityInfos call"); + int32_t pid = data.ReadInt32(); + std::vector infos; + int32_t ret = GetAllAbilityInfos(pid, infos); + if (!reply.WriteInt32(ret)) { + TAG_LOGE(AAFwkTag::APPMGR, "fail to write result."); + return ERR_APPEXECFWK_PARCEL_ERROR; + } + if (ret != ERR_OK) { + return ret; + } + + if (!reply.WriteInt32(infos.size())) { + TAG_LOGE(AAFwkTag::APPMGR, "fail to write info size."); + return ERR_APPEXECFWK_PARCEL_ERROR; + } + for (auto &info : infos) { + if (!reply.WriteParcelable(&info)) { + TAG_LOGE(AAFwkTag::APPMGR, "fail to write info."); + return ERR_APPEXECFWK_PARCEL_ERROR; + } + } + return NO_ERROR; +} } // namespace AppExecFwk } // namespace OHOS diff --git a/services/appmgr/include/app_mgr_service.h b/services/appmgr/include/app_mgr_service.h index 489c70e5f5..150bc53213 100644 --- a/services/appmgr/include/app_mgr_service.h +++ b/services/appmgr/include/app_mgr_service.h @@ -944,6 +944,15 @@ public: */ virtual int32_t PreloadExtension(const AAFwk::Want &want, int32_t appIndex, int32_t userId) override; + /** + * Get all ability infos + * + * @param pid if pid is -1, query all ability infos, otherwise query ability infos for this pid + * @param infos ability infos + * @return Returns ERR_OK on success, others on failure. + */ + virtual int32_t GetAllAbilityInfos(const int32_t pid, std::vector &infos) override; + private: /** * Init, Initialize application services. diff --git a/services/appmgr/include/app_mgr_service_inner.h b/services/appmgr/include/app_mgr_service_inner.h index 0648b9cba4..ebb03412b0 100644 --- a/services/appmgr/include/app_mgr_service_inner.h +++ b/services/appmgr/include/app_mgr_service_inner.h @@ -1730,6 +1730,15 @@ public: void NotifyTerminateAbility(const sptr token); + /** + * Get all ability infos + * + * @param pid if pid is -1, query all ability infos, otherwise query ability infos for this pid + * @param infos ability infos + * @return Returns ERR_OK on success, others on failure. + */ + int32_t GetAllAbilityInfos(const int32_t pid, std::vector &infos); + private: int32_t ForceKillApplicationInner(const std::string &bundleName, const int userId = -1, const int appIndex = 0); diff --git a/services/appmgr/include/app_running_manager.h b/services/appmgr/include/app_running_manager.h index a77f00e099..84aea00d6f 100644 --- a/services/appmgr/include/app_running_manager.h +++ b/services/appmgr/include/app_running_manager.h @@ -451,6 +451,7 @@ public: std::shared_ptr childRecord, std::shared_ptr appRecord); std::shared_ptr CheckAppRunningRecordForUIExtension( int32_t uid, const std::string &instanceKey, const std::string &customProcessFlag); + int32_t GetAllAbilityInfos(const int32_t pid, std::vector &infos); private: std::shared_ptr GetAbilityRunningRecord(const int64_t eventId); diff --git a/services/appmgr/include/app_running_record.h b/services/appmgr/include/app_running_record.h index b66d3fa368..457d4f984e 100644 --- a/services/appmgr/include/app_running_record.h +++ b/services/appmgr/include/app_running_record.h @@ -1098,6 +1098,8 @@ public: */ void SetWatchdogBackgroundStatusRunning(bool status); + void GetAllAbilityInfos(std::vector &infos); + void SetUserRequestCleaning(); bool IsUserRequestCleaning() const; bool IsAllAbilityReadyToCleanedByUserRequest(); diff --git a/services/appmgr/src/app_mgr_service.cpp b/services/appmgr/src/app_mgr_service.cpp index 22077666ae..9b882d880e 100644 --- a/services/appmgr/src/app_mgr_service.cpp +++ b/services/appmgr/src/app_mgr_service.cpp @@ -2250,5 +2250,21 @@ void AppMgrService::SetProcessPrepareExit(int32_t pid) } appMgrServiceInner_->SetProcessPrepareExit(pid); } + +int32_t AppMgrService::GetAllAbilityInfos(const int32_t pid, std::vector &infos) +{ + auto callingUid = IPCSkeleton::GetCallingUid(); + bool isRssCalling = AAFwk::PermissionVerification::GetInstance()->CheckSpecificSystemAbilityAccessPermission( + BS_PROCESS_NAME) && (callingUid == RESOURCE_MANAGER_UID); + if (!isRssCalling) { + TAG_LOGE(AAFwkTag::APPMGR, "Permission denied"); + return ERR_PERMISSION_DENIED; + } + if (!appMgrServiceInner_) { + TAG_LOGE(AAFwkTag::APPMGR, "appMgrServiceInner_ is nullptr"); + return AAFwk::ERR_APP_MGR_SERVICE_NOT_READY; + } + return appMgrServiceInner_->GetAllAbilityInfos(pid, infos); +} } // namespace AppExecFwk } // namespace OHOS diff --git a/services/appmgr/src/app_mgr_service_inner.cpp b/services/appmgr/src/app_mgr_service_inner.cpp index 33c421290e..4400bc474e 100644 --- a/services/appmgr/src/app_mgr_service_inner.cpp +++ b/services/appmgr/src/app_mgr_service_inner.cpp @@ -12617,5 +12617,14 @@ void AppMgrServiceInner::HandleForegroundAbilityDied( AppRecoveryMgr::AppRecoveryMgr::GetInstance().HandleAppDied(token); } } + +int32_t AppMgrServiceInner::GetAllAbilityInfos(const int32_t pid, std::vector &infos) +{ + if (!appRunningManager_) { + TAG_LOGE(AAFwkTag::APPMGR, "appRunningManager_ is nullptr"); + return ERR_NO_INIT; + } + return appRunningManager_->GetAllAbilityInfos(pid, infos); +} } // namespace AppExecFwk } // namespace OHOS diff --git a/services/appmgr/src/app_running_manager.cpp b/services/appmgr/src/app_running_manager.cpp index 96033888f0..f9920d5a60 100644 --- a/services/appmgr/src/app_running_manager.cpp +++ b/services/appmgr/src/app_running_manager.cpp @@ -2363,5 +2363,27 @@ int32_t AppRunningManager::QueryUIExtensionBindItemById( } return ERR_INVALID_VALUE; } + +int32_t AppRunningManager::GetAllAbilityInfos(const int32_t pid, std::vector &infos) +{ + std::lock_guard guard(runningRecordMapMutex_); + if (pid != -1) { + for (const auto &[recordId, appRecord] : appRunningRecordMap_) { + if (appRecord != nullptr && appRecord->GetPid() == pid) { + appRecord->GetAllAbilityInfos(infos); + return ERR_OK; + } + } + TAG_LOGE(AAFwkTag::APPMGR, "not find pid:%{public}d appRecord", pid); + return ERR_INVALID_VALUE; + } else { + for (const auto &[recordId, appRecord] : appRunningRecordMap_) { + if (appRecord != nullptr) { + appRecord->GetAllAbilityInfos(infos); + } + } + } + return ERR_OK; +} } // namespace AppExecFwk } // namespace OHOS diff --git a/services/appmgr/src/app_running_record.cpp b/services/appmgr/src/app_running_record.cpp index 04a3b2a07a..74c2448a2b 100644 --- a/services/appmgr/src/app_running_record.cpp +++ b/services/appmgr/src/app_running_record.cpp @@ -2982,5 +2982,23 @@ void AppRunningRecord::SetSupportMultiProcessDeviceFeature(bool support) std::lock_guard supportLock(supportMultiProcessDeviceFeatureLock_); supportMultiProcessDeviceFeature_ = support; } + +void AppRunningRecord::GetAllAbilityInfos(std::vector &infos) +{ + auto abilitiesMap = GetAbilities(); + for (const auto &pair : abilitiesMap) { + const auto &abilityRecord = pair.second; + if (abilityRecord == nullptr || abilityRecord->GetAbilityInfo() == nullptr) { + continue; + } + AbilityStateData info; + info.abilityRecordId = abilityRecord->GetAbilityRecordId(); + info.pid = GetPid(); + info.uid = abilityRecord->GetAbilityInfo()->applicationInfo.uid; + info.abilityType = static_cast(abilityRecord->GetAbilityInfo()->type); + info.extensionAbilityType = static_cast(abilityRecord->GetAbilityInfo()->extensionAbilityType); + infos.emplace_back(info); + } +} } // namespace AppExecFwk } // namespace OHOS diff --git a/test/mock/mock_appmgr_service/include/mock_app_mgr_service_inner.h b/test/mock/mock_appmgr_service/include/mock_app_mgr_service_inner.h index fd165417f5..d52e7ae930 100644 --- a/test/mock/mock_appmgr_service/include/mock_app_mgr_service_inner.h +++ b/test/mock/mock_appmgr_service/include/mock_app_mgr_service_inner.h @@ -103,6 +103,7 @@ public: MOCK_METHOD2(DestroyImage, int32_t(uint64_t checkpointId, sptr)); MOCK_METHOD1(RegisterImageProcessStateObserver, int32_t(const sptr &observer)); MOCK_METHOD1(UnregisterImageProcessStateObserver, int32_t(const sptr &observer)); + MOCK_METHOD2(GetAllAbilityInfos, int32_t(const int32_t pid, std::vector &infos)); void StartSpecifiedAbility(const AAFwk::Want&, const AppExecFwk::AbilityInfo&, int32_t, const std::string&) {} diff --git a/test/mock/services_appmgr_test/include/mock_app_mgr_service.h b/test/mock/services_appmgr_test/include/mock_app_mgr_service.h index 7c7a06038b..58db0c5097 100644 --- a/test/mock/services_appmgr_test/include/mock_app_mgr_service.h +++ b/test/mock/services_appmgr_test/include/mock_app_mgr_service.h @@ -144,6 +144,7 @@ public: MOCK_METHOD2(DestroyImage, int32_t(uint64_t checkpointId, sptr)); MOCK_METHOD1(RegisterImageProcessStateObserver, int32_t(const sptr &observer)); MOCK_METHOD1(UnregisterImageProcessStateObserver, int32_t(const sptr &observer)); + MOCK_METHOD2(GetAllAbilityInfos, int32_t(const int32_t pid, std::vector &infos)); virtual int StartUserTestProcess( const AAFwk::Want &want, const sptr &observer, const BundleInfo &bundleInfo, int32_t userId) { diff --git a/test/mock/services_appmgr_test/include/mock_app_mgr_service_inner.h b/test/mock/services_appmgr_test/include/mock_app_mgr_service_inner.h index 679b29d6db..0fe4a59d72 100644 --- a/test/mock/services_appmgr_test/include/mock_app_mgr_service_inner.h +++ b/test/mock/services_appmgr_test/include/mock_app_mgr_service_inner.h @@ -87,6 +87,7 @@ public: MOCK_METHOD3(CheckPreloadAppRecordExist, bool(const std::string&, int32_t, int32_t)); MOCK_CONST_METHOD0(IsFoundationCall, bool()); MOCK_METHOD2(QueryRunningSharedBundles, int32_t(pid_t pid, std::map &sharedBundles)); + MOCK_METHOD2(GetAllAbilityInfos, int32_t(const int32_t pid, std::vector &infos)); void StartSpecifiedAbility(const AAFwk::Want&, const AppExecFwk::AbilityInfo&, int32_t, const std::string&) {} diff --git a/test/unittest/ams_app_mgr_client_test/ams_app_mgr_client_test.cpp b/test/unittest/ams_app_mgr_client_test/ams_app_mgr_client_test.cpp index a0f48c3687..6ba7f0b62d 100644 --- a/test/unittest/ams_app_mgr_client_test/ams_app_mgr_client_test.cpp +++ b/test/unittest/ams_app_mgr_client_test/ams_app_mgr_client_test.cpp @@ -853,5 +853,67 @@ HWTEST_F(AmsAppMgrClientTest, QueryRunningSharedBundles_003, TestSize.Level1) EXPECT_EQ(result, ERR_OK); TAG_LOGI(AAFwkTag::TEST, "QueryRunningSharedBundles_003 end"); } + +/* + * Feature: AppMgrService + * Function: AppMgrClient::GetAllAbilityInfos + * SubFunction: GetAllAbilityInfos + * FunctionPoints: AppMgrClient GetAllAbilityInfos interface + * EnvConditions: Mobile that can run ohos test framework + * CaseDescription: Test GetAllAbilityInfos with invalid parameters. + */ +HWTEST_F(AmsAppMgrClientTest, GetAllAbilityInfos_001, TestSize.Level1) +{ + TAG_LOGI(AAFwkTag::TEST, "GetAllAbilityInfos_001 start"); + client_->mgrHolder_ = nullptr; + + std::vector infos; + int32_t result = client_->GetAllAbilityInfos(-1, infos); + EXPECT_EQ(result, AppMgrResultCode::ERROR_SERVICE_NOT_CONNECTED); + TAG_LOGI(AAFwkTag::TEST, "GetAllAbilityInfos_001 end"); +} + +/* + * Feature: AppMgrService + * Function: AppMgrClient::GetAllAbilityInfos + * SubFunction: GetAllAbilityInfos + * FunctionPoints: AppMgrClient GetAllAbilityInfos interface + * EnvConditions: Mobile that can run ohos test framework + * CaseDescription: Test GetAllAbilityInfos with invalid parameters. + */ +HWTEST_F(AmsAppMgrClientTest, GetAllAbilityInfos_002, TestSize.Level1) +{ + TAG_LOGI(AAFwkTag::TEST, "GetAllAbilityInfos_002 start"); + client_->SetServiceManager(nullptr); + + std::vector infos; + int32_t result = client_->GetAllAbilityInfos(-1, infos); + EXPECT_EQ(result, AppMgrResultCode::ERROR_SERVICE_NOT_CONNECTED); + TAG_LOGI(AAFwkTag::TEST, "GetAllAbilityInfos_002 end"); +} + +/* + * Feature: AppMgrService + * Function: AppMgrClient::GetAllAbilityInfos + * SubFunction: GetAllAbilityInfos + * FunctionPoints: AppMgrClient GetAllAbilityInfos interface + * EnvConditions: Mobile that can run ohos test framework + * CaseDescription: Test GetAllAbilityInfos with valid parameters. + */ +HWTEST_F(AmsAppMgrClientTest, GetAllAbilityInfos_003, TestSize.Level1) +{ + TAG_LOGI(AAFwkTag::TEST, "GetAllAbilityInfos_003 start"); + EXPECT_EQ(client_->ConnectAppMgrService(), AppMgrResultCode::RESULT_OK); + + EXPECT_CALL(*(static_cast((iface_cast(client_->GetRemoteObject())).GetRefPtr())), + GetAllAbilityInfos(_, _)) + .Times(1) + .WillOnce(Return(ERR_OK)); + + std::vector infos; + int32_t result = client_->GetAllAbilityInfos(-1, infos); + EXPECT_EQ(result, ERR_OK); + TAG_LOGI(AAFwkTag::TEST, "GetAllAbilityInfos_003 end"); +} } // namespace AppExecFwk } // namespace OHOS diff --git a/test/unittest/app_mgr_proxy_test/app_mgr_proxy_test.cpp b/test/unittest/app_mgr_proxy_test/app_mgr_proxy_test.cpp index b142a39314..7c65940cff 100644 --- a/test/unittest/app_mgr_proxy_test/app_mgr_proxy_test.cpp +++ b/test/unittest/app_mgr_proxy_test/app_mgr_proxy_test.cpp @@ -1409,5 +1409,129 @@ HWTEST_F(AppMgrProxyTest, UnregisterImageProcessStateObserver_0100, TestSize.Lev auto result = appMgrProxy_->UnregisterImageProcessStateObserver(observer); EXPECT_EQ(result, OHOS::ERR_INVALID_VALUE); } + +/** + * @tc.name: GetAllAbilityInfos_0100 + * @tc.desc: Test GetAllAbilityInfos when SendRequest fails. + * @tc.type: FUNC + */ +HWTEST_F(AppMgrProxyTest, GetAllAbilityInfos_0100, TestSize.Level1) +{ + EXPECT_CALL(*mockAppMgrService_, SendRequest(_, _, _, _)) + .Times(1) + .WillOnce(Return(ERR_INVALID_VALUE)); + + std::vector infos; + auto ret = appMgrProxy_->GetAllAbilityInfos(-1, infos); + EXPECT_EQ(ret, ERR_INVALID_VALUE); +} + +/** + * @tc.name: GetAllAbilityInfos_0200 + * @tc.desc: Test GetAllAbilityInfos when service returns error. + * @tc.type: FUNC + */ +HWTEST_F(AppMgrProxyTest, GetAllAbilityInfos_0200, TestSize.Level1) +{ + EXPECT_CALL(*mockAppMgrService_, SendRequest(_, _, _, _)) + .Times(1) + .WillOnce([](uint32_t code, MessageParcel& data, MessageParcel& reply, MessageOption& option) { + reply.WriteInt32(ERR_PERMISSION_DENIED); + return NO_ERROR; + }); + + std::vector infos; + auto ret = appMgrProxy_->GetAllAbilityInfos(-1, infos); + EXPECT_EQ(ret, ERR_PERMISSION_DENIED); +} + +/** + * @tc.name: GetAllAbilityInfos_0300 + * @tc.desc: Test GetAllAbilityInfos when reading infos size fails. + * @tc.type: FUNC + */ +HWTEST_F(AppMgrProxyTest, GetAllAbilityInfos_0300, TestSize.Level1) +{ + EXPECT_CALL(*mockAppMgrService_, SendRequest(_, _, _, _)) + .Times(1) + .WillOnce([](uint32_t code, MessageParcel& data, MessageParcel& reply, MessageOption& option) { + reply.WriteInt32(ERR_OK); + reply.WriteInt32(-1); + return NO_ERROR; + }); + + std::vector infos; + auto ret = appMgrProxy_->GetAllAbilityInfos(-1, infos); + EXPECT_EQ(ret, ERR_INVALID_VALUE); +} + +/** + * @tc.name: GetAllAbilityInfos_0400 + * @tc.desc: Test GetAllAbilityInfos when reading Parcelable fails. + * @tc.type: FUNC + */ +HWTEST_F(AppMgrProxyTest, GetAllAbilityInfos_0400, TestSize.Level1) +{ + EXPECT_CALL(*mockAppMgrService_, SendRequest(_, _, _, _)) + .Times(1) + .WillOnce([](uint32_t code, MessageParcel& data, MessageParcel& reply, MessageOption& option) { + reply.WriteInt32(ERR_OK); + reply.WriteInt32(1); + return NO_ERROR; + }); + + std::vector infos; + auto ret = appMgrProxy_->GetAllAbilityInfos(-1, infos); + EXPECT_EQ(ret, ERR_INVALID_VALUE); +} + +/** + * @tc.name: GetAllAbilityInfos_0500 + * @tc.desc: Test GetAllAbilityInfos when reading empty info list. + * @tc.type: FUNC + */ +HWTEST_F(AppMgrProxyTest, GetAllAbilityInfos_0500, TestSize.Level1) +{ + EXPECT_CALL(*mockAppMgrService_, SendRequest(_, _, _, _)) + .Times(1) + .WillOnce([](uint32_t code, MessageParcel& data, MessageParcel& reply, MessageOption& option) { + reply.WriteInt32(ERR_OK); + reply.WriteInt32(0); + return NO_ERROR; + }); + + std::vector infos; + auto ret = appMgrProxy_->GetAllAbilityInfos(-1, infos); + EXPECT_EQ(ret, ERR_OK); + EXPECT_TRUE(infos.empty()); +} + +/** + * @tc.name: GetAllAbilityInfos_0600 + * @tc.desc: Test GetAllAbilityInfos when reading one ability info. + * @tc.type: FUNC + */ +HWTEST_F(AppMgrProxyTest, GetAllAbilityInfos_0600, TestSize.Level1) +{ + EXPECT_CALL(*mockAppMgrService_, SendRequest(_, _, _, _)) + .Times(1) + .WillOnce([](uint32_t code, MessageParcel& data, MessageParcel& reply, MessageOption& option) { + reply.WriteInt32(ERR_OK); + reply.WriteInt32(1); + AppExecFwk::AbilityStateData info; + info.abilityRecordId = 1001; + info.pid = 1001; + info.uid = 1001; + info.abilityType = 1; + info.extensionAbilityType = 0; + reply.WriteParcelable(&info); + return NO_ERROR; + }); + + std::vector infos; + auto ret = appMgrProxy_->GetAllAbilityInfos(1001, infos); + EXPECT_EQ(ret, ERR_OK); + EXPECT_EQ(infos.size(), 1); +} } // namespace AppExecFwk } // namespace OHOS \ No newline at end of file diff --git a/test/unittest/app_mgr_service_fourth_test/app_mgr_service_fourth_test.cpp b/test/unittest/app_mgr_service_fourth_test/app_mgr_service_fourth_test.cpp index 42bf672342..e543cbb6b4 100644 --- a/test/unittest/app_mgr_service_fourth_test/app_mgr_service_fourth_test.cpp +++ b/test/unittest/app_mgr_service_fourth_test/app_mgr_service_fourth_test.cpp @@ -39,6 +39,7 @@ namespace { const std::string BUNDLE_NAME = "com.example.test"; constexpr int UID = 1000; constexpr pid_t PID = 1000; +constexpr pid_t RSS_UID = 1096; } class MockTaskHandlerWrap : public TaskHandlerWrap { @@ -325,5 +326,38 @@ HWTEST_F(AppMgrServiceFourthTest, StartUserTestProcess_0100, TestSize.Level1) EXPECT_EQ(result, ERR_INVALID_OPERATION); TAG_LOGI(AAFwkTag::TEST, "StartUserTestProcess_0100 end"); } + +/** + * @tc.name: GetAllAbilityInfos_001 + * @tc.desc: verify GetAllAbilityInfos works when inner service is null + * @tc.type: FUNC + */ +HWTEST_F(AppMgrServiceFourthTest, GetAllAbilityInfos_001, TestSize.Level1) +{ + TAG_LOGI(AAFwkTag::TEST, "GetAllAbilityInfos_001 start"); + appMgrService_->appMgrServiceInner_ = nullptr; + AAFwk::MyStatus::GetInstance().isCheckSpecificSystemAbilityAccessPermission_ = true; + IPCSkeleton::SetCallingUid(RSS_UID); + std::vector infos; + int32_t result = appMgrService_->GetAllAbilityInfos(-1, infos); + EXPECT_EQ(result, AAFwk::ERR_APP_MGR_SERVICE_NOT_READY); + AAFwk::MyStatus::GetInstance().isCheckSpecificSystemAbilityAccessPermission_ = false; + appMgrService_->appMgrServiceInner_ = mockAppMgrServiceInner_; + TAG_LOGI(AAFwkTag::TEST, "GetAllAbilityInfos_001 end"); +} + +/** + * @tc.name: GetAllAbilityInfos_002 + * @tc.desc: verify GetAllAbilityInfos works when permission denied + * @tc.type: FUNC + */ +HWTEST_F(AppMgrServiceFourthTest, GetAllAbilityInfos_002, TestSize.Level1) +{ + TAG_LOGI(AAFwkTag::TEST, "GetAllAbilityInfos_002 start"); + std::vector infos; + int32_t result = appMgrService_->GetAllAbilityInfos(-1, infos); + EXPECT_EQ(result, ERR_PERMISSION_DENIED); + TAG_LOGI(AAFwkTag::TEST, "GetAllAbilityInfos_002 end"); +} } // namespace AppExecFwk } // namespace OHOS \ No newline at end of file diff --git a/test/unittest/app_mgr_service_fourth_test/mock/src/mock_app_running_record.cpp b/test/unittest/app_mgr_service_fourth_test/mock/src/mock_app_running_record.cpp index 669a638e67..4b084aeb80 100644 --- a/test/unittest/app_mgr_service_fourth_test/mock/src/mock_app_running_record.cpp +++ b/test/unittest/app_mgr_service_fourth_test/mock/src/mock_app_running_record.cpp @@ -1399,5 +1399,9 @@ bool AppRunningRecord::HasAgentExtensionAbility() { return false; } + +void AppRunningRecord::GetAllAbilityInfos(std::vector &infos) +{ +} } // namespace AppExecFwk } // namespace OHOS diff --git a/test/unittest/app_mgr_service_inner_eighth_test/mock/src/mock_app_running_manager.cpp b/test/unittest/app_mgr_service_inner_eighth_test/mock/src/mock_app_running_manager.cpp index 154c3ceb4f..8d3874302a 100644 --- a/test/unittest/app_mgr_service_inner_eighth_test/mock/src/mock_app_running_manager.cpp +++ b/test/unittest/app_mgr_service_inner_eighth_test/mock/src/mock_app_running_manager.cpp @@ -494,5 +494,10 @@ bool AppRunningManager::CheckAppRunningRecordIsLast(const std::shared_ptr &infos) +{ + return ERR_OK; +} } // namespace AppExecFwk } // namespace OHOS diff --git a/test/unittest/app_mgr_service_inner_eighth_test/mock/src/mock_app_running_record.cpp b/test/unittest/app_mgr_service_inner_eighth_test/mock/src/mock_app_running_record.cpp index 9fb8a38f25..c796d7c7df 100644 --- a/test/unittest/app_mgr_service_inner_eighth_test/mock/src/mock_app_running_record.cpp +++ b/test/unittest/app_mgr_service_inner_eighth_test/mock/src/mock_app_running_record.cpp @@ -1419,5 +1419,9 @@ bool AppRunningRecord::HasAgentExtensionAbility() { return false; } + +void AppRunningRecord::GetAllAbilityInfos(std::vector &infos) +{ +} } // namespace AppExecFwk } // namespace OHOS diff --git a/test/unittest/app_mgr_service_inner_ninth_test/mock/src/mock_app_running_manager.cpp b/test/unittest/app_mgr_service_inner_ninth_test/mock/src/mock_app_running_manager.cpp index 4cfe669cec..061665b0e3 100644 --- a/test/unittest/app_mgr_service_inner_ninth_test/mock/src/mock_app_running_manager.cpp +++ b/test/unittest/app_mgr_service_inner_ninth_test/mock/src/mock_app_running_manager.cpp @@ -574,5 +574,10 @@ int32_t AppRunningManager::SignRestartProcess(int32_t) { return AAFwk::MyStatus::GetInstance().signRestartProcessStatus_; } + +int32_t AppRunningManager::GetAllAbilityInfos(const int32_t pid, std::vector &infos) +{ + return ERR_OK; +} } // namespace AppExecFwk } // namespace OHOS diff --git a/test/unittest/app_mgr_service_inner_ninth_test/mock/src/mock_app_running_record.cpp b/test/unittest/app_mgr_service_inner_ninth_test/mock/src/mock_app_running_record.cpp index 9ed7cf3d3d..9aa16534cd 100644 --- a/test/unittest/app_mgr_service_inner_ninth_test/mock/src/mock_app_running_record.cpp +++ b/test/unittest/app_mgr_service_inner_ninth_test/mock/src/mock_app_running_record.cpp @@ -1519,5 +1519,9 @@ bool AppRunningRecord::HasAgentExtensionAbility() { return false; } + +void AppRunningRecord::GetAllAbilityInfos(std::vector &infos) +{ +} } // namespace AppExecFwk } // namespace OHOS diff --git a/test/unittest/app_mgr_service_inner_seventh_test/mock/src/mock_app_running_manager.cpp b/test/unittest/app_mgr_service_inner_seventh_test/mock/src/mock_app_running_manager.cpp index ee69f12ded..4e34d6afbc 100644 --- a/test/unittest/app_mgr_service_inner_seventh_test/mock/src/mock_app_running_manager.cpp +++ b/test/unittest/app_mgr_service_inner_seventh_test/mock/src/mock_app_running_manager.cpp @@ -484,5 +484,10 @@ bool AppRunningManager::CheckAppRunningRecordIsLast(const std::shared_ptr &infos) +{ + return ERR_OK; +} } // namespace AppExecFwk } // namespace OHOS diff --git a/test/unittest/app_mgr_service_inner_seventh_test/mock/src/mock_app_running_record.cpp b/test/unittest/app_mgr_service_inner_seventh_test/mock/src/mock_app_running_record.cpp index c36219099d..5d2648a441 100644 --- a/test/unittest/app_mgr_service_inner_seventh_test/mock/src/mock_app_running_record.cpp +++ b/test/unittest/app_mgr_service_inner_seventh_test/mock/src/mock_app_running_record.cpp @@ -1419,5 +1419,9 @@ bool AppRunningRecord::HasAgentExtensionAbility() { return false; } + +void AppRunningRecord::GetAllAbilityInfos(std::vector &infos) +{ +} } // namespace AppExecFwk } // namespace OHOS 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 c77f433dfa..1701e374d3 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 @@ -6490,5 +6490,60 @@ HWTEST_F(AppMgrServiceInnerTest, UpdateConfigurationByUserIds_0002, TestSize.Lev EXPECT_EQ(res, ERR_OK); TAG_LOGI(AAFwkTag::TEST, "UpdateConfigurationByUserIds_0002 end"); } + +/** + * @tc.name: GetAllAbilityInfos_0001 + * @tc.desc: GetAllAbilityInfos when appRunningManager is null + * @tc.type: FUNC + */ +HWTEST_F(AppMgrServiceInnerTest, GetAllAbilityInfos_0001, TestSize.Level1) +{ + TAG_LOGI(AAFwkTag::TEST, "GetAllAbilityInfos_0001 start"); + auto appMgrServiceInner = std::make_shared(); + EXPECT_NE(appMgrServiceInner, nullptr); + appMgrServiceInner->appRunningManager_ = nullptr; + + std::vector infos; + auto ret = appMgrServiceInner->GetAllAbilityInfos(-1, infos); + EXPECT_EQ(ret, ERR_NO_INIT); + TAG_LOGI(AAFwkTag::TEST, "GetAllAbilityInfos_0001 end"); +} + +/** + * @tc.name: GetAllAbilityInfos_0002 + * @tc.desc: GetAllAbilityInfos with valid appRunningManager + * @tc.type: FUNC + */ +HWTEST_F(AppMgrServiceInnerTest, GetAllAbilityInfos_0002, TestSize.Level1) +{ + TAG_LOGI(AAFwkTag::TEST, "GetAllAbilityInfos_0002 start"); + auto appMgrServiceInner = std::make_shared(); + EXPECT_NE(appMgrServiceInner, nullptr); + appMgrServiceInner->appRunningManager_ = std::make_shared(); + + std::vector infos; + auto ret = appMgrServiceInner->GetAllAbilityInfos(-1, infos); + EXPECT_EQ(ret, ERR_OK); + TAG_LOGI(AAFwkTag::TEST, "GetAllAbilityInfos_0002 end"); +} + +/** + * @tc.name: GetAllAbilityInfos_0003 + * @tc.desc: GetAllAbilityInfos with valid appRunningManager + * @tc.type: FUNC + */ +HWTEST_F(AppMgrServiceInnerTest, GetAllAbilityInfos_0003, TestSize.Level1) +{ + TAG_LOGI(AAFwkTag::TEST, "GetAllAbilityInfos_0003 start"); + auto appMgrServiceInner = std::make_shared(); + EXPECT_NE(appMgrServiceInner, nullptr); + appMgrServiceInner->appRunningManager_ = std::make_shared(); + + std::vector infos; + int32_t pid = 1001; + auto ret = appMgrServiceInner->GetAllAbilityInfos(pid, infos); + EXPECT_EQ(ret, ERR_INVALID_VALUE); + TAG_LOGI(AAFwkTag::TEST, "GetAllAbilityInfos_0003 end"); +} } // namespace AppExecFwk } // namespace OHOS diff --git a/test/unittest/app_mgr_stub_test/app_mgr_stub_test.cpp b/test/unittest/app_mgr_stub_test/app_mgr_stub_test.cpp index bc6a437bd7..9e72144228 100644 --- a/test/unittest/app_mgr_stub_test/app_mgr_stub_test.cpp +++ b/test/unittest/app_mgr_stub_test/app_mgr_stub_test.cpp @@ -1254,5 +1254,122 @@ HWTEST_F(AppMgrStubTest, HandleDestroyImage_ShouldReturnNoErrorWhenFlagIsTrueAnd auto result = mockAppMgrService_->HandleDestroyImage(data, reply); EXPECT_EQ(result, NO_ERROR); } + +/** + * @tc.name: HandleGetAllAbilityInfos_0100 + * @tc.desc: Test HandleGetAllAbilityInfos when service returns error. + * @tc.type: FUNC + */ +HWTEST_F(AppMgrStubTest, HandleGetAllAbilityInfos_0100, TestSize.Level1) +{ + MessageParcel data; + MessageParcel reply; + MessageOption option; + + WriteInterfaceToken(data); + int32_t pid = 1001; + data.WriteInt32(pid); + + EXPECT_CALL(*mockAppMgrService_, GetAllAbilityInfos(_, _)) + .Times(1) + .WillOnce(Return(ERR_PERMISSION_DENIED)); + + mockAppMgrService_->OnRemoteRequest( + static_cast(AppMgrInterfaceCode::GET_ALL_ABILITY_INFOS), data, reply, option); + auto replyResult = reply.ReadInt32(); + EXPECT_EQ(replyResult, ERR_PERMISSION_DENIED); +} + +/** + * @tc.name: HandleGetAllAbilityInfos_0200 + * @tc.desc: Test HandleGetAllAbilityInfos when write result fails. + * @tc.type: FUNC + */ +HWTEST_F(AppMgrStubTest, HandleGetAllAbilityInfos_0200, TestSize.Level1) +{ + MessageParcel data; + MessageParcel reply; + MessageOption option; + + WriteInterfaceToken(data); + int32_t pid = 1001; + data.WriteInt32(pid); + + EXPECT_CALL(*mockAppMgrService_, GetAllAbilityInfos(_, _)) + .Times(1) + .WillOnce(DoAll(SetArgReferee<1>(std::vector()), Return(ERR_OK))); + + auto ret = mockAppMgrService_->OnRemoteRequest( + static_cast(AppMgrInterfaceCode::GET_ALL_ABILITY_INFOS), data, reply, option); + EXPECT_EQ(ret, NO_ERROR); + auto replyResult = reply.ReadInt32(); + EXPECT_EQ(replyResult, ERR_OK); +} + +/** + * @tc.name: HandleGetAllAbilityInfos_0300 + * @tc.desc: Test HandleGetAllAbilityInfos with empty info list. + * @tc.type: FUNC + */ +HWTEST_F(AppMgrStubTest, HandleGetAllAbilityInfos_0300, TestSize.Level1) +{ + MessageParcel data; + MessageParcel reply; + MessageOption option; + + WriteInterfaceToken(data); + int32_t pid = -1; + data.WriteInt32(pid); + + std::vector infos; + EXPECT_CALL(*mockAppMgrService_, GetAllAbilityInfos(_, _)) + .Times(1) + .WillOnce(DoAll(SetArgReferee<1>(infos), Return(ERR_OK))); + + auto ret = mockAppMgrService_->OnRemoteRequest( + static_cast(AppMgrInterfaceCode::GET_ALL_ABILITY_INFOS), data, reply, option); + EXPECT_EQ(ret, NO_ERROR); + auto replyResult = reply.ReadInt32(); + EXPECT_EQ(replyResult, ERR_OK); + int32_t infoSize = reply.ReadInt32(); + EXPECT_EQ(infoSize, 0); +} + +/** + * @tc.name: HandleGetAllAbilityInfos_0400 + * @tc.desc: Test HandleGetAllAbilityInfos with one ability info. + * @tc.type: FUNC + */ +HWTEST_F(AppMgrStubTest, HandleGetAllAbilityInfos_0400, TestSize.Level1) +{ + MessageParcel data; + MessageParcel reply; + MessageOption option; + + WriteInterfaceToken(data); + int32_t pid = 1001; + data.WriteInt32(pid); + + std::vector infos; + AppExecFwk::AbilityStateData info; + info.abilityRecordId = 1001; + info.pid = 1001; + info.uid = 1001; + info.abilityType = 1; + info.extensionAbilityType = 0; + infos.push_back(info); + + EXPECT_CALL(*mockAppMgrService_, GetAllAbilityInfos(_, _)) + .Times(1) + .WillOnce(DoAll(SetArgReferee<1>(infos), Return(ERR_OK))); + + auto ret = mockAppMgrService_->OnRemoteRequest( + static_cast(AppMgrInterfaceCode::GET_ALL_ABILITY_INFOS), data, reply, option); + EXPECT_EQ(ret, NO_ERROR); + auto replyResult = reply.ReadInt32(); + EXPECT_EQ(replyResult, ERR_OK); + int32_t infoSize = reply.ReadInt32(); + EXPECT_EQ(infoSize, 1); +} } // namespace AppExecFwk } // namespace OHOS diff --git a/test/unittest/app_running_manager_test/app_running_manager_test.cpp b/test/unittest/app_running_manager_test/app_running_manager_test.cpp index 73fcf8e1b9..e11465f360 100644 --- a/test/unittest/app_running_manager_test/app_running_manager_test.cpp +++ b/test/unittest/app_running_manager_test/app_running_manager_test.cpp @@ -1048,5 +1048,84 @@ HWTEST_F(AppRunningManagerTest, AppRunningManager_ProcessExitByBundleNameAndUid_ bool result = appRunningManager->ProcessExitByBundleNameAndUid(bundleName, uid, pids, config); EXPECT_EQ(result, false); } + +/** + * @tc.name: GetAllAbilityInfos_0100 + * @tc.desc: Test GetAllAbilityInfos with empty map + * @tc.type: FUNC + */ +HWTEST_F(AppRunningManagerTest, GetAllAbilityInfos_0100, TestSize.Level1) +{ + auto appRunningManager = std::make_shared(); + EXPECT_NE(appRunningManager, nullptr); + + std::vector infos; + auto ret = appRunningManager->GetAllAbilityInfos(-1, infos); + EXPECT_EQ(ret, ERR_OK); + EXPECT_TRUE(infos.empty()); +} + +/** + * @tc.name: GetAllAbilityInfos_0200 + * @tc.desc: Test GetAllAbilityInfos with specific pid not found + * @tc.type: FUNC + */ +HWTEST_F(AppRunningManagerTest, GetAllAbilityInfos_0200, TestSize.Level1) +{ + auto appRunningManager = std::make_shared(); + EXPECT_NE(appRunningManager, nullptr); + + std::vector infos; + int32_t pid = 1001; + auto ret = appRunningManager->GetAllAbilityInfos(pid, infos); + EXPECT_EQ(ret, ERR_INVALID_VALUE); +} + +/** + * @tc.name: GetAllAbilityInfos_0300 + * @tc.desc: Test GetAllAbilityInfos with specific pid found + * @tc.type: FUNC + */ +HWTEST_F(AppRunningManagerTest, GetAllAbilityInfos_0300, TestSize.Level1) +{ + auto appRunningManager = std::make_shared(); + EXPECT_NE(appRunningManager, nullptr); + + auto appInfo = std::make_shared(); + appInfo->name = "test.app"; + int32_t recordId = RECORD_ID; + std::string processName = "test.app"; + auto appRecord = std::make_shared(appInfo, recordId, processName); + appRecord->GetPriorityObject()->SetPid(1001); + appRunningManager->appRunningRecordMap_.emplace(recordId, appRecord); + + std::vector infos; + int32_t pid = 1001; + auto ret = appRunningManager->GetAllAbilityInfos(pid, infos); + EXPECT_EQ(ret, ERR_OK); +} + +/** + * @tc.name: GetAllAbilityInfos_0400 + * @tc.desc: Test GetAllAbilityInfos with pid=-1 to get all + * @tc.type: FUNC + */ +HWTEST_F(AppRunningManagerTest, GetAllAbilityInfos_0400, TestSize.Level1) +{ + auto appRunningManager = std::make_shared(); + EXPECT_NE(appRunningManager, nullptr); + + auto appInfo = std::make_shared(); + appInfo->name = "test.app"; + int32_t recordId = RECORD_ID; + std::string processName = "test.app"; + auto appRecord = std::make_shared(appInfo, recordId, processName); + appRecord->GetPriorityObject()->SetPid(1001); + appRunningManager->appRunningRecordMap_.emplace(recordId, appRecord); + + std::vector infos; + auto ret = appRunningManager->GetAllAbilityInfos(-1, infos); + EXPECT_EQ(ret, ERR_OK); +} } // namespace AppExecFwk } // namespace OHOS diff --git a/test/unittest/app_running_record_test/app_running_record_test.cpp b/test/unittest/app_running_record_test/app_running_record_test.cpp index cbd2016c24..f10a825572 100644 --- a/test/unittest/app_running_record_test/app_running_record_test.cpp +++ b/test/unittest/app_running_record_test/app_running_record_test.cpp @@ -1855,5 +1855,55 @@ HWTEST_F(AppRunningRecordTest, AppRunningRecord_SetKillAppReason_0100, TestSize. appRunningRecord->SetInnerMsg(reason); EXPECT_EQ(appRunningRecord->GetInnerMsg(), reason); } + +/** + * @tc.name: GetAllAbilityInfos_0100 + * @tc.desc: Test GetAllAbilityInfos with empty ability list + * @tc.type: FUNC + */ +HWTEST_F(AppRunningRecordTest, GetAllAbilityInfos_0100, TestSize.Level1) +{ + std::shared_ptr appInfo = std::make_shared(); + ASSERT_NE(appInfo, nullptr); + int32_t recordId = RECORD_ID; + std::string processName = "test.process"; + auto appRunningRecord = std::make_shared(appInfo, recordId, processName); + ASSERT_NE(appRunningRecord, nullptr); + appRunningRecord->GetPriorityObject()->SetPid(1001); + + std::vector infos; + appRunningRecord->GetAllAbilityInfos(infos); + EXPECT_TRUE(infos.empty()); +} + +/** + * @tc.name: GetAllAbilityInfos_0200 + * @tc.desc: Test GetAllAbilityInfos with empty ability list + * @tc.type: FUNC + */ +HWTEST_F(AppRunningRecordTest, GetAllAbilityInfos_0200, TestSize.Level1) +{ + std::shared_ptr appInfo = std::make_shared(); + appInfo->bundleName = "com.test.bundle"; + ASSERT_NE(appInfo, nullptr); + int32_t recordId = RECORD_ID; + std::string processName = "com.test.bundle"; + auto appRunningRecord = std::make_shared(appInfo, recordId, processName); + ASSERT_NE(appRunningRecord, nullptr); + appRunningRecord->GetPriorityObject()->SetPid(1001); + + auto moduleRecord = std::make_shared(appInfo, nullptr); + ASSERT_NE(moduleRecord, nullptr); + auto abilityInfo = std::make_shared(); + abilityInfo->type = AbilityType::EXTENSION; + abilityInfo->extensionAbilityType = ExtensionAbilityType::SERVICE; + sptr token = new (std::nothrow) MockAbilityToken(); + ASSERT_NE(token, nullptr); + ASSERT_NE(moduleRecord->AddAbility(token, abilityInfo, nullptr, RECORD_ID), nullptr); + appRunningRecord->hapModules_[appInfo->bundleName].emplace_back(moduleRecord); + std::vector infos; + appRunningRecord->GetAllAbilityInfos(infos); + EXPECT_EQ(infos.size(), 1); +} } // namespace AppExecFwk } // namespace OHOS diff --git a/test/unittest/cache_process_manager_second_test/mock/src/mock_app_running_record.cpp b/test/unittest/cache_process_manager_second_test/mock/src/mock_app_running_record.cpp index bf2db8ba28..d5b740f611 100644 --- a/test/unittest/cache_process_manager_second_test/mock/src/mock_app_running_record.cpp +++ b/test/unittest/cache_process_manager_second_test/mock/src/mock_app_running_record.cpp @@ -1461,5 +1461,9 @@ bool AppRunningRecord::IsLastAgentExtensionAbility(const sptr &to { return false; } + +void AppRunningRecord::GetAllAbilityInfos(std::vector &infos) +{ +} } // namespace AppExecFwk } // namespace OHOS