From ea81039b439d32dce7f8653d6d3a37f6fe698c84 Mon Sep 17 00:00:00 2001 From: njupthan Date: Sat, 5 Mar 2022 13:04:16 +0000 Subject: [PATCH] App UT code submission Signed-off-by: njupthan --- services/appmgr/test/BUILD.gn | 5 + .../test/mock/src/mock_bundle_manager.cpp | 31 ++ .../ams_ability_running_record_test/BUILD.gn | 1 + .../ams_app_life_cycle_test.cpp | 230 ++++++++++- .../ams_app_mgr_client_test.cpp | 63 +++ .../ams_app_running_record_test/BUILD.gn | 1 + .../ams_app_running_record_test.cpp | 309 +++++++++++++-- .../ams_mgr_kill_process_test/BUILD.gn | 66 ++++ .../ams_mgr_kill_process_test.cpp | 121 ++++++ .../ams_service_load_ability_process_test.cpp | 8 - .../app_running_processes_info_test/BUILD.gn | 75 ++++ .../app_running_processes_info_test.cpp | 365 ++++++++++++++++++ 12 files changed, 1234 insertions(+), 41 deletions(-) create mode 100644 services/appmgr/test/unittest/ams_mgr_kill_process_test/BUILD.gn create mode 100644 services/appmgr/test/unittest/ams_mgr_kill_process_test/ams_mgr_kill_process_test.cpp create mode 100644 services/appmgr/test/unittest/app_running_processes_info_test/BUILD.gn create mode 100644 services/appmgr/test/unittest/app_running_processes_info_test/app_running_processes_info_test.cpp diff --git a/services/appmgr/test/BUILD.gn b/services/appmgr/test/BUILD.gn index 4f1eee18f96..b8fdede6cfe 100644 --- a/services/appmgr/test/BUILD.gn +++ b/services/appmgr/test/BUILD.gn @@ -81,6 +81,7 @@ ohos_source_set("appmgr_test_source") { cflags += [ "-DBINDER_IPC_32BIT" ] } deps = [ + "${services_path}/common:perm_verification", "//base/global/i18n_standard/frameworks/intl:intl_util", "//base/security/permission/interfaces/innerkits/permission_standard/permissionsdk:libpermissionsdk_standard", "//foundation/aafwk/standard/interfaces/innerkits/app_manager:app_manager", @@ -112,6 +113,9 @@ group("unittest") { "unittest/ams_app_state_callback_test:unittest", "unittest/ams_app_workflow_test:unittest", "unittest/ams_ipc_interface_test:unittest", + + # "unittest/ams_lmks_client_test:unittest", + "unittest/ams_mgr_kill_process_test:unittest", "unittest/ams_mgr_scheduler_test:unittest", "unittest/ams_recent_app_list_test:unittest", "unittest/ams_service_app_spawn_client_test:unittest", @@ -121,5 +125,6 @@ group("unittest") { "unittest/ams_service_load_ability_process_test:unittest", "unittest/ams_service_startup_test:unittest", "unittest/app_mgr_service_event_handler_test:unittest", + "unittest/app_running_processes_info_test:unittest", ] } diff --git a/services/appmgr/test/mock/src/mock_bundle_manager.cpp b/services/appmgr/test/mock/src/mock_bundle_manager.cpp index ca3ec28c311..fe0d3f33cf5 100644 --- a/services/appmgr/test/mock/src/mock_bundle_manager.cpp +++ b/services/appmgr/test/mock/src/mock_bundle_manager.cpp @@ -140,6 +140,30 @@ bool BundleMgrService::GetBundleInfo( bundleInfo.jointUserId = ""; bundleInfo.appId = bundleName + "_xxx"; } + if (bundleName == "KeepAliveApplication") { + HapModuleInfo hapModuleInfo; + hapModuleInfo.moduleName = "KeepAliveApplication"; + ApplicationInfo appInfo; + appInfo.name = "KeepAliveApp"; + appInfo.bundleName = bundleName; + appInfo.uid = 2100; + bundleInfo.uid = 2100; + bundleInfo.name = bundleName; + bundleInfo.applicationInfo = appInfo; + bundleInfo.hapModuleInfos.push_back(hapModuleInfo); + } + if (bundleName == "KeepAliveApplication1") { + HapModuleInfo hapModuleInfo; + hapModuleInfo.moduleName = "KeepAliveApplication1"; + ApplicationInfo appInfo; + appInfo.name = "KeepAliveApp1"; + appInfo.bundleName = bundleName; + appInfo.uid = 2101; + bundleInfo.uid = 2101; + bundleInfo.name = bundleName; + bundleInfo.applicationInfo = appInfo; + bundleInfo.hapModuleInfos.push_back(hapModuleInfo); + } return true; } bool BundleMgrService::GetBundleGids(const std::string &bundleName, std::vector &gids) @@ -156,6 +180,7 @@ bool BundleMgrService::GetBundleGids(const std::string &bundleName, std::vector< bool BundleMgrService::GetBundleInfos( const BundleFlag flag, std::vector &bundleInfos, int32_t userId) { + GTEST_LOG_(INFO) << "BundleMgrService::GetBundleInfos"; bundleInfos = bundleInfos_; return true; } @@ -242,12 +267,18 @@ void BundleMgrService::MakingPackageData() void BundleMgrService::MakingResidentProcData() { int appUid = 2100; + int appUid1 = 2101; GTEST_LOG_(INFO) << "MakingResidentProcData()"; BundleInfo bundleInfo; bundleInfo.uid = appUid; bundleInfo.name = "KeepAliveApplication"; + BundleInfo bundleInfo1; + bundleInfo1.uid = appUid1; + bundleInfo1.name = "KeepAliveApplication1"; + bundleInfos_.emplace_back(bundleInfo); + bundleInfos_.emplace_back(bundleInfo1); } } // namespace AppExecFwk } // namespace OHOS diff --git a/services/appmgr/test/unittest/ams_ability_running_record_test/BUILD.gn b/services/appmgr/test/unittest/ams_ability_running_record_test/BUILD.gn index aa631564e0a..5a98e62e904 100644 --- a/services/appmgr/test/unittest/ams_ability_running_record_test/BUILD.gn +++ b/services/appmgr/test/unittest/ams_ability_running_record_test/BUILD.gn @@ -53,6 +53,7 @@ ohos_unittest("AmsAbilityRunningRecordTest") { cflags += [ "-DBINDER_IPC_32BIT" ] } deps = [ + "${aafwk_path}/interfaces/innerkits/ability_manager:ability_manager", "${appexecfwk_path}/libs/libeventhandler:libeventhandler_target", "${innerkits_path}/uri_permission:uri_permission_mgr", "${services_path}/appmgr/test:appmgr_test_source", diff --git a/services/appmgr/test/unittest/ams_app_life_cycle_test/ams_app_life_cycle_test.cpp b/services/appmgr/test/unittest/ams_app_life_cycle_test/ams_app_life_cycle_test.cpp index 60033cec962..f82c0cf621e 100644 --- a/services/appmgr/test/unittest/ams_app_life_cycle_test/ams_app_life_cycle_test.cpp +++ b/services/appmgr/test/unittest/ams_app_life_cycle_test/ams_app_life_cycle_test.cpp @@ -1831,6 +1831,60 @@ HWTEST_F(AmsAppLifeCycleTest, ClearUpApplicationData_001, TestSize.Level1) serviceInner_->ClearUpApplicationData(appRecord->GetBundleName(), appRecord->GetUid(), NEW_PID); } +/* + * Feature: AMS + * Function: AppLifeCycle + * SubFunction: ClearUpApplicationData + * FunctionPoints: ClearUpApplicationData + * CaseDescription: clear the application data. + */ +HWTEST_F(AmsAppLifeCycleTest, ClearUpApplicationData_002, TestSize.Level1) +{ + const pid_t NEW_PID = 1234; + auto abilityInfo = GetAbilityInfoByIndex("110"); + auto appInfo = GetApplication(); + sptr token = GetMockToken(); + + auto appRecord = StartProcessAndLoadAbility(token, nullptr, abilityInfo, appInfo, NEW_PID); + appRecord->SetUid(101); + + EXPECT_CALL(*mockBundleMgr, CleanBundleDataFiles(_, _)).Times(1).WillOnce(Return(101)); + EXPECT_CALL(*mockBundleMgr, GetUidByBundleName(_, _)).Times(1); + + sptr mockAppScheduler = new MockAppScheduler(); + sptr client = iface_cast(mockAppScheduler.GetRefPtr()); + appRecord->SetApplicationClient(client); + + serviceInner_->ClearUpApplicationData(appRecord->GetBundleName(), appRecord->GetUid(), NEW_PID); +} + +/* + * Feature: AMS + * Function: AppLifeCycle + * SubFunction: ClearUpApplicationData + * FunctionPoints: ClearUpApplicationData + * CaseDescription: clear the application data. + */ +HWTEST_F(AmsAppLifeCycleTest, ClearUpApplicationData_003, TestSize.Level1) +{ + const pid_t NEW_PID = 1234; + auto abilityInfo = GetAbilityInfoByIndex("110"); + auto appInfo = GetApplication(); + sptr token = GetMockToken(); + + auto appRecord = StartProcessAndLoadAbility(token, nullptr, abilityInfo, appInfo, NEW_PID); + appRecord->SetUid(101); + + EXPECT_CALL(*mockBundleMgr, CleanBundleDataFiles(_, _)).Times(1).WillOnce(Return(0)); + EXPECT_CALL(*mockBundleMgr, GetUidByBundleName(_, _)).Times(0); + + sptr mockAppScheduler = new MockAppScheduler(); + sptr client = iface_cast(mockAppScheduler.GetRefPtr()); + appRecord->SetApplicationClient(client); + + serviceInner_->ClearUpApplicationData(appRecord->GetBundleName(), appRecord->GetUid(), NEW_PID); +} + /* * Feature: AMS * Function: AppLifeCycle::CreateAppRunningRecord @@ -1902,8 +1956,8 @@ HWTEST_F(AmsAppLifeCycleTest, CheckAppRunningRecordIsExist_001, TestSize.Level1) * Feature: AMS * Function: AppLifeCycle::StartResidentProcess * SubFunction: NA - * FunctionPoints: NA - * CaseDescription: NA + * FunctionPoints: start resident process + * CaseDescription: try to start a resident process */ HWTEST_F(AmsAppLifeCycleTest, StartResidentProcess_001, TestSize.Level1) { @@ -1952,8 +2006,8 @@ HWTEST_F(AmsAppLifeCycleTest, StartResidentProcess_001, TestSize.Level1) * Feature: AMS * Function: AppLifeCycle::StartResidentProcess * SubFunction: NA - * FunctionPoints: NA - * CaseDescription: NA + * FunctionPoints: start resident process + * CaseDescription: try to start a resident process */ HWTEST_F(AmsAppLifeCycleTest, StartResidentProcess_002, TestSize.Level1) { @@ -2004,12 +2058,29 @@ HWTEST_F(AmsAppLifeCycleTest, StartResidentProcess_002, TestSize.Level1) EXPECT_TRUE(recordId == id); } +/* + * Feature: AMS + * Function: AppLifeCycle::StartResidentProcess + * SubFunction: NA + * FunctionPoints: start resident process + * CaseDescription: try to start resident process + */ +HWTEST_F(AmsAppLifeCycleTest, StartResidentProcess_003, TestSize.Level1) +{ + std::vector infos; + serviceInner_->appRunningManager_->ClearAppRunningRecordMap(); + + // EMPTY VECTOR + serviceInner_->StartResidentProcess(infos, -1); + + EXPECT_TRUE(serviceInner_->appRunningManager_->GetAppRunningRecordMap().empty()); +} /* * Feature: AMS * Function: AppLifeCycle::RestartResidentProcess * SubFunction: NA - * FunctionPoints: NA - * CaseDescription: NA + * FunctionPoints: Guaranteed to re-pull + * CaseDescription: Restart the abnormal resident process */ HWTEST_F(AmsAppLifeCycleTest, RestartResidentProcess_001, TestSize.Level1) { @@ -2046,5 +2117,152 @@ HWTEST_F(AmsAppLifeCycleTest, RestartResidentProcess_001, TestSize.Level1) appRecord = serviceInner_->appRunningManager_->CheckAppRunningRecordIsExist(appName, proc, uid, bundleInfo); EXPECT_FALSE(appRecord); } + +/* + * Feature: AMS + * Function: AppLifeCycle::RestartResidentProcess + * SubFunction: NA + * FunctionPoints: Guaranteed to re-pull + * CaseDescription: Restart the abnormal resident process + */ +HWTEST_F(AmsAppLifeCycleTest, RestartResidentProcess_002, TestSize.Level1) +{ + pid_t pid = 123; + sptr token = GetMockToken(); + std::string appName = "KeepAliveApp"; + std::string proc = "KeepAliveApplication"; + int uid = 2100; + + HapModuleInfo hapModuleInfo; + hapModuleInfo.moduleName = "KeepAliveApplication"; + std::vector infos; + BundleInfo info; + info.name = proc; + info.uid = uid; + info.hapModuleInfos.push_back(hapModuleInfo); + + ApplicationInfo appInfo; + appInfo.name = "KeepAliveApp"; + appInfo.bundleName = "KeepAliveApplication"; + appInfo.uid = 2100; + info.applicationInfo = appInfo; + infos.push_back(info); + BundleInfo bundleInfo; + bundleInfo.appId = "com.ohos.test.helloworld_code123"; + + auto appRecord = serviceInner_->appRunningManager_->CheckAppRunningRecordIsExist(appName, proc, uid, bundleInfo); + EXPECT_FALSE(appRecord); + + MockAppSpawnClient *mockClientPtrT = new (std::nothrow) MockAppSpawnClient(); + EXPECT_TRUE(mockClientPtrT); + EXPECT_CALL(*mockClientPtrT, StartProcess(_, _)).Times(1).WillOnce(DoAll(SetArgReferee<1>(pid), Return(ERR_OK))); + + serviceInner_->SetAppSpawnClient(std::unique_ptr(mockClientPtrT)); + + serviceInner_->StartResidentProcess(infos, -1); + + appRecord = serviceInner_->appRunningManager_->CheckAppRunningRecordIsExist(appName, proc, uid, bundleInfo); + EXPECT_TRUE(appRecord); + + // Restart the existing application when there is no abnormality + serviceInner_->RestartResidentProcess(appRecord); + + auto newAppRecord = serviceInner_->appRunningManager_->CheckAppRunningRecordIsExist(appName, proc, uid, bundleInfo); + EXPECT_TRUE(newAppRecord); + EXPECT_TRUE(appRecord == newAppRecord); + EXPECT_TRUE(appRecord->GetRecordId() == newAppRecord->GetRecordId()); +} + +/* + * Feature: AMS + * Function: AppLifeCycle::UpdateConfiguration + * SubFunction: NA + * FunctionPoints: Environmental Change Notification + * CaseDescription: Determine the default value + */ +HWTEST_F(AmsAppLifeCycleTest, UpdateConfiguration_001, TestSize.Level1) +{ + serviceInner_->Init(); + auto configMgr = serviceInner_->GetConfiguration(); + EXPECT_TRUE(configMgr); + auto language = configMgr->GetItem(GlobalConfigurationKey::SYSTEM_LANGUAGE); + // has default value + EXPECT_TRUE(!language.empty()); +} + +/* + * Feature: AMS + * Function: AppLifeCycle::UpdateConfiguration + * SubFunction: NA + * FunctionPoints: Environmental Change Notification + * CaseDescription: Trigger an environment change notification + */ +HWTEST_F(AmsAppLifeCycleTest, UpdateConfiguration_002, TestSize.Level1) +{ + auto testLanguge = "ch-zh"; + auto configUpdate = [testLanguge](const Configuration &config) { + auto l = config.GetItem(GlobalConfigurationKey::SYSTEM_LANGUAGE); + EXPECT_TRUE(testLanguge == l); + }; + + auto testAppPreRecord = + CreateTestApplicationRecord(AbilityState::ABILITY_STATE_FOREGROUND, ApplicationState::APP_STATE_FOREGROUND); + + EXPECT_CALL(*(testAppPreRecord.mockAppScheduler_), ScheduleConfigurationUpdated(_)) + .Times(1) + .WillOnce(testing::Invoke(configUpdate)); + + Configuration config; + config.AddItem(GlobalConfigurationKey::SYSTEM_LANGUAGE, testLanguge); + serviceInner_->UpdateConfiguration(config); +} + +/* + * Feature: AMS + * Function: AppLifeCycle::UpdateConfiguration + * SubFunction: NA + * FunctionPoints: Environmental Change Notification + * CaseDescription: Trigger an environment change notification + */ +HWTEST_F(AmsAppLifeCycleTest, UpdateConfiguration_003, TestSize.Level1) +{ + auto testLanguge = "ch-zh"; + auto configUpdate = [testLanguge](const Configuration &config) { + auto l = config.GetItem(GlobalConfigurationKey::SYSTEM_LANGUAGE); + EXPECT_TRUE(testLanguge == l); + }; + + auto testAppPreRecord = + CreateTestApplicationRecord(AbilityState::ABILITY_STATE_FOREGROUND, ApplicationState::APP_STATE_FOREGROUND); + + EXPECT_CALL(*(testAppPreRecord.mockAppScheduler_), ScheduleConfigurationUpdated(_)) + .Times(1) + .WillOnce(testing::Invoke(configUpdate)); + + Configuration config; + config.AddItem(GlobalConfigurationKey::SYSTEM_LANGUAGE, testLanguge); + serviceInner_->UpdateConfiguration(config); + serviceInner_->UpdateConfiguration(config); +} + +/* + * Feature: AMS + * Function: AppLifeCycle::GetGlobalConfiguration + * SubFunction: initialization + * FunctionPoints: NA + * CaseDescription: Initialize a persistent environment variable object + */ +HWTEST_F(AmsAppLifeCycleTest, GetGlobalConfiguration_001, TestSize.Level1) +{ + auto configMgr = serviceInner_->GetConfiguration(); + EXPECT_TRUE(configMgr); + + auto language = configMgr->GetItem(GlobalConfigurationKey::SYSTEM_LANGUAGE); + EXPECT_TRUE(language.empty()); + + serviceInner_->GetGlobalConfiguration(); + language = configMgr->GetItem(GlobalConfigurationKey::SYSTEM_LANGUAGE); + EXPECT_TRUE(!language.empty()); +} } // namespace AppExecFwk } // namespace OHOS \ No newline at end of file diff --git a/services/appmgr/test/unittest/ams_app_mgr_client_test/ams_app_mgr_client_test.cpp b/services/appmgr/test/unittest/ams_app_mgr_client_test/ams_app_mgr_client_test.cpp index 6b5595f19fd..3b4ed4c33ba 100644 --- a/services/appmgr/test/unittest/ams_app_mgr_client_test/ams_app_mgr_client_test.cpp +++ b/services/appmgr/test/unittest/ams_app_mgr_client_test/ams_app_mgr_client_test.cpp @@ -366,5 +366,68 @@ HWTEST_F(AmsAppMgrClientTest, AppMgrClient_013, TestSize.Level1) EXPECT_EQ(AppMgrResultCode::ERROR_SERVICE_NOT_CONNECTED, client_->KillProcessByAbilityToken(token)); APP_LOGI("ams_app_mgr_client_test_013 end"); } + +/* + * Feature: AppMgrService + * Function: AppMgrClient::ClearUpApplicationData + * SubFunction: ClearUpApplicationData + * FunctionPoints: AppMgrClient ClearUpApplicationData interface + * EnvConditions: Mobile that can run ohos test framework + * CaseDescription: clear the application data. + */ +HWTEST_F(AmsAppMgrClientTest, AppMgrClient_014, TestSize.Level1) +{ + sptr token; + EXPECT_EQ(AppMgrResultCode::RESULT_OK, client_->ConnectAppMgrService()); + EXPECT_CALL(*(static_cast(client_->remote_.GetRefPtr())), ClearUpApplicationData(_)) + .Times(1) + .WillOnce(Return(ERR_NO_MEMORY)); + EXPECT_EQ(AppMgrResultCode::ERROR_SERVICE_NOT_READY, client_->ClearUpApplicationData("com.test")); +} + +/* + * Feature: AppMgrService + * Function: AppMgrClient::ClearUpApplicationData + * SubFunction: ClearUpApplicationData + * FunctionPoints: AppMgrClient ClearUpApplicationData interface + * EnvConditions: Mobile that can run ohos test framework + * CaseDescription: clear the application data. + */ +HWTEST_F(AmsAppMgrClientTest, AppMgrClient_015, TestSize.Level1) +{ + sptr token; + EXPECT_EQ(AppMgrResultCode::RESULT_OK, client_->ConnectAppMgrService()); + sptr appMgr(new MockAppMgrService()); + EXPECT_CALL(*(static_cast(client_->remote_.GetRefPtr())), ClearUpApplicationData(_)) + .Times(1) + .WillOnce(Return(ERR_OK)); + EXPECT_EQ(AppMgrResultCode::RESULT_OK, client_->ClearUpApplicationData("com.test")); +} + +/* + * Feature: AppMgrService + * Function: AppMgrClient::UpdateConfiguration + * SubFunction: RegisterAppStateCallback Function + * FunctionPoints: AppMgrClient UpdateConfiguration interface + * EnvConditions: Mobile that can run ohos test framework + * CaseDescription: Notify app of configuration change + */ +HWTEST_F(AmsAppMgrClientTest, AppMgrClient_016, TestSize.Level1) +{ + APP_LOGI("UpdateConfiguration start"); + EXPECT_EQ(AppMgrResultCode::RESULT_OK, client_->ConnectAppMgrService()); + sptr amsMgrScheduler(new MockAmsMgrScheduler()); + + EXPECT_CALL(*(static_cast(amsMgrScheduler.GetRefPtr())), UpdateConfiguration(_)) + .Times(1); + + EXPECT_CALL(*(static_cast(client_->remote_.GetRefPtr())), GetAmsMgr()) + .Times(1) + .WillOnce(Return(amsMgrScheduler)); + + Configuration config; + EXPECT_EQ(AppMgrResultCode::RESULT_OK, client_->UpdateConfiguration(config)); + APP_LOGI("UpdateConfiguration end"); +} } // namespace AppExecFwk } // namespace OHOS \ No newline at end of file diff --git a/services/appmgr/test/unittest/ams_app_running_record_test/BUILD.gn b/services/appmgr/test/unittest/ams_app_running_record_test/BUILD.gn index 481fb3f8b6a..fd1f8d28ded 100644 --- a/services/appmgr/test/unittest/ams_app_running_record_test/BUILD.gn +++ b/services/appmgr/test/unittest/ams_app_running_record_test/BUILD.gn @@ -50,6 +50,7 @@ ohos_unittest("AmsAppRunningRecordTest") { cflags += [ "-DBINDER_IPC_32BIT" ] } deps = [ + "${aafwk_path}/interfaces/innerkits/ability_manager:ability_manager", "${aafwk_path}/interfaces/innerkits/app_manager:app_manager", "${appexecfwk_path}/libs/libeventhandler:libeventhandler_target", "${innerkits_path}/uri_permission:uri_permission_mgr", diff --git a/services/appmgr/test/unittest/ams_app_running_record_test/ams_app_running_record_test.cpp b/services/appmgr/test/unittest/ams_app_running_record_test/ams_app_running_record_test.cpp index 5cd8f60743f..ab16f6c38a1 100644 --- a/services/appmgr/test/unittest/ams_app_running_record_test/ams_app_running_record_test.cpp +++ b/services/appmgr/test/unittest/ams_app_running_record_test/ams_app_running_record_test.cpp @@ -208,12 +208,12 @@ HWTEST_F(AmsAppRunningRecordTest, CreateAppRunningRecord_002, TestSize.Level1) EXPECT_TRUE(service_ != nullptr); // Create sptr token = GetMockToken(); - auto record = service_->CreateAppRunningRecord(GetMockToken(), nullptr, appInfo, abilityInfo, - GetTestProcessName(), bundleInfo, hapModuleInfo, nullptr); + auto record = service_->CreateAppRunningRecord( + GetMockToken(), nullptr, appInfo, abilityInfo, GetTestProcessName(), bundleInfo, hapModuleInfo, nullptr); record->SetUid(1010); // Get - auto record1 = service_->CreateAppRunningRecord(GetMockToken(), nullptr, appInfo, abilityInfo, - GetTestProcessName(), bundleInfo, hapModuleInfo, nullptr); + auto record1 = service_->CreateAppRunningRecord( + GetMockToken(), nullptr, appInfo, abilityInfo, GetTestProcessName(), bundleInfo, hapModuleInfo, nullptr); EXPECT_TRUE(record1 != nullptr); EXPECT_EQ(record1->GetName(), GetTestAppName()); EXPECT_EQ(record1->GetProcessName(), GetTestProcessName()); @@ -242,16 +242,22 @@ HWTEST_F(AmsAppRunningRecordTest, CreateAppRunningRecord_003, TestSize.Level1) HapModuleInfo hapModuleInfo; hapModuleInfo.moduleName = "module789"; EXPECT_TRUE(service_ != nullptr); - auto record = service_->CreateAppRunningRecord(GetMockToken(), nullptr, appInfo, abilityInfo, - GetTestProcessName(), bundleInfo, hapModuleInfo, nullptr); + auto record = service_->CreateAppRunningRecord( + GetMockToken(), nullptr, appInfo, abilityInfo, GetTestProcessName(), bundleInfo, hapModuleInfo, nullptr); record->SetUid(1010); auto anotherAbilityInfo = std::make_shared(); anotherAbilityInfo->name = "Another_ability"; anotherAbilityInfo->applicationInfo.uid = 1010; sptr anotherToken = new (std::nothrow) MockAbilityToken(); - auto record1 = service_->CreateAppRunningRecord(GetMockToken(), anotherToken, appInfo, anotherAbilityInfo, - GetTestProcessName(), bundleInfo, hapModuleInfo, nullptr); + auto record1 = service_->CreateAppRunningRecord(GetMockToken(), + anotherToken, + appInfo, + anotherAbilityInfo, + GetTestProcessName(), + bundleInfo, + hapModuleInfo, + nullptr); EXPECT_EQ(record1->GetName(), GetTestAppName()); EXPECT_EQ(record1->GetProcessName(), GetTestProcessName()); @@ -280,8 +286,8 @@ HWTEST_F(AmsAppRunningRecordTest, CreateAppRunningRecord_004, TestSize.Level1) hapModuleInfo.moduleName = "module789"; EXPECT_TRUE(service_ != nullptr); // Create - auto record = service_->CreateAppRunningRecord(GetMockToken(), nullptr, nullptr, abilityInfo, - GetTestProcessName(), bundleInfo, hapModuleInfo, nullptr); + auto record = service_->CreateAppRunningRecord( + GetMockToken(), nullptr, nullptr, abilityInfo, GetTestProcessName(), bundleInfo, hapModuleInfo, nullptr); EXPECT_TRUE(record == nullptr); } @@ -801,8 +807,8 @@ HWTEST_F(AmsAppRunningRecordTest, LaunchAbilityForApp_002, TestSize.Level1) const int EXPECT_ABILITY_LAUNCH_TIME = 3; EXPECT_TRUE(service_ != nullptr); - std::shared_ptr record = service_->CreateAppRunningRecord(GetMockToken(), nullptr, appInfo, - abilityInfo, GetTestProcessName(), bundleInfo, hapModuleInfo, nullptr); + std::shared_ptr record = service_->CreateAppRunningRecord( + GetMockToken(), nullptr, appInfo, abilityInfo, GetTestProcessName(), bundleInfo, hapModuleInfo, nullptr); auto abilityRecord = record->GetAbilityRunningRecord(GetTestAbilityName()); EXPECT_TRUE(abilityRecord != nullptr); @@ -851,8 +857,8 @@ HWTEST_F(AmsAppRunningRecordTest, LaunchAbilityForApp_003, TestSize.Level1) hapModuleInfo.moduleName = "module789"; EXPECT_TRUE(service_ != nullptr); - std::shared_ptr record = service_->CreateAppRunningRecord(GetMockToken(), nullptr, appInfo, - abilityInfo, GetTestProcessName(), bundleInfo, hapModuleInfo, nullptr); + std::shared_ptr record = service_->CreateAppRunningRecord( + GetMockToken(), nullptr, appInfo, abilityInfo, GetTestProcessName(), bundleInfo, hapModuleInfo, nullptr); record->SetState(ApplicationState::APP_STATE_READY); record->SetApplicationClient(GetMockedAppSchedulerClient()); auto abilityRecord = record->GetAbilityRunningRecord(GetTestAbilityName()); @@ -897,8 +903,8 @@ HWTEST_F(AmsAppRunningRecordTest, LaunchAbilityForApp_004, TestSize.Level1) hapModuleInfo.moduleName = "module789"; EXPECT_TRUE(service_ != nullptr); - std::shared_ptr record = service_->CreateAppRunningRecord(GetMockToken(), nullptr, appInfo, - abilityInfo, GetTestProcessName(), bundleInfo, hapModuleInfo, nullptr); + std::shared_ptr record = service_->CreateAppRunningRecord( + GetMockToken(), nullptr, appInfo, abilityInfo, GetTestProcessName(), bundleInfo, hapModuleInfo, nullptr); auto abilityRecord = record->GetAbilityRunningRecord(GetTestAbilityName()); EXPECT_TRUE(abilityRecord != nullptr); @@ -945,8 +951,8 @@ HWTEST_F(AmsAppRunningRecordTest, LaunchAbilityForApp_005, TestSize.Level1) const int EXPECT_ABILITY_LAUNCH_TIME = 2; EXPECT_TRUE(service_ != nullptr); - std::shared_ptr record = service_->CreateAppRunningRecord(GetMockToken(), nullptr, appInfo, - abilityInfo, GetTestProcessName(), bundleInfo, hapModuleInfo, nullptr); + std::shared_ptr record = service_->CreateAppRunningRecord( + GetMockToken(), nullptr, appInfo, abilityInfo, GetTestProcessName(), bundleInfo, hapModuleInfo, nullptr); auto abilityRecord = record->GetAbilityRunningRecord(GetTestAbilityName()); EXPECT_TRUE(abilityRecord != nullptr); @@ -1014,8 +1020,8 @@ HWTEST_F(AmsAppRunningRecordTest, TerminateAbility_002, TestSize.Level1) appInfo->name = GetTestAppName(); appInfo->bundleName = GetTestAppName(); - std::shared_ptr record = service_->CreateAppRunningRecord(GetMockToken(), nullptr, appInfo, - abilityInfo, GetTestProcessName(), bundleInfo, hapModuleInfo, nullptr); + std::shared_ptr record = service_->CreateAppRunningRecord( + GetMockToken(), nullptr, appInfo, abilityInfo, GetTestProcessName(), bundleInfo, hapModuleInfo, nullptr); auto abilityRecord = record->GetAbilityRunningRecord(GetTestAbilityName()); EXPECT_TRUE(abilityRecord != nullptr); @@ -1067,8 +1073,8 @@ HWTEST_F(AmsAppRunningRecordTest, GetAbilityRunningRecord_001, TestSize.Level1) appInfo->name = GetTestAppName(); appInfo->bundleName = GetTestAppName(); - std::shared_ptr record = service_->CreateAppRunningRecord(GetMockToken(), nullptr, appInfo, - abilityInfo, GetTestProcessName(), bundleInfo, hapModuleInfo, nullptr); + std::shared_ptr record = service_->CreateAppRunningRecord( + GetMockToken(), nullptr, appInfo, abilityInfo, GetTestProcessName(), bundleInfo, hapModuleInfo, nullptr); auto abilityRecord = record->GetAbilityRunningRecord(GetTestAbilityName()); EXPECT_TRUE(abilityRecord != nullptr); @@ -1118,8 +1124,8 @@ HWTEST_F(AmsAppRunningRecordTest, SetUid_GetUid_001, TestSize.Level1) HapModuleInfo hapModuleInfo; hapModuleInfo.moduleName = "module789"; EXPECT_TRUE(service_ != nullptr); - auto record = service_->CreateAppRunningRecord(GetMockToken(), nullptr, appInfo, abilityInfo, - GetTestProcessName(), bundleInfo, hapModuleInfo, nullptr); + auto record = service_->CreateAppRunningRecord( + GetMockToken(), nullptr, appInfo, abilityInfo, GetTestProcessName(), bundleInfo, hapModuleInfo, nullptr); EXPECT_TRUE(record != nullptr); record->SetUid(102); @@ -1153,8 +1159,8 @@ HWTEST_F(AmsAppRunningRecordTest, OnAbilityStateChanged_001, TestSize.Level1) HapModuleInfo hapModuleInfo; hapModuleInfo.moduleName = "module789"; - std::shared_ptr record = service_->CreateAppRunningRecord(GetMockToken(), nullptr, appInfo, - abilityInfo, GetTestProcessName(), bundleInfo, hapModuleInfo, nullptr); + std::shared_ptr record = service_->CreateAppRunningRecord( + GetMockToken(), nullptr, appInfo, abilityInfo, GetTestProcessName(), bundleInfo, hapModuleInfo, nullptr); auto moduleRecord = record->GetModuleRecordByModuleName(appInfo->bundleName, hapModuleInfo.moduleName); EXPECT_TRUE(moduleRecord != nullptr); auto abilityRecord = record->GetAbilityRunningRecord(GetTestAbilityName()); @@ -1383,7 +1389,6 @@ HWTEST_F(AmsAppRunningRecordTest, GetAbilities_002, TestSize.Level1) * EnvConditions: Mobile that can run ohos test framework * CaseDescription: Remove ModuleRecord */ - HWTEST_F(AmsAppRunningRecordTest, RemoveModuleRecord_001, TestSize.Level1) { auto abilityInfo = std::make_shared(); @@ -1417,5 +1422,255 @@ HWTEST_F(AmsAppRunningRecordTest, RemoveModuleRecord_001, TestSize.Level1) auto moduleRecordList = record->GetAllModuleRecord(); EXPECT_TRUE(moduleRecordList.size() == 1); } + +/* + * Feature: AbilityManagerService + * Function: UpdateConfiguration + * SubFunction: NA + * FunctionPoints: Environmental Change Notification + * EnvConditions: NA + * CaseDescription: Make an environment object and update + */ +HWTEST_F(AmsAppRunningRecordTest, UpdateConfiguration_001, TestSize.Level1) +{ + auto testLanguge = std::string("ch-zh"); + auto configUpdate = [testLanguge](const Configuration &config) { + auto l = config.GetItem(GlobalConfigurationKey::SYSTEM_LANGUAGE); + EXPECT_TRUE(testLanguge == l); + }; + + Configuration config; + config.AddItem(GlobalConfigurationKey::SYSTEM_LANGUAGE, testLanguge); + auto record = GetTestAppRunningRecord(); + EXPECT_CALL(*mockAppSchedulerClient_, ScheduleConfigurationUpdated(_)) + .Times(1) + .WillOnce(testing::Invoke(configUpdate)); + + record->UpdateConfiguration(config); +} + +/* + * Feature: AbilityManagerService + * Function: UpdateConfiguration + * SubFunction: NA + * FunctionPoints: Environmental Change Notification + * EnvConditions: NA + * CaseDescription: Make an environment object and update + */ +HWTEST_F(AmsAppRunningRecordTest, UpdateConfiguration_002, TestSize.Level1) +{ + auto test = std::string("colour"); + auto configUpdate = [test](const Configuration &config) { + auto l = config.GetItem(GlobalConfigurationKey::SYSTEM_COLORMODE); + EXPECT_TRUE(test == l); + }; + + Configuration config; + config.AddItem(GlobalConfigurationKey::SYSTEM_COLORMODE, test); + auto record = GetTestAppRunningRecord(); + EXPECT_CALL(*mockAppSchedulerClient_, ScheduleConfigurationUpdated(_)) + .Times(1) + .WillOnce(testing::Invoke(configUpdate)); + + record->UpdateConfiguration(config); +} + +/* + * Feature: AMS + * Function: SetSpecifiedAbilityFlagAndWant + * SubFunction: SetSpecifiedAbilityFlagAndWant + * FunctionPoints: check params + * EnvConditions: Mobile that can run ohos test framework + * CaseDescription: Remove ModuleRecord + */ +HWTEST_F(AmsAppRunningRecordTest, SetSpecifiedAbilityFlagAndWant_001, TestSize.Level1) +{ + auto appInfo = std::make_shared(); + appInfo->name = GetTestAppName(); + appInfo->bundleName = GetTestAppName(); + int32_t recordId = 11; + std::string processName = "processName"; + auto record = std::make_shared(appInfo, recordId, processName); + + bool flag = true; + AAFwk::Want want; + std::string moduleName = "module123"; + record->SetSpecifiedAbilityFlagAndWant(flag, want, moduleName); + EXPECT_TRUE(record->isSpecifiedAbility_ == flag); + EXPECT_TRUE(record->moduleName_ == moduleName); +} + +/* + * Feature: AMS + * Function: IsStartSpecifiedAbility + * SubFunction: IsStartSpecifiedAbility + * FunctionPoints: check params + * EnvConditions: Mobile that can run ohos test framework + * CaseDescription: Remove ModuleRecord + */ +HWTEST_F(AmsAppRunningRecordTest, IsStartSpecifiedAbility_001, TestSize.Level1) +{ + auto appInfo = std::make_shared(); + appInfo->name = GetTestAppName(); + appInfo->bundleName = GetTestAppName(); + int32_t recordId = 11; + std::string processName = "processName"; + auto record = std::make_shared(appInfo, recordId, processName); + + bool flag = true; + AAFwk::Want want; + std::string moduleName = "module123"; + record->SetSpecifiedAbilityFlagAndWant(flag, want, moduleName); + EXPECT_TRUE(record->IsStartSpecifiedAbility() == flag); +} + +/* + * Feature: AMS + * Function: GetSpecifiedWant + * SubFunction: GetSpecifiedWant + * FunctionPoints: check params + * EnvConditions: Mobile that can run ohos test framework + * CaseDescription: Remove ModuleRecord + */ +HWTEST_F(AmsAppRunningRecordTest, GetSpecifiedWant_001, TestSize.Level1) +{ + auto appInfo = std::make_shared(); + appInfo->name = GetTestAppName(); + appInfo->bundleName = GetTestAppName(); + int32_t recordId = 11; + std::string processName = "processName"; + auto record = std::make_shared(appInfo, recordId, processName); + + bool flag = true; + Want want; + want.SetElementName("DemoDeviceId", "DemoBundleName", "DemoAbilityName"); + std::string moduleName = "module123"; + record->SetSpecifiedAbilityFlagAndWant(flag, want, moduleName); + EXPECT_TRUE(record->GetSpecifiedWant().GetBundle() == want.GetBundle()); +} + +/* + * Feature: AMS + * Function: RegisterStartSpecifiedAbilityResponse + * SubFunction: RegisterStartSpecifiedAbilityResponse + * FunctionPoints: check params + * EnvConditions: Mobile that can run ohos test framework + * CaseDescription: Remove ModuleRecord + */ +HWTEST_F(AmsAppRunningRecordTest, RegisterStartSpecifiedAbilityResponse_001, TestSize.Level1) +{ + sptr response; + service_->RegisterStartSpecifiedAbilityResponse(response); + EXPECT_TRUE(service_->startSpecifiedAbilityResponse_ == response); +} + +/* + * Feature: AMS + * Function: StartSpecifiedAbility + * SubFunction: StartSpecifiedAbility + * FunctionPoints: check params + * EnvConditions: Mobile that can run ohos test framework + * CaseDescription: Remove ModuleRecord + */ +HWTEST_F(AmsAppRunningRecordTest, StartSpecifiedAbility_001, TestSize.Level1) +{ + auto abilityInfo = std::make_shared(); + abilityInfo->name = GetTestAbilityName(); + auto appInfo = std::make_shared(); + appInfo->name = GetTestAppName(); + BundleInfo bundleInfo; + bundleInfo.appId = "com.ohos.test.helloworld_code123"; + bundleInfo.jointUserId = "joint456"; + HapModuleInfo hapModuleInfo; + hapModuleInfo.moduleName = "module789"; + EXPECT_TRUE(service_ != nullptr); + auto record = service_->CreateAppRunningRecord( + GetMockToken(), nullptr, appInfo, abilityInfo, GetTestProcessName(), bundleInfo, hapModuleInfo, nullptr); + + EXPECT_TRUE(record != nullptr); + EXPECT_EQ(record->GetName(), GetTestAppName()); + EXPECT_EQ(record->GetProcessName(), GetTestProcessName()); + + Want want; + want.SetElementName("DemoDeviceIdB", "DemoBundleNameB", "DemoAbilityNameB"); + service_->StartSpecifiedAbility(want, *abilityInfo); + + auto modules = record->GetModules(); + EXPECT_EQ(modules.size(), 1); +} + +/* + * Feature: AMS + * Function: StartSpecifiedAbility + * SubFunction: StartSpecifiedAbility + * FunctionPoints: check params + * EnvConditions: Mobile that can run ohos test framework + * CaseDescription: Remove ModuleRecord + */ +HWTEST_F(AmsAppRunningRecordTest, StartSpecifiedAbility_002, TestSize.Level1) +{ + auto abilityInfo = std::make_shared(); + abilityInfo->name = GetTestAbilityName(); + auto appInfo = std::make_shared(); + appInfo->name = GetTestAppName(); + BundleInfo bundleInfo; + bundleInfo.appId = "com.ohos.test.helloworld_code123"; + bundleInfo.jointUserId = "joint456"; + HapModuleInfo hapModuleInfo; + hapModuleInfo.moduleName = "module789"; + EXPECT_TRUE(service_ != nullptr); + auto record = service_->CreateAppRunningRecord( + GetMockToken(), nullptr, appInfo, abilityInfo, GetTestProcessName(), bundleInfo, hapModuleInfo, nullptr); + + EXPECT_TRUE(record != nullptr); + EXPECT_EQ(record->GetName(), GetTestAppName()); + EXPECT_EQ(record->GetProcessName(), GetTestProcessName()); + + auto abilityInfo1 = std::make_shared(); + abilityInfo1->name = "test_ability_name_2"; + Want want; + want.SetElementName("DemoDeviceId", "DemoBundleName", "DemoAbilityName"); + service_->StartSpecifiedAbility(want, *abilityInfo1); + + auto modules = record->GetModules(); + EXPECT_EQ(modules.size(), 1); +} + +/* + * Feature: AMS + * Function: LaunchApplication + * SubFunction: LaunchApplication + * FunctionPoints: check params + * EnvConditions: Mobile that can run ohos test framework + * CaseDescription: Remove ModuleRecord + */ +HWTEST_F(AmsAppRunningRecordTest, Specified_LaunchApplication_001, TestSize.Level1) +{ + auto abilityInfo = std::make_shared(); + abilityInfo->name = GetTestAbilityName(); + abilityInfo->applicationName = GetTestAppName(); + abilityInfo->process = GetTestAppName(); + abilityInfo->applicationInfo.bundleName = GetTestAppName(); + + auto appInfo = std::make_shared(); + appInfo->bundleName = GetTestAppName(); + BundleInfo bundleInfo; + bundleInfo.appId = "com.ohos.test.helloworld_code123"; + bundleInfo.jointUserId = "joint456"; + HapModuleInfo hapModuleInfo; + hapModuleInfo.moduleName = "module789"; + + EXPECT_TRUE(service_ != nullptr); + std::shared_ptr record = service_->CreateAppRunningRecord( + GetMockToken(), nullptr, appInfo, abilityInfo, GetTestProcessName(), bundleInfo, hapModuleInfo, nullptr); + auto abilityRecord = record->GetAbilityRunningRecord(GetTestAbilityName()); + EXPECT_TRUE(abilityRecord != nullptr); + + record->SetApplicationClient(GetMockedAppSchedulerClient()); + record->isSpecifiedAbility_ = true; + service_->LaunchApplication(record); + auto ability = record->GetAbilityRunningRecord(GetTestAbilityName()); + EXPECT_TRUE(ability->GetState() != AbilityState::ABILITY_STATE_READY); +} } // namespace AppExecFwk } // namespace OHOS diff --git a/services/appmgr/test/unittest/ams_mgr_kill_process_test/BUILD.gn b/services/appmgr/test/unittest/ams_mgr_kill_process_test/BUILD.gn new file mode 100644 index 00000000000..fa89374231a --- /dev/null +++ b/services/appmgr/test/unittest/ams_mgr_kill_process_test/BUILD.gn @@ -0,0 +1,66 @@ +# Copyright (c) 2021 Huawei Device Co., Ltd. +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +import("//build/test.gni") +import("//foundation/aafwk/standard/aafwk.gni") + +module_output_path = "ability_runtime/appmgrservice" + +mock_sources = + [ "${aafwk_path}/services/appmgr/test/mock/src/mock_bundle_manager.cpp" ] + +ohos_unittest("AmsMgrKillProcessTest") { + module_out_path = module_output_path + + include_dirs = [ "${aafwk_path}/services/appmgr/test/mock/include" ] + + sources = [ "ams_mgr_kill_process_test.cpp" ] + sources += mock_sources + + configs = [ + "${aafwk_path}/frameworks/kits/ability/native:ability_config", + "${aafwk_path}/services/appmgr:appmgr_config", + ] + + cflags = [] + if (target_cpu == "arm") { + cflags += [ "-DBINDER_IPC_32BIT" ] + } + + deps = [ + "${aafwk_path}/frameworks/kits/appkit:appkit_native", + "${aafwk_path}/interfaces/innerkits/app_manager:app_manager", + "${aafwk_path}/services/appmgr:libams", + "${appexecfwk_path}/common:libappexecfwk_common", + "//third_party/googletest:gmock_main", + "//third_party/googletest:gtest_main", + ] + + external_deps = [ + "ability_base:want", + "bundle_framework:appexecfwk_base", + "bundle_framework:appexecfwk_core", + "eventhandler:libeventhandler", + "hiviewdfx_hilog_native:libhilog", + "ipc:ipc_core", + "safwk:system_ability_fwk", + "samgr_standard:samgr_proxy", + "utils_base:utils", + ] +} + +group("unittest") { + testonly = true + + deps = [ ":AmsMgrKillProcessTest" ] +} diff --git a/services/appmgr/test/unittest/ams_mgr_kill_process_test/ams_mgr_kill_process_test.cpp b/services/appmgr/test/unittest/ams_mgr_kill_process_test/ams_mgr_kill_process_test.cpp new file mode 100644 index 00000000000..e85f21b5b07 --- /dev/null +++ b/services/appmgr/test/unittest/ams_mgr_kill_process_test/ams_mgr_kill_process_test.cpp @@ -0,0 +1,121 @@ +/* + * Copyright (c) 2021 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include + +#define private public +#include "ams_mgr_scheduler.h" +#undef private +#include "app_log_wrapper.h" +#include "app_mgr_interface.h" +#include "mock_bundle_manager.h" +#include "system_ability_definition.h" +#include "sys_mgr_client.h" + +using namespace testing::ext; +using namespace OHOS; +using namespace OHOS::AppExecFwk; + +namespace { +const std::string STRING_BUNDLE_NAME = "com.example.bundle"; + +constexpr int ACCOUNT_ID = 100; +} // namespace + +class AmsMgrKillProcessTest : public testing::Test { +public: + static void SetUpTestCase(void); + static void TearDownTestCase(void); + void SetUp(void) override; + void TearDown(void) override; + + std::shared_ptr GetAppMgrServiceInner(void); + sptr GetAppMgrProxy(void); +}; + +void AmsMgrKillProcessTest::SetUpTestCase(void) +{} + +void AmsMgrKillProcessTest::TearDownTestCase(void) +{} + +void AmsMgrKillProcessTest::SetUp(void) +{} + +void AmsMgrKillProcessTest::TearDown(void) +{} + +std::shared_ptr AmsMgrKillProcessTest::GetAppMgrServiceInner(void) +{ + auto appMgrServiceInner = std::make_shared(); + EXPECT_NE(appMgrServiceInner, nullptr); + + sptr bundleMgr = new BundleMgrService(); + appMgrServiceInner->remoteClientManager_->SetBundleManager(bundleMgr); + + return appMgrServiceInner; +} + +sptr AmsMgrKillProcessTest::GetAppMgrProxy(void) +{ + auto instance = DelayedSingleton::GetInstance(); + EXPECT_NE(instance, nullptr); + + auto object = instance->GetSystemAbility(APP_MGR_SERVICE_ID); + EXPECT_NE(object, nullptr); + + auto proxy = iface_cast(object); + EXPECT_NE(proxy, nullptr); + + return proxy; +} + +/* + * Feature: AppMgrServiceInner + * Function: KillApplicationByUserId + * SubFunction: NA + * FunctionPoints:Kill process + * EnvConditions: NA + * CaseDescription: creat AppMgrServiceInner object, call function. + */ +HWTEST_F(AmsMgrKillProcessTest, KillProcess_0100, TestSize.Level0) +{ + APP_LOGI("AmsMgrKillProcessTest_KillProcess_0100"); + + auto appMgrServiceInner = GetAppMgrServiceInner(); + EXPECT_NE(appMgrServiceInner, nullptr); + + ErrCode result = appMgrServiceInner->KillApplicationByUserId(STRING_BUNDLE_NAME, ACCOUNT_ID); + EXPECT_EQ(result, ERR_OK); +} + +/* + * Feature: AppMgrProxy + * Function: KillApplicationByUserId + * SubFunction: NA + * FunctionPoints:Kill process + * EnvConditions: NA + * CaseDescription: creat AppMgrProxy object, call function. + */ +HWTEST_F(AmsMgrKillProcessTest, KillProcess_0200, TestSize.Level0) +{ + APP_LOGI("AmsMgrKillProcessTest_KillProcess_0200"); + + auto proxy = GetAppMgrProxy(); + EXPECT_NE(proxy, nullptr); + + ErrCode result = proxy->GetAmsMgr()->KillProcessWithAccount(STRING_BUNDLE_NAME, ACCOUNT_ID); + EXPECT_EQ(result, ERR_OK); +} diff --git a/services/appmgr/test/unittest/ams_service_load_ability_process_test/ams_service_load_ability_process_test.cpp b/services/appmgr/test/unittest/ams_service_load_ability_process_test/ams_service_load_ability_process_test.cpp index 1790a7297bc..e5a0e354bc0 100644 --- a/services/appmgr/test/unittest/ams_service_load_ability_process_test/ams_service_load_ability_process_test.cpp +++ b/services/appmgr/test/unittest/ams_service_load_ability_process_test/ams_service_load_ability_process_test.cpp @@ -216,26 +216,18 @@ HWTEST_F(AmsServiceLoadAbilityProcessTest, LoadAbility_002, TestSize.Level1) appInfo2->bundleName = "com.ohos.test.special"; const pid_t PID2 = 2234; - APP_LOGI("xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"); StartLoadAbility(token2, token, abilityInfo2, appInfo2, PID2); - APP_LOGI("00000000000000000000000000000000000000"); const uint32_t EXPECT_MAP_SIZE = 2; EXPECT_EQ(recordMap.size(), EXPECT_MAP_SIZE); - APP_LOGI("11111111111111111111111111111111111111111"); auto record2 = service_->appRunningManager_->CheckAppRunningRecordIsExist( appInfo2->name, "com.ohos.test.special", appInfo2->uid, bundleInfo); - APP_LOGI("22222222222222222222222222222222222222"); EXPECT_NE(record2, nullptr); CHECK_POINTER_IS_NULLPTR(record2); - APP_LOGI("3333333333333333333333333333333333333"); EXPECT_EQ(record2->GetState(), ApplicationState::APP_STATE_CREATE); - APP_LOGI("444444444444444444444444444444444444444444444"); auto abilityRecord2 = record2->GetAbilityRunningRecordByToken(token2); - APP_LOGI("5555555555555555555555555555555555555"); EXPECT_NE(abilityRecord2, nullptr); CHECK_POINTER_IS_NULLPTR(abilityRecord2); - APP_LOGI("666666666666666666666666666666666"); EXPECT_EQ(abilityRecord2->GetState(), AbilityState::ABILITY_STATE_CREATE); APP_LOGI("AmsServiceLoadAbilityProcessTest LoadAbility_002 end"); } diff --git a/services/appmgr/test/unittest/app_running_processes_info_test/BUILD.gn b/services/appmgr/test/unittest/app_running_processes_info_test/BUILD.gn new file mode 100644 index 00000000000..4cb46f2197e --- /dev/null +++ b/services/appmgr/test/unittest/app_running_processes_info_test/BUILD.gn @@ -0,0 +1,75 @@ +# Copyright (c) 2021 Huawei Device Co., Ltd. +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +import("//build/test.gni") +import("//foundation/aafwk/standard/aafwk.gni") + +module_output_path = "ability_runtime/appmgrservice" + +ohos_unittest("AppRunningProcessesInfoTest") { + module_out_path = module_output_path + + include_dirs = [ + "//foundation/aafwk/standard/interfaces/innerkits/base/include", + "//foundation/aafwk/standard/interfaces/innerkits/want/include", + "//foundation/appexecfwk/standard/interfaces/innerkits/appexecfwk_core/include", + "//foundation/distributedschedule/dmsfwk/services/dtbschedmgr/include", + "//foundation/distributedschedule/samgr/interfaces/innerkits/samgr_proxy/include", + "//foundation/distributedschedule/samgr/adapter/interfaces/innerkits/include/", + "//foundation/aafwk/standard/interfaces/innerkits/want/include/ohos/aafwk/content", + "//base/account/os_account/frameworks/common/database/include", + "//base/account/os_account/frameworks/common/account_error/include", + ] + + sources = [ + "${services_path}/appmgr/src/ability_running_record.cpp", + "${services_path}/appmgr/src/app_mgr_service_inner.cpp", + "${services_path}/appmgr/src/app_running_record.cpp", + "${services_path}/appmgr/src/app_spawn_client.cpp", + "${services_path}/appmgr/src/app_spawn_msg_wrapper.cpp", + "${services_path}/appmgr/src/app_spawn_socket.cpp", + "${services_path}/appmgr/test/mock/src/mock_bundle_manager.cpp", + ] + + sources += [ "app_running_processes_info_test.cpp" ] + + configs = [ "${appexecfwk_path}/libs/libeventhandler:libeventhandler_config" ] + cflags = [] + if (target_cpu == "arm") { + cflags += [ "-DBINDER_IPC_32BIT" ] + } + deps = [ + "${aafwk_path}/interfaces/innerkits/ability_manager:ability_manager", + "${aafwk_path}/interfaces/innerkits/app_manager:app_manager", + "${appexecfwk_path}/libs/libeventhandler:libeventhandler_target", + "${innerkits_path}/uri_permission:uri_permission_mgr", + "${services_path}/appmgr/test:appmgr_test_source", + "//base/account/os_account/frameworks/osaccount/native:os_account_innerkits", + "//foundation/aafwk/standard/services/appmgr:libams", + "//foundation/appexecfwk/standard/interfaces/innerkits/appexecfwk_core:appexecfwk_core", + ] + + external_deps = [ + "appspawn:appspawn_socket_client", + "bytrace_standard:bytrace_core", + "hisysevent_native:libhisysevent", + "hiviewdfx_hilog_native:libhilog", + "ipc:ipc_core", + ] +} + +group("unittest") { + testonly = true + + deps = [ ":AppRunningProcessesInfoTest" ] +} diff --git a/services/appmgr/test/unittest/app_running_processes_info_test/app_running_processes_info_test.cpp b/services/appmgr/test/unittest/app_running_processes_info_test/app_running_processes_info_test.cpp new file mode 100644 index 00000000000..0785dcac996 --- /dev/null +++ b/services/appmgr/test/unittest/app_running_processes_info_test/app_running_processes_info_test.cpp @@ -0,0 +1,365 @@ +/* + * Copyright (c) 2021 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#define private public +#include "app_mgr_service_inner.h" +#undef private + +#include +#include +#include "iremote_object.h" +#include "refbase.h" +#include "application_info.h" +#include "app_log_wrapper.h" +#include "app_record_id.h" +#include "app_scheduler_host.h" +#include "ability_info.h" +#include "ability_running_record.h" +#include "mock_app_scheduler.h" +#include "mock_ability_token.h" +#include "mock_app_spawn_client.h" +#include "mock_app_mgr_service_inner.h" +#include "mock_iapp_state_callback.h" +#include "mock_bundle_manager.h" +#include "mock_application.h" + +using namespace testing::ext; +using OHOS::iface_cast; +using OHOS::IRemoteObject; +using OHOS::sptr; +using testing::_; +using testing::Invoke; +using testing::InvokeWithoutArgs; +using testing::Return; +using testing::SetArgReferee; + +namespace OHOS { +namespace AppExecFwk { +class AppRunningProcessesInfoTest : public testing::Test { +public: + static void SetUpTestCase(); + static void TearDownTestCase(); + void SetUp(); + void TearDown(); + +protected: + static const std::string GetTestProcessName() + { + return "com.ohos.test.helloworld"; + } + static const std::string GetTestAppName() + { + return "com.ohos.test.helloworld"; + } + static const std::string GetTestAbilityName() + { + return "test_ability_name"; + } + static int GetTestUid() + { + // a valid inner uid value which is not border value. + const static int VALID_UID_VALUE = 1010; + return VALID_UID_VALUE; + } + + std::shared_ptr GetTestAppRunningRecord(); + sptr GetMockedAppSchedulerClient() const; + std::shared_ptr StartLoadAbility(const sptr &token, + const std::shared_ptr &abilityInfo, const std::shared_ptr &appInfo, + const pid_t newPid) const; + sptr GetMockToken() const + { + return mock_token_; + } + +protected: + std::shared_ptr testAbilityRecord_; + sptr client_; + sptr mockAppSchedulerClient_; + std::shared_ptr testAppRecord_; + std::unique_ptr service_; + sptr mock_token_; + sptr mockBundleMgr; +}; + +void AppRunningProcessesInfoTest::SetUpTestCase() +{} + +void AppRunningProcessesInfoTest::TearDownTestCase() +{} + +void AppRunningProcessesInfoTest::SetUp() +{ + mockAppSchedulerClient_ = new (std::nothrow) MockAppScheduler(); + service_.reset(new (std::nothrow) AppMgrServiceInner()); + mock_token_ = new (std::nothrow) MockAbilityToken(); + client_ = iface_cast(mockAppSchedulerClient_.GetRefPtr()); + mockBundleMgr = new (std::nothrow) BundleMgrService(); + service_->SetBundleManager(mockBundleMgr); +} + +void AppRunningProcessesInfoTest::TearDown() +{ + testAbilityRecord_.reset(); + testAppRecord_.reset(); +} + +sptr AppRunningProcessesInfoTest::GetMockedAppSchedulerClient() const +{ + if (client_) { + return client_; + } + return nullptr; +} + +std::shared_ptr AppRunningProcessesInfoTest::GetTestAppRunningRecord() +{ + if (!testAppRecord_) { + auto appInfo = std::make_shared(); + appInfo->name = GetTestAppName(); + testAppRecord_ = std::make_shared(appInfo, AppRecordId::Create(), GetTestProcessName()); + testAppRecord_->SetApplicationClient(GetMockedAppSchedulerClient()); + auto abilityInfo = std::make_shared(); + abilityInfo->name = GetTestAbilityName(); + HapModuleInfo hapModuleInfo; + hapModuleInfo.moduleName = "module789"; + testAppRecord_->AddModule(appInfo, abilityInfo, GetMockToken(), hapModuleInfo, nullptr); + } + return testAppRecord_; +} + +std::shared_ptr AppRunningProcessesInfoTest::StartLoadAbility(const sptr &token, + const std::shared_ptr &abilityInfo, const std::shared_ptr &appInfo, + const pid_t newPid) const +{ + std::shared_ptr mockClientPtr = std::make_shared(); + service_->SetAppSpawnClient(mockClientPtr); + EXPECT_CALL(*mockClientPtr, StartProcess(_, _)).Times(1).WillOnce(DoAll(SetArgReferee<1>(newPid), Return(ERR_OK))); + + service_->LoadAbility(token, nullptr, abilityInfo, appInfo, nullptr); + + BundleInfo bundleInfo; + bundleInfo.appId = "com.ohos.test.helloworld_code123"; + + auto record = service_->appRunningManager_->CheckAppRunningRecordIsExist( + appInfo->name, GetTestProcessName(), appInfo->uid, bundleInfo); + + EXPECT_TRUE(record); + auto clent = GetMockedAppSchedulerClient(); + record->SetApplicationClient(clent); + EXPECT_EQ(record->GetPriorityObject()->GetPid(), newPid); + EXPECT_NE(record->GetApplicationClient(), nullptr); + return record; +} + +/* + * Feature: AppMgrServiceInner + * Function: GetRunningProcessInfoByToken + * SubFunction: NA + * FunctionPoints: get running process info by token. + * EnvConditions: NA + * CaseDescription: creat apprunningrecord, set record state, call query function. + */ +HWTEST_F(AppRunningProcessesInfoTest, UpdateAppRunningRecord_001, TestSize.Level1) +{ + auto abilityInfo = std::make_shared(); + abilityInfo->name = GetTestAbilityName(); + auto appInfo = std::make_shared(); + appInfo->name = GetTestAppName(); + BundleInfo bundleInfo; + bundleInfo.appId = "com.ohos.test.helloworld_code123"; + bundleInfo.jointUserId = "joint456"; + HapModuleInfo hapModuleInfo; + hapModuleInfo.moduleName = "module789"; + EXPECT_TRUE(service_ != nullptr); + auto record = service_->CreateAppRunningRecord( + GetMockToken(), nullptr, appInfo, abilityInfo, GetTestProcessName(), bundleInfo, hapModuleInfo, nullptr); + EXPECT_TRUE(record != nullptr); + record->SetState(ApplicationState::APP_STATE_FOREGROUND); + record->SetApplicationClient(GetMockedAppSchedulerClient()); + RunningProcessInfo info; + service_->GetRunningProcessInfoByToken(GetMockToken(), info); + EXPECT_TRUE(info.processName_ == GetTestProcessName()); +} + +/* + * Feature: AppMgrServiceInner + * Function: GetAllRunningProcesses + * SubFunction: NA + * FunctionPoints: get running process info by token. + * EnvConditions: NA + * CaseDescription: creat apprunningrecord, set record state, call query function. + */ +HWTEST_F(AppRunningProcessesInfoTest, UpdateAppRunningRecord_002, TestSize.Level1) +{ + auto abilityInfo = std::make_shared(); + int uid = 0; + abilityInfo->name = GetTestAbilityName(); + abilityInfo->applicationInfo.uid = uid; + auto appInfo = std::make_shared(); + appInfo->name = GetTestAppName(); + appInfo->uid = uid; + BundleInfo bundleInfo; + HapModuleInfo hapModuleInfo; + EXPECT_TRUE(service_->GetBundleAndHapInfo(*abilityInfo, appInfo, bundleInfo, hapModuleInfo)); + EXPECT_TRUE(service_ != nullptr); + auto record = service_->CreateAppRunningRecord( + GetMockToken(), nullptr, appInfo, abilityInfo, GetTestProcessName(), bundleInfo, hapModuleInfo, nullptr); + EXPECT_TRUE(record != nullptr); + + record->SetUid(uid); + EXPECT_TRUE(record != nullptr) << ",create apprunningrecord fail!"; + + sptr mockApplication(new MockApplication()); + sptr client = iface_cast(mockApplication); + record->SetApplicationClient(client); + EXPECT_CALL(*mockApplication, ScheduleLaunchApplication(_, _)) + .Times(1) + .WillOnce(Invoke(mockApplication.GetRefPtr(), &MockApplication::LaunchApplication)); + Configuration config; + record->LaunchApplication(config); + mockApplication->Wait(); + + EXPECT_CALL(*mockApplication, ScheduleForegroundApplication()) + .Times(1) + .WillOnce(InvokeWithoutArgs(mockApplication.GetRefPtr(), &MockApplication::Post)); + // application enter in foreground and check the result + record->ScheduleForegroundRunning(); + mockApplication->Wait(); + + // update application state and check the state + record->SetState(ApplicationState::APP_STATE_FOREGROUND); + auto newRecord = service_->appRunningManager_->CheckAppRunningRecordIsExist( + appInfo->name, GetTestProcessName(), appInfo->uid, bundleInfo); + EXPECT_TRUE(newRecord); + newRecord->SetUid(uid); + auto stateFromRec = newRecord->GetState(); + EXPECT_EQ(stateFromRec, ApplicationState::APP_STATE_FOREGROUND); + + std::vector info; + size_t infoCount {0}; + auto res = service_->GetAllRunningProcesses(info); + EXPECT_TRUE (res == ERR_OK); + EXPECT_TRUE(info.size() == infoCount); +} + +/* + * Feature: AppMgrServiceInner + * Function: GetAllRunningProcesses + * SubFunction: NA + * FunctionPoints: get running process info by token. + * EnvConditions: NA + * CaseDescription: creat two apprunningrecords, set record state, call query function. + */ +HWTEST_F(AppRunningProcessesInfoTest, UpdateAppRunningRecord_003, TestSize.Level1) +{ + auto abilityInfo = std::make_shared(); + abilityInfo->name = GetTestAbilityName(); + auto appInfo = std::make_shared(); + appInfo->name = GetTestAppName(); + int uid = 0; + BundleInfo bundleInfo; + bundleInfo.appId = "com.ohos.test.helloworld_code123"; + bundleInfo.jointUserId = "joint456"; + HapModuleInfo hapModuleInfo; + hapModuleInfo.moduleName = "module789"; + EXPECT_TRUE(service_ != nullptr); + auto record = service_->CreateAppRunningRecord( + GetMockToken(), nullptr, appInfo, abilityInfo, GetTestProcessName(), bundleInfo, hapModuleInfo, nullptr); + EXPECT_TRUE(record != nullptr); + + record->SetUid(uid); + EXPECT_TRUE(record != nullptr) << ",create apprunningrecord fail!"; + + sptr mockApplication(new MockApplication()); + sptr client = iface_cast(mockApplication); + record->SetApplicationClient(client); + EXPECT_CALL(*mockApplication, ScheduleLaunchApplication(_, _)) + .Times(1) + .WillOnce(Invoke(mockApplication.GetRefPtr(), &MockApplication::LaunchApplication)); + Configuration config; + record->LaunchApplication(config); + mockApplication->Wait(); + + EXPECT_CALL(*mockApplication, ScheduleForegroundApplication()) + .Times(1) + .WillOnce(InvokeWithoutArgs(mockApplication.GetRefPtr(), &MockApplication::Post)); + // application enter in foreground and check the result + record->ScheduleForegroundRunning(); + mockApplication->Wait(); + + // update application state and check the state + record->SetState(ApplicationState::APP_STATE_FOREGROUND); + auto newRecord = service_->appRunningManager_->CheckAppRunningRecordIsExist( + appInfo->name, GetTestProcessName(), appInfo->uid, bundleInfo); + EXPECT_TRUE(newRecord); + newRecord->SetUid(0); + auto stateFromRec = newRecord->GetState(); + EXPECT_EQ(stateFromRec, ApplicationState::APP_STATE_FOREGROUND); + + auto abilityInfo2 = std::make_shared(); + abilityInfo2->name = GetTestAbilityName() + "2"; + abilityInfo2->applicationInfo.uid = uid; + auto appInfo2 = std::make_shared(); + appInfo2->name = GetTestAppName() + "2"; + appInfo2->uid = uid; + BundleInfo bundleInfo2; + HapModuleInfo hapModuleInfo2; + EXPECT_TRUE(service_->GetBundleAndHapInfo(*abilityInfo2, appInfo2, bundleInfo2, hapModuleInfo2)); + EXPECT_TRUE(service_ != nullptr); + auto mock_token = new (std::nothrow) MockAbilityToken(); + auto record2 = service_->CreateAppRunningRecord( + mock_token, nullptr, appInfo2, abilityInfo2, GetTestProcessName() + "2", bundleInfo2, hapModuleInfo2, nullptr); + EXPECT_TRUE(record != nullptr); + record2->SetUid(uid); + + std::vector info; + size_t infoCount {0}; + auto res = service_->GetAllRunningProcesses(info); + EXPECT_TRUE (res == ERR_OK); + EXPECT_TRUE(info.size() == infoCount); +} + +/* + * Feature: AppMgrServiceInner + * Function: GetRunningProcessInfoByToken + * SubFunction: NA + * FunctionPoints: get running process info by token. + * EnvConditions: NA + * CaseDescription: creat apprunningrecords, set record state, call query function. + */ +HWTEST_F(AppRunningProcessesInfoTest, UpdateAppRunningRecord_004, TestSize.Level1) +{ + auto abilityInfo = std::make_shared(); + abilityInfo->name = GetTestAbilityName(); + auto appInfo = std::make_shared(); + appInfo->name = GetTestAppName(); + BundleInfo bundleInfo; + bundleInfo.appId = "com.ohos.test.helloworld_code123"; + bundleInfo.jointUserId = "joint456"; + HapModuleInfo hapModuleInfo; + hapModuleInfo.moduleName = "module789"; + EXPECT_TRUE(service_ != nullptr); + auto record = service_->CreateAppRunningRecord( + GetMockToken(), nullptr, appInfo, abilityInfo, GetTestProcessName(), bundleInfo, hapModuleInfo, nullptr); + EXPECT_TRUE(record != nullptr); + record->SetState(ApplicationState::APP_STATE_BACKGROUND); + record->SetApplicationClient(GetMockedAppSchedulerClient()); + RunningProcessInfo info; + service_->appRunningManager_->GetRunningProcessInfoByToken(GetMockToken(), info); + EXPECT_TRUE(info.processName_ == GetTestProcessName()); +} +} // namespace AppExecFwk +} // namespace OHOS \ No newline at end of file -- Gitee