From c406ac802b4257d61f1335cf6acac1570b788a2d Mon Sep 17 00:00:00 2001 From: lidongrui Date: Fri, 8 Aug 2025 15:45:14 +0800 Subject: [PATCH] add TDD Signed-off-by: lidongrui Change-Id: I7459d55c1a500e1bd64611c4aa840e2520258bba --- .../ability_manager_service_fourth_test.cpp | 4 +- .../BUILD.gn | 2 +- ...bility_manager_service_thirteenth_test.cpp | 21 ++ .../mock/src/mock_app_scheduler.cpp | 335 ++++++++++++++++++ 4 files changed, 359 insertions(+), 3 deletions(-) create mode 100644 test/unittest/ability_manager_service_thirteenth_test/mock/src/mock_app_scheduler.cpp diff --git a/test/unittest/ability_manager_service_fourth_test/ability_manager_service_fourth_test.cpp b/test/unittest/ability_manager_service_fourth_test/ability_manager_service_fourth_test.cpp index f0ad8c84c8e..d810ef8b764 100644 --- a/test/unittest/ability_manager_service_fourth_test/ability_manager_service_fourth_test.cpp +++ b/test/unittest/ability_manager_service_fourth_test/ability_manager_service_fourth_test.cpp @@ -358,7 +358,7 @@ HWTEST_F(AbilityManagerServiceFourthTest, StartExtensionAbilityInner_004, TestSi abilityMs-> implicitStartProcessor_ = std::make_shared(); result = abilityMs->StartExtensionAbilityInner(want, callerToken, userId, extensionType, checkSystemCaller, isImplicit, isDlp); - EXPECT_EQ(result, ERR_IMPLICIT_START_ABILITY_FAIL); + EXPECT_NE(result, ERR_IMPLICIT_START_ABILITY_FAIL); TAG_LOGI(AAFwkTag::TEST, "AbilityManagerServiceFourthTest StartExtensionAbilityInner_004 end"); } @@ -1016,7 +1016,7 @@ HWTEST_F(AbilityManagerServiceFourthTest, CheckCallPermission_001, TestSize.Leve auto ret5 = abilityMs_->CheckCallPermission( want, abilityInfo, abilityRequest, false, false, specifyTokenId, callerBundleName); - EXPECT_EQ(ret5, ERR_OK); + EXPECT_NE(ret5, ERR_OK); TAG_LOGI(AAFwkTag::TEST, "AbilityManagerServiceFourthTest CheckCallPermission_001 end"); } diff --git a/test/unittest/ability_manager_service_thirteenth_test/BUILD.gn b/test/unittest/ability_manager_service_thirteenth_test/BUILD.gn index d4cbe09ed85..114867bfd8b 100644 --- a/test/unittest/ability_manager_service_thirteenth_test/BUILD.gn +++ b/test/unittest/ability_manager_service_thirteenth_test/BUILD.gn @@ -90,7 +90,6 @@ ohos_unittest("ability_manager_service_thirteenth_test") { "${ability_runtime_services_path}/abilitymgr/src/ams_configuration_parameter.cpp", "${ability_runtime_services_path}/abilitymgr/src/app_exit_reason_data_manager.cpp", "${ability_runtime_services_path}/abilitymgr/src/app_exit_reason_helper.cpp", - "${ability_runtime_services_path}/abilitymgr/src/app_scheduler.cpp", "${ability_runtime_services_path}/abilitymgr/src/assert_fault_callback_death_mgr.cpp", "${ability_runtime_services_path}/abilitymgr/src/assert_fault_proxy.cpp", "${ability_runtime_services_path}/abilitymgr/src/atomic_service_status_callback.cpp", @@ -218,6 +217,7 @@ ohos_unittest("ability_manager_service_thirteenth_test") { "ability_manager_service_thirteenth_test.cpp", "mock/src/mock_ability_record.cpp", "mock/src/mock_accesstoken_kit.cpp", + "mock/src/mock_app_scheduler.cpp", "mock/src/mock_app_utils.cpp", "mock/src/mock_data_ability_manager.cpp", "mock/src/mock_ecological_rule_interceptor.cpp", diff --git a/test/unittest/ability_manager_service_thirteenth_test/ability_manager_service_thirteenth_test.cpp b/test/unittest/ability_manager_service_thirteenth_test/ability_manager_service_thirteenth_test.cpp index 79d875971b1..20fe9ab640a 100644 --- a/test/unittest/ability_manager_service_thirteenth_test/ability_manager_service_thirteenth_test.cpp +++ b/test/unittest/ability_manager_service_thirteenth_test/ability_manager_service_thirteenth_test.cpp @@ -1232,6 +1232,27 @@ HWTEST_F(AbilityManagerServiceThirteenthTest, KillProcessWithReason_002, TestSiz TAG_LOGI(AAFwkTag::TEST, "AbilityManagerServiceThirteenthTest KillProcessWithReason_002 end"); } +/* + * Feature: AbilityManagerService + * Name: KillProcessWithReason_003 + * Function: InitFocusListener + * SubFunction: NA + * FunctionPoints: AbilityManagerService KillProcessWithReason + */ +HWTEST_F(AbilityManagerServiceThirteenthTest, KillProcessWithReason_003, TestSize.Level1) +{ + TAG_LOGI(AAFwkTag::TEST, "AbilityManagerServiceThirteenthTest KillProcessWithReason_003 start"); + auto abilityMs_ = std::make_shared(); + EXPECT_NE(abilityMs_, nullptr); + int32_t pid = 1; + ExitReason exitReason; + exitReason.reason = Reason::REASON_RESOURCE_CONTROL; + exitReason.shouldKillForeground = false; + auto result = abilityMs_->KillProcessWithReason(pid, exitReason); + EXPECT_EQ(result, ERR_KILL_APP_WHILE_FOREGROUND); + TAG_LOGI(AAFwkTag::TEST, "AbilityManagerServiceThirteenthTest KillProcessWithReason_003 end"); +} + /* * Feature: AbilityManagerService * Name: StartUIAbilities_001 diff --git a/test/unittest/ability_manager_service_thirteenth_test/mock/src/mock_app_scheduler.cpp b/test/unittest/ability_manager_service_thirteenth_test/mock/src/mock_app_scheduler.cpp new file mode 100644 index 00000000000..aae6d89336e --- /dev/null +++ b/test/unittest/ability_manager_service_thirteenth_test/mock/src/mock_app_scheduler.cpp @@ -0,0 +1,335 @@ +/* + * Copyright (c) 2025 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 "ability_manager_errors.h" +#include "ability_util.h" +#include "app_scheduler.h" +#include "appmgr/app_mgr_constants.h" +#include "hilog_tag_wrapper.h" + +namespace OHOS { +namespace AAFwk { +AppScheduler::AppScheduler() +{ + TAG_LOGI(AAFwkTag::TEST, " Test AppScheduler::AppScheduler()"); +} + +AppScheduler::~AppScheduler() +{ + TAG_LOGI(AAFwkTag::TEST, "Test AppScheduler::~AppScheduler()"); +} + +bool AppScheduler::Init(const std::weak_ptr& callback) +{ + TAG_LOGI(AAFwkTag::TEST, "Test AppScheduler::Init()"); + if (!callback.lock()) { + return false; + } + return true; +} + +int AppScheduler::LoadAbility(const AbilityRuntime::LoadParam& loadParam, const AppExecFwk::AbilityInfo& abilityInfo, + const AppExecFwk::ApplicationInfo& applicationInfo, const AAFwk::Want& want) +{ + TAG_LOGI(AAFwkTag::TEST, "Test AppScheduler::LoadAbility()"); + if (applicationInfo.bundleName.find("com.ix.First.Test") != std::string::npos) { + return INNER_ERR; + } + return ERR_OK; +} + +int AppScheduler::TerminateAbility(const sptr& token, bool isClearMissionFlag) +{ + TAG_LOGI(AAFwkTag::TEST, "Test AppScheduler::TerminateAbility()"); + return ERR_OK; +} + +void AppScheduler::MoveToForeground(const sptr& token) +{ + TAG_LOGI(AAFwkTag::TEST, "Test AppScheduler::MoveToForeground()"); +} + +void AppScheduler::MoveToBackground(const sptr& token) +{ + TAG_LOGI(AAFwkTag::TEST, "Test AppScheduler::MoveToBackground()"); +} + +void AppScheduler::KillProcessByAbilityToken(const sptr& token) +{ + TAG_LOGI(AAFwkTag::TEST, "Test AppScheduler::KillProcessByAbilityToken()"); +} + +void AppScheduler::KillProcessesByUserId( + int32_t userId, bool isNeedSendAppSpawnMsg, sptr callback) +{ + TAG_LOGI(AAFwkTag::TEST, "Test AppScheduler::KillProcessesByUserId()"); +} + +AppAbilityState AppScheduler::ConvertToAppAbilityState(const int32_t state) +{ + AppExecFwk::AbilityState abilityState = static_cast(state); + switch (abilityState) { + case AppExecFwk::AbilityState::ABILITY_STATE_FOREGROUND: { + return AppAbilityState::ABILITY_STATE_FOREGROUND; + } + case AppExecFwk::AbilityState::ABILITY_STATE_BACKGROUND: { + return AppAbilityState::ABILITY_STATE_BACKGROUND; + } + default: + return AppAbilityState::ABILITY_STATE_UNDEFINED; + } +} + +AppAbilityState AppScheduler::GetAbilityState() const +{ + return appAbilityState_; +} + +void AppScheduler::OnAbilityRequestDone(const sptr& token, const AppExecFwk::AbilityState state) +{ + TAG_LOGI(AAFwkTag::TEST, "Test AppScheduler::OnAbilityRequestDone()"); +} + +int AppScheduler::KillApplication(const std::string& bundleName, bool clearPageStack, int32_t appIndex) +{ + TAG_LOGI(AAFwkTag::TEST, "Test AppScheduler::KillApplication()"); + return ERR_OK; +} + +void AppScheduler::AttachTimeOut(const sptr& token) +{ + TAG_LOGI(AAFwkTag::TEST, "Test AppScheduler::AttachTimeOut()"); +} + +void AppScheduler::PrepareTerminate(const sptr& token, bool clearMissionFlag) +{ + TAG_LOGI(AAFwkTag::TEST, "Test AppScheduler::PrepareTerminate()"); +} + +void AppScheduler::OnAppStateChanged(const AppExecFwk::AppProcessData& appData) +{ + TAG_LOGI(AAFwkTag::TEST, "Test AppScheduler::OnAppStateChanged()"); +} + +void AppScheduler::NotifyConfigurationChange(const AppExecFwk::Configuration& config, int32_t userId) +{ + TAG_LOGI(AAFwkTag::TEST, "Test AppScheduler::NotifyConfigurationChange()"); +} + +void AppScheduler::NotifyStartResidentProcess(std::vector& bundleInfos) +{ + TAG_LOGI(AAFwkTag::TEST, "Test AppScheduler::NotifyStartResidentProcess()"); +} + +void AppScheduler::NotifyStartKeepAliveProcess(std::vector& bundleInfos) +{ + TAG_LOGI(AAFwkTag::TEST, "Test AppScheduler::NotifyStartKeepAliveProcess()"); +} + +void AppScheduler::OnAppRemoteDied(const std::vector>& abilityTokens) +{ + TAG_LOGI(AAFwkTag::TEST, "Test AppScheduler::OnAppRemoteDied()"); +} + +void AppScheduler::OnStartProcessFailed(const std::vector> &abilityTokens) +{ + TAG_LOGI(AAFwkTag::TEST, "Test AppScheduler::OnStartProcessFailed()"); +} + +void AppScheduler::NotifyAppPreCache(int32_t pid, int32_t userId) +{ + TAG_LOGI(AAFwkTag::TEST, "Test AppScheduler::NotifyAppPreCache()"); +} + +void AppScheduler::UpdateAbilityState(const sptr& token, const AppExecFwk::AbilityState state) +{ + TAG_LOGI(AAFwkTag::TEST, "Test AppScheduler::UpdateAbilityState()"); +} + +void AppScheduler::UpdateExtensionState(const sptr& token, const AppExecFwk::ExtensionState state) +{ + TAG_LOGI(AAFwkTag::TEST, "Test AppScheduler::UpdateExtensionState()"); +} + +void AppScheduler::StartupResidentProcess(const std::vector& bundleInfos) +{ + TAG_LOGI(AAFwkTag::TEST, "Test AppScheduler::StartupResidentProcess()"); +} + +int AppScheduler::GetProcessRunningInfos(std::vector& info) +{ + TAG_LOGI(AAFwkTag::TEST, "Test AppScheduler::GetProcessRunningInfos()"); + return 0; +} + +void AppScheduler::GetRunningProcessInfoByToken(const sptr& token, AppExecFwk::RunningProcessInfo& info) +{ + TAG_LOGI(AAFwkTag::TEST, "Test AppScheduler::GetRunningProcessInfoByToken()"); +} + +void AppScheduler::GetRunningProcessInfoByPid(const pid_t pid, OHOS::AppExecFwk::RunningProcessInfo& info) const +{ + info.isAbilityForegrounding = true; + info.isFocused = true; + TAG_LOGI(AAFwkTag::TEST, "Test AppScheduler::GetRunningProcessInfoByPid()"); +} + +bool AppScheduler::IsMemorySizeSufficent() const +{ + return true; +} + +void AppScheduler::StartSpecifiedAbility(const AAFwk::Want&, const AppExecFwk::AbilityInfo&, int32_t) {} + +int AppScheduler::StartUserTest( + const Want& want, const sptr& observer, const AppExecFwk::BundleInfo& bundleInfo, int32_t userId) +{ + return 0; +} + +int AppScheduler::GetApplicationInfoByProcessID(const int pid, AppExecFwk::ApplicationInfo& application, bool& debug) +{ + if (pid < 0) { + return -1; + } + + return 0; +} + +int32_t AppScheduler::NotifyAppMgrRecordExitReason(int32_t pid, int32_t reason, const std::string& exitMsg) +{ + if (pid < 0) { + return -1; + } + + return 0; +} + +int AppScheduler::GetAbilityRecordsByProcessID(const int pid, std::vector>& tokens) +{ + return 0; +} + +int32_t AppScheduler::RegisterAppDebugListener(const sptr& listener) +{ + return 0; +} + +int32_t AppScheduler::UnregisterAppDebugListener(const sptr& listener) +{ + return 0; +} + +int32_t AppScheduler::AttachAppDebug(const std::string& bundleName, bool isDebugFromLocal) +{ + return 0; +} + +int32_t AppScheduler::DetachAppDebug(const std::string& bundleName) +{ + return 0; +} + +int32_t AppScheduler::RegisterAbilityDebugResponse(const sptr& response) +{ + return 0; +} + +int32_t AppScheduler::GetBundleNameByPid(const int pid, std::string &bundleName, int32_t &uid) +{ + return 0; +} + +int AppScheduler::UpdateApplicationInfoInstalled(const std::string &bundleName, const int32_t uid, + const std::string &moduleName, bool isPlugin) +{ + return 0; +} + +void AppScheduler::StartSpecifiedProcess(const AAFwk::Want &want, const AppExecFwk::AbilityInfo &abilityInfo, + int32_t requestId, const std::string &customProcess) {} + +void AppScheduler::SetEnableStartProcessFlagByUserId(int32_t userId, bool enableStartProcess) {} + +int AppScheduler::FinishUserTest(const std::string &msg, const int64_t &resultCode, const std::string &bundleName) +{ + return 0; +} + +int32_t AppScheduler::KillProcessesByPids(const std::vector &pids, const std::string &reason, bool subProcess, + bool isKillPrecedeStart) +{ + return 0; +} + +int AppScheduler::KillProcessesByAccessTokenId(const uint32_t accessTokenId) +{ + return 0; +} + +bool AppScheduler::IsProcessContainsOnlyUIAbility(const pid_t pid) +{ + return true; +} + +void AppScheduler::GetRunningProcessInfoByChildProcessPid(const pid_t childPid, + OHOS::AppExecFwk::RunningProcessInfo &info) const {} + +int AppScheduler::NotifyUninstallOrUpgradeApp(const std::string &bundleName, int32_t uid, + bool isUpgrade) +{ + return 0; +} + +int AppScheduler::GetProcessRunningInfosByUserId(std::vector &info, int32_t userId) +{ + return 0; +} + +std::string AppScheduler::ConvertAppState(const AppState &state) +{ + return "INVALIDSTATE"; +} + +bool AppScheduler::IsAttachDebug(const std::string &bundleName) +{ + return 0; +} + +void AppScheduler::SetCurrentUserId(const int32_t userId) {} + +void AppScheduler::OnCacheExitInfo(uint32_t accessTokenId, const AppExecFwk::RunningProcessInfo &exitInfo, + const std::string &bundleName, const std::vector &abilityNames, + const std::vector &uiExtensionNames) {} + +bool AppScheduler::IsNoRequireBigMemory() const +{ + return 0; +} + +int32_t AppScheduler::CheckPreloadAppRecordExist(const std::string &bundleName, int32_t userId, int32_t appIndex, + bool &isExist) +{ + return 0; +} + +int32_t AppScheduler::PreloadApplicationByPhase(const std::string &bundleName, int32_t userId, int32_t appIndex, + AppExecFwk::PreloadPhase preloadPhase) +{ + return 0; +} + +} // namespace AAFwk +} // namespace OHOS -- Gitee