diff --git a/services/test/BUILD.gn b/services/test/BUILD.gn index 42d989e53933455c8b7f56fea362a2b0ed63195c..9faaf31958f4f313c3d6fd6ba00605c83d43a6b1 100644 --- a/services/test/BUILD.gn +++ b/services/test/BUILD.gn @@ -44,6 +44,7 @@ group("moduletest") { testonly = true deps = [ + "moduletest/ability_mgr_module_account_test:moduletest", "moduletest/ability_mgr_service_test:moduletest", "moduletest/ability_record_test:moduletest", @@ -54,7 +55,11 @@ group("moduletest") { "moduletest/ipc_ability_mgr_test:moduletest", "moduletest/ipc_ability_scheduler_test:moduletest", "moduletest/module_test_dump_util:module_test_dump_util", + "moduletest/on_new_want_module_test:moduletest", "moduletest/panding_want_manager_test:moduletest", + "moduletest/running_infos_module_test:moduletest", + "moduletest/start_option_display_id_test:moduletest", + "moduletest/static_start_ability_test:moduletest", "//third_party/jsoncpp:jsoncpp", ] } diff --git a/services/test/mock/include/mock_app_mgr_client.h b/services/test/mock/include/mock_app_mgr_client.h index c7ba6fdf679d031afd6861a0110726d3114f2a19..6277e5e76edf7c391933ac576b45c9fbe848f0fa 100644 --- a/services/test/mock/include/mock_app_mgr_client.h +++ b/services/test/mock/include/mock_app_mgr_client.h @@ -34,6 +34,9 @@ public: MOCK_METHOD1(KillProcessesByUserId, AppMgrResultCode(int32_t userId)); MOCK_METHOD4(CompelVerifyPermission, int(const std::string &permission, int pid, int uid, std::string &message)); MOCK_METHOD1(AbilityAttachTimeOut, void(const sptr &token)); + MOCK_METHOD2(GetRunningProcessInfoByToken, void((const sptr &token, + AppExecFwk::RunningProcessInfo &info))); + MOCK_METHOD1(GetAllRunningProcesses, AppMgrResultCode(std::vector &info)); AppMgrResultCode AbilityBehaviorAnalysis(const sptr &token, const sptr &preToken, const int32_t visibility, const int32_t perceptibility, const int32_t connectionState) override; diff --git a/services/test/mock/include/mock_bundle_mgr.h b/services/test/mock/include/mock_bundle_mgr.h index 7d6a92b32791d90bce78916e273d087a64367519..e7c8ffea07fdf2463beefcad5a56ea8f16419481 100644 --- a/services/test/mock/include/mock_bundle_mgr.h +++ b/services/test/mock/include/mock_bundle_mgr.h @@ -37,8 +37,16 @@ const std::string COM_IX_HIDATA = "com.ix.hiData"; const std::string COM_IX_PHONE = "com.ix.hiPhone"; const std::string COM_IX_TV = "com.ix.hiTV"; const std::string COM_IX_Film = "com.ix.hiFilm"; +const std::string COM_IX_HIBACKGROUNDMUSIC = "com.ix.hiBackgroundMusic"; +const std::string COM_IX_HIBACKGROUNDDATA = "com.ix.hiBackgroundData"; +const std::string COM_IX_HISINGLEMUSIC = "com.ix.hiSingleMusicInfo"; +const std::string COM_OHOS_Test = "com.ohos.test"; +const std::string COM_IX_HIACCOUNT = "com.ix.hiAccount"; +const std::string COM_IX_HIACCOUNTSERVICE = "com.ix.hiAccountService"; constexpr int32_t MAX_SYS_UID = 2899; constexpr int32_t ROOT_UID = 0; +const int32_t BASE_USER_RANGE = 200000; +const int32_t MOCK_MAIN_UID = 20000000; auto HiWordInfo = [](std::string bundleName, AbilityInfo &abilityInfo, ElementName &elementTemp) { abilityInfo.name = elementTemp.GetAbilityName(); @@ -191,6 +199,102 @@ auto HiFilmInfo = [](std::string bundleName, AbilityInfo &abilityInfo, ElementNa } return true; }; +auto HiBAckgroundMusicInfo = [](std::string bundleName, AbilityInfo &abilityInfo, ElementName &elementTemp) { + abilityInfo.name = elementTemp.GetAbilityName(); + abilityInfo.bundleName = elementTemp.GetBundleName(); + abilityInfo.applicationInfo.bundleName = elementTemp.GetBundleName(); + abilityInfo.applicationInfo.singleUser = true; + abilityInfo.applicationName = "hiBackgroundMusic"; + abilityInfo.applicationInfo.name = "hiBackgroundMusic"; + abilityInfo.type = AbilityType::SERVICE; + abilityInfo.process = "p4"; + return true; +}; + +auto HiBAckgroundDataInfo = [](std::string bundleName, AbilityInfo &abilityInfo, ElementName &elementTemp) { + abilityInfo.name = elementTemp.GetAbilityName(); + abilityInfo.bundleName = elementTemp.GetBundleName(); + abilityInfo.applicationInfo.bundleName = elementTemp.GetBundleName(); + abilityInfo.applicationInfo.singleUser = true; + abilityInfo.applicationName = "hiBackgroundData"; + abilityInfo.applicationInfo.name = "hiBackgroundData"; + abilityInfo.type = AbilityType::SERVICE; + abilityInfo.process = "p4"; + return true; +}; + +auto HiSingleMusicInfo = [](std::string bundleName, AbilityInfo &abilityInfo, ElementName &elementTemp) { + abilityInfo.name = elementTemp.GetAbilityName(); + abilityInfo.bundleName = elementTemp.GetBundleName(); + abilityInfo.applicationInfo.bundleName = elementTemp.GetBundleName(); + abilityInfo.applicationInfo.singleUser = true; + abilityInfo.applicationName = "hiSingleMusic"; + abilityInfo.applicationInfo.name = "hiSingleMusic"; + abilityInfo.type = AbilityType::PAGE; + abilityInfo.process = "p3"; + if (elementTemp.GetAbilityName() == "SingleMusicAbility") { + abilityInfo.launchMode = LaunchMode::STANDARD; + } + if (elementTemp.GetAbilityName() == "SingleMusicTopAbility") { + abilityInfo.launchMode = LaunchMode::SINGLETON; + } + return true; +}; + +auto TestInfo = [](std::string bundleName, AbilityInfo &abilityInfo, ElementName &elementTemp) { + abilityInfo.name = elementTemp.GetAbilityName(); + abilityInfo.bundleName = elementTemp.GetBundleName(); + abilityInfo.applicationInfo.bundleName = elementTemp.GetBundleName(); + abilityInfo.applicationName = "com.ohos.test"; + abilityInfo.applicationInfo.name = "com.ohos.test"; + abilityInfo.moduleName = ".MyApplication"; + abilityInfo.package = "com.ohos.test"; + abilityInfo.type = AbilityType::PAGE; + abilityInfo.applicationInfo.isLauncherApp = false; + abilityInfo.kind = "page"; + abilityInfo.process = "p10"; + abilityInfo.labelId = 10001; + abilityInfo.label = "$string:label"; + abilityInfo.descriptionId = 10002; + abilityInfo.description = "$string:mainability_description"; + abilityInfo.iconId = 10003; + abilityInfo.iconPath = "$media:icon"; + abilityInfo.theme = "mytheme"; + abilityInfo.readPermission = "readPermission"; + abilityInfo.writePermission = "writePermission"; + abilityInfo.resourcePath = "/data/app/com.ohos.test"; + abilityInfo.srcPath = "/resources/base/profile"; + abilityInfo.srcLanguage = "C++"; + abilityInfo.isLauncherAbility = false; + abilityInfo.isNativeAbility = false; + abilityInfo.enabled = false; + abilityInfo.supportPipMode = false; + abilityInfo.formEnabled = false; + return true; +}; +auto HiAccountInfo = [](std::string bundleName, AbilityInfo &abilityInfo, ElementName &elementTemp) { + abilityInfo.name = elementTemp.GetAbilityName(); + abilityInfo.bundleName = elementTemp.GetBundleName(); + abilityInfo.applicationInfo.bundleName = elementTemp.GetBundleName(); + abilityInfo.applicationInfo.uid = MOCK_MAIN_UID; + abilityInfo.applicationName = "AccountTest"; + abilityInfo.applicationInfo.name = "AccountTest"; + abilityInfo.type = AbilityType::PAGE; + abilityInfo.applicationInfo.isLauncherApp = true; + return true; +}; + +auto HiAccountServiceInfo = [](std::string bundleName, AbilityInfo &abilityInfo, ElementName &elementTemp) { + abilityInfo.name = elementTemp.GetAbilityName(); + abilityInfo.bundleName = elementTemp.GetBundleName(); + abilityInfo.applicationInfo.bundleName = elementTemp.GetBundleName(); + abilityInfo.applicationInfo.uid = MOCK_MAIN_UID; + abilityInfo.applicationName = "AccountServiceTest"; + abilityInfo.applicationInfo.name = "AccountServiceTest"; + abilityInfo.type = AbilityType::SERVICE; + abilityInfo.applicationInfo.isLauncherApp = true; + return true; +}; } // namespace class BundleMgrProxy : public IRemoteProxy { public: @@ -270,7 +374,6 @@ public: MOCK_METHOD2(QueryWantAbility, int(const AAFwk::Want &want, std::vector &abilityInfos)); MOCK_METHOD3(GetApplicationInfos, bool(const ApplicationFlag flag, const int userId, std::vector &appInfos)); - MOCK_METHOD3(GetBundleInfos, bool(const BundleFlag flag, std::vector &bundleInfos, int32_t userId)); MOCK_METHOD2(GetBundleNameForUid, bool(const int uid, std::string &bundleName)); MOCK_METHOD2(GetBundleGids, bool(const std::string &bundleName, std::vector &gids)); MOCK_METHOD1(GetAppType, std::string(const std::string &bundleName)); @@ -372,6 +475,13 @@ public: { return true; } + virtual bool GetBundleInfos( + const BundleFlag flag, + std::vector &bundleInfos, + int32_t userId = Constants::UNSPECIFIED_USERID) + { + return false; + } BundleMgrService() { abilityInfoMap_.emplace(COM_IX_HIWORLD, HiWordInfo); @@ -383,6 +493,12 @@ public: abilityInfoMap_.emplace(COM_IX_PHONE, HiPhoneInfo); abilityInfoMap_.emplace(COM_IX_TV, HiTVInfo); abilityInfoMap_.emplace(COM_IX_Film, HiFilmInfo); + abilityInfoMap_.emplace(COM_IX_HIBACKGROUNDMUSIC, HiBAckgroundMusicInfo); + abilityInfoMap_.emplace(COM_IX_HIBACKGROUNDDATA, HiBAckgroundDataInfo); + abilityInfoMap_.emplace(COM_IX_HISINGLEMUSIC, HiSingleMusicInfo); + abilityInfoMap_.emplace(COM_OHOS_Test, TestInfo); + abilityInfoMap_.emplace(COM_IX_HIACCOUNT, HiAccountInfo); + abilityInfoMap_.emplace(COM_IX_HIACCOUNTSERVICE, HiAccountServiceInfo); } virtual ~BundleMgrService() diff --git a/services/test/mock/src/mock_bundle_mgr.cpp b/services/test/mock/src/mock_bundle_mgr.cpp index 390ba3a2a7162e4a321cfa32128c8e99738d1e5d..419d6ef5756fbfa19b608edb8abfe218b585849e 100644 --- a/services/test/mock/src/mock_bundle_mgr.cpp +++ b/services/test/mock/src/mock_bundle_mgr.cpp @@ -22,6 +22,9 @@ using namespace OHOS::AAFwk; namespace OHOS { namespace AppExecFwk { +namespace { +const int32_t ERROR_USER_ID_U256 = 256; +} int BundleMgrProxy::QueryWantAbility( const AAFwk::Want &__attribute__((unused)) want, std::vector &__attribute__((unused)) abilityInfos) @@ -82,7 +85,12 @@ bool BundleMgrService::GetBundleInfo( bool BundleMgrService::QueryAbilityInfo(const AAFwk::Want &want, int32_t flags, int32_t userId, AbilityInfo &abilityInfo) { - return QueryAbilityInfo(want, abilityInfo); + auto flag = QueryAbilityInfo(want, abilityInfo); + if (userId == ERROR_USER_ID_U256) { + abilityInfo.applicationInfo.singleUser = false; + } + return flag; + } bool BundleMgrService::QueryAbilityInfo(const AAFwk::Want &want, AbilityInfo &abilityInfo) @@ -109,9 +117,15 @@ bool BundleMgrService::QueryAbilityInfo(const AAFwk::Want &want, AbilityInfo &ab return true; } } - if (std::string::npos != elementTemp.GetBundleName().find("service")) { + if (std::string::npos != elementTemp.GetBundleName().find("Service")) { abilityInfo.type = AppExecFwk::AbilityType::SERVICE; } + if (std::string::npos != elementTemp.GetBundleName().find("Data")) { + abilityInfo.type = AppExecFwk::AbilityType::DATA; + } + if (std::string::npos != elementTemp.GetBundleName().find("Extension")) { + abilityInfo.type = AppExecFwk::AbilityType::EXTENSION; + } abilityInfo.name = elementTemp.GetAbilityName(); abilityInfo.bundleName = elementTemp.GetBundleName(); abilityInfo.applicationName = elementTemp.GetBundleName(); @@ -131,8 +145,9 @@ bool BundleMgrService::QueryAbilityInfo(const AAFwk::Want &want, AbilityInfo &ab bool BundleMgrService::GetApplicationInfo( const std::string &appName, const ApplicationFlag flag, const int userId, ApplicationInfo &appInfo) { - appInfo.name = "test_app"; - appInfo.bundleName = "com.ix.hiMusic"; + appInfo.name = appName; + appInfo.bundleName = appName; + appInfo.uid = userId * BASE_USER_RANGE; return true; } diff --git a/services/test/moduletest/ability_mgr_module_account_test/BUILD.gn b/services/test/moduletest/ability_mgr_module_account_test/BUILD.gn new file mode 100644 index 0000000000000000000000000000000000000000..728c5234a0c691c6d638c32671cc00cc0b5b2bd7 --- /dev/null +++ b/services/test/moduletest/ability_mgr_module_account_test/BUILD.gn @@ -0,0 +1,131 @@ +# Copyright (c) 2021-2022 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/mstabilitymgrservice" + +ohos_moduletest("ability_mgr_module_account_test") { + module_out_path = module_output_path + + include_dirs = [ + "${services_path}/test/mock/include", + "//third_party/jsoncpp/include", + "//foundation/aafwk/standard/frameworks/kits/ability/native/include/distributed_ability_runtime", + "//foundation/aafwk/standard/interfaces/innerkits/dataobs_manager/include", + "//base/hiviewdfx/hicollie/interfaces/native/innerkits/include/xcollie", + ] + + sources = [ "ability_mgr_module_account_test.cpp" ] + sources += [ + "//foundation/aafwk/standard/services/abilitymgr/src/ability_connect_callback_stub.cpp", + "//foundation/aafwk/standard/services/abilitymgr/src/ability_connect_manager.cpp", + "//foundation/aafwk/standard/services/abilitymgr/src/ability_event_handler.cpp", + "//foundation/aafwk/standard/services/abilitymgr/src/ability_manager_proxy.cpp", + "//foundation/aafwk/standard/services/abilitymgr/src/ability_manager_service.cpp", + "//foundation/aafwk/standard/services/abilitymgr/src/ability_manager_stub.cpp", + "//foundation/aafwk/standard/services/abilitymgr/src/ability_mission_info.cpp", + "//foundation/aafwk/standard/services/abilitymgr/src/ability_record.cpp", + "//foundation/aafwk/standard/services/abilitymgr/src/ability_record_info.cpp", + "//foundation/aafwk/standard/services/abilitymgr/src/ability_scheduler_proxy.cpp", + "//foundation/aafwk/standard/services/abilitymgr/src/ability_scheduler_stub.cpp", + "//foundation/aafwk/standard/services/abilitymgr/src/ability_stack_manager.cpp", + "//foundation/aafwk/standard/services/abilitymgr/src/ability_token_stub.cpp", + "//foundation/aafwk/standard/services/abilitymgr/src/call_container.cpp", + "//foundation/aafwk/standard/services/abilitymgr/src/call_record.cpp", + "//foundation/aafwk/standard/services/abilitymgr/src/caller_info.cpp", + "//foundation/aafwk/standard/services/abilitymgr/src/connection_record.cpp", + "//foundation/aafwk/standard/services/abilitymgr/src/data_ability_manager.cpp", + "//foundation/aafwk/standard/services/abilitymgr/src/data_ability_record.cpp", + "//foundation/aafwk/standard/services/abilitymgr/src/launch_param.cpp", + "//foundation/aafwk/standard/services/abilitymgr/src/lifecycle_deal.cpp", + "//foundation/aafwk/standard/services/abilitymgr/src/lifecycle_state_info.cpp", + "//foundation/aafwk/standard/services/abilitymgr/src/mission_description_info.cpp", + "//foundation/aafwk/standard/services/abilitymgr/src/mission_option.cpp", + "//foundation/aafwk/standard/services/abilitymgr/src/mission_record.cpp", + "//foundation/aafwk/standard/services/abilitymgr/src/mission_record_info.cpp", + "//foundation/aafwk/standard/services/abilitymgr/src/mission_snapshot.cpp", + "//foundation/aafwk/standard/services/abilitymgr/src/mission_stack.cpp", + "//foundation/aafwk/standard/services/abilitymgr/src/mission_stack_info.cpp", + "//foundation/aafwk/standard/services/abilitymgr/src/pending_want_key.cpp", + "//foundation/aafwk/standard/services/abilitymgr/src/pending_want_manager.cpp", + "//foundation/aafwk/standard/services/abilitymgr/src/pending_want_record.cpp", + "//foundation/aafwk/standard/services/abilitymgr/src/power_storage.cpp", + "//foundation/aafwk/standard/services/abilitymgr/src/sender_info.cpp", + "//foundation/aafwk/standard/services/abilitymgr/src/stack_info.cpp", + "//foundation/aafwk/standard/services/abilitymgr/src/want_receiver_stub.cpp", + "//foundation/aafwk/standard/services/abilitymgr/src/want_sender_info.cpp", + "//foundation/aafwk/standard/services/abilitymgr/src/want_sender_stub.cpp", + "//foundation/aafwk/standard/services/abilitymgr/src/wants_info.cpp", + "//foundation/aafwk/standard/services/abilitymgr/test/mock/libs/sa_mgr/src/sa_mgr_client_mock.cpp", + "//foundation/aafwk/standard/services/test/mock/src/mock_app_mgr_client.cpp", + "//foundation/aafwk/standard/services/test/mock/src/mock_bundle_mgr.cpp", + ] + + configs = [ + "${services_path}/test:aafwk_module_test_config", + "//foundation/aafwk/standard/interfaces/innerkits/ability_manager:ability_manager_public_config", + "//foundation/aafwk/standard/services/abilitymgr/test/mock/libs/sa_mgr:sa_mgr_mock_config", + "//foundation/appexecfwk/standard/interfaces/innerkits/appexecfwk_base:appexecfwk_base_sdk_config", + "${aafwk_path}/interfaces/innerkits/app_manager:appmgr_sdk_config", + ] + cflags = [] + if (target_cpu == "arm") { + cflags += [ "-DBINDER_IPC_32BIT" ] + } + deps = [ + "${aafwk_path}/interfaces/innerkits/app_manager:app_manager", + "${innerkits_path}/uri_permission:uri_permission_mgr", + "${innerkits_path}/want:want", + "${services_path}/common:perm_verification", + "//base/account/os_account/frameworks/osaccount/native:os_account_innerkits", + "//base/global/i18n_standard/frameworks/intl:intl_util", + "//base/startup/syspara_lite/interfaces/innerkits/native/syspara:syspara", + "//foundation/aafwk/standard/frameworks/kits/ability/native:abilitykit_native", + "//foundation/aafwk/standard/frameworks/kits/ability/native:dummy_classes", + "//foundation/aafwk/standard/services/abilitymgr:abilityms", + "//foundation/ace/ace_engine/interfaces/innerkits/ui_service_manager:ui_service_mgr", + "//foundation/appexecfwk/standard/interfaces/innerkits/appexecfwk_base:appexecfwk_base", + "//foundation/appexecfwk/standard/interfaces/innerkits/appexecfwk_core:appexecfwk_core", + "//foundation/appexecfwk/standard/interfaces/innerkits/libeventhandler:libeventhandler", + "//foundation/distributedschedule/dmsfwk/interfaces/innerkits/uri:zuri", + "//foundation/distributedschedule/safwk/interfaces/innerkits/safwk:system_ability_fwk", + "//foundation/distributedschedule/samgr/interfaces/innerkits/samgr_proxy:samgr_proxy", + "//foundation/multimedia/image_standard/interfaces/innerkits:image_native", + "//third_party/googletest:gmock_main", + "//third_party/googletest:gtest_main", + "//third_party/jsoncpp:jsoncpp", + "//third_party/libpng:libpng", + "//utils/native/base:utils", + ] + + external_deps = [ + "access_token:libaccesstoken_sdk", + "bytrace_standard:bytrace_core", + "ces_standard:cesfwk_core", + "ces_standard:cesfwk_innerkits", + "dsoftbus_standard:softbus_client", + "hicollie_native:libhicollie", + "hiviewdfx_hilog_native:libhilog", + "ipc:ipc_core", + "native_appdatamgr:native_appdatafwk", + "native_appdatamgr:native_dataability", + "native_appdatamgr:native_rdb", + ] +} + +group("moduletest") { + testonly = true + deps = [ ":ability_mgr_module_account_test" ] +} diff --git a/services/test/moduletest/ability_mgr_module_account_test/ability_mgr_module_account_test.cpp b/services/test/moduletest/ability_mgr_module_account_test/ability_mgr_module_account_test.cpp new file mode 100644 index 0000000000000000000000000000000000000000..acc398dc693ddc1bd148a307a2fc184014704497 --- /dev/null +++ b/services/test/moduletest/ability_mgr_module_account_test/ability_mgr_module_account_test.cpp @@ -0,0 +1,878 @@ +/* + * 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 +#include +#include +#include + +#include +#include + +#define private public +#define protected public +#include "sa_mgr_client.h" +#include "mock_ability_connect_callback_stub.h" +#include "mock_ability_scheduler.h" +#include "mock_app_mgr_client.h" +#include "mock_bundle_mgr.h" +#include "ability_record_info.h" +#include "ability_manager_errors.h" +#include "sa_mgr_client.h" +#include "system_ability_definition.h" +#include "ability_manager_service.h" +#include "ability_connect_callback_proxy.h" +#include "ability_config.h" +#include "pending_want_manager.h" +#include "pending_want_record.h" +#undef private +#undef protected +#include "wants_info.h" +#include "want_receiver_stub.h" +#include "want_sender_stub.h" +#include "os_account_manager.h" + +using namespace std::placeholders; +using namespace testing::ext; +using namespace OHOS::AppExecFwk; +using OHOS::iface_cast; +using OHOS::IRemoteObject; +using OHOS::sptr; +using testing::_; +using testing::Invoke; +using testing::Return; + +namespace OHOS { +namespace AAFwk { +namespace { +const int32_t MOCK_MAIN_USER_ID = 100; +const int32_t ERROR_USER_ID_U256 = 256; +} // namespace +class AbilityMgrModuleAccountTest : public testing::Test { +public: + static void SetUpTestCase(void); + static void TearDownTestCase(void); + void SetUp(); + void TearDown(); + void WaitAMS(); + bool MockAppClent(); + Want CreateWant(const std::string &abilityName, const std::string &bundleName); + AbilityInfo CreateAbilityInfo(const std::string &name, const std::string &appName, const std::string &bundleName); + void CreateAbilityRequest(const std::string &abilityName, const std::string bundleName, Want &want, + std::shared_ptr &curMissionStack, sptr &recordToken); + void MockServiceAbilityLoadHandlerInner(bool &testResult, const std::string &bundleName, + const std::string &abilityName, sptr &testToken); + ApplicationInfo CreateAppInfo(const std::string &appName, const std::string &name); + inline static std::shared_ptr abilityMgrServ_ {nullptr}; + inline static std::shared_ptr mockAppMgrClient_ {nullptr}; + sptr scheduler_ {nullptr}; + inline static bool doOnce_ = false; +}; + +bool AbilityMgrModuleAccountTest::MockAppClent() +{ + if (!mockAppMgrClient_) { + GTEST_LOG_(INFO) << "MockAppClent::1"; + return false; + } + + if (!abilityMgrServ_->appScheduler_) { + GTEST_LOG_(INFO) << "MockAppClent::2"; + return false; + } + + abilityMgrServ_->appScheduler_->appMgrClient_.reset(mockAppMgrClient_.get()); + return true; +} + +Want AbilityMgrModuleAccountTest::CreateWant(const std::string &abilityName, const std::string &bundleName) +{ + ElementName element; + element.SetDeviceID(""); + element.SetAbilityName(abilityName); + element.SetBundleName(bundleName); + Want want; + want.SetElement(element); + return want; +} + +AbilityInfo AbilityMgrModuleAccountTest::CreateAbilityInfo( + const std::string &name, const std::string &appName, const std::string &bundleName) +{ + AbilityInfo abilityInfo; + abilityInfo.visible = true; + abilityInfo.name = name; + abilityInfo.applicationName = appName; + abilityInfo.bundleName = bundleName; + abilityInfo.applicationInfo.bundleName = bundleName; + abilityInfo.applicationInfo.name = name; + abilityInfo.type = AbilityType::PAGE; + abilityInfo.applicationInfo.isLauncherApp = false; + + return abilityInfo; +} + +ApplicationInfo AbilityMgrModuleAccountTest::CreateAppInfo(const std::string &appName, const std::string &bundleName) +{ + ApplicationInfo appInfo; + appInfo.name = appName; + appInfo.bundleName = bundleName; + + return appInfo; +} + +void AbilityMgrModuleAccountTest::CreateAbilityRequest(const std::string &abilityName, const std::string bundleName, + Want &want, std::shared_ptr &curMissionStack, sptr &recordToken) +{ + Want want2 = CreateWant(abilityName, bundleName); + AbilityRequest abilityRequest2; + abilityRequest2.want = want2; + abilityRequest2.abilityInfo.type = OHOS::AppExecFwk::AbilityType::PAGE; + abilityRequest2.abilityInfo = CreateAbilityInfo(abilityName, bundleName, bundleName); + + std::shared_ptr abilityRecord2 = AbilityRecord::CreateAbilityRecord(abilityRequest2); + abilityRecord2->SetAbilityState(OHOS::AAFwk::AbilityState::ACTIVE); + std::shared_ptr mission = std::make_shared(bundleName); + mission->AddAbilityRecordToTop(abilityRecord2); + auto stackManager_ = abilityMgrServ_->GetStackManager(); + curMissionStack = stackManager_->GetCurrentMissionStack(); + curMissionStack->AddMissionRecordToTop(mission); + recordToken = abilityRecord2->GetToken(); + + want = CreateWant(abilityName, bundleName); + AbilityRequest abilityRequest; + abilityRequest.want = want; + abilityRequest.abilityInfo = + CreateAbilityInfo(abilityName, bundleName, bundleName); + abilityRequest.abilityInfo.type = OHOS::AppExecFwk::AbilityType::SERVICE; + abilityRequest.appInfo = CreateAppInfo(bundleName, bundleName); + abilityMgrServ_->RemoveAllServiceRecord(); +} + +void AbilityMgrModuleAccountTest::MockServiceAbilityLoadHandlerInner( + bool &testResult, const std::string &bundleName, const std::string &abilityName, sptr &testToken) +{ + auto mockHandler = [&testResult, &bundleName, &abilityName, &testToken](const sptr &token, + const sptr &preToken, + const AbilityInfo &abilityInfo, + const ApplicationInfo &appInfo, + const Want &want) { + testToken = token; + testResult = !!testToken && abilityInfo.bundleName == bundleName && abilityInfo.name == abilityName && + appInfo.bundleName == bundleName; + return AppMgrResultCode::RESULT_OK; + }; + + EXPECT_CALL(*mockAppMgrClient_, LoadAbility(_, _, _, _, _)).Times(1).WillOnce(Invoke(mockHandler)); +} + +void AbilityMgrModuleAccountTest::WaitAMS() +{ + const uint32_t maxRetryCount = 1000; + const uint32_t sleepTime = 1000; + uint32_t count = 0; + if (!abilityMgrServ_) { + return; + } + auto handler = abilityMgrServ_->GetEventHandler(); + if (!handler) { + return; + } + std::atomic taskCalled(false); + auto f = [&taskCalled]() { taskCalled.store(true); }; + if (handler->PostTask(f)) { + while (!taskCalled.load()) { + ++count; + if (count >= maxRetryCount) { + break; + } + usleep(sleepTime); + } + } +} + +void AbilityMgrModuleAccountTest::SetUpTestCase() +{ + GTEST_LOG_(INFO) << "AbilityMgrModuleAccountTest SetUpTestCase called"; + OHOS::DelayedSingleton::GetInstance()->RegisterSystemAbility( + OHOS::BUNDLE_MGR_SERVICE_SYS_ABILITY_ID, new BundleMgrService()); + abilityMgrServ_ = OHOS::DelayedSingleton::GetInstance(); + mockAppMgrClient_ = std::make_shared(); + abilityMgrServ_->OnStart(); + abilityMgrServ_->StartUser(MOCK_MAIN_USER_ID); +} + +void AbilityMgrModuleAccountTest::TearDownTestCase() +{ + GTEST_LOG_(INFO) << "AbilityMgrModuleAccountTest TearDownTestCase called"; + abilityMgrServ_->OnStop(); + mockAppMgrClient_.reset(); + OHOS::DelayedSingleton::DestroyInstance(); +} + +void AbilityMgrModuleAccountTest::SetUp() +{ + GTEST_LOG_(INFO) << "AbilityMgrModuleAccountTest SetUp called"; + scheduler_ = new MockAbilityScheduler(); + if (!doOnce_) { + doOnce_ = true; + MockAppClent(); + } + WaitAMS(); +} + +void AbilityMgrModuleAccountTest::TearDown() +{} + +/* + * Feature: AaFwk + * Function: AbilityMgrModuleAccountTest_StartAbilityInner_001 + * SubFunction: NA + * FunctionPoints: Start single PageAbility with 0 user authentication StartAbilityInner interface + * EnvConditions: NA + * CaseDescription: terminate ability. + */ +HWTEST_F(AbilityMgrModuleAccountTest, AbilityMgrModuleAccountTest_StartAbilityInner_001, TestSize.Level1) +{ + GTEST_LOG_(INFO) << "AbilityMgrModuleAccountTest AbilityMgrModuleAccountTest_StartAbilityInner_001 start"; + Want want; + ElementName element("", "com.ix.hiSingleMusicInfo", "SingleMusicAbility"); + want.SetElement(element); + EXPECT_CALL(*mockAppMgrClient_, LoadAbility(_, _, _, _, _)).Times(1).WillOnce(Return(AppMgrResultCode::RESULT_OK)); + auto result = abilityMgrServ_->StartAbilityInner(want, nullptr, -1); + WaitAMS(); + EXPECT_EQ(OHOS::ERR_OK, result); + + GTEST_LOG_(INFO) << "AbilityMgrModuleAccountTest AbilityMgrModuleAccountTest_StartAbilityInner_001 end"; +} + +/* + * Feature: AaFwk + * Function: AbilityMgrModuleAccountTest_StartAbilityInner_002 + * SubFunction: NA + * FunctionPoints: Start single ServiceAbility with 0 user authentication StartAbilityInner interface + * EnvConditions: NA + * CaseDescription: terminate ability. + */ +HWTEST_F(AbilityMgrModuleAccountTest, AbilityMgrModuleAccountTest_StartAbilityInner_002, TestSize.Level1) +{ + GTEST_LOG_(INFO) << "AbilityMgrModuleAccountTest AbilityMgrModuleAccountTest_StartAbilityInner_002 start"; + Want want; + ElementName element("", "com.ix.hiBackgroundMusic", "hiBackgroundMusic"); + want.SetElement(element); + EXPECT_CALL(*mockAppMgrClient_, LoadAbility(_, _, _, _, _)).Times(1).WillOnce(Return(AppMgrResultCode::RESULT_OK)); + auto result = abilityMgrServ_->StartAbilityInner(want, nullptr, -1); + WaitAMS(); + EXPECT_EQ(OHOS::ERR_OK, result); + GTEST_LOG_(INFO) << "AbilityMgrModuleAccountTest AbilityMgrModuleAccountTest_StartAbilityInner_002 end"; +} + +/* + * Feature: AaFwk + * Function: AbilityMgrModuleAccountTest_StartAbilityInner_003 + * SubFunction: NA + * FunctionPoints: Start single PageAbility with 100 user authentication StartAbilityInner interface + * EnvConditions: NA + * CaseDescription: terminate ability. + */ +HWTEST_F(AbilityMgrModuleAccountTest, AbilityMgrModuleAccountTest_StartAbilityInner_003, TestSize.Level1) +{ + GTEST_LOG_(INFO) << "AbilityMgrModuleAccountTest AbilityMgrModuleAccountTest_StartAbilityInner_003 start"; + Want want; + ElementName element("", "com.ix.hiSingleMusicInfo", "SingleMusicAbility"); + want.SetElement(element); + EXPECT_CALL(*mockAppMgrClient_, LoadAbility(_, _, _, _, _)).Times(1).WillOnce(Return(AppMgrResultCode::RESULT_OK)); + auto result = abilityMgrServ_->StartAbilityInner(want, nullptr, -1, -1, MOCK_MAIN_USER_ID); + WaitAMS(); + EXPECT_EQ(OHOS::ERR_OK, result); + GTEST_LOG_(INFO) << "AbilityMgrModuleAccountTest AbilityMgrModuleAccountTest_StartAbilityInner_003 start"; +} + +/* + * Feature: AaFwk + * Function: AbilityMgrModuleAccountTest_StartAbilityInner_004 + * SubFunction: NA + * FunctionPoints: Start single ServiceAbility with 100 user authentication StartAbilityInner interface + * EnvConditions: NA + * CaseDescription: terminate ability. + */ +HWTEST_F(AbilityMgrModuleAccountTest, AbilityMgrModuleAccountTest_StartAbilityInner_004, TestSize.Level1) +{ + Want want; + GTEST_LOG_(INFO) << "AbilityMgrModuleAccountTest AbilityMgrModuleAccountTest_StartAbilityInner_004 start"; + ElementName element("", "com.ix.hiBackgroundData", "hiBackgroundData"); + want.SetElement(element); + EXPECT_CALL(*mockAppMgrClient_, LoadAbility(_, _, _, _, _)).Times(1).WillOnce(Return(AppMgrResultCode::RESULT_OK)); + auto result = abilityMgrServ_->StartAbilityInner(want, nullptr, -1, -1, MOCK_MAIN_USER_ID); + WaitAMS(); + EXPECT_EQ(OHOS::ERR_OK, result); + GTEST_LOG_(INFO) << "AbilityMgrModuleAccountTest AbilityMgrModuleAccountTest_StartAbilityInner_004 end"; +} + +/* + * Feature: AaFwk + * Function: AbilityMgrModuleAccountTest_StartAbilityInner_005 + * SubFunction: NA + * FunctionPoints: AbilityManagerService StartAbilityInner + * EnvConditions: NA + * CaseDescription: Start single PageAbility with error user authentication StartAbilityInner interface + */ +HWTEST_F(AbilityMgrModuleAccountTest, AbilityMgrModuleAccountTest_StartAbilityInner_005, TestSize.Level1) +{ + GTEST_LOG_(INFO) << "AbilityMgrModuleAccountTest AbilityMgrModuleAccountTest_StartAbilityInner_005 start"; + Want want; + ElementName element("", "com.ix.hiSingleMusicInfo", "SingleMusicAbility"); + want.SetElement(element); + auto result = abilityMgrServ_->StartAbilityInner(want, nullptr, -1, -1, ERROR_USER_ID_U256); + WaitAMS(); + EXPECT_NE(OHOS::ERR_OK, result); + GTEST_LOG_(INFO) << "AbilityMgrModuleAccountTest AbilityMgrModuleAccountTest_StartAbilityInner_005 end"; +} + +/* + * Feature: AaFwk + * Function: AbilityMgrModuleAccountTest_StartAbilityInner_006 + * SubFunction: NA + * FunctionPoints: AbilityManagerService StartAbilityInner + * EnvConditions: NA + * CaseDescription: Start single ServiceAbility with error user authentication StartAbilityInner interface + */ +HWTEST_F(AbilityMgrModuleAccountTest, AbilityMgrModuleAccountTest_StartAbilityInner_006, TestSize.Level1) +{ + GTEST_LOG_(INFO) << "AbilityMgrModuleAccountTest AbilityMgrModuleAccountTest_StartAbilityInner_006 start"; + Want want; + ElementName element("", "com.ix.hiBackgroundMusic", "hiBackgroundMusic"); + want.SetElement(element); + auto result = abilityMgrServ_->StartAbilityInner(want, nullptr, -1, -1, ERROR_USER_ID_U256); + WaitAMS(); + EXPECT_NE(OHOS::ERR_OK, result); + GTEST_LOG_(INFO) << "AbilityMgrModuleAccountTest AbilityMgrModuleAccountTest_StartAbilityInner_006 end"; +} + +/* + * Feature: AaFwk + * Function: AbilityMgrModuleAccountTest_StartAbility_001 + * SubFunction: NA + * FunctionPoints: AbilityManagerService StartAbility + * EnvConditions: NA + * CaseDescription: Start single PageAbility with 0 user authentication StartAbility interface + */ +HWTEST_F(AbilityMgrModuleAccountTest, AbilityMgrModuleAccountTest_StartAbility_001, TestSize.Level1) +{ + GTEST_LOG_(INFO) << "AbilityMgrModuleAccountTest AbilityMgrModuleAccountTest_StartAbility_001 start"; + Want want; + ElementName element("", "com.ix.hiSingleMusicInfo", "SingleMusicAbility"); + want.SetElement(element); + EXPECT_CALL(*mockAppMgrClient_, LoadAbility(_, _, _, _, _)).Times(1).WillOnce(Return(AppMgrResultCode::RESULT_OK)); + StartOptions abilityStartOptions; + auto result = abilityMgrServ_->StartAbility(want, abilityStartOptions, nullptr, 0, -1); + WaitAMS(); + EXPECT_EQ(OHOS::ERR_OK, result); + GTEST_LOG_(INFO) << "AbilityMgrModuleAccountTest AbilityMgrModuleAccountTest_StartAbility_001 end"; +} + +/* + * Feature: AaFwk + * Function: AbilityMgrModuleAccountTest_StartAbility_002 + * SubFunction: NA + * FunctionPoints: AbilityManagerService StartAbility + * EnvConditions: NA + * CaseDescription: Start single ServiceAbility with 0 user authentication StartAbility interface + */ +HWTEST_F(AbilityMgrModuleAccountTest, AbilityMgrModuleAccountTest_StartAbility_002, TestSize.Level1) +{ + GTEST_LOG_(INFO) << "AbilityMgrModuleAccountTest AbilityMgrModuleAccountTest_StartAbility_002 start"; + Want want; + ElementName element("", "com.ix.hiBackgroundMusic", "hiBackgroundMusic"); + want.SetElement(element); + StartOptions abilityStartOptions; + auto result = abilityMgrServ_->StartAbility(want, abilityStartOptions, nullptr, 0, -1); + WaitAMS(); + EXPECT_NE(OHOS::ERR_OK, result); + GTEST_LOG_(INFO) << "AbilityMgrModuleAccountTest AbilityMgrModuleAccountTest_StartAbility_002 end"; +} + +/* + * Feature: AaFwk + * Function: AbilityMgrModuleAccountTest_StartAbility_003 + * SubFunction: NA + * FunctionPoints: AbilityManagerService StartAbility + * EnvConditions: NA + * CaseDescription: Start single PageAbility with 100 user authentication StartAbility interface + */ +HWTEST_F(AbilityMgrModuleAccountTest, AbilityMgrModuleAccountTest_StartAbility_003, TestSize.Level1) +{ + GTEST_LOG_(INFO) << "AbilityMgrModuleAccountTest AbilityMgrModuleAccountTest_StartAbility_003 start"; + Want want; + ElementName element("", "com.ix.hiSingleMusicInfo", "SingleMusicAbility"); + want.SetElement(element); + EXPECT_CALL(*mockAppMgrClient_, LoadAbility(_, _, _, _, _)).Times(1).WillOnce(Return(AppMgrResultCode::RESULT_OK)); + StartOptions abilityStartOptions; + auto result = abilityMgrServ_->StartAbility(want, abilityStartOptions, nullptr, MOCK_MAIN_USER_ID, -1); + WaitAMS(); + EXPECT_EQ(OHOS::ERR_OK, result); + GTEST_LOG_(INFO) << "AbilityMgrModuleAccountTest AbilityMgrModuleAccountTest_StartAbility_003 end"; +} + +/* + * Feature: AaFwk + * Function: AbilityMgrModuleAccountTest_StartAbility_004 + * SubFunction: NA + * FunctionPoints: AbilityManagerService StartAbility + * EnvConditions: NA + * CaseDescription: Start single ServiceAbility with 100 user authentication StartAbility interface + */ +HWTEST_F(AbilityMgrModuleAccountTest, AbilityMgrModuleAccountTest_StartAbility_004, TestSize.Level1) +{ + GTEST_LOG_(INFO) << "AbilityMgrModuleAccountTest AbilityMgrModuleAccountTest_StartAbility_004 start"; + Want want; + ElementName element("", "com.ix.hiBackgroundMusic", "hiBackgroundMusic"); + want.SetElement(element); + StartOptions abilityStartOptions; + auto result = abilityMgrServ_->StartAbility(want, abilityStartOptions, nullptr, MOCK_MAIN_USER_ID, -1); + WaitAMS(); + EXPECT_NE(OHOS::ERR_OK, result); + GTEST_LOG_(INFO) << "AbilityMgrModuleAccountTest AbilityMgrModuleAccountTest_StartAbility_004 end"; +} + +/* + * Feature: AaFwk + * Function: AbilityMgrModuleAccountTest_StartAbility_005 + * SubFunction: NA + * FunctionPoints: AbilityManagerService StartAbility + * EnvConditions: NA + * CaseDescription: Start single PageAbility with error user authentication StartAbility interface + */ +HWTEST_F(AbilityMgrModuleAccountTest, AbilityMgrModuleAccountTest_StartAbility_005, TestSize.Level1) +{ + GTEST_LOG_(INFO) << "AbilityMgrModuleAccountTest AbilityMgrModuleAccountTest_StartAbility_005 start"; + Want want; + ElementName element("", "com.ix.hiSingleMusicInfo", "SingleMusicAbility"); + want.SetElement(element); + StartOptions abilityStartOptions; + auto result = abilityMgrServ_->StartAbility(want, abilityStartOptions, nullptr, ERROR_USER_ID_U256, -1); + WaitAMS(); + EXPECT_NE(OHOS::ERR_OK, result); + GTEST_LOG_(INFO) << "AbilityMgrModuleAccountTest AbilityMgrModuleAccountTest_StartAbility_005 end"; +} + +/* + * Feature: AaFwk + * Function: AbilityMgrModuleAccountTest_StartAbility_006 + * SubFunction: NA + * FunctionPoints: AbilityManagerService StartAbility + * EnvConditions: NA + * CaseDescription: Start single ServiceAbility with error user authentication StartAbility interface + */ +HWTEST_F(AbilityMgrModuleAccountTest, AbilityMgrModuleAccountTest_StartAbility_006, TestSize.Level1) +{ + GTEST_LOG_(INFO) << "AbilityMgrModuleAccountTest AbilityMgrModuleAccountTest_StartAbility_006 start"; + Want want; + ElementName element("", "com.ix.hiBackgroundMusic", "hiBackgroundMusic"); + want.SetElement(element); + StartOptions abilityStartOptions; + auto result = abilityMgrServ_->StartAbility(want, abilityStartOptions, nullptr, ERROR_USER_ID_U256, -1); + WaitAMS(); + EXPECT_NE(OHOS::ERR_OK, result); + GTEST_LOG_(INFO) << "AbilityMgrModuleAccountTest AbilityMgrModuleAccountTest_StartAbility_006 end"; +} + +/* + * Feature: AaFwk + * Function: AbilityMgrModuleAccountTest_StartAbility_007 + * SubFunction: NA + * FunctionPoints: AbilityManagerService StartAbility + * EnvConditions: NA + * CaseDescription: Specify a user to start a page ability + */ +HWTEST_F(AbilityMgrModuleAccountTest, AbilityMgrModuleAccountTest_StartAbility_007, TestSize.Level1) +{ + GTEST_LOG_(INFO) << "AbilityMgrModuleAccountTest AbilityMgrModuleAccountTest_StartAbility_007 start"; + auto topAbility = abilityMgrServ_->GetListManagerByUserId(MOCK_MAIN_USER_ID)->GetCurrentTopAbilityLocked(); + if (topAbility) { + topAbility->SetAbilityState(AAFwk::AbilityState::FOREGROUND_NEW); + } + Want want; + ElementName element("", "com.ix.hiAccount", "AccountTest"); + want.SetElement(element); + EXPECT_CALL(*mockAppMgrClient_, LoadAbility(_, _, _, _, _)).Times(1).WillOnce(Return(AppMgrResultCode::RESULT_OK)); + int result = abilityMgrServ_->StartAbility(want, MOCK_MAIN_USER_ID, -1); + EXPECT_EQ(ERR_OK, result); + + topAbility = abilityMgrServ_->GetListManagerByUserId(MOCK_MAIN_USER_ID)->GetCurrentTopAbilityLocked(); + sptr token = nullptr; + if (topAbility) { + token = topAbility->GetToken(); + } + sptr scheduler = new MockAbilityScheduler(); + EXPECT_TRUE(scheduler); + EXPECT_CALL(*mockAppMgrClient_, UpdateAbilityState(_, _)).Times(1); + auto resultFunction = abilityMgrServ_->AttachAbilityThread(scheduler, token); + EXPECT_EQ(resultFunction, ERR_OK); + + topAbility = abilityMgrServ_->GetListManagerByUserId(MOCK_MAIN_USER_ID)->GetCurrentTopAbilityLocked(); + EXPECT_CALL(*scheduler, ScheduleAbilityTransaction(_, _)).Times(1); + abilityMgrServ_->OnAbilityRequestDone( + topAbility->GetToken(), (int32_t)OHOS::AppExecFwk::AbilityState::ABILITY_STATE_FOREGROUND); + EXPECT_EQ(OHOS::AAFwk::AbilityState::FOREGROUNDING_NEW, topAbility->GetAbilityState()); + + AppInfo appInfo; + appInfo.processName = topAbility->GetAbilityInfo().process; + appInfo.state = AppState::FOREGROUND; + abilityMgrServ_->OnAppStateChanged(appInfo); + PacMap saveData; + abilityMgrServ_->AbilityTransitionDone(topAbility->GetToken(), + AbilityLifeCycleState::ABILITY_STATE_FOREGROUND_NEW, saveData); + + WaitAMS(); + EXPECT_EQ(OHOS::AAFwk::AbilityState::FOREGROUND_NEW, topAbility->GetAbilityState()); + testing::Mock::AllowLeak(scheduler); + GTEST_LOG_(INFO) << "AbilityMgrModuleAccountTest AbilityMgrModuleAccountTest_StartAbility_007 end"; +} + +/* + * Feature: AaFwk + * Function: AbilityMgrModuleAccountTest_StartAbility_006 + * SubFunction: NA + * FunctionPoints: AbilityManagerService StartAbility + * EnvConditions: NA + * CaseDescription: Do not specify a user to start a page ability + */ +HWTEST_F(AbilityMgrModuleAccountTest, AbilityMgrModuleAccountTest_StartAbility_008, TestSize.Level1) +{ + GTEST_LOG_(INFO) << "AbilityMgrModuleAccountTest AbilityMgrModuleAccountTest_StartAbility_008 start"; + auto topAbility = abilityMgrServ_->GetListManagerByUserId(MOCK_MAIN_USER_ID)->GetCurrentTopAbilityLocked(); + if (topAbility) { + topAbility->SetAbilityState(AAFwk::AbilityState::FOREGROUND_NEW); + } + Want want; + ElementName element("", "com.ix.hiAccount", "AccountTest"); + want.SetElement(element); + EXPECT_CALL(*mockAppMgrClient_, LoadAbility(_, _, _, _, _)).Times(1).WillOnce(Return(AppMgrResultCode::RESULT_OK)); + int result = abilityMgrServ_->StartAbility(want); + EXPECT_EQ(ERR_OK, result); + + topAbility = abilityMgrServ_->GetListManagerByUserId(MOCK_MAIN_USER_ID)->GetCurrentTopAbilityLocked(); + sptr token = nullptr; + if (topAbility) { + token = topAbility->GetToken(); + } + sptr scheduler = new MockAbilityScheduler(); + EXPECT_TRUE(scheduler); + EXPECT_CALL(*mockAppMgrClient_, UpdateAbilityState(_, _)).Times(1); + auto resultFunction = abilityMgrServ_->AttachAbilityThread(scheduler, token); + EXPECT_EQ(resultFunction, ERR_OK); + + topAbility = abilityMgrServ_->GetListManagerByUserId(MOCK_MAIN_USER_ID)->GetCurrentTopAbilityLocked(); + EXPECT_CALL(*scheduler, ScheduleAbilityTransaction(_, _)).Times(1); + abilityMgrServ_->OnAbilityRequestDone( + topAbility->GetToken(), (int32_t)OHOS::AppExecFwk::AbilityState::ABILITY_STATE_FOREGROUND); + EXPECT_EQ(OHOS::AAFwk::AbilityState::FOREGROUNDING_NEW, topAbility->GetAbilityState()); + + AppInfo appInfo; + appInfo.processName = topAbility->GetAbilityInfo().process; + appInfo.state = AppState::FOREGROUND; + abilityMgrServ_->OnAppStateChanged(appInfo); + PacMap saveData; + abilityMgrServ_->AbilityTransitionDone(topAbility->GetToken(), + AbilityLifeCycleState::ABILITY_STATE_FOREGROUND_NEW, saveData); + + WaitAMS(); + EXPECT_EQ(OHOS::AAFwk::AbilityState::FOREGROUND_NEW, topAbility->GetAbilityState()); + testing::Mock::AllowLeak(scheduler); + GTEST_LOG_(INFO) << "AbilityMgrModuleAccountTest AbilityMgrModuleAccountTest_StartAbility_008 end"; +} + +/* + * Feature: AaFwk + * Function: AbilityMgrModuleAccountTest_ConnectAbility_001 + * SubFunction: NA + * FunctionPoints: AbilityManagerService ConnectAbility + * EnvConditions: NA + * CaseDescription: Start single ServiceAbility with 0 user authentication ConnectAbility interface + */ +HWTEST_F(AbilityMgrModuleAccountTest, AbilityMgrModuleAccountTest_ConnectAbility_001, TestSize.Level1) +{ + GTEST_LOG_(INFO) << "AbilityMgrModuleAccountTest AbilityMgrModuleAccountTest_ConnectAbility_001 start"; + Want want; + ElementName element("", "com.ix.hiBackgroundMusic", "hiBackgroundMusic"); + want.SetElement(element); + sptr stub(new MockAbilityConnectCallbackStub()); + const sptr callback(new AbilityConnectionProxy(stub)); + auto result = abilityMgrServ_->ConnectAbility(want, callback, nullptr, 0); + EXPECT_EQ(OHOS::ERR_OK, result); + GTEST_LOG_(INFO) << "AbilityMgrModuleAccountTest AbilityMgrModuleAccountTest_ConnectAbility_001 end"; +} + +/* + * Feature: AaFwk + * Function: AbilityMgrModuleAccountTest_ConnectAbility_002 + * SubFunction: NA + * FunctionPoints: AbilityManagerService ConnectAbility + * EnvConditions: NA + * CaseDescription: Start single ServiceAbility with 100 user authentication ConnectAbility interface + */ +HWTEST_F(AbilityMgrModuleAccountTest, AbilityMgrModuleAccountTest_ConnectAbility_002, TestSize.Level1) +{ + GTEST_LOG_(INFO) << "AbilityMgrModuleAccountTest AbilityMgrModuleAccountTest_ConnectAbility_002 start"; + Want want; + ElementName element("", "com.ix.hiBackgroundMusic", "hiBackgroundMusic"); + want.SetElement(element); + sptr stub(new MockAbilityConnectCallbackStub()); + const sptr callback(new AbilityConnectionProxy(stub)); + auto result = abilityMgrServ_->ConnectAbility(want, callback, nullptr, MOCK_MAIN_USER_ID); + EXPECT_EQ(OHOS::ERR_OK, result); + GTEST_LOG_(INFO) << "AbilityMgrModuleAccountTest AbilityMgrModuleAccountTest_ConnectAbility_002 end"; +} + +/* + * Feature: AaFwk + * Function: AbilityMgrModuleAccountTest_ConnectAbility_003 + * SubFunction: NA + * FunctionPoints: AbilityManagerService ConnectAbility + * EnvConditions: NA + * CaseDescription: Start single ServiceAbility with error user authentication ConnectAbility interface + */ +HWTEST_F(AbilityMgrModuleAccountTest, AbilityMgrModuleAccountTest_ConnectAbility_003, TestSize.Level1) +{ + GTEST_LOG_(INFO) << "AbilityMgrModuleAccountTest AbilityMgrModuleAccountTest_ConnectAbility_003 start"; + Want want; + ElementName element("", "com.ix.hiBackgroundMusic", "hiBackgroundMusic"); + want.SetElement(element); + sptr stub(new MockAbilityConnectCallbackStub()); + const sptr callback(new AbilityConnectionProxy(stub)); + auto result = abilityMgrServ_->ConnectAbility(want, callback, nullptr, ERROR_USER_ID_U256); + EXPECT_NE(OHOS::ERR_OK, result); + GTEST_LOG_(INFO) << "AbilityMgrModuleAccountTest AbilityMgrModuleAccountTest_ConnectAbility_003 end"; +} + +/* + * Feature: AaFwk + * Function: AbilityMgrModuleAccountTest_ConnectAbility_004 + * SubFunction: NA + * FunctionPoints: AbilityManagerService ConnectAbility + * EnvConditions: NA + * CaseDescription: Specify a user to bind a Serviceability + */ +HWTEST_F(AbilityMgrModuleAccountTest, AbilityMgrModuleAccountTest_ConnectAbility_004, TestSize.Level1) +{ + GTEST_LOG_(INFO) << "AbilityMgrModuleAccountTest AbilityMgrModuleAccountTest_ConnectAbility_004 start"; + std::string abilityName = "AccountServiceTest"; + std::string bundleName = "com.ix.hiAccountService"; + + Want want; + std::shared_ptr curMissionStack; + sptr recordToken; + CreateAbilityRequest(abilityName, bundleName, want, curMissionStack, recordToken); + + sptr stub(new MockAbilityConnectCallbackStub()); + const sptr callback(new AbilityConnectionProxy(stub)); + + bool testResult = false; + sptr testToken; + MockServiceAbilityLoadHandlerInner(testResult, bundleName, abilityName, testToken); + + int result = abilityMgrServ_->ConnectAbility(want, callback, recordToken, MOCK_MAIN_USER_ID); + EXPECT_EQ(OHOS::ERR_OK, result); + + EXPECT_EQ((std::size_t)1, abilityMgrServ_->connectManager_->connectMap_.size()); + EXPECT_EQ((std::size_t)1, abilityMgrServ_->connectManager_->serviceMap_.size()); + std::shared_ptr record = + abilityMgrServ_->GetConnectManagerByUserId(MOCK_MAIN_USER_ID)->GetServiceRecordByToken(testToken); + EXPECT_TRUE(record); + ElementName element; + + std::shared_ptr connectRecord = record->GetConnectRecordList().front(); + EXPECT_TRUE(connectRecord); + + sptr scheduler = new MockAbilityScheduler(); + EXPECT_TRUE(scheduler); + + EXPECT_CALL(*mockAppMgrClient_, UpdateAbilityState(_, _)).Times(3); + abilityMgrServ_->AttachAbilityThread(scheduler, record->GetToken()); + EXPECT_TRUE(record->isReady_); + + EXPECT_CALL(*scheduler, ScheduleAbilityTransaction(_, _)).Times(1); + abilityMgrServ_->OnAbilityRequestDone( + record->GetToken(), (int32_t)OHOS::AppExecFwk::AbilityState::ABILITY_STATE_FOREGROUND); + EXPECT_EQ(OHOS::AAFwk::AbilityState::INACTIVATING, record->GetAbilityState()); + + EXPECT_CALL(*scheduler, ScheduleConnectAbility(_)).Times(1); + PacMap saveData; + abilityMgrServ_->AbilityTransitionDone(record->GetToken(), AbilityLifeCycleState::ABILITY_STATE_INACTIVE, saveData); + EXPECT_TRUE(record->GetConnectingRecord()); + EXPECT_EQ(OHOS::AAFwk::ConnectionState::CONNECTING, connectRecord->GetConnectState()); + + EXPECT_CALL(*stub, OnAbilityConnectDone(_, _, _)).Times(1); + abilityMgrServ_->ScheduleConnectAbilityDone(record->GetToken(), nullptr); + + EXPECT_EQ(OHOS::AAFwk::ConnectionState::CONNECTED, connectRecord->GetConnectState()); + EXPECT_EQ(OHOS::AAFwk::AbilityState::ACTIVE, record->GetAbilityState()); + + abilityMgrServ_->RemoveAllServiceRecord(); + curMissionStack->RemoveAll(); + + testing::Mock::AllowLeak(scheduler); + testing::Mock::AllowLeak(stub); + testing::Mock::AllowLeak(callback); + GTEST_LOG_(INFO) << "AbilityMgrModuleAccountTest AbilityMgrModuleAccountTest_ConnectAbility_004 end"; +} + +/* + * Feature: AaFwk + * Function: AbilityMgrModuleAccountTest_ConnectAbility_005 + * SubFunction: NA + * FunctionPoints: AbilityManagerService ConnectAbility + * EnvConditions: NA + * CaseDescription: Do not specify a user to bind a Serviceability + */ +HWTEST_F(AbilityMgrModuleAccountTest, AbilityMgrModuleAccountTest_ConnectAbility_005, TestSize.Level1) +{ + GTEST_LOG_(INFO) << "AbilityMgrModuleAccountTest AbilityMgrModuleAccountTest_ConnectAbility_005 start"; + std::string abilityName = "AccountServiceTest"; + std::string bundleName = "com.ix.hiAccountService"; + + Want want; + std::shared_ptr curMissionStack; + sptr recordToken; + CreateAbilityRequest(abilityName, bundleName, want, curMissionStack, recordToken); + + sptr stub(new MockAbilityConnectCallbackStub()); + const sptr callback(new AbilityConnectionProxy(stub)); + + bool testResult = false; + sptr testToken; + MockServiceAbilityLoadHandlerInner(testResult, bundleName, abilityName, testToken); + + int result = abilityMgrServ_->ConnectAbility(want, callback, recordToken); + EXPECT_EQ(OHOS::ERR_OK, result); + + EXPECT_EQ((std::size_t)1, abilityMgrServ_->connectManager_->connectMap_.size()); + EXPECT_EQ((std::size_t)1, abilityMgrServ_->connectManager_->serviceMap_.size()); + std::shared_ptr record = + abilityMgrServ_->GetConnectManagerByUserId(MOCK_MAIN_USER_ID)->GetServiceRecordByToken(testToken); + EXPECT_TRUE(record); + ElementName element; + + std::shared_ptr connectRecord = record->GetConnectRecordList().front(); + EXPECT_TRUE(connectRecord); + + sptr scheduler = new MockAbilityScheduler(); + EXPECT_TRUE(scheduler); + + EXPECT_CALL(*mockAppMgrClient_, UpdateAbilityState(_, _)).Times(3); + abilityMgrServ_->AttachAbilityThread(scheduler, record->GetToken()); + EXPECT_TRUE(record->isReady_); + + EXPECT_CALL(*scheduler, ScheduleAbilityTransaction(_, _)).Times(1); + abilityMgrServ_->OnAbilityRequestDone( + record->GetToken(), (int32_t)OHOS::AppExecFwk::AbilityState::ABILITY_STATE_FOREGROUND); + EXPECT_EQ(OHOS::AAFwk::AbilityState::INACTIVATING, record->GetAbilityState()); + + EXPECT_CALL(*scheduler, ScheduleConnectAbility(_)).Times(1); + PacMap saveData; + abilityMgrServ_->AbilityTransitionDone(record->GetToken(), AbilityLifeCycleState::ABILITY_STATE_INACTIVE, saveData); + EXPECT_TRUE(record->GetConnectingRecord()); + EXPECT_EQ(OHOS::AAFwk::ConnectionState::CONNECTING, connectRecord->GetConnectState()); + + EXPECT_CALL(*stub, OnAbilityConnectDone(_, _, _)).Times(1); + abilityMgrServ_->ScheduleConnectAbilityDone(record->GetToken(), nullptr); + + EXPECT_EQ(OHOS::AAFwk::ConnectionState::CONNECTED, connectRecord->GetConnectState()); + EXPECT_EQ(OHOS::AAFwk::AbilityState::ACTIVE, record->GetAbilityState()); + + abilityMgrServ_->RemoveAllServiceRecord(); + curMissionStack->RemoveAll(); + + testing::Mock::AllowLeak(scheduler); + testing::Mock::AllowLeak(stub); + testing::Mock::AllowLeak(callback); + GTEST_LOG_(INFO) << "AbilityMgrModuleAccountTest AbilityMgrModuleAccountTest_ConnectAbility_005 end"; +} + +/* + * Feature: AaFwk + * Function: AbilityMgrModuleAccountTest_StopServiceAbility_001 + * SubFunction: NA + * FunctionPoints: AbilityManagerService StopServiceAbility + * EnvConditions: NA + * CaseDescription: Use single ServiceAbility with 0 user authentication StopServiceAbility interface + */ +HWTEST_F(AbilityMgrModuleAccountTest, AbilityMgrModuleAccountTest_StopServiceAbility_001, TestSize.Level1) +{ + GTEST_LOG_(INFO) << "AbilityMgrModuleAccountTest AbilityMgrModuleAccountTest_StopServiceAbility_001 start"; + Want want; + ElementName element("", "com.ix.hiBackgroundMusic", "hiBackgroundMusic"); + want.SetElement(element); + sptr stub(new MockAbilityConnectCallbackStub()); + const sptr callback(new AbilityConnectionProxy(stub)); + auto result = abilityMgrServ_->ConnectAbility(want, callback, nullptr, 0); + EXPECT_EQ(OHOS::ERR_OK, result); + EXPECT_CALL(*stub, OnAbilityDisconnectDone(_, _)).Times(1); + result = abilityMgrServ_->StopServiceAbility(want, 0); + EXPECT_EQ(OHOS::ERR_OK, result); + testing::Mock::AllowLeak(stub); + GTEST_LOG_(INFO) << "AbilityMgrModuleAccountTest AbilityMgrModuleAccountTest_StopServiceAbility_001 end"; +} + +/* + * Feature: AaFwk + * Function: AbilityMgrModuleAccountTest_StopServiceAbility_002 + * SubFunction: NA + * FunctionPoints: AbilityManagerService StopServiceAbility + * EnvConditions: NA + * CaseDescription: Use single ServiceAbility with 100 user authentication StopServiceAbility interface + */ +HWTEST_F(AbilityMgrModuleAccountTest, AbilityMgrModuleAccountTest_StopServiceAbility_002, TestSize.Level1) +{ + GTEST_LOG_(INFO) << "AbilityMgrModuleAccountTest AbilityMgrModuleAccountTest_StopServiceAbility_002 start"; + Want want; + ElementName element("", "com.ix.hiBackgroundMusic", "hiBackgroundMusic"); + want.SetElement(element); + sptr stub(new MockAbilityConnectCallbackStub()); + const sptr callback(new AbilityConnectionProxy(stub)); + auto result = abilityMgrServ_->ConnectAbility(want, callback, nullptr, MOCK_MAIN_USER_ID); + EXPECT_EQ(OHOS::ERR_OK, result); + EXPECT_CALL(*stub, OnAbilityDisconnectDone(_, _)).Times(1); + result = abilityMgrServ_->StopServiceAbility(want, MOCK_MAIN_USER_ID); + EXPECT_EQ(OHOS::ERR_OK, result); + testing::Mock::AllowLeak(stub); + GTEST_LOG_(INFO) << "AbilityMgrModuleAccountTest AbilityMgrModuleAccountTest_StopServiceAbility_002 end"; +} + +/* + * Feature: AaFwk + * Function: AbilityMgrModuleAccountTest_StopServiceAbility_003 + * SubFunction: NA + * FunctionPoints: AbilityManagerService StopServiceAbility + * EnvConditions: NA + * CaseDescription: Use single ServiceAbility with error user authentication StopServiceAbility interface + */ +HWTEST_F(AbilityMgrModuleAccountTest, AbilityMgrModuleAccountTest_StopServiceAbility_003, TestSize.Level1) +{ + GTEST_LOG_(INFO) << "AbilityMgrModuleAccountTest AbilityMgrModuleAccountTest_StopServiceAbility_003 start"; + Want want; + ElementName element("", "com.ix.hiBackgroundMusic", "hiBackgroundMusic"); + want.SetElement(element); + sptr stub(new MockAbilityConnectCallbackStub()); + const sptr callback(new AbilityConnectionProxy(stub)); + auto result = abilityMgrServ_->ConnectAbility(want, callback, nullptr, ERROR_USER_ID_U256); + EXPECT_NE(OHOS::ERR_OK, result); + result = abilityMgrServ_->StopServiceAbility(want, ERROR_USER_ID_U256); + EXPECT_NE(OHOS::ERR_OK, result); + GTEST_LOG_(INFO) << "AbilityMgrModuleAccountTest AbilityMgrModuleAccountTest_StopServiceAbility_003 end"; +} +} // namespace AAFwk +} // namespace OHOS diff --git a/services/test/moduletest/ability_mgr_service_test/BUILD.gn b/services/test/moduletest/ability_mgr_service_test/BUILD.gn index e25767356bc7e761868b105515811de9f5c1dad4..8938b6d6aa3c2849e8319b1d2ce9c0f9919294cf 100644 --- a/services/test/moduletest/ability_mgr_service_test/BUILD.gn +++ b/services/test/moduletest/ability_mgr_service_test/BUILD.gn @@ -92,6 +92,7 @@ ohos_moduletest("ability_mgr_module_test") { "${services_path}/common:perm_verification", "//base/account/os_account/frameworks/osaccount/native:os_account_innerkits", "//base/global/i18n_standard/frameworks/intl:intl_util", + "//base/startup/syspara_lite/interfaces/innerkits/native/syspara:syspara", "//foundation/aafwk/standard/frameworks/kits/ability/native:abilitykit_native", "//foundation/aafwk/standard/frameworks/kits/ability/native:dummy_classes", "//foundation/aafwk/standard/services/abilitymgr:abilityms", diff --git a/services/test/moduletest/ability_stack_test/BUILD.gn b/services/test/moduletest/ability_stack_test/BUILD.gn index 84467eb03d224c18d85ff93cbd4b127e3bffc95a..77a6672ba847739b24c5e5a10a8af1a1cc2c0472 100644 --- a/services/test/moduletest/ability_stack_test/BUILD.gn +++ b/services/test/moduletest/ability_stack_test/BUILD.gn @@ -94,6 +94,7 @@ ohos_moduletest("ability_stack_module_test") { "${services_path}/common:perm_verification", "//base/account/os_account/frameworks/osaccount/native:os_account_innerkits", "//base/global/i18n_standard/frameworks/intl:intl_util", + "//base/startup/syspara_lite/interfaces/innerkits/native/syspara:syspara", "//foundation/aafwk/standard/frameworks/kits/ability/native:abilitykit_native", "//foundation/aafwk/standard/frameworks/kits/ability/native:dummy_classes", "//foundation/aafwk/standard/services/abilitymgr:abilityms", diff --git a/services/test/moduletest/common/ams/BUILD.gn b/services/test/moduletest/common/ams/BUILD.gn index 5259503d20aa15707ade0143d3f800dd6a97d796..6813c43e6d5295fed83bdc92eaa9ab2627e1c570 100755 --- a/services/test/moduletest/common/ams/BUILD.gn +++ b/services/test/moduletest/common/ams/BUILD.gn @@ -70,10 +70,12 @@ ohos_source_set("appmgr_mst_source") { cflags += [ "-DBINDER_IPC_32BIT" ] } public_deps = [ + "${aafwk_path}/interfaces/innerkits/ability_manager:ability_manager", "${aafwk_path}/interfaces/innerkits/app_manager:app_manager", "${appexecfwk_path}/common:libappexecfwk_common", "${appexecfwk_path}/libs/libeventhandler:libeventhandler_target", "${innerkits_path}/uri_permission:uri_permission_mgr", + "${services_path}/common:perm_verification", "//base/account/os_account/frameworks/osaccount/native:os_account_innerkits", "//base/security/permission/interfaces/innerkits/permission_standard/permissionsdk:libpermissionsdk_standard", "//foundation/aafwk/standard/interfaces/innerkits/app_manager:app_manager", @@ -92,6 +94,7 @@ ohos_source_set("appmgr_mst_source") { "bytrace_standard:bytrace_core", "ces_standard:cesfwk_core", "ces_standard:cesfwk_innerkits", + "hicollie_native:libhicollie", "hisysevent_native:libhisysevent", "hiviewdfx_hilog_native:libhilog", "ipc:ipc_core", @@ -106,6 +109,7 @@ group("moduletest") { "app_life_cycle_test:moduletest", "app_mgr_service_test:moduletest", "app_recent_list_test:moduletest", + "app_running_processes_info_module_test:moduletest", "app_running_record_test:moduletest", "app_service_flow_test:moduletest", "ipc_ams_mgr_test:moduletest", @@ -114,5 +118,6 @@ group("moduletest") { "service_app_spawn_client_test:moduletest", "service_event_drive_test:moduletest", "service_start_process_test:moduletest", + "specified_ability_service_test:moduletest", ] } diff --git a/services/test/moduletest/common/ams/app_life_cycle_test/BUILD.gn b/services/test/moduletest/common/ams/app_life_cycle_test/BUILD.gn index 3d24e62bff461dd90bdd7ef2d1ff89a6b96357d6..868eaa1e9c8af8a0ea7cac3c18a39d2df20c0ca6 100755 --- a/services/test/moduletest/common/ams/app_life_cycle_test/BUILD.gn +++ b/services/test/moduletest/common/ams/app_life_cycle_test/BUILD.gn @@ -28,12 +28,16 @@ ohos_moduletest("AmsAppLifeCycleModuleTest") { deps = [ "${services_path}/common:perm_verification", "${services_path}/test/moduletest/common/ams:appmgr_mst_source", + "//foundation/aafwk/standard/frameworks/kits/ability/native:abilitykit_native", ] - external_deps = [ "ipc:ipc_core" ] + external_deps = [ + "ipc:ipc_core", + "samgr_standard:samgr_proxy", + ] } group("moduletest") { testonly = true - # deps = [ ":AmsAppLifeCycleModuleTest" ] + deps = [ ":AmsAppLifeCycleModuleTest" ] } diff --git a/services/test/moduletest/common/ams/app_life_cycle_test/ams_app_life_cycle_module_test.cpp b/services/test/moduletest/common/ams/app_life_cycle_test/ams_app_life_cycle_module_test.cpp index 2e1ec26087bc88cc31c9d2e1188ba8ebcb803a40..16b9b75b909092ea413d166544f4f85cda11f8e8 100755 --- a/services/test/moduletest/common/ams/app_life_cycle_test/ams_app_life_cycle_module_test.cpp +++ b/services/test/moduletest/common/ams/app_life_cycle_test/ams_app_life_cycle_module_test.cpp @@ -21,6 +21,7 @@ #include #include #include "app_launch_data.h" +#include "app_mgr_interface.h" #include "iremote_object.h" #include "app_state_callback_proxy.h" #include "app_log_wrapper.h" @@ -31,6 +32,8 @@ #include "mock_app_spawn_client.h" #include "mock_app_spawn_socket.h" #include "mock_iapp_state_callback.h" +#include "system_ability_definition.h" +#include "sys_mgr_client.h" using namespace testing::ext; using testing::_; @@ -860,66 +863,6 @@ HWTEST_F(AmsAppLifeCycleModuleTest, StateChange_009, TestSize.Level3) } } -/* - * Feature: Ams - * Function: AppLifeCycle - * SubFunction: NA - * FunctionPoints: test get and stop all process. - * EnvConditions: system running normally - * CaseDescription: 1.call loadAbility API to start 100 app - * 2.stop all process - */ -HWTEST_F(AmsAppLifeCycleModuleTest, StateChange_010, TestSize.Level3) -{ - pid_t pid = 1025; - EXPECT_NE(serviceInner_, nullptr); - CHECK_POINTER_IS_NULLPTR(serviceInner_); - std::shared_ptr appRunningRecord = nullptr; - int32_t recordId[APPLICATION_NUM]; - sptr mockAppScheduler[APPLICATION_NUM]; - - TestProcessInfo testProcessInfo; - for (int i = 0; i < APPLICATION_NUM; i++) { - mockAppScheduler[i] = new MockAppScheduler(); - - char index[32]; - int ref = snprintf_s(index, sizeof(index), sizeof(index) - 1, "%d", i); - EXPECT_TRUE(ref > 0); - char name[128]; - ref = snprintf_s(name, sizeof(name), sizeof(name) - 1, "com.ohos.test.helloworld%d", i); - EXPECT_TRUE(ref > 0); - auto abilityInfo = GetAbilityInfo(index, "MainAbility", index, name); - auto appInfo = GetApplicationInfo(name); - auto token = new (std::nothrow) MockAbilityToken(); - pid += i; - - testProcessInfo.pid = pid; - testProcessInfo.isStart = false; - - appRunningRecord = - StartProcessAndLoadAbility(mockAppScheduler[i], token, abilityInfo, appInfo, testProcessInfo); - EXPECT_TRUE(appRunningRecord); - - ChangeAbilityStateAfterAppStart(mockAppScheduler[i], testProcessInfo.pid); - - recordId[i] = appRunningRecord->GetRecordId(); - - CheckState(appRunningRecord, token, AbilityState::ABILITY_STATE_READY, ApplicationState::APP_STATE_READY); - ChangeAbilityStateToForegroud(mockAppScheduler[i], appRunningRecord, token); - - ChangeAbilityStateToBackGroud(mockAppScheduler[i], appRunningRecord, token); - CheckState( - appRunningRecord, token, AbilityState::ABILITY_STATE_BACKGROUND, ApplicationState::APP_STATE_BACKGROUND); - EXPECT_CALL(*mockAppScheduler[i], ScheduleProcessSecurityExit()).Times(1); - } - - sptr bundleMgr = new BundleMgrService(); - serviceInner_->SetBundleManager(bundleMgr.GetRefPtr()); - std::vector allRunningProcessInfo; - serviceInner_->GetAllRunningProcesses(allRunningProcessInfo); - EXPECT_EQ(allRunningProcessInfo.size(), size_t(APPLICATION_NUM)); -} - /* * Feature: Ams * Function: AppLifeCycle @@ -1397,55 +1340,6 @@ HWTEST_F(AmsAppLifeCycleModuleTest, AbilityBehaviorAnalysis_06, TestSize.Level1) EXPECT_EQ(abilityRecord_1->GetConnectionState(), connectionState); } -/* - * Feature: AbilityMgr - * Function: StartResidentProcess - * SubFunction: NA - * FunctionPoints: NA - * EnvConditions: NA - * CaseDescription: NA - */ -HWTEST_F(AmsAppLifeCycleModuleTest, StartResidentProcess_01, TestSize.Level1) -{ - pid_t pid = 123; - sptr token = GetAbilityToken(); - std::string appName = "KeepAliveApp"; - std::string proc = "KeepAliveApplication"; - int uid = 2100; - - std::vector infos; - BundleInfo info; - info.name = proc; - info.uid = uid; - - ApplicationInfo appInfo; - appInfo.name = "KeepAliveApp"; - appInfo.bundleName = "KeepAliveApplication"; - appInfo.uid = 2100; - - info.applicationInfo = appInfo; - HapModuleInfo hapModuleInfo; - hapModuleInfo.name = "Module"; - HapModuleInfo hapModuleInfo1; - hapModuleInfo1.name = "Module1"; - info.hapModuleInfos.push_back(hapModuleInfo); - info.hapModuleInfos.push_back(hapModuleInfo1); - - infos.push_back(info); - - sptr mockAppScheduler = new MockAppScheduler(); - - auto appRecord = serviceInner_->appRunningManager_->CheckAppRunningRecordIsExist(appName, proc, uid, info); - EXPECT_FALSE(appRecord); - - StartAppProcess(pid); - serviceInner_->StartResidentProcess(infos, -1); - appRecord = serviceInner_->appRunningManager_->CheckAppRunningRecordIsExist(appName, proc, uid, info); - EXPECT_TRUE(appRecord); - pid_t newPid = appRecord->GetPriorityObject()->GetPid(); - EXPECT_TRUE(newPid == pid); -} - /* * Feature: AbilityMgr * Function: LoadAbility @@ -1777,5 +1671,529 @@ HWTEST_F(AmsAppLifeCycleModuleTest, KillApplication_003, TestSize.Level1) auto appMap1 = serviceInner_->appRunningManager_->GetAppRunningRecordMap(); EXPECT_EQ(0, static_cast(appMap1.size())); } + +/* + * Feature: AbilityMgr + * Function: UpdateConfiguration + * SubFunction: NA + * FunctionPoints: Environmental Change Notification + * EnvConditions: NA + * CaseDescription: Start two apps and then make environment change notifications + */ +HWTEST_F(AmsAppLifeCycleModuleTest, UpdateConfiguration_001, TestSize.Level1) +{ + EXPECT_NE(serviceInner_, nullptr); + CHECK_POINTER_IS_NULLPTR(serviceInner_); + pid_t pid_0 = 1024; + pid_t pid_1 = 2048; + sptr token_0 = new (std::nothrow) MockAbilityToken(); + sptr token_1 = new (std::nothrow) MockAbilityToken(); + auto abilityInfo_0 = GetAbilityInfo("0", "MainAbility", "p1", "com.ohos.test.helloworld0"); + auto appInfo_0 = GetApplicationInfo("com.ohos.test.helloworld0"); + auto abilityInfo_1 = GetAbilityInfo("0", "MainAbility1", "p2", "com.ohos.test.helloworld0"); + auto appInfo_1 = GetApplicationInfo("com.ohos.test.helloworld0"); + sptr mockAppScheduler = new (std::nothrow) MockAppScheduler(); + sptr mockAppScheduler1 = new (std::nothrow) MockAppScheduler(); + TestProcessInfo testProcessInfo; + + testProcessInfo.pid = pid_0; + testProcessInfo.isStart = false; + auto appRunningRecord_0 = + StartProcessAndLoadAbility(mockAppScheduler, token_0, abilityInfo_0, appInfo_0, testProcessInfo); + + testProcessInfo.pid = pid_1; + testProcessInfo.isStart = false; + auto appRunningRecord_1 = + StartProcessAndLoadAbility(mockAppScheduler1, token_1, abilityInfo_1, appInfo_1, testProcessInfo); + ChangeAbilityStateAfterAppStart(mockAppScheduler, pid_0); + CheckState(appRunningRecord_0, token_0, AbilityState::ABILITY_STATE_READY, ApplicationState::APP_STATE_READY); + ChangeAbilityStateAfterAppStart(mockAppScheduler1, pid_1); + CheckState(appRunningRecord_1, token_1, AbilityState::ABILITY_STATE_READY, ApplicationState::APP_STATE_READY); + + 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 appMap = serviceInner_->appRunningManager_->GetAppRunningRecordMap(); + EXPECT_EQ(2, static_cast(appMap.size())); + + EXPECT_CALL(*mockAppScheduler, ScheduleConfigurationUpdated(_)) + .Times(1) + .WillOnce(testing::Invoke(configUpdate)); + EXPECT_CALL(*mockAppScheduler1, ScheduleConfigurationUpdated(_)) + .Times(1) + .WillOnce(testing::Invoke(configUpdate)); + + serviceInner_->UpdateConfiguration(config); +} + +/* + * Feature: AbilityMgr + * Function: UpdateConfiguration + * SubFunction: NA + * FunctionPoints: Environmental Change Notification + * EnvConditions: NA + * CaseDescription: Verify duplicate notification conditions + */ +HWTEST_F(AmsAppLifeCycleModuleTest, UpdateConfiguration_002, TestSize.Level1) +{ + EXPECT_NE(serviceInner_, nullptr); + CHECK_POINTER_IS_NULLPTR(serviceInner_); + pid_t pid_0 = 1024; + pid_t pid_1 = 2048; + sptr token_0 = new (std::nothrow) MockAbilityToken(); + sptr token_1 = new (std::nothrow) MockAbilityToken(); + auto abilityInfo_0 = GetAbilityInfo("0", "MainAbility", "p1", "com.ohos.test.helloworld0"); + auto appInfo_0 = GetApplicationInfo("com.ohos.test.helloworld0"); + auto abilityInfo_1 = GetAbilityInfo("0", "MainAbility1", "p2", "com.ohos.test.helloworld0"); + auto appInfo_1 = GetApplicationInfo("com.ohos.test.helloworld0"); + sptr mockAppScheduler = new (std::nothrow) MockAppScheduler(); + sptr mockAppScheduler1 = new (std::nothrow) MockAppScheduler(); + TestProcessInfo testProcessInfo; + + testProcessInfo.pid = pid_0; + testProcessInfo.isStart = false; + auto appRunningRecord_0 = + StartProcessAndLoadAbility(mockAppScheduler, token_0, abilityInfo_0, appInfo_0, testProcessInfo); + + testProcessInfo.pid = pid_1; + testProcessInfo.isStart = false; + auto appRunningRecord_1 = + StartProcessAndLoadAbility(mockAppScheduler1, token_1, abilityInfo_1, appInfo_1, testProcessInfo); + ChangeAbilityStateAfterAppStart(mockAppScheduler, pid_0); + CheckState(appRunningRecord_0, token_0, AbilityState::ABILITY_STATE_READY, ApplicationState::APP_STATE_READY); + ChangeAbilityStateAfterAppStart(mockAppScheduler1, pid_1); + CheckState(appRunningRecord_1, token_1, AbilityState::ABILITY_STATE_READY, ApplicationState::APP_STATE_READY); + + auto testLanguge = std::string("ch-zh"); + auto again = std::string("Russian"); + auto displayId = 10; + auto configUpdate = [testLanguge, displayId](const Configuration &config) { + auto l = config.GetItem(displayId, GlobalConfigurationKey::SYSTEM_LANGUAGE); + EXPECT_TRUE(testLanguge == l); + }; + + auto configUpdateAgain = [again, displayId](const Configuration &config) { + auto l = config.GetItem(displayId, GlobalConfigurationKey::SYSTEM_LANGUAGE); + EXPECT_TRUE(again == l); + }; + + Configuration config; + config.AddItem(displayId, GlobalConfigurationKey::SYSTEM_LANGUAGE, testLanguge); + + auto appMap = serviceInner_->appRunningManager_->GetAppRunningRecordMap(); + EXPECT_EQ(2, static_cast(appMap.size())); + + EXPECT_CALL(*mockAppScheduler, ScheduleConfigurationUpdated(_)) + .Times(2) + .WillOnce(testing::Invoke(configUpdate)) + .WillOnce(testing::Invoke(configUpdateAgain)); + + EXPECT_CALL(*mockAppScheduler1, ScheduleConfigurationUpdated(_)) + .Times(2) + .WillOnce(testing::Invoke(configUpdate)) + .WillOnce(testing::Invoke(configUpdateAgain)); + + serviceInner_->UpdateConfiguration(config); + + config.AddItem(displayId, GlobalConfigurationKey::SYSTEM_LANGUAGE, again); + + serviceInner_->UpdateConfiguration(config); +} + +/* + * Feature: AbilityMgr + * Function: UpdateConfiguration + * SubFunction: NA + * FunctionPoints: Environmental Change Notification + * EnvConditions: NA + * CaseDescription: Two types of notifications + */ +HWTEST_F(AmsAppLifeCycleModuleTest, UpdateConfiguration_003, TestSize.Level1) +{ + EXPECT_NE(serviceInner_, nullptr); + CHECK_POINTER_IS_NULLPTR(serviceInner_); + pid_t pid_0 = 1024; + pid_t pid_1 = 2048; + sptr token_0 = new (std::nothrow) MockAbilityToken(); + sptr token_1 = new (std::nothrow) MockAbilityToken(); + auto abilityInfo_0 = GetAbilityInfo("0", "MainAbility", "p1", "com.ohos.test.helloworld0"); + auto appInfo_0 = GetApplicationInfo("com.ohos.test.helloworld0"); + auto abilityInfo_1 = GetAbilityInfo("0", "MainAbility1", "p2", "com.ohos.test.helloworld0"); + auto appInfo_1 = GetApplicationInfo("com.ohos.test.helloworld0"); + sptr mockAppScheduler = new (std::nothrow) MockAppScheduler(); + sptr mockAppScheduler1 = new (std::nothrow) MockAppScheduler(); + TestProcessInfo testProcessInfo; + + testProcessInfo.pid = pid_0; + testProcessInfo.isStart = false; + auto appRunningRecord_0 = + StartProcessAndLoadAbility(mockAppScheduler, token_0, abilityInfo_0, appInfo_0, testProcessInfo); + + testProcessInfo.pid = pid_1; + testProcessInfo.isStart = false; + auto appRunningRecord_1 = + StartProcessAndLoadAbility(mockAppScheduler1, token_1, abilityInfo_1, appInfo_1, testProcessInfo); + ChangeAbilityStateAfterAppStart(mockAppScheduler, pid_0); + CheckState(appRunningRecord_0, token_0, AbilityState::ABILITY_STATE_READY, ApplicationState::APP_STATE_READY); + ChangeAbilityStateAfterAppStart(mockAppScheduler1, pid_1); + CheckState(appRunningRecord_1, token_1, AbilityState::ABILITY_STATE_READY, ApplicationState::APP_STATE_READY); + + auto testLanguge = std::string("ch-zh"); + auto displayId = 10; + auto configUpdate = [testLanguge, displayId](const Configuration &config) { + auto ld = config.GetItem(displayId, GlobalConfigurationKey::SYSTEM_LANGUAGE); + auto l = config.GetItem(displayId, GlobalConfigurationKey::SYSTEM_LANGUAGE); + EXPECT_TRUE(testLanguge == ld); + EXPECT_TRUE(testLanguge == l); + }; + + Configuration config; + config.AddItem(displayId, GlobalConfigurationKey::SYSTEM_LANGUAGE, testLanguge); + config.AddItem(GlobalConfigurationKey::SYSTEM_LANGUAGE, testLanguge); + + auto appMap = serviceInner_->appRunningManager_->GetAppRunningRecordMap(); + EXPECT_EQ(2, static_cast(appMap.size())); + + EXPECT_CALL(*mockAppScheduler, ScheduleConfigurationUpdated(_)) + .Times(1) + .WillOnce(testing::Invoke(configUpdate)); + + EXPECT_CALL(*mockAppScheduler1, ScheduleConfigurationUpdated(_)) + .Times(1) + .WillOnce(testing::Invoke(configUpdate)); + + serviceInner_->UpdateConfiguration(config); +} + +/* + * Feature: AbilityMgr + * Function: LoadResidentProcess + * SubFunction: NA + * FunctionPoints: start resident process + * EnvConditions: NA + * CaseDescription: Start a resident process normally + */ +HWTEST_F(AmsAppLifeCycleModuleTest, LoadResidentProcess_001, TestSize.Level1) +{ + pid_t pid = 123; + sptr token = GetAbilityToken(); + std::string appName = "KeepAliveApp"; + std::string proc = "KeepAliveApplication"; + int uid = 2100; + + std::vector infos; + BundleInfo info; + info.name = proc; + info.uid = uid; + + ApplicationInfo appInfo; + appInfo.name = "KeepAliveApp"; + appInfo.bundleName = "KeepAliveApplication"; + appInfo.uid = 2100; + + info.applicationInfo = appInfo; + HapModuleInfo hapModuleInfo; + hapModuleInfo.name = "Module"; + HapModuleInfo hapModuleInfo1; + hapModuleInfo1.name = "Module1"; + info.hapModuleInfos.push_back(hapModuleInfo); + info.hapModuleInfos.push_back(hapModuleInfo1); + + infos.push_back(info); + + sptr mockAppScheduler = new MockAppScheduler(); + + auto appRecord = serviceInner_->appRunningManager_->CheckAppRunningRecordIsExist(appName, proc, uid, info); + EXPECT_FALSE(appRecord); + + StartAppProcess(pid); + serviceInner_->StartResidentProcess(infos, -1); + appRecord = serviceInner_->appRunningManager_->CheckAppRunningRecordIsExist(appName, proc, uid, info); + EXPECT_TRUE(appRecord); + pid_t newPid = appRecord->GetPriorityObject()->GetPid(); + EXPECT_TRUE(newPid == pid); +} + +/* + * Feature: AbilityMgr + * Function: LoadResidentProcess + * SubFunction: NA + * FunctionPoints: start resident process + * EnvConditions: NA + * CaseDescription: Start multiple resident processes + */ +HWTEST_F(AmsAppLifeCycleModuleTest, LoadResidentProcess_002, TestSize.Level1) +{ + std::vector infos; + pid_t pid = 123; + std::string appName = "KeepAliveApp"; + std::string proc = "KeepAliveApplication"; + int uid = 2100; + + HapModuleInfo hapModuleInfo; + hapModuleInfo.moduleName = "KeepAliveApplication"; + BundleInfo info; + info.name = proc; + info.uid = uid; + info.isKeepAlive = true; + info.appId = "com.ohos.test.helloworld_code123"; + info.hapModuleInfos.push_back(hapModuleInfo); + + ApplicationInfo appInfo; + appInfo.name = appName; + appInfo.bundleName = proc; + appInfo.uid = 2100; + info.applicationInfo = appInfo; + + pid_t pid1 = 1234; + int appUid1 = 2101; + std::string appName1 = "KeepAliveApp1"; + std::string proc1 = "KeepAliveApplication1"; + + HapModuleInfo hapModuleInfo1; + hapModuleInfo1.moduleName = proc1; + + ApplicationInfo appInfo1; + appInfo1.name = appName1; + appInfo1.bundleName = proc1; + appInfo1.uid = appUid1; + + BundleInfo info1; + info1.name = proc1; + info1.uid = appUid1; + info1.isKeepAlive = true; + info1.appId = "com.ohos.test.helloworld_code123"; + info1.hapModuleInfos.push_back(hapModuleInfo1); + info1.applicationInfo = appInfo1; + + infos.push_back(info); + infos.push_back(info1); + + MockAppSpawnClient *mockClientPtr = new (std::nothrow) MockAppSpawnClient(); + EXPECT_TRUE(mockClientPtr); + EXPECT_CALL(*mockClientPtr, StartProcess(_, _)) + .Times(2) + .WillOnce(DoAll(SetArgReferee<1>(pid), Return(ERR_OK))) + .WillOnce(DoAll(SetArgReferee<1>(pid1), Return(ERR_OK))); + EXPECT_CALL(*mockAppStateCallbackStub_, OnAppStateChanged(_)).Times(2); + + serviceInner_->SetAppSpawnClient(std::unique_ptr(mockClientPtr)); + + // start process + serviceInner_->StartResidentProcess(infos, -1); + auto recordMap = serviceInner_->appRunningManager_->GetAppRunningRecordMap(); + EXPECT_TRUE(recordMap.size() == 2); +} + +/* + * Feature: AbilityMgr + * Function: RestartResidentProcess + * SubFunction: NA + * FunctionPoints: Resident process exception recovery + * EnvConditions: NA + * CaseDescription: Start the resident process and let it die abnormally, then resume + */ +HWTEST_F(AmsAppLifeCycleModuleTest, RestartResidentProcess_001, TestSize.Level1) +{ + pid_t pid = 123; + pid_t pid1 = 1254; + 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.appId = "com.ohos.test.helloworld_code123"; + info.hapModuleInfos.push_back(hapModuleInfo); + + ApplicationInfo appInfo; + appInfo.name = "KeepAliveApp"; + appInfo.bundleName = "KeepAliveApplication"; + appInfo.uid = 2100; + info.applicationInfo = appInfo; + infos.push_back(info); + + MockAppSpawnClient *mockClientPtr = new (std::nothrow) MockAppSpawnClient(); + EXPECT_TRUE(mockClientPtr); + EXPECT_CALL(*mockClientPtr, StartProcess(_, _)).Times(2) + .WillOnce(DoAll(SetArgReferee<1>(pid), Return(ERR_OK))) + .WillOnce(DoAll(SetArgReferee<1>(pid1), Return(ERR_OK))); + + EXPECT_CALL(*mockAppStateCallbackStub_, OnAppStateChanged(_)).Times(2); + serviceInner_->SetAppSpawnClient(std::unique_ptr(mockClientPtr)); + sptr mockAppScheduler = new (std::nothrow) MockAppScheduler(); + sptr client = iface_cast(mockAppScheduler.GetRefPtr()); + + // start process + serviceInner_->StartResidentProcess(infos, -1); + auto appRecord = serviceInner_->appRunningManager_->CheckAppRunningRecordIsExist(appName, proc, uid, info); + EXPECT_TRUE(appRecord); + + EXPECT_TRUE(appRecord->GetUid() == uid); + EXPECT_TRUE(appRecord->GetProcessName() == proc); + EXPECT_TRUE(appRecord->IsKeepAliveApp()); + + serviceInner_->AttachApplication(pid, client); + + serviceInner_->OnRemoteDied(mockAppScheduler, false); + appRecord = serviceInner_->appRunningManager_->CheckAppRunningRecordIsExist(appName, proc, uid, info); + EXPECT_FALSE(appRecord); + + sleep(1); + appRecord = serviceInner_->appRunningManager_->CheckAppRunningRecordIsExist(appName, proc, uid, info); + EXPECT_TRUE(appRecord); +} + +/* + * Feature: AbilityMgr + * Function: RestartResidentProcess + * SubFunction: NA + * FunctionPoints: Resident process exception recovery + * EnvConditions: NA + * CaseDescription: Start an ordinary process, it should not be pulled up after abnormal death + */ +HWTEST_F(AmsAppLifeCycleModuleTest, RestartResidentProcess_002, TestSize.Level1) +{ + pid_t pid_0 = 1024; + + sptr token_0 = new (std::nothrow) MockAbilityToken(); + auto abilityInfo_0 = GetAbilityInfo("0", "MainAbility", "p1", "com.ohos.test.helloworld103"); + auto appInfo_0 = GetApplicationInfo("com.ohos.test.helloworld103"); + + sptr mockAppScheduler = new (std::nothrow) MockAppScheduler(); + TestProcessInfo testProcessInfo; + + testProcessInfo.pid = pid_0; + testProcessInfo.isStart = false; + auto appRunningRecord_0 = + StartProcessAndLoadAbility(mockAppScheduler, token_0, abilityInfo_0, appInfo_0, testProcessInfo); + EXPECT_TRUE(appRunningRecord_0); + + sptr client = iface_cast(mockAppScheduler.GetRefPtr()); + serviceInner_->AttachApplication(pid_0, client); + serviceInner_->OnRemoteDied(mockAppScheduler, false); + + sleep(1); + auto recordMap = serviceInner_->appRunningManager_->GetAppRunningRecordMap(); + EXPECT_TRUE(recordMap.empty()); +} + +/* + * Feature: AbilityMgr + * Function: RestartResidentProcess + * SubFunction: NA + * FunctionPoints: Resident process exception recovery + * EnvConditions: NA + * CaseDescription: 1.start a normal process + * 2.start a resident process + * 3.make both processes die + */ +HWTEST_F(AmsAppLifeCycleModuleTest, RestartResidentProcess_003, TestSize.Level1) +{ + pid_t pid_0 = 1024; + + sptr token_0 = new (std::nothrow) MockAbilityToken(); + auto abilityInfo_0 = GetAbilityInfo("0", "MainAbility", "p1", "com.ohos.test.helloworld103"); + auto appInfo_0 = GetApplicationInfo("com.ohos.test.helloworld103"); + + sptr mockAppScheduler = new (std::nothrow) MockAppScheduler(); + TestProcessInfo testProcessInfo; + + testProcessInfo.pid = pid_0; + testProcessInfo.isStart = false; + auto appRunningRecord_0 = + StartProcessAndLoadAbility(mockAppScheduler, token_0, abilityInfo_0, appInfo_0, testProcessInfo); + EXPECT_TRUE(appRunningRecord_0); + + pid_t pid = 123; + 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.isKeepAlive = true; + info.appId = "com.ohos.test.helloworld_code123"; + info.hapModuleInfos.push_back(hapModuleInfo); + + ApplicationInfo appInfo; + appInfo.name = "KeepAliveApp"; + appInfo.bundleName = "KeepAliveApplication"; + appInfo.uid = 2100; + info.applicationInfo = appInfo; + infos.push_back(info); + + MockAppSpawnClient *mockClientPtr = new (std::nothrow) MockAppSpawnClient(); + EXPECT_TRUE(mockClientPtr); + EXPECT_CALL(*mockClientPtr, StartProcess(_, _)) + .Times(2) + .WillOnce(DoAll(SetArgReferee<1>(pid), Return(ERR_OK))) + .WillOnce(DoAll(SetArgReferee<1>(pid+1), Return(ERR_OK))); + EXPECT_CALL(*mockAppStateCallbackStub_, OnAppStateChanged(_)).Times(2); + + serviceInner_->SetAppSpawnClient(std::unique_ptr(mockClientPtr)); + + // start process + serviceInner_->StartResidentProcess(infos, -1); + auto residentRecord = serviceInner_->appRunningManager_->CheckAppRunningRecordIsExist(appName, proc, uid, info); + EXPECT_TRUE(residentRecord); + + sptr client = iface_cast(mockAppScheduler.GetRefPtr()); + serviceInner_->AttachApplication(pid_0, client); + + sptr mockAppSchedulerResident = new (std::nothrow) MockAppScheduler(); + EXPECT_CALL(*mockAppSchedulerResident, ScheduleLaunchApplication(_, _)).Times(1); + sptr residentClient = iface_cast(mockAppSchedulerResident.GetRefPtr()); + serviceInner_->AttachApplication(pid, residentClient); + + serviceInner_->OnRemoteDied(mockAppScheduler, false); + serviceInner_->OnRemoteDied(mockAppSchedulerResident, false); + + auto recordMap = serviceInner_->appRunningManager_->GetAppRunningRecordMap(); + EXPECT_TRUE(recordMap.empty()); + + sleep(1); + auto appRecord = serviceInner_->appRunningManager_->CheckAppRunningRecordIsExist(appName, proc, uid, info); + EXPECT_TRUE(appRecord); +} + +/* + * Feature: AbilityMgr + * Function: KillProcessWithAccount + * SubFunction: NA + * FunctionPoints: Test KillProcessWithAccount + * EnvConditions: NA + * CaseDescription: Specify user to kill process + */ +HWTEST_F(AmsAppLifeCycleModuleTest, KillProcessWithAccount_001, TestSize.Level1) +{ + const std::string STRING_BUNDLE_NAME = "com.ix.hiworld"; + constexpr int ACCOUNT_ID = 100; + 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); + + ErrCode result = proxy->GetAmsMgr()->KillProcessWithAccount(STRING_BUNDLE_NAME, ACCOUNT_ID); + EXPECT_EQ(result, ERR_OK); +} } // namespace AppExecFwk } // namespace OHOS \ No newline at end of file diff --git a/services/test/moduletest/common/ams/app_running_processes_info_module_test/BUILD.gn b/services/test/moduletest/common/ams/app_running_processes_info_module_test/BUILD.gn new file mode 100644 index 0000000000000000000000000000000000000000..70a4c2d34421bc8eee3d0d3224123f5468c8c605 --- /dev/null +++ b/services/test/moduletest/common/ams/app_running_processes_info_module_test/BUILD.gn @@ -0,0 +1,37 @@ +# 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/mstappmgrservice" + +ohos_moduletest("AppRunningProcessesInfoModuleTest") { + module_out_path = module_output_path + + include_dirs = [ + "//base/hiviewdfx/hilog/interfaces/native/innerkits/include/hilog/", + "//base/hiviewdfx/hilog/interfaces/native/innerkits/include", + ] + sources = [ "app_running_processes_info_module_test.cpp" ] + + deps = [ "${services_path}/test/moduletest/common/ams:appmgr_mst_source" ] + + external_deps = [ "ipc:ipc_core" ] +} + +group("moduletest") { + testonly = true + + deps = [ ":AppRunningProcessesInfoModuleTest" ] +} diff --git a/services/test/moduletest/common/ams/app_running_processes_info_module_test/app_running_processes_info_module_test.cpp b/services/test/moduletest/common/ams/app_running_processes_info_module_test/app_running_processes_info_module_test.cpp new file mode 100644 index 0000000000000000000000000000000000000000..5965bdb0e12f8889401b1824f42a7d8aaecc07cb --- /dev/null +++ b/services/test/moduletest/common/ams/app_running_processes_info_module_test/app_running_processes_info_module_test.cpp @@ -0,0 +1,426 @@ +/* + * 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_running_record.h" +#undef private +#include +#include +#include "iremote_object.h" +#include "app_record_id.h" +#include "app_scheduler_proxy.h" +#include "app_scheduler_host.h" +#define private public +#include "app_mgr_service_inner.h" +#undef private +#include "mock_application.h" +#include "ability_info.h" +#include "application_info.h" +#include "mock_bundle_manager.h" +#include "mock_ability_token.h" +#include "mock_app_scheduler.h" +#include "mock_app_spawn_client.h" + +using namespace testing::ext; +using OHOS::iface_cast; +using OHOS::IRemoteObject; +using OHOS::sptr; +using testing::_; +using testing::Invoke; +using testing::InvokeWithoutArgs; + +namespace OHOS { +namespace AppExecFwk { +class AppRunningProcessesInfoModuleTest : public testing::Test { +public: + static void SetUpTestCase(); + static void TearDownTestCase(); + void SetUp(); + void TearDown(); + +protected: + std::string GetTestAppName(const unsigned long num) const + { + if (num < appName_.size()) { + return appName_[num]; + } + return ""; + } + + std::string GetTestAbilityName(const unsigned long num) const + { + if (num < abilityName_.size()) { + return abilityName_[num]; + } + return ""; + } + + void CheckLaunchApplication(const sptr &mockApplication, const unsigned long index, + std::shared_ptr record, const std::string &testPoint) const + { + EXPECT_TRUE(record != nullptr) << "record is nullptr!"; + sptr client = iface_cast(mockApplication); + record->SetApplicationClient(client); + + std::string applicationName(GetTestAppName(index)); + ApplicationInfo info; + info.name = applicationName; + std::string processInfoName(GetTestAppName(index)); + pid_t pidId = 123; + ProcessInfo processInfo(processInfoName, pidId); + + AppLaunchData launchData; + launchData.SetApplicationInfo(info); + launchData.SetProcessInfo(processInfo); + + EXPECT_CALL(*mockApplication, ScheduleLaunchApplication(_, _)) + .Times(1) + .WillOnce(Invoke(mockApplication.GetRefPtr(), &MockApplication::LaunchApplication)); + Configuration config; + record->LaunchApplication(config); + mockApplication->Wait(); + + bool isEqual = mockApplication->CompareAppLaunchData(launchData); + EXPECT_EQ(isEqual, true) << testPoint << ",fail"; + } + + void CheckAppRunningRecording(const std::shared_ptr appInfo, + const std::shared_ptr abilityInfo, const std::shared_ptr record, const int index, + RecordQueryResult &result) const + { + EXPECT_TRUE(service_ != nullptr) << "init service fail!"; + EXPECT_TRUE(appInfo != nullptr) << "appInfo is nullptr!"; + EXPECT_TRUE(abilityInfo != nullptr) << "abilityInfo is nullptr!"; + EXPECT_TRUE(record != nullptr) << "record is nullptr!"; + auto abilityRecord = record->GetAbilityRunningRecord(GetTestAbilityName(index)); + int32_t id = record->GetRecordId(); + auto name = record->GetName(); + sptr token = abilityRecord->GetToken(); + auto abilityName = abilityRecord->GetName(); + std::string processName = GetTestAppName(index); + BundleInfo bundleInfo; + HapModuleInfo hapModuleInfo; + EXPECT_TRUE(service_->GetBundleAndHapInfo(*abilityInfo, appInfo, bundleInfo, hapModuleInfo)); + + auto appRecordFromServ = service_->appRunningManager_->CheckAppRunningRecordIsExist( + appInfo->name, processName, appInfo->uid, bundleInfo); + EXPECT_TRUE(appRecordFromServ); + auto abilityRecordFromServ = appRecordFromServ->GetAbilityRunningRecord(GetTestAbilityName(index)); + int32_t idFromServ = appRecordFromServ->GetRecordId(); + sptr tokenFromServ = abilityRecordFromServ->GetToken(); + auto nameFromServ = appRecordFromServ->GetName(); + auto abilityNameFromServ = abilityRecordFromServ->GetName(); + EXPECT_TRUE(id == idFromServ) << "fail, RecordId is not equal!"; + EXPECT_TRUE(tokenFromServ.GetRefPtr() == token.GetRefPtr()) << "fail, token is not equal!"; + EXPECT_EQ(name, nameFromServ) << "fail, app record name is not equal!"; + EXPECT_EQ(abilityName, abilityNameFromServ) << "fail, app record name is not equal!"; + } + + std::unique_ptr service_ {nullptr}; + sptr mockBundleMgr_ {nullptr}; + + sptr GetMockToken() const + { + return mockToken_; + } + +private: + std::vector appName_ = { + "test_app_name1", + "test_app_name2", + "test_app_name3", + "test_app_name4", + "test_app_name5", + }; + std::vector abilityName_ = { + "test_ability_name1", + "test_ability_name2", + "test_ability_name3", + "test_ability_name4", + "test_ability_name5", + }; + sptr mockToken_ {nullptr}; +}; + +void AppRunningProcessesInfoModuleTest::SetUpTestCase() +{} + +void AppRunningProcessesInfoModuleTest::TearDownTestCase() +{} + +void AppRunningProcessesInfoModuleTest::SetUp() +{ + service_.reset(new (std::nothrow) AppMgrServiceInner()); + mockToken_ = new (std::nothrow) MockAbilityToken(); + mockBundleMgr_ = new (std::nothrow) BundleMgrService(); + service_->SetBundleManager(mockBundleMgr_); +} + +void AppRunningProcessesInfoModuleTest::TearDown() +{} + +/* + * Feature: AppMgrServiceInner + * Function: GetRunningProcessInfoByToken + * SubFunction: NA + * FunctionPoints: get running process info by token. + * EnvConditions: NA + * CaseDescription: creat apprunningrecord, set record state, mock object, call query function. + */ +HWTEST_F(AppRunningProcessesInfoModuleTest, ApplicationStart_001, TestSize.Level1) +{ + // init AppRunningRecord + unsigned long index = 0L; + int uid = 100; + auto abilityInfo = std::make_shared(); + abilityInfo->name = GetTestAbilityName(index); + abilityInfo->applicationInfo.uid = uid; + auto appInfo = std::make_shared(); + appInfo->name = GetTestAppName(index); + appInfo->uid = uid; + std::string processName = GetTestAppName(index); + RecordQueryResult result; + BundleInfo bundleInfo; + HapModuleInfo hapModuleInfo; + EXPECT_TRUE(service_->GetBundleAndHapInfo(*abilityInfo, appInfo, bundleInfo, hapModuleInfo)); + auto record = service_->CreateAppRunningRecord( + GetMockToken(), nullptr, appInfo, abilityInfo, processName, bundleInfo, hapModuleInfo, nullptr); + record->SetUid(uid); + EXPECT_TRUE(record != nullptr) << ",create apprunningrecord fail!"; + + // check apprunningrecord + CheckAppRunningRecording(appInfo, abilityInfo, record, index, result); + + // LaunchApplication + sptr mockApplication(new MockApplication()); + std::string testPoint = "ApplicationStart_001"; + CheckLaunchApplication(mockApplication, index, record, testPoint); + + 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, processName, appInfo->uid, bundleInfo); + EXPECT_TRUE(newRecord); + newRecord->SetUid(uid); + auto stateFromRec = newRecord->GetState(); + EXPECT_EQ(stateFromRec, ApplicationState::APP_STATE_FOREGROUND); + + RunningProcessInfo info; + service_->GetRunningProcessInfoByToken(GetMockToken(), info); + EXPECT_TRUE(info.processName_ == processName); + EXPECT_TRUE(info.uid_ == uid); +} + +/* + * Feature: AppMgrServiceInner + * Function: GetAllRunningProcesses + * SubFunction: NA + * FunctionPoints: get running process info by token. + * EnvConditions: NA + * CaseDescription: creat apprunningrecord, set record state, mock object, call query function. + */ +HWTEST_F(AppRunningProcessesInfoModuleTest, ApplicationStart_002, TestSize.Level1) +{ + // init AppRunningRecord + unsigned long index = 0L; + int uid = 0; + auto abilityInfo = std::make_shared(); + abilityInfo->name = GetTestAbilityName(index); + abilityInfo->applicationInfo.uid = uid; + auto appInfo = std::make_shared(); + appInfo->name = GetTestAppName(index); + appInfo->uid = uid; + std::string processName = GetTestAppName(index); + RecordQueryResult result; + BundleInfo bundleInfo; + HapModuleInfo hapModuleInfo; + EXPECT_TRUE(service_->GetBundleAndHapInfo(*abilityInfo, appInfo, bundleInfo, hapModuleInfo)); + auto record = service_->CreateAppRunningRecord( + GetMockToken(), nullptr, appInfo, abilityInfo, processName, bundleInfo, hapModuleInfo, nullptr); + record->SetUid(uid); + EXPECT_TRUE(record != nullptr) << ",create apprunningrecord fail!"; + + // check apprunningrecord + CheckAppRunningRecording(appInfo, abilityInfo, record, index, result); + + // LaunchApplication + sptr mockApplication(new MockApplication()); + std::string testPoint = "ApplicationStart_001"; + CheckLaunchApplication(mockApplication, index, record, testPoint); + + 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, processName, 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, mock object, call query function. + */ +HWTEST_F(AppRunningProcessesInfoModuleTest, ApplicationStart_003, TestSize.Level1) +{ + // init AppRunningRecord + unsigned long index = 0L; + int uid = 0; + auto abilityInfo = std::make_shared(); + abilityInfo->name = GetTestAbilityName(index); + abilityInfo->applicationInfo.uid = uid; + auto appInfo = std::make_shared(); + appInfo->name = GetTestAppName(index); + appInfo->uid = uid; + std::string processName = GetTestAppName(index); + RecordQueryResult result; + BundleInfo bundleInfo; + HapModuleInfo hapModuleInfo; + EXPECT_TRUE(service_->GetBundleAndHapInfo(*abilityInfo, appInfo, bundleInfo, hapModuleInfo)); + auto record = service_->CreateAppRunningRecord( + GetMockToken(), nullptr, appInfo, abilityInfo, processName, bundleInfo, hapModuleInfo, nullptr); + record->SetUid(uid); + EXPECT_TRUE(record != nullptr) << ",create apprunningrecord fail!"; + + // check apprunningrecord + CheckAppRunningRecording(appInfo, abilityInfo, record, index, result); + + // LaunchApplication + sptr mockApplication(new MockApplication()); + std::string testPoint = "ApplicationStart_001"; + CheckLaunchApplication(mockApplication, index, record, testPoint); + + 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, processName, appInfo->uid, bundleInfo); + EXPECT_TRUE(newRecord); + newRecord->SetUid(uid); + auto stateFromRec = newRecord->GetState(); + EXPECT_EQ(stateFromRec, ApplicationState::APP_STATE_FOREGROUND); + + index = 1L; + auto abilityInfo2 = std::make_shared(); + abilityInfo2->name = GetTestAbilityName(index); + abilityInfo2->applicationInfo.uid = uid; + auto appInfo2 = std::make_shared(); + appInfo2->name = GetTestAppName(index); + appInfo2->uid = uid; + std::string processName2 = GetTestAppName(index); + BundleInfo bundleInfo2; + HapModuleInfo hapModuleInfo2; + EXPECT_TRUE(service_->GetBundleAndHapInfo(*abilityInfo2, appInfo2, bundleInfo2, hapModuleInfo2)); + sptr mockToken = new (std::nothrow) MockAbilityToken(); + auto record2 = service_->CreateAppRunningRecord( + mockToken, nullptr, appInfo2, abilityInfo2, processName2, bundleInfo2, hapModuleInfo2, nullptr); + record2->SetUid(uid); + EXPECT_TRUE(record != nullptr) << ",create apprunningrecord fail!"; + + 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, mock object, call query function. + */ +HWTEST_F(AppRunningProcessesInfoModuleTest, ApplicationStart_004, TestSize.Level1) +{ + // init AppRunningRecord + unsigned long index = 0L; + int uid = 0; + auto abilityInfo = std::make_shared(); + abilityInfo->name = GetTestAbilityName(index); + abilityInfo->applicationInfo.uid = uid; + auto appInfo = std::make_shared(); + appInfo->name = GetTestAppName(index); + appInfo->uid = uid; + std::string processName = GetTestAppName(index); + RecordQueryResult result; + BundleInfo bundleInfo; + HapModuleInfo hapModuleInfo; + EXPECT_TRUE(service_->GetBundleAndHapInfo(*abilityInfo, appInfo, bundleInfo, hapModuleInfo)); + auto record = service_->CreateAppRunningRecord( + GetMockToken(), nullptr, appInfo, abilityInfo, processName, bundleInfo, hapModuleInfo, nullptr); + record->SetUid(uid); + EXPECT_TRUE(record != nullptr) << ",create apprunningrecord fail!"; + + CheckAppRunningRecording(appInfo, abilityInfo, record, index, result); + + sptr mockApplication(new MockApplication()); + std::string testPoint = "ApplicationStart_001"; + CheckLaunchApplication(mockApplication, index, record, testPoint); + + 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_BACKGROUND); + auto newRecord = service_->appRunningManager_->CheckAppRunningRecordIsExist( + appInfo->name, processName, appInfo->uid, bundleInfo); + EXPECT_TRUE(newRecord); + newRecord->SetUid(uid); + auto stateFromRec = newRecord->GetState(); + EXPECT_EQ(stateFromRec, ApplicationState::APP_STATE_BACKGROUND); + + RunningProcessInfo info; + service_->appRunningManager_->GetRunningProcessInfoByToken(GetMockToken(), info); + EXPECT_TRUE(info.processName_ == processName); + EXPECT_TRUE(info.uid_ == uid); +} +} // namespace AppExecFwk +} // namespace OHOS \ No newline at end of file diff --git a/services/test/moduletest/common/ams/app_running_record_test/ams_app_running_record_module_test.cpp b/services/test/moduletest/common/ams/app_running_record_test/ams_app_running_record_module_test.cpp index c4c5d25242a281e7f7be044b782d66693a7b14d6..887ebe7cab49a82914f2b8b7d2b0b9b9acfd0382 100644 --- a/services/test/moduletest/common/ams/app_running_record_test/ams_app_running_record_module_test.cpp +++ b/services/test/moduletest/common/ams/app_running_record_test/ams_app_running_record_module_test.cpp @@ -513,5 +513,147 @@ HWTEST_F(AmsAppRunningRecordModuleTest, ApplicationStatusChange_006, TestSize.Le auto stateFromRec = record->GetState(); EXPECT_EQ(stateFromRec, ApplicationState::APP_STATE_TERMINATED); } + +/* + * Feature: StartSpecifiedAbility + * Function: AppManagerService + * SubFunction: ApprunningRecord + * FunctionPoints: not set Specified flag,start ability + * EnvConditions: system running normally + * CaseDescription:When the process exists and the moduleRecord exists, + * calling StartSpecifiedAbility will call the ScheduleAcceptWant method once + */ +HWTEST_F(AmsAppRunningRecordModuleTest, StartSpecifiedAbility_001, TestSize.Level2) +{ + EXPECT_TRUE(service_ != nullptr) << "init service fail!"; + + unsigned long index = 0; + auto appInfo = std::make_shared(); + appInfo->name = GetTestAppName(index); + appInfo->bundleName = GetTestAppName(index); + appInfo->uid = 10003; + auto abilityInfo = std::make_shared(); + abilityInfo->name = GetTestAbilityName(index); + abilityInfo->applicationInfo = *appInfo; + std::string processName = GetTestAppName(index); + BundleInfo bundleInfo; + HapModuleInfo hapModuleInfo; + EXPECT_TRUE(service_->GetBundleAndHapInfo(*abilityInfo, appInfo, bundleInfo, hapModuleInfo)); + auto record = service_->CreateAppRunningRecord( + GetMockToken(), nullptr, appInfo, abilityInfo, processName, bundleInfo, hapModuleInfo, nullptr); + EXPECT_TRUE(record != nullptr) << "create apprunningrecord fail!"; + + // LaunchApplication + sptr mockApplication(new MockApplication()); + std::string testPoint = "StartSpecifiedAbility_001"; + CheckLaunchApplication(mockApplication, index, record, testPoint); + + EXPECT_CALL(*mockApplication, ScheduleAcceptWant(_, _)) + .Times(1) + .WillOnce(InvokeWithoutArgs(mockApplication.GetRefPtr(), &MockApplication::Post)); + + Want want; + want.SetElementName("DemoDeviceId", "DemoBundleName", "DemoAbilityName"); + service_->StartSpecifiedAbility(want, *abilityInfo); + + mockApplication->Wait(); + EXPECT_TRUE(record->IsStartSpecifiedAbility()); +} + +/* + * Feature: StartSpecifiedAbility + * Function: AppManagerService + * SubFunction: ApprunningRecord + * FunctionPoints: not set Specified flag,start ability + * EnvConditions: system running normally + * CaseDescription:When the process exists and the moduleRecord does not exist, + * calling StartSpecifiedAbility will call the ScheduleAbilityStage method once + */ +HWTEST_F(AmsAppRunningRecordModuleTest, StartSpecifiedAbility_002, TestSize.Level2) +{ + EXPECT_TRUE(service_ != nullptr) << "init service fail!"; + + unsigned long index = 0; + auto appInfo = std::make_shared(); + appInfo->name = GetTestAppName(index); + appInfo->bundleName = GetTestAppName(index); + appInfo->uid = 10003; + auto abilityInfo = std::make_shared(); + abilityInfo->name = GetTestAbilityName(index); + abilityInfo->applicationInfo = *appInfo; + std::string processName = GetTestAppName(index); + BundleInfo bundleInfo; + HapModuleInfo hapModuleInfo; + hapModuleInfo.moduleName = "moduleName1"; + EXPECT_TRUE(service_->GetBundleAndHapInfo(*abilityInfo, appInfo, bundleInfo, hapModuleInfo)); + auto record = service_->CreateAppRunningRecord( + GetMockToken(), nullptr, appInfo, abilityInfo, processName, bundleInfo, hapModuleInfo, nullptr); + EXPECT_TRUE(record != nullptr) << "create apprunningrecord fail!"; + + auto abilityInfo1 = std::make_shared(); + abilityInfo1->name = GetTestAbilityName(index + 1); + abilityInfo1->applicationInfo = *appInfo; + + // LaunchApplication + sptr mockApplication(new MockApplication()); + std::string testPoint = "StartSpecifiedAbility_001"; + CheckLaunchApplication(mockApplication, index, record, testPoint); + + EXPECT_CALL(*mockApplication, ScheduleAbilityStage(_)) + .Times(1) + .WillOnce(InvokeWithoutArgs(mockApplication.GetRefPtr(), &MockApplication::Post)); + + Want want; + want.SetElementName("DemoDeviceId", "DemoBundleName", "DemoAbilityName"); + service_->StartSpecifiedAbility(want, *abilityInfo1); + + mockApplication->Wait(); + EXPECT_TRUE(record->IsStartSpecifiedAbility()); +} + +/* + * Feature: StartSpecifiedAbility + * Function: AppManagerService + * SubFunction: ApprunningRecord + * FunctionPoints: not set Specified flag,start ability + * EnvConditions: system running normally + * CaseDescription:When the process does not exist, call StartSpecifiedAbility, ScheduleAbilityStage and + * ScheduleAcceptWant methods will not be called, and there is a module information + */ +HWTEST_F(AmsAppRunningRecordModuleTest, StartSpecifiedAbility_003, TestSize.Level2) +{ + EXPECT_TRUE(service_ != nullptr) << "init service fail!"; + + unsigned long index = 0; + auto appInfo = std::make_shared(); + appInfo->name = GetTestAppName(index); + appInfo->bundleName = GetTestAppName(index); + appInfo->uid = 10003; + appInfo->process = GetTestAppName(index); + auto abilityInfo = std::make_shared(); + abilityInfo->name = GetTestAbilityName(index); + abilityInfo->applicationInfo = *appInfo; + + // LaunchApplication + sptr mockApplication(new MockApplication()); + + EXPECT_CALL(*mockApplication, ScheduleAcceptWant(_, _)) + .Times(0) + .WillOnce(InvokeWithoutArgs(mockApplication.GetRefPtr(), &MockApplication::Post)); + + EXPECT_CALL(*mockApplication, ScheduleAbilityStage(_)) + .Times(0) + .WillOnce(InvokeWithoutArgs(mockApplication.GetRefPtr(), &MockApplication::Post)); + + Want want; + want.SetElementName("DemoDeviceId", "DemoBundleName", "DemoAbilityName"); + service_->StartSpecifiedAbility(want, *abilityInfo); + mockApplication->Wait(); + + BundleInfo bundleInfo; + auto appRecord = service_->appRunningManager_->CheckAppRunningRecordIsExist( + appInfo->name, GetTestAppName(index), appInfo->uid, bundleInfo); + EXPECT_TRUE(appRecord->GetModules().size() == 1); +} } // namespace AppExecFwk } // namespace OHOS \ No newline at end of file diff --git a/services/test/moduletest/common/ams/specified_ability_service_test/BUILD.gn b/services/test/moduletest/common/ams/specified_ability_service_test/BUILD.gn new file mode 100644 index 0000000000000000000000000000000000000000..9d27752489fffdef10601ee25aa634fd427d627e --- /dev/null +++ b/services/test/moduletest/common/ams/specified_ability_service_test/BUILD.gn @@ -0,0 +1,95 @@ +# 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/mstabilitymgrservice" + +ohos_moduletest("specified_ability_service_test") { + module_out_path = module_output_path + + include_dirs = [ + "${services_path}/test/mock/include", + "//third_party/jsoncpp/include", + "//foundation/aafwk/standard/frameworks/kits/ability/native/include/distributed_ability_runtime", + "//foundation/aafwk/standard/interfaces/innerkits/dataobs_manager/include", + "//base/hiviewdfx/hicollie/interfaces/native/innerkits/include/xcollie", + ] + + sources = [ "specified_ability_service_test.cpp" ] + sources += [ + "//foundation/aafwk/standard/services/abilitymgr/src/ability_manager_proxy.cpp", + "//foundation/aafwk/standard/services/abilitymgr/src/ability_manager_service.cpp", + "//foundation/aafwk/standard/services/abilitymgr/test/mock/libs/sa_mgr/src/sa_mgr_client_mock.cpp", + "//foundation/aafwk/standard/services/common/src/permission_verification.cpp", + "//foundation/aafwk/standard/services/test/mock/src/mock_app_mgr_client.cpp", + "//foundation/aafwk/standard/services/test/mock/src/mock_bundle_mgr.cpp", + "//foundation/distributedhardware/devicemanager/test/unittest/mock/parameter.cpp", + ] + + configs = [ + "${services_path}/test:aafwk_module_test_config", + "//foundation/aafwk/standard/interfaces/innerkits/ability_manager:ability_manager_public_config", + "//foundation/aafwk/standard/services/abilitymgr/test/mock/libs/sa_mgr:sa_mgr_mock_config", + "//foundation/appexecfwk/standard/interfaces/innerkits/appexecfwk_base:appexecfwk_base_sdk_config", + "${aafwk_path}/interfaces/innerkits/app_manager:appmgr_sdk_config", + ] + cflags = [] + if (target_cpu == "arm") { + cflags += [ "-DBINDER_IPC_32BIT" ] + } + deps = [ + "${aafwk_path}/interfaces/innerkits/app_manager:app_manager", + "${innerkits_path}/uri_permission:uri_permission_mgr", + "${innerkits_path}/want:want", + "//base/account/os_account/frameworks/osaccount/native:os_account_innerkits", + "//base/global/i18n_standard/frameworks/intl:intl_util", + "//foundation/aafwk/standard/frameworks/kits/ability/native:abilitykit_native", + "//foundation/aafwk/standard/frameworks/kits/ability/native:dummy_classes", + "//foundation/aafwk/standard/services/abilitymgr:abilityms", + "//foundation/ace/ace_engine/interfaces/innerkits/ui_service_manager:ui_service_mgr", + "//foundation/appexecfwk/standard/interfaces/innerkits/appexecfwk_base:appexecfwk_base", + "//foundation/appexecfwk/standard/interfaces/innerkits/appexecfwk_core:appexecfwk_core", + "//foundation/appexecfwk/standard/interfaces/innerkits/libeventhandler:libeventhandler", + "//foundation/distributedschedule/dmsfwk/interfaces/innerkits/uri:zuri", + "//foundation/distributedschedule/safwk/interfaces/innerkits/safwk:system_ability_fwk", + "//foundation/distributedschedule/samgr/interfaces/innerkits/samgr_proxy:samgr_proxy", + "//foundation/multimedia/image_standard/interfaces/innerkits:image_native", + "//third_party/googletest:gmock_main", + "//third_party/googletest:gtest_main", + "//third_party/jsoncpp:jsoncpp", + "//third_party/libpng:libpng", + "//utils/native/base:utils", + ] + + external_deps = [ + "access_token:libaccesstoken_sdk", + "bytrace_standard:bytrace_core", + "ces_standard:cesfwk_core", + "ces_standard:cesfwk_innerkits", + "dsoftbus_standard:softbus_client", + "hicollie_native:libhicollie", + "hiviewdfx_hilog_native:libhilog", + "ipc:ipc_core", + "native_appdatamgr:native_appdatafwk", + "native_appdatamgr:native_dataability", + "native_appdatamgr:native_rdb", + ] +} + +group("moduletest") { + testonly = true + + deps = [ ":specified_ability_service_test" ] +} diff --git a/services/test/moduletest/common/ams/specified_ability_service_test/specified_ability_service_test.cpp b/services/test/moduletest/common/ams/specified_ability_service_test/specified_ability_service_test.cpp new file mode 100644 index 0000000000000000000000000000000000000000..49be07b7022484dd8e78d475ccc9873ff5ede4d2 --- /dev/null +++ b/services/test/moduletest/common/ams/specified_ability_service_test/specified_ability_service_test.cpp @@ -0,0 +1,251 @@ +/* + * 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 +#include +#include +#include + +#include +#include + +#define private public +#define protected public +#include "sa_mgr_client.h" +#include "mock_ability_connect_callback_stub.h" +#include "mock_ability_scheduler.h" +#include "mock_app_mgr_client.h" +#include "mock_bundle_mgr.h" +#include "ability_record_info.h" +#include "ability_manager_errors.h" +#include "sa_mgr_client.h" +#include "system_ability_definition.h" +#include "ability_manager_service.h" +#include "ability_connect_callback_proxy.h" +#include "ability_config.h" +#include "pending_want_manager.h" +#include "pending_want_record.h" +#undef private +#undef protected +#include "wants_info.h" +#include "want_receiver_stub.h" +#include "want_sender_stub.h" +#include "os_account_manager.h" + +using namespace std::placeholders; +using namespace testing::ext; +using namespace OHOS::AppExecFwk; +using OHOS::iface_cast; +using OHOS::IRemoteObject; +using OHOS::sptr; +using testing::_; +using testing::Invoke; +using testing::Return; + +namespace OHOS { +namespace AAFwk { +class SpecifiedAbilityServiceTest : public testing::Test { +public: + static void SetUpTestCase(void); + static void TearDownTestCase(void); + void SetUp(); + void TearDown(); + Want CreateWant(const std::string &entity); + AbilityInfo CreateAbilityInfo(const std::string &name, const std::string &appName, const std::string &bundleName); + ApplicationInfo CreateAppInfo(const std::string &appName, const std::string &name); + bool MockAppClent(); + void WaitAMS(); + + inline static std::shared_ptr mockAppMgrClient_ {nullptr}; + inline static std::shared_ptr abilityMgrServ_ {nullptr}; + sptr scheduler_ {nullptr}; + inline static bool doOnce_ = false; // In order for mock to execute once +}; + +Want SpecifiedAbilityServiceTest::CreateWant(const std::string &entity) +{ + Want want; + if (!entity.empty()) { + want.AddEntity(entity); + } + return want; +} + +AbilityInfo SpecifiedAbilityServiceTest::CreateAbilityInfo( + const std::string &name, const std::string &appName, const std::string &bundleName) +{ + AbilityInfo abilityInfo; + abilityInfo.visible = true; + abilityInfo.name = name; + abilityInfo.applicationName = appName; + abilityInfo.bundleName = bundleName; + abilityInfo.applicationInfo.bundleName = bundleName; + abilityInfo.applicationName = "hiMusic"; + abilityInfo.applicationInfo.name = "hiMusic"; + abilityInfo.type = AbilityType::PAGE; + abilityInfo.applicationInfo.isLauncherApp = false; + + return abilityInfo; +} + +ApplicationInfo SpecifiedAbilityServiceTest::CreateAppInfo(const std::string &appName, const std::string &bundleName) +{ + ApplicationInfo appInfo; + appInfo.name = appName; + appInfo.bundleName = bundleName; + + return appInfo; +} + +bool SpecifiedAbilityServiceTest::MockAppClent() +{ + if (!mockAppMgrClient_) { + GTEST_LOG_(INFO) << "MockAppClent::1"; + return false; + } + + if (!abilityMgrServ_->appScheduler_) { + GTEST_LOG_(INFO) << "MockAppClent::2"; + return false; + } + + abilityMgrServ_->appScheduler_->appMgrClient_.reset(mockAppMgrClient_.get()); + return true; +} + +void SpecifiedAbilityServiceTest::WaitAMS() +{ + const uint32_t maxRetryCount = 1000; + const uint32_t sleepTime = 1000; + uint32_t count = 0; + if (!abilityMgrServ_) { + return; + } + auto handler = abilityMgrServ_->GetEventHandler(); + if (!handler) { + return; + } + std::atomic taskCalled(false); + auto f = [&taskCalled]() { taskCalled.store(true); }; + if (handler->PostTask(f)) { + while (!taskCalled.load()) { + ++count; + if (count >= maxRetryCount) { + break; + } + usleep(sleepTime); + } + } +} + +void SpecifiedAbilityServiceTest::SetUpTestCase(void) +{ + OHOS::DelayedSingleton::GetInstance()->RegisterSystemAbility( + OHOS::BUNDLE_MGR_SERVICE_SYS_ABILITY_ID, new (std::nothrow) BundleMgrService()); + abilityMgrServ_ = OHOS::DelayedSingleton::GetInstance(); + mockAppMgrClient_ = std::make_shared(); +} + +void SpecifiedAbilityServiceTest::TearDownTestCase(void) +{ + abilityMgrServ_->OnStop(); + mockAppMgrClient_.reset(); +} + +void SpecifiedAbilityServiceTest::SetUp(void) +{ + scheduler_ = new MockAbilityScheduler(); + if (!doOnce_) { + doOnce_ = true; + MockAppClent(); + } + WaitAMS(); +} + +void SpecifiedAbilityServiceTest::TearDown(void) +{} + +/** + * @tc.name: OnAcceptWantResponse_001 + * @tc.desc: test OnAcceptWantResponse + * @tc.type: FUNC + * @tc.require: AR000GJUND + */ +HWTEST_F(SpecifiedAbilityServiceTest, OnAcceptWantResponse_001, TestSize.Level1) +{ + std::string abilityName = "MusicAbility"; + std::string appName = "test_app"; + std::string bundleName = "com.ix.hiMusic"; + + AbilityRequest abilityRequest; + abilityRequest.want = CreateWant(""); + abilityRequest.abilityInfo = CreateAbilityInfo(abilityName + "1", appName, bundleName); + abilityRequest.appInfo = CreateAppInfo(appName, bundleName); + + std::shared_ptr abilityRecord = AbilityRecord::CreateAbilityRecord(abilityRequest); + abilityRecord->SetAbilityState(OHOS::AAFwk::AbilityState::FOREGROUND_NEW); + + abilityMgrServ_->InitMissionListManager(11, true); + Want want; + want.SetElementName("DemoDeviceId", "DemoBundleName", "DemoAbilityName"); + EXPECT_TRUE(abilityMgrServ_->currentMissionListManager_); + abilityMgrServ_->currentMissionListManager_->EnqueueWaittingAbility(abilityRequest); + abilityMgrServ_->OnAcceptWantResponse(want, "flag"); + + EXPECT_EQ(false, abilityRecord->IsNewWant()); +} + +/** + * @tc.name: OnAcceptWantResponse_002 + * @tc.desc: test OnAcceptWantResponse + * @tc.type: FUNC + * @tc.require: AR000GJUND + */ +HWTEST_F(SpecifiedAbilityServiceTest, OnAcceptWantResponse_002, TestSize.Level1) +{ + std::string abilityName = "MusicAbility"; + std::string appName = "test_app"; + std::string bundleName = "com.ix.hiMusic"; + + AbilityRequest abilityRequest; + abilityRequest.want = CreateWant(""); + abilityRequest.abilityInfo = CreateAbilityInfo(abilityName + "1", appName, bundleName); + abilityRequest.appInfo = CreateAppInfo(appName, bundleName); + + std::shared_ptr abilityRecord = AbilityRecord::CreateAbilityRecord(abilityRequest); + abilityRecord->SetAbilityState(OHOS::AAFwk::AbilityState::FOREGROUND_NEW); + abilityRecord->SetSpecifiedFlag("flag"); + + std::shared_ptr missionRecord = std::make_shared(bundleName); + missionRecord->AddAbilityRecordToTop(abilityRecord); + abilityRecord->SetMissionRecord(missionRecord); + + abilityRequest.callerToken = abilityRecord->GetToken(); + auto manager = abilityMgrServ_->currentMissionListManager_; + manager->Init(); + auto mission = std::make_shared(11, abilityRecord, "missionName"); + manager->defaultStandardList_->AddMissionToTop(mission); + + abilityMgrServ_->InitMissionListManager(11, true); + Want want; + want.SetElementName("DemoDeviceId", "DemoBundleName", "DemoAbilityName"); + EXPECT_TRUE(abilityMgrServ_->currentMissionListManager_); + abilityMgrServ_->currentMissionListManager_->EnqueueWaittingAbility(abilityRequest); + abilityMgrServ_->OnAcceptWantResponse(want, "flag"); + + EXPECT_EQ(true, abilityRecord->IsNewWant()); +} +} // namespace AAFwk +} // namespace OHOS diff --git a/services/test/moduletest/on_new_want_module_test/BUILD.gn b/services/test/moduletest/on_new_want_module_test/BUILD.gn new file mode 100644 index 0000000000000000000000000000000000000000..4293e83b35f91e1955c6bfd0518beaa9d127f72a --- /dev/null +++ b/services/test/moduletest/on_new_want_module_test/BUILD.gn @@ -0,0 +1,94 @@ +# Copyright (c) 2021-2022 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/mstabilitymgrservice" + +ohos_moduletest("on_new_want_module_test") { + module_out_path = module_output_path + + include_dirs = [ + "${services_path}/test/mock/include", + "//third_party/jsoncpp/include", + "//foundation/aafwk/standard/frameworks/kits/ability/native/include/distributed_ability_runtime", + "//foundation/aafwk/standard/interfaces/innerkits/dataobs_manager/include", + "//base/hiviewdfx/hicollie/interfaces/native/innerkits/include/xcollie", + ] + + sources = [ "on_new_want_module_test.cpp" ] + sources += [ + "//foundation/aafwk/standard/services/abilitymgr/src/ability_manager_service.cpp", + "//foundation/aafwk/standard/services/abilitymgr/src/mission_list.cpp", + "//foundation/aafwk/standard/services/abilitymgr/src/mission_list_manager.cpp", + "//foundation/aafwk/standard/services/common/src/permission_verification.cpp", + "//foundation/distributedhardware/devicemanager/test/unittest/mock/parameter.cpp", + ] + + configs = [ + "${services_path}/test:aafwk_module_test_config", + "//foundation/aafwk/standard/interfaces/innerkits/ability_manager:ability_manager_public_config", + "//foundation/aafwk/standard/services/abilitymgr/test/mock/libs/sa_mgr:sa_mgr_mock_config", + "//foundation/appexecfwk/standard/interfaces/innerkits/appexecfwk_base:appexecfwk_base_sdk_config", + "${aafwk_path}/interfaces/innerkits/app_manager:appmgr_sdk_config", + ] + cflags = [] + if (target_cpu == "arm") { + cflags += [ "-DBINDER_IPC_32BIT" ] + } + deps = [ + "${aafwk_path}/interfaces/innerkits/app_manager:app_manager", + "${innerkits_path}/uri_permission:uri_permission_mgr", + "${innerkits_path}/want:want", + "//base/account/os_account/frameworks/osaccount/native:os_account_innerkits", + "//base/global/i18n_standard/frameworks/intl:intl_util", + "//foundation/aafwk/standard/frameworks/kits/ability/native:abilitykit_native", + "//foundation/aafwk/standard/frameworks/kits/ability/native:dummy_classes", + "//foundation/aafwk/standard/services/abilitymgr:abilityms", + "//foundation/ace/ace_engine/interfaces/innerkits/ui_service_manager:ui_service_mgr", + "//foundation/appexecfwk/standard/interfaces/innerkits/appexecfwk_base:appexecfwk_base", + "//foundation/appexecfwk/standard/interfaces/innerkits/appexecfwk_core:appexecfwk_core", + "//foundation/appexecfwk/standard/interfaces/innerkits/libeventhandler:libeventhandler", + "//foundation/distributedschedule/dmsfwk/interfaces/innerkits/uri:zuri", + "//foundation/distributedschedule/safwk/interfaces/innerkits/safwk:system_ability_fwk", + "//foundation/distributedschedule/samgr/interfaces/innerkits/samgr_proxy:samgr_proxy", + "//foundation/multimedia/image_standard/interfaces/innerkits:image_native", + "//third_party/googletest:gmock_main", + "//third_party/googletest:gtest_main", + "//third_party/jsoncpp:jsoncpp", + "//third_party/libpng:libpng", + "//utils/native/base:utils", + ] + + external_deps = [ + "access_token:libaccesstoken_sdk", + "bytrace_standard:bytrace_core", + "ces_standard:cesfwk_core", + "ces_standard:cesfwk_innerkits", + "dsoftbus_standard:softbus_client", + "hicollie_native:libhicollie", + "hisysevent_native:libhisysevent", + "hiviewdfx_hilog_native:libhilog", + "ipc:ipc_core", + "native_appdatamgr:native_appdatafwk", + "native_appdatamgr:native_dataability", + "native_appdatamgr:native_rdb", + ] +} + +group("moduletest") { + testonly = true + + deps = [ ":on_new_want_module_test" ] +} diff --git a/services/test/moduletest/on_new_want_module_test/on_new_want_module_test.cpp b/services/test/moduletest/on_new_want_module_test/on_new_want_module_test.cpp new file mode 100644 index 0000000000000000000000000000000000000000..a46b32fe1a1a3c0acc2ea76e9e6ee8033f230eb5 --- /dev/null +++ b/services/test/moduletest/on_new_want_module_test/on_new_want_module_test.cpp @@ -0,0 +1,181 @@ +/* + * 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 +#include + +#include +#include + +#define private public +#include "ability_manager_service.h" +#include "mission_list_manager.h" +#undef private +#include "ability_util.h" +#include "mission_list.h" + +using namespace testing::ext; +using namespace OHOS::AppExecFwk; +using OHOS::iface_cast; +using OHOS::sptr; +using testing::_; +using testing::Invoke; +using testing::Return; + +namespace OHOS { +namespace AAFwk { +class OnNewWantModuleTest : public testing::Test { +public: + static void SetUpTestCase(); + static void TearDownTestCase(); + void SetUp(); + void TearDown(); + AbilityInfo CreateAbilityInfo( + const std::string &name, const std::string &appName, const std::string &bundleName, bool mode); + ApplicationInfo CreateAppInfo(const std::string &appName, const std::string &name); + inline static std::shared_ptr abilityMgrServ_ {nullptr}; +}; + +AbilityInfo OnNewWantModuleTest::CreateAbilityInfo( + const std::string &name, const std::string &appName, const std::string &bundleName, bool mode) +{ + AbilityInfo abilityInfo; + abilityInfo.visible = true; + abilityInfo.name = name; + abilityInfo.applicationName = appName; + abilityInfo.bundleName = bundleName; + abilityInfo.applicationInfo.bundleName = bundleName; + abilityInfo.applicationName = "com.ix.hiMusic"; + abilityInfo.applicationInfo.name = "com.ix.hiMusic"; + abilityInfo.type = AbilityType::PAGE; + abilityInfo.applicationInfo.isLauncherApp = false; + + if (mode) { + abilityInfo.launchMode = LaunchMode::SINGLETON; + } else { + abilityInfo.launchMode = LaunchMode::STANDARD; + } + + return abilityInfo; +} + +ApplicationInfo OnNewWantModuleTest::CreateAppInfo(const std::string &appName, const std::string &bundleName) +{ + ApplicationInfo appInfo; + appInfo.name = appName; + appInfo.bundleName = bundleName; + + return appInfo; +} + +void OnNewWantModuleTest::SetUpTestCase() +{ + abilityMgrServ_ = OHOS::DelayedSingleton::GetInstance(); +} + +void OnNewWantModuleTest::TearDownTestCase() +{ + abilityMgrServ_->OnStop(); +} + +void OnNewWantModuleTest::SetUp() +{} + +void OnNewWantModuleTest::TearDown() +{} + +/* + * Feature: MissionListManager + * Function: Startability + * SubFunction: NA + * FunctionPoints: NA + * CaseDescription:Start the singleton ability multiple times, and the ability is marked with newwant + */ +HWTEST_F(OnNewWantModuleTest, Startability_001, TestSize.Level1) +{ + int userId = 100; + auto missionListManager = std::make_shared(userId); + EXPECT_TRUE(missionListManager); + + std::string abilityName = "MusicAbility"; + std::string appName = "test_app"; + std::string bundleName = "com.ix.hiMusic"; + + Want want; + ElementName element("device", "com.ix.hiMusic", "MusicAbility"); + want.SetElement(element); + + AbilityRequest abilityRequest; + abilityRequest.want = want; + abilityRequest.abilityInfo = CreateAbilityInfo(abilityName, appName, bundleName, true); + abilityRequest.appInfo = CreateAppInfo(appName, bundleName); + abilityRequest.compatibleVersion = 8; + + std::shared_ptr abilityRecord = AbilityRecord::CreateAbilityRecord(abilityRequest); + abilityRecord->SetAbilityState(OHOS::AAFwk::AbilityState::FOREGROUND_NEW); + + std::string missionName = + AbilityUtil::ConvertBundleNameSingleton(abilityRequest.abilityInfo.bundleName, abilityRequest.abilityInfo.name); + std::shared_ptr mission = std::make_shared(1, abilityRecord, missionName); + + missionListManager->Init(); + missionListManager->launcherList_->AddMissionToTop(mission); + missionListManager->StartAbility(abilityRequest); + + EXPECT_TRUE(abilityRecord->IsNewWant()); +} + +/* + * Feature: MissionListManager + * Function: Startability + * SubFunction: NA + * FunctionPoints: NA + * CaseDescription:Start standard ability multiple times, ability has no newwant mark + */ +HWTEST_F(OnNewWantModuleTest, Startability_002, TestSize.Level1) +{ + int userId = 100; + auto missionListManager = std::make_shared(userId); + EXPECT_TRUE(missionListManager); + + std::string abilityName = "MusicAbility"; + std::string appName = "test_app"; + std::string bundleName = "com.ix.hiMusic"; + + Want want; + ElementName element("device", "com.ix.hiMusic", "MusicAbility"); + want.SetElement(element); + + AbilityRequest abilityRequest; + abilityRequest.want = want; + abilityRequest.abilityInfo = CreateAbilityInfo(abilityName, appName, bundleName, false); + abilityRequest.appInfo = CreateAppInfo(appName, bundleName); + abilityRequest.compatibleVersion = 8; + + std::shared_ptr abilityRecord = AbilityRecord::CreateAbilityRecord(abilityRequest); + abilityRecord->SetAbilityState(OHOS::AAFwk::AbilityState::FOREGROUND_NEW); + + std::string missionName = + AbilityUtil::ConvertBundleNameSingleton(abilityRequest.abilityInfo.bundleName, abilityRequest.abilityInfo.name); + std::shared_ptr mission = std::make_shared(1, abilityRecord, missionName); + + missionListManager->Init(); + missionListManager->launcherList_->AddMissionToTop(mission); + missionListManager->StartAbility(abilityRequest); + + EXPECT_EQ(abilityRecord->IsNewWant(), false); +} +} // namespace AAFwk +} // namespace OHOS diff --git a/services/test/moduletest/running_infos_module_test/BUILD.gn b/services/test/moduletest/running_infos_module_test/BUILD.gn new file mode 100644 index 0000000000000000000000000000000000000000..c0e24fc3d313ef98942a8280b56888bf1c96ca3c --- /dev/null +++ b/services/test/moduletest/running_infos_module_test/BUILD.gn @@ -0,0 +1,129 @@ +# 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/mstabilitymgrservice" + +ohos_moduletest("running_infos_module_test") { + module_out_path = module_output_path + + include_dirs = [ + "${services_path}/test/mock/include", + "//third_party/jsoncpp/include", + "//foundation/aafwk/standard/frameworks/kits/ability/native/include/distributed_ability_runtime", + "//foundation/aafwk/standard/interfaces/innerkits/dataobs_manager/include", + ] + + sources = [ "running_infos_module_test.cpp" ] + sources += [ + "//foundation/aafwk/standard/services/abilitymgr/src/ability_connect_callback_stub.cpp", + "//foundation/aafwk/standard/services/abilitymgr/src/ability_connect_manager.cpp", + "//foundation/aafwk/standard/services/abilitymgr/src/ability_event_handler.cpp", + "//foundation/aafwk/standard/services/abilitymgr/src/ability_manager_proxy.cpp", + "//foundation/aafwk/standard/services/abilitymgr/src/ability_manager_service.cpp", + "//foundation/aafwk/standard/services/abilitymgr/src/ability_manager_stub.cpp", + "//foundation/aafwk/standard/services/abilitymgr/src/ability_mission_info.cpp", + "//foundation/aafwk/standard/services/abilitymgr/src/ability_record.cpp", + "//foundation/aafwk/standard/services/abilitymgr/src/ability_record_info.cpp", + "//foundation/aafwk/standard/services/abilitymgr/src/ability_scheduler_proxy.cpp", + "//foundation/aafwk/standard/services/abilitymgr/src/ability_scheduler_stub.cpp", + "//foundation/aafwk/standard/services/abilitymgr/src/ability_stack_manager.cpp", + "//foundation/aafwk/standard/services/abilitymgr/src/ability_token_stub.cpp", + "//foundation/aafwk/standard/services/abilitymgr/src/caller_info.cpp", + "//foundation/aafwk/standard/services/abilitymgr/src/connection_record.cpp", + "//foundation/aafwk/standard/services/abilitymgr/src/data_ability_manager.cpp", + "//foundation/aafwk/standard/services/abilitymgr/src/data_ability_record.cpp", + "//foundation/aafwk/standard/services/abilitymgr/src/launch_param.cpp", + "//foundation/aafwk/standard/services/abilitymgr/src/lifecycle_deal.cpp", + "//foundation/aafwk/standard/services/abilitymgr/src/lifecycle_state_info.cpp", + "//foundation/aafwk/standard/services/abilitymgr/src/mission_description_info.cpp", + "//foundation/aafwk/standard/services/abilitymgr/src/mission_option.cpp", + "//foundation/aafwk/standard/services/abilitymgr/src/mission_record.cpp", + "//foundation/aafwk/standard/services/abilitymgr/src/mission_record_info.cpp", + "//foundation/aafwk/standard/services/abilitymgr/src/mission_snapshot.cpp", + "//foundation/aafwk/standard/services/abilitymgr/src/mission_stack.cpp", + "//foundation/aafwk/standard/services/abilitymgr/src/mission_stack_info.cpp", + "//foundation/aafwk/standard/services/abilitymgr/src/pending_want_key.cpp", + "//foundation/aafwk/standard/services/abilitymgr/src/pending_want_manager.cpp", + "//foundation/aafwk/standard/services/abilitymgr/src/pending_want_record.cpp", + "//foundation/aafwk/standard/services/abilitymgr/src/power_storage.cpp", + "//foundation/aafwk/standard/services/abilitymgr/src/sender_info.cpp", + "//foundation/aafwk/standard/services/abilitymgr/src/stack_info.cpp", + "//foundation/aafwk/standard/services/abilitymgr/src/want_receiver_stub.cpp", + "//foundation/aafwk/standard/services/abilitymgr/src/want_sender_info.cpp", + "//foundation/aafwk/standard/services/abilitymgr/src/want_sender_stub.cpp", + "//foundation/aafwk/standard/services/abilitymgr/src/wants_info.cpp", + "//foundation/aafwk/standard/services/abilitymgr/test/mock/libs/sa_mgr/src/sa_mgr_client_mock.cpp", + "//foundation/aafwk/standard/services/test/mock/src/mock_app_mgr_client.cpp", + "//foundation/aafwk/standard/services/test/mock/src/mock_bundle_mgr.cpp", + ] + + configs = [ + "${services_path}/test:aafwk_module_test_config", + "//foundation/aafwk/standard/interfaces/innerkits/ability_manager:ability_manager_public_config", + "//foundation/aafwk/standard/services/abilitymgr/test/mock/libs/sa_mgr:sa_mgr_mock_config", + "//foundation/appexecfwk/standard/interfaces/innerkits/appexecfwk_base:appexecfwk_base_sdk_config", + "${aafwk_path}/interfaces/innerkits/app_manager:appmgr_sdk_config", + ] + cflags = [] + if (target_cpu == "arm") { + cflags += [ "-DBINDER_IPC_32BIT" ] + } + deps = [ + "${aafwk_path}/interfaces/innerkits/app_manager:app_manager", + "${innerkits_path}/uri_permission:uri_permission_mgr", + "${innerkits_path}/want:want", + "${services_path}/common:perm_verification", + "//base/account/os_account/frameworks/osaccount/native:os_account_innerkits", + "//base/global/i18n_standard/frameworks/intl:intl_util", + "//base/startup/syspara_lite/interfaces/innerkits/native/syspara:syspara", + "//foundation/aafwk/standard/frameworks/kits/ability/native:abilitykit_native", + "//foundation/aafwk/standard/frameworks/kits/ability/native:dummy_classes", + "//foundation/aafwk/standard/services/abilitymgr:abilityms", + "//foundation/ace/ace_engine/interfaces/innerkits/ui_service_manager:ui_service_mgr", + "//foundation/appexecfwk/standard/interfaces/innerkits/appexecfwk_base:appexecfwk_base", + "//foundation/appexecfwk/standard/interfaces/innerkits/appexecfwk_core:appexecfwk_core", + "//foundation/appexecfwk/standard/interfaces/innerkits/libeventhandler:libeventhandler", + "//foundation/distributedschedule/dmsfwk/interfaces/innerkits/uri:zuri", + "//foundation/distributedschedule/safwk/interfaces/innerkits/safwk:system_ability_fwk", + "//foundation/distributedschedule/samgr/interfaces/innerkits/samgr_proxy:samgr_proxy", + "//foundation/multimedia/image_standard/interfaces/innerkits:image_native", + "//third_party/googletest:gmock_main", + "//third_party/googletest:gtest_main", + "//third_party/jsoncpp:jsoncpp", + "//third_party/libpng:libpng", + "//utils/native/base:utils", + ] + + external_deps = [ + "access_token:libaccesstoken_sdk", + "bytrace_standard:bytrace_core", + "ces_standard:cesfwk_core", + "ces_standard:cesfwk_innerkits", + "dsoftbus_standard:softbus_client", + "hicollie_native:libhicollie", + "hiviewdfx_hilog_native:libhilog", + "ipc:ipc_core", + "native_appdatamgr:native_appdatafwk", + "native_appdatamgr:native_dataability", + "native_appdatamgr:native_rdb", + ] +} + +group("moduletest") { + testonly = true + + deps = [ ":running_infos_module_test" ] +} diff --git a/services/test/moduletest/running_infos_module_test/running_infos_module_test.cpp b/services/test/moduletest/running_infos_module_test/running_infos_module_test.cpp new file mode 100644 index 0000000000000000000000000000000000000000..6fd87087cf341a4532058c2928146133209188c4 --- /dev/null +++ b/services/test/moduletest/running_infos_module_test/running_infos_module_test.cpp @@ -0,0 +1,477 @@ +/* + * 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 +#include +#include +#include + +#include +#include + +#define private public +#define protected public +#include "sa_mgr_client.h" +#include "mock_ability_connect_callback_stub.h" +#include "mock_ability_scheduler.h" +#include "mock_app_mgr_client.h" +#include "mock_bundle_mgr.h" +#include "ability_record_info.h" +#include "ability_manager_errors.h" +#include "sa_mgr_client.h" +#include "system_ability_definition.h" +#include "ability_manager_service.h" +#include "ability_connect_callback_proxy.h" +#include "ability_config.h" +#include "pending_want_manager.h" +#include "pending_want_record.h" +#undef private +#undef protected +#include "wants_info.h" +#include "want_receiver_stub.h" +#include "want_sender_stub.h" + +using namespace std::placeholders; +using namespace testing::ext; +using namespace OHOS::AppExecFwk; +using OHOS::iface_cast; +using OHOS::IRemoteObject; +using OHOS::sptr; +using testing::_; +using testing::Invoke; +using testing::Return; + +namespace OHOS { +namespace AAFwk { +namespace { +const int32_t MOCK_MAIN_USER_ID = 100; + +static void WaitUntilTaskFinished() +{ + const uint32_t maxRetryCount = 1000; + const uint32_t sleepTime = 1000; + uint32_t count = 0; + auto handler = OHOS::DelayedSingleton::GetInstance()->GetEventHandler(); + std::atomic taskCalled(false); + auto f = [&taskCalled]() { taskCalled.store(true); }; + if (handler->PostTask(f)) { + while (!taskCalled.load()) { + ++count; + if (count >= maxRetryCount) { + break; + } + usleep(sleepTime); + } + } +} +} // namespace +class RunningInfosModuleTest : public testing::Test { +public: + static void SetUpTestCase(void); + static void TearDownTestCase(void); + void SetUp(); + void TearDown(); + void OnStartAms(); + void OnStopAms(); + Want CreateWant(const std::string &abilityName, const std::string &bundleName); + + inline static std::shared_ptr abilityMgrServ_ {nullptr}; + inline static MockAppMgrClient *mockAppMgrClient_ = nullptr; +}; + +Want RunningInfosModuleTest::CreateWant(const std::string &abilityName, const std::string &bundleName) +{ + ElementName element; + element.SetDeviceID("device"); + element.SetAbilityName(abilityName); + element.SetBundleName(bundleName); + Want want; + want.SetElement(element); + return want; +} + +void RunningInfosModuleTest::OnStartAms() +{ + if (abilityMgrServ_) { + if (abilityMgrServ_->state_ == ServiceRunningState::STATE_RUNNING) { + return; + } + + abilityMgrServ_->state_ = ServiceRunningState::STATE_RUNNING; + abilityMgrServ_->useNewMission_ = true; + + abilityMgrServ_->eventLoop_ = AppExecFwk::EventRunner::Create(AbilityConfig::NAME_ABILITY_MGR_SERVICE); + EXPECT_TRUE(abilityMgrServ_->eventLoop_); + + abilityMgrServ_->handler_ = std::make_shared(abilityMgrServ_->eventLoop_, abilityMgrServ_); + EXPECT_TRUE(abilityMgrServ_->handler_); + + // init user controller. + abilityMgrServ_->userController_ = std::make_shared(); + EXPECT_TRUE(abilityMgrServ_->userController_); + abilityMgrServ_->userController_->Init(); + int userId = MOCK_MAIN_USER_ID; + abilityMgrServ_->userController_->SetCurrentUserId(userId); + abilityMgrServ_->InitConnectManager(userId, true); + abilityMgrServ_->InitDataAbilityManager(userId, true); + abilityMgrServ_->InitPendWantManager(userId, true); + abilityMgrServ_->systemDataAbilityManager_ = std::make_shared(); + EXPECT_TRUE(abilityMgrServ_->systemDataAbilityManager_); + + abilityMgrServ_->amsConfigResolver_ = std::make_shared(); + EXPECT_TRUE(abilityMgrServ_->amsConfigResolver_); + abilityMgrServ_->amsConfigResolver_->Parse(); + abilityMgrServ_->useNewMission_ = abilityMgrServ_->amsConfigResolver_->IsUseNewMission(); + + abilityMgrServ_->SetStackManager(userId, true); + abilityMgrServ_->InitMissionListManager(userId, true); + abilityMgrServ_->connectManager_->SetEventHandler(abilityMgrServ_->handler_); + abilityMgrServ_->eventLoop_->Run(); + auto topAbility = abilityMgrServ_->GetListManagerByUserId(MOCK_MAIN_USER_ID)->GetCurrentTopAbilityLocked(); + if (topAbility) { + topAbility->SetAbilityState(AAFwk::AbilityState::FOREGROUND_NEW); + } + WaitUntilTaskFinished(); + return; + } + + GTEST_LOG_(INFO) << "OnStart fail"; +} + +void RunningInfosModuleTest::OnStopAms() +{ + abilityMgrServ_->eventLoop_.reset(); + abilityMgrServ_->handler_.reset(); + abilityMgrServ_->state_ = ServiceRunningState::STATE_NOT_START; +} + +void RunningInfosModuleTest::SetUpTestCase() +{ + OHOS::DelayedSingleton::GetInstance()->RegisterSystemAbility( + OHOS::BUNDLE_MGR_SERVICE_SYS_ABILITY_ID, new BundleMgrService()); + auto appScheduler = DelayedSingleton::GetInstance(); + mockAppMgrClient_ = new MockAppMgrClient(); + if (mockAppMgrClient_) { + appScheduler->appMgrClient_.reset(mockAppMgrClient_); + GTEST_LOG_(INFO) << "mock appMgrClient_ ok"; + } +} + +void RunningInfosModuleTest::TearDownTestCase() +{ + delete mockAppMgrClient_; + mockAppMgrClient_ = nullptr; + OHOS::DelayedSingleton::DestroyInstance(); +} + +void RunningInfosModuleTest::SetUp() +{ + abilityMgrServ_ = OHOS::DelayedSingleton::GetInstance(); + OnStartAms(); +} + +void RunningInfosModuleTest::TearDown() +{ + OnStopAms(); + OHOS::DelayedSingleton::DestroyInstance(); +} + +/* + * Feature: AbilityManagerService + * Function: GetAbilityRunningInfos + * SubFunction: NA + * FunctionPoints:query ability running infos + * EnvConditions: NA + * CaseDescription: start page ability, mock object, call query function. + */ +HWTEST_F(RunningInfosModuleTest, GetAbilityRunningInfos_001, TestSize.Level1) +{ + std::string abilityName = "MusicAbility"; + std::string bundleName = "com.ix.hiMusic"; + EXPECT_CALL(*mockAppMgrClient_, LoadAbility(_, _, _, _, _)).Times(1); + Want want = CreateWant(abilityName, bundleName); + + auto result = abilityMgrServ_->StartAbility(want); + EXPECT_EQ(OHOS::ERR_OK, result); + EXPECT_CALL(*mockAppMgrClient_, GetRunningProcessInfoByToken(_, _)).Times(1); + + std::vector infos; + abilityMgrServ_->GetAbilityRunningInfos(infos); + size_t infoCount {1}; + EXPECT_TRUE(infos.size() == infoCount); + if (infos.size() == infoCount) { + EXPECT_TRUE(infos[0].ability.GetAbilityName() == abilityName); + EXPECT_TRUE(infos[0].abilityState == static_cast(AbilityState::INITIAL)); + } +} + +/* + * Feature: AbilityManagerService + * Function: GetAbilityRunningInfos + * SubFunction: NA + * FunctionPoints:query ability running infos + * EnvConditions: NA + * CaseDescription: start service ability, mock object, call query function. + */ +HWTEST_F(RunningInfosModuleTest, GetAbilityRunningInfos_002, TestSize.Level1) +{ + std::string abilityName = "ServiceAbility"; + std::string bundleName = "com.ix.hiService"; + EXPECT_CALL(*mockAppMgrClient_, LoadAbility(_, _, _, _, _)).Times(1); + Want want = CreateWant(abilityName, bundleName); + auto result = abilityMgrServ_->StartAbility(want); + EXPECT_EQ(OHOS::ERR_OK, result); + EXPECT_CALL(*mockAppMgrClient_, GetRunningProcessInfoByToken(_, _)).Times(1); + + std::vector infos; + abilityMgrServ_->GetAbilityRunningInfos(infos); + size_t infoCount {1}; + EXPECT_TRUE(infos.size() == infoCount); + if (infos.size() == infoCount) { + EXPECT_TRUE(infos[0].ability.GetAbilityName() == abilityName); + EXPECT_TRUE(infos[0].abilityState == static_cast(AbilityState::INITIAL)); + } +} + +/* + * Feature: AbilityManagerService + * Function: GetAbilityRunningInfos + * SubFunction: NA + * FunctionPoints:query ability running infos + * EnvConditions: NA + * CaseDescription: start launcher, mock object, call query function. + */ +HWTEST_F(RunningInfosModuleTest, GetAbilityRunningInfos_003, TestSize.Level1) +{ + std::string abilityName = "com.ohos.launcher.MainAbility"; + std::string bundleName = "com.ohos.launcher"; + EXPECT_CALL(*mockAppMgrClient_, LoadAbility(_, _, _, _, _)).Times(1); + Want want = CreateWant(abilityName, bundleName); + auto result = abilityMgrServ_->StartAbility(want); + EXPECT_EQ(OHOS::ERR_OK, result); + EXPECT_CALL(*mockAppMgrClient_, GetRunningProcessInfoByToken(_, _)).Times(1); + + std::vector infos; + abilityMgrServ_->GetAbilityRunningInfos(infos); + size_t infoCount {1}; + EXPECT_TRUE(infos.size() == infoCount); + if (infos.size() == infoCount) { + EXPECT_TRUE(infos[0].ability.GetAbilityName() == abilityName); + EXPECT_TRUE(infos[0].abilityState == static_cast(AbilityState::INITIAL)); + } +} + +/* + * Feature: AbilityManagerService + * Function: GetAbilityRunningInfos + * SubFunction: NA + * FunctionPoints:query ability running infos + * EnvConditions: NA + * CaseDescription: start two page abilities, mock object, call query function. + */ +HWTEST_F(RunningInfosModuleTest, GetAbilityRunningInfos_004, TestSize.Level1) +{ + std::string abilityName = "MusicAbility"; + std::string bundleName = "com.ix.hiMusic"; + EXPECT_CALL(*mockAppMgrClient_, LoadAbility(_, _, _, _, _)).Times(2); + Want want = CreateWant(abilityName, bundleName); + auto result = abilityMgrServ_->StartAbility(want); + EXPECT_EQ(OHOS::ERR_OK, result); + + auto topAbility = abilityMgrServ_->currentMissionListManager_->GetCurrentTopAbilityLocked(); + EXPECT_TRUE(topAbility); + topAbility->SetAbilityState(AbilityState::FOREGROUND_NEW); + + std::string abilityName2 = "MusicAbilityOther"; + std::string bundleName2 = "com.ix.hiMusicOther"; + want = CreateWant(abilityName2, bundleName2); + auto result2 = abilityMgrServ_->StartAbility(want); + EXPECT_EQ(OHOS::ERR_OK, result2); + EXPECT_CALL(*mockAppMgrClient_, GetRunningProcessInfoByToken(_, _)).Times(2); + + std::vector infos; + abilityMgrServ_->GetAbilityRunningInfos(infos); + size_t infoCount {2}; + EXPECT_TRUE(infos.size() == infoCount); + if (infos.size() == infoCount) { + EXPECT_TRUE(infos[0].ability.GetAbilityName() == abilityName2); + EXPECT_TRUE(infos[0].abilityState == static_cast(AbilityState::INITIAL)); + EXPECT_TRUE(infos[1].ability.GetAbilityName() == abilityName); + EXPECT_TRUE(infos[1].abilityState == static_cast(AbilityState::FOREGROUND_NEW)); + } +} + +/* + * Feature: AbilityManagerService + * Function: GetAbilityRunningInfos + * SubFunction: NA + * FunctionPoints:query ability running infos + * EnvConditions: NA + * CaseDescription: start two service abilities, mock object, call query function. + */ +HWTEST_F(RunningInfosModuleTest, GetAbilityRunningInfos_005, TestSize.Level1) +{ + std::string abilityName = "ServiceAbility"; + std::string bundleName = "com.ix.hiService"; + EXPECT_CALL(*mockAppMgrClient_, LoadAbility(_, _, _, _, _)).Times(2); + Want want = CreateWant(abilityName, bundleName); + auto result = abilityMgrServ_->StartAbility(want); + EXPECT_EQ(OHOS::ERR_OK, result); + + std::string abilityName2 = "ServiceAbilityOther"; + std::string bundleName2 = "com.ix.hiServiceOther"; + want = CreateWant(abilityName2, bundleName2); + auto result2 = abilityMgrServ_->StartAbility(want); + EXPECT_EQ(OHOS::ERR_OK, result2); + EXPECT_CALL(*mockAppMgrClient_, GetRunningProcessInfoByToken(_, _)).Times(2); + + std::vector infos; + abilityMgrServ_->GetAbilityRunningInfos(infos); + size_t infoCount {2}; + EXPECT_TRUE(infos.size() == infoCount); + if (infos.size() == infoCount) { + EXPECT_TRUE(infos[0].ability.GetAbilityName() == abilityName); + EXPECT_TRUE(infos[0].abilityState == static_cast(AbilityState::INITIAL)); + EXPECT_TRUE(infos[1].ability.GetAbilityName() == abilityName2); + EXPECT_TRUE(infos[1].abilityState == static_cast(AbilityState::INITIAL)); + } +} + +/* + * Feature: AbilityManagerService + * Function: GetAbilityRunningInfos + * SubFunction: NA + * FunctionPoints:query ability running infos + * EnvConditions: NA + * CaseDescription: start two launcher abilities, mock object, call query function. + */ +HWTEST_F(RunningInfosModuleTest, GetAbilityRunningInfos_006, TestSize.Level1) +{ + std::string abilityName = "com.ohos.launcher.MainAbility"; + std::string bundleName = "com.ohos.launcher"; + EXPECT_CALL(*mockAppMgrClient_, LoadAbility(_, _, _, _, _)).Times(2); + Want want = CreateWant(abilityName, bundleName); + auto result = abilityMgrServ_->StartAbility(want); + EXPECT_EQ(OHOS::ERR_OK, result); + + auto topAbility = abilityMgrServ_->currentMissionListManager_->GetCurrentTopAbilityLocked(); + EXPECT_TRUE(topAbility); + topAbility->SetAbilityState(AbilityState::FOREGROUND_NEW); + + std::string abilityName2 = "com.ohos.launcher.MainAbilityOther"; + std::string bundleName2 = "com.ohos.launcherOther"; + want = CreateWant(abilityName2, bundleName2); + auto result2 = abilityMgrServ_->StartAbility(want); + EXPECT_EQ(OHOS::ERR_OK, result2); + EXPECT_CALL(*mockAppMgrClient_, GetRunningProcessInfoByToken(_, _)).Times(2); + + std::vector infos; + abilityMgrServ_->GetAbilityRunningInfos(infos); + size_t infoCount {2}; + EXPECT_TRUE(infos.size() == infoCount); + if (infos.size() == infoCount) { + EXPECT_TRUE(infos[0].ability.GetAbilityName() == abilityName2); + EXPECT_TRUE(infos[0].abilityState == static_cast(AbilityState::INITIAL)); + EXPECT_TRUE(infos[1].ability.GetAbilityName() == abilityName); + EXPECT_TRUE(infos[1].abilityState == static_cast(AbilityState::FOREGROUND_NEW)); + } +} + +/* + * Feature: AbilityManagerService + * Function: GetExtensionRunningInfos + * SubFunction: NA + * FunctionPoints:query extension running infos + * EnvConditions: NA + * CaseDescription: start service ability, mock object, call query function. + */ +HWTEST_F(RunningInfosModuleTest, GetExtensionRunningInfos_001, TestSize.Level1) +{ + std::string abilityName = "hiExtension"; + std::string bundleName = "com.ix.hiExtension"; + EXPECT_CALL(*mockAppMgrClient_, LoadAbility(_, _, _, _, _)).Times(1); + Want want = CreateWant(abilityName, bundleName); + auto result = abilityMgrServ_->StartAbility(want); + EXPECT_EQ(OHOS::ERR_OK, result); + EXPECT_CALL(*mockAppMgrClient_, GetRunningProcessInfoByToken(_, _)).Times(1); + + std::vector infos; + int upperLimit = 10; + abilityMgrServ_->GetExtensionRunningInfos(upperLimit, infos); + size_t infoCount {1}; + EXPECT_TRUE(infos.size() == infoCount); + if (infos.size() == infoCount) { + EXPECT_TRUE(infos[0].extension.GetAbilityName() == abilityName); + } +} + +/* + * Feature: AbilityManagerService + * Function: GetExtensionRunningInfos + * SubFunction: NA + * FunctionPoints:query extension running infos + * EnvConditions: NA + * CaseDescription: start service abilities, mock object, call query function. + */ +HWTEST_F(RunningInfosModuleTest, GetExtensionRunningInfos_002, TestSize.Level1) +{ + std::string abilityName = "hiExtension"; + std::string bundleName = "com.ix.hiExtension"; + EXPECT_CALL(*mockAppMgrClient_, LoadAbility(_, _, _, _, _)).Times(2); + Want want = CreateWant(abilityName, bundleName); + auto result = abilityMgrServ_->StartAbility(want); + EXPECT_EQ(OHOS::ERR_OK, result); + + std::string abilityName2 = "hiExtensionOther"; + std::string bundleName2 = "com.ix.hiExtension"; + want = CreateWant(abilityName2, bundleName2); + auto result2 = abilityMgrServ_->StartAbility(want); + EXPECT_EQ(OHOS::ERR_OK, result2); + EXPECT_CALL(*mockAppMgrClient_, GetRunningProcessInfoByToken(_, _)).Times(2); + + std::vector infos; + int upperLimit = 10; + abilityMgrServ_->GetExtensionRunningInfos(upperLimit, infos); + size_t infoCount {2}; + if (infos.size() == infoCount) { + EXPECT_TRUE(infos[0].extension.GetAbilityName() == abilityName); + EXPECT_TRUE(infos[1].extension.GetAbilityName() == abilityName2); + } +} + +/* + * Feature: AbilityManagerService + * Function: GetProcessRunningInfos + * SubFunction: NA + * FunctionPoints:query process running infos + * EnvConditions: NA + * CaseDescription: start service ability, mock object, call query function. + */ +HWTEST_F(RunningInfosModuleTest, GetProcessRunningInfos_001, TestSize.Level1) +{ + std::string abilityName = "hiExtension"; + std::string bundleName = "com.ix.hiExtension"; + EXPECT_CALL(*mockAppMgrClient_, LoadAbility(_, _, _, _, _)).Times(1); + Want want = CreateWant(abilityName, bundleName); + auto result = abilityMgrServ_->StartAbility(want); + EXPECT_EQ(OHOS::ERR_OK, result); + EXPECT_CALL(*mockAppMgrClient_, GetAllRunningProcesses(_)).Times(1); + + std::vector infos; + auto ret = abilityMgrServ_->GetProcessRunningInfos(infos); + EXPECT_EQ(OHOS::ERR_OK, ret); +} +} // namespace AAFwk +} // namespace OHOS \ No newline at end of file diff --git a/services/test/moduletest/start_option_display_id_test/BUILD.gn b/services/test/moduletest/start_option_display_id_test/BUILD.gn new file mode 100644 index 0000000000000000000000000000000000000000..ba238c981189b89c478bda9b5b140a8c422c2934 --- /dev/null +++ b/services/test/moduletest/start_option_display_id_test/BUILD.gn @@ -0,0 +1,74 @@ +# Copyright (c) 2021-2022 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/mstabilitymgrservice" + +ohos_moduletest("start_option_display_id_test") { + module_out_path = module_output_path + + include_dirs = [ + "${services_path}/abilitymgr/test/mock/libs/system_ability_mock", + "//foundation/distributedschedule/samgr/interfaces/innerkits/samgr_proxy/include", + "//foundation/distributedschedule/samgr/adapter/interfaces/innerkits/include/", + "//foundation/aafwk/standard/interfaces/innerkits/app_manager/include/appmgr", + "//foundation/aafwk/standard/frameworks/kits/ability/native/test/mock/include", + ] + + sources = [ "start_option_display_id_test.cpp" ] + sources += [ + "//foundation/aafwk/standard/services/abilitymgr/test/mock/libs/appexecfwk_core/src/appmgr/mock_app_scheduler.cpp", + "//foundation/aafwk/standard/services/common/src/permission_verification.cpp", + "//foundation/distributedhardware/devicemanager/test/unittest/mock/parameter.cpp", + ] + + configs = [ "${services_path}/abilitymgr:abilityms_config" ] + cflags = [] + if (target_cpu == "arm") { + cflags += [ "-DBINDER_IPC_32BIT" ] + } + deps = [ + "${innerkits_path}/want:want", + "${services_path}/abilitymgr/test:abilityms_test_source", + "${services_path}/abilitymgr/test/mock/libs/aakit:aakit_mock", + "${services_path}/abilitymgr/test/mock/libs/appexecfwk_core:appexecfwk_appmgr_mock", + "${services_path}/abilitymgr/test/mock/libs/appexecfwk_core:appexecfwk_bundlemgr_mock", + "//foundation/aafwk/standard/frameworks/kits/ability/native:abilitykit_native", + "//foundation/aafwk/standard/frameworks/kits/ability/native:dummy_classes", + "//foundation/aafwk/standard/interfaces/innerkits/app_manager:app_manager", + "//foundation/aafwk/standard/services/abilitymgr:abilityms", + "//foundation/appexecfwk/standard/interfaces/innerkits/appexecfwk_base:appexecfwk_base", + "//foundation/appexecfwk/standard/interfaces/innerkits/libeventhandler:libeventhandler", + "//foundation/distributedschedule/dmsfwk/interfaces/innerkits/uri:zuri", + "//third_party/googletest:gmock_main", + "//third_party/googletest:gtest_main", + "//third_party/libpng:libpng", + "//utils/native/base:utils", + ] + + external_deps = [ + "access_token:libaccesstoken_sdk", + "device_manager_base:devicemanagersdk", + "dsoftbus_standard:softbus_client", + "hiviewdfx_hilog_native:libhilog", + "ipc:ipc_core", + ] +} + +group("moduletest") { + testonly = true + + deps = [ ":start_option_display_id_test" ] +} diff --git a/services/test/moduletest/start_option_display_id_test/start_option_display_id_test.cpp b/services/test/moduletest/start_option_display_id_test/start_option_display_id_test.cpp new file mode 100644 index 0000000000000000000000000000000000000000..8ac9fd22876f007f0adb959410c73926ea38e428 --- /dev/null +++ b/services/test/moduletest/start_option_display_id_test/start_option_display_id_test.cpp @@ -0,0 +1,137 @@ +/* + * 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 +#define protected public +#include "ability_manager_service.h" +#include "ability_event_handler.h" +#include "ams_configuration_parameter.h" +#include "ability_stack_manager.h" +#undef private +#undef protected + +#include "app_process_data.h" +#include "system_ability_definition.h" +#include "ability_manager_errors.h" +#include "ability_scheduler.h" +#include "bundlemgr/mock_bundle_manager.h" +#include "sa_mgr_client.h" +#include "mock_ability_connect_callback.h" +#include "mock_ability_token.h" +#include "if_system_ability_manager.h" +#include "iservice_registry.h" +#include "os_account_manager.h" +#include "os_account_info.h" +using namespace testing; +using namespace testing::ext; +using namespace OHOS::AppExecFwk; +using namespace OHOS::AccountSA; +namespace OHOS { +namespace AAFwk { +namespace { +const int32_t USER_ID_U100 = 100; +constexpr int32_t DISPLAY_ID = 256; + +static void WaitUntilTaskFinished() +{ + const uint32_t maxRetryCount = 1000; + const uint32_t sleepTime = 1000; + uint32_t count = 0; + auto handler = OHOS::DelayedSingleton::GetInstance()->GetEventHandler(); + std::atomic taskCalled(false); + auto f = [&taskCalled]() { taskCalled.store(true); }; + if (handler->PostTask(f)) { + while (!taskCalled.load()) { + ++count; + if (count >= maxRetryCount) { + break; + } + usleep(sleepTime); + } + } +} +} // namespace + +class StartOptionDisplayIdTest : public testing::Test { +public: + static void SetUpTestCase(); + static void TearDownTestCase(); + void SetUp(); + void TearDown(); + +public: + std::shared_ptr abilityMgrServ_ {nullptr}; +}; + +void StartOptionDisplayIdTest::SetUpTestCase() +{ + OHOS::DelayedSingleton::GetInstance()->RegisterSystemAbility( + OHOS::BUNDLE_MGR_SERVICE_SYS_ABILITY_ID, new BundleMgrService()); +} + +void StartOptionDisplayIdTest::TearDownTestCase() +{ + OHOS::DelayedSingleton::DestroyInstance(); +} + +void StartOptionDisplayIdTest::SetUp() +{ + abilityMgrServ_ = OHOS::DelayedSingleton::GetInstance(); + abilityMgrServ_->OnStart(); + WaitUntilTaskFinished(); + + abilityMgrServ_->StartUser(USER_ID_U100); + auto topAbility = abilityMgrServ_->GetListManagerByUserId(USER_ID_U100)->GetCurrentTopAbilityLocked(); + if (topAbility) { + topAbility->SetAbilityState(AAFwk::AbilityState::FOREGROUND_NEW); + } +} + +void StartOptionDisplayIdTest::TearDown() +{ + abilityMgrServ_->OnStop(); + OHOS::DelayedSingleton::DestroyInstance(); +} + +/* + * Feature: AbilityManagerService + * Function: StartAbility + * SubFunction: NA + * FunctionPoints: AbilityManagerService StartAbility + * EnvConditions: NA + * CaseDescription: Set displayId, enable ability, get displayId value consistent with the setting + */ +HWTEST_F(StartOptionDisplayIdTest, start_option_001, TestSize.Level1) +{ + Want want; + ElementName element("device", "com.ix.hiMusic", "MusicAbility"); + want.SetElement(element); + StartOptions option; + option.SetDisplayID(DISPLAY_ID); + auto result = abilityMgrServ_->StartAbility(want, option, nullptr, 100, 0); + WaitUntilTaskFinished(); + EXPECT_EQ(OHOS::ERR_OK, result); + + auto topAbility = abilityMgrServ_->GetListManagerByUserId(USER_ID_U100)->GetCurrentTopAbilityLocked(); + EXPECT_TRUE(topAbility); + if (topAbility) { + auto defualtDisplayId = 0; + auto displayId = topAbility->GetWant().GetIntParam(Want::PARAM_RESV_DISPLAY_ID, defualtDisplayId); + EXPECT_EQ(displayId, DISPLAY_ID); + } +} +} // namespace AAFwk +} // namespace OHOS diff --git a/services/test/moduletest/static_start_ability_test/BUILD.gn b/services/test/moduletest/static_start_ability_test/BUILD.gn new file mode 100644 index 0000000000000000000000000000000000000000..006c5e82d981c7b819b3b6ca81c225f911e1372e --- /dev/null +++ b/services/test/moduletest/static_start_ability_test/BUILD.gn @@ -0,0 +1,132 @@ +# Copyright (c) 2021-2022 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/mstabilitymgrservice" + +ohos_moduletest("static_start_ability_test") { + module_out_path = module_output_path + + include_dirs = [ + "${services_path}/test/mock/include", + "//third_party/jsoncpp/include", + "//foundation/aafwk/standard/frameworks/kits/ability/native/include/distributed_ability_runtime", + "//foundation/aafwk/standard/interfaces/innerkits/dataobs_manager/include", + "//base/hiviewdfx/hicollie/interfaces/native/innerkits/include/xcollie", + ] + + sources = [ "static_start_ability_test.cpp" ] + sources += [ + "//foundation/aafwk/standard/services/abilitymgr/src/ability_connect_callback_stub.cpp", + "//foundation/aafwk/standard/services/abilitymgr/src/ability_connect_manager.cpp", + "//foundation/aafwk/standard/services/abilitymgr/src/ability_event_handler.cpp", + "//foundation/aafwk/standard/services/abilitymgr/src/ability_manager_proxy.cpp", + "//foundation/aafwk/standard/services/abilitymgr/src/ability_manager_service.cpp", + "//foundation/aafwk/standard/services/abilitymgr/src/ability_manager_stub.cpp", + "//foundation/aafwk/standard/services/abilitymgr/src/ability_mission_info.cpp", + "//foundation/aafwk/standard/services/abilitymgr/src/ability_record.cpp", + "//foundation/aafwk/standard/services/abilitymgr/src/ability_record_info.cpp", + "//foundation/aafwk/standard/services/abilitymgr/src/ability_scheduler_proxy.cpp", + "//foundation/aafwk/standard/services/abilitymgr/src/ability_scheduler_stub.cpp", + "//foundation/aafwk/standard/services/abilitymgr/src/ability_stack_manager.cpp", + "//foundation/aafwk/standard/services/abilitymgr/src/ability_token_stub.cpp", + "//foundation/aafwk/standard/services/abilitymgr/src/call_container.cpp", + "//foundation/aafwk/standard/services/abilitymgr/src/call_record.cpp", + "//foundation/aafwk/standard/services/abilitymgr/src/caller_info.cpp", + "//foundation/aafwk/standard/services/abilitymgr/src/connection_record.cpp", + "//foundation/aafwk/standard/services/abilitymgr/src/data_ability_manager.cpp", + "//foundation/aafwk/standard/services/abilitymgr/src/data_ability_record.cpp", + "//foundation/aafwk/standard/services/abilitymgr/src/launch_param.cpp", + "//foundation/aafwk/standard/services/abilitymgr/src/lifecycle_deal.cpp", + "//foundation/aafwk/standard/services/abilitymgr/src/lifecycle_state_info.cpp", + "//foundation/aafwk/standard/services/abilitymgr/src/mission_description_info.cpp", + "//foundation/aafwk/standard/services/abilitymgr/src/mission_option.cpp", + "//foundation/aafwk/standard/services/abilitymgr/src/mission_record.cpp", + "//foundation/aafwk/standard/services/abilitymgr/src/mission_record_info.cpp", + "//foundation/aafwk/standard/services/abilitymgr/src/mission_snapshot.cpp", + "//foundation/aafwk/standard/services/abilitymgr/src/mission_stack.cpp", + "//foundation/aafwk/standard/services/abilitymgr/src/mission_stack_info.cpp", + "//foundation/aafwk/standard/services/abilitymgr/src/pending_want_key.cpp", + "//foundation/aafwk/standard/services/abilitymgr/src/pending_want_manager.cpp", + "//foundation/aafwk/standard/services/abilitymgr/src/pending_want_record.cpp", + "//foundation/aafwk/standard/services/abilitymgr/src/power_storage.cpp", + "//foundation/aafwk/standard/services/abilitymgr/src/sender_info.cpp", + "//foundation/aafwk/standard/services/abilitymgr/src/stack_info.cpp", + "//foundation/aafwk/standard/services/abilitymgr/src/want_receiver_stub.cpp", + "//foundation/aafwk/standard/services/abilitymgr/src/want_sender_info.cpp", + "//foundation/aafwk/standard/services/abilitymgr/src/want_sender_stub.cpp", + "//foundation/aafwk/standard/services/abilitymgr/src/wants_info.cpp", + "//foundation/aafwk/standard/services/abilitymgr/test/mock/libs/sa_mgr/src/sa_mgr_client_mock.cpp", + "//foundation/aafwk/standard/services/test/mock/src/mock_app_mgr_client.cpp", + "//foundation/aafwk/standard/services/test/mock/src/mock_bundle_mgr.cpp", + ] + + configs = [ + "${services_path}/test:aafwk_module_test_config", + "//foundation/aafwk/standard/interfaces/innerkits/ability_manager:ability_manager_public_config", + "//foundation/aafwk/standard/services/abilitymgr/test/mock/libs/sa_mgr:sa_mgr_mock_config", + "//foundation/appexecfwk/standard/interfaces/innerkits/appexecfwk_base:appexecfwk_base_sdk_config", + "${aafwk_path}/interfaces/innerkits/app_manager:appmgr_sdk_config", + ] + cflags = [] + if (target_cpu == "arm") { + cflags += [ "-DBINDER_IPC_32BIT" ] + } + deps = [ + "${aafwk_path}/interfaces/innerkits/app_manager:app_manager", + "${innerkits_path}/uri_permission:uri_permission_mgr", + "${innerkits_path}/want:want", + "${services_path}/common:perm_verification", + "//base/account/os_account/frameworks/osaccount/native:os_account_innerkits", + "//base/global/i18n_standard/frameworks/intl:intl_util", + "//base/startup/syspara_lite/interfaces/innerkits/native/syspara:syspara", + "//foundation/aafwk/standard/frameworks/kits/ability/native:abilitykit_native", + "//foundation/aafwk/standard/frameworks/kits/ability/native:dummy_classes", + "//foundation/aafwk/standard/services/abilitymgr:abilityms", + "//foundation/ace/ace_engine/interfaces/innerkits/ui_service_manager:ui_service_mgr", + "//foundation/appexecfwk/standard/interfaces/innerkits/appexecfwk_base:appexecfwk_base", + "//foundation/appexecfwk/standard/interfaces/innerkits/appexecfwk_core:appexecfwk_core", + "//foundation/appexecfwk/standard/interfaces/innerkits/libeventhandler:libeventhandler", + "//foundation/distributedschedule/dmsfwk/interfaces/innerkits/uri:zuri", + "//foundation/distributedschedule/safwk/interfaces/innerkits/safwk:system_ability_fwk", + "//foundation/distributedschedule/samgr/interfaces/innerkits/samgr_proxy:samgr_proxy", + "//foundation/multimedia/image_standard/interfaces/innerkits:image_native", + "//third_party/googletest:gmock_main", + "//third_party/googletest:gtest_main", + "//third_party/jsoncpp:jsoncpp", + "//third_party/libpng:libpng", + "//utils/native/base:utils", + ] + + external_deps = [ + "access_token:libaccesstoken_sdk", + "bytrace_standard:bytrace_core", + "ces_standard:cesfwk_core", + "ces_standard:cesfwk_innerkits", + "dsoftbus_standard:softbus_client", + "hicollie_native:libhicollie", + "hiviewdfx_hilog_native:libhilog", + "ipc:ipc_core", + "native_appdatamgr:native_appdatafwk", + "native_appdatamgr:native_dataability", + "native_appdatamgr:native_rdb", + ] +} + +group("moduletest") { + testonly = true + + deps = [ ":static_start_ability_test" ] +} diff --git a/services/test/moduletest/static_start_ability_test/static_start_ability_test.cpp b/services/test/moduletest/static_start_ability_test/static_start_ability_test.cpp new file mode 100644 index 0000000000000000000000000000000000000000..8202f4760d7e4f6efe089a741d907fafbc806b24 --- /dev/null +++ b/services/test/moduletest/static_start_ability_test/static_start_ability_test.cpp @@ -0,0 +1,322 @@ +/* + * 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 +#include +#include +#include + +#include +#include + +#define private public +#define protected public +#include "sa_mgr_client.h" +#include "mock_ability_connect_callback_stub.h" +#include "mock_ability_scheduler.h" +#include "mock_app_mgr_client.h" +#include "mock_bundle_mgr.h" +#include "ability_record_info.h" +#include "ability_manager_errors.h" +#include "sa_mgr_client.h" +#include "system_ability_definition.h" +#include "ability_manager_service.h" +#include "ability_connect_callback_proxy.h" +#include "ability_config.h" +#include "pending_want_manager.h" +#include "pending_want_record.h" +#undef private +#undef protected +#include "wants_info.h" +#include "want_receiver_stub.h" +#include "want_sender_stub.h" +#include "os_account_manager.h" + +using namespace std::placeholders; +using namespace testing::ext; +using namespace OHOS::AppExecFwk; +using OHOS::iface_cast; +using OHOS::IRemoteObject; +using OHOS::sptr; +using testing::_; +using testing::Invoke; +using testing::Return; + +namespace OHOS { +namespace AAFwk { +namespace { +const std::string COM_OHOS_Test = "com.ohos.test"; +const std::string MAIN_ABILITY = ".MainAbility"; +const std::string MY_APPLICATION = ".MyApplication"; +const std::string DESCRIPTION = "$string:mainability_description"; +const std::string LABEL = "$string:label"; +const std::string ICON = "$media:icon"; +const std::string THEME = "mytheme"; +const std::string KIND = "page"; +const std::string PROCESS = "p10"; +const std::string READ_PERMISSION = "readPermission"; +const std::string WRITE_PERMISSION = "writePermission"; +const std::string RESOURCE_PATH = "/data/app/com.ohos.test"; +const std::string SRC_PATH = "/resources/base/profile"; +const std::string SRC_LANGUAGE = "C++"; +const uint32_t NUMBER_ONE = 10001; +const uint32_t NUMBER_TWO = 10002; +const uint32_t NUMBER_THREE = 10003; +} // namespace +class StaticStartAbilityTest : public testing::Test { +public: + static void SetUpTestCase(void); + static void TearDownTestCase(void); + void SetUp(); + void TearDown(); + bool MockAppClent(); + void SetActive(); + void WaitAMS(); + Want CreateWant(const std::string &abilityName, const std::string &bundleName); + std::shared_ptr GreatePageAbility(const std::string &abilityName, const std::string &bundleName); + + inline static std::shared_ptr mockAppMgrClient_ {nullptr}; + inline static std::shared_ptr abilityMgrServ_ {nullptr}; + inline static BundleMgrService *bundleObject_ {nullptr}; + std::shared_ptr stackManager_ {nullptr}; + sptr scheduler_ {nullptr}; + inline static bool doOnce_ = false; // In order for mock to execute once +}; + +void StaticStartAbilityTest::SetUpTestCase(void) +{ + OHOS::DelayedSingleton::GetInstance()->RegisterSystemAbility( + OHOS::BUNDLE_MGR_SERVICE_SYS_ABILITY_ID, new (std::nothrow) BundleMgrService()); + abilityMgrServ_ = OHOS::DelayedSingleton::GetInstance(); + mockAppMgrClient_ = std::make_shared(); + + if (StaticStartAbilityTest::abilityMgrServ_) { + if (StaticStartAbilityTest::abilityMgrServ_->state_ == ServiceRunningState::STATE_RUNNING) { + return; + } + StaticStartAbilityTest::abilityMgrServ_->state_ = ServiceRunningState::STATE_RUNNING; + StaticStartAbilityTest::abilityMgrServ_->eventLoop_ = + AppExecFwk::EventRunner::Create(AbilityConfig::NAME_ABILITY_MGR_SERVICE); + EXPECT_TRUE(StaticStartAbilityTest::abilityMgrServ_->eventLoop_); + + StaticStartAbilityTest::abilityMgrServ_->handler_ =std::make_shared( + StaticStartAbilityTest::abilityMgrServ_->eventLoop_, StaticStartAbilityTest::abilityMgrServ_); + StaticStartAbilityTest::abilityMgrServ_->connectManager_ = std::make_shared(); + StaticStartAbilityTest::abilityMgrServ_->connectManagers_.emplace(0, + StaticStartAbilityTest::abilityMgrServ_->connectManager_); + EXPECT_TRUE(StaticStartAbilityTest::abilityMgrServ_->handler_); + EXPECT_TRUE(StaticStartAbilityTest::abilityMgrServ_->connectManager_); + + StaticStartAbilityTest::abilityMgrServ_->connectManager_-> + SetEventHandler(StaticStartAbilityTest::abilityMgrServ_->handler_); + + StaticStartAbilityTest::abilityMgrServ_->dataAbilityManager_ = std::make_shared(); + StaticStartAbilityTest::abilityMgrServ_->dataAbilityManagers_.emplace(0, + StaticStartAbilityTest::abilityMgrServ_->dataAbilityManager_); + EXPECT_TRUE(StaticStartAbilityTest::abilityMgrServ_->dataAbilityManager_); + + StaticStartAbilityTest::abilityMgrServ_->amsConfigResolver_ = std::make_shared(); + EXPECT_TRUE(StaticStartAbilityTest::abilityMgrServ_->amsConfigResolver_); + StaticStartAbilityTest::abilityMgrServ_->amsConfigResolver_->Parse(); + + StaticStartAbilityTest::abilityMgrServ_->currentMissionListManager_ = std::make_shared(0); + StaticStartAbilityTest::abilityMgrServ_->currentMissionListManager_->Init(); + + StaticStartAbilityTest::abilityMgrServ_->pendingWantManager_ = std::make_shared(); + EXPECT_TRUE(StaticStartAbilityTest::abilityMgrServ_->pendingWantManager_); + + int userId = StaticStartAbilityTest::abilityMgrServ_->GetUserId(); + StaticStartAbilityTest::abilityMgrServ_->SetStackManager(userId, true); + StaticStartAbilityTest::abilityMgrServ_->stackManagers_.emplace(0, + StaticStartAbilityTest::abilityMgrServ_->GetStackManager()); + StaticStartAbilityTest::abilityMgrServ_->eventLoop_->Run(); + + GTEST_LOG_(INFO) << "OnStart success"; + return; + } + + GTEST_LOG_(INFO) << "OnStart fail"; +} + +void StaticStartAbilityTest::TearDownTestCase(void) +{ + abilityMgrServ_->OnStop(); + mockAppMgrClient_.reset(); +} + +void StaticStartAbilityTest::SetUp(void) +{ + scheduler_ = new MockAbilityScheduler(); + if (!doOnce_) { + doOnce_ = true; + MockAppClent(); + } + WaitAMS(); +} + +void StaticStartAbilityTest::TearDown(void) +{} + +bool StaticStartAbilityTest::MockAppClent() +{ + if (!mockAppMgrClient_) { + GTEST_LOG_(INFO) << "MockAppClent::1"; + return false; + } + + if (!abilityMgrServ_->appScheduler_) { + GTEST_LOG_(INFO) << "MockAppClent::2"; + return false; + } + + abilityMgrServ_->appScheduler_->appMgrClient_.reset(mockAppMgrClient_.get()); + return true; +} + +void StaticStartAbilityTest::SetActive() +{ + if (!abilityMgrServ_) { + return; + } + + auto stackMgr = abilityMgrServ_->GetStackManager(); + if (stackMgr) { + auto topAbility = stackMgr->GetCurrentTopAbility(); + if (topAbility) { + topAbility->SetAbilityState(OHOS::AAFwk::AbilityState::ACTIVE); + } + } +} + +void StaticStartAbilityTest::WaitAMS() +{ + const uint32_t maxRetryCount = 1000; + const uint32_t sleepTime = 1000; + uint32_t count = 0; + if (!abilityMgrServ_) { + return; + } + auto handler = abilityMgrServ_->GetEventHandler(); + if (!handler) { + return; + } + std::atomic taskCalled(false); + auto f = [&taskCalled]() { taskCalled.store(true); }; + if (handler->PostTask(f)) { + while (!taskCalled.load()) { + ++count; + if (count >= maxRetryCount) { + break; + } + usleep(sleepTime); + } + } +} + +Want StaticStartAbilityTest::CreateWant(const std::string &abilityName, const std::string &bundleName) +{ + ElementName element; + element.SetDeviceID(""); + element.SetAbilityName(abilityName); + element.SetBundleName(bundleName); + Want want; + want.SetElement(element); + return want; +} + +std::shared_ptr StaticStartAbilityTest::GreatePageAbility( + const std::string &abilityName, const std::string &bundleName) +{ + Want want = CreateWant(abilityName, bundleName); + EXPECT_CALL(*mockAppMgrClient_, LoadAbility(_, _, _, _, _)).Times(1).WillOnce(Return(AppMgrResultCode::RESULT_OK)); + int testRequestCode = 1; + SetActive(); + abilityMgrServ_->StartAbility(want, 0, testRequestCode); + WaitAMS(); + + auto stack = abilityMgrServ_->GetStackManager(); + if (!stack) { + return nullptr; + } + auto topAbility = stack->GetCurrentTopAbility(); + if (!topAbility) { + return nullptr; + } + topAbility->SetAbilityState(OHOS::AAFwk::AbilityState::ACTIVE); + + return topAbility; +} + +/* + * Feature: AaFwk + * Function: ability manager service + * SubFunction: StartAbility + * FunctionPoints: NA + * EnvConditions: NA + * CaseDescription: Test the abilityInfo query in StartAbility. + */ +HWTEST_F(StaticStartAbilityTest, StaticStartAbility_001, TestSize.Level1) +{ + std::string abilityName = "com.ohos.test.MainAbility"; + std::string bundleName = "com.ohos.test"; + abilityMgrServ_->RemoveAllServiceRecord(); + Want want = CreateWant(abilityName, bundleName); + EXPECT_CALL(*mockAppMgrClient_, LoadAbility(_, _, _, _, _)).Times(1).WillOnce(Return(AppMgrResultCode::RESULT_OK)); + int testRequestCode = 1; + SetActive(); + abilityMgrServ_->StartAbility(want, 0, testRequestCode); + WaitAMS(); + + auto stack = abilityMgrServ_->GetStackManager(); + EXPECT_TRUE(stack); + std::shared_ptr record = stack->GetCurrentTopAbility(); + record->SetAbilityState(OHOS::AAFwk::AbilityState::ACTIVE); + EXPECT_TRUE(record); + AbilityInfo abilityInfo = record->GetAbilityInfo(); + abilityMgrServ_->RemoveAllServiceRecord(); + EXPECT_EQ(abilityInfo.name, COM_OHOS_Test + MAIN_ABILITY); + EXPECT_EQ(abilityInfo.bundleName, COM_OHOS_Test); + EXPECT_EQ(abilityInfo.applicationInfo.bundleName, COM_OHOS_Test); + EXPECT_EQ(abilityInfo.applicationName, COM_OHOS_Test); + EXPECT_EQ(abilityInfo.applicationInfo.name, COM_OHOS_Test); + EXPECT_EQ(abilityInfo.bundleName, COM_OHOS_Test); + EXPECT_EQ(abilityInfo.bundleName, COM_OHOS_Test); + EXPECT_EQ(abilityInfo.bundleName, COM_OHOS_Test); + EXPECT_EQ(abilityInfo.bundleName, COM_OHOS_Test); + EXPECT_EQ(abilityInfo.moduleName, MY_APPLICATION); + EXPECT_EQ(abilityInfo.kind, KIND); + EXPECT_EQ(abilityInfo.process, PROCESS); + EXPECT_EQ(abilityInfo.labelId, NUMBER_ONE); + EXPECT_EQ(abilityInfo.label, LABEL); + EXPECT_EQ(abilityInfo.descriptionId, NUMBER_TWO); + EXPECT_EQ(abilityInfo.description, DESCRIPTION); + EXPECT_EQ(abilityInfo.iconId, NUMBER_THREE); + EXPECT_EQ(abilityInfo.iconPath, ICON); + EXPECT_EQ(abilityInfo.theme, THEME); + EXPECT_EQ(abilityInfo.readPermission, READ_PERMISSION); + EXPECT_EQ(abilityInfo.writePermission, WRITE_PERMISSION); + EXPECT_EQ(abilityInfo.resourcePath, RESOURCE_PATH); + EXPECT_EQ(abilityInfo.srcPath, SRC_PATH); + EXPECT_EQ(abilityInfo.srcLanguage, SRC_LANGUAGE); + EXPECT_FALSE(abilityInfo.multiUserShared); + EXPECT_FALSE(abilityInfo.supportPipMode); + EXPECT_FALSE(abilityInfo.grantPermission); + EXPECT_FALSE(abilityInfo.isLauncherAbility); + EXPECT_FALSE(abilityInfo.isNativeAbility); + EXPECT_FALSE(abilityInfo.enabled); + EXPECT_FALSE(abilityInfo.formEnabled); +} +} // namespace AAFwk +} // namespace OHOS \ No newline at end of file